LCOV - code coverage report
Current view: directory - db/sqlite3/src - sqlite3.c (source / functions) Found Hit Coverage
Test: app.info Lines: 35539 24081 67.8 %
Date: 2012-06-02 Functions: 1865 1442 77.3 %

       1                 : /******************************************************************************
       2                 : ** This file is an amalgamation of many separate C source files from SQLite
       3                 : ** version 3.7.10.  By combining all the individual C code files into this 
       4                 : ** single large file, the entire code can be compiled as a single translation
       5                 : ** unit.  This allows many compilers to do optimizations that would not be
       6                 : ** possible if the files were compiled separately.  Performance improvements
       7                 : ** of 5% or more are commonly seen when SQLite is compiled as a single
       8                 : ** translation unit.
       9                 : **
      10                 : ** This file is all you need to compile SQLite.  To use SQLite in other
      11                 : ** programs, you need this file and the "sqlite3.h" header file that defines
      12                 : ** the programming interface to the SQLite library.  (If you do not have 
      13                 : ** the "sqlite3.h" header file at hand, you will find a copy embedded within
      14                 : ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
      15                 : ** of the embedded sqlite3.h header file.) Additional code files may be needed
      16                 : ** if you want a wrapper to interface SQLite with your choice of programming
      17                 : ** language. The code for the "sqlite3" command-line shell is also in a
      18                 : ** separate file. This file contains only code for the core SQLite library.
      19                 : */
      20                 : #define SQLITE_CORE 1
      21                 : #define SQLITE_AMALGAMATION 1
      22                 : #ifndef SQLITE_PRIVATE
      23                 : # define SQLITE_PRIVATE static
      24                 : #endif
      25                 : #ifndef SQLITE_API
      26                 : # define SQLITE_API
      27                 : #endif
      28                 : /************** Begin file sqliteInt.h ***************************************/
      29                 : /*
      30                 : ** 2001 September 15
      31                 : **
      32                 : ** The author disclaims copyright to this source code.  In place of
      33                 : ** a legal notice, here is a blessing:
      34                 : **
      35                 : **    May you do good and not evil.
      36                 : **    May you find forgiveness for yourself and forgive others.
      37                 : **    May you share freely, never taking more than you give.
      38                 : **
      39                 : *************************************************************************
      40                 : ** Internal interface definitions for SQLite.
      41                 : **
      42                 : */
      43                 : #ifndef _SQLITEINT_H_
      44                 : #define _SQLITEINT_H_
      45                 : 
      46                 : /*
      47                 : ** These #defines should enable >2GB file support on POSIX if the
      48                 : ** underlying operating system supports it.  If the OS lacks
      49                 : ** large file support, or if the OS is windows, these should be no-ops.
      50                 : **
      51                 : ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
      52                 : ** system #includes.  Hence, this block of code must be the very first
      53                 : ** code in all source files.
      54                 : **
      55                 : ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
      56                 : ** on the compiler command line.  This is necessary if you are compiling
      57                 : ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
      58                 : ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
      59                 : ** without this option, LFS is enable.  But LFS does not exist in the kernel
      60                 : ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
      61                 : ** portability you should omit LFS.
      62                 : **
      63                 : ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
      64                 : */
      65                 : #ifndef SQLITE_DISABLE_LFS
      66                 : # define _LARGE_FILE       1
      67                 : # ifndef _FILE_OFFSET_BITS
      68                 : #   define _FILE_OFFSET_BITS 64
      69                 : # endif
      70                 : # define _LARGEFILE_SOURCE 1
      71                 : #endif
      72                 : 
      73                 : /*
      74                 : ** Include the configuration header output by 'configure' if we're using the
      75                 : ** autoconf-based build
      76                 : */
      77                 : #ifdef _HAVE_SQLITE_CONFIG_H
      78                 : #include "config.h"
      79                 : #endif
      80                 : 
      81                 : /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
      82                 : /************** Begin file sqliteLimit.h *************************************/
      83                 : /*
      84                 : ** 2007 May 7
      85                 : **
      86                 : ** The author disclaims copyright to this source code.  In place of
      87                 : ** a legal notice, here is a blessing:
      88                 : **
      89                 : **    May you do good and not evil.
      90                 : **    May you find forgiveness for yourself and forgive others.
      91                 : **    May you share freely, never taking more than you give.
      92                 : **
      93                 : *************************************************************************
      94                 : ** 
      95                 : ** This file defines various limits of what SQLite can process.
      96                 : */
      97                 : 
      98                 : /*
      99                 : ** The maximum length of a TEXT or BLOB in bytes.   This also
     100                 : ** limits the size of a row in a table or index.
     101                 : **
     102                 : ** The hard limit is the ability of a 32-bit signed integer
     103                 : ** to count the size: 2^31-1 or 2147483647.
     104                 : */
     105                 : #ifndef SQLITE_MAX_LENGTH
     106                 : # define SQLITE_MAX_LENGTH 1000000000
     107                 : #endif
     108                 : 
     109                 : /*
     110                 : ** This is the maximum number of
     111                 : **
     112                 : **    * Columns in a table
     113                 : **    * Columns in an index
     114                 : **    * Columns in a view
     115                 : **    * Terms in the SET clause of an UPDATE statement
     116                 : **    * Terms in the result set of a SELECT statement
     117                 : **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
     118                 : **    * Terms in the VALUES clause of an INSERT statement
     119                 : **
     120                 : ** The hard upper limit here is 32676.  Most database people will
     121                 : ** tell you that in a well-normalized database, you usually should
     122                 : ** not have more than a dozen or so columns in any table.  And if
     123                 : ** that is the case, there is no point in having more than a few
     124                 : ** dozen values in any of the other situations described above.
     125                 : */
     126                 : #ifndef SQLITE_MAX_COLUMN
     127                 : # define SQLITE_MAX_COLUMN 2000
     128                 : #endif
     129                 : 
     130                 : /*
     131                 : ** The maximum length of a single SQL statement in bytes.
     132                 : **
     133                 : ** It used to be the case that setting this value to zero would
     134                 : ** turn the limit off.  That is no longer true.  It is not possible
     135                 : ** to turn this limit off.
     136                 : */
     137                 : #ifndef SQLITE_MAX_SQL_LENGTH
     138                 : # define SQLITE_MAX_SQL_LENGTH 1000000000
     139                 : #endif
     140                 : 
     141                 : /*
     142                 : ** The maximum depth of an expression tree. This is limited to 
     143                 : ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might 
     144                 : ** want to place more severe limits on the complexity of an 
     145                 : ** expression.
     146                 : **
     147                 : ** A value of 0 used to mean that the limit was not enforced.
     148                 : ** But that is no longer true.  The limit is now strictly enforced
     149                 : ** at all times.
     150                 : */
     151                 : #ifndef SQLITE_MAX_EXPR_DEPTH
     152                 : # define SQLITE_MAX_EXPR_DEPTH 1000
     153                 : #endif
     154                 : 
     155                 : /*
     156                 : ** The maximum number of terms in a compound SELECT statement.
     157                 : ** The code generator for compound SELECT statements does one
     158                 : ** level of recursion for each term.  A stack overflow can result
     159                 : ** if the number of terms is too large.  In practice, most SQL
     160                 : ** never has more than 3 or 4 terms.  Use a value of 0 to disable
     161                 : ** any limit on the number of terms in a compount SELECT.
     162                 : */
     163                 : #ifndef SQLITE_MAX_COMPOUND_SELECT
     164                 : # define SQLITE_MAX_COMPOUND_SELECT 500
     165                 : #endif
     166                 : 
     167                 : /*
     168                 : ** The maximum number of opcodes in a VDBE program.
     169                 : ** Not currently enforced.
     170                 : */
     171                 : #ifndef SQLITE_MAX_VDBE_OP
     172                 : # define SQLITE_MAX_VDBE_OP 25000
     173                 : #endif
     174                 : 
     175                 : /*
     176                 : ** The maximum number of arguments to an SQL function.
     177                 : */
     178                 : #ifndef SQLITE_MAX_FUNCTION_ARG
     179                 : # define SQLITE_MAX_FUNCTION_ARG 127
     180                 : #endif
     181                 : 
     182                 : /*
     183                 : ** The maximum number of in-memory pages to use for the main database
     184                 : ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
     185                 : */
     186                 : #ifndef SQLITE_DEFAULT_CACHE_SIZE
     187                 : # define SQLITE_DEFAULT_CACHE_SIZE  2000
     188                 : #endif
     189                 : #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
     190                 : # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
     191                 : #endif
     192                 : 
     193                 : /*
     194                 : ** The default number of frames to accumulate in the log file before
     195                 : ** checkpointing the database in WAL mode.
     196                 : */
     197                 : #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
     198                 : # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
     199                 : #endif
     200                 : 
     201                 : /*
     202                 : ** The maximum number of attached databases.  This must be between 0
     203                 : ** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
     204                 : ** is used internally to track attached databases.
     205                 : */
     206                 : #ifndef SQLITE_MAX_ATTACHED
     207                 : # define SQLITE_MAX_ATTACHED 10
     208                 : #endif
     209                 : 
     210                 : 
     211                 : /*
     212                 : ** The maximum value of a ?nnn wildcard that the parser will accept.
     213                 : */
     214                 : #ifndef SQLITE_MAX_VARIABLE_NUMBER
     215                 : # define SQLITE_MAX_VARIABLE_NUMBER 999
     216                 : #endif
     217                 : 
     218                 : /* Maximum page size.  The upper bound on this value is 65536.  This a limit
     219                 : ** imposed by the use of 16-bit offsets within each page.
     220                 : **
     221                 : ** Earlier versions of SQLite allowed the user to change this value at
     222                 : ** compile time. This is no longer permitted, on the grounds that it creates
     223                 : ** a library that is technically incompatible with an SQLite library 
     224                 : ** compiled with a different limit. If a process operating on a database 
     225                 : ** with a page-size of 65536 bytes crashes, then an instance of SQLite 
     226                 : ** compiled with the default page-size limit will not be able to rollback 
     227                 : ** the aborted transaction. This could lead to database corruption.
     228                 : */
     229                 : #ifdef SQLITE_MAX_PAGE_SIZE
     230                 : # undef SQLITE_MAX_PAGE_SIZE
     231                 : #endif
     232                 : #define SQLITE_MAX_PAGE_SIZE 65536
     233                 : 
     234                 : 
     235                 : /*
     236                 : ** The default size of a database page.
     237                 : */
     238                 : #ifndef SQLITE_DEFAULT_PAGE_SIZE
     239                 : # define SQLITE_DEFAULT_PAGE_SIZE 1024
     240                 : #endif
     241                 : #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
     242                 : # undef SQLITE_DEFAULT_PAGE_SIZE
     243                 : # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
     244                 : #endif
     245                 : 
     246                 : /*
     247                 : ** Ordinarily, if no value is explicitly provided, SQLite creates databases
     248                 : ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
     249                 : ** device characteristics (sector-size and atomic write() support),
     250                 : ** SQLite may choose a larger value. This constant is the maximum value
     251                 : ** SQLite will choose on its own.
     252                 : */
     253                 : #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
     254                 : # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
     255                 : #endif
     256                 : #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
     257                 : # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
     258                 : # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
     259                 : #endif
     260                 : 
     261                 : 
     262                 : /*
     263                 : ** Maximum number of pages in one database file.
     264                 : **
     265                 : ** This is really just the default value for the max_page_count pragma.
     266                 : ** This value can be lowered (or raised) at run-time using that the
     267                 : ** max_page_count macro.
     268                 : */
     269                 : #ifndef SQLITE_MAX_PAGE_COUNT
     270                 : # define SQLITE_MAX_PAGE_COUNT 1073741823
     271                 : #endif
     272                 : 
     273                 : /*
     274                 : ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
     275                 : ** operator.
     276                 : */
     277                 : #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
     278                 : # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
     279                 : #endif
     280                 : 
     281                 : /*
     282                 : ** Maximum depth of recursion for triggers.
     283                 : **
     284                 : ** A value of 1 means that a trigger program will not be able to itself
     285                 : ** fire any triggers. A value of 0 means that no trigger programs at all 
     286                 : ** may be executed.
     287                 : */
     288                 : #ifndef SQLITE_MAX_TRIGGER_DEPTH
     289                 : # define SQLITE_MAX_TRIGGER_DEPTH 1000
     290                 : #endif
     291                 : 
     292                 : /************** End of sqliteLimit.h *****************************************/
     293                 : /************** Continuing where we left off in sqliteInt.h ******************/
     294                 : 
     295                 : /* Disable nuisance warnings on Borland compilers */
     296                 : #if defined(__BORLANDC__)
     297                 : #pragma warn -rch /* unreachable code */
     298                 : #pragma warn -ccc /* Condition is always true or false */
     299                 : #pragma warn -aus /* Assigned value is never used */
     300                 : #pragma warn -csu /* Comparing signed and unsigned */
     301                 : #pragma warn -spa /* Suspicious pointer arithmetic */
     302                 : #endif
     303                 : 
     304                 : /* Needed for various definitions... */
     305                 : #ifndef _GNU_SOURCE
     306                 : # define _GNU_SOURCE
     307                 : #endif
     308                 : 
     309                 : /*
     310                 : ** Include standard header files as necessary
     311                 : */
     312                 : #ifdef HAVE_STDINT_H
     313                 : #include <stdint.h>
     314                 : #endif
     315                 : #ifdef HAVE_INTTYPES_H
     316                 : #include <inttypes.h>
     317                 : #endif
     318                 : 
     319                 : /*
     320                 : ** The following macros are used to cast pointers to integers and
     321                 : ** integers to pointers.  The way you do this varies from one compiler
     322                 : ** to the next, so we have developed the following set of #if statements
     323                 : ** to generate appropriate macros for a wide range of compilers.
     324                 : **
     325                 : ** The correct "ANSI" way to do this is to use the intptr_t type. 
     326                 : ** Unfortunately, that typedef is not available on all compilers, or
     327                 : ** if it is available, it requires an #include of specific headers
     328                 : ** that vary from one machine to the next.
     329                 : **
     330                 : ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
     331                 : ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
     332                 : ** So we have to define the macros in different ways depending on the
     333                 : ** compiler.
     334                 : */
     335                 : #if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
     336                 : # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
     337                 : # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
     338                 : #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
     339                 : # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
     340                 : # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
     341                 : #elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
     342                 : # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
     343                 : # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
     344                 : #else                          /* Generates a warning - but it always works */
     345                 : # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
     346                 : # define SQLITE_PTR_TO_INT(X)  ((int)(X))
     347                 : #endif
     348                 : 
     349                 : /*
     350                 : ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
     351                 : ** 0 means mutexes are permanently disable and the library is never
     352                 : ** threadsafe.  1 means the library is serialized which is the highest
     353                 : ** level of threadsafety.  2 means the libary is multithreaded - multiple
     354                 : ** threads can use SQLite as long as no two threads try to use the same
     355                 : ** database connection at the same time.
     356                 : **
     357                 : ** Older versions of SQLite used an optional THREADSAFE macro.
     358                 : ** We support that for legacy.
     359                 : */
     360                 : #if !defined(SQLITE_THREADSAFE)
     361                 : #if defined(THREADSAFE)
     362                 : # define SQLITE_THREADSAFE THREADSAFE
     363                 : #else
     364                 : # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
     365                 : #endif
     366                 : #endif
     367                 : 
     368                 : /*
     369                 : ** Powersafe overwrite is on by default.  But can be turned off using
     370                 : ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
     371                 : */
     372                 : #ifndef SQLITE_POWERSAFE_OVERWRITE
     373                 : # define SQLITE_POWERSAFE_OVERWRITE 1
     374                 : #endif
     375                 : 
     376                 : /*
     377                 : ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
     378                 : ** It determines whether or not the features related to 
     379                 : ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
     380                 : ** be overridden at runtime using the sqlite3_config() API.
     381                 : */
     382                 : #if !defined(SQLITE_DEFAULT_MEMSTATUS)
     383                 : # define SQLITE_DEFAULT_MEMSTATUS 1
     384                 : #endif
     385                 : 
     386                 : /*
     387                 : ** Exactly one of the following macros must be defined in order to
     388                 : ** specify which memory allocation subsystem to use.
     389                 : **
     390                 : **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
     391                 : **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
     392                 : **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
     393                 : **
     394                 : ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
     395                 : ** assert() macro is enabled, each call into the Win32 native heap subsystem
     396                 : ** will cause HeapValidate to be called.  If heap validation should fail, an
     397                 : ** assertion will be triggered.
     398                 : **
     399                 : ** (Historical note:  There used to be several other options, but we've
     400                 : ** pared it down to just these three.)
     401                 : **
     402                 : ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
     403                 : ** the default.
     404                 : */
     405                 : #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
     406                 : # error "At most one of the following compile-time configuration options\
     407                 :  is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
     408                 : #endif
     409                 : #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
     410                 : # define SQLITE_SYSTEM_MALLOC 1
     411                 : #endif
     412                 : 
     413                 : /*
     414                 : ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
     415                 : ** sizes of memory allocations below this value where possible.
     416                 : */
     417                 : #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
     418                 : # define SQLITE_MALLOC_SOFT_LIMIT 1024
     419                 : #endif
     420                 : 
     421                 : /*
     422                 : ** We need to define _XOPEN_SOURCE as follows in order to enable
     423                 : ** recursive mutexes on most Unix systems.  But Mac OS X is different.
     424                 : ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
     425                 : ** so it is omitted there.  See ticket #2673.
     426                 : **
     427                 : ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
     428                 : ** implemented on some systems.  So we avoid defining it at all
     429                 : ** if it is already defined or if it is unneeded because we are
     430                 : ** not doing a threadsafe build.  Ticket #2681.
     431                 : **
     432                 : ** See also ticket #2741.
     433                 : */
     434                 : #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
     435                 : #  define _XOPEN_SOURCE 500  /* Needed to enable pthread recursive mutexes */
     436                 : #endif
     437                 : 
     438                 : /*
     439                 : ** The TCL headers are only needed when compiling the TCL bindings.
     440                 : */
     441                 : #if defined(SQLITE_TCL) || defined(TCLSH)
     442                 : # include <tcl.h>
     443                 : #endif
     444                 : 
     445                 : /*
     446                 : ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
     447                 : ** Setting NDEBUG makes the code smaller and run faster.  So the following
     448                 : ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
     449                 : ** option is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
     450                 : ** feature.
     451                 : */
     452                 : #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
     453                 : # define NDEBUG 1
     454                 : #endif
     455                 : 
     456                 : /*
     457                 : ** The testcase() macro is used to aid in coverage testing.  When 
     458                 : ** doing coverage testing, the condition inside the argument to
     459                 : ** testcase() must be evaluated both true and false in order to
     460                 : ** get full branch coverage.  The testcase() macro is inserted
     461                 : ** to help ensure adequate test coverage in places where simple
     462                 : ** condition/decision coverage is inadequate.  For example, testcase()
     463                 : ** can be used to make sure boundary values are tested.  For
     464                 : ** bitmask tests, testcase() can be used to make sure each bit
     465                 : ** is significant and used at least once.  On switch statements
     466                 : ** where multiple cases go to the same block of code, testcase()
     467                 : ** can insure that all cases are evaluated.
     468                 : **
     469                 : */
     470                 : #ifdef SQLITE_COVERAGE_TEST
     471                 : SQLITE_PRIVATE   void sqlite3Coverage(int);
     472                 : # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
     473                 : #else
     474                 : # define testcase(X)
     475                 : #endif
     476                 : 
     477                 : /*
     478                 : ** The TESTONLY macro is used to enclose variable declarations or
     479                 : ** other bits of code that are needed to support the arguments
     480                 : ** within testcase() and assert() macros.
     481                 : */
     482                 : #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
     483                 : # define TESTONLY(X)  X
     484                 : #else
     485                 : # define TESTONLY(X)
     486                 : #endif
     487                 : 
     488                 : /*
     489                 : ** Sometimes we need a small amount of code such as a variable initialization
     490                 : ** to setup for a later assert() statement.  We do not want this code to
     491                 : ** appear when assert() is disabled.  The following macro is therefore
     492                 : ** used to contain that setup code.  The "VVA" acronym stands for
     493                 : ** "Verification, Validation, and Accreditation".  In other words, the
     494                 : ** code within VVA_ONLY() will only run during verification processes.
     495                 : */
     496                 : #ifndef NDEBUG
     497                 : # define VVA_ONLY(X)  X
     498                 : #else
     499                 : # define VVA_ONLY(X)
     500                 : #endif
     501                 : 
     502                 : /*
     503                 : ** The ALWAYS and NEVER macros surround boolean expressions which 
     504                 : ** are intended to always be true or false, respectively.  Such
     505                 : ** expressions could be omitted from the code completely.  But they
     506                 : ** are included in a few cases in order to enhance the resilience
     507                 : ** of SQLite to unexpected behavior - to make the code "self-healing"
     508                 : ** or "ductile" rather than being "brittle" and crashing at the first
     509                 : ** hint of unplanned behavior.
     510                 : **
     511                 : ** In other words, ALWAYS and NEVER are added for defensive code.
     512                 : **
     513                 : ** When doing coverage testing ALWAYS and NEVER are hard-coded to
     514                 : ** be true and false so that the unreachable code then specify will
     515                 : ** not be counted as untested code.
     516                 : */
     517                 : #if defined(SQLITE_COVERAGE_TEST)
     518                 : # define ALWAYS(X)      (1)
     519                 : # define NEVER(X)       (0)
     520                 : #elif !defined(NDEBUG)
     521                 : # define ALWAYS(X)      ((X)?1:(assert(0),0))
     522                 : # define NEVER(X)       ((X)?(assert(0),1):0)
     523                 : #else
     524                 : # define ALWAYS(X)      (X)
     525                 : # define NEVER(X)       (X)
     526                 : #endif
     527                 : 
     528                 : /*
     529                 : ** Return true (non-zero) if the input is a integer that is too large
     530                 : ** to fit in 32-bits.  This macro is used inside of various testcase()
     531                 : ** macros to verify that we have tested SQLite for large-file support.
     532                 : */
     533                 : #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
     534                 : 
     535                 : /*
     536                 : ** The macro unlikely() is a hint that surrounds a boolean
     537                 : ** expression that is usually false.  Macro likely() surrounds
     538                 : ** a boolean expression that is usually true.  GCC is able to
     539                 : ** use these hints to generate better code, sometimes.
     540                 : */
     541                 : #if defined(__GNUC__) && 0
     542                 : # define likely(X)    __builtin_expect((X),1)
     543                 : # define unlikely(X)  __builtin_expect((X),0)
     544                 : #else
     545                 : # define likely(X)    !!(X)
     546                 : # define unlikely(X)  !!(X)
     547                 : #endif
     548                 : 
     549                 : /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
     550                 : /************** Begin file sqlite3.h *****************************************/
     551                 : /*
     552                 : ** 2001 September 15
     553                 : **
     554                 : ** The author disclaims copyright to this source code.  In place of
     555                 : ** a legal notice, here is a blessing:
     556                 : **
     557                 : **    May you do good and not evil.
     558                 : **    May you find forgiveness for yourself and forgive others.
     559                 : **    May you share freely, never taking more than you give.
     560                 : **
     561                 : *************************************************************************
     562                 : ** This header file defines the interface that the SQLite library
     563                 : ** presents to client programs.  If a C-function, structure, datatype,
     564                 : ** or constant definition does not appear in this file, then it is
     565                 : ** not a published API of SQLite, is subject to change without
     566                 : ** notice, and should not be referenced by programs that use SQLite.
     567                 : **
     568                 : ** Some of the definitions that are in this file are marked as
     569                 : ** "experimental".  Experimental interfaces are normally new
     570                 : ** features recently added to SQLite.  We do not anticipate changes
     571                 : ** to experimental interfaces but reserve the right to make minor changes
     572                 : ** if experience from use "in the wild" suggest such changes are prudent.
     573                 : **
     574                 : ** The official C-language API documentation for SQLite is derived
     575                 : ** from comments in this file.  This file is the authoritative source
     576                 : ** on how SQLite interfaces are suppose to operate.
     577                 : **
     578                 : ** The name of this file under configuration management is "sqlite.h.in".
     579                 : ** The makefile makes some minor changes to this file (such as inserting
     580                 : ** the version number) and changes its name to "sqlite3.h" as
     581                 : ** part of the build process.
     582                 : */
     583                 : #ifndef _SQLITE3_H_
     584                 : #define _SQLITE3_H_
     585                 : #include <stdarg.h>     /* Needed for the definition of va_list */
     586                 : 
     587                 : /*
     588                 : ** Make sure we can call this stuff from C++.
     589                 : */
     590                 : #if 0
     591                 : extern "C" {
     592                 : #endif
     593                 : 
     594                 : 
     595                 : /*
     596                 : ** Add the ability to override 'extern'
     597                 : */
     598                 : #ifndef SQLITE_EXTERN
     599                 : # define SQLITE_EXTERN extern
     600                 : #endif
     601                 : 
     602                 : #ifndef SQLITE_API
     603                 : # define SQLITE_API
     604                 : #endif
     605                 : 
     606                 : 
     607                 : /*
     608                 : ** These no-op macros are used in front of interfaces to mark those
     609                 : ** interfaces as either deprecated or experimental.  New applications
     610                 : ** should not use deprecated interfaces - they are support for backwards
     611                 : ** compatibility only.  Application writers should be aware that
     612                 : ** experimental interfaces are subject to change in point releases.
     613                 : **
     614                 : ** These macros used to resolve to various kinds of compiler magic that
     615                 : ** would generate warning messages when they were used.  But that
     616                 : ** compiler magic ended up generating such a flurry of bug reports
     617                 : ** that we have taken it all out and gone back to using simple
     618                 : ** noop macros.
     619                 : */
     620                 : #define SQLITE_DEPRECATED
     621                 : #define SQLITE_EXPERIMENTAL
     622                 : 
     623                 : /*
     624                 : ** Ensure these symbols were not defined by some previous header file.
     625                 : */
     626                 : #ifdef SQLITE_VERSION
     627                 : # undef SQLITE_VERSION
     628                 : #endif
     629                 : #ifdef SQLITE_VERSION_NUMBER
     630                 : # undef SQLITE_VERSION_NUMBER
     631                 : #endif
     632                 : 
     633                 : /*
     634                 : ** CAPI3REF: Compile-Time Library Version Numbers
     635                 : **
     636                 : ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
     637                 : ** evaluates to a string literal that is the SQLite version in the
     638                 : ** format "X.Y.Z" where X is the major version number (always 3 for
     639                 : ** SQLite3) and Y is the minor version number and Z is the release number.)^
     640                 : ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
     641                 : ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
     642                 : ** numbers used in [SQLITE_VERSION].)^
     643                 : ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
     644                 : ** be larger than the release from which it is derived.  Either Y will
     645                 : ** be held constant and Z will be incremented or else Y will be incremented
     646                 : ** and Z will be reset to zero.
     647                 : **
     648                 : ** Since version 3.6.18, SQLite source code has been stored in the
     649                 : ** <a href="http://www.fossil-scm.org/">Fossil configuration management
     650                 : ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
     651                 : ** a string which identifies a particular check-in of SQLite
     652                 : ** within its configuration management system.  ^The SQLITE_SOURCE_ID
     653                 : ** string contains the date and time of the check-in (UTC) and an SHA1
     654                 : ** hash of the entire source tree.
     655                 : **
     656                 : ** See also: [sqlite3_libversion()],
     657                 : ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
     658                 : ** [sqlite_version()] and [sqlite_source_id()].
     659                 : */
     660                 : #define SQLITE_VERSION        "3.7.10"
     661                 : #define SQLITE_VERSION_NUMBER 3007010
     662                 : #define SQLITE_SOURCE_ID      "2012-01-16 13:28:40 ebd01a8deffb5024a5d7494eef800d2366d97204"
     663                 : 
     664                 : /*
     665                 : ** CAPI3REF: Run-Time Library Version Numbers
     666                 : ** KEYWORDS: sqlite3_version, sqlite3_sourceid
     667                 : **
     668                 : ** These interfaces provide the same information as the [SQLITE_VERSION],
     669                 : ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
     670                 : ** but are associated with the library instead of the header file.  ^(Cautious
     671                 : ** programmers might include assert() statements in their application to
     672                 : ** verify that values returned by these interfaces match the macros in
     673                 : ** the header, and thus insure that the application is
     674                 : ** compiled with matching library and header files.
     675                 : **
     676                 : ** <blockquote><pre>
     677                 : ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
     678                 : ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
     679                 : ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
     680                 : ** </pre></blockquote>)^
     681                 : **
     682                 : ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
     683                 : ** macro.  ^The sqlite3_libversion() function returns a pointer to the
     684                 : ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
     685                 : ** function is provided for use in DLLs since DLL users usually do not have
     686                 : ** direct access to string constants within the DLL.  ^The
     687                 : ** sqlite3_libversion_number() function returns an integer equal to
     688                 : ** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns 
     689                 : ** a pointer to a string constant whose value is the same as the 
     690                 : ** [SQLITE_SOURCE_ID] C preprocessor macro.
     691                 : **
     692                 : ** See also: [sqlite_version()] and [sqlite_source_id()].
     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: Run-Time Library Compilation Options Diagnostics
     701                 : **
     702                 : ** ^The sqlite3_compileoption_used() function returns 0 or 1 
     703                 : ** indicating whether the specified option was defined at 
     704                 : ** compile time.  ^The SQLITE_ prefix may be omitted from the 
     705                 : ** option name passed to sqlite3_compileoption_used().  
     706                 : **
     707                 : ** ^The sqlite3_compileoption_get() function allows iterating
     708                 : ** over the list of options that were defined at compile time by
     709                 : ** returning the N-th compile time option string.  ^If N is out of range,
     710                 : ** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_ 
     711                 : ** prefix is omitted from any strings returned by 
     712                 : ** sqlite3_compileoption_get().
     713                 : **
     714                 : ** ^Support for the diagnostic functions sqlite3_compileoption_used()
     715                 : ** and sqlite3_compileoption_get() may be omitted by specifying the 
     716                 : ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
     717                 : **
     718                 : ** See also: SQL functions [sqlite_compileoption_used()] and
     719                 : ** [sqlite_compileoption_get()] and the [compile_options pragma].
     720                 : */
     721                 : #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
     722                 : SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
     723                 : SQLITE_API const char *sqlite3_compileoption_get(int N);
     724                 : #endif
     725                 : 
     726                 : /*
     727                 : ** CAPI3REF: Test To See If The Library Is Threadsafe
     728                 : **
     729                 : ** ^The sqlite3_threadsafe() function returns zero if and only if
     730                 : ** SQLite was compiled with mutexing code omitted due to the
     731                 : ** [SQLITE_THREADSAFE] compile-time option being set to 0.
     732                 : **
     733                 : ** SQLite can be compiled with or without mutexes.  When
     734                 : ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
     735                 : ** are enabled and SQLite is threadsafe.  When the
     736                 : ** [SQLITE_THREADSAFE] macro is 0, 
     737                 : ** the mutexes are omitted.  Without the mutexes, it is not safe
     738                 : ** to use SQLite concurrently from more than one thread.
     739                 : **
     740                 : ** Enabling mutexes incurs a measurable performance penalty.
     741                 : ** So if speed is of utmost importance, it makes sense to disable
     742                 : ** the mutexes.  But for maximum safety, mutexes should be enabled.
     743                 : ** ^The default behavior is for mutexes to be enabled.
     744                 : **
     745                 : ** This interface can be used by an application to make sure that the
     746                 : ** version of SQLite that it is linking against was compiled with
     747                 : ** the desired setting of the [SQLITE_THREADSAFE] macro.
     748                 : **
     749                 : ** This interface only reports on the compile-time mutex setting
     750                 : ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
     751                 : ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
     752                 : ** can be fully or partially disabled using a call to [sqlite3_config()]
     753                 : ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
     754                 : ** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
     755                 : ** sqlite3_threadsafe() function shows only the compile-time setting of
     756                 : ** thread safety, not any run-time changes to that setting made by
     757                 : ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
     758                 : ** is unchanged by calls to sqlite3_config().)^
     759                 : **
     760                 : ** See the [threading mode] documentation for additional information.
     761                 : */
     762                 : SQLITE_API int sqlite3_threadsafe(void);
     763                 : 
     764                 : /*
     765                 : ** CAPI3REF: Database Connection Handle
     766                 : ** KEYWORDS: {database connection} {database connections}
     767                 : **
     768                 : ** Each open SQLite database is represented by a pointer to an instance of
     769                 : ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
     770                 : ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
     771                 : ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
     772                 : ** is its destructor.  There are many other interfaces (such as
     773                 : ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
     774                 : ** [sqlite3_busy_timeout()] to name but three) that are methods on an
     775                 : ** sqlite3 object.
     776                 : */
     777                 : typedef struct sqlite3 sqlite3;
     778                 : 
     779                 : /*
     780                 : ** CAPI3REF: 64-Bit Integer Types
     781                 : ** KEYWORDS: sqlite_int64 sqlite_uint64
     782                 : **
     783                 : ** Because there is no cross-platform way to specify 64-bit integer types
     784                 : ** SQLite includes typedefs for 64-bit signed and unsigned integers.
     785                 : **
     786                 : ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
     787                 : ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
     788                 : ** compatibility only.
     789                 : **
     790                 : ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
     791                 : ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
     792                 : ** sqlite3_uint64 and sqlite_uint64 types can store integer values 
     793                 : ** between 0 and +18446744073709551615 inclusive.
     794                 : */
     795                 : #ifdef SQLITE_INT64_TYPE
     796                 :   typedef SQLITE_INT64_TYPE sqlite_int64;
     797                 :   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
     798                 : #elif defined(_MSC_VER) || defined(__BORLANDC__)
     799                 :   typedef __int64 sqlite_int64;
     800                 :   typedef unsigned __int64 sqlite_uint64;
     801                 : #else
     802                 :   typedef long long int sqlite_int64;
     803                 :   typedef unsigned long long int sqlite_uint64;
     804                 : #endif
     805                 : typedef sqlite_int64 sqlite3_int64;
     806                 : typedef sqlite_uint64 sqlite3_uint64;
     807                 : 
     808                 : /*
     809                 : ** If compiling for a processor that lacks floating point support,
     810                 : ** substitute integer for floating-point.
     811                 : */
     812                 : #ifdef SQLITE_OMIT_FLOATING_POINT
     813                 : # define double sqlite3_int64
     814                 : #endif
     815                 : 
     816                 : /*
     817                 : ** CAPI3REF: Closing A Database Connection
     818                 : **
     819                 : ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
     820                 : ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
     821                 : ** successfully destroyed and all associated resources are deallocated.
     822                 : **
     823                 : ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
     824                 : ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
     825                 : ** the [sqlite3] object prior to attempting to close the object.  ^If
     826                 : ** sqlite3_close() is called on a [database connection] that still has
     827                 : ** outstanding [prepared statements] or [BLOB handles], then it returns
     828                 : ** SQLITE_BUSY.
     829                 : **
     830                 : ** ^If [sqlite3_close()] is invoked while a transaction is open,
     831                 : ** the transaction is automatically rolled back.
     832                 : **
     833                 : ** The C parameter to [sqlite3_close(C)] must be either a NULL
     834                 : ** pointer or an [sqlite3] object pointer obtained
     835                 : ** from [sqlite3_open()], [sqlite3_open16()], or
     836                 : ** [sqlite3_open_v2()], and not previously closed.
     837                 : ** ^Calling sqlite3_close() with a NULL pointer argument is a 
     838                 : ** harmless no-op.
     839                 : */
     840                 : SQLITE_API int sqlite3_close(sqlite3 *);
     841                 : 
     842                 : /*
     843                 : ** The type for a callback function.
     844                 : ** This is legacy and deprecated.  It is included for historical
     845                 : ** compatibility and is not documented.
     846                 : */
     847                 : typedef int (*sqlite3_callback)(void*,int,char**, char**);
     848                 : 
     849                 : /*
     850                 : ** CAPI3REF: One-Step Query Execution Interface
     851                 : **
     852                 : ** The sqlite3_exec() interface is a convenience wrapper around
     853                 : ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
     854                 : ** that allows an application to run multiple statements of SQL
     855                 : ** without having to use a lot of C code. 
     856                 : **
     857                 : ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
     858                 : ** semicolon-separate SQL statements passed into its 2nd argument,
     859                 : ** in the context of the [database connection] passed in as its 1st
     860                 : ** argument.  ^If the callback function of the 3rd argument to
     861                 : ** sqlite3_exec() is not NULL, then it is invoked for each result row
     862                 : ** coming out of the evaluated SQL statements.  ^The 4th argument to
     863                 : ** sqlite3_exec() is relayed through to the 1st argument of each
     864                 : ** callback invocation.  ^If the callback pointer to sqlite3_exec()
     865                 : ** is NULL, then no callback is ever invoked and result rows are
     866                 : ** ignored.
     867                 : **
     868                 : ** ^If an error occurs while evaluating the SQL statements passed into
     869                 : ** sqlite3_exec(), then execution of the current statement stops and
     870                 : ** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
     871                 : ** is not NULL then any error message is written into memory obtained
     872                 : ** from [sqlite3_malloc()] and passed back through the 5th parameter.
     873                 : ** To avoid memory leaks, the application should invoke [sqlite3_free()]
     874                 : ** on error message strings returned through the 5th parameter of
     875                 : ** of sqlite3_exec() after the error message string is no longer needed.
     876                 : ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
     877                 : ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
     878                 : ** NULL before returning.
     879                 : **
     880                 : ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
     881                 : ** routine returns SQLITE_ABORT without invoking the callback again and
     882                 : ** without running any subsequent SQL statements.
     883                 : **
     884                 : ** ^The 2nd argument to the sqlite3_exec() callback function is the
     885                 : ** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
     886                 : ** callback is an array of pointers to strings obtained as if from
     887                 : ** [sqlite3_column_text()], one for each column.  ^If an element of a
     888                 : ** result row is NULL then the corresponding string pointer for the
     889                 : ** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
     890                 : ** sqlite3_exec() callback is an array of pointers to strings where each
     891                 : ** entry represents the name of corresponding result column as obtained
     892                 : ** from [sqlite3_column_name()].
     893                 : **
     894                 : ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
     895                 : ** to an empty string, or a pointer that contains only whitespace and/or 
     896                 : ** SQL comments, then no SQL statements are evaluated and the database
     897                 : ** is not changed.
     898                 : **
     899                 : ** Restrictions:
     900                 : **
     901                 : ** <ul>
     902                 : ** <li> The application must insure that the 1st parameter to sqlite3_exec()
     903                 : **      is a valid and open [database connection].
     904                 : ** <li> The application must not close [database connection] specified by
     905                 : **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
     906                 : ** <li> The application must not modify the SQL statement text passed into
     907                 : **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
     908                 : ** </ul>
     909                 : */
     910                 : SQLITE_API int sqlite3_exec(
     911                 :   sqlite3*,                                  /* An open database */
     912                 :   const char *sql,                           /* SQL to be evaluated */
     913                 :   int (*callback)(void*,int,char**,char**),  /* Callback function */
     914                 :   void *,                                    /* 1st argument to callback */
     915                 :   char **errmsg                              /* Error msg written here */
     916                 : );
     917                 : 
     918                 : /*
     919                 : ** CAPI3REF: Result Codes
     920                 : ** KEYWORDS: SQLITE_OK {error code} {error codes}
     921                 : ** KEYWORDS: {result code} {result codes}
     922                 : **
     923                 : ** Many SQLite functions return an integer result code from the set shown
     924                 : ** here in order to indicate success or failure.
     925                 : **
     926                 : ** New error codes may be added in future versions of SQLite.
     927                 : **
     928                 : ** See also: [SQLITE_IOERR_READ | extended result codes],
     929                 : ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
     930                 : */
     931                 : #define SQLITE_OK           0   /* Successful result */
     932                 : /* beginning-of-error-codes */
     933                 : #define SQLITE_ERROR        1   /* SQL error or missing database */
     934                 : #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
     935                 : #define SQLITE_PERM         3   /* Access permission denied */
     936                 : #define SQLITE_ABORT        4   /* Callback routine requested an abort */
     937                 : #define SQLITE_BUSY         5   /* The database file is locked */
     938                 : #define SQLITE_LOCKED       6   /* A table in the database is locked */
     939                 : #define SQLITE_NOMEM        7   /* A malloc() failed */
     940                 : #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
     941                 : #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
     942                 : #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
     943                 : #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
     944                 : #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
     945                 : #define SQLITE_FULL        13   /* Insertion failed because database is full */
     946                 : #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
     947                 : #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
     948                 : #define SQLITE_EMPTY       16   /* Database is empty */
     949                 : #define SQLITE_SCHEMA      17   /* The database schema changed */
     950                 : #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
     951                 : #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
     952                 : #define SQLITE_MISMATCH    20   /* Data type mismatch */
     953                 : #define SQLITE_MISUSE      21   /* Library used incorrectly */
     954                 : #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
     955                 : #define SQLITE_AUTH        23   /* Authorization denied */
     956                 : #define SQLITE_FORMAT      24   /* Auxiliary database format error */
     957                 : #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
     958                 : #define SQLITE_NOTADB      26   /* File opened that is not a database file */
     959                 : #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
     960                 : #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
     961                 : /* end-of-error-codes */
     962                 : 
     963                 : /*
     964                 : ** CAPI3REF: Extended Result Codes
     965                 : ** KEYWORDS: {extended error code} {extended error codes}
     966                 : ** KEYWORDS: {extended result code} {extended result codes}
     967                 : **
     968                 : ** In its default configuration, SQLite API routines return one of 26 integer
     969                 : ** [SQLITE_OK | result codes].  However, experience has shown that many of
     970                 : ** these result codes are too coarse-grained.  They do not provide as
     971                 : ** much information about problems as programmers might like.  In an effort to
     972                 : ** address this, newer versions of SQLite (version 3.3.8 and later) include
     973                 : ** support for additional result codes that provide more detailed information
     974                 : ** about errors. The extended result codes are enabled or disabled
     975                 : ** on a per database connection basis using the
     976                 : ** [sqlite3_extended_result_codes()] API.
     977                 : **
     978                 : ** Some of the available extended result codes are listed here.
     979                 : ** One may expect the number of extended result codes will be expand
     980                 : ** over time.  Software that uses extended result codes should expect
     981                 : ** to see new result codes in future releases of SQLite.
     982                 : **
     983                 : ** The SQLITE_OK result code will never be extended.  It will always
     984                 : ** be exactly zero.
     985                 : */
     986                 : #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
     987                 : #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
     988                 : #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
     989                 : #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
     990                 : #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
     991                 : #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
     992                 : #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
     993                 : #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
     994                 : #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
     995                 : #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
     996                 : #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
     997                 : #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
     998                 : #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
     999                 : #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
    1000                 : #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
    1001                 : #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
    1002                 : #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
    1003                 : #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
    1004                 : #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
    1005                 : #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
    1006                 : #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
    1007                 : #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
    1008                 : #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
    1009                 : #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
    1010                 : #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
    1011                 : #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
    1012                 : #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
    1013                 : #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
    1014                 : 
    1015                 : /*
    1016                 : ** CAPI3REF: Flags For File Open Operations
    1017                 : **
    1018                 : ** These bit values are intended for use in the
    1019                 : ** 3rd parameter to the [sqlite3_open_v2()] interface and
    1020                 : ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
    1021                 : */
    1022                 : #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
    1023                 : #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
    1024                 : #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
    1025                 : #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
    1026                 : #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
    1027                 : #define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
    1028                 : #define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
    1029                 : #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
    1030                 : #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
    1031                 : #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
    1032                 : #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
    1033                 : #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
    1034                 : #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
    1035                 : #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
    1036                 : #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
    1037                 : #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
    1038                 : #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
    1039                 : #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
    1040                 : #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
    1041                 : 
    1042                 : /* Reserved:                         0x00F00000 */
    1043                 : 
    1044                 : /*
    1045                 : ** CAPI3REF: Device Characteristics
    1046                 : **
    1047                 : ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
    1048                 : ** object returns an integer which is a vector of the these
    1049                 : ** bit values expressing I/O characteristics of the mass storage
    1050                 : ** device that holds the file that the [sqlite3_io_methods]
    1051                 : ** refers to.
    1052                 : **
    1053                 : ** The SQLITE_IOCAP_ATOMIC property means that all writes of
    1054                 : ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
    1055                 : ** mean that writes of blocks that are nnn bytes in size and
    1056                 : ** are aligned to an address which is an integer multiple of
    1057                 : ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
    1058                 : ** that when data is appended to a file, the data is appended
    1059                 : ** first then the size of the file is extended, never the other
    1060                 : ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
    1061                 : ** information is written to disk in the same order as calls
    1062                 : ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
    1063                 : ** after reboot following a crash or power loss, the only bytes in a
    1064                 : ** file that were written at the application level might have changed
    1065                 : ** and that adjacent bytes, even bytes within the same sector are
    1066                 : ** guaranteed to be unchanged.
    1067                 : */
    1068                 : #define SQLITE_IOCAP_ATOMIC                 0x00000001
    1069                 : #define SQLITE_IOCAP_ATOMIC512              0x00000002
    1070                 : #define SQLITE_IOCAP_ATOMIC1K               0x00000004
    1071                 : #define SQLITE_IOCAP_ATOMIC2K               0x00000008
    1072                 : #define SQLITE_IOCAP_ATOMIC4K               0x00000010
    1073                 : #define SQLITE_IOCAP_ATOMIC8K               0x00000020
    1074                 : #define SQLITE_IOCAP_ATOMIC16K              0x00000040
    1075                 : #define SQLITE_IOCAP_ATOMIC32K              0x00000080
    1076                 : #define SQLITE_IOCAP_ATOMIC64K              0x00000100
    1077                 : #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
    1078                 : #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
    1079                 : #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
    1080                 : #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
    1081                 : 
    1082                 : /*
    1083                 : ** CAPI3REF: File Locking Levels
    1084                 : **
    1085                 : ** SQLite uses one of these integer values as the second
    1086                 : ** argument to calls it makes to the xLock() and xUnlock() methods
    1087                 : ** of an [sqlite3_io_methods] object.
    1088                 : */
    1089                 : #define SQLITE_LOCK_NONE          0
    1090                 : #define SQLITE_LOCK_SHARED        1
    1091                 : #define SQLITE_LOCK_RESERVED      2
    1092                 : #define SQLITE_LOCK_PENDING       3
    1093                 : #define SQLITE_LOCK_EXCLUSIVE     4
    1094                 : 
    1095                 : /*
    1096                 : ** CAPI3REF: Synchronization Type Flags
    1097                 : **
    1098                 : ** When SQLite invokes the xSync() method of an
    1099                 : ** [sqlite3_io_methods] object it uses a combination of
    1100                 : ** these integer values as the second argument.
    1101                 : **
    1102                 : ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
    1103                 : ** sync operation only needs to flush data to mass storage.  Inode
    1104                 : ** information need not be flushed. If the lower four bits of the flag
    1105                 : ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
    1106                 : ** If the lower four bits equal SQLITE_SYNC_FULL, that means
    1107                 : ** to use Mac OS X style fullsync instead of fsync().
    1108                 : **
    1109                 : ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
    1110                 : ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
    1111                 : ** settings.  The [synchronous pragma] determines when calls to the
    1112                 : ** xSync VFS method occur and applies uniformly across all platforms.
    1113                 : ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
    1114                 : ** energetic or rigorous or forceful the sync operations are and
    1115                 : ** only make a difference on Mac OSX for the default SQLite code.
    1116                 : ** (Third-party VFS implementations might also make the distinction
    1117                 : ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
    1118                 : ** operating systems natively supported by SQLite, only Mac OSX
    1119                 : ** cares about the difference.)
    1120                 : */
    1121                 : #define SQLITE_SYNC_NORMAL        0x00002
    1122                 : #define SQLITE_SYNC_FULL          0x00003
    1123                 : #define SQLITE_SYNC_DATAONLY      0x00010
    1124                 : 
    1125                 : /*
    1126                 : ** CAPI3REF: OS Interface Open File Handle
    1127                 : **
    1128                 : ** An [sqlite3_file] object represents an open file in the 
    1129                 : ** [sqlite3_vfs | OS interface layer].  Individual OS interface
    1130                 : ** implementations will
    1131                 : ** want to subclass this object by appending additional fields
    1132                 : ** for their own use.  The pMethods entry is a pointer to an
    1133                 : ** [sqlite3_io_methods] object that defines methods for performing
    1134                 : ** I/O operations on the open file.
    1135                 : */
    1136                 : typedef struct sqlite3_file sqlite3_file;
    1137                 : struct sqlite3_file {
    1138                 :   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
    1139                 : };
    1140                 : 
    1141                 : /*
    1142                 : ** CAPI3REF: OS Interface File Virtual Methods Object
    1143                 : **
    1144                 : ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
    1145                 : ** [sqlite3_file] object (or, more commonly, a subclass of the
    1146                 : ** [sqlite3_file] object) with a pointer to an instance of this object.
    1147                 : ** This object defines the methods used to perform various operations
    1148                 : ** against the open file represented by the [sqlite3_file] object.
    1149                 : **
    1150                 : ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element 
    1151                 : ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
    1152                 : ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
    1153                 : ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
    1154                 : ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
    1155                 : ** to NULL.
    1156                 : **
    1157                 : ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
    1158                 : ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
    1159                 : ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
    1160                 : ** flag may be ORed in to indicate that only the data of the file
    1161                 : ** and not its inode needs to be synced.
    1162                 : **
    1163                 : ** The integer values to xLock() and xUnlock() are one of
    1164                 : ** <ul>
    1165                 : ** <li> [SQLITE_LOCK_NONE],
    1166                 : ** <li> [SQLITE_LOCK_SHARED],
    1167                 : ** <li> [SQLITE_LOCK_RESERVED],
    1168                 : ** <li> [SQLITE_LOCK_PENDING], or
    1169                 : ** <li> [SQLITE_LOCK_EXCLUSIVE].
    1170                 : ** </ul>
    1171                 : ** xLock() increases the lock. xUnlock() decreases the lock.
    1172                 : ** The xCheckReservedLock() method checks whether any database connection,
    1173                 : ** either in this process or in some other process, is holding a RESERVED,
    1174                 : ** PENDING, or EXCLUSIVE lock on the file.  It returns true
    1175                 : ** if such a lock exists and false otherwise.
    1176                 : **
    1177                 : ** The xFileControl() method is a generic interface that allows custom
    1178                 : ** VFS implementations to directly control an open file using the
    1179                 : ** [sqlite3_file_control()] interface.  The second "op" argument is an
    1180                 : ** integer opcode.  The third argument is a generic pointer intended to
    1181                 : ** point to a structure that may contain arguments or space in which to
    1182                 : ** write return values.  Potential uses for xFileControl() might be
    1183                 : ** functions to enable blocking locks with timeouts, to change the
    1184                 : ** locking strategy (for example to use dot-file locks), to inquire
    1185                 : ** about the status of a lock, or to break stale locks.  The SQLite
    1186                 : ** core reserves all opcodes less than 100 for its own use.
    1187                 : ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
    1188                 : ** Applications that define a custom xFileControl method should use opcodes
    1189                 : ** greater than 100 to avoid conflicts.  VFS implementations should
    1190                 : ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
    1191                 : ** recognize.
    1192                 : **
    1193                 : ** The xSectorSize() method returns the sector size of the
    1194                 : ** device that underlies the file.  The sector size is the
    1195                 : ** minimum write that can be performed without disturbing
    1196                 : ** other bytes in the file.  The xDeviceCharacteristics()
    1197                 : ** method returns a bit vector describing behaviors of the
    1198                 : ** underlying device:
    1199                 : **
    1200                 : ** <ul>
    1201                 : ** <li> [SQLITE_IOCAP_ATOMIC]
    1202                 : ** <li> [SQLITE_IOCAP_ATOMIC512]
    1203                 : ** <li> [SQLITE_IOCAP_ATOMIC1K]
    1204                 : ** <li> [SQLITE_IOCAP_ATOMIC2K]
    1205                 : ** <li> [SQLITE_IOCAP_ATOMIC4K]
    1206                 : ** <li> [SQLITE_IOCAP_ATOMIC8K]
    1207                 : ** <li> [SQLITE_IOCAP_ATOMIC16K]
    1208                 : ** <li> [SQLITE_IOCAP_ATOMIC32K]
    1209                 : ** <li> [SQLITE_IOCAP_ATOMIC64K]
    1210                 : ** <li> [SQLITE_IOCAP_SAFE_APPEND]
    1211                 : ** <li> [SQLITE_IOCAP_SEQUENTIAL]
    1212                 : ** </ul>
    1213                 : **
    1214                 : ** The SQLITE_IOCAP_ATOMIC property means that all writes of
    1215                 : ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
    1216                 : ** mean that writes of blocks that are nnn bytes in size and
    1217                 : ** are aligned to an address which is an integer multiple of
    1218                 : ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
    1219                 : ** that when data is appended to a file, the data is appended
    1220                 : ** first then the size of the file is extended, never the other
    1221                 : ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
    1222                 : ** information is written to disk in the same order as calls
    1223                 : ** to xWrite().
    1224                 : **
    1225                 : ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
    1226                 : ** in the unread portions of the buffer with zeros.  A VFS that
    1227                 : ** fails to zero-fill short reads might seem to work.  However,
    1228                 : ** failure to zero-fill short reads will eventually lead to
    1229                 : ** database corruption.
    1230                 : */
    1231                 : typedef struct sqlite3_io_methods sqlite3_io_methods;
    1232                 : struct sqlite3_io_methods {
    1233                 :   int iVersion;
    1234                 :   int (*xClose)(sqlite3_file*);
    1235                 :   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
    1236                 :   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
    1237                 :   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
    1238                 :   int (*xSync)(sqlite3_file*, int flags);
    1239                 :   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
    1240                 :   int (*xLock)(sqlite3_file*, int);
    1241                 :   int (*xUnlock)(sqlite3_file*, int);
    1242                 :   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
    1243                 :   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
    1244                 :   int (*xSectorSize)(sqlite3_file*);
    1245                 :   int (*xDeviceCharacteristics)(sqlite3_file*);
    1246                 :   /* Methods above are valid for version 1 */
    1247                 :   int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
    1248                 :   int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
    1249                 :   void (*xShmBarrier)(sqlite3_file*);
    1250                 :   int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
    1251                 :   /* Methods above are valid for version 2 */
    1252                 :   /* Additional methods may be added in future releases */
    1253                 : };
    1254                 : 
    1255                 : /*
    1256                 : ** CAPI3REF: Standard File Control Opcodes
    1257                 : **
    1258                 : ** These integer constants are opcodes for the xFileControl method
    1259                 : ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
    1260                 : ** interface.
    1261                 : **
    1262                 : ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
    1263                 : ** opcode causes the xFileControl method to write the current state of
    1264                 : ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
    1265                 : ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
    1266                 : ** into an integer that the pArg argument points to. This capability
    1267                 : ** is used during testing and only needs to be supported when SQLITE_TEST
    1268                 : ** is defined.
    1269                 : **
    1270                 : ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
    1271                 : ** layer a hint of how large the database file will grow to be during the
    1272                 : ** current transaction.  This hint is not guaranteed to be accurate but it
    1273                 : ** is often close.  The underlying VFS might choose to preallocate database
    1274                 : ** file space based on this hint in order to help writes to the database
    1275                 : ** file run faster.
    1276                 : **
    1277                 : ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
    1278                 : ** extends and truncates the database file in chunks of a size specified
    1279                 : ** by the user. The fourth argument to [sqlite3_file_control()] should 
    1280                 : ** point to an integer (type int) containing the new chunk-size to use
    1281                 : ** for the nominated database. Allocating database file space in large
    1282                 : ** chunks (say 1MB at a time), may reduce file-system fragmentation and
    1283                 : ** improve performance on some systems.
    1284                 : **
    1285                 : ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
    1286                 : ** to the [sqlite3_file] object associated with a particular database
    1287                 : ** connection.  See the [sqlite3_file_control()] documentation for
    1288                 : ** additional information.
    1289                 : **
    1290                 : ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
    1291                 : ** SQLite and sent to all VFSes in place of a call to the xSync method
    1292                 : ** when the database connection has [PRAGMA synchronous] set to OFF.)^
    1293                 : ** Some specialized VFSes need this signal in order to operate correctly
    1294                 : ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most 
    1295                 : ** VFSes do not need this signal and should silently ignore this opcode.
    1296                 : ** Applications should not call [sqlite3_file_control()] with this
    1297                 : ** opcode as doing so may disrupt the operation of the specialized VFSes
    1298                 : ** that do require it.  
    1299                 : **
    1300                 : ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
    1301                 : ** retry counts and intervals for certain disk I/O operations for the
    1302                 : ** windows [VFS] in order to provide robustness in the presence of
    1303                 : ** anti-virus programs.  By default, the windows VFS will retry file read,
    1304                 : ** file write, and file delete operations up to 10 times, with a delay
    1305                 : ** of 25 milliseconds before the first retry and with the delay increasing
    1306                 : ** by an additional 25 milliseconds with each subsequent retry.  This
    1307                 : ** opcode allows these two values (10 retries and 25 milliseconds of delay)
    1308                 : ** to be adjusted.  The values are changed for all database connections
    1309                 : ** within the same process.  The argument is a pointer to an array of two
    1310                 : ** integers where the first integer i the new retry count and the second
    1311                 : ** integer is the delay.  If either integer is negative, then the setting
    1312                 : ** is not changed but instead the prior value of that setting is written
    1313                 : ** into the array entry, allowing the current retry settings to be
    1314                 : ** interrogated.  The zDbName parameter is ignored.
    1315                 : **
    1316                 : ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
    1317                 : ** persistent [WAL | Write AHead Log] setting.  By default, the auxiliary
    1318                 : ** write ahead log and shared memory files used for transaction control
    1319                 : ** are automatically deleted when the latest connection to the database
    1320                 : ** closes.  Setting persistent WAL mode causes those files to persist after
    1321                 : ** close.  Persisting the files is useful when other processes that do not
    1322                 : ** have write permission on the directory containing the database file want
    1323                 : ** to read the database file, as the WAL and shared memory files must exist
    1324                 : ** in order for the database to be readable.  The fourth parameter to
    1325                 : ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
    1326                 : ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
    1327                 : ** WAL mode.  If the integer is -1, then it is overwritten with the current
    1328                 : ** WAL persistence setting.
    1329                 : **
    1330                 : ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
    1331                 : ** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
    1332                 : ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
    1333                 : ** xDeviceCharacteristics methods. The fourth parameter to
    1334                 : ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
    1335                 : ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
    1336                 : ** mode.  If the integer is -1, then it is overwritten with the current
    1337                 : ** zero-damage mode setting.
    1338                 : **
    1339                 : ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
    1340                 : ** a write transaction to indicate that, unless it is rolled back for some
    1341                 : ** reason, the entire database file will be overwritten by the current 
    1342                 : ** transaction. This is used by VACUUM operations.
    1343                 : **
    1344                 : ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
    1345                 : ** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
    1346                 : ** final bottom-level VFS are written into memory obtained from 
    1347                 : ** [sqlite3_malloc()] and the result is stored in the char* variable
    1348                 : ** that the fourth parameter of [sqlite3_file_control()] points to.
    1349                 : ** The caller is responsible for freeing the memory when done.  As with
    1350                 : ** all file-control actions, there is no guarantee that this will actually
    1351                 : ** do anything.  Callers should initialize the char* variable to a NULL
    1352                 : ** pointer in case this file-control is not implemented.  This file-control
    1353                 : ** is intended for diagnostic use only.
    1354                 : */
    1355                 : #define SQLITE_FCNTL_LOCKSTATE               1
    1356                 : #define SQLITE_GET_LOCKPROXYFILE             2
    1357                 : #define SQLITE_SET_LOCKPROXYFILE             3
    1358                 : #define SQLITE_LAST_ERRNO                    4
    1359                 : #define SQLITE_FCNTL_SIZE_HINT               5
    1360                 : #define SQLITE_FCNTL_CHUNK_SIZE              6
    1361                 : #define SQLITE_FCNTL_FILE_POINTER            7
    1362                 : #define SQLITE_FCNTL_SYNC_OMITTED            8
    1363                 : #define SQLITE_FCNTL_WIN32_AV_RETRY          9
    1364                 : #define SQLITE_FCNTL_PERSIST_WAL            10
    1365                 : #define SQLITE_FCNTL_OVERWRITE              11
    1366                 : #define SQLITE_FCNTL_VFSNAME                12
    1367                 : #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
    1368                 : 
    1369                 : /*
    1370                 : ** CAPI3REF: Mutex Handle
    1371                 : **
    1372                 : ** The mutex module within SQLite defines [sqlite3_mutex] to be an
    1373                 : ** abstract type for a mutex object.  The SQLite core never looks
    1374                 : ** at the internal representation of an [sqlite3_mutex].  It only
    1375                 : ** deals with pointers to the [sqlite3_mutex] object.
    1376                 : **
    1377                 : ** Mutexes are created using [sqlite3_mutex_alloc()].
    1378                 : */
    1379                 : typedef struct sqlite3_mutex sqlite3_mutex;
    1380                 : 
    1381                 : /*
    1382                 : ** CAPI3REF: OS Interface Object
    1383                 : **
    1384                 : ** An instance of the sqlite3_vfs object defines the interface between
    1385                 : ** the SQLite core and the underlying operating system.  The "vfs"
    1386                 : ** in the name of the object stands for "virtual file system".  See
    1387                 : ** the [VFS | VFS documentation] for further information.
    1388                 : **
    1389                 : ** The value of the iVersion field is initially 1 but may be larger in
    1390                 : ** future versions of SQLite.  Additional fields may be appended to this
    1391                 : ** object when the iVersion value is increased.  Note that the structure
    1392                 : ** of the sqlite3_vfs object changes in the transaction between
    1393                 : ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
    1394                 : ** modified.
    1395                 : **
    1396                 : ** The szOsFile field is the size of the subclassed [sqlite3_file]
    1397                 : ** structure used by this VFS.  mxPathname is the maximum length of
    1398                 : ** a pathname in this VFS.
    1399                 : **
    1400                 : ** Registered sqlite3_vfs objects are kept on a linked list formed by
    1401                 : ** the pNext pointer.  The [sqlite3_vfs_register()]
    1402                 : ** and [sqlite3_vfs_unregister()] interfaces manage this list
    1403                 : ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
    1404                 : ** searches the list.  Neither the application code nor the VFS
    1405                 : ** implementation should use the pNext pointer.
    1406                 : **
    1407                 : ** The pNext field is the only field in the sqlite3_vfs
    1408                 : ** structure that SQLite will ever modify.  SQLite will only access
    1409                 : ** or modify this field while holding a particular static mutex.
    1410                 : ** The application should never modify anything within the sqlite3_vfs
    1411                 : ** object once the object has been registered.
    1412                 : **
    1413                 : ** The zName field holds the name of the VFS module.  The name must
    1414                 : ** be unique across all VFS modules.
    1415                 : **
    1416                 : ** [[sqlite3_vfs.xOpen]]
    1417                 : ** ^SQLite guarantees that the zFilename parameter to xOpen
    1418                 : ** is either a NULL pointer or string obtained
    1419                 : ** from xFullPathname() with an optional suffix added.
    1420                 : ** ^If a suffix is added to the zFilename parameter, it will
    1421                 : ** consist of a single "-" character followed by no more than
    1422                 : ** 11 alphanumeric and/or "-" characters.
    1423                 : ** ^SQLite further guarantees that
    1424                 : ** the string will be valid and unchanged until xClose() is
    1425                 : ** called. Because of the previous sentence,
    1426                 : ** the [sqlite3_file] can safely store a pointer to the
    1427                 : ** filename if it needs to remember the filename for some reason.
    1428                 : ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
    1429                 : ** must invent its own temporary name for the file.  ^Whenever the 
    1430                 : ** xFilename parameter is NULL it will also be the case that the
    1431                 : ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
    1432                 : **
    1433                 : ** The flags argument to xOpen() includes all bits set in
    1434                 : ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
    1435                 : ** or [sqlite3_open16()] is used, then flags includes at least
    1436                 : ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. 
    1437                 : ** If xOpen() opens a file read-only then it sets *pOutFlags to
    1438                 : ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
    1439                 : **
    1440                 : ** ^(SQLite will also add one of the following flags to the xOpen()
    1441                 : ** call, depending on the object being opened:
    1442                 : **
    1443                 : ** <ul>
    1444                 : ** <li>  [SQLITE_OPEN_MAIN_DB]
    1445                 : ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
    1446                 : ** <li>  [SQLITE_OPEN_TEMP_DB]
    1447                 : ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
    1448                 : ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
    1449                 : ** <li>  [SQLITE_OPEN_SUBJOURNAL]
    1450                 : ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
    1451                 : ** <li>  [SQLITE_OPEN_WAL]
    1452                 : ** </ul>)^
    1453                 : **
    1454                 : ** The file I/O implementation can use the object type flags to
    1455                 : ** change the way it deals with files.  For example, an application
    1456                 : ** that does not care about crash recovery or rollback might make
    1457                 : ** the open of a journal file a no-op.  Writes to this journal would
    1458                 : ** also be no-ops, and any attempt to read the journal would return
    1459                 : ** SQLITE_IOERR.  Or the implementation might recognize that a database
    1460                 : ** file will be doing page-aligned sector reads and writes in a random
    1461                 : ** order and set up its I/O subsystem accordingly.
    1462                 : **
    1463                 : ** SQLite might also add one of the following flags to the xOpen method:
    1464                 : **
    1465                 : ** <ul>
    1466                 : ** <li> [SQLITE_OPEN_DELETEONCLOSE]
    1467                 : ** <li> [SQLITE_OPEN_EXCLUSIVE]
    1468                 : ** </ul>
    1469                 : **
    1470                 : ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
    1471                 : ** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
    1472                 : ** will be set for TEMP databases and their journals, transient
    1473                 : ** databases, and subjournals.
    1474                 : **
    1475                 : ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
    1476                 : ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
    1477                 : ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
    1478                 : ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the 
    1479                 : ** SQLITE_OPEN_CREATE, is used to indicate that file should always
    1480                 : ** be created, and that it is an error if it already exists.
    1481                 : ** It is <i>not</i> used to indicate the file should be opened 
    1482                 : ** for exclusive access.
    1483                 : **
    1484                 : ** ^At least szOsFile bytes of memory are allocated by SQLite
    1485                 : ** to hold the  [sqlite3_file] structure passed as the third
    1486                 : ** argument to xOpen.  The xOpen method does not have to
    1487                 : ** allocate the structure; it should just fill it in.  Note that
    1488                 : ** the xOpen method must set the sqlite3_file.pMethods to either
    1489                 : ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
    1490                 : ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
    1491                 : ** element will be valid after xOpen returns regardless of the success
    1492                 : ** or failure of the xOpen call.
    1493                 : **
    1494                 : ** [[sqlite3_vfs.xAccess]]
    1495                 : ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
    1496                 : ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
    1497                 : ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
    1498                 : ** to test whether a file is at least readable.   The file can be a
    1499                 : ** directory.
    1500                 : **
    1501                 : ** ^SQLite will always allocate at least mxPathname+1 bytes for the
    1502                 : ** output buffer xFullPathname.  The exact size of the output buffer
    1503                 : ** is also passed as a parameter to both  methods. If the output buffer
    1504                 : ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
    1505                 : ** handled as a fatal error by SQLite, vfs implementations should endeavor
    1506                 : ** to prevent this by setting mxPathname to a sufficiently large value.
    1507                 : **
    1508                 : ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
    1509                 : ** interfaces are not strictly a part of the filesystem, but they are
    1510                 : ** included in the VFS structure for completeness.
    1511                 : ** The xRandomness() function attempts to return nBytes bytes
    1512                 : ** of good-quality randomness into zOut.  The return value is
    1513                 : ** the actual number of bytes of randomness obtained.
    1514                 : ** The xSleep() method causes the calling thread to sleep for at
    1515                 : ** least the number of microseconds given.  ^The xCurrentTime()
    1516                 : ** method returns a Julian Day Number for the current date and time as
    1517                 : ** a floating point value.
    1518                 : ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
    1519                 : ** Day Number multiplied by 86400000 (the number of milliseconds in 
    1520                 : ** a 24-hour day).  
    1521                 : ** ^SQLite will use the xCurrentTimeInt64() method to get the current
    1522                 : ** date and time if that method is available (if iVersion is 2 or 
    1523                 : ** greater and the function pointer is not NULL) and will fall back
    1524                 : ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
    1525                 : **
    1526                 : ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
    1527                 : ** are not used by the SQLite core.  These optional interfaces are provided
    1528                 : ** by some VFSes to facilitate testing of the VFS code. By overriding 
    1529                 : ** system calls with functions under its control, a test program can
    1530                 : ** simulate faults and error conditions that would otherwise be difficult
    1531                 : ** or impossible to induce.  The set of system calls that can be overridden
    1532                 : ** varies from one VFS to another, and from one version of the same VFS to the
    1533                 : ** next.  Applications that use these interfaces must be prepared for any
    1534                 : ** or all of these interfaces to be NULL or for their behavior to change
    1535                 : ** from one release to the next.  Applications must not attempt to access
    1536                 : ** any of these methods if the iVersion of the VFS is less than 3.
    1537                 : */
    1538                 : typedef struct sqlite3_vfs sqlite3_vfs;
    1539                 : typedef void (*sqlite3_syscall_ptr)(void);
    1540                 : struct sqlite3_vfs {
    1541                 :   int iVersion;            /* Structure version number (currently 3) */
    1542                 :   int szOsFile;            /* Size of subclassed sqlite3_file */
    1543                 :   int mxPathname;          /* Maximum file pathname length */
    1544                 :   sqlite3_vfs *pNext;      /* Next registered VFS */
    1545                 :   const char *zName;       /* Name of this virtual file system */
    1546                 :   void *pAppData;          /* Pointer to application-specific data */
    1547                 :   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
    1548                 :                int flags, int *pOutFlags);
    1549                 :   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
    1550                 :   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
    1551                 :   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
    1552                 :   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
    1553                 :   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
    1554                 :   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
    1555                 :   void (*xDlClose)(sqlite3_vfs*, void*);
    1556                 :   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
    1557                 :   int (*xSleep)(sqlite3_vfs*, int microseconds);
    1558                 :   int (*xCurrentTime)(sqlite3_vfs*, double*);
    1559                 :   int (*xGetLastError)(sqlite3_vfs*, int, char *);
    1560                 :   /*
    1561                 :   ** The methods above are in version 1 of the sqlite_vfs object
    1562                 :   ** definition.  Those that follow are added in version 2 or later
    1563                 :   */
    1564                 :   int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
    1565                 :   /*
    1566                 :   ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
    1567                 :   ** Those below are for version 3 and greater.
    1568                 :   */
    1569                 :   int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
    1570                 :   sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
    1571                 :   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
    1572                 :   /*
    1573                 :   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
    1574                 :   ** New fields may be appended in figure versions.  The iVersion
    1575                 :   ** value will increment whenever this happens. 
    1576                 :   */
    1577                 : };
    1578                 : 
    1579                 : /*
    1580                 : ** CAPI3REF: Flags for the xAccess VFS method
    1581                 : **
    1582                 : ** These integer constants can be used as the third parameter to
    1583                 : ** the xAccess method of an [sqlite3_vfs] object.  They determine
    1584                 : ** what kind of permissions the xAccess method is looking for.
    1585                 : ** With SQLITE_ACCESS_EXISTS, the xAccess method
    1586                 : ** simply checks whether the file exists.
    1587                 : ** With SQLITE_ACCESS_READWRITE, the xAccess method
    1588                 : ** checks whether the named directory is both readable and writable
    1589                 : ** (in other words, if files can be added, removed, and renamed within
    1590                 : ** the directory).
    1591                 : ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
    1592                 : ** [temp_store_directory pragma], though this could change in a future
    1593                 : ** release of SQLite.
    1594                 : ** With SQLITE_ACCESS_READ, the xAccess method
    1595                 : ** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
    1596                 : ** currently unused, though it might be used in a future release of
    1597                 : ** SQLite.
    1598                 : */
    1599                 : #define SQLITE_ACCESS_EXISTS    0
    1600                 : #define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
    1601                 : #define SQLITE_ACCESS_READ      2   /* Unused */
    1602                 : 
    1603                 : /*
    1604                 : ** CAPI3REF: Flags for the xShmLock VFS method
    1605                 : **
    1606                 : ** These integer constants define the various locking operations
    1607                 : ** allowed by the xShmLock method of [sqlite3_io_methods].  The
    1608                 : ** following are the only legal combinations of flags to the
    1609                 : ** xShmLock method:
    1610                 : **
    1611                 : ** <ul>
    1612                 : ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
    1613                 : ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
    1614                 : ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
    1615                 : ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
    1616                 : ** </ul>
    1617                 : **
    1618                 : ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
    1619                 : ** was given no the corresponding lock.  
    1620                 : **
    1621                 : ** The xShmLock method can transition between unlocked and SHARED or
    1622                 : ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
    1623                 : ** and EXCLUSIVE.
    1624                 : */
    1625                 : #define SQLITE_SHM_UNLOCK       1
    1626                 : #define SQLITE_SHM_LOCK         2
    1627                 : #define SQLITE_SHM_SHARED       4
    1628                 : #define SQLITE_SHM_EXCLUSIVE    8
    1629                 : 
    1630                 : /*
    1631                 : ** CAPI3REF: Maximum xShmLock index
    1632                 : **
    1633                 : ** The xShmLock method on [sqlite3_io_methods] may use values
    1634                 : ** between 0 and this upper bound as its "offset" argument.
    1635                 : ** The SQLite core will never attempt to acquire or release a
    1636                 : ** lock outside of this range
    1637                 : */
    1638                 : #define SQLITE_SHM_NLOCK        8
    1639                 : 
    1640                 : 
    1641                 : /*
    1642                 : ** CAPI3REF: Initialize The SQLite Library
    1643                 : **
    1644                 : ** ^The sqlite3_initialize() routine initializes the
    1645                 : ** SQLite library.  ^The sqlite3_shutdown() routine
    1646                 : ** deallocates any resources that were allocated by sqlite3_initialize().
    1647                 : ** These routines are designed to aid in process initialization and
    1648                 : ** shutdown on embedded systems.  Workstation applications using
    1649                 : ** SQLite normally do not need to invoke either of these routines.
    1650                 : **
    1651                 : ** A call to sqlite3_initialize() is an "effective" call if it is
    1652                 : ** the first time sqlite3_initialize() is invoked during the lifetime of
    1653                 : ** the process, or if it is the first time sqlite3_initialize() is invoked
    1654                 : ** following a call to sqlite3_shutdown().  ^(Only an effective call
    1655                 : ** of sqlite3_initialize() does any initialization.  All other calls
    1656                 : ** are harmless no-ops.)^
    1657                 : **
    1658                 : ** A call to sqlite3_shutdown() is an "effective" call if it is the first
    1659                 : ** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
    1660                 : ** an effective call to sqlite3_shutdown() does any deinitialization.
    1661                 : ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
    1662                 : **
    1663                 : ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
    1664                 : ** is not.  The sqlite3_shutdown() interface must only be called from a
    1665                 : ** single thread.  All open [database connections] must be closed and all
    1666                 : ** other SQLite resources must be deallocated prior to invoking
    1667                 : ** sqlite3_shutdown().
    1668                 : **
    1669                 : ** Among other things, ^sqlite3_initialize() will invoke
    1670                 : ** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
    1671                 : ** will invoke sqlite3_os_end().
    1672                 : **
    1673                 : ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
    1674                 : ** ^If for some reason, sqlite3_initialize() is unable to initialize
    1675                 : ** the library (perhaps it is unable to allocate a needed resource such
    1676                 : ** as a mutex) it returns an [error code] other than [SQLITE_OK].
    1677                 : **
    1678                 : ** ^The sqlite3_initialize() routine is called internally by many other
    1679                 : ** SQLite interfaces so that an application usually does not need to
    1680                 : ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
    1681                 : ** calls sqlite3_initialize() so the SQLite library will be automatically
    1682                 : ** initialized when [sqlite3_open()] is called if it has not be initialized
    1683                 : ** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
    1684                 : ** compile-time option, then the automatic calls to sqlite3_initialize()
    1685                 : ** are omitted and the application must call sqlite3_initialize() directly
    1686                 : ** prior to using any other SQLite interface.  For maximum portability,
    1687                 : ** it is recommended that applications always invoke sqlite3_initialize()
    1688                 : ** directly prior to using any other SQLite interface.  Future releases
    1689                 : ** of SQLite may require this.  In other words, the behavior exhibited
    1690                 : ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
    1691                 : ** default behavior in some future release of SQLite.
    1692                 : **
    1693                 : ** The sqlite3_os_init() routine does operating-system specific
    1694                 : ** initialization of the SQLite library.  The sqlite3_os_end()
    1695                 : ** routine undoes the effect of sqlite3_os_init().  Typical tasks
    1696                 : ** performed by these routines include allocation or deallocation
    1697                 : ** of static resources, initialization of global variables,
    1698                 : ** setting up a default [sqlite3_vfs] module, or setting up
    1699                 : ** a default configuration using [sqlite3_config()].
    1700                 : **
    1701                 : ** The application should never invoke either sqlite3_os_init()
    1702                 : ** or sqlite3_os_end() directly.  The application should only invoke
    1703                 : ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
    1704                 : ** interface is called automatically by sqlite3_initialize() and
    1705                 : ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
    1706                 : ** implementations for sqlite3_os_init() and sqlite3_os_end()
    1707                 : ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
    1708                 : ** When [custom builds | built for other platforms]
    1709                 : ** (using the [SQLITE_OS_OTHER=1] compile-time
    1710                 : ** option) the application must supply a suitable implementation for
    1711                 : ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
    1712                 : ** implementation of sqlite3_os_init() or sqlite3_os_end()
    1713                 : ** must return [SQLITE_OK] on success and some other [error code] upon
    1714                 : ** failure.
    1715                 : */
    1716                 : SQLITE_API int sqlite3_initialize(void);
    1717                 : SQLITE_API int sqlite3_shutdown(void);
    1718                 : SQLITE_API int sqlite3_os_init(void);
    1719                 : SQLITE_API int sqlite3_os_end(void);
    1720                 : 
    1721                 : /*
    1722                 : ** CAPI3REF: Configuring The SQLite Library
    1723                 : **
    1724                 : ** The sqlite3_config() interface is used to make global configuration
    1725                 : ** changes to SQLite in order to tune SQLite to the specific needs of
    1726                 : ** the application.  The default configuration is recommended for most
    1727                 : ** applications and so this routine is usually not necessary.  It is
    1728                 : ** provided to support rare applications with unusual needs.
    1729                 : **
    1730                 : ** The sqlite3_config() interface is not threadsafe.  The application
    1731                 : ** must insure that no other SQLite interfaces are invoked by other
    1732                 : ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
    1733                 : ** may only be invoked prior to library initialization using
    1734                 : ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
    1735                 : ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
    1736                 : ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
    1737                 : ** Note, however, that ^sqlite3_config() can be called as part of the
    1738                 : ** implementation of an application-defined [sqlite3_os_init()].
    1739                 : **
    1740                 : ** The first argument to sqlite3_config() is an integer
    1741                 : ** [configuration option] that determines
    1742                 : ** what property of SQLite is to be configured.  Subsequent arguments
    1743                 : ** vary depending on the [configuration option]
    1744                 : ** in the first argument.
    1745                 : **
    1746                 : ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
    1747                 : ** ^If the option is unknown or SQLite is unable to set the option
    1748                 : ** then this routine returns a non-zero [error code].
    1749                 : */
    1750                 : SQLITE_API int sqlite3_config(int, ...);
    1751                 : 
    1752                 : /*
    1753                 : ** CAPI3REF: Configure database connections
    1754                 : **
    1755                 : ** The sqlite3_db_config() interface is used to make configuration
    1756                 : ** changes to a [database connection].  The interface is similar to
    1757                 : ** [sqlite3_config()] except that the changes apply to a single
    1758                 : ** [database connection] (specified in the first argument).
    1759                 : **
    1760                 : ** The second argument to sqlite3_db_config(D,V,...)  is the
    1761                 : ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code 
    1762                 : ** that indicates what aspect of the [database connection] is being configured.
    1763                 : ** Subsequent arguments vary depending on the configuration verb.
    1764                 : **
    1765                 : ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
    1766                 : ** the call is considered successful.
    1767                 : */
    1768                 : SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
    1769                 : 
    1770                 : /*
    1771                 : ** CAPI3REF: Memory Allocation Routines
    1772                 : **
    1773                 : ** An instance of this object defines the interface between SQLite
    1774                 : ** and low-level memory allocation routines.
    1775                 : **
    1776                 : ** This object is used in only one place in the SQLite interface.
    1777                 : ** A pointer to an instance of this object is the argument to
    1778                 : ** [sqlite3_config()] when the configuration option is
    1779                 : ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].  
    1780                 : ** By creating an instance of this object
    1781                 : ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
    1782                 : ** during configuration, an application can specify an alternative
    1783                 : ** memory allocation subsystem for SQLite to use for all of its
    1784                 : ** dynamic memory needs.
    1785                 : **
    1786                 : ** Note that SQLite comes with several [built-in memory allocators]
    1787                 : ** that are perfectly adequate for the overwhelming majority of applications
    1788                 : ** and that this object is only useful to a tiny minority of applications
    1789                 : ** with specialized memory allocation requirements.  This object is
    1790                 : ** also used during testing of SQLite in order to specify an alternative
    1791                 : ** memory allocator that simulates memory out-of-memory conditions in
    1792                 : ** order to verify that SQLite recovers gracefully from such
    1793                 : ** conditions.
    1794                 : **
    1795                 : ** The xMalloc, xRealloc, and xFree methods must work like the
    1796                 : ** malloc(), realloc() and free() functions from the standard C library.
    1797                 : ** ^SQLite guarantees that the second argument to
    1798                 : ** xRealloc is always a value returned by a prior call to xRoundup.
    1799                 : **
    1800                 : ** xSize should return the allocated size of a memory allocation
    1801                 : ** previously obtained from xMalloc or xRealloc.  The allocated size
    1802                 : ** is always at least as big as the requested size but may be larger.
    1803                 : **
    1804                 : ** The xRoundup method returns what would be the allocated size of
    1805                 : ** a memory allocation given a particular requested size.  Most memory
    1806                 : ** allocators round up memory allocations at least to the next multiple
    1807                 : ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
    1808                 : ** Every memory allocation request coming in through [sqlite3_malloc()]
    1809                 : ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0, 
    1810                 : ** that causes the corresponding memory allocation to fail.
    1811                 : **
    1812                 : ** The xInit method initializes the memory allocator.  (For example,
    1813                 : ** it might allocate any require mutexes or initialize internal data
    1814                 : ** structures.  The xShutdown method is invoked (indirectly) by
    1815                 : ** [sqlite3_shutdown()] and should deallocate any resources acquired
    1816                 : ** by xInit.  The pAppData pointer is used as the only parameter to
    1817                 : ** xInit and xShutdown.
    1818                 : **
    1819                 : ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
    1820                 : ** the xInit method, so the xInit method need not be threadsafe.  The
    1821                 : ** xShutdown method is only called from [sqlite3_shutdown()] so it does
    1822                 : ** not need to be threadsafe either.  For all other methods, SQLite
    1823                 : ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
    1824                 : ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
    1825                 : ** it is by default) and so the methods are automatically serialized.
    1826                 : ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
    1827                 : ** methods must be threadsafe or else make their own arrangements for
    1828                 : ** serialization.
    1829                 : **
    1830                 : ** SQLite will never invoke xInit() more than once without an intervening
    1831                 : ** call to xShutdown().
    1832                 : */
    1833                 : typedef struct sqlite3_mem_methods sqlite3_mem_methods;
    1834                 : struct sqlite3_mem_methods {
    1835                 :   void *(*xMalloc)(int);         /* Memory allocation function */
    1836                 :   void (*xFree)(void*);          /* Free a prior allocation */
    1837                 :   void *(*xRealloc)(void*,int);  /* Resize an allocation */
    1838                 :   int (*xSize)(void*);           /* Return the size of an allocation */
    1839                 :   int (*xRoundup)(int);          /* Round up request size to allocation size */
    1840                 :   int (*xInit)(void*);           /* Initialize the memory allocator */
    1841                 :   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
    1842                 :   void *pAppData;                /* Argument to xInit() and xShutdown() */
    1843                 : };
    1844                 : 
    1845                 : /*
    1846                 : ** CAPI3REF: Configuration Options
    1847                 : ** KEYWORDS: {configuration option}
    1848                 : **
    1849                 : ** These constants are the available integer configuration options that
    1850                 : ** can be passed as the first argument to the [sqlite3_config()] interface.
    1851                 : **
    1852                 : ** New configuration options may be added in future releases of SQLite.
    1853                 : ** Existing configuration options might be discontinued.  Applications
    1854                 : ** should check the return code from [sqlite3_config()] to make sure that
    1855                 : ** the call worked.  The [sqlite3_config()] interface will return a
    1856                 : ** non-zero [error code] if a discontinued or unsupported configuration option
    1857                 : ** is invoked.
    1858                 : **
    1859                 : ** <dl>
    1860                 : ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
    1861                 : ** <dd>There are no arguments to this option.  ^This option sets the
    1862                 : ** [threading mode] to Single-thread.  In other words, it disables
    1863                 : ** all mutexing and puts SQLite into a mode where it can only be used
    1864                 : ** by a single thread.   ^If SQLite is compiled with
    1865                 : ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
    1866                 : ** it is not possible to change the [threading mode] from its default
    1867                 : ** value of Single-thread and so [sqlite3_config()] will return 
    1868                 : ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
    1869                 : ** configuration option.</dd>
    1870                 : **
    1871                 : ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
    1872                 : ** <dd>There are no arguments to this option.  ^This option sets the
    1873                 : ** [threading mode] to Multi-thread.  In other words, it disables
    1874                 : ** mutexing on [database connection] and [prepared statement] objects.
    1875                 : ** The application is responsible for serializing access to
    1876                 : ** [database connections] and [prepared statements].  But other mutexes
    1877                 : ** are enabled so that SQLite will be safe to use in a multi-threaded
    1878                 : ** environment as long as no two threads attempt to use the same
    1879                 : ** [database connection] at the same time.  ^If SQLite is compiled with
    1880                 : ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
    1881                 : ** it is not possible to set the Multi-thread [threading mode] and
    1882                 : ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
    1883                 : ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
    1884                 : **
    1885                 : ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
    1886                 : ** <dd>There are no arguments to this option.  ^This option sets the
    1887                 : ** [threading mode] to Serialized. In other words, this option enables
    1888                 : ** all mutexes including the recursive
    1889                 : ** mutexes on [database connection] and [prepared statement] objects.
    1890                 : ** In this mode (which is the default when SQLite is compiled with
    1891                 : ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
    1892                 : ** to [database connections] and [prepared statements] so that the
    1893                 : ** application is free to use the same [database connection] or the
    1894                 : ** same [prepared statement] in different threads at the same time.
    1895                 : ** ^If SQLite is compiled with
    1896                 : ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
    1897                 : ** it is not possible to set the Serialized [threading mode] and
    1898                 : ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
    1899                 : ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
    1900                 : **
    1901                 : ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
    1902                 : ** <dd> ^(This option takes a single argument which is a pointer to an
    1903                 : ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
    1904                 : ** alternative low-level memory allocation routines to be used in place of
    1905                 : ** the memory allocation routines built into SQLite.)^ ^SQLite makes
    1906                 : ** its own private copy of the content of the [sqlite3_mem_methods] structure
    1907                 : ** before the [sqlite3_config()] call returns.</dd>
    1908                 : **
    1909                 : ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
    1910                 : ** <dd> ^(This option takes a single argument which is a pointer to an
    1911                 : ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
    1912                 : ** structure is filled with the currently defined memory allocation routines.)^
    1913                 : ** This option can be used to overload the default memory allocation
    1914                 : ** routines with a wrapper that simulations memory allocation failure or
    1915                 : ** tracks memory usage, for example. </dd>
    1916                 : **
    1917                 : ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
    1918                 : ** <dd> ^This option takes single argument of type int, interpreted as a 
    1919                 : ** boolean, which enables or disables the collection of memory allocation 
    1920                 : ** statistics. ^(When memory allocation statistics are disabled, the 
    1921                 : ** following SQLite interfaces become non-operational:
    1922                 : **   <ul>
    1923                 : **   <li> [sqlite3_memory_used()]
    1924                 : **   <li> [sqlite3_memory_highwater()]
    1925                 : **   <li> [sqlite3_soft_heap_limit64()]
    1926                 : **   <li> [sqlite3_status()]
    1927                 : **   </ul>)^
    1928                 : ** ^Memory allocation statistics are enabled by default unless SQLite is
    1929                 : ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
    1930                 : ** allocation statistics are disabled by default.
    1931                 : ** </dd>
    1932                 : **
    1933                 : ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
    1934                 : ** <dd> ^This option specifies a static memory buffer that SQLite can use for
    1935                 : ** scratch memory.  There are three arguments:  A pointer an 8-byte
    1936                 : ** aligned memory buffer from which the scratch allocations will be
    1937                 : ** drawn, the size of each scratch allocation (sz),
    1938                 : ** and the maximum number of scratch allocations (N).  The sz
    1939                 : ** argument must be a multiple of 16.
    1940                 : ** The first argument must be a pointer to an 8-byte aligned buffer
    1941                 : ** of at least sz*N bytes of memory.
    1942                 : ** ^SQLite will use no more than two scratch buffers per thread.  So
    1943                 : ** N should be set to twice the expected maximum number of threads.
    1944                 : ** ^SQLite will never require a scratch buffer that is more than 6
    1945                 : ** times the database page size. ^If SQLite needs needs additional
    1946                 : ** scratch memory beyond what is provided by this configuration option, then 
    1947                 : ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
    1948                 : **
    1949                 : ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
    1950                 : ** <dd> ^This option specifies a static memory buffer that SQLite can use for
    1951                 : ** the database page cache with the default page cache implementation.  
    1952                 : ** This configuration should not be used if an application-define page
    1953                 : ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
    1954                 : ** There are three arguments to this option: A pointer to 8-byte aligned
    1955                 : ** memory, the size of each page buffer (sz), and the number of pages (N).
    1956                 : ** The sz argument should be the size of the largest database page
    1957                 : ** (a power of two between 512 and 32768) plus a little extra for each
    1958                 : ** page header.  ^The page header size is 20 to 40 bytes depending on
    1959                 : ** the host architecture.  ^It is harmless, apart from the wasted memory,
    1960                 : ** to make sz a little too large.  The first
    1961                 : ** argument should point to an allocation of at least sz*N bytes of memory.
    1962                 : ** ^SQLite will use the memory provided by the first argument to satisfy its
    1963                 : ** memory needs for the first N pages that it adds to cache.  ^If additional
    1964                 : ** page cache memory is needed beyond what is provided by this option, then
    1965                 : ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
    1966                 : ** The pointer in the first argument must
    1967                 : ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
    1968                 : ** will be undefined.</dd>
    1969                 : **
    1970                 : ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
    1971                 : ** <dd> ^This option specifies a static memory buffer that SQLite will use
    1972                 : ** for all of its dynamic memory allocation needs beyond those provided
    1973                 : ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
    1974                 : ** There are three arguments: An 8-byte aligned pointer to the memory,
    1975                 : ** the number of bytes in the memory buffer, and the minimum allocation size.
    1976                 : ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
    1977                 : ** to using its default memory allocator (the system malloc() implementation),
    1978                 : ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
    1979                 : ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
    1980                 : ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
    1981                 : ** allocator is engaged to handle all of SQLites memory allocation needs.
    1982                 : ** The first pointer (the memory pointer) must be aligned to an 8-byte
    1983                 : ** boundary or subsequent behavior of SQLite will be undefined.
    1984                 : ** The minimum allocation size is capped at 2**12. Reasonable values
    1985                 : ** for the minimum allocation size are 2**5 through 2**8.</dd>
    1986                 : **
    1987                 : ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
    1988                 : ** <dd> ^(This option takes a single argument which is a pointer to an
    1989                 : ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
    1990                 : ** alternative low-level mutex routines to be used in place
    1991                 : ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
    1992                 : ** content of the [sqlite3_mutex_methods] structure before the call to
    1993                 : ** [sqlite3_config()] returns. ^If SQLite is compiled with
    1994                 : ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
    1995                 : ** the entire mutexing subsystem is omitted from the build and hence calls to
    1996                 : ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
    1997                 : ** return [SQLITE_ERROR].</dd>
    1998                 : **
    1999                 : ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
    2000                 : ** <dd> ^(This option takes a single argument which is a pointer to an
    2001                 : ** instance of the [sqlite3_mutex_methods] structure.  The
    2002                 : ** [sqlite3_mutex_methods]
    2003                 : ** structure is filled with the currently defined mutex routines.)^
    2004                 : ** This option can be used to overload the default mutex allocation
    2005                 : ** routines with a wrapper used to track mutex usage for performance
    2006                 : ** profiling or testing, for example.   ^If SQLite is compiled with
    2007                 : ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
    2008                 : ** the entire mutexing subsystem is omitted from the build and hence calls to
    2009                 : ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
    2010                 : ** return [SQLITE_ERROR].</dd>
    2011                 : **
    2012                 : ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
    2013                 : ** <dd> ^(This option takes two arguments that determine the default
    2014                 : ** memory allocation for the lookaside memory allocator on each
    2015                 : ** [database connection].  The first argument is the
    2016                 : ** size of each lookaside buffer slot and the second is the number of
    2017                 : ** slots allocated to each database connection.)^  ^(This option sets the
    2018                 : ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
    2019                 : ** verb to [sqlite3_db_config()] can be used to change the lookaside
    2020                 : ** configuration on individual connections.)^ </dd>
    2021                 : **
    2022                 : ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
    2023                 : ** <dd> ^(This option takes a single argument which is a pointer to
    2024                 : ** an [sqlite3_pcache_methods2] object.  This object specifies the interface
    2025                 : ** to a custom page cache implementation.)^  ^SQLite makes a copy of the
    2026                 : ** object and uses it for page cache memory allocations.</dd>
    2027                 : **
    2028                 : ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
    2029                 : ** <dd> ^(This option takes a single argument which is a pointer to an
    2030                 : ** [sqlite3_pcache_methods2] object.  SQLite copies of the current
    2031                 : ** page cache implementation into that object.)^ </dd>
    2032                 : **
    2033                 : ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
    2034                 : ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
    2035                 : ** function with a call signature of void(*)(void*,int,const char*), 
    2036                 : ** and a pointer to void. ^If the function pointer is not NULL, it is
    2037                 : ** invoked by [sqlite3_log()] to process each logging event.  ^If the
    2038                 : ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
    2039                 : ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
    2040                 : ** passed through as the first parameter to the application-defined logger
    2041                 : ** function whenever that function is invoked.  ^The second parameter to
    2042                 : ** the logger function is a copy of the first parameter to the corresponding
    2043                 : ** [sqlite3_log()] call and is intended to be a [result code] or an
    2044                 : ** [extended result code].  ^The third parameter passed to the logger is
    2045                 : ** log message after formatting via [sqlite3_snprintf()].
    2046                 : ** The SQLite logging interface is not reentrant; the logger function
    2047                 : ** supplied by the application must not invoke any SQLite interface.
    2048                 : ** In a multi-threaded application, the application-defined logger
    2049                 : ** function must be threadsafe. </dd>
    2050                 : **
    2051                 : ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
    2052                 : ** <dd> This option takes a single argument of type int. If non-zero, then
    2053                 : ** URI handling is globally enabled. If the parameter is zero, then URI handling
    2054                 : ** is globally disabled. If URI handling is globally enabled, all filenames
    2055                 : ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
    2056                 : ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
    2057                 : ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
    2058                 : ** connection is opened. If it is globally disabled, filenames are
    2059                 : ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
    2060                 : ** database connection is opened. By default, URI handling is globally
    2061                 : ** disabled. The default value may be changed by compiling with the
    2062                 : ** [SQLITE_USE_URI] symbol defined.
    2063                 : **
    2064                 : ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
    2065                 : ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
    2066                 : ** <dd> These options are obsolete and should not be used by new code.
    2067                 : ** They are retained for backwards compatibility but are now no-ops.
    2068                 : ** </dl>
    2069                 : */
    2070                 : #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
    2071                 : #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
    2072                 : #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
    2073                 : #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
    2074                 : #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
    2075                 : #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
    2076                 : #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
    2077                 : #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
    2078                 : #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
    2079                 : #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
    2080                 : #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
    2081                 : /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 
    2082                 : #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
    2083                 : #define SQLITE_CONFIG_PCACHE       14  /* no-op */
    2084                 : #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
    2085                 : #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
    2086                 : #define SQLITE_CONFIG_URI          17  /* int */
    2087                 : #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
    2088                 : #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
    2089                 : 
    2090                 : /*
    2091                 : ** CAPI3REF: Database Connection Configuration Options
    2092                 : **
    2093                 : ** These constants are the available integer configuration options that
    2094                 : ** can be passed as the second argument to the [sqlite3_db_config()] interface.
    2095                 : **
    2096                 : ** New configuration options may be added in future releases of SQLite.
    2097                 : ** Existing configuration options might be discontinued.  Applications
    2098                 : ** should check the return code from [sqlite3_db_config()] to make sure that
    2099                 : ** the call worked.  ^The [sqlite3_db_config()] interface will return a
    2100                 : ** non-zero [error code] if a discontinued or unsupported configuration option
    2101                 : ** is invoked.
    2102                 : **
    2103                 : ** <dl>
    2104                 : ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
    2105                 : ** <dd> ^This option takes three additional arguments that determine the 
    2106                 : ** [lookaside memory allocator] configuration for the [database connection].
    2107                 : ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
    2108                 : ** pointer to a memory buffer to use for lookaside memory.
    2109                 : ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
    2110                 : ** may be NULL in which case SQLite will allocate the
    2111                 : ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
    2112                 : ** size of each lookaside buffer slot.  ^The third argument is the number of
    2113                 : ** slots.  The size of the buffer in the first argument must be greater than
    2114                 : ** or equal to the product of the second and third arguments.  The buffer
    2115                 : ** must be aligned to an 8-byte boundary.  ^If the second argument to
    2116                 : ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
    2117                 : ** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
    2118                 : ** configuration for a database connection can only be changed when that
    2119                 : ** connection is not currently using lookaside memory, or in other words
    2120                 : ** when the "current value" returned by
    2121                 : ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
    2122                 : ** Any attempt to change the lookaside memory configuration when lookaside
    2123                 : ** memory is in use leaves the configuration unchanged and returns 
    2124                 : ** [SQLITE_BUSY].)^</dd>
    2125                 : **
    2126                 : ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
    2127                 : ** <dd> ^This option is used to enable or disable the enforcement of
    2128                 : ** [foreign key constraints].  There should be two additional arguments.
    2129                 : ** The first argument is an integer which is 0 to disable FK enforcement,
    2130                 : ** positive to enable FK enforcement or negative to leave FK enforcement
    2131                 : ** unchanged.  The second parameter is a pointer to an integer into which
    2132                 : ** is written 0 or 1 to indicate whether FK enforcement is off or on
    2133                 : ** following this call.  The second parameter may be a NULL pointer, in
    2134                 : ** which case the FK enforcement setting is not reported back. </dd>
    2135                 : **
    2136                 : ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
    2137                 : ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
    2138                 : ** There should be two additional arguments.
    2139                 : ** The first argument is an integer which is 0 to disable triggers,
    2140                 : ** positive to enable triggers or negative to leave the setting unchanged.
    2141                 : ** The second parameter is a pointer to an integer into which
    2142                 : ** is written 0 or 1 to indicate whether triggers are disabled or enabled
    2143                 : ** following this call.  The second parameter may be a NULL pointer, in
    2144                 : ** which case the trigger setting is not reported back. </dd>
    2145                 : **
    2146                 : ** </dl>
    2147                 : */
    2148                 : #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
    2149                 : #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
    2150                 : #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
    2151                 : 
    2152                 : 
    2153                 : /*
    2154                 : ** CAPI3REF: Enable Or Disable Extended Result Codes
    2155                 : **
    2156                 : ** ^The sqlite3_extended_result_codes() routine enables or disables the
    2157                 : ** [extended result codes] feature of SQLite. ^The extended result
    2158                 : ** codes are disabled by default for historical compatibility.
    2159                 : */
    2160                 : SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
    2161                 : 
    2162                 : /*
    2163                 : ** CAPI3REF: Last Insert Rowid
    2164                 : **
    2165                 : ** ^Each entry in an SQLite table has a unique 64-bit signed
    2166                 : ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
    2167                 : ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
    2168                 : ** names are not also used by explicitly declared columns. ^If
    2169                 : ** the table has a column of type [INTEGER PRIMARY KEY] then that column
    2170                 : ** is another alias for the rowid.
    2171                 : **
    2172                 : ** ^This routine returns the [rowid] of the most recent
    2173                 : ** successful [INSERT] into the database from the [database connection]
    2174                 : ** in the first argument.  ^As of SQLite version 3.7.7, this routines
    2175                 : ** records the last insert rowid of both ordinary tables and [virtual tables].
    2176                 : ** ^If no successful [INSERT]s
    2177                 : ** have ever occurred on that database connection, zero is returned.
    2178                 : **
    2179                 : ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
    2180                 : ** method, then this routine will return the [rowid] of the inserted
    2181                 : ** row as long as the trigger or virtual table method is running.
    2182                 : ** But once the trigger or virtual table method ends, the value returned 
    2183                 : ** by this routine reverts to what it was before the trigger or virtual
    2184                 : ** table method began.)^
    2185                 : **
    2186                 : ** ^An [INSERT] that fails due to a constraint violation is not a
    2187                 : ** successful [INSERT] and does not change the value returned by this
    2188                 : ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
    2189                 : ** and INSERT OR ABORT make no changes to the return value of this
    2190                 : ** routine when their insertion fails.  ^(When INSERT OR REPLACE
    2191                 : ** encounters a constraint violation, it does not fail.  The
    2192                 : ** INSERT continues to completion after deleting rows that caused
    2193                 : ** the constraint problem so INSERT OR REPLACE will always change
    2194                 : ** the return value of this interface.)^
    2195                 : **
    2196                 : ** ^For the purposes of this routine, an [INSERT] is considered to
    2197                 : ** be successful even if it is subsequently rolled back.
    2198                 : **
    2199                 : ** This function is accessible to SQL statements via the
    2200                 : ** [last_insert_rowid() SQL function].
    2201                 : **
    2202                 : ** If a separate thread performs a new [INSERT] on the same
    2203                 : ** database connection while the [sqlite3_last_insert_rowid()]
    2204                 : ** function is running and thus changes the last insert [rowid],
    2205                 : ** then the value returned by [sqlite3_last_insert_rowid()] is
    2206                 : ** unpredictable and might not equal either the old or the new
    2207                 : ** last insert [rowid].
    2208                 : */
    2209                 : SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
    2210                 : 
    2211                 : /*
    2212                 : ** CAPI3REF: Count The Number Of Rows Modified
    2213                 : **
    2214                 : ** ^This function returns the number of database rows that were changed
    2215                 : ** or inserted or deleted by the most recently completed SQL statement
    2216                 : ** on the [database connection] specified by the first parameter.
    2217                 : ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
    2218                 : ** or [DELETE] statement are counted.  Auxiliary changes caused by
    2219                 : ** triggers or [foreign key actions] are not counted.)^ Use the
    2220                 : ** [sqlite3_total_changes()] function to find the total number of changes
    2221                 : ** including changes caused by triggers and foreign key actions.
    2222                 : **
    2223                 : ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
    2224                 : ** are not counted.  Only real table changes are counted.
    2225                 : **
    2226                 : ** ^(A "row change" is a change to a single row of a single table
    2227                 : ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
    2228                 : ** are changed as side effects of [REPLACE] constraint resolution,
    2229                 : ** rollback, ABORT processing, [DROP TABLE], or by any other
    2230                 : ** mechanisms do not count as direct row changes.)^
    2231                 : **
    2232                 : ** A "trigger context" is a scope of execution that begins and
    2233                 : ** ends with the script of a [CREATE TRIGGER | trigger]. 
    2234                 : ** Most SQL statements are
    2235                 : ** evaluated outside of any trigger.  This is the "top level"
    2236                 : ** trigger context.  If a trigger fires from the top level, a
    2237                 : ** new trigger context is entered for the duration of that one
    2238                 : ** trigger.  Subtriggers create subcontexts for their duration.
    2239                 : **
    2240                 : ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
    2241                 : ** not create a new trigger context.
    2242                 : **
    2243                 : ** ^This function returns the number of direct row changes in the
    2244                 : ** most recent INSERT, UPDATE, or DELETE statement within the same
    2245                 : ** trigger context.
    2246                 : **
    2247                 : ** ^Thus, when called from the top level, this function returns the
    2248                 : ** number of changes in the most recent INSERT, UPDATE, or DELETE
    2249                 : ** that also occurred at the top level.  ^(Within the body of a trigger,
    2250                 : ** the sqlite3_changes() interface can be called to find the number of
    2251                 : ** changes in the most recently completed INSERT, UPDATE, or DELETE
    2252                 : ** statement within the body of the same trigger.
    2253                 : ** However, the number returned does not include changes
    2254                 : ** caused by subtriggers since those have their own context.)^
    2255                 : **
    2256                 : ** See also the [sqlite3_total_changes()] interface, the
    2257                 : ** [count_changes pragma], and the [changes() SQL function].
    2258                 : **
    2259                 : ** If a separate thread makes changes on the same database connection
    2260                 : ** while [sqlite3_changes()] is running then the value returned
    2261                 : ** is unpredictable and not meaningful.
    2262                 : */
    2263                 : SQLITE_API int sqlite3_changes(sqlite3*);
    2264                 : 
    2265                 : /*
    2266                 : ** CAPI3REF: Total Number Of Rows Modified
    2267                 : **
    2268                 : ** ^This function returns the number of row changes caused by [INSERT],
    2269                 : ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
    2270                 : ** ^(The count returned by sqlite3_total_changes() includes all changes
    2271                 : ** from all [CREATE TRIGGER | trigger] contexts and changes made by
    2272                 : ** [foreign key actions]. However,
    2273                 : ** the count does not include changes used to implement [REPLACE] constraints,
    2274                 : ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
    2275                 : ** count does not include rows of views that fire an [INSTEAD OF trigger],
    2276                 : ** though if the INSTEAD OF trigger makes changes of its own, those changes 
    2277                 : ** are counted.)^
    2278                 : ** ^The sqlite3_total_changes() function counts the changes as soon as
    2279                 : ** the statement that makes them is completed (when the statement handle
    2280                 : ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
    2281                 : **
    2282                 : ** See also the [sqlite3_changes()] interface, the
    2283                 : ** [count_changes pragma], and the [total_changes() SQL function].
    2284                 : **
    2285                 : ** If a separate thread makes changes on the same database connection
    2286                 : ** while [sqlite3_total_changes()] is running then the value
    2287                 : ** returned is unpredictable and not meaningful.
    2288                 : */
    2289                 : SQLITE_API int sqlite3_total_changes(sqlite3*);
    2290                 : 
    2291                 : /*
    2292                 : ** CAPI3REF: Interrupt A Long-Running Query
    2293                 : **
    2294                 : ** ^This function causes any pending database operation to abort and
    2295                 : ** return at its earliest opportunity. This routine is typically
    2296                 : ** called in response to a user action such as pressing "Cancel"
    2297                 : ** or Ctrl-C where the user wants a long query operation to halt
    2298                 : ** immediately.
    2299                 : **
    2300                 : ** ^It is safe to call this routine from a thread different from the
    2301                 : ** thread that is currently running the database operation.  But it
    2302                 : ** is not safe to call this routine with a [database connection] that
    2303                 : ** is closed or might close before sqlite3_interrupt() returns.
    2304                 : **
    2305                 : ** ^If an SQL operation is very nearly finished at the time when
    2306                 : ** sqlite3_interrupt() is called, then it might not have an opportunity
    2307                 : ** to be interrupted and might continue to completion.
    2308                 : **
    2309                 : ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
    2310                 : ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
    2311                 : ** that is inside an explicit transaction, then the entire transaction
    2312                 : ** will be rolled back automatically.
    2313                 : **
    2314                 : ** ^The sqlite3_interrupt(D) call is in effect until all currently running
    2315                 : ** SQL statements on [database connection] D complete.  ^Any new SQL statements
    2316                 : ** that are started after the sqlite3_interrupt() call and before the 
    2317                 : ** running statements reaches zero are interrupted as if they had been
    2318                 : ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
    2319                 : ** that are started after the running statement count reaches zero are
    2320                 : ** not effected by the sqlite3_interrupt().
    2321                 : ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
    2322                 : ** SQL statements is a no-op and has no effect on SQL statements
    2323                 : ** that are started after the sqlite3_interrupt() call returns.
    2324                 : **
    2325                 : ** If the database connection closes while [sqlite3_interrupt()]
    2326                 : ** is running then bad things will likely happen.
    2327                 : */
    2328                 : SQLITE_API void sqlite3_interrupt(sqlite3*);
    2329                 : 
    2330                 : /*
    2331                 : ** CAPI3REF: Determine If An SQL Statement Is Complete
    2332                 : **
    2333                 : ** These routines are useful during command-line input to determine if the
    2334                 : ** currently entered text seems to form a complete SQL statement or
    2335                 : ** if additional input is needed before sending the text into
    2336                 : ** SQLite for parsing.  ^These routines return 1 if the input string
    2337                 : ** appears to be a complete SQL statement.  ^A statement is judged to be
    2338                 : ** complete if it ends with a semicolon token and is not a prefix of a
    2339                 : ** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
    2340                 : ** string literals or quoted identifier names or comments are not
    2341                 : ** independent tokens (they are part of the token in which they are
    2342                 : ** embedded) and thus do not count as a statement terminator.  ^Whitespace
    2343                 : ** and comments that follow the final semicolon are ignored.
    2344                 : **
    2345                 : ** ^These routines return 0 if the statement is incomplete.  ^If a
    2346                 : ** memory allocation fails, then SQLITE_NOMEM is returned.
    2347                 : **
    2348                 : ** ^These routines do not parse the SQL statements thus
    2349                 : ** will not detect syntactically incorrect SQL.
    2350                 : **
    2351                 : ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior 
    2352                 : ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
    2353                 : ** automatically by sqlite3_complete16().  If that initialization fails,
    2354                 : ** then the return value from sqlite3_complete16() will be non-zero
    2355                 : ** regardless of whether or not the input SQL is complete.)^
    2356                 : **
    2357                 : ** The input to [sqlite3_complete()] must be a zero-terminated
    2358                 : ** UTF-8 string.
    2359                 : **
    2360                 : ** The input to [sqlite3_complete16()] must be a zero-terminated
    2361                 : ** UTF-16 string in native byte order.
    2362                 : */
    2363                 : SQLITE_API int sqlite3_complete(const char *sql);
    2364                 : SQLITE_API int sqlite3_complete16(const void *sql);
    2365                 : 
    2366                 : /*
    2367                 : ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
    2368                 : **
    2369                 : ** ^This routine sets a callback function that might be invoked whenever
    2370                 : ** an attempt is made to open a database table that another thread
    2371                 : ** or process has locked.
    2372                 : **
    2373                 : ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
    2374                 : ** is returned immediately upon encountering the lock.  ^If the busy callback
    2375                 : ** is not NULL, then the callback might be invoked with two arguments.
    2376                 : **
    2377                 : ** ^The first argument to the busy handler is a copy of the void* pointer which
    2378                 : ** is the third argument to sqlite3_busy_handler().  ^The second argument to
    2379                 : ** the busy handler callback is the number of times that the busy handler has
    2380                 : ** been invoked for this locking event.  ^If the
    2381                 : ** busy callback returns 0, then no additional attempts are made to
    2382                 : ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
    2383                 : ** ^If the callback returns non-zero, then another attempt
    2384                 : ** is made to open the database for reading and the cycle repeats.
    2385                 : **
    2386                 : ** The presence of a busy handler does not guarantee that it will be invoked
    2387                 : ** when there is lock contention. ^If SQLite determines that invoking the busy
    2388                 : ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
    2389                 : ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
    2390                 : ** Consider a scenario where one process is holding a read lock that
    2391                 : ** it is trying to promote to a reserved lock and
    2392                 : ** a second process is holding a reserved lock that it is trying
    2393                 : ** to promote to an exclusive lock.  The first process cannot proceed
    2394                 : ** because it is blocked by the second and the second process cannot
    2395                 : ** proceed because it is blocked by the first.  If both processes
    2396                 : ** invoke the busy handlers, neither will make any progress.  Therefore,
    2397                 : ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
    2398                 : ** will induce the first process to release its read lock and allow
    2399                 : ** the second process to proceed.
    2400                 : **
    2401                 : ** ^The default busy callback is NULL.
    2402                 : **
    2403                 : ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
    2404                 : ** when SQLite is in the middle of a large transaction where all the
    2405                 : ** changes will not fit into the in-memory cache.  SQLite will
    2406                 : ** already hold a RESERVED lock on the database file, but it needs
    2407                 : ** to promote this lock to EXCLUSIVE so that it can spill cache
    2408                 : ** pages into the database file without harm to concurrent
    2409                 : ** readers.  ^If it is unable to promote the lock, then the in-memory
    2410                 : ** cache will be left in an inconsistent state and so the error
    2411                 : ** code is promoted from the relatively benign [SQLITE_BUSY] to
    2412                 : ** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
    2413                 : ** forces an automatic rollback of the changes.  See the
    2414                 : ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
    2415                 : ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
    2416                 : ** this is important.
    2417                 : **
    2418                 : ** ^(There can only be a single busy handler defined for each
    2419                 : ** [database connection].  Setting a new busy handler clears any
    2420                 : ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
    2421                 : ** will also set or clear the busy handler.
    2422                 : **
    2423                 : ** The busy callback should not take any actions which modify the
    2424                 : ** database connection that invoked the busy handler.  Any such actions
    2425                 : ** result in undefined behavior.
    2426                 : ** 
    2427                 : ** A busy handler must not close the database connection
    2428                 : ** or [prepared statement] that invoked the busy handler.
    2429                 : */
    2430                 : SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
    2431                 : 
    2432                 : /*
    2433                 : ** CAPI3REF: Set A Busy Timeout
    2434                 : **
    2435                 : ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
    2436                 : ** for a specified amount of time when a table is locked.  ^The handler
    2437                 : ** will sleep multiple times until at least "ms" milliseconds of sleeping
    2438                 : ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
    2439                 : ** the handler returns 0 which causes [sqlite3_step()] to return
    2440                 : ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
    2441                 : **
    2442                 : ** ^Calling this routine with an argument less than or equal to zero
    2443                 : ** turns off all busy handlers.
    2444                 : **
    2445                 : ** ^(There can only be a single busy handler for a particular
    2446                 : ** [database connection] any any given moment.  If another busy handler
    2447                 : ** was defined  (using [sqlite3_busy_handler()]) prior to calling
    2448                 : ** this routine, that other busy handler is cleared.)^
    2449                 : */
    2450                 : SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
    2451                 : 
    2452                 : /*
    2453                 : ** CAPI3REF: Convenience Routines For Running Queries
    2454                 : **
    2455                 : ** This is a legacy interface that is preserved for backwards compatibility.
    2456                 : ** Use of this interface is not recommended.
    2457                 : **
    2458                 : ** Definition: A <b>result table</b> is memory data structure created by the
    2459                 : ** [sqlite3_get_table()] interface.  A result table records the
    2460                 : ** complete query results from one or more queries.
    2461                 : **
    2462                 : ** The table conceptually has a number of rows and columns.  But
    2463                 : ** these numbers are not part of the result table itself.  These
    2464                 : ** numbers are obtained separately.  Let N be the number of rows
    2465                 : ** and M be the number of columns.
    2466                 : **
    2467                 : ** A result table is an array of pointers to zero-terminated UTF-8 strings.
    2468                 : ** There are (N+1)*M elements in the array.  The first M pointers point
    2469                 : ** to zero-terminated strings that  contain the names of the columns.
    2470                 : ** The remaining entries all point to query results.  NULL values result
    2471                 : ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
    2472                 : ** string representation as returned by [sqlite3_column_text()].
    2473                 : **
    2474                 : ** A result table might consist of one or more memory allocations.
    2475                 : ** It is not safe to pass a result table directly to [sqlite3_free()].
    2476                 : ** A result table should be deallocated using [sqlite3_free_table()].
    2477                 : **
    2478                 : ** ^(As an example of the result table format, suppose a query result
    2479                 : ** is as follows:
    2480                 : **
    2481                 : ** <blockquote><pre>
    2482                 : **        Name        | Age
    2483                 : **        -----------------------
    2484                 : **        Alice       | 43
    2485                 : **        Bob         | 28
    2486                 : **        Cindy       | 21
    2487                 : ** </pre></blockquote>
    2488                 : **
    2489                 : ** There are two column (M==2) and three rows (N==3).  Thus the
    2490                 : ** result table has 8 entries.  Suppose the result table is stored
    2491                 : ** in an array names azResult.  Then azResult holds this content:
    2492                 : **
    2493                 : ** <blockquote><pre>
    2494                 : **        azResult&#91;0] = "Name";
    2495                 : **        azResult&#91;1] = "Age";
    2496                 : **        azResult&#91;2] = "Alice";
    2497                 : **        azResult&#91;3] = "43";
    2498                 : **        azResult&#91;4] = "Bob";
    2499                 : **        azResult&#91;5] = "28";
    2500                 : **        azResult&#91;6] = "Cindy";
    2501                 : **        azResult&#91;7] = "21";
    2502                 : ** </pre></blockquote>)^
    2503                 : **
    2504                 : ** ^The sqlite3_get_table() function evaluates one or more
    2505                 : ** semicolon-separated SQL statements in the zero-terminated UTF-8
    2506                 : ** string of its 2nd parameter and returns a result table to the
    2507                 : ** pointer given in its 3rd parameter.
    2508                 : **
    2509                 : ** After the application has finished with the result from sqlite3_get_table(),
    2510                 : ** it must pass the result table pointer to sqlite3_free_table() in order to
    2511                 : ** release the memory that was malloced.  Because of the way the
    2512                 : ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
    2513                 : ** function must not try to call [sqlite3_free()] directly.  Only
    2514                 : ** [sqlite3_free_table()] is able to release the memory properly and safely.
    2515                 : **
    2516                 : ** The sqlite3_get_table() interface is implemented as a wrapper around
    2517                 : ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
    2518                 : ** to any internal data structures of SQLite.  It uses only the public
    2519                 : ** interface defined here.  As a consequence, errors that occur in the
    2520                 : ** wrapper layer outside of the internal [sqlite3_exec()] call are not
    2521                 : ** reflected in subsequent calls to [sqlite3_errcode()] or
    2522                 : ** [sqlite3_errmsg()].
    2523                 : */
    2524                 : SQLITE_API int sqlite3_get_table(
    2525                 :   sqlite3 *db,          /* An open database */
    2526                 :   const char *zSql,     /* SQL to be evaluated */
    2527                 :   char ***pazResult,    /* Results of the query */
    2528                 :   int *pnRow,           /* Number of result rows written here */
    2529                 :   int *pnColumn,        /* Number of result columns written here */
    2530                 :   char **pzErrmsg       /* Error msg written here */
    2531                 : );
    2532                 : SQLITE_API void sqlite3_free_table(char **result);
    2533                 : 
    2534                 : /*
    2535                 : ** CAPI3REF: Formatted String Printing Functions
    2536                 : **
    2537                 : ** These routines are work-alikes of the "printf()" family of functions
    2538                 : ** from the standard C library.
    2539                 : **
    2540                 : ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
    2541                 : ** results into memory obtained from [sqlite3_malloc()].
    2542                 : ** The strings returned by these two routines should be
    2543                 : ** released by [sqlite3_free()].  ^Both routines return a
    2544                 : ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
    2545                 : ** memory to hold the resulting string.
    2546                 : **
    2547                 : ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
    2548                 : ** the standard C library.  The result is written into the
    2549                 : ** buffer supplied as the second parameter whose size is given by
    2550                 : ** the first parameter. Note that the order of the
    2551                 : ** first two parameters is reversed from snprintf().)^  This is an
    2552                 : ** historical accident that cannot be fixed without breaking
    2553                 : ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
    2554                 : ** returns a pointer to its buffer instead of the number of
    2555                 : ** characters actually written into the buffer.)^  We admit that
    2556                 : ** the number of characters written would be a more useful return
    2557                 : ** value but we cannot change the implementation of sqlite3_snprintf()
    2558                 : ** now without breaking compatibility.
    2559                 : **
    2560                 : ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
    2561                 : ** guarantees that the buffer is always zero-terminated.  ^The first
    2562                 : ** parameter "n" is the total size of the buffer, including space for
    2563                 : ** the zero terminator.  So the longest string that can be completely
    2564                 : ** written will be n-1 characters.
    2565                 : **
    2566                 : ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
    2567                 : **
    2568                 : ** These routines all implement some additional formatting
    2569                 : ** options that are useful for constructing SQL statements.
    2570                 : ** All of the usual printf() formatting options apply.  In addition, there
    2571                 : ** is are "%q", "%Q", and "%z" options.
    2572                 : **
    2573                 : ** ^(The %q option works like %s in that it substitutes a nul-terminated
    2574                 : ** string from the argument list.  But %q also doubles every '\'' character.
    2575                 : ** %q is designed for use inside a string literal.)^  By doubling each '\''
    2576                 : ** character it escapes that character and allows it to be inserted into
    2577                 : ** the string.
    2578                 : **
    2579                 : ** For example, assume the string variable zText contains text as follows:
    2580                 : **
    2581                 : ** <blockquote><pre>
    2582                 : **  char *zText = "It's a happy day!";
    2583                 : ** </pre></blockquote>
    2584                 : **
    2585                 : ** One can use this text in an SQL statement as follows:
    2586                 : **
    2587                 : ** <blockquote><pre>
    2588                 : **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    2589                 : **  sqlite3_exec(db, zSQL, 0, 0, 0);
    2590                 : **  sqlite3_free(zSQL);
    2591                 : ** </pre></blockquote>
    2592                 : **
    2593                 : ** Because the %q format string is used, the '\'' character in zText
    2594                 : ** is escaped and the SQL generated is as follows:
    2595                 : **
    2596                 : ** <blockquote><pre>
    2597                 : **  INSERT INTO table1 VALUES('It''s a happy day!')
    2598                 : ** </pre></blockquote>
    2599                 : **
    2600                 : ** This is correct.  Had we used %s instead of %q, the generated SQL
    2601                 : ** would have looked like this:
    2602                 : **
    2603                 : ** <blockquote><pre>
    2604                 : **  INSERT INTO table1 VALUES('It's a happy day!');
    2605                 : ** </pre></blockquote>
    2606                 : **
    2607                 : ** This second example is an SQL syntax error.  As a general rule you should
    2608                 : ** always use %q instead of %s when inserting text into a string literal.
    2609                 : **
    2610                 : ** ^(The %Q option works like %q except it also adds single quotes around
    2611                 : ** the outside of the total string.  Additionally, if the parameter in the
    2612                 : ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
    2613                 : ** single quotes).)^  So, for example, one could say:
    2614                 : **
    2615                 : ** <blockquote><pre>
    2616                 : **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    2617                 : **  sqlite3_exec(db, zSQL, 0, 0, 0);
    2618                 : **  sqlite3_free(zSQL);
    2619                 : ** </pre></blockquote>
    2620                 : **
    2621                 : ** The code above will render a correct SQL statement in the zSQL
    2622                 : ** variable even if the zText variable is a NULL pointer.
    2623                 : **
    2624                 : ** ^(The "%z" formatting option works like "%s" but with the
    2625                 : ** addition that after the string has been read and copied into
    2626                 : ** the result, [sqlite3_free()] is called on the input string.)^
    2627                 : */
    2628                 : SQLITE_API char *sqlite3_mprintf(const char*,...);
    2629                 : SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
    2630                 : SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
    2631                 : SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
    2632                 : 
    2633                 : /*
    2634                 : ** CAPI3REF: Memory Allocation Subsystem
    2635                 : **
    2636                 : ** The SQLite core uses these three routines for all of its own
    2637                 : ** internal memory allocation needs. "Core" in the previous sentence
    2638                 : ** does not include operating-system specific VFS implementation.  The
    2639                 : ** Windows VFS uses native malloc() and free() for some operations.
    2640                 : **
    2641                 : ** ^The sqlite3_malloc() routine returns a pointer to a block
    2642                 : ** of memory at least N bytes in length, where N is the parameter.
    2643                 : ** ^If sqlite3_malloc() is unable to obtain sufficient free
    2644                 : ** memory, it returns a NULL pointer.  ^If the parameter N to
    2645                 : ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
    2646                 : ** a NULL pointer.
    2647                 : **
    2648                 : ** ^Calling sqlite3_free() with a pointer previously returned
    2649                 : ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
    2650                 : ** that it might be reused.  ^The sqlite3_free() routine is
    2651                 : ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
    2652                 : ** to sqlite3_free() is harmless.  After being freed, memory
    2653                 : ** should neither be read nor written.  Even reading previously freed
    2654                 : ** memory might result in a segmentation fault or other severe error.
    2655                 : ** Memory corruption, a segmentation fault, or other severe error
    2656                 : ** might result if sqlite3_free() is called with a non-NULL pointer that
    2657                 : ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
    2658                 : **
    2659                 : ** ^(The sqlite3_realloc() interface attempts to resize a
    2660                 : ** prior memory allocation to be at least N bytes, where N is the
    2661                 : ** second parameter.  The memory allocation to be resized is the first
    2662                 : ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
    2663                 : ** is a NULL pointer then its behavior is identical to calling
    2664                 : ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
    2665                 : ** ^If the second parameter to sqlite3_realloc() is zero or
    2666                 : ** negative then the behavior is exactly the same as calling
    2667                 : ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
    2668                 : ** ^sqlite3_realloc() returns a pointer to a memory allocation
    2669                 : ** of at least N bytes in size or NULL if sufficient memory is unavailable.
    2670                 : ** ^If M is the size of the prior allocation, then min(N,M) bytes
    2671                 : ** of the prior allocation are copied into the beginning of buffer returned
    2672                 : ** by sqlite3_realloc() and the prior allocation is freed.
    2673                 : ** ^If sqlite3_realloc() returns NULL, then the prior allocation
    2674                 : ** is not freed.
    2675                 : **
    2676                 : ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
    2677                 : ** is always aligned to at least an 8 byte boundary, or to a
    2678                 : ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
    2679                 : ** option is used.
    2680                 : **
    2681                 : ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
    2682                 : ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
    2683                 : ** implementation of these routines to be omitted.  That capability
    2684                 : ** is no longer provided.  Only built-in memory allocators can be used.
    2685                 : **
    2686                 : ** The Windows OS interface layer calls
    2687                 : ** the system malloc() and free() directly when converting
    2688                 : ** filenames between the UTF-8 encoding used by SQLite
    2689                 : ** and whatever filename encoding is used by the particular Windows
    2690                 : ** installation.  Memory allocation errors are detected, but
    2691                 : ** they are reported back as [SQLITE_CANTOPEN] or
    2692                 : ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
    2693                 : **
    2694                 : ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
    2695                 : ** must be either NULL or else pointers obtained from a prior
    2696                 : ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
    2697                 : ** not yet been released.
    2698                 : **
    2699                 : ** The application must not read or write any part of
    2700                 : ** a block of memory after it has been released using
    2701                 : ** [sqlite3_free()] or [sqlite3_realloc()].
    2702                 : */
    2703                 : SQLITE_API void *sqlite3_malloc(int);
    2704                 : SQLITE_API void *sqlite3_realloc(void*, int);
    2705                 : SQLITE_API void sqlite3_free(void*);
    2706                 : 
    2707                 : /*
    2708                 : ** CAPI3REF: Memory Allocator Statistics
    2709                 : **
    2710                 : ** SQLite provides these two interfaces for reporting on the status
    2711                 : ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
    2712                 : ** routines, which form the built-in memory allocation subsystem.
    2713                 : **
    2714                 : ** ^The [sqlite3_memory_used()] routine returns the number of bytes
    2715                 : ** of memory currently outstanding (malloced but not freed).
    2716                 : ** ^The [sqlite3_memory_highwater()] routine returns the maximum
    2717                 : ** value of [sqlite3_memory_used()] since the high-water mark
    2718                 : ** was last reset.  ^The values returned by [sqlite3_memory_used()] and
    2719                 : ** [sqlite3_memory_highwater()] include any overhead
    2720                 : ** added by SQLite in its implementation of [sqlite3_malloc()],
    2721                 : ** but not overhead added by the any underlying system library
    2722                 : ** routines that [sqlite3_malloc()] may call.
    2723                 : **
    2724                 : ** ^The memory high-water mark is reset to the current value of
    2725                 : ** [sqlite3_memory_used()] if and only if the parameter to
    2726                 : ** [sqlite3_memory_highwater()] is true.  ^The value returned
    2727                 : ** by [sqlite3_memory_highwater(1)] is the high-water mark
    2728                 : ** prior to the reset.
    2729                 : */
    2730                 : SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
    2731                 : SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
    2732                 : 
    2733                 : /*
    2734                 : ** CAPI3REF: Pseudo-Random Number Generator
    2735                 : **
    2736                 : ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
    2737                 : ** select random [ROWID | ROWIDs] when inserting new records into a table that
    2738                 : ** already uses the largest possible [ROWID].  The PRNG is also used for
    2739                 : ** the build-in random() and randomblob() SQL functions.  This interface allows
    2740                 : ** applications to access the same PRNG for other purposes.
    2741                 : **
    2742                 : ** ^A call to this routine stores N bytes of randomness into buffer P.
    2743                 : **
    2744                 : ** ^The first time this routine is invoked (either internally or by
    2745                 : ** the application) the PRNG is seeded using randomness obtained
    2746                 : ** from the xRandomness method of the default [sqlite3_vfs] object.
    2747                 : ** ^On all subsequent invocations, the pseudo-randomness is generated
    2748                 : ** internally and without recourse to the [sqlite3_vfs] xRandomness
    2749                 : ** method.
    2750                 : */
    2751                 : SQLITE_API void sqlite3_randomness(int N, void *P);
    2752                 : 
    2753                 : /*
    2754                 : ** CAPI3REF: Compile-Time Authorization Callbacks
    2755                 : **
    2756                 : ** ^This routine registers an authorizer callback with a particular
    2757                 : ** [database connection], supplied in the first argument.
    2758                 : ** ^The authorizer callback is invoked as SQL statements are being compiled
    2759                 : ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
    2760                 : ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
    2761                 : ** points during the compilation process, as logic is being created
    2762                 : ** to perform various actions, the authorizer callback is invoked to
    2763                 : ** see if those actions are allowed.  ^The authorizer callback should
    2764                 : ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
    2765                 : ** specific action but allow the SQL statement to continue to be
    2766                 : ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
    2767                 : ** rejected with an error.  ^If the authorizer callback returns
    2768                 : ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
    2769                 : ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
    2770                 : ** the authorizer will fail with an error message.
    2771                 : **
    2772                 : ** When the callback returns [SQLITE_OK], that means the operation
    2773                 : ** requested is ok.  ^When the callback returns [SQLITE_DENY], the
    2774                 : ** [sqlite3_prepare_v2()] or equivalent call that triggered the
    2775                 : ** authorizer will fail with an error message explaining that
    2776                 : ** access is denied. 
    2777                 : **
    2778                 : ** ^The first parameter to the authorizer callback is a copy of the third
    2779                 : ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
    2780                 : ** to the callback is an integer [SQLITE_COPY | action code] that specifies
    2781                 : ** the particular action to be authorized. ^The third through sixth parameters
    2782                 : ** to the callback are zero-terminated strings that contain additional
    2783                 : ** details about the action to be authorized.
    2784                 : **
    2785                 : ** ^If the action code is [SQLITE_READ]
    2786                 : ** and the callback returns [SQLITE_IGNORE] then the
    2787                 : ** [prepared statement] statement is constructed to substitute
    2788                 : ** a NULL value in place of the table column that would have
    2789                 : ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
    2790                 : ** return can be used to deny an untrusted user access to individual
    2791                 : ** columns of a table.
    2792                 : ** ^If the action code is [SQLITE_DELETE] and the callback returns
    2793                 : ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
    2794                 : ** [truncate optimization] is disabled and all rows are deleted individually.
    2795                 : **
    2796                 : ** An authorizer is used when [sqlite3_prepare | preparing]
    2797                 : ** SQL statements from an untrusted source, to ensure that the SQL statements
    2798                 : ** do not try to access data they are not allowed to see, or that they do not
    2799                 : ** try to execute malicious statements that damage the database.  For
    2800                 : ** example, an application may allow a user to enter arbitrary
    2801                 : ** SQL queries for evaluation by a database.  But the application does
    2802                 : ** not want the user to be able to make arbitrary changes to the
    2803                 : ** database.  An authorizer could then be put in place while the
    2804                 : ** user-entered SQL is being [sqlite3_prepare | prepared] that
    2805                 : ** disallows everything except [SELECT] statements.
    2806                 : **
    2807                 : ** Applications that need to process SQL from untrusted sources
    2808                 : ** might also consider lowering resource limits using [sqlite3_limit()]
    2809                 : ** and limiting database size using the [max_page_count] [PRAGMA]
    2810                 : ** in addition to using an authorizer.
    2811                 : **
    2812                 : ** ^(Only a single authorizer can be in place on a database connection
    2813                 : ** at a time.  Each call to sqlite3_set_authorizer overrides the
    2814                 : ** previous call.)^  ^Disable the authorizer by installing a NULL callback.
    2815                 : ** The authorizer is disabled by default.
    2816                 : **
    2817                 : ** The authorizer callback must not do anything that will modify
    2818                 : ** the database connection that invoked the authorizer callback.
    2819                 : ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
    2820                 : ** database connections for the meaning of "modify" in this paragraph.
    2821                 : **
    2822                 : ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
    2823                 : ** statement might be re-prepared during [sqlite3_step()] due to a 
    2824                 : ** schema change.  Hence, the application should ensure that the
    2825                 : ** correct authorizer callback remains in place during the [sqlite3_step()].
    2826                 : **
    2827                 : ** ^Note that the authorizer callback is invoked only during
    2828                 : ** [sqlite3_prepare()] or its variants.  Authorization is not
    2829                 : ** performed during statement evaluation in [sqlite3_step()], unless
    2830                 : ** as stated in the previous paragraph, sqlite3_step() invokes
    2831                 : ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
    2832                 : */
    2833                 : SQLITE_API int sqlite3_set_authorizer(
    2834                 :   sqlite3*,
    2835                 :   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
    2836                 :   void *pUserData
    2837                 : );
    2838                 : 
    2839                 : /*
    2840                 : ** CAPI3REF: Authorizer Return Codes
    2841                 : **
    2842                 : ** The [sqlite3_set_authorizer | authorizer callback function] must
    2843                 : ** return either [SQLITE_OK] or one of these two constants in order
    2844                 : ** to signal SQLite whether or not the action is permitted.  See the
    2845                 : ** [sqlite3_set_authorizer | authorizer documentation] for additional
    2846                 : ** information.
    2847                 : **
    2848                 : ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
    2849                 : ** from the [sqlite3_vtab_on_conflict()] interface.
    2850                 : */
    2851                 : #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
    2852                 : #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
    2853                 : 
    2854                 : /*
    2855                 : ** CAPI3REF: Authorizer Action Codes
    2856                 : **
    2857                 : ** The [sqlite3_set_authorizer()] interface registers a callback function
    2858                 : ** that is invoked to authorize certain SQL statement actions.  The
    2859                 : ** second parameter to the callback is an integer code that specifies
    2860                 : ** what action is being authorized.  These are the integer action codes that
    2861                 : ** the authorizer callback may be passed.
    2862                 : **
    2863                 : ** These action code values signify what kind of operation is to be
    2864                 : ** authorized.  The 3rd and 4th parameters to the authorization
    2865                 : ** callback function will be parameters or NULL depending on which of these
    2866                 : ** codes is used as the second parameter.  ^(The 5th parameter to the
    2867                 : ** authorizer callback is the name of the database ("main", "temp",
    2868                 : ** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
    2869                 : ** is the name of the inner-most trigger or view that is responsible for
    2870                 : ** the access attempt or NULL if this access attempt is directly from
    2871                 : ** top-level SQL code.
    2872                 : */
    2873                 : /******************************************* 3rd ************ 4th ***********/
    2874                 : #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
    2875                 : #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
    2876                 : #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
    2877                 : #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
    2878                 : #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
    2879                 : #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
    2880                 : #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
    2881                 : #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
    2882                 : #define SQLITE_DELETE                9   /* Table Name      NULL            */
    2883                 : #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
    2884                 : #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
    2885                 : #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
    2886                 : #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
    2887                 : #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
    2888                 : #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
    2889                 : #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
    2890                 : #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
    2891                 : #define SQLITE_INSERT               18   /* Table Name      NULL            */
    2892                 : #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
    2893                 : #define SQLITE_READ                 20   /* Table Name      Column Name     */
    2894                 : #define SQLITE_SELECT               21   /* NULL            NULL            */
    2895                 : #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
    2896                 : #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
    2897                 : #define SQLITE_ATTACH               24   /* Filename        NULL            */
    2898                 : #define SQLITE_DETACH               25   /* Database Name   NULL            */
    2899                 : #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
    2900                 : #define SQLITE_REINDEX              27   /* Index Name      NULL            */
    2901                 : #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
    2902                 : #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
    2903                 : #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
    2904                 : #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
    2905                 : #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
    2906                 : #define SQLITE_COPY                  0   /* No longer used */
    2907                 : 
    2908                 : /*
    2909                 : ** CAPI3REF: Tracing And Profiling Functions
    2910                 : **
    2911                 : ** These routines register callback functions that can be used for
    2912                 : ** tracing and profiling the execution of SQL statements.
    2913                 : **
    2914                 : ** ^The callback function registered by sqlite3_trace() is invoked at
    2915                 : ** various times when an SQL statement is being run by [sqlite3_step()].
    2916                 : ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
    2917                 : ** SQL statement text as the statement first begins executing.
    2918                 : ** ^(Additional sqlite3_trace() callbacks might occur
    2919                 : ** as each triggered subprogram is entered.  The callbacks for triggers
    2920                 : ** contain a UTF-8 SQL comment that identifies the trigger.)^
    2921                 : **
    2922                 : ** ^The callback function registered by sqlite3_profile() is invoked
    2923                 : ** as each SQL statement finishes.  ^The profile callback contains
    2924                 : ** the original statement text and an estimate of wall-clock time
    2925                 : ** of how long that statement took to run.  ^The profile callback
    2926                 : ** time is in units of nanoseconds, however the current implementation
    2927                 : ** is only capable of millisecond resolution so the six least significant
    2928                 : ** digits in the time are meaningless.  Future versions of SQLite
    2929                 : ** might provide greater resolution on the profiler callback.  The
    2930                 : ** sqlite3_profile() function is considered experimental and is
    2931                 : ** subject to change in future versions of SQLite.
    2932                 : */
    2933                 : SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
    2934                 : SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
    2935                 :    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
    2936                 : 
    2937                 : /*
    2938                 : ** CAPI3REF: Query Progress Callbacks
    2939                 : **
    2940                 : ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
    2941                 : ** function X to be invoked periodically during long running calls to
    2942                 : ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
    2943                 : ** database connection D.  An example use for this
    2944                 : ** interface is to keep a GUI updated during a large query.
    2945                 : **
    2946                 : ** ^The parameter P is passed through as the only parameter to the 
    2947                 : ** callback function X.  ^The parameter N is the number of 
    2948                 : ** [virtual machine instructions] that are evaluated between successive
    2949                 : ** invocations of the callback X.
    2950                 : **
    2951                 : ** ^Only a single progress handler may be defined at one time per
    2952                 : ** [database connection]; setting a new progress handler cancels the
    2953                 : ** old one.  ^Setting parameter X to NULL disables the progress handler.
    2954                 : ** ^The progress handler is also disabled by setting N to a value less
    2955                 : ** than 1.
    2956                 : **
    2957                 : ** ^If the progress callback returns non-zero, the operation is
    2958                 : ** interrupted.  This feature can be used to implement a
    2959                 : ** "Cancel" button on a GUI progress dialog box.
    2960                 : **
    2961                 : ** The progress handler callback must not do anything that will modify
    2962                 : ** the database connection that invoked the progress handler.
    2963                 : ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
    2964                 : ** database connections for the meaning of "modify" in this paragraph.
    2965                 : **
    2966                 : */
    2967                 : SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
    2968                 : 
    2969                 : /*
    2970                 : ** CAPI3REF: Opening A New Database Connection
    2971                 : **
    2972                 : ** ^These routines open an SQLite database file as specified by the 
    2973                 : ** filename argument. ^The filename argument is interpreted as UTF-8 for
    2974                 : ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
    2975                 : ** order for sqlite3_open16(). ^(A [database connection] handle is usually
    2976                 : ** returned in *ppDb, even if an error occurs.  The only exception is that
    2977                 : ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
    2978                 : ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
    2979                 : ** object.)^ ^(If the database is opened (and/or created) successfully, then
    2980                 : ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
    2981                 : ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
    2982                 : ** an English language description of the error following a failure of any
    2983                 : ** of the sqlite3_open() routines.
    2984                 : **
    2985                 : ** ^The default encoding for the database will be UTF-8 if
    2986                 : ** sqlite3_open() or sqlite3_open_v2() is called and
    2987                 : ** UTF-16 in the native byte order if sqlite3_open16() is used.
    2988                 : **
    2989                 : ** Whether or not an error occurs when it is opened, resources
    2990                 : ** associated with the [database connection] handle should be released by
    2991                 : ** passing it to [sqlite3_close()] when it is no longer required.
    2992                 : **
    2993                 : ** The sqlite3_open_v2() interface works like sqlite3_open()
    2994                 : ** except that it accepts two additional parameters for additional control
    2995                 : ** over the new database connection.  ^(The flags parameter to
    2996                 : ** sqlite3_open_v2() can take one of
    2997                 : ** the following three values, optionally combined with the 
    2998                 : ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
    2999                 : ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
    3000                 : **
    3001                 : ** <dl>
    3002                 : ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
    3003                 : ** <dd>The database is opened in read-only mode.  If the database does not
    3004                 : ** already exist, an error is returned.</dd>)^
    3005                 : **
    3006                 : ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
    3007                 : ** <dd>The database is opened for reading and writing if possible, or reading
    3008                 : ** only if the file is write protected by the operating system.  In either
    3009                 : ** case the database must already exist, otherwise an error is returned.</dd>)^
    3010                 : **
    3011                 : ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
    3012                 : ** <dd>The database is opened for reading and writing, and is created if
    3013                 : ** it does not already exist. This is the behavior that is always used for
    3014                 : ** sqlite3_open() and sqlite3_open16().</dd>)^
    3015                 : ** </dl>
    3016                 : **
    3017                 : ** If the 3rd parameter to sqlite3_open_v2() is not one of the
    3018                 : ** combinations shown above optionally combined with other
    3019                 : ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
    3020                 : ** then the behavior is undefined.
    3021                 : **
    3022                 : ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
    3023                 : ** opens in the multi-thread [threading mode] as long as the single-thread
    3024                 : ** mode has not been set at compile-time or start-time.  ^If the
    3025                 : ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
    3026                 : ** in the serialized [threading mode] unless single-thread was
    3027                 : ** previously selected at compile-time or start-time.
    3028                 : ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
    3029                 : ** eligible to use [shared cache mode], regardless of whether or not shared
    3030                 : ** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
    3031                 : ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
    3032                 : ** participate in [shared cache mode] even if it is enabled.
    3033                 : **
    3034                 : ** ^The fourth parameter to sqlite3_open_v2() is the name of the
    3035                 : ** [sqlite3_vfs] object that defines the operating system interface that
    3036                 : ** the new database connection should use.  ^If the fourth parameter is
    3037                 : ** a NULL pointer then the default [sqlite3_vfs] object is used.
    3038                 : **
    3039                 : ** ^If the filename is ":memory:", then a private, temporary in-memory database
    3040                 : ** is created for the connection.  ^This in-memory database will vanish when
    3041                 : ** the database connection is closed.  Future versions of SQLite might
    3042                 : ** make use of additional special filenames that begin with the ":" character.
    3043                 : ** It is recommended that when a database filename actually does begin with
    3044                 : ** a ":" character you should prefix the filename with a pathname such as
    3045                 : ** "./" to avoid ambiguity.
    3046                 : **
    3047                 : ** ^If the filename is an empty string, then a private, temporary
    3048                 : ** on-disk database will be created.  ^This private database will be
    3049                 : ** automatically deleted as soon as the database connection is closed.
    3050                 : **
    3051                 : ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
    3052                 : **
    3053                 : ** ^If [URI filename] interpretation is enabled, and the filename argument
    3054                 : ** begins with "file:", then the filename is interpreted as a URI. ^URI
    3055                 : ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
    3056                 : ** set in the fourth argument to sqlite3_open_v2(), or if it has
    3057                 : ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
    3058                 : ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
    3059                 : ** As of SQLite version 3.7.7, URI filename interpretation is turned off
    3060                 : ** by default, but future releases of SQLite might enable URI filename
    3061                 : ** interpretation by default.  See "[URI filenames]" for additional
    3062                 : ** information.
    3063                 : **
    3064                 : ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
    3065                 : ** authority, then it must be either an empty string or the string 
    3066                 : ** "localhost". ^If the authority is not an empty string or "localhost", an 
    3067                 : ** error is returned to the caller. ^The fragment component of a URI, if 
    3068                 : ** present, is ignored.
    3069                 : **
    3070                 : ** ^SQLite uses the path component of the URI as the name of the disk file
    3071                 : ** which contains the database. ^If the path begins with a '/' character, 
    3072                 : ** then it is interpreted as an absolute path. ^If the path does not begin 
    3073                 : ** with a '/' (meaning that the authority section is omitted from the URI)
    3074                 : ** then the path is interpreted as a relative path. 
    3075                 : ** ^On windows, the first component of an absolute path 
    3076                 : ** is a drive specification (e.g. "C:").
    3077                 : **
    3078                 : ** [[core URI query parameters]]
    3079                 : ** The query component of a URI may contain parameters that are interpreted
    3080                 : ** either by SQLite itself, or by a [VFS | custom VFS implementation].
    3081                 : ** SQLite interprets the following three query parameters:
    3082                 : **
    3083                 : ** <ul>
    3084                 : **   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
    3085                 : **     a VFS object that provides the operating system interface that should
    3086                 : **     be used to access the database file on disk. ^If this option is set to
    3087                 : **     an empty string the default VFS object is used. ^Specifying an unknown
    3088                 : **     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
    3089                 : **     present, then the VFS specified by the option takes precedence over
    3090                 : **     the value passed as the fourth parameter to sqlite3_open_v2().
    3091                 : **
    3092                 : **   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw" or
    3093                 : **     "rwc". Attempting to set it to any other value is an error)^. 
    3094                 : **     ^If "ro" is specified, then the database is opened for read-only 
    3095                 : **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 
    3096                 : **     third argument to sqlite3_prepare_v2(). ^If the mode option is set to 
    3097                 : **     "rw", then the database is opened for read-write (but not create) 
    3098                 : **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 
    3099                 : **     been set. ^Value "rwc" is equivalent to setting both 
    3100                 : **     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If sqlite3_open_v2() is 
    3101                 : **     used, it is an error to specify a value for the mode parameter that is 
    3102                 : **     less restrictive than that specified by the flags passed as the third 
    3103                 : **     parameter.
    3104                 : **
    3105                 : **   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
    3106                 : **     "private". ^Setting it to "shared" is equivalent to setting the
    3107                 : **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
    3108                 : **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is 
    3109                 : **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
    3110                 : **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
    3111                 : **     a URI filename, its value overrides any behaviour requested by setting
    3112                 : **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
    3113                 : ** </ul>
    3114                 : **
    3115                 : ** ^Specifying an unknown parameter in the query component of a URI is not an
    3116                 : ** error.  Future versions of SQLite might understand additional query
    3117                 : ** parameters.  See "[query parameters with special meaning to SQLite]" for
    3118                 : ** additional information.
    3119                 : **
    3120                 : ** [[URI filename examples]] <h3>URI filename examples</h3>
    3121                 : **
    3122                 : ** <table border="1" align=center cellpadding=5>
    3123                 : ** <tr><th> URI filenames <th> Results
    3124                 : ** <tr><td> file:data.db <td> 
    3125                 : **          Open the file "data.db" in the current directory.
    3126                 : ** <tr><td> file:/home/fred/data.db<br>
    3127                 : **          file:///home/fred/data.db <br> 
    3128                 : **          file://localhost/home/fred/data.db <br> <td> 
    3129                 : **          Open the database file "/home/fred/data.db".
    3130                 : ** <tr><td> file://darkstar/home/fred/data.db <td> 
    3131                 : **          An error. "darkstar" is not a recognized authority.
    3132                 : ** <tr><td style="white-space:nowrap"> 
    3133                 : **          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
    3134                 : **     <td> Windows only: Open the file "data.db" on fred's desktop on drive
    3135                 : **          C:. Note that the %20 escaping in this example is not strictly 
    3136                 : **          necessary - space characters can be used literally
    3137                 : **          in URI filenames.
    3138                 : ** <tr><td> file:data.db?mode=ro&cache=private <td> 
    3139                 : **          Open file "data.db" in the current directory for read-only access.
    3140                 : **          Regardless of whether or not shared-cache mode is enabled by
    3141                 : **          default, use a private cache.
    3142                 : ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
    3143                 : **          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
    3144                 : ** <tr><td> file:data.db?mode=readonly <td> 
    3145                 : **          An error. "readonly" is not a valid option for the "mode" parameter.
    3146                 : ** </table>
    3147                 : **
    3148                 : ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
    3149                 : ** query components of a URI. A hexadecimal escape sequence consists of a
    3150                 : ** percent sign - "%" - followed by exactly two hexadecimal digits 
    3151                 : ** specifying an octet value. ^Before the path or query components of a
    3152                 : ** URI filename are interpreted, they are encoded using UTF-8 and all 
    3153                 : ** hexadecimal escape sequences replaced by a single byte containing the
    3154                 : ** corresponding octet. If this process generates an invalid UTF-8 encoding,
    3155                 : ** the results are undefined.
    3156                 : **
    3157                 : ** <b>Note to Windows users:</b>  The encoding used for the filename argument
    3158                 : ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
    3159                 : ** codepage is currently defined.  Filenames containing international
    3160                 : ** characters must be converted to UTF-8 prior to passing them into
    3161                 : ** sqlite3_open() or sqlite3_open_v2().
    3162                 : */
    3163                 : SQLITE_API int sqlite3_open(
    3164                 :   const char *filename,   /* Database filename (UTF-8) */
    3165                 :   sqlite3 **ppDb          /* OUT: SQLite db handle */
    3166                 : );
    3167                 : SQLITE_API int sqlite3_open16(
    3168                 :   const void *filename,   /* Database filename (UTF-16) */
    3169                 :   sqlite3 **ppDb          /* OUT: SQLite db handle */
    3170                 : );
    3171                 : SQLITE_API int sqlite3_open_v2(
    3172                 :   const char *filename,   /* Database filename (UTF-8) */
    3173                 :   sqlite3 **ppDb,         /* OUT: SQLite db handle */
    3174                 :   int flags,              /* Flags */
    3175                 :   const char *zVfs        /* Name of VFS module to use */
    3176                 : );
    3177                 : 
    3178                 : /*
    3179                 : ** CAPI3REF: Obtain Values For URI Parameters
    3180                 : **
    3181                 : ** These are utility routines, useful to VFS implementations, that check
    3182                 : ** to see if a database file was a URI that contained a specific query 
    3183                 : ** parameter, and if so obtains the value of that query parameter.
    3184                 : **
    3185                 : ** If F is the database filename pointer passed into the xOpen() method of 
    3186                 : ** a VFS implementation when the flags parameter to xOpen() has one or 
    3187                 : ** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
    3188                 : ** P is the name of the query parameter, then
    3189                 : ** sqlite3_uri_parameter(F,P) returns the value of the P
    3190                 : ** parameter if it exists or a NULL pointer if P does not appear as a 
    3191                 : ** query parameter on F.  If P is a query parameter of F
    3192                 : ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
    3193                 : ** a pointer to an empty string.
    3194                 : **
    3195                 : ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
    3196                 : ** parameter and returns true (1) or false (0) according to the value
    3197                 : ** of P.  The value of P is true if it is "yes" or "true" or "on" or 
    3198                 : ** a non-zero number and is false otherwise.  If P is not a query parameter
    3199                 : ** on F then sqlite3_uri_boolean(F,P,B) returns (B!=0).
    3200                 : **
    3201                 : ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
    3202                 : ** 64-bit signed integer and returns that integer, or D if P does not
    3203                 : ** exist.  If the value of P is something other than an integer, then
    3204                 : ** zero is returned.
    3205                 : ** 
    3206                 : ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
    3207                 : ** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
    3208                 : ** is not a database file pathname pointer that SQLite passed into the xOpen
    3209                 : ** VFS method, then the behavior of this routine is undefined and probably
    3210                 : ** undesirable.
    3211                 : */
    3212                 : SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
    3213                 : SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
    3214                 : SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
    3215                 : 
    3216                 : 
    3217                 : /*
    3218                 : ** CAPI3REF: Error Codes And Messages
    3219                 : **
    3220                 : ** ^The sqlite3_errcode() interface returns the numeric [result code] or
    3221                 : ** [extended result code] for the most recent failed sqlite3_* API call
    3222                 : ** associated with a [database connection]. If a prior API call failed
    3223                 : ** but the most recent API call succeeded, the return value from
    3224                 : ** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
    3225                 : ** interface is the same except that it always returns the 
    3226                 : ** [extended result code] even when extended result codes are
    3227                 : ** disabled.
    3228                 : **
    3229                 : ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
    3230                 : ** text that describes the error, as either UTF-8 or UTF-16 respectively.
    3231                 : ** ^(Memory to hold the error message string is managed internally.
    3232                 : ** The application does not need to worry about freeing the result.
    3233                 : ** However, the error string might be overwritten or deallocated by
    3234                 : ** subsequent calls to other SQLite interface functions.)^
    3235                 : **
    3236                 : ** When the serialized [threading mode] is in use, it might be the
    3237                 : ** case that a second error occurs on a separate thread in between
    3238                 : ** the time of the first error and the call to these interfaces.
    3239                 : ** When that happens, the second error will be reported since these
    3240                 : ** interfaces always report the most recent result.  To avoid
    3241                 : ** this, each thread can obtain exclusive use of the [database connection] D
    3242                 : ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
    3243                 : ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
    3244                 : ** all calls to the interfaces listed here are completed.
    3245                 : **
    3246                 : ** If an interface fails with SQLITE_MISUSE, that means the interface
    3247                 : ** was invoked incorrectly by the application.  In that case, the
    3248                 : ** error code and message may or may not be set.
    3249                 : */
    3250                 : SQLITE_API int sqlite3_errcode(sqlite3 *db);
    3251                 : SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
    3252                 : SQLITE_API const char *sqlite3_errmsg(sqlite3*);
    3253                 : SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
    3254                 : 
    3255                 : /*
    3256                 : ** CAPI3REF: SQL Statement Object
    3257                 : ** KEYWORDS: {prepared statement} {prepared statements}
    3258                 : **
    3259                 : ** An instance of this object represents a single SQL statement.
    3260                 : ** This object is variously known as a "prepared statement" or a
    3261                 : ** "compiled SQL statement" or simply as a "statement".
    3262                 : **
    3263                 : ** The life of a statement object goes something like this:
    3264                 : **
    3265                 : ** <ol>
    3266                 : ** <li> Create the object using [sqlite3_prepare_v2()] or a related
    3267                 : **      function.
    3268                 : ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
    3269                 : **      interfaces.
    3270                 : ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
    3271                 : ** <li> Reset the statement using [sqlite3_reset()] then go back
    3272                 : **      to step 2.  Do this zero or more times.
    3273                 : ** <li> Destroy the object using [sqlite3_finalize()].
    3274                 : ** </ol>
    3275                 : **
    3276                 : ** Refer to documentation on individual methods above for additional
    3277                 : ** information.
    3278                 : */
    3279                 : typedef struct sqlite3_stmt sqlite3_stmt;
    3280                 : 
    3281                 : /*
    3282                 : ** CAPI3REF: Run-time Limits
    3283                 : **
    3284                 : ** ^(This interface allows the size of various constructs to be limited
    3285                 : ** on a connection by connection basis.  The first parameter is the
    3286                 : ** [database connection] whose limit is to be set or queried.  The
    3287                 : ** second parameter is one of the [limit categories] that define a
    3288                 : ** class of constructs to be size limited.  The third parameter is the
    3289                 : ** new limit for that construct.)^
    3290                 : **
    3291                 : ** ^If the new limit is a negative number, the limit is unchanged.
    3292                 : ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a 
    3293                 : ** [limits | hard upper bound]
    3294                 : ** set at compile-time by a C preprocessor macro called
    3295                 : ** [limits | SQLITE_MAX_<i>NAME</i>].
    3296                 : ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
    3297                 : ** ^Attempts to increase a limit above its hard upper bound are
    3298                 : ** silently truncated to the hard upper bound.
    3299                 : **
    3300                 : ** ^Regardless of whether or not the limit was changed, the 
    3301                 : ** [sqlite3_limit()] interface returns the prior value of the limit.
    3302                 : ** ^Hence, to find the current value of a limit without changing it,
    3303                 : ** simply invoke this interface with the third parameter set to -1.
    3304                 : **
    3305                 : ** Run-time limits are intended for use in applications that manage
    3306                 : ** both their own internal database and also databases that are controlled
    3307                 : ** by untrusted external sources.  An example application might be a
    3308                 : ** web browser that has its own databases for storing history and
    3309                 : ** separate databases controlled by JavaScript applications downloaded
    3310                 : ** off the Internet.  The internal databases can be given the
    3311                 : ** large, default limits.  Databases managed by external sources can
    3312                 : ** be given much smaller limits designed to prevent a denial of service
    3313                 : ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
    3314                 : ** interface to further control untrusted SQL.  The size of the database
    3315                 : ** created by an untrusted script can be contained using the
    3316                 : ** [max_page_count] [PRAGMA].
    3317                 : **
    3318                 : ** New run-time limit categories may be added in future releases.
    3319                 : */
    3320                 : SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
    3321                 : 
    3322                 : /*
    3323                 : ** CAPI3REF: Run-Time Limit Categories
    3324                 : ** KEYWORDS: {limit category} {*limit categories}
    3325                 : **
    3326                 : ** These constants define various performance limits
    3327                 : ** that can be lowered at run-time using [sqlite3_limit()].
    3328                 : ** The synopsis of the meanings of the various limits is shown below.
    3329                 : ** Additional information is available at [limits | Limits in SQLite].
    3330                 : **
    3331                 : ** <dl>
    3332                 : ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
    3333                 : ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
    3334                 : **
    3335                 : ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
    3336                 : ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
    3337                 : **
    3338                 : ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
    3339                 : ** <dd>The maximum number of columns in a table definition or in the
    3340                 : ** result set of a [SELECT] or the maximum number of columns in an index
    3341                 : ** or in an ORDER BY or GROUP BY clause.</dd>)^
    3342                 : **
    3343                 : ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
    3344                 : ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
    3345                 : **
    3346                 : ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
    3347                 : ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
    3348                 : **
    3349                 : ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
    3350                 : ** <dd>The maximum number of instructions in a virtual machine program
    3351                 : ** used to implement an SQL statement.  This limit is not currently
    3352                 : ** enforced, though that might be added in some future release of
    3353                 : ** SQLite.</dd>)^
    3354                 : **
    3355                 : ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
    3356                 : ** <dd>The maximum number of arguments on a function.</dd>)^
    3357                 : **
    3358                 : ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
    3359                 : ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
    3360                 : **
    3361                 : ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
    3362                 : ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
    3363                 : ** <dd>The maximum length of the pattern argument to the [LIKE] or
    3364                 : ** [GLOB] operators.</dd>)^
    3365                 : **
    3366                 : ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
    3367                 : ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
    3368                 : ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
    3369                 : **
    3370                 : ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
    3371                 : ** <dd>The maximum depth of recursion for triggers.</dd>)^
    3372                 : ** </dl>
    3373                 : */
    3374                 : #define SQLITE_LIMIT_LENGTH                    0
    3375                 : #define SQLITE_LIMIT_SQL_LENGTH                1
    3376                 : #define SQLITE_LIMIT_COLUMN                    2
    3377                 : #define SQLITE_LIMIT_EXPR_DEPTH                3
    3378                 : #define SQLITE_LIMIT_COMPOUND_SELECT           4
    3379                 : #define SQLITE_LIMIT_VDBE_OP                   5
    3380                 : #define SQLITE_LIMIT_FUNCTION_ARG              6
    3381                 : #define SQLITE_LIMIT_ATTACHED                  7
    3382                 : #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
    3383                 : #define SQLITE_LIMIT_VARIABLE_NUMBER           9
    3384                 : #define SQLITE_LIMIT_TRIGGER_DEPTH            10
    3385                 : 
    3386                 : /*
    3387                 : ** CAPI3REF: Compiling An SQL Statement
    3388                 : ** KEYWORDS: {SQL statement compiler}
    3389                 : **
    3390                 : ** To execute an SQL query, it must first be compiled into a byte-code
    3391                 : ** program using one of these routines.
    3392                 : **
    3393                 : ** The first argument, "db", is a [database connection] obtained from a
    3394                 : ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
    3395                 : ** [sqlite3_open16()].  The database connection must not have been closed.
    3396                 : **
    3397                 : ** The second argument, "zSql", is the statement to be compiled, encoded
    3398                 : ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
    3399                 : ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
    3400                 : ** use UTF-16.
    3401                 : **
    3402                 : ** ^If the nByte argument is less than zero, then zSql is read up to the
    3403                 : ** first zero terminator. ^If nByte is non-negative, then it is the maximum
    3404                 : ** number of  bytes read from zSql.  ^When nByte is non-negative, the
    3405                 : ** zSql string ends at either the first '\000' or '\u0000' character or
    3406                 : ** the nByte-th byte, whichever comes first. If the caller knows
    3407                 : ** that the supplied string is nul-terminated, then there is a small
    3408                 : ** performance advantage to be gained by passing an nByte parameter that
    3409                 : ** is equal to the number of bytes in the input string <i>including</i>
    3410                 : ** the nul-terminator bytes as this saves SQLite from having to
    3411                 : ** make a copy of the input string.
    3412                 : **
    3413                 : ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
    3414                 : ** past the end of the first SQL statement in zSql.  These routines only
    3415                 : ** compile the first statement in zSql, so *pzTail is left pointing to
    3416                 : ** what remains uncompiled.
    3417                 : **
    3418                 : ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
    3419                 : ** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
    3420                 : ** to NULL.  ^If the input text contains no SQL (if the input is an empty
    3421                 : ** string or a comment) then *ppStmt is set to NULL.
    3422                 : ** The calling procedure is responsible for deleting the compiled
    3423                 : ** SQL statement using [sqlite3_finalize()] after it has finished with it.
    3424                 : ** ppStmt may not be NULL.
    3425                 : **
    3426                 : ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
    3427                 : ** otherwise an [error code] is returned.
    3428                 : **
    3429                 : ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
    3430                 : ** recommended for all new programs. The two older interfaces are retained
    3431                 : ** for backwards compatibility, but their use is discouraged.
    3432                 : ** ^In the "v2" interfaces, the prepared statement
    3433                 : ** that is returned (the [sqlite3_stmt] object) contains a copy of the
    3434                 : ** original SQL text. This causes the [sqlite3_step()] interface to
    3435                 : ** behave differently in three ways:
    3436                 : **
    3437                 : ** <ol>
    3438                 : ** <li>
    3439                 : ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
    3440                 : ** always used to do, [sqlite3_step()] will automatically recompile the SQL
    3441                 : ** statement and try to run it again.
    3442                 : ** </li>
    3443                 : **
    3444                 : ** <li>
    3445                 : ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
    3446                 : ** [error codes] or [extended error codes].  ^The legacy behavior was that
    3447                 : ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
    3448                 : ** and the application would have to make a second call to [sqlite3_reset()]
    3449                 : ** in order to find the underlying cause of the problem. With the "v2" prepare
    3450                 : ** interfaces, the underlying reason for the error is returned immediately.
    3451                 : ** </li>
    3452                 : **
    3453                 : ** <li>
    3454                 : ** ^If the specific value bound to [parameter | host parameter] in the 
    3455                 : ** WHERE clause might influence the choice of query plan for a statement,
    3456                 : ** then the statement will be automatically recompiled, as if there had been 
    3457                 : ** a schema change, on the first  [sqlite3_step()] call following any change
    3458                 : ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
    3459                 : ** ^The specific value of WHERE-clause [parameter] might influence the 
    3460                 : ** choice of query plan if the parameter is the left-hand side of a [LIKE]
    3461                 : ** or [GLOB] operator or if the parameter is compared to an indexed column
    3462                 : ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
    3463                 : ** the 
    3464                 : ** </li>
    3465                 : ** </ol>
    3466                 : */
    3467                 : SQLITE_API int sqlite3_prepare(
    3468                 :   sqlite3 *db,            /* Database handle */
    3469                 :   const char *zSql,       /* SQL statement, UTF-8 encoded */
    3470                 :   int nByte,              /* Maximum length of zSql in bytes. */
    3471                 :   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
    3472                 :   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
    3473                 : );
    3474                 : SQLITE_API int sqlite3_prepare_v2(
    3475                 :   sqlite3 *db,            /* Database handle */
    3476                 :   const char *zSql,       /* SQL statement, UTF-8 encoded */
    3477                 :   int nByte,              /* Maximum length of zSql in bytes. */
    3478                 :   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
    3479                 :   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
    3480                 : );
    3481                 : SQLITE_API int sqlite3_prepare16(
    3482                 :   sqlite3 *db,            /* Database handle */
    3483                 :   const void *zSql,       /* SQL statement, UTF-16 encoded */
    3484                 :   int nByte,              /* Maximum length of zSql in bytes. */
    3485                 :   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
    3486                 :   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
    3487                 : );
    3488                 : SQLITE_API int sqlite3_prepare16_v2(
    3489                 :   sqlite3 *db,            /* Database handle */
    3490                 :   const void *zSql,       /* SQL statement, UTF-16 encoded */
    3491                 :   int nByte,              /* Maximum length of zSql in bytes. */
    3492                 :   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
    3493                 :   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
    3494                 : );
    3495                 : 
    3496                 : /*
    3497                 : ** CAPI3REF: Retrieving Statement SQL
    3498                 : **
    3499                 : ** ^This interface can be used to retrieve a saved copy of the original
    3500                 : ** SQL text used to create a [prepared statement] if that statement was
    3501                 : ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
    3502                 : */
    3503                 : SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
    3504                 : 
    3505                 : /*
    3506                 : ** CAPI3REF: Determine If An SQL Statement Writes The Database
    3507                 : **
    3508                 : ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
    3509                 : ** and only if the [prepared statement] X makes no direct changes to
    3510                 : ** the content of the database file.
    3511                 : **
    3512                 : ** Note that [application-defined SQL functions] or
    3513                 : ** [virtual tables] might change the database indirectly as a side effect.  
    3514                 : ** ^(For example, if an application defines a function "eval()" that 
    3515                 : ** calls [sqlite3_exec()], then the following SQL statement would
    3516                 : ** change the database file through side-effects:
    3517                 : **
    3518                 : ** <blockquote><pre>
    3519                 : **    SELECT eval('DELETE FROM t1') FROM t2;
    3520                 : ** </pre></blockquote>
    3521                 : **
    3522                 : ** But because the [SELECT] statement does not change the database file
    3523                 : ** directly, sqlite3_stmt_readonly() would still return true.)^
    3524                 : **
    3525                 : ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
    3526                 : ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
    3527                 : ** since the statements themselves do not actually modify the database but
    3528                 : ** rather they control the timing of when other statements modify the 
    3529                 : ** database.  ^The [ATTACH] and [DETACH] statements also cause
    3530                 : ** sqlite3_stmt_readonly() to return true since, while those statements
    3531                 : ** change the configuration of a database connection, they do not make 
    3532                 : ** changes to the content of the database files on disk.
    3533                 : */
    3534                 : SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
    3535                 : 
    3536                 : /*
    3537                 : ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
    3538                 : **
    3539                 : ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
    3540                 : ** [prepared statement] S has been stepped at least once using 
    3541                 : ** [sqlite3_step(S)] but has not run to completion and/or has not 
    3542                 : ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
    3543                 : ** interface returns false if S is a NULL pointer.  If S is not a 
    3544                 : ** NULL pointer and is not a pointer to a valid [prepared statement]
    3545                 : ** object, then the behavior is undefined and probably undesirable.
    3546                 : **
    3547                 : ** This interface can be used in combination [sqlite3_next_stmt()]
    3548                 : ** to locate all prepared statements associated with a database 
    3549                 : ** connection that are in need of being reset.  This can be used,
    3550                 : ** for example, in diagnostic routines to search for prepared 
    3551                 : ** statements that are holding a transaction open.
    3552                 : */
    3553                 : SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
    3554                 : 
    3555                 : /*
    3556                 : ** CAPI3REF: Dynamically Typed Value Object
    3557                 : ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
    3558                 : **
    3559                 : ** SQLite uses the sqlite3_value object to represent all values
    3560                 : ** that can be stored in a database table. SQLite uses dynamic typing
    3561                 : ** for the values it stores.  ^Values stored in sqlite3_value objects
    3562                 : ** can be integers, floating point values, strings, BLOBs, or NULL.
    3563                 : **
    3564                 : ** An sqlite3_value object may be either "protected" or "unprotected".
    3565                 : ** Some interfaces require a protected sqlite3_value.  Other interfaces
    3566                 : ** will accept either a protected or an unprotected sqlite3_value.
    3567                 : ** Every interface that accepts sqlite3_value arguments specifies
    3568                 : ** whether or not it requires a protected sqlite3_value.
    3569                 : **
    3570                 : ** The terms "protected" and "unprotected" refer to whether or not
    3571                 : ** a mutex is held.  An internal mutex is held for a protected
    3572                 : ** sqlite3_value object but no mutex is held for an unprotected
    3573                 : ** sqlite3_value object.  If SQLite is compiled to be single-threaded
    3574                 : ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
    3575                 : ** or if SQLite is run in one of reduced mutex modes 
    3576                 : ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
    3577                 : ** then there is no distinction between protected and unprotected
    3578                 : ** sqlite3_value objects and they can be used interchangeably.  However,
    3579                 : ** for maximum code portability it is recommended that applications
    3580                 : ** still make the distinction between protected and unprotected
    3581                 : ** sqlite3_value objects even when not strictly required.
    3582                 : **
    3583                 : ** ^The sqlite3_value objects that are passed as parameters into the
    3584                 : ** implementation of [application-defined SQL functions] are protected.
    3585                 : ** ^The sqlite3_value object returned by
    3586                 : ** [sqlite3_column_value()] is unprotected.
    3587                 : ** Unprotected sqlite3_value objects may only be used with
    3588                 : ** [sqlite3_result_value()] and [sqlite3_bind_value()].
    3589                 : ** The [sqlite3_value_blob | sqlite3_value_type()] family of
    3590                 : ** interfaces require protected sqlite3_value objects.
    3591                 : */
    3592                 : typedef struct Mem sqlite3_value;
    3593                 : 
    3594                 : /*
    3595                 : ** CAPI3REF: SQL Function Context Object
    3596                 : **
    3597                 : ** The context in which an SQL function executes is stored in an
    3598                 : ** sqlite3_context object.  ^A pointer to an sqlite3_context object
    3599                 : ** is always first parameter to [application-defined SQL functions].
    3600                 : ** The application-defined SQL function implementation will pass this
    3601                 : ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
    3602                 : ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
    3603                 : ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
    3604                 : ** and/or [sqlite3_set_auxdata()].
    3605                 : */
    3606                 : typedef struct sqlite3_context sqlite3_context;
    3607                 : 
    3608                 : /*
    3609                 : ** CAPI3REF: Binding Values To Prepared Statements
    3610                 : ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
    3611                 : ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
    3612                 : **
    3613                 : ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
    3614                 : ** literals may be replaced by a [parameter] that matches one of following
    3615                 : ** templates:
    3616                 : **
    3617                 : ** <ul>
    3618                 : ** <li>  ?
    3619                 : ** <li>  ?NNN
    3620                 : ** <li>  :VVV
    3621                 : ** <li>  @VVV
    3622                 : ** <li>  $VVV
    3623                 : ** </ul>
    3624                 : **
    3625                 : ** In the templates above, NNN represents an integer literal,
    3626                 : ** and VVV represents an alphanumeric identifier.)^  ^The values of these
    3627                 : ** parameters (also called "host parameter names" or "SQL parameters")
    3628                 : ** can be set using the sqlite3_bind_*() routines defined here.
    3629                 : **
    3630                 : ** ^The first argument to the sqlite3_bind_*() routines is always
    3631                 : ** a pointer to the [sqlite3_stmt] object returned from
    3632                 : ** [sqlite3_prepare_v2()] or its variants.
    3633                 : **
    3634                 : ** ^The second argument is the index of the SQL parameter to be set.
    3635                 : ** ^The leftmost SQL parameter has an index of 1.  ^When the same named
    3636                 : ** SQL parameter is used more than once, second and subsequent
    3637                 : ** occurrences have the same index as the first occurrence.
    3638                 : ** ^The index for named parameters can be looked up using the
    3639                 : ** [sqlite3_bind_parameter_index()] API if desired.  ^The index
    3640                 : ** for "?NNN" parameters is the value of NNN.
    3641                 : ** ^The NNN value must be between 1 and the [sqlite3_limit()]
    3642                 : ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
    3643                 : **
    3644                 : ** ^The third argument is the value to bind to the parameter.
    3645                 : **
    3646                 : ** ^(In those routines that have a fourth argument, its value is the
    3647                 : ** number of bytes in the parameter.  To be clear: the value is the
    3648                 : ** number of <u>bytes</u> in the value, not the number of characters.)^
    3649                 : ** ^If the fourth parameter is negative, the length of the string is
    3650                 : ** the number of bytes up to the first zero terminator.
    3651                 : ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
    3652                 : ** or sqlite3_bind_text16() then that parameter must be the byte offset
    3653                 : ** where the NUL terminator would occur assuming the string were NUL
    3654                 : ** terminated.  If any NUL characters occur at byte offsets less than 
    3655                 : ** the value of the fourth parameter then the resulting string value will
    3656                 : ** contain embedded NULs.  The result of expressions involving strings
    3657                 : ** with embedded NULs is undefined.
    3658                 : **
    3659                 : ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
    3660                 : ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
    3661                 : ** string after SQLite has finished with it.  ^The destructor is called
    3662                 : ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
    3663                 : ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
    3664                 : ** ^If the fifth argument is
    3665                 : ** the special value [SQLITE_STATIC], then SQLite assumes that the
    3666                 : ** information is in static, unmanaged space and does not need to be freed.
    3667                 : ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
    3668                 : ** SQLite makes its own private copy of the data immediately, before
    3669                 : ** the sqlite3_bind_*() routine returns.
    3670                 : **
    3671                 : ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
    3672                 : ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
    3673                 : ** (just an integer to hold its size) while it is being processed.
    3674                 : ** Zeroblobs are intended to serve as placeholders for BLOBs whose
    3675                 : ** content is later written using
    3676                 : ** [sqlite3_blob_open | incremental BLOB I/O] routines.
    3677                 : ** ^A negative value for the zeroblob results in a zero-length BLOB.
    3678                 : **
    3679                 : ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
    3680                 : ** for the [prepared statement] or with a prepared statement for which
    3681                 : ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
    3682                 : ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
    3683                 : ** routine is passed a [prepared statement] that has been finalized, the
    3684                 : ** result is undefined and probably harmful.
    3685                 : **
    3686                 : ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
    3687                 : ** ^Unbound parameters are interpreted as NULL.
    3688                 : **
    3689                 : ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
    3690                 : ** [error code] if anything goes wrong.
    3691                 : ** ^[SQLITE_RANGE] is returned if the parameter
    3692                 : ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
    3693                 : **
    3694                 : ** See also: [sqlite3_bind_parameter_count()],
    3695                 : ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
    3696                 : */
    3697                 : SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
    3698                 : SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
    3699                 : SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
    3700                 : SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
    3701                 : SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
    3702                 : SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
    3703                 : SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
    3704                 : SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
    3705                 : SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
    3706                 : 
    3707                 : /*
    3708                 : ** CAPI3REF: Number Of SQL Parameters
    3709                 : **
    3710                 : ** ^This routine can be used to find the number of [SQL parameters]
    3711                 : ** in a [prepared statement].  SQL parameters are tokens of the
    3712                 : ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
    3713                 : ** placeholders for values that are [sqlite3_bind_blob | bound]
    3714                 : ** to the parameters at a later time.
    3715                 : **
    3716                 : ** ^(This routine actually returns the index of the largest (rightmost)
    3717                 : ** parameter. For all forms except ?NNN, this will correspond to the
    3718                 : ** number of unique parameters.  If parameters of the ?NNN form are used,
    3719                 : ** there may be gaps in the list.)^
    3720                 : **
    3721                 : ** See also: [sqlite3_bind_blob|sqlite3_bind()],
    3722                 : ** [sqlite3_bind_parameter_name()], and
    3723                 : ** [sqlite3_bind_parameter_index()].
    3724                 : */
    3725                 : SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
    3726                 : 
    3727                 : /*
    3728                 : ** CAPI3REF: Name Of A Host Parameter
    3729                 : **
    3730                 : ** ^The sqlite3_bind_parameter_name(P,N) interface returns
    3731                 : ** the name of the N-th [SQL parameter] in the [prepared statement] P.
    3732                 : ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
    3733                 : ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
    3734                 : ** respectively.
    3735                 : ** In other words, the initial ":" or "$" or "@" or "?"
    3736                 : ** is included as part of the name.)^
    3737                 : ** ^Parameters of the form "?" without a following integer have no name
    3738                 : ** and are referred to as "nameless" or "anonymous parameters".
    3739                 : **
    3740                 : ** ^The first host parameter has an index of 1, not 0.
    3741                 : **
    3742                 : ** ^If the value N is out of range or if the N-th parameter is
    3743                 : ** nameless, then NULL is returned.  ^The returned string is
    3744                 : ** always in UTF-8 encoding even if the named parameter was
    3745                 : ** originally specified as UTF-16 in [sqlite3_prepare16()] or
    3746                 : ** [sqlite3_prepare16_v2()].
    3747                 : **
    3748                 : ** See also: [sqlite3_bind_blob|sqlite3_bind()],
    3749                 : ** [sqlite3_bind_parameter_count()], and
    3750                 : ** [sqlite3_bind_parameter_index()].
    3751                 : */
    3752                 : SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
    3753                 : 
    3754                 : /*
    3755                 : ** CAPI3REF: Index Of A Parameter With A Given Name
    3756                 : **
    3757                 : ** ^Return the index of an SQL parameter given its name.  ^The
    3758                 : ** index value returned is suitable for use as the second
    3759                 : ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
    3760                 : ** is returned if no matching parameter is found.  ^The parameter
    3761                 : ** name must be given in UTF-8 even if the original statement
    3762                 : ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
    3763                 : **
    3764                 : ** See also: [sqlite3_bind_blob|sqlite3_bind()],
    3765                 : ** [sqlite3_bind_parameter_count()], and
    3766                 : ** [sqlite3_bind_parameter_index()].
    3767                 : */
    3768                 : SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
    3769                 : 
    3770                 : /*
    3771                 : ** CAPI3REF: Reset All Bindings On A Prepared Statement
    3772                 : **
    3773                 : ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
    3774                 : ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
    3775                 : ** ^Use this routine to reset all host parameters to NULL.
    3776                 : */
    3777                 : SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
    3778                 : 
    3779                 : /*
    3780                 : ** CAPI3REF: Number Of Columns In A Result Set
    3781                 : **
    3782                 : ** ^Return the number of columns in the result set returned by the
    3783                 : ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
    3784                 : ** statement that does not return data (for example an [UPDATE]).
    3785                 : **
    3786                 : ** See also: [sqlite3_data_count()]
    3787                 : */
    3788                 : SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
    3789                 : 
    3790                 : /*
    3791                 : ** CAPI3REF: Column Names In A Result Set
    3792                 : **
    3793                 : ** ^These routines return the name assigned to a particular column
    3794                 : ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
    3795                 : ** interface returns a pointer to a zero-terminated UTF-8 string
    3796                 : ** and sqlite3_column_name16() returns a pointer to a zero-terminated
    3797                 : ** UTF-16 string.  ^The first parameter is the [prepared statement]
    3798                 : ** that implements the [SELECT] statement. ^The second parameter is the
    3799                 : ** column number.  ^The leftmost column is number 0.
    3800                 : **
    3801                 : ** ^The returned string pointer is valid until either the [prepared statement]
    3802                 : ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
    3803                 : ** reprepared by the first call to [sqlite3_step()] for a particular run
    3804                 : ** or until the next call to
    3805                 : ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
    3806                 : **
    3807                 : ** ^If sqlite3_malloc() fails during the processing of either routine
    3808                 : ** (for example during a conversion from UTF-8 to UTF-16) then a
    3809                 : ** NULL pointer is returned.
    3810                 : **
    3811                 : ** ^The name of a result column is the value of the "AS" clause for
    3812                 : ** that column, if there is an AS clause.  If there is no AS clause
    3813                 : ** then the name of the column is unspecified and may change from
    3814                 : ** one release of SQLite to the next.
    3815                 : */
    3816                 : SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
    3817                 : SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
    3818                 : 
    3819                 : /*
    3820                 : ** CAPI3REF: Source Of Data In A Query Result
    3821                 : **
    3822                 : ** ^These routines provide a means to determine the database, table, and
    3823                 : ** table column that is the origin of a particular result column in
    3824                 : ** [SELECT] statement.
    3825                 : ** ^The name of the database or table or column can be returned as
    3826                 : ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
    3827                 : ** the database name, the _table_ routines return the table name, and
    3828                 : ** the origin_ routines return the column name.
    3829                 : ** ^The returned string is valid until the [prepared statement] is destroyed
    3830                 : ** using [sqlite3_finalize()] or until the statement is automatically
    3831                 : ** reprepared by the first call to [sqlite3_step()] for a particular run
    3832                 : ** or until the same information is requested
    3833                 : ** again in a different encoding.
    3834                 : **
    3835                 : ** ^The names returned are the original un-aliased names of the
    3836                 : ** database, table, and column.
    3837                 : **
    3838                 : ** ^The first argument to these interfaces is a [prepared statement].
    3839                 : ** ^These functions return information about the Nth result column returned by
    3840                 : ** the statement, where N is the second function argument.
    3841                 : ** ^The left-most column is column 0 for these routines.
    3842                 : **
    3843                 : ** ^If the Nth column returned by the statement is an expression or
    3844                 : ** subquery and is not a column value, then all of these functions return
    3845                 : ** NULL.  ^These routine might also return NULL if a memory allocation error
    3846                 : ** occurs.  ^Otherwise, they return the name of the attached database, table,
    3847                 : ** or column that query result column was extracted from.
    3848                 : **
    3849                 : ** ^As with all other SQLite APIs, those whose names end with "16" return
    3850                 : ** UTF-16 encoded strings and the other functions return UTF-8.
    3851                 : **
    3852                 : ** ^These APIs are only available if the library was compiled with the
    3853                 : ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
    3854                 : **
    3855                 : ** If two or more threads call one or more of these routines against the same
    3856                 : ** prepared statement and column at the same time then the results are
    3857                 : ** undefined.
    3858                 : **
    3859                 : ** If two or more threads call one or more
    3860                 : ** [sqlite3_column_database_name | column metadata interfaces]
    3861                 : ** for the same [prepared statement] and result column
    3862                 : ** at the same time then the results are undefined.
    3863                 : */
    3864                 : SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
    3865                 : SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
    3866                 : SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
    3867                 : SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
    3868                 : SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
    3869                 : SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
    3870                 : 
    3871                 : /*
    3872                 : ** CAPI3REF: Declared Datatype Of A Query Result
    3873                 : **
    3874                 : ** ^(The first parameter is a [prepared statement].
    3875                 : ** If this statement is a [SELECT] statement and the Nth column of the
    3876                 : ** returned result set of that [SELECT] is a table column (not an
    3877                 : ** expression or subquery) then the declared type of the table
    3878                 : ** column is returned.)^  ^If the Nth column of the result set is an
    3879                 : ** expression or subquery, then a NULL pointer is returned.
    3880                 : ** ^The returned string is always UTF-8 encoded.
    3881                 : **
    3882                 : ** ^(For example, given the database schema:
    3883                 : **
    3884                 : ** CREATE TABLE t1(c1 VARIANT);
    3885                 : **
    3886                 : ** and the following statement to be compiled:
    3887                 : **
    3888                 : ** SELECT c1 + 1, c1 FROM t1;
    3889                 : **
    3890                 : ** this routine would return the string "VARIANT" for the second result
    3891                 : ** column (i==1), and a NULL pointer for the first result column (i==0).)^
    3892                 : **
    3893                 : ** ^SQLite uses dynamic run-time typing.  ^So just because a column
    3894                 : ** is declared to contain a particular type does not mean that the
    3895                 : ** data stored in that column is of the declared type.  SQLite is
    3896                 : ** strongly typed, but the typing is dynamic not static.  ^Type
    3897                 : ** is associated with individual values, not with the containers
    3898                 : ** used to hold those values.
    3899                 : */
    3900                 : SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
    3901                 : SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
    3902                 : 
    3903                 : /*
    3904                 : ** CAPI3REF: Evaluate An SQL Statement
    3905                 : **
    3906                 : ** After a [prepared statement] has been prepared using either
    3907                 : ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
    3908                 : ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
    3909                 : ** must be called one or more times to evaluate the statement.
    3910                 : **
    3911                 : ** The details of the behavior of the sqlite3_step() interface depend
    3912                 : ** on whether the statement was prepared using the newer "v2" interface
    3913                 : ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
    3914                 : ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
    3915                 : ** new "v2" interface is recommended for new applications but the legacy
    3916                 : ** interface will continue to be supported.
    3917                 : **
    3918                 : ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
    3919                 : ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
    3920                 : ** ^With the "v2" interface, any of the other [result codes] or
    3921                 : ** [extended result codes] might be returned as well.
    3922                 : **
    3923                 : ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
    3924                 : ** database locks it needs to do its job.  ^If the statement is a [COMMIT]
    3925                 : ** or occurs outside of an explicit transaction, then you can retry the
    3926                 : ** statement.  If the statement is not a [COMMIT] and occurs within an
    3927                 : ** explicit transaction then you should rollback the transaction before
    3928                 : ** continuing.
    3929                 : **
    3930                 : ** ^[SQLITE_DONE] means that the statement has finished executing
    3931                 : ** successfully.  sqlite3_step() should not be called again on this virtual
    3932                 : ** machine without first calling [sqlite3_reset()] to reset the virtual
    3933                 : ** machine back to its initial state.
    3934                 : **
    3935                 : ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
    3936                 : ** is returned each time a new row of data is ready for processing by the
    3937                 : ** caller. The values may be accessed using the [column access functions].
    3938                 : ** sqlite3_step() is called again to retrieve the next row of data.
    3939                 : **
    3940                 : ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
    3941                 : ** violation) has occurred.  sqlite3_step() should not be called again on
    3942                 : ** the VM. More information may be found by calling [sqlite3_errmsg()].
    3943                 : ** ^With the legacy interface, a more specific error code (for example,
    3944                 : ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
    3945                 : ** can be obtained by calling [sqlite3_reset()] on the
    3946                 : ** [prepared statement].  ^In the "v2" interface,
    3947                 : ** the more specific error code is returned directly by sqlite3_step().
    3948                 : **
    3949                 : ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
    3950                 : ** Perhaps it was called on a [prepared statement] that has
    3951                 : ** already been [sqlite3_finalize | finalized] or on one that had
    3952                 : ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
    3953                 : ** be the case that the same database connection is being used by two or
    3954                 : ** more threads at the same moment in time.
    3955                 : **
    3956                 : ** For all versions of SQLite up to and including 3.6.23.1, a call to
    3957                 : ** [sqlite3_reset()] was required after sqlite3_step() returned anything
    3958                 : ** other than [SQLITE_ROW] before any subsequent invocation of
    3959                 : ** sqlite3_step().  Failure to reset the prepared statement using 
    3960                 : ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
    3961                 : ** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
    3962                 : ** calling [sqlite3_reset()] automatically in this circumstance rather
    3963                 : ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
    3964                 : ** break because any application that ever receives an SQLITE_MISUSE error
    3965                 : ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
    3966                 : ** can be used to restore the legacy behavior.
    3967                 : **
    3968                 : ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
    3969                 : ** API always returns a generic error code, [SQLITE_ERROR], following any
    3970                 : ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
    3971                 : ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
    3972                 : ** specific [error codes] that better describes the error.
    3973                 : ** We admit that this is a goofy design.  The problem has been fixed
    3974                 : ** with the "v2" interface.  If you prepare all of your SQL statements
    3975                 : ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
    3976                 : ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
    3977                 : ** then the more specific [error codes] are returned directly
    3978                 : ** by sqlite3_step().  The use of the "v2" interface is recommended.
    3979                 : */
    3980                 : SQLITE_API int sqlite3_step(sqlite3_stmt*);
    3981                 : 
    3982                 : /*
    3983                 : ** CAPI3REF: Number of columns in a result set
    3984                 : **
    3985                 : ** ^The sqlite3_data_count(P) interface returns the number of columns in the
    3986                 : ** current row of the result set of [prepared statement] P.
    3987                 : ** ^If prepared statement P does not have results ready to return
    3988                 : ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
    3989                 : ** interfaces) then sqlite3_data_count(P) returns 0.
    3990                 : ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
    3991                 : ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
    3992                 : ** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
    3993                 : ** will return non-zero if previous call to [sqlite3_step](P) returned
    3994                 : ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
    3995                 : ** where it always returns zero since each step of that multi-step
    3996                 : ** pragma returns 0 columns of data.
    3997                 : **
    3998                 : ** See also: [sqlite3_column_count()]
    3999                 : */
    4000                 : SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
    4001                 : 
    4002                 : /*
    4003                 : ** CAPI3REF: Fundamental Datatypes
    4004                 : ** KEYWORDS: SQLITE_TEXT
    4005                 : **
    4006                 : ** ^(Every value in SQLite has one of five fundamental datatypes:
    4007                 : **
    4008                 : ** <ul>
    4009                 : ** <li> 64-bit signed integer
    4010                 : ** <li> 64-bit IEEE floating point number
    4011                 : ** <li> string
    4012                 : ** <li> BLOB
    4013                 : ** <li> NULL
    4014                 : ** </ul>)^
    4015                 : **
    4016                 : ** These constants are codes for each of those types.
    4017                 : **
    4018                 : ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
    4019                 : ** for a completely different meaning.  Software that links against both
    4020                 : ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
    4021                 : ** SQLITE_TEXT.
    4022                 : */
    4023                 : #define SQLITE_INTEGER  1
    4024                 : #define SQLITE_FLOAT    2
    4025                 : #define SQLITE_BLOB     4
    4026                 : #define SQLITE_NULL     5
    4027                 : #ifdef SQLITE_TEXT
    4028                 : # undef SQLITE_TEXT
    4029                 : #else
    4030                 : # define SQLITE_TEXT     3
    4031                 : #endif
    4032                 : #define SQLITE3_TEXT     3
    4033                 : 
    4034                 : /*
    4035                 : ** CAPI3REF: Result Values From A Query
    4036                 : ** KEYWORDS: {column access functions}
    4037                 : **
    4038                 : ** These routines form the "result set" interface.
    4039                 : **
    4040                 : ** ^These routines return information about a single column of the current
    4041                 : ** result row of a query.  ^In every case the first argument is a pointer
    4042                 : ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
    4043                 : ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
    4044                 : ** and the second argument is the index of the column for which information
    4045                 : ** should be returned. ^The leftmost column of the result set has the index 0.
    4046                 : ** ^The number of columns in the result can be determined using
    4047                 : ** [sqlite3_column_count()].
    4048                 : **
    4049                 : ** If the SQL statement does not currently point to a valid row, or if the
    4050                 : ** column index is out of range, the result is undefined.
    4051                 : ** These routines may only be called when the most recent call to
    4052                 : ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
    4053                 : ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
    4054                 : ** If any of these routines are called after [sqlite3_reset()] or
    4055                 : ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
    4056                 : ** something other than [SQLITE_ROW], the results are undefined.
    4057                 : ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
    4058                 : ** are called from a different thread while any of these routines
    4059                 : ** are pending, then the results are undefined.
    4060                 : **
    4061                 : ** ^The sqlite3_column_type() routine returns the
    4062                 : ** [SQLITE_INTEGER | datatype code] for the initial data type
    4063                 : ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
    4064                 : ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
    4065                 : ** returned by sqlite3_column_type() is only meaningful if no type
    4066                 : ** conversions have occurred as described below.  After a type conversion,
    4067                 : ** the value returned by sqlite3_column_type() is undefined.  Future
    4068                 : ** versions of SQLite may change the behavior of sqlite3_column_type()
    4069                 : ** following a type conversion.
    4070                 : **
    4071                 : ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
    4072                 : ** routine returns the number of bytes in that BLOB or string.
    4073                 : ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
    4074                 : ** the string to UTF-8 and then returns the number of bytes.
    4075                 : ** ^If the result is a numeric value then sqlite3_column_bytes() uses
    4076                 : ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
    4077                 : ** the number of bytes in that string.
    4078                 : ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
    4079                 : **
    4080                 : ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
    4081                 : ** routine returns the number of bytes in that BLOB or string.
    4082                 : ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
    4083                 : ** the string to UTF-16 and then returns the number of bytes.
    4084                 : ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
    4085                 : ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
    4086                 : ** the number of bytes in that string.
    4087                 : ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
    4088                 : **
    4089                 : ** ^The values returned by [sqlite3_column_bytes()] and 
    4090                 : ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
    4091                 : ** of the string.  ^For clarity: the values returned by
    4092                 : ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
    4093                 : ** bytes in the string, not the number of characters.
    4094                 : **
    4095                 : ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
    4096                 : ** even empty strings, are always zero-terminated.  ^The return
    4097                 : ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
    4098                 : **
    4099                 : ** ^The object returned by [sqlite3_column_value()] is an
    4100                 : ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
    4101                 : ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
    4102                 : ** If the [unprotected sqlite3_value] object returned by
    4103                 : ** [sqlite3_column_value()] is used in any other way, including calls
    4104                 : ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
    4105                 : ** or [sqlite3_value_bytes()], then the behavior is undefined.
    4106                 : **
    4107                 : ** These routines attempt to convert the value where appropriate.  ^For
    4108                 : ** example, if the internal representation is FLOAT and a text result
    4109                 : ** is requested, [sqlite3_snprintf()] is used internally to perform the
    4110                 : ** conversion automatically.  ^(The following table details the conversions
    4111                 : ** that are applied:
    4112                 : **
    4113                 : ** <blockquote>
    4114                 : ** <table border="1">
    4115                 : ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
    4116                 : **
    4117                 : ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
    4118                 : ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
    4119                 : ** <tr><td>  NULL    <td>   TEXT    <td> Result is NULL pointer
    4120                 : ** <tr><td>  NULL    <td>   BLOB    <td> Result is NULL pointer
    4121                 : ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
    4122                 : ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
    4123                 : ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
    4124                 : ** <tr><td>  FLOAT   <td> INTEGER   <td> Convert from float to integer
    4125                 : ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
    4126                 : ** <tr><td>  FLOAT   <td>   BLOB    <td> Same as FLOAT->TEXT
    4127                 : ** <tr><td>  TEXT    <td> INTEGER   <td> Use atoi()
    4128                 : ** <tr><td>  TEXT    <td>  FLOAT    <td> Use atof()
    4129                 : ** <tr><td>  TEXT    <td>   BLOB    <td> No change
    4130                 : ** <tr><td>  BLOB    <td> INTEGER   <td> Convert to TEXT then use atoi()
    4131                 : ** <tr><td>  BLOB    <td>  FLOAT    <td> Convert to TEXT then use atof()
    4132                 : ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
    4133                 : ** </table>
    4134                 : ** </blockquote>)^
    4135                 : **
    4136                 : ** The table above makes reference to standard C library functions atoi()
    4137                 : ** and atof().  SQLite does not really use these functions.  It has its
    4138                 : ** own equivalent internal routines.  The atoi() and atof() names are
    4139                 : ** used in the table for brevity and because they are familiar to most
    4140                 : ** C programmers.
    4141                 : **
    4142                 : ** Note that when type conversions occur, pointers returned by prior
    4143                 : ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
    4144                 : ** sqlite3_column_text16() may be invalidated.
    4145                 : ** Type conversions and pointer invalidations might occur
    4146                 : ** in the following cases:
    4147                 : **
    4148                 : ** <ul>
    4149                 : ** <li> The initial content is a BLOB and sqlite3_column_text() or
    4150                 : **      sqlite3_column_text16() is called.  A zero-terminator might
    4151                 : **      need to be added to the string.</li>
    4152                 : ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
    4153                 : **      sqlite3_column_text16() is called.  The content must be converted
    4154                 : **      to UTF-16.</li>
    4155                 : ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
    4156                 : **      sqlite3_column_text() is called.  The content must be converted
    4157                 : **      to UTF-8.</li>
    4158                 : ** </ul>
    4159                 : **
    4160                 : ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
    4161                 : ** not invalidate a prior pointer, though of course the content of the buffer
    4162                 : ** that the prior pointer references will have been modified.  Other kinds
    4163                 : ** of conversion are done in place when it is possible, but sometimes they
    4164                 : ** are not possible and in those cases prior pointers are invalidated.
    4165                 : **
    4166                 : ** The safest and easiest to remember policy is to invoke these routines
    4167                 : ** in one of the following ways:
    4168                 : **
    4169                 : ** <ul>
    4170                 : **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
    4171                 : **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
    4172                 : **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
    4173                 : ** </ul>
    4174                 : **
    4175                 : ** In other words, you should call sqlite3_column_text(),
    4176                 : ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
    4177                 : ** into the desired format, then invoke sqlite3_column_bytes() or
    4178                 : ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
    4179                 : ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
    4180                 : ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
    4181                 : ** with calls to sqlite3_column_bytes().
    4182                 : **
    4183                 : ** ^The pointers returned are valid until a type conversion occurs as
    4184                 : ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
    4185                 : ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
    4186                 : ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
    4187                 : ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
    4188                 : ** [sqlite3_free()].
    4189                 : **
    4190                 : ** ^(If a memory allocation error occurs during the evaluation of any
    4191                 : ** of these routines, a default value is returned.  The default value
    4192                 : ** is either the integer 0, the floating point number 0.0, or a NULL
    4193                 : ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
    4194                 : ** [SQLITE_NOMEM].)^
    4195                 : */
    4196                 : SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
    4197                 : SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
    4198                 : SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
    4199                 : SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
    4200                 : SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
    4201                 : SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
    4202                 : SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
    4203                 : SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
    4204                 : SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
    4205                 : SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
    4206                 : 
    4207                 : /*
    4208                 : ** CAPI3REF: Destroy A Prepared Statement Object
    4209                 : **
    4210                 : ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
    4211                 : ** ^If the most recent evaluation of the statement encountered no errors
    4212                 : ** or if the statement is never been evaluated, then sqlite3_finalize() returns
    4213                 : ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
    4214                 : ** sqlite3_finalize(S) returns the appropriate [error code] or
    4215                 : ** [extended error code].
    4216                 : **
    4217                 : ** ^The sqlite3_finalize(S) routine can be called at any point during
    4218                 : ** the life cycle of [prepared statement] S:
    4219                 : ** before statement S is ever evaluated, after
    4220                 : ** one or more calls to [sqlite3_reset()], or after any call
    4221                 : ** to [sqlite3_step()] regardless of whether or not the statement has
    4222                 : ** completed execution.
    4223                 : **
    4224                 : ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
    4225                 : **
    4226                 : ** The application must finalize every [prepared statement] in order to avoid
    4227                 : ** resource leaks.  It is a grievous error for the application to try to use
    4228                 : ** a prepared statement after it has been finalized.  Any use of a prepared
    4229                 : ** statement after it has been finalized can result in undefined and
    4230                 : ** undesirable behavior such as segfaults and heap corruption.
    4231                 : */
    4232                 : SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
    4233                 : 
    4234                 : /*
    4235                 : ** CAPI3REF: Reset A Prepared Statement Object
    4236                 : **
    4237                 : ** The sqlite3_reset() function is called to reset a [prepared statement]
    4238                 : ** object back to its initial state, ready to be re-executed.
    4239                 : ** ^Any SQL statement variables that had values bound to them using
    4240                 : ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
    4241                 : ** Use [sqlite3_clear_bindings()] to reset the bindings.
    4242                 : **
    4243                 : ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
    4244                 : ** back to the beginning of its program.
    4245                 : **
    4246                 : ** ^If the most recent call to [sqlite3_step(S)] for the
    4247                 : ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
    4248                 : ** or if [sqlite3_step(S)] has never before been called on S,
    4249                 : ** then [sqlite3_reset(S)] returns [SQLITE_OK].
    4250                 : **
    4251                 : ** ^If the most recent call to [sqlite3_step(S)] for the
    4252                 : ** [prepared statement] S indicated an error, then
    4253                 : ** [sqlite3_reset(S)] returns an appropriate [error code].
    4254                 : **
    4255                 : ** ^The [sqlite3_reset(S)] interface does not change the values
    4256                 : ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
    4257                 : */
    4258                 : SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
    4259                 : 
    4260                 : /*
    4261                 : ** CAPI3REF: Create Or Redefine SQL Functions
    4262                 : ** KEYWORDS: {function creation routines}
    4263                 : ** KEYWORDS: {application-defined SQL function}
    4264                 : ** KEYWORDS: {application-defined SQL functions}
    4265                 : **
    4266                 : ** ^These functions (collectively known as "function creation routines")
    4267                 : ** are used to add SQL functions or aggregates or to redefine the behavior
    4268                 : ** of existing SQL functions or aggregates.  The only differences between
    4269                 : ** these routines are the text encoding expected for
    4270                 : ** the second parameter (the name of the function being created)
    4271                 : ** and the presence or absence of a destructor callback for
    4272                 : ** the application data pointer.
    4273                 : **
    4274                 : ** ^The first parameter is the [database connection] to which the SQL
    4275                 : ** function is to be added.  ^If an application uses more than one database
    4276                 : ** connection then application-defined SQL functions must be added
    4277                 : ** to each database connection separately.
    4278                 : **
    4279                 : ** ^The second parameter is the name of the SQL function to be created or
    4280                 : ** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
    4281                 : ** representation, exclusive of the zero-terminator.  ^Note that the name
    4282                 : ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.  
    4283                 : ** ^Any attempt to create a function with a longer name
    4284                 : ** will result in [SQLITE_MISUSE] being returned.
    4285                 : **
    4286                 : ** ^The third parameter (nArg)
    4287                 : ** is the number of arguments that the SQL function or
    4288                 : ** aggregate takes. ^If this parameter is -1, then the SQL function or
    4289                 : ** aggregate may take any number of arguments between 0 and the limit
    4290                 : ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
    4291                 : ** parameter is less than -1 or greater than 127 then the behavior is
    4292                 : ** undefined.
    4293                 : **
    4294                 : ** ^The fourth parameter, eTextRep, specifies what
    4295                 : ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
    4296                 : ** its parameters.  Every SQL function implementation must be able to work
    4297                 : ** with UTF-8, UTF-16le, or UTF-16be.  But some implementations may be
    4298                 : ** more efficient with one encoding than another.  ^An application may
    4299                 : ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
    4300                 : ** times with the same function but with different values of eTextRep.
    4301                 : ** ^When multiple implementations of the same function are available, SQLite
    4302                 : ** will pick the one that involves the least amount of data conversion.
    4303                 : ** If there is only a single implementation which does not care what text
    4304                 : ** encoding is used, then the fourth argument should be [SQLITE_ANY].
    4305                 : **
    4306                 : ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
    4307                 : ** function can gain access to this pointer using [sqlite3_user_data()].)^
    4308                 : **
    4309                 : ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
    4310                 : ** pointers to C-language functions that implement the SQL function or
    4311                 : ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
    4312                 : ** callback only; NULL pointers must be passed as the xStep and xFinal
    4313                 : ** parameters. ^An aggregate SQL function requires an implementation of xStep
    4314                 : ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
    4315                 : ** SQL function or aggregate, pass NULL pointers for all three function
    4316                 : ** callbacks.
    4317                 : **
    4318                 : ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
    4319                 : ** then it is destructor for the application data pointer. 
    4320                 : ** The destructor is invoked when the function is deleted, either by being
    4321                 : ** overloaded or when the database connection closes.)^
    4322                 : ** ^The destructor is also invoked if the call to
    4323                 : ** sqlite3_create_function_v2() fails.
    4324                 : ** ^When the destructor callback of the tenth parameter is invoked, it
    4325                 : ** is passed a single argument which is a copy of the application data 
    4326                 : ** pointer which was the fifth parameter to sqlite3_create_function_v2().
    4327                 : **
    4328                 : ** ^It is permitted to register multiple implementations of the same
    4329                 : ** functions with the same name but with either differing numbers of
    4330                 : ** arguments or differing preferred text encodings.  ^SQLite will use
    4331                 : ** the implementation that most closely matches the way in which the
    4332                 : ** SQL function is used.  ^A function implementation with a non-negative
    4333                 : ** nArg parameter is a better match than a function implementation with
    4334                 : ** a negative nArg.  ^A function where the preferred text encoding
    4335                 : ** matches the database encoding is a better
    4336                 : ** match than a function where the encoding is different.  
    4337                 : ** ^A function where the encoding difference is between UTF16le and UTF16be
    4338                 : ** is a closer match than a function where the encoding difference is
    4339                 : ** between UTF8 and UTF16.
    4340                 : **
    4341                 : ** ^Built-in functions may be overloaded by new application-defined functions.
    4342                 : **
    4343                 : ** ^An application-defined function is permitted to call other
    4344                 : ** SQLite interfaces.  However, such calls must not
    4345                 : ** close the database connection nor finalize or reset the prepared
    4346                 : ** statement in which the function is running.
    4347                 : */
    4348                 : SQLITE_API int sqlite3_create_function(
    4349                 :   sqlite3 *db,
    4350                 :   const char *zFunctionName,
    4351                 :   int nArg,
    4352                 :   int eTextRep,
    4353                 :   void *pApp,
    4354                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
    4355                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
    4356                 :   void (*xFinal)(sqlite3_context*)
    4357                 : );
    4358                 : SQLITE_API int sqlite3_create_function16(
    4359                 :   sqlite3 *db,
    4360                 :   const void *zFunctionName,
    4361                 :   int nArg,
    4362                 :   int eTextRep,
    4363                 :   void *pApp,
    4364                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
    4365                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
    4366                 :   void (*xFinal)(sqlite3_context*)
    4367                 : );
    4368                 : SQLITE_API int sqlite3_create_function_v2(
    4369                 :   sqlite3 *db,
    4370                 :   const char *zFunctionName,
    4371                 :   int nArg,
    4372                 :   int eTextRep,
    4373                 :   void *pApp,
    4374                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
    4375                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
    4376                 :   void (*xFinal)(sqlite3_context*),
    4377                 :   void(*xDestroy)(void*)
    4378                 : );
    4379                 : 
    4380                 : /*
    4381                 : ** CAPI3REF: Text Encodings
    4382                 : **
    4383                 : ** These constant define integer codes that represent the various
    4384                 : ** text encodings supported by SQLite.
    4385                 : */
    4386                 : #define SQLITE_UTF8           1
    4387                 : #define SQLITE_UTF16LE        2
    4388                 : #define SQLITE_UTF16BE        3
    4389                 : #define SQLITE_UTF16          4    /* Use native byte order */
    4390                 : #define SQLITE_ANY            5    /* sqlite3_create_function only */
    4391                 : #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
    4392                 : 
    4393                 : /*
    4394                 : ** CAPI3REF: Deprecated Functions
    4395                 : ** DEPRECATED
    4396                 : **
    4397                 : ** These functions are [deprecated].  In order to maintain
    4398                 : ** backwards compatibility with older code, these functions continue 
    4399                 : ** to be supported.  However, new applications should avoid
    4400                 : ** the use of these functions.  To help encourage people to avoid
    4401                 : ** using these functions, we are not going to tell you what they do.
    4402                 : */
    4403                 : #ifndef SQLITE_OMIT_DEPRECATED
    4404                 : SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
    4405                 : SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
    4406                 : SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
    4407                 : SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
    4408                 : SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
    4409                 : SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
    4410                 : #endif
    4411                 : 
    4412                 : /*
    4413                 : ** CAPI3REF: Obtaining SQL Function Parameter Values
    4414                 : **
    4415                 : ** The C-language implementation of SQL functions and aggregates uses
    4416                 : ** this set of interface routines to access the parameter values on
    4417                 : ** the function or aggregate.
    4418                 : **
    4419                 : ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
    4420                 : ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
    4421                 : ** define callbacks that implement the SQL functions and aggregates.
    4422                 : ** The 3rd parameter to these callbacks is an array of pointers to
    4423                 : ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
    4424                 : ** each parameter to the SQL function.  These routines are used to
    4425                 : ** extract values from the [sqlite3_value] objects.
    4426                 : **
    4427                 : ** These routines work only with [protected sqlite3_value] objects.
    4428                 : ** Any attempt to use these routines on an [unprotected sqlite3_value]
    4429                 : ** object results in undefined behavior.
    4430                 : **
    4431                 : ** ^These routines work just like the corresponding [column access functions]
    4432                 : ** except that  these routines take a single [protected sqlite3_value] object
    4433                 : ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
    4434                 : **
    4435                 : ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
    4436                 : ** in the native byte-order of the host machine.  ^The
    4437                 : ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
    4438                 : ** extract UTF-16 strings as big-endian and little-endian respectively.
    4439                 : **
    4440                 : ** ^(The sqlite3_value_numeric_type() interface attempts to apply
    4441                 : ** numeric affinity to the value.  This means that an attempt is
    4442                 : ** made to convert the value to an integer or floating point.  If
    4443                 : ** such a conversion is possible without loss of information (in other
    4444                 : ** words, if the value is a string that looks like a number)
    4445                 : ** then the conversion is performed.  Otherwise no conversion occurs.
    4446                 : ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
    4447                 : **
    4448                 : ** Please pay particular attention to the fact that the pointer returned
    4449                 : ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
    4450                 : ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
    4451                 : ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
    4452                 : ** or [sqlite3_value_text16()].
    4453                 : **
    4454                 : ** These routines must be called from the same thread as
    4455                 : ** the SQL function that supplied the [sqlite3_value*] parameters.
    4456                 : */
    4457                 : SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
    4458                 : SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
    4459                 : SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
    4460                 : SQLITE_API double sqlite3_value_double(sqlite3_value*);
    4461                 : SQLITE_API int sqlite3_value_int(sqlite3_value*);
    4462                 : SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
    4463                 : SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
    4464                 : SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
    4465                 : SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
    4466                 : SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
    4467                 : SQLITE_API int sqlite3_value_type(sqlite3_value*);
    4468                 : SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
    4469                 : 
    4470                 : /*
    4471                 : ** CAPI3REF: Obtain Aggregate Function Context
    4472                 : **
    4473                 : ** Implementations of aggregate SQL functions use this
    4474                 : ** routine to allocate memory for storing their state.
    4475                 : **
    4476                 : ** ^The first time the sqlite3_aggregate_context(C,N) routine is called 
    4477                 : ** for a particular aggregate function, SQLite
    4478                 : ** allocates N of memory, zeroes out that memory, and returns a pointer
    4479                 : ** to the new memory. ^On second and subsequent calls to
    4480                 : ** sqlite3_aggregate_context() for the same aggregate function instance,
    4481                 : ** the same buffer is returned.  Sqlite3_aggregate_context() is normally
    4482                 : ** called once for each invocation of the xStep callback and then one
    4483                 : ** last time when the xFinal callback is invoked.  ^(When no rows match
    4484                 : ** an aggregate query, the xStep() callback of the aggregate function
    4485                 : ** implementation is never called and xFinal() is called exactly once.
    4486                 : ** In those cases, sqlite3_aggregate_context() might be called for the
    4487                 : ** first time from within xFinal().)^
    4488                 : **
    4489                 : ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
    4490                 : ** less than or equal to zero or if a memory allocate error occurs.
    4491                 : **
    4492                 : ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
    4493                 : ** determined by the N parameter on first successful call.  Changing the
    4494                 : ** value of N in subsequent call to sqlite3_aggregate_context() within
    4495                 : ** the same aggregate function instance will not resize the memory
    4496                 : ** allocation.)^
    4497                 : **
    4498                 : ** ^SQLite automatically frees the memory allocated by 
    4499                 : ** sqlite3_aggregate_context() when the aggregate query concludes.
    4500                 : **
    4501                 : ** The first parameter must be a copy of the
    4502                 : ** [sqlite3_context | SQL function context] that is the first parameter
    4503                 : ** to the xStep or xFinal callback routine that implements the aggregate
    4504                 : ** function.
    4505                 : **
    4506                 : ** This routine must be called from the same thread in which
    4507                 : ** the aggregate SQL function is running.
    4508                 : */
    4509                 : SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
    4510                 : 
    4511                 : /*
    4512                 : ** CAPI3REF: User Data For Functions
    4513                 : **
    4514                 : ** ^The sqlite3_user_data() interface returns a copy of
    4515                 : ** the pointer that was the pUserData parameter (the 5th parameter)
    4516                 : ** of the [sqlite3_create_function()]
    4517                 : ** and [sqlite3_create_function16()] routines that originally
    4518                 : ** registered the application defined function.
    4519                 : **
    4520                 : ** This routine must be called from the same thread in which
    4521                 : ** the application-defined function is running.
    4522                 : */
    4523                 : SQLITE_API void *sqlite3_user_data(sqlite3_context*);
    4524                 : 
    4525                 : /*
    4526                 : ** CAPI3REF: Database Connection For Functions
    4527                 : **
    4528                 : ** ^The sqlite3_context_db_handle() interface returns a copy of
    4529                 : ** the pointer to the [database connection] (the 1st parameter)
    4530                 : ** of the [sqlite3_create_function()]
    4531                 : ** and [sqlite3_create_function16()] routines that originally
    4532                 : ** registered the application defined function.
    4533                 : */
    4534                 : SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
    4535                 : 
    4536                 : /*
    4537                 : ** CAPI3REF: Function Auxiliary Data
    4538                 : **
    4539                 : ** The following two functions may be used by scalar SQL functions to
    4540                 : ** associate metadata with argument values. If the same value is passed to
    4541                 : ** multiple invocations of the same SQL function during query execution, under
    4542                 : ** some circumstances the associated metadata may be preserved. This may
    4543                 : ** be used, for example, to add a regular-expression matching scalar
    4544                 : ** function. The compiled version of the regular expression is stored as
    4545                 : ** metadata associated with the SQL value passed as the regular expression
    4546                 : ** pattern.  The compiled regular expression can be reused on multiple
    4547                 : ** invocations of the same function so that the original pattern string
    4548                 : ** does not need to be recompiled on each invocation.
    4549                 : **
    4550                 : ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
    4551                 : ** associated by the sqlite3_set_auxdata() function with the Nth argument
    4552                 : ** value to the application-defined function. ^If no metadata has been ever
    4553                 : ** been set for the Nth argument of the function, or if the corresponding
    4554                 : ** function parameter has changed since the meta-data was set,
    4555                 : ** then sqlite3_get_auxdata() returns a NULL pointer.
    4556                 : **
    4557                 : ** ^The sqlite3_set_auxdata() interface saves the metadata
    4558                 : ** pointed to by its 3rd parameter as the metadata for the N-th
    4559                 : ** argument of the application-defined function.  Subsequent
    4560                 : ** calls to sqlite3_get_auxdata() might return this data, if it has
    4561                 : ** not been destroyed.
    4562                 : ** ^If it is not NULL, SQLite will invoke the destructor
    4563                 : ** function given by the 4th parameter to sqlite3_set_auxdata() on
    4564                 : ** the metadata when the corresponding function parameter changes
    4565                 : ** or when the SQL statement completes, whichever comes first.
    4566                 : **
    4567                 : ** SQLite is free to call the destructor and drop metadata on any
    4568                 : ** parameter of any function at any time.  ^The only guarantee is that
    4569                 : ** the destructor will be called before the metadata is dropped.
    4570                 : **
    4571                 : ** ^(In practice, metadata is preserved between function calls for
    4572                 : ** expressions that are constant at compile time. This includes literal
    4573                 : ** values and [parameters].)^
    4574                 : **
    4575                 : ** These routines must be called from the same thread in which
    4576                 : ** the SQL function is running.
    4577                 : */
    4578                 : SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
    4579                 : SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
    4580                 : 
    4581                 : 
    4582                 : /*
    4583                 : ** CAPI3REF: Constants Defining Special Destructor Behavior
    4584                 : **
    4585                 : ** These are special values for the destructor that is passed in as the
    4586                 : ** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
    4587                 : ** argument is SQLITE_STATIC, it means that the content pointer is constant
    4588                 : ** and will never change.  It does not need to be destroyed.  ^The
    4589                 : ** SQLITE_TRANSIENT value means that the content will likely change in
    4590                 : ** the near future and that SQLite should make its own private copy of
    4591                 : ** the content before returning.
    4592                 : **
    4593                 : ** The typedef is necessary to work around problems in certain
    4594                 : ** C++ compilers.  See ticket #2191.
    4595                 : */
    4596                 : typedef void (*sqlite3_destructor_type)(void*);
    4597                 : #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
    4598                 : #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
    4599                 : 
    4600                 : /*
    4601                 : ** CAPI3REF: Setting The Result Of An SQL Function
    4602                 : **
    4603                 : ** These routines are used by the xFunc or xFinal callbacks that
    4604                 : ** implement SQL functions and aggregates.  See
    4605                 : ** [sqlite3_create_function()] and [sqlite3_create_function16()]
    4606                 : ** for additional information.
    4607                 : **
    4608                 : ** These functions work very much like the [parameter binding] family of
    4609                 : ** functions used to bind values to host parameters in prepared statements.
    4610                 : ** Refer to the [SQL parameter] documentation for additional information.
    4611                 : **
    4612                 : ** ^The sqlite3_result_blob() interface sets the result from
    4613                 : ** an application-defined function to be the BLOB whose content is pointed
    4614                 : ** to by the second parameter and which is N bytes long where N is the
    4615                 : ** third parameter.
    4616                 : **
    4617                 : ** ^The sqlite3_result_zeroblob() interfaces set the result of
    4618                 : ** the application-defined function to be a BLOB containing all zero
    4619                 : ** bytes and N bytes in size, where N is the value of the 2nd parameter.
    4620                 : **
    4621                 : ** ^The sqlite3_result_double() interface sets the result from
    4622                 : ** an application-defined function to be a floating point value specified
    4623                 : ** by its 2nd argument.
    4624                 : **
    4625                 : ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
    4626                 : ** cause the implemented SQL function to throw an exception.
    4627                 : ** ^SQLite uses the string pointed to by the
    4628                 : ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
    4629                 : ** as the text of an error message.  ^SQLite interprets the error
    4630                 : ** message string from sqlite3_result_error() as UTF-8. ^SQLite
    4631                 : ** interprets the string from sqlite3_result_error16() as UTF-16 in native
    4632                 : ** byte order.  ^If the third parameter to sqlite3_result_error()
    4633                 : ** or sqlite3_result_error16() is negative then SQLite takes as the error
    4634                 : ** message all text up through the first zero character.
    4635                 : ** ^If the third parameter to sqlite3_result_error() or
    4636                 : ** sqlite3_result_error16() is non-negative then SQLite takes that many
    4637                 : ** bytes (not characters) from the 2nd parameter as the error message.
    4638                 : ** ^The sqlite3_result_error() and sqlite3_result_error16()
    4639                 : ** routines make a private copy of the error message text before
    4640                 : ** they return.  Hence, the calling function can deallocate or
    4641                 : ** modify the text after they return without harm.
    4642                 : ** ^The sqlite3_result_error_code() function changes the error code
    4643                 : ** returned by SQLite as a result of an error in a function.  ^By default,
    4644                 : ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
    4645                 : ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
    4646                 : **
    4647                 : ** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
    4648                 : ** indicating that a string or BLOB is too long to represent.
    4649                 : **
    4650                 : ** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
    4651                 : ** indicating that a memory allocation failed.
    4652                 : **
    4653                 : ** ^The sqlite3_result_int() interface sets the return value
    4654                 : ** of the application-defined function to be the 32-bit signed integer
    4655                 : ** value given in the 2nd argument.
    4656                 : ** ^The sqlite3_result_int64() interface sets the return value
    4657                 : ** of the application-defined function to be the 64-bit signed integer
    4658                 : ** value given in the 2nd argument.
    4659                 : **
    4660                 : ** ^The sqlite3_result_null() interface sets the return value
    4661                 : ** of the application-defined function to be NULL.
    4662                 : **
    4663                 : ** ^The sqlite3_result_text(), sqlite3_result_text16(),
    4664                 : ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
    4665                 : ** set the return value of the application-defined function to be
    4666                 : ** a text string which is represented as UTF-8, UTF-16 native byte order,
    4667                 : ** UTF-16 little endian, or UTF-16 big endian, respectively.
    4668                 : ** ^SQLite takes the text result from the application from
    4669                 : ** the 2nd parameter of the sqlite3_result_text* interfaces.
    4670                 : ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
    4671                 : ** is negative, then SQLite takes result text from the 2nd parameter
    4672                 : ** through the first zero character.
    4673                 : ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
    4674                 : ** is non-negative, then as many bytes (not characters) of the text
    4675                 : ** pointed to by the 2nd parameter are taken as the application-defined
    4676                 : ** function result.  If the 3rd parameter is non-negative, then it
    4677                 : ** must be the byte offset into the string where the NUL terminator would
    4678                 : ** appear if the string where NUL terminated.  If any NUL characters occur
    4679                 : ** in the string at a byte offset that is less than the value of the 3rd
    4680                 : ** parameter, then the resulting string will contain embedded NULs and the
    4681                 : ** result of expressions operating on strings with embedded NULs is undefined.
    4682                 : ** ^If the 4th parameter to the sqlite3_result_text* interfaces
    4683                 : ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
    4684                 : ** function as the destructor on the text or BLOB result when it has
    4685                 : ** finished using that result.
    4686                 : ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
    4687                 : ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
    4688                 : ** assumes that the text or BLOB result is in constant space and does not
    4689                 : ** copy the content of the parameter nor call a destructor on the content
    4690                 : ** when it has finished using that result.
    4691                 : ** ^If the 4th parameter to the sqlite3_result_text* interfaces
    4692                 : ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
    4693                 : ** then SQLite makes a copy of the result into space obtained from
    4694                 : ** from [sqlite3_malloc()] before it returns.
    4695                 : **
    4696                 : ** ^The sqlite3_result_value() interface sets the result of
    4697                 : ** the application-defined function to be a copy the
    4698                 : ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
    4699                 : ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
    4700                 : ** so that the [sqlite3_value] specified in the parameter may change or
    4701                 : ** be deallocated after sqlite3_result_value() returns without harm.
    4702                 : ** ^A [protected sqlite3_value] object may always be used where an
    4703                 : ** [unprotected sqlite3_value] object is required, so either
    4704                 : ** kind of [sqlite3_value] object can be used with this interface.
    4705                 : **
    4706                 : ** If these routines are called from within the different thread
    4707                 : ** than the one containing the application-defined function that received
    4708                 : ** the [sqlite3_context] pointer, the results are undefined.
    4709                 : */
    4710                 : SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
    4711                 : SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
    4712                 : SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
    4713                 : SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
    4714                 : SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
    4715                 : SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
    4716                 : SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
    4717                 : SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
    4718                 : SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
    4719                 : SQLITE_API void sqlite3_result_null(sqlite3_context*);
    4720                 : SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
    4721                 : SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
    4722                 : SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
    4723                 : SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
    4724                 : SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
    4725                 : SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
    4726                 : 
    4727                 : /*
    4728                 : ** CAPI3REF: Define New Collating Sequences
    4729                 : **
    4730                 : ** ^These functions add, remove, or modify a [collation] associated
    4731                 : ** with the [database connection] specified as the first argument.
    4732                 : **
    4733                 : ** ^The name of the collation is a UTF-8 string
    4734                 : ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
    4735                 : ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
    4736                 : ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
    4737                 : ** considered to be the same name.
    4738                 : **
    4739                 : ** ^(The third argument (eTextRep) must be one of the constants:
    4740                 : ** <ul>
    4741                 : ** <li> [SQLITE_UTF8],
    4742                 : ** <li> [SQLITE_UTF16LE],
    4743                 : ** <li> [SQLITE_UTF16BE],
    4744                 : ** <li> [SQLITE_UTF16], or
    4745                 : ** <li> [SQLITE_UTF16_ALIGNED].
    4746                 : ** </ul>)^
    4747                 : ** ^The eTextRep argument determines the encoding of strings passed
    4748                 : ** to the collating function callback, xCallback.
    4749                 : ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
    4750                 : ** force strings to be UTF16 with native byte order.
    4751                 : ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
    4752                 : ** on an even byte address.
    4753                 : **
    4754                 : ** ^The fourth argument, pArg, is an application data pointer that is passed
    4755                 : ** through as the first argument to the collating function callback.
    4756                 : **
    4757                 : ** ^The fifth argument, xCallback, is a pointer to the collating function.
    4758                 : ** ^Multiple collating functions can be registered using the same name but
    4759                 : ** with different eTextRep parameters and SQLite will use whichever
    4760                 : ** function requires the least amount of data transformation.
    4761                 : ** ^If the xCallback argument is NULL then the collating function is
    4762                 : ** deleted.  ^When all collating functions having the same name are deleted,
    4763                 : ** that collation is no longer usable.
    4764                 : **
    4765                 : ** ^The collating function callback is invoked with a copy of the pArg 
    4766                 : ** application data pointer and with two strings in the encoding specified
    4767                 : ** by the eTextRep argument.  The collating function must return an
    4768                 : ** integer that is negative, zero, or positive
    4769                 : ** if the first string is less than, equal to, or greater than the second,
    4770                 : ** respectively.  A collating function must always return the same answer
    4771                 : ** given the same inputs.  If two or more collating functions are registered
    4772                 : ** to the same collation name (using different eTextRep values) then all
    4773                 : ** must give an equivalent answer when invoked with equivalent strings.
    4774                 : ** The collating function must obey the following properties for all
    4775                 : ** strings A, B, and C:
    4776                 : **
    4777                 : ** <ol>
    4778                 : ** <li> If A==B then B==A.
    4779                 : ** <li> If A==B and B==C then A==C.
    4780                 : ** <li> If A&lt;B THEN B&gt;A.
    4781                 : ** <li> If A&lt;B and B&lt;C then A&lt;C.
    4782                 : ** </ol>
    4783                 : **
    4784                 : ** If a collating function fails any of the above constraints and that
    4785                 : ** collating function is  registered and used, then the behavior of SQLite
    4786                 : ** is undefined.
    4787                 : **
    4788                 : ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
    4789                 : ** with the addition that the xDestroy callback is invoked on pArg when
    4790                 : ** the collating function is deleted.
    4791                 : ** ^Collating functions are deleted when they are overridden by later
    4792                 : ** calls to the collation creation functions or when the
    4793                 : ** [database connection] is closed using [sqlite3_close()].
    4794                 : **
    4795                 : ** ^The xDestroy callback is <u>not</u> called if the 
    4796                 : ** sqlite3_create_collation_v2() function fails.  Applications that invoke
    4797                 : ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should 
    4798                 : ** check the return code and dispose of the application data pointer
    4799                 : ** themselves rather than expecting SQLite to deal with it for them.
    4800                 : ** This is different from every other SQLite interface.  The inconsistency 
    4801                 : ** is unfortunate but cannot be changed without breaking backwards 
    4802                 : ** compatibility.
    4803                 : **
    4804                 : ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
    4805                 : */
    4806                 : SQLITE_API int sqlite3_create_collation(
    4807                 :   sqlite3*, 
    4808                 :   const char *zName, 
    4809                 :   int eTextRep, 
    4810                 :   void *pArg,
    4811                 :   int(*xCompare)(void*,int,const void*,int,const void*)
    4812                 : );
    4813                 : SQLITE_API int sqlite3_create_collation_v2(
    4814                 :   sqlite3*, 
    4815                 :   const char *zName, 
    4816                 :   int eTextRep, 
    4817                 :   void *pArg,
    4818                 :   int(*xCompare)(void*,int,const void*,int,const void*),
    4819                 :   void(*xDestroy)(void*)
    4820                 : );
    4821                 : SQLITE_API int sqlite3_create_collation16(
    4822                 :   sqlite3*, 
    4823                 :   const void *zName,
    4824                 :   int eTextRep, 
    4825                 :   void *pArg,
    4826                 :   int(*xCompare)(void*,int,const void*,int,const void*)
    4827                 : );
    4828                 : 
    4829                 : /*
    4830                 : ** CAPI3REF: Collation Needed Callbacks
    4831                 : **
    4832                 : ** ^To avoid having to register all collation sequences before a database
    4833                 : ** can be used, a single callback function may be registered with the
    4834                 : ** [database connection] to be invoked whenever an undefined collation
    4835                 : ** sequence is required.
    4836                 : **
    4837                 : ** ^If the function is registered using the sqlite3_collation_needed() API,
    4838                 : ** then it is passed the names of undefined collation sequences as strings
    4839                 : ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
    4840                 : ** the names are passed as UTF-16 in machine native byte order.
    4841                 : ** ^A call to either function replaces the existing collation-needed callback.
    4842                 : **
    4843                 : ** ^(When the callback is invoked, the first argument passed is a copy
    4844                 : ** of the second argument to sqlite3_collation_needed() or
    4845                 : ** sqlite3_collation_needed16().  The second argument is the database
    4846                 : ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
    4847                 : ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
    4848                 : ** sequence function required.  The fourth parameter is the name of the
    4849                 : ** required collation sequence.)^
    4850                 : **
    4851                 : ** The callback function should register the desired collation using
    4852                 : ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
    4853                 : ** [sqlite3_create_collation_v2()].
    4854                 : */
    4855                 : SQLITE_API int sqlite3_collation_needed(
    4856                 :   sqlite3*, 
    4857                 :   void*, 
    4858                 :   void(*)(void*,sqlite3*,int eTextRep,const char*)
    4859                 : );
    4860                 : SQLITE_API int sqlite3_collation_needed16(
    4861                 :   sqlite3*, 
    4862                 :   void*,
    4863                 :   void(*)(void*,sqlite3*,int eTextRep,const void*)
    4864                 : );
    4865                 : 
    4866                 : #ifdef SQLITE_HAS_CODEC
    4867                 : /*
    4868                 : ** Specify the key for an encrypted database.  This routine should be
    4869                 : ** called right after sqlite3_open().
    4870                 : **
    4871                 : ** The code to implement this API is not available in the public release
    4872                 : ** of SQLite.
    4873                 : */
    4874                 : SQLITE_API int sqlite3_key(
    4875                 :   sqlite3 *db,                   /* Database to be rekeyed */
    4876                 :   const void *pKey, int nKey     /* The key */
    4877                 : );
    4878                 : 
    4879                 : /*
    4880                 : ** Change the key on an open database.  If the current database is not
    4881                 : ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
    4882                 : ** database is decrypted.
    4883                 : **
    4884                 : ** The code to implement this API is not available in the public release
    4885                 : ** of SQLite.
    4886                 : */
    4887                 : SQLITE_API int sqlite3_rekey(
    4888                 :   sqlite3 *db,                   /* Database to be rekeyed */
    4889                 :   const void *pKey, int nKey     /* The new key */
    4890                 : );
    4891                 : 
    4892                 : /*
    4893                 : ** Specify the activation key for a SEE database.  Unless 
    4894                 : ** activated, none of the SEE routines will work.
    4895                 : */
    4896                 : SQLITE_API void sqlite3_activate_see(
    4897                 :   const char *zPassPhrase        /* Activation phrase */
    4898                 : );
    4899                 : #endif
    4900                 : 
    4901                 : #ifdef SQLITE_ENABLE_CEROD
    4902                 : /*
    4903                 : ** Specify the activation key for a CEROD database.  Unless 
    4904                 : ** activated, none of the CEROD routines will work.
    4905                 : */
    4906                 : SQLITE_API void sqlite3_activate_cerod(
    4907                 :   const char *zPassPhrase        /* Activation phrase */
    4908                 : );
    4909                 : #endif
    4910                 : 
    4911                 : /*
    4912                 : ** CAPI3REF: Suspend Execution For A Short Time
    4913                 : **
    4914                 : ** The sqlite3_sleep() function causes the current thread to suspend execution
    4915                 : ** for at least a number of milliseconds specified in its parameter.
    4916                 : **
    4917                 : ** If the operating system does not support sleep requests with
    4918                 : ** millisecond time resolution, then the time will be rounded up to
    4919                 : ** the nearest second. The number of milliseconds of sleep actually
    4920                 : ** requested from the operating system is returned.
    4921                 : **
    4922                 : ** ^SQLite implements this interface by calling the xSleep()
    4923                 : ** method of the default [sqlite3_vfs] object.  If the xSleep() method
    4924                 : ** of the default VFS is not implemented correctly, or not implemented at
    4925                 : ** all, then the behavior of sqlite3_sleep() may deviate from the description
    4926                 : ** in the previous paragraphs.
    4927                 : */
    4928                 : SQLITE_API int sqlite3_sleep(int);
    4929                 : 
    4930                 : /*
    4931                 : ** CAPI3REF: Name Of The Folder Holding Temporary Files
    4932                 : **
    4933                 : ** ^(If this global variable is made to point to a string which is
    4934                 : ** the name of a folder (a.k.a. directory), then all temporary files
    4935                 : ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
    4936                 : ** will be placed in that directory.)^  ^If this variable
    4937                 : ** is a NULL pointer, then SQLite performs a search for an appropriate
    4938                 : ** temporary file directory.
    4939                 : **
    4940                 : ** It is not safe to read or modify this variable in more than one
    4941                 : ** thread at a time.  It is not safe to read or modify this variable
    4942                 : ** if a [database connection] is being used at the same time in a separate
    4943                 : ** thread.
    4944                 : ** It is intended that this variable be set once
    4945                 : ** as part of process initialization and before any SQLite interface
    4946                 : ** routines have been called and that this variable remain unchanged
    4947                 : ** thereafter.
    4948                 : **
    4949                 : ** ^The [temp_store_directory pragma] may modify this variable and cause
    4950                 : ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
    4951                 : ** the [temp_store_directory pragma] always assumes that any string
    4952                 : ** that this variable points to is held in memory obtained from 
    4953                 : ** [sqlite3_malloc] and the pragma may attempt to free that memory
    4954                 : ** using [sqlite3_free].
    4955                 : ** Hence, if this variable is modified directly, either it should be
    4956                 : ** made NULL or made to point to memory obtained from [sqlite3_malloc]
    4957                 : ** or else the use of the [temp_store_directory pragma] should be avoided.
    4958                 : */
    4959                 : SQLITE_API char *sqlite3_temp_directory;
    4960                 : 
    4961                 : /*
    4962                 : ** CAPI3REF: Test For Auto-Commit Mode
    4963                 : ** KEYWORDS: {autocommit mode}
    4964                 : **
    4965                 : ** ^The sqlite3_get_autocommit() interface returns non-zero or
    4966                 : ** zero if the given database connection is or is not in autocommit mode,
    4967                 : ** respectively.  ^Autocommit mode is on by default.
    4968                 : ** ^Autocommit mode is disabled by a [BEGIN] statement.
    4969                 : ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
    4970                 : **
    4971                 : ** If certain kinds of errors occur on a statement within a multi-statement
    4972                 : ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
    4973                 : ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
    4974                 : ** transaction might be rolled back automatically.  The only way to
    4975                 : ** find out whether SQLite automatically rolled back the transaction after
    4976                 : ** an error is to use this function.
    4977                 : **
    4978                 : ** If another thread changes the autocommit status of the database
    4979                 : ** connection while this routine is running, then the return value
    4980                 : ** is undefined.
    4981                 : */
    4982                 : SQLITE_API int sqlite3_get_autocommit(sqlite3*);
    4983                 : 
    4984                 : /*
    4985                 : ** CAPI3REF: Find The Database Handle Of A Prepared Statement
    4986                 : **
    4987                 : ** ^The sqlite3_db_handle interface returns the [database connection] handle
    4988                 : ** to which a [prepared statement] belongs.  ^The [database connection]
    4989                 : ** returned by sqlite3_db_handle is the same [database connection]
    4990                 : ** that was the first argument
    4991                 : ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
    4992                 : ** create the statement in the first place.
    4993                 : */
    4994                 : SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
    4995                 : 
    4996                 : /*
    4997                 : ** CAPI3REF: Return The Filename For A Database Connection
    4998                 : **
    4999                 : ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
    5000                 : ** associated with database N of connection D.  ^The main database file
    5001                 : ** has the name "main".  If there is no attached database N on the database
    5002                 : ** connection D, or if database N is a temporary or in-memory database, then
    5003                 : ** a NULL pointer is returned.
    5004                 : **
    5005                 : ** ^The filename returned by this function is the output of the
    5006                 : ** xFullPathname method of the [VFS].  ^In other words, the filename
    5007                 : ** will be an absolute pathname, even if the filename used
    5008                 : ** to open the database originally was a URI or relative pathname.
    5009                 : */
    5010                 : SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
    5011                 : 
    5012                 : /*
    5013                 : ** CAPI3REF: Find the next prepared statement
    5014                 : **
    5015                 : ** ^This interface returns a pointer to the next [prepared statement] after
    5016                 : ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
    5017                 : ** then this interface returns a pointer to the first prepared statement
    5018                 : ** associated with the database connection pDb.  ^If no prepared statement
    5019                 : ** satisfies the conditions of this routine, it returns NULL.
    5020                 : **
    5021                 : ** The [database connection] pointer D in a call to
    5022                 : ** [sqlite3_next_stmt(D,S)] must refer to an open database
    5023                 : ** connection and in particular must not be a NULL pointer.
    5024                 : */
    5025                 : SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
    5026                 : 
    5027                 : /*
    5028                 : ** CAPI3REF: Commit And Rollback Notification Callbacks
    5029                 : **
    5030                 : ** ^The sqlite3_commit_hook() interface registers a callback
    5031                 : ** function to be invoked whenever a transaction is [COMMIT | committed].
    5032                 : ** ^Any callback set by a previous call to sqlite3_commit_hook()
    5033                 : ** for the same database connection is overridden.
    5034                 : ** ^The sqlite3_rollback_hook() interface registers a callback
    5035                 : ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
    5036                 : ** ^Any callback set by a previous call to sqlite3_rollback_hook()
    5037                 : ** for the same database connection is overridden.
    5038                 : ** ^The pArg argument is passed through to the callback.
    5039                 : ** ^If the callback on a commit hook function returns non-zero,
    5040                 : ** then the commit is converted into a rollback.
    5041                 : **
    5042                 : ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
    5043                 : ** return the P argument from the previous call of the same function
    5044                 : ** on the same [database connection] D, or NULL for
    5045                 : ** the first call for each function on D.
    5046                 : **
    5047                 : ** The commit and rollback hook callbacks are not reentrant.
    5048                 : ** The callback implementation must not do anything that will modify
    5049                 : ** the database connection that invoked the callback.  Any actions
    5050                 : ** to modify the database connection must be deferred until after the
    5051                 : ** completion of the [sqlite3_step()] call that triggered the commit
    5052                 : ** or rollback hook in the first place.
    5053                 : ** Note that running any other SQL statements, including SELECT statements,
    5054                 : ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
    5055                 : ** the database connections for the meaning of "modify" in this paragraph.
    5056                 : **
    5057                 : ** ^Registering a NULL function disables the callback.
    5058                 : **
    5059                 : ** ^When the commit hook callback routine returns zero, the [COMMIT]
    5060                 : ** operation is allowed to continue normally.  ^If the commit hook
    5061                 : ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
    5062                 : ** ^The rollback hook is invoked on a rollback that results from a commit
    5063                 : ** hook returning non-zero, just as it would be with any other rollback.
    5064                 : **
    5065                 : ** ^For the purposes of this API, a transaction is said to have been
    5066                 : ** rolled back if an explicit "ROLLBACK" statement is executed, or
    5067                 : ** an error or constraint causes an implicit rollback to occur.
    5068                 : ** ^The rollback callback is not invoked if a transaction is
    5069                 : ** automatically rolled back because the database connection is closed.
    5070                 : **
    5071                 : ** See also the [sqlite3_update_hook()] interface.
    5072                 : */
    5073                 : SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
    5074                 : SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
    5075                 : 
    5076                 : /*
    5077                 : ** CAPI3REF: Data Change Notification Callbacks
    5078                 : **
    5079                 : ** ^The sqlite3_update_hook() interface registers a callback function
    5080                 : ** with the [database connection] identified by the first argument
    5081                 : ** to be invoked whenever a row is updated, inserted or deleted.
    5082                 : ** ^Any callback set by a previous call to this function
    5083                 : ** for the same database connection is overridden.
    5084                 : **
    5085                 : ** ^The second argument is a pointer to the function to invoke when a
    5086                 : ** row is updated, inserted or deleted.
    5087                 : ** ^The first argument to the callback is a copy of the third argument
    5088                 : ** to sqlite3_update_hook().
    5089                 : ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
    5090                 : ** or [SQLITE_UPDATE], depending on the operation that caused the callback
    5091                 : ** to be invoked.
    5092                 : ** ^The third and fourth arguments to the callback contain pointers to the
    5093                 : ** database and table name containing the affected row.
    5094                 : ** ^The final callback parameter is the [rowid] of the row.
    5095                 : ** ^In the case of an update, this is the [rowid] after the update takes place.
    5096                 : **
    5097                 : ** ^(The update hook is not invoked when internal system tables are
    5098                 : ** modified (i.e. sqlite_master and sqlite_sequence).)^
    5099                 : **
    5100                 : ** ^In the current implementation, the update hook
    5101                 : ** is not invoked when duplication rows are deleted because of an
    5102                 : ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
    5103                 : ** invoked when rows are deleted using the [truncate optimization].
    5104                 : ** The exceptions defined in this paragraph might change in a future
    5105                 : ** release of SQLite.
    5106                 : **
    5107                 : ** The update hook implementation must not do anything that will modify
    5108                 : ** the database connection that invoked the update hook.  Any actions
    5109                 : ** to modify the database connection must be deferred until after the
    5110                 : ** completion of the [sqlite3_step()] call that triggered the update hook.
    5111                 : ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
    5112                 : ** database connections for the meaning of "modify" in this paragraph.
    5113                 : **
    5114                 : ** ^The sqlite3_update_hook(D,C,P) function
    5115                 : ** returns the P argument from the previous call
    5116                 : ** on the same [database connection] D, or NULL for
    5117                 : ** the first call on D.
    5118                 : **
    5119                 : ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
    5120                 : ** interfaces.
    5121                 : */
    5122                 : SQLITE_API void *sqlite3_update_hook(
    5123                 :   sqlite3*, 
    5124                 :   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
    5125                 :   void*
    5126                 : );
    5127                 : 
    5128                 : /*
    5129                 : ** CAPI3REF: Enable Or Disable Shared Pager Cache
    5130                 : ** KEYWORDS: {shared cache}
    5131                 : **
    5132                 : ** ^(This routine enables or disables the sharing of the database cache
    5133                 : ** and schema data structures between [database connection | connections]
    5134                 : ** to the same database. Sharing is enabled if the argument is true
    5135                 : ** and disabled if the argument is false.)^
    5136                 : **
    5137                 : ** ^Cache sharing is enabled and disabled for an entire process.
    5138                 : ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
    5139                 : ** sharing was enabled or disabled for each thread separately.
    5140                 : **
    5141                 : ** ^(The cache sharing mode set by this interface effects all subsequent
    5142                 : ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
    5143                 : ** Existing database connections continue use the sharing mode
    5144                 : ** that was in effect at the time they were opened.)^
    5145                 : **
    5146                 : ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
    5147                 : ** successfully.  An [error code] is returned otherwise.)^
    5148                 : **
    5149                 : ** ^Shared cache is disabled by default. But this might change in
    5150                 : ** future releases of SQLite.  Applications that care about shared
    5151                 : ** cache setting should set it explicitly.
    5152                 : **
    5153                 : ** See Also:  [SQLite Shared-Cache Mode]
    5154                 : */
    5155                 : SQLITE_API int sqlite3_enable_shared_cache(int);
    5156                 : 
    5157                 : /*
    5158                 : ** CAPI3REF: Attempt To Free Heap Memory
    5159                 : **
    5160                 : ** ^The sqlite3_release_memory() interface attempts to free N bytes
    5161                 : ** of heap memory by deallocating non-essential memory allocations
    5162                 : ** held by the database library.   Memory used to cache database
    5163                 : ** pages to improve performance is an example of non-essential memory.
    5164                 : ** ^sqlite3_release_memory() returns the number of bytes actually freed,
    5165                 : ** which might be more or less than the amount requested.
    5166                 : ** ^The sqlite3_release_memory() routine is a no-op returning zero
    5167                 : ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
    5168                 : **
    5169                 : ** See also: [sqlite3_db_release_memory()]
    5170                 : */
    5171                 : SQLITE_API int sqlite3_release_memory(int);
    5172                 : 
    5173                 : /*
    5174                 : ** CAPI3REF: Free Memory Used By A Database Connection
    5175                 : **
    5176                 : ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
    5177                 : ** memory as possible from database connection D. Unlike the
    5178                 : ** [sqlite3_release_memory()] interface, this interface is effect even
    5179                 : ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
    5180                 : ** omitted.
    5181                 : **
    5182                 : ** See also: [sqlite3_release_memory()]
    5183                 : */
    5184                 : SQLITE_API int sqlite3_db_release_memory(sqlite3*);
    5185                 : 
    5186                 : /*
    5187                 : ** CAPI3REF: Impose A Limit On Heap Size
    5188                 : **
    5189                 : ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
    5190                 : ** soft limit on the amount of heap memory that may be allocated by SQLite.
    5191                 : ** ^SQLite strives to keep heap memory utilization below the soft heap
    5192                 : ** limit by reducing the number of pages held in the page cache
    5193                 : ** as heap memory usages approaches the limit.
    5194                 : ** ^The soft heap limit is "soft" because even though SQLite strives to stay
    5195                 : ** below the limit, it will exceed the limit rather than generate
    5196                 : ** an [SQLITE_NOMEM] error.  In other words, the soft heap limit 
    5197                 : ** is advisory only.
    5198                 : **
    5199                 : ** ^The return value from sqlite3_soft_heap_limit64() is the size of
    5200                 : ** the soft heap limit prior to the call, or negative in the case of an
    5201                 : ** error.  ^If the argument N is negative
    5202                 : ** then no change is made to the soft heap limit.  Hence, the current
    5203                 : ** size of the soft heap limit can be determined by invoking
    5204                 : ** sqlite3_soft_heap_limit64() with a negative argument.
    5205                 : **
    5206                 : ** ^If the argument N is zero then the soft heap limit is disabled.
    5207                 : **
    5208                 : ** ^(The soft heap limit is not enforced in the current implementation
    5209                 : ** if one or more of following conditions are true:
    5210                 : **
    5211                 : ** <ul>
    5212                 : ** <li> The soft heap limit is set to zero.
    5213                 : ** <li> Memory accounting is disabled using a combination of the
    5214                 : **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
    5215                 : **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
    5216                 : ** <li> An alternative page cache implementation is specified using
    5217                 : **      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
    5218                 : ** <li> The page cache allocates from its own memory pool supplied
    5219                 : **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
    5220                 : **      from the heap.
    5221                 : ** </ul>)^
    5222                 : **
    5223                 : ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
    5224                 : ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
    5225                 : ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
    5226                 : ** the soft heap limit is enforced on every memory allocation.  Without
    5227                 : ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
    5228                 : ** when memory is allocated by the page cache.  Testing suggests that because
    5229                 : ** the page cache is the predominate memory user in SQLite, most
    5230                 : ** applications will achieve adequate soft heap limit enforcement without
    5231                 : ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
    5232                 : **
    5233                 : ** The circumstances under which SQLite will enforce the soft heap limit may
    5234                 : ** changes in future releases of SQLite.
    5235                 : */
    5236                 : SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
    5237                 : 
    5238                 : /*
    5239                 : ** CAPI3REF: Deprecated Soft Heap Limit Interface
    5240                 : ** DEPRECATED
    5241                 : **
    5242                 : ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
    5243                 : ** interface.  This routine is provided for historical compatibility
    5244                 : ** only.  All new applications should use the
    5245                 : ** [sqlite3_soft_heap_limit64()] interface rather than this one.
    5246                 : */
    5247                 : SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
    5248                 : 
    5249                 : 
    5250                 : /*
    5251                 : ** CAPI3REF: Extract Metadata About A Column Of A Table
    5252                 : **
    5253                 : ** ^This routine returns metadata about a specific column of a specific
    5254                 : ** database table accessible using the [database connection] handle
    5255                 : ** passed as the first function argument.
    5256                 : **
    5257                 : ** ^The column is identified by the second, third and fourth parameters to
    5258                 : ** this function. ^The second parameter is either the name of the database
    5259                 : ** (i.e. "main", "temp", or an attached database) containing the specified
    5260                 : ** table or NULL. ^If it is NULL, then all attached databases are searched
    5261                 : ** for the table using the same algorithm used by the database engine to
    5262                 : ** resolve unqualified table references.
    5263                 : **
    5264                 : ** ^The third and fourth parameters to this function are the table and column
    5265                 : ** name of the desired column, respectively. Neither of these parameters
    5266                 : ** may be NULL.
    5267                 : **
    5268                 : ** ^Metadata is returned by writing to the memory locations passed as the 5th
    5269                 : ** and subsequent parameters to this function. ^Any of these arguments may be
    5270                 : ** NULL, in which case the corresponding element of metadata is omitted.
    5271                 : **
    5272                 : ** ^(<blockquote>
    5273                 : ** <table border="1">
    5274                 : ** <tr><th> Parameter <th> Output<br>Type <th>  Description
    5275                 : **
    5276                 : ** <tr><td> 5th <td> const char* <td> Data type
    5277                 : ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
    5278                 : ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
    5279                 : ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
    5280                 : ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
    5281                 : ** </table>
    5282                 : ** </blockquote>)^
    5283                 : **
    5284                 : ** ^The memory pointed to by the character pointers returned for the
    5285                 : ** declaration type and collation sequence is valid only until the next
    5286                 : ** call to any SQLite API function.
    5287                 : **
    5288                 : ** ^If the specified table is actually a view, an [error code] is returned.
    5289                 : **
    5290                 : ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
    5291                 : ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
    5292                 : ** parameters are set for the explicitly declared column. ^(If there is no
    5293                 : ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
    5294                 : ** parameters are set as follows:
    5295                 : **
    5296                 : ** <pre>
    5297                 : **     data type: "INTEGER"
    5298                 : **     collation sequence: "BINARY"
    5299                 : **     not null: 0
    5300                 : **     primary key: 1
    5301                 : **     auto increment: 0
    5302                 : ** </pre>)^
    5303                 : **
    5304                 : ** ^(This function may load one or more schemas from database files. If an
    5305                 : ** error occurs during this process, or if the requested table or column
    5306                 : ** cannot be found, an [error code] is returned and an error message left
    5307                 : ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
    5308                 : **
    5309                 : ** ^This API is only available if the library was compiled with the
    5310                 : ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
    5311                 : */
    5312                 : SQLITE_API int sqlite3_table_column_metadata(
    5313                 :   sqlite3 *db,                /* Connection handle */
    5314                 :   const char *zDbName,        /* Database name or NULL */
    5315                 :   const char *zTableName,     /* Table name */
    5316                 :   const char *zColumnName,    /* Column name */
    5317                 :   char const **pzDataType,    /* OUTPUT: Declared data type */
    5318                 :   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
    5319                 :   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
    5320                 :   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
    5321                 :   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
    5322                 : );
    5323                 : 
    5324                 : /*
    5325                 : ** CAPI3REF: Load An Extension
    5326                 : **
    5327                 : ** ^This interface loads an SQLite extension library from the named file.
    5328                 : **
    5329                 : ** ^The sqlite3_load_extension() interface attempts to load an
    5330                 : ** SQLite extension library contained in the file zFile.
    5331                 : **
    5332                 : ** ^The entry point is zProc.
    5333                 : ** ^zProc may be 0, in which case the name of the entry point
    5334                 : ** defaults to "sqlite3_extension_init".
    5335                 : ** ^The sqlite3_load_extension() interface returns
    5336                 : ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
    5337                 : ** ^If an error occurs and pzErrMsg is not 0, then the
    5338                 : ** [sqlite3_load_extension()] interface shall attempt to
    5339                 : ** fill *pzErrMsg with error message text stored in memory
    5340                 : ** obtained from [sqlite3_malloc()]. The calling function
    5341                 : ** should free this memory by calling [sqlite3_free()].
    5342                 : **
    5343                 : ** ^Extension loading must be enabled using
    5344                 : ** [sqlite3_enable_load_extension()] prior to calling this API,
    5345                 : ** otherwise an error will be returned.
    5346                 : **
    5347                 : ** See also the [load_extension() SQL function].
    5348                 : */
    5349                 : SQLITE_API int sqlite3_load_extension(
    5350                 :   sqlite3 *db,          /* Load the extension into this database connection */
    5351                 :   const char *zFile,    /* Name of the shared library containing extension */
    5352                 :   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
    5353                 :   char **pzErrMsg       /* Put error message here if not 0 */
    5354                 : );
    5355                 : 
    5356                 : /*
    5357                 : ** CAPI3REF: Enable Or Disable Extension Loading
    5358                 : **
    5359                 : ** ^So as not to open security holes in older applications that are
    5360                 : ** unprepared to deal with extension loading, and as a means of disabling
    5361                 : ** extension loading while evaluating user-entered SQL, the following API
    5362                 : ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
    5363                 : **
    5364                 : ** ^Extension loading is off by default. See ticket #1863.
    5365                 : ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
    5366                 : ** to turn extension loading on and call it with onoff==0 to turn
    5367                 : ** it back off again.
    5368                 : */
    5369                 : SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
    5370                 : 
    5371                 : /*
    5372                 : ** CAPI3REF: Automatically Load Statically Linked Extensions
    5373                 : **
    5374                 : ** ^This interface causes the xEntryPoint() function to be invoked for
    5375                 : ** each new [database connection] that is created.  The idea here is that
    5376                 : ** xEntryPoint() is the entry point for a statically linked SQLite extension
    5377                 : ** that is to be automatically loaded into all new database connections.
    5378                 : **
    5379                 : ** ^(Even though the function prototype shows that xEntryPoint() takes
    5380                 : ** no arguments and returns void, SQLite invokes xEntryPoint() with three
    5381                 : ** arguments and expects and integer result as if the signature of the
    5382                 : ** entry point where as follows:
    5383                 : **
    5384                 : ** <blockquote><pre>
    5385                 : ** &nbsp;  int xEntryPoint(
    5386                 : ** &nbsp;    sqlite3 *db,
    5387                 : ** &nbsp;    const char **pzErrMsg,
    5388                 : ** &nbsp;    const struct sqlite3_api_routines *pThunk
    5389                 : ** &nbsp;  );
    5390                 : ** </pre></blockquote>)^
    5391                 : **
    5392                 : ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
    5393                 : ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
    5394                 : ** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
    5395                 : ** is NULL before calling the xEntryPoint().  ^SQLite will invoke
    5396                 : ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
    5397                 : ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
    5398                 : ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
    5399                 : **
    5400                 : ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
    5401                 : ** on the list of automatic extensions is a harmless no-op. ^No entry point
    5402                 : ** will be called more than once for each database connection that is opened.
    5403                 : **
    5404                 : ** See also: [sqlite3_reset_auto_extension()].
    5405                 : */
    5406                 : SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
    5407                 : 
    5408                 : /*
    5409                 : ** CAPI3REF: Reset Automatic Extension Loading
    5410                 : **
    5411                 : ** ^This interface disables all automatic extensions previously
    5412                 : ** registered using [sqlite3_auto_extension()].
    5413                 : */
    5414                 : SQLITE_API void sqlite3_reset_auto_extension(void);
    5415                 : 
    5416                 : /*
    5417                 : ** The interface to the virtual-table mechanism is currently considered
    5418                 : ** to be experimental.  The interface might change in incompatible ways.
    5419                 : ** If this is a problem for you, do not use the interface at this time.
    5420                 : **
    5421                 : ** When the virtual-table mechanism stabilizes, we will declare the
    5422                 : ** interface fixed, support it indefinitely, and remove this comment.
    5423                 : */
    5424                 : 
    5425                 : /*
    5426                 : ** Structures used by the virtual table interface
    5427                 : */
    5428                 : typedef struct sqlite3_vtab sqlite3_vtab;
    5429                 : typedef struct sqlite3_index_info sqlite3_index_info;
    5430                 : typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
    5431                 : typedef struct sqlite3_module sqlite3_module;
    5432                 : 
    5433                 : /*
    5434                 : ** CAPI3REF: Virtual Table Object
    5435                 : ** KEYWORDS: sqlite3_module {virtual table module}
    5436                 : **
    5437                 : ** This structure, sometimes called a "virtual table module", 
    5438                 : ** defines the implementation of a [virtual tables].  
    5439                 : ** This structure consists mostly of methods for the module.
    5440                 : **
    5441                 : ** ^A virtual table module is created by filling in a persistent
    5442                 : ** instance of this structure and passing a pointer to that instance
    5443                 : ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
    5444                 : ** ^The registration remains valid until it is replaced by a different
    5445                 : ** module or until the [database connection] closes.  The content
    5446                 : ** of this structure must not change while it is registered with
    5447                 : ** any database connection.
    5448                 : */
    5449                 : struct sqlite3_module {
    5450                 :   int iVersion;
    5451                 :   int (*xCreate)(sqlite3*, void *pAux,
    5452                 :                int argc, const char *const*argv,
    5453                 :                sqlite3_vtab **ppVTab, char**);
    5454                 :   int (*xConnect)(sqlite3*, void *pAux,
    5455                 :                int argc, const char *const*argv,
    5456                 :                sqlite3_vtab **ppVTab, char**);
    5457                 :   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
    5458                 :   int (*xDisconnect)(sqlite3_vtab *pVTab);
    5459                 :   int (*xDestroy)(sqlite3_vtab *pVTab);
    5460                 :   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
    5461                 :   int (*xClose)(sqlite3_vtab_cursor*);
    5462                 :   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
    5463                 :                 int argc, sqlite3_value **argv);
    5464                 :   int (*xNext)(sqlite3_vtab_cursor*);
    5465                 :   int (*xEof)(sqlite3_vtab_cursor*);
    5466                 :   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
    5467                 :   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
    5468                 :   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
    5469                 :   int (*xBegin)(sqlite3_vtab *pVTab);
    5470                 :   int (*xSync)(sqlite3_vtab *pVTab);
    5471                 :   int (*xCommit)(sqlite3_vtab *pVTab);
    5472                 :   int (*xRollback)(sqlite3_vtab *pVTab);
    5473                 :   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
    5474                 :                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
    5475                 :                        void **ppArg);
    5476                 :   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
    5477                 :   /* The methods above are in version 1 of the sqlite_module object. Those 
    5478                 :   ** below are for version 2 and greater. */
    5479                 :   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
    5480                 :   int (*xRelease)(sqlite3_vtab *pVTab, int);
    5481                 :   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
    5482                 : };
    5483                 : 
    5484                 : /*
    5485                 : ** CAPI3REF: Virtual Table Indexing Information
    5486                 : ** KEYWORDS: sqlite3_index_info
    5487                 : **
    5488                 : ** The sqlite3_index_info structure and its substructures is used as part
    5489                 : ** of the [virtual table] interface to
    5490                 : ** pass information into and receive the reply from the [xBestIndex]
    5491                 : ** method of a [virtual table module].  The fields under **Inputs** are the
    5492                 : ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
    5493                 : ** results into the **Outputs** fields.
    5494                 : **
    5495                 : ** ^(The aConstraint[] array records WHERE clause constraints of the form:
    5496                 : **
    5497                 : ** <blockquote>column OP expr</blockquote>
    5498                 : **
    5499                 : ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
    5500                 : ** stored in aConstraint[].op using one of the
    5501                 : ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
    5502                 : ** ^(The index of the column is stored in
    5503                 : ** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
    5504                 : ** expr on the right-hand side can be evaluated (and thus the constraint
    5505                 : ** is usable) and false if it cannot.)^
    5506                 : **
    5507                 : ** ^The optimizer automatically inverts terms of the form "expr OP column"
    5508                 : ** and makes other simplifications to the WHERE clause in an attempt to
    5509                 : ** get as many WHERE clause terms into the form shown above as possible.
    5510                 : ** ^The aConstraint[] array only reports WHERE clause terms that are
    5511                 : ** relevant to the particular virtual table being queried.
    5512                 : **
    5513                 : ** ^Information about the ORDER BY clause is stored in aOrderBy[].
    5514                 : ** ^Each term of aOrderBy records a column of the ORDER BY clause.
    5515                 : **
    5516                 : ** The [xBestIndex] method must fill aConstraintUsage[] with information
    5517                 : ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
    5518                 : ** the right-hand side of the corresponding aConstraint[] is evaluated
    5519                 : ** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
    5520                 : ** is true, then the constraint is assumed to be fully handled by the
    5521                 : ** virtual table and is not checked again by SQLite.)^
    5522                 : **
    5523                 : ** ^The idxNum and idxPtr values are recorded and passed into the
    5524                 : ** [xFilter] method.
    5525                 : ** ^[sqlite3_free()] is used to free idxPtr if and only if
    5526                 : ** needToFreeIdxPtr is true.
    5527                 : **
    5528                 : ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
    5529                 : ** the correct order to satisfy the ORDER BY clause so that no separate
    5530                 : ** sorting step is required.
    5531                 : **
    5532                 : ** ^The estimatedCost value is an estimate of the cost of doing the
    5533                 : ** particular lookup.  A full scan of a table with N entries should have
    5534                 : ** a cost of N.  A binary search of a table of N entries should have a
    5535                 : ** cost of approximately log(N).
    5536                 : */
    5537                 : struct sqlite3_index_info {
    5538                 :   /* Inputs */
    5539                 :   int nConstraint;           /* Number of entries in aConstraint */
    5540                 :   struct sqlite3_index_constraint {
    5541                 :      int iColumn;              /* Column on left-hand side of constraint */
    5542                 :      unsigned char op;         /* Constraint operator */
    5543                 :      unsigned char usable;     /* True if this constraint is usable */
    5544                 :      int iTermOffset;          /* Used internally - xBestIndex should ignore */
    5545                 :   } *aConstraint;            /* Table of WHERE clause constraints */
    5546                 :   int nOrderBy;              /* Number of terms in the ORDER BY clause */
    5547                 :   struct sqlite3_index_orderby {
    5548                 :      int iColumn;              /* Column number */
    5549                 :      unsigned char desc;       /* True for DESC.  False for ASC. */
    5550                 :   } *aOrderBy;               /* The ORDER BY clause */
    5551                 :   /* Outputs */
    5552                 :   struct sqlite3_index_constraint_usage {
    5553                 :     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
    5554                 :     unsigned char omit;      /* Do not code a test for this constraint */
    5555                 :   } *aConstraintUsage;
    5556                 :   int idxNum;                /* Number used to identify the index */
    5557                 :   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
    5558                 :   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
    5559                 :   int orderByConsumed;       /* True if output is already ordered */
    5560                 :   double estimatedCost;      /* Estimated cost of using this index */
    5561                 : };
    5562                 : 
    5563                 : /*
    5564                 : ** CAPI3REF: Virtual Table Constraint Operator Codes
    5565                 : **
    5566                 : ** These macros defined the allowed values for the
    5567                 : ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
    5568                 : ** an operator that is part of a constraint term in the wHERE clause of
    5569                 : ** a query that uses a [virtual table].
    5570                 : */
    5571                 : #define SQLITE_INDEX_CONSTRAINT_EQ    2
    5572                 : #define SQLITE_INDEX_CONSTRAINT_GT    4
    5573                 : #define SQLITE_INDEX_CONSTRAINT_LE    8
    5574                 : #define SQLITE_INDEX_CONSTRAINT_LT    16
    5575                 : #define SQLITE_INDEX_CONSTRAINT_GE    32
    5576                 : #define SQLITE_INDEX_CONSTRAINT_MATCH 64
    5577                 : 
    5578                 : /*
    5579                 : ** CAPI3REF: Register A Virtual Table Implementation
    5580                 : **
    5581                 : ** ^These routines are used to register a new [virtual table module] name.
    5582                 : ** ^Module names must be registered before
    5583                 : ** creating a new [virtual table] using the module and before using a
    5584                 : ** preexisting [virtual table] for the module.
    5585                 : **
    5586                 : ** ^The module name is registered on the [database connection] specified
    5587                 : ** by the first parameter.  ^The name of the module is given by the 
    5588                 : ** second parameter.  ^The third parameter is a pointer to
    5589                 : ** the implementation of the [virtual table module].   ^The fourth
    5590                 : ** parameter is an arbitrary client data pointer that is passed through
    5591                 : ** into the [xCreate] and [xConnect] methods of the virtual table module
    5592                 : ** when a new virtual table is be being created or reinitialized.
    5593                 : **
    5594                 : ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
    5595                 : ** is a pointer to a destructor for the pClientData.  ^SQLite will
    5596                 : ** invoke the destructor function (if it is not NULL) when SQLite
    5597                 : ** no longer needs the pClientData pointer.  ^The destructor will also
    5598                 : ** be invoked if the call to sqlite3_create_module_v2() fails.
    5599                 : ** ^The sqlite3_create_module()
    5600                 : ** interface is equivalent to sqlite3_create_module_v2() with a NULL
    5601                 : ** destructor.
    5602                 : */
    5603                 : SQLITE_API int sqlite3_create_module(
    5604                 :   sqlite3 *db,               /* SQLite connection to register module with */
    5605                 :   const char *zName,         /* Name of the module */
    5606                 :   const sqlite3_module *p,   /* Methods for the module */
    5607                 :   void *pClientData          /* Client data for xCreate/xConnect */
    5608                 : );
    5609                 : SQLITE_API int sqlite3_create_module_v2(
    5610                 :   sqlite3 *db,               /* SQLite connection to register module with */
    5611                 :   const char *zName,         /* Name of the module */
    5612                 :   const sqlite3_module *p,   /* Methods for the module */
    5613                 :   void *pClientData,         /* Client data for xCreate/xConnect */
    5614                 :   void(*xDestroy)(void*)     /* Module destructor function */
    5615                 : );
    5616                 : 
    5617                 : /*
    5618                 : ** CAPI3REF: Virtual Table Instance Object
    5619                 : ** KEYWORDS: sqlite3_vtab
    5620                 : **
    5621                 : ** Every [virtual table module] implementation uses a subclass
    5622                 : ** of this object to describe a particular instance
    5623                 : ** of the [virtual table].  Each subclass will
    5624                 : ** be tailored to the specific needs of the module implementation.
    5625                 : ** The purpose of this superclass is to define certain fields that are
    5626                 : ** common to all module implementations.
    5627                 : **
    5628                 : ** ^Virtual tables methods can set an error message by assigning a
    5629                 : ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
    5630                 : ** take care that any prior string is freed by a call to [sqlite3_free()]
    5631                 : ** prior to assigning a new string to zErrMsg.  ^After the error message
    5632                 : ** is delivered up to the client application, the string will be automatically
    5633                 : ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
    5634                 : */
    5635                 : struct sqlite3_vtab {
    5636                 :   const sqlite3_module *pModule;  /* The module for this virtual table */
    5637                 :   int nRef;                       /* NO LONGER USED */
    5638                 :   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
    5639                 :   /* Virtual table implementations will typically add additional fields */
    5640                 : };
    5641                 : 
    5642                 : /*
    5643                 : ** CAPI3REF: Virtual Table Cursor Object
    5644                 : ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
    5645                 : **
    5646                 : ** Every [virtual table module] implementation uses a subclass of the
    5647                 : ** following structure to describe cursors that point into the
    5648                 : ** [virtual table] and are used
    5649                 : ** to loop through the virtual table.  Cursors are created using the
    5650                 : ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
    5651                 : ** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
    5652                 : ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
    5653                 : ** of the module.  Each module implementation will define
    5654                 : ** the content of a cursor structure to suit its own needs.
    5655                 : **
    5656                 : ** This superclass exists in order to define fields of the cursor that
    5657                 : ** are common to all implementations.
    5658                 : */
    5659                 : struct sqlite3_vtab_cursor {
    5660                 :   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
    5661                 :   /* Virtual table implementations will typically add additional fields */
    5662                 : };
    5663                 : 
    5664                 : /*
    5665                 : ** CAPI3REF: Declare The Schema Of A Virtual Table
    5666                 : **
    5667                 : ** ^The [xCreate] and [xConnect] methods of a
    5668                 : ** [virtual table module] call this interface
    5669                 : ** to declare the format (the names and datatypes of the columns) of
    5670                 : ** the virtual tables they implement.
    5671                 : */
    5672                 : SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
    5673                 : 
    5674                 : /*
    5675                 : ** CAPI3REF: Overload A Function For A Virtual Table
    5676                 : **
    5677                 : ** ^(Virtual tables can provide alternative implementations of functions
    5678                 : ** using the [xFindFunction] method of the [virtual table module].  
    5679                 : ** But global versions of those functions
    5680                 : ** must exist in order to be overloaded.)^
    5681                 : **
    5682                 : ** ^(This API makes sure a global version of a function with a particular
    5683                 : ** name and number of parameters exists.  If no such function exists
    5684                 : ** before this API is called, a new function is created.)^  ^The implementation
    5685                 : ** of the new function always causes an exception to be thrown.  So
    5686                 : ** the new function is not good for anything by itself.  Its only
    5687                 : ** purpose is to be a placeholder function that can be overloaded
    5688                 : ** by a [virtual table].
    5689                 : */
    5690                 : SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
    5691                 : 
    5692                 : /*
    5693                 : ** The interface to the virtual-table mechanism defined above (back up
    5694                 : ** to a comment remarkably similar to this one) is currently considered
    5695                 : ** to be experimental.  The interface might change in incompatible ways.
    5696                 : ** If this is a problem for you, do not use the interface at this time.
    5697                 : **
    5698                 : ** When the virtual-table mechanism stabilizes, we will declare the
    5699                 : ** interface fixed, support it indefinitely, and remove this comment.
    5700                 : */
    5701                 : 
    5702                 : /*
    5703                 : ** CAPI3REF: A Handle To An Open BLOB
    5704                 : ** KEYWORDS: {BLOB handle} {BLOB handles}
    5705                 : **
    5706                 : ** An instance of this object represents an open BLOB on which
    5707                 : ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
    5708                 : ** ^Objects of this type are created by [sqlite3_blob_open()]
    5709                 : ** and destroyed by [sqlite3_blob_close()].
    5710                 : ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
    5711                 : ** can be used to read or write small subsections of the BLOB.
    5712                 : ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
    5713                 : */
    5714                 : typedef struct sqlite3_blob sqlite3_blob;
    5715                 : 
    5716                 : /*
    5717                 : ** CAPI3REF: Open A BLOB For Incremental I/O
    5718                 : **
    5719                 : ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
    5720                 : ** in row iRow, column zColumn, table zTable in database zDb;
    5721                 : ** in other words, the same BLOB that would be selected by:
    5722                 : **
    5723                 : ** <pre>
    5724                 : **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
    5725                 : ** </pre>)^
    5726                 : **
    5727                 : ** ^If the flags parameter is non-zero, then the BLOB is opened for read
    5728                 : ** and write access. ^If it is zero, the BLOB is opened for read access.
    5729                 : ** ^It is not possible to open a column that is part of an index or primary 
    5730                 : ** key for writing. ^If [foreign key constraints] are enabled, it is 
    5731                 : ** not possible to open a column that is part of a [child key] for writing.
    5732                 : **
    5733                 : ** ^Note that the database name is not the filename that contains
    5734                 : ** the database but rather the symbolic name of the database that
    5735                 : ** appears after the AS keyword when the database is connected using [ATTACH].
    5736                 : ** ^For the main database file, the database name is "main".
    5737                 : ** ^For TEMP tables, the database name is "temp".
    5738                 : **
    5739                 : ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
    5740                 : ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
    5741                 : ** to be a null pointer.)^
    5742                 : ** ^This function sets the [database connection] error code and message
    5743                 : ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
    5744                 : ** functions. ^Note that the *ppBlob variable is always initialized in a
    5745                 : ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
    5746                 : ** regardless of the success or failure of this routine.
    5747                 : **
    5748                 : ** ^(If the row that a BLOB handle points to is modified by an
    5749                 : ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
    5750                 : ** then the BLOB handle is marked as "expired".
    5751                 : ** This is true if any column of the row is changed, even a column
    5752                 : ** other than the one the BLOB handle is open on.)^
    5753                 : ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
    5754                 : ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
    5755                 : ** ^(Changes written into a BLOB prior to the BLOB expiring are not
    5756                 : ** rolled back by the expiration of the BLOB.  Such changes will eventually
    5757                 : ** commit if the transaction continues to completion.)^
    5758                 : **
    5759                 : ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
    5760                 : ** the opened blob.  ^The size of a blob may not be changed by this
    5761                 : ** interface.  Use the [UPDATE] SQL command to change the size of a
    5762                 : ** blob.
    5763                 : **
    5764                 : ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
    5765                 : ** and the built-in [zeroblob] SQL function can be used, if desired,
    5766                 : ** to create an empty, zero-filled blob in which to read or write using
    5767                 : ** this interface.
    5768                 : **
    5769                 : ** To avoid a resource leak, every open [BLOB handle] should eventually
    5770                 : ** be released by a call to [sqlite3_blob_close()].
    5771                 : */
    5772                 : SQLITE_API int sqlite3_blob_open(
    5773                 :   sqlite3*,
    5774                 :   const char *zDb,
    5775                 :   const char *zTable,
    5776                 :   const char *zColumn,
    5777                 :   sqlite3_int64 iRow,
    5778                 :   int flags,
    5779                 :   sqlite3_blob **ppBlob
    5780                 : );
    5781                 : 
    5782                 : /*
    5783                 : ** CAPI3REF: Move a BLOB Handle to a New Row
    5784                 : **
    5785                 : ** ^This function is used to move an existing blob handle so that it points
    5786                 : ** to a different row of the same database table. ^The new row is identified
    5787                 : ** by the rowid value passed as the second argument. Only the row can be
    5788                 : ** changed. ^The database, table and column on which the blob handle is open
    5789                 : ** remain the same. Moving an existing blob handle to a new row can be
    5790                 : ** faster than closing the existing handle and opening a new one.
    5791                 : **
    5792                 : ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
    5793                 : ** it must exist and there must be either a blob or text value stored in
    5794                 : ** the nominated column.)^ ^If the new row is not present in the table, or if
    5795                 : ** it does not contain a blob or text value, or if another error occurs, an
    5796                 : ** SQLite error code is returned and the blob handle is considered aborted.
    5797                 : ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
    5798                 : ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
    5799                 : ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
    5800                 : ** always returns zero.
    5801                 : **
    5802                 : ** ^This function sets the database handle error code and message.
    5803                 : */
    5804                 : SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
    5805                 : 
    5806                 : /*
    5807                 : ** CAPI3REF: Close A BLOB Handle
    5808                 : **
    5809                 : ** ^Closes an open [BLOB handle].
    5810                 : **
    5811                 : ** ^Closing a BLOB shall cause the current transaction to commit
    5812                 : ** if there are no other BLOBs, no pending prepared statements, and the
    5813                 : ** database connection is in [autocommit mode].
    5814                 : ** ^If any writes were made to the BLOB, they might be held in cache
    5815                 : ** until the close operation if they will fit.
    5816                 : **
    5817                 : ** ^(Closing the BLOB often forces the changes
    5818                 : ** out to disk and so if any I/O errors occur, they will likely occur
    5819                 : ** at the time when the BLOB is closed.  Any errors that occur during
    5820                 : ** closing are reported as a non-zero return value.)^
    5821                 : **
    5822                 : ** ^(The BLOB is closed unconditionally.  Even if this routine returns
    5823                 : ** an error code, the BLOB is still closed.)^
    5824                 : **
    5825                 : ** ^Calling this routine with a null pointer (such as would be returned
    5826                 : ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
    5827                 : */
    5828                 : SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
    5829                 : 
    5830                 : /*
    5831                 : ** CAPI3REF: Return The Size Of An Open BLOB
    5832                 : **
    5833                 : ** ^Returns the size in bytes of the BLOB accessible via the 
    5834                 : ** successfully opened [BLOB handle] in its only argument.  ^The
    5835                 : ** incremental blob I/O routines can only read or overwriting existing
    5836                 : ** blob content; they cannot change the size of a blob.
    5837                 : **
    5838                 : ** This routine only works on a [BLOB handle] which has been created
    5839                 : ** by a prior successful call to [sqlite3_blob_open()] and which has not
    5840                 : ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
    5841                 : ** to this routine results in undefined and probably undesirable behavior.
    5842                 : */
    5843                 : SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
    5844                 : 
    5845                 : /*
    5846                 : ** CAPI3REF: Read Data From A BLOB Incrementally
    5847                 : **
    5848                 : ** ^(This function is used to read data from an open [BLOB handle] into a
    5849                 : ** caller-supplied buffer. N bytes of data are copied into buffer Z
    5850                 : ** from the open BLOB, starting at offset iOffset.)^
    5851                 : **
    5852                 : ** ^If offset iOffset is less than N bytes from the end of the BLOB,
    5853                 : ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
    5854                 : ** less than zero, [SQLITE_ERROR] is returned and no data is read.
    5855                 : ** ^The size of the blob (and hence the maximum value of N+iOffset)
    5856                 : ** can be determined using the [sqlite3_blob_bytes()] interface.
    5857                 : **
    5858                 : ** ^An attempt to read from an expired [BLOB handle] fails with an
    5859                 : ** error code of [SQLITE_ABORT].
    5860                 : **
    5861                 : ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
    5862                 : ** Otherwise, an [error code] or an [extended error code] is returned.)^
    5863                 : **
    5864                 : ** This routine only works on a [BLOB handle] which has been created
    5865                 : ** by a prior successful call to [sqlite3_blob_open()] and which has not
    5866                 : ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
    5867                 : ** to this routine results in undefined and probably undesirable behavior.
    5868                 : **
    5869                 : ** See also: [sqlite3_blob_write()].
    5870                 : */
    5871                 : SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
    5872                 : 
    5873                 : /*
    5874                 : ** CAPI3REF: Write Data Into A BLOB Incrementally
    5875                 : **
    5876                 : ** ^This function is used to write data into an open [BLOB handle] from a
    5877                 : ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
    5878                 : ** into the open BLOB, starting at offset iOffset.
    5879                 : **
    5880                 : ** ^If the [BLOB handle] passed as the first argument was not opened for
    5881                 : ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
    5882                 : ** this function returns [SQLITE_READONLY].
    5883                 : **
    5884                 : ** ^This function may only modify the contents of the BLOB; it is
    5885                 : ** not possible to increase the size of a BLOB using this API.
    5886                 : ** ^If offset iOffset is less than N bytes from the end of the BLOB,
    5887                 : ** [SQLITE_ERROR] is returned and no data is written.  ^If N is
    5888                 : ** less than zero [SQLITE_ERROR] is returned and no data is written.
    5889                 : ** The size of the BLOB (and hence the maximum value of N+iOffset)
    5890                 : ** can be determined using the [sqlite3_blob_bytes()] interface.
    5891                 : **
    5892                 : ** ^An attempt to write to an expired [BLOB handle] fails with an
    5893                 : ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
    5894                 : ** before the [BLOB handle] expired are not rolled back by the
    5895                 : ** expiration of the handle, though of course those changes might
    5896                 : ** have been overwritten by the statement that expired the BLOB handle
    5897                 : ** or by other independent statements.
    5898                 : **
    5899                 : ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
    5900                 : ** Otherwise, an  [error code] or an [extended error code] is returned.)^
    5901                 : **
    5902                 : ** This routine only works on a [BLOB handle] which has been created
    5903                 : ** by a prior successful call to [sqlite3_blob_open()] and which has not
    5904                 : ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
    5905                 : ** to this routine results in undefined and probably undesirable behavior.
    5906                 : **
    5907                 : ** See also: [sqlite3_blob_read()].
    5908                 : */
    5909                 : SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
    5910                 : 
    5911                 : /*
    5912                 : ** CAPI3REF: Virtual File System Objects
    5913                 : **
    5914                 : ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
    5915                 : ** that SQLite uses to interact
    5916                 : ** with the underlying operating system.  Most SQLite builds come with a
    5917                 : ** single default VFS that is appropriate for the host computer.
    5918                 : ** New VFSes can be registered and existing VFSes can be unregistered.
    5919                 : ** The following interfaces are provided.
    5920                 : **
    5921                 : ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
    5922                 : ** ^Names are case sensitive.
    5923                 : ** ^Names are zero-terminated UTF-8 strings.
    5924                 : ** ^If there is no match, a NULL pointer is returned.
    5925                 : ** ^If zVfsName is NULL then the default VFS is returned.
    5926                 : **
    5927                 : ** ^New VFSes are registered with sqlite3_vfs_register().
    5928                 : ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
    5929                 : ** ^The same VFS can be registered multiple times without injury.
    5930                 : ** ^To make an existing VFS into the default VFS, register it again
    5931                 : ** with the makeDflt flag set.  If two different VFSes with the
    5932                 : ** same name are registered, the behavior is undefined.  If a
    5933                 : ** VFS is registered with a name that is NULL or an empty string,
    5934                 : ** then the behavior is undefined.
    5935                 : **
    5936                 : ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
    5937                 : ** ^(If the default VFS is unregistered, another VFS is chosen as
    5938                 : ** the default.  The choice for the new VFS is arbitrary.)^
    5939                 : */
    5940                 : SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
    5941                 : SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
    5942                 : SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
    5943                 : 
    5944                 : /*
    5945                 : ** CAPI3REF: Mutexes
    5946                 : **
    5947                 : ** The SQLite core uses these routines for thread
    5948                 : ** synchronization. Though they are intended for internal
    5949                 : ** use by SQLite, code that links against SQLite is
    5950                 : ** permitted to use any of these routines.
    5951                 : **
    5952                 : ** The SQLite source code contains multiple implementations
    5953                 : ** of these mutex routines.  An appropriate implementation
    5954                 : ** is selected automatically at compile-time.  ^(The following
    5955                 : ** implementations are available in the SQLite core:
    5956                 : **
    5957                 : ** <ul>
    5958                 : ** <li>   SQLITE_MUTEX_OS2
    5959                 : ** <li>   SQLITE_MUTEX_PTHREADS
    5960                 : ** <li>   SQLITE_MUTEX_W32
    5961                 : ** <li>   SQLITE_MUTEX_NOOP
    5962                 : ** </ul>)^
    5963                 : **
    5964                 : ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
    5965                 : ** that does no real locking and is appropriate for use in
    5966                 : ** a single-threaded application.  ^The SQLITE_MUTEX_OS2,
    5967                 : ** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
    5968                 : ** are appropriate for use on OS/2, Unix, and Windows.
    5969                 : **
    5970                 : ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
    5971                 : ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
    5972                 : ** implementation is included with the library. In this case the
    5973                 : ** application must supply a custom mutex implementation using the
    5974                 : ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
    5975                 : ** before calling sqlite3_initialize() or any other public sqlite3_
    5976                 : ** function that calls sqlite3_initialize().)^
    5977                 : **
    5978                 : ** ^The sqlite3_mutex_alloc() routine allocates a new
    5979                 : ** mutex and returns a pointer to it. ^If it returns NULL
    5980                 : ** that means that a mutex could not be allocated.  ^SQLite
    5981                 : ** will unwind its stack and return an error.  ^(The argument
    5982                 : ** to sqlite3_mutex_alloc() is one of these integer constants:
    5983                 : **
    5984                 : ** <ul>
    5985                 : ** <li>  SQLITE_MUTEX_FAST
    5986                 : ** <li>  SQLITE_MUTEX_RECURSIVE
    5987                 : ** <li>  SQLITE_MUTEX_STATIC_MASTER
    5988                 : ** <li>  SQLITE_MUTEX_STATIC_MEM
    5989                 : ** <li>  SQLITE_MUTEX_STATIC_MEM2
    5990                 : ** <li>  SQLITE_MUTEX_STATIC_PRNG
    5991                 : ** <li>  SQLITE_MUTEX_STATIC_LRU
    5992                 : ** <li>  SQLITE_MUTEX_STATIC_LRU2
    5993                 : ** </ul>)^
    5994                 : **
    5995                 : ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
    5996                 : ** cause sqlite3_mutex_alloc() to create
    5997                 : ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
    5998                 : ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
    5999                 : ** The mutex implementation does not need to make a distinction
    6000                 : ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
    6001                 : ** not want to.  ^SQLite will only request a recursive mutex in
    6002                 : ** cases where it really needs one.  ^If a faster non-recursive mutex
    6003                 : ** implementation is available on the host platform, the mutex subsystem
    6004                 : ** might return such a mutex in response to SQLITE_MUTEX_FAST.
    6005                 : **
    6006                 : ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
    6007                 : ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
    6008                 : ** a pointer to a static preexisting mutex.  ^Six static mutexes are
    6009                 : ** used by the current version of SQLite.  Future versions of SQLite
    6010                 : ** may add additional static mutexes.  Static mutexes are for internal
    6011                 : ** use by SQLite only.  Applications that use SQLite mutexes should
    6012                 : ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
    6013                 : ** SQLITE_MUTEX_RECURSIVE.
    6014                 : **
    6015                 : ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
    6016                 : ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
    6017                 : ** returns a different mutex on every call.  ^But for the static
    6018                 : ** mutex types, the same mutex is returned on every call that has
    6019                 : ** the same type number.
    6020                 : **
    6021                 : ** ^The sqlite3_mutex_free() routine deallocates a previously
    6022                 : ** allocated dynamic mutex.  ^SQLite is careful to deallocate every
    6023                 : ** dynamic mutex that it allocates.  The dynamic mutexes must not be in
    6024                 : ** use when they are deallocated.  Attempting to deallocate a static
    6025                 : ** mutex results in undefined behavior.  ^SQLite never deallocates
    6026                 : ** a static mutex.
    6027                 : **
    6028                 : ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
    6029                 : ** to enter a mutex.  ^If another thread is already within the mutex,
    6030                 : ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
    6031                 : ** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
    6032                 : ** upon successful entry.  ^(Mutexes created using
    6033                 : ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
    6034                 : ** In such cases the,
    6035                 : ** mutex must be exited an equal number of times before another thread
    6036                 : ** can enter.)^  ^(If the same thread tries to enter any other
    6037                 : ** kind of mutex more than once, the behavior is undefined.
    6038                 : ** SQLite will never exhibit
    6039                 : ** such behavior in its own use of mutexes.)^
    6040                 : **
    6041                 : ** ^(Some systems (for example, Windows 95) do not support the operation
    6042                 : ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
    6043                 : ** will always return SQLITE_BUSY.  The SQLite core only ever uses
    6044                 : ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
    6045                 : **
    6046                 : ** ^The sqlite3_mutex_leave() routine exits a mutex that was
    6047                 : ** previously entered by the same thread.   ^(The behavior
    6048                 : ** is undefined if the mutex is not currently entered by the
    6049                 : ** calling thread or is not currently allocated.  SQLite will
    6050                 : ** never do either.)^
    6051                 : **
    6052                 : ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
    6053                 : ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
    6054                 : ** behave as no-ops.
    6055                 : **
    6056                 : ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
    6057                 : */
    6058                 : SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
    6059                 : SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
    6060                 : SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
    6061                 : SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
    6062                 : SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
    6063                 : 
    6064                 : /*
    6065                 : ** CAPI3REF: Mutex Methods Object
    6066                 : **
    6067                 : ** An instance of this structure defines the low-level routines
    6068                 : ** used to allocate and use mutexes.
    6069                 : **
    6070                 : ** Usually, the default mutex implementations provided by SQLite are
    6071                 : ** sufficient, however the user has the option of substituting a custom
    6072                 : ** implementation for specialized deployments or systems for which SQLite
    6073                 : ** does not provide a suitable implementation. In this case, the user
    6074                 : ** creates and populates an instance of this structure to pass
    6075                 : ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
    6076                 : ** Additionally, an instance of this structure can be used as an
    6077                 : ** output variable when querying the system for the current mutex
    6078                 : ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
    6079                 : **
    6080                 : ** ^The xMutexInit method defined by this structure is invoked as
    6081                 : ** part of system initialization by the sqlite3_initialize() function.
    6082                 : ** ^The xMutexInit routine is called by SQLite exactly once for each
    6083                 : ** effective call to [sqlite3_initialize()].
    6084                 : **
    6085                 : ** ^The xMutexEnd method defined by this structure is invoked as
    6086                 : ** part of system shutdown by the sqlite3_shutdown() function. The
    6087                 : ** implementation of this method is expected to release all outstanding
    6088                 : ** resources obtained by the mutex methods implementation, especially
    6089                 : ** those obtained by the xMutexInit method.  ^The xMutexEnd()
    6090                 : ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
    6091                 : **
    6092                 : ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
    6093                 : ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
    6094                 : ** xMutexNotheld) implement the following interfaces (respectively):
    6095                 : **
    6096                 : ** <ul>
    6097                 : **   <li>  [sqlite3_mutex_alloc()] </li>
    6098                 : **   <li>  [sqlite3_mutex_free()] </li>
    6099                 : **   <li>  [sqlite3_mutex_enter()] </li>
    6100                 : **   <li>  [sqlite3_mutex_try()] </li>
    6101                 : **   <li>  [sqlite3_mutex_leave()] </li>
    6102                 : **   <li>  [sqlite3_mutex_held()] </li>
    6103                 : **   <li>  [sqlite3_mutex_notheld()] </li>
    6104                 : ** </ul>)^
    6105                 : **
    6106                 : ** The only difference is that the public sqlite3_XXX functions enumerated
    6107                 : ** above silently ignore any invocations that pass a NULL pointer instead
    6108                 : ** of a valid mutex handle. The implementations of the methods defined
    6109                 : ** by this structure are not required to handle this case, the results
    6110                 : ** of passing a NULL pointer instead of a valid mutex handle are undefined
    6111                 : ** (i.e. it is acceptable to provide an implementation that segfaults if
    6112                 : ** it is passed a NULL pointer).
    6113                 : **
    6114                 : ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
    6115                 : ** invoke xMutexInit() multiple times within the same process and without
    6116                 : ** intervening calls to xMutexEnd().  Second and subsequent calls to
    6117                 : ** xMutexInit() must be no-ops.
    6118                 : **
    6119                 : ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
    6120                 : ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
    6121                 : ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
    6122                 : ** memory allocation for a fast or recursive mutex.
    6123                 : **
    6124                 : ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
    6125                 : ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
    6126                 : ** If xMutexInit fails in any way, it is expected to clean up after itself
    6127                 : ** prior to returning.
    6128                 : */
    6129                 : typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
    6130                 : struct sqlite3_mutex_methods {
    6131                 :   int (*xMutexInit)(void);
    6132                 :   int (*xMutexEnd)(void);
    6133                 :   sqlite3_mutex *(*xMutexAlloc)(int);
    6134                 :   void (*xMutexFree)(sqlite3_mutex *);
    6135                 :   void (*xMutexEnter)(sqlite3_mutex *);
    6136                 :   int (*xMutexTry)(sqlite3_mutex *);
    6137                 :   void (*xMutexLeave)(sqlite3_mutex *);
    6138                 :   int (*xMutexHeld)(sqlite3_mutex *);
    6139                 :   int (*xMutexNotheld)(sqlite3_mutex *);
    6140                 : };
    6141                 : 
    6142                 : /*
    6143                 : ** CAPI3REF: Mutex Verification Routines
    6144                 : **
    6145                 : ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
    6146                 : ** are intended for use inside assert() statements.  ^The SQLite core
    6147                 : ** never uses these routines except inside an assert() and applications
    6148                 : ** are advised to follow the lead of the core.  ^The SQLite core only
    6149                 : ** provides implementations for these routines when it is compiled
    6150                 : ** with the SQLITE_DEBUG flag.  ^External mutex implementations
    6151                 : ** are only required to provide these routines if SQLITE_DEBUG is
    6152                 : ** defined and if NDEBUG is not defined.
    6153                 : **
    6154                 : ** ^These routines should return true if the mutex in their argument
    6155                 : ** is held or not held, respectively, by the calling thread.
    6156                 : **
    6157                 : ** ^The implementation is not required to provide versions of these
    6158                 : ** routines that actually work. If the implementation does not provide working
    6159                 : ** versions of these routines, it should at least provide stubs that always
    6160                 : ** return true so that one does not get spurious assertion failures.
    6161                 : **
    6162                 : ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
    6163                 : ** the routine should return 1.   This seems counter-intuitive since
    6164                 : ** clearly the mutex cannot be held if it does not exist.  But
    6165                 : ** the reason the mutex does not exist is because the build is not
    6166                 : ** using mutexes.  And we do not want the assert() containing the
    6167                 : ** call to sqlite3_mutex_held() to fail, so a non-zero return is
    6168                 : ** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
    6169                 : ** interface should also return 1 when given a NULL pointer.
    6170                 : */
    6171                 : #ifndef NDEBUG
    6172                 : SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
    6173                 : SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
    6174                 : #endif
    6175                 : 
    6176                 : /*
    6177                 : ** CAPI3REF: Mutex Types
    6178                 : **
    6179                 : ** The [sqlite3_mutex_alloc()] interface takes a single argument
    6180                 : ** which is one of these integer constants.
    6181                 : **
    6182                 : ** The set of static mutexes may change from one SQLite release to the
    6183                 : ** next.  Applications that override the built-in mutex logic must be
    6184                 : ** prepared to accommodate additional static mutexes.
    6185                 : */
    6186                 : #define SQLITE_MUTEX_FAST             0
    6187                 : #define SQLITE_MUTEX_RECURSIVE        1
    6188                 : #define SQLITE_MUTEX_STATIC_MASTER    2
    6189                 : #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
    6190                 : #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
    6191                 : #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
    6192                 : #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
    6193                 : #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
    6194                 : #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
    6195                 : #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
    6196                 : 
    6197                 : /*
    6198                 : ** CAPI3REF: Retrieve the mutex for a database connection
    6199                 : **
    6200                 : ** ^This interface returns a pointer the [sqlite3_mutex] object that 
    6201                 : ** serializes access to the [database connection] given in the argument
    6202                 : ** when the [threading mode] is Serialized.
    6203                 : ** ^If the [threading mode] is Single-thread or Multi-thread then this
    6204                 : ** routine returns a NULL pointer.
    6205                 : */
    6206                 : SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
    6207                 : 
    6208                 : /*
    6209                 : ** CAPI3REF: Low-Level Control Of Database Files
    6210                 : **
    6211                 : ** ^The [sqlite3_file_control()] interface makes a direct call to the
    6212                 : ** xFileControl method for the [sqlite3_io_methods] object associated
    6213                 : ** with a particular database identified by the second argument. ^The
    6214                 : ** name of the database is "main" for the main database or "temp" for the
    6215                 : ** TEMP database, or the name that appears after the AS keyword for
    6216                 : ** databases that are added using the [ATTACH] SQL command.
    6217                 : ** ^A NULL pointer can be used in place of "main" to refer to the
    6218                 : ** main database file.
    6219                 : ** ^The third and fourth parameters to this routine
    6220                 : ** are passed directly through to the second and third parameters of
    6221                 : ** the xFileControl method.  ^The return value of the xFileControl
    6222                 : ** method becomes the return value of this routine.
    6223                 : **
    6224                 : ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
    6225                 : ** a pointer to the underlying [sqlite3_file] object to be written into
    6226                 : ** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
    6227                 : ** case is a short-circuit path which does not actually invoke the
    6228                 : ** underlying sqlite3_io_methods.xFileControl method.
    6229                 : **
    6230                 : ** ^If the second parameter (zDbName) does not match the name of any
    6231                 : ** open database file, then SQLITE_ERROR is returned.  ^This error
    6232                 : ** code is not remembered and will not be recalled by [sqlite3_errcode()]
    6233                 : ** or [sqlite3_errmsg()].  The underlying xFileControl method might
    6234                 : ** also return SQLITE_ERROR.  There is no way to distinguish between
    6235                 : ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
    6236                 : ** xFileControl method.
    6237                 : **
    6238                 : ** See also: [SQLITE_FCNTL_LOCKSTATE]
    6239                 : */
    6240                 : SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
    6241                 : 
    6242                 : /*
    6243                 : ** CAPI3REF: Testing Interface
    6244                 : **
    6245                 : ** ^The sqlite3_test_control() interface is used to read out internal
    6246                 : ** state of SQLite and to inject faults into SQLite for testing
    6247                 : ** purposes.  ^The first parameter is an operation code that determines
    6248                 : ** the number, meaning, and operation of all subsequent parameters.
    6249                 : **
    6250                 : ** This interface is not for use by applications.  It exists solely
    6251                 : ** for verifying the correct operation of the SQLite library.  Depending
    6252                 : ** on how the SQLite library is compiled, this interface might not exist.
    6253                 : **
    6254                 : ** The details of the operation codes, their meanings, the parameters
    6255                 : ** they take, and what they do are all subject to change without notice.
    6256                 : ** Unlike most of the SQLite API, this function is not guaranteed to
    6257                 : ** operate consistently from one release to the next.
    6258                 : */
    6259                 : SQLITE_API int sqlite3_test_control(int op, ...);
    6260                 : 
    6261                 : /*
    6262                 : ** CAPI3REF: Testing Interface Operation Codes
    6263                 : **
    6264                 : ** These constants are the valid operation code parameters used
    6265                 : ** as the first argument to [sqlite3_test_control()].
    6266                 : **
    6267                 : ** These parameters and their meanings are subject to change
    6268                 : ** without notice.  These values are for testing purposes only.
    6269                 : ** Applications should not use any of these parameters or the
    6270                 : ** [sqlite3_test_control()] interface.
    6271                 : */
    6272                 : #define SQLITE_TESTCTRL_FIRST                    5
    6273                 : #define SQLITE_TESTCTRL_PRNG_SAVE                5
    6274                 : #define SQLITE_TESTCTRL_PRNG_RESTORE             6
    6275                 : #define SQLITE_TESTCTRL_PRNG_RESET               7
    6276                 : #define SQLITE_TESTCTRL_BITVEC_TEST              8
    6277                 : #define SQLITE_TESTCTRL_FAULT_INSTALL            9
    6278                 : #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
    6279                 : #define SQLITE_TESTCTRL_PENDING_BYTE            11
    6280                 : #define SQLITE_TESTCTRL_ASSERT                  12
    6281                 : #define SQLITE_TESTCTRL_ALWAYS                  13
    6282                 : #define SQLITE_TESTCTRL_RESERVE                 14
    6283                 : #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
    6284                 : #define SQLITE_TESTCTRL_ISKEYWORD               16
    6285                 : #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
    6286                 : #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
    6287                 : #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
    6288                 : #define SQLITE_TESTCTRL_LAST                    19
    6289                 : 
    6290                 : /*
    6291                 : ** CAPI3REF: SQLite Runtime Status
    6292                 : **
    6293                 : ** ^This interface is used to retrieve runtime status information
    6294                 : ** about the performance of SQLite, and optionally to reset various
    6295                 : ** highwater marks.  ^The first argument is an integer code for
    6296                 : ** the specific parameter to measure.  ^(Recognized integer codes
    6297                 : ** are of the form [status parameters | SQLITE_STATUS_...].)^
    6298                 : ** ^The current value of the parameter is returned into *pCurrent.
    6299                 : ** ^The highest recorded value is returned in *pHighwater.  ^If the
    6300                 : ** resetFlag is true, then the highest record value is reset after
    6301                 : ** *pHighwater is written.  ^(Some parameters do not record the highest
    6302                 : ** value.  For those parameters
    6303                 : ** nothing is written into *pHighwater and the resetFlag is ignored.)^
    6304                 : ** ^(Other parameters record only the highwater mark and not the current
    6305                 : ** value.  For these latter parameters nothing is written into *pCurrent.)^
    6306                 : **
    6307                 : ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
    6308                 : ** non-zero [error code] on failure.
    6309                 : **
    6310                 : ** This routine is threadsafe but is not atomic.  This routine can be
    6311                 : ** called while other threads are running the same or different SQLite
    6312                 : ** interfaces.  However the values returned in *pCurrent and
    6313                 : ** *pHighwater reflect the status of SQLite at different points in time
    6314                 : ** and it is possible that another thread might change the parameter
    6315                 : ** in between the times when *pCurrent and *pHighwater are written.
    6316                 : **
    6317                 : ** See also: [sqlite3_db_status()]
    6318                 : */
    6319                 : SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
    6320                 : 
    6321                 : 
    6322                 : /*
    6323                 : ** CAPI3REF: Status Parameters
    6324                 : ** KEYWORDS: {status parameters}
    6325                 : **
    6326                 : ** These integer constants designate various run-time status parameters
    6327                 : ** that can be returned by [sqlite3_status()].
    6328                 : **
    6329                 : ** <dl>
    6330                 : ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
    6331                 : ** <dd>This parameter is the current amount of memory checked out
    6332                 : ** using [sqlite3_malloc()], either directly or indirectly.  The
    6333                 : ** figure includes calls made to [sqlite3_malloc()] by the application
    6334                 : ** and internal memory usage by the SQLite library.  Scratch memory
    6335                 : ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
    6336                 : ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
    6337                 : ** this parameter.  The amount returned is the sum of the allocation
    6338                 : ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
    6339                 : **
    6340                 : ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
    6341                 : ** <dd>This parameter records the largest memory allocation request
    6342                 : ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
    6343                 : ** internal equivalents).  Only the value returned in the
    6344                 : ** *pHighwater parameter to [sqlite3_status()] is of interest.  
    6345                 : ** The value written into the *pCurrent parameter is undefined.</dd>)^
    6346                 : **
    6347                 : ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
    6348                 : ** <dd>This parameter records the number of separate memory allocations
    6349                 : ** currently checked out.</dd>)^
    6350                 : **
    6351                 : ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
    6352                 : ** <dd>This parameter returns the number of pages used out of the
    6353                 : ** [pagecache memory allocator] that was configured using 
    6354                 : ** [SQLITE_CONFIG_PAGECACHE].  The
    6355                 : ** value returned is in pages, not in bytes.</dd>)^
    6356                 : **
    6357                 : ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] 
    6358                 : ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
    6359                 : ** <dd>This parameter returns the number of bytes of page cache
    6360                 : ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
    6361                 : ** buffer and where forced to overflow to [sqlite3_malloc()].  The
    6362                 : ** returned value includes allocations that overflowed because they
    6363                 : ** where too large (they were larger than the "sz" parameter to
    6364                 : ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
    6365                 : ** no space was left in the page cache.</dd>)^
    6366                 : **
    6367                 : ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
    6368                 : ** <dd>This parameter records the largest memory allocation request
    6369                 : ** handed to [pagecache memory allocator].  Only the value returned in the
    6370                 : ** *pHighwater parameter to [sqlite3_status()] is of interest.  
    6371                 : ** The value written into the *pCurrent parameter is undefined.</dd>)^
    6372                 : **
    6373                 : ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
    6374                 : ** <dd>This parameter returns the number of allocations used out of the
    6375                 : ** [scratch memory allocator] configured using
    6376                 : ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
    6377                 : ** in bytes.  Since a single thread may only have one scratch allocation
    6378                 : ** outstanding at time, this parameter also reports the number of threads
    6379                 : ** using scratch memory at the same time.</dd>)^
    6380                 : **
    6381                 : ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
    6382                 : ** <dd>This parameter returns the number of bytes of scratch memory
    6383                 : ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
    6384                 : ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
    6385                 : ** returned include overflows because the requested allocation was too
    6386                 : ** larger (that is, because the requested allocation was larger than the
    6387                 : ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
    6388                 : ** slots were available.
    6389                 : ** </dd>)^
    6390                 : **
    6391                 : ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
    6392                 : ** <dd>This parameter records the largest memory allocation request
    6393                 : ** handed to [scratch memory allocator].  Only the value returned in the
    6394                 : ** *pHighwater parameter to [sqlite3_status()] is of interest.  
    6395                 : ** The value written into the *pCurrent parameter is undefined.</dd>)^
    6396                 : **
    6397                 : ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
    6398                 : ** <dd>This parameter records the deepest parser stack.  It is only
    6399                 : ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
    6400                 : ** </dl>
    6401                 : **
    6402                 : ** New status parameters may be added from time to time.
    6403                 : */
    6404                 : #define SQLITE_STATUS_MEMORY_USED          0
    6405                 : #define SQLITE_STATUS_PAGECACHE_USED       1
    6406                 : #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
    6407                 : #define SQLITE_STATUS_SCRATCH_USED         3
    6408                 : #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
    6409                 : #define SQLITE_STATUS_MALLOC_SIZE          5
    6410                 : #define SQLITE_STATUS_PARSER_STACK         6
    6411                 : #define SQLITE_STATUS_PAGECACHE_SIZE       7
    6412                 : #define SQLITE_STATUS_SCRATCH_SIZE         8
    6413                 : #define SQLITE_STATUS_MALLOC_COUNT         9
    6414                 : 
    6415                 : /*
    6416                 : ** CAPI3REF: Database Connection Status
    6417                 : **
    6418                 : ** ^This interface is used to retrieve runtime status information 
    6419                 : ** about a single [database connection].  ^The first argument is the
    6420                 : ** database connection object to be interrogated.  ^The second argument
    6421                 : ** is an integer constant, taken from the set of
    6422                 : ** [SQLITE_DBSTATUS options], that
    6423                 : ** determines the parameter to interrogate.  The set of 
    6424                 : ** [SQLITE_DBSTATUS options] is likely
    6425                 : ** to grow in future releases of SQLite.
    6426                 : **
    6427                 : ** ^The current value of the requested parameter is written into *pCur
    6428                 : ** and the highest instantaneous value is written into *pHiwtr.  ^If
    6429                 : ** the resetFlg is true, then the highest instantaneous value is
    6430                 : ** reset back down to the current value.
    6431                 : **
    6432                 : ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
    6433                 : ** non-zero [error code] on failure.
    6434                 : **
    6435                 : ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
    6436                 : */
    6437                 : SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
    6438                 : 
    6439                 : /*
    6440                 : ** CAPI3REF: Status Parameters for database connections
    6441                 : ** KEYWORDS: {SQLITE_DBSTATUS options}
    6442                 : **
    6443                 : ** These constants are the available integer "verbs" that can be passed as
    6444                 : ** the second argument to the [sqlite3_db_status()] interface.
    6445                 : **
    6446                 : ** New verbs may be added in future releases of SQLite. Existing verbs
    6447                 : ** might be discontinued. Applications should check the return code from
    6448                 : ** [sqlite3_db_status()] to make sure that the call worked.
    6449                 : ** The [sqlite3_db_status()] interface will return a non-zero error code
    6450                 : ** if a discontinued or unsupported verb is invoked.
    6451                 : **
    6452                 : ** <dl>
    6453                 : ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
    6454                 : ** <dd>This parameter returns the number of lookaside memory slots currently
    6455                 : ** checked out.</dd>)^
    6456                 : **
    6457                 : ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
    6458                 : ** <dd>This parameter returns the number malloc attempts that were 
    6459                 : ** satisfied using lookaside memory. Only the high-water value is meaningful;
    6460                 : ** the current value is always zero.)^
    6461                 : **
    6462                 : ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
    6463                 : ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
    6464                 : ** <dd>This parameter returns the number malloc attempts that might have
    6465                 : ** been satisfied using lookaside memory but failed due to the amount of
    6466                 : ** memory requested being larger than the lookaside slot size.
    6467                 : ** Only the high-water value is meaningful;
    6468                 : ** the current value is always zero.)^
    6469                 : **
    6470                 : ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
    6471                 : ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
    6472                 : ** <dd>This parameter returns the number malloc attempts that might have
    6473                 : ** been satisfied using lookaside memory but failed due to all lookaside
    6474                 : ** memory already being in use.
    6475                 : ** Only the high-water value is meaningful;
    6476                 : ** the current value is always zero.)^
    6477                 : **
    6478                 : ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
    6479                 : ** <dd>This parameter returns the approximate number of of bytes of heap
    6480                 : ** memory used by all pager caches associated with the database connection.)^
    6481                 : ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
    6482                 : **
    6483                 : ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
    6484                 : ** <dd>This parameter returns the approximate number of of bytes of heap
    6485                 : ** memory used to store the schema for all databases associated
    6486                 : ** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 
    6487                 : ** ^The full amount of memory used by the schemas is reported, even if the
    6488                 : ** schema memory is shared with other database connections due to
    6489                 : ** [shared cache mode] being enabled.
    6490                 : ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
    6491                 : **
    6492                 : ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
    6493                 : ** <dd>This parameter returns the approximate number of of bytes of heap
    6494                 : ** and lookaside memory used by all prepared statements associated with
    6495                 : ** the database connection.)^
    6496                 : ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
    6497                 : ** </dd>
    6498                 : **
    6499                 : ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
    6500                 : ** <dd>This parameter returns the number of pager cache hits that have
    6501                 : ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 
    6502                 : ** is always 0.
    6503                 : ** </dd>
    6504                 : **
    6505                 : ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
    6506                 : ** <dd>This parameter returns the number of pager cache misses that have
    6507                 : ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 
    6508                 : ** is always 0.
    6509                 : ** </dd>
    6510                 : ** </dl>
    6511                 : */
    6512                 : #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
    6513                 : #define SQLITE_DBSTATUS_CACHE_USED           1
    6514                 : #define SQLITE_DBSTATUS_SCHEMA_USED          2
    6515                 : #define SQLITE_DBSTATUS_STMT_USED            3
    6516                 : #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
    6517                 : #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
    6518                 : #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
    6519                 : #define SQLITE_DBSTATUS_CACHE_HIT            7
    6520                 : #define SQLITE_DBSTATUS_CACHE_MISS           8
    6521                 : #define SQLITE_DBSTATUS_MAX                  8   /* Largest defined DBSTATUS */
    6522                 : 
    6523                 : 
    6524                 : /*
    6525                 : ** CAPI3REF: Prepared Statement Status
    6526                 : **
    6527                 : ** ^(Each prepared statement maintains various
    6528                 : ** [SQLITE_STMTSTATUS counters] that measure the number
    6529                 : ** of times it has performed specific operations.)^  These counters can
    6530                 : ** be used to monitor the performance characteristics of the prepared
    6531                 : ** statements.  For example, if the number of table steps greatly exceeds
    6532                 : ** the number of table searches or result rows, that would tend to indicate
    6533                 : ** that the prepared statement is using a full table scan rather than
    6534                 : ** an index.  
    6535                 : **
    6536                 : ** ^(This interface is used to retrieve and reset counter values from
    6537                 : ** a [prepared statement].  The first argument is the prepared statement
    6538                 : ** object to be interrogated.  The second argument
    6539                 : ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
    6540                 : ** to be interrogated.)^
    6541                 : ** ^The current value of the requested counter is returned.
    6542                 : ** ^If the resetFlg is true, then the counter is reset to zero after this
    6543                 : ** interface call returns.
    6544                 : **
    6545                 : ** See also: [sqlite3_status()] and [sqlite3_db_status()].
    6546                 : */
    6547                 : SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
    6548                 : 
    6549                 : /*
    6550                 : ** CAPI3REF: Status Parameters for prepared statements
    6551                 : ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
    6552                 : **
    6553                 : ** These preprocessor macros define integer codes that name counter
    6554                 : ** values associated with the [sqlite3_stmt_status()] interface.
    6555                 : ** The meanings of the various counters are as follows:
    6556                 : **
    6557                 : ** <dl>
    6558                 : ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
    6559                 : ** <dd>^This is the number of times that SQLite has stepped forward in
    6560                 : ** a table as part of a full table scan.  Large numbers for this counter
    6561                 : ** may indicate opportunities for performance improvement through 
    6562                 : ** careful use of indices.</dd>
    6563                 : **
    6564                 : ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
    6565                 : ** <dd>^This is the number of sort operations that have occurred.
    6566                 : ** A non-zero value in this counter may indicate an opportunity to
    6567                 : ** improvement performance through careful use of indices.</dd>
    6568                 : **
    6569                 : ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
    6570                 : ** <dd>^This is the number of rows inserted into transient indices that
    6571                 : ** were created automatically in order to help joins run faster.
    6572                 : ** A non-zero value in this counter may indicate an opportunity to
    6573                 : ** improvement performance by adding permanent indices that do not
    6574                 : ** need to be reinitialized each time the statement is run.</dd>
    6575                 : ** </dl>
    6576                 : */
    6577                 : #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
    6578                 : #define SQLITE_STMTSTATUS_SORT              2
    6579                 : #define SQLITE_STMTSTATUS_AUTOINDEX         3
    6580                 : 
    6581                 : /*
    6582                 : ** CAPI3REF: Custom Page Cache Object
    6583                 : **
    6584                 : ** The sqlite3_pcache type is opaque.  It is implemented by
    6585                 : ** the pluggable module.  The SQLite core has no knowledge of
    6586                 : ** its size or internal structure and never deals with the
    6587                 : ** sqlite3_pcache object except by holding and passing pointers
    6588                 : ** to the object.
    6589                 : **
    6590                 : ** See [sqlite3_pcache_methods2] for additional information.
    6591                 : */
    6592                 : typedef struct sqlite3_pcache sqlite3_pcache;
    6593                 : 
    6594                 : /*
    6595                 : ** CAPI3REF: Custom Page Cache Object
    6596                 : **
    6597                 : ** The sqlite3_pcache_page object represents a single page in the
    6598                 : ** page cache.  The page cache will allocate instances of this
    6599                 : ** object.  Various methods of the page cache use pointers to instances
    6600                 : ** of this object as parameters or as their return value.
    6601                 : **
    6602                 : ** See [sqlite3_pcache_methods2] for additional information.
    6603                 : */
    6604                 : typedef struct sqlite3_pcache_page sqlite3_pcache_page;
    6605                 : struct sqlite3_pcache_page {
    6606                 :   void *pBuf;        /* The content of the page */
    6607                 :   void *pExtra;      /* Extra information associated with the page */
    6608                 : };
    6609                 : 
    6610                 : /*
    6611                 : ** CAPI3REF: Application Defined Page Cache.
    6612                 : ** KEYWORDS: {page cache}
    6613                 : **
    6614                 : ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
    6615                 : ** register an alternative page cache implementation by passing in an 
    6616                 : ** instance of the sqlite3_pcache_methods2 structure.)^
    6617                 : ** In many applications, most of the heap memory allocated by 
    6618                 : ** SQLite is used for the page cache.
    6619                 : ** By implementing a 
    6620                 : ** custom page cache using this API, an application can better control
    6621                 : ** the amount of memory consumed by SQLite, the way in which 
    6622                 : ** that memory is allocated and released, and the policies used to 
    6623                 : ** determine exactly which parts of a database file are cached and for 
    6624                 : ** how long.
    6625                 : **
    6626                 : ** The alternative page cache mechanism is an
    6627                 : ** extreme measure that is only needed by the most demanding applications.
    6628                 : ** The built-in page cache is recommended for most uses.
    6629                 : **
    6630                 : ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
    6631                 : ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
    6632                 : ** the application may discard the parameter after the call to
    6633                 : ** [sqlite3_config()] returns.)^
    6634                 : **
    6635                 : ** [[the xInit() page cache method]]
    6636                 : ** ^(The xInit() method is called once for each effective 
    6637                 : ** call to [sqlite3_initialize()])^
    6638                 : ** (usually only once during the lifetime of the process). ^(The xInit()
    6639                 : ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
    6640                 : ** The intent of the xInit() method is to set up global data structures 
    6641                 : ** required by the custom page cache implementation. 
    6642                 : ** ^(If the xInit() method is NULL, then the 
    6643                 : ** built-in default page cache is used instead of the application defined
    6644                 : ** page cache.)^
    6645                 : **
    6646                 : ** [[the xShutdown() page cache method]]
    6647                 : ** ^The xShutdown() method is called by [sqlite3_shutdown()].
    6648                 : ** It can be used to clean up 
    6649                 : ** any outstanding resources before process shutdown, if required.
    6650                 : ** ^The xShutdown() method may be NULL.
    6651                 : **
    6652                 : ** ^SQLite automatically serializes calls to the xInit method,
    6653                 : ** so the xInit method need not be threadsafe.  ^The
    6654                 : ** xShutdown method is only called from [sqlite3_shutdown()] so it does
    6655                 : ** not need to be threadsafe either.  All other methods must be threadsafe
    6656                 : ** in multithreaded applications.
    6657                 : **
    6658                 : ** ^SQLite will never invoke xInit() more than once without an intervening
    6659                 : ** call to xShutdown().
    6660                 : **
    6661                 : ** [[the xCreate() page cache methods]]
    6662                 : ** ^SQLite invokes the xCreate() method to construct a new cache instance.
    6663                 : ** SQLite will typically create one cache instance for each open database file,
    6664                 : ** though this is not guaranteed. ^The
    6665                 : ** first parameter, szPage, is the size in bytes of the pages that must
    6666                 : ** be allocated by the cache.  ^szPage will always a power of two.  ^The
    6667                 : ** second parameter szExtra is a number of bytes of extra storage 
    6668                 : ** associated with each page cache entry.  ^The szExtra parameter will
    6669                 : ** a number less than 250.  SQLite will use the
    6670                 : ** extra szExtra bytes on each page to store metadata about the underlying
    6671                 : ** database page on disk.  The value passed into szExtra depends
    6672                 : ** on the SQLite version, the target platform, and how SQLite was compiled.
    6673                 : ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
    6674                 : ** created will be used to cache database pages of a file stored on disk, or
    6675                 : ** false if it is used for an in-memory database. The cache implementation
    6676                 : ** does not have to do anything special based with the value of bPurgeable;
    6677                 : ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
    6678                 : ** never invoke xUnpin() except to deliberately delete a page.
    6679                 : ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
    6680                 : ** false will always have the "discard" flag set to true.  
    6681                 : ** ^Hence, a cache created with bPurgeable false will
    6682                 : ** never contain any unpinned pages.
    6683                 : **
    6684                 : ** [[the xCachesize() page cache method]]
    6685                 : ** ^(The xCachesize() method may be called at any time by SQLite to set the
    6686                 : ** suggested maximum cache-size (number of pages stored by) the cache
    6687                 : ** instance passed as the first argument. This is the value configured using
    6688                 : ** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
    6689                 : ** parameter, the implementation is not required to do anything with this
    6690                 : ** value; it is advisory only.
    6691                 : **
    6692                 : ** [[the xPagecount() page cache methods]]
    6693                 : ** The xPagecount() method must return the number of pages currently
    6694                 : ** stored in the cache, both pinned and unpinned.
    6695                 : ** 
    6696                 : ** [[the xFetch() page cache methods]]
    6697                 : ** The xFetch() method locates a page in the cache and returns a pointer to 
    6698                 : ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
    6699                 : ** The pBuf element of the returned sqlite3_pcache_page object will be a
    6700                 : ** pointer to a buffer of szPage bytes used to store the content of a 
    6701                 : ** single database page.  The pExtra element of sqlite3_pcache_page will be
    6702                 : ** a pointer to the szExtra bytes of extra storage that SQLite has requested
    6703                 : ** for each entry in the page cache.
    6704                 : **
    6705                 : ** The page to be fetched is determined by the key. ^The minimum key value
    6706                 : ** is 1.  After it has been retrieved using xFetch, the page is considered
    6707                 : ** to be "pinned".
    6708                 : **
    6709                 : ** If the requested page is already in the page cache, then the page cache
    6710                 : ** implementation must return a pointer to the page buffer with its content
    6711                 : ** intact.  If the requested page is not already in the cache, then the
    6712                 : ** cache implementation should use the value of the createFlag
    6713                 : ** parameter to help it determined what action to take:
    6714                 : **
    6715                 : ** <table border=1 width=85% align=center>
    6716                 : ** <tr><th> createFlag <th> Behaviour when page is not already in cache
    6717                 : ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
    6718                 : ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
    6719                 : **                 Otherwise return NULL.
    6720                 : ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
    6721                 : **                 NULL if allocating a new page is effectively impossible.
    6722                 : ** </table>
    6723                 : **
    6724                 : ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
    6725                 : ** will only use a createFlag of 2 after a prior call with a createFlag of 1
    6726                 : ** failed.)^  In between the to xFetch() calls, SQLite may
    6727                 : ** attempt to unpin one or more cache pages by spilling the content of
    6728                 : ** pinned pages to disk and synching the operating system disk cache.
    6729                 : **
    6730                 : ** [[the xUnpin() page cache method]]
    6731                 : ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
    6732                 : ** as its second argument.  If the third parameter, discard, is non-zero,
    6733                 : ** then the page must be evicted from the cache.
    6734                 : ** ^If the discard parameter is
    6735                 : ** zero, then the page may be discarded or retained at the discretion of
    6736                 : ** page cache implementation. ^The page cache implementation
    6737                 : ** may choose to evict unpinned pages at any time.
    6738                 : **
    6739                 : ** The cache must not perform any reference counting. A single 
    6740                 : ** call to xUnpin() unpins the page regardless of the number of prior calls 
    6741                 : ** to xFetch().
    6742                 : **
    6743                 : ** [[the xRekey() page cache methods]]
    6744                 : ** The xRekey() method is used to change the key value associated with the
    6745                 : ** page passed as the second argument. If the cache
    6746                 : ** previously contains an entry associated with newKey, it must be
    6747                 : ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
    6748                 : ** to be pinned.
    6749                 : **
    6750                 : ** When SQLite calls the xTruncate() method, the cache must discard all
    6751                 : ** existing cache entries with page numbers (keys) greater than or equal
    6752                 : ** to the value of the iLimit parameter passed to xTruncate(). If any
    6753                 : ** of these pages are pinned, they are implicitly unpinned, meaning that
    6754                 : ** they can be safely discarded.
    6755                 : **
    6756                 : ** [[the xDestroy() page cache method]]
    6757                 : ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
    6758                 : ** All resources associated with the specified cache should be freed. ^After
    6759                 : ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
    6760                 : ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
    6761                 : ** functions.
    6762                 : **
    6763                 : ** [[the xShrink() page cache method]]
    6764                 : ** ^SQLite invokes the xShrink() method when it wants the page cache to
    6765                 : ** free up as much of heap memory as possible.  The page cache implementation
    6766                 : ** is not obligated to free any memory, but well-behaved implementations should
    6767                 : ** do their best.
    6768                 : */
    6769                 : typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
    6770                 : struct sqlite3_pcache_methods2 {
    6771                 :   int iVersion;
    6772                 :   void *pArg;
    6773                 :   int (*xInit)(void*);
    6774                 :   void (*xShutdown)(void*);
    6775                 :   sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
    6776                 :   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
    6777                 :   int (*xPagecount)(sqlite3_pcache*);
    6778                 :   sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
    6779                 :   void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
    6780                 :   void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, 
    6781                 :       unsigned oldKey, unsigned newKey);
    6782                 :   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
    6783                 :   void (*xDestroy)(sqlite3_pcache*);
    6784                 :   void (*xShrink)(sqlite3_pcache*);
    6785                 : };
    6786                 : 
    6787                 : /*
    6788                 : ** This is the obsolete pcache_methods object that has now been replaced
    6789                 : ** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
    6790                 : ** retained in the header file for backwards compatibility only.
    6791                 : */
    6792                 : typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
    6793                 : struct sqlite3_pcache_methods {
    6794                 :   void *pArg;
    6795                 :   int (*xInit)(void*);
    6796                 :   void (*xShutdown)(void*);
    6797                 :   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
    6798                 :   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
    6799                 :   int (*xPagecount)(sqlite3_pcache*);
    6800                 :   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
    6801                 :   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
    6802                 :   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
    6803                 :   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
    6804                 :   void (*xDestroy)(sqlite3_pcache*);
    6805                 : };
    6806                 : 
    6807                 : 
    6808                 : /*
    6809                 : ** CAPI3REF: Online Backup Object
    6810                 : **
    6811                 : ** The sqlite3_backup object records state information about an ongoing
    6812                 : ** online backup operation.  ^The sqlite3_backup object is created by
    6813                 : ** a call to [sqlite3_backup_init()] and is destroyed by a call to
    6814                 : ** [sqlite3_backup_finish()].
    6815                 : **
    6816                 : ** See Also: [Using the SQLite Online Backup API]
    6817                 : */
    6818                 : typedef struct sqlite3_backup sqlite3_backup;
    6819                 : 
    6820                 : /*
    6821                 : ** CAPI3REF: Online Backup API.
    6822                 : **
    6823                 : ** The backup API copies the content of one database into another.
    6824                 : ** It is useful either for creating backups of databases or
    6825                 : ** for copying in-memory databases to or from persistent files. 
    6826                 : **
    6827                 : ** See Also: [Using the SQLite Online Backup API]
    6828                 : **
    6829                 : ** ^SQLite holds a write transaction open on the destination database file
    6830                 : ** for the duration of the backup operation.
    6831                 : ** ^The source database is read-locked only while it is being read;
    6832                 : ** it is not locked continuously for the entire backup operation.
    6833                 : ** ^Thus, the backup may be performed on a live source database without
    6834                 : ** preventing other database connections from
    6835                 : ** reading or writing to the source database while the backup is underway.
    6836                 : ** 
    6837                 : ** ^(To perform a backup operation: 
    6838                 : **   <ol>
    6839                 : **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
    6840                 : **         backup, 
    6841                 : **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 
    6842                 : **         the data between the two databases, and finally
    6843                 : **     <li><b>sqlite3_backup_finish()</b> is called to release all resources 
    6844                 : **         associated with the backup operation. 
    6845                 : **   </ol>)^
    6846                 : ** There should be exactly one call to sqlite3_backup_finish() for each
    6847                 : ** successful call to sqlite3_backup_init().
    6848                 : **
    6849                 : ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
    6850                 : **
    6851                 : ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the 
    6852                 : ** [database connection] associated with the destination database 
    6853                 : ** and the database name, respectively.
    6854                 : ** ^The database name is "main" for the main database, "temp" for the
    6855                 : ** temporary database, or the name specified after the AS keyword in
    6856                 : ** an [ATTACH] statement for an attached database.
    6857                 : ** ^The S and M arguments passed to 
    6858                 : ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
    6859                 : ** and database name of the source database, respectively.
    6860                 : ** ^The source and destination [database connections] (parameters S and D)
    6861                 : ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
    6862                 : ** an error.
    6863                 : **
    6864                 : ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
    6865                 : ** returned and an error code and error message are stored in the
    6866                 : ** destination [database connection] D.
    6867                 : ** ^The error code and message for the failed call to sqlite3_backup_init()
    6868                 : ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
    6869                 : ** [sqlite3_errmsg16()] functions.
    6870                 : ** ^A successful call to sqlite3_backup_init() returns a pointer to an
    6871                 : ** [sqlite3_backup] object.
    6872                 : ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
    6873                 : ** sqlite3_backup_finish() functions to perform the specified backup 
    6874                 : ** operation.
    6875                 : **
    6876                 : ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
    6877                 : **
    6878                 : ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between 
    6879                 : ** the source and destination databases specified by [sqlite3_backup] object B.
    6880                 : ** ^If N is negative, all remaining source pages are copied. 
    6881                 : ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
    6882                 : ** are still more pages to be copied, then the function returns [SQLITE_OK].
    6883                 : ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
    6884                 : ** from source to destination, then it returns [SQLITE_DONE].
    6885                 : ** ^If an error occurs while running sqlite3_backup_step(B,N),
    6886                 : ** then an [error code] is returned. ^As well as [SQLITE_OK] and
    6887                 : ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
    6888                 : ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
    6889                 : ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
    6890                 : **
    6891                 : ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
    6892                 : ** <ol>
    6893                 : ** <li> the destination database was opened read-only, or
    6894                 : ** <li> the destination database is using write-ahead-log journaling
    6895                 : ** and the destination and source page sizes differ, or
    6896                 : ** <li> the destination database is an in-memory database and the
    6897                 : ** destination and source page sizes differ.
    6898                 : ** </ol>)^
    6899                 : **
    6900                 : ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
    6901                 : ** the [sqlite3_busy_handler | busy-handler function]
    6902                 : ** is invoked (if one is specified). ^If the 
    6903                 : ** busy-handler returns non-zero before the lock is available, then 
    6904                 : ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
    6905                 : ** sqlite3_backup_step() can be retried later. ^If the source
    6906                 : ** [database connection]
    6907                 : ** is being used to write to the source database when sqlite3_backup_step()
    6908                 : ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
    6909                 : ** case the call to sqlite3_backup_step() can be retried later on. ^(If
    6910                 : ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
    6911                 : ** [SQLITE_READONLY] is returned, then 
    6912                 : ** there is no point in retrying the call to sqlite3_backup_step(). These 
    6913                 : ** errors are considered fatal.)^  The application must accept 
    6914                 : ** that the backup operation has failed and pass the backup operation handle 
    6915                 : ** to the sqlite3_backup_finish() to release associated resources.
    6916                 : **
    6917                 : ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
    6918                 : ** on the destination file. ^The exclusive lock is not released until either 
    6919                 : ** sqlite3_backup_finish() is called or the backup operation is complete 
    6920                 : ** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
    6921                 : ** sqlite3_backup_step() obtains a [shared lock] on the source database that
    6922                 : ** lasts for the duration of the sqlite3_backup_step() call.
    6923                 : ** ^Because the source database is not locked between calls to
    6924                 : ** sqlite3_backup_step(), the source database may be modified mid-way
    6925                 : ** through the backup process.  ^If the source database is modified by an
    6926                 : ** external process or via a database connection other than the one being
    6927                 : ** used by the backup operation, then the backup will be automatically
    6928                 : ** restarted by the next call to sqlite3_backup_step(). ^If the source 
    6929                 : ** database is modified by the using the same database connection as is used
    6930                 : ** by the backup operation, then the backup database is automatically
    6931                 : ** updated at the same time.
    6932                 : **
    6933                 : ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
    6934                 : **
    6935                 : ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the 
    6936                 : ** application wishes to abandon the backup operation, the application
    6937                 : ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
    6938                 : ** ^The sqlite3_backup_finish() interfaces releases all
    6939                 : ** resources associated with the [sqlite3_backup] object. 
    6940                 : ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
    6941                 : ** active write-transaction on the destination database is rolled back.
    6942                 : ** The [sqlite3_backup] object is invalid
    6943                 : ** and may not be used following a call to sqlite3_backup_finish().
    6944                 : **
    6945                 : ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
    6946                 : ** sqlite3_backup_step() errors occurred, regardless or whether or not
    6947                 : ** sqlite3_backup_step() completed.
    6948                 : ** ^If an out-of-memory condition or IO error occurred during any prior
    6949                 : ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
    6950                 : ** sqlite3_backup_finish() returns the corresponding [error code].
    6951                 : **
    6952                 : ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
    6953                 : ** is not a permanent error and does not affect the return value of
    6954                 : ** sqlite3_backup_finish().
    6955                 : **
    6956                 : ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
    6957                 : ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
    6958                 : **
    6959                 : ** ^Each call to sqlite3_backup_step() sets two values inside
    6960                 : ** the [sqlite3_backup] object: the number of pages still to be backed
    6961                 : ** up and the total number of pages in the source database file.
    6962                 : ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
    6963                 : ** retrieve these two values, respectively.
    6964                 : **
    6965                 : ** ^The values returned by these functions are only updated by
    6966                 : ** sqlite3_backup_step(). ^If the source database is modified during a backup
    6967                 : ** operation, then the values are not updated to account for any extra
    6968                 : ** pages that need to be updated or the size of the source database file
    6969                 : ** changing.
    6970                 : **
    6971                 : ** <b>Concurrent Usage of Database Handles</b>
    6972                 : **
    6973                 : ** ^The source [database connection] may be used by the application for other
    6974                 : ** purposes while a backup operation is underway or being initialized.
    6975                 : ** ^If SQLite is compiled and configured to support threadsafe database
    6976                 : ** connections, then the source database connection may be used concurrently
    6977                 : ** from within other threads.
    6978                 : **
    6979                 : ** However, the application must guarantee that the destination 
    6980                 : ** [database connection] is not passed to any other API (by any thread) after 
    6981                 : ** sqlite3_backup_init() is called and before the corresponding call to
    6982                 : ** sqlite3_backup_finish().  SQLite does not currently check to see
    6983                 : ** if the application incorrectly accesses the destination [database connection]
    6984                 : ** and so no error code is reported, but the operations may malfunction
    6985                 : ** nevertheless.  Use of the destination database connection while a
    6986                 : ** backup is in progress might also also cause a mutex deadlock.
    6987                 : **
    6988                 : ** If running in [shared cache mode], the application must
    6989                 : ** guarantee that the shared cache used by the destination database
    6990                 : ** is not accessed while the backup is running. In practice this means
    6991                 : ** that the application must guarantee that the disk file being 
    6992                 : ** backed up to is not accessed by any connection within the process,
    6993                 : ** not just the specific connection that was passed to sqlite3_backup_init().
    6994                 : **
    6995                 : ** The [sqlite3_backup] object itself is partially threadsafe. Multiple 
    6996                 : ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
    6997                 : ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
    6998                 : ** APIs are not strictly speaking threadsafe. If they are invoked at the
    6999                 : ** same time as another thread is invoking sqlite3_backup_step() it is
    7000                 : ** possible that they return invalid values.
    7001                 : */
    7002                 : SQLITE_API sqlite3_backup *sqlite3_backup_init(
    7003                 :   sqlite3 *pDest,                        /* Destination database handle */
    7004                 :   const char *zDestName,                 /* Destination database name */
    7005                 :   sqlite3 *pSource,                      /* Source database handle */
    7006                 :   const char *zSourceName                /* Source database name */
    7007                 : );
    7008                 : SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
    7009                 : SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
    7010                 : SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
    7011                 : SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
    7012                 : 
    7013                 : /*
    7014                 : ** CAPI3REF: Unlock Notification
    7015                 : **
    7016                 : ** ^When running in shared-cache mode, a database operation may fail with
    7017                 : ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
    7018                 : ** individual tables within the shared-cache cannot be obtained. See
    7019                 : ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 
    7020                 : ** ^This API may be used to register a callback that SQLite will invoke 
    7021                 : ** when the connection currently holding the required lock relinquishes it.
    7022                 : ** ^This API is only available if the library was compiled with the
    7023                 : ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
    7024                 : **
    7025                 : ** See Also: [Using the SQLite Unlock Notification Feature].
    7026                 : **
    7027                 : ** ^Shared-cache locks are released when a database connection concludes
    7028                 : ** its current transaction, either by committing it or rolling it back. 
    7029                 : **
    7030                 : ** ^When a connection (known as the blocked connection) fails to obtain a
    7031                 : ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
    7032                 : ** identity of the database connection (the blocking connection) that
    7033                 : ** has locked the required resource is stored internally. ^After an 
    7034                 : ** application receives an SQLITE_LOCKED error, it may call the
    7035                 : ** sqlite3_unlock_notify() method with the blocked connection handle as 
    7036                 : ** the first argument to register for a callback that will be invoked
    7037                 : ** when the blocking connections current transaction is concluded. ^The
    7038                 : ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
    7039                 : ** call that concludes the blocking connections transaction.
    7040                 : **
    7041                 : ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
    7042                 : ** there is a chance that the blocking connection will have already
    7043                 : ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
    7044                 : ** If this happens, then the specified callback is invoked immediately,
    7045                 : ** from within the call to sqlite3_unlock_notify().)^
    7046                 : **
    7047                 : ** ^If the blocked connection is attempting to obtain a write-lock on a
    7048                 : ** shared-cache table, and more than one other connection currently holds
    7049                 : ** a read-lock on the same table, then SQLite arbitrarily selects one of 
    7050                 : ** the other connections to use as the blocking connection.
    7051                 : **
    7052                 : ** ^(There may be at most one unlock-notify callback registered by a 
    7053                 : ** blocked connection. If sqlite3_unlock_notify() is called when the
    7054                 : ** blocked connection already has a registered unlock-notify callback,
    7055                 : ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
    7056                 : ** called with a NULL pointer as its second argument, then any existing
    7057                 : ** unlock-notify callback is canceled. ^The blocked connections 
    7058                 : ** unlock-notify callback may also be canceled by closing the blocked
    7059                 : ** connection using [sqlite3_close()].
    7060                 : **
    7061                 : ** The unlock-notify callback is not reentrant. If an application invokes
    7062                 : ** any sqlite3_xxx API functions from within an unlock-notify callback, a
    7063                 : ** crash or deadlock may be the result.
    7064                 : **
    7065                 : ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
    7066                 : ** returns SQLITE_OK.
    7067                 : **
    7068                 : ** <b>Callback Invocation Details</b>
    7069                 : **
    7070                 : ** When an unlock-notify callback is registered, the application provides a 
    7071                 : ** single void* pointer that is passed to the callback when it is invoked.
    7072                 : ** However, the signature of the callback function allows SQLite to pass
    7073                 : ** it an array of void* context pointers. The first argument passed to
    7074                 : ** an unlock-notify callback is a pointer to an array of void* pointers,
    7075                 : ** and the second is the number of entries in the array.
    7076                 : **
    7077                 : ** When a blocking connections transaction is concluded, there may be
    7078                 : ** more than one blocked connection that has registered for an unlock-notify
    7079                 : ** callback. ^If two or more such blocked connections have specified the
    7080                 : ** same callback function, then instead of invoking the callback function
    7081                 : ** multiple times, it is invoked once with the set of void* context pointers
    7082                 : ** specified by the blocked connections bundled together into an array.
    7083                 : ** This gives the application an opportunity to prioritize any actions 
    7084                 : ** related to the set of unblocked database connections.
    7085                 : **
    7086                 : ** <b>Deadlock Detection</b>
    7087                 : **
    7088                 : ** Assuming that after registering for an unlock-notify callback a 
    7089                 : ** database waits for the callback to be issued before taking any further
    7090                 : ** action (a reasonable assumption), then using this API may cause the
    7091                 : ** application to deadlock. For example, if connection X is waiting for
    7092                 : ** connection Y's transaction to be concluded, and similarly connection
    7093                 : ** Y is waiting on connection X's transaction, then neither connection
    7094                 : ** will proceed and the system may remain deadlocked indefinitely.
    7095                 : **
    7096                 : ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
    7097                 : ** detection. ^If a given call to sqlite3_unlock_notify() would put the
    7098                 : ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
    7099                 : ** unlock-notify callback is registered. The system is said to be in
    7100                 : ** a deadlocked state if connection A has registered for an unlock-notify
    7101                 : ** callback on the conclusion of connection B's transaction, and connection
    7102                 : ** B has itself registered for an unlock-notify callback when connection
    7103                 : ** A's transaction is concluded. ^Indirect deadlock is also detected, so
    7104                 : ** the system is also considered to be deadlocked if connection B has
    7105                 : ** registered for an unlock-notify callback on the conclusion of connection
    7106                 : ** C's transaction, where connection C is waiting on connection A. ^Any
    7107                 : ** number of levels of indirection are allowed.
    7108                 : **
    7109                 : ** <b>The "DROP TABLE" Exception</b>
    7110                 : **
    7111                 : ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost 
    7112                 : ** always appropriate to call sqlite3_unlock_notify(). There is however,
    7113                 : ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
    7114                 : ** SQLite checks if there are any currently executing SELECT statements
    7115                 : ** that belong to the same connection. If there are, SQLITE_LOCKED is
    7116                 : ** returned. In this case there is no "blocking connection", so invoking
    7117                 : ** sqlite3_unlock_notify() results in the unlock-notify callback being
    7118                 : ** invoked immediately. If the application then re-attempts the "DROP TABLE"
    7119                 : ** or "DROP INDEX" query, an infinite loop might be the result.
    7120                 : **
    7121                 : ** One way around this problem is to check the extended error code returned
    7122                 : ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
    7123                 : ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
    7124                 : ** the special "DROP TABLE/INDEX" case, the extended error code is just 
    7125                 : ** SQLITE_LOCKED.)^
    7126                 : */
    7127                 : SQLITE_API int sqlite3_unlock_notify(
    7128                 :   sqlite3 *pBlocked,                          /* Waiting connection */
    7129                 :   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
    7130                 :   void *pNotifyArg                            /* Argument to pass to xNotify */
    7131                 : );
    7132                 : 
    7133                 : 
    7134                 : /*
    7135                 : ** CAPI3REF: String Comparison
    7136                 : **
    7137                 : ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
    7138                 : ** compare the contents of two buffers containing UTF-8 strings in a
    7139                 : ** case-independent fashion, using the same definition of case independence 
    7140                 : ** that SQLite uses internally when comparing identifiers.
    7141                 : */
    7142                 : SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
    7143                 : 
    7144                 : /*
    7145                 : ** CAPI3REF: Error Logging Interface
    7146                 : **
    7147                 : ** ^The [sqlite3_log()] interface writes a message into the error log
    7148                 : ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
    7149                 : ** ^If logging is enabled, the zFormat string and subsequent arguments are
    7150                 : ** used with [sqlite3_snprintf()] to generate the final output string.
    7151                 : **
    7152                 : ** The sqlite3_log() interface is intended for use by extensions such as
    7153                 : ** virtual tables, collating functions, and SQL functions.  While there is
    7154                 : ** nothing to prevent an application from calling sqlite3_log(), doing so
    7155                 : ** is considered bad form.
    7156                 : **
    7157                 : ** The zFormat string must not be NULL.
    7158                 : **
    7159                 : ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
    7160                 : ** will not use dynamically allocated memory.  The log message is stored in
    7161                 : ** a fixed-length buffer on the stack.  If the log message is longer than
    7162                 : ** a few hundred characters, it will be truncated to the length of the
    7163                 : ** buffer.
    7164                 : */
    7165                 : SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
    7166                 : 
    7167                 : /*
    7168                 : ** CAPI3REF: Write-Ahead Log Commit Hook
    7169                 : **
    7170                 : ** ^The [sqlite3_wal_hook()] function is used to register a callback that
    7171                 : ** will be invoked each time a database connection commits data to a
    7172                 : ** [write-ahead log] (i.e. whenever a transaction is committed in
    7173                 : ** [journal_mode | journal_mode=WAL mode]). 
    7174                 : **
    7175                 : ** ^The callback is invoked by SQLite after the commit has taken place and 
    7176                 : ** the associated write-lock on the database released, so the implementation 
    7177                 : ** may read, write or [checkpoint] the database as required.
    7178                 : **
    7179                 : ** ^The first parameter passed to the callback function when it is invoked
    7180                 : ** is a copy of the third parameter passed to sqlite3_wal_hook() when
    7181                 : ** registering the callback. ^The second is a copy of the database handle.
    7182                 : ** ^The third parameter is the name of the database that was written to -
    7183                 : ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
    7184                 : ** is the number of pages currently in the write-ahead log file,
    7185                 : ** including those that were just committed.
    7186                 : **
    7187                 : ** The callback function should normally return [SQLITE_OK].  ^If an error
    7188                 : ** code is returned, that error will propagate back up through the
    7189                 : ** SQLite code base to cause the statement that provoked the callback
    7190                 : ** to report an error, though the commit will have still occurred. If the
    7191                 : ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
    7192                 : ** that does not correspond to any valid SQLite error code, the results
    7193                 : ** are undefined.
    7194                 : **
    7195                 : ** A single database handle may have at most a single write-ahead log callback 
    7196                 : ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
    7197                 : ** previously registered write-ahead log callback. ^Note that the
    7198                 : ** [sqlite3_wal_autocheckpoint()] interface and the
    7199                 : ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
    7200                 : ** those overwrite any prior [sqlite3_wal_hook()] settings.
    7201                 : */
    7202                 : SQLITE_API void *sqlite3_wal_hook(
    7203                 :   sqlite3*, 
    7204                 :   int(*)(void *,sqlite3*,const char*,int),
    7205                 :   void*
    7206                 : );
    7207                 : 
    7208                 : /*
    7209                 : ** CAPI3REF: Configure an auto-checkpoint
    7210                 : **
    7211                 : ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
    7212                 : ** [sqlite3_wal_hook()] that causes any database on [database connection] D
    7213                 : ** to automatically [checkpoint]
    7214                 : ** after committing a transaction if there are N or
    7215                 : ** more frames in the [write-ahead log] file.  ^Passing zero or 
    7216                 : ** a negative value as the nFrame parameter disables automatic
    7217                 : ** checkpoints entirely.
    7218                 : **
    7219                 : ** ^The callback registered by this function replaces any existing callback
    7220                 : ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
    7221                 : ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
    7222                 : ** configured by this function.
    7223                 : **
    7224                 : ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
    7225                 : ** from SQL.
    7226                 : **
    7227                 : ** ^Every new [database connection] defaults to having the auto-checkpoint
    7228                 : ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
    7229                 : ** pages.  The use of this interface
    7230                 : ** is only necessary if the default setting is found to be suboptimal
    7231                 : ** for a particular application.
    7232                 : */
    7233                 : SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
    7234                 : 
    7235                 : /*
    7236                 : ** CAPI3REF: Checkpoint a database
    7237                 : **
    7238                 : ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
    7239                 : ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
    7240                 : ** empty string, then a checkpoint is run on all databases of
    7241                 : ** connection D.  ^If the database connection D is not in
    7242                 : ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
    7243                 : **
    7244                 : ** ^The [wal_checkpoint pragma] can be used to invoke this interface
    7245                 : ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
    7246                 : ** [wal_autocheckpoint pragma] can be used to cause this interface to be
    7247                 : ** run whenever the WAL reaches a certain size threshold.
    7248                 : **
    7249                 : ** See also: [sqlite3_wal_checkpoint_v2()]
    7250                 : */
    7251                 : SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
    7252                 : 
    7253                 : /*
    7254                 : ** CAPI3REF: Checkpoint a database
    7255                 : **
    7256                 : ** Run a checkpoint operation on WAL database zDb attached to database 
    7257                 : ** handle db. The specific operation is determined by the value of the 
    7258                 : ** eMode parameter:
    7259                 : **
    7260                 : ** <dl>
    7261                 : ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
    7262                 : **   Checkpoint as many frames as possible without waiting for any database 
    7263                 : **   readers or writers to finish. Sync the db file if all frames in the log
    7264                 : **   are checkpointed. This mode is the same as calling 
    7265                 : **   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
    7266                 : **
    7267                 : ** <dt>SQLITE_CHECKPOINT_FULL<dd>
    7268                 : **   This mode blocks (calls the busy-handler callback) until there is no
    7269                 : **   database writer and all readers are reading from the most recent database
    7270                 : **   snapshot. It then checkpoints all frames in the log file and syncs the
    7271                 : **   database file. This call blocks database writers while it is running,
    7272                 : **   but not database readers.
    7273                 : **
    7274                 : ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
    7275                 : **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after 
    7276                 : **   checkpointing the log file it blocks (calls the busy-handler callback)
    7277                 : **   until all readers are reading from the database file only. This ensures 
    7278                 : **   that the next client to write to the database file restarts the log file 
    7279                 : **   from the beginning. This call blocks database writers while it is running,
    7280                 : **   but not database readers.
    7281                 : ** </dl>
    7282                 : **
    7283                 : ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
    7284                 : ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
    7285                 : ** the total number of checkpointed frames (including any that were already
    7286                 : ** checkpointed when this function is called). *pnLog and *pnCkpt may be
    7287                 : ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
    7288                 : ** If no values are available because of an error, they are both set to -1
    7289                 : ** before returning to communicate this to the caller.
    7290                 : **
    7291                 : ** All calls obtain an exclusive "checkpoint" lock on the database file. If
    7292                 : ** any other process is running a checkpoint operation at the same time, the 
    7293                 : ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a 
    7294                 : ** busy-handler configured, it will not be invoked in this case.
    7295                 : **
    7296                 : ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive 
    7297                 : ** "writer" lock on the database file. If the writer lock cannot be obtained
    7298                 : ** immediately, and a busy-handler is configured, it is invoked and the writer
    7299                 : ** lock retried until either the busy-handler returns 0 or the lock is
    7300                 : ** successfully obtained. The busy-handler is also invoked while waiting for
    7301                 : ** database readers as described above. If the busy-handler returns 0 before
    7302                 : ** the writer lock is obtained or while waiting for database readers, the
    7303                 : ** checkpoint operation proceeds from that point in the same way as 
    7304                 : ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 
    7305                 : ** without blocking any further. SQLITE_BUSY is returned in this case.
    7306                 : **
    7307                 : ** If parameter zDb is NULL or points to a zero length string, then the
    7308                 : ** specified operation is attempted on all WAL databases. In this case the
    7309                 : ** values written to output parameters *pnLog and *pnCkpt are undefined. If 
    7310                 : ** an SQLITE_BUSY error is encountered when processing one or more of the 
    7311                 : ** attached WAL databases, the operation is still attempted on any remaining 
    7312                 : ** attached databases and SQLITE_BUSY is returned to the caller. If any other 
    7313                 : ** error occurs while processing an attached database, processing is abandoned 
    7314                 : ** and the error code returned to the caller immediately. If no error 
    7315                 : ** (SQLITE_BUSY or otherwise) is encountered while processing the attached 
    7316                 : ** databases, SQLITE_OK is returned.
    7317                 : **
    7318                 : ** If database zDb is the name of an attached database that is not in WAL
    7319                 : ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
    7320                 : ** zDb is not NULL (or a zero length string) and is not the name of any
    7321                 : ** attached database, SQLITE_ERROR is returned to the caller.
    7322                 : */
    7323                 : SQLITE_API int sqlite3_wal_checkpoint_v2(
    7324                 :   sqlite3 *db,                    /* Database handle */
    7325                 :   const char *zDb,                /* Name of attached database (or NULL) */
    7326                 :   int eMode,                      /* SQLITE_CHECKPOINT_* value */
    7327                 :   int *pnLog,                     /* OUT: Size of WAL log in frames */
    7328                 :   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
    7329                 : );
    7330                 : 
    7331                 : /*
    7332                 : ** CAPI3REF: Checkpoint operation parameters
    7333                 : **
    7334                 : ** These constants can be used as the 3rd parameter to
    7335                 : ** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
    7336                 : ** documentation for additional information about the meaning and use of
    7337                 : ** each of these values.
    7338                 : */
    7339                 : #define SQLITE_CHECKPOINT_PASSIVE 0
    7340                 : #define SQLITE_CHECKPOINT_FULL    1
    7341                 : #define SQLITE_CHECKPOINT_RESTART 2
    7342                 : 
    7343                 : /*
    7344                 : ** CAPI3REF: Virtual Table Interface Configuration
    7345                 : **
    7346                 : ** This function may be called by either the [xConnect] or [xCreate] method
    7347                 : ** of a [virtual table] implementation to configure
    7348                 : ** various facets of the virtual table interface.
    7349                 : **
    7350                 : ** If this interface is invoked outside the context of an xConnect or
    7351                 : ** xCreate virtual table method then the behavior is undefined.
    7352                 : **
    7353                 : ** At present, there is only one option that may be configured using
    7354                 : ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
    7355                 : ** may be added in the future.
    7356                 : */
    7357                 : SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
    7358                 : 
    7359                 : /*
    7360                 : ** CAPI3REF: Virtual Table Configuration Options
    7361                 : **
    7362                 : ** These macros define the various options to the
    7363                 : ** [sqlite3_vtab_config()] interface that [virtual table] implementations
    7364                 : ** can use to customize and optimize their behavior.
    7365                 : **
    7366                 : ** <dl>
    7367                 : ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
    7368                 : ** <dd>Calls of the form
    7369                 : ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
    7370                 : ** where X is an integer.  If X is zero, then the [virtual table] whose
    7371                 : ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
    7372                 : ** support constraints.  In this configuration (which is the default) if
    7373                 : ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
    7374                 : ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
    7375                 : ** specified as part of the users SQL statement, regardless of the actual
    7376                 : ** ON CONFLICT mode specified.
    7377                 : **
    7378                 : ** If X is non-zero, then the virtual table implementation guarantees
    7379                 : ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
    7380                 : ** any modifications to internal or persistent data structures have been made.
    7381                 : ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite 
    7382                 : ** is able to roll back a statement or database transaction, and abandon
    7383                 : ** or continue processing the current SQL statement as appropriate. 
    7384                 : ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
    7385                 : ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
    7386                 : ** had been ABORT.
    7387                 : **
    7388                 : ** Virtual table implementations that are required to handle OR REPLACE
    7389                 : ** must do so within the [xUpdate] method. If a call to the 
    7390                 : ** [sqlite3_vtab_on_conflict()] function indicates that the current ON 
    7391                 : ** CONFLICT policy is REPLACE, the virtual table implementation should 
    7392                 : ** silently replace the appropriate rows within the xUpdate callback and
    7393                 : ** return SQLITE_OK. Or, if this is not possible, it may return
    7394                 : ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT 
    7395                 : ** constraint handling.
    7396                 : ** </dl>
    7397                 : */
    7398                 : #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
    7399                 : 
    7400                 : /*
    7401                 : ** CAPI3REF: Determine The Virtual Table Conflict Policy
    7402                 : **
    7403                 : ** This function may only be called from within a call to the [xUpdate] method
    7404                 : ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
    7405                 : ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
    7406                 : ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
    7407                 : ** of the SQL statement that triggered the call to the [xUpdate] method of the
    7408                 : ** [virtual table].
    7409                 : */
    7410                 : SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
    7411                 : 
    7412                 : /*
    7413                 : ** CAPI3REF: Conflict resolution modes
    7414                 : **
    7415                 : ** These constants are returned by [sqlite3_vtab_on_conflict()] to
    7416                 : ** inform a [virtual table] implementation what the [ON CONFLICT] mode
    7417                 : ** is for the SQL statement being evaluated.
    7418                 : **
    7419                 : ** Note that the [SQLITE_IGNORE] constant is also used as a potential
    7420                 : ** return value from the [sqlite3_set_authorizer()] callback and that
    7421                 : ** [SQLITE_ABORT] is also a [result code].
    7422                 : */
    7423                 : #define SQLITE_ROLLBACK 1
    7424                 : /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
    7425                 : #define SQLITE_FAIL     3
    7426                 : /* #define SQLITE_ABORT 4  // Also an error code */
    7427                 : #define SQLITE_REPLACE  5
    7428                 : 
    7429                 : 
    7430                 : 
    7431                 : /*
    7432                 : ** Undo the hack that converts floating point types to integer for
    7433                 : ** builds on processors without floating point support.
    7434                 : */
    7435                 : #ifdef SQLITE_OMIT_FLOATING_POINT
    7436                 : # undef double
    7437                 : #endif
    7438                 : 
    7439                 : #if 0
    7440                 : }  /* End of the 'extern "C"' block */
    7441                 : #endif
    7442                 : #endif
    7443                 : 
    7444                 : /*
    7445                 : ** 2010 August 30
    7446                 : **
    7447                 : ** The author disclaims copyright to this source code.  In place of
    7448                 : ** a legal notice, here is a blessing:
    7449                 : **
    7450                 : **    May you do good and not evil.
    7451                 : **    May you find forgiveness for yourself and forgive others.
    7452                 : **    May you share freely, never taking more than you give.
    7453                 : **
    7454                 : *************************************************************************
    7455                 : */
    7456                 : 
    7457                 : #ifndef _SQLITE3RTREE_H_
    7458                 : #define _SQLITE3RTREE_H_
    7459                 : 
    7460                 : 
    7461                 : #if 0
    7462                 : extern "C" {
    7463                 : #endif
    7464                 : 
    7465                 : typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
    7466                 : 
    7467                 : /*
    7468                 : ** Register a geometry callback named zGeom that can be used as part of an
    7469                 : ** R-Tree geometry query as follows:
    7470                 : **
    7471                 : **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
    7472                 : */
    7473                 : SQLITE_API int sqlite3_rtree_geometry_callback(
    7474                 :   sqlite3 *db,
    7475                 :   const char *zGeom,
    7476                 :   int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
    7477                 :   void *pContext
    7478                 : );
    7479                 : 
    7480                 : 
    7481                 : /*
    7482                 : ** A pointer to a structure of the following type is passed as the first
    7483                 : ** argument to callbacks registered using rtree_geometry_callback().
    7484                 : */
    7485                 : struct sqlite3_rtree_geometry {
    7486                 :   void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
    7487                 :   int nParam;                     /* Size of array aParam[] */
    7488                 :   double *aParam;                 /* Parameters passed to SQL geom function */
    7489                 :   void *pUser;                    /* Callback implementation user data */
    7490                 :   void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
    7491                 : };
    7492                 : 
    7493                 : 
    7494                 : #if 0
    7495                 : }  /* end of the 'extern "C"' block */
    7496                 : #endif
    7497                 : 
    7498                 : #endif  /* ifndef _SQLITE3RTREE_H_ */
    7499                 : 
    7500                 : 
    7501                 : /************** End of sqlite3.h *********************************************/
    7502                 : /************** Continuing where we left off in sqliteInt.h ******************/
    7503                 : /************** Include hash.h in the middle of sqliteInt.h ******************/
    7504                 : /************** Begin file hash.h ********************************************/
    7505                 : /*
    7506                 : ** 2001 September 22
    7507                 : **
    7508                 : ** The author disclaims copyright to this source code.  In place of
    7509                 : ** a legal notice, here is a blessing:
    7510                 : **
    7511                 : **    May you do good and not evil.
    7512                 : **    May you find forgiveness for yourself and forgive others.
    7513                 : **    May you share freely, never taking more than you give.
    7514                 : **
    7515                 : *************************************************************************
    7516                 : ** This is the header file for the generic hash-table implemenation
    7517                 : ** used in SQLite.
    7518                 : */
    7519                 : #ifndef _SQLITE_HASH_H_
    7520                 : #define _SQLITE_HASH_H_
    7521                 : 
    7522                 : /* Forward declarations of structures. */
    7523                 : typedef struct Hash Hash;
    7524                 : typedef struct HashElem HashElem;
    7525                 : 
    7526                 : /* A complete hash table is an instance of the following structure.
    7527                 : ** The internals of this structure are intended to be opaque -- client
    7528                 : ** code should not attempt to access or modify the fields of this structure
    7529                 : ** directly.  Change this structure only by using the routines below.
    7530                 : ** However, some of the "procedures" and "functions" for modifying and
    7531                 : ** accessing this structure are really macros, so we can't really make
    7532                 : ** this structure opaque.
    7533                 : **
    7534                 : ** All elements of the hash table are on a single doubly-linked list.
    7535                 : ** Hash.first points to the head of this list.
    7536                 : **
    7537                 : ** There are Hash.htsize buckets.  Each bucket points to a spot in
    7538                 : ** the global doubly-linked list.  The contents of the bucket are the
    7539                 : ** element pointed to plus the next _ht.count-1 elements in the list.
    7540                 : **
    7541                 : ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
    7542                 : ** by a linear search of the global list.  For small tables, the 
    7543                 : ** Hash.ht table is never allocated because if there are few elements
    7544                 : ** in the table, it is faster to do a linear search than to manage
    7545                 : ** the hash table.
    7546                 : */
    7547                 : struct Hash {
    7548                 :   unsigned int htsize;      /* Number of buckets in the hash table */
    7549                 :   unsigned int count;       /* Number of entries in this table */
    7550                 :   HashElem *first;          /* The first element of the array */
    7551                 :   struct _ht {              /* the hash table */
    7552                 :     int count;                 /* Number of entries with this hash */
    7553                 :     HashElem *chain;           /* Pointer to first entry with this hash */
    7554                 :   } *ht;
    7555                 : };
    7556                 : 
    7557                 : /* Each element in the hash table is an instance of the following 
    7558                 : ** structure.  All elements are stored on a single doubly-linked list.
    7559                 : **
    7560                 : ** Again, this structure is intended to be opaque, but it can't really
    7561                 : ** be opaque because it is used by macros.
    7562                 : */
    7563                 : struct HashElem {
    7564                 :   HashElem *next, *prev;       /* Next and previous elements in the table */
    7565                 :   void *data;                  /* Data associated with this element */
    7566                 :   const char *pKey; int nKey;  /* Key associated with this element */
    7567                 : };
    7568                 : 
    7569                 : /*
    7570                 : ** Access routines.  To delete, insert a NULL pointer.
    7571                 : */
    7572                 : SQLITE_PRIVATE void sqlite3HashInit(Hash*);
    7573                 : SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
    7574                 : SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
    7575                 : SQLITE_PRIVATE void sqlite3HashClear(Hash*);
    7576                 : 
    7577                 : /*
    7578                 : ** Macros for looping over all elements of a hash table.  The idiom is
    7579                 : ** like this:
    7580                 : **
    7581                 : **   Hash h;
    7582                 : **   HashElem *p;
    7583                 : **   ...
    7584                 : **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
    7585                 : **     SomeStructure *pData = sqliteHashData(p);
    7586                 : **     // do something with pData
    7587                 : **   }
    7588                 : */
    7589                 : #define sqliteHashFirst(H)  ((H)->first)
    7590                 : #define sqliteHashNext(E)   ((E)->next)
    7591                 : #define sqliteHashData(E)   ((E)->data)
    7592                 : /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
    7593                 : /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
    7594                 : 
    7595                 : /*
    7596                 : ** Number of entries in a hash table
    7597                 : */
    7598                 : /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
    7599                 : 
    7600                 : #endif /* _SQLITE_HASH_H_ */
    7601                 : 
    7602                 : /************** End of hash.h ************************************************/
    7603                 : /************** Continuing where we left off in sqliteInt.h ******************/
    7604                 : /************** Include parse.h in the middle of sqliteInt.h *****************/
    7605                 : /************** Begin file parse.h *******************************************/
    7606                 : #define TK_SEMI                            1
    7607                 : #define TK_EXPLAIN                         2
    7608                 : #define TK_QUERY                           3
    7609                 : #define TK_PLAN                            4
    7610                 : #define TK_BEGIN                           5
    7611                 : #define TK_TRANSACTION                     6
    7612                 : #define TK_DEFERRED                        7
    7613                 : #define TK_IMMEDIATE                       8
    7614                 : #define TK_EXCLUSIVE                       9
    7615                 : #define TK_COMMIT                         10
    7616                 : #define TK_END                            11
    7617                 : #define TK_ROLLBACK                       12
    7618                 : #define TK_SAVEPOINT                      13
    7619                 : #define TK_RELEASE                        14
    7620                 : #define TK_TO                             15
    7621                 : #define TK_TABLE                          16
    7622                 : #define TK_CREATE                         17
    7623                 : #define TK_IF                             18
    7624                 : #define TK_NOT                            19
    7625                 : #define TK_EXISTS                         20
    7626                 : #define TK_TEMP                           21
    7627                 : #define TK_LP                             22
    7628                 : #define TK_RP                             23
    7629                 : #define TK_AS                             24
    7630                 : #define TK_COMMA                          25
    7631                 : #define TK_ID                             26
    7632                 : #define TK_INDEXED                        27
    7633                 : #define TK_ABORT                          28
    7634                 : #define TK_ACTION                         29
    7635                 : #define TK_AFTER                          30
    7636                 : #define TK_ANALYZE                        31
    7637                 : #define TK_ASC                            32
    7638                 : #define TK_ATTACH                         33
    7639                 : #define TK_BEFORE                         34
    7640                 : #define TK_BY                             35
    7641                 : #define TK_CASCADE                        36
    7642                 : #define TK_CAST                           37
    7643                 : #define TK_COLUMNKW                       38
    7644                 : #define TK_CONFLICT                       39
    7645                 : #define TK_DATABASE                       40
    7646                 : #define TK_DESC                           41
    7647                 : #define TK_DETACH                         42
    7648                 : #define TK_EACH                           43
    7649                 : #define TK_FAIL                           44
    7650                 : #define TK_FOR                            45
    7651                 : #define TK_IGNORE                         46
    7652                 : #define TK_INITIALLY                      47
    7653                 : #define TK_INSTEAD                        48
    7654                 : #define TK_LIKE_KW                        49
    7655                 : #define TK_MATCH                          50
    7656                 : #define TK_NO                             51
    7657                 : #define TK_KEY                            52
    7658                 : #define TK_OF                             53
    7659                 : #define TK_OFFSET                         54
    7660                 : #define TK_PRAGMA                         55
    7661                 : #define TK_RAISE                          56
    7662                 : #define TK_REPLACE                        57
    7663                 : #define TK_RESTRICT                       58
    7664                 : #define TK_ROW                            59
    7665                 : #define TK_TRIGGER                        60
    7666                 : #define TK_VACUUM                         61
    7667                 : #define TK_VIEW                           62
    7668                 : #define TK_VIRTUAL                        63
    7669                 : #define TK_REINDEX                        64
    7670                 : #define TK_RENAME                         65
    7671                 : #define TK_CTIME_KW                       66
    7672                 : #define TK_ANY                            67
    7673                 : #define TK_OR                             68
    7674                 : #define TK_AND                            69
    7675                 : #define TK_IS                             70
    7676                 : #define TK_BETWEEN                        71
    7677                 : #define TK_IN                             72
    7678                 : #define TK_ISNULL                         73
    7679                 : #define TK_NOTNULL                        74
    7680                 : #define TK_NE                             75
    7681                 : #define TK_EQ                             76
    7682                 : #define TK_GT                             77
    7683                 : #define TK_LE                             78
    7684                 : #define TK_LT                             79
    7685                 : #define TK_GE                             80
    7686                 : #define TK_ESCAPE                         81
    7687                 : #define TK_BITAND                         82
    7688                 : #define TK_BITOR                          83
    7689                 : #define TK_LSHIFT                         84
    7690                 : #define TK_RSHIFT                         85
    7691                 : #define TK_PLUS                           86
    7692                 : #define TK_MINUS                          87
    7693                 : #define TK_STAR                           88
    7694                 : #define TK_SLASH                          89
    7695                 : #define TK_REM                            90
    7696                 : #define TK_CONCAT                         91
    7697                 : #define TK_COLLATE                        92
    7698                 : #define TK_BITNOT                         93
    7699                 : #define TK_STRING                         94
    7700                 : #define TK_JOIN_KW                        95
    7701                 : #define TK_CONSTRAINT                     96
    7702                 : #define TK_DEFAULT                        97
    7703                 : #define TK_NULL                           98
    7704                 : #define TK_PRIMARY                        99
    7705                 : #define TK_UNIQUE                         100
    7706                 : #define TK_CHECK                          101
    7707                 : #define TK_REFERENCES                     102
    7708                 : #define TK_AUTOINCR                       103
    7709                 : #define TK_ON                             104
    7710                 : #define TK_INSERT                         105
    7711                 : #define TK_DELETE                         106
    7712                 : #define TK_UPDATE                         107
    7713                 : #define TK_SET                            108
    7714                 : #define TK_DEFERRABLE                     109
    7715                 : #define TK_FOREIGN                        110
    7716                 : #define TK_DROP                           111
    7717                 : #define TK_UNION                          112
    7718                 : #define TK_ALL                            113
    7719                 : #define TK_EXCEPT                         114
    7720                 : #define TK_INTERSECT                      115
    7721                 : #define TK_SELECT                         116
    7722                 : #define TK_DISTINCT                       117
    7723                 : #define TK_DOT                            118
    7724                 : #define TK_FROM                           119
    7725                 : #define TK_JOIN                           120
    7726                 : #define TK_USING                          121
    7727                 : #define TK_ORDER                          122
    7728                 : #define TK_GROUP                          123
    7729                 : #define TK_HAVING                         124
    7730                 : #define TK_LIMIT                          125
    7731                 : #define TK_WHERE                          126
    7732                 : #define TK_INTO                           127
    7733                 : #define TK_VALUES                         128
    7734                 : #define TK_INTEGER                        129
    7735                 : #define TK_FLOAT                          130
    7736                 : #define TK_BLOB                           131
    7737                 : #define TK_REGISTER                       132
    7738                 : #define TK_VARIABLE                       133
    7739                 : #define TK_CASE                           134
    7740                 : #define TK_WHEN                           135
    7741                 : #define TK_THEN                           136
    7742                 : #define TK_ELSE                           137
    7743                 : #define TK_INDEX                          138
    7744                 : #define TK_ALTER                          139
    7745                 : #define TK_ADD                            140
    7746                 : #define TK_TO_TEXT                        141
    7747                 : #define TK_TO_BLOB                        142
    7748                 : #define TK_TO_NUMERIC                     143
    7749                 : #define TK_TO_INT                         144
    7750                 : #define TK_TO_REAL                        145
    7751                 : #define TK_ISNOT                          146
    7752                 : #define TK_END_OF_FILE                    147
    7753                 : #define TK_ILLEGAL                        148
    7754                 : #define TK_SPACE                          149
    7755                 : #define TK_UNCLOSED_STRING                150
    7756                 : #define TK_FUNCTION                       151
    7757                 : #define TK_COLUMN                         152
    7758                 : #define TK_AGG_FUNCTION                   153
    7759                 : #define TK_AGG_COLUMN                     154
    7760                 : #define TK_CONST_FUNC                     155
    7761                 : #define TK_UMINUS                         156
    7762                 : #define TK_UPLUS                          157
    7763                 : 
    7764                 : /************** End of parse.h ***********************************************/
    7765                 : /************** Continuing where we left off in sqliteInt.h ******************/
    7766                 : #include <stdio.h>
    7767                 : #include <stdlib.h>
    7768                 : #include <string.h>
    7769                 : #include <assert.h>
    7770                 : #include <stddef.h>
    7771                 : 
    7772                 : /*
    7773                 : ** If compiling for a processor that lacks floating point support,
    7774                 : ** substitute integer for floating-point
    7775                 : */
    7776                 : #ifdef SQLITE_OMIT_FLOATING_POINT
    7777                 : # define double sqlite_int64
    7778                 : # define float sqlite_int64
    7779                 : # define LONGDOUBLE_TYPE sqlite_int64
    7780                 : # ifndef SQLITE_BIG_DBL
    7781                 : #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
    7782                 : # endif
    7783                 : # define SQLITE_OMIT_DATETIME_FUNCS 1
    7784                 : # define SQLITE_OMIT_TRACE 1
    7785                 : # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
    7786                 : # undef SQLITE_HAVE_ISNAN
    7787                 : #endif
    7788                 : #ifndef SQLITE_BIG_DBL
    7789                 : # define SQLITE_BIG_DBL (1e99)
    7790                 : #endif
    7791                 : 
    7792                 : /*
    7793                 : ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
    7794                 : ** afterward. Having this macro allows us to cause the C compiler 
    7795                 : ** to omit code used by TEMP tables without messy #ifndef statements.
    7796                 : */
    7797                 : #ifdef SQLITE_OMIT_TEMPDB
    7798                 : #define OMIT_TEMPDB 1
    7799                 : #else
    7800                 : #define OMIT_TEMPDB 0
    7801                 : #endif
    7802                 : 
    7803                 : /*
    7804                 : ** The "file format" number is an integer that is incremented whenever
    7805                 : ** the VDBE-level file format changes.  The following macros define the
    7806                 : ** the default file format for new databases and the maximum file format
    7807                 : ** that the library can read.
    7808                 : */
    7809                 : #define SQLITE_MAX_FILE_FORMAT 4
    7810                 : #ifndef SQLITE_DEFAULT_FILE_FORMAT
    7811                 : # define SQLITE_DEFAULT_FILE_FORMAT 4
    7812                 : #endif
    7813                 : 
    7814                 : /*
    7815                 : ** Determine whether triggers are recursive by default.  This can be
    7816                 : ** changed at run-time using a pragma.
    7817                 : */
    7818                 : #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
    7819                 : # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
    7820                 : #endif
    7821                 : 
    7822                 : /*
    7823                 : ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
    7824                 : ** on the command-line
    7825                 : */
    7826                 : #ifndef SQLITE_TEMP_STORE
    7827                 : # define SQLITE_TEMP_STORE 1
    7828                 : #endif
    7829                 : 
    7830                 : /*
    7831                 : ** GCC does not define the offsetof() macro so we'll have to do it
    7832                 : ** ourselves.
    7833                 : */
    7834                 : #ifndef offsetof
    7835                 : #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
    7836                 : #endif
    7837                 : 
    7838                 : /*
    7839                 : ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
    7840                 : ** not, there are still machines out there that use EBCDIC.)
    7841                 : */
    7842                 : #if 'A' == '\301'
    7843                 : # define SQLITE_EBCDIC 1
    7844                 : #else
    7845                 : # define SQLITE_ASCII 1
    7846                 : #endif
    7847                 : 
    7848                 : /*
    7849                 : ** Integers of known sizes.  These typedefs might change for architectures
    7850                 : ** where the sizes very.  Preprocessor macros are available so that the
    7851                 : ** types can be conveniently redefined at compile-type.  Like this:
    7852                 : **
    7853                 : **         cc '-DUINTPTR_TYPE=long long int' ...
    7854                 : */
    7855                 : #ifndef UINT32_TYPE
    7856                 : # ifdef HAVE_UINT32_T
    7857                 : #  define UINT32_TYPE uint32_t
    7858                 : # else
    7859                 : #  define UINT32_TYPE unsigned int
    7860                 : # endif
    7861                 : #endif
    7862                 : #ifndef UINT16_TYPE
    7863                 : # ifdef HAVE_UINT16_T
    7864                 : #  define UINT16_TYPE uint16_t
    7865                 : # else
    7866                 : #  define UINT16_TYPE unsigned short int
    7867                 : # endif
    7868                 : #endif
    7869                 : #ifndef INT16_TYPE
    7870                 : # ifdef HAVE_INT16_T
    7871                 : #  define INT16_TYPE int16_t
    7872                 : # else
    7873                 : #  define INT16_TYPE short int
    7874                 : # endif
    7875                 : #endif
    7876                 : #ifndef UINT8_TYPE
    7877                 : # ifdef HAVE_UINT8_T
    7878                 : #  define UINT8_TYPE uint8_t
    7879                 : # else
    7880                 : #  define UINT8_TYPE unsigned char
    7881                 : # endif
    7882                 : #endif
    7883                 : #ifndef INT8_TYPE
    7884                 : # ifdef HAVE_INT8_T
    7885                 : #  define INT8_TYPE int8_t
    7886                 : # else
    7887                 : #  define INT8_TYPE signed char
    7888                 : # endif
    7889                 : #endif
    7890                 : #ifndef LONGDOUBLE_TYPE
    7891                 : # define LONGDOUBLE_TYPE long double
    7892                 : #endif
    7893                 : typedef sqlite_int64 i64;          /* 8-byte signed integer */
    7894                 : typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
    7895                 : typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
    7896                 : typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
    7897                 : typedef INT16_TYPE i16;            /* 2-byte signed integer */
    7898                 : typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
    7899                 : typedef INT8_TYPE i8;              /* 1-byte signed integer */
    7900                 : 
    7901                 : /*
    7902                 : ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
    7903                 : ** that can be stored in a u32 without loss of data.  The value
    7904                 : ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
    7905                 : ** have to specify the value in the less intuitive manner shown:
    7906                 : */
    7907                 : #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
    7908                 : 
    7909                 : /*
    7910                 : ** The datatype used to store estimates of the number of rows in a
    7911                 : ** table or index.  This is an unsigned integer type.  For 99.9% of
    7912                 : ** the world, a 32-bit integer is sufficient.  But a 64-bit integer
    7913                 : ** can be used at compile-time if desired.
    7914                 : */
    7915                 : #ifdef SQLITE_64BIT_STATS
    7916                 :  typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
    7917                 : #else
    7918                 :  typedef u32 tRowcnt;    /* 32-bit is the default */
    7919                 : #endif
    7920                 : 
    7921                 : /*
    7922                 : ** Macros to determine whether the machine is big or little endian,
    7923                 : ** evaluated at runtime.
    7924                 : */
    7925                 : #ifdef SQLITE_AMALGAMATION
    7926                 : SQLITE_PRIVATE const int sqlite3one = 1;
    7927                 : #else
    7928                 : SQLITE_PRIVATE const int sqlite3one;
    7929                 : #endif
    7930                 : #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
    7931                 :                              || defined(__x86_64) || defined(__x86_64__)
    7932                 : # define SQLITE_BIGENDIAN    0
    7933                 : # define SQLITE_LITTLEENDIAN 1
    7934                 : # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
    7935                 : #else
    7936                 : # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
    7937                 : # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
    7938                 : # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
    7939                 : #endif
    7940                 : 
    7941                 : /*
    7942                 : ** Constants for the largest and smallest possible 64-bit signed integers.
    7943                 : ** These macros are designed to work correctly on both 32-bit and 64-bit
    7944                 : ** compilers.
    7945                 : */
    7946                 : #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
    7947                 : #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
    7948                 : 
    7949                 : /* 
    7950                 : ** Round up a number to the next larger multiple of 8.  This is used
    7951                 : ** to force 8-byte alignment on 64-bit architectures.
    7952                 : */
    7953                 : #define ROUND8(x)     (((x)+7)&~7)
    7954                 : 
    7955                 : /*
    7956                 : ** Round down to the nearest multiple of 8
    7957                 : */
    7958                 : #define ROUNDDOWN8(x) ((x)&~7)
    7959                 : 
    7960                 : /*
    7961                 : ** Assert that the pointer X is aligned to an 8-byte boundary.  This
    7962                 : ** macro is used only within assert() to verify that the code gets
    7963                 : ** all alignment restrictions correct.
    7964                 : **
    7965                 : ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
    7966                 : ** underlying malloc() implemention might return us 4-byte aligned
    7967                 : ** pointers.  In that case, only verify 4-byte alignment.
    7968                 : */
    7969                 : #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
    7970                 : # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
    7971                 : #else
    7972                 : # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
    7973                 : #endif
    7974                 : 
    7975                 : 
    7976                 : /*
    7977                 : ** An instance of the following structure is used to store the busy-handler
    7978                 : ** callback for a given sqlite handle. 
    7979                 : **
    7980                 : ** The sqlite.busyHandler member of the sqlite struct contains the busy
    7981                 : ** callback for the database handle. Each pager opened via the sqlite
    7982                 : ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
    7983                 : ** callback is currently invoked only from within pager.c.
    7984                 : */
    7985                 : typedef struct BusyHandler BusyHandler;
    7986                 : struct BusyHandler {
    7987                 :   int (*xFunc)(void *,int);  /* The busy callback */
    7988                 :   void *pArg;                /* First arg to busy callback */
    7989                 :   int nBusy;                 /* Incremented with each busy call */
    7990                 : };
    7991                 : 
    7992                 : /*
    7993                 : ** Name of the master database table.  The master database table
    7994                 : ** is a special table that holds the names and attributes of all
    7995                 : ** user tables and indices.
    7996                 : */
    7997                 : #define MASTER_NAME       "sqlite_master"
    7998                 : #define TEMP_MASTER_NAME  "sqlite_temp_master"
    7999                 : 
    8000                 : /*
    8001                 : ** The root-page of the master database table.
    8002                 : */
    8003                 : #define MASTER_ROOT       1
    8004                 : 
    8005                 : /*
    8006                 : ** The name of the schema table.
    8007                 : */
    8008                 : #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
    8009                 : 
    8010                 : /*
    8011                 : ** A convenience macro that returns the number of elements in
    8012                 : ** an array.
    8013                 : */
    8014                 : #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
    8015                 : 
    8016                 : /*
    8017                 : ** The following value as a destructor means to use sqlite3DbFree().
    8018                 : ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
    8019                 : */
    8020                 : #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3DbFree)
    8021                 : 
    8022                 : /*
    8023                 : ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
    8024                 : ** not support Writable Static Data (WSD) such as global and static variables.
    8025                 : ** All variables must either be on the stack or dynamically allocated from
    8026                 : ** the heap.  When WSD is unsupported, the variable declarations scattered
    8027                 : ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
    8028                 : ** macro is used for this purpose.  And instead of referencing the variable
    8029                 : ** directly, we use its constant as a key to lookup the run-time allocated
    8030                 : ** buffer that holds real variable.  The constant is also the initializer
    8031                 : ** for the run-time allocated buffer.
    8032                 : **
    8033                 : ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
    8034                 : ** macros become no-ops and have zero performance impact.
    8035                 : */
    8036                 : #ifdef SQLITE_OMIT_WSD
    8037                 :   #define SQLITE_WSD const
    8038                 :   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
    8039                 :   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
    8040                 : SQLITE_API   int sqlite3_wsd_init(int N, int J);
    8041                 : SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
    8042                 : #else
    8043                 :   #define SQLITE_WSD 
    8044                 :   #define GLOBAL(t,v) v
    8045                 :   #define sqlite3GlobalConfig sqlite3Config
    8046                 : #endif
    8047                 : 
    8048                 : /*
    8049                 : ** The following macros are used to suppress compiler warnings and to
    8050                 : ** make it clear to human readers when a function parameter is deliberately 
    8051                 : ** left unused within the body of a function. This usually happens when
    8052                 : ** a function is called via a function pointer. For example the 
    8053                 : ** implementation of an SQL aggregate step callback may not use the
    8054                 : ** parameter indicating the number of arguments passed to the aggregate,
    8055                 : ** if it knows that this is enforced elsewhere.
    8056                 : **
    8057                 : ** When a function parameter is not used at all within the body of a function,
    8058                 : ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
    8059                 : ** However, these macros may also be used to suppress warnings related to
    8060                 : ** parameters that may or may not be used depending on compilation options.
    8061                 : ** For example those parameters only used in assert() statements. In these
    8062                 : ** cases the parameters are named as per the usual conventions.
    8063                 : */
    8064                 : #define UNUSED_PARAMETER(x) (void)(x)
    8065                 : #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
    8066                 : 
    8067                 : /*
    8068                 : ** Forward references to structures
    8069                 : */
    8070                 : typedef struct AggInfo AggInfo;
    8071                 : typedef struct AuthContext AuthContext;
    8072                 : typedef struct AutoincInfo AutoincInfo;
    8073                 : typedef struct Bitvec Bitvec;
    8074                 : typedef struct CollSeq CollSeq;
    8075                 : typedef struct Column Column;
    8076                 : typedef struct Db Db;
    8077                 : typedef struct Schema Schema;
    8078                 : typedef struct Expr Expr;
    8079                 : typedef struct ExprList ExprList;
    8080                 : typedef struct ExprSpan ExprSpan;
    8081                 : typedef struct FKey FKey;
    8082                 : typedef struct FuncDestructor FuncDestructor;
    8083                 : typedef struct FuncDef FuncDef;
    8084                 : typedef struct FuncDefHash FuncDefHash;
    8085                 : typedef struct IdList IdList;
    8086                 : typedef struct Index Index;
    8087                 : typedef struct IndexSample IndexSample;
    8088                 : typedef struct KeyClass KeyClass;
    8089                 : typedef struct KeyInfo KeyInfo;
    8090                 : typedef struct Lookaside Lookaside;
    8091                 : typedef struct LookasideSlot LookasideSlot;
    8092                 : typedef struct Module Module;
    8093                 : typedef struct NameContext NameContext;
    8094                 : typedef struct Parse Parse;
    8095                 : typedef struct RowSet RowSet;
    8096                 : typedef struct Savepoint Savepoint;
    8097                 : typedef struct Select Select;
    8098                 : typedef struct SrcList SrcList;
    8099                 : typedef struct StrAccum StrAccum;
    8100                 : typedef struct Table Table;
    8101                 : typedef struct TableLock TableLock;
    8102                 : typedef struct Token Token;
    8103                 : typedef struct Trigger Trigger;
    8104                 : typedef struct TriggerPrg TriggerPrg;
    8105                 : typedef struct TriggerStep TriggerStep;
    8106                 : typedef struct UnpackedRecord UnpackedRecord;
    8107                 : typedef struct VTable VTable;
    8108                 : typedef struct VtabCtx VtabCtx;
    8109                 : typedef struct Walker Walker;
    8110                 : typedef struct WherePlan WherePlan;
    8111                 : typedef struct WhereInfo WhereInfo;
    8112                 : typedef struct WhereLevel WhereLevel;
    8113                 : 
    8114                 : /*
    8115                 : ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
    8116                 : ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
    8117                 : ** pointer types (i.e. FuncDef) defined above.
    8118                 : */
    8119                 : /************** Include btree.h in the middle of sqliteInt.h *****************/
    8120                 : /************** Begin file btree.h *******************************************/
    8121                 : /*
    8122                 : ** 2001 September 15
    8123                 : **
    8124                 : ** The author disclaims copyright to this source code.  In place of
    8125                 : ** a legal notice, here is a blessing:
    8126                 : **
    8127                 : **    May you do good and not evil.
    8128                 : **    May you find forgiveness for yourself and forgive others.
    8129                 : **    May you share freely, never taking more than you give.
    8130                 : **
    8131                 : *************************************************************************
    8132                 : ** This header file defines the interface that the sqlite B-Tree file
    8133                 : ** subsystem.  See comments in the source code for a detailed description
    8134                 : ** of what each interface routine does.
    8135                 : */
    8136                 : #ifndef _BTREE_H_
    8137                 : #define _BTREE_H_
    8138                 : 
    8139                 : /* TODO: This definition is just included so other modules compile. It
    8140                 : ** needs to be revisited.
    8141                 : */
    8142                 : #define SQLITE_N_BTREE_META 10
    8143                 : 
    8144                 : /*
    8145                 : ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
    8146                 : ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
    8147                 : */
    8148                 : #ifndef SQLITE_DEFAULT_AUTOVACUUM
    8149                 :   #define SQLITE_DEFAULT_AUTOVACUUM 0
    8150                 : #endif
    8151                 : 
    8152                 : #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
    8153                 : #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
    8154                 : #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
    8155                 : 
    8156                 : /*
    8157                 : ** Forward declarations of structure
    8158                 : */
    8159                 : typedef struct Btree Btree;
    8160                 : typedef struct BtCursor BtCursor;
    8161                 : typedef struct BtShared BtShared;
    8162                 : 
    8163                 : 
    8164                 : SQLITE_PRIVATE int sqlite3BtreeOpen(
    8165                 :   sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
    8166                 :   const char *zFilename,   /* Name of database file to open */
    8167                 :   sqlite3 *db,             /* Associated database connection */
    8168                 :   Btree **ppBtree,         /* Return open Btree* here */
    8169                 :   int flags,               /* Flags */
    8170                 :   int vfsFlags             /* Flags passed through to VFS open */
    8171                 : );
    8172                 : 
    8173                 : /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
    8174                 : ** following values.
    8175                 : **
    8176                 : ** NOTE:  These values must match the corresponding PAGER_ values in
    8177                 : ** pager.h.
    8178                 : */
    8179                 : #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
    8180                 : #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
    8181                 : #define BTREE_MEMORY        4  /* This is an in-memory DB */
    8182                 : #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
    8183                 : #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
    8184                 : 
    8185                 : SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
    8186                 : SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
    8187                 : SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
    8188                 : SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
    8189                 : SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
    8190                 : SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
    8191                 : SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
    8192                 : SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
    8193                 : SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
    8194                 : SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
    8195                 : SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
    8196                 : SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
    8197                 : SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
    8198                 : SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
    8199                 : SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
    8200                 : SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
    8201                 : SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
    8202                 : SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
    8203                 : SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
    8204                 : SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
    8205                 : SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
    8206                 : SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
    8207                 : SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
    8208                 : SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
    8209                 : SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
    8210                 : SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
    8211                 : 
    8212                 : SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
    8213                 : SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
    8214                 : SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
    8215                 : 
    8216                 : SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
    8217                 : 
    8218                 : /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
    8219                 : ** of the flags shown below.
    8220                 : **
    8221                 : ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
    8222                 : ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
    8223                 : ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
    8224                 : ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
    8225                 : ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
    8226                 : ** indices.)
    8227                 : */
    8228                 : #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
    8229                 : #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
    8230                 : 
    8231                 : SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
    8232                 : SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
    8233                 : SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
    8234                 : 
    8235                 : SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
    8236                 : SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
    8237                 : 
    8238                 : /*
    8239                 : ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
    8240                 : ** should be one of the following values. The integer values are assigned 
    8241                 : ** to constants so that the offset of the corresponding field in an
    8242                 : ** SQLite database header may be found using the following formula:
    8243                 : **
    8244                 : **   offset = 36 + (idx * 4)
    8245                 : **
    8246                 : ** For example, the free-page-count field is located at byte offset 36 of
    8247                 : ** the database file header. The incr-vacuum-flag field is located at
    8248                 : ** byte offset 64 (== 36+4*7).
    8249                 : */
    8250                 : #define BTREE_FREE_PAGE_COUNT     0
    8251                 : #define BTREE_SCHEMA_VERSION      1
    8252                 : #define BTREE_FILE_FORMAT         2
    8253                 : #define BTREE_DEFAULT_CACHE_SIZE  3
    8254                 : #define BTREE_LARGEST_ROOT_PAGE   4
    8255                 : #define BTREE_TEXT_ENCODING       5
    8256                 : #define BTREE_USER_VERSION        6
    8257                 : #define BTREE_INCR_VACUUM         7
    8258                 : 
    8259                 : SQLITE_PRIVATE int sqlite3BtreeCursor(
    8260                 :   Btree*,                              /* BTree containing table to open */
    8261                 :   int iTable,                          /* Index of root page */
    8262                 :   int wrFlag,                          /* 1 for writing.  0 for read-only */
    8263                 :   struct KeyInfo*,                     /* First argument to compare function */
    8264                 :   BtCursor *pCursor                    /* Space to write cursor structure */
    8265                 : );
    8266                 : SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
    8267                 : SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
    8268                 : 
    8269                 : SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
    8270                 : SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
    8271                 :   BtCursor*,
    8272                 :   UnpackedRecord *pUnKey,
    8273                 :   i64 intKey,
    8274                 :   int bias,
    8275                 :   int *pRes
    8276                 : );
    8277                 : SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
    8278                 : SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
    8279                 : SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
    8280                 :                                   const void *pData, int nData,
    8281                 :                                   int nZero, int bias, int seekResult);
    8282                 : SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
    8283                 : SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
    8284                 : SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
    8285                 : SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
    8286                 : SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
    8287                 : SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
    8288                 : SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
    8289                 : SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
    8290                 : SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
    8291                 : SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
    8292                 : SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
    8293                 : SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
    8294                 : SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
    8295                 : 
    8296                 : SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
    8297                 : SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
    8298                 : 
    8299                 : SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
    8300                 : SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
    8301                 : SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
    8302                 : 
    8303                 : SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
    8304                 : 
    8305                 : #ifndef NDEBUG
    8306                 : SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
    8307                 : #endif
    8308                 : 
    8309                 : #ifndef SQLITE_OMIT_BTREECOUNT
    8310                 : SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
    8311                 : #endif
    8312                 : 
    8313                 : #ifdef SQLITE_TEST
    8314                 : SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
    8315                 : SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
    8316                 : #endif
    8317                 : 
    8318                 : #ifndef SQLITE_OMIT_WAL
    8319                 : SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
    8320                 : #endif
    8321                 : 
    8322                 : /*
    8323                 : ** If we are not using shared cache, then there is no need to
    8324                 : ** use mutexes to access the BtShared structures.  So make the
    8325                 : ** Enter and Leave procedures no-ops.
    8326                 : */
    8327                 : #ifndef SQLITE_OMIT_SHARED_CACHE
    8328                 : SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
    8329                 : SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
    8330                 : #else
    8331                 : # define sqlite3BtreeEnter(X) 
    8332                 : # define sqlite3BtreeEnterAll(X)
    8333                 : #endif
    8334                 : 
    8335                 : #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
    8336                 : SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
    8337                 : SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
    8338                 : SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
    8339                 : SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
    8340                 : SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
    8341                 : #ifndef NDEBUG
    8342                 :   /* These routines are used inside assert() statements only. */
    8343                 : SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
    8344                 : SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
    8345                 : SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
    8346                 : #endif
    8347                 : #else
    8348                 : 
    8349                 : # define sqlite3BtreeSharable(X) 0
    8350                 : # define sqlite3BtreeLeave(X)
    8351                 : # define sqlite3BtreeEnterCursor(X)
    8352                 : # define sqlite3BtreeLeaveCursor(X)
    8353                 : # define sqlite3BtreeLeaveAll(X)
    8354                 : 
    8355                 : # define sqlite3BtreeHoldsMutex(X) 1
    8356                 : # define sqlite3BtreeHoldsAllMutexes(X) 1
    8357                 : # define sqlite3SchemaMutexHeld(X,Y,Z) 1
    8358                 : #endif
    8359                 : 
    8360                 : 
    8361                 : #endif /* _BTREE_H_ */
    8362                 : 
    8363                 : /************** End of btree.h ***********************************************/
    8364                 : /************** Continuing where we left off in sqliteInt.h ******************/
    8365                 : /************** Include vdbe.h in the middle of sqliteInt.h ******************/
    8366                 : /************** Begin file vdbe.h ********************************************/
    8367                 : /*
    8368                 : ** 2001 September 15
    8369                 : **
    8370                 : ** The author disclaims copyright to this source code.  In place of
    8371                 : ** a legal notice, here is a blessing:
    8372                 : **
    8373                 : **    May you do good and not evil.
    8374                 : **    May you find forgiveness for yourself and forgive others.
    8375                 : **    May you share freely, never taking more than you give.
    8376                 : **
    8377                 : *************************************************************************
    8378                 : ** Header file for the Virtual DataBase Engine (VDBE)
    8379                 : **
    8380                 : ** This header defines the interface to the virtual database engine
    8381                 : ** or VDBE.  The VDBE implements an abstract machine that runs a
    8382                 : ** simple program to access and modify the underlying database.
    8383                 : */
    8384                 : #ifndef _SQLITE_VDBE_H_
    8385                 : #define _SQLITE_VDBE_H_
    8386                 : /* #include <stdio.h> */
    8387                 : 
    8388                 : /*
    8389                 : ** A single VDBE is an opaque structure named "Vdbe".  Only routines
    8390                 : ** in the source file sqliteVdbe.c are allowed to see the insides
    8391                 : ** of this structure.
    8392                 : */
    8393                 : typedef struct Vdbe Vdbe;
    8394                 : 
    8395                 : /*
    8396                 : ** The names of the following types declared in vdbeInt.h are required
    8397                 : ** for the VdbeOp definition.
    8398                 : */
    8399                 : typedef struct VdbeFunc VdbeFunc;
    8400                 : typedef struct Mem Mem;
    8401                 : typedef struct SubProgram SubProgram;
    8402                 : 
    8403                 : /*
    8404                 : ** A single instruction of the virtual machine has an opcode
    8405                 : ** and as many as three operands.  The instruction is recorded
    8406                 : ** as an instance of the following structure:
    8407                 : */
    8408                 : struct VdbeOp {
    8409                 :   u8 opcode;          /* What operation to perform */
    8410                 :   signed char p4type; /* One of the P4_xxx constants for p4 */
    8411                 :   u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
    8412                 :   u8 p5;              /* Fifth parameter is an unsigned character */
    8413                 :   int p1;             /* First operand */
    8414                 :   int p2;             /* Second parameter (often the jump destination) */
    8415                 :   int p3;             /* The third parameter */
    8416                 :   union {             /* fourth parameter */
    8417                 :     int i;                 /* Integer value if p4type==P4_INT32 */
    8418                 :     void *p;               /* Generic pointer */
    8419                 :     char *z;               /* Pointer to data for string (char array) types */
    8420                 :     i64 *pI64;             /* Used when p4type is P4_INT64 */
    8421                 :     double *pReal;         /* Used when p4type is P4_REAL */
    8422                 :     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
    8423                 :     VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
    8424                 :     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
    8425                 :     Mem *pMem;             /* Used when p4type is P4_MEM */
    8426                 :     VTable *pVtab;         /* Used when p4type is P4_VTAB */
    8427                 :     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
    8428                 :     int *ai;               /* Used when p4type is P4_INTARRAY */
    8429                 :     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
    8430                 :     int (*xAdvance)(BtCursor *, int *);
    8431                 :   } p4;
    8432                 : #ifdef SQLITE_DEBUG
    8433                 :   char *zComment;          /* Comment to improve readability */
    8434                 : #endif
    8435                 : #ifdef VDBE_PROFILE
    8436                 :   int cnt;                 /* Number of times this instruction was executed */
    8437                 :   u64 cycles;              /* Total time spent executing this instruction */
    8438                 : #endif
    8439                 : };
    8440                 : typedef struct VdbeOp VdbeOp;
    8441                 : 
    8442                 : 
    8443                 : /*
    8444                 : ** A sub-routine used to implement a trigger program.
    8445                 : */
    8446                 : struct SubProgram {
    8447                 :   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
    8448                 :   int nOp;                      /* Elements in aOp[] */
    8449                 :   int nMem;                     /* Number of memory cells required */
    8450                 :   int nCsr;                     /* Number of cursors required */
    8451                 :   int nOnce;                    /* Number of OP_Once instructions */
    8452                 :   void *token;                  /* id that may be used to recursive triggers */
    8453                 :   SubProgram *pNext;            /* Next sub-program already visited */
    8454                 : };
    8455                 : 
    8456                 : /*
    8457                 : ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
    8458                 : ** it takes up less space.
    8459                 : */
    8460                 : struct VdbeOpList {
    8461                 :   u8 opcode;          /* What operation to perform */
    8462                 :   signed char p1;     /* First operand */
    8463                 :   signed char p2;     /* Second parameter (often the jump destination) */
    8464                 :   signed char p3;     /* Third parameter */
    8465                 : };
    8466                 : typedef struct VdbeOpList VdbeOpList;
    8467                 : 
    8468                 : /*
    8469                 : ** Allowed values of VdbeOp.p4type
    8470                 : */
    8471                 : #define P4_NOTUSED    0   /* The P4 parameter is not used */
    8472                 : #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
    8473                 : #define P4_STATIC   (-2)  /* Pointer to a static string */
    8474                 : #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
    8475                 : #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
    8476                 : #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
    8477                 : #define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
    8478                 : #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
    8479                 : #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
    8480                 : #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
    8481                 : #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
    8482                 : #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
    8483                 : #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
    8484                 : #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
    8485                 : #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
    8486                 : #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
    8487                 : #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
    8488                 : 
    8489                 : /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
    8490                 : ** is made.  That copy is freed when the Vdbe is finalized.  But if the
    8491                 : ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
    8492                 : ** gets freed when the Vdbe is finalized so it still should be obtained
    8493                 : ** from a single sqliteMalloc().  But no copy is made and the calling
    8494                 : ** function should *not* try to free the KeyInfo.
    8495                 : */
    8496                 : #define P4_KEYINFO_HANDOFF (-16)
    8497                 : #define P4_KEYINFO_STATIC  (-17)
    8498                 : 
    8499                 : /*
    8500                 : ** The Vdbe.aColName array contains 5n Mem structures, where n is the 
    8501                 : ** number of columns of data returned by the statement.
    8502                 : */
    8503                 : #define COLNAME_NAME     0
    8504                 : #define COLNAME_DECLTYPE 1
    8505                 : #define COLNAME_DATABASE 2
    8506                 : #define COLNAME_TABLE    3
    8507                 : #define COLNAME_COLUMN   4
    8508                 : #ifdef SQLITE_ENABLE_COLUMN_METADATA
    8509                 : # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
    8510                 : #else
    8511                 : # ifdef SQLITE_OMIT_DECLTYPE
    8512                 : #   define COLNAME_N      1      /* Store only the name */
    8513                 : # else
    8514                 : #   define COLNAME_N      2      /* Store the name and decltype */
    8515                 : # endif
    8516                 : #endif
    8517                 : 
    8518                 : /*
    8519                 : ** The following macro converts a relative address in the p2 field
    8520                 : ** of a VdbeOp structure into a negative number so that 
    8521                 : ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
    8522                 : ** the macro again restores the address.
    8523                 : */
    8524                 : #define ADDR(X)  (-1-(X))
    8525                 : 
    8526                 : /*
    8527                 : ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
    8528                 : ** header file that defines a number for each opcode used by the VDBE.
    8529                 : */
    8530                 : /************** Include opcodes.h in the middle of vdbe.h ********************/
    8531                 : /************** Begin file opcodes.h *****************************************/
    8532                 : /* Automatically generated.  Do not edit */
    8533                 : /* See the mkopcodeh.awk script for details */
    8534                 : #define OP_Goto                                 1
    8535                 : #define OP_Gosub                                2
    8536                 : #define OP_Return                               3
    8537                 : #define OP_Yield                                4
    8538                 : #define OP_HaltIfNull                           5
    8539                 : #define OP_Halt                                 6
    8540                 : #define OP_Integer                              7
    8541                 : #define OP_Int64                                8
    8542                 : #define OP_Real                               130   /* same as TK_FLOAT    */
    8543                 : #define OP_String8                             94   /* same as TK_STRING   */
    8544                 : #define OP_String                               9
    8545                 : #define OP_Null                                10
    8546                 : #define OP_Blob                                11
    8547                 : #define OP_Variable                            12
    8548                 : #define OP_Move                                13
    8549                 : #define OP_Copy                                14
    8550                 : #define OP_SCopy                               15
    8551                 : #define OP_ResultRow                           16
    8552                 : #define OP_Concat                              91   /* same as TK_CONCAT   */
    8553                 : #define OP_Add                                 86   /* same as TK_PLUS     */
    8554                 : #define OP_Subtract                            87   /* same as TK_MINUS    */
    8555                 : #define OP_Multiply                            88   /* same as TK_STAR     */
    8556                 : #define OP_Divide                              89   /* same as TK_SLASH    */
    8557                 : #define OP_Remainder                           90   /* same as TK_REM      */
    8558                 : #define OP_CollSeq                             17
    8559                 : #define OP_Function                            18
    8560                 : #define OP_BitAnd                              82   /* same as TK_BITAND   */
    8561                 : #define OP_BitOr                               83   /* same as TK_BITOR    */
    8562                 : #define OP_ShiftLeft                           84   /* same as TK_LSHIFT   */
    8563                 : #define OP_ShiftRight                          85   /* same as TK_RSHIFT   */
    8564                 : #define OP_AddImm                              20
    8565                 : #define OP_MustBeInt                           21
    8566                 : #define OP_RealAffinity                        22
    8567                 : #define OP_ToText                             141   /* same as TK_TO_TEXT  */
    8568                 : #define OP_ToBlob                             142   /* same as TK_TO_BLOB  */
    8569                 : #define OP_ToNumeric                          143   /* same as TK_TO_NUMERIC*/
    8570                 : #define OP_ToInt                              144   /* same as TK_TO_INT   */
    8571                 : #define OP_ToReal                             145   /* same as TK_TO_REAL  */
    8572                 : #define OP_Eq                                  76   /* same as TK_EQ       */
    8573                 : #define OP_Ne                                  75   /* same as TK_NE       */
    8574                 : #define OP_Lt                                  79   /* same as TK_LT       */
    8575                 : #define OP_Le                                  78   /* same as TK_LE       */
    8576                 : #define OP_Gt                                  77   /* same as TK_GT       */
    8577                 : #define OP_Ge                                  80   /* same as TK_GE       */
    8578                 : #define OP_Permutation                         23
    8579                 : #define OP_Compare                             24
    8580                 : #define OP_Jump                                25
    8581                 : #define OP_And                                 69   /* same as TK_AND      */
    8582                 : #define OP_Or                                  68   /* same as TK_OR       */
    8583                 : #define OP_Not                                 19   /* same as TK_NOT      */
    8584                 : #define OP_BitNot                              93   /* same as TK_BITNOT   */
    8585                 : #define OP_Once                                26
    8586                 : #define OP_If                                  27
    8587                 : #define OP_IfNot                               28
    8588                 : #define OP_IsNull                              73   /* same as TK_ISNULL   */
    8589                 : #define OP_NotNull                             74   /* same as TK_NOTNULL  */
    8590                 : #define OP_Column                              29
    8591                 : #define OP_Affinity                            30
    8592                 : #define OP_MakeRecord                          31
    8593                 : #define OP_Count                               32
    8594                 : #define OP_Savepoint                           33
    8595                 : #define OP_AutoCommit                          34
    8596                 : #define OP_Transaction                         35
    8597                 : #define OP_ReadCookie                          36
    8598                 : #define OP_SetCookie                           37
    8599                 : #define OP_VerifyCookie                        38
    8600                 : #define OP_OpenRead                            39
    8601                 : #define OP_OpenWrite                           40
    8602                 : #define OP_OpenAutoindex                       41
    8603                 : #define OP_OpenEphemeral                       42
    8604                 : #define OP_SorterOpen                          43
    8605                 : #define OP_OpenPseudo                          44
    8606                 : #define OP_Close                               45
    8607                 : #define OP_SeekLt                              46
    8608                 : #define OP_SeekLe                              47
    8609                 : #define OP_SeekGe                              48
    8610                 : #define OP_SeekGt                              49
    8611                 : #define OP_Seek                                50
    8612                 : #define OP_NotFound                            51
    8613                 : #define OP_Found                               52
    8614                 : #define OP_IsUnique                            53
    8615                 : #define OP_NotExists                           54
    8616                 : #define OP_Sequence                            55
    8617                 : #define OP_NewRowid                            56
    8618                 : #define OP_Insert                              57
    8619                 : #define OP_InsertInt                           58
    8620                 : #define OP_Delete                              59
    8621                 : #define OP_ResetCount                          60
    8622                 : #define OP_SorterCompare                       61
    8623                 : #define OP_SorterData                          62
    8624                 : #define OP_RowKey                              63
    8625                 : #define OP_RowData                             64
    8626                 : #define OP_Rowid                               65
    8627                 : #define OP_NullRow                             66
    8628                 : #define OP_Last                                67
    8629                 : #define OP_SorterSort                          70
    8630                 : #define OP_Sort                                71
    8631                 : #define OP_Rewind                              72
    8632                 : #define OP_SorterNext                          81
    8633                 : #define OP_Prev                                92
    8634                 : #define OP_Next                                95
    8635                 : #define OP_SorterInsert                        96
    8636                 : #define OP_IdxInsert                           97
    8637                 : #define OP_IdxDelete                           98
    8638                 : #define OP_IdxRowid                            99
    8639                 : #define OP_IdxLT                              100
    8640                 : #define OP_IdxGE                              101
    8641                 : #define OP_Destroy                            102
    8642                 : #define OP_Clear                              103
    8643                 : #define OP_CreateIndex                        104
    8644                 : #define OP_CreateTable                        105
    8645                 : #define OP_ParseSchema                        106
    8646                 : #define OP_LoadAnalysis                       107
    8647                 : #define OP_DropTable                          108
    8648                 : #define OP_DropIndex                          109
    8649                 : #define OP_DropTrigger                        110
    8650                 : #define OP_IntegrityCk                        111
    8651                 : #define OP_RowSetAdd                          112
    8652                 : #define OP_RowSetRead                         113
    8653                 : #define OP_RowSetTest                         114
    8654                 : #define OP_Program                            115
    8655                 : #define OP_Param                              116
    8656                 : #define OP_FkCounter                          117
    8657                 : #define OP_FkIfZero                           118
    8658                 : #define OP_MemMax                             119
    8659                 : #define OP_IfPos                              120
    8660                 : #define OP_IfNeg                              121
    8661                 : #define OP_IfZero                             122
    8662                 : #define OP_AggStep                            123
    8663                 : #define OP_AggFinal                           124
    8664                 : #define OP_Checkpoint                         125
    8665                 : #define OP_JournalMode                        126
    8666                 : #define OP_Vacuum                             127
    8667                 : #define OP_IncrVacuum                         128
    8668                 : #define OP_Expire                             129
    8669                 : #define OP_TableLock                          131
    8670                 : #define OP_VBegin                             132
    8671                 : #define OP_VCreate                            133
    8672                 : #define OP_VDestroy                           134
    8673                 : #define OP_VOpen                              135
    8674                 : #define OP_VFilter                            136
    8675                 : #define OP_VColumn                            137
    8676                 : #define OP_VNext                              138
    8677                 : #define OP_VRename                            139
    8678                 : #define OP_VUpdate                            140
    8679                 : #define OP_Pagecount                          146
    8680                 : #define OP_MaxPgcnt                           147
    8681                 : #define OP_Trace                              148
    8682                 : #define OP_Noop                               149
    8683                 : #define OP_Explain                            150
    8684                 : 
    8685                 : 
    8686                 : /* Properties such as "out2" or "jump" that are specified in
    8687                 : ** comments following the "case" for each opcode in the vdbe.c
    8688                 : ** are encoded into bitvectors as follows:
    8689                 : */
    8690                 : #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
    8691                 : #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
    8692                 : #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
    8693                 : #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
    8694                 : #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
    8695                 : #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
    8696                 : #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
    8697                 : #define OPFLG_INITIALIZER {\
    8698                 : /*   0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
    8699                 : /*   8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
    8700                 : /*  16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
    8701                 : /*  24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
    8702                 : /*  32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
    8703                 : /*  40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
    8704                 : /*  48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
    8705                 : /*  56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
    8706                 : /*  64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
    8707                 : /*  72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
    8708                 : /*  80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
    8709                 : /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
    8710                 : /*  96 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
    8711                 : /* 104 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
    8712                 : /* 112 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
    8713                 : /* 120 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00,\
    8714                 : /* 128 */ 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
    8715                 : /* 136 */ 0x01, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x04,\
    8716                 : /* 144 */ 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00,}
    8717                 : 
    8718                 : /************** End of opcodes.h *********************************************/
    8719                 : /************** Continuing where we left off in vdbe.h ***********************/
    8720                 : 
    8721                 : /*
    8722                 : ** Prototypes for the VDBE interface.  See comments on the implementation
    8723                 : ** for a description of what each of these routines does.
    8724                 : */
    8725                 : SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
    8726                 : SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
    8727                 : SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
    8728                 : SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
    8729                 : SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
    8730                 : SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
    8731                 : SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
    8732                 : SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
    8733                 : SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
    8734                 : SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
    8735                 : SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
    8736                 : SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
    8737                 : SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
    8738                 : SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
    8739                 : SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
    8740                 : SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
    8741                 : SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
    8742                 : SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
    8743                 : SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
    8744                 : SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
    8745                 : SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
    8746                 : SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
    8747                 : SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
    8748                 : SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
    8749                 : SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
    8750                 : SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
    8751                 : #ifdef SQLITE_DEBUG
    8752                 : SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
    8753                 : SQLITE_PRIVATE   void sqlite3VdbeTrace(Vdbe*,FILE*);
    8754                 : #endif
    8755                 : SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
    8756                 : SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
    8757                 : SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
    8758                 : SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
    8759                 : SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
    8760                 : SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
    8761                 : SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
    8762                 : SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
    8763                 : SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
    8764                 : SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
    8765                 : SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
    8766                 : SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
    8767                 : #ifndef SQLITE_OMIT_TRACE
    8768                 : SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
    8769                 : #endif
    8770                 : 
    8771                 : SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
    8772                 : SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
    8773                 : SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
    8774                 : 
    8775                 : #ifndef SQLITE_OMIT_TRIGGER
    8776                 : SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
    8777                 : #endif
    8778                 : 
    8779                 : 
    8780                 : #ifndef NDEBUG
    8781                 : SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
    8782                 : # define VdbeComment(X)  sqlite3VdbeComment X
    8783                 : SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
    8784                 : # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
    8785                 : #else
    8786                 : # define VdbeComment(X)
    8787                 : # define VdbeNoopComment(X)
    8788                 : #endif
    8789                 : 
    8790                 : #endif
    8791                 : 
    8792                 : /************** End of vdbe.h ************************************************/
    8793                 : /************** Continuing where we left off in sqliteInt.h ******************/
    8794                 : /************** Include pager.h in the middle of sqliteInt.h *****************/
    8795                 : /************** Begin file pager.h *******************************************/
    8796                 : /*
    8797                 : ** 2001 September 15
    8798                 : **
    8799                 : ** The author disclaims copyright to this source code.  In place of
    8800                 : ** a legal notice, here is a blessing:
    8801                 : **
    8802                 : **    May you do good and not evil.
    8803                 : **    May you find forgiveness for yourself and forgive others.
    8804                 : **    May you share freely, never taking more than you give.
    8805                 : **
    8806                 : *************************************************************************
    8807                 : ** This header file defines the interface that the sqlite page cache
    8808                 : ** subsystem.  The page cache subsystem reads and writes a file a page
    8809                 : ** at a time and provides a journal for rollback.
    8810                 : */
    8811                 : 
    8812                 : #ifndef _PAGER_H_
    8813                 : #define _PAGER_H_
    8814                 : 
    8815                 : /*
    8816                 : ** Default maximum size for persistent journal files. A negative 
    8817                 : ** value means no limit. This value may be overridden using the 
    8818                 : ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
    8819                 : */
    8820                 : #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
    8821                 :   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
    8822                 : #endif
    8823                 : 
    8824                 : /*
    8825                 : ** The type used to represent a page number.  The first page in a file
    8826                 : ** is called page 1.  0 is used to represent "not a page".
    8827                 : */
    8828                 : typedef u32 Pgno;
    8829                 : 
    8830                 : /*
    8831                 : ** Each open file is managed by a separate instance of the "Pager" structure.
    8832                 : */
    8833                 : typedef struct Pager Pager;
    8834                 : 
    8835                 : /*
    8836                 : ** Handle type for pages.
    8837                 : */
    8838                 : typedef struct PgHdr DbPage;
    8839                 : 
    8840                 : /*
    8841                 : ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
    8842                 : ** reserved for working around a windows/posix incompatibility). It is
    8843                 : ** used in the journal to signify that the remainder of the journal file 
    8844                 : ** is devoted to storing a master journal name - there are no more pages to
    8845                 : ** roll back. See comments for function writeMasterJournal() in pager.c 
    8846                 : ** for details.
    8847                 : */
    8848                 : #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
    8849                 : 
    8850                 : /*
    8851                 : ** Allowed values for the flags parameter to sqlite3PagerOpen().
    8852                 : **
    8853                 : ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
    8854                 : */
    8855                 : #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
    8856                 : #define PAGER_NO_READLOCK   0x0002    /* Omit readlocks on readonly files */
    8857                 : #define PAGER_MEMORY        0x0004    /* In-memory database */
    8858                 : 
    8859                 : /*
    8860                 : ** Valid values for the second argument to sqlite3PagerLockingMode().
    8861                 : */
    8862                 : #define PAGER_LOCKINGMODE_QUERY      -1
    8863                 : #define PAGER_LOCKINGMODE_NORMAL      0
    8864                 : #define PAGER_LOCKINGMODE_EXCLUSIVE   1
    8865                 : 
    8866                 : /*
    8867                 : ** Numeric constants that encode the journalmode.  
    8868                 : */
    8869                 : #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
    8870                 : #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
    8871                 : #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
    8872                 : #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
    8873                 : #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
    8874                 : #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
    8875                 : #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
    8876                 : 
    8877                 : /*
    8878                 : ** The remainder of this file contains the declarations of the functions
    8879                 : ** that make up the Pager sub-system API. See source code comments for 
    8880                 : ** a detailed description of each routine.
    8881                 : */
    8882                 : 
    8883                 : /* Open and close a Pager connection. */ 
    8884                 : SQLITE_PRIVATE int sqlite3PagerOpen(
    8885                 :   sqlite3_vfs*,
    8886                 :   Pager **ppPager,
    8887                 :   const char*,
    8888                 :   int,
    8889                 :   int,
    8890                 :   int,
    8891                 :   void(*)(DbPage*)
    8892                 : );
    8893                 : SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
    8894                 : SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
    8895                 : 
    8896                 : /* Functions used to configure a Pager object. */
    8897                 : SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
    8898                 : SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
    8899                 : SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
    8900                 : SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
    8901                 : SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
    8902                 : SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
    8903                 : SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
    8904                 : SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
    8905                 : SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
    8906                 : SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
    8907                 : SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
    8908                 : SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
    8909                 : 
    8910                 : /* Functions used to obtain and release page references. */ 
    8911                 : SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
    8912                 : #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
    8913                 : SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
    8914                 : SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
    8915                 : SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
    8916                 : 
    8917                 : /* Operations on page references. */
    8918                 : SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
    8919                 : SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
    8920                 : SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
    8921                 : SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
    8922                 : SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); 
    8923                 : SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); 
    8924                 : 
    8925                 : /* Functions used to manage pager transactions and savepoints. */
    8926                 : SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
    8927                 : SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
    8928                 : SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
    8929                 : SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
    8930                 : SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
    8931                 : SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
    8932                 : SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
    8933                 : SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
    8934                 : SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
    8935                 : SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
    8936                 : 
    8937                 : SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
    8938                 : SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
    8939                 : SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
    8940                 : SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
    8941                 : SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
    8942                 : 
    8943                 : /* Functions used to query pager state and configuration. */
    8944                 : SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
    8945                 : SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
    8946                 : SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
    8947                 : SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
    8948                 : SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
    8949                 : SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
    8950                 : SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
    8951                 : SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
    8952                 : SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
    8953                 : SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
    8954                 : SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
    8955                 : SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
    8956                 : 
    8957                 : /* Functions used to truncate the database file. */
    8958                 : SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
    8959                 : 
    8960                 : #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
    8961                 : SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
    8962                 : #endif
    8963                 : 
    8964                 : /* Functions to support testing and debugging. */
    8965                 : #if !defined(NDEBUG) || defined(SQLITE_TEST)
    8966                 : SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
    8967                 : SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
    8968                 : #endif
    8969                 : #ifdef SQLITE_TEST
    8970                 : SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
    8971                 : SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
    8972                 :   void disable_simulated_io_errors(void);
    8973                 :   void enable_simulated_io_errors(void);
    8974                 : #else
    8975                 : # define disable_simulated_io_errors()
    8976                 : # define enable_simulated_io_errors()
    8977                 : #endif
    8978                 : 
    8979                 : #endif /* _PAGER_H_ */
    8980                 : 
    8981                 : /************** End of pager.h ***********************************************/
    8982                 : /************** Continuing where we left off in sqliteInt.h ******************/
    8983                 : /************** Include pcache.h in the middle of sqliteInt.h ****************/
    8984                 : /************** Begin file pcache.h ******************************************/
    8985                 : /*
    8986                 : ** 2008 August 05
    8987                 : **
    8988                 : ** The author disclaims copyright to this source code.  In place of
    8989                 : ** a legal notice, here is a blessing:
    8990                 : **
    8991                 : **    May you do good and not evil.
    8992                 : **    May you find forgiveness for yourself and forgive others.
    8993                 : **    May you share freely, never taking more than you give.
    8994                 : **
    8995                 : *************************************************************************
    8996                 : ** This header file defines the interface that the sqlite page cache
    8997                 : ** subsystem. 
    8998                 : */
    8999                 : 
    9000                 : #ifndef _PCACHE_H_
    9001                 : 
    9002                 : typedef struct PgHdr PgHdr;
    9003                 : typedef struct PCache PCache;
    9004                 : 
    9005                 : /*
    9006                 : ** Every page in the cache is controlled by an instance of the following
    9007                 : ** structure.
    9008                 : */
    9009                 : struct PgHdr {
    9010                 :   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
    9011                 :   void *pData;                   /* Page data */
    9012                 :   void *pExtra;                  /* Extra content */
    9013                 :   PgHdr *pDirty;                 /* Transient list of dirty pages */
    9014                 :   Pgno pgno;                     /* Page number for this page */
    9015                 :   Pager *pPager;                 /* The pager this page is part of */
    9016                 : #ifdef SQLITE_CHECK_PAGES
    9017                 :   u32 pageHash;                  /* Hash of page content */
    9018                 : #endif
    9019                 :   u16 flags;                     /* PGHDR flags defined below */
    9020                 : 
    9021                 :   /**********************************************************************
    9022                 :   ** Elements above are public.  All that follows is private to pcache.c
    9023                 :   ** and should not be accessed by other modules.
    9024                 :   */
    9025                 :   i16 nRef;                      /* Number of users of this page */
    9026                 :   PCache *pCache;                /* Cache that owns this page */
    9027                 : 
    9028                 :   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
    9029                 :   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
    9030                 : };
    9031                 : 
    9032                 : /* Bit values for PgHdr.flags */
    9033                 : #define PGHDR_DIRTY             0x002  /* Page has changed */
    9034                 : #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
    9035                 :                                        ** writing this page to the database */
    9036                 : #define PGHDR_NEED_READ         0x008  /* Content is unread */
    9037                 : #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
    9038                 : #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
    9039                 : 
    9040                 : /* Initialize and shutdown the page cache subsystem */
    9041                 : SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
    9042                 : SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
    9043                 : 
    9044                 : /* Page cache buffer management:
    9045                 : ** These routines implement SQLITE_CONFIG_PAGECACHE.
    9046                 : */
    9047                 : SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
    9048                 : 
    9049                 : /* Create a new pager cache.
    9050                 : ** Under memory stress, invoke xStress to try to make pages clean.
    9051                 : ** Only clean and unpinned pages can be reclaimed.
    9052                 : */
    9053                 : SQLITE_PRIVATE void sqlite3PcacheOpen(
    9054                 :   int szPage,                    /* Size of every page */
    9055                 :   int szExtra,                   /* Extra space associated with each page */
    9056                 :   int bPurgeable,                /* True if pages are on backing store */
    9057                 :   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
    9058                 :   void *pStress,                 /* Argument to xStress */
    9059                 :   PCache *pToInit                /* Preallocated space for the PCache */
    9060                 : );
    9061                 : 
    9062                 : /* Modify the page-size after the cache has been created. */
    9063                 : SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
    9064                 : 
    9065                 : /* Return the size in bytes of a PCache object.  Used to preallocate
    9066                 : ** storage space.
    9067                 : */
    9068                 : SQLITE_PRIVATE int sqlite3PcacheSize(void);
    9069                 : 
    9070                 : /* One release per successful fetch.  Page is pinned until released.
    9071                 : ** Reference counted. 
    9072                 : */
    9073                 : SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
    9074                 : SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
    9075                 : 
    9076                 : SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
    9077                 : SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
    9078                 : SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
    9079                 : SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
    9080                 : 
    9081                 : /* Change a page number.  Used by incr-vacuum. */
    9082                 : SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
    9083                 : 
    9084                 : /* Remove all pages with pgno>x.  Reset the cache if x==0 */
    9085                 : SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
    9086                 : 
    9087                 : /* Get a list of all dirty pages in the cache, sorted by page number */
    9088                 : SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
    9089                 : 
    9090                 : /* Reset and close the cache object */
    9091                 : SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
    9092                 : 
    9093                 : /* Clear flags from pages of the page cache */
    9094                 : SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
    9095                 : 
    9096                 : /* Discard the contents of the cache */
    9097                 : SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
    9098                 : 
    9099                 : /* Return the total number of outstanding page references */
    9100                 : SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
    9101                 : 
    9102                 : /* Increment the reference count of an existing page */
    9103                 : SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
    9104                 : 
    9105                 : SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
    9106                 : 
    9107                 : /* Return the total number of pages stored in the cache */
    9108                 : SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
    9109                 : 
    9110                 : #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
    9111                 : /* Iterate through all dirty pages currently stored in the cache. This
    9112                 : ** interface is only available if SQLITE_CHECK_PAGES is defined when the 
    9113                 : ** library is built.
    9114                 : */
    9115                 : SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
    9116                 : #endif
    9117                 : 
    9118                 : /* Set and get the suggested cache-size for the specified pager-cache.
    9119                 : **
    9120                 : ** If no global maximum is configured, then the system attempts to limit
    9121                 : ** the total number of pages cached by purgeable pager-caches to the sum
    9122                 : ** of the suggested cache-sizes.
    9123                 : */
    9124                 : SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
    9125                 : #ifdef SQLITE_TEST
    9126                 : SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
    9127                 : #endif
    9128                 : 
    9129                 : /* Free up as much memory as possible from the page cache */
    9130                 : SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
    9131                 : 
    9132                 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
    9133                 : /* Try to return memory used by the pcache module to the main memory heap */
    9134                 : SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
    9135                 : #endif
    9136                 : 
    9137                 : #ifdef SQLITE_TEST
    9138                 : SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
    9139                 : #endif
    9140                 : 
    9141                 : SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
    9142                 : 
    9143                 : #endif /* _PCACHE_H_ */
    9144                 : 
    9145                 : /************** End of pcache.h **********************************************/
    9146                 : /************** Continuing where we left off in sqliteInt.h ******************/
    9147                 : 
    9148                 : /************** Include os.h in the middle of sqliteInt.h ********************/
    9149                 : /************** Begin file os.h **********************************************/
    9150                 : /*
    9151                 : ** 2001 September 16
    9152                 : **
    9153                 : ** The author disclaims copyright to this source code.  In place of
    9154                 : ** a legal notice, here is a blessing:
    9155                 : **
    9156                 : **    May you do good and not evil.
    9157                 : **    May you find forgiveness for yourself and forgive others.
    9158                 : **    May you share freely, never taking more than you give.
    9159                 : **
    9160                 : ******************************************************************************
    9161                 : **
    9162                 : ** This header file (together with is companion C source-code file
    9163                 : ** "os.c") attempt to abstract the underlying operating system so that
    9164                 : ** the SQLite library will work on both POSIX and windows systems.
    9165                 : **
    9166                 : ** This header file is #include-ed by sqliteInt.h and thus ends up
    9167                 : ** being included by every source file.
    9168                 : */
    9169                 : #ifndef _SQLITE_OS_H_
    9170                 : #define _SQLITE_OS_H_
    9171                 : 
    9172                 : /*
    9173                 : ** Figure out if we are dealing with Unix, Windows, or some other
    9174                 : ** operating system.  After the following block of preprocess macros,
    9175                 : ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 
    9176                 : ** will defined to either 1 or 0.  One of the four will be 1.  The other 
    9177                 : ** three will be 0.
    9178                 : */
    9179                 : #if defined(SQLITE_OS_OTHER)
    9180                 : # if SQLITE_OS_OTHER==1
    9181                 : #   undef SQLITE_OS_UNIX
    9182                 : #   define SQLITE_OS_UNIX 0
    9183                 : #   undef SQLITE_OS_WIN
    9184                 : #   define SQLITE_OS_WIN 0
    9185                 : #   undef SQLITE_OS_OS2
    9186                 : #   define SQLITE_OS_OS2 0
    9187                 : # else
    9188                 : #   undef SQLITE_OS_OTHER
    9189                 : # endif
    9190                 : #endif
    9191                 : #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
    9192                 : # define SQLITE_OS_OTHER 0
    9193                 : # ifndef SQLITE_OS_WIN
    9194                 : #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
    9195                 : #     define SQLITE_OS_WIN 1
    9196                 : #     define SQLITE_OS_UNIX 0
    9197                 : #     define SQLITE_OS_OS2 0
    9198                 : #   elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
    9199                 : #     define SQLITE_OS_WIN 0
    9200                 : #     define SQLITE_OS_UNIX 0
    9201                 : #     define SQLITE_OS_OS2 1
    9202                 : #   else
    9203                 : #     define SQLITE_OS_WIN 0
    9204                 : #     define SQLITE_OS_UNIX 1
    9205                 : #     define SQLITE_OS_OS2 0
    9206                 : #  endif
    9207                 : # else
    9208                 : #  define SQLITE_OS_UNIX 0
    9209                 : #  define SQLITE_OS_OS2 0
    9210                 : # endif
    9211                 : #else
    9212                 : # ifndef SQLITE_OS_WIN
    9213                 : #  define SQLITE_OS_WIN 0
    9214                 : # endif
    9215                 : #endif
    9216                 : 
    9217                 : /*
    9218                 : ** Define the maximum size of a temporary filename
    9219                 : */
    9220                 : #if SQLITE_OS_WIN
    9221                 : # include <windows.h>
    9222                 : # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
    9223                 : #elif SQLITE_OS_OS2
    9224                 : # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
    9225                 : #  include <os2safe.h> /* has to be included before os2.h for linking to work */
    9226                 : # endif
    9227                 : # define INCL_DOSDATETIME
    9228                 : # define INCL_DOSFILEMGR
    9229                 : # define INCL_DOSERRORS
    9230                 : # define INCL_DOSMISC
    9231                 : # define INCL_DOSPROCESS
    9232                 : # define INCL_DOSMODULEMGR
    9233                 : # define INCL_DOSSEMAPHORES
    9234                 : # include <os2.h>
    9235                 : # include <uconv.h>
    9236                 : # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
    9237                 : #else
    9238                 : # define SQLITE_TEMPNAME_SIZE 200
    9239                 : #endif
    9240                 : 
    9241                 : /*
    9242                 : ** Determine if we are dealing with Windows NT.
    9243                 : */
    9244                 : #if defined(_WIN32_WINNT)
    9245                 : # define SQLITE_OS_WINNT 1
    9246                 : #else
    9247                 : # define SQLITE_OS_WINNT 0
    9248                 : #endif
    9249                 : 
    9250                 : /*
    9251                 : ** Determine if we are dealing with WindowsCE - which has a much
    9252                 : ** reduced API.
    9253                 : */
    9254                 : #if defined(_WIN32_WCE)
    9255                 : # define SQLITE_OS_WINCE 1
    9256                 : #else
    9257                 : # define SQLITE_OS_WINCE 0
    9258                 : #endif
    9259                 : 
    9260                 : /* If the SET_FULLSYNC macro is not defined above, then make it
    9261                 : ** a no-op
    9262                 : */
    9263                 : #ifndef SET_FULLSYNC
    9264                 : # define SET_FULLSYNC(x,y)
    9265                 : #endif
    9266                 : 
    9267                 : /*
    9268                 : ** The default size of a disk sector
    9269                 : */
    9270                 : #ifndef SQLITE_DEFAULT_SECTOR_SIZE
    9271                 : # define SQLITE_DEFAULT_SECTOR_SIZE 4096
    9272                 : #endif
    9273                 : 
    9274                 : /*
    9275                 : ** Temporary files are named starting with this prefix followed by 16 random
    9276                 : ** alphanumeric characters, and no file extension. They are stored in the
    9277                 : ** OS's standard temporary file directory, and are deleted prior to exit.
    9278                 : ** If sqlite is being embedded in another program, you may wish to change the
    9279                 : ** prefix to reflect your program's name, so that if your program exits
    9280                 : ** prematurely, old temporary files can be easily identified. This can be done
    9281                 : ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
    9282                 : **
    9283                 : ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
    9284                 : ** Mcafee started using SQLite in their anti-virus product and it
    9285                 : ** started putting files with the "sqlite" name in the c:/temp folder.
    9286                 : ** This annoyed many windows users.  Those users would then do a 
    9287                 : ** Google search for "sqlite", find the telephone numbers of the
    9288                 : ** developers and call to wake them up at night and complain.
    9289                 : ** For this reason, the default name prefix is changed to be "sqlite" 
    9290                 : ** spelled backwards.  So the temp files are still identified, but
    9291                 : ** anybody smart enough to figure out the code is also likely smart
    9292                 : ** enough to know that calling the developer will not help get rid
    9293                 : ** of the file.
    9294                 : */
    9295                 : #ifndef SQLITE_TEMP_FILE_PREFIX
    9296                 : # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
    9297                 : #endif
    9298                 : 
    9299                 : /*
    9300                 : ** The following values may be passed as the second argument to
    9301                 : ** sqlite3OsLock(). The various locks exhibit the following semantics:
    9302                 : **
    9303                 : ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
    9304                 : ** RESERVED:  A single process may hold a RESERVED lock on a file at
    9305                 : **            any time. Other processes may hold and obtain new SHARED locks.
    9306                 : ** PENDING:   A single process may hold a PENDING lock on a file at
    9307                 : **            any one time. Existing SHARED locks may persist, but no new
    9308                 : **            SHARED locks may be obtained by other processes.
    9309                 : ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
    9310                 : **
    9311                 : ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
    9312                 : ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
    9313                 : ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
    9314                 : ** sqlite3OsLock().
    9315                 : */
    9316                 : #define NO_LOCK         0
    9317                 : #define SHARED_LOCK     1
    9318                 : #define RESERVED_LOCK   2
    9319                 : #define PENDING_LOCK    3
    9320                 : #define EXCLUSIVE_LOCK  4
    9321                 : 
    9322                 : /*
    9323                 : ** File Locking Notes:  (Mostly about windows but also some info for Unix)
    9324                 : **
    9325                 : ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
    9326                 : ** those functions are not available.  So we use only LockFile() and
    9327                 : ** UnlockFile().
    9328                 : **
    9329                 : ** LockFile() prevents not just writing but also reading by other processes.
    9330                 : ** A SHARED_LOCK is obtained by locking a single randomly-chosen 
    9331                 : ** byte out of a specific range of bytes. The lock byte is obtained at 
    9332                 : ** random so two separate readers can probably access the file at the 
    9333                 : ** same time, unless they are unlucky and choose the same lock byte.
    9334                 : ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
    9335                 : ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
    9336                 : ** a single byte of the file that is designated as the reserved lock byte.
    9337                 : ** A PENDING_LOCK is obtained by locking a designated byte different from
    9338                 : ** the RESERVED_LOCK byte.
    9339                 : **
    9340                 : ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
    9341                 : ** which means we can use reader/writer locks.  When reader/writer locks
    9342                 : ** are used, the lock is placed on the same range of bytes that is used
    9343                 : ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
    9344                 : ** will support two or more Win95 readers or two or more WinNT readers.
    9345                 : ** But a single Win95 reader will lock out all WinNT readers and a single
    9346                 : ** WinNT reader will lock out all other Win95 readers.
    9347                 : **
    9348                 : ** The following #defines specify the range of bytes used for locking.
    9349                 : ** SHARED_SIZE is the number of bytes available in the pool from which
    9350                 : ** a random byte is selected for a shared lock.  The pool of bytes for
    9351                 : ** shared locks begins at SHARED_FIRST. 
    9352                 : **
    9353                 : ** The same locking strategy and
    9354                 : ** byte ranges are used for Unix.  This leaves open the possiblity of having
    9355                 : ** clients on win95, winNT, and unix all talking to the same shared file
    9356                 : ** and all locking correctly.  To do so would require that samba (or whatever
    9357                 : ** tool is being used for file sharing) implements locks correctly between
    9358                 : ** windows and unix.  I'm guessing that isn't likely to happen, but by
    9359                 : ** using the same locking range we are at least open to the possibility.
    9360                 : **
    9361                 : ** Locking in windows is manditory.  For this reason, we cannot store
    9362                 : ** actual data in the bytes used for locking.  The pager never allocates
    9363                 : ** the pages involved in locking therefore.  SHARED_SIZE is selected so
    9364                 : ** that all locks will fit on a single page even at the minimum page size.
    9365                 : ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
    9366                 : ** is set high so that we don't have to allocate an unused page except
    9367                 : ** for very large databases.  But one should test the page skipping logic 
    9368                 : ** by setting PENDING_BYTE low and running the entire regression suite.
    9369                 : **
    9370                 : ** Changing the value of PENDING_BYTE results in a subtly incompatible
    9371                 : ** file format.  Depending on how it is changed, you might not notice
    9372                 : ** the incompatibility right away, even running a full regression test.
    9373                 : ** The default location of PENDING_BYTE is the first byte past the
    9374                 : ** 1GB boundary.
    9375                 : **
    9376                 : */
    9377                 : #ifdef SQLITE_OMIT_WSD
    9378                 : # define PENDING_BYTE     (0x40000000)
    9379                 : #else
    9380                 : # define PENDING_BYTE      sqlite3PendingByte
    9381                 : #endif
    9382                 : #define RESERVED_BYTE     (PENDING_BYTE+1)
    9383                 : #define SHARED_FIRST      (PENDING_BYTE+2)
    9384                 : #define SHARED_SIZE       510
    9385                 : 
    9386                 : /*
    9387                 : ** Wrapper around OS specific sqlite3_os_init() function.
    9388                 : */
    9389                 : SQLITE_PRIVATE int sqlite3OsInit(void);
    9390                 : 
    9391                 : /* 
    9392                 : ** Functions for accessing sqlite3_file methods 
    9393                 : */
    9394                 : SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
    9395                 : SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
    9396                 : SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
    9397                 : SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
    9398                 : SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
    9399                 : SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
    9400                 : SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
    9401                 : SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
    9402                 : SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
    9403                 : SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
    9404                 : SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
    9405                 : #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
    9406                 : SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
    9407                 : SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
    9408                 : SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
    9409                 : SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
    9410                 : SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
    9411                 : SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
    9412                 : 
    9413                 : 
    9414                 : /* 
    9415                 : ** Functions for accessing sqlite3_vfs methods 
    9416                 : */
    9417                 : SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
    9418                 : SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
    9419                 : SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
    9420                 : SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
    9421                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
    9422                 : SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
    9423                 : SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
    9424                 : SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
    9425                 : SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
    9426                 : #endif /* SQLITE_OMIT_LOAD_EXTENSION */
    9427                 : SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
    9428                 : SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
    9429                 : SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
    9430                 : 
    9431                 : /*
    9432                 : ** Convenience functions for opening and closing files using 
    9433                 : ** sqlite3_malloc() to obtain space for the file-handle structure.
    9434                 : */
    9435                 : SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
    9436                 : SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
    9437                 : 
    9438                 : #endif /* _SQLITE_OS_H_ */
    9439                 : 
    9440                 : /************** End of os.h **************************************************/
    9441                 : /************** Continuing where we left off in sqliteInt.h ******************/
    9442                 : /************** Include mutex.h in the middle of sqliteInt.h *****************/
    9443                 : /************** Begin file mutex.h *******************************************/
    9444                 : /*
    9445                 : ** 2007 August 28
    9446                 : **
    9447                 : ** The author disclaims copyright to this source code.  In place of
    9448                 : ** a legal notice, here is a blessing:
    9449                 : **
    9450                 : **    May you do good and not evil.
    9451                 : **    May you find forgiveness for yourself and forgive others.
    9452                 : **    May you share freely, never taking more than you give.
    9453                 : **
    9454                 : *************************************************************************
    9455                 : **
    9456                 : ** This file contains the common header for all mutex implementations.
    9457                 : ** The sqliteInt.h header #includes this file so that it is available
    9458                 : ** to all source files.  We break it out in an effort to keep the code
    9459                 : ** better organized.
    9460                 : **
    9461                 : ** NOTE:  source files should *not* #include this header file directly.
    9462                 : ** Source files should #include the sqliteInt.h file and let that file
    9463                 : ** include this one indirectly.
    9464                 : */
    9465                 : 
    9466                 : 
    9467                 : /*
    9468                 : ** Figure out what version of the code to use.  The choices are
    9469                 : **
    9470                 : **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
    9471                 : **                             mutexes implemention cannot be overridden
    9472                 : **                             at start-time.
    9473                 : **
    9474                 : **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
    9475                 : **                             mutual exclusion is provided.  But this
    9476                 : **                             implementation can be overridden at
    9477                 : **                             start-time.
    9478                 : **
    9479                 : **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
    9480                 : **
    9481                 : **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
    9482                 : **
    9483                 : **   SQLITE_MUTEX_OS2          For multi-threaded applications on OS/2.
    9484                 : */
    9485                 : #if !SQLITE_THREADSAFE
    9486                 : # define SQLITE_MUTEX_OMIT
    9487                 : #endif
    9488                 : #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
    9489                 : #  if SQLITE_OS_UNIX
    9490                 : #    define SQLITE_MUTEX_PTHREADS
    9491                 : #  elif SQLITE_OS_WIN
    9492                 : #    define SQLITE_MUTEX_W32
    9493                 : #  elif SQLITE_OS_OS2
    9494                 : #    define SQLITE_MUTEX_OS2
    9495                 : #  else
    9496                 : #    define SQLITE_MUTEX_NOOP
    9497                 : #  endif
    9498                 : #endif
    9499                 : 
    9500                 : #ifdef SQLITE_MUTEX_OMIT
    9501                 : /*
    9502                 : ** If this is a no-op implementation, implement everything as macros.
    9503                 : */
    9504                 : #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
    9505                 : #define sqlite3_mutex_free(X)
    9506                 : #define sqlite3_mutex_enter(X)    
    9507                 : #define sqlite3_mutex_try(X)      SQLITE_OK
    9508                 : #define sqlite3_mutex_leave(X)    
    9509                 : #define sqlite3_mutex_held(X)     ((void)(X),1)
    9510                 : #define sqlite3_mutex_notheld(X)  ((void)(X),1)
    9511                 : #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
    9512                 : #define sqlite3MutexInit()        SQLITE_OK
    9513                 : #define sqlite3MutexEnd()
    9514                 : #define MUTEX_LOGIC(X)
    9515                 : #else
    9516                 : #define MUTEX_LOGIC(X)            X
    9517                 : #endif /* defined(SQLITE_MUTEX_OMIT) */
    9518                 : 
    9519                 : /************** End of mutex.h ***********************************************/
    9520                 : /************** Continuing where we left off in sqliteInt.h ******************/
    9521                 : 
    9522                 : 
    9523                 : /*
    9524                 : ** Each database file to be accessed by the system is an instance
    9525                 : ** of the following structure.  There are normally two of these structures
    9526                 : ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
    9527                 : ** aDb[1] is the database file used to hold temporary tables.  Additional
    9528                 : ** databases may be attached.
    9529                 : */
    9530                 : struct Db {
    9531                 :   char *zName;         /* Name of this database */
    9532                 :   Btree *pBt;          /* The B*Tree structure for this database file */
    9533                 :   u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
    9534                 :   u8 safety_level;     /* How aggressive at syncing data to disk */
    9535                 :   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
    9536                 : };
    9537                 : 
    9538                 : /*
    9539                 : ** An instance of the following structure stores a database schema.
    9540                 : **
    9541                 : ** Most Schema objects are associated with a Btree.  The exception is
    9542                 : ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
    9543                 : ** In shared cache mode, a single Schema object can be shared by multiple
    9544                 : ** Btrees that refer to the same underlying BtShared object.
    9545                 : ** 
    9546                 : ** Schema objects are automatically deallocated when the last Btree that
    9547                 : ** references them is destroyed.   The TEMP Schema is manually freed by
    9548                 : ** sqlite3_close().
    9549                 : *
    9550                 : ** A thread must be holding a mutex on the corresponding Btree in order
    9551                 : ** to access Schema content.  This implies that the thread must also be
    9552                 : ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
    9553                 : ** For a TEMP Schema, only the connection mutex is required.
    9554                 : */
    9555                 : struct Schema {
    9556                 :   int schema_cookie;   /* Database schema version number for this file */
    9557                 :   int iGeneration;     /* Generation counter.  Incremented with each change */
    9558                 :   Hash tblHash;        /* All tables indexed by name */
    9559                 :   Hash idxHash;        /* All (named) indices indexed by name */
    9560                 :   Hash trigHash;       /* All triggers indexed by name */
    9561                 :   Hash fkeyHash;       /* All foreign keys by referenced table name */
    9562                 :   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
    9563                 :   u8 file_format;      /* Schema format version for this file */
    9564                 :   u8 enc;              /* Text encoding used by this database */
    9565                 :   u16 flags;           /* Flags associated with this schema */
    9566                 :   int cache_size;      /* Number of pages to use in the cache */
    9567                 : };
    9568                 : 
    9569                 : /*
    9570                 : ** These macros can be used to test, set, or clear bits in the 
    9571                 : ** Db.pSchema->flags field.
    9572                 : */
    9573                 : #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
    9574                 : #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
    9575                 : #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
    9576                 : #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
    9577                 : 
    9578                 : /*
    9579                 : ** Allowed values for the DB.pSchema->flags field.
    9580                 : **
    9581                 : ** The DB_SchemaLoaded flag is set after the database schema has been
    9582                 : ** read into internal hash tables.
    9583                 : **
    9584                 : ** DB_UnresetViews means that one or more views have column names that
    9585                 : ** have been filled out.  If the schema changes, these column names might
    9586                 : ** changes and so the view will need to be reset.
    9587                 : */
    9588                 : #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
    9589                 : #define DB_UnresetViews    0x0002  /* Some views have defined column names */
    9590                 : #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
    9591                 : 
    9592                 : /*
    9593                 : ** The number of different kinds of things that can be limited
    9594                 : ** using the sqlite3_limit() interface.
    9595                 : */
    9596                 : #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
    9597                 : 
    9598                 : /*
    9599                 : ** Lookaside malloc is a set of fixed-size buffers that can be used
    9600                 : ** to satisfy small transient memory allocation requests for objects
    9601                 : ** associated with a particular database connection.  The use of
    9602                 : ** lookaside malloc provides a significant performance enhancement
    9603                 : ** (approx 10%) by avoiding numerous malloc/free requests while parsing
    9604                 : ** SQL statements.
    9605                 : **
    9606                 : ** The Lookaside structure holds configuration information about the
    9607                 : ** lookaside malloc subsystem.  Each available memory allocation in
    9608                 : ** the lookaside subsystem is stored on a linked list of LookasideSlot
    9609                 : ** objects.
    9610                 : **
    9611                 : ** Lookaside allocations are only allowed for objects that are associated
    9612                 : ** with a particular database connection.  Hence, schema information cannot
    9613                 : ** be stored in lookaside because in shared cache mode the schema information
    9614                 : ** is shared by multiple database connections.  Therefore, while parsing
    9615                 : ** schema information, the Lookaside.bEnabled flag is cleared so that
    9616                 : ** lookaside allocations are not used to construct the schema objects.
    9617                 : */
    9618                 : struct Lookaside {
    9619                 :   u16 sz;                 /* Size of each buffer in bytes */
    9620                 :   u8 bEnabled;            /* False to disable new lookaside allocations */
    9621                 :   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
    9622                 :   int nOut;               /* Number of buffers currently checked out */
    9623                 :   int mxOut;              /* Highwater mark for nOut */
    9624                 :   int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
    9625                 :   LookasideSlot *pFree;   /* List of available buffers */
    9626                 :   void *pStart;           /* First byte of available memory space */
    9627                 :   void *pEnd;             /* First byte past end of available space */
    9628                 : };
    9629                 : struct LookasideSlot {
    9630                 :   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
    9631                 : };
    9632                 : 
    9633                 : /*
    9634                 : ** A hash table for function definitions.
    9635                 : **
    9636                 : ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
    9637                 : ** Collisions are on the FuncDef.pHash chain.
    9638                 : */
    9639                 : struct FuncDefHash {
    9640                 :   FuncDef *a[23];       /* Hash table for functions */
    9641                 : };
    9642                 : 
    9643                 : /*
    9644                 : ** Each database connection is an instance of the following structure.
    9645                 : **
    9646                 : ** The sqlite.lastRowid records the last insert rowid generated by an
    9647                 : ** insert statement.  Inserts on views do not affect its value.  Each
    9648                 : ** trigger has its own context, so that lastRowid can be updated inside
    9649                 : ** triggers as usual.  The previous value will be restored once the trigger
    9650                 : ** exits.  Upon entering a before or instead of trigger, lastRowid is no
    9651                 : ** longer (since after version 2.8.12) reset to -1.
    9652                 : **
    9653                 : ** The sqlite.nChange does not count changes within triggers and keeps no
    9654                 : ** context.  It is reset at start of sqlite3_exec.
    9655                 : ** The sqlite.lsChange represents the number of changes made by the last
    9656                 : ** insert, update, or delete statement.  It remains constant throughout the
    9657                 : ** length of a statement and is then updated by OP_SetCounts.  It keeps a
    9658                 : ** context stack just like lastRowid so that the count of changes
    9659                 : ** within a trigger is not seen outside the trigger.  Changes to views do not
    9660                 : ** affect the value of lsChange.
    9661                 : ** The sqlite.csChange keeps track of the number of current changes (since
    9662                 : ** the last statement) and is used to update sqlite_lsChange.
    9663                 : **
    9664                 : ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
    9665                 : ** store the most recent error code and, if applicable, string. The
    9666                 : ** internal function sqlite3Error() is used to set these variables
    9667                 : ** consistently.
    9668                 : */
    9669                 : struct sqlite3 {
    9670                 :   sqlite3_vfs *pVfs;            /* OS Interface */
    9671                 :   int nDb;                      /* Number of backends currently in use */
    9672                 :   Db *aDb;                      /* All backends */
    9673                 :   int flags;                    /* Miscellaneous flags. See below */
    9674                 :   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
    9675                 :   int errCode;                  /* Most recent error code (SQLITE_*) */
    9676                 :   int errMask;                  /* & result codes with this before returning */
    9677                 :   u8 autoCommit;                /* The auto-commit flag. */
    9678                 :   u8 temp_store;                /* 1: file 2: memory 0: default */
    9679                 :   u8 mallocFailed;              /* True if we have seen a malloc failure */
    9680                 :   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
    9681                 :   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
    9682                 :   u8 suppressErr;               /* Do not issue error messages if true */
    9683                 :   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
    9684                 :   int nextPagesize;             /* Pagesize after VACUUM if >0 */
    9685                 :   int nTable;                   /* Number of tables in the database */
    9686                 :   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
    9687                 :   i64 lastRowid;                /* ROWID of most recent insert (see above) */
    9688                 :   u32 magic;                    /* Magic number for detect library misuse */
    9689                 :   int nChange;                  /* Value returned by sqlite3_changes() */
    9690                 :   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
    9691                 :   sqlite3_mutex *mutex;         /* Connection mutex */
    9692                 :   int aLimit[SQLITE_N_LIMIT];   /* Limits */
    9693                 :   struct sqlite3InitInfo {      /* Information used during initialization */
    9694                 :     int iDb;                    /* When back is being initialized */
    9695                 :     int newTnum;                /* Rootpage of table being initialized */
    9696                 :     u8 busy;                    /* TRUE if currently initializing */
    9697                 :     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
    9698                 :   } init;
    9699                 :   int nExtension;               /* Number of loaded extensions */
    9700                 :   void **aExtension;            /* Array of shared library handles */
    9701                 :   struct Vdbe *pVdbe;           /* List of active virtual machines */
    9702                 :   int activeVdbeCnt;            /* Number of VDBEs currently executing */
    9703                 :   int writeVdbeCnt;             /* Number of active VDBEs that are writing */
    9704                 :   int vdbeExecCnt;              /* Number of nested calls to VdbeExec() */
    9705                 :   void (*xTrace)(void*,const char*);        /* Trace function */
    9706                 :   void *pTraceArg;                          /* Argument to the trace function */
    9707                 :   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
    9708                 :   void *pProfileArg;                        /* Argument to profile function */
    9709                 :   void *pCommitArg;                 /* Argument to xCommitCallback() */   
    9710                 :   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
    9711                 :   void *pRollbackArg;               /* Argument to xRollbackCallback() */   
    9712                 :   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
    9713                 :   void *pUpdateArg;
    9714                 :   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
    9715                 : #ifndef SQLITE_OMIT_WAL
    9716                 :   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
    9717                 :   void *pWalArg;
    9718                 : #endif
    9719                 :   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
    9720                 :   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
    9721                 :   void *pCollNeededArg;
    9722                 :   sqlite3_value *pErr;          /* Most recent error message */
    9723                 :   char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
    9724                 :   char *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
    9725                 :   union {
    9726                 :     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
    9727                 :     double notUsed1;            /* Spacer */
    9728                 :   } u1;
    9729                 :   Lookaside lookaside;          /* Lookaside malloc configuration */
    9730                 : #ifndef SQLITE_OMIT_AUTHORIZATION
    9731                 :   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
    9732                 :                                 /* Access authorization function */
    9733                 :   void *pAuthArg;               /* 1st argument to the access auth function */
    9734                 : #endif
    9735                 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
    9736                 :   int (*xProgress)(void *);     /* The progress callback */
    9737                 :   void *pProgressArg;           /* Argument to the progress callback */
    9738                 :   int nProgressOps;             /* Number of opcodes for progress callback */
    9739                 : #endif
    9740                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
    9741                 :   Hash aModule;                 /* populated by sqlite3_create_module() */
    9742                 :   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
    9743                 :   VTable **aVTrans;             /* Virtual tables with open transactions */
    9744                 :   int nVTrans;                  /* Allocated size of aVTrans */
    9745                 :   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
    9746                 : #endif
    9747                 :   FuncDefHash aFunc;            /* Hash table of connection functions */
    9748                 :   Hash aCollSeq;                /* All collating sequences */
    9749                 :   BusyHandler busyHandler;      /* Busy callback */
    9750                 :   int busyTimeout;              /* Busy handler timeout, in msec */
    9751                 :   Db aDbStatic[2];              /* Static space for the 2 default backends */
    9752                 :   Savepoint *pSavepoint;        /* List of active savepoints */
    9753                 :   int nSavepoint;               /* Number of non-transaction savepoints */
    9754                 :   int nStatement;               /* Number of nested statement-transactions  */
    9755                 :   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
    9756                 :   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
    9757                 :   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
    9758                 : 
    9759                 : #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
    9760                 :   /* The following variables are all protected by the STATIC_MASTER 
    9761                 :   ** mutex, not by sqlite3.mutex. They are used by code in notify.c. 
    9762                 :   **
    9763                 :   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
    9764                 :   ** unlock so that it can proceed.
    9765                 :   **
    9766                 :   ** When X.pBlockingConnection==Y, that means that something that X tried
    9767                 :   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
    9768                 :   ** held by Y.
    9769                 :   */
    9770                 :   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
    9771                 :   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
    9772                 :   void *pUnlockArg;                     /* Argument to xUnlockNotify */
    9773                 :   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
    9774                 :   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
    9775                 : #endif
    9776                 : };
    9777                 : 
    9778                 : /*
    9779                 : ** A macro to discover the encoding of a database.
    9780                 : */
    9781                 : #define ENC(db) ((db)->aDb[0].pSchema->enc)
    9782                 : 
    9783                 : /*
    9784                 : ** Possible values for the sqlite3.flags.
    9785                 : */
    9786                 : #define SQLITE_VdbeTrace      0x00000100  /* True to trace VDBE execution */
    9787                 : #define SQLITE_InternChanges  0x00000200  /* Uncommitted Hash table changes */
    9788                 : #define SQLITE_FullColNames   0x00000400  /* Show full column names on SELECT */
    9789                 : #define SQLITE_ShortColNames  0x00000800  /* Show short columns names */
    9790                 : #define SQLITE_CountRows      0x00001000  /* Count rows changed by INSERT, */
    9791                 :                                           /*   DELETE, or UPDATE and return */
    9792                 :                                           /*   the count using a callback. */
    9793                 : #define SQLITE_NullCallback   0x00002000  /* Invoke the callback once if the */
    9794                 :                                           /*   result set is empty */
    9795                 : #define SQLITE_SqlTrace       0x00004000  /* Debug print SQL as it executes */
    9796                 : #define SQLITE_VdbeListing    0x00008000  /* Debug listings of VDBE programs */
    9797                 : #define SQLITE_WriteSchema    0x00010000  /* OK to update SQLITE_MASTER */
    9798                 : #define SQLITE_NoReadlock     0x00020000  /* Readlocks are omitted when 
    9799                 :                                           ** accessing read-only databases */
    9800                 : #define SQLITE_IgnoreChecks   0x00040000  /* Do not enforce check constraints */
    9801                 : #define SQLITE_ReadUncommitted 0x0080000  /* For shared-cache mode */
    9802                 : #define SQLITE_LegacyFileFmt  0x00100000  /* Create new databases in format 1 */
    9803                 : #define SQLITE_FullFSync      0x00200000  /* Use full fsync on the backend */
    9804                 : #define SQLITE_CkptFullFSync  0x00400000  /* Use full fsync for checkpoint */
    9805                 : #define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
    9806                 : #define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
    9807                 : #define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
    9808                 : #define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
    9809                 : #define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
    9810                 : #define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
    9811                 : #define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
    9812                 : #define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
    9813                 : 
    9814                 : /*
    9815                 : ** Bits of the sqlite3.flags field that are used by the
    9816                 : ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
    9817                 : ** These must be the low-order bits of the flags field.
    9818                 : */
    9819                 : #define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
    9820                 : #define SQLITE_ColumnCache    0x02        /* Disable the column cache */
    9821                 : #define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
    9822                 : #define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
    9823                 : #define SQLITE_IndexCover     0x10        /* Disable index covering table */
    9824                 : #define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
    9825                 : #define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
    9826                 : #define SQLITE_IdxRealAsInt   0x80        /* Store REAL as INT in indices */
    9827                 : #define SQLITE_DistinctOpt    0x80        /* DISTINCT using indexes */
    9828                 : #define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
    9829                 : 
    9830                 : /*
    9831                 : ** Possible values for the sqlite.magic field.
    9832                 : ** The numbers are obtained at random and have no special meaning, other
    9833                 : ** than being distinct from one another.
    9834                 : */
    9835                 : #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
    9836                 : #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
    9837                 : #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
    9838                 : #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
    9839                 : #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
    9840                 : 
    9841                 : /*
    9842                 : ** Each SQL function is defined by an instance of the following
    9843                 : ** structure.  A pointer to this structure is stored in the sqlite.aFunc
    9844                 : ** hash table.  When multiple functions have the same name, the hash table
    9845                 : ** points to a linked list of these structures.
    9846                 : */
    9847                 : struct FuncDef {
    9848                 :   i16 nArg;            /* Number of arguments.  -1 means unlimited */
    9849                 :   u8 iPrefEnc;         /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
    9850                 :   u8 flags;            /* Some combination of SQLITE_FUNC_* */
    9851                 :   void *pUserData;     /* User data parameter */
    9852                 :   FuncDef *pNext;      /* Next function with same name */
    9853                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
    9854                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
    9855                 :   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
    9856                 :   char *zName;         /* SQL name of the function. */
    9857                 :   FuncDef *pHash;      /* Next with a different name but the same hash */
    9858                 :   FuncDestructor *pDestructor;   /* Reference counted destructor function */
    9859                 : };
    9860                 : 
    9861                 : /*
    9862                 : ** This structure encapsulates a user-function destructor callback (as
    9863                 : ** configured using create_function_v2()) and a reference counter. When
    9864                 : ** create_function_v2() is called to create a function with a destructor,
    9865                 : ** a single object of this type is allocated. FuncDestructor.nRef is set to 
    9866                 : ** the number of FuncDef objects created (either 1 or 3, depending on whether
    9867                 : ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
    9868                 : ** member of each of the new FuncDef objects is set to point to the allocated
    9869                 : ** FuncDestructor.
    9870                 : **
    9871                 : ** Thereafter, when one of the FuncDef objects is deleted, the reference
    9872                 : ** count on this object is decremented. When it reaches 0, the destructor
    9873                 : ** is invoked and the FuncDestructor structure freed.
    9874                 : */
    9875                 : struct FuncDestructor {
    9876                 :   int nRef;
    9877                 :   void (*xDestroy)(void *);
    9878                 :   void *pUserData;
    9879                 : };
    9880                 : 
    9881                 : /*
    9882                 : ** Possible values for FuncDef.flags
    9883                 : */
    9884                 : #define SQLITE_FUNC_LIKE     0x01 /* Candidate for the LIKE optimization */
    9885                 : #define SQLITE_FUNC_CASE     0x02 /* Case-sensitive LIKE-type function */
    9886                 : #define SQLITE_FUNC_EPHEM    0x04 /* Ephemeral.  Delete with VDBE */
    9887                 : #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
    9888                 : #define SQLITE_FUNC_PRIVATE  0x10 /* Allowed for internal use only */
    9889                 : #define SQLITE_FUNC_COUNT    0x20 /* Built-in count(*) aggregate */
    9890                 : #define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
    9891                 : 
    9892                 : /*
    9893                 : ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
    9894                 : ** used to create the initializers for the FuncDef structures.
    9895                 : **
    9896                 : **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
    9897                 : **     Used to create a scalar function definition of a function zName 
    9898                 : **     implemented by C function xFunc that accepts nArg arguments. The
    9899                 : **     value passed as iArg is cast to a (void*) and made available
    9900                 : **     as the user-data (sqlite3_user_data()) for the function. If 
    9901                 : **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
    9902                 : **
    9903                 : **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
    9904                 : **     Used to create an aggregate function definition implemented by
    9905                 : **     the C functions xStep and xFinal. The first four parameters
    9906                 : **     are interpreted in the same way as the first 4 parameters to
    9907                 : **     FUNCTION().
    9908                 : **
    9909                 : **   LIKEFUNC(zName, nArg, pArg, flags)
    9910                 : **     Used to create a scalar function definition of a function zName 
    9911                 : **     that accepts nArg arguments and is implemented by a call to C 
    9912                 : **     function likeFunc. Argument pArg is cast to a (void *) and made
    9913                 : **     available as the function user-data (sqlite3_user_data()). The
    9914                 : **     FuncDef.flags variable is set to the value passed as the flags
    9915                 : **     parameter.
    9916                 : */
    9917                 : #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
    9918                 :   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
    9919                 :    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
    9920                 : #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
    9921                 :   {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
    9922                 :    pArg, 0, xFunc, 0, 0, #zName, 0, 0}
    9923                 : #define LIKEFUNC(zName, nArg, arg, flags) \
    9924                 :   {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
    9925                 : #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
    9926                 :   {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
    9927                 :    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
    9928                 : 
    9929                 : /*
    9930                 : ** All current savepoints are stored in a linked list starting at
    9931                 : ** sqlite3.pSavepoint. The first element in the list is the most recently
    9932                 : ** opened savepoint. Savepoints are added to the list by the vdbe
    9933                 : ** OP_Savepoint instruction.
    9934                 : */
    9935                 : struct Savepoint {
    9936                 :   char *zName;                        /* Savepoint name (nul-terminated) */
    9937                 :   i64 nDeferredCons;                  /* Number of deferred fk violations */
    9938                 :   Savepoint *pNext;                   /* Parent savepoint (if any) */
    9939                 : };
    9940                 : 
    9941                 : /*
    9942                 : ** The following are used as the second parameter to sqlite3Savepoint(),
    9943                 : ** and as the P1 argument to the OP_Savepoint instruction.
    9944                 : */
    9945                 : #define SAVEPOINT_BEGIN      0
    9946                 : #define SAVEPOINT_RELEASE    1
    9947                 : #define SAVEPOINT_ROLLBACK   2
    9948                 : 
    9949                 : 
    9950                 : /*
    9951                 : ** Each SQLite module (virtual table definition) is defined by an
    9952                 : ** instance of the following structure, stored in the sqlite3.aModule
    9953                 : ** hash table.
    9954                 : */
    9955                 : struct Module {
    9956                 :   const sqlite3_module *pModule;       /* Callback pointers */
    9957                 :   const char *zName;                   /* Name passed to create_module() */
    9958                 :   void *pAux;                          /* pAux passed to create_module() */
    9959                 :   void (*xDestroy)(void *);            /* Module destructor function */
    9960                 : };
    9961                 : 
    9962                 : /*
    9963                 : ** information about each column of an SQL table is held in an instance
    9964                 : ** of this structure.
    9965                 : */
    9966                 : struct Column {
    9967                 :   char *zName;     /* Name of this column */
    9968                 :   Expr *pDflt;     /* Default value of this column */
    9969                 :   char *zDflt;     /* Original text of the default value */
    9970                 :   char *zType;     /* Data type for this column */
    9971                 :   char *zColl;     /* Collating sequence.  If NULL, use the default */
    9972                 :   u8 notNull;      /* True if there is a NOT NULL constraint */
    9973                 :   u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
    9974                 :   char affinity;   /* One of the SQLITE_AFF_... values */
    9975                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
    9976                 :   u8 isHidden;     /* True if this column is 'hidden' */
    9977                 : #endif
    9978                 : };
    9979                 : 
    9980                 : /*
    9981                 : ** A "Collating Sequence" is defined by an instance of the following
    9982                 : ** structure. Conceptually, a collating sequence consists of a name and
    9983                 : ** a comparison routine that defines the order of that sequence.
    9984                 : **
    9985                 : ** There may two separate implementations of the collation function, one
    9986                 : ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
    9987                 : ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
    9988                 : ** native byte order. When a collation sequence is invoked, SQLite selects
    9989                 : ** the version that will require the least expensive encoding
    9990                 : ** translations, if any.
    9991                 : **
    9992                 : ** The CollSeq.pUser member variable is an extra parameter that passed in
    9993                 : ** as the first argument to the UTF-8 comparison function, xCmp.
    9994                 : ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
    9995                 : ** xCmp16.
    9996                 : **
    9997                 : ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
    9998                 : ** collating sequence is undefined.  Indices built on an undefined
    9999                 : ** collating sequence may not be read or written.
   10000                 : */
   10001                 : struct CollSeq {
   10002                 :   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
   10003                 :   u8 enc;               /* Text encoding handled by xCmp() */
   10004                 :   void *pUser;          /* First argument to xCmp() */
   10005                 :   int (*xCmp)(void*,int, const void*, int, const void*);
   10006                 :   void (*xDel)(void*);  /* Destructor for pUser */
   10007                 : };
   10008                 : 
   10009                 : /*
   10010                 : ** A sort order can be either ASC or DESC.
   10011                 : */
   10012                 : #define SQLITE_SO_ASC       0  /* Sort in ascending order */
   10013                 : #define SQLITE_SO_DESC      1  /* Sort in ascending order */
   10014                 : 
   10015                 : /*
   10016                 : ** Column affinity types.
   10017                 : **
   10018                 : ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
   10019                 : ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
   10020                 : ** the speed a little by numbering the values consecutively.  
   10021                 : **
   10022                 : ** But rather than start with 0 or 1, we begin with 'a'.  That way,
   10023                 : ** when multiple affinity types are concatenated into a string and
   10024                 : ** used as the P4 operand, they will be more readable.
   10025                 : **
   10026                 : ** Note also that the numeric types are grouped together so that testing
   10027                 : ** for a numeric type is a single comparison.
   10028                 : */
   10029                 : #define SQLITE_AFF_TEXT     'a'
   10030                 : #define SQLITE_AFF_NONE     'b'
   10031                 : #define SQLITE_AFF_NUMERIC  'c'
   10032                 : #define SQLITE_AFF_INTEGER  'd'
   10033                 : #define SQLITE_AFF_REAL     'e'
   10034                 : 
   10035                 : #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
   10036                 : 
   10037                 : /*
   10038                 : ** The SQLITE_AFF_MASK values masks off the significant bits of an
   10039                 : ** affinity value. 
   10040                 : */
   10041                 : #define SQLITE_AFF_MASK     0x67
   10042                 : 
   10043                 : /*
   10044                 : ** Additional bit values that can be ORed with an affinity without
   10045                 : ** changing the affinity.
   10046                 : */
   10047                 : #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
   10048                 : #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
   10049                 : #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
   10050                 : 
   10051                 : /*
   10052                 : ** An object of this type is created for each virtual table present in
   10053                 : ** the database schema. 
   10054                 : **
   10055                 : ** If the database schema is shared, then there is one instance of this
   10056                 : ** structure for each database connection (sqlite3*) that uses the shared
   10057                 : ** schema. This is because each database connection requires its own unique
   10058                 : ** instance of the sqlite3_vtab* handle used to access the virtual table 
   10059                 : ** implementation. sqlite3_vtab* handles can not be shared between 
   10060                 : ** database connections, even when the rest of the in-memory database 
   10061                 : ** schema is shared, as the implementation often stores the database
   10062                 : ** connection handle passed to it via the xConnect() or xCreate() method
   10063                 : ** during initialization internally. This database connection handle may
   10064                 : ** then be used by the virtual table implementation to access real tables 
   10065                 : ** within the database. So that they appear as part of the callers 
   10066                 : ** transaction, these accesses need to be made via the same database 
   10067                 : ** connection as that used to execute SQL operations on the virtual table.
   10068                 : **
   10069                 : ** All VTable objects that correspond to a single table in a shared
   10070                 : ** database schema are initially stored in a linked-list pointed to by
   10071                 : ** the Table.pVTable member variable of the corresponding Table object.
   10072                 : ** When an sqlite3_prepare() operation is required to access the virtual
   10073                 : ** table, it searches the list for the VTable that corresponds to the
   10074                 : ** database connection doing the preparing so as to use the correct
   10075                 : ** sqlite3_vtab* handle in the compiled query.
   10076                 : **
   10077                 : ** When an in-memory Table object is deleted (for example when the
   10078                 : ** schema is being reloaded for some reason), the VTable objects are not 
   10079                 : ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed 
   10080                 : ** immediately. Instead, they are moved from the Table.pVTable list to
   10081                 : ** another linked list headed by the sqlite3.pDisconnect member of the
   10082                 : ** corresponding sqlite3 structure. They are then deleted/xDisconnected 
   10083                 : ** next time a statement is prepared using said sqlite3*. This is done
   10084                 : ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
   10085                 : ** Refer to comments above function sqlite3VtabUnlockList() for an
   10086                 : ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
   10087                 : ** list without holding the corresponding sqlite3.mutex mutex.
   10088                 : **
   10089                 : ** The memory for objects of this type is always allocated by 
   10090                 : ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as 
   10091                 : ** the first argument.
   10092                 : */
   10093                 : struct VTable {
   10094                 :   sqlite3 *db;              /* Database connection associated with this table */
   10095                 :   Module *pMod;             /* Pointer to module implementation */
   10096                 :   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
   10097                 :   int nRef;                 /* Number of pointers to this structure */
   10098                 :   u8 bConstraint;           /* True if constraints are supported */
   10099                 :   int iSavepoint;           /* Depth of the SAVEPOINT stack */
   10100                 :   VTable *pNext;            /* Next in linked list (see above) */
   10101                 : };
   10102                 : 
   10103                 : /*
   10104                 : ** Each SQL table is represented in memory by an instance of the
   10105                 : ** following structure.
   10106                 : **
   10107                 : ** Table.zName is the name of the table.  The case of the original
   10108                 : ** CREATE TABLE statement is stored, but case is not significant for
   10109                 : ** comparisons.
   10110                 : **
   10111                 : ** Table.nCol is the number of columns in this table.  Table.aCol is a
   10112                 : ** pointer to an array of Column structures, one for each column.
   10113                 : **
   10114                 : ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
   10115                 : ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
   10116                 : ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
   10117                 : ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
   10118                 : ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
   10119                 : ** is generated for each row of the table.  TF_HasPrimaryKey is set if
   10120                 : ** the table has any PRIMARY KEY, INTEGER or otherwise.
   10121                 : **
   10122                 : ** Table.tnum is the page number for the root BTree page of the table in the
   10123                 : ** database file.  If Table.iDb is the index of the database table backend
   10124                 : ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
   10125                 : ** holds temporary tables and indices.  If TF_Ephemeral is set
   10126                 : ** then the table is stored in a file that is automatically deleted
   10127                 : ** when the VDBE cursor to the table is closed.  In this case Table.tnum 
   10128                 : ** refers VDBE cursor number that holds the table open, not to the root
   10129                 : ** page number.  Transient tables are used to hold the results of a
   10130                 : ** sub-query that appears instead of a real table name in the FROM clause 
   10131                 : ** of a SELECT statement.
   10132                 : */
   10133                 : struct Table {
   10134                 :   char *zName;         /* Name of the table or view */
   10135                 :   int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
   10136                 :   int nCol;            /* Number of columns in this table */
   10137                 :   Column *aCol;        /* Information about each column */
   10138                 :   Index *pIndex;       /* List of SQL indexes on this table. */
   10139                 :   int tnum;            /* Root BTree node for this table (see note above) */
   10140                 :   tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
   10141                 :   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
   10142                 :   u16 nRef;            /* Number of pointers to this Table */
   10143                 :   u8 tabFlags;         /* Mask of TF_* values */
   10144                 :   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
   10145                 :   FKey *pFKey;         /* Linked list of all foreign keys in this table */
   10146                 :   char *zColAff;       /* String defining the affinity of each column */
   10147                 : #ifndef SQLITE_OMIT_CHECK
   10148                 :   Expr *pCheck;        /* The AND of all CHECK constraints */
   10149                 : #endif
   10150                 : #ifndef SQLITE_OMIT_ALTERTABLE
   10151                 :   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
   10152                 : #endif
   10153                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   10154                 :   VTable *pVTable;     /* List of VTable objects. */
   10155                 :   int nModuleArg;      /* Number of arguments to the module */
   10156                 :   char **azModuleArg;  /* Text of all module args. [0] is module name */
   10157                 : #endif
   10158                 :   Trigger *pTrigger;   /* List of triggers stored in pSchema */
   10159                 :   Schema *pSchema;     /* Schema that contains this table */
   10160                 :   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
   10161                 : };
   10162                 : 
   10163                 : /*
   10164                 : ** Allowed values for Tabe.tabFlags.
   10165                 : */
   10166                 : #define TF_Readonly        0x01    /* Read-only system table */
   10167                 : #define TF_Ephemeral       0x02    /* An ephemeral table */
   10168                 : #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
   10169                 : #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
   10170                 : #define TF_Virtual         0x10    /* Is a virtual table */
   10171                 : #define TF_NeedMetadata    0x20    /* aCol[].zType and aCol[].pColl missing */
   10172                 : 
   10173                 : 
   10174                 : 
   10175                 : /*
   10176                 : ** Test to see whether or not a table is a virtual table.  This is
   10177                 : ** done as a macro so that it will be optimized out when virtual
   10178                 : ** table support is omitted from the build.
   10179                 : */
   10180                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   10181                 : #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
   10182                 : #  define IsHiddenColumn(X) ((X)->isHidden)
   10183                 : #else
   10184                 : #  define IsVirtual(X)      0
   10185                 : #  define IsHiddenColumn(X) 0
   10186                 : #endif
   10187                 : 
   10188                 : /*
   10189                 : ** Each foreign key constraint is an instance of the following structure.
   10190                 : **
   10191                 : ** A foreign key is associated with two tables.  The "from" table is
   10192                 : ** the table that contains the REFERENCES clause that creates the foreign
   10193                 : ** key.  The "to" table is the table that is named in the REFERENCES clause.
   10194                 : ** Consider this example:
   10195                 : **
   10196                 : **     CREATE TABLE ex1(
   10197                 : **       a INTEGER PRIMARY KEY,
   10198                 : **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
   10199                 : **     );
   10200                 : **
   10201                 : ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
   10202                 : **
   10203                 : ** Each REFERENCES clause generates an instance of the following structure
   10204                 : ** which is attached to the from-table.  The to-table need not exist when
   10205                 : ** the from-table is created.  The existence of the to-table is not checked.
   10206                 : */
   10207                 : struct FKey {
   10208                 :   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
   10209                 :   FKey *pNextFrom;  /* Next foreign key in pFrom */
   10210                 :   char *zTo;        /* Name of table that the key points to (aka: Parent) */
   10211                 :   FKey *pNextTo;    /* Next foreign key on table named zTo */
   10212                 :   FKey *pPrevTo;    /* Previous foreign key on table named zTo */
   10213                 :   int nCol;         /* Number of columns in this key */
   10214                 :   /* EV: R-30323-21917 */
   10215                 :   u8 isDeferred;    /* True if constraint checking is deferred till COMMIT */
   10216                 :   u8 aAction[2];          /* ON DELETE and ON UPDATE actions, respectively */
   10217                 :   Trigger *apTrigger[2];  /* Triggers for aAction[] actions */
   10218                 :   struct sColMap {  /* Mapping of columns in pFrom to columns in zTo */
   10219                 :     int iFrom;         /* Index of column in pFrom */
   10220                 :     char *zCol;        /* Name of column in zTo.  If 0 use PRIMARY KEY */
   10221                 :   } aCol[1];        /* One entry for each of nCol column s */
   10222                 : };
   10223                 : 
   10224                 : /*
   10225                 : ** SQLite supports many different ways to resolve a constraint
   10226                 : ** error.  ROLLBACK processing means that a constraint violation
   10227                 : ** causes the operation in process to fail and for the current transaction
   10228                 : ** to be rolled back.  ABORT processing means the operation in process
   10229                 : ** fails and any prior changes from that one operation are backed out,
   10230                 : ** but the transaction is not rolled back.  FAIL processing means that
   10231                 : ** the operation in progress stops and returns an error code.  But prior
   10232                 : ** changes due to the same operation are not backed out and no rollback
   10233                 : ** occurs.  IGNORE means that the particular row that caused the constraint
   10234                 : ** error is not inserted or updated.  Processing continues and no error
   10235                 : ** is returned.  REPLACE means that preexisting database rows that caused
   10236                 : ** a UNIQUE constraint violation are removed so that the new insert or
   10237                 : ** update can proceed.  Processing continues and no error is reported.
   10238                 : **
   10239                 : ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
   10240                 : ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
   10241                 : ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
   10242                 : ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
   10243                 : ** referenced table row is propagated into the row that holds the
   10244                 : ** foreign key.
   10245                 : ** 
   10246                 : ** The following symbolic values are used to record which type
   10247                 : ** of action to take.
   10248                 : */
   10249                 : #define OE_None     0   /* There is no constraint to check */
   10250                 : #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
   10251                 : #define OE_Abort    2   /* Back out changes but do no rollback transaction */
   10252                 : #define OE_Fail     3   /* Stop the operation but leave all prior changes */
   10253                 : #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
   10254                 : #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
   10255                 : 
   10256                 : #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
   10257                 : #define OE_SetNull  7   /* Set the foreign key value to NULL */
   10258                 : #define OE_SetDflt  8   /* Set the foreign key value to its default */
   10259                 : #define OE_Cascade  9   /* Cascade the changes */
   10260                 : 
   10261                 : #define OE_Default  99  /* Do whatever the default action is */
   10262                 : 
   10263                 : 
   10264                 : /*
   10265                 : ** An instance of the following structure is passed as the first
   10266                 : ** argument to sqlite3VdbeKeyCompare and is used to control the 
   10267                 : ** comparison of the two index keys.
   10268                 : */
   10269                 : struct KeyInfo {
   10270                 :   sqlite3 *db;        /* The database connection */
   10271                 :   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
   10272                 :   u16 nField;         /* Number of entries in aColl[] */
   10273                 :   u8 *aSortOrder;     /* Sort order for each column.  May be NULL */
   10274                 :   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
   10275                 : };
   10276                 : 
   10277                 : /*
   10278                 : ** An instance of the following structure holds information about a
   10279                 : ** single index record that has already been parsed out into individual
   10280                 : ** values.
   10281                 : **
   10282                 : ** A record is an object that contains one or more fields of data.
   10283                 : ** Records are used to store the content of a table row and to store
   10284                 : ** the key of an index.  A blob encoding of a record is created by
   10285                 : ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
   10286                 : ** OP_Column opcode.
   10287                 : **
   10288                 : ** This structure holds a record that has already been disassembled
   10289                 : ** into its constituent fields.
   10290                 : */
   10291                 : struct UnpackedRecord {
   10292                 :   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
   10293                 :   u16 nField;         /* Number of entries in apMem[] */
   10294                 :   u8 flags;           /* Boolean settings.  UNPACKED_... below */
   10295                 :   i64 rowid;          /* Used by UNPACKED_PREFIX_SEARCH */
   10296                 :   Mem *aMem;          /* Values */
   10297                 : };
   10298                 : 
   10299                 : /*
   10300                 : ** Allowed values of UnpackedRecord.flags
   10301                 : */
   10302                 : #define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
   10303                 : #define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
   10304                 : #define UNPACKED_PREFIX_SEARCH 0x04  /* Ignore final (rowid) field */
   10305                 : 
   10306                 : /*
   10307                 : ** Each SQL index is represented in memory by an
   10308                 : ** instance of the following structure.
   10309                 : **
   10310                 : ** The columns of the table that are to be indexed are described
   10311                 : ** by the aiColumn[] field of this structure.  For example, suppose
   10312                 : ** we have the following table and index:
   10313                 : **
   10314                 : **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
   10315                 : **     CREATE INDEX Ex2 ON Ex1(c3,c1);
   10316                 : **
   10317                 : ** In the Table structure describing Ex1, nCol==3 because there are
   10318                 : ** three columns in the table.  In the Index structure describing
   10319                 : ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
   10320                 : ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the 
   10321                 : ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
   10322                 : ** The second column to be indexed (c1) has an index of 0 in
   10323                 : ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
   10324                 : **
   10325                 : ** The Index.onError field determines whether or not the indexed columns
   10326                 : ** must be unique and what to do if they are not.  When Index.onError=OE_None,
   10327                 : ** it means this is not a unique index.  Otherwise it is a unique index
   10328                 : ** and the value of Index.onError indicate the which conflict resolution 
   10329                 : ** algorithm to employ whenever an attempt is made to insert a non-unique
   10330                 : ** element.
   10331                 : */
   10332                 : struct Index {
   10333                 :   char *zName;     /* Name of this index */
   10334                 :   int nColumn;     /* Number of columns in the table used by this index */
   10335                 :   int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
   10336                 :   tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
   10337                 :   Table *pTable;   /* The SQL table being indexed */
   10338                 :   int tnum;        /* Page containing root of this index in database file */
   10339                 :   u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
   10340                 :   u8 autoIndex;    /* True if is automatically created (ex: by UNIQUE) */
   10341                 :   u8 bUnordered;   /* Use this index for == or IN queries only */
   10342                 :   char *zColAff;   /* String defining the affinity of each column */
   10343                 :   Index *pNext;    /* The next index associated with the same table */
   10344                 :   Schema *pSchema; /* Schema containing this index */
   10345                 :   u8 *aSortOrder;  /* Array of size Index.nColumn. True==DESC, False==ASC */
   10346                 :   char **azColl;   /* Array of collation sequence names for index */
   10347                 : #ifdef SQLITE_ENABLE_STAT3
   10348                 :   int nSample;             /* Number of elements in aSample[] */
   10349                 :   tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
   10350                 :   IndexSample *aSample;    /* Samples of the left-most key */
   10351                 : #endif
   10352                 : };
   10353                 : 
   10354                 : /*
   10355                 : ** Each sample stored in the sqlite_stat3 table is represented in memory 
   10356                 : ** using a structure of this type.  See documentation at the top of the
   10357                 : ** analyze.c source file for additional information.
   10358                 : */
   10359                 : struct IndexSample {
   10360                 :   union {
   10361                 :     char *z;        /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
   10362                 :     double r;       /* Value if eType is SQLITE_FLOAT */
   10363                 :     i64 i;          /* Value if eType is SQLITE_INTEGER */
   10364                 :   } u;
   10365                 :   u8 eType;         /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
   10366                 :   int nByte;        /* Size in byte of text or blob. */
   10367                 :   tRowcnt nEq;      /* Est. number of rows where the key equals this sample */
   10368                 :   tRowcnt nLt;      /* Est. number of rows where key is less than this sample */
   10369                 :   tRowcnt nDLt;     /* Est. number of distinct keys less than this sample */
   10370                 : };
   10371                 : 
   10372                 : /*
   10373                 : ** Each token coming out of the lexer is an instance of
   10374                 : ** this structure.  Tokens are also used as part of an expression.
   10375                 : **
   10376                 : ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
   10377                 : ** may contain random values.  Do not make any assumptions about Token.dyn
   10378                 : ** and Token.n when Token.z==0.
   10379                 : */
   10380                 : struct Token {
   10381                 :   const char *z;     /* Text of the token.  Not NULL-terminated! */
   10382                 :   unsigned int n;    /* Number of characters in this token */
   10383                 : };
   10384                 : 
   10385                 : /*
   10386                 : ** An instance of this structure contains information needed to generate
   10387                 : ** code for a SELECT that contains aggregate functions.
   10388                 : **
   10389                 : ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
   10390                 : ** pointer to this structure.  The Expr.iColumn field is the index in
   10391                 : ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
   10392                 : ** code for that node.
   10393                 : **
   10394                 : ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
   10395                 : ** original Select structure that describes the SELECT statement.  These
   10396                 : ** fields do not need to be freed when deallocating the AggInfo structure.
   10397                 : */
   10398                 : struct AggInfo {
   10399                 :   u8 directMode;          /* Direct rendering mode means take data directly
   10400                 :                           ** from source tables rather than from accumulators */
   10401                 :   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
   10402                 :                           ** than the source table */
   10403                 :   int sortingIdx;         /* Cursor number of the sorting index */
   10404                 :   int sortingIdxPTab;     /* Cursor number of pseudo-table */
   10405                 :   ExprList *pGroupBy;     /* The group by clause */
   10406                 :   int nSortingColumn;     /* Number of columns in the sorting index */
   10407                 :   struct AggInfo_col {    /* For each column used in source tables */
   10408                 :     Table *pTab;             /* Source table */
   10409                 :     int iTable;              /* Cursor number of the source table */
   10410                 :     int iColumn;             /* Column number within the source table */
   10411                 :     int iSorterColumn;       /* Column number in the sorting index */
   10412                 :     int iMem;                /* Memory location that acts as accumulator */
   10413                 :     Expr *pExpr;             /* The original expression */
   10414                 :   } *aCol;
   10415                 :   int nColumn;            /* Number of used entries in aCol[] */
   10416                 :   int nColumnAlloc;       /* Number of slots allocated for aCol[] */
   10417                 :   int nAccumulator;       /* Number of columns that show through to the output.
   10418                 :                           ** Additional columns are used only as parameters to
   10419                 :                           ** aggregate functions */
   10420                 :   struct AggInfo_func {   /* For each aggregate function */
   10421                 :     Expr *pExpr;             /* Expression encoding the function */
   10422                 :     FuncDef *pFunc;          /* The aggregate function implementation */
   10423                 :     int iMem;                /* Memory location that acts as accumulator */
   10424                 :     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
   10425                 :   } *aFunc;
   10426                 :   int nFunc;              /* Number of entries in aFunc[] */
   10427                 :   int nFuncAlloc;         /* Number of slots allocated for aFunc[] */
   10428                 : };
   10429                 : 
   10430                 : /*
   10431                 : ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
   10432                 : ** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
   10433                 : ** than 32767 we have to make it 32-bit.  16-bit is preferred because
   10434                 : ** it uses less memory in the Expr object, which is a big memory user
   10435                 : ** in systems with lots of prepared statements.  And few applications
   10436                 : ** need more than about 10 or 20 variables.  But some extreme users want
   10437                 : ** to have prepared statements with over 32767 variables, and for them
   10438                 : ** the option is available (at compile-time).
   10439                 : */
   10440                 : #if SQLITE_MAX_VARIABLE_NUMBER<=32767
   10441                 : typedef i16 ynVar;
   10442                 : #else
   10443                 : typedef int ynVar;
   10444                 : #endif
   10445                 : 
   10446                 : /*
   10447                 : ** Each node of an expression in the parse tree is an instance
   10448                 : ** of this structure.
   10449                 : **
   10450                 : ** Expr.op is the opcode. The integer parser token codes are reused
   10451                 : ** as opcodes here. For example, the parser defines TK_GE to be an integer
   10452                 : ** code representing the ">=" operator. This same integer code is reused
   10453                 : ** to represent the greater-than-or-equal-to operator in the expression
   10454                 : ** tree.
   10455                 : **
   10456                 : ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, 
   10457                 : ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
   10458                 : ** the expression is a variable (TK_VARIABLE), then Expr.token contains the 
   10459                 : ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
   10460                 : ** then Expr.token contains the name of the function.
   10461                 : **
   10462                 : ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
   10463                 : ** binary operator. Either or both may be NULL.
   10464                 : **
   10465                 : ** Expr.x.pList is a list of arguments if the expression is an SQL function,
   10466                 : ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
   10467                 : ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
   10468                 : ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
   10469                 : ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is 
   10470                 : ** valid.
   10471                 : **
   10472                 : ** An expression of the form ID or ID.ID refers to a column in a table.
   10473                 : ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
   10474                 : ** the integer cursor number of a VDBE cursor pointing to that table and
   10475                 : ** Expr.iColumn is the column number for the specific column.  If the
   10476                 : ** expression is used as a result in an aggregate SELECT, then the
   10477                 : ** value is also stored in the Expr.iAgg column in the aggregate so that
   10478                 : ** it can be accessed after all aggregates are computed.
   10479                 : **
   10480                 : ** If the expression is an unbound variable marker (a question mark 
   10481                 : ** character '?' in the original SQL) then the Expr.iTable holds the index 
   10482                 : ** number for that variable.
   10483                 : **
   10484                 : ** If the expression is a subquery then Expr.iColumn holds an integer
   10485                 : ** register number containing the result of the subquery.  If the
   10486                 : ** subquery gives a constant result, then iTable is -1.  If the subquery
   10487                 : ** gives a different answer at different times during statement processing
   10488                 : ** then iTable is the address of a subroutine that computes the subquery.
   10489                 : **
   10490                 : ** If the Expr is of type OP_Column, and the table it is selecting from
   10491                 : ** is a disk table or the "old.*" pseudo-table, then pTab points to the
   10492                 : ** corresponding table definition.
   10493                 : **
   10494                 : ** ALLOCATION NOTES:
   10495                 : **
   10496                 : ** Expr objects can use a lot of memory space in database schema.  To
   10497                 : ** help reduce memory requirements, sometimes an Expr object will be
   10498                 : ** truncated.  And to reduce the number of memory allocations, sometimes
   10499                 : ** two or more Expr objects will be stored in a single memory allocation,
   10500                 : ** together with Expr.zToken strings.
   10501                 : **
   10502                 : ** If the EP_Reduced and EP_TokenOnly flags are set when
   10503                 : ** an Expr object is truncated.  When EP_Reduced is set, then all
   10504                 : ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
   10505                 : ** are contained within the same memory allocation.  Note, however, that
   10506                 : ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
   10507                 : ** allocated, regardless of whether or not EP_Reduced is set.
   10508                 : */
   10509                 : struct Expr {
   10510                 :   u8 op;                 /* Operation performed by this node */
   10511                 :   char affinity;         /* The affinity of the column or 0 if not a column */
   10512                 :   u16 flags;             /* Various flags.  EP_* See below */
   10513                 :   union {
   10514                 :     char *zToken;          /* Token value. Zero terminated and dequoted */
   10515                 :     int iValue;            /* Non-negative integer value if EP_IntValue */
   10516                 :   } u;
   10517                 : 
   10518                 :   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
   10519                 :   ** space is allocated for the fields below this point. An attempt to
   10520                 :   ** access them will result in a segfault or malfunction. 
   10521                 :   *********************************************************************/
   10522                 : 
   10523                 :   Expr *pLeft;           /* Left subnode */
   10524                 :   Expr *pRight;          /* Right subnode */
   10525                 :   union {
   10526                 :     ExprList *pList;     /* Function arguments or in "<expr> IN (<expr-list)" */
   10527                 :     Select *pSelect;     /* Used for sub-selects and "<expr> IN (<select>)" */
   10528                 :   } x;
   10529                 :   CollSeq *pColl;        /* The collation type of the column or 0 */
   10530                 : 
   10531                 :   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
   10532                 :   ** space is allocated for the fields below this point. An attempt to
   10533                 :   ** access them will result in a segfault or malfunction.
   10534                 :   *********************************************************************/
   10535                 : 
   10536                 :   int iTable;            /* TK_COLUMN: cursor number of table holding column
   10537                 :                          ** TK_REGISTER: register number
   10538                 :                          ** TK_TRIGGER: 1 -> new, 0 -> old */
   10539                 :   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
   10540                 :                          ** TK_VARIABLE: variable number (always >= 1). */
   10541                 :   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
   10542                 :   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
   10543                 :   u8 flags2;             /* Second set of flags.  EP2_... */
   10544                 :   u8 op2;                /* If a TK_REGISTER, the original value of Expr.op */
   10545                 :   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
   10546                 :   Table *pTab;           /* Table for TK_COLUMN expressions. */
   10547                 : #if SQLITE_MAX_EXPR_DEPTH>0
   10548                 :   int nHeight;           /* Height of the tree headed by this node */
   10549                 : #endif
   10550                 : };
   10551                 : 
   10552                 : /*
   10553                 : ** The following are the meanings of bits in the Expr.flags field.
   10554                 : */
   10555                 : #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
   10556                 : #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
   10557                 : #define EP_Resolved   0x0004  /* IDs have been resolved to COLUMNs */
   10558                 : #define EP_Error      0x0008  /* Expression contains one or more errors */
   10559                 : #define EP_Distinct   0x0010  /* Aggregate function with DISTINCT keyword */
   10560                 : #define EP_VarSelect  0x0020  /* pSelect is correlated, not constant */
   10561                 : #define EP_DblQuoted  0x0040  /* token.z was originally in "..." */
   10562                 : #define EP_InfixFunc  0x0080  /* True for an infix function: LIKE, GLOB, etc */
   10563                 : #define EP_ExpCollate 0x0100  /* Collating sequence specified explicitly */
   10564                 : #define EP_FixedDest  0x0200  /* Result needed in a specific register */
   10565                 : #define EP_IntValue   0x0400  /* Integer value contained in u.iValue */
   10566                 : #define EP_xIsSelect  0x0800  /* x.pSelect is valid (otherwise x.pList is) */
   10567                 : #define EP_Hint       0x1000  /* Optimizer hint. Not required for correctness */
   10568                 : #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
   10569                 : #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
   10570                 : #define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
   10571                 : 
   10572                 : /*
   10573                 : ** The following are the meanings of bits in the Expr.flags2 field.
   10574                 : */
   10575                 : #define EP2_MallocedToken  0x0001  /* Need to sqlite3DbFree() Expr.zToken */
   10576                 : #define EP2_Irreducible    0x0002  /* Cannot EXPRDUP_REDUCE this Expr */
   10577                 : 
   10578                 : /*
   10579                 : ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
   10580                 : ** flag on an expression structure.  This flag is used for VV&A only.  The
   10581                 : ** routine is implemented as a macro that only works when in debugging mode,
   10582                 : ** so as not to burden production code.
   10583                 : */
   10584                 : #ifdef SQLITE_DEBUG
   10585                 : # define ExprSetIrreducible(X)  (X)->flags2 |= EP2_Irreducible
   10586                 : #else
   10587                 : # define ExprSetIrreducible(X)
   10588                 : #endif
   10589                 : 
   10590                 : /*
   10591                 : ** These macros can be used to test, set, or clear bits in the 
   10592                 : ** Expr.flags field.
   10593                 : */
   10594                 : #define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
   10595                 : #define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
   10596                 : #define ExprSetProperty(E,P)     (E)->flags|=(P)
   10597                 : #define ExprClearProperty(E,P)   (E)->flags&=~(P)
   10598                 : 
   10599                 : /*
   10600                 : ** Macros to determine the number of bytes required by a normal Expr 
   10601                 : ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
   10602                 : ** and an Expr struct with the EP_TokenOnly flag set.
   10603                 : */
   10604                 : #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
   10605                 : #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
   10606                 : #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
   10607                 : 
   10608                 : /*
   10609                 : ** Flags passed to the sqlite3ExprDup() function. See the header comment 
   10610                 : ** above sqlite3ExprDup() for details.
   10611                 : */
   10612                 : #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
   10613                 : 
   10614                 : /*
   10615                 : ** A list of expressions.  Each expression may optionally have a
   10616                 : ** name.  An expr/name combination can be used in several ways, such
   10617                 : ** as the list of "expr AS ID" fields following a "SELECT" or in the
   10618                 : ** list of "ID = expr" items in an UPDATE.  A list of expressions can
   10619                 : ** also be used as the argument to a function, in which case the a.zName
   10620                 : ** field is not used.
   10621                 : */
   10622                 : struct ExprList {
   10623                 :   int nExpr;             /* Number of expressions on the list */
   10624                 :   int nAlloc;            /* Number of entries allocated below */
   10625                 :   int iECursor;          /* VDBE Cursor associated with this ExprList */
   10626                 :   struct ExprList_item {
   10627                 :     Expr *pExpr;           /* The list of expressions */
   10628                 :     char *zName;           /* Token associated with this expression */
   10629                 :     char *zSpan;           /* Original text of the expression */
   10630                 :     u8 sortOrder;          /* 1 for DESC or 0 for ASC */
   10631                 :     u8 done;               /* A flag to indicate when processing is finished */
   10632                 :     u16 iOrderByCol;       /* For ORDER BY, column number in result set */
   10633                 :     u16 iAlias;            /* Index into Parse.aAlias[] for zName */
   10634                 :   } *a;                  /* One entry for each expression */
   10635                 : };
   10636                 : 
   10637                 : /*
   10638                 : ** An instance of this structure is used by the parser to record both
   10639                 : ** the parse tree for an expression and the span of input text for an
   10640                 : ** expression.
   10641                 : */
   10642                 : struct ExprSpan {
   10643                 :   Expr *pExpr;          /* The expression parse tree */
   10644                 :   const char *zStart;   /* First character of input text */
   10645                 :   const char *zEnd;     /* One character past the end of input text */
   10646                 : };
   10647                 : 
   10648                 : /*
   10649                 : ** An instance of this structure can hold a simple list of identifiers,
   10650                 : ** such as the list "a,b,c" in the following statements:
   10651                 : **
   10652                 : **      INSERT INTO t(a,b,c) VALUES ...;
   10653                 : **      CREATE INDEX idx ON t(a,b,c);
   10654                 : **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
   10655                 : **
   10656                 : ** The IdList.a.idx field is used when the IdList represents the list of
   10657                 : ** column names after a table name in an INSERT statement.  In the statement
   10658                 : **
   10659                 : **     INSERT INTO t(a,b,c) ...
   10660                 : **
   10661                 : ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
   10662                 : */
   10663                 : struct IdList {
   10664                 :   struct IdList_item {
   10665                 :     char *zName;      /* Name of the identifier */
   10666                 :     int idx;          /* Index in some Table.aCol[] of a column named zName */
   10667                 :   } *a;
   10668                 :   int nId;         /* Number of identifiers on the list */
   10669                 :   int nAlloc;      /* Number of entries allocated for a[] below */
   10670                 : };
   10671                 : 
   10672                 : /*
   10673                 : ** The bitmask datatype defined below is used for various optimizations.
   10674                 : **
   10675                 : ** Changing this from a 64-bit to a 32-bit type limits the number of
   10676                 : ** tables in a join to 32 instead of 64.  But it also reduces the size
   10677                 : ** of the library by 738 bytes on ix86.
   10678                 : */
   10679                 : typedef u64 Bitmask;
   10680                 : 
   10681                 : /*
   10682                 : ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
   10683                 : */
   10684                 : #define BMS  ((int)(sizeof(Bitmask)*8))
   10685                 : 
   10686                 : /*
   10687                 : ** The following structure describes the FROM clause of a SELECT statement.
   10688                 : ** Each table or subquery in the FROM clause is a separate element of
   10689                 : ** the SrcList.a[] array.
   10690                 : **
   10691                 : ** With the addition of multiple database support, the following structure
   10692                 : ** can also be used to describe a particular table such as the table that
   10693                 : ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
   10694                 : ** such a table must be a simple name: ID.  But in SQLite, the table can
   10695                 : ** now be identified by a database name, a dot, then the table name: ID.ID.
   10696                 : **
   10697                 : ** The jointype starts out showing the join type between the current table
   10698                 : ** and the next table on the list.  The parser builds the list this way.
   10699                 : ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
   10700                 : ** jointype expresses the join between the table and the previous table.
   10701                 : **
   10702                 : ** In the colUsed field, the high-order bit (bit 63) is set if the table
   10703                 : ** contains more than 63 columns and the 64-th or later column is used.
   10704                 : */
   10705                 : struct SrcList {
   10706                 :   i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
   10707                 :   i16 nAlloc;      /* Number of entries allocated in a[] below */
   10708                 :   struct SrcList_item {
   10709                 :     char *zDatabase;  /* Name of database holding this table */
   10710                 :     char *zName;      /* Name of the table */
   10711                 :     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
   10712                 :     Table *pTab;      /* An SQL table corresponding to zName */
   10713                 :     Select *pSelect;  /* A SELECT statement used in place of a table name */
   10714                 :     int addrFillSub;  /* Address of subroutine to manifest a subquery */
   10715                 :     int regReturn;    /* Register holding return address of addrFillSub */
   10716                 :     u8 jointype;      /* Type of join between this able and the previous */
   10717                 :     u8 notIndexed;    /* True if there is a NOT INDEXED clause */
   10718                 :     u8 isCorrelated;  /* True if sub-query is correlated */
   10719                 : #ifndef SQLITE_OMIT_EXPLAIN
   10720                 :     u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
   10721                 : #endif
   10722                 :     int iCursor;      /* The VDBE cursor number used to access this table */
   10723                 :     Expr *pOn;        /* The ON clause of a join */
   10724                 :     IdList *pUsing;   /* The USING clause of a join */
   10725                 :     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
   10726                 :     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
   10727                 :     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
   10728                 :   } a[1];             /* One entry for each identifier on the list */
   10729                 : };
   10730                 : 
   10731                 : /*
   10732                 : ** Permitted values of the SrcList.a.jointype field
   10733                 : */
   10734                 : #define JT_INNER     0x0001    /* Any kind of inner or cross join */
   10735                 : #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
   10736                 : #define JT_NATURAL   0x0004    /* True for a "natural" join */
   10737                 : #define JT_LEFT      0x0008    /* Left outer join */
   10738                 : #define JT_RIGHT     0x0010    /* Right outer join */
   10739                 : #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
   10740                 : #define JT_ERROR     0x0040    /* unknown or unsupported join type */
   10741                 : 
   10742                 : 
   10743                 : /*
   10744                 : ** A WherePlan object holds information that describes a lookup
   10745                 : ** strategy.
   10746                 : **
   10747                 : ** This object is intended to be opaque outside of the where.c module.
   10748                 : ** It is included here only so that that compiler will know how big it
   10749                 : ** is.  None of the fields in this object should be used outside of
   10750                 : ** the where.c module.
   10751                 : **
   10752                 : ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
   10753                 : ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
   10754                 : ** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
   10755                 : ** case that more than one of these conditions is true.
   10756                 : */
   10757                 : struct WherePlan {
   10758                 :   u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
   10759                 :   u32 nEq;                       /* Number of == constraints */
   10760                 :   double nRow;                   /* Estimated number of rows (for EQP) */
   10761                 :   union {
   10762                 :     Index *pIdx;                   /* Index when WHERE_INDEXED is true */
   10763                 :     struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
   10764                 :     sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
   10765                 :   } u;
   10766                 : };
   10767                 : 
   10768                 : /*
   10769                 : ** For each nested loop in a WHERE clause implementation, the WhereInfo
   10770                 : ** structure contains a single instance of this structure.  This structure
   10771                 : ** is intended to be private the the where.c module and should not be
   10772                 : ** access or modified by other modules.
   10773                 : **
   10774                 : ** The pIdxInfo field is used to help pick the best index on a
   10775                 : ** virtual table.  The pIdxInfo pointer contains indexing
   10776                 : ** information for the i-th table in the FROM clause before reordering.
   10777                 : ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
   10778                 : ** All other information in the i-th WhereLevel object for the i-th table
   10779                 : ** after FROM clause ordering.
   10780                 : */
   10781                 : struct WhereLevel {
   10782                 :   WherePlan plan;       /* query plan for this element of the FROM clause */
   10783                 :   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
   10784                 :   int iTabCur;          /* The VDBE cursor used to access the table */
   10785                 :   int iIdxCur;          /* The VDBE cursor used to access pIdx */
   10786                 :   int addrBrk;          /* Jump here to break out of the loop */
   10787                 :   int addrNxt;          /* Jump here to start the next IN combination */
   10788                 :   int addrCont;         /* Jump here to continue with the next loop cycle */
   10789                 :   int addrFirst;        /* First instruction of interior of the loop */
   10790                 :   u8 iFrom;             /* Which entry in the FROM clause */
   10791                 :   u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
   10792                 :   int p1, p2;           /* Operands of the opcode used to ends the loop */
   10793                 :   union {               /* Information that depends on plan.wsFlags */
   10794                 :     struct {
   10795                 :       int nIn;              /* Number of entries in aInLoop[] */
   10796                 :       struct InLoop {
   10797                 :         int iCur;              /* The VDBE cursor used by this IN operator */
   10798                 :         int addrInTop;         /* Top of the IN loop */
   10799                 :       } *aInLoop;           /* Information about each nested IN operator */
   10800                 :     } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
   10801                 :   } u;
   10802                 : 
   10803                 :   /* The following field is really not part of the current level.  But
   10804                 :   ** we need a place to cache virtual table index information for each
   10805                 :   ** virtual table in the FROM clause and the WhereLevel structure is
   10806                 :   ** a convenient place since there is one WhereLevel for each FROM clause
   10807                 :   ** element.
   10808                 :   */
   10809                 :   sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
   10810                 : };
   10811                 : 
   10812                 : /*
   10813                 : ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
   10814                 : ** and the WhereInfo.wctrlFlags member.
   10815                 : */
   10816                 : #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
   10817                 : #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
   10818                 : #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
   10819                 : #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
   10820                 : #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
   10821                 : #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
   10822                 : #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
   10823                 : #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
   10824                 : #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
   10825                 : 
   10826                 : /*
   10827                 : ** The WHERE clause processing routine has two halves.  The
   10828                 : ** first part does the start of the WHERE loop and the second
   10829                 : ** half does the tail of the WHERE loop.  An instance of
   10830                 : ** this structure is returned by the first half and passed
   10831                 : ** into the second half to give some continuity.
   10832                 : */
   10833                 : struct WhereInfo {
   10834                 :   Parse *pParse;       /* Parsing and code generating context */
   10835                 :   u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
   10836                 :   u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
   10837                 :   u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
   10838                 :   u8 eDistinct;
   10839                 :   SrcList *pTabList;             /* List of tables in the join */
   10840                 :   int iTop;                      /* The very beginning of the WHERE loop */
   10841                 :   int iContinue;                 /* Jump here to continue with next record */
   10842                 :   int iBreak;                    /* Jump here to break out of the loop */
   10843                 :   int nLevel;                    /* Number of nested loop */
   10844                 :   struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
   10845                 :   double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
   10846                 :   double nRowOut;                /* Estimated number of output rows */
   10847                 :   WhereLevel a[1];               /* Information about each nest loop in WHERE */
   10848                 : };
   10849                 : 
   10850                 : #define WHERE_DISTINCT_UNIQUE 1
   10851                 : #define WHERE_DISTINCT_ORDERED 2
   10852                 : 
   10853                 : /*
   10854                 : ** A NameContext defines a context in which to resolve table and column
   10855                 : ** names.  The context consists of a list of tables (the pSrcList) field and
   10856                 : ** a list of named expression (pEList).  The named expression list may
   10857                 : ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
   10858                 : ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
   10859                 : ** pEList corresponds to the result set of a SELECT and is NULL for
   10860                 : ** other statements.
   10861                 : **
   10862                 : ** NameContexts can be nested.  When resolving names, the inner-most 
   10863                 : ** context is searched first.  If no match is found, the next outer
   10864                 : ** context is checked.  If there is still no match, the next context
   10865                 : ** is checked.  This process continues until either a match is found
   10866                 : ** or all contexts are check.  When a match is found, the nRef member of
   10867                 : ** the context containing the match is incremented. 
   10868                 : **
   10869                 : ** Each subquery gets a new NameContext.  The pNext field points to the
   10870                 : ** NameContext in the parent query.  Thus the process of scanning the
   10871                 : ** NameContext list corresponds to searching through successively outer
   10872                 : ** subqueries looking for a match.
   10873                 : */
   10874                 : struct NameContext {
   10875                 :   Parse *pParse;       /* The parser */
   10876                 :   SrcList *pSrcList;   /* One or more tables used to resolve names */
   10877                 :   ExprList *pEList;    /* Optional list of named expressions */
   10878                 :   int nRef;            /* Number of names resolved by this context */
   10879                 :   int nErr;            /* Number of errors encountered while resolving names */
   10880                 :   u8 allowAgg;         /* Aggregate functions allowed here */
   10881                 :   u8 hasAgg;           /* True if aggregates are seen */
   10882                 :   u8 isCheck;          /* True if resolving names in a CHECK constraint */
   10883                 :   int nDepth;          /* Depth of subquery recursion. 1 for no recursion */
   10884                 :   AggInfo *pAggInfo;   /* Information about aggregates at this level */
   10885                 :   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
   10886                 : };
   10887                 : 
   10888                 : /*
   10889                 : ** An instance of the following structure contains all information
   10890                 : ** needed to generate code for a single SELECT statement.
   10891                 : **
   10892                 : ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
   10893                 : ** If there is a LIMIT clause, the parser sets nLimit to the value of the
   10894                 : ** limit and nOffset to the value of the offset (or 0 if there is not
   10895                 : ** offset).  But later on, nLimit and nOffset become the memory locations
   10896                 : ** in the VDBE that record the limit and offset counters.
   10897                 : **
   10898                 : ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
   10899                 : ** These addresses must be stored so that we can go back and fill in
   10900                 : ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
   10901                 : ** the number of columns in P2 can be computed at the same time
   10902                 : ** as the OP_OpenEphm instruction is coded because not
   10903                 : ** enough information about the compound query is known at that point.
   10904                 : ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
   10905                 : ** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
   10906                 : ** sequences for the ORDER BY clause.
   10907                 : */
   10908                 : struct Select {
   10909                 :   ExprList *pEList;      /* The fields of the result */
   10910                 :   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
   10911                 :   char affinity;         /* MakeRecord with this affinity for SRT_Set */
   10912                 :   u16 selFlags;          /* Various SF_* values */
   10913                 :   SrcList *pSrc;         /* The FROM clause */
   10914                 :   Expr *pWhere;          /* The WHERE clause */
   10915                 :   ExprList *pGroupBy;    /* The GROUP BY clause */
   10916                 :   Expr *pHaving;         /* The HAVING clause */
   10917                 :   ExprList *pOrderBy;    /* The ORDER BY clause */
   10918                 :   Select *pPrior;        /* Prior select in a compound select statement */
   10919                 :   Select *pNext;         /* Next select to the left in a compound */
   10920                 :   Select *pRightmost;    /* Right-most select in a compound select statement */
   10921                 :   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
   10922                 :   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
   10923                 :   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
   10924                 :   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
   10925                 :   double nSelectRow;     /* Estimated number of result rows */
   10926                 : };
   10927                 : 
   10928                 : /*
   10929                 : ** Allowed values for Select.selFlags.  The "SF" prefix stands for
   10930                 : ** "Select Flag".
   10931                 : */
   10932                 : #define SF_Distinct        0x01  /* Output should be DISTINCT */
   10933                 : #define SF_Resolved        0x02  /* Identifiers have been resolved */
   10934                 : #define SF_Aggregate       0x04  /* Contains aggregate functions */
   10935                 : #define SF_UsesEphemeral   0x08  /* Uses the OpenEphemeral opcode */
   10936                 : #define SF_Expanded        0x10  /* sqlite3SelectExpand() called on this */
   10937                 : #define SF_HasTypeInfo     0x20  /* FROM subqueries have Table metadata */
   10938                 : #define SF_UseSorter       0x40  /* Sort using a sorter */
   10939                 : 
   10940                 : 
   10941                 : /*
   10942                 : ** The results of a select can be distributed in several ways.  The
   10943                 : ** "SRT" prefix means "SELECT Result Type".
   10944                 : */
   10945                 : #define SRT_Union        1  /* Store result as keys in an index */
   10946                 : #define SRT_Except       2  /* Remove result from a UNION index */
   10947                 : #define SRT_Exists       3  /* Store 1 if the result is not empty */
   10948                 : #define SRT_Discard      4  /* Do not save the results anywhere */
   10949                 : 
   10950                 : /* The ORDER BY clause is ignored for all of the above */
   10951                 : #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
   10952                 : 
   10953                 : #define SRT_Output       5  /* Output each row of result */
   10954                 : #define SRT_Mem          6  /* Store result in a memory cell */
   10955                 : #define SRT_Set          7  /* Store results as keys in an index */
   10956                 : #define SRT_Table        8  /* Store result as data with an automatic rowid */
   10957                 : #define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
   10958                 : #define SRT_Coroutine   10  /* Generate a single row of result */
   10959                 : 
   10960                 : /*
   10961                 : ** A structure used to customize the behavior of sqlite3Select(). See
   10962                 : ** comments above sqlite3Select() for details.
   10963                 : */
   10964                 : typedef struct SelectDest SelectDest;
   10965                 : struct SelectDest {
   10966                 :   u8 eDest;         /* How to dispose of the results */
   10967                 :   u8 affinity;      /* Affinity used when eDest==SRT_Set */
   10968                 :   int iParm;        /* A parameter used by the eDest disposal method */
   10969                 :   int iMem;         /* Base register where results are written */
   10970                 :   int nMem;         /* Number of registers allocated */
   10971                 : };
   10972                 : 
   10973                 : /*
   10974                 : ** During code generation of statements that do inserts into AUTOINCREMENT 
   10975                 : ** tables, the following information is attached to the Table.u.autoInc.p
   10976                 : ** pointer of each autoincrement table to record some side information that
   10977                 : ** the code generator needs.  We have to keep per-table autoincrement
   10978                 : ** information in case inserts are down within triggers.  Triggers do not
   10979                 : ** normally coordinate their activities, but we do need to coordinate the
   10980                 : ** loading and saving of autoincrement information.
   10981                 : */
   10982                 : struct AutoincInfo {
   10983                 :   AutoincInfo *pNext;   /* Next info block in a list of them all */
   10984                 :   Table *pTab;          /* Table this info block refers to */
   10985                 :   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
   10986                 :   int regCtr;           /* Memory register holding the rowid counter */
   10987                 : };
   10988                 : 
   10989                 : /*
   10990                 : ** Size of the column cache
   10991                 : */
   10992                 : #ifndef SQLITE_N_COLCACHE
   10993                 : # define SQLITE_N_COLCACHE 10
   10994                 : #endif
   10995                 : 
   10996                 : /*
   10997                 : ** At least one instance of the following structure is created for each 
   10998                 : ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
   10999                 : ** statement. All such objects are stored in the linked list headed at
   11000                 : ** Parse.pTriggerPrg and deleted once statement compilation has been
   11001                 : ** completed.
   11002                 : **
   11003                 : ** A Vdbe sub-program that implements the body and WHEN clause of trigger
   11004                 : ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
   11005                 : ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
   11006                 : ** The Parse.pTriggerPrg list never contains two entries with the same
   11007                 : ** values for both pTrigger and orconf.
   11008                 : **
   11009                 : ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
   11010                 : ** accessed (or set to 0 for triggers fired as a result of INSERT 
   11011                 : ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
   11012                 : ** a mask of new.* columns used by the program.
   11013                 : */
   11014                 : struct TriggerPrg {
   11015                 :   Trigger *pTrigger;      /* Trigger this program was coded from */
   11016                 :   int orconf;             /* Default ON CONFLICT policy */
   11017                 :   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
   11018                 :   u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
   11019                 :   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
   11020                 : };
   11021                 : 
   11022                 : /*
   11023                 : ** The yDbMask datatype for the bitmask of all attached databases.
   11024                 : */
   11025                 : #if SQLITE_MAX_ATTACHED>30
   11026                 :   typedef sqlite3_uint64 yDbMask;
   11027                 : #else
   11028                 :   typedef unsigned int yDbMask;
   11029                 : #endif
   11030                 : 
   11031                 : /*
   11032                 : ** An SQL parser context.  A copy of this structure is passed through
   11033                 : ** the parser and down into all the parser action routine in order to
   11034                 : ** carry around information that is global to the entire parse.
   11035                 : **
   11036                 : ** The structure is divided into two parts.  When the parser and code
   11037                 : ** generate call themselves recursively, the first part of the structure
   11038                 : ** is constant but the second part is reset at the beginning and end of
   11039                 : ** each recursion.
   11040                 : **
   11041                 : ** The nTableLock and aTableLock variables are only used if the shared-cache 
   11042                 : ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
   11043                 : ** used to store the set of table-locks required by the statement being
   11044                 : ** compiled. Function sqlite3TableLock() is used to add entries to the
   11045                 : ** list.
   11046                 : */
   11047                 : struct Parse {
   11048                 :   sqlite3 *db;         /* The main database structure */
   11049                 :   int rc;              /* Return code from execution */
   11050                 :   char *zErrMsg;       /* An error message */
   11051                 :   Vdbe *pVdbe;         /* An engine for executing database bytecode */
   11052                 :   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
   11053                 :   u8 checkSchema;      /* Causes schema cookie check after an error */
   11054                 :   u8 nested;           /* Number of nested calls to the parser/code generator */
   11055                 :   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
   11056                 :   u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
   11057                 :   int aTempReg[8];     /* Holding area for temporary registers */
   11058                 :   int nRangeReg;       /* Size of the temporary register block */
   11059                 :   int iRangeReg;       /* First register in temporary register block */
   11060                 :   int nErr;            /* Number of errors seen */
   11061                 :   int nTab;            /* Number of previously allocated VDBE cursors */
   11062                 :   int nMem;            /* Number of memory cells used so far */
   11063                 :   int nSet;            /* Number of sets used so far */
   11064                 :   int nOnce;           /* Number of OP_Once instructions so far */
   11065                 :   int ckBase;          /* Base register of data during check constraints */
   11066                 :   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
   11067                 :   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
   11068                 :   u8 nColCache;        /* Number of entries in aColCache[] */
   11069                 :   u8 iColCache;        /* Next entry in aColCache[] to replace */
   11070                 :   struct yColCache {
   11071                 :     int iTable;           /* Table cursor number */
   11072                 :     int iColumn;          /* Table column number */
   11073                 :     u8 tempReg;           /* iReg is a temp register that needs to be freed */
   11074                 :     int iLevel;           /* Nesting level */
   11075                 :     int iReg;             /* Reg with value of this column. 0 means none. */
   11076                 :     int lru;              /* Least recently used entry has the smallest value */
   11077                 :   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
   11078                 :   yDbMask writeMask;   /* Start a write transaction on these databases */
   11079                 :   yDbMask cookieMask;  /* Bitmask of schema verified databases */
   11080                 :   u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
   11081                 :   u8 mayAbort;         /* True if statement may throw an ABORT exception */
   11082                 :   int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
   11083                 :   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
   11084                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   11085                 :   int nTableLock;        /* Number of locks in aTableLock */
   11086                 :   TableLock *aTableLock; /* Required table locks for shared-cache mode */
   11087                 : #endif
   11088                 :   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
   11089                 :   int regRoot;         /* Register holding root page number for new objects */
   11090                 :   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
   11091                 :   int nMaxArg;         /* Max args passed to user function by sub-program */
   11092                 : 
   11093                 :   /* Information used while coding trigger programs. */
   11094                 :   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
   11095                 :   Table *pTriggerTab;  /* Table triggers are being coded for */
   11096                 :   u32 oldmask;         /* Mask of old.* columns referenced */
   11097                 :   u32 newmask;         /* Mask of new.* columns referenced */
   11098                 :   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
   11099                 :   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
   11100                 :   u8 disableTriggers;  /* True to disable triggers */
   11101                 :   double nQueryLoop;   /* Estimated number of iterations of a query */
   11102                 : 
   11103                 :   /* Above is constant between recursions.  Below is reset before and after
   11104                 :   ** each recursion */
   11105                 : 
   11106                 :   int nVar;            /* Number of '?' variables seen in the SQL so far */
   11107                 :   int nzVar;           /* Number of available slots in azVar[] */
   11108                 :   char **azVar;        /* Pointers to names of parameters */
   11109                 :   Vdbe *pReprepare;    /* VM being reprepared (sqlite3Reprepare()) */
   11110                 :   int nAlias;          /* Number of aliased result set columns */
   11111                 :   int *aAlias;         /* Register used to hold aliased result */
   11112                 :   u8 explain;          /* True if the EXPLAIN flag is found on the query */
   11113                 :   Token sNameToken;    /* Token with unqualified schema object name */
   11114                 :   Token sLastToken;    /* The last token parsed */
   11115                 :   const char *zTail;   /* All SQL text past the last semicolon parsed */
   11116                 :   Table *pNewTable;    /* A table being constructed by CREATE TABLE */
   11117                 :   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
   11118                 :   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
   11119                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   11120                 :   Token sArg;                /* Complete text of a module argument */
   11121                 :   u8 declareVtab;            /* True if inside sqlite3_declare_vtab() */
   11122                 :   int nVtabLock;             /* Number of virtual tables to lock */
   11123                 :   Table **apVtabLock;        /* Pointer to virtual tables needing locking */
   11124                 : #endif
   11125                 :   int nHeight;            /* Expression tree height of current sub-select */
   11126                 :   Table *pZombieTab;      /* List of Table objects to delete after code gen */
   11127                 :   TriggerPrg *pTriggerPrg;    /* Linked list of coded triggers */
   11128                 : 
   11129                 : #ifndef SQLITE_OMIT_EXPLAIN
   11130                 :   int iSelectId;
   11131                 :   int iNextSelectId;
   11132                 : #endif
   11133                 : };
   11134                 : 
   11135                 : #ifdef SQLITE_OMIT_VIRTUALTABLE
   11136                 :   #define IN_DECLARE_VTAB 0
   11137                 : #else
   11138                 :   #define IN_DECLARE_VTAB (pParse->declareVtab)
   11139                 : #endif
   11140                 : 
   11141                 : /*
   11142                 : ** An instance of the following structure can be declared on a stack and used
   11143                 : ** to save the Parse.zAuthContext value so that it can be restored later.
   11144                 : */
   11145                 : struct AuthContext {
   11146                 :   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
   11147                 :   Parse *pParse;              /* The Parse structure */
   11148                 : };
   11149                 : 
   11150                 : /*
   11151                 : ** Bitfield flags for P5 value in OP_Insert and OP_Delete
   11152                 : */
   11153                 : #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
   11154                 : #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
   11155                 : #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
   11156                 : #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
   11157                 : #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
   11158                 : #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
   11159                 : 
   11160                 : /*
   11161                 :  * Each trigger present in the database schema is stored as an instance of
   11162                 :  * struct Trigger. 
   11163                 :  *
   11164                 :  * Pointers to instances of struct Trigger are stored in two ways.
   11165                 :  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the 
   11166                 :  *    database). This allows Trigger structures to be retrieved by name.
   11167                 :  * 2. All triggers associated with a single table form a linked list, using the
   11168                 :  *    pNext member of struct Trigger. A pointer to the first element of the
   11169                 :  *    linked list is stored as the "pTrigger" member of the associated
   11170                 :  *    struct Table.
   11171                 :  *
   11172                 :  * The "step_list" member points to the first element of a linked list
   11173                 :  * containing the SQL statements specified as the trigger program.
   11174                 :  */
   11175                 : struct Trigger {
   11176                 :   char *zName;            /* The name of the trigger                        */
   11177                 :   char *table;            /* The table or view to which the trigger applies */
   11178                 :   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
   11179                 :   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
   11180                 :   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
   11181                 :   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
   11182                 :                              the <column-list> is stored here */
   11183                 :   Schema *pSchema;        /* Schema containing the trigger */
   11184                 :   Schema *pTabSchema;     /* Schema containing the table */
   11185                 :   TriggerStep *step_list; /* Link list of trigger program steps             */
   11186                 :   Trigger *pNext;         /* Next trigger associated with the table */
   11187                 : };
   11188                 : 
   11189                 : /*
   11190                 : ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
   11191                 : ** determine which. 
   11192                 : **
   11193                 : ** If there are multiple triggers, you might of some BEFORE and some AFTER.
   11194                 : ** In that cases, the constants below can be ORed together.
   11195                 : */
   11196                 : #define TRIGGER_BEFORE  1
   11197                 : #define TRIGGER_AFTER   2
   11198                 : 
   11199                 : /*
   11200                 :  * An instance of struct TriggerStep is used to store a single SQL statement
   11201                 :  * that is a part of a trigger-program. 
   11202                 :  *
   11203                 :  * Instances of struct TriggerStep are stored in a singly linked list (linked
   11204                 :  * using the "pNext" member) referenced by the "step_list" member of the 
   11205                 :  * associated struct Trigger instance. The first element of the linked list is
   11206                 :  * the first step of the trigger-program.
   11207                 :  * 
   11208                 :  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
   11209                 :  * "SELECT" statement. The meanings of the other members is determined by the 
   11210                 :  * value of "op" as follows:
   11211                 :  *
   11212                 :  * (op == TK_INSERT)
   11213                 :  * orconf    -> stores the ON CONFLICT algorithm
   11214                 :  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
   11215                 :  *              this stores a pointer to the SELECT statement. Otherwise NULL.
   11216                 :  * target    -> A token holding the quoted name of the table to insert into.
   11217                 :  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
   11218                 :  *              this stores values to be inserted. Otherwise NULL.
   11219                 :  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
   11220                 :  *              statement, then this stores the column-names to be
   11221                 :  *              inserted into.
   11222                 :  *
   11223                 :  * (op == TK_DELETE)
   11224                 :  * target    -> A token holding the quoted name of the table to delete from.
   11225                 :  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
   11226                 :  *              Otherwise NULL.
   11227                 :  * 
   11228                 :  * (op == TK_UPDATE)
   11229                 :  * target    -> A token holding the quoted name of the table to update rows of.
   11230                 :  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
   11231                 :  *              Otherwise NULL.
   11232                 :  * pExprList -> A list of the columns to update and the expressions to update
   11233                 :  *              them to. See sqlite3Update() documentation of "pChanges"
   11234                 :  *              argument.
   11235                 :  * 
   11236                 :  */
   11237                 : struct TriggerStep {
   11238                 :   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
   11239                 :   u8 orconf;           /* OE_Rollback etc. */
   11240                 :   Trigger *pTrig;      /* The trigger that this step is a part of */
   11241                 :   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
   11242                 :   Token target;        /* Target table for DELETE, UPDATE, INSERT */
   11243                 :   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
   11244                 :   ExprList *pExprList; /* SET clause for UPDATE.  VALUES clause for INSERT */
   11245                 :   IdList *pIdList;     /* Column names for INSERT */
   11246                 :   TriggerStep *pNext;  /* Next in the link-list */
   11247                 :   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
   11248                 : };
   11249                 : 
   11250                 : /*
   11251                 : ** The following structure contains information used by the sqliteFix...
   11252                 : ** routines as they walk the parse tree to make database references
   11253                 : ** explicit.  
   11254                 : */
   11255                 : typedef struct DbFixer DbFixer;
   11256                 : struct DbFixer {
   11257                 :   Parse *pParse;      /* The parsing context.  Error messages written here */
   11258                 :   const char *zDb;    /* Make sure all objects are contained in this database */
   11259                 :   const char *zType;  /* Type of the container - used for error messages */
   11260                 :   const Token *pName; /* Name of the container - used for error messages */
   11261                 : };
   11262                 : 
   11263                 : /*
   11264                 : ** An objected used to accumulate the text of a string where we
   11265                 : ** do not necessarily know how big the string will be in the end.
   11266                 : */
   11267                 : struct StrAccum {
   11268                 :   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
   11269                 :   char *zBase;         /* A base allocation.  Not from malloc. */
   11270                 :   char *zText;         /* The string collected so far */
   11271                 :   int  nChar;          /* Length of the string so far */
   11272                 :   int  nAlloc;         /* Amount of space allocated in zText */
   11273                 :   int  mxAlloc;        /* Maximum allowed string length */
   11274                 :   u8   mallocFailed;   /* Becomes true if any memory allocation fails */
   11275                 :   u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
   11276                 :   u8   tooBig;         /* Becomes true if string size exceeds limits */
   11277                 : };
   11278                 : 
   11279                 : /*
   11280                 : ** A pointer to this structure is used to communicate information
   11281                 : ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
   11282                 : */
   11283                 : typedef struct {
   11284                 :   sqlite3 *db;        /* The database being initialized */
   11285                 :   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
   11286                 :   char **pzErrMsg;    /* Error message stored here */
   11287                 :   int rc;             /* Result code stored here */
   11288                 : } InitData;
   11289                 : 
   11290                 : /*
   11291                 : ** Structure containing global configuration data for the SQLite library.
   11292                 : **
   11293                 : ** This structure also contains some state information.
   11294                 : */
   11295                 : struct Sqlite3Config {
   11296                 :   int bMemstat;                     /* True to enable memory status */
   11297                 :   int bCoreMutex;                   /* True to enable core mutexing */
   11298                 :   int bFullMutex;                   /* True to enable full mutexing */
   11299                 :   int bOpenUri;                     /* True to interpret filenames as URIs */
   11300                 :   int mxStrlen;                     /* Maximum string length */
   11301                 :   int szLookaside;                  /* Default lookaside buffer size */
   11302                 :   int nLookaside;                   /* Default lookaside buffer count */
   11303                 :   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
   11304                 :   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
   11305                 :   sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
   11306                 :   void *pHeap;                      /* Heap storage space */
   11307                 :   int nHeap;                        /* Size of pHeap[] */
   11308                 :   int mnReq, mxReq;                 /* Min and max heap requests sizes */
   11309                 :   void *pScratch;                   /* Scratch memory */
   11310                 :   int szScratch;                    /* Size of each scratch buffer */
   11311                 :   int nScratch;                     /* Number of scratch buffers */
   11312                 :   void *pPage;                      /* Page cache memory */
   11313                 :   int szPage;                       /* Size of each page in pPage[] */
   11314                 :   int nPage;                        /* Number of pages in pPage[] */
   11315                 :   int mxParserStack;                /* maximum depth of the parser stack */
   11316                 :   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
   11317                 :   /* The above might be initialized to non-zero.  The following need to always
   11318                 :   ** initially be zero, however. */
   11319                 :   int isInit;                       /* True after initialization has finished */
   11320                 :   int inProgress;                   /* True while initialization in progress */
   11321                 :   int isMutexInit;                  /* True after mutexes are initialized */
   11322                 :   int isMallocInit;                 /* True after malloc is initialized */
   11323                 :   int isPCacheInit;                 /* True after malloc is initialized */
   11324                 :   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
   11325                 :   int nRefInitMutex;                /* Number of users of pInitMutex */
   11326                 :   void (*xLog)(void*,int,const char*); /* Function for logging */
   11327                 :   void *pLogArg;                       /* First argument to xLog() */
   11328                 :   int bLocaltimeFault;              /* True to fail localtime() calls */
   11329                 : };
   11330                 : 
   11331                 : /*
   11332                 : ** Context pointer passed down through the tree-walk.
   11333                 : */
   11334                 : struct Walker {
   11335                 :   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
   11336                 :   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
   11337                 :   Parse *pParse;                            /* Parser context.  */
   11338                 :   union {                                   /* Extra data for callback */
   11339                 :     NameContext *pNC;                          /* Naming context */
   11340                 :     int i;                                     /* Integer value */
   11341                 :   } u;
   11342                 : };
   11343                 : 
   11344                 : /* Forward declarations */
   11345                 : SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
   11346                 : SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
   11347                 : SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
   11348                 : SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
   11349                 : SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
   11350                 : 
   11351                 : /*
   11352                 : ** Return code from the parse-tree walking primitives and their
   11353                 : ** callbacks.
   11354                 : */
   11355                 : #define WRC_Continue    0   /* Continue down into children */
   11356                 : #define WRC_Prune       1   /* Omit children but continue walking siblings */
   11357                 : #define WRC_Abort       2   /* Abandon the tree walk */
   11358                 : 
   11359                 : /*
   11360                 : ** Assuming zIn points to the first byte of a UTF-8 character,
   11361                 : ** advance zIn to point to the first byte of the next UTF-8 character.
   11362                 : */
   11363                 : #define SQLITE_SKIP_UTF8(zIn) {                        \
   11364                 :   if( (*(zIn++))>=0xc0 ){                              \
   11365                 :     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
   11366                 :   }                                                    \
   11367                 : }
   11368                 : 
   11369                 : /*
   11370                 : ** The SQLITE_*_BKPT macros are substitutes for the error codes with
   11371                 : ** the same name but without the _BKPT suffix.  These macros invoke
   11372                 : ** routines that report the line-number on which the error originated
   11373                 : ** using sqlite3_log().  The routines also provide a convenient place
   11374                 : ** to set a debugger breakpoint.
   11375                 : */
   11376                 : SQLITE_PRIVATE int sqlite3CorruptError(int);
   11377                 : SQLITE_PRIVATE int sqlite3MisuseError(int);
   11378                 : SQLITE_PRIVATE int sqlite3CantopenError(int);
   11379                 : #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
   11380                 : #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
   11381                 : #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
   11382                 : 
   11383                 : 
   11384                 : /*
   11385                 : ** FTS4 is really an extension for FTS3.  It is enabled using the
   11386                 : ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
   11387                 : ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
   11388                 : */
   11389                 : #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
   11390                 : # define SQLITE_ENABLE_FTS3
   11391                 : #endif
   11392                 : 
   11393                 : /*
   11394                 : ** The ctype.h header is needed for non-ASCII systems.  It is also
   11395                 : ** needed by FTS3 when FTS3 is included in the amalgamation.
   11396                 : */
   11397                 : #if !defined(SQLITE_ASCII) || \
   11398                 :     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
   11399                 : # include <ctype.h>
   11400                 : #endif
   11401                 : 
   11402                 : /*
   11403                 : ** The following macros mimic the standard library functions toupper(),
   11404                 : ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
   11405                 : ** sqlite versions only work for ASCII characters, regardless of locale.
   11406                 : */
   11407                 : #ifdef SQLITE_ASCII
   11408                 : # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
   11409                 : # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
   11410                 : # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
   11411                 : # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
   11412                 : # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
   11413                 : # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
   11414                 : # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
   11415                 : #else
   11416                 : # define sqlite3Toupper(x)   toupper((unsigned char)(x))
   11417                 : # define sqlite3Isspace(x)   isspace((unsigned char)(x))
   11418                 : # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
   11419                 : # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
   11420                 : # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
   11421                 : # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
   11422                 : # define sqlite3Tolower(x)   tolower((unsigned char)(x))
   11423                 : #endif
   11424                 : 
   11425                 : /*
   11426                 : ** Internal function prototypes
   11427                 : */
   11428                 : SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
   11429                 : SQLITE_PRIVATE int sqlite3Strlen30(const char*);
   11430                 : #define sqlite3StrNICmp sqlite3_strnicmp
   11431                 : 
   11432                 : SQLITE_PRIVATE int sqlite3MallocInit(void);
   11433                 : SQLITE_PRIVATE void sqlite3MallocEnd(void);
   11434                 : SQLITE_PRIVATE void *sqlite3Malloc(int);
   11435                 : SQLITE_PRIVATE void *sqlite3MallocZero(int);
   11436                 : SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
   11437                 : SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
   11438                 : SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
   11439                 : SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
   11440                 : SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
   11441                 : SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
   11442                 : SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
   11443                 : SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
   11444                 : SQLITE_PRIVATE int sqlite3MallocSize(void*);
   11445                 : SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
   11446                 : SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
   11447                 : SQLITE_PRIVATE void sqlite3ScratchFree(void*);
   11448                 : SQLITE_PRIVATE void *sqlite3PageMalloc(int);
   11449                 : SQLITE_PRIVATE void sqlite3PageFree(void*);
   11450                 : SQLITE_PRIVATE void sqlite3MemSetDefault(void);
   11451                 : SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
   11452                 : SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
   11453                 : 
   11454                 : /*
   11455                 : ** On systems with ample stack space and that support alloca(), make
   11456                 : ** use of alloca() to obtain space for large automatic objects.  By default,
   11457                 : ** obtain space from malloc().
   11458                 : **
   11459                 : ** The alloca() routine never returns NULL.  This will cause code paths
   11460                 : ** that deal with sqlite3StackAlloc() failures to be unreachable.
   11461                 : */
   11462                 : #ifdef SQLITE_USE_ALLOCA
   11463                 : # define sqlite3StackAllocRaw(D,N)   alloca(N)
   11464                 : # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
   11465                 : # define sqlite3StackFree(D,P)       
   11466                 : #else
   11467                 : # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
   11468                 : # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
   11469                 : # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
   11470                 : #endif
   11471                 : 
   11472                 : #ifdef SQLITE_ENABLE_MEMSYS3
   11473                 : SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
   11474                 : #endif
   11475                 : #ifdef SQLITE_ENABLE_MEMSYS5
   11476                 : SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
   11477                 : #endif
   11478                 : 
   11479                 : 
   11480                 : #ifndef SQLITE_MUTEX_OMIT
   11481                 : SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
   11482                 : SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
   11483                 : SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
   11484                 : SQLITE_PRIVATE   int sqlite3MutexInit(void);
   11485                 : SQLITE_PRIVATE   int sqlite3MutexEnd(void);
   11486                 : #endif
   11487                 : 
   11488                 : SQLITE_PRIVATE int sqlite3StatusValue(int);
   11489                 : SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
   11490                 : SQLITE_PRIVATE void sqlite3StatusSet(int, int);
   11491                 : 
   11492                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   11493                 : SQLITE_PRIVATE   int sqlite3IsNaN(double);
   11494                 : #else
   11495                 : # define sqlite3IsNaN(X)  0
   11496                 : #endif
   11497                 : 
   11498                 : SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
   11499                 : #ifndef SQLITE_OMIT_TRACE
   11500                 : SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
   11501                 : #endif
   11502                 : SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
   11503                 : SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
   11504                 : SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
   11505                 : #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
   11506                 : SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
   11507                 : #endif
   11508                 : #if defined(SQLITE_TEST)
   11509                 : SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
   11510                 : #endif
   11511                 : 
   11512                 : /* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
   11513                 : #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
   11514                 : SQLITE_PRIVATE   void sqlite3ExplainBegin(Vdbe*);
   11515                 : SQLITE_PRIVATE   void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
   11516                 : SQLITE_PRIVATE   void sqlite3ExplainNL(Vdbe*);
   11517                 : SQLITE_PRIVATE   void sqlite3ExplainPush(Vdbe*);
   11518                 : SQLITE_PRIVATE   void sqlite3ExplainPop(Vdbe*);
   11519                 : SQLITE_PRIVATE   void sqlite3ExplainFinish(Vdbe*);
   11520                 : SQLITE_PRIVATE   void sqlite3ExplainSelect(Vdbe*, Select*);
   11521                 : SQLITE_PRIVATE   void sqlite3ExplainExpr(Vdbe*, Expr*);
   11522                 : SQLITE_PRIVATE   void sqlite3ExplainExprList(Vdbe*, ExprList*);
   11523                 : SQLITE_PRIVATE   const char *sqlite3VdbeExplanation(Vdbe*);
   11524                 : #else
   11525                 : # define sqlite3ExplainBegin(X)
   11526                 : # define sqlite3ExplainSelect(A,B)
   11527                 : # define sqlite3ExplainExpr(A,B)
   11528                 : # define sqlite3ExplainExprList(A,B)
   11529                 : # define sqlite3ExplainFinish(X)
   11530                 : # define sqlite3VdbeExplanation(X) 0
   11531                 : #endif
   11532                 : 
   11533                 : 
   11534                 : SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
   11535                 : SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
   11536                 : SQLITE_PRIVATE int sqlite3Dequote(char*);
   11537                 : SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
   11538                 : SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
   11539                 : SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
   11540                 : SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
   11541                 : SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
   11542                 : SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
   11543                 : SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
   11544                 : SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
   11545                 : SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
   11546                 : SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
   11547                 : SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
   11548                 : SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
   11549                 : SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
   11550                 : SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
   11551                 : SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
   11552                 : SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
   11553                 : SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
   11554                 : SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
   11555                 : SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
   11556                 : SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
   11557                 : SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
   11558                 : SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
   11559                 : SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
   11560                 : SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
   11561                 : SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
   11562                 : SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
   11563                 : SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
   11564                 : SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
   11565                 : SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
   11566                 : SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
   11567                 : SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
   11568                 : SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
   11569                 : SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
   11570                 : SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
   11571                 : SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
   11572                 : SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
   11573                 : SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
   11574                 : SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
   11575                 :                     sqlite3_vfs**,char**,char **);
   11576                 : SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
   11577                 : 
   11578                 : SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
   11579                 : SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
   11580                 : SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
   11581                 : SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
   11582                 : SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
   11583                 : SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
   11584                 : SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
   11585                 : 
   11586                 : SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
   11587                 : SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
   11588                 : SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
   11589                 : SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
   11590                 : SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
   11591                 : 
   11592                 : SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
   11593                 : 
   11594                 : #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
   11595                 : SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
   11596                 : #else
   11597                 : # define sqlite3ViewGetColumnNames(A,B) 0
   11598                 : #endif
   11599                 : 
   11600                 : SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
   11601                 : SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
   11602                 : SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
   11603                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   11604                 : SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
   11605                 : SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
   11606                 : #else
   11607                 : # define sqlite3AutoincrementBegin(X)
   11608                 : # define sqlite3AutoincrementEnd(X)
   11609                 : #endif
   11610                 : SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
   11611                 : SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
   11612                 : SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
   11613                 : SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
   11614                 : SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
   11615                 : SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
   11616                 : SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
   11617                 :                                       Token*, Select*, Expr*, IdList*);
   11618                 : SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
   11619                 : SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
   11620                 : SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
   11621                 : SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
   11622                 : SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
   11623                 : SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
   11624                 : SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
   11625                 :                         Token*, int, int);
   11626                 : SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
   11627                 : SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
   11628                 : SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
   11629                 :                          Expr*,ExprList*,int,Expr*,Expr*);
   11630                 : SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
   11631                 : SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
   11632                 : SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
   11633                 : SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
   11634                 : #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
   11635                 : SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
   11636                 : #endif
   11637                 : SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
   11638                 : SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
   11639                 : SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**,ExprList*,u16);
   11640                 : SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
   11641                 : SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
   11642                 : SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
   11643                 : SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
   11644                 : SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
   11645                 : SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
   11646                 : SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
   11647                 : SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
   11648                 : SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
   11649                 : SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
   11650                 : SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
   11651                 : SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
   11652                 : SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
   11653                 : SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
   11654                 : SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
   11655                 : SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
   11656                 : SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
   11657                 : SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
   11658                 : SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
   11659                 : SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
   11660                 : SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
   11661                 : SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
   11662                 : SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
   11663                 : SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
   11664                 : SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
   11665                 : SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
   11666                 : SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
   11667                 : SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
   11668                 : SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*);
   11669                 : SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
   11670                 : SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
   11671                 : SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
   11672                 : SQLITE_PRIVATE void sqlite3PrngSaveState(void);
   11673                 : SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
   11674                 : SQLITE_PRIVATE void sqlite3PrngResetState(void);
   11675                 : SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
   11676                 : SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
   11677                 : SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
   11678                 : SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
   11679                 : SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
   11680                 : SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
   11681                 : SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
   11682                 : SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
   11683                 : SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
   11684                 : SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
   11685                 : SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
   11686                 : SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
   11687                 : SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
   11688                 : SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
   11689                 : SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
   11690                 : SQLITE_PRIVATE int sqlite3IsRowid(const char*);
   11691                 : SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
   11692                 : SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
   11693                 : SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
   11694                 : SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
   11695                 :                                      int*,int,int,int,int,int*);
   11696                 : SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
   11697                 : SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
   11698                 : SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
   11699                 : SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
   11700                 : SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
   11701                 : SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
   11702                 : SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
   11703                 : SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
   11704                 : SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
   11705                 : SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
   11706                 : SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
   11707                 : SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
   11708                 : SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
   11709                 : SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
   11710                 : SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
   11711                 : SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
   11712                 : SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
   11713                 : SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
   11714                 : SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
   11715                 : 
   11716                 : #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   11717                 : SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
   11718                 : #endif
   11719                 : 
   11720                 : #ifndef SQLITE_OMIT_TRIGGER
   11721                 : SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
   11722                 :                            Expr*,int, int);
   11723                 : SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
   11724                 : SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
   11725                 : SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
   11726                 : SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
   11727                 : SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
   11728                 : SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
   11729                 :                             int, int, int);
   11730                 : SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
   11731                 :   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
   11732                 : SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
   11733                 : SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
   11734                 : SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
   11735                 :                                         ExprList*,Select*,u8);
   11736                 : SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
   11737                 : SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
   11738                 : SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
   11739                 : SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
   11740                 : SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
   11741                 : # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
   11742                 : #else
   11743                 : # define sqlite3TriggersExist(B,C,D,E,F) 0
   11744                 : # define sqlite3DeleteTrigger(A,B)
   11745                 : # define sqlite3DropTriggerPtr(A,B)
   11746                 : # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
   11747                 : # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
   11748                 : # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
   11749                 : # define sqlite3TriggerList(X, Y) 0
   11750                 : # define sqlite3ParseToplevel(p) p
   11751                 : # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
   11752                 : #endif
   11753                 : 
   11754                 : SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
   11755                 : SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
   11756                 : SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
   11757                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   11758                 : SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
   11759                 : SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
   11760                 : SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
   11761                 : SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
   11762                 : SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
   11763                 : #else
   11764                 : # define sqlite3AuthRead(a,b,c,d)
   11765                 : # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
   11766                 : # define sqlite3AuthContextPush(a,b,c)
   11767                 : # define sqlite3AuthContextPop(a)  ((void)(a))
   11768                 : #endif
   11769                 : SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
   11770                 : SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
   11771                 : SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
   11772                 : SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
   11773                 : SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
   11774                 : SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
   11775                 : SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
   11776                 : SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
   11777                 : SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
   11778                 : SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
   11779                 : SQLITE_PRIVATE int sqlite3Atoi(const char*);
   11780                 : SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
   11781                 : SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
   11782                 : SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8*, const u8**);
   11783                 : 
   11784                 : /*
   11785                 : ** Routines to read and write variable-length integers.  These used to
   11786                 : ** be defined locally, but now we use the varint routines in the util.c
   11787                 : ** file.  Code should use the MACRO forms below, as the Varint32 versions
   11788                 : ** are coded to assume the single byte case is already handled (which 
   11789                 : ** the MACRO form does).
   11790                 : */
   11791                 : SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
   11792                 : SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
   11793                 : SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
   11794                 : SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
   11795                 : SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
   11796                 : 
   11797                 : /*
   11798                 : ** The header of a record consists of a sequence variable-length integers.
   11799                 : ** These integers are almost always small and are encoded as a single byte.
   11800                 : ** The following macros take advantage this fact to provide a fast encode
   11801                 : ** and decode of the integers in a record header.  It is faster for the common
   11802                 : ** case where the integer is a single byte.  It is a little slower when the
   11803                 : ** integer is two or more bytes.  But overall it is faster.
   11804                 : **
   11805                 : ** The following expressions are equivalent:
   11806                 : **
   11807                 : **     x = sqlite3GetVarint32( A, &B );
   11808                 : **     x = sqlite3PutVarint32( A, B );
   11809                 : **
   11810                 : **     x = getVarint32( A, B );
   11811                 : **     x = putVarint32( A, B );
   11812                 : **
   11813                 : */
   11814                 : #define getVarint32(A,B)  (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
   11815                 : #define putVarint32(A,B)  (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
   11816                 : #define getVarint    sqlite3GetVarint
   11817                 : #define putVarint    sqlite3PutVarint
   11818                 : 
   11819                 : 
   11820                 : SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
   11821                 : SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
   11822                 : SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
   11823                 : SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
   11824                 : SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
   11825                 : SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
   11826                 : SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
   11827                 : SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
   11828                 : SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
   11829                 : SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
   11830                 : SQLITE_PRIVATE const char *sqlite3ErrStr(int);
   11831                 : SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
   11832                 : SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
   11833                 : SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
   11834                 : SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
   11835                 : SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr*, CollSeq*);
   11836                 : SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*);
   11837                 : SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
   11838                 : SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
   11839                 : SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
   11840                 : SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
   11841                 : SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
   11842                 : SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
   11843                 : SQLITE_PRIVATE int sqlite3AbsInt32(int);
   11844                 : #ifdef SQLITE_ENABLE_8_3_NAMES
   11845                 : SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
   11846                 : #else
   11847                 : # define sqlite3FileSuffix3(X,Y)
   11848                 : #endif
   11849                 : SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z);
   11850                 : 
   11851                 : SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
   11852                 : SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
   11853                 : SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, 
   11854                 :                         void(*)(void*));
   11855                 : SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
   11856                 : SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
   11857                 : SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
   11858                 : #ifdef SQLITE_ENABLE_STAT3
   11859                 : SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
   11860                 : #endif
   11861                 : SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
   11862                 : SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
   11863                 : #ifndef SQLITE_AMALGAMATION
   11864                 : SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
   11865                 : SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
   11866                 : SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
   11867                 : SQLITE_PRIVATE const Token sqlite3IntTokens[];
   11868                 : SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
   11869                 : SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
   11870                 : #ifndef SQLITE_OMIT_WSD
   11871                 : SQLITE_PRIVATE int sqlite3PendingByte;
   11872                 : #endif
   11873                 : #endif
   11874                 : SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
   11875                 : SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
   11876                 : SQLITE_PRIVATE void sqlite3AlterFunctions(void);
   11877                 : SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
   11878                 : SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
   11879                 : SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
   11880                 : SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
   11881                 : SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
   11882                 : SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
   11883                 : SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
   11884                 : SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
   11885                 : SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
   11886                 : SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
   11887                 : SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
   11888                 : SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
   11889                 : SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
   11890                 : SQLITE_PRIVATE char sqlite3AffinityType(const char*);
   11891                 : SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
   11892                 : SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
   11893                 : SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
   11894                 : SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
   11895                 : SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
   11896                 : SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
   11897                 : SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
   11898                 : SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
   11899                 : SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
   11900                 : SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
   11901                 : SQLITE_PRIVATE void sqlite3SchemaClear(void *);
   11902                 : SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
   11903                 : SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
   11904                 : SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
   11905                 : SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *, 
   11906                 :   void (*)(sqlite3_context*,int,sqlite3_value **),
   11907                 :   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
   11908                 :   FuncDestructor *pDestructor
   11909                 : );
   11910                 : SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
   11911                 : SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
   11912                 : 
   11913                 : SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
   11914                 : SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
   11915                 : SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
   11916                 : SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
   11917                 : SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
   11918                 : SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
   11919                 : SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
   11920                 : 
   11921                 : SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
   11922                 : SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
   11923                 : 
   11924                 : /*
   11925                 : ** The interface to the LEMON-generated parser
   11926                 : */
   11927                 : SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
   11928                 : SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
   11929                 : SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
   11930                 : #ifdef YYTRACKMAXSTACKDEPTH
   11931                 : SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
   11932                 : #endif
   11933                 : 
   11934                 : SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
   11935                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
   11936                 : SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
   11937                 : #else
   11938                 : # define sqlite3CloseExtensions(X)
   11939                 : #endif
   11940                 : 
   11941                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   11942                 : SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
   11943                 : #else
   11944                 :   #define sqlite3TableLock(v,w,x,y,z)
   11945                 : #endif
   11946                 : 
   11947                 : #ifdef SQLITE_TEST
   11948                 : SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
   11949                 : #endif
   11950                 : 
   11951                 : #ifdef SQLITE_OMIT_VIRTUALTABLE
   11952                 : #  define sqlite3VtabClear(Y)
   11953                 : #  define sqlite3VtabSync(X,Y) SQLITE_OK
   11954                 : #  define sqlite3VtabRollback(X)
   11955                 : #  define sqlite3VtabCommit(X)
   11956                 : #  define sqlite3VtabInSync(db) 0
   11957                 : #  define sqlite3VtabLock(X) 
   11958                 : #  define sqlite3VtabUnlock(X)
   11959                 : #  define sqlite3VtabUnlockList(X)
   11960                 : #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
   11961                 : #  define sqlite3GetVTable(X,Y)  ((VTable*)0)
   11962                 : #else
   11963                 : SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
   11964                 : SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, char **);
   11965                 : SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
   11966                 : SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
   11967                 : SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
   11968                 : SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
   11969                 : SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
   11970                 : SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
   11971                 : SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
   11972                 : #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
   11973                 : #endif
   11974                 : SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
   11975                 : SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
   11976                 : SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
   11977                 : SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
   11978                 : SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
   11979                 : SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
   11980                 : SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
   11981                 : SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
   11982                 : SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
   11983                 : SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
   11984                 : SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
   11985                 : SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
   11986                 : SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
   11987                 : SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
   11988                 : SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
   11989                 : SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
   11990                 : SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
   11991                 : SQLITE_PRIVATE const char *sqlite3JournalModename(int);
   11992                 : SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
   11993                 : SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
   11994                 : 
   11995                 : /* Declarations for functions in fkey.c. All of these are replaced by
   11996                 : ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
   11997                 : ** key functionality is available. If OMIT_TRIGGER is defined but
   11998                 : ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
   11999                 : ** this case foreign keys are parsed, but no other functionality is 
   12000                 : ** provided (enforcement of FK constraints requires the triggers sub-system).
   12001                 : */
   12002                 : #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   12003                 : SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int);
   12004                 : SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
   12005                 : SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int);
   12006                 : SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
   12007                 : SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
   12008                 : SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
   12009                 : #else
   12010                 :   #define sqlite3FkActions(a,b,c,d)
   12011                 :   #define sqlite3FkCheck(a,b,c,d)
   12012                 :   #define sqlite3FkDropTable(a,b,c)
   12013                 :   #define sqlite3FkOldmask(a,b)      0
   12014                 :   #define sqlite3FkRequired(a,b,c,d) 0
   12015                 : #endif
   12016                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   12017                 : SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
   12018                 : #else
   12019                 :   #define sqlite3FkDelete(a,b)
   12020                 : #endif
   12021                 : 
   12022                 : 
   12023                 : /*
   12024                 : ** Available fault injectors.  Should be numbered beginning with 0.
   12025                 : */
   12026                 : #define SQLITE_FAULTINJECTOR_MALLOC     0
   12027                 : #define SQLITE_FAULTINJECTOR_COUNT      1
   12028                 : 
   12029                 : /*
   12030                 : ** The interface to the code in fault.c used for identifying "benign"
   12031                 : ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
   12032                 : ** is not defined.
   12033                 : */
   12034                 : #ifndef SQLITE_OMIT_BUILTIN_TEST
   12035                 : SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
   12036                 : SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
   12037                 : #else
   12038                 :   #define sqlite3BeginBenignMalloc()
   12039                 :   #define sqlite3EndBenignMalloc()
   12040                 : #endif
   12041                 : 
   12042                 : #define IN_INDEX_ROWID           1
   12043                 : #define IN_INDEX_EPH             2
   12044                 : #define IN_INDEX_INDEX           3
   12045                 : SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
   12046                 : 
   12047                 : #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   12048                 : SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
   12049                 : SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
   12050                 : SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
   12051                 : #else
   12052                 :   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
   12053                 : #endif
   12054                 : 
   12055                 : SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
   12056                 : SQLITE_PRIVATE int sqlite3MemJournalSize(void);
   12057                 : SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
   12058                 : 
   12059                 : #if SQLITE_MAX_EXPR_DEPTH>0
   12060                 : SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
   12061                 : SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
   12062                 : SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
   12063                 : #else
   12064                 :   #define sqlite3ExprSetHeight(x,y)
   12065                 :   #define sqlite3SelectExprHeight(x) 0
   12066                 :   #define sqlite3ExprCheckHeight(x,y)
   12067                 : #endif
   12068                 : 
   12069                 : SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
   12070                 : SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
   12071                 : 
   12072                 : #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
   12073                 : SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
   12074                 : SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
   12075                 : SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
   12076                 : #else
   12077                 :   #define sqlite3ConnectionBlocked(x,y)
   12078                 :   #define sqlite3ConnectionUnlocked(x)
   12079                 :   #define sqlite3ConnectionClosed(x)
   12080                 : #endif
   12081                 : 
   12082                 : #ifdef SQLITE_DEBUG
   12083                 : SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
   12084                 : #endif
   12085                 : 
   12086                 : /*
   12087                 : ** If the SQLITE_ENABLE IOTRACE exists then the global variable
   12088                 : ** sqlite3IoTrace is a pointer to a printf-like routine used to
   12089                 : ** print I/O tracing messages. 
   12090                 : */
   12091                 : #ifdef SQLITE_ENABLE_IOTRACE
   12092                 : # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
   12093                 : SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
   12094                 : SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
   12095                 : #else
   12096                 : # define IOTRACE(A)
   12097                 : # define sqlite3VdbeIOTraceSql(X)
   12098                 : #endif
   12099                 : 
   12100                 : /*
   12101                 : ** These routines are available for the mem2.c debugging memory allocator
   12102                 : ** only.  They are used to verify that different "types" of memory
   12103                 : ** allocations are properly tracked by the system.
   12104                 : **
   12105                 : ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
   12106                 : ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
   12107                 : ** a single bit set.
   12108                 : **
   12109                 : ** sqlite3MemdebugHasType() returns true if any of the bits in its second
   12110                 : ** argument match the type set by the previous sqlite3MemdebugSetType().
   12111                 : ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
   12112                 : **
   12113                 : ** sqlite3MemdebugNoType() returns true if none of the bits in its second
   12114                 : ** argument match the type set by the previous sqlite3MemdebugSetType().
   12115                 : **
   12116                 : ** Perhaps the most important point is the difference between MEMTYPE_HEAP
   12117                 : ** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
   12118                 : ** it might have been allocated by lookaside, except the allocation was
   12119                 : ** too large or lookaside was already full.  It is important to verify
   12120                 : ** that allocations that might have been satisfied by lookaside are not
   12121                 : ** passed back to non-lookaside free() routines.  Asserts such as the
   12122                 : ** example above are placed on the non-lookaside free() routines to verify
   12123                 : ** this constraint. 
   12124                 : **
   12125                 : ** All of this is no-op for a production build.  It only comes into
   12126                 : ** play when the SQLITE_MEMDEBUG compile-time option is used.
   12127                 : */
   12128                 : #ifdef SQLITE_MEMDEBUG
   12129                 : SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
   12130                 : SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
   12131                 : SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
   12132                 : #else
   12133                 : # define sqlite3MemdebugSetType(X,Y)  /* no-op */
   12134                 : # define sqlite3MemdebugHasType(X,Y)  1
   12135                 : # define sqlite3MemdebugNoType(X,Y)   1
   12136                 : #endif
   12137                 : #define MEMTYPE_HEAP       0x01  /* General heap allocations */
   12138                 : #define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
   12139                 : #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
   12140                 : #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
   12141                 : #define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
   12142                 : 
   12143                 : #endif /* _SQLITEINT_H_ */
   12144                 : 
   12145                 : /************** End of sqliteInt.h *******************************************/
   12146                 : /************** Begin file global.c ******************************************/
   12147                 : /*
   12148                 : ** 2008 June 13
   12149                 : **
   12150                 : ** The author disclaims copyright to this source code.  In place of
   12151                 : ** a legal notice, here is a blessing:
   12152                 : **
   12153                 : **    May you do good and not evil.
   12154                 : **    May you find forgiveness for yourself and forgive others.
   12155                 : **    May you share freely, never taking more than you give.
   12156                 : **
   12157                 : *************************************************************************
   12158                 : **
   12159                 : ** This file contains definitions of global variables and contants.
   12160                 : */
   12161                 : 
   12162                 : /* An array to map all upper-case characters into their corresponding
   12163                 : ** lower-case character. 
   12164                 : **
   12165                 : ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
   12166                 : ** handle case conversions for the UTF character set since the tables
   12167                 : ** involved are nearly as big or bigger than SQLite itself.
   12168                 : */
   12169                 : SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
   12170                 : #ifdef SQLITE_ASCII
   12171                 :       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
   12172                 :      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
   12173                 :      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
   12174                 :      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
   12175                 :     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
   12176                 :     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
   12177                 :     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
   12178                 :     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
   12179                 :     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
   12180                 :     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
   12181                 :     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
   12182                 :     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
   12183                 :     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
   12184                 :     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
   12185                 :     252,253,254,255
   12186                 : #endif
   12187                 : #ifdef SQLITE_EBCDIC
   12188                 :       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
   12189                 :      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
   12190                 :      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
   12191                 :      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
   12192                 :      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
   12193                 :      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
   12194                 :      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
   12195                 :     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
   12196                 :     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
   12197                 :     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
   12198                 :     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
   12199                 :     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
   12200                 :     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
   12201                 :     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
   12202                 :     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
   12203                 :     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
   12204                 : #endif
   12205                 : };
   12206                 : 
   12207                 : /*
   12208                 : ** The following 256 byte lookup table is used to support SQLites built-in
   12209                 : ** equivalents to the following standard library functions:
   12210                 : **
   12211                 : **   isspace()                        0x01
   12212                 : **   isalpha()                        0x02
   12213                 : **   isdigit()                        0x04
   12214                 : **   isalnum()                        0x06
   12215                 : **   isxdigit()                       0x08
   12216                 : **   toupper()                        0x20
   12217                 : **   SQLite identifier character      0x40
   12218                 : **
   12219                 : ** Bit 0x20 is set if the mapped character requires translation to upper
   12220                 : ** case. i.e. if the character is a lower-case ASCII character.
   12221                 : ** If x is a lower-case ASCII character, then its upper-case equivalent
   12222                 : ** is (x - 0x20). Therefore toupper() can be implemented as:
   12223                 : **
   12224                 : **   (x & ~(map[x]&0x20))
   12225                 : **
   12226                 : ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
   12227                 : ** array. tolower() is used more often than toupper() by SQLite.
   12228                 : **
   12229                 : ** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
   12230                 : ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
   12231                 : ** non-ASCII UTF character. Hence the test for whether or not a character is
   12232                 : ** part of an identifier is 0x46.
   12233                 : **
   12234                 : ** SQLite's versions are identical to the standard versions assuming a
   12235                 : ** locale of "C". They are implemented as macros in sqliteInt.h.
   12236                 : */
   12237                 : #ifdef SQLITE_ASCII
   12238                 : SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
   12239                 :   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
   12240                 :   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
   12241                 :   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
   12242                 :   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
   12243                 :   0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
   12244                 :   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
   12245                 :   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
   12246                 :   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
   12247                 : 
   12248                 :   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
   12249                 :   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
   12250                 :   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
   12251                 :   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
   12252                 :   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
   12253                 :   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
   12254                 :   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
   12255                 :   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
   12256                 : 
   12257                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
   12258                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
   12259                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
   12260                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
   12261                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
   12262                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
   12263                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
   12264                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
   12265                 : 
   12266                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
   12267                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
   12268                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
   12269                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
   12270                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
   12271                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
   12272                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
   12273                 :   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
   12274                 : };
   12275                 : #endif
   12276                 : 
   12277                 : #ifndef SQLITE_USE_URI
   12278                 : # define  SQLITE_USE_URI 0
   12279                 : #endif
   12280                 : 
   12281                 : /*
   12282                 : ** The following singleton contains the global configuration for
   12283                 : ** the SQLite library.
   12284                 : */
   12285                 : SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
   12286                 :    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
   12287                 :    1,                         /* bCoreMutex */
   12288                 :    SQLITE_THREADSAFE==1,      /* bFullMutex */
   12289                 :    SQLITE_USE_URI,            /* bOpenUri */
   12290                 :    0x7ffffffe,                /* mxStrlen */
   12291                 :    128,                       /* szLookaside */
   12292                 :    500,                       /* nLookaside */
   12293                 :    {0,0,0,0,0,0,0,0},         /* m */
   12294                 :    {0,0,0,0,0,0,0,0,0},       /* mutex */
   12295                 :    {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
   12296                 :    (void*)0,                  /* pHeap */
   12297                 :    0,                         /* nHeap */
   12298                 :    0, 0,                      /* mnHeap, mxHeap */
   12299                 :    (void*)0,                  /* pScratch */
   12300                 :    0,                         /* szScratch */
   12301                 :    0,                         /* nScratch */
   12302                 :    (void*)0,                  /* pPage */
   12303                 :    0,                         /* szPage */
   12304                 :    0,                         /* nPage */
   12305                 :    0,                         /* mxParserStack */
   12306                 :    0,                         /* sharedCacheEnabled */
   12307                 :    /* All the rest should always be initialized to zero */
   12308                 :    0,                         /* isInit */
   12309                 :    0,                         /* inProgress */
   12310                 :    0,                         /* isMutexInit */
   12311                 :    0,                         /* isMallocInit */
   12312                 :    0,                         /* isPCacheInit */
   12313                 :    0,                         /* pInitMutex */
   12314                 :    0,                         /* nRefInitMutex */
   12315                 :    0,                         /* xLog */
   12316                 :    0,                         /* pLogArg */
   12317                 :    0,                         /* bLocaltimeFault */
   12318                 : };
   12319                 : 
   12320                 : 
   12321                 : /*
   12322                 : ** Hash table for global functions - functions common to all
   12323                 : ** database connections.  After initialization, this table is
   12324                 : ** read-only.
   12325                 : */
   12326                 : SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
   12327                 : 
   12328                 : /*
   12329                 : ** Constant tokens for values 0 and 1.
   12330                 : */
   12331                 : SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
   12332                 :    { "0", 1 },
   12333                 :    { "1", 1 }
   12334                 : };
   12335                 : 
   12336                 : 
   12337                 : /*
   12338                 : ** The value of the "pending" byte must be 0x40000000 (1 byte past the
   12339                 : ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
   12340                 : ** the database page that contains the pending byte.  It never attempts
   12341                 : ** to read or write that page.  The pending byte page is set assign
   12342                 : ** for use by the VFS layers as space for managing file locks.
   12343                 : **
   12344                 : ** During testing, it is often desirable to move the pending byte to
   12345                 : ** a different position in the file.  This allows code that has to
   12346                 : ** deal with the pending byte to run on files that are much smaller
   12347                 : ** than 1 GiB.  The sqlite3_test_control() interface can be used to
   12348                 : ** move the pending byte.
   12349                 : **
   12350                 : ** IMPORTANT:  Changing the pending byte to any value other than
   12351                 : ** 0x40000000 results in an incompatible database file format!
   12352                 : ** Changing the pending byte during operating results in undefined
   12353                 : ** and dileterious behavior.
   12354                 : */
   12355                 : #ifndef SQLITE_OMIT_WSD
   12356                 : SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
   12357                 : #endif
   12358                 : 
   12359                 : /*
   12360                 : ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
   12361                 : ** created by mkopcodeh.awk during compilation.  Data is obtained
   12362                 : ** from the comments following the "case OP_xxxx:" statements in
   12363                 : ** the vdbe.c file.  
   12364                 : */
   12365                 : SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
   12366                 : 
   12367                 : /************** End of global.c **********************************************/
   12368                 : /************** Begin file ctime.c *******************************************/
   12369                 : /*
   12370                 : ** 2010 February 23
   12371                 : **
   12372                 : ** The author disclaims copyright to this source code.  In place of
   12373                 : ** a legal notice, here is a blessing:
   12374                 : **
   12375                 : **    May you do good and not evil.
   12376                 : **    May you find forgiveness for yourself and forgive others.
   12377                 : **    May you share freely, never taking more than you give.
   12378                 : **
   12379                 : *************************************************************************
   12380                 : **
   12381                 : ** This file implements routines used to report what compile-time options
   12382                 : ** SQLite was built with.
   12383                 : */
   12384                 : 
   12385                 : #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   12386                 : 
   12387                 : 
   12388                 : /*
   12389                 : ** An array of names of all compile-time options.  This array should 
   12390                 : ** be sorted A-Z.
   12391                 : **
   12392                 : ** This array looks large, but in a typical installation actually uses
   12393                 : ** only a handful of compile-time options, so most times this array is usually
   12394                 : ** rather short and uses little memory space.
   12395                 : */
   12396                 : static const char * const azCompileOpt[] = {
   12397                 : 
   12398                 : /* These macros are provided to "stringify" the value of the define
   12399                 : ** for those options in which the value is meaningful. */
   12400                 : #define CTIMEOPT_VAL_(opt) #opt
   12401                 : #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
   12402                 : 
   12403                 : #ifdef SQLITE_32BIT_ROWID
   12404                 :   "32BIT_ROWID",
   12405                 : #endif
   12406                 : #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
   12407                 :   "4_BYTE_ALIGNED_MALLOC",
   12408                 : #endif
   12409                 : #ifdef SQLITE_CASE_SENSITIVE_LIKE
   12410                 :   "CASE_SENSITIVE_LIKE",
   12411                 : #endif
   12412                 : #ifdef SQLITE_CHECK_PAGES
   12413                 :   "CHECK_PAGES",
   12414                 : #endif
   12415                 : #ifdef SQLITE_COVERAGE_TEST
   12416                 :   "COVERAGE_TEST",
   12417                 : #endif
   12418                 : #ifdef SQLITE_DEBUG
   12419                 :   "DEBUG",
   12420                 : #endif
   12421                 : #ifdef SQLITE_DEFAULT_LOCKING_MODE
   12422                 :   "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
   12423                 : #endif
   12424                 : #ifdef SQLITE_DISABLE_DIRSYNC
   12425                 :   "DISABLE_DIRSYNC",
   12426                 : #endif
   12427                 : #ifdef SQLITE_DISABLE_LFS
   12428                 :   "DISABLE_LFS",
   12429                 : #endif
   12430                 : #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   12431                 :   "ENABLE_ATOMIC_WRITE",
   12432                 : #endif
   12433                 : #ifdef SQLITE_ENABLE_CEROD
   12434                 :   "ENABLE_CEROD",
   12435                 : #endif
   12436                 : #ifdef SQLITE_ENABLE_COLUMN_METADATA
   12437                 :   "ENABLE_COLUMN_METADATA",
   12438                 : #endif
   12439                 : #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
   12440                 :   "ENABLE_EXPENSIVE_ASSERT",
   12441                 : #endif
   12442                 : #ifdef SQLITE_ENABLE_FTS1
   12443                 :   "ENABLE_FTS1",
   12444                 : #endif
   12445                 : #ifdef SQLITE_ENABLE_FTS2
   12446                 :   "ENABLE_FTS2",
   12447                 : #endif
   12448                 : #ifdef SQLITE_ENABLE_FTS3
   12449                 :   "ENABLE_FTS3",
   12450                 : #endif
   12451                 : #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
   12452                 :   "ENABLE_FTS3_PARENTHESIS",
   12453                 : #endif
   12454                 : #ifdef SQLITE_ENABLE_FTS4
   12455                 :   "ENABLE_FTS4",
   12456                 : #endif
   12457                 : #ifdef SQLITE_ENABLE_ICU
   12458                 :   "ENABLE_ICU",
   12459                 : #endif
   12460                 : #ifdef SQLITE_ENABLE_IOTRACE
   12461                 :   "ENABLE_IOTRACE",
   12462                 : #endif
   12463                 : #ifdef SQLITE_ENABLE_LOAD_EXTENSION
   12464                 :   "ENABLE_LOAD_EXTENSION",
   12465                 : #endif
   12466                 : #ifdef SQLITE_ENABLE_LOCKING_STYLE
   12467                 :   "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
   12468                 : #endif
   12469                 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   12470                 :   "ENABLE_MEMORY_MANAGEMENT",
   12471                 : #endif
   12472                 : #ifdef SQLITE_ENABLE_MEMSYS3
   12473                 :   "ENABLE_MEMSYS3",
   12474                 : #endif
   12475                 : #ifdef SQLITE_ENABLE_MEMSYS5
   12476                 :   "ENABLE_MEMSYS5",
   12477                 : #endif
   12478                 : #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
   12479                 :   "ENABLE_OVERSIZE_CELL_CHECK",
   12480                 : #endif
   12481                 : #ifdef SQLITE_ENABLE_RTREE
   12482                 :   "ENABLE_RTREE",
   12483                 : #endif
   12484                 : #ifdef SQLITE_ENABLE_STAT3
   12485                 :   "ENABLE_STAT3",
   12486                 : #endif
   12487                 : #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
   12488                 :   "ENABLE_UNLOCK_NOTIFY",
   12489                 : #endif
   12490                 : #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   12491                 :   "ENABLE_UPDATE_DELETE_LIMIT",
   12492                 : #endif
   12493                 : #ifdef SQLITE_HAS_CODEC
   12494                 :   "HAS_CODEC",
   12495                 : #endif
   12496                 : #ifdef SQLITE_HAVE_ISNAN
   12497                 :   "HAVE_ISNAN",
   12498                 : #endif
   12499                 : #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   12500                 :   "HOMEGROWN_RECURSIVE_MUTEX",
   12501                 : #endif
   12502                 : #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
   12503                 :   "IGNORE_AFP_LOCK_ERRORS",
   12504                 : #endif
   12505                 : #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   12506                 :   "IGNORE_FLOCK_LOCK_ERRORS",
   12507                 : #endif
   12508                 : #ifdef SQLITE_INT64_TYPE
   12509                 :   "INT64_TYPE",
   12510                 : #endif
   12511                 : #ifdef SQLITE_LOCK_TRACE
   12512                 :   "LOCK_TRACE",
   12513                 : #endif
   12514                 : #ifdef SQLITE_MAX_SCHEMA_RETRY
   12515                 :   "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
   12516                 : #endif
   12517                 : #ifdef SQLITE_MEMDEBUG
   12518                 :   "MEMDEBUG",
   12519                 : #endif
   12520                 : #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
   12521                 :   "MIXED_ENDIAN_64BIT_FLOAT",
   12522                 : #endif
   12523                 : #ifdef SQLITE_NO_SYNC
   12524                 :   "NO_SYNC",
   12525                 : #endif
   12526                 : #ifdef SQLITE_OMIT_ALTERTABLE
   12527                 :   "OMIT_ALTERTABLE",
   12528                 : #endif
   12529                 : #ifdef SQLITE_OMIT_ANALYZE
   12530                 :   "OMIT_ANALYZE",
   12531                 : #endif
   12532                 : #ifdef SQLITE_OMIT_ATTACH
   12533                 :   "OMIT_ATTACH",
   12534                 : #endif
   12535                 : #ifdef SQLITE_OMIT_AUTHORIZATION
   12536                 :   "OMIT_AUTHORIZATION",
   12537                 : #endif
   12538                 : #ifdef SQLITE_OMIT_AUTOINCREMENT
   12539                 :   "OMIT_AUTOINCREMENT",
   12540                 : #endif
   12541                 : #ifdef SQLITE_OMIT_AUTOINIT
   12542                 :   "OMIT_AUTOINIT",
   12543                 : #endif
   12544                 : #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
   12545                 :   "OMIT_AUTOMATIC_INDEX",
   12546                 : #endif
   12547                 : #ifdef SQLITE_OMIT_AUTORESET
   12548                 :   "OMIT_AUTORESET",
   12549                 : #endif
   12550                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   12551                 :   "OMIT_AUTOVACUUM",
   12552                 : #endif
   12553                 : #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
   12554                 :   "OMIT_BETWEEN_OPTIMIZATION",
   12555                 : #endif
   12556                 : #ifdef SQLITE_OMIT_BLOB_LITERAL
   12557                 :   "OMIT_BLOB_LITERAL",
   12558                 : #endif
   12559                 : #ifdef SQLITE_OMIT_BTREECOUNT
   12560                 :   "OMIT_BTREECOUNT",
   12561                 : #endif
   12562                 : #ifdef SQLITE_OMIT_BUILTIN_TEST
   12563                 :   "OMIT_BUILTIN_TEST",
   12564                 : #endif
   12565                 : #ifdef SQLITE_OMIT_CAST
   12566                 :   "OMIT_CAST",
   12567                 : #endif
   12568                 : #ifdef SQLITE_OMIT_CHECK
   12569                 :   "OMIT_CHECK",
   12570                 : #endif
   12571                 : /* // redundant
   12572                 : ** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
   12573                 : **   "OMIT_COMPILEOPTION_DIAGS",
   12574                 : ** #endif
   12575                 : */
   12576                 : #ifdef SQLITE_OMIT_COMPLETE
   12577                 :   "OMIT_COMPLETE",
   12578                 : #endif
   12579                 : #ifdef SQLITE_OMIT_COMPOUND_SELECT
   12580                 :   "OMIT_COMPOUND_SELECT",
   12581                 : #endif
   12582                 : #ifdef SQLITE_OMIT_DATETIME_FUNCS
   12583                 :   "OMIT_DATETIME_FUNCS",
   12584                 : #endif
   12585                 : #ifdef SQLITE_OMIT_DECLTYPE
   12586                 :   "OMIT_DECLTYPE",
   12587                 : #endif
   12588                 : #ifdef SQLITE_OMIT_DEPRECATED
   12589                 :   "OMIT_DEPRECATED",
   12590                 : #endif
   12591                 : #ifdef SQLITE_OMIT_DISKIO
   12592                 :   "OMIT_DISKIO",
   12593                 : #endif
   12594                 : #ifdef SQLITE_OMIT_EXPLAIN
   12595                 :   "OMIT_EXPLAIN",
   12596                 : #endif
   12597                 : #ifdef SQLITE_OMIT_FLAG_PRAGMAS
   12598                 :   "OMIT_FLAG_PRAGMAS",
   12599                 : #endif
   12600                 : #ifdef SQLITE_OMIT_FLOATING_POINT
   12601                 :   "OMIT_FLOATING_POINT",
   12602                 : #endif
   12603                 : #ifdef SQLITE_OMIT_FOREIGN_KEY
   12604                 :   "OMIT_FOREIGN_KEY",
   12605                 : #endif
   12606                 : #ifdef SQLITE_OMIT_GET_TABLE
   12607                 :   "OMIT_GET_TABLE",
   12608                 : #endif
   12609                 : #ifdef SQLITE_OMIT_INCRBLOB
   12610                 :   "OMIT_INCRBLOB",
   12611                 : #endif
   12612                 : #ifdef SQLITE_OMIT_INTEGRITY_CHECK
   12613                 :   "OMIT_INTEGRITY_CHECK",
   12614                 : #endif
   12615                 : #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
   12616                 :   "OMIT_LIKE_OPTIMIZATION",
   12617                 : #endif
   12618                 : #ifdef SQLITE_OMIT_LOAD_EXTENSION
   12619                 :   "OMIT_LOAD_EXTENSION",
   12620                 : #endif
   12621                 : #ifdef SQLITE_OMIT_LOCALTIME
   12622                 :   "OMIT_LOCALTIME",
   12623                 : #endif
   12624                 : #ifdef SQLITE_OMIT_LOOKASIDE
   12625                 :   "OMIT_LOOKASIDE",
   12626                 : #endif
   12627                 : #ifdef SQLITE_OMIT_MEMORYDB
   12628                 :   "OMIT_MEMORYDB",
   12629                 : #endif
   12630                 : #ifdef SQLITE_OMIT_MERGE_SORT
   12631                 :   "OMIT_MERGE_SORT",
   12632                 : #endif
   12633                 : #ifdef SQLITE_OMIT_OR_OPTIMIZATION
   12634                 :   "OMIT_OR_OPTIMIZATION",
   12635                 : #endif
   12636                 : #ifdef SQLITE_OMIT_PAGER_PRAGMAS
   12637                 :   "OMIT_PAGER_PRAGMAS",
   12638                 : #endif
   12639                 : #ifdef SQLITE_OMIT_PRAGMA
   12640                 :   "OMIT_PRAGMA",
   12641                 : #endif
   12642                 : #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
   12643                 :   "OMIT_PROGRESS_CALLBACK",
   12644                 : #endif
   12645                 : #ifdef SQLITE_OMIT_QUICKBALANCE
   12646                 :   "OMIT_QUICKBALANCE",
   12647                 : #endif
   12648                 : #ifdef SQLITE_OMIT_REINDEX
   12649                 :   "OMIT_REINDEX",
   12650                 : #endif
   12651                 : #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
   12652                 :   "OMIT_SCHEMA_PRAGMAS",
   12653                 : #endif
   12654                 : #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
   12655                 :   "OMIT_SCHEMA_VERSION_PRAGMAS",
   12656                 : #endif
   12657                 : #ifdef SQLITE_OMIT_SHARED_CACHE
   12658                 :   "OMIT_SHARED_CACHE",
   12659                 : #endif
   12660                 : #ifdef SQLITE_OMIT_SUBQUERY
   12661                 :   "OMIT_SUBQUERY",
   12662                 : #endif
   12663                 : #ifdef SQLITE_OMIT_TCL_VARIABLE
   12664                 :   "OMIT_TCL_VARIABLE",
   12665                 : #endif
   12666                 : #ifdef SQLITE_OMIT_TEMPDB
   12667                 :   "OMIT_TEMPDB",
   12668                 : #endif
   12669                 : #ifdef SQLITE_OMIT_TRACE
   12670                 :   "OMIT_TRACE",
   12671                 : #endif
   12672                 : #ifdef SQLITE_OMIT_TRIGGER
   12673                 :   "OMIT_TRIGGER",
   12674                 : #endif
   12675                 : #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
   12676                 :   "OMIT_TRUNCATE_OPTIMIZATION",
   12677                 : #endif
   12678                 : #ifdef SQLITE_OMIT_UTF16
   12679                 :   "OMIT_UTF16",
   12680                 : #endif
   12681                 : #ifdef SQLITE_OMIT_VACUUM
   12682                 :   "OMIT_VACUUM",
   12683                 : #endif
   12684                 : #ifdef SQLITE_OMIT_VIEW
   12685                 :   "OMIT_VIEW",
   12686                 : #endif
   12687                 : #ifdef SQLITE_OMIT_VIRTUALTABLE
   12688                 :   "OMIT_VIRTUALTABLE",
   12689                 : #endif
   12690                 : #ifdef SQLITE_OMIT_WAL
   12691                 :   "OMIT_WAL",
   12692                 : #endif
   12693                 : #ifdef SQLITE_OMIT_WSD
   12694                 :   "OMIT_WSD",
   12695                 : #endif
   12696                 : #ifdef SQLITE_OMIT_XFER_OPT
   12697                 :   "OMIT_XFER_OPT",
   12698                 : #endif
   12699                 : #ifdef SQLITE_PERFORMANCE_TRACE
   12700                 :   "PERFORMANCE_TRACE",
   12701                 : #endif
   12702                 : #ifdef SQLITE_PROXY_DEBUG
   12703                 :   "PROXY_DEBUG",
   12704                 : #endif
   12705                 : #ifdef SQLITE_SECURE_DELETE
   12706                 :   "SECURE_DELETE",
   12707                 : #endif
   12708                 : #ifdef SQLITE_SMALL_STACK
   12709                 :   "SMALL_STACK",
   12710                 : #endif
   12711                 : #ifdef SQLITE_SOUNDEX
   12712                 :   "SOUNDEX",
   12713                 : #endif
   12714                 : #ifdef SQLITE_TCL
   12715                 :   "TCL",
   12716                 : #endif
   12717                 : #ifdef SQLITE_TEMP_STORE
   12718                 :   "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
   12719                 : #endif
   12720                 : #ifdef SQLITE_TEST
   12721                 :   "TEST",
   12722                 : #endif
   12723                 : #ifdef SQLITE_THREADSAFE
   12724                 :   "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
   12725                 : #endif
   12726                 : #ifdef SQLITE_USE_ALLOCA
   12727                 :   "USE_ALLOCA",
   12728                 : #endif
   12729                 : #ifdef SQLITE_ZERO_MALLOC
   12730                 :   "ZERO_MALLOC"
   12731                 : #endif
   12732                 : };
   12733                 : 
   12734                 : /*
   12735                 : ** Given the name of a compile-time option, return true if that option
   12736                 : ** was used and false if not.
   12737                 : **
   12738                 : ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
   12739                 : ** is not required for a match.
   12740                 : */
   12741               0 : SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
   12742                 :   int i, n;
   12743               0 :   if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
   12744               0 :   n = sqlite3Strlen30(zOptName);
   12745                 : 
   12746                 :   /* Since ArraySize(azCompileOpt) is normally in single digits, a
   12747                 :   ** linear search is adequate.  No need for a binary search. */
   12748               0 :   for(i=0; i<ArraySize(azCompileOpt); i++){
   12749               0 :     if(   (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
   12750               0 :        && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
   12751                 :   }
   12752               0 :   return 0;
   12753                 : }
   12754                 : 
   12755                 : /*
   12756                 : ** Return the N-th compile-time option string.  If N is out of range,
   12757                 : ** return a NULL pointer.
   12758                 : */
   12759               0 : SQLITE_API const char *sqlite3_compileoption_get(int N){
   12760               0 :   if( N>=0 && N<ArraySize(azCompileOpt) ){
   12761               0 :     return azCompileOpt[N];
   12762                 :   }
   12763               0 :   return 0;
   12764                 : }
   12765                 : 
   12766                 : #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
   12767                 : 
   12768                 : /************** End of ctime.c ***********************************************/
   12769                 : /************** Begin file status.c ******************************************/
   12770                 : /*
   12771                 : ** 2008 June 18
   12772                 : **
   12773                 : ** The author disclaims copyright to this source code.  In place of
   12774                 : ** a legal notice, here is a blessing:
   12775                 : **
   12776                 : **    May you do good and not evil.
   12777                 : **    May you find forgiveness for yourself and forgive others.
   12778                 : **    May you share freely, never taking more than you give.
   12779                 : **
   12780                 : *************************************************************************
   12781                 : **
   12782                 : ** This module implements the sqlite3_status() interface and related
   12783                 : ** functionality.
   12784                 : */
   12785                 : /************** Include vdbeInt.h in the middle of status.c ******************/
   12786                 : /************** Begin file vdbeInt.h *****************************************/
   12787                 : /*
   12788                 : ** 2003 September 6
   12789                 : **
   12790                 : ** The author disclaims copyright to this source code.  In place of
   12791                 : ** a legal notice, here is a blessing:
   12792                 : **
   12793                 : **    May you do good and not evil.
   12794                 : **    May you find forgiveness for yourself and forgive others.
   12795                 : **    May you share freely, never taking more than you give.
   12796                 : **
   12797                 : *************************************************************************
   12798                 : ** This is the header file for information that is private to the
   12799                 : ** VDBE.  This information used to all be at the top of the single
   12800                 : ** source code file "vdbe.c".  When that file became too big (over
   12801                 : ** 6000 lines long) it was split up into several smaller files and
   12802                 : ** this header information was factored out.
   12803                 : */
   12804                 : #ifndef _VDBEINT_H_
   12805                 : #define _VDBEINT_H_
   12806                 : 
   12807                 : /*
   12808                 : ** SQL is translated into a sequence of instructions to be
   12809                 : ** executed by a virtual machine.  Each instruction is an instance
   12810                 : ** of the following structure.
   12811                 : */
   12812                 : typedef struct VdbeOp Op;
   12813                 : 
   12814                 : /*
   12815                 : ** Boolean values
   12816                 : */
   12817                 : typedef unsigned char Bool;
   12818                 : 
   12819                 : /* Opaque type used by code in vdbesort.c */
   12820                 : typedef struct VdbeSorter VdbeSorter;
   12821                 : 
   12822                 : /* Opaque type used by the explainer */
   12823                 : typedef struct Explain Explain;
   12824                 : 
   12825                 : /*
   12826                 : ** A cursor is a pointer into a single BTree within a database file.
   12827                 : ** The cursor can seek to a BTree entry with a particular key, or
   12828                 : ** loop over all entries of the Btree.  You can also insert new BTree
   12829                 : ** entries or retrieve the key or data from the entry that the cursor
   12830                 : ** is currently pointing to.
   12831                 : ** 
   12832                 : ** Every cursor that the virtual machine has open is represented by an
   12833                 : ** instance of the following structure.
   12834                 : */
   12835                 : struct VdbeCursor {
   12836                 :   BtCursor *pCursor;    /* The cursor structure of the backend */
   12837                 :   Btree *pBt;           /* Separate file holding temporary table */
   12838                 :   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
   12839                 :   int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
   12840                 :   int pseudoTableReg;   /* Register holding pseudotable content. */
   12841                 :   int nField;           /* Number of fields in the header */
   12842                 :   Bool zeroed;          /* True if zeroed out and ready for reuse */
   12843                 :   Bool rowidIsValid;    /* True if lastRowid is valid */
   12844                 :   Bool atFirst;         /* True if pointing to first entry */
   12845                 :   Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
   12846                 :   Bool nullRow;         /* True if pointing to a row with no data */
   12847                 :   Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
   12848                 :   Bool isTable;         /* True if a table requiring integer keys */
   12849                 :   Bool isIndex;         /* True if an index containing keys only - no data */
   12850                 :   Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
   12851                 :   Bool isSorter;        /* True if a new-style sorter */
   12852                 :   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
   12853                 :   const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
   12854                 :   i64 seqCount;         /* Sequence counter */
   12855                 :   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
   12856                 :   i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
   12857                 :   VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
   12858                 : 
   12859                 :   /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
   12860                 :   ** OP_IsUnique opcode on this cursor. */
   12861                 :   int seekResult;
   12862                 : 
   12863                 :   /* Cached information about the header for the data record that the
   12864                 :   ** cursor is currently pointing to.  Only valid if cacheStatus matches
   12865                 :   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
   12866                 :   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
   12867                 :   ** the cache is out of date.
   12868                 :   **
   12869                 :   ** aRow might point to (ephemeral) data for the current row, or it might
   12870                 :   ** be NULL.
   12871                 :   */
   12872                 :   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
   12873                 :   int payloadSize;      /* Total number of bytes in the record */
   12874                 :   u32 *aType;           /* Type values for all entries in the record */
   12875                 :   u32 *aOffset;         /* Cached offsets to the start of each columns data */
   12876                 :   u8 *aRow;             /* Data for the current row, if all on one page */
   12877                 : };
   12878                 : typedef struct VdbeCursor VdbeCursor;
   12879                 : 
   12880                 : /*
   12881                 : ** When a sub-program is executed (OP_Program), a structure of this type
   12882                 : ** is allocated to store the current value of the program counter, as
   12883                 : ** well as the current memory cell array and various other frame specific
   12884                 : ** values stored in the Vdbe struct. When the sub-program is finished, 
   12885                 : ** these values are copied back to the Vdbe from the VdbeFrame structure,
   12886                 : ** restoring the state of the VM to as it was before the sub-program
   12887                 : ** began executing.
   12888                 : **
   12889                 : ** The memory for a VdbeFrame object is allocated and managed by a memory
   12890                 : ** cell in the parent (calling) frame. When the memory cell is deleted or
   12891                 : ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
   12892                 : ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
   12893                 : ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
   12894                 : ** this instead of deleting the VdbeFrame immediately is to avoid recursive
   12895                 : ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
   12896                 : ** child frame are released.
   12897                 : **
   12898                 : ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
   12899                 : ** set to NULL if the currently executing frame is the main program.
   12900                 : */
   12901                 : typedef struct VdbeFrame VdbeFrame;
   12902                 : struct VdbeFrame {
   12903                 :   Vdbe *v;                /* VM this frame belongs to */
   12904                 :   int pc;                 /* Program Counter in parent (calling) frame */
   12905                 :   Op *aOp;                /* Program instructions for parent frame */
   12906                 :   int nOp;                /* Size of aOp array */
   12907                 :   Mem *aMem;              /* Array of memory cells for parent frame */
   12908                 :   int nMem;               /* Number of entries in aMem */
   12909                 :   u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
   12910                 :   int nOnceFlag;          /* Number of entries in aOnceFlag */
   12911                 :   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
   12912                 :   u16 nCursor;            /* Number of entries in apCsr */
   12913                 :   void *token;            /* Copy of SubProgram.token */
   12914                 :   int nChildMem;          /* Number of memory cells for child frame */
   12915                 :   int nChildCsr;          /* Number of cursors for child frame */
   12916                 :   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
   12917                 :   int nChange;            /* Statement changes (Vdbe.nChanges)     */
   12918                 :   VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
   12919                 : };
   12920                 : 
   12921                 : #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
   12922                 : 
   12923                 : /*
   12924                 : ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
   12925                 : */
   12926                 : #define CACHE_STALE 0
   12927                 : 
   12928                 : /*
   12929                 : ** Internally, the vdbe manipulates nearly all SQL values as Mem
   12930                 : ** structures. Each Mem struct may cache multiple representations (string,
   12931                 : ** integer etc.) of the same value.
   12932                 : */
   12933                 : struct Mem {
   12934                 :   sqlite3 *db;        /* The associated database connection */
   12935                 :   char *z;            /* String or BLOB value */
   12936                 :   double r;           /* Real value */
   12937                 :   union {
   12938                 :     i64 i;              /* Integer value used when MEM_Int is set in flags */
   12939                 :     int nZero;          /* Used when bit MEM_Zero is set in flags */
   12940                 :     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   12941                 :     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
   12942                 :     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
   12943                 :   } u;
   12944                 :   int n;              /* Number of characters in string value, excluding '\0' */
   12945                 :   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   12946                 :   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
   12947                 :   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   12948                 : #ifdef SQLITE_DEBUG
   12949                 :   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   12950                 :   void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   12951                 : #endif
   12952                 :   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
   12953                 :   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
   12954                 : };
   12955                 : 
   12956                 : /* One or more of the following flags are set to indicate the validOK
   12957                 : ** representations of the value stored in the Mem struct.
   12958                 : **
   12959                 : ** If the MEM_Null flag is set, then the value is an SQL NULL value.
   12960                 : ** No other flags may be set in this case.
   12961                 : **
   12962                 : ** If the MEM_Str flag is set then Mem.z points at a string representation.
   12963                 : ** Usually this is encoded in the same unicode encoding as the main
   12964                 : ** database (see below for exceptions). If the MEM_Term flag is also
   12965                 : ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
   12966                 : ** flags may coexist with the MEM_Str flag.
   12967                 : */
   12968                 : #define MEM_Null      0x0001   /* Value is NULL */
   12969                 : #define MEM_Str       0x0002   /* Value is a string */
   12970                 : #define MEM_Int       0x0004   /* Value is an integer */
   12971                 : #define MEM_Real      0x0008   /* Value is a real number */
   12972                 : #define MEM_Blob      0x0010   /* Value is a BLOB */
   12973                 : #define MEM_RowSet    0x0020   /* Value is a RowSet object */
   12974                 : #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
   12975                 : #define MEM_Invalid   0x0080   /* Value is undefined */
   12976                 : #define MEM_TypeMask  0x00ff   /* Mask of type bits */
   12977                 : 
   12978                 : /* Whenever Mem contains a valid string or blob representation, one of
   12979                 : ** the following flags must be set to determine the memory management
   12980                 : ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
   12981                 : ** string is \000 or \u0000 terminated
   12982                 : */
   12983                 : #define MEM_Term      0x0200   /* String rep is nul terminated */
   12984                 : #define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
   12985                 : #define MEM_Static    0x0800   /* Mem.z points to a static string */
   12986                 : #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
   12987                 : #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
   12988                 : #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
   12989                 : #ifdef SQLITE_OMIT_INCRBLOB
   12990                 :   #undef MEM_Zero
   12991                 :   #define MEM_Zero 0x0000
   12992                 : #endif
   12993                 : 
   12994                 : /*
   12995                 : ** Clear any existing type flags from a Mem and replace them with f
   12996                 : */
   12997                 : #define MemSetTypeFlag(p, f) \
   12998                 :    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
   12999                 : 
   13000                 : /*
   13001                 : ** Return true if a memory cell is not marked as invalid.  This macro
   13002                 : ** is for use inside assert() statements only.
   13003                 : */
   13004                 : #ifdef SQLITE_DEBUG
   13005                 : #define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
   13006                 : #endif
   13007                 : 
   13008                 : 
   13009                 : /* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
   13010                 : ** additional information about auxiliary information bound to arguments
   13011                 : ** of the function.  This is used to implement the sqlite3_get_auxdata()
   13012                 : ** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
   13013                 : ** that can be associated with a constant argument to a function.  This
   13014                 : ** allows functions such as "regexp" to compile their constant regular
   13015                 : ** expression argument once and reused the compiled code for multiple
   13016                 : ** invocations.
   13017                 : */
   13018                 : struct VdbeFunc {
   13019                 :   FuncDef *pFunc;               /* The definition of the function */
   13020                 :   int nAux;                     /* Number of entries allocated for apAux[] */
   13021                 :   struct AuxData {
   13022                 :     void *pAux;                   /* Aux data for the i-th argument */
   13023                 :     void (*xDelete)(void *);      /* Destructor for the aux data */
   13024                 :   } apAux[1];                   /* One slot for each function argument */
   13025                 : };
   13026                 : 
   13027                 : /*
   13028                 : ** The "context" argument for a installable function.  A pointer to an
   13029                 : ** instance of this structure is the first argument to the routines used
   13030                 : ** implement the SQL functions.
   13031                 : **
   13032                 : ** There is a typedef for this structure in sqlite.h.  So all routines,
   13033                 : ** even the public interface to SQLite, can use a pointer to this structure.
   13034                 : ** But this file is the only place where the internal details of this
   13035                 : ** structure are known.
   13036                 : **
   13037                 : ** This structure is defined inside of vdbeInt.h because it uses substructures
   13038                 : ** (Mem) which are only defined there.
   13039                 : */
   13040                 : struct sqlite3_context {
   13041                 :   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
   13042                 :   VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
   13043                 :   Mem s;                /* The return value is stored here */
   13044                 :   Mem *pMem;            /* Memory cell used to store aggregate context */
   13045                 :   int isError;          /* Error code returned by the function. */
   13046                 :   CollSeq *pColl;       /* Collating sequence */
   13047                 : };
   13048                 : 
   13049                 : /*
   13050                 : ** An Explain object accumulates indented output which is helpful
   13051                 : ** in describing recursive data structures.
   13052                 : */
   13053                 : struct Explain {
   13054                 :   Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
   13055                 :   StrAccum str;      /* The string being accumulated */
   13056                 :   int nIndent;       /* Number of elements in aIndent */
   13057                 :   u16 aIndent[100];  /* Levels of indentation */
   13058                 :   char zBase[100];   /* Initial space */
   13059                 : };
   13060                 : 
   13061                 : /*
   13062                 : ** An instance of the virtual machine.  This structure contains the complete
   13063                 : ** state of the virtual machine.
   13064                 : **
   13065                 : ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
   13066                 : ** is really a pointer to an instance of this structure.
   13067                 : **
   13068                 : ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
   13069                 : ** any virtual table method invocations made by the vdbe program. It is
   13070                 : ** set to 2 for xDestroy method calls and 1 for all other methods. This
   13071                 : ** variable is used for two purposes: to allow xDestroy methods to execute
   13072                 : ** "DROP TABLE" statements and to prevent some nasty side effects of
   13073                 : ** malloc failure when SQLite is invoked recursively by a virtual table 
   13074                 : ** method function.
   13075                 : */
   13076                 : struct Vdbe {
   13077                 :   sqlite3 *db;            /* The database connection that owns this statement */
   13078                 :   Op *aOp;                /* Space to hold the virtual machine's program */
   13079                 :   Mem *aMem;              /* The memory locations */
   13080                 :   Mem **apArg;            /* Arguments to currently executing user function */
   13081                 :   Mem *aColName;          /* Column names to return */
   13082                 :   Mem *pResultSet;        /* Pointer to an array of results */
   13083                 :   int nMem;               /* Number of memory locations currently allocated */
   13084                 :   int nOp;                /* Number of instructions in the program */
   13085                 :   int nOpAlloc;           /* Number of slots allocated for aOp[] */
   13086                 :   int nLabel;             /* Number of labels used */
   13087                 :   int nLabelAlloc;        /* Number of slots allocated in aLabel[] */
   13088                 :   int *aLabel;            /* Space to hold the labels */
   13089                 :   u16 nResColumn;         /* Number of columns in one row of the result set */
   13090                 :   u16 nCursor;            /* Number of slots in apCsr[] */
   13091                 :   u32 magic;              /* Magic number for sanity checking */
   13092                 :   char *zErrMsg;          /* Error message written here */
   13093                 :   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
   13094                 :   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
   13095                 :   Mem *aVar;              /* Values for the OP_Variable opcode. */
   13096                 :   char **azVar;           /* Name of variables */
   13097                 :   ynVar nVar;             /* Number of entries in aVar[] */
   13098                 :   ynVar nzVar;            /* Number of entries in azVar[] */
   13099                 :   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
   13100                 :   int pc;                 /* The program counter */
   13101                 :   int rc;                 /* Value to return */
   13102                 :   u8 errorAction;         /* Recovery action to do in case of an error */
   13103                 :   u8 explain;             /* True if EXPLAIN present on SQL command */
   13104                 :   u8 changeCntOn;         /* True to update the change-counter */
   13105                 :   u8 expired;             /* True if the VM needs to be recompiled */
   13106                 :   u8 runOnlyOnce;         /* Automatically expire on reset */
   13107                 :   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   13108                 :   u8 inVtabMethod;        /* See comments above */
   13109                 :   u8 usesStmtJournal;     /* True if uses a statement journal */
   13110                 :   u8 readOnly;            /* True for read-only statements */
   13111                 :   u8 isPrepareV2;         /* True if prepared with prepare_v2() */
   13112                 :   int nChange;            /* Number of db changes made since last reset */
   13113                 :   yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
   13114                 :   yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
   13115                 :   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
   13116                 :   int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
   13117                 : #ifndef SQLITE_OMIT_TRACE
   13118                 :   i64 startTime;          /* Time when query started - used for profiling */
   13119                 : #endif
   13120                 :   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
   13121                 :   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
   13122                 :   char *zSql;             /* Text of the SQL statement that generated this */
   13123                 :   void *pFree;            /* Free this when deleting the vdbe */
   13124                 : #ifdef SQLITE_DEBUG
   13125                 :   FILE *trace;            /* Write an execution trace here, if not NULL */
   13126                 : #endif
   13127                 : #ifdef SQLITE_ENABLE_TREE_EXPLAIN
   13128                 :   Explain *pExplain;      /* The explainer */
   13129                 :   char *zExplain;         /* Explanation of data structures */
   13130                 : #endif
   13131                 :   VdbeFrame *pFrame;      /* Parent frame */
   13132                 :   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
   13133                 :   int nFrame;             /* Number of frames in pFrame list */
   13134                 :   u32 expmask;            /* Binding to these vars invalidates VM */
   13135                 :   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
   13136                 :   int nOnceFlag;          /* Size of array aOnceFlag[] */
   13137                 :   u8 *aOnceFlag;          /* Flags for OP_Once */
   13138                 : };
   13139                 : 
   13140                 : /*
   13141                 : ** The following are allowed values for Vdbe.magic
   13142                 : */
   13143                 : #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
   13144                 : #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
   13145                 : #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
   13146                 : #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
   13147                 : 
   13148                 : /*
   13149                 : ** Function prototypes
   13150                 : */
   13151                 : SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
   13152                 : void sqliteVdbePopStack(Vdbe*,int);
   13153                 : SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
   13154                 : #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
   13155                 : SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
   13156                 : #endif
   13157                 : SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
   13158                 : SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
   13159                 : SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
   13160                 : SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   13161                 : SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
   13162                 : 
   13163                 : int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   13164                 : SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
   13165                 : SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
   13166                 : SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
   13167                 : SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
   13168                 : SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
   13169                 : SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
   13170                 : SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
   13171                 : SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
   13172                 : SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
   13173                 : SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
   13174                 : SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
   13175                 : SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
   13176                 : SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
   13177                 : SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
   13178                 : #ifdef SQLITE_OMIT_FLOATING_POINT
   13179                 : # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
   13180                 : #else
   13181                 : SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
   13182                 : #endif
   13183                 : SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
   13184                 : SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
   13185                 : SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
   13186                 : SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
   13187                 : SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
   13188                 : SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
   13189                 : SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
   13190                 : SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
   13191                 : SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
   13192                 : SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
   13193                 : SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
   13194                 : SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
   13195                 : SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
   13196                 : SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
   13197                 : #define VdbeMemRelease(X)  \
   13198                 :   if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
   13199                 :     sqlite3VdbeMemReleaseExternal(X);
   13200                 : SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   13201                 : SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
   13202                 : SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   13203                 : SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
   13204                 : SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
   13205                 : SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
   13206                 : SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
   13207                 : SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
   13208                 : 
   13209                 : #ifdef SQLITE_OMIT_MERGE_SORT
   13210                 : # define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
   13211                 : # define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
   13212                 : # define sqlite3VdbeSorterClose(Y,Z)
   13213                 : # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
   13214                 : # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
   13215                 : # define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
   13216                 : # define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
   13217                 : #else
   13218                 : SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   13219                 : SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   13220                 : SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
   13221                 : SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
   13222                 : SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
   13223                 : SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
   13224                 : SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
   13225                 : #endif
   13226                 : 
   13227                 : #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   13228                 : SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
   13229                 : SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
   13230                 : #else
   13231                 : # define sqlite3VdbeEnter(X)
   13232                 : # define sqlite3VdbeLeave(X)
   13233                 : #endif
   13234                 : 
   13235                 : #ifdef SQLITE_DEBUG
   13236                 : SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
   13237                 : #endif
   13238                 : 
   13239                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   13240                 : SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
   13241                 : #else
   13242                 : # define sqlite3VdbeCheckFk(p,i) 0
   13243                 : #endif
   13244                 : 
   13245                 : SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
   13246                 : #ifdef SQLITE_DEBUG
   13247                 : SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
   13248                 : SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
   13249                 : #endif
   13250                 : SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
   13251                 : 
   13252                 : #ifndef SQLITE_OMIT_INCRBLOB
   13253                 : SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
   13254                 :   #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
   13255                 : #else
   13256                 :   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
   13257                 :   #define ExpandBlob(P) SQLITE_OK
   13258                 : #endif
   13259                 : 
   13260                 : #endif /* !defined(_VDBEINT_H_) */
   13261                 : 
   13262                 : /************** End of vdbeInt.h *********************************************/
   13263                 : /************** Continuing where we left off in status.c *********************/
   13264                 : 
   13265                 : /*
   13266                 : ** Variables in which to record status information.
   13267                 : */
   13268                 : typedef struct sqlite3StatType sqlite3StatType;
   13269                 : static SQLITE_WSD struct sqlite3StatType {
   13270                 :   int nowValue[10];         /* Current value */
   13271                 :   int mxValue[10];          /* Maximum value */
   13272                 : } sqlite3Stat = { {0,}, {0,} };
   13273                 : 
   13274                 : 
   13275                 : /* The "wsdStat" macro will resolve to the status information
   13276                 : ** state vector.  If writable static data is unsupported on the target,
   13277                 : ** we have to locate the state vector at run-time.  In the more common
   13278                 : ** case where writable static data is supported, wsdStat can refer directly
   13279                 : ** to the "sqlite3Stat" state vector declared above.
   13280                 : */
   13281                 : #ifdef SQLITE_OMIT_WSD
   13282                 : # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
   13283                 : # define wsdStat x[0]
   13284                 : #else
   13285                 : # define wsdStatInit
   13286                 : # define wsdStat sqlite3Stat
   13287                 : #endif
   13288                 : 
   13289                 : /*
   13290                 : ** Return the current value of a status parameter.
   13291                 : */
   13292          269031 : SQLITE_PRIVATE int sqlite3StatusValue(int op){
   13293                 :   wsdStatInit;
   13294          269031 :   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   13295          269031 :   return wsdStat.nowValue[op];
   13296                 : }
   13297                 : 
   13298                 : /*
   13299                 : ** Add N to the value of a status record.  It is assumed that the
   13300                 : ** caller holds appropriate locks.
   13301                 : */
   13302        24796555 : SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
   13303                 :   wsdStatInit;
   13304        24796555 :   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   13305        24796555 :   wsdStat.nowValue[op] += N;
   13306        24796555 :   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
   13307         1173382 :     wsdStat.mxValue[op] = wsdStat.nowValue[op];
   13308                 :   }
   13309        24796555 : }
   13310                 : 
   13311                 : /*
   13312                 : ** Set the value of a status to X.
   13313                 : */
   13314         6454482 : SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
   13315                 :   wsdStatInit;
   13316         6454482 :   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
   13317         6454482 :   wsdStat.nowValue[op] = X;
   13318         6454482 :   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
   13319            5343 :     wsdStat.mxValue[op] = wsdStat.nowValue[op];
   13320                 :   }
   13321         6454482 : }
   13322                 : 
   13323                 : /*
   13324                 : ** Query status information.
   13325                 : **
   13326                 : ** This implementation assumes that reading or writing an aligned
   13327                 : ** 32-bit integer is an atomic operation.  If that assumption is not true,
   13328                 : ** then this routine is not threadsafe.
   13329                 : */
   13330               6 : SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
   13331                 :   wsdStatInit;
   13332               6 :   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
   13333               0 :     return SQLITE_MISUSE_BKPT;
   13334                 :   }
   13335               6 :   *pCurrent = wsdStat.nowValue[op];
   13336               6 :   *pHighwater = wsdStat.mxValue[op];
   13337               6 :   if( resetFlag ){
   13338               0 :     wsdStat.mxValue[op] = wsdStat.nowValue[op];
   13339                 :   }
   13340               6 :   return SQLITE_OK;
   13341                 : }
   13342                 : 
   13343                 : /*
   13344                 : ** Query status information for a single database connection
   13345                 : */
   13346              27 : SQLITE_API int sqlite3_db_status(
   13347                 :   sqlite3 *db,          /* The database connection whose status is desired */
   13348                 :   int op,               /* Status verb */
   13349                 :   int *pCurrent,        /* Write current value here */
   13350                 :   int *pHighwater,      /* Write high-water mark here */
   13351                 :   int resetFlag         /* Reset high-water mark if true */
   13352                 : ){
   13353              27 :   int rc = SQLITE_OK;   /* Return code */
   13354              27 :   sqlite3_mutex_enter(db->mutex);
   13355              27 :   switch( op ){
   13356                 :     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
   13357               0 :       *pCurrent = db->lookaside.nOut;
   13358               0 :       *pHighwater = db->lookaside.mxOut;
   13359               0 :       if( resetFlag ){
   13360               0 :         db->lookaside.mxOut = db->lookaside.nOut;
   13361                 :       }
   13362               0 :       break;
   13363                 :     }
   13364                 : 
   13365                 :     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
   13366                 :     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
   13367                 :     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
   13368                 :       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
   13369                 :       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
   13370                 :       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
   13371               0 :       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
   13372               0 :       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
   13373               0 :       *pCurrent = 0;
   13374               0 :       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
   13375               0 :       if( resetFlag ){
   13376               0 :         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
   13377                 :       }
   13378               0 :       break;
   13379                 :     }
   13380                 : 
   13381                 :     /* 
   13382                 :     ** Return an approximation for the amount of memory currently used
   13383                 :     ** by all pagers associated with the given database connection.  The
   13384                 :     ** highwater mark is meaningless and is returned as zero.
   13385                 :     */
   13386                 :     case SQLITE_DBSTATUS_CACHE_USED: {
   13387               9 :       int totalUsed = 0;
   13388                 :       int i;
   13389               9 :       sqlite3BtreeEnterAll(db);
   13390              27 :       for(i=0; i<db->nDb; i++){
   13391              18 :         Btree *pBt = db->aDb[i].pBt;
   13392              18 :         if( pBt ){
   13393               9 :           Pager *pPager = sqlite3BtreePager(pBt);
   13394               9 :           totalUsed += sqlite3PagerMemUsed(pPager);
   13395                 :         }
   13396                 :       }
   13397               9 :       sqlite3BtreeLeaveAll(db);
   13398               9 :       *pCurrent = totalUsed;
   13399               9 :       *pHighwater = 0;
   13400               9 :       break;
   13401                 :     }
   13402                 : 
   13403                 :     /*
   13404                 :     ** *pCurrent gets an accurate estimate of the amount of memory used
   13405                 :     ** to store the schema for all databases (main, temp, and any ATTACHed
   13406                 :     ** databases.  *pHighwater is set to zero.
   13407                 :     */
   13408                 :     case SQLITE_DBSTATUS_SCHEMA_USED: {
   13409                 :       int i;                      /* Used to iterate through schemas */
   13410               9 :       int nByte = 0;              /* Used to accumulate return value */
   13411                 : 
   13412               9 :       sqlite3BtreeEnterAll(db);
   13413               9 :       db->pnBytesFreed = &nByte;
   13414              27 :       for(i=0; i<db->nDb; i++){
   13415              18 :         Schema *pSchema = db->aDb[i].pSchema;
   13416              18 :         if( ALWAYS(pSchema!=0) ){
   13417                 :           HashElem *p;
   13418                 : 
   13419              36 :           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
   13420              18 :               pSchema->tblHash.count 
   13421              18 :             + pSchema->trigHash.count
   13422              18 :             + pSchema->idxHash.count
   13423              18 :             + pSchema->fkeyHash.count
   13424                 :           );
   13425              18 :           nByte += sqlite3MallocSize(pSchema->tblHash.ht);
   13426              18 :           nByte += sqlite3MallocSize(pSchema->trigHash.ht);
   13427              18 :           nByte += sqlite3MallocSize(pSchema->idxHash.ht);
   13428              18 :           nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
   13429                 : 
   13430              27 :           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
   13431               9 :             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
   13432                 :           }
   13433              63 :           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
   13434              45 :             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
   13435                 :           }
   13436                 :         }
   13437                 :       }
   13438               9 :       db->pnBytesFreed = 0;
   13439               9 :       sqlite3BtreeLeaveAll(db);
   13440                 : 
   13441               9 :       *pHighwater = 0;
   13442               9 :       *pCurrent = nByte;
   13443               9 :       break;
   13444                 :     }
   13445                 : 
   13446                 :     /*
   13447                 :     ** *pCurrent gets an accurate estimate of the amount of memory used
   13448                 :     ** to store all prepared statements.
   13449                 :     ** *pHighwater is set to zero.
   13450                 :     */
   13451                 :     case SQLITE_DBSTATUS_STMT_USED: {
   13452                 :       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
   13453               9 :       int nByte = 0;              /* Used to accumulate return value */
   13454                 : 
   13455               9 :       db->pnBytesFreed = &nByte;
   13456              66 :       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
   13457              57 :         sqlite3VdbeDeleteObject(db, pVdbe);
   13458                 :       }
   13459               9 :       db->pnBytesFreed = 0;
   13460                 : 
   13461               9 :       *pHighwater = 0;
   13462               9 :       *pCurrent = nByte;
   13463                 : 
   13464               9 :       break;
   13465                 :     }
   13466                 : 
   13467                 :     /*
   13468                 :     ** Set *pCurrent to the total cache hits or misses encountered by all
   13469                 :     ** pagers the database handle is connected to. *pHighwater is always set 
   13470                 :     ** to zero.
   13471                 :     */
   13472                 :     case SQLITE_DBSTATUS_CACHE_HIT:
   13473                 :     case SQLITE_DBSTATUS_CACHE_MISS: {
   13474                 :       int i;
   13475               0 :       int nRet = 0;
   13476                 :       assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
   13477                 : 
   13478               0 :       for(i=0; i<db->nDb; i++){
   13479               0 :         if( db->aDb[i].pBt ){
   13480               0 :           Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
   13481               0 :           sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
   13482                 :         }
   13483                 :       }
   13484               0 :       *pHighwater = 0;
   13485               0 :       *pCurrent = nRet;
   13486               0 :       break;
   13487                 :     }
   13488                 : 
   13489                 :     default: {
   13490               0 :       rc = SQLITE_ERROR;
   13491                 :     }
   13492                 :   }
   13493              27 :   sqlite3_mutex_leave(db->mutex);
   13494              27 :   return rc;
   13495                 : }
   13496                 : 
   13497                 : /************** End of status.c **********************************************/
   13498                 : /************** Begin file date.c ********************************************/
   13499                 : /*
   13500                 : ** 2003 October 31
   13501                 : **
   13502                 : ** The author disclaims copyright to this source code.  In place of
   13503                 : ** a legal notice, here is a blessing:
   13504                 : **
   13505                 : **    May you do good and not evil.
   13506                 : **    May you find forgiveness for yourself and forgive others.
   13507                 : **    May you share freely, never taking more than you give.
   13508                 : **
   13509                 : *************************************************************************
   13510                 : ** This file contains the C functions that implement date and time
   13511                 : ** functions for SQLite.  
   13512                 : **
   13513                 : ** There is only one exported symbol in this file - the function
   13514                 : ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
   13515                 : ** All other code has file scope.
   13516                 : **
   13517                 : ** SQLite processes all times and dates as Julian Day numbers.  The
   13518                 : ** dates and times are stored as the number of days since noon
   13519                 : ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
   13520                 : ** calendar system. 
   13521                 : **
   13522                 : ** 1970-01-01 00:00:00 is JD 2440587.5
   13523                 : ** 2000-01-01 00:00:00 is JD 2451544.5
   13524                 : **
   13525                 : ** This implemention requires years to be expressed as a 4-digit number
   13526                 : ** which means that only dates between 0000-01-01 and 9999-12-31 can
   13527                 : ** be represented, even though julian day numbers allow a much wider
   13528                 : ** range of dates.
   13529                 : **
   13530                 : ** The Gregorian calendar system is used for all dates and times,
   13531                 : ** even those that predate the Gregorian calendar.  Historians usually
   13532                 : ** use the Julian calendar for dates prior to 1582-10-15 and for some
   13533                 : ** dates afterwards, depending on locale.  Beware of this difference.
   13534                 : **
   13535                 : ** The conversion algorithms are implemented based on descriptions
   13536                 : ** in the following text:
   13537                 : **
   13538                 : **      Jean Meeus
   13539                 : **      Astronomical Algorithms, 2nd Edition, 1998
   13540                 : **      ISBM 0-943396-61-1
   13541                 : **      Willmann-Bell, Inc
   13542                 : **      Richmond, Virginia (USA)
   13543                 : */
   13544                 : /* #include <stdlib.h> */
   13545                 : /* #include <assert.h> */
   13546                 : #include <time.h>
   13547                 : 
   13548                 : #ifndef SQLITE_OMIT_DATETIME_FUNCS
   13549                 : 
   13550                 : 
   13551                 : /*
   13552                 : ** A structure for holding a single date and time.
   13553                 : */
   13554                 : typedef struct DateTime DateTime;
   13555                 : struct DateTime {
   13556                 :   sqlite3_int64 iJD; /* The julian day number times 86400000 */
   13557                 :   int Y, M, D;       /* Year, month, and day */
   13558                 :   int h, m;          /* Hour and minutes */
   13559                 :   int tz;            /* Timezone offset in minutes */
   13560                 :   double s;          /* Seconds */
   13561                 :   char validYMD;     /* True (1) if Y,M,D are valid */
   13562                 :   char validHMS;     /* True (1) if h,m,s are valid */
   13563                 :   char validJD;      /* True (1) if iJD is valid */
   13564                 :   char validTZ;      /* True (1) if tz is valid */
   13565                 : };
   13566                 : 
   13567                 : 
   13568                 : /*
   13569                 : ** Convert zDate into one or more integers.  Additional arguments
   13570                 : ** come in groups of 5 as follows:
   13571                 : **
   13572                 : **       N       number of digits in the integer
   13573                 : **       min     minimum allowed value of the integer
   13574                 : **       max     maximum allowed value of the integer
   13575                 : **       nextC   first character after the integer
   13576                 : **       pVal    where to write the integers value.
   13577                 : **
   13578                 : ** Conversions continue until one with nextC==0 is encountered.
   13579                 : ** The function returns the number of successful conversions.
   13580                 : */
   13581           21310 : static int getDigits(const char *zDate, ...){
   13582                 :   va_list ap;
   13583                 :   int val;
   13584                 :   int N;
   13585                 :   int min;
   13586                 :   int max;
   13587                 :   int nextC;
   13588                 :   int *pVal;
   13589           21310 :   int cnt = 0;
   13590           21310 :   va_start(ap, zDate);
   13591                 :   do{
   13592           21310 :     N = va_arg(ap, int);
   13593           21310 :     min = va_arg(ap, int);
   13594           21310 :     max = va_arg(ap, int);
   13595           21310 :     nextC = va_arg(ap, int);
   13596           21310 :     pVal = va_arg(ap, int*);
   13597           21310 :     val = 0;
   13598           42620 :     while( N-- ){
   13599           21310 :       if( !sqlite3Isdigit(*zDate) ){
   13600           21310 :         goto end_getDigits;
   13601                 :       }
   13602               0 :       val = val*10 + *zDate - '0';
   13603               0 :       zDate++;
   13604                 :     }
   13605               0 :     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
   13606                 :       goto end_getDigits;
   13607                 :     }
   13608               0 :     *pVal = val;
   13609               0 :     zDate++;
   13610               0 :     cnt++;
   13611               0 :   }while( nextC );
   13612                 : end_getDigits:
   13613           21310 :   va_end(ap);
   13614           21310 :   return cnt;
   13615                 : }
   13616                 : 
   13617                 : /*
   13618                 : ** Parse a timezone extension on the end of a date-time.
   13619                 : ** The extension is of the form:
   13620                 : **
   13621                 : **        (+/-)HH:MM
   13622                 : **
   13623                 : ** Or the "zulu" notation:
   13624                 : **
   13625                 : **        Z
   13626                 : **
   13627                 : ** If the parse is successful, write the number of minutes
   13628                 : ** of change in p->tz and return 0.  If a parser error occurs,
   13629                 : ** return non-zero.
   13630                 : **
   13631                 : ** A missing specifier is not considered an error.
   13632                 : */
   13633               0 : static int parseTimezone(const char *zDate, DateTime *p){
   13634               0 :   int sgn = 0;
   13635                 :   int nHr, nMn;
   13636                 :   int c;
   13637               0 :   while( sqlite3Isspace(*zDate) ){ zDate++; }
   13638               0 :   p->tz = 0;
   13639               0 :   c = *zDate;
   13640               0 :   if( c=='-' ){
   13641               0 :     sgn = -1;
   13642               0 :   }else if( c=='+' ){
   13643               0 :     sgn = +1;
   13644               0 :   }else if( c=='Z' || c=='z' ){
   13645               0 :     zDate++;
   13646               0 :     goto zulu_time;
   13647                 :   }else{
   13648               0 :     return c!=0;
   13649                 :   }
   13650               0 :   zDate++;
   13651               0 :   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
   13652               0 :     return 1;
   13653                 :   }
   13654               0 :   zDate += 5;
   13655               0 :   p->tz = sgn*(nMn + nHr*60);
   13656                 : zulu_time:
   13657               0 :   while( sqlite3Isspace(*zDate) ){ zDate++; }
   13658               0 :   return *zDate!=0;
   13659                 : }
   13660                 : 
   13661                 : /*
   13662                 : ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
   13663                 : ** The HH, MM, and SS must each be exactly 2 digits.  The
   13664                 : ** fractional seconds FFFF can be one or more digits.
   13665                 : **
   13666                 : ** Return 1 if there is a parsing error and 0 on success.
   13667                 : */
   13668           10655 : static int parseHhMmSs(const char *zDate, DateTime *p){
   13669                 :   int h, m, s;
   13670           10655 :   double ms = 0.0;
   13671           10655 :   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
   13672           10655 :     return 1;
   13673                 :   }
   13674               0 :   zDate += 5;
   13675               0 :   if( *zDate==':' ){
   13676               0 :     zDate++;
   13677               0 :     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
   13678               0 :       return 1;
   13679                 :     }
   13680               0 :     zDate += 2;
   13681               0 :     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
   13682               0 :       double rScale = 1.0;
   13683               0 :       zDate++;
   13684               0 :       while( sqlite3Isdigit(*zDate) ){
   13685               0 :         ms = ms*10.0 + *zDate - '0';
   13686               0 :         rScale *= 10.0;
   13687               0 :         zDate++;
   13688                 :       }
   13689               0 :       ms /= rScale;
   13690                 :     }
   13691                 :   }else{
   13692               0 :     s = 0;
   13693                 :   }
   13694               0 :   p->validJD = 0;
   13695               0 :   p->validHMS = 1;
   13696               0 :   p->h = h;
   13697               0 :   p->m = m;
   13698               0 :   p->s = s + ms;
   13699               0 :   if( parseTimezone(zDate, p) ) return 1;
   13700               0 :   p->validTZ = (p->tz!=0)?1:0;
   13701               0 :   return 0;
   13702                 : }
   13703                 : 
   13704                 : /*
   13705                 : ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
   13706                 : ** that the YYYY-MM-DD is according to the Gregorian calendar.
   13707                 : **
   13708                 : ** Reference:  Meeus page 61
   13709                 : */
   13710          140018 : static void computeJD(DateTime *p){
   13711                 :   int Y, M, D, A, B, X1, X2;
   13712                 : 
   13713          140018 :   if( p->validJD ) return;
   13714           66505 :   if( p->validYMD ){
   13715           66505 :     Y = p->Y;
   13716           66505 :     M = p->M;
   13717           66505 :     D = p->D;
   13718                 :   }else{
   13719               0 :     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
   13720               0 :     M = 1;
   13721               0 :     D = 1;
   13722                 :   }
   13723           66505 :   if( M<=2 ){
   13724             688 :     Y--;
   13725             688 :     M += 12;
   13726                 :   }
   13727           66505 :   A = Y/100;
   13728           66505 :   B = 2 - A + (A/4);
   13729           66505 :   X1 = 36525*(Y+4716)/100;
   13730           66505 :   X2 = 306001*(M+1)/10000;
   13731           66505 :   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
   13732           66505 :   p->validJD = 1;
   13733           66505 :   if( p->validHMS ){
   13734           66505 :     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
   13735           66505 :     if( p->validTZ ){
   13736               0 :       p->iJD -= p->tz*60000;
   13737               0 :       p->validYMD = 0;
   13738               0 :       p->validHMS = 0;
   13739               0 :       p->validTZ = 0;
   13740                 :     }
   13741                 :   }
   13742                 : }
   13743                 : 
   13744                 : /*
   13745                 : ** Parse dates of the form
   13746                 : **
   13747                 : **     YYYY-MM-DD HH:MM:SS.FFF
   13748                 : **     YYYY-MM-DD HH:MM:SS
   13749                 : **     YYYY-MM-DD HH:MM
   13750                 : **     YYYY-MM-DD
   13751                 : **
   13752                 : ** Write the result into the DateTime structure and return 0
   13753                 : ** on success and 1 if the input string is not a well-formed
   13754                 : ** date.
   13755                 : */
   13756           10655 : static int parseYyyyMmDd(const char *zDate, DateTime *p){
   13757                 :   int Y, M, D, neg;
   13758                 : 
   13759           10655 :   if( zDate[0]=='-' ){
   13760               0 :     zDate++;
   13761               0 :     neg = 1;
   13762                 :   }else{
   13763           10655 :     neg = 0;
   13764                 :   }
   13765           10655 :   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
   13766           10655 :     return 1;
   13767                 :   }
   13768               0 :   zDate += 10;
   13769               0 :   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
   13770               0 :   if( parseHhMmSs(zDate, p)==0 ){
   13771                 :     /* We got the time */
   13772               0 :   }else if( *zDate==0 ){
   13773               0 :     p->validHMS = 0;
   13774                 :   }else{
   13775               0 :     return 1;
   13776                 :   }
   13777               0 :   p->validJD = 0;
   13778               0 :   p->validYMD = 1;
   13779               0 :   p->Y = neg ? -Y : Y;
   13780               0 :   p->M = M;
   13781               0 :   p->D = D;
   13782               0 :   if( p->validTZ ){
   13783               0 :     computeJD(p);
   13784                 :   }
   13785               0 :   return 0;
   13786                 : }
   13787                 : 
   13788                 : /*
   13789                 : ** Set the time to the current time reported by the VFS.
   13790                 : **
   13791                 : ** Return the number of errors.
   13792                 : */
   13793           10655 : static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
   13794           10655 :   sqlite3 *db = sqlite3_context_db_handle(context);
   13795           10655 :   if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){
   13796           10655 :     p->validJD = 1;
   13797           10655 :     return 0;
   13798                 :   }else{
   13799               0 :     return 1;
   13800                 :   }
   13801                 : }
   13802                 : 
   13803                 : /*
   13804                 : ** Attempt to parse the given string into a Julian Day Number.  Return
   13805                 : ** the number of errors.
   13806                 : **
   13807                 : ** The following are acceptable forms for the input string:
   13808                 : **
   13809                 : **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
   13810                 : **      DDDD.DD 
   13811                 : **      now
   13812                 : **
   13813                 : ** In the first form, the +/-HH:MM is always optional.  The fractional
   13814                 : ** seconds extension (the ".FFF") is optional.  The seconds portion
   13815                 : ** (":SS.FFF") is option.  The year and date can be omitted as long
   13816                 : ** as there is a time string.  The time string can be omitted as long
   13817                 : ** as there is a year and date.
   13818                 : */
   13819           10655 : static int parseDateOrTime(
   13820                 :   sqlite3_context *context, 
   13821                 :   const char *zDate, 
   13822                 :   DateTime *p
   13823                 : ){
   13824                 :   double r;
   13825           10655 :   if( parseYyyyMmDd(zDate,p)==0 ){
   13826               0 :     return 0;
   13827           10655 :   }else if( parseHhMmSs(zDate, p)==0 ){
   13828               0 :     return 0;
   13829           10655 :   }else if( sqlite3StrICmp(zDate,"now")==0){
   13830           10655 :     return setDateTimeToCurrent(context, p);
   13831               0 :   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
   13832               0 :     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
   13833               0 :     p->validJD = 1;
   13834               0 :     return 0;
   13835                 :   }
   13836               0 :   return 1;
   13837                 : }
   13838                 : 
   13839                 : /*
   13840                 : ** Compute the Year, Month, and Day from the julian day number.
   13841                 : */
   13842           45227 : static void computeYMD(DateTime *p){
   13843                 :   int Z, A, B, C, D, E, X1;
   13844           45227 :   if( p->validYMD ) return;
   13845           44085 :   if( !p->validJD ){
   13846               0 :     p->Y = 2000;
   13847               0 :     p->M = 1;
   13848               0 :     p->D = 1;
   13849                 :   }else{
   13850           44085 :     Z = (int)((p->iJD + 43200000)/86400000);
   13851           44085 :     A = (int)((Z - 1867216.25)/36524.25);
   13852           44085 :     A = Z + 1 + A - (A/4);
   13853           44085 :     B = A + 1524;
   13854           44085 :     C = (int)((B - 122.1)/365.25);
   13855           44085 :     D = (36525*C)/100;
   13856           44085 :     E = (int)((B-D)/30.6001);
   13857           44085 :     X1 = (int)(30.6001*E);
   13858           44085 :     p->D = B - D - X1;
   13859           44085 :     p->M = E<14 ? E-1 : E-13;
   13860           44085 :     p->Y = p->M>2 ? C - 4716 : C - 4715;
   13861                 :   }
   13862           44085 :   p->validYMD = 1;
   13863                 : }
   13864                 : 
   13865                 : /*
   13866                 : ** Compute the Hour, Minute, and Seconds from the julian day number.
   13867                 : */
   13868           43242 : static void computeHMS(DateTime *p){
   13869                 :   int s;
   13870           43242 :   if( p->validHMS ) return;
   13871           42100 :   computeJD(p);
   13872           42100 :   s = (int)((p->iJD + 43200000) % 86400000);
   13873           42100 :   p->s = s/1000.0;
   13874           42100 :   s = (int)p->s;
   13875           42100 :   p->s -= s;
   13876           42100 :   p->h = s/3600;
   13877           42100 :   s -= p->h*3600;
   13878           42100 :   p->m = s/60;
   13879           42100 :   p->s += s - p->m*60;
   13880           42100 :   p->validHMS = 1;
   13881                 : }
   13882                 : 
   13883                 : /*
   13884                 : ** Compute both YMD and HMS
   13885                 : */
   13886           43242 : static void computeYMD_HMS(DateTime *p){
   13887           43242 :   computeYMD(p);
   13888           43242 :   computeHMS(p);
   13889           43242 : }
   13890                 : 
   13891                 : /*
   13892                 : ** Clear the YMD and HMS and the TZ
   13893                 : */
   13894           22775 : static void clearYMD_HMS_TZ(DateTime *p){
   13895           22775 :   p->validYMD = 0;
   13896           22775 :   p->validHMS = 0;
   13897           22775 :   p->validTZ = 0;
   13898           22775 : }
   13899                 : 
   13900                 : /*
   13901                 : ** On recent Windows platforms, the localtime_s() function is available
   13902                 : ** as part of the "Secure CRT". It is essentially equivalent to 
   13903                 : ** localtime_r() available under most POSIX platforms, except that the 
   13904                 : ** order of the parameters is reversed.
   13905                 : **
   13906                 : ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
   13907                 : **
   13908                 : ** If the user has not indicated to use localtime_r() or localtime_s()
   13909                 : ** already, check for an MSVC build environment that provides 
   13910                 : ** localtime_s().
   13911                 : */
   13912                 : #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
   13913                 :      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
   13914                 : #define HAVE_LOCALTIME_S 1
   13915                 : #endif
   13916                 : 
   13917                 : #ifndef SQLITE_OMIT_LOCALTIME
   13918                 : /*
   13919                 : ** The following routine implements the rough equivalent of localtime_r()
   13920                 : ** using whatever operating-system specific localtime facility that
   13921                 : ** is available.  This routine returns 0 on success and
   13922                 : ** non-zero on any kind of error.
   13923                 : **
   13924                 : ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
   13925                 : ** routine will always fail.
   13926                 : */
   13927           31965 : static int osLocaltime(time_t *t, struct tm *pTm){
   13928                 :   int rc;
   13929                 : #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
   13930                 :       && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
   13931                 :   struct tm *pX;
   13932                 : #if SQLITE_THREADSAFE>0
   13933                 :   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   13934                 : #endif
   13935                 :   sqlite3_mutex_enter(mutex);
   13936                 :   pX = localtime(t);
   13937                 : #ifndef SQLITE_OMIT_BUILTIN_TEST
   13938                 :   if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
   13939                 : #endif
   13940                 :   if( pX ) *pTm = *pX;
   13941                 :   sqlite3_mutex_leave(mutex);
   13942                 :   rc = pX==0;
   13943                 : #else
   13944                 : #ifndef SQLITE_OMIT_BUILTIN_TEST
   13945           31965 :   if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
   13946                 : #endif
   13947                 : #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
   13948           31965 :   rc = localtime_r(t, pTm)==0;
   13949                 : #else
   13950                 :   rc = localtime_s(pTm, t);
   13951                 : #endif /* HAVE_LOCALTIME_R */
   13952                 : #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
   13953           31965 :   return rc;
   13954                 : }
   13955                 : #endif /* SQLITE_OMIT_LOCALTIME */
   13956                 : 
   13957                 : 
   13958                 : #ifndef SQLITE_OMIT_LOCALTIME
   13959                 : /*
   13960                 : ** Compute the difference (in milliseconds) between localtime and UTC
   13961                 : ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
   13962                 : ** return this value and set *pRc to SQLITE_OK. 
   13963                 : **
   13964                 : ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
   13965                 : ** is undefined in this case.
   13966                 : */
   13967           31965 : static sqlite3_int64 localtimeOffset(
   13968                 :   DateTime *p,                    /* Date at which to calculate offset */
   13969                 :   sqlite3_context *pCtx,          /* Write error here if one occurs */
   13970                 :   int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
   13971                 : ){
   13972                 :   DateTime x, y;
   13973                 :   time_t t;
   13974                 :   struct tm sLocal;
   13975                 : 
   13976                 :   /* Initialize the contents of sLocal to avoid a compiler warning. */
   13977           31965 :   memset(&sLocal, 0, sizeof(sLocal));
   13978                 : 
   13979           31965 :   x = *p;
   13980           31965 :   computeYMD_HMS(&x);
   13981           31965 :   if( x.Y<1971 || x.Y>=2038 ){
   13982               0 :     x.Y = 2000;
   13983               0 :     x.M = 1;
   13984               0 :     x.D = 1;
   13985               0 :     x.h = 0;
   13986               0 :     x.m = 0;
   13987               0 :     x.s = 0.0;
   13988                 :   } else {
   13989           31965 :     int s = (int)(x.s + 0.5);
   13990           31965 :     x.s = s;
   13991                 :   }
   13992           31965 :   x.tz = 0;
   13993           31965 :   x.validJD = 0;
   13994           31965 :   computeJD(&x);
   13995           31965 :   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
   13996           31965 :   if( osLocaltime(&t, &sLocal) ){
   13997               0 :     sqlite3_result_error(pCtx, "local time unavailable", -1);
   13998               0 :     *pRc = SQLITE_ERROR;
   13999               0 :     return 0;
   14000                 :   }
   14001           31965 :   y.Y = sLocal.tm_year + 1900;
   14002           31965 :   y.M = sLocal.tm_mon + 1;
   14003           31965 :   y.D = sLocal.tm_mday;
   14004           31965 :   y.h = sLocal.tm_hour;
   14005           31965 :   y.m = sLocal.tm_min;
   14006           31965 :   y.s = sLocal.tm_sec;
   14007           31965 :   y.validYMD = 1;
   14008           31965 :   y.validHMS = 1;
   14009           31965 :   y.validJD = 0;
   14010           31965 :   y.validTZ = 0;
   14011           31965 :   computeJD(&y);
   14012           31965 :   *pRc = SQLITE_OK;
   14013           31965 :   return y.iJD - x.iJD;
   14014                 : }
   14015                 : #endif /* SQLITE_OMIT_LOCALTIME */
   14016                 : 
   14017                 : /*
   14018                 : ** Process a modifier to a date-time stamp.  The modifiers are
   14019                 : ** as follows:
   14020                 : **
   14021                 : **     NNN days
   14022                 : **     NNN hours
   14023                 : **     NNN minutes
   14024                 : **     NNN.NNNN seconds
   14025                 : **     NNN months
   14026                 : **     NNN years
   14027                 : **     start of month
   14028                 : **     start of year
   14029                 : **     start of week
   14030                 : **     start of day
   14031                 : **     weekday N
   14032                 : **     unixepoch
   14033                 : **     localtime
   14034                 : **     utc
   14035                 : **
   14036                 : ** Return 0 on success and 1 if there is any kind of error. If the error
   14037                 : ** is in a system call (i.e. localtime()), then an error message is written
   14038                 : ** to context pCtx. If the error is an unrecognized modifier, no error is
   14039                 : ** written to pCtx.
   14040                 : */
   14041           24760 : static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
   14042           24760 :   int rc = 1;
   14043                 :   int n;
   14044                 :   double r;
   14045                 :   char *z, zBuf[30];
   14046           24760 :   z = zBuf;
   14047          188884 :   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
   14048          164124 :     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
   14049                 :   }
   14050           24760 :   z[n] = 0;
   14051           24760 :   switch( z[0] ){
   14052                 : #ifndef SQLITE_OMIT_LOCALTIME
   14053                 :     case 'l': {
   14054                 :       /*    localtime
   14055                 :       **
   14056                 :       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
   14057                 :       ** show local time.
   14058                 :       */
   14059           10655 :       if( strcmp(z, "localtime")==0 ){
   14060           10655 :         computeJD(p);
   14061           10655 :         p->iJD += localtimeOffset(p, pCtx, &rc);
   14062           10655 :         clearYMD_HMS_TZ(p);
   14063                 :       }
   14064           10655 :       break;
   14065                 :     }
   14066                 : #endif
   14067                 :     case 'u': {
   14068                 :       /*
   14069                 :       **    unixepoch
   14070                 :       **
   14071                 :       ** Treat the current value of p->iJD as the number of
   14072                 :       ** seconds since 1970.  Convert to a real julian day number.
   14073                 :       */
   14074           10687 :       if( strcmp(z, "unixepoch")==0 && p->validJD ){
   14075              32 :         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
   14076              32 :         clearYMD_HMS_TZ(p);
   14077              32 :         rc = 0;
   14078                 :       }
   14079                 : #ifndef SQLITE_OMIT_LOCALTIME
   14080           10655 :       else if( strcmp(z, "utc")==0 ){
   14081                 :         sqlite3_int64 c1;
   14082           10655 :         computeJD(p);
   14083           10655 :         c1 = localtimeOffset(p, pCtx, &rc);
   14084           10655 :         if( rc==SQLITE_OK ){
   14085           10655 :           p->iJD -= c1;
   14086           10655 :           clearYMD_HMS_TZ(p);
   14087           10655 :           p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
   14088                 :         }
   14089                 :       }
   14090                 : #endif
   14091           10687 :       break;
   14092                 :     }
   14093                 :     case 'w': {
   14094                 :       /*
   14095                 :       **    weekday N
   14096                 :       **
   14097                 :       ** Move the date to the same time on the next occurrence of
   14098                 :       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
   14099                 :       ** date is already on the appropriate weekday, this is a no-op.
   14100                 :       */
   14101               0 :       if( strncmp(z, "weekday ", 8)==0
   14102               0 :                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
   14103               0 :                && (n=(int)r)==r && n>=0 && r<7 ){
   14104                 :         sqlite3_int64 Z;
   14105               0 :         computeYMD_HMS(p);
   14106               0 :         p->validTZ = 0;
   14107               0 :         p->validJD = 0;
   14108               0 :         computeJD(p);
   14109               0 :         Z = ((p->iJD + 129600000)/86400000) % 7;
   14110               0 :         if( Z>n ) Z -= 7;
   14111               0 :         p->iJD += (n - Z)*86400000;
   14112               0 :         clearYMD_HMS_TZ(p);
   14113               0 :         rc = 0;
   14114                 :       }
   14115               0 :       break;
   14116                 :     }
   14117                 :     case 's': {
   14118                 :       /*
   14119                 :       **    start of TTTTT
   14120                 :       **
   14121                 :       ** Move the date backwards to the beginning of the current day,
   14122                 :       ** or month or year.
   14123                 :       */
   14124            1985 :       if( strncmp(z, "start of ", 9)!=0 ) break;
   14125            1985 :       z += 9;
   14126            1985 :       computeYMD(p);
   14127            1985 :       p->validHMS = 1;
   14128            1985 :       p->h = p->m = 0;
   14129            1985 :       p->s = 0.0;
   14130            1985 :       p->validTZ = 0;
   14131            1985 :       p->validJD = 0;
   14132            1985 :       if( strcmp(z,"month")==0 ){
   14133             702 :         p->D = 1;
   14134             702 :         rc = 0;
   14135            1283 :       }else if( strcmp(z,"year")==0 ){
   14136               0 :         computeYMD(p);
   14137               0 :         p->M = 1;
   14138               0 :         p->D = 1;
   14139               0 :         rc = 0;
   14140            1283 :       }else if( strcmp(z,"day")==0 ){
   14141            1283 :         rc = 0;
   14142                 :       }
   14143            1985 :       break;
   14144                 :     }
   14145                 :     case '+':
   14146                 :     case '-':
   14147                 :     case '0':
   14148                 :     case '1':
   14149                 :     case '2':
   14150                 :     case '3':
   14151                 :     case '4':
   14152                 :     case '5':
   14153                 :     case '6':
   14154                 :     case '7':
   14155                 :     case '8':
   14156                 :     case '9': {
   14157                 :       double rRounder;
   14158            1433 :       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
   14159            1433 :       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
   14160               0 :         rc = 1;
   14161               0 :         break;
   14162                 :       }
   14163            1433 :       if( z[n]==':' ){
   14164                 :         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
   14165                 :         ** specified number of hours, minutes, seconds, and fractional seconds
   14166                 :         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
   14167                 :         ** omitted.
   14168                 :         */
   14169               0 :         const char *z2 = z;
   14170                 :         DateTime tx;
   14171                 :         sqlite3_int64 day;
   14172               0 :         if( !sqlite3Isdigit(*z2) ) z2++;
   14173               0 :         memset(&tx, 0, sizeof(tx));
   14174               0 :         if( parseHhMmSs(z2, &tx) ) break;
   14175               0 :         computeJD(&tx);
   14176               0 :         tx.iJD -= 43200000;
   14177               0 :         day = tx.iJD/86400000;
   14178               0 :         tx.iJD -= day*86400000;
   14179               0 :         if( z[0]=='-' ) tx.iJD = -tx.iJD;
   14180               0 :         computeJD(p);
   14181               0 :         clearYMD_HMS_TZ(p);
   14182               0 :         p->iJD += tx.iJD;
   14183               0 :         rc = 0;
   14184               0 :         break;
   14185                 :       }
   14186            1433 :       z += n;
   14187            1433 :       while( sqlite3Isspace(*z) ) z++;
   14188            1433 :       n = sqlite3Strlen30(z);
   14189            1433 :       if( n>10 || n<3 ) break;
   14190            1433 :       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
   14191            1433 :       computeJD(p);
   14192            1433 :       rc = 0;
   14193            1433 :       rRounder = r<0 ? -0.5 : +0.5;
   14194            1433 :       if( n==3 && strcmp(z,"day")==0 ){
   14195             843 :         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
   14196             590 :       }else if( n==4 && strcmp(z,"hour")==0 ){
   14197               0 :         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
   14198             590 :       }else if( n==6 && strcmp(z,"minute")==0 ){
   14199               0 :         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
   14200             590 :       }else if( n==6 && strcmp(z,"second")==0 ){
   14201               0 :         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
   14202            1180 :       }else if( n==5 && strcmp(z,"month")==0 ){
   14203                 :         int x, y;
   14204             590 :         computeYMD_HMS(p);
   14205             590 :         p->M += (int)r;
   14206             590 :         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
   14207             590 :         p->Y += x;
   14208             590 :         p->M -= x*12;
   14209             590 :         p->validJD = 0;
   14210             590 :         computeJD(p);
   14211             590 :         y = (int)r;
   14212             590 :         if( y!=r ){
   14213               0 :           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
   14214                 :         }
   14215               0 :       }else if( n==4 && strcmp(z,"year")==0 ){
   14216               0 :         int y = (int)r;
   14217               0 :         computeYMD_HMS(p);
   14218               0 :         p->Y += y;
   14219               0 :         p->validJD = 0;
   14220               0 :         computeJD(p);
   14221               0 :         if( y!=r ){
   14222               0 :           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
   14223                 :         }
   14224                 :       }else{
   14225               0 :         rc = 1;
   14226                 :       }
   14227            1433 :       clearYMD_HMS_TZ(p);
   14228            1433 :       break;
   14229                 :     }
   14230                 :     default: {
   14231               0 :       break;
   14232                 :     }
   14233                 :   }
   14234           24760 :   return rc;
   14235                 : }
   14236                 : 
   14237                 : /*
   14238                 : ** Process time function arguments.  argv[0] is a date-time stamp.
   14239                 : ** argv[1] and following are modifiers.  Parse them all and write
   14240                 : ** the resulting time into the DateTime structure p.  Return 0
   14241                 : ** on success and 1 if there are any errors.
   14242                 : **
   14243                 : ** If there are zero parameters (if even argv[0] is undefined)
   14244                 : ** then assume a default value of "now" for argv[0].
   14245                 : */
   14246           10687 : static int isDate(
   14247                 :   sqlite3_context *context, 
   14248                 :   int argc, 
   14249                 :   sqlite3_value **argv, 
   14250                 :   DateTime *p
   14251                 : ){
   14252                 :   int i;
   14253                 :   const unsigned char *z;
   14254                 :   int eType;
   14255           10687 :   memset(p, 0, sizeof(*p));
   14256           10687 :   if( argc==0 ){
   14257               0 :     return setDateTimeToCurrent(context, p);
   14258                 :   }
   14259           10687 :   if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
   14260           10687 :                    || eType==SQLITE_INTEGER ){
   14261              32 :     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
   14262              32 :     p->validJD = 1;
   14263                 :   }else{
   14264           10655 :     z = sqlite3_value_text(argv[0]);
   14265           10655 :     if( !z || parseDateOrTime(context, (char*)z, p) ){
   14266               0 :       return 1;
   14267                 :     }
   14268                 :   }
   14269           35447 :   for(i=1; i<argc; i++){
   14270           24760 :     z = sqlite3_value_text(argv[i]);
   14271           24760 :     if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
   14272                 :   }
   14273           10687 :   return 0;
   14274                 : }
   14275                 : 
   14276                 : 
   14277                 : /*
   14278                 : ** The following routines implement the various date and time functions
   14279                 : ** of SQLite.
   14280                 : */
   14281                 : 
   14282                 : /*
   14283                 : **    julianday( TIMESTRING, MOD, MOD, ...)
   14284                 : **
   14285                 : ** Return the julian day number of the date specified in the arguments
   14286                 : */
   14287               0 : static void juliandayFunc(
   14288                 :   sqlite3_context *context,
   14289                 :   int argc,
   14290                 :   sqlite3_value **argv
   14291                 : ){
   14292                 :   DateTime x;
   14293               0 :   if( isDate(context, argc, argv, &x)==0 ){
   14294               0 :     computeJD(&x);
   14295               0 :     sqlite3_result_double(context, x.iJD/86400000.0);
   14296                 :   }
   14297               0 : }
   14298                 : 
   14299                 : /*
   14300                 : **    datetime( TIMESTRING, MOD, MOD, ...)
   14301                 : **
   14302                 : ** Return YYYY-MM-DD HH:MM:SS
   14303                 : */
   14304              32 : static void datetimeFunc(
   14305                 :   sqlite3_context *context,
   14306                 :   int argc,
   14307                 :   sqlite3_value **argv
   14308                 : ){
   14309                 :   DateTime x;
   14310              32 :   if( isDate(context, argc, argv, &x)==0 ){
   14311                 :     char zBuf[100];
   14312              32 :     computeYMD_HMS(&x);
   14313              32 :     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
   14314              32 :                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
   14315              32 :     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   14316                 :   }
   14317              32 : }
   14318                 : 
   14319                 : /*
   14320                 : **    time( TIMESTRING, MOD, MOD, ...)
   14321                 : **
   14322                 : ** Return HH:MM:SS
   14323                 : */
   14324               0 : static void timeFunc(
   14325                 :   sqlite3_context *context,
   14326                 :   int argc,
   14327                 :   sqlite3_value **argv
   14328                 : ){
   14329                 :   DateTime x;
   14330               0 :   if( isDate(context, argc, argv, &x)==0 ){
   14331                 :     char zBuf[100];
   14332               0 :     computeHMS(&x);
   14333               0 :     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
   14334               0 :     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   14335                 :   }
   14336               0 : }
   14337                 : 
   14338                 : /*
   14339                 : **    date( TIMESTRING, MOD, MOD, ...)
   14340                 : **
   14341                 : ** Return YYYY-MM-DD
   14342                 : */
   14343               0 : static void dateFunc(
   14344                 :   sqlite3_context *context,
   14345                 :   int argc,
   14346                 :   sqlite3_value **argv
   14347                 : ){
   14348                 :   DateTime x;
   14349               0 :   if( isDate(context, argc, argv, &x)==0 ){
   14350                 :     char zBuf[100];
   14351               0 :     computeYMD(&x);
   14352               0 :     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
   14353               0 :     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   14354                 :   }
   14355               0 : }
   14356                 : 
   14357                 : /*
   14358                 : **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
   14359                 : **
   14360                 : ** Return a string described by FORMAT.  Conversions as follows:
   14361                 : **
   14362                 : **   %d  day of month
   14363                 : **   %f  ** fractional seconds  SS.SSS
   14364                 : **   %H  hour 00-24
   14365                 : **   %j  day of year 000-366
   14366                 : **   %J  ** Julian day number
   14367                 : **   %m  month 01-12
   14368                 : **   %M  minute 00-59
   14369                 : **   %s  seconds since 1970-01-01
   14370                 : **   %S  seconds 00-59
   14371                 : **   %w  day of week 0-6  sunday==0
   14372                 : **   %W  week of year 00-53
   14373                 : **   %Y  year 0000-9999
   14374                 : **   %%  %
   14375                 : */
   14376           10655 : static void strftimeFunc(
   14377                 :   sqlite3_context *context,
   14378                 :   int argc,
   14379                 :   sqlite3_value **argv
   14380                 : ){
   14381                 :   DateTime x;
   14382                 :   u64 n;
   14383                 :   size_t i,j;
   14384                 :   char *z;
   14385                 :   sqlite3 *db;
   14386           10655 :   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
   14387                 :   char zBuf[100];
   14388           10655 :   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
   14389           10655 :   db = sqlite3_context_db_handle(context);
   14390           21310 :   for(i=0, n=1; zFmt[i]; i++, n++){
   14391           10655 :     if( zFmt[i]=='%' ){
   14392           10655 :       switch( zFmt[i+1] ){
   14393                 :         case 'd':
   14394                 :         case 'H':
   14395                 :         case 'm':
   14396                 :         case 'M':
   14397                 :         case 'S':
   14398                 :         case 'W':
   14399               0 :           n++;
   14400                 :           /* fall thru */
   14401                 :         case 'w':
   14402                 :         case '%':
   14403               0 :           break;
   14404                 :         case 'f':
   14405               0 :           n += 8;
   14406               0 :           break;
   14407                 :         case 'j':
   14408               0 :           n += 3;
   14409               0 :           break;
   14410                 :         case 'Y':
   14411               0 :           n += 8;
   14412               0 :           break;
   14413                 :         case 's':
   14414                 :         case 'J':
   14415           10655 :           n += 50;
   14416           10655 :           break;
   14417                 :         default:
   14418               0 :           return;  /* ERROR.  return a NULL */
   14419                 :       }
   14420           10655 :       i++;
   14421                 :     }
   14422                 :   }
   14423                 :   testcase( n==sizeof(zBuf)-1 );
   14424                 :   testcase( n==sizeof(zBuf) );
   14425                 :   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
   14426                 :   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
   14427           10655 :   if( n<sizeof(zBuf) ){
   14428           10655 :     z = zBuf;
   14429               0 :   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
   14430               0 :     sqlite3_result_error_toobig(context);
   14431               0 :     return;
   14432                 :   }else{
   14433               0 :     z = sqlite3DbMallocRaw(db, (int)n);
   14434               0 :     if( z==0 ){
   14435               0 :       sqlite3_result_error_nomem(context);
   14436               0 :       return;
   14437                 :     }
   14438                 :   }
   14439           10655 :   computeJD(&x);
   14440           10655 :   computeYMD_HMS(&x);
   14441           21310 :   for(i=j=0; zFmt[i]; i++){
   14442           10655 :     if( zFmt[i]!='%' ){
   14443               0 :       z[j++] = zFmt[i];
   14444                 :     }else{
   14445           10655 :       i++;
   14446           10655 :       switch( zFmt[i] ){
   14447               0 :         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
   14448                 :         case 'f': {
   14449               0 :           double s = x.s;
   14450               0 :           if( s>59.999 ) s = 59.999;
   14451               0 :           sqlite3_snprintf(7, &z[j],"%06.3f", s);
   14452               0 :           j += sqlite3Strlen30(&z[j]);
   14453               0 :           break;
   14454                 :         }
   14455               0 :         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
   14456                 :         case 'W': /* Fall thru */
   14457                 :         case 'j': {
   14458                 :           int nDay;             /* Number of days since 1st day of year */
   14459               0 :           DateTime y = x;
   14460               0 :           y.validJD = 0;
   14461               0 :           y.M = 1;
   14462               0 :           y.D = 1;
   14463               0 :           computeJD(&y);
   14464               0 :           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
   14465               0 :           if( zFmt[i]=='W' ){
   14466                 :             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
   14467               0 :             wd = (int)(((x.iJD+43200000)/86400000)%7);
   14468               0 :             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
   14469               0 :             j += 2;
   14470                 :           }else{
   14471               0 :             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
   14472               0 :             j += 3;
   14473                 :           }
   14474               0 :           break;
   14475                 :         }
   14476                 :         case 'J': {
   14477               0 :           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
   14478               0 :           j+=sqlite3Strlen30(&z[j]);
   14479               0 :           break;
   14480                 :         }
   14481               0 :         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
   14482               0 :         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
   14483                 :         case 's': {
   14484           10655 :           sqlite3_snprintf(30,&z[j],"%lld",
   14485           10655 :                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
   14486           10655 :           j += sqlite3Strlen30(&z[j]);
   14487           10655 :           break;
   14488                 :         }
   14489               0 :         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
   14490                 :         case 'w': {
   14491               0 :           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
   14492               0 :           break;
   14493                 :         }
   14494                 :         case 'Y': {
   14495               0 :           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
   14496               0 :           break;
   14497                 :         }
   14498               0 :         default:   z[j++] = '%'; break;
   14499                 :       }
   14500                 :     }
   14501                 :   }
   14502           10655 :   z[j] = 0;
   14503           10655 :   sqlite3_result_text(context, z, -1,
   14504                 :                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
   14505                 : }
   14506                 : 
   14507                 : /*
   14508                 : ** current_time()
   14509                 : **
   14510                 : ** This function returns the same value as time('now').
   14511                 : */
   14512               0 : static void ctimeFunc(
   14513                 :   sqlite3_context *context,
   14514                 :   int NotUsed,
   14515                 :   sqlite3_value **NotUsed2
   14516                 : ){
   14517                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   14518               0 :   timeFunc(context, 0, 0);
   14519               0 : }
   14520                 : 
   14521                 : /*
   14522                 : ** current_date()
   14523                 : **
   14524                 : ** This function returns the same value as date('now').
   14525                 : */
   14526               0 : static void cdateFunc(
   14527                 :   sqlite3_context *context,
   14528                 :   int NotUsed,
   14529                 :   sqlite3_value **NotUsed2
   14530                 : ){
   14531                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   14532               0 :   dateFunc(context, 0, 0);
   14533               0 : }
   14534                 : 
   14535                 : /*
   14536                 : ** current_timestamp()
   14537                 : **
   14538                 : ** This function returns the same value as datetime('now').
   14539                 : */
   14540               0 : static void ctimestampFunc(
   14541                 :   sqlite3_context *context,
   14542                 :   int NotUsed,
   14543                 :   sqlite3_value **NotUsed2
   14544                 : ){
   14545                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   14546               0 :   datetimeFunc(context, 0, 0);
   14547               0 : }
   14548                 : #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
   14549                 : 
   14550                 : #ifdef SQLITE_OMIT_DATETIME_FUNCS
   14551                 : /*
   14552                 : ** If the library is compiled to omit the full-scale date and time
   14553                 : ** handling (to get a smaller binary), the following minimal version
   14554                 : ** of the functions current_time(), current_date() and current_timestamp()
   14555                 : ** are included instead. This is to support column declarations that
   14556                 : ** include "DEFAULT CURRENT_TIME" etc.
   14557                 : **
   14558                 : ** This function uses the C-library functions time(), gmtime()
   14559                 : ** and strftime(). The format string to pass to strftime() is supplied
   14560                 : ** as the user-data for the function.
   14561                 : */
   14562                 : static void currentTimeFunc(
   14563                 :   sqlite3_context *context,
   14564                 :   int argc,
   14565                 :   sqlite3_value **argv
   14566                 : ){
   14567                 :   time_t t;
   14568                 :   char *zFormat = (char *)sqlite3_user_data(context);
   14569                 :   sqlite3 *db;
   14570                 :   sqlite3_int64 iT;
   14571                 :   struct tm *pTm;
   14572                 :   struct tm sNow;
   14573                 :   char zBuf[20];
   14574                 : 
   14575                 :   UNUSED_PARAMETER(argc);
   14576                 :   UNUSED_PARAMETER(argv);
   14577                 : 
   14578                 :   db = sqlite3_context_db_handle(context);
   14579                 :   if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return;
   14580                 :   t = iT/1000 - 10000*(sqlite3_int64)21086676;
   14581                 : #ifdef HAVE_GMTIME_R
   14582                 :   pTm = gmtime_r(&t, &sNow);
   14583                 : #else
   14584                 :   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   14585                 :   pTm = gmtime(&t);
   14586                 :   if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
   14587                 :   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   14588                 : #endif
   14589                 :   if( pTm ){
   14590                 :     strftime(zBuf, 20, zFormat, &sNow);
   14591                 :     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
   14592                 :   }
   14593                 : }
   14594                 : #endif
   14595                 : 
   14596                 : /*
   14597                 : ** This function registered all of the above C functions as SQL
   14598                 : ** functions.  This should be the only routine in this file with
   14599                 : ** external linkage.
   14600                 : */
   14601             806 : SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
   14602                 :   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
   14603                 : #ifndef SQLITE_OMIT_DATETIME_FUNCS
   14604                 :     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
   14605                 :     FUNCTION(date,             -1, 0, 0, dateFunc      ),
   14606                 :     FUNCTION(time,             -1, 0, 0, timeFunc      ),
   14607                 :     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
   14608                 :     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
   14609                 :     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
   14610                 :     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
   14611                 :     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
   14612                 : #else
   14613                 :     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
   14614                 :     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
   14615                 :     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
   14616                 : #endif
   14617                 :   };
   14618                 :   int i;
   14619             806 :   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
   14620             806 :   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
   14621                 : 
   14622            7254 :   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
   14623            6448 :     sqlite3FuncDefInsert(pHash, &aFunc[i]);
   14624                 :   }
   14625             806 : }
   14626                 : 
   14627                 : /************** End of date.c ************************************************/
   14628                 : /************** Begin file os.c **********************************************/
   14629                 : /*
   14630                 : ** 2005 November 29
   14631                 : **
   14632                 : ** The author disclaims copyright to this source code.  In place of
   14633                 : ** a legal notice, here is a blessing:
   14634                 : **
   14635                 : **    May you do good and not evil.
   14636                 : **    May you find forgiveness for yourself and forgive others.
   14637                 : **    May you share freely, never taking more than you give.
   14638                 : **
   14639                 : ******************************************************************************
   14640                 : **
   14641                 : ** This file contains OS interface code that is common to all
   14642                 : ** architectures.
   14643                 : */
   14644                 : #define _SQLITE_OS_C_ 1
   14645                 : #undef _SQLITE_OS_C_
   14646                 : 
   14647                 : /*
   14648                 : ** The default SQLite sqlite3_vfs implementations do not allocate
   14649                 : ** memory (actually, os_unix.c allocates a small amount of memory
   14650                 : ** from within OsOpen()), but some third-party implementations may.
   14651                 : ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
   14652                 : ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
   14653                 : **
   14654                 : ** The following functions are instrumented for malloc() failure 
   14655                 : ** testing:
   14656                 : **
   14657                 : **     sqlite3OsRead()
   14658                 : **     sqlite3OsWrite()
   14659                 : **     sqlite3OsSync()
   14660                 : **     sqlite3OsFileSize()
   14661                 : **     sqlite3OsLock()
   14662                 : **     sqlite3OsCheckReservedLock()
   14663                 : **     sqlite3OsFileControl()
   14664                 : **     sqlite3OsShmMap()
   14665                 : **     sqlite3OsOpen()
   14666                 : **     sqlite3OsDelete()
   14667                 : **     sqlite3OsAccess()
   14668                 : **     sqlite3OsFullPathname()
   14669                 : **
   14670                 : */
   14671                 : #if defined(SQLITE_TEST)
   14672                 : SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
   14673                 :   #define DO_OS_MALLOC_TEST(x)                                       \
   14674                 :   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
   14675                 :     void *pTstAlloc = sqlite3Malloc(10);                             \
   14676                 :     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
   14677                 :     sqlite3_free(pTstAlloc);                                         \
   14678                 :   }
   14679                 : #else
   14680                 :   #define DO_OS_MALLOC_TEST(x)
   14681                 : #endif
   14682                 : 
   14683                 : /*
   14684                 : ** The following routines are convenience wrappers around methods
   14685                 : ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
   14686                 : ** of this would be completely automatic if SQLite were coded using
   14687                 : ** C++ instead of plain old C.
   14688                 : */
   14689          192156 : SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
   14690          192156 :   int rc = SQLITE_OK;
   14691          192156 :   if( pId->pMethods ){
   14692           18250 :     rc = pId->pMethods->xClose(pId);
   14693           18250 :     pId->pMethods = 0;
   14694                 :   }
   14695          192156 :   return rc;
   14696                 : }
   14697           78582 : SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
   14698                 :   DO_OS_MALLOC_TEST(id);
   14699           78582 :   return id->pMethods->xRead(id, pBuf, amt, offset);
   14700                 : }
   14701          472421 : SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
   14702                 :   DO_OS_MALLOC_TEST(id);
   14703          472421 :   return id->pMethods->xWrite(id, pBuf, amt, offset);
   14704                 : }
   14705           23387 : SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
   14706           23387 :   return id->pMethods->xTruncate(id, size);
   14707                 : }
   14708           27757 : SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
   14709                 :   DO_OS_MALLOC_TEST(id);
   14710           27757 :   return id->pMethods->xSync(id, flags);
   14711                 : }
   14712           55660 : SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
   14713                 :   DO_OS_MALLOC_TEST(id);
   14714           55660 :   return id->pMethods->xFileSize(id, pSize);
   14715                 : }
   14716           31534 : SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
   14717                 :   DO_OS_MALLOC_TEST(id);
   14718           31534 :   return id->pMethods->xLock(id, lockType);
   14719                 : }
   14720           25243 : SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
   14721           25243 :   return id->pMethods->xUnlock(id, lockType);
   14722                 : }
   14723               0 : SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
   14724                 :   DO_OS_MALLOC_TEST(id);
   14725               0 :   return id->pMethods->xCheckReservedLock(id, pResOut);
   14726                 : }
   14727                 : 
   14728                 : /*
   14729                 : ** Use sqlite3OsFileControl() when we are doing something that might fail
   14730                 : ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
   14731                 : ** when simply tossing information over the wall to the VFS and we do not
   14732                 : ** really care if the VFS receives and understands the information since it
   14733                 : ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
   14734                 : ** routine has no return value since the return value would be meaningless.
   14735                 : */
   14736            2897 : SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
   14737                 :   DO_OS_MALLOC_TEST(id);
   14738            2897 :   return id->pMethods->xFileControl(id, op, pArg);
   14739                 : }
   14740            3183 : SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
   14741            3183 :   (void)id->pMethods->xFileControl(id, op, pArg);
   14742            3183 : }
   14743                 : 
   14744               0 : SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
   14745               0 :   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
   14746               0 :   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
   14747                 : }
   14748           37131 : SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
   14749           37131 :   return id->pMethods->xDeviceCharacteristics(id);
   14750                 : }
   14751          237317 : SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
   14752          237317 :   return id->pMethods->xShmLock(id, offset, n, flags);
   14753                 : }
   14754          121778 : SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
   14755          121778 :   id->pMethods->xShmBarrier(id);
   14756          121778 : }
   14757             426 : SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
   14758             426 :   return id->pMethods->xShmUnmap(id, deleteFlag);
   14759                 : }
   14760             779 : SQLITE_PRIVATE int sqlite3OsShmMap(
   14761                 :   sqlite3_file *id,               /* Database file handle */
   14762                 :   int iPage,
   14763                 :   int pgsz,
   14764                 :   int bExtend,                    /* True to extend file if necessary */
   14765                 :   void volatile **pp              /* OUT: Pointer to mapping */
   14766                 : ){
   14767                 :   DO_OS_MALLOC_TEST(id);
   14768             779 :   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
   14769                 : }
   14770                 : 
   14771                 : /*
   14772                 : ** The next group of routines are convenience wrappers around the
   14773                 : ** VFS methods.
   14774                 : */
   14775           11044 : SQLITE_PRIVATE int sqlite3OsOpen(
   14776                 :   sqlite3_vfs *pVfs, 
   14777                 :   const char *zPath, 
   14778                 :   sqlite3_file *pFile, 
   14779                 :   int flags, 
   14780                 :   int *pFlagsOut
   14781                 : ){
   14782                 :   int rc;
   14783                 :   DO_OS_MALLOC_TEST(0);
   14784                 :   /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
   14785                 :   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
   14786                 :   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
   14787                 :   ** reaching the VFS. */
   14788           11044 :   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
   14789           11044 :   assert( rc==SQLITE_OK || pFile->pMethods==0 );
   14790           11044 :   return rc;
   14791                 : }
   14792           10941 : SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
   14793                 :   DO_OS_MALLOC_TEST(0);
   14794           10941 :   assert( dirSync==0 || dirSync==1 );
   14795           10941 :   return pVfs->xDelete(pVfs, zPath, dirSync);
   14796                 : }
   14797           28865 : SQLITE_PRIVATE int sqlite3OsAccess(
   14798                 :   sqlite3_vfs *pVfs, 
   14799                 :   const char *zPath, 
   14800                 :   int flags, 
   14801                 :   int *pResOut
   14802                 : ){
   14803                 :   DO_OS_MALLOC_TEST(0);
   14804           28865 :   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
   14805                 : }
   14806            4512 : SQLITE_PRIVATE int sqlite3OsFullPathname(
   14807                 :   sqlite3_vfs *pVfs, 
   14808                 :   const char *zPath, 
   14809                 :   int nPathOut, 
   14810                 :   char *zPathOut
   14811                 : ){
   14812                 :   DO_OS_MALLOC_TEST(0);
   14813            4512 :   zPathOut[0] = 0;
   14814            4512 :   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
   14815                 : }
   14816                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
   14817               0 : SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
   14818               0 :   return pVfs->xDlOpen(pVfs, zPath);
   14819                 : }
   14820               0 : SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
   14821               0 :   pVfs->xDlError(pVfs, nByte, zBufOut);
   14822               0 : }
   14823               0 : SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
   14824               0 :   return pVfs->xDlSym(pVfs, pHdle, zSym);
   14825                 : }
   14826               0 : SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
   14827               0 :   pVfs->xDlClose(pVfs, pHandle);
   14828               0 : }
   14829                 : #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   14830             689 : SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
   14831             689 :   return pVfs->xRandomness(pVfs, nByte, zBufOut);
   14832                 : }
   14833               0 : SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
   14834               0 :   return pVfs->xSleep(pVfs, nMicro);
   14835                 : }
   14836           10655 : SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
   14837                 :   int rc;
   14838                 :   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
   14839                 :   ** method to get the current date and time if that method is available
   14840                 :   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
   14841                 :   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
   14842                 :   ** unavailable.
   14843                 :   */
   14844           10655 :   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
   14845           10655 :     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
   14846                 :   }else{
   14847                 :     double r;
   14848               0 :     rc = pVfs->xCurrentTime(pVfs, &r);
   14849               0 :     *pTimeOut = (sqlite3_int64)(r*86400000.0);
   14850                 :   }
   14851           10655 :   return rc;
   14852                 : }
   14853                 : 
   14854               0 : SQLITE_PRIVATE int sqlite3OsOpenMalloc(
   14855                 :   sqlite3_vfs *pVfs, 
   14856                 :   const char *zFile, 
   14857                 :   sqlite3_file **ppFile, 
   14858                 :   int flags,
   14859                 :   int *pOutFlags
   14860                 : ){
   14861               0 :   int rc = SQLITE_NOMEM;
   14862                 :   sqlite3_file *pFile;
   14863               0 :   pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
   14864               0 :   if( pFile ){
   14865               0 :     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
   14866               0 :     if( rc!=SQLITE_OK ){
   14867               0 :       sqlite3_free(pFile);
   14868                 :     }else{
   14869               0 :       *ppFile = pFile;
   14870                 :     }
   14871                 :   }
   14872               0 :   return rc;
   14873                 : }
   14874               0 : SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
   14875               0 :   int rc = SQLITE_OK;
   14876               0 :   assert( pFile );
   14877               0 :   rc = sqlite3OsClose(pFile);
   14878               0 :   sqlite3_free(pFile);
   14879               0 :   return rc;
   14880                 : }
   14881                 : 
   14882                 : /*
   14883                 : ** This function is a wrapper around the OS specific implementation of
   14884                 : ** sqlite3_os_init(). The purpose of the wrapper is to provide the
   14885                 : ** ability to simulate a malloc failure, so that the handling of an
   14886                 : ** error in sqlite3_os_init() by the upper layers can be tested.
   14887                 : */
   14888             806 : SQLITE_PRIVATE int sqlite3OsInit(void){
   14889             806 :   void *p = sqlite3_malloc(10);
   14890             806 :   if( p==0 ) return SQLITE_NOMEM;
   14891             806 :   sqlite3_free(p);
   14892             806 :   return sqlite3_os_init();
   14893                 : }
   14894                 : 
   14895                 : /*
   14896                 : ** The list of all registered VFS implementations.
   14897                 : */
   14898                 : static sqlite3_vfs * SQLITE_WSD vfsList = 0;
   14899                 : #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
   14900                 : 
   14901                 : /*
   14902                 : ** Locate a VFS by name.  If no name is given, simply return the
   14903                 : ** first VFS on the list.
   14904                 : */
   14905            5582 : SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
   14906            5582 :   sqlite3_vfs *pVfs = 0;
   14907                 : #if SQLITE_THREADSAFE
   14908                 :   sqlite3_mutex *mutex;
   14909                 : #endif
   14910                 : #ifndef SQLITE_OMIT_AUTOINIT
   14911            5582 :   int rc = sqlite3_initialize();
   14912            5582 :   if( rc ) return 0;
   14913                 : #endif
   14914                 : #if SQLITE_THREADSAFE
   14915            5582 :   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   14916                 : #endif
   14917            5582 :   sqlite3_mutex_enter(mutex);
   14918            6187 :   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
   14919            6187 :     if( zVfs==0 ) break;
   14920            2019 :     if( strcmp(zVfs, pVfs->zName)==0 ) break;
   14921                 :   }
   14922            5582 :   sqlite3_mutex_leave(mutex);
   14923            5582 :   return pVfs;
   14924                 : }
   14925                 : 
   14926                 : /*
   14927                 : ** Unlink a VFS from the linked list
   14928                 : */
   14929            6418 : static void vfsUnlink(sqlite3_vfs *pVfs){
   14930            6418 :   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
   14931            6418 :   if( pVfs==0 ){
   14932                 :     /* No-op */
   14933            6418 :   }else if( vfsList==pVfs ){
   14934            1589 :     vfsList = pVfs->pNext;
   14935            4829 :   }else if( vfsList ){
   14936            4024 :     sqlite3_vfs *p = vfsList;
   14937           16090 :     while( p->pNext && p->pNext!=pVfs ){
   14938            8042 :       p = p->pNext;
   14939                 :     }
   14940            4024 :     if( p->pNext==pVfs ){
   14941               3 :       p->pNext = pVfs->pNext;
   14942                 :     }
   14943                 :   }
   14944            6418 : }
   14945                 : 
   14946                 : /*
   14947                 : ** Register a VFS with the system.  It is harmless to register the same
   14948                 : ** VFS multiple times.  The new VFS becomes the default if makeDflt is
   14949                 : ** true.
   14950                 : */
   14951            4830 : SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
   14952                 :   MUTEX_LOGIC(sqlite3_mutex *mutex;)
   14953                 : #ifndef SQLITE_OMIT_AUTOINIT
   14954            4830 :   int rc = sqlite3_initialize();
   14955            4830 :   if( rc ) return rc;
   14956                 : #endif
   14957            4830 :   MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
   14958            4830 :   sqlite3_mutex_enter(mutex);
   14959            4830 :   vfsUnlink(pVfs);
   14960            4830 :   if( makeDflt || vfsList==0 ){
   14961            1609 :     pVfs->pNext = vfsList;
   14962            1609 :     vfsList = pVfs;
   14963                 :   }else{
   14964            3221 :     pVfs->pNext = vfsList->pNext;
   14965            3221 :     vfsList->pNext = pVfs;
   14966                 :   }
   14967            4830 :   assert(vfsList);
   14968            4830 :   sqlite3_mutex_leave(mutex);
   14969            4830 :   return SQLITE_OK;
   14970                 : }
   14971                 : 
   14972                 : /*
   14973                 : ** Unregister a VFS so that it is no longer accessible.
   14974                 : */
   14975            1588 : SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
   14976                 : #if SQLITE_THREADSAFE
   14977            1588 :   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   14978                 : #endif
   14979            1588 :   sqlite3_mutex_enter(mutex);
   14980            1588 :   vfsUnlink(pVfs);
   14981            1588 :   sqlite3_mutex_leave(mutex);
   14982            1588 :   return SQLITE_OK;
   14983                 : }
   14984                 : 
   14985                 : /************** End of os.c **************************************************/
   14986                 : /************** Begin file fault.c *******************************************/
   14987                 : /*
   14988                 : ** 2008 Jan 22
   14989                 : **
   14990                 : ** The author disclaims copyright to this source code.  In place of
   14991                 : ** a legal notice, here is a blessing:
   14992                 : **
   14993                 : **    May you do good and not evil.
   14994                 : **    May you find forgiveness for yourself and forgive others.
   14995                 : **    May you share freely, never taking more than you give.
   14996                 : **
   14997                 : *************************************************************************
   14998                 : **
   14999                 : ** This file contains code to support the concept of "benign" 
   15000                 : ** malloc failures (when the xMalloc() or xRealloc() method of the
   15001                 : ** sqlite3_mem_methods structure fails to allocate a block of memory
   15002                 : ** and returns 0). 
   15003                 : **
   15004                 : ** Most malloc failures are non-benign. After they occur, SQLite
   15005                 : ** abandons the current operation and returns an error code (usually
   15006                 : ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
   15007                 : ** fatal. For example, if a malloc fails while resizing a hash table, this 
   15008                 : ** is completely recoverable simply by not carrying out the resize. The 
   15009                 : ** hash table will continue to function normally.  So a malloc failure 
   15010                 : ** during a hash table resize is a benign fault.
   15011                 : */
   15012                 : 
   15013                 : 
   15014                 : #ifndef SQLITE_OMIT_BUILTIN_TEST
   15015                 : 
   15016                 : /*
   15017                 : ** Global variables.
   15018                 : */
   15019                 : typedef struct BenignMallocHooks BenignMallocHooks;
   15020                 : static SQLITE_WSD struct BenignMallocHooks {
   15021                 :   void (*xBenignBegin)(void);
   15022                 :   void (*xBenignEnd)(void);
   15023                 : } sqlite3Hooks = { 0, 0 };
   15024                 : 
   15025                 : /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
   15026                 : ** structure.  If writable static data is unsupported on the target,
   15027                 : ** we have to locate the state vector at run-time.  In the more common
   15028                 : ** case where writable static data is supported, wsdHooks can refer directly
   15029                 : ** to the "sqlite3Hooks" state vector declared above.
   15030                 : */
   15031                 : #ifdef SQLITE_OMIT_WSD
   15032                 : # define wsdHooksInit \
   15033                 :   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
   15034                 : # define wsdHooks x[0]
   15035                 : #else
   15036                 : # define wsdHooksInit
   15037                 : # define wsdHooks sqlite3Hooks
   15038                 : #endif
   15039                 : 
   15040                 : 
   15041                 : /*
   15042                 : ** Register hooks to call when sqlite3BeginBenignMalloc() and
   15043                 : ** sqlite3EndBenignMalloc() are called, respectively.
   15044                 : */
   15045               0 : SQLITE_PRIVATE void sqlite3BenignMallocHooks(
   15046                 :   void (*xBenignBegin)(void),
   15047                 :   void (*xBenignEnd)(void)
   15048                 : ){
   15049                 :   wsdHooksInit;
   15050               0 :   wsdHooks.xBenignBegin = xBenignBegin;
   15051               0 :   wsdHooks.xBenignEnd = xBenignEnd;
   15052               0 : }
   15053                 : 
   15054                 : /*
   15055                 : ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
   15056                 : ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
   15057                 : ** indicates that subsequent malloc failures are non-benign.
   15058                 : */
   15059          442535 : SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
   15060                 :   wsdHooksInit;
   15061          442535 :   if( wsdHooks.xBenignBegin ){
   15062               0 :     wsdHooks.xBenignBegin();
   15063                 :   }
   15064          442535 : }
   15065          442535 : SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
   15066                 :   wsdHooksInit;
   15067          442535 :   if( wsdHooks.xBenignEnd ){
   15068               0 :     wsdHooks.xBenignEnd();
   15069                 :   }
   15070          442535 : }
   15071                 : 
   15072                 : #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
   15073                 : 
   15074                 : /************** End of fault.c ***********************************************/
   15075                 : /************** Begin file mem0.c ********************************************/
   15076                 : /*
   15077                 : ** 2008 October 28
   15078                 : **
   15079                 : ** The author disclaims copyright to this source code.  In place of
   15080                 : ** a legal notice, here is a blessing:
   15081                 : **
   15082                 : **    May you do good and not evil.
   15083                 : **    May you find forgiveness for yourself and forgive others.
   15084                 : **    May you share freely, never taking more than you give.
   15085                 : **
   15086                 : *************************************************************************
   15087                 : **
   15088                 : ** This file contains a no-op memory allocation drivers for use when
   15089                 : ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
   15090                 : ** here always fail.  SQLite will not operate with these drivers.  These
   15091                 : ** are merely placeholders.  Real drivers must be substituted using
   15092                 : ** sqlite3_config() before SQLite will operate.
   15093                 : */
   15094                 : 
   15095                 : /*
   15096                 : ** This version of the memory allocator is the default.  It is
   15097                 : ** used when no other memory allocator is specified using compile-time
   15098                 : ** macros.
   15099                 : */
   15100                 : #ifdef SQLITE_ZERO_MALLOC
   15101                 : 
   15102                 : /*
   15103                 : ** No-op versions of all memory allocation routines
   15104                 : */
   15105                 : static void *sqlite3MemMalloc(int nByte){ return 0; }
   15106                 : static void sqlite3MemFree(void *pPrior){ return; }
   15107                 : static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
   15108                 : static int sqlite3MemSize(void *pPrior){ return 0; }
   15109                 : static int sqlite3MemRoundup(int n){ return n; }
   15110                 : static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
   15111                 : static void sqlite3MemShutdown(void *NotUsed){ return; }
   15112                 : 
   15113                 : /*
   15114                 : ** This routine is the only routine in this file with external linkage.
   15115                 : **
   15116                 : ** Populate the low-level memory allocation function pointers in
   15117                 : ** sqlite3GlobalConfig.m with pointers to the routines in this file.
   15118                 : */
   15119                 : SQLITE_PRIVATE void sqlite3MemSetDefault(void){
   15120                 :   static const sqlite3_mem_methods defaultMethods = {
   15121                 :      sqlite3MemMalloc,
   15122                 :      sqlite3MemFree,
   15123                 :      sqlite3MemRealloc,
   15124                 :      sqlite3MemSize,
   15125                 :      sqlite3MemRoundup,
   15126                 :      sqlite3MemInit,
   15127                 :      sqlite3MemShutdown,
   15128                 :      0
   15129                 :   };
   15130                 :   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   15131                 : }
   15132                 : 
   15133                 : #endif /* SQLITE_ZERO_MALLOC */
   15134                 : 
   15135                 : /************** End of mem0.c ************************************************/
   15136                 : /************** Begin file mem1.c ********************************************/
   15137                 : /*
   15138                 : ** 2007 August 14
   15139                 : **
   15140                 : ** The author disclaims copyright to this source code.  In place of
   15141                 : ** a legal notice, here is a blessing:
   15142                 : **
   15143                 : **    May you do good and not evil.
   15144                 : **    May you find forgiveness for yourself and forgive others.
   15145                 : **    May you share freely, never taking more than you give.
   15146                 : **
   15147                 : *************************************************************************
   15148                 : **
   15149                 : ** This file contains low-level memory allocation drivers for when
   15150                 : ** SQLite will use the standard C-library malloc/realloc/free interface
   15151                 : ** to obtain the memory it needs.
   15152                 : **
   15153                 : ** This file contains implementations of the low-level memory allocation
   15154                 : ** routines specified in the sqlite3_mem_methods object.
   15155                 : */
   15156                 : 
   15157                 : /*
   15158                 : ** This version of the memory allocator is the default.  It is
   15159                 : ** used when no other memory allocator is specified using compile-time
   15160                 : ** macros.
   15161                 : */
   15162                 : #ifdef SQLITE_SYSTEM_MALLOC
   15163                 : 
   15164                 : /*
   15165                 : ** Windows systems have malloc_usable_size() but it is called _msize()
   15166                 : */
   15167                 : #if !defined(HAVE_MALLOC_USABLE_SIZE) && SQLITE_OS_WIN
   15168                 : # define HAVE_MALLOC_USABLE_SIZE 1
   15169                 : # define malloc_usable_size _msize
   15170                 : #endif
   15171                 : 
   15172                 : #if defined(__APPLE__)
   15173                 : 
   15174                 : /*
   15175                 : ** Use the zone allocator available on apple products
   15176                 : */
   15177                 : #include <sys/sysctl.h>
   15178                 : #include <malloc/malloc.h>
   15179                 : #include <libkern/OSAtomic.h>
   15180                 : static malloc_zone_t* _sqliteZone_;
   15181                 : #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
   15182                 : #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
   15183                 : #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
   15184                 : #define SQLITE_MALLOCSIZE(x) \
   15185                 :         (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
   15186                 : 
   15187                 : #else /* if not __APPLE__ */
   15188                 : 
   15189                 : /*
   15190                 : ** Use standard C library malloc and free on non-Apple systems.
   15191                 : */
   15192                 : #define SQLITE_MALLOC(x)    malloc(x)
   15193                 : #define SQLITE_FREE(x)      free(x)
   15194                 : #define SQLITE_REALLOC(x,y) realloc((x),(y))
   15195                 : 
   15196                 : #ifdef HAVE_MALLOC_USABLE_SIZE
   15197                 : #include <malloc.h>
   15198                 : #define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
   15199                 : #else
   15200                 : #undef SQLITE_MALLOCSIZE
   15201                 : #endif
   15202                 : 
   15203                 : #endif /* __APPLE__ or not __APPLE__ */
   15204                 : 
   15205                 : /*
   15206                 : ** Like malloc(), but remember the size of the allocation
   15207                 : ** so that we can find it later using sqlite3MemSize().
   15208                 : **
   15209                 : ** For this low-level routine, we are guaranteed that nByte>0 because
   15210                 : ** cases of nByte<=0 will be intercepted and dealt with by higher level
   15211                 : ** routines.
   15212                 : */
   15213              14 : static void *sqlite3MemMalloc(int nByte){
   15214                 : #ifdef SQLITE_MALLOCSIZE
   15215                 :   void *p = SQLITE_MALLOC( nByte );
   15216                 :   if( p==0 ){
   15217                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   15218                 :     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
   15219                 :   }
   15220                 :   return p;
   15221                 : #else
   15222                 :   sqlite3_int64 *p;
   15223              14 :   assert( nByte>0 );
   15224              14 :   nByte = ROUND8(nByte);
   15225              14 :   p = SQLITE_MALLOC( nByte+8 );
   15226              14 :   if( p ){
   15227              14 :     p[0] = nByte;
   15228              14 :     p++;
   15229                 :   }else{
   15230                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   15231               0 :     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
   15232                 :   }
   15233              14 :   return (void *)p;
   15234                 : #endif
   15235                 : }
   15236                 : 
   15237                 : /*
   15238                 : ** Like free() but works for allocations obtained from sqlite3MemMalloc()
   15239                 : ** or sqlite3MemRealloc().
   15240                 : **
   15241                 : ** For this low-level routine, we already know that pPrior!=0 since
   15242                 : ** cases where pPrior==0 will have been intecepted and dealt with
   15243                 : ** by higher-level routines.
   15244                 : */
   15245              14 : static void sqlite3MemFree(void *pPrior){
   15246                 : #ifdef SQLITE_MALLOCSIZE
   15247                 :   SQLITE_FREE(pPrior);
   15248                 : #else
   15249              14 :   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
   15250              14 :   assert( pPrior!=0 );
   15251              14 :   p--;
   15252              14 :   SQLITE_FREE(p);
   15253                 : #endif
   15254              14 : }
   15255                 : 
   15256                 : /*
   15257                 : ** Report the allocated size of a prior return from xMalloc()
   15258                 : ** or xRealloc().
   15259                 : */
   15260              28 : static int sqlite3MemSize(void *pPrior){
   15261                 : #ifdef SQLITE_MALLOCSIZE
   15262                 :   return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
   15263                 : #else
   15264                 :   sqlite3_int64 *p;
   15265              28 :   if( pPrior==0 ) return 0;
   15266              28 :   p = (sqlite3_int64*)pPrior;
   15267              28 :   p--;
   15268              28 :   return (int)p[0];
   15269                 : #endif
   15270                 : }
   15271                 : 
   15272                 : /*
   15273                 : ** Like realloc().  Resize an allocation previously obtained from
   15274                 : ** sqlite3MemMalloc().
   15275                 : **
   15276                 : ** For this low-level interface, we know that pPrior!=0.  Cases where
   15277                 : ** pPrior==0 while have been intercepted by higher-level routine and
   15278                 : ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
   15279                 : ** cases where nByte<=0 will have been intercepted by higher-level
   15280                 : ** routines and redirected to xFree.
   15281                 : */
   15282               0 : static void *sqlite3MemRealloc(void *pPrior, int nByte){
   15283                 : #ifdef SQLITE_MALLOCSIZE
   15284                 :   void *p = SQLITE_REALLOC(pPrior, nByte);
   15285                 :   if( p==0 ){
   15286                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   15287                 :     sqlite3_log(SQLITE_NOMEM,
   15288                 :       "failed memory resize %u to %u bytes",
   15289                 :       SQLITE_MALLOCSIZE(pPrior), nByte);
   15290                 :   }
   15291                 :   return p;
   15292                 : #else
   15293               0 :   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
   15294               0 :   assert( pPrior!=0 && nByte>0 );
   15295               0 :   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
   15296               0 :   p--;
   15297               0 :   p = SQLITE_REALLOC(p, nByte+8 );
   15298               0 :   if( p ){
   15299               0 :     p[0] = nByte;
   15300               0 :     p++;
   15301                 :   }else{
   15302                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   15303               0 :     sqlite3_log(SQLITE_NOMEM,
   15304                 :       "failed memory resize %u to %u bytes",
   15305                 :       sqlite3MemSize(pPrior), nByte);
   15306                 :   }
   15307               0 :   return (void*)p;
   15308                 : #endif
   15309                 : }
   15310                 : 
   15311                 : /*
   15312                 : ** Round up a request size to the next valid allocation size.
   15313                 : */
   15314              14 : static int sqlite3MemRoundup(int n){
   15315              14 :   return ROUND8(n);
   15316                 : }
   15317                 : 
   15318                 : /*
   15319                 : ** Initialize this module.
   15320                 : */
   15321               3 : static int sqlite3MemInit(void *NotUsed){
   15322                 : #if defined(__APPLE__)
   15323                 :   int cpuCount;
   15324                 :   size_t len;
   15325                 :   if( _sqliteZone_ ){
   15326                 :     return SQLITE_OK;
   15327                 :   }
   15328                 :   len = sizeof(cpuCount);
   15329                 :   /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
   15330                 :   sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
   15331                 :   if( cpuCount>1 ){
   15332                 :     /* defer MT decisions to system malloc */
   15333                 :     _sqliteZone_ = malloc_default_zone();
   15334                 :   }else{
   15335                 :     /* only 1 core, use our own zone to contention over global locks, 
   15336                 :     ** e.g. we have our own dedicated locks */
   15337                 :     bool success;               
   15338                 :     malloc_zone_t* newzone = malloc_create_zone(4096, 0);
   15339                 :     malloc_set_zone_name(newzone, "Sqlite_Heap");
   15340                 :     do{
   15341                 :       success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone, 
   15342                 :                                  (void * volatile *)&_sqliteZone_);
   15343                 :     }while(!_sqliteZone_);
   15344                 :     if( !success ){     
   15345                 :       /* somebody registered a zone first */
   15346                 :       malloc_destroy_zone(newzone);
   15347                 :     }
   15348                 :   }
   15349                 : #endif
   15350                 :   UNUSED_PARAMETER(NotUsed);
   15351               3 :   return SQLITE_OK;
   15352                 : }
   15353                 : 
   15354                 : /*
   15355                 : ** Deinitialize this module.
   15356                 : */
   15357               1 : static void sqlite3MemShutdown(void *NotUsed){
   15358                 :   UNUSED_PARAMETER(NotUsed);
   15359                 :   return;
   15360                 : }
   15361                 : 
   15362                 : /*
   15363                 : ** This routine is the only routine in this file with external linkage.
   15364                 : **
   15365                 : ** Populate the low-level memory allocation function pointers in
   15366                 : ** sqlite3GlobalConfig.m with pointers to the routines in this file.
   15367                 : */
   15368               3 : SQLITE_PRIVATE void sqlite3MemSetDefault(void){
   15369                 :   static const sqlite3_mem_methods defaultMethods = {
   15370                 :      sqlite3MemMalloc,
   15371                 :      sqlite3MemFree,
   15372                 :      sqlite3MemRealloc,
   15373                 :      sqlite3MemSize,
   15374                 :      sqlite3MemRoundup,
   15375                 :      sqlite3MemInit,
   15376                 :      sqlite3MemShutdown,
   15377                 :      0
   15378                 :   };
   15379               3 :   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   15380               3 : }
   15381                 : 
   15382                 : #endif /* SQLITE_SYSTEM_MALLOC */
   15383                 : 
   15384                 : /************** End of mem1.c ************************************************/
   15385                 : /************** Begin file mem2.c ********************************************/
   15386                 : /*
   15387                 : ** 2007 August 15
   15388                 : **
   15389                 : ** The author disclaims copyright to this source code.  In place of
   15390                 : ** a legal notice, here is a blessing:
   15391                 : **
   15392                 : **    May you do good and not evil.
   15393                 : **    May you find forgiveness for yourself and forgive others.
   15394                 : **    May you share freely, never taking more than you give.
   15395                 : **
   15396                 : *************************************************************************
   15397                 : **
   15398                 : ** This file contains low-level memory allocation drivers for when
   15399                 : ** SQLite will use the standard C-library malloc/realloc/free interface
   15400                 : ** to obtain the memory it needs while adding lots of additional debugging
   15401                 : ** information to each allocation in order to help detect and fix memory
   15402                 : ** leaks and memory usage errors.
   15403                 : **
   15404                 : ** This file contains implementations of the low-level memory allocation
   15405                 : ** routines specified in the sqlite3_mem_methods object.
   15406                 : */
   15407                 : 
   15408                 : /*
   15409                 : ** This version of the memory allocator is used only if the
   15410                 : ** SQLITE_MEMDEBUG macro is defined
   15411                 : */
   15412                 : #ifdef SQLITE_MEMDEBUG
   15413                 : 
   15414                 : /*
   15415                 : ** The backtrace functionality is only available with GLIBC
   15416                 : */
   15417                 : #ifdef __GLIBC__
   15418                 :   extern int backtrace(void**,int);
   15419                 :   extern void backtrace_symbols_fd(void*const*,int,int);
   15420                 : #else
   15421                 : # define backtrace(A,B) 1
   15422                 : # define backtrace_symbols_fd(A,B,C)
   15423                 : #endif
   15424                 : /* #include <stdio.h> */
   15425                 : 
   15426                 : /*
   15427                 : ** Each memory allocation looks like this:
   15428                 : **
   15429                 : **  ------------------------------------------------------------------------
   15430                 : **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
   15431                 : **  ------------------------------------------------------------------------
   15432                 : **
   15433                 : ** The application code sees only a pointer to the allocation.  We have
   15434                 : ** to back up from the allocation pointer to find the MemBlockHdr.  The
   15435                 : ** MemBlockHdr tells us the size of the allocation and the number of
   15436                 : ** backtrace pointers.  There is also a guard word at the end of the
   15437                 : ** MemBlockHdr.
   15438                 : */
   15439                 : struct MemBlockHdr {
   15440                 :   i64 iSize;                          /* Size of this allocation */
   15441                 :   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
   15442                 :   char nBacktrace;                    /* Number of backtraces on this alloc */
   15443                 :   char nBacktraceSlots;               /* Available backtrace slots */
   15444                 :   u8 nTitle;                          /* Bytes of title; includes '\0' */
   15445                 :   u8 eType;                           /* Allocation type code */
   15446                 :   int iForeGuard;                     /* Guard word for sanity */
   15447                 : };
   15448                 : 
   15449                 : /*
   15450                 : ** Guard words
   15451                 : */
   15452                 : #define FOREGUARD 0x80F5E153
   15453                 : #define REARGUARD 0xE4676B53
   15454                 : 
   15455                 : /*
   15456                 : ** Number of malloc size increments to track.
   15457                 : */
   15458                 : #define NCSIZE  1000
   15459                 : 
   15460                 : /*
   15461                 : ** All of the static variables used by this module are collected
   15462                 : ** into a single structure named "mem".  This is to keep the
   15463                 : ** static variables organized and to reduce namespace pollution
   15464                 : ** when this module is combined with other in the amalgamation.
   15465                 : */
   15466                 : static struct {
   15467                 :   
   15468                 :   /*
   15469                 :   ** Mutex to control access to the memory allocation subsystem.
   15470                 :   */
   15471                 :   sqlite3_mutex *mutex;
   15472                 : 
   15473                 :   /*
   15474                 :   ** Head and tail of a linked list of all outstanding allocations
   15475                 :   */
   15476                 :   struct MemBlockHdr *pFirst;
   15477                 :   struct MemBlockHdr *pLast;
   15478                 :   
   15479                 :   /*
   15480                 :   ** The number of levels of backtrace to save in new allocations.
   15481                 :   */
   15482                 :   int nBacktrace;
   15483                 :   void (*xBacktrace)(int, int, void **);
   15484                 : 
   15485                 :   /*
   15486                 :   ** Title text to insert in front of each block
   15487                 :   */
   15488                 :   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
   15489                 :   char zTitle[100];  /* The title text */
   15490                 : 
   15491                 :   /* 
   15492                 :   ** sqlite3MallocDisallow() increments the following counter.
   15493                 :   ** sqlite3MallocAllow() decrements it.
   15494                 :   */
   15495                 :   int disallow; /* Do not allow memory allocation */
   15496                 : 
   15497                 :   /*
   15498                 :   ** Gather statistics on the sizes of memory allocations.
   15499                 :   ** nAlloc[i] is the number of allocation attempts of i*8
   15500                 :   ** bytes.  i==NCSIZE is the number of allocation attempts for
   15501                 :   ** sizes more than NCSIZE*8 bytes.
   15502                 :   */
   15503                 :   int nAlloc[NCSIZE];      /* Total number of allocations */
   15504                 :   int nCurrent[NCSIZE];    /* Current number of allocations */
   15505                 :   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
   15506                 : 
   15507                 : } mem;
   15508                 : 
   15509                 : 
   15510                 : /*
   15511                 : ** Adjust memory usage statistics
   15512                 : */
   15513                 : static void adjustStats(int iSize, int increment){
   15514                 :   int i = ROUND8(iSize)/8;
   15515                 :   if( i>NCSIZE-1 ){
   15516                 :     i = NCSIZE - 1;
   15517                 :   }
   15518                 :   if( increment>0 ){
   15519                 :     mem.nAlloc[i]++;
   15520                 :     mem.nCurrent[i]++;
   15521                 :     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
   15522                 :       mem.mxCurrent[i] = mem.nCurrent[i];
   15523                 :     }
   15524                 :   }else{
   15525                 :     mem.nCurrent[i]--;
   15526                 :     assert( mem.nCurrent[i]>=0 );
   15527                 :   }
   15528                 : }
   15529                 : 
   15530                 : /*
   15531                 : ** Given an allocation, find the MemBlockHdr for that allocation.
   15532                 : **
   15533                 : ** This routine checks the guards at either end of the allocation and
   15534                 : ** if they are incorrect it asserts.
   15535                 : */
   15536                 : static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
   15537                 :   struct MemBlockHdr *p;
   15538                 :   int *pInt;
   15539                 :   u8 *pU8;
   15540                 :   int nReserve;
   15541                 : 
   15542                 :   p = (struct MemBlockHdr*)pAllocation;
   15543                 :   p--;
   15544                 :   assert( p->iForeGuard==(int)FOREGUARD );
   15545                 :   nReserve = ROUND8(p->iSize);
   15546                 :   pInt = (int*)pAllocation;
   15547                 :   pU8 = (u8*)pAllocation;
   15548                 :   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
   15549                 :   /* This checks any of the "extra" bytes allocated due
   15550                 :   ** to rounding up to an 8 byte boundary to ensure 
   15551                 :   ** they haven't been overwritten.
   15552                 :   */
   15553                 :   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
   15554                 :   return p;
   15555                 : }
   15556                 : 
   15557                 : /*
   15558                 : ** Return the number of bytes currently allocated at address p.
   15559                 : */
   15560                 : static int sqlite3MemSize(void *p){
   15561                 :   struct MemBlockHdr *pHdr;
   15562                 :   if( !p ){
   15563                 :     return 0;
   15564                 :   }
   15565                 :   pHdr = sqlite3MemsysGetHeader(p);
   15566                 :   return pHdr->iSize;
   15567                 : }
   15568                 : 
   15569                 : /*
   15570                 : ** Initialize the memory allocation subsystem.
   15571                 : */
   15572                 : static int sqlite3MemInit(void *NotUsed){
   15573                 :   UNUSED_PARAMETER(NotUsed);
   15574                 :   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
   15575                 :   if( !sqlite3GlobalConfig.bMemstat ){
   15576                 :     /* If memory status is enabled, then the malloc.c wrapper will already
   15577                 :     ** hold the STATIC_MEM mutex when the routines here are invoked. */
   15578                 :     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   15579                 :   }
   15580                 :   return SQLITE_OK;
   15581                 : }
   15582                 : 
   15583                 : /*
   15584                 : ** Deinitialize the memory allocation subsystem.
   15585                 : */
   15586                 : static void sqlite3MemShutdown(void *NotUsed){
   15587                 :   UNUSED_PARAMETER(NotUsed);
   15588                 :   mem.mutex = 0;
   15589                 : }
   15590                 : 
   15591                 : /*
   15592                 : ** Round up a request size to the next valid allocation size.
   15593                 : */
   15594                 : static int sqlite3MemRoundup(int n){
   15595                 :   return ROUND8(n);
   15596                 : }
   15597                 : 
   15598                 : /*
   15599                 : ** Fill a buffer with pseudo-random bytes.  This is used to preset
   15600                 : ** the content of a new memory allocation to unpredictable values and
   15601                 : ** to clear the content of a freed allocation to unpredictable values.
   15602                 : */
   15603                 : static void randomFill(char *pBuf, int nByte){
   15604                 :   unsigned int x, y, r;
   15605                 :   x = SQLITE_PTR_TO_INT(pBuf);
   15606                 :   y = nByte | 1;
   15607                 :   while( nByte >= 4 ){
   15608                 :     x = (x>>1) ^ (-(x&1) & 0xd0000001);
   15609                 :     y = y*1103515245 + 12345;
   15610                 :     r = x ^ y;
   15611                 :     *(int*)pBuf = r;
   15612                 :     pBuf += 4;
   15613                 :     nByte -= 4;
   15614                 :   }
   15615                 :   while( nByte-- > 0 ){
   15616                 :     x = (x>>1) ^ (-(x&1) & 0xd0000001);
   15617                 :     y = y*1103515245 + 12345;
   15618                 :     r = x ^ y;
   15619                 :     *(pBuf++) = r & 0xff;
   15620                 :   }
   15621                 : }
   15622                 : 
   15623                 : /*
   15624                 : ** Allocate nByte bytes of memory.
   15625                 : */
   15626                 : static void *sqlite3MemMalloc(int nByte){
   15627                 :   struct MemBlockHdr *pHdr;
   15628                 :   void **pBt;
   15629                 :   char *z;
   15630                 :   int *pInt;
   15631                 :   void *p = 0;
   15632                 :   int totalSize;
   15633                 :   int nReserve;
   15634                 :   sqlite3_mutex_enter(mem.mutex);
   15635                 :   assert( mem.disallow==0 );
   15636                 :   nReserve = ROUND8(nByte);
   15637                 :   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
   15638                 :                mem.nBacktrace*sizeof(void*) + mem.nTitle;
   15639                 :   p = malloc(totalSize);
   15640                 :   if( p ){
   15641                 :     z = p;
   15642                 :     pBt = (void**)&z[mem.nTitle];
   15643                 :     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
   15644                 :     pHdr->pNext = 0;
   15645                 :     pHdr->pPrev = mem.pLast;
   15646                 :     if( mem.pLast ){
   15647                 :       mem.pLast->pNext = pHdr;
   15648                 :     }else{
   15649                 :       mem.pFirst = pHdr;
   15650                 :     }
   15651                 :     mem.pLast = pHdr;
   15652                 :     pHdr->iForeGuard = FOREGUARD;
   15653                 :     pHdr->eType = MEMTYPE_HEAP;
   15654                 :     pHdr->nBacktraceSlots = mem.nBacktrace;
   15655                 :     pHdr->nTitle = mem.nTitle;
   15656                 :     if( mem.nBacktrace ){
   15657                 :       void *aAddr[40];
   15658                 :       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
   15659                 :       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
   15660                 :       assert(pBt[0]);
   15661                 :       if( mem.xBacktrace ){
   15662                 :         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
   15663                 :       }
   15664                 :     }else{
   15665                 :       pHdr->nBacktrace = 0;
   15666                 :     }
   15667                 :     if( mem.nTitle ){
   15668                 :       memcpy(z, mem.zTitle, mem.nTitle);
   15669                 :     }
   15670                 :     pHdr->iSize = nByte;
   15671                 :     adjustStats(nByte, +1);
   15672                 :     pInt = (int*)&pHdr[1];
   15673                 :     pInt[nReserve/sizeof(int)] = REARGUARD;
   15674                 :     randomFill((char*)pInt, nByte);
   15675                 :     memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
   15676                 :     p = (void*)pInt;
   15677                 :   }
   15678                 :   sqlite3_mutex_leave(mem.mutex);
   15679                 :   return p; 
   15680                 : }
   15681                 : 
   15682                 : /*
   15683                 : ** Free memory.
   15684                 : */
   15685                 : static void sqlite3MemFree(void *pPrior){
   15686                 :   struct MemBlockHdr *pHdr;
   15687                 :   void **pBt;
   15688                 :   char *z;
   15689                 :   assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0 
   15690                 :        || mem.mutex!=0 );
   15691                 :   pHdr = sqlite3MemsysGetHeader(pPrior);
   15692                 :   pBt = (void**)pHdr;
   15693                 :   pBt -= pHdr->nBacktraceSlots;
   15694                 :   sqlite3_mutex_enter(mem.mutex);
   15695                 :   if( pHdr->pPrev ){
   15696                 :     assert( pHdr->pPrev->pNext==pHdr );
   15697                 :     pHdr->pPrev->pNext = pHdr->pNext;
   15698                 :   }else{
   15699                 :     assert( mem.pFirst==pHdr );
   15700                 :     mem.pFirst = pHdr->pNext;
   15701                 :   }
   15702                 :   if( pHdr->pNext ){
   15703                 :     assert( pHdr->pNext->pPrev==pHdr );
   15704                 :     pHdr->pNext->pPrev = pHdr->pPrev;
   15705                 :   }else{
   15706                 :     assert( mem.pLast==pHdr );
   15707                 :     mem.pLast = pHdr->pPrev;
   15708                 :   }
   15709                 :   z = (char*)pBt;
   15710                 :   z -= pHdr->nTitle;
   15711                 :   adjustStats(pHdr->iSize, -1);
   15712                 :   randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
   15713                 :                 pHdr->iSize + sizeof(int) + pHdr->nTitle);
   15714                 :   free(z);
   15715                 :   sqlite3_mutex_leave(mem.mutex);  
   15716                 : }
   15717                 : 
   15718                 : /*
   15719                 : ** Change the size of an existing memory allocation.
   15720                 : **
   15721                 : ** For this debugging implementation, we *always* make a copy of the
   15722                 : ** allocation into a new place in memory.  In this way, if the 
   15723                 : ** higher level code is using pointer to the old allocation, it is 
   15724                 : ** much more likely to break and we are much more liking to find
   15725                 : ** the error.
   15726                 : */
   15727                 : static void *sqlite3MemRealloc(void *pPrior, int nByte){
   15728                 :   struct MemBlockHdr *pOldHdr;
   15729                 :   void *pNew;
   15730                 :   assert( mem.disallow==0 );
   15731                 :   assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
   15732                 :   pOldHdr = sqlite3MemsysGetHeader(pPrior);
   15733                 :   pNew = sqlite3MemMalloc(nByte);
   15734                 :   if( pNew ){
   15735                 :     memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
   15736                 :     if( nByte>pOldHdr->iSize ){
   15737                 :       randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
   15738                 :     }
   15739                 :     sqlite3MemFree(pPrior);
   15740                 :   }
   15741                 :   return pNew;
   15742                 : }
   15743                 : 
   15744                 : /*
   15745                 : ** Populate the low-level memory allocation function pointers in
   15746                 : ** sqlite3GlobalConfig.m with pointers to the routines in this file.
   15747                 : */
   15748                 : SQLITE_PRIVATE void sqlite3MemSetDefault(void){
   15749                 :   static const sqlite3_mem_methods defaultMethods = {
   15750                 :      sqlite3MemMalloc,
   15751                 :      sqlite3MemFree,
   15752                 :      sqlite3MemRealloc,
   15753                 :      sqlite3MemSize,
   15754                 :      sqlite3MemRoundup,
   15755                 :      sqlite3MemInit,
   15756                 :      sqlite3MemShutdown,
   15757                 :      0
   15758                 :   };
   15759                 :   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
   15760                 : }
   15761                 : 
   15762                 : /*
   15763                 : ** Set the "type" of an allocation.
   15764                 : */
   15765                 : SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
   15766                 :   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   15767                 :     struct MemBlockHdr *pHdr;
   15768                 :     pHdr = sqlite3MemsysGetHeader(p);
   15769                 :     assert( pHdr->iForeGuard==FOREGUARD );
   15770                 :     pHdr->eType = eType;
   15771                 :   }
   15772                 : }
   15773                 : 
   15774                 : /*
   15775                 : ** Return TRUE if the mask of type in eType matches the type of the
   15776                 : ** allocation p.  Also return true if p==NULL.
   15777                 : **
   15778                 : ** This routine is designed for use within an assert() statement, to
   15779                 : ** verify the type of an allocation.  For example:
   15780                 : **
   15781                 : **     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   15782                 : */
   15783                 : SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
   15784                 :   int rc = 1;
   15785                 :   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   15786                 :     struct MemBlockHdr *pHdr;
   15787                 :     pHdr = sqlite3MemsysGetHeader(p);
   15788                 :     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
   15789                 :     if( (pHdr->eType&eType)==0 ){
   15790                 :       rc = 0;
   15791                 :     }
   15792                 :   }
   15793                 :   return rc;
   15794                 : }
   15795                 : 
   15796                 : /*
   15797                 : ** Return TRUE if the mask of type in eType matches no bits of the type of the
   15798                 : ** allocation p.  Also return true if p==NULL.
   15799                 : **
   15800                 : ** This routine is designed for use within an assert() statement, to
   15801                 : ** verify the type of an allocation.  For example:
   15802                 : **
   15803                 : **     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   15804                 : */
   15805                 : SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
   15806                 :   int rc = 1;
   15807                 :   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   15808                 :     struct MemBlockHdr *pHdr;
   15809                 :     pHdr = sqlite3MemsysGetHeader(p);
   15810                 :     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
   15811                 :     if( (pHdr->eType&eType)!=0 ){
   15812                 :       rc = 0;
   15813                 :     }
   15814                 :   }
   15815                 :   return rc;
   15816                 : }
   15817                 : 
   15818                 : /*
   15819                 : ** Set the number of backtrace levels kept for each allocation.
   15820                 : ** A value of zero turns off backtracing.  The number is always rounded
   15821                 : ** up to a multiple of 2.
   15822                 : */
   15823                 : SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
   15824                 :   if( depth<0 ){ depth = 0; }
   15825                 :   if( depth>20 ){ depth = 20; }
   15826                 :   depth = (depth+1)&0xfe;
   15827                 :   mem.nBacktrace = depth;
   15828                 : }
   15829                 : 
   15830                 : SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
   15831                 :   mem.xBacktrace = xBacktrace;
   15832                 : }
   15833                 : 
   15834                 : /*
   15835                 : ** Set the title string for subsequent allocations.
   15836                 : */
   15837                 : SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
   15838                 :   unsigned int n = sqlite3Strlen30(zTitle) + 1;
   15839                 :   sqlite3_mutex_enter(mem.mutex);
   15840                 :   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
   15841                 :   memcpy(mem.zTitle, zTitle, n);
   15842                 :   mem.zTitle[n] = 0;
   15843                 :   mem.nTitle = ROUND8(n);
   15844                 :   sqlite3_mutex_leave(mem.mutex);
   15845                 : }
   15846                 : 
   15847                 : SQLITE_PRIVATE void sqlite3MemdebugSync(){
   15848                 :   struct MemBlockHdr *pHdr;
   15849                 :   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
   15850                 :     void **pBt = (void**)pHdr;
   15851                 :     pBt -= pHdr->nBacktraceSlots;
   15852                 :     mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
   15853                 :   }
   15854                 : }
   15855                 : 
   15856                 : /*
   15857                 : ** Open the file indicated and write a log of all unfreed memory 
   15858                 : ** allocations into that log.
   15859                 : */
   15860                 : SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
   15861                 :   FILE *out;
   15862                 :   struct MemBlockHdr *pHdr;
   15863                 :   void **pBt;
   15864                 :   int i;
   15865                 :   out = fopen(zFilename, "w");
   15866                 :   if( out==0 ){
   15867                 :     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   15868                 :                     zFilename);
   15869                 :     return;
   15870                 :   }
   15871                 :   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
   15872                 :     char *z = (char*)pHdr;
   15873                 :     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
   15874                 :     fprintf(out, "**** %lld bytes at %p from %s ****\n", 
   15875                 :             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
   15876                 :     if( pHdr->nBacktrace ){
   15877                 :       fflush(out);
   15878                 :       pBt = (void**)pHdr;
   15879                 :       pBt -= pHdr->nBacktraceSlots;
   15880                 :       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
   15881                 :       fprintf(out, "\n");
   15882                 :     }
   15883                 :   }
   15884                 :   fprintf(out, "COUNTS:\n");
   15885                 :   for(i=0; i<NCSIZE-1; i++){
   15886                 :     if( mem.nAlloc[i] ){
   15887                 :       fprintf(out, "   %5d: %10d %10d %10d\n", 
   15888                 :             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
   15889                 :     }
   15890                 :   }
   15891                 :   if( mem.nAlloc[NCSIZE-1] ){
   15892                 :     fprintf(out, "   %5d: %10d %10d %10d\n",
   15893                 :              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
   15894                 :              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
   15895                 :   }
   15896                 :   fclose(out);
   15897                 : }
   15898                 : 
   15899                 : /*
   15900                 : ** Return the number of times sqlite3MemMalloc() has been called.
   15901                 : */
   15902                 : SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
   15903                 :   int i;
   15904                 :   int nTotal = 0;
   15905                 :   for(i=0; i<NCSIZE; i++){
   15906                 :     nTotal += mem.nAlloc[i];
   15907                 :   }
   15908                 :   return nTotal;
   15909                 : }
   15910                 : 
   15911                 : 
   15912                 : #endif /* SQLITE_MEMDEBUG */
   15913                 : 
   15914                 : /************** End of mem2.c ************************************************/
   15915                 : /************** Begin file mem3.c ********************************************/
   15916                 : /*
   15917                 : ** 2007 October 14
   15918                 : **
   15919                 : ** The author disclaims copyright to this source code.  In place of
   15920                 : ** a legal notice, here is a blessing:
   15921                 : **
   15922                 : **    May you do good and not evil.
   15923                 : **    May you find forgiveness for yourself and forgive others.
   15924                 : **    May you share freely, never taking more than you give.
   15925                 : **
   15926                 : *************************************************************************
   15927                 : ** This file contains the C functions that implement a memory
   15928                 : ** allocation subsystem for use by SQLite. 
   15929                 : **
   15930                 : ** This version of the memory allocation subsystem omits all
   15931                 : ** use of malloc(). The SQLite user supplies a block of memory
   15932                 : ** before calling sqlite3_initialize() from which allocations
   15933                 : ** are made and returned by the xMalloc() and xRealloc() 
   15934                 : ** implementations. Once sqlite3_initialize() has been called,
   15935                 : ** the amount of memory available to SQLite is fixed and cannot
   15936                 : ** be changed.
   15937                 : **
   15938                 : ** This version of the memory allocation subsystem is included
   15939                 : ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
   15940                 : */
   15941                 : 
   15942                 : /*
   15943                 : ** This version of the memory allocator is only built into the library
   15944                 : ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
   15945                 : ** mean that the library will use a memory-pool by default, just that
   15946                 : ** it is available. The mempool allocator is activated by calling
   15947                 : ** sqlite3_config().
   15948                 : */
   15949                 : #ifdef SQLITE_ENABLE_MEMSYS3
   15950                 : 
   15951                 : /*
   15952                 : ** Maximum size (in Mem3Blocks) of a "small" chunk.
   15953                 : */
   15954                 : #define MX_SMALL 10
   15955                 : 
   15956                 : 
   15957                 : /*
   15958                 : ** Number of freelist hash slots
   15959                 : */
   15960                 : #define N_HASH  61
   15961                 : 
   15962                 : /*
   15963                 : ** A memory allocation (also called a "chunk") consists of two or 
   15964                 : ** more blocks where each block is 8 bytes.  The first 8 bytes are 
   15965                 : ** a header that is not returned to the user.
   15966                 : **
   15967                 : ** A chunk is two or more blocks that is either checked out or
   15968                 : ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
   15969                 : ** size of the allocation in blocks if the allocation is free.
   15970                 : ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
   15971                 : ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
   15972                 : ** is true if the previous chunk is checked out and false if the
   15973                 : ** previous chunk is free.  The u.hdr.prevSize field is the size of
   15974                 : ** the previous chunk in blocks if the previous chunk is on the
   15975                 : ** freelist. If the previous chunk is checked out, then
   15976                 : ** u.hdr.prevSize can be part of the data for that chunk and should
   15977                 : ** not be read or written.
   15978                 : **
   15979                 : ** We often identify a chunk by its index in mem3.aPool[].  When
   15980                 : ** this is done, the chunk index refers to the second block of
   15981                 : ** the chunk.  In this way, the first chunk has an index of 1.
   15982                 : ** A chunk index of 0 means "no such chunk" and is the equivalent
   15983                 : ** of a NULL pointer.
   15984                 : **
   15985                 : ** The second block of free chunks is of the form u.list.  The
   15986                 : ** two fields form a double-linked list of chunks of related sizes.
   15987                 : ** Pointers to the head of the list are stored in mem3.aiSmall[] 
   15988                 : ** for smaller chunks and mem3.aiHash[] for larger chunks.
   15989                 : **
   15990                 : ** The second block of a chunk is user data if the chunk is checked 
   15991                 : ** out.  If a chunk is checked out, the user data may extend into
   15992                 : ** the u.hdr.prevSize value of the following chunk.
   15993                 : */
   15994                 : typedef struct Mem3Block Mem3Block;
   15995                 : struct Mem3Block {
   15996                 :   union {
   15997                 :     struct {
   15998                 :       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
   15999                 :       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
   16000                 :     } hdr;
   16001                 :     struct {
   16002                 :       u32 next;       /* Index in mem3.aPool[] of next free chunk */
   16003                 :       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
   16004                 :     } list;
   16005                 :   } u;
   16006                 : };
   16007                 : 
   16008                 : /*
   16009                 : ** All of the static variables used by this module are collected
   16010                 : ** into a single structure named "mem3".  This is to keep the
   16011                 : ** static variables organized and to reduce namespace pollution
   16012                 : ** when this module is combined with other in the amalgamation.
   16013                 : */
   16014                 : static SQLITE_WSD struct Mem3Global {
   16015                 :   /*
   16016                 :   ** Memory available for allocation. nPool is the size of the array
   16017                 :   ** (in Mem3Blocks) pointed to by aPool less 2.
   16018                 :   */
   16019                 :   u32 nPool;
   16020                 :   Mem3Block *aPool;
   16021                 : 
   16022                 :   /*
   16023                 :   ** True if we are evaluating an out-of-memory callback.
   16024                 :   */
   16025                 :   int alarmBusy;
   16026                 :   
   16027                 :   /*
   16028                 :   ** Mutex to control access to the memory allocation subsystem.
   16029                 :   */
   16030                 :   sqlite3_mutex *mutex;
   16031                 :   
   16032                 :   /*
   16033                 :   ** The minimum amount of free space that we have seen.
   16034                 :   */
   16035                 :   u32 mnMaster;
   16036                 : 
   16037                 :   /*
   16038                 :   ** iMaster is the index of the master chunk.  Most new allocations
   16039                 :   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
   16040                 :   ** of the current master.  iMaster is 0 if there is not master chunk.
   16041                 :   ** The master chunk is not in either the aiHash[] or aiSmall[].
   16042                 :   */
   16043                 :   u32 iMaster;
   16044                 :   u32 szMaster;
   16045                 : 
   16046                 :   /*
   16047                 :   ** Array of lists of free blocks according to the block size 
   16048                 :   ** for smaller chunks, or a hash on the block size for larger
   16049                 :   ** chunks.
   16050                 :   */
   16051                 :   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
   16052                 :   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
   16053                 : } mem3 = { 97535575 };
   16054                 : 
   16055                 : #define mem3 GLOBAL(struct Mem3Global, mem3)
   16056                 : 
   16057                 : /*
   16058                 : ** Unlink the chunk at mem3.aPool[i] from list it is currently
   16059                 : ** on.  *pRoot is the list that i is a member of.
   16060                 : */
   16061                 : static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
   16062                 :   u32 next = mem3.aPool[i].u.list.next;
   16063                 :   u32 prev = mem3.aPool[i].u.list.prev;
   16064                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16065                 :   if( prev==0 ){
   16066                 :     *pRoot = next;
   16067                 :   }else{
   16068                 :     mem3.aPool[prev].u.list.next = next;
   16069                 :   }
   16070                 :   if( next ){
   16071                 :     mem3.aPool[next].u.list.prev = prev;
   16072                 :   }
   16073                 :   mem3.aPool[i].u.list.next = 0;
   16074                 :   mem3.aPool[i].u.list.prev = 0;
   16075                 : }
   16076                 : 
   16077                 : /*
   16078                 : ** Unlink the chunk at index i from 
   16079                 : ** whatever list is currently a member of.
   16080                 : */
   16081                 : static void memsys3Unlink(u32 i){
   16082                 :   u32 size, hash;
   16083                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16084                 :   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
   16085                 :   assert( i>=1 );
   16086                 :   size = mem3.aPool[i-1].u.hdr.size4x/4;
   16087                 :   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
   16088                 :   assert( size>=2 );
   16089                 :   if( size <= MX_SMALL ){
   16090                 :     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
   16091                 :   }else{
   16092                 :     hash = size % N_HASH;
   16093                 :     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
   16094                 :   }
   16095                 : }
   16096                 : 
   16097                 : /*
   16098                 : ** Link the chunk at mem3.aPool[i] so that is on the list rooted
   16099                 : ** at *pRoot.
   16100                 : */
   16101                 : static void memsys3LinkIntoList(u32 i, u32 *pRoot){
   16102                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16103                 :   mem3.aPool[i].u.list.next = *pRoot;
   16104                 :   mem3.aPool[i].u.list.prev = 0;
   16105                 :   if( *pRoot ){
   16106                 :     mem3.aPool[*pRoot].u.list.prev = i;
   16107                 :   }
   16108                 :   *pRoot = i;
   16109                 : }
   16110                 : 
   16111                 : /*
   16112                 : ** Link the chunk at index i into either the appropriate
   16113                 : ** small chunk list, or into the large chunk hash table.
   16114                 : */
   16115                 : static void memsys3Link(u32 i){
   16116                 :   u32 size, hash;
   16117                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16118                 :   assert( i>=1 );
   16119                 :   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
   16120                 :   size = mem3.aPool[i-1].u.hdr.size4x/4;
   16121                 :   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
   16122                 :   assert( size>=2 );
   16123                 :   if( size <= MX_SMALL ){
   16124                 :     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
   16125                 :   }else{
   16126                 :     hash = size % N_HASH;
   16127                 :     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
   16128                 :   }
   16129                 : }
   16130                 : 
   16131                 : /*
   16132                 : ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
   16133                 : ** will already be held (obtained by code in malloc.c) if
   16134                 : ** sqlite3GlobalConfig.bMemStat is true.
   16135                 : */
   16136                 : static void memsys3Enter(void){
   16137                 :   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
   16138                 :     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   16139                 :   }
   16140                 :   sqlite3_mutex_enter(mem3.mutex);
   16141                 : }
   16142                 : static void memsys3Leave(void){
   16143                 :   sqlite3_mutex_leave(mem3.mutex);
   16144                 : }
   16145                 : 
   16146                 : /*
   16147                 : ** Called when we are unable to satisfy an allocation of nBytes.
   16148                 : */
   16149                 : static void memsys3OutOfMemory(int nByte){
   16150                 :   if( !mem3.alarmBusy ){
   16151                 :     mem3.alarmBusy = 1;
   16152                 :     assert( sqlite3_mutex_held(mem3.mutex) );
   16153                 :     sqlite3_mutex_leave(mem3.mutex);
   16154                 :     sqlite3_release_memory(nByte);
   16155                 :     sqlite3_mutex_enter(mem3.mutex);
   16156                 :     mem3.alarmBusy = 0;
   16157                 :   }
   16158                 : }
   16159                 : 
   16160                 : 
   16161                 : /*
   16162                 : ** Chunk i is a free chunk that has been unlinked.  Adjust its 
   16163                 : ** size parameters for check-out and return a pointer to the 
   16164                 : ** user portion of the chunk.
   16165                 : */
   16166                 : static void *memsys3Checkout(u32 i, u32 nBlock){
   16167                 :   u32 x;
   16168                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16169                 :   assert( i>=1 );
   16170                 :   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
   16171                 :   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
   16172                 :   x = mem3.aPool[i-1].u.hdr.size4x;
   16173                 :   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
   16174                 :   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
   16175                 :   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
   16176                 :   return &mem3.aPool[i];
   16177                 : }
   16178                 : 
   16179                 : /*
   16180                 : ** Carve a piece off of the end of the mem3.iMaster free chunk.
   16181                 : ** Return a pointer to the new allocation.  Or, if the master chunk
   16182                 : ** is not large enough, return 0.
   16183                 : */
   16184                 : static void *memsys3FromMaster(u32 nBlock){
   16185                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16186                 :   assert( mem3.szMaster>=nBlock );
   16187                 :   if( nBlock>=mem3.szMaster-1 ){
   16188                 :     /* Use the entire master */
   16189                 :     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
   16190                 :     mem3.iMaster = 0;
   16191                 :     mem3.szMaster = 0;
   16192                 :     mem3.mnMaster = 0;
   16193                 :     return p;
   16194                 :   }else{
   16195                 :     /* Split the master block.  Return the tail. */
   16196                 :     u32 newi, x;
   16197                 :     newi = mem3.iMaster + mem3.szMaster - nBlock;
   16198                 :     assert( newi > mem3.iMaster+1 );
   16199                 :     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
   16200                 :     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
   16201                 :     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
   16202                 :     mem3.szMaster -= nBlock;
   16203                 :     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
   16204                 :     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
   16205                 :     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
   16206                 :     if( mem3.szMaster < mem3.mnMaster ){
   16207                 :       mem3.mnMaster = mem3.szMaster;
   16208                 :     }
   16209                 :     return (void*)&mem3.aPool[newi];
   16210                 :   }
   16211                 : }
   16212                 : 
   16213                 : /*
   16214                 : ** *pRoot is the head of a list of free chunks of the same size
   16215                 : ** or same size hash.  In other words, *pRoot is an entry in either
   16216                 : ** mem3.aiSmall[] or mem3.aiHash[].  
   16217                 : **
   16218                 : ** This routine examines all entries on the given list and tries
   16219                 : ** to coalesce each entries with adjacent free chunks.  
   16220                 : **
   16221                 : ** If it sees a chunk that is larger than mem3.iMaster, it replaces 
   16222                 : ** the current mem3.iMaster with the new larger chunk.  In order for
   16223                 : ** this mem3.iMaster replacement to work, the master chunk must be
   16224                 : ** linked into the hash tables.  That is not the normal state of
   16225                 : ** affairs, of course.  The calling routine must link the master
   16226                 : ** chunk before invoking this routine, then must unlink the (possibly
   16227                 : ** changed) master chunk once this routine has finished.
   16228                 : */
   16229                 : static void memsys3Merge(u32 *pRoot){
   16230                 :   u32 iNext, prev, size, i, x;
   16231                 : 
   16232                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16233                 :   for(i=*pRoot; i>0; i=iNext){
   16234                 :     iNext = mem3.aPool[i].u.list.next;
   16235                 :     size = mem3.aPool[i-1].u.hdr.size4x;
   16236                 :     assert( (size&1)==0 );
   16237                 :     if( (size&2)==0 ){
   16238                 :       memsys3UnlinkFromList(i, pRoot);
   16239                 :       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
   16240                 :       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
   16241                 :       if( prev==iNext ){
   16242                 :         iNext = mem3.aPool[prev].u.list.next;
   16243                 :       }
   16244                 :       memsys3Unlink(prev);
   16245                 :       size = i + size/4 - prev;
   16246                 :       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
   16247                 :       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
   16248                 :       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
   16249                 :       memsys3Link(prev);
   16250                 :       i = prev;
   16251                 :     }else{
   16252                 :       size /= 4;
   16253                 :     }
   16254                 :     if( size>mem3.szMaster ){
   16255                 :       mem3.iMaster = i;
   16256                 :       mem3.szMaster = size;
   16257                 :     }
   16258                 :   }
   16259                 : }
   16260                 : 
   16261                 : /*
   16262                 : ** Return a block of memory of at least nBytes in size.
   16263                 : ** Return NULL if unable.
   16264                 : **
   16265                 : ** This function assumes that the necessary mutexes, if any, are
   16266                 : ** already held by the caller. Hence "Unsafe".
   16267                 : */
   16268                 : static void *memsys3MallocUnsafe(int nByte){
   16269                 :   u32 i;
   16270                 :   u32 nBlock;
   16271                 :   u32 toFree;
   16272                 : 
   16273                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16274                 :   assert( sizeof(Mem3Block)==8 );
   16275                 :   if( nByte<=12 ){
   16276                 :     nBlock = 2;
   16277                 :   }else{
   16278                 :     nBlock = (nByte + 11)/8;
   16279                 :   }
   16280                 :   assert( nBlock>=2 );
   16281                 : 
   16282                 :   /* STEP 1:
   16283                 :   ** Look for an entry of the correct size in either the small
   16284                 :   ** chunk table or in the large chunk hash table.  This is
   16285                 :   ** successful most of the time (about 9 times out of 10).
   16286                 :   */
   16287                 :   if( nBlock <= MX_SMALL ){
   16288                 :     i = mem3.aiSmall[nBlock-2];
   16289                 :     if( i>0 ){
   16290                 :       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
   16291                 :       return memsys3Checkout(i, nBlock);
   16292                 :     }
   16293                 :   }else{
   16294                 :     int hash = nBlock % N_HASH;
   16295                 :     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
   16296                 :       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
   16297                 :         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
   16298                 :         return memsys3Checkout(i, nBlock);
   16299                 :       }
   16300                 :     }
   16301                 :   }
   16302                 : 
   16303                 :   /* STEP 2:
   16304                 :   ** Try to satisfy the allocation by carving a piece off of the end
   16305                 :   ** of the master chunk.  This step usually works if step 1 fails.
   16306                 :   */
   16307                 :   if( mem3.szMaster>=nBlock ){
   16308                 :     return memsys3FromMaster(nBlock);
   16309                 :   }
   16310                 : 
   16311                 : 
   16312                 :   /* STEP 3:  
   16313                 :   ** Loop through the entire memory pool.  Coalesce adjacent free
   16314                 :   ** chunks.  Recompute the master chunk as the largest free chunk.
   16315                 :   ** Then try again to satisfy the allocation by carving a piece off
   16316                 :   ** of the end of the master chunk.  This step happens very
   16317                 :   ** rarely (we hope!)
   16318                 :   */
   16319                 :   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
   16320                 :     memsys3OutOfMemory(toFree);
   16321                 :     if( mem3.iMaster ){
   16322                 :       memsys3Link(mem3.iMaster);
   16323                 :       mem3.iMaster = 0;
   16324                 :       mem3.szMaster = 0;
   16325                 :     }
   16326                 :     for(i=0; i<N_HASH; i++){
   16327                 :       memsys3Merge(&mem3.aiHash[i]);
   16328                 :     }
   16329                 :     for(i=0; i<MX_SMALL-1; i++){
   16330                 :       memsys3Merge(&mem3.aiSmall[i]);
   16331                 :     }
   16332                 :     if( mem3.szMaster ){
   16333                 :       memsys3Unlink(mem3.iMaster);
   16334                 :       if( mem3.szMaster>=nBlock ){
   16335                 :         return memsys3FromMaster(nBlock);
   16336                 :       }
   16337                 :     }
   16338                 :   }
   16339                 : 
   16340                 :   /* If none of the above worked, then we fail. */
   16341                 :   return 0;
   16342                 : }
   16343                 : 
   16344                 : /*
   16345                 : ** Free an outstanding memory allocation.
   16346                 : **
   16347                 : ** This function assumes that the necessary mutexes, if any, are
   16348                 : ** already held by the caller. Hence "Unsafe".
   16349                 : */
   16350                 : static void memsys3FreeUnsafe(void *pOld){
   16351                 :   Mem3Block *p = (Mem3Block*)pOld;
   16352                 :   int i;
   16353                 :   u32 size, x;
   16354                 :   assert( sqlite3_mutex_held(mem3.mutex) );
   16355                 :   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
   16356                 :   i = p - mem3.aPool;
   16357                 :   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
   16358                 :   size = mem3.aPool[i-1].u.hdr.size4x/4;
   16359                 :   assert( i+size<=mem3.nPool+1 );
   16360                 :   mem3.aPool[i-1].u.hdr.size4x &= ~1;
   16361                 :   mem3.aPool[i+size-1].u.hdr.prevSize = size;
   16362                 :   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
   16363                 :   memsys3Link(i);
   16364                 : 
   16365                 :   /* Try to expand the master using the newly freed chunk */
   16366                 :   if( mem3.iMaster ){
   16367                 :     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
   16368                 :       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
   16369                 :       mem3.iMaster -= size;
   16370                 :       mem3.szMaster += size;
   16371                 :       memsys3Unlink(mem3.iMaster);
   16372                 :       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
   16373                 :       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
   16374                 :       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
   16375                 :     }
   16376                 :     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
   16377                 :     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
   16378                 :       memsys3Unlink(mem3.iMaster+mem3.szMaster);
   16379                 :       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
   16380                 :       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
   16381                 :       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
   16382                 :     }
   16383                 :   }
   16384                 : }
   16385                 : 
   16386                 : /*
   16387                 : ** Return the size of an outstanding allocation, in bytes.  The
   16388                 : ** size returned omits the 8-byte header overhead.  This only
   16389                 : ** works for chunks that are currently checked out.
   16390                 : */
   16391                 : static int memsys3Size(void *p){
   16392                 :   Mem3Block *pBlock;
   16393                 :   if( p==0 ) return 0;
   16394                 :   pBlock = (Mem3Block*)p;
   16395                 :   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
   16396                 :   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
   16397                 : }
   16398                 : 
   16399                 : /*
   16400                 : ** Round up a request size to the next valid allocation size.
   16401                 : */
   16402                 : static int memsys3Roundup(int n){
   16403                 :   if( n<=12 ){
   16404                 :     return 12;
   16405                 :   }else{
   16406                 :     return ((n+11)&~7) - 4;
   16407                 :   }
   16408                 : }
   16409                 : 
   16410                 : /*
   16411                 : ** Allocate nBytes of memory.
   16412                 : */
   16413                 : static void *memsys3Malloc(int nBytes){
   16414                 :   sqlite3_int64 *p;
   16415                 :   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
   16416                 :   memsys3Enter();
   16417                 :   p = memsys3MallocUnsafe(nBytes);
   16418                 :   memsys3Leave();
   16419                 :   return (void*)p; 
   16420                 : }
   16421                 : 
   16422                 : /*
   16423                 : ** Free memory.
   16424                 : */
   16425                 : static void memsys3Free(void *pPrior){
   16426                 :   assert( pPrior );
   16427                 :   memsys3Enter();
   16428                 :   memsys3FreeUnsafe(pPrior);
   16429                 :   memsys3Leave();
   16430                 : }
   16431                 : 
   16432                 : /*
   16433                 : ** Change the size of an existing memory allocation
   16434                 : */
   16435                 : static void *memsys3Realloc(void *pPrior, int nBytes){
   16436                 :   int nOld;
   16437                 :   void *p;
   16438                 :   if( pPrior==0 ){
   16439                 :     return sqlite3_malloc(nBytes);
   16440                 :   }
   16441                 :   if( nBytes<=0 ){
   16442                 :     sqlite3_free(pPrior);
   16443                 :     return 0;
   16444                 :   }
   16445                 :   nOld = memsys3Size(pPrior);
   16446                 :   if( nBytes<=nOld && nBytes>=nOld-128 ){
   16447                 :     return pPrior;
   16448                 :   }
   16449                 :   memsys3Enter();
   16450                 :   p = memsys3MallocUnsafe(nBytes);
   16451                 :   if( p ){
   16452                 :     if( nOld<nBytes ){
   16453                 :       memcpy(p, pPrior, nOld);
   16454                 :     }else{
   16455                 :       memcpy(p, pPrior, nBytes);
   16456                 :     }
   16457                 :     memsys3FreeUnsafe(pPrior);
   16458                 :   }
   16459                 :   memsys3Leave();
   16460                 :   return p;
   16461                 : }
   16462                 : 
   16463                 : /*
   16464                 : ** Initialize this module.
   16465                 : */
   16466                 : static int memsys3Init(void *NotUsed){
   16467                 :   UNUSED_PARAMETER(NotUsed);
   16468                 :   if( !sqlite3GlobalConfig.pHeap ){
   16469                 :     return SQLITE_ERROR;
   16470                 :   }
   16471                 : 
   16472                 :   /* Store a pointer to the memory block in global structure mem3. */
   16473                 :   assert( sizeof(Mem3Block)==8 );
   16474                 :   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
   16475                 :   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
   16476                 : 
   16477                 :   /* Initialize the master block. */
   16478                 :   mem3.szMaster = mem3.nPool;
   16479                 :   mem3.mnMaster = mem3.szMaster;
   16480                 :   mem3.iMaster = 1;
   16481                 :   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
   16482                 :   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
   16483                 :   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
   16484                 : 
   16485                 :   return SQLITE_OK;
   16486                 : }
   16487                 : 
   16488                 : /*
   16489                 : ** Deinitialize this module.
   16490                 : */
   16491                 : static void memsys3Shutdown(void *NotUsed){
   16492                 :   UNUSED_PARAMETER(NotUsed);
   16493                 :   mem3.mutex = 0;
   16494                 :   return;
   16495                 : }
   16496                 : 
   16497                 : 
   16498                 : 
   16499                 : /*
   16500                 : ** Open the file indicated and write a log of all unfreed memory 
   16501                 : ** allocations into that log.
   16502                 : */
   16503                 : SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
   16504                 : #ifdef SQLITE_DEBUG
   16505                 :   FILE *out;
   16506                 :   u32 i, j;
   16507                 :   u32 size;
   16508                 :   if( zFilename==0 || zFilename[0]==0 ){
   16509                 :     out = stdout;
   16510                 :   }else{
   16511                 :     out = fopen(zFilename, "w");
   16512                 :     if( out==0 ){
   16513                 :       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   16514                 :                       zFilename);
   16515                 :       return;
   16516                 :     }
   16517                 :   }
   16518                 :   memsys3Enter();
   16519                 :   fprintf(out, "CHUNKS:\n");
   16520                 :   for(i=1; i<=mem3.nPool; i+=size/4){
   16521                 :     size = mem3.aPool[i-1].u.hdr.size4x;
   16522                 :     if( size/4<=1 ){
   16523                 :       fprintf(out, "%p size error\n", &mem3.aPool[i]);
   16524                 :       assert( 0 );
   16525                 :       break;
   16526                 :     }
   16527                 :     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
   16528                 :       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
   16529                 :       assert( 0 );
   16530                 :       break;
   16531                 :     }
   16532                 :     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
   16533                 :       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
   16534                 :       assert( 0 );
   16535                 :       break;
   16536                 :     }
   16537                 :     if( size&1 ){
   16538                 :       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
   16539                 :     }else{
   16540                 :       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
   16541                 :                   i==mem3.iMaster ? " **master**" : "");
   16542                 :     }
   16543                 :   }
   16544                 :   for(i=0; i<MX_SMALL-1; i++){
   16545                 :     if( mem3.aiSmall[i]==0 ) continue;
   16546                 :     fprintf(out, "small(%2d):", i);
   16547                 :     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
   16548                 :       fprintf(out, " %p(%d)", &mem3.aPool[j],
   16549                 :               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
   16550                 :     }
   16551                 :     fprintf(out, "\n"); 
   16552                 :   }
   16553                 :   for(i=0; i<N_HASH; i++){
   16554                 :     if( mem3.aiHash[i]==0 ) continue;
   16555                 :     fprintf(out, "hash(%2d):", i);
   16556                 :     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
   16557                 :       fprintf(out, " %p(%d)", &mem3.aPool[j],
   16558                 :               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
   16559                 :     }
   16560                 :     fprintf(out, "\n"); 
   16561                 :   }
   16562                 :   fprintf(out, "master=%d\n", mem3.iMaster);
   16563                 :   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
   16564                 :   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
   16565                 :   sqlite3_mutex_leave(mem3.mutex);
   16566                 :   if( out==stdout ){
   16567                 :     fflush(stdout);
   16568                 :   }else{
   16569                 :     fclose(out);
   16570                 :   }
   16571                 : #else
   16572                 :   UNUSED_PARAMETER(zFilename);
   16573                 : #endif
   16574                 : }
   16575                 : 
   16576                 : /*
   16577                 : ** This routine is the only routine in this file with external 
   16578                 : ** linkage.
   16579                 : **
   16580                 : ** Populate the low-level memory allocation function pointers in
   16581                 : ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
   16582                 : ** arguments specify the block of memory to manage.
   16583                 : **
   16584                 : ** This routine is only called by sqlite3_config(), and therefore
   16585                 : ** is not required to be threadsafe (it is not).
   16586                 : */
   16587                 : SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
   16588                 :   static const sqlite3_mem_methods mempoolMethods = {
   16589                 :      memsys3Malloc,
   16590                 :      memsys3Free,
   16591                 :      memsys3Realloc,
   16592                 :      memsys3Size,
   16593                 :      memsys3Roundup,
   16594                 :      memsys3Init,
   16595                 :      memsys3Shutdown,
   16596                 :      0
   16597                 :   };
   16598                 :   return &mempoolMethods;
   16599                 : }
   16600                 : 
   16601                 : #endif /* SQLITE_ENABLE_MEMSYS3 */
   16602                 : 
   16603                 : /************** End of mem3.c ************************************************/
   16604                 : /************** Begin file mem5.c ********************************************/
   16605                 : /*
   16606                 : ** 2007 October 14
   16607                 : **
   16608                 : ** The author disclaims copyright to this source code.  In place of
   16609                 : ** a legal notice, here is a blessing:
   16610                 : **
   16611                 : **    May you do good and not evil.
   16612                 : **    May you find forgiveness for yourself and forgive others.
   16613                 : **    May you share freely, never taking more than you give.
   16614                 : **
   16615                 : *************************************************************************
   16616                 : ** This file contains the C functions that implement a memory
   16617                 : ** allocation subsystem for use by SQLite. 
   16618                 : **
   16619                 : ** This version of the memory allocation subsystem omits all
   16620                 : ** use of malloc(). The application gives SQLite a block of memory
   16621                 : ** before calling sqlite3_initialize() from which allocations
   16622                 : ** are made and returned by the xMalloc() and xRealloc() 
   16623                 : ** implementations. Once sqlite3_initialize() has been called,
   16624                 : ** the amount of memory available to SQLite is fixed and cannot
   16625                 : ** be changed.
   16626                 : **
   16627                 : ** This version of the memory allocation subsystem is included
   16628                 : ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
   16629                 : **
   16630                 : ** This memory allocator uses the following algorithm:
   16631                 : **
   16632                 : **   1.  All memory allocations sizes are rounded up to a power of 2.
   16633                 : **
   16634                 : **   2.  If two adjacent free blocks are the halves of a larger block,
   16635                 : **       then the two blocks are coalesed into the single larger block.
   16636                 : **
   16637                 : **   3.  New memory is allocated from the first available free block.
   16638                 : **
   16639                 : ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
   16640                 : ** Concerning Dynamic Storage Allocation". Journal of the Association for
   16641                 : ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
   16642                 : ** 
   16643                 : ** Let n be the size of the largest allocation divided by the minimum
   16644                 : ** allocation size (after rounding all sizes up to a power of 2.)  Let M
   16645                 : ** be the maximum amount of memory ever outstanding at one time.  Let
   16646                 : ** N be the total amount of memory available for allocation.  Robson
   16647                 : ** proved that this memory allocator will never breakdown due to 
   16648                 : ** fragmentation as long as the following constraint holds:
   16649                 : **
   16650                 : **      N >=  M*(1 + log2(n)/2) - n + 1
   16651                 : **
   16652                 : ** The sqlite3_status() logic tracks the maximum values of n and M so
   16653                 : ** that an application can, at any time, verify this constraint.
   16654                 : */
   16655                 : 
   16656                 : /*
   16657                 : ** This version of the memory allocator is used only when 
   16658                 : ** SQLITE_ENABLE_MEMSYS5 is defined.
   16659                 : */
   16660                 : #ifdef SQLITE_ENABLE_MEMSYS5
   16661                 : 
   16662                 : /*
   16663                 : ** A minimum allocation is an instance of the following structure.
   16664                 : ** Larger allocations are an array of these structures where the
   16665                 : ** size of the array is a power of 2.
   16666                 : **
   16667                 : ** The size of this object must be a power of two.  That fact is
   16668                 : ** verified in memsys5Init().
   16669                 : */
   16670                 : typedef struct Mem5Link Mem5Link;
   16671                 : struct Mem5Link {
   16672                 :   int next;       /* Index of next free chunk */
   16673                 :   int prev;       /* Index of previous free chunk */
   16674                 : };
   16675                 : 
   16676                 : /*
   16677                 : ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
   16678                 : ** mem5.szAtom is always at least 8 and 32-bit integers are used,
   16679                 : ** it is not actually possible to reach this limit.
   16680                 : */
   16681                 : #define LOGMAX 30
   16682                 : 
   16683                 : /*
   16684                 : ** Masks used for mem5.aCtrl[] elements.
   16685                 : */
   16686                 : #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
   16687                 : #define CTRL_FREE     0x20    /* True if not checked out */
   16688                 : 
   16689                 : /*
   16690                 : ** All of the static variables used by this module are collected
   16691                 : ** into a single structure named "mem5".  This is to keep the
   16692                 : ** static variables organized and to reduce namespace pollution
   16693                 : ** when this module is combined with other in the amalgamation.
   16694                 : */
   16695                 : static SQLITE_WSD struct Mem5Global {
   16696                 :   /*
   16697                 :   ** Memory available for allocation
   16698                 :   */
   16699                 :   int szAtom;      /* Smallest possible allocation in bytes */
   16700                 :   int nBlock;      /* Number of szAtom sized blocks in zPool */
   16701                 :   u8 *zPool;       /* Memory available to be allocated */
   16702                 :   
   16703                 :   /*
   16704                 :   ** Mutex to control access to the memory allocation subsystem.
   16705                 :   */
   16706                 :   sqlite3_mutex *mutex;
   16707                 : 
   16708                 :   /*
   16709                 :   ** Performance statistics
   16710                 :   */
   16711                 :   u64 nAlloc;         /* Total number of calls to malloc */
   16712                 :   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
   16713                 :   u64 totalExcess;    /* Total internal fragmentation */
   16714                 :   u32 currentOut;     /* Current checkout, including internal fragmentation */
   16715                 :   u32 currentCount;   /* Current number of distinct checkouts */
   16716                 :   u32 maxOut;         /* Maximum instantaneous currentOut */
   16717                 :   u32 maxCount;       /* Maximum instantaneous currentCount */
   16718                 :   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
   16719                 :   
   16720                 :   /*
   16721                 :   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
   16722                 :   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
   16723                 :   ** and so forth.
   16724                 :   */
   16725                 :   int aiFreelist[LOGMAX+1];
   16726                 : 
   16727                 :   /*
   16728                 :   ** Space for tracking which blocks are checked out and the size
   16729                 :   ** of each block.  One byte per block.
   16730                 :   */
   16731                 :   u8 *aCtrl;
   16732                 : 
   16733                 : } mem5;
   16734                 : 
   16735                 : /*
   16736                 : ** Access the static variable through a macro for SQLITE_OMIT_WSD
   16737                 : */
   16738                 : #define mem5 GLOBAL(struct Mem5Global, mem5)
   16739                 : 
   16740                 : /*
   16741                 : ** Assuming mem5.zPool is divided up into an array of Mem5Link
   16742                 : ** structures, return a pointer to the idx-th such lik.
   16743                 : */
   16744                 : #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
   16745                 : 
   16746                 : /*
   16747                 : ** Unlink the chunk at mem5.aPool[i] from list it is currently
   16748                 : ** on.  It should be found on mem5.aiFreelist[iLogsize].
   16749                 : */
   16750                 : static void memsys5Unlink(int i, int iLogsize){
   16751                 :   int next, prev;
   16752                 :   assert( i>=0 && i<mem5.nBlock );
   16753                 :   assert( iLogsize>=0 && iLogsize<=LOGMAX );
   16754                 :   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
   16755                 : 
   16756                 :   next = MEM5LINK(i)->next;
   16757                 :   prev = MEM5LINK(i)->prev;
   16758                 :   if( prev<0 ){
   16759                 :     mem5.aiFreelist[iLogsize] = next;
   16760                 :   }else{
   16761                 :     MEM5LINK(prev)->next = next;
   16762                 :   }
   16763                 :   if( next>=0 ){
   16764                 :     MEM5LINK(next)->prev = prev;
   16765                 :   }
   16766                 : }
   16767                 : 
   16768                 : /*
   16769                 : ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
   16770                 : ** free list.
   16771                 : */
   16772                 : static void memsys5Link(int i, int iLogsize){
   16773                 :   int x;
   16774                 :   assert( sqlite3_mutex_held(mem5.mutex) );
   16775                 :   assert( i>=0 && i<mem5.nBlock );
   16776                 :   assert( iLogsize>=0 && iLogsize<=LOGMAX );
   16777                 :   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
   16778                 : 
   16779                 :   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
   16780                 :   MEM5LINK(i)->prev = -1;
   16781                 :   if( x>=0 ){
   16782                 :     assert( x<mem5.nBlock );
   16783                 :     MEM5LINK(x)->prev = i;
   16784                 :   }
   16785                 :   mem5.aiFreelist[iLogsize] = i;
   16786                 : }
   16787                 : 
   16788                 : /*
   16789                 : ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
   16790                 : ** will already be held (obtained by code in malloc.c) if
   16791                 : ** sqlite3GlobalConfig.bMemStat is true.
   16792                 : */
   16793                 : static void memsys5Enter(void){
   16794                 :   sqlite3_mutex_enter(mem5.mutex);
   16795                 : }
   16796                 : static void memsys5Leave(void){
   16797                 :   sqlite3_mutex_leave(mem5.mutex);
   16798                 : }
   16799                 : 
   16800                 : /*
   16801                 : ** Return the size of an outstanding allocation, in bytes.  The
   16802                 : ** size returned omits the 8-byte header overhead.  This only
   16803                 : ** works for chunks that are currently checked out.
   16804                 : */
   16805                 : static int memsys5Size(void *p){
   16806                 :   int iSize = 0;
   16807                 :   if( p ){
   16808                 :     int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
   16809                 :     assert( i>=0 && i<mem5.nBlock );
   16810                 :     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
   16811                 :   }
   16812                 :   return iSize;
   16813                 : }
   16814                 : 
   16815                 : /*
   16816                 : ** Find the first entry on the freelist iLogsize.  Unlink that
   16817                 : ** entry and return its index. 
   16818                 : */
   16819                 : static int memsys5UnlinkFirst(int iLogsize){
   16820                 :   int i;
   16821                 :   int iFirst;
   16822                 : 
   16823                 :   assert( iLogsize>=0 && iLogsize<=LOGMAX );
   16824                 :   i = iFirst = mem5.aiFreelist[iLogsize];
   16825                 :   assert( iFirst>=0 );
   16826                 :   while( i>0 ){
   16827                 :     if( i<iFirst ) iFirst = i;
   16828                 :     i = MEM5LINK(i)->next;
   16829                 :   }
   16830                 :   memsys5Unlink(iFirst, iLogsize);
   16831                 :   return iFirst;
   16832                 : }
   16833                 : 
   16834                 : /*
   16835                 : ** Return a block of memory of at least nBytes in size.
   16836                 : ** Return NULL if unable.  Return NULL if nBytes==0.
   16837                 : **
   16838                 : ** The caller guarantees that nByte positive.
   16839                 : **
   16840                 : ** The caller has obtained a mutex prior to invoking this
   16841                 : ** routine so there is never any chance that two or more
   16842                 : ** threads can be in this routine at the same time.
   16843                 : */
   16844                 : static void *memsys5MallocUnsafe(int nByte){
   16845                 :   int i;           /* Index of a mem5.aPool[] slot */
   16846                 :   int iBin;        /* Index into mem5.aiFreelist[] */
   16847                 :   int iFullSz;     /* Size of allocation rounded up to power of 2 */
   16848                 :   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
   16849                 : 
   16850                 :   /* nByte must be a positive */
   16851                 :   assert( nByte>0 );
   16852                 : 
   16853                 :   /* Keep track of the maximum allocation request.  Even unfulfilled
   16854                 :   ** requests are counted */
   16855                 :   if( (u32)nByte>mem5.maxRequest ){
   16856                 :     mem5.maxRequest = nByte;
   16857                 :   }
   16858                 : 
   16859                 :   /* Abort if the requested allocation size is larger than the largest
   16860                 :   ** power of two that we can represent using 32-bit signed integers.
   16861                 :   */
   16862                 :   if( nByte > 0x40000000 ){
   16863                 :     return 0;
   16864                 :   }
   16865                 : 
   16866                 :   /* Round nByte up to the next valid power of two */
   16867                 :   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
   16868                 : 
   16869                 :   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
   16870                 :   ** block.  If not, then split a block of the next larger power of
   16871                 :   ** two in order to create a new free block of size iLogsize.
   16872                 :   */
   16873                 :   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
   16874                 :   if( iBin>LOGMAX ){
   16875                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   16876                 :     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
   16877                 :     return 0;
   16878                 :   }
   16879                 :   i = memsys5UnlinkFirst(iBin);
   16880                 :   while( iBin>iLogsize ){
   16881                 :     int newSize;
   16882                 : 
   16883                 :     iBin--;
   16884                 :     newSize = 1 << iBin;
   16885                 :     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
   16886                 :     memsys5Link(i+newSize, iBin);
   16887                 :   }
   16888                 :   mem5.aCtrl[i] = iLogsize;
   16889                 : 
   16890                 :   /* Update allocator performance statistics. */
   16891                 :   mem5.nAlloc++;
   16892                 :   mem5.totalAlloc += iFullSz;
   16893                 :   mem5.totalExcess += iFullSz - nByte;
   16894                 :   mem5.currentCount++;
   16895                 :   mem5.currentOut += iFullSz;
   16896                 :   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
   16897                 :   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
   16898                 : 
   16899                 :   /* Return a pointer to the allocated memory. */
   16900                 :   return (void*)&mem5.zPool[i*mem5.szAtom];
   16901                 : }
   16902                 : 
   16903                 : /*
   16904                 : ** Free an outstanding memory allocation.
   16905                 : */
   16906                 : static void memsys5FreeUnsafe(void *pOld){
   16907                 :   u32 size, iLogsize;
   16908                 :   int iBlock;
   16909                 : 
   16910                 :   /* Set iBlock to the index of the block pointed to by pOld in 
   16911                 :   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
   16912                 :   */
   16913                 :   iBlock = ((u8 *)pOld-mem5.zPool)/mem5.szAtom;
   16914                 : 
   16915                 :   /* Check that the pointer pOld points to a valid, non-free block. */
   16916                 :   assert( iBlock>=0 && iBlock<mem5.nBlock );
   16917                 :   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
   16918                 :   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
   16919                 : 
   16920                 :   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
   16921                 :   size = 1<<iLogsize;
   16922                 :   assert( iBlock+size-1<(u32)mem5.nBlock );
   16923                 : 
   16924                 :   mem5.aCtrl[iBlock] |= CTRL_FREE;
   16925                 :   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
   16926                 :   assert( mem5.currentCount>0 );
   16927                 :   assert( mem5.currentOut>=(size*mem5.szAtom) );
   16928                 :   mem5.currentCount--;
   16929                 :   mem5.currentOut -= size*mem5.szAtom;
   16930                 :   assert( mem5.currentOut>0 || mem5.currentCount==0 );
   16931                 :   assert( mem5.currentCount>0 || mem5.currentOut==0 );
   16932                 : 
   16933                 :   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
   16934                 :   while( ALWAYS(iLogsize<LOGMAX) ){
   16935                 :     int iBuddy;
   16936                 :     if( (iBlock>>iLogsize) & 1 ){
   16937                 :       iBuddy = iBlock - size;
   16938                 :     }else{
   16939                 :       iBuddy = iBlock + size;
   16940                 :     }
   16941                 :     assert( iBuddy>=0 );
   16942                 :     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
   16943                 :     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
   16944                 :     memsys5Unlink(iBuddy, iLogsize);
   16945                 :     iLogsize++;
   16946                 :     if( iBuddy<iBlock ){
   16947                 :       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
   16948                 :       mem5.aCtrl[iBlock] = 0;
   16949                 :       iBlock = iBuddy;
   16950                 :     }else{
   16951                 :       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
   16952                 :       mem5.aCtrl[iBuddy] = 0;
   16953                 :     }
   16954                 :     size *= 2;
   16955                 :   }
   16956                 :   memsys5Link(iBlock, iLogsize);
   16957                 : }
   16958                 : 
   16959                 : /*
   16960                 : ** Allocate nBytes of memory
   16961                 : */
   16962                 : static void *memsys5Malloc(int nBytes){
   16963                 :   sqlite3_int64 *p = 0;
   16964                 :   if( nBytes>0 ){
   16965                 :     memsys5Enter();
   16966                 :     p = memsys5MallocUnsafe(nBytes);
   16967                 :     memsys5Leave();
   16968                 :   }
   16969                 :   return (void*)p; 
   16970                 : }
   16971                 : 
   16972                 : /*
   16973                 : ** Free memory.
   16974                 : **
   16975                 : ** The outer layer memory allocator prevents this routine from
   16976                 : ** being called with pPrior==0.
   16977                 : */
   16978                 : static void memsys5Free(void *pPrior){
   16979                 :   assert( pPrior!=0 );
   16980                 :   memsys5Enter();
   16981                 :   memsys5FreeUnsafe(pPrior);
   16982                 :   memsys5Leave();  
   16983                 : }
   16984                 : 
   16985                 : /*
   16986                 : ** Change the size of an existing memory allocation.
   16987                 : **
   16988                 : ** The outer layer memory allocator prevents this routine from
   16989                 : ** being called with pPrior==0.  
   16990                 : **
   16991                 : ** nBytes is always a value obtained from a prior call to
   16992                 : ** memsys5Round().  Hence nBytes is always a non-negative power
   16993                 : ** of two.  If nBytes==0 that means that an oversize allocation
   16994                 : ** (an allocation larger than 0x40000000) was requested and this
   16995                 : ** routine should return 0 without freeing pPrior.
   16996                 : */
   16997                 : static void *memsys5Realloc(void *pPrior, int nBytes){
   16998                 :   int nOld;
   16999                 :   void *p;
   17000                 :   assert( pPrior!=0 );
   17001                 :   assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
   17002                 :   assert( nBytes>=0 );
   17003                 :   if( nBytes==0 ){
   17004                 :     return 0;
   17005                 :   }
   17006                 :   nOld = memsys5Size(pPrior);
   17007                 :   if( nBytes<=nOld ){
   17008                 :     return pPrior;
   17009                 :   }
   17010                 :   memsys5Enter();
   17011                 :   p = memsys5MallocUnsafe(nBytes);
   17012                 :   if( p ){
   17013                 :     memcpy(p, pPrior, nOld);
   17014                 :     memsys5FreeUnsafe(pPrior);
   17015                 :   }
   17016                 :   memsys5Leave();
   17017                 :   return p;
   17018                 : }
   17019                 : 
   17020                 : /*
   17021                 : ** Round up a request size to the next valid allocation size.  If
   17022                 : ** the allocation is too large to be handled by this allocation system,
   17023                 : ** return 0.
   17024                 : **
   17025                 : ** All allocations must be a power of two and must be expressed by a
   17026                 : ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
   17027                 : ** or 1073741824 bytes.
   17028                 : */
   17029                 : static int memsys5Roundup(int n){
   17030                 :   int iFullSz;
   17031                 :   if( n > 0x40000000 ) return 0;
   17032                 :   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
   17033                 :   return iFullSz;
   17034                 : }
   17035                 : 
   17036                 : /*
   17037                 : ** Return the ceiling of the logarithm base 2 of iValue.
   17038                 : **
   17039                 : ** Examples:   memsys5Log(1) -> 0
   17040                 : **             memsys5Log(2) -> 1
   17041                 : **             memsys5Log(4) -> 2
   17042                 : **             memsys5Log(5) -> 3
   17043                 : **             memsys5Log(8) -> 3
   17044                 : **             memsys5Log(9) -> 4
   17045                 : */
   17046                 : static int memsys5Log(int iValue){
   17047                 :   int iLog;
   17048                 :   for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
   17049                 :   return iLog;
   17050                 : }
   17051                 : 
   17052                 : /*
   17053                 : ** Initialize the memory allocator.
   17054                 : **
   17055                 : ** This routine is not threadsafe.  The caller must be holding a mutex
   17056                 : ** to prevent multiple threads from entering at the same time.
   17057                 : */
   17058                 : static int memsys5Init(void *NotUsed){
   17059                 :   int ii;            /* Loop counter */
   17060                 :   int nByte;         /* Number of bytes of memory available to this allocator */
   17061                 :   u8 *zByte;         /* Memory usable by this allocator */
   17062                 :   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
   17063                 :   int iOffset;       /* An offset into mem5.aCtrl[] */
   17064                 : 
   17065                 :   UNUSED_PARAMETER(NotUsed);
   17066                 : 
   17067                 :   /* For the purposes of this routine, disable the mutex */
   17068                 :   mem5.mutex = 0;
   17069                 : 
   17070                 :   /* The size of a Mem5Link object must be a power of two.  Verify that
   17071                 :   ** this is case.
   17072                 :   */
   17073                 :   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
   17074                 : 
   17075                 :   nByte = sqlite3GlobalConfig.nHeap;
   17076                 :   zByte = (u8*)sqlite3GlobalConfig.pHeap;
   17077                 :   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
   17078                 : 
   17079                 :   /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
   17080                 :   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
   17081                 :   mem5.szAtom = (1<<nMinLog);
   17082                 :   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
   17083                 :     mem5.szAtom = mem5.szAtom << 1;
   17084                 :   }
   17085                 : 
   17086                 :   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
   17087                 :   mem5.zPool = zByte;
   17088                 :   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
   17089                 : 
   17090                 :   for(ii=0; ii<=LOGMAX; ii++){
   17091                 :     mem5.aiFreelist[ii] = -1;
   17092                 :   }
   17093                 : 
   17094                 :   iOffset = 0;
   17095                 :   for(ii=LOGMAX; ii>=0; ii--){
   17096                 :     int nAlloc = (1<<ii);
   17097                 :     if( (iOffset+nAlloc)<=mem5.nBlock ){
   17098                 :       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
   17099                 :       memsys5Link(iOffset, ii);
   17100                 :       iOffset += nAlloc;
   17101                 :     }
   17102                 :     assert((iOffset+nAlloc)>mem5.nBlock);
   17103                 :   }
   17104                 : 
   17105                 :   /* If a mutex is required for normal operation, allocate one */
   17106                 :   if( sqlite3GlobalConfig.bMemstat==0 ){
   17107                 :     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   17108                 :   }
   17109                 : 
   17110                 :   return SQLITE_OK;
   17111                 : }
   17112                 : 
   17113                 : /*
   17114                 : ** Deinitialize this module.
   17115                 : */
   17116                 : static void memsys5Shutdown(void *NotUsed){
   17117                 :   UNUSED_PARAMETER(NotUsed);
   17118                 :   mem5.mutex = 0;
   17119                 :   return;
   17120                 : }
   17121                 : 
   17122                 : #ifdef SQLITE_TEST
   17123                 : /*
   17124                 : ** Open the file indicated and write a log of all unfreed memory 
   17125                 : ** allocations into that log.
   17126                 : */
   17127                 : SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
   17128                 :   FILE *out;
   17129                 :   int i, j, n;
   17130                 :   int nMinLog;
   17131                 : 
   17132                 :   if( zFilename==0 || zFilename[0]==0 ){
   17133                 :     out = stdout;
   17134                 :   }else{
   17135                 :     out = fopen(zFilename, "w");
   17136                 :     if( out==0 ){
   17137                 :       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   17138                 :                       zFilename);
   17139                 :       return;
   17140                 :     }
   17141                 :   }
   17142                 :   memsys5Enter();
   17143                 :   nMinLog = memsys5Log(mem5.szAtom);
   17144                 :   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
   17145                 :     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
   17146                 :     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
   17147                 :   }
   17148                 :   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
   17149                 :   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
   17150                 :   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
   17151                 :   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
   17152                 :   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
   17153                 :   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
   17154                 :   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
   17155                 :   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
   17156                 :   memsys5Leave();
   17157                 :   if( out==stdout ){
   17158                 :     fflush(stdout);
   17159                 :   }else{
   17160                 :     fclose(out);
   17161                 :   }
   17162                 : }
   17163                 : #endif
   17164                 : 
   17165                 : /*
   17166                 : ** This routine is the only routine in this file with external 
   17167                 : ** linkage. It returns a pointer to a static sqlite3_mem_methods
   17168                 : ** struct populated with the memsys5 methods.
   17169                 : */
   17170                 : SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
   17171                 :   static const sqlite3_mem_methods memsys5Methods = {
   17172                 :      memsys5Malloc,
   17173                 :      memsys5Free,
   17174                 :      memsys5Realloc,
   17175                 :      memsys5Size,
   17176                 :      memsys5Roundup,
   17177                 :      memsys5Init,
   17178                 :      memsys5Shutdown,
   17179                 :      0
   17180                 :   };
   17181                 :   return &memsys5Methods;
   17182                 : }
   17183                 : 
   17184                 : #endif /* SQLITE_ENABLE_MEMSYS5 */
   17185                 : 
   17186                 : /************** End of mem5.c ************************************************/
   17187                 : /************** Begin file mutex.c *******************************************/
   17188                 : /*
   17189                 : ** 2007 August 14
   17190                 : **
   17191                 : ** The author disclaims copyright to this source code.  In place of
   17192                 : ** a legal notice, here is a blessing:
   17193                 : **
   17194                 : **    May you do good and not evil.
   17195                 : **    May you find forgiveness for yourself and forgive others.
   17196                 : **    May you share freely, never taking more than you give.
   17197                 : **
   17198                 : *************************************************************************
   17199                 : ** This file contains the C functions that implement mutexes.
   17200                 : **
   17201                 : ** This file contains code that is common across all mutex implementations.
   17202                 : */
   17203                 : 
   17204                 : #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
   17205                 : /*
   17206                 : ** For debugging purposes, record when the mutex subsystem is initialized
   17207                 : ** and uninitialized so that we can assert() if there is an attempt to
   17208                 : ** allocate a mutex while the system is uninitialized.
   17209                 : */
   17210                 : static SQLITE_WSD int mutexIsInit = 0;
   17211                 : #endif /* SQLITE_DEBUG */
   17212                 : 
   17213                 : 
   17214                 : #ifndef SQLITE_MUTEX_OMIT
   17215                 : /*
   17216                 : ** Initialize the mutex system.
   17217                 : */
   17218            6448 : SQLITE_PRIVATE int sqlite3MutexInit(void){ 
   17219            6448 :   int rc = SQLITE_OK;
   17220            6448 :   if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
   17221                 :     /* If the xMutexAlloc method has not been set, then the user did not
   17222                 :     ** install a mutex implementation via sqlite3_config() prior to 
   17223                 :     ** sqlite3_initialize() being called. This block copies pointers to
   17224                 :     ** the default implementation into the sqlite3GlobalConfig structure.
   17225                 :     */
   17226                 :     sqlite3_mutex_methods const *pFrom;
   17227             805 :     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
   17228                 : 
   17229             805 :     if( sqlite3GlobalConfig.bCoreMutex ){
   17230             805 :       pFrom = sqlite3DefaultMutex();
   17231                 :     }else{
   17232               0 :       pFrom = sqlite3NoopMutex();
   17233                 :     }
   17234             805 :     memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
   17235             805 :     memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
   17236                 :            sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
   17237             805 :     pTo->xMutexAlloc = pFrom->xMutexAlloc;
   17238                 :   }
   17239            6448 :   rc = sqlite3GlobalConfig.mutex.xMutexInit();
   17240                 : 
   17241                 : #ifdef SQLITE_DEBUG
   17242            6448 :   GLOBAL(int, mutexIsInit) = 1;
   17243                 : #endif
   17244                 : 
   17245            6448 :   return rc;
   17246                 : }
   17247                 : 
   17248                 : /*
   17249                 : ** Shutdown the mutex system. This call frees resources allocated by
   17250                 : ** sqlite3MutexInit().
   17251                 : */
   17252             795 : SQLITE_PRIVATE int sqlite3MutexEnd(void){
   17253             795 :   int rc = SQLITE_OK;
   17254             795 :   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
   17255             795 :     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
   17256                 :   }
   17257                 : 
   17258                 : #ifdef SQLITE_DEBUG
   17259             795 :   GLOBAL(int, mutexIsInit) = 0;
   17260                 : #endif
   17261                 : 
   17262             795 :   return rc;
   17263                 : }
   17264                 : 
   17265                 : /*
   17266                 : ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
   17267                 : */
   17268            2776 : SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
   17269                 : #ifndef SQLITE_OMIT_AUTOINIT
   17270            2776 :   if( sqlite3_initialize() ) return 0;
   17271                 : #endif
   17272            2776 :   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
   17273                 : }
   17274                 : 
   17275         1194517 : SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
   17276         1194517 :   if( !sqlite3GlobalConfig.bCoreMutex ){
   17277               0 :     return 0;
   17278                 :   }
   17279         1194517 :   assert( GLOBAL(int, mutexIsInit) );
   17280         1194517 :   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
   17281                 : }
   17282                 : 
   17283                 : /*
   17284                 : ** Free a dynamic mutex.
   17285                 : */
   17286            8290 : SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
   17287            8290 :   if( p ){
   17288            8290 :     sqlite3GlobalConfig.mutex.xMutexFree(p);
   17289                 :   }
   17290            8290 : }
   17291                 : 
   17292                 : /*
   17293                 : ** Obtain the mutex p. If some other thread already has the mutex, block
   17294                 : ** until it can be obtained.
   17295                 : */
   17296        19668620 : SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
   17297        19668620 :   if( p ){
   17298        18453209 :     sqlite3GlobalConfig.mutex.xMutexEnter(p);
   17299                 :   }
   17300        19668533 : }
   17301                 : 
   17302                 : /*
   17303                 : ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
   17304                 : ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
   17305                 : */
   17306          396478 : SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
   17307          396478 :   int rc = SQLITE_OK;
   17308          396478 :   if( p ){
   17309          396478 :     return sqlite3GlobalConfig.mutex.xMutexTry(p);
   17310                 :   }
   17311               0 :   return rc;
   17312                 : }
   17313                 : 
   17314                 : /*
   17315                 : ** The sqlite3_mutex_leave() routine exits a mutex that was previously
   17316                 : ** entered by the same thread.  The behavior is undefined if the mutex 
   17317                 : ** is not currently entered. If a NULL pointer is passed as an argument
   17318                 : ** this function is a no-op.
   17319                 : */
   17320        20063501 : SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
   17321        20063501 :   if( p ){
   17322        18848218 :     sqlite3GlobalConfig.mutex.xMutexLeave(p);
   17323                 :   }
   17324        20063599 : }
   17325                 : 
   17326                 : #ifndef NDEBUG
   17327                 : /*
   17328                 : ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   17329                 : ** intended for use inside assert() statements.
   17330                 : */
   17331        80296356 : SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
   17332        80296356 :   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
   17333                 : }
   17334          111575 : SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
   17335          111575 :   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
   17336                 : }
   17337                 : #endif
   17338                 : 
   17339                 : #endif /* !defined(SQLITE_MUTEX_OMIT) */
   17340                 : 
   17341                 : /************** End of mutex.c ***********************************************/
   17342                 : /************** Begin file mutex_noop.c **************************************/
   17343                 : /*
   17344                 : ** 2008 October 07
   17345                 : **
   17346                 : ** The author disclaims copyright to this source code.  In place of
   17347                 : ** a legal notice, here is a blessing:
   17348                 : **
   17349                 : **    May you do good and not evil.
   17350                 : **    May you find forgiveness for yourself and forgive others.
   17351                 : **    May you share freely, never taking more than you give.
   17352                 : **
   17353                 : *************************************************************************
   17354                 : ** This file contains the C functions that implement mutexes.
   17355                 : **
   17356                 : ** This implementation in this file does not provide any mutual
   17357                 : ** exclusion and is thus suitable for use only in applications
   17358                 : ** that use SQLite in a single thread.  The routines defined
   17359                 : ** here are place-holders.  Applications can substitute working
   17360                 : ** mutex routines at start-time using the
   17361                 : **
   17362                 : **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
   17363                 : **
   17364                 : ** interface.
   17365                 : **
   17366                 : ** If compiled with SQLITE_DEBUG, then additional logic is inserted
   17367                 : ** that does error checking on mutexes to make sure they are being
   17368                 : ** called correctly.
   17369                 : */
   17370                 : 
   17371                 : #ifndef SQLITE_MUTEX_OMIT
   17372                 : 
   17373                 : #ifndef SQLITE_DEBUG
   17374                 : /*
   17375                 : ** Stub routines for all mutex methods.
   17376                 : **
   17377                 : ** This routines provide no mutual exclusion or error checking.
   17378                 : */
   17379                 : static int noopMutexInit(void){ return SQLITE_OK; }
   17380                 : static int noopMutexEnd(void){ return SQLITE_OK; }
   17381                 : static sqlite3_mutex *noopMutexAlloc(int id){ 
   17382                 :   UNUSED_PARAMETER(id);
   17383                 :   return (sqlite3_mutex*)8; 
   17384                 : }
   17385                 : static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
   17386                 : static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
   17387                 : static int noopMutexTry(sqlite3_mutex *p){
   17388                 :   UNUSED_PARAMETER(p);
   17389                 :   return SQLITE_OK;
   17390                 : }
   17391                 : static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
   17392                 : 
   17393                 : SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
   17394                 :   static const sqlite3_mutex_methods sMutex = {
   17395                 :     noopMutexInit,
   17396                 :     noopMutexEnd,
   17397                 :     noopMutexAlloc,
   17398                 :     noopMutexFree,
   17399                 :     noopMutexEnter,
   17400                 :     noopMutexTry,
   17401                 :     noopMutexLeave,
   17402                 : 
   17403                 :     0,
   17404                 :     0,
   17405                 :   };
   17406                 : 
   17407                 :   return &sMutex;
   17408                 : }
   17409                 : #endif /* !SQLITE_DEBUG */
   17410                 : 
   17411                 : #ifdef SQLITE_DEBUG
   17412                 : /*
   17413                 : ** In this implementation, error checking is provided for testing
   17414                 : ** and debugging purposes.  The mutexes still do not provide any
   17415                 : ** mutual exclusion.
   17416                 : */
   17417                 : 
   17418                 : /*
   17419                 : ** The mutex object
   17420                 : */
   17421                 : typedef struct sqlite3_debug_mutex {
   17422                 :   int id;     /* The mutex type */
   17423                 :   int cnt;    /* Number of entries without a matching leave */
   17424                 : } sqlite3_debug_mutex;
   17425                 : 
   17426                 : /*
   17427                 : ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   17428                 : ** intended for use inside assert() statements.
   17429                 : */
   17430               0 : static int debugMutexHeld(sqlite3_mutex *pX){
   17431               0 :   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   17432               0 :   return p==0 || p->cnt>0;
   17433                 : }
   17434               0 : static int debugMutexNotheld(sqlite3_mutex *pX){
   17435               0 :   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   17436               0 :   return p==0 || p->cnt==0;
   17437                 : }
   17438                 : 
   17439                 : /*
   17440                 : ** Initialize and deinitialize the mutex subsystem.
   17441                 : */
   17442               0 : static int debugMutexInit(void){ return SQLITE_OK; }
   17443               0 : static int debugMutexEnd(void){ return SQLITE_OK; }
   17444                 : 
   17445                 : /*
   17446                 : ** The sqlite3_mutex_alloc() routine allocates a new
   17447                 : ** mutex and returns a pointer to it.  If it returns NULL
   17448                 : ** that means that a mutex could not be allocated. 
   17449                 : */
   17450               0 : static sqlite3_mutex *debugMutexAlloc(int id){
   17451                 :   static sqlite3_debug_mutex aStatic[6];
   17452               0 :   sqlite3_debug_mutex *pNew = 0;
   17453               0 :   switch( id ){
   17454                 :     case SQLITE_MUTEX_FAST:
   17455                 :     case SQLITE_MUTEX_RECURSIVE: {
   17456               0 :       pNew = sqlite3Malloc(sizeof(*pNew));
   17457               0 :       if( pNew ){
   17458               0 :         pNew->id = id;
   17459               0 :         pNew->cnt = 0;
   17460                 :       }
   17461               0 :       break;
   17462                 :     }
   17463                 :     default: {
   17464               0 :       assert( id-2 >= 0 );
   17465               0 :       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
   17466               0 :       pNew = &aStatic[id-2];
   17467               0 :       pNew->id = id;
   17468               0 :       break;
   17469                 :     }
   17470                 :   }
   17471               0 :   return (sqlite3_mutex*)pNew;
   17472                 : }
   17473                 : 
   17474                 : /*
   17475                 : ** This routine deallocates a previously allocated mutex.
   17476                 : */
   17477               0 : static void debugMutexFree(sqlite3_mutex *pX){
   17478               0 :   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   17479               0 :   assert( p->cnt==0 );
   17480               0 :   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   17481               0 :   sqlite3_free(p);
   17482               0 : }
   17483                 : 
   17484                 : /*
   17485                 : ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   17486                 : ** to enter a mutex.  If another thread is already within the mutex,
   17487                 : ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   17488                 : ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   17489                 : ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   17490                 : ** be entered multiple times by the same thread.  In such cases the,
   17491                 : ** mutex must be exited an equal number of times before another thread
   17492                 : ** can enter.  If the same thread tries to enter any other kind of mutex
   17493                 : ** more than once, the behavior is undefined.
   17494                 : */
   17495               0 : static void debugMutexEnter(sqlite3_mutex *pX){
   17496               0 :   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   17497               0 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
   17498               0 :   p->cnt++;
   17499               0 : }
   17500               0 : static int debugMutexTry(sqlite3_mutex *pX){
   17501               0 :   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   17502               0 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
   17503               0 :   p->cnt++;
   17504               0 :   return SQLITE_OK;
   17505                 : }
   17506                 : 
   17507                 : /*
   17508                 : ** The sqlite3_mutex_leave() routine exits a mutex that was
   17509                 : ** previously entered by the same thread.  The behavior
   17510                 : ** is undefined if the mutex is not currently entered or
   17511                 : ** is not currently allocated.  SQLite will never do either.
   17512                 : */
   17513               0 : static void debugMutexLeave(sqlite3_mutex *pX){
   17514               0 :   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
   17515               0 :   assert( debugMutexHeld(pX) );
   17516               0 :   p->cnt--;
   17517               0 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
   17518               0 : }
   17519                 : 
   17520               0 : SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
   17521                 :   static const sqlite3_mutex_methods sMutex = {
   17522                 :     debugMutexInit,
   17523                 :     debugMutexEnd,
   17524                 :     debugMutexAlloc,
   17525                 :     debugMutexFree,
   17526                 :     debugMutexEnter,
   17527                 :     debugMutexTry,
   17528                 :     debugMutexLeave,
   17529                 : 
   17530                 :     debugMutexHeld,
   17531                 :     debugMutexNotheld
   17532                 :   };
   17533                 : 
   17534               0 :   return &sMutex;
   17535                 : }
   17536                 : #endif /* SQLITE_DEBUG */
   17537                 : 
   17538                 : /*
   17539                 : ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
   17540                 : ** is used regardless of the run-time threadsafety setting.
   17541                 : */
   17542                 : #ifdef SQLITE_MUTEX_NOOP
   17543                 : SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   17544                 :   return sqlite3NoopMutex();
   17545                 : }
   17546                 : #endif /* defined(SQLITE_MUTEX_NOOP) */
   17547                 : #endif /* !defined(SQLITE_MUTEX_OMIT) */
   17548                 : 
   17549                 : /************** End of mutex_noop.c ******************************************/
   17550                 : /************** Begin file mutex_os2.c ***************************************/
   17551                 : /*
   17552                 : ** 2007 August 28
   17553                 : **
   17554                 : ** The author disclaims copyright to this source code.  In place of
   17555                 : ** a legal notice, here is a blessing:
   17556                 : **
   17557                 : **    May you do good and not evil.
   17558                 : **    May you find forgiveness for yourself and forgive others.
   17559                 : **    May you share freely, never taking more than you give.
   17560                 : **
   17561                 : *************************************************************************
   17562                 : ** This file contains the C functions that implement mutexes for OS/2
   17563                 : */
   17564                 : 
   17565                 : /*
   17566                 : ** The code in this file is only used if SQLITE_MUTEX_OS2 is defined.
   17567                 : ** See the mutex.h file for details.
   17568                 : */
   17569                 : #ifdef SQLITE_MUTEX_OS2
   17570                 : 
   17571                 : /********************** OS/2 Mutex Implementation **********************
   17572                 : **
   17573                 : ** This implementation of mutexes is built using the OS/2 API.
   17574                 : */
   17575                 : 
   17576                 : /*
   17577                 : ** The mutex object
   17578                 : ** Each recursive mutex is an instance of the following structure.
   17579                 : */
   17580                 : struct sqlite3_mutex {
   17581                 :   HMTX mutex;       /* Mutex controlling the lock */
   17582                 :   int  id;          /* Mutex type */
   17583                 : #ifdef SQLITE_DEBUG
   17584                 :  int   trace;       /* True to trace changes */
   17585                 : #endif
   17586                 : };
   17587                 : 
   17588                 : #ifdef SQLITE_DEBUG
   17589                 : #define SQLITE3_MUTEX_INITIALIZER { 0, 0, 0 }
   17590                 : #else
   17591                 : #define SQLITE3_MUTEX_INITIALIZER { 0, 0 }
   17592                 : #endif
   17593                 : 
   17594                 : /*
   17595                 : ** Initialize and deinitialize the mutex subsystem.
   17596                 : */
   17597                 : static int os2MutexInit(void){ return SQLITE_OK; }
   17598                 : static int os2MutexEnd(void){ return SQLITE_OK; }
   17599                 : 
   17600                 : /*
   17601                 : ** The sqlite3_mutex_alloc() routine allocates a new
   17602                 : ** mutex and returns a pointer to it.  If it returns NULL
   17603                 : ** that means that a mutex could not be allocated. 
   17604                 : ** SQLite will unwind its stack and return an error.  The argument
   17605                 : ** to sqlite3_mutex_alloc() is one of these integer constants:
   17606                 : **
   17607                 : ** <ul>
   17608                 : ** <li>  SQLITE_MUTEX_FAST
   17609                 : ** <li>  SQLITE_MUTEX_RECURSIVE
   17610                 : ** <li>  SQLITE_MUTEX_STATIC_MASTER
   17611                 : ** <li>  SQLITE_MUTEX_STATIC_MEM
   17612                 : ** <li>  SQLITE_MUTEX_STATIC_MEM2
   17613                 : ** <li>  SQLITE_MUTEX_STATIC_PRNG
   17614                 : ** <li>  SQLITE_MUTEX_STATIC_LRU
   17615                 : ** <li>  SQLITE_MUTEX_STATIC_LRU2
   17616                 : ** </ul>
   17617                 : **
   17618                 : ** The first two constants cause sqlite3_mutex_alloc() to create
   17619                 : ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   17620                 : ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   17621                 : ** The mutex implementation does not need to make a distinction
   17622                 : ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
   17623                 : ** not want to.  But SQLite will only request a recursive mutex in
   17624                 : ** cases where it really needs one.  If a faster non-recursive mutex
   17625                 : ** implementation is available on the host platform, the mutex subsystem
   17626                 : ** might return such a mutex in response to SQLITE_MUTEX_FAST.
   17627                 : **
   17628                 : ** The other allowed parameters to sqlite3_mutex_alloc() each return
   17629                 : ** a pointer to a static preexisting mutex.  Six static mutexes are
   17630                 : ** used by the current version of SQLite.  Future versions of SQLite
   17631                 : ** may add additional static mutexes.  Static mutexes are for internal
   17632                 : ** use by SQLite only.  Applications that use SQLite mutexes should
   17633                 : ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   17634                 : ** SQLITE_MUTEX_RECURSIVE.
   17635                 : **
   17636                 : ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   17637                 : ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   17638                 : ** returns a different mutex on every call.  But for the static
   17639                 : ** mutex types, the same mutex is returned on every call that has
   17640                 : ** the same type number.
   17641                 : */
   17642                 : static sqlite3_mutex *os2MutexAlloc(int iType){
   17643                 :   sqlite3_mutex *p = NULL;
   17644                 :   switch( iType ){
   17645                 :     case SQLITE_MUTEX_FAST:
   17646                 :     case SQLITE_MUTEX_RECURSIVE: {
   17647                 :       p = sqlite3MallocZero( sizeof(*p) );
   17648                 :       if( p ){
   17649                 :         p->id = iType;
   17650                 :         if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){
   17651                 :           sqlite3_free( p );
   17652                 :           p = NULL;
   17653                 :         }
   17654                 :       }
   17655                 :       break;
   17656                 :     }
   17657                 :     default: {
   17658                 :       static volatile int isInit = 0;
   17659                 :       static sqlite3_mutex staticMutexes[6] = {
   17660                 :         SQLITE3_MUTEX_INITIALIZER,
   17661                 :         SQLITE3_MUTEX_INITIALIZER,
   17662                 :         SQLITE3_MUTEX_INITIALIZER,
   17663                 :         SQLITE3_MUTEX_INITIALIZER,
   17664                 :         SQLITE3_MUTEX_INITIALIZER,
   17665                 :         SQLITE3_MUTEX_INITIALIZER,
   17666                 :       };
   17667                 :       if ( !isInit ){
   17668                 :         APIRET rc;
   17669                 :         PTIB ptib;
   17670                 :         PPIB ppib;
   17671                 :         HMTX mutex;
   17672                 :         char name[32];
   17673                 :         DosGetInfoBlocks( &ptib, &ppib );
   17674                 :         sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x",
   17675                 :                           ppib->pib_ulpid );
   17676                 :         while( !isInit ){
   17677                 :           mutex = 0;
   17678                 :           rc = DosCreateMutexSem( name, &mutex, 0, FALSE);
   17679                 :           if( rc == NO_ERROR ){
   17680                 :             unsigned int i;
   17681                 :             if( !isInit ){
   17682                 :               for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){
   17683                 :                 DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE );
   17684                 :               }
   17685                 :               isInit = 1;
   17686                 :             }
   17687                 :             DosCloseMutexSem( mutex );
   17688                 :           }else if( rc == ERROR_DUPLICATE_NAME ){
   17689                 :             DosSleep( 1 );
   17690                 :           }else{
   17691                 :             return p;
   17692                 :           }
   17693                 :         }
   17694                 :       }
   17695                 :       assert( iType-2 >= 0 );
   17696                 :       assert( iType-2 < sizeof(staticMutexes)/sizeof(staticMutexes[0]) );
   17697                 :       p = &staticMutexes[iType-2];
   17698                 :       p->id = iType;
   17699                 :       break;
   17700                 :     }
   17701                 :   }
   17702                 :   return p;
   17703                 : }
   17704                 : 
   17705                 : 
   17706                 : /*
   17707                 : ** This routine deallocates a previously allocated mutex.
   17708                 : ** SQLite is careful to deallocate every mutex that it allocates.
   17709                 : */
   17710                 : static void os2MutexFree(sqlite3_mutex *p){
   17711                 : #ifdef SQLITE_DEBUG
   17712                 :   TID tid;
   17713                 :   PID pid;
   17714                 :   ULONG ulCount;
   17715                 :   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   17716                 :   assert( ulCount==0 );
   17717                 :   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   17718                 : #endif
   17719                 :   DosCloseMutexSem( p->mutex );
   17720                 :   sqlite3_free( p );
   17721                 : }
   17722                 : 
   17723                 : #ifdef SQLITE_DEBUG
   17724                 : /*
   17725                 : ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   17726                 : ** intended for use inside assert() statements.
   17727                 : */
   17728                 : static int os2MutexHeld(sqlite3_mutex *p){
   17729                 :   TID tid;
   17730                 :   PID pid;
   17731                 :   ULONG ulCount;
   17732                 :   PTIB ptib;
   17733                 :   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   17734                 :   if( ulCount==0 || ( ulCount>1 && p->id!=SQLITE_MUTEX_RECURSIVE ) )
   17735                 :     return 0;
   17736                 :   DosGetInfoBlocks(&ptib, NULL);
   17737                 :   return tid==ptib->tib_ptib2->tib2_ultid;
   17738                 : }
   17739                 : static int os2MutexNotheld(sqlite3_mutex *p){
   17740                 :   TID tid;
   17741                 :   PID pid;
   17742                 :   ULONG ulCount;
   17743                 :   PTIB ptib;
   17744                 :   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   17745                 :   if( ulCount==0 )
   17746                 :     return 1;
   17747                 :   DosGetInfoBlocks(&ptib, NULL);
   17748                 :   return tid!=ptib->tib_ptib2->tib2_ultid;
   17749                 : }
   17750                 : static void os2MutexTrace(sqlite3_mutex *p, char *pAction){
   17751                 :   TID   tid;
   17752                 :   PID   pid;
   17753                 :   ULONG ulCount;
   17754                 :   DosQueryMutexSem(p->mutex, &pid, &tid, &ulCount);
   17755                 :   printf("%s mutex %p (%d) with nRef=%ld\n", pAction, (void*)p, p->trace, ulCount);
   17756                 : }
   17757                 : #endif
   17758                 : 
   17759                 : /*
   17760                 : ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   17761                 : ** to enter a mutex.  If another thread is already within the mutex,
   17762                 : ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   17763                 : ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   17764                 : ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   17765                 : ** be entered multiple times by the same thread.  In such cases the,
   17766                 : ** mutex must be exited an equal number of times before another thread
   17767                 : ** can enter.  If the same thread tries to enter any other kind of mutex
   17768                 : ** more than once, the behavior is undefined.
   17769                 : */
   17770                 : static void os2MutexEnter(sqlite3_mutex *p){
   17771                 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
   17772                 :   DosRequestMutexSem(p->mutex, SEM_INDEFINITE_WAIT);
   17773                 : #ifdef SQLITE_DEBUG
   17774                 :   if( p->trace ) os2MutexTrace(p, "enter");
   17775                 : #endif
   17776                 : }
   17777                 : static int os2MutexTry(sqlite3_mutex *p){
   17778                 :   int rc = SQLITE_BUSY;
   17779                 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || os2MutexNotheld(p) );
   17780                 :   if( DosRequestMutexSem(p->mutex, SEM_IMMEDIATE_RETURN) == NO_ERROR ) {
   17781                 :     rc = SQLITE_OK;
   17782                 : #ifdef SQLITE_DEBUG
   17783                 :     if( p->trace ) os2MutexTrace(p, "try");
   17784                 : #endif
   17785                 :   }
   17786                 :   return rc;
   17787                 : }
   17788                 : 
   17789                 : /*
   17790                 : ** The sqlite3_mutex_leave() routine exits a mutex that was
   17791                 : ** previously entered by the same thread.  The behavior
   17792                 : ** is undefined if the mutex is not currently entered or
   17793                 : ** is not currently allocated.  SQLite will never do either.
   17794                 : */
   17795                 : static void os2MutexLeave(sqlite3_mutex *p){
   17796                 :   assert( os2MutexHeld(p) );
   17797                 :   DosReleaseMutexSem(p->mutex);
   17798                 : #ifdef SQLITE_DEBUG
   17799                 :   if( p->trace ) os2MutexTrace(p, "leave");
   17800                 : #endif
   17801                 : }
   17802                 : 
   17803                 : SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   17804                 :   static const sqlite3_mutex_methods sMutex = {
   17805                 :     os2MutexInit,
   17806                 :     os2MutexEnd,
   17807                 :     os2MutexAlloc,
   17808                 :     os2MutexFree,
   17809                 :     os2MutexEnter,
   17810                 :     os2MutexTry,
   17811                 :     os2MutexLeave,
   17812                 : #ifdef SQLITE_DEBUG
   17813                 :     os2MutexHeld,
   17814                 :     os2MutexNotheld
   17815                 : #else
   17816                 :     0,
   17817                 :     0
   17818                 : #endif
   17819                 :   };
   17820                 : 
   17821                 :   return &sMutex;
   17822                 : }
   17823                 : #endif /* SQLITE_MUTEX_OS2 */
   17824                 : 
   17825                 : /************** End of mutex_os2.c *******************************************/
   17826                 : /************** Begin file mutex_unix.c **************************************/
   17827                 : /*
   17828                 : ** 2007 August 28
   17829                 : **
   17830                 : ** The author disclaims copyright to this source code.  In place of
   17831                 : ** a legal notice, here is a blessing:
   17832                 : **
   17833                 : **    May you do good and not evil.
   17834                 : **    May you find forgiveness for yourself and forgive others.
   17835                 : **    May you share freely, never taking more than you give.
   17836                 : **
   17837                 : *************************************************************************
   17838                 : ** This file contains the C functions that implement mutexes for pthreads
   17839                 : */
   17840                 : 
   17841                 : /*
   17842                 : ** The code in this file is only used if we are compiling threadsafe
   17843                 : ** under unix with pthreads.
   17844                 : **
   17845                 : ** Note that this implementation requires a version of pthreads that
   17846                 : ** supports recursive mutexes.
   17847                 : */
   17848                 : #ifdef SQLITE_MUTEX_PTHREADS
   17849                 : 
   17850                 : #include <pthread.h>
   17851                 : 
   17852                 : /*
   17853                 : ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
   17854                 : ** are necessary under two condidtions:  (1) Debug builds and (2) using
   17855                 : ** home-grown mutexes.  Encapsulate these conditions into a single #define.
   17856                 : */
   17857                 : #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
   17858                 : # define SQLITE_MUTEX_NREF 1
   17859                 : #else
   17860                 : # define SQLITE_MUTEX_NREF 0
   17861                 : #endif
   17862                 : 
   17863                 : /*
   17864                 : ** Each recursive mutex is an instance of the following structure.
   17865                 : */
   17866                 : struct sqlite3_mutex {
   17867                 :   pthread_mutex_t mutex;     /* Mutex controlling the lock */
   17868                 : #if SQLITE_MUTEX_NREF
   17869                 :   int id;                    /* Mutex type */
   17870                 :   volatile int nRef;         /* Number of entrances */
   17871                 :   volatile pthread_t owner;  /* Thread that is within this mutex */
   17872                 :   int trace;                 /* True to trace changes */
   17873                 : #endif
   17874                 : };
   17875                 : #if SQLITE_MUTEX_NREF
   17876                 : #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
   17877                 : #else
   17878                 : #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
   17879                 : #endif
   17880                 : 
   17881                 : /*
   17882                 : ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   17883                 : ** intended for use only inside assert() statements.  On some platforms,
   17884                 : ** there might be race conditions that can cause these routines to
   17885                 : ** deliver incorrect results.  In particular, if pthread_equal() is
   17886                 : ** not an atomic operation, then these routines might delivery
   17887                 : ** incorrect results.  On most platforms, pthread_equal() is a 
   17888                 : ** comparison of two integers and is therefore atomic.  But we are
   17889                 : ** told that HPUX is not such a platform.  If so, then these routines
   17890                 : ** will not always work correctly on HPUX.
   17891                 : **
   17892                 : ** On those platforms where pthread_equal() is not atomic, SQLite
   17893                 : ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
   17894                 : ** make sure no assert() statements are evaluated and hence these
   17895                 : ** routines are never called.
   17896                 : */
   17897                 : #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
   17898        97249167 : static int pthreadMutexHeld(sqlite3_mutex *p){
   17899        97249167 :   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
   17900                 : }
   17901        14726044 : static int pthreadMutexNotheld(sqlite3_mutex *p){
   17902        14726044 :   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
   17903                 : }
   17904                 : #endif
   17905                 : 
   17906                 : /*
   17907                 : ** Initialize and deinitialize the mutex subsystem.
   17908                 : */
   17909            6448 : static int pthreadMutexInit(void){ return SQLITE_OK; }
   17910             795 : static int pthreadMutexEnd(void){ return SQLITE_OK; }
   17911                 : 
   17912                 : /*
   17913                 : ** The sqlite3_mutex_alloc() routine allocates a new
   17914                 : ** mutex and returns a pointer to it.  If it returns NULL
   17915                 : ** that means that a mutex could not be allocated.  SQLite
   17916                 : ** will unwind its stack and return an error.  The argument
   17917                 : ** to sqlite3_mutex_alloc() is one of these integer constants:
   17918                 : **
   17919                 : ** <ul>
   17920                 : ** <li>  SQLITE_MUTEX_FAST
   17921                 : ** <li>  SQLITE_MUTEX_RECURSIVE
   17922                 : ** <li>  SQLITE_MUTEX_STATIC_MASTER
   17923                 : ** <li>  SQLITE_MUTEX_STATIC_MEM
   17924                 : ** <li>  SQLITE_MUTEX_STATIC_MEM2
   17925                 : ** <li>  SQLITE_MUTEX_STATIC_PRNG
   17926                 : ** <li>  SQLITE_MUTEX_STATIC_LRU
   17927                 : ** <li>  SQLITE_MUTEX_STATIC_PMEM
   17928                 : ** </ul>
   17929                 : **
   17930                 : ** The first two constants cause sqlite3_mutex_alloc() to create
   17931                 : ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   17932                 : ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   17933                 : ** The mutex implementation does not need to make a distinction
   17934                 : ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
   17935                 : ** not want to.  But SQLite will only request a recursive mutex in
   17936                 : ** cases where it really needs one.  If a faster non-recursive mutex
   17937                 : ** implementation is available on the host platform, the mutex subsystem
   17938                 : ** might return such a mutex in response to SQLITE_MUTEX_FAST.
   17939                 : **
   17940                 : ** The other allowed parameters to sqlite3_mutex_alloc() each return
   17941                 : ** a pointer to a static preexisting mutex.  Six static mutexes are
   17942                 : ** used by the current version of SQLite.  Future versions of SQLite
   17943                 : ** may add additional static mutexes.  Static mutexes are for internal
   17944                 : ** use by SQLite only.  Applications that use SQLite mutexes should
   17945                 : ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   17946                 : ** SQLITE_MUTEX_RECURSIVE.
   17947                 : **
   17948                 : ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   17949                 : ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   17950                 : ** returns a different mutex on every call.  But for the static 
   17951                 : ** mutex types, the same mutex is returned on every call that has
   17952                 : ** the same type number.
   17953                 : */
   17954         1197291 : static sqlite3_mutex *pthreadMutexAlloc(int iType){
   17955                 :   static sqlite3_mutex staticMutexes[] = {
   17956                 :     SQLITE3_MUTEX_INITIALIZER,
   17957                 :     SQLITE3_MUTEX_INITIALIZER,
   17958                 :     SQLITE3_MUTEX_INITIALIZER,
   17959                 :     SQLITE3_MUTEX_INITIALIZER,
   17960                 :     SQLITE3_MUTEX_INITIALIZER,
   17961                 :     SQLITE3_MUTEX_INITIALIZER
   17962                 :   };
   17963                 :   sqlite3_mutex *p;
   17964         1197291 :   switch( iType ){
   17965                 :     case SQLITE_MUTEX_RECURSIVE: {
   17966            4089 :       p = sqlite3MallocZero( sizeof(*p) );
   17967            4089 :       if( p ){
   17968                 : #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   17969                 :         /* If recursive mutexes are not available, we will have to
   17970                 :         ** build our own.  See below. */
   17971                 :         pthread_mutex_init(&p->mutex, 0);
   17972                 : #else
   17973                 :         /* Use a recursive mutex if it is available */
   17974                 :         pthread_mutexattr_t recursiveAttr;
   17975            4089 :         pthread_mutexattr_init(&recursiveAttr);
   17976            4089 :         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
   17977            4089 :         pthread_mutex_init(&p->mutex, &recursiveAttr);
   17978            4089 :         pthread_mutexattr_destroy(&recursiveAttr);
   17979                 : #endif
   17980                 : #if SQLITE_MUTEX_NREF
   17981            4089 :         p->id = iType;
   17982                 : #endif
   17983                 :       }
   17984            4089 :       break;
   17985                 :     }
   17986                 :     case SQLITE_MUTEX_FAST: {
   17987            4214 :       p = sqlite3MallocZero( sizeof(*p) );
   17988            4214 :       if( p ){
   17989                 : #if SQLITE_MUTEX_NREF
   17990            4214 :         p->id = iType;
   17991                 : #endif
   17992            4214 :         pthread_mutex_init(&p->mutex, 0);
   17993                 :       }
   17994            4214 :       break;
   17995                 :     }
   17996                 :     default: {
   17997         1188988 :       assert( iType-2 >= 0 );
   17998         1188988 :       assert( iType-2 < ArraySize(staticMutexes) );
   17999         1188988 :       p = &staticMutexes[iType-2];
   18000                 : #if SQLITE_MUTEX_NREF
   18001         1188988 :       p->id = iType;
   18002                 : #endif
   18003         1188988 :       break;
   18004                 :     }
   18005                 :   }
   18006         1197291 :   return p;
   18007                 : }
   18008                 : 
   18009                 : 
   18010                 : /*
   18011                 : ** This routine deallocates a previously
   18012                 : ** allocated mutex.  SQLite is careful to deallocate every
   18013                 : ** mutex that it allocates.
   18014                 : */
   18015            8290 : static void pthreadMutexFree(sqlite3_mutex *p){
   18016            8290 :   assert( p->nRef==0 );
   18017            8290 :   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   18018            8290 :   pthread_mutex_destroy(&p->mutex);
   18019            8290 :   sqlite3_free(p);
   18020            8290 : }
   18021                 : 
   18022                 : /*
   18023                 : ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   18024                 : ** to enter a mutex.  If another thread is already within the mutex,
   18025                 : ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   18026                 : ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   18027                 : ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   18028                 : ** be entered multiple times by the same thread.  In such cases the,
   18029                 : ** mutex must be exited an equal number of times before another thread
   18030                 : ** can enter.  If the same thread tries to enter any other kind of mutex
   18031                 : ** more than once, the behavior is undefined.
   18032                 : */
   18033        18453201 : static void pthreadMutexEnter(sqlite3_mutex *p){
   18034        18453201 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
   18035                 : 
   18036                 : #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   18037                 :   /* If recursive mutexes are not available, then we have to grow
   18038                 :   ** our own.  This implementation assumes that pthread_equal()
   18039                 :   ** is atomic - that it cannot be deceived into thinking self
   18040                 :   ** and p->owner are equal if p->owner changes between two values
   18041                 :   ** that are not equal to self while the comparison is taking place.
   18042                 :   ** This implementation also assumes a coherent cache - that 
   18043                 :   ** separate processes cannot read different values from the same
   18044                 :   ** address at the same time.  If either of these two conditions
   18045                 :   ** are not met, then the mutexes will fail and problems will result.
   18046                 :   */
   18047                 :   {
   18048                 :     pthread_t self = pthread_self();
   18049                 :     if( p->nRef>0 && pthread_equal(p->owner, self) ){
   18050                 :       p->nRef++;
   18051                 :     }else{
   18052                 :       pthread_mutex_lock(&p->mutex);
   18053                 :       assert( p->nRef==0 );
   18054                 :       p->owner = self;
   18055                 :       p->nRef = 1;
   18056                 :     }
   18057                 :   }
   18058                 : #else
   18059                 :   /* Use the built-in recursive mutexes if they are available.
   18060                 :   */
   18061        18453177 :   pthread_mutex_lock(&p->mutex);
   18062                 : #if SQLITE_MUTEX_NREF
   18063        18453147 :   assert( p->nRef>0 || p->owner==0 );
   18064        18453147 :   p->owner = pthread_self();
   18065        18453068 :   p->nRef++;
   18066                 : #endif
   18067                 : #endif
   18068                 : 
   18069                 : #ifdef SQLITE_DEBUG
   18070        18453068 :   if( p->trace ){
   18071               0 :     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   18072                 :   }
   18073                 : #endif
   18074        18453068 : }
   18075          396478 : static int pthreadMutexTry(sqlite3_mutex *p){
   18076                 :   int rc;
   18077          396478 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
   18078                 : 
   18079                 : #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   18080                 :   /* If recursive mutexes are not available, then we have to grow
   18081                 :   ** our own.  This implementation assumes that pthread_equal()
   18082                 :   ** is atomic - that it cannot be deceived into thinking self
   18083                 :   ** and p->owner are equal if p->owner changes between two values
   18084                 :   ** that are not equal to self while the comparison is taking place.
   18085                 :   ** This implementation also assumes a coherent cache - that 
   18086                 :   ** separate processes cannot read different values from the same
   18087                 :   ** address at the same time.  If either of these two conditions
   18088                 :   ** are not met, then the mutexes will fail and problems will result.
   18089                 :   */
   18090                 :   {
   18091                 :     pthread_t self = pthread_self();
   18092                 :     if( p->nRef>0 && pthread_equal(p->owner, self) ){
   18093                 :       p->nRef++;
   18094                 :       rc = SQLITE_OK;
   18095                 :     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
   18096                 :       assert( p->nRef==0 );
   18097                 :       p->owner = self;
   18098                 :       p->nRef = 1;
   18099                 :       rc = SQLITE_OK;
   18100                 :     }else{
   18101                 :       rc = SQLITE_BUSY;
   18102                 :     }
   18103                 :   }
   18104                 : #else
   18105                 :   /* Use the built-in recursive mutexes if they are available.
   18106                 :   */
   18107          396479 :   if( pthread_mutex_trylock(&p->mutex)==0 ){
   18108                 : #if SQLITE_MUTEX_NREF
   18109          395259 :     p->owner = pthread_self();
   18110          395259 :     p->nRef++;
   18111                 : #endif
   18112          395259 :     rc = SQLITE_OK;
   18113                 :   }else{
   18114            1220 :     rc = SQLITE_BUSY;
   18115                 :   }
   18116                 : #endif
   18117                 : 
   18118                 : #ifdef SQLITE_DEBUG
   18119          396479 :   if( rc==SQLITE_OK && p->trace ){
   18120               0 :     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   18121                 :   }
   18122                 : #endif
   18123          396479 :   return rc;
   18124                 : }
   18125                 : 
   18126                 : /*
   18127                 : ** The sqlite3_mutex_leave() routine exits a mutex that was
   18128                 : ** previously entered by the same thread.  The behavior
   18129                 : ** is undefined if the mutex is not currently entered or
   18130                 : ** is not currently allocated.  SQLite will never do either.
   18131                 : */
   18132        18848180 : static void pthreadMutexLeave(sqlite3_mutex *p){
   18133        18848180 :   assert( pthreadMutexHeld(p) );
   18134                 : #if SQLITE_MUTEX_NREF
   18135        18848063 :   p->nRef--;
   18136        18848063 :   if( p->nRef==0 ) p->owner = 0;
   18137                 : #endif
   18138        18848063 :   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   18139                 : 
   18140                 : #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
   18141                 :   if( p->nRef==0 ){
   18142                 :     pthread_mutex_unlock(&p->mutex);
   18143                 :   }
   18144                 : #else
   18145        18848063 :   pthread_mutex_unlock(&p->mutex);
   18146                 : #endif
   18147                 : 
   18148                 : #ifdef SQLITE_DEBUG
   18149        18848083 :   if( p->trace ){
   18150               0 :     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   18151                 :   }
   18152                 : #endif
   18153        18848083 : }
   18154                 : 
   18155             805 : SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   18156                 :   static const sqlite3_mutex_methods sMutex = {
   18157                 :     pthreadMutexInit,
   18158                 :     pthreadMutexEnd,
   18159                 :     pthreadMutexAlloc,
   18160                 :     pthreadMutexFree,
   18161                 :     pthreadMutexEnter,
   18162                 :     pthreadMutexTry,
   18163                 :     pthreadMutexLeave,
   18164                 : #ifdef SQLITE_DEBUG
   18165                 :     pthreadMutexHeld,
   18166                 :     pthreadMutexNotheld
   18167                 : #else
   18168                 :     0,
   18169                 :     0
   18170                 : #endif
   18171                 :   };
   18172                 : 
   18173             805 :   return &sMutex;
   18174                 : }
   18175                 : 
   18176                 : #endif /* SQLITE_MUTEX_PTHREADS */
   18177                 : 
   18178                 : /************** End of mutex_unix.c ******************************************/
   18179                 : /************** Begin file mutex_w32.c ***************************************/
   18180                 : /*
   18181                 : ** 2007 August 14
   18182                 : **
   18183                 : ** The author disclaims copyright to this source code.  In place of
   18184                 : ** a legal notice, here is a blessing:
   18185                 : **
   18186                 : **    May you do good and not evil.
   18187                 : **    May you find forgiveness for yourself and forgive others.
   18188                 : **    May you share freely, never taking more than you give.
   18189                 : **
   18190                 : *************************************************************************
   18191                 : ** This file contains the C functions that implement mutexes for win32
   18192                 : */
   18193                 : 
   18194                 : /*
   18195                 : ** The code in this file is only used if we are compiling multithreaded
   18196                 : ** on a win32 system.
   18197                 : */
   18198                 : #ifdef SQLITE_MUTEX_W32
   18199                 : 
   18200                 : /*
   18201                 : ** Each recursive mutex is an instance of the following structure.
   18202                 : */
   18203                 : struct sqlite3_mutex {
   18204                 :   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
   18205                 :   int id;                    /* Mutex type */
   18206                 : #ifdef SQLITE_DEBUG
   18207                 :   volatile int nRef;         /* Number of enterances */
   18208                 :   volatile DWORD owner;      /* Thread holding this mutex */
   18209                 :   int trace;                 /* True to trace changes */
   18210                 : #endif
   18211                 : };
   18212                 : #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
   18213                 : #ifdef SQLITE_DEBUG
   18214                 : #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
   18215                 : #else
   18216                 : #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
   18217                 : #endif
   18218                 : 
   18219                 : /*
   18220                 : ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
   18221                 : ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
   18222                 : **
   18223                 : ** Here is an interesting observation:  Win95, Win98, and WinME lack
   18224                 : ** the LockFileEx() API.  But we can still statically link against that
   18225                 : ** API as long as we don't call it win running Win95/98/ME.  A call to
   18226                 : ** this routine is used to determine if the host is Win95/98/ME or
   18227                 : ** WinNT/2K/XP so that we will know whether or not we can safely call
   18228                 : ** the LockFileEx() API.
   18229                 : **
   18230                 : ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
   18231                 : ** which is only available if your application was compiled with 
   18232                 : ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
   18233                 : ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef 
   18234                 : ** this out as well.
   18235                 : */
   18236                 : #if 0
   18237                 : #if SQLITE_OS_WINCE
   18238                 : # define mutexIsNT()  (1)
   18239                 : #else
   18240                 :   static int mutexIsNT(void){
   18241                 :     static int osType = 0;
   18242                 :     if( osType==0 ){
   18243                 :       OSVERSIONINFO sInfo;
   18244                 :       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
   18245                 :       GetVersionEx(&sInfo);
   18246                 :       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
   18247                 :     }
   18248                 :     return osType==2;
   18249                 :   }
   18250                 : #endif /* SQLITE_OS_WINCE */
   18251                 : #endif
   18252                 : 
   18253                 : #ifdef SQLITE_DEBUG
   18254                 : /*
   18255                 : ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
   18256                 : ** intended for use only inside assert() statements.
   18257                 : */
   18258                 : static int winMutexHeld(sqlite3_mutex *p){
   18259                 :   return p->nRef!=0 && p->owner==GetCurrentThreadId();
   18260                 : }
   18261                 : static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
   18262                 :   return p->nRef==0 || p->owner!=tid;
   18263                 : }
   18264                 : static int winMutexNotheld(sqlite3_mutex *p){
   18265                 :   DWORD tid = GetCurrentThreadId(); 
   18266                 :   return winMutexNotheld2(p, tid);
   18267                 : }
   18268                 : #endif
   18269                 : 
   18270                 : 
   18271                 : /*
   18272                 : ** Initialize and deinitialize the mutex subsystem.
   18273                 : */
   18274                 : static sqlite3_mutex winMutex_staticMutexes[6] = {
   18275                 :   SQLITE3_MUTEX_INITIALIZER,
   18276                 :   SQLITE3_MUTEX_INITIALIZER,
   18277                 :   SQLITE3_MUTEX_INITIALIZER,
   18278                 :   SQLITE3_MUTEX_INITIALIZER,
   18279                 :   SQLITE3_MUTEX_INITIALIZER,
   18280                 :   SQLITE3_MUTEX_INITIALIZER
   18281                 : };
   18282                 : static int winMutex_isInit = 0;
   18283                 : /* As winMutexInit() and winMutexEnd() are called as part
   18284                 : ** of the sqlite3_initialize and sqlite3_shutdown()
   18285                 : ** processing, the "interlocked" magic is probably not
   18286                 : ** strictly necessary.
   18287                 : */
   18288                 : static long winMutex_lock = 0;
   18289                 : 
   18290                 : static int winMutexInit(void){ 
   18291                 :   /* The first to increment to 1 does actual initialization */
   18292                 :   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
   18293                 :     int i;
   18294                 :     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
   18295                 :       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
   18296                 :     }
   18297                 :     winMutex_isInit = 1;
   18298                 :   }else{
   18299                 :     /* Someone else is in the process of initing the static mutexes */
   18300                 :     while( !winMutex_isInit ){
   18301                 :       Sleep(1);
   18302                 :     }
   18303                 :   }
   18304                 :   return SQLITE_OK; 
   18305                 : }
   18306                 : 
   18307                 : static int winMutexEnd(void){ 
   18308                 :   /* The first to decrement to 0 does actual shutdown 
   18309                 :   ** (which should be the last to shutdown.) */
   18310                 :   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
   18311                 :     if( winMutex_isInit==1 ){
   18312                 :       int i;
   18313                 :       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
   18314                 :         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
   18315                 :       }
   18316                 :       winMutex_isInit = 0;
   18317                 :     }
   18318                 :   }
   18319                 :   return SQLITE_OK; 
   18320                 : }
   18321                 : 
   18322                 : /*
   18323                 : ** The sqlite3_mutex_alloc() routine allocates a new
   18324                 : ** mutex and returns a pointer to it.  If it returns NULL
   18325                 : ** that means that a mutex could not be allocated.  SQLite
   18326                 : ** will unwind its stack and return an error.  The argument
   18327                 : ** to sqlite3_mutex_alloc() is one of these integer constants:
   18328                 : **
   18329                 : ** <ul>
   18330                 : ** <li>  SQLITE_MUTEX_FAST
   18331                 : ** <li>  SQLITE_MUTEX_RECURSIVE
   18332                 : ** <li>  SQLITE_MUTEX_STATIC_MASTER
   18333                 : ** <li>  SQLITE_MUTEX_STATIC_MEM
   18334                 : ** <li>  SQLITE_MUTEX_STATIC_MEM2
   18335                 : ** <li>  SQLITE_MUTEX_STATIC_PRNG
   18336                 : ** <li>  SQLITE_MUTEX_STATIC_LRU
   18337                 : ** <li>  SQLITE_MUTEX_STATIC_PMEM
   18338                 : ** </ul>
   18339                 : **
   18340                 : ** The first two constants cause sqlite3_mutex_alloc() to create
   18341                 : ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   18342                 : ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   18343                 : ** The mutex implementation does not need to make a distinction
   18344                 : ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
   18345                 : ** not want to.  But SQLite will only request a recursive mutex in
   18346                 : ** cases where it really needs one.  If a faster non-recursive mutex
   18347                 : ** implementation is available on the host platform, the mutex subsystem
   18348                 : ** might return such a mutex in response to SQLITE_MUTEX_FAST.
   18349                 : **
   18350                 : ** The other allowed parameters to sqlite3_mutex_alloc() each return
   18351                 : ** a pointer to a static preexisting mutex.  Six static mutexes are
   18352                 : ** used by the current version of SQLite.  Future versions of SQLite
   18353                 : ** may add additional static mutexes.  Static mutexes are for internal
   18354                 : ** use by SQLite only.  Applications that use SQLite mutexes should
   18355                 : ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
   18356                 : ** SQLITE_MUTEX_RECURSIVE.
   18357                 : **
   18358                 : ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
   18359                 : ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   18360                 : ** returns a different mutex on every call.  But for the static 
   18361                 : ** mutex types, the same mutex is returned on every call that has
   18362                 : ** the same type number.
   18363                 : */
   18364                 : static sqlite3_mutex *winMutexAlloc(int iType){
   18365                 :   sqlite3_mutex *p;
   18366                 : 
   18367                 :   switch( iType ){
   18368                 :     case SQLITE_MUTEX_FAST:
   18369                 :     case SQLITE_MUTEX_RECURSIVE: {
   18370                 :       p = sqlite3MallocZero( sizeof(*p) );
   18371                 :       if( p ){  
   18372                 : #ifdef SQLITE_DEBUG
   18373                 :         p->id = iType;
   18374                 : #endif
   18375                 :         InitializeCriticalSection(&p->mutex);
   18376                 :       }
   18377                 :       break;
   18378                 :     }
   18379                 :     default: {
   18380                 :       assert( winMutex_isInit==1 );
   18381                 :       assert( iType-2 >= 0 );
   18382                 :       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
   18383                 :       p = &winMutex_staticMutexes[iType-2];
   18384                 : #ifdef SQLITE_DEBUG
   18385                 :       p->id = iType;
   18386                 : #endif
   18387                 :       break;
   18388                 :     }
   18389                 :   }
   18390                 :   return p;
   18391                 : }
   18392                 : 
   18393                 : 
   18394                 : /*
   18395                 : ** This routine deallocates a previously
   18396                 : ** allocated mutex.  SQLite is careful to deallocate every
   18397                 : ** mutex that it allocates.
   18398                 : */
   18399                 : static void winMutexFree(sqlite3_mutex *p){
   18400                 :   assert( p );
   18401                 :   assert( p->nRef==0 && p->owner==0 );
   18402                 :   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   18403                 :   DeleteCriticalSection(&p->mutex);
   18404                 :   sqlite3_free(p);
   18405                 : }
   18406                 : 
   18407                 : /*
   18408                 : ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   18409                 : ** to enter a mutex.  If another thread is already within the mutex,
   18410                 : ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
   18411                 : ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
   18412                 : ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
   18413                 : ** be entered multiple times by the same thread.  In such cases the,
   18414                 : ** mutex must be exited an equal number of times before another thread
   18415                 : ** can enter.  If the same thread tries to enter any other kind of mutex
   18416                 : ** more than once, the behavior is undefined.
   18417                 : */
   18418                 : static void winMutexEnter(sqlite3_mutex *p){
   18419                 : #ifdef SQLITE_DEBUG
   18420                 :   DWORD tid = GetCurrentThreadId(); 
   18421                 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
   18422                 : #endif
   18423                 :   EnterCriticalSection(&p->mutex);
   18424                 : #ifdef SQLITE_DEBUG
   18425                 :   assert( p->nRef>0 || p->owner==0 );
   18426                 :   p->owner = tid; 
   18427                 :   p->nRef++;
   18428                 :   if( p->trace ){
   18429                 :     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   18430                 :   }
   18431                 : #endif
   18432                 : }
   18433                 : static int winMutexTry(sqlite3_mutex *p){
   18434                 : #ifndef NDEBUG
   18435                 :   DWORD tid = GetCurrentThreadId(); 
   18436                 : #endif
   18437                 :   int rc = SQLITE_BUSY;
   18438                 :   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
   18439                 :   /*
   18440                 :   ** The sqlite3_mutex_try() routine is very rarely used, and when it
   18441                 :   ** is used it is merely an optimization.  So it is OK for it to always
   18442                 :   ** fail.  
   18443                 :   **
   18444                 :   ** The TryEnterCriticalSection() interface is only available on WinNT.
   18445                 :   ** And some windows compilers complain if you try to use it without
   18446                 :   ** first doing some #defines that prevent SQLite from building on Win98.
   18447                 :   ** For that reason, we will omit this optimization for now.  See
   18448                 :   ** ticket #2685.
   18449                 :   */
   18450                 : #if 0
   18451                 :   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
   18452                 :     p->owner = tid;
   18453                 :     p->nRef++;
   18454                 :     rc = SQLITE_OK;
   18455                 :   }
   18456                 : #else
   18457                 :   UNUSED_PARAMETER(p);
   18458                 : #endif
   18459                 : #ifdef SQLITE_DEBUG
   18460                 :   if( rc==SQLITE_OK && p->trace ){
   18461                 :     printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   18462                 :   }
   18463                 : #endif
   18464                 :   return rc;
   18465                 : }
   18466                 : 
   18467                 : /*
   18468                 : ** The sqlite3_mutex_leave() routine exits a mutex that was
   18469                 : ** previously entered by the same thread.  The behavior
   18470                 : ** is undefined if the mutex is not currently entered or
   18471                 : ** is not currently allocated.  SQLite will never do either.
   18472                 : */
   18473                 : static void winMutexLeave(sqlite3_mutex *p){
   18474                 : #ifndef NDEBUG
   18475                 :   DWORD tid = GetCurrentThreadId();
   18476                 :   assert( p->nRef>0 );
   18477                 :   assert( p->owner==tid );
   18478                 :   p->nRef--;
   18479                 :   if( p->nRef==0 ) p->owner = 0;
   18480                 :   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   18481                 : #endif
   18482                 :   LeaveCriticalSection(&p->mutex);
   18483                 : #ifdef SQLITE_DEBUG
   18484                 :   if( p->trace ){
   18485                 :     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
   18486                 :   }
   18487                 : #endif
   18488                 : }
   18489                 : 
   18490                 : SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
   18491                 :   static const sqlite3_mutex_methods sMutex = {
   18492                 :     winMutexInit,
   18493                 :     winMutexEnd,
   18494                 :     winMutexAlloc,
   18495                 :     winMutexFree,
   18496                 :     winMutexEnter,
   18497                 :     winMutexTry,
   18498                 :     winMutexLeave,
   18499                 : #ifdef SQLITE_DEBUG
   18500                 :     winMutexHeld,
   18501                 :     winMutexNotheld
   18502                 : #else
   18503                 :     0,
   18504                 :     0
   18505                 : #endif
   18506                 :   };
   18507                 : 
   18508                 :   return &sMutex;
   18509                 : }
   18510                 : #endif /* SQLITE_MUTEX_W32 */
   18511                 : 
   18512                 : /************** End of mutex_w32.c *******************************************/
   18513                 : /************** Begin file malloc.c ******************************************/
   18514                 : /*
   18515                 : ** 2001 September 15
   18516                 : **
   18517                 : ** The author disclaims copyright to this source code.  In place of
   18518                 : ** a legal notice, here is a blessing:
   18519                 : **
   18520                 : **    May you do good and not evil.
   18521                 : **    May you find forgiveness for yourself and forgive others.
   18522                 : **    May you share freely, never taking more than you give.
   18523                 : **
   18524                 : *************************************************************************
   18525                 : **
   18526                 : ** Memory allocation functions used throughout sqlite.
   18527                 : */
   18528                 : /* #include <stdarg.h> */
   18529                 : 
   18530                 : /*
   18531                 : ** Attempt to release up to n bytes of non-essential memory currently
   18532                 : ** held by SQLite. An example of non-essential memory is memory used to
   18533                 : ** cache database pages that are not currently in use.
   18534                 : */
   18535               0 : SQLITE_API int sqlite3_release_memory(int n){
   18536                 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   18537                 :   return sqlite3PcacheReleaseMemory(n);
   18538                 : #else
   18539                 :   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
   18540                 :   ** is a no-op returning zero if SQLite is not compiled with
   18541                 :   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
   18542                 :   UNUSED_PARAMETER(n);
   18543               0 :   return 0;
   18544                 : #endif
   18545                 : }
   18546                 : 
   18547                 : /*
   18548                 : ** An instance of the following object records the location of
   18549                 : ** each unused scratch buffer.
   18550                 : */
   18551                 : typedef struct ScratchFreeslot {
   18552                 :   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
   18553                 : } ScratchFreeslot;
   18554                 : 
   18555                 : /*
   18556                 : ** State information local to the memory allocation subsystem.
   18557                 : */
   18558                 : static SQLITE_WSD struct Mem0Global {
   18559                 :   sqlite3_mutex *mutex;         /* Mutex to serialize access */
   18560                 : 
   18561                 :   /*
   18562                 :   ** The alarm callback and its arguments.  The mem0.mutex lock will
   18563                 :   ** be held while the callback is running.  Recursive calls into
   18564                 :   ** the memory subsystem are allowed, but no new callbacks will be
   18565                 :   ** issued.
   18566                 :   */
   18567                 :   sqlite3_int64 alarmThreshold;
   18568                 :   void (*alarmCallback)(void*, sqlite3_int64,int);
   18569                 :   void *alarmArg;
   18570                 : 
   18571                 :   /*
   18572                 :   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
   18573                 :   ** (so that a range test can be used to determine if an allocation
   18574                 :   ** being freed came from pScratch) and a pointer to the list of
   18575                 :   ** unused scratch allocations.
   18576                 :   */
   18577                 :   void *pScratchEnd;
   18578                 :   ScratchFreeslot *pScratchFree;
   18579                 :   u32 nScratchFree;
   18580                 : 
   18581                 :   /*
   18582                 :   ** True if heap is nearly "full" where "full" is defined by the
   18583                 :   ** sqlite3_soft_heap_limit() setting.
   18584                 :   */
   18585                 :   int nearlyFull;
   18586                 : } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
   18587                 : 
   18588                 : #define mem0 GLOBAL(struct Mem0Global, mem0)
   18589                 : 
   18590                 : /*
   18591                 : ** This routine runs when the memory allocator sees that the
   18592                 : ** total memory allocation is about to exceed the soft heap
   18593                 : ** limit.
   18594                 : */
   18595               0 : static void softHeapLimitEnforcer(
   18596                 :   void *NotUsed, 
   18597                 :   sqlite3_int64 NotUsed2,
   18598                 :   int allocSize
   18599                 : ){
   18600                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   18601               0 :   sqlite3_release_memory(allocSize);
   18602               0 : }
   18603                 : 
   18604                 : /*
   18605                 : ** Change the alarm callback
   18606                 : */
   18607               0 : static int sqlite3MemoryAlarm(
   18608                 :   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
   18609                 :   void *pArg,
   18610                 :   sqlite3_int64 iThreshold
   18611                 : ){
   18612                 :   int nUsed;
   18613               0 :   sqlite3_mutex_enter(mem0.mutex);
   18614               0 :   mem0.alarmCallback = xCallback;
   18615               0 :   mem0.alarmArg = pArg;
   18616               0 :   mem0.alarmThreshold = iThreshold;
   18617               0 :   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   18618               0 :   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
   18619               0 :   sqlite3_mutex_leave(mem0.mutex);
   18620               0 :   return SQLITE_OK;
   18621                 : }
   18622                 : 
   18623                 : #ifndef SQLITE_OMIT_DEPRECATED
   18624                 : /*
   18625                 : ** Deprecated external interface.  Internal/core SQLite code
   18626                 : ** should call sqlite3MemoryAlarm.
   18627                 : */
   18628               0 : SQLITE_API int sqlite3_memory_alarm(
   18629                 :   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
   18630                 :   void *pArg,
   18631                 :   sqlite3_int64 iThreshold
   18632                 : ){
   18633               0 :   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
   18634                 : }
   18635                 : #endif
   18636                 : 
   18637                 : /*
   18638                 : ** Set the soft heap-size limit for the library. Passing a zero or 
   18639                 : ** negative value indicates no limit.
   18640                 : */
   18641               0 : SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
   18642                 :   sqlite3_int64 priorLimit;
   18643                 :   sqlite3_int64 excess;
   18644                 : #ifndef SQLITE_OMIT_AUTOINIT
   18645               0 :   int rc = sqlite3_initialize();
   18646               0 :   if( rc ) return -1;
   18647                 : #endif
   18648               0 :   sqlite3_mutex_enter(mem0.mutex);
   18649               0 :   priorLimit = mem0.alarmThreshold;
   18650               0 :   sqlite3_mutex_leave(mem0.mutex);
   18651               0 :   if( n<0 ) return priorLimit;
   18652               0 :   if( n>0 ){
   18653               0 :     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
   18654                 :   }else{
   18655               0 :     sqlite3MemoryAlarm(0, 0, 0);
   18656                 :   }
   18657               0 :   excess = sqlite3_memory_used() - n;
   18658               0 :   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
   18659               0 :   return priorLimit;
   18660                 : }
   18661               0 : SQLITE_API void sqlite3_soft_heap_limit(int n){
   18662               0 :   if( n<0 ) n = 0;
   18663               0 :   sqlite3_soft_heap_limit64(n);
   18664               0 : }
   18665                 : 
   18666                 : /*
   18667                 : ** Initialize the memory allocation subsystem.
   18668                 : */
   18669             806 : SQLITE_PRIVATE int sqlite3MallocInit(void){
   18670             806 :   if( sqlite3GlobalConfig.m.xMalloc==0 ){
   18671               3 :     sqlite3MemSetDefault();
   18672                 :   }
   18673             806 :   memset(&mem0, 0, sizeof(mem0));
   18674             806 :   if( sqlite3GlobalConfig.bCoreMutex ){
   18675             806 :     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
   18676                 :   }
   18677             806 :   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
   18678               0 :       && sqlite3GlobalConfig.nScratch>0 ){
   18679                 :     int i, n, sz;
   18680                 :     ScratchFreeslot *pSlot;
   18681               0 :     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
   18682               0 :     sqlite3GlobalConfig.szScratch = sz;
   18683               0 :     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
   18684               0 :     n = sqlite3GlobalConfig.nScratch;
   18685               0 :     mem0.pScratchFree = pSlot;
   18686               0 :     mem0.nScratchFree = n;
   18687               0 :     for(i=0; i<n-1; i++){
   18688               0 :       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
   18689               0 :       pSlot = pSlot->pNext;
   18690                 :     }
   18691               0 :     pSlot->pNext = 0;
   18692               0 :     mem0.pScratchEnd = (void*)&pSlot[1];
   18693                 :   }else{
   18694             806 :     mem0.pScratchEnd = 0;
   18695             806 :     sqlite3GlobalConfig.pScratch = 0;
   18696             806 :     sqlite3GlobalConfig.szScratch = 0;
   18697             806 :     sqlite3GlobalConfig.nScratch = 0;
   18698                 :   }
   18699             806 :   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
   18700               0 :       || sqlite3GlobalConfig.nPage<1 ){
   18701             806 :     sqlite3GlobalConfig.pPage = 0;
   18702             806 :     sqlite3GlobalConfig.szPage = 0;
   18703             806 :     sqlite3GlobalConfig.nPage = 0;
   18704                 :   }
   18705             806 :   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
   18706                 : }
   18707                 : 
   18708                 : /*
   18709                 : ** Return true if the heap is currently under memory pressure - in other
   18710                 : ** words if the amount of heap used is close to the limit set by
   18711                 : ** sqlite3_soft_heap_limit().
   18712                 : */
   18713           27552 : SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
   18714           27552 :   return mem0.nearlyFull;
   18715                 : }
   18716                 : 
   18717                 : /*
   18718                 : ** Deinitialize the memory allocation subsystem.
   18719                 : */
   18720             795 : SQLITE_PRIVATE void sqlite3MallocEnd(void){
   18721             795 :   if( sqlite3GlobalConfig.m.xShutdown ){
   18722             795 :     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
   18723                 :   }
   18724             795 :   memset(&mem0, 0, sizeof(mem0));
   18725             795 : }
   18726                 : 
   18727                 : /*
   18728                 : ** Return the amount of memory currently checked out.
   18729                 : */
   18730               6 : SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
   18731                 :   int n, mx;
   18732                 :   sqlite3_int64 res;
   18733               6 :   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
   18734               6 :   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
   18735               6 :   return res;
   18736                 : }
   18737                 : 
   18738                 : /*
   18739                 : ** Return the maximum amount of memory that has ever been
   18740                 : ** checked out since either the beginning of this process
   18741                 : ** or since the most recent reset.
   18742                 : */
   18743               0 : SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
   18744                 :   int n, mx;
   18745                 :   sqlite3_int64 res;
   18746               0 :   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
   18747               0 :   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
   18748               0 :   return res;
   18749                 : }
   18750                 : 
   18751                 : /*
   18752                 : ** Trigger the alarm 
   18753                 : */
   18754          269031 : static void sqlite3MallocAlarm(int nByte){
   18755                 :   void (*xCallback)(void*,sqlite3_int64,int);
   18756                 :   sqlite3_int64 nowUsed;
   18757                 :   void *pArg;
   18758          269031 :   if( mem0.alarmCallback==0 ) return;
   18759               0 :   xCallback = mem0.alarmCallback;
   18760               0 :   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   18761               0 :   pArg = mem0.alarmArg;
   18762               0 :   mem0.alarmCallback = 0;
   18763               0 :   sqlite3_mutex_leave(mem0.mutex);
   18764               0 :   xCallback(pArg, nowUsed, nByte);
   18765               0 :   sqlite3_mutex_enter(mem0.mutex);
   18766               0 :   mem0.alarmCallback = xCallback;
   18767               0 :   mem0.alarmArg = pArg;
   18768                 : }
   18769                 : 
   18770                 : /*
   18771                 : ** Do a memory allocation with statistics and alarms.  Assume the
   18772                 : ** lock is already held.
   18773                 : */
   18774         6078356 : static int mallocWithAlarm(int n, void **pp){
   18775                 :   int nFull;
   18776                 :   void *p;
   18777         6078356 :   assert( sqlite3_mutex_held(mem0.mutex) );
   18778         6078356 :   nFull = sqlite3GlobalConfig.m.xRoundup(n);
   18779         6078356 :   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
   18780         6078356 :   if( mem0.alarmCallback!=0 ){
   18781               0 :     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
   18782               0 :     if( nUsed >= mem0.alarmThreshold - nFull ){
   18783               0 :       mem0.nearlyFull = 1;
   18784               0 :       sqlite3MallocAlarm(nFull);
   18785                 :     }else{
   18786               0 :       mem0.nearlyFull = 0;
   18787                 :     }
   18788                 :   }
   18789         6078356 :   p = sqlite3GlobalConfig.m.xMalloc(nFull);
   18790                 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   18791                 :   if( p==0 && mem0.alarmCallback ){
   18792                 :     sqlite3MallocAlarm(nFull);
   18793                 :     p = sqlite3GlobalConfig.m.xMalloc(nFull);
   18794                 :   }
   18795                 : #endif
   18796         6078356 :   if( p ){
   18797         6078356 :     nFull = sqlite3MallocSize(p);
   18798         6078356 :     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
   18799         6078356 :     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
   18800                 :   }
   18801         6078356 :   *pp = p;
   18802         6078356 :   return nFull;
   18803                 : }
   18804                 : 
   18805                 : /*
   18806                 : ** Allocate memory.  This routine is like sqlite3_malloc() except that it
   18807                 : ** assumes the memory subsystem has already been initialized.
   18808                 : */
   18809         6067170 : SQLITE_PRIVATE void *sqlite3Malloc(int n){
   18810                 :   void *p;
   18811         6067170 :   if( n<=0               /* IMP: R-65312-04917 */ 
   18812         6067169 :    || n>=0x7fffff00
   18813                 :   ){
   18814                 :     /* A memory allocation of a number of bytes which is near the maximum
   18815                 :     ** signed integer value might cause an integer overflow inside of the
   18816                 :     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
   18817                 :     ** 255 bytes of overhead.  SQLite itself will never use anything near
   18818                 :     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
   18819               0 :     p = 0;
   18820         6067170 :   }else if( sqlite3GlobalConfig.bMemstat ){
   18821         6067170 :     sqlite3_mutex_enter(mem0.mutex);
   18822         6067173 :     mallocWithAlarm(n, &p);
   18823         6067173 :     sqlite3_mutex_leave(mem0.mutex);
   18824                 :   }else{
   18825               0 :     p = sqlite3GlobalConfig.m.xMalloc(n);
   18826                 :   }
   18827         6067173 :   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
   18828         6067173 :   return p;
   18829                 : }
   18830                 : 
   18831                 : /*
   18832                 : ** This version of the memory allocation is for use by the application.
   18833                 : ** First make sure the memory subsystem is initialized, then do the
   18834                 : ** allocation.
   18835                 : */
   18836         1432491 : SQLITE_API void *sqlite3_malloc(int n){
   18837                 : #ifndef SQLITE_OMIT_AUTOINIT
   18838         1432491 :   if( sqlite3_initialize() ) return 0;
   18839                 : #endif
   18840         1432491 :   return sqlite3Malloc(n);
   18841                 : }
   18842                 : 
   18843                 : /*
   18844                 : ** Each thread may only have a single outstanding allocation from
   18845                 : ** xScratchMalloc().  We verify this constraint in the single-threaded
   18846                 : ** case by setting scratchAllocOut to 1 when an allocation
   18847                 : ** is outstanding clearing it when the allocation is freed.
   18848                 : */
   18849                 : #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   18850                 : static int scratchAllocOut = 0;
   18851                 : #endif
   18852                 : 
   18853                 : 
   18854                 : /*
   18855                 : ** Allocate memory that is to be used and released right away.
   18856                 : ** This routine is similar to alloca() in that it is not intended
   18857                 : ** for situations where the memory might be held long-term.  This
   18858                 : ** routine is intended to get memory to old large transient data
   18859                 : ** structures that would not normally fit on the stack of an
   18860                 : ** embedded processor.
   18861                 : */
   18862           11183 : SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
   18863                 :   void *p;
   18864           11183 :   assert( n>0 );
   18865                 : 
   18866           11183 :   sqlite3_mutex_enter(mem0.mutex);
   18867           11183 :   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
   18868               0 :     p = mem0.pScratchFree;
   18869               0 :     mem0.pScratchFree = mem0.pScratchFree->pNext;
   18870               0 :     mem0.nScratchFree--;
   18871               0 :     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
   18872               0 :     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   18873               0 :     sqlite3_mutex_leave(mem0.mutex);
   18874                 :   }else{
   18875           11183 :     if( sqlite3GlobalConfig.bMemstat ){
   18876           11183 :       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
   18877           11183 :       n = mallocWithAlarm(n, &p);
   18878           11183 :       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
   18879           11183 :       sqlite3_mutex_leave(mem0.mutex);
   18880                 :     }else{
   18881               0 :       sqlite3_mutex_leave(mem0.mutex);
   18882               0 :       p = sqlite3GlobalConfig.m.xMalloc(n);
   18883                 :     }
   18884                 :     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
   18885                 :   }
   18886           11183 :   assert( sqlite3_mutex_notheld(mem0.mutex) );
   18887                 : 
   18888                 : 
   18889                 : #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   18890                 :   /* Verify that no more than two scratch allocations per thread
   18891                 :   ** are outstanding at one time.  (This is only checked in the
   18892                 :   ** single-threaded case since checking in the multi-threaded case
   18893                 :   ** would be much more complicated.) */
   18894                 :   assert( scratchAllocOut<=1 );
   18895                 :   if( p ) scratchAllocOut++;
   18896                 : #endif
   18897                 : 
   18898           11183 :   return p;
   18899                 : }
   18900           11183 : SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
   18901           11183 :   if( p ){
   18902                 : 
   18903                 : #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
   18904                 :     /* Verify that no more than two scratch allocation per thread
   18905                 :     ** is outstanding at one time.  (This is only checked in the
   18906                 :     ** single-threaded case since checking in the multi-threaded case
   18907                 :     ** would be much more complicated.) */
   18908                 :     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
   18909                 :     scratchAllocOut--;
   18910                 : #endif
   18911                 : 
   18912           11183 :     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
   18913                 :       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
   18914                 :       ScratchFreeslot *pSlot;
   18915               0 :       pSlot = (ScratchFreeslot*)p;
   18916               0 :       sqlite3_mutex_enter(mem0.mutex);
   18917               0 :       pSlot->pNext = mem0.pScratchFree;
   18918               0 :       mem0.pScratchFree = pSlot;
   18919               0 :       mem0.nScratchFree++;
   18920               0 :       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
   18921               0 :       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
   18922               0 :       sqlite3_mutex_leave(mem0.mutex);
   18923                 :     }else{
   18924                 :       /* Release memory back to the heap */
   18925                 :       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
   18926                 :       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
   18927                 :       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   18928           11183 :       if( sqlite3GlobalConfig.bMemstat ){
   18929           11183 :         int iSize = sqlite3MallocSize(p);
   18930           11183 :         sqlite3_mutex_enter(mem0.mutex);
   18931           11183 :         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
   18932           11183 :         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
   18933           11183 :         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   18934           11183 :         sqlite3GlobalConfig.m.xFree(p);
   18935           11183 :         sqlite3_mutex_leave(mem0.mutex);
   18936                 :       }else{
   18937               0 :         sqlite3GlobalConfig.m.xFree(p);
   18938                 :       }
   18939                 :     }
   18940                 :   }
   18941           11183 : }
   18942                 : 
   18943                 : /*
   18944                 : ** TRUE if p is a lookaside memory allocation from db
   18945                 : */
   18946                 : #ifndef SQLITE_OMIT_LOOKASIDE
   18947        22923433 : static int isLookaside(sqlite3 *db, void *p){
   18948        22923433 :   return p && p>=db->lookaside.pStart && p<db->lookaside.pEnd;
   18949                 : }
   18950                 : #else
   18951                 : #define isLookaside(A,B) 0
   18952                 : #endif
   18953                 : 
   18954                 : /*
   18955                 : ** Return the size of a memory allocation previously obtained from
   18956                 : ** sqlite3Malloc() or sqlite3_malloc().
   18957                 : */
   18958        13660448 : SQLITE_PRIVATE int sqlite3MallocSize(void *p){
   18959                 :   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   18960                 :   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   18961        13660448 :   return sqlite3GlobalConfig.m.xSize(p);
   18962                 : }
   18963         2967768 : SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
   18964         2967768 :   assert( db==0 || sqlite3_mutex_held(db->mutex) );
   18965         2967768 :   if( db && isLookaside(db, p) ){
   18966          785821 :     return db->lookaside.sz;
   18967                 :   }else{
   18968                 :     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   18969                 :     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   18970                 :     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   18971         2181940 :     return sqlite3GlobalConfig.m.xSize(p);
   18972                 :   }
   18973                 : }
   18974                 : 
   18975                 : /*
   18976                 : ** Free memory previously obtained from sqlite3Malloc().
   18977                 : */
   18978        22569275 : SQLITE_API void sqlite3_free(void *p){
   18979        22569275 :   if( p==0 ) return;  /* IMP: R-49053-54554 */
   18980                 :   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   18981                 :   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   18982         6067124 :   if( sqlite3GlobalConfig.bMemstat ){
   18983         6067124 :     sqlite3_mutex_enter(mem0.mutex);
   18984         6067128 :     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   18985         6067128 :     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   18986         6067128 :     sqlite3GlobalConfig.m.xFree(p);
   18987         6067128 :     sqlite3_mutex_leave(mem0.mutex);
   18988                 :   }else{
   18989               0 :     sqlite3GlobalConfig.m.xFree(p);
   18990                 :   }
   18991                 : }
   18992                 : 
   18993                 : /*
   18994                 : ** Free memory that might be associated with a particular database
   18995                 : ** connection.
   18996                 : */
   18997        21041821 : SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
   18998        21041821 :   assert( db==0 || sqlite3_mutex_held(db->mutex) );
   18999        21041706 :   if( db ){
   19000        18694756 :     if( db->pnBytesFreed ){
   19001            5166 :       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
   19002            5166 :       return;
   19003                 :     }
   19004        18689590 :     if( isLookaside(db, p) ){
   19005         4313491 :       LookasideSlot *pBuf = (LookasideSlot*)p;
   19006         4313491 :       pBuf->pNext = db->lookaside.pFree;
   19007         4313491 :       db->lookaside.pFree = pBuf;
   19008         4313491 :       db->lookaside.nOut--;
   19009         4313491 :       return;
   19010                 :     }
   19011                 :   }
   19012                 :   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   19013                 :   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   19014                 :   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   19015                 :   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   19016        16722866 :   sqlite3_free(p);
   19017                 : }
   19018                 : 
   19019                 : /*
   19020                 : ** Change the size of an existing memory allocation
   19021                 : */
   19022         1046362 : SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
   19023                 :   int nOld, nNew, nDiff;
   19024                 :   void *pNew;
   19025         1046362 :   if( pOld==0 ){
   19026            7941 :     return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
   19027                 :   }
   19028         1038421 :   if( nBytes<=0 ){
   19029               0 :     sqlite3_free(pOld); /* IMP: R-31593-10574 */
   19030               0 :     return 0;
   19031                 :   }
   19032         1038421 :   if( nBytes>=0x7fffff00 ){
   19033                 :     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
   19034               0 :     return 0;
   19035                 :   }
   19036         1038421 :   nOld = sqlite3MallocSize(pOld);
   19037                 :   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
   19038                 :   ** argument to xRealloc is always a value returned by a prior call to
   19039                 :   ** xRoundup. */
   19040         1038421 :   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
   19041         1038421 :   if( nOld==nNew ){
   19042          769390 :     pNew = pOld;
   19043          269031 :   }else if( sqlite3GlobalConfig.bMemstat ){
   19044          269031 :     sqlite3_mutex_enter(mem0.mutex);
   19045          269031 :     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
   19046          269031 :     nDiff = nNew - nOld;
   19047          538062 :     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
   19048          269031 :           mem0.alarmThreshold-nDiff ){
   19049          269031 :       sqlite3MallocAlarm(nDiff);
   19050                 :     }
   19051                 :     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   19052                 :     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   19053          269031 :     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   19054          269031 :     if( pNew==0 && mem0.alarmCallback ){
   19055               0 :       sqlite3MallocAlarm(nBytes);
   19056               0 :       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   19057                 :     }
   19058          269031 :     if( pNew ){
   19059          269031 :       nNew = sqlite3MallocSize(pNew);
   19060          269031 :       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
   19061                 :     }
   19062          269031 :     sqlite3_mutex_leave(mem0.mutex);
   19063                 :   }else{
   19064               0 :     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   19065                 :   }
   19066         1038421 :   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
   19067         1038421 :   return pNew;
   19068                 : }
   19069                 : 
   19070                 : /*
   19071                 : ** The public interface to sqlite3Realloc.  Make sure that the memory
   19072                 : ** subsystem is initialized prior to invoking sqliteRealloc.
   19073                 : */
   19074         1023728 : SQLITE_API void *sqlite3_realloc(void *pOld, int n){
   19075                 : #ifndef SQLITE_OMIT_AUTOINIT
   19076         1023728 :   if( sqlite3_initialize() ) return 0;
   19077                 : #endif
   19078         1023728 :   return sqlite3Realloc(pOld, n);
   19079                 : }
   19080                 : 
   19081                 : 
   19082                 : /*
   19083                 : ** Allocate and zero memory.
   19084                 : */ 
   19085          221438 : SQLITE_PRIVATE void *sqlite3MallocZero(int n){
   19086          221438 :   void *p = sqlite3Malloc(n);
   19087          221438 :   if( p ){
   19088          221438 :     memset(p, 0, n);
   19089                 :   }
   19090          221438 :   return p;
   19091                 : }
   19092                 : 
   19093                 : /*
   19094                 : ** Allocate and zero memory.  If the allocation fails, make
   19095                 : ** the mallocFailed flag in the connection pointer.
   19096                 : */
   19097         2467569 : SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
   19098         2467569 :   void *p = sqlite3DbMallocRaw(db, n);
   19099         2467569 :   if( p ){
   19100         2467569 :     memset(p, 0, n);
   19101                 :   }
   19102         2467569 :   return p;
   19103                 : }
   19104                 : 
   19105                 : /*
   19106                 : ** Allocate and zero memory.  If the allocation fails, make
   19107                 : ** the mallocFailed flag in the connection pointer.
   19108                 : **
   19109                 : ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
   19110                 : ** failure on the same database connection) then always return 0.
   19111                 : ** Hence for a particular database connection, once malloc starts
   19112                 : ** failing, it fails consistently until mallocFailed is reset.
   19113                 : ** This is an important assumption.  There are many places in the
   19114                 : ** code that do things like this:
   19115                 : **
   19116                 : **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
   19117                 : **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
   19118                 : **         if( b ) a[10] = 9;
   19119                 : **
   19120                 : ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
   19121                 : ** that all prior mallocs (ex: "a") worked too.
   19122                 : */
   19123         8308616 : SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
   19124                 :   void *p;
   19125         8308616 :   assert( db==0 || sqlite3_mutex_held(db->mutex) );
   19126         8308608 :   assert( db==0 || db->pnBytesFreed==0 );
   19127                 : #ifndef SQLITE_OMIT_LOOKASIDE
   19128         8308608 :   if( db ){
   19129                 :     LookasideSlot *pBuf;
   19130         8266136 :     if( db->mallocFailed ){
   19131               0 :       return 0;
   19132                 :     }
   19133         8266136 :     if( db->lookaside.bEnabled ){
   19134         6482701 :       if( n>db->lookaside.sz ){
   19135         1243010 :         db->lookaside.anStat[1]++;
   19136         5239691 :       }else if( (pBuf = db->lookaside.pFree)==0 ){
   19137          926195 :         db->lookaside.anStat[2]++;
   19138                 :       }else{
   19139         4313496 :         db->lookaside.pFree = pBuf->pNext;
   19140         4313496 :         db->lookaside.nOut++;
   19141         4313496 :         db->lookaside.anStat[0]++;
   19142         4313496 :         if( db->lookaside.nOut>db->lookaside.mxOut ){
   19143          829929 :           db->lookaside.mxOut = db->lookaside.nOut;
   19144                 :         }
   19145         4313496 :         return (void*)pBuf;
   19146                 :       }
   19147                 :     }
   19148                 :   }
   19149                 : #else
   19150                 :   if( db && db->mallocFailed ){
   19151                 :     return 0;
   19152                 :   }
   19153                 : #endif
   19154         3995112 :   p = sqlite3Malloc(n);
   19155         3995128 :   if( !p && db ){
   19156               0 :     db->mallocFailed = 1;
   19157                 :   }
   19158                 :   sqlite3MemdebugSetType(p, MEMTYPE_DB |
   19159                 :          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   19160         3995128 :   return p;
   19161                 : }
   19162                 : 
   19163                 : /*
   19164                 : ** Resize the block of memory pointed to by p to n bytes. If the
   19165                 : ** resize fails, set the mallocFailed flag in the connection object.
   19166                 : */
   19167         2041619 : SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
   19168         2041619 :   void *pNew = 0;
   19169         2041619 :   assert( db!=0 );
   19170         2041619 :   assert( sqlite3_mutex_held(db->mutex) );
   19171         2041618 :   if( db->mallocFailed==0 ){
   19172         2041619 :     if( p==0 ){
   19173          774989 :       return sqlite3DbMallocRaw(db, n);
   19174                 :     }
   19175         1266630 :     if( isLookaside(db, p) ){
   19176          245311 :       if( n<=db->lookaside.sz ){
   19177          218611 :         return p;
   19178                 :       }
   19179           26700 :       pNew = sqlite3DbMallocRaw(db, n);
   19180           26700 :       if( pNew ){
   19181           26700 :         memcpy(pNew, p, db->lookaside.sz);
   19182           26700 :         sqlite3DbFree(db, p);
   19183                 :       }
   19184                 :     }else{
   19185                 :       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   19186                 :       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   19187                 :       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   19188         1021319 :       pNew = sqlite3_realloc(p, n);
   19189         1021319 :       if( !pNew ){
   19190                 :         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
   19191               0 :         db->mallocFailed = 1;
   19192                 :       }
   19193                 :       sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
   19194                 :             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   19195                 :     }
   19196                 :   }
   19197         1048019 :   return pNew;
   19198                 : }
   19199                 : 
   19200                 : /*
   19201                 : ** Attempt to reallocate p.  If the reallocation fails, then free p
   19202                 : ** and set the mallocFailed flag in the database connection.
   19203                 : */
   19204          190207 : SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
   19205                 :   void *pNew;
   19206          190207 :   pNew = sqlite3DbRealloc(db, p, n);
   19207          190207 :   if( !pNew ){
   19208               0 :     sqlite3DbFree(db, p);
   19209                 :   }
   19210          190207 :   return pNew;
   19211                 : }
   19212                 : 
   19213                 : /*
   19214                 : ** Make a copy of a string in memory obtained from sqliteMalloc(). These 
   19215                 : ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
   19216                 : ** is because when memory debugging is turned on, these two functions are 
   19217                 : ** called via macros that record the current file and line number in the
   19218                 : ** ThreadData structure.
   19219                 : */
   19220          291277 : SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
   19221                 :   char *zNew;
   19222                 :   size_t n;
   19223          291277 :   if( z==0 ){
   19224          159889 :     return 0;
   19225                 :   }
   19226          131388 :   n = sqlite3Strlen30(z) + 1;
   19227          131388 :   assert( (n&0x7fffffff)==n );
   19228          131388 :   zNew = sqlite3DbMallocRaw(db, (int)n);
   19229          131388 :   if( zNew ){
   19230          131388 :     memcpy(zNew, z, n);
   19231                 :   }
   19232          131388 :   return zNew;
   19233                 : }
   19234         1700014 : SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
   19235                 :   char *zNew;
   19236         1700014 :   if( z==0 ){
   19237           85403 :     return 0;
   19238                 :   }
   19239         1614611 :   assert( (n&0x7fffffff)==n );
   19240         1614611 :   zNew = sqlite3DbMallocRaw(db, n+1);
   19241         1614611 :   if( zNew ){
   19242         1614611 :     memcpy(zNew, z, n);
   19243         1614611 :     zNew[n] = 0;
   19244                 :   }
   19245         1614611 :   return zNew;
   19246                 : }
   19247                 : 
   19248                 : /*
   19249                 : ** Create a string from the zFromat argument and the va_list that follows.
   19250                 : ** Store the string in memory obtained from sqliteMalloc() and make *pz
   19251                 : ** point to that string.
   19252                 : */
   19253             367 : SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
   19254                 :   va_list ap;
   19255                 :   char *z;
   19256                 : 
   19257             367 :   va_start(ap, zFormat);
   19258             367 :   z = sqlite3VMPrintf(db, zFormat, ap);
   19259             367 :   va_end(ap);
   19260             367 :   sqlite3DbFree(db, *pz);
   19261             367 :   *pz = z;
   19262             367 : }
   19263                 : 
   19264                 : 
   19265                 : /*
   19266                 : ** This function must be called before exiting any API function (i.e. 
   19267                 : ** returning control to the user) that has called sqlite3_malloc or
   19268                 : ** sqlite3_realloc.
   19269                 : **
   19270                 : ** The returned value is normally a copy of the second argument to this
   19271                 : ** function. However, if a malloc() failure has occurred since the previous
   19272                 : ** invocation SQLITE_NOMEM is returned instead. 
   19273                 : **
   19274                 : ** If the first argument, db, is not NULL and a malloc() error has occurred,
   19275                 : ** then the connection error-code (the value returned by sqlite3_errcode())
   19276                 : ** is set to SQLITE_NOMEM.
   19277                 : */
   19278         2899647 : SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
   19279                 :   /* If the db handle is not NULL, then we must hold the connection handle
   19280                 :   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed 
   19281                 :   ** is unsafe, as is the call to sqlite3Error().
   19282                 :   */
   19283         2899647 :   assert( !db || sqlite3_mutex_held(db->mutex) );
   19284         2899593 :   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
   19285               7 :     sqlite3Error(db, SQLITE_NOMEM, 0);
   19286               0 :     db->mallocFailed = 0;
   19287               0 :     rc = SQLITE_NOMEM;
   19288                 :   }
   19289         2899586 :   return rc & (db ? db->errMask : 0xff);
   19290                 : }
   19291                 : 
   19292                 : /************** End of malloc.c **********************************************/
   19293                 : /************** Begin file printf.c ******************************************/
   19294                 : /*
   19295                 : ** The "printf" code that follows dates from the 1980's.  It is in
   19296                 : ** the public domain.  The original comments are included here for
   19297                 : ** completeness.  They are very out-of-date but might be useful as
   19298                 : ** an historical reference.  Most of the "enhancements" have been backed
   19299                 : ** out so that the functionality is now the same as standard printf().
   19300                 : **
   19301                 : **************************************************************************
   19302                 : **
   19303                 : ** This file contains code for a set of "printf"-like routines.  These
   19304                 : ** routines format strings much like the printf() from the standard C
   19305                 : ** library, though the implementation here has enhancements to support
   19306                 : ** SQLlite.
   19307                 : */
   19308                 : 
   19309                 : /*
   19310                 : ** Conversion types fall into various categories as defined by the
   19311                 : ** following enumeration.
   19312                 : */
   19313                 : #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
   19314                 : #define etFLOAT       2 /* Floating point.  %f */
   19315                 : #define etEXP         3 /* Exponentional notation. %e and %E */
   19316                 : #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
   19317                 : #define etSIZE        5 /* Return number of characters processed so far. %n */
   19318                 : #define etSTRING      6 /* Strings. %s */
   19319                 : #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
   19320                 : #define etPERCENT     8 /* Percent symbol. %% */
   19321                 : #define etCHARX       9 /* Characters. %c */
   19322                 : /* The rest are extensions, not normally found in printf() */
   19323                 : #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
   19324                 : #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
   19325                 :                           NULL pointers replaced by SQL NULL.  %Q */
   19326                 : #define etTOKEN      12 /* a pointer to a Token structure */
   19327                 : #define etSRCLIST    13 /* a pointer to a SrcList */
   19328                 : #define etPOINTER    14 /* The %p conversion */
   19329                 : #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
   19330                 : #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
   19331                 : 
   19332                 : #define etINVALID     0 /* Any unrecognized conversion type */
   19333                 : 
   19334                 : 
   19335                 : /*
   19336                 : ** An "etByte" is an 8-bit unsigned value.
   19337                 : */
   19338                 : typedef unsigned char etByte;
   19339                 : 
   19340                 : /*
   19341                 : ** Each builtin conversion character (ex: the 'd' in "%d") is described
   19342                 : ** by an instance of the following structure
   19343                 : */
   19344                 : typedef struct et_info {   /* Information about each format field */
   19345                 :   char fmttype;            /* The format field code letter */
   19346                 :   etByte base;             /* The base for radix conversion */
   19347                 :   etByte flags;            /* One or more of FLAG_ constants below */
   19348                 :   etByte type;             /* Conversion paradigm */
   19349                 :   etByte charset;          /* Offset into aDigits[] of the digits string */
   19350                 :   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
   19351                 : } et_info;
   19352                 : 
   19353                 : /*
   19354                 : ** Allowed values for et_info.flags
   19355                 : */
   19356                 : #define FLAG_SIGNED  1     /* True if the value to convert is signed */
   19357                 : #define FLAG_INTERN  2     /* True if for internal use only */
   19358                 : #define FLAG_STRING  4     /* Allow infinity precision */
   19359                 : 
   19360                 : 
   19361                 : /*
   19362                 : ** The following table is searched linearly, so it is good to put the
   19363                 : ** most frequently used conversion types first.
   19364                 : */
   19365                 : static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
   19366                 : static const char aPrefix[] = "-x0\000X0";
   19367                 : static const et_info fmtinfo[] = {
   19368                 :   {  'd', 10, 1, etRADIX,      0,  0 },
   19369                 :   {  's',  0, 4, etSTRING,     0,  0 },
   19370                 :   {  'g',  0, 1, etGENERIC,    30, 0 },
   19371                 :   {  'z',  0, 4, etDYNSTRING,  0,  0 },
   19372                 :   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
   19373                 :   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
   19374                 :   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
   19375                 :   {  'c',  0, 0, etCHARX,      0,  0 },
   19376                 :   {  'o',  8, 0, etRADIX,      0,  2 },
   19377                 :   {  'u', 10, 0, etRADIX,      0,  0 },
   19378                 :   {  'x', 16, 0, etRADIX,      16, 1 },
   19379                 :   {  'X', 16, 0, etRADIX,      0,  4 },
   19380                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   19381                 :   {  'f',  0, 1, etFLOAT,      0,  0 },
   19382                 :   {  'e',  0, 1, etEXP,        30, 0 },
   19383                 :   {  'E',  0, 1, etEXP,        14, 0 },
   19384                 :   {  'G',  0, 1, etGENERIC,    14, 0 },
   19385                 : #endif
   19386                 :   {  'i', 10, 1, etRADIX,      0,  0 },
   19387                 :   {  'n',  0, 0, etSIZE,       0,  0 },
   19388                 :   {  '%',  0, 0, etPERCENT,    0,  0 },
   19389                 :   {  'p', 16, 0, etPOINTER,    0,  1 },
   19390                 : 
   19391                 : /* All the rest have the FLAG_INTERN bit set and are thus for internal
   19392                 : ** use only */
   19393                 :   {  'T',  0, 2, etTOKEN,      0,  0 },
   19394                 :   {  'S',  0, 2, etSRCLIST,    0,  0 },
   19395                 :   {  'r', 10, 3, etORDINAL,    0,  0 },
   19396                 : };
   19397                 : 
   19398                 : /*
   19399                 : ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
   19400                 : ** conversions will work.
   19401                 : */
   19402                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   19403                 : /*
   19404                 : ** "*val" is a double such that 0.1 <= *val < 10.0
   19405                 : ** Return the ascii code for the leading digit of *val, then
   19406                 : ** multiply "*val" by 10.0 to renormalize.
   19407                 : **
   19408                 : ** Example:
   19409                 : **     input:     *val = 3.14159
   19410                 : **     output:    *val = 1.4159    function return = '3'
   19411                 : **
   19412                 : ** The counter *cnt is incremented each time.  After counter exceeds
   19413                 : ** 16 (the number of significant digits in a 64-bit float) '0' is
   19414                 : ** always returned.
   19415                 : */
   19416          195382 : static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
   19417                 :   int digit;
   19418                 :   LONGDOUBLE_TYPE d;
   19419          195382 :   if( (*cnt)++ >= 16 ) return '0';
   19420          195246 :   digit = (int)*val;
   19421          195246 :   d = digit;
   19422          195246 :   digit += '0';
   19423          195246 :   *val = (*val - d)*10.0;
   19424          195246 :   return (char)digit;
   19425                 : }
   19426                 : #endif /* SQLITE_OMIT_FLOATING_POINT */
   19427                 : 
   19428                 : /*
   19429                 : ** Append N space characters to the given string buffer.
   19430                 : */
   19431               0 : SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
   19432                 :   static const char zSpaces[] = "                             ";
   19433               0 :   while( N>=(int)sizeof(zSpaces)-1 ){
   19434               0 :     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
   19435               0 :     N -= sizeof(zSpaces)-1;
   19436                 :   }
   19437               0 :   if( N>0 ){
   19438               0 :     sqlite3StrAccumAppend(pAccum, zSpaces, N);
   19439                 :   }
   19440               0 : }
   19441                 : 
   19442                 : /*
   19443                 : ** On machines with a small stack size, you can redefine the
   19444                 : ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
   19445                 : */
   19446                 : #ifndef SQLITE_PRINT_BUF_SIZE
   19447                 : # define SQLITE_PRINT_BUF_SIZE 70
   19448                 : #endif
   19449                 : #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
   19450                 : 
   19451                 : /*
   19452                 : ** Render a string given by "fmt" into the StrAccum object.
   19453                 : */
   19454         1417686 : SQLITE_PRIVATE void sqlite3VXPrintf(
   19455                 :   StrAccum *pAccum,                  /* Accumulate results here */
   19456                 :   int useExtended,                   /* Allow extended %-conversions */
   19457                 :   const char *fmt,                   /* Format string */
   19458                 :   va_list ap                         /* arguments */
   19459                 : ){
   19460                 :   int c;                     /* Next character in the format string */
   19461                 :   char *bufpt;               /* Pointer to the conversion buffer */
   19462                 :   int precision;             /* Precision of the current field */
   19463                 :   int length;                /* Length of the field */
   19464                 :   int idx;                   /* A general purpose loop counter */
   19465                 :   int width;                 /* Width of the current field */
   19466                 :   etByte flag_leftjustify;   /* True if "-" flag is present */
   19467                 :   etByte flag_plussign;      /* True if "+" flag is present */
   19468                 :   etByte flag_blanksign;     /* True if " " flag is present */
   19469                 :   etByte flag_alternateform; /* True if "#" flag is present */
   19470                 :   etByte flag_altform2;      /* True if "!" flag is present */
   19471                 :   etByte flag_zeropad;       /* True if field width constant starts with zero */
   19472                 :   etByte flag_long;          /* True if "l" flag is present */
   19473                 :   etByte flag_longlong;      /* True if the "ll" flag is present */
   19474                 :   etByte done;               /* Loop termination flag */
   19475         1417686 :   etByte xtype = 0;          /* Conversion paradigm */
   19476                 :   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
   19477                 :   sqlite_uint64 longvalue;   /* Value for integer types */
   19478                 :   LONGDOUBLE_TYPE realvalue; /* Value for real types */
   19479                 :   const et_info *infop;      /* Pointer to the appropriate info structure */
   19480                 :   char *zOut;                /* Rendering buffer */
   19481                 :   int nOut;                  /* Size of the rendering buffer */
   19482                 :   char *zExtra;              /* Malloced memory used by some conversion */
   19483                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   19484                 :   int  exp, e2;              /* exponent of real numbers */
   19485                 :   int nsd;                   /* Number of significant digits returned */
   19486                 :   double rounder;            /* Used for rounding floating point values */
   19487                 :   etByte flag_dp;            /* True if decimal point should be shown */
   19488                 :   etByte flag_rtz;           /* True if trailing zeros should be removed */
   19489                 : #endif
   19490                 :   char buf[etBUFSIZE];       /* Conversion buffer */
   19491                 : 
   19492         1417686 :   bufpt = 0;
   19493         3445653 :   for(; (c=(*fmt))!=0; ++fmt){
   19494         2308933 :     if( c!='%' ){
   19495                 :       int amt;
   19496         1209662 :       bufpt = (char *)fmt;
   19497         1209662 :       amt = 1;
   19498         1209662 :       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
   19499         1209662 :       sqlite3StrAccumAppend(pAccum, bufpt, amt);
   19500         1209661 :       if( c==0 ) break;
   19501                 :     }
   19502         2027966 :     if( (c=(*++fmt))==0 ){
   19503               0 :       sqlite3StrAccumAppend(pAccum, "%", 1);
   19504               0 :       break;
   19505                 :     }
   19506                 :     /* Find out what flags are present */
   19507         2027966 :     flag_leftjustify = flag_plussign = flag_blanksign = 
   19508         2027966 :      flag_alternateform = flag_altform2 = flag_zeropad = 0;
   19509         2027966 :     done = 0;
   19510                 :     do{
   19511         2190496 :       switch( c ){
   19512               0 :         case '-':   flag_leftjustify = 1;     break;
   19513               0 :         case '+':   flag_plussign = 1;        break;
   19514               0 :         case ' ':   flag_blanksign = 1;       break;
   19515               0 :         case '#':   flag_alternateform = 1;   break;
   19516           12826 :         case '!':   flag_altform2 = 1;        break;
   19517          149703 :         case '0':   flag_zeropad = 1;         break;
   19518         2027967 :         default:    done = 1;                 break;
   19519                 :       }
   19520         2190496 :     }while( !done && (c=(*++fmt))!=0 );
   19521                 :     /* Get the field width */
   19522         2027966 :     width = 0;
   19523         2027966 :     if( c=='*' ){
   19524               0 :       width = va_arg(ap,int);
   19525               0 :       if( width<0 ){
   19526               0 :         flag_leftjustify = 1;
   19527               0 :         width = -width;
   19528                 :       }
   19529               0 :       c = *++fmt;
   19530                 :     }else{
   19531         4205637 :       while( c>='0' && c<='9' ){
   19532          149703 :         width = width*10 + c - '0';
   19533          149703 :         c = *++fmt;
   19534                 :       }
   19535                 :     }
   19536                 :     /* Get the precision */
   19537         2027966 :     if( c=='.' ){
   19538          175065 :       precision = 0;
   19539          175065 :       c = *++fmt;
   19540          175065 :       if( c=='*' ){
   19541          162239 :         precision = va_arg(ap,int);
   19542          162239 :         if( precision<0 ) precision = -precision;
   19543          162239 :         c = *++fmt;
   19544                 :       }else{
   19545           51304 :         while( c>='0' && c<='9' ){
   19546           25652 :           precision = precision*10 + c - '0';
   19547           25652 :           c = *++fmt;
   19548                 :         }
   19549                 :       }
   19550                 :     }else{
   19551         1852901 :       precision = -1;
   19552                 :     }
   19553                 :     /* Get the conversion type modifier */
   19554         2027966 :     if( c=='l' ){
   19555          303585 :       flag_long = 1;
   19556          303585 :       c = *++fmt;
   19557          303585 :       if( c=='l' ){
   19558          303585 :         flag_longlong = 1;
   19559          303585 :         c = *++fmt;
   19560                 :       }else{
   19561               0 :         flag_longlong = 0;
   19562                 :       }
   19563                 :     }else{
   19564         1724381 :       flag_long = flag_longlong = 0;
   19565                 :     }
   19566                 :     /* Fetch the info entry for the field */
   19567         2027966 :     infop = &fmtinfo[0];
   19568         2027966 :     xtype = etINVALID;
   19569         5919886 :     for(idx=0; idx<ArraySize(fmtinfo); idx++){
   19570         5919887 :       if( c==fmtinfo[idx].fmttype ){
   19571         2027967 :         infop = &fmtinfo[idx];
   19572         2027967 :         if( useExtended || (infop->flags & FLAG_INTERN)==0 ){
   19573         2027967 :           xtype = infop->type;
   19574                 :         }else{
   19575               0 :           return;
   19576                 :         }
   19577         2027967 :         break;
   19578                 :       }
   19579                 :     }
   19580         2027966 :     zExtra = 0;
   19581                 : 
   19582                 :     /*
   19583                 :     ** At this point, variables are initialized as follows:
   19584                 :     **
   19585                 :     **   flag_alternateform          TRUE if a '#' is present.
   19586                 :     **   flag_altform2               TRUE if a '!' is present.
   19587                 :     **   flag_plussign               TRUE if a '+' is present.
   19588                 :     **   flag_leftjustify            TRUE if a '-' is present or if the
   19589                 :     **                               field width was negative.
   19590                 :     **   flag_zeropad                TRUE if the width began with 0.
   19591                 :     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
   19592                 :     **                               the conversion character.
   19593                 :     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
   19594                 :     **                               the conversion character.
   19595                 :     **   flag_blanksign              TRUE if a ' ' is present.
   19596                 :     **   width                       The specified field width.  This is
   19597                 :     **                               always non-negative.  Zero is the default.
   19598                 :     **   precision                   The specified precision.  The default
   19599                 :     **                               is -1.
   19600                 :     **   xtype                       The class of the conversion.
   19601                 :     **   infop                       Pointer to the appropriate info struct.
   19602                 :     */
   19603         2027966 :     switch( xtype ){
   19604                 :       case etPOINTER:
   19605             122 :         flag_longlong = sizeof(char*)==sizeof(i64);
   19606             122 :         flag_long = sizeof(char*)==sizeof(long int);
   19607                 :         /* Fall through into the next case */
   19608                 :       case etORDINAL:
   19609                 :       case etRADIX:
   19610          522090 :         if( infop->flags & FLAG_SIGNED ){
   19611                 :           i64 v;
   19612          372457 :           if( flag_longlong ){
   19613          303586 :             v = va_arg(ap,i64);
   19614           68871 :           }else if( flag_long ){
   19615               0 :             v = va_arg(ap,long int);
   19616                 :           }else{
   19617           68871 :             v = va_arg(ap,int);
   19618                 :           }
   19619          372457 :           if( v<0 ){
   19620            3543 :             if( v==SMALLEST_INT64 ){
   19621               0 :               longvalue = ((u64)1)<<63;
   19622                 :             }else{
   19623            3543 :               longvalue = -v;
   19624                 :             }
   19625            3543 :             prefix = '-';
   19626                 :           }else{
   19627          368914 :             longvalue = v;
   19628          368914 :             if( flag_plussign )        prefix = '+';
   19629          368914 :             else if( flag_blanksign )  prefix = ' ';
   19630          368914 :             else                       prefix = 0;
   19631                 :           }
   19632                 :         }else{
   19633          149633 :           if( flag_longlong ){
   19634               0 :             longvalue = va_arg(ap,u64);
   19635          149633 :           }else if( flag_long ){
   19636             122 :             longvalue = va_arg(ap,unsigned long int);
   19637                 :           }else{
   19638          149511 :             longvalue = va_arg(ap,unsigned int);
   19639                 :           }
   19640          149633 :           prefix = 0;
   19641                 :         }
   19642          522090 :         if( longvalue==0 ) flag_alternateform = 0;
   19643          522090 :         if( flag_zeropad && precision<width-(prefix!=0) ){
   19644          149703 :           precision = width-(prefix!=0);
   19645                 :         }
   19646          522090 :         if( precision<etBUFSIZE-10 ){
   19647          522090 :           nOut = etBUFSIZE;
   19648          522090 :           zOut = buf;
   19649                 :         }else{
   19650               0 :           nOut = precision + 10;
   19651               0 :           zOut = zExtra = sqlite3Malloc( nOut );
   19652               0 :           if( zOut==0 ){
   19653               0 :             pAccum->mallocFailed = 1;
   19654               0 :             return;
   19655                 :           }
   19656                 :         }
   19657          522090 :         bufpt = &zOut[nOut-1];
   19658          522090 :         if( xtype==etORDINAL ){
   19659                 :           static const char zOrd[] = "thstndrd";
   19660               0 :           int x = (int)(longvalue % 10);
   19661               0 :           if( x>=4 || (longvalue/10)%10==1 ){
   19662               0 :             x = 0;
   19663                 :           }
   19664               0 :           *(--bufpt) = zOrd[x*2+1];
   19665               0 :           *(--bufpt) = zOrd[x*2];
   19666                 :         }
   19667                 :         {
   19668                 :           register const char *cset;      /* Use registers for speed */
   19669                 :           register int base;
   19670          522090 :           cset = &aDigits[infop->charset];
   19671          522090 :           base = infop->base;
   19672                 :           do{                                           /* Convert to ascii */
   19673         1694709 :             *(--bufpt) = cset[longvalue%base];
   19674         1694709 :             longvalue = longvalue/base;
   19675         1694709 :           }while( longvalue>0 );
   19676                 :         }
   19677          522090 :         length = (int)(&zOut[nOut-1]-bufpt);
   19678          572827 :         for(idx=precision-length; idx>0; idx--){
   19679           50737 :           *(--bufpt) = '0';                             /* Zero pad */
   19680                 :         }
   19681          522090 :         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
   19682          522090 :         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
   19683                 :           const char *pre;
   19684                 :           char x;
   19685               0 :           pre = &aPrefix[infop->prefix];
   19686               0 :           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
   19687                 :         }
   19688          522090 :         length = (int)(&zOut[nOut-1]-bufpt);
   19689          522090 :         break;
   19690                 :       case etFLOAT:
   19691                 :       case etEXP:
   19692                 :       case etGENERIC:
   19693           13274 :         realvalue = va_arg(ap,double);
   19694                 : #ifdef SQLITE_OMIT_FLOATING_POINT
   19695                 :         length = 0;
   19696                 : #else
   19697           13274 :         if( precision<0 ) precision = 6;         /* Set default precision */
   19698           13274 :         if( realvalue<0.0 ){
   19699               4 :           realvalue = -realvalue;
   19700               4 :           prefix = '-';
   19701                 :         }else{
   19702           13270 :           if( flag_plussign )          prefix = '+';
   19703           13270 :           else if( flag_blanksign )    prefix = ' ';
   19704           13270 :           else                         prefix = 0;
   19705                 :         }
   19706           13274 :         if( xtype==etGENERIC && precision>0 ) precision--;
   19707                 : #if 0
   19708                 :         /* Rounding works like BSD when the constant 0.4999 is used.  Wierd! */
   19709                 :         for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
   19710                 : #else
   19711                 :         /* It makes more sense to use 0.5 */
   19712           13274 :         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
   19713                 : #endif
   19714           13274 :         if( xtype==etFLOAT ) realvalue += rounder;
   19715                 :         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
   19716           13274 :         exp = 0;
   19717           13274 :         if( sqlite3IsNaN((double)realvalue) ){
   19718               0 :           bufpt = "NaN";
   19719               0 :           length = 3;
   19720               0 :           break;
   19721                 :         }
   19722           13274 :         if( realvalue>0.0 ){
   19723           13273 :           while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
   19724           13273 :           while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
   19725           13273 :           while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
   19726           13273 :           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
   19727           13273 :           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
   19728           13273 :           if( exp>350 ){
   19729               0 :             if( prefix=='-' ){
   19730               0 :               bufpt = "-Inf";
   19731               0 :             }else if( prefix=='+' ){
   19732               0 :               bufpt = "+Inf";
   19733                 :             }else{
   19734               0 :               bufpt = "Inf";
   19735                 :             }
   19736               0 :             length = sqlite3Strlen30(bufpt);
   19737               0 :             break;
   19738                 :           }
   19739                 :         }
   19740           13274 :         bufpt = buf;
   19741                 :         /*
   19742                 :         ** If the field type is etGENERIC, then convert to either etEXP
   19743                 :         ** or etFLOAT, as appropriate.
   19744                 :         */
   19745           13274 :         if( xtype!=etFLOAT ){
   19746           12826 :           realvalue += rounder;
   19747           12826 :           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
   19748                 :         }
   19749           13274 :         if( xtype==etGENERIC ){
   19750           12826 :           flag_rtz = !flag_alternateform;
   19751           12826 :           if( exp<-4 || exp>precision ){
   19752            2631 :             xtype = etEXP;
   19753                 :           }else{
   19754           10195 :             precision = precision - exp;
   19755           10195 :             xtype = etFLOAT;
   19756                 :           }
   19757                 :         }else{
   19758             448 :           flag_rtz = 0;
   19759                 :         }
   19760           13274 :         if( xtype==etEXP ){
   19761            2631 :           e2 = 0;
   19762                 :         }else{
   19763           10643 :           e2 = exp;
   19764                 :         }
   19765           13274 :         if( e2+precision+width > etBUFSIZE - 15 ){
   19766               0 :           bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
   19767               0 :           if( bufpt==0 ){
   19768               0 :             pAccum->mallocFailed = 1;
   19769               0 :             return;
   19770                 :           }
   19771                 :         }
   19772           13274 :         zOut = bufpt;
   19773           13274 :         nsd = 0;
   19774           13274 :         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
   19775                 :         /* The sign in front of the number */
   19776           13274 :         if( prefix ){
   19777               4 :           *(bufpt++) = prefix;
   19778                 :         }
   19779                 :         /* Digits prior to the decimal point */
   19780           13274 :         if( e2<0 ){
   19781               0 :           *(bufpt++) = '0';
   19782                 :         }else{
   19783           86829 :           for(; e2>=0; e2--){
   19784           73555 :             *(bufpt++) = et_getdigit(&realvalue,&nsd);
   19785                 :           }
   19786                 :         }
   19787                 :         /* The decimal point */
   19788           13274 :         if( flag_dp ){
   19789           13274 :           *(bufpt++) = '.';
   19790                 :         }
   19791                 :         /* "0" digits after the decimal point but before the first
   19792                 :         ** significant digit of the number */
   19793           13274 :         for(e2++; e2<0; precision--, e2++){
   19794               0 :           assert( precision>0 );
   19795               0 :           *(bufpt++) = '0';
   19796                 :         }
   19797                 :         /* Significant digits after the decimal point */
   19798          148375 :         while( (precision--)>0 ){
   19799          121827 :           *(bufpt++) = et_getdigit(&realvalue,&nsd);
   19800                 :         }
   19801                 :         /* Remove trailing zeros and the "." if no digits follow the "." */
   19802           13274 :         if( flag_rtz && flag_dp ){
   19803           12826 :           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
   19804           12826 :           assert( bufpt>zOut );
   19805           12826 :           if( bufpt[-1]=='.' ){
   19806           10122 :             if( flag_altform2 ){
   19807           10122 :               *(bufpt++) = '0';
   19808                 :             }else{
   19809               0 :               *(--bufpt) = 0;
   19810                 :             }
   19811                 :           }
   19812                 :         }
   19813                 :         /* Add the "eNNN" suffix */
   19814           13274 :         if( xtype==etEXP ){
   19815            2631 :           *(bufpt++) = aDigits[infop->charset];
   19816            2631 :           if( exp<0 ){
   19817               4 :             *(bufpt++) = '-'; exp = -exp;
   19818                 :           }else{
   19819            2627 :             *(bufpt++) = '+';
   19820                 :           }
   19821            2631 :           if( exp>=100 ){
   19822               0 :             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
   19823               0 :             exp %= 100;
   19824                 :           }
   19825            2631 :           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
   19826            2631 :           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
   19827                 :         }
   19828           13274 :         *bufpt = 0;
   19829                 : 
   19830                 :         /* The converted number is in buf[] and zero terminated. Output it.
   19831                 :         ** Note that the number is in the usual order, not reversed as with
   19832                 :         ** integer conversions. */
   19833           13274 :         length = (int)(bufpt-zOut);
   19834           13274 :         bufpt = zOut;
   19835                 : 
   19836                 :         /* Special case:  Add leading zeros if the flag_zeropad flag is
   19837                 :         ** set and we are not left justified */
   19838           13274 :         if( flag_zeropad && !flag_leftjustify && length < width){
   19839                 :           int i;
   19840               0 :           int nPad = width - length;
   19841               0 :           for(i=width; i>=nPad; i--){
   19842               0 :             bufpt[i] = bufpt[i-nPad];
   19843                 :           }
   19844               0 :           i = prefix!=0;
   19845               0 :           while( nPad-- ) bufpt[i++] = '0';
   19846               0 :           length = width;
   19847                 :         }
   19848                 : #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
   19849           13274 :         break;
   19850                 :       case etSIZE:
   19851               0 :         *(va_arg(ap,int*)) = pAccum->nChar;
   19852               0 :         length = width = 0;
   19853               0 :         break;
   19854                 :       case etPERCENT:
   19855               0 :         buf[0] = '%';
   19856               0 :         bufpt = buf;
   19857               0 :         length = 1;
   19858               0 :         break;
   19859                 :       case etCHARX:
   19860               0 :         c = va_arg(ap,int);
   19861               0 :         buf[0] = (char)c;
   19862               0 :         if( precision>=0 ){
   19863               0 :           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
   19864               0 :           length = precision;
   19865                 :         }else{
   19866               0 :           length =1;
   19867                 :         }
   19868               0 :         bufpt = buf;
   19869               0 :         break;
   19870                 :       case etSTRING:
   19871                 :       case etDYNSTRING:
   19872         1228811 :         bufpt = va_arg(ap,char*);
   19873         1228811 :         if( bufpt==0 ){
   19874             462 :           bufpt = "";
   19875         1228349 :         }else if( xtype==etDYNSTRING ){
   19876               3 :           zExtra = bufpt;
   19877                 :         }
   19878         1228811 :         if( precision>=0 ){
   19879           12754 :           for(length=0; length<precision && bufpt[length]; length++){}
   19880                 :         }else{
   19881         1216057 :           length = sqlite3Strlen30(bufpt);
   19882                 :         }
   19883         1228812 :         break;
   19884                 :       case etSQLESCAPE:
   19885                 :       case etSQLESCAPE2:
   19886                 :       case etSQLESCAPE3: {
   19887                 :         int i, j, k, n, isnull;
   19888                 :         int needQuote;
   19889                 :         char ch;
   19890          263698 :         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
   19891          263698 :         char *escarg = va_arg(ap,char*);
   19892          263698 :         isnull = escarg==0;
   19893          263698 :         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
   19894          263698 :         k = precision;
   19895         5552834 :         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
   19896         5289136 :           if( ch==q )  n++;
   19897                 :         }
   19898          263698 :         needQuote = !isnull && xtype==etSQLESCAPE2;
   19899          263698 :         n += i + 1 + needQuote*2;
   19900          263698 :         if( n>etBUFSIZE ){
   19901           13678 :           bufpt = zExtra = sqlite3Malloc( n );
   19902           13678 :           if( bufpt==0 ){
   19903               0 :             pAccum->mallocFailed = 1;
   19904               0 :             return;
   19905                 :           }
   19906                 :         }else{
   19907          250020 :           bufpt = buf;
   19908                 :         }
   19909          263698 :         j = 0;
   19910          263698 :         if( needQuote ) bufpt[j++] = q;
   19911          263698 :         k = i;
   19912         5552834 :         for(i=0; i<k; i++){
   19913         5289136 :           bufpt[j++] = ch = escarg[i];
   19914         5289136 :           if( ch==q ) bufpt[j++] = ch;
   19915                 :         }
   19916          263698 :         if( needQuote ) bufpt[j++] = q;
   19917          263698 :         bufpt[j] = 0;
   19918          263698 :         length = j;
   19919                 :         /* The precision in %q and %Q means how many input characters to
   19920                 :         ** consume, not the length of the output...
   19921                 :         ** if( precision>=0 && precision<length ) length = precision; */
   19922          263698 :         break;
   19923                 :       }
   19924                 :       case etTOKEN: {
   19925              93 :         Token *pToken = va_arg(ap, Token*);
   19926              93 :         if( pToken ){
   19927              93 :           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
   19928                 :         }
   19929              93 :         length = width = 0;
   19930              93 :         break;
   19931                 :       }
   19932                 :       case etSRCLIST: {
   19933               0 :         SrcList *pSrc = va_arg(ap, SrcList*);
   19934               0 :         int k = va_arg(ap, int);
   19935               0 :         struct SrcList_item *pItem = &pSrc->a[k];
   19936               0 :         assert( k>=0 && k<pSrc->nSrc );
   19937               0 :         if( pItem->zDatabase ){
   19938               0 :           sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
   19939               0 :           sqlite3StrAccumAppend(pAccum, ".", 1);
   19940                 :         }
   19941               0 :         sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
   19942               0 :         length = width = 0;
   19943               0 :         break;
   19944                 :       }
   19945                 :       default: {
   19946               0 :         assert( xtype==etINVALID );
   19947               0 :         return;
   19948                 :       }
   19949                 :     }/* End switch over the format type */
   19950                 :     /*
   19951                 :     ** The text of the conversion is pointed to by "bufpt" and is
   19952                 :     ** "length" characters long.  The field width is "width".  Do
   19953                 :     ** the output.
   19954                 :     */
   19955         2027967 :     if( !flag_leftjustify ){
   19956                 :       register int nspace;
   19957         2027966 :       nspace = width-length;
   19958         2027966 :       if( nspace>0 ){
   19959               0 :         sqlite3AppendSpace(pAccum, nspace);
   19960                 :       }
   19961                 :     }
   19962         2027966 :     if( length>0 ){
   19963         2015246 :       sqlite3StrAccumAppend(pAccum, bufpt, length);
   19964                 :     }
   19965         2027967 :     if( flag_leftjustify ){
   19966                 :       register int nspace;
   19967               0 :       nspace = width-length;
   19968               0 :       if( nspace>0 ){
   19969               0 :         sqlite3AppendSpace(pAccum, nspace);
   19970                 :       }
   19971                 :     }
   19972         2027967 :     sqlite3_free(zExtra);
   19973                 :   }/* End for loop over the format string */
   19974                 : } /* End of function */
   19975                 : 
   19976                 : /*
   19977                 : ** Append N bytes of text from z to the StrAccum object.
   19978                 : */
   19979         3894981 : SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
   19980         3894981 :   assert( z!=0 || N==0 );
   19981         3894981 :   if( p->tooBig | p->mallocFailed ){
   19982                 :     testcase(p->tooBig);
   19983                 :     testcase(p->mallocFailed);
   19984               0 :     return;
   19985                 :   }
   19986         3894981 :   assert( p->zText!=0 || p->nChar==0 );
   19987         3894981 :   if( N<0 ){
   19988           24831 :     N = sqlite3Strlen30(z);
   19989                 :   }
   19990         3894983 :   if( N==0 || NEVER(z==0) ){
   19991               0 :     return;
   19992                 :   }
   19993         3894983 :   if( p->nChar+N >= p->nAlloc ){
   19994                 :     char *zNew;
   19995         1275180 :     if( !p->useMalloc ){
   19996               0 :       p->tooBig = 1;
   19997               0 :       N = p->nAlloc - p->nChar - 1;
   19998               0 :       if( N<=0 ){
   19999               0 :         return;
   20000                 :       }
   20001                 :     }else{
   20002         1275180 :       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   20003         1275180 :       i64 szNew = p->nChar;
   20004         1275180 :       szNew += N + 1;
   20005         1275180 :       if( szNew > p->mxAlloc ){
   20006               0 :         sqlite3StrAccumReset(p);
   20007               0 :         p->tooBig = 1;
   20008               0 :         return;
   20009                 :       }else{
   20010         1275180 :         p->nAlloc = (int)szNew;
   20011                 :       }
   20012         1275180 :       if( p->useMalloc==1 ){
   20013         1273611 :         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   20014                 :       }else{
   20015            1569 :         zNew = sqlite3_realloc(zOld, p->nAlloc);
   20016                 :       }
   20017         1275178 :       if( zNew ){
   20018         1275178 :         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
   20019         1275178 :         p->zText = zNew;
   20020                 :       }else{
   20021               0 :         p->mallocFailed = 1;
   20022               0 :         sqlite3StrAccumReset(p);
   20023               0 :         return;
   20024                 :       }
   20025                 :     }
   20026                 :   }
   20027         3894981 :   assert( p->zText );
   20028         3894981 :   memcpy(&p->zText[p->nChar], z, N);
   20029         3894981 :   p->nChar += N;
   20030                 : }
   20031                 : 
   20032                 : /*
   20033                 : ** Finish off a string by making sure it is zero-terminated.
   20034                 : ** Return a pointer to the resulting string.  Return a NULL
   20035                 : ** pointer if any kind of error was encountered.
   20036                 : */
   20037         1087184 : SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
   20038         1087184 :   if( p->zText ){
   20039         1087132 :     p->zText[p->nChar] = 0;
   20040         1087132 :     if( p->useMalloc && p->zText==p->zBase ){
   20041          855862 :       if( p->useMalloc==1 ){
   20042          855393 :         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   20043                 :       }else{
   20044             469 :         p->zText = sqlite3_malloc(p->nChar+1);
   20045                 :       }
   20046          855862 :       if( p->zText ){
   20047          855862 :         memcpy(p->zText, p->zBase, p->nChar+1);
   20048                 :       }else{
   20049               0 :         p->mallocFailed = 1;
   20050                 :       }
   20051                 :     }
   20052                 :   }
   20053         1087184 :   return p->zText;
   20054                 : }
   20055                 : 
   20056                 : /*
   20057                 : ** Reset an StrAccum string.  Reclaim all malloced memory.
   20058                 : */
   20059              52 : SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
   20060              52 :   if( p->zText!=p->zBase ){
   20061               0 :     if( p->useMalloc==1 ){
   20062               0 :       sqlite3DbFree(p->db, p->zText);
   20063                 :     }else{
   20064               0 :       sqlite3_free(p->zText);
   20065                 :     }
   20066                 :   }
   20067              52 :   p->zText = 0;
   20068              52 : }
   20069                 : 
   20070                 : /*
   20071                 : ** Initialize a string accumulator
   20072                 : */
   20073         1085971 : SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
   20074         1085971 :   p->zText = p->zBase = zBase;
   20075         1085971 :   p->db = 0;
   20076         1085971 :   p->nChar = 0;
   20077         1085971 :   p->nAlloc = n;
   20078         1085971 :   p->mxAlloc = mx;
   20079         1085971 :   p->useMalloc = 1;
   20080         1085971 :   p->tooBig = 0;
   20081         1085971 :   p->mallocFailed = 0;
   20082         1085971 : }
   20083                 : 
   20084                 : /*
   20085                 : ** Print into memory obtained from sqliteMalloc().  Use the internal
   20086                 : ** %-conversion extensions.
   20087                 : */
   20088          787980 : SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
   20089                 :   char *z;
   20090                 :   char zBase[SQLITE_PRINT_BUF_SIZE];
   20091                 :   StrAccum acc;
   20092          787980 :   assert( db!=0 );
   20093          787980 :   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
   20094                 :                       db->aLimit[SQLITE_LIMIT_LENGTH]);
   20095          787980 :   acc.db = db;
   20096          787980 :   sqlite3VXPrintf(&acc, 1, zFormat, ap);
   20097          787980 :   z = sqlite3StrAccumFinish(&acc);
   20098          787980 :   if( acc.mallocFailed ){
   20099               0 :     db->mallocFailed = 1;
   20100                 :   }
   20101          787980 :   return z;
   20102                 : }
   20103                 : 
   20104                 : /*
   20105                 : ** Print into memory obtained from sqliteMalloc().  Use the internal
   20106                 : ** %-conversion extensions.
   20107                 : */
   20108           78938 : SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
   20109                 :   va_list ap;
   20110                 :   char *z;
   20111           78938 :   va_start(ap, zFormat);
   20112           78938 :   z = sqlite3VMPrintf(db, zFormat, ap);
   20113           78938 :   va_end(ap);
   20114           78938 :   return z;
   20115                 : }
   20116                 : 
   20117                 : /*
   20118                 : ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
   20119                 : ** the string and before returnning.  This routine is intended to be used
   20120                 : ** to modify an existing string.  For example:
   20121                 : **
   20122                 : **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
   20123                 : **
   20124                 : */
   20125               0 : SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
   20126                 :   va_list ap;
   20127                 :   char *z;
   20128               0 :   va_start(ap, zFormat);
   20129               0 :   z = sqlite3VMPrintf(db, zFormat, ap);
   20130               0 :   va_end(ap);
   20131               0 :   sqlite3DbFree(db, zStr);
   20132               0 :   return z;
   20133                 : }
   20134                 : 
   20135                 : /*
   20136                 : ** Print into memory obtained from sqlite3_malloc().  Omit the internal
   20137                 : ** %-conversion extensions.
   20138                 : */
   20139             479 : SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
   20140                 :   char *z;
   20141                 :   char zBase[SQLITE_PRINT_BUF_SIZE];
   20142                 :   StrAccum acc;
   20143                 : #ifndef SQLITE_OMIT_AUTOINIT
   20144             479 :   if( sqlite3_initialize() ) return 0;
   20145                 : #endif
   20146             479 :   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
   20147             479 :   acc.useMalloc = 2;
   20148             479 :   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   20149             479 :   z = sqlite3StrAccumFinish(&acc);
   20150             479 :   return z;
   20151                 : }
   20152                 : 
   20153                 : /*
   20154                 : ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
   20155                 : ** %-conversion extensions.
   20156                 : */
   20157             469 : SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
   20158                 :   va_list ap;
   20159                 :   char *z;
   20160                 : #ifndef SQLITE_OMIT_AUTOINIT
   20161             469 :   if( sqlite3_initialize() ) return 0;
   20162                 : #endif
   20163             469 :   va_start(ap, zFormat);
   20164             469 :   z = sqlite3_vmprintf(zFormat, ap);
   20165             469 :   va_end(ap);
   20166             469 :   return z;
   20167                 : }
   20168                 : 
   20169                 : /*
   20170                 : ** sqlite3_snprintf() works like snprintf() except that it ignores the
   20171                 : ** current locale settings.  This is important for SQLite because we
   20172                 : ** are not able to use a "," as the decimal point in place of "." as
   20173                 : ** specified by some locales.
   20174                 : **
   20175                 : ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
   20176                 : ** from the snprintf() standard.  Unfortunately, it is too late to change
   20177                 : ** this without breaking compatibility, so we just have to live with the
   20178                 : ** mistake.
   20179                 : **
   20180                 : ** sqlite3_vsnprintf() is the varargs version.
   20181                 : */
   20182           86666 : SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
   20183                 :   StrAccum acc;
   20184           86666 :   if( n<=0 ) return zBuf;
   20185           86666 :   sqlite3StrAccumInit(&acc, zBuf, n, 0);
   20186           86666 :   acc.useMalloc = 0;
   20187           86666 :   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   20188           86666 :   return sqlite3StrAccumFinish(&acc);
   20189                 : }
   20190           86666 : SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
   20191                 :   char *z;
   20192                 :   va_list ap;
   20193           86666 :   va_start(ap,zFormat);
   20194           86666 :   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
   20195           86666 :   va_end(ap);
   20196           86666 :   return z;
   20197                 : }
   20198                 : 
   20199                 : /*
   20200                 : ** This is the routine that actually formats the sqlite3_log() message.
   20201                 : ** We house it in a separate routine from sqlite3_log() to avoid using
   20202                 : ** stack space on small-stack systems when logging is disabled.
   20203                 : **
   20204                 : ** sqlite3_log() must render into a static buffer.  It cannot dynamically
   20205                 : ** allocate memory because it might be called while the memory allocator
   20206                 : ** mutex is held.
   20207                 : */
   20208               0 : static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
   20209                 :   StrAccum acc;                          /* String accumulator */
   20210                 :   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
   20211                 : 
   20212               0 :   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
   20213               0 :   acc.useMalloc = 0;
   20214               0 :   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   20215               0 :   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
   20216               0 :                            sqlite3StrAccumFinish(&acc));
   20217               0 : }
   20218                 : 
   20219                 : /*
   20220                 : ** Format and write a message to the log if logging is enabled.
   20221                 : */
   20222             757 : SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
   20223                 :   va_list ap;                             /* Vararg list */
   20224             757 :   if( sqlite3GlobalConfig.xLog ){
   20225               0 :     va_start(ap, zFormat);
   20226               0 :     renderLogMsg(iErrCode, zFormat, ap);
   20227               0 :     va_end(ap);
   20228                 :   }
   20229             757 : }
   20230                 : 
   20231                 : #if defined(SQLITE_DEBUG)
   20232                 : /*
   20233                 : ** A version of printf() that understands %lld.  Used for debugging.
   20234                 : ** The printf() built into some versions of windows does not understand %lld
   20235                 : ** and segfaults if you give it a long long int.
   20236                 : */
   20237               0 : SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
   20238                 :   va_list ap;
   20239                 :   StrAccum acc;
   20240                 :   char zBuf[500];
   20241               0 :   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
   20242               0 :   acc.useMalloc = 0;
   20243               0 :   va_start(ap,zFormat);
   20244               0 :   sqlite3VXPrintf(&acc, 0, zFormat, ap);
   20245               0 :   va_end(ap);
   20246               0 :   sqlite3StrAccumFinish(&acc);
   20247               0 :   fprintf(stdout,"%s", zBuf);
   20248               0 :   fflush(stdout);
   20249               0 : }
   20250                 : #endif
   20251                 : 
   20252                 : #ifndef SQLITE_OMIT_TRACE
   20253                 : /*
   20254                 : ** variable-argument wrapper around sqlite3VXPrintf().
   20255                 : */
   20256          542561 : SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
   20257                 :   va_list ap;
   20258          542561 :   va_start(ap,zFormat);
   20259          542561 :   sqlite3VXPrintf(p, 1, zFormat, ap);
   20260          542560 :   va_end(ap);
   20261          542560 : }
   20262                 : #endif
   20263                 : 
   20264                 : /************** End of printf.c **********************************************/
   20265                 : /************** Begin file random.c ******************************************/
   20266                 : /*
   20267                 : ** 2001 September 15
   20268                 : **
   20269                 : ** The author disclaims copyright to this source code.  In place of
   20270                 : ** a legal notice, here is a blessing:
   20271                 : **
   20272                 : **    May you do good and not evil.
   20273                 : **    May you find forgiveness for yourself and forgive others.
   20274                 : **    May you share freely, never taking more than you give.
   20275                 : **
   20276                 : *************************************************************************
   20277                 : ** This file contains code to implement a pseudo-random number
   20278                 : ** generator (PRNG) for SQLite.
   20279                 : **
   20280                 : ** Random numbers are used by some of the database backends in order
   20281                 : ** to generate random integer keys for tables or random filenames.
   20282                 : */
   20283                 : 
   20284                 : 
   20285                 : /* All threads share a single random number generator.
   20286                 : ** This structure is the current state of the generator.
   20287                 : */
   20288                 : static SQLITE_WSD struct sqlite3PrngType {
   20289                 :   unsigned char isInit;          /* True if initialized */
   20290                 :   unsigned char i, j;            /* State variables */
   20291                 :   unsigned char s[256];          /* State variables */
   20292                 : } sqlite3Prng;
   20293                 : 
   20294                 : /*
   20295                 : ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
   20296                 : ** must be held while executing this routine.
   20297                 : **
   20298                 : ** Why not just use a library random generator like lrand48() for this?
   20299                 : ** Because the OP_NewRowid opcode in the VDBE depends on having a very
   20300                 : ** good source of random numbers.  The lrand48() library function may
   20301                 : ** well be good enough.  But maybe not.  Or maybe lrand48() has some
   20302                 : ** subtle problems on some systems that could cause problems.  It is hard
   20303                 : ** to know.  To minimize the risk of problems due to bad lrand48()
   20304                 : ** implementations, SQLite uses this random number generator based
   20305                 : ** on RC4, which we know works very well.
   20306                 : **
   20307                 : ** (Later):  Actually, OP_NewRowid does not depend on a good source of
   20308                 : ** randomness any more.  But we will leave this code in all the same.
   20309                 : */
   20310           75553 : static u8 randomByte(void){
   20311                 :   unsigned char t;
   20312                 : 
   20313                 : 
   20314                 :   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
   20315                 :   ** state vector.  If writable static data is unsupported on the target,
   20316                 :   ** we have to locate the state vector at run-time.  In the more common
   20317                 :   ** case where writable static data is supported, wsdPrng can refer directly
   20318                 :   ** to the "sqlite3Prng" state vector declared above.
   20319                 :   */
   20320                 : #ifdef SQLITE_OMIT_WSD
   20321                 :   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
   20322                 : # define wsdPrng p[0]
   20323                 : #else
   20324                 : # define wsdPrng sqlite3Prng
   20325                 : #endif
   20326                 : 
   20327                 : 
   20328                 :   /* Initialize the state of the random number generator once,
   20329                 :   ** the first time this routine is called.  The seed value does
   20330                 :   ** not need to contain a lot of randomness since we are not
   20331                 :   ** trying to do secure encryption or anything like that...
   20332                 :   **
   20333                 :   ** Nothing in this file or anywhere else in SQLite does any kind of
   20334                 :   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
   20335                 :   ** number generator) not as an encryption device.
   20336                 :   */
   20337           75553 :   if( !wsdPrng.isInit ){
   20338                 :     int i;
   20339                 :     char k[256];
   20340             689 :     wsdPrng.j = 0;
   20341             689 :     wsdPrng.i = 0;
   20342             689 :     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
   20343          177073 :     for(i=0; i<256; i++){
   20344          176384 :       wsdPrng.s[i] = (u8)i;
   20345                 :     }
   20346          177073 :     for(i=0; i<256; i++){
   20347          176384 :       wsdPrng.j += wsdPrng.s[i] + k[i];
   20348          176384 :       t = wsdPrng.s[wsdPrng.j];
   20349          176384 :       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
   20350          176384 :       wsdPrng.s[i] = t;
   20351                 :     }
   20352             689 :     wsdPrng.isInit = 1;
   20353                 :   }
   20354                 : 
   20355                 :   /* Generate and return single random byte
   20356                 :   */
   20357           75553 :   wsdPrng.i++;
   20358           75553 :   t = wsdPrng.s[wsdPrng.i];
   20359           75553 :   wsdPrng.j += t;
   20360           75553 :   wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
   20361           75553 :   wsdPrng.s[wsdPrng.j] = t;
   20362           75553 :   t += wsdPrng.s[wsdPrng.i];
   20363           75553 :   return wsdPrng.s[t];
   20364                 : }
   20365                 : 
   20366                 : /*
   20367                 : ** Return N random bytes.
   20368                 : */
   20369           17054 : SQLITE_API void sqlite3_randomness(int N, void *pBuf){
   20370           17054 :   unsigned char *zBuf = pBuf;
   20371                 : #if SQLITE_THREADSAFE
   20372           17054 :   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
   20373                 : #endif
   20374           17054 :   sqlite3_mutex_enter(mutex);
   20375          109661 :   while( N-- ){
   20376           75553 :     *(zBuf++) = randomByte();
   20377                 :   }
   20378           17054 :   sqlite3_mutex_leave(mutex);
   20379           17054 : }
   20380                 : 
   20381                 : #ifndef SQLITE_OMIT_BUILTIN_TEST
   20382                 : /*
   20383                 : ** For testing purposes, we sometimes want to preserve the state of
   20384                 : ** PRNG and restore the PRNG to its saved state at a later time, or
   20385                 : ** to reset the PRNG to its initial state.  These routines accomplish
   20386                 : ** those tasks.
   20387                 : **
   20388                 : ** The sqlite3_test_control() interface calls these routines to
   20389                 : ** control the PRNG.
   20390                 : */
   20391                 : static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
   20392               0 : SQLITE_PRIVATE void sqlite3PrngSaveState(void){
   20393               0 :   memcpy(
   20394                 :     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
   20395                 :     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
   20396                 :     sizeof(sqlite3Prng)
   20397                 :   );
   20398               0 : }
   20399               0 : SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
   20400               0 :   memcpy(
   20401                 :     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
   20402                 :     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
   20403                 :     sizeof(sqlite3Prng)
   20404                 :   );
   20405               0 : }
   20406               0 : SQLITE_PRIVATE void sqlite3PrngResetState(void){
   20407               0 :   GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
   20408               0 : }
   20409                 : #endif /* SQLITE_OMIT_BUILTIN_TEST */
   20410                 : 
   20411                 : /************** End of random.c **********************************************/
   20412                 : /************** Begin file utf.c *********************************************/
   20413                 : /*
   20414                 : ** 2004 April 13
   20415                 : **
   20416                 : ** The author disclaims copyright to this source code.  In place of
   20417                 : ** a legal notice, here is a blessing:
   20418                 : **
   20419                 : **    May you do good and not evil.
   20420                 : **    May you find forgiveness for yourself and forgive others.
   20421                 : **    May you share freely, never taking more than you give.
   20422                 : **
   20423                 : *************************************************************************
   20424                 : ** This file contains routines used to translate between UTF-8, 
   20425                 : ** UTF-16, UTF-16BE, and UTF-16LE.
   20426                 : **
   20427                 : ** Notes on UTF-8:
   20428                 : **
   20429                 : **   Byte-0    Byte-1    Byte-2    Byte-3    Value
   20430                 : **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
   20431                 : **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
   20432                 : **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
   20433                 : **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
   20434                 : **
   20435                 : **
   20436                 : ** Notes on UTF-16:  (with wwww+1==uuuuu)
   20437                 : **
   20438                 : **      Word-0               Word-1          Value
   20439                 : **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
   20440                 : **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
   20441                 : **
   20442                 : **
   20443                 : ** BOM or Byte Order Mark:
   20444                 : **     0xff 0xfe   little-endian utf-16 follows
   20445                 : **     0xfe 0xff   big-endian utf-16 follows
   20446                 : **
   20447                 : */
   20448                 : /* #include <assert.h> */
   20449                 : 
   20450                 : #ifndef SQLITE_AMALGAMATION
   20451                 : /*
   20452                 : ** The following constant value is used by the SQLITE_BIGENDIAN and
   20453                 : ** SQLITE_LITTLEENDIAN macros.
   20454                 : */
   20455                 : SQLITE_PRIVATE const int sqlite3one = 1;
   20456                 : #endif /* SQLITE_AMALGAMATION */
   20457                 : 
   20458                 : /*
   20459                 : ** This lookup table is used to help decode the first byte of
   20460                 : ** a multi-byte UTF8 character.
   20461                 : */
   20462                 : static const unsigned char sqlite3Utf8Trans1[] = {
   20463                 :   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   20464                 :   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
   20465                 :   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   20466                 :   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
   20467                 :   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   20468                 :   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
   20469                 :   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   20470                 :   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
   20471                 : };
   20472                 : 
   20473                 : 
   20474                 : #define WRITE_UTF8(zOut, c) {                          \
   20475                 :   if( c<0x00080 ){                                     \
   20476                 :     *zOut++ = (u8)(c&0xFF);                            \
   20477                 :   }                                                    \
   20478                 :   else if( c<0x00800 ){                                \
   20479                 :     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
   20480                 :     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
   20481                 :   }                                                    \
   20482                 :   else if( c<0x10000 ){                                \
   20483                 :     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
   20484                 :     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
   20485                 :     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
   20486                 :   }else{                                               \
   20487                 :     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
   20488                 :     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
   20489                 :     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
   20490                 :     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
   20491                 :   }                                                    \
   20492                 : }
   20493                 : 
   20494                 : #define WRITE_UTF16LE(zOut, c) {                                    \
   20495                 :   if( c<=0xFFFF ){                                                  \
   20496                 :     *zOut++ = (u8)(c&0x00FF);                                       \
   20497                 :     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
   20498                 :   }else{                                                            \
   20499                 :     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
   20500                 :     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
   20501                 :     *zOut++ = (u8)(c&0x00FF);                                       \
   20502                 :     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
   20503                 :   }                                                                 \
   20504                 : }
   20505                 : 
   20506                 : #define WRITE_UTF16BE(zOut, c) {                                    \
   20507                 :   if( c<=0xFFFF ){                                                  \
   20508                 :     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
   20509                 :     *zOut++ = (u8)(c&0x00FF);                                       \
   20510                 :   }else{                                                            \
   20511                 :     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
   20512                 :     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
   20513                 :     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
   20514                 :     *zOut++ = (u8)(c&0x00FF);                                       \
   20515                 :   }                                                                 \
   20516                 : }
   20517                 : 
   20518                 : #define READ_UTF16LE(zIn, TERM, c){                                   \
   20519                 :   c = (*zIn++);                                                       \
   20520                 :   c += ((*zIn++)<<8);                                                 \
   20521                 :   if( c>=0xD800 && c<0xE000 && TERM ){                                \
   20522                 :     int c2 = (*zIn++);                                                \
   20523                 :     c2 += ((*zIn++)<<8);                                              \
   20524                 :     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   20525                 :   }                                                                   \
   20526                 : }
   20527                 : 
   20528                 : #define READ_UTF16BE(zIn, TERM, c){                                   \
   20529                 :   c = ((*zIn++)<<8);                                                  \
   20530                 :   c += (*zIn++);                                                      \
   20531                 :   if( c>=0xD800 && c<0xE000 && TERM ){                                \
   20532                 :     int c2 = ((*zIn++)<<8);                                           \
   20533                 :     c2 += (*zIn++);                                                   \
   20534                 :     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
   20535                 :   }                                                                   \
   20536                 : }
   20537                 : 
   20538                 : /*
   20539                 : ** Translate a single UTF-8 character.  Return the unicode value.
   20540                 : **
   20541                 : ** During translation, assume that the byte that zTerm points
   20542                 : ** is a 0x00.
   20543                 : **
   20544                 : ** Write a pointer to the next unread byte back into *pzNext.
   20545                 : **
   20546                 : ** Notes On Invalid UTF-8:
   20547                 : **
   20548                 : **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
   20549                 : **     be encoded as a multi-byte character.  Any multi-byte character that
   20550                 : **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
   20551                 : **
   20552                 : **  *  This routine never allows a UTF16 surrogate value to be encoded.
   20553                 : **     If a multi-byte character attempts to encode a value between
   20554                 : **     0xd800 and 0xe000 then it is rendered as 0xfffd.
   20555                 : **
   20556                 : **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
   20557                 : **     byte of a character are interpreted as single-byte characters
   20558                 : **     and rendered as themselves even though they are technically
   20559                 : **     invalid characters.
   20560                 : **
   20561                 : **  *  This routine accepts an infinite number of different UTF8 encodings
   20562                 : **     for unicode values 0x80 and greater.  It do not change over-length
   20563                 : **     encodings to 0xfffd as some systems recommend.
   20564                 : */
   20565                 : #define READ_UTF8(zIn, zTerm, c)                           \
   20566                 :   c = *(zIn++);                                            \
   20567                 :   if( c>=0xc0 ){                                           \
   20568                 :     c = sqlite3Utf8Trans1[c-0xc0];                         \
   20569                 :     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
   20570                 :       c = (c<<6) + (0x3f & *(zIn++));                      \
   20571                 :     }                                                      \
   20572                 :     if( c<0x80                                             \
   20573                 :         || (c&0xFFFFF800)==0xD800                          \
   20574                 :         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
   20575                 :   }
   20576            2718 : SQLITE_PRIVATE u32 sqlite3Utf8Read(
   20577                 :   const unsigned char *zIn,       /* First byte of UTF-8 character */
   20578                 :   const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
   20579                 : ){
   20580                 :   unsigned int c;
   20581                 : 
   20582                 :   /* Same as READ_UTF8() above but without the zTerm parameter.
   20583                 :   ** For this routine, we assume the UTF8 string is always zero-terminated.
   20584                 :   */
   20585            2718 :   c = *(zIn++);
   20586            2718 :   if( c>=0xc0 ){
   20587               0 :     c = sqlite3Utf8Trans1[c-0xc0];
   20588               0 :     while( (*zIn & 0xc0)==0x80 ){
   20589               0 :       c = (c<<6) + (0x3f & *(zIn++));
   20590                 :     }
   20591               0 :     if( c<0x80
   20592               0 :         || (c&0xFFFFF800)==0xD800
   20593               0 :         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
   20594                 :   }
   20595            2718 :   *pzNext = zIn;
   20596            2718 :   return c;
   20597                 : }
   20598                 : 
   20599                 : 
   20600                 : 
   20601                 : 
   20602                 : /*
   20603                 : ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
   20604                 : ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
   20605                 : */ 
   20606                 : /* #define TRANSLATE_TRACE 1 */
   20607                 : 
   20608                 : #ifndef SQLITE_OMIT_UTF16
   20609                 : /*
   20610                 : ** This routine transforms the internal text encoding used by pMem to
   20611                 : ** desiredEnc. It is an error if the string is already of the desired
   20612                 : ** encoding, or if *pMem does not contain a string value.
   20613                 : */
   20614          239716 : SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
   20615                 :   int len;                    /* Maximum length of output string in bytes */
   20616                 :   unsigned char *zOut;                  /* Output buffer */
   20617                 :   unsigned char *zIn;                   /* Input iterator */
   20618                 :   unsigned char *zTerm;                 /* End of input */
   20619                 :   unsigned char *z;                     /* Output iterator */
   20620                 :   unsigned int c;
   20621                 : 
   20622          239716 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   20623          239716 :   assert( pMem->flags&MEM_Str );
   20624          239716 :   assert( pMem->enc!=desiredEnc );
   20625          239716 :   assert( pMem->enc!=0 );
   20626          239716 :   assert( pMem->n>=0 );
   20627                 : 
   20628                 : #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   20629                 :   {
   20630                 :     char zBuf[100];
   20631                 :     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   20632                 :     fprintf(stderr, "INPUT:  %s\n", zBuf);
   20633                 :   }
   20634                 : #endif
   20635                 : 
   20636                 :   /* If the translation is between UTF-16 little and big endian, then 
   20637                 :   ** all that is required is to swap the byte order. This case is handled
   20638                 :   ** differently from the others.
   20639                 :   */
   20640          239716 :   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
   20641                 :     u8 temp;
   20642                 :     int rc;
   20643               0 :     rc = sqlite3VdbeMemMakeWriteable(pMem);
   20644               0 :     if( rc!=SQLITE_OK ){
   20645               0 :       assert( rc==SQLITE_NOMEM );
   20646               0 :       return SQLITE_NOMEM;
   20647                 :     }
   20648               0 :     zIn = (u8*)pMem->z;
   20649               0 :     zTerm = &zIn[pMem->n&~1];
   20650               0 :     while( zIn<zTerm ){
   20651               0 :       temp = *zIn;
   20652               0 :       *zIn = *(zIn+1);
   20653               0 :       zIn++;
   20654               0 :       *zIn++ = temp;
   20655                 :     }
   20656               0 :     pMem->enc = desiredEnc;
   20657               0 :     goto translate_out;
   20658                 :   }
   20659                 : 
   20660                 :   /* Set len to the maximum number of bytes required in the output buffer. */
   20661          239716 :   if( desiredEnc==SQLITE_UTF8 ){
   20662                 :     /* When converting from UTF-16, the maximum growth results from
   20663                 :     ** translating a 2-byte character to a 4-byte UTF-8 character.
   20664                 :     ** A single byte is required for the output string
   20665                 :     ** nul-terminator.
   20666                 :     */
   20667           75207 :     pMem->n &= ~1;
   20668           75207 :     len = pMem->n * 2 + 1;
   20669                 :   }else{
   20670                 :     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
   20671                 :     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
   20672                 :     ** character. Two bytes are required in the output buffer for the
   20673                 :     ** nul-terminator.
   20674                 :     */
   20675          164509 :     len = pMem->n * 2 + 2;
   20676                 :   }
   20677                 : 
   20678                 :   /* Set zIn to point at the start of the input buffer and zTerm to point 1
   20679                 :   ** byte past the end.
   20680                 :   **
   20681                 :   ** Variable zOut is set to point at the output buffer, space obtained
   20682                 :   ** from sqlite3_malloc().
   20683                 :   */
   20684          239716 :   zIn = (u8*)pMem->z;
   20685          239716 :   zTerm = &zIn[pMem->n];
   20686          239716 :   zOut = sqlite3DbMallocRaw(pMem->db, len);
   20687          239716 :   if( !zOut ){
   20688               0 :     return SQLITE_NOMEM;
   20689                 :   }
   20690          239716 :   z = zOut;
   20691                 : 
   20692          239716 :   if( pMem->enc==SQLITE_UTF8 ){
   20693          164509 :     if( desiredEnc==SQLITE_UTF16LE ){
   20694                 :       /* UTF-8 -> UTF-16 Little-endian */
   20695         2458404 :       while( zIn<zTerm ){
   20696                 :         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
   20697         2129386 :         READ_UTF8(zIn, zTerm, c);
   20698         2129386 :         WRITE_UTF16LE(z, c);
   20699                 :       }
   20700                 :     }else{
   20701               0 :       assert( desiredEnc==SQLITE_UTF16BE );
   20702                 :       /* UTF-8 -> UTF-16 Big-endian */
   20703               0 :       while( zIn<zTerm ){
   20704                 :         /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
   20705               0 :         READ_UTF8(zIn, zTerm, c);
   20706               0 :         WRITE_UTF16BE(z, c);
   20707                 :       }
   20708                 :     }
   20709          164509 :     pMem->n = (int)(z - zOut);
   20710          164509 :     *z++ = 0;
   20711                 :   }else{
   20712           75207 :     assert( desiredEnc==SQLITE_UTF8 );
   20713           75207 :     if( pMem->enc==SQLITE_UTF16LE ){
   20714                 :       /* UTF-16 Little-endian -> UTF-8 */
   20715         1364387 :       while( zIn<zTerm ){
   20716         1213973 :         READ_UTF16LE(zIn, zIn<zTerm, c); 
   20717         1213973 :         WRITE_UTF8(z, c);
   20718                 :       }
   20719                 :     }else{
   20720                 :       /* UTF-16 Big-endian -> UTF-8 */
   20721               0 :       while( zIn<zTerm ){
   20722               0 :         READ_UTF16BE(zIn, zIn<zTerm, c); 
   20723               0 :         WRITE_UTF8(z, c);
   20724                 :       }
   20725                 :     }
   20726           75207 :     pMem->n = (int)(z - zOut);
   20727                 :   }
   20728          239716 :   *z = 0;
   20729          239716 :   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
   20730                 : 
   20731          239716 :   sqlite3VdbeMemRelease(pMem);
   20732          239716 :   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
   20733          239716 :   pMem->enc = desiredEnc;
   20734          239716 :   pMem->flags |= (MEM_Term|MEM_Dyn);
   20735          239716 :   pMem->z = (char*)zOut;
   20736          239716 :   pMem->zMalloc = pMem->z;
   20737                 : 
   20738                 : translate_out:
   20739                 : #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
   20740                 :   {
   20741                 :     char zBuf[100];
   20742                 :     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
   20743                 :     fprintf(stderr, "OUTPUT: %s\n", zBuf);
   20744                 :   }
   20745                 : #endif
   20746          239716 :   return SQLITE_OK;
   20747                 : }
   20748                 : 
   20749                 : /*
   20750                 : ** This routine checks for a byte-order mark at the beginning of the 
   20751                 : ** UTF-16 string stored in *pMem. If one is present, it is removed and
   20752                 : ** the encoding of the Mem adjusted. This routine does not do any
   20753                 : ** byte-swapping, it just sets Mem.enc appropriately.
   20754                 : **
   20755                 : ** The allocation (static, dynamic etc.) and encoding of the Mem may be
   20756                 : ** changed by this function.
   20757                 : */
   20758           75404 : SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
   20759           75404 :   int rc = SQLITE_OK;
   20760           75404 :   u8 bom = 0;
   20761                 : 
   20762           75404 :   assert( pMem->n>=0 );
   20763           75404 :   if( pMem->n>1 ){
   20764           74783 :     u8 b1 = *(u8 *)pMem->z;
   20765           74783 :     u8 b2 = *(((u8 *)pMem->z) + 1);
   20766           74783 :     if( b1==0xFE && b2==0xFF ){
   20767               0 :       bom = SQLITE_UTF16BE;
   20768                 :     }
   20769           74783 :     if( b1==0xFF && b2==0xFE ){
   20770               0 :       bom = SQLITE_UTF16LE;
   20771                 :     }
   20772                 :   }
   20773                 :   
   20774           75404 :   if( bom ){
   20775               0 :     rc = sqlite3VdbeMemMakeWriteable(pMem);
   20776               0 :     if( rc==SQLITE_OK ){
   20777               0 :       pMem->n -= 2;
   20778               0 :       memmove(pMem->z, &pMem->z[2], pMem->n);
   20779               0 :       pMem->z[pMem->n] = '\0';
   20780               0 :       pMem->z[pMem->n+1] = '\0';
   20781               0 :       pMem->flags |= MEM_Term;
   20782               0 :       pMem->enc = bom;
   20783                 :     }
   20784                 :   }
   20785           75404 :   return rc;
   20786                 : }
   20787                 : #endif /* SQLITE_OMIT_UTF16 */
   20788                 : 
   20789                 : /*
   20790                 : ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
   20791                 : ** return the number of unicode characters in pZ up to (but not including)
   20792                 : ** the first 0x00 byte. If nByte is not less than zero, return the
   20793                 : ** number of unicode characters in the first nByte of pZ (or up to 
   20794                 : ** the first 0x00, whichever comes first).
   20795                 : */
   20796           28321 : SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
   20797           28321 :   int r = 0;
   20798           28321 :   const u8 *z = (const u8*)zIn;
   20799                 :   const u8 *zTerm;
   20800           28321 :   if( nByte>=0 ){
   20801           28321 :     zTerm = &z[nByte];
   20802                 :   }else{
   20803               0 :     zTerm = (const u8*)(-1);
   20804                 :   }
   20805           28321 :   assert( z<=zTerm );
   20806         3707186 :   while( *z!=0 && z<zTerm ){
   20807         3650544 :     SQLITE_SKIP_UTF8(z);
   20808         3650544 :     r++;
   20809                 :   }
   20810           28321 :   return r;
   20811                 : }
   20812                 : 
   20813                 : /* This test function is not currently used by the automated test-suite. 
   20814                 : ** Hence it is only available in debug builds.
   20815                 : */
   20816                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   20817                 : /*
   20818                 : ** Translate UTF-8 to UTF-8.
   20819                 : **
   20820                 : ** This has the effect of making sure that the string is well-formed
   20821                 : ** UTF-8.  Miscoded characters are removed.
   20822                 : **
   20823                 : ** The translation is done in-place and aborted if the output
   20824                 : ** overruns the input.
   20825                 : */
   20826                 : SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
   20827                 :   unsigned char *zOut = zIn;
   20828                 :   unsigned char *zStart = zIn;
   20829                 :   u32 c;
   20830                 : 
   20831                 :   while( zIn[0] && zOut<=zIn ){
   20832                 :     c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
   20833                 :     if( c!=0xfffd ){
   20834                 :       WRITE_UTF8(zOut, c);
   20835                 :     }
   20836                 :   }
   20837                 :   *zOut = 0;
   20838                 :   return (int)(zOut - zStart);
   20839                 : }
   20840                 : #endif
   20841                 : 
   20842                 : #ifndef SQLITE_OMIT_UTF16
   20843                 : /*
   20844                 : ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
   20845                 : ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
   20846                 : ** be freed by the calling function.
   20847                 : **
   20848                 : ** NULL is returned if there is an allocation error.
   20849                 : */
   20850               0 : SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
   20851                 :   Mem m;
   20852               0 :   memset(&m, 0, sizeof(m));
   20853               0 :   m.db = db;
   20854               0 :   sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
   20855               0 :   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
   20856               0 :   if( db->mallocFailed ){
   20857               0 :     sqlite3VdbeMemRelease(&m);
   20858               0 :     m.z = 0;
   20859                 :   }
   20860               0 :   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
   20861               0 :   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
   20862               0 :   assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
   20863               0 :   assert( m.z || db->mallocFailed );
   20864               0 :   return m.z;
   20865                 : }
   20866                 : 
   20867                 : /*
   20868                 : ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
   20869                 : ** enc. A pointer to the new string is returned, and the value of *pnOut
   20870                 : ** is set to the length of the returned string in bytes. The call should
   20871                 : ** arrange to call sqlite3DbFree() on the returned pointer when it is
   20872                 : ** no longer required.
   20873                 : ** 
   20874                 : ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
   20875                 : ** flag set.
   20876                 : */
   20877                 : #ifdef SQLITE_ENABLE_STAT3
   20878                 : SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
   20879                 :   Mem m;
   20880                 :   memset(&m, 0, sizeof(m));
   20881                 :   m.db = db;
   20882                 :   sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
   20883                 :   if( sqlite3VdbeMemTranslate(&m, enc) ){
   20884                 :     assert( db->mallocFailed );
   20885                 :     return 0;
   20886                 :   }
   20887                 :   assert( m.z==m.zMalloc );
   20888                 :   *pnOut = m.n;
   20889                 :   return m.z;
   20890                 : }
   20891                 : #endif
   20892                 : 
   20893                 : /*
   20894                 : ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
   20895                 : ** Return the number of bytes in the first nChar unicode characters
   20896                 : ** in pZ.  nChar must be non-negative.
   20897                 : */
   20898               0 : SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
   20899                 :   int c;
   20900               0 :   unsigned char const *z = zIn;
   20901               0 :   int n = 0;
   20902                 :   
   20903                 :   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
   20904                 :     while( n<nChar ){
   20905                 :       READ_UTF16BE(z, 1, c);
   20906                 :       n++;
   20907                 :     }
   20908                 :   }else{
   20909               0 :     while( n<nChar ){
   20910               0 :       READ_UTF16LE(z, 1, c);
   20911               0 :       n++;
   20912                 :     }
   20913                 :   }
   20914               0 :   return (int)(z-(unsigned char const *)zIn);
   20915                 : }
   20916                 : 
   20917                 : #if defined(SQLITE_TEST)
   20918                 : /*
   20919                 : ** This routine is called from the TCL test function "translate_selftest".
   20920                 : ** It checks that the primitives for serializing and deserializing
   20921                 : ** characters in each encoding are inverses of each other.
   20922                 : */
   20923                 : SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
   20924                 :   unsigned int i, t;
   20925                 :   unsigned char zBuf[20];
   20926                 :   unsigned char *z;
   20927                 :   int n;
   20928                 :   unsigned int c;
   20929                 : 
   20930                 :   for(i=0; i<0x00110000; i++){
   20931                 :     z = zBuf;
   20932                 :     WRITE_UTF8(z, i);
   20933                 :     n = (int)(z-zBuf);
   20934                 :     assert( n>0 && n<=4 );
   20935                 :     z[0] = 0;
   20936                 :     z = zBuf;
   20937                 :     c = sqlite3Utf8Read(z, (const u8**)&z);
   20938                 :     t = i;
   20939                 :     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
   20940                 :     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
   20941                 :     assert( c==t );
   20942                 :     assert( (z-zBuf)==n );
   20943                 :   }
   20944                 :   for(i=0; i<0x00110000; i++){
   20945                 :     if( i>=0xD800 && i<0xE000 ) continue;
   20946                 :     z = zBuf;
   20947                 :     WRITE_UTF16LE(z, i);
   20948                 :     n = (int)(z-zBuf);
   20949                 :     assert( n>0 && n<=4 );
   20950                 :     z[0] = 0;
   20951                 :     z = zBuf;
   20952                 :     READ_UTF16LE(z, 1, c);
   20953                 :     assert( c==i );
   20954                 :     assert( (z-zBuf)==n );
   20955                 :   }
   20956                 :   for(i=0; i<0x00110000; i++){
   20957                 :     if( i>=0xD800 && i<0xE000 ) continue;
   20958                 :     z = zBuf;
   20959                 :     WRITE_UTF16BE(z, i);
   20960                 :     n = (int)(z-zBuf);
   20961                 :     assert( n>0 && n<=4 );
   20962                 :     z[0] = 0;
   20963                 :     z = zBuf;
   20964                 :     READ_UTF16BE(z, 1, c);
   20965                 :     assert( c==i );
   20966                 :     assert( (z-zBuf)==n );
   20967                 :   }
   20968                 : }
   20969                 : #endif /* SQLITE_TEST */
   20970                 : #endif /* SQLITE_OMIT_UTF16 */
   20971                 : 
   20972                 : /************** End of utf.c *************************************************/
   20973                 : /************** Begin file util.c ********************************************/
   20974                 : /*
   20975                 : ** 2001 September 15
   20976                 : **
   20977                 : ** The author disclaims copyright to this source code.  In place of
   20978                 : ** a legal notice, here is a blessing:
   20979                 : **
   20980                 : **    May you do good and not evil.
   20981                 : **    May you find forgiveness for yourself and forgive others.
   20982                 : **    May you share freely, never taking more than you give.
   20983                 : **
   20984                 : *************************************************************************
   20985                 : ** Utility functions used throughout sqlite.
   20986                 : **
   20987                 : ** This file contains functions for allocating memory, comparing
   20988                 : ** strings, and stuff like that.
   20989                 : **
   20990                 : */
   20991                 : /* #include <stdarg.h> */
   20992                 : #ifdef SQLITE_HAVE_ISNAN
   20993                 : # include <math.h>
   20994                 : #endif
   20995                 : 
   20996                 : /*
   20997                 : ** Routine needed to support the testcase() macro.
   20998                 : */
   20999                 : #ifdef SQLITE_COVERAGE_TEST
   21000                 : SQLITE_PRIVATE void sqlite3Coverage(int x){
   21001                 :   static unsigned dummy = 0;
   21002                 :   dummy += (unsigned)x;
   21003                 : }
   21004                 : #endif
   21005                 : 
   21006                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   21007                 : /*
   21008                 : ** Return true if the floating point value is Not a Number (NaN).
   21009                 : **
   21010                 : ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
   21011                 : ** Otherwise, we have our own implementation that works on most systems.
   21012                 : */
   21013           59823 : SQLITE_PRIVATE int sqlite3IsNaN(double x){
   21014                 :   int rc;   /* The value return */
   21015                 : #if !defined(SQLITE_HAVE_ISNAN)
   21016                 :   /*
   21017                 :   ** Systems that support the isnan() library function should probably
   21018                 :   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
   21019                 :   ** found that many systems do not have a working isnan() function so
   21020                 :   ** this implementation is provided as an alternative.
   21021                 :   **
   21022                 :   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
   21023                 :   ** On the other hand, the use of -ffast-math comes with the following
   21024                 :   ** warning:
   21025                 :   **
   21026                 :   **      This option [-ffast-math] should never be turned on by any
   21027                 :   **      -O option since it can result in incorrect output for programs
   21028                 :   **      which depend on an exact implementation of IEEE or ISO 
   21029                 :   **      rules/specifications for math functions.
   21030                 :   **
   21031                 :   ** Under MSVC, this NaN test may fail if compiled with a floating-
   21032                 :   ** point precision mode other than /fp:precise.  From the MSDN 
   21033                 :   ** documentation:
   21034                 :   **
   21035                 :   **      The compiler [with /fp:precise] will properly handle comparisons 
   21036                 :   **      involving NaN. For example, x != x evaluates to true if x is NaN 
   21037                 :   **      ...
   21038                 :   */
   21039                 : #ifdef __FAST_MATH__
   21040                 : # error SQLite will not work correctly with the -ffast-math option of GCC.
   21041                 : #endif
   21042           59823 :   volatile double y = x;
   21043           59823 :   volatile double z = y;
   21044           59823 :   rc = (y!=z);
   21045                 : #else  /* if defined(SQLITE_HAVE_ISNAN) */
   21046                 :   rc = isnan(x);
   21047                 : #endif /* SQLITE_HAVE_ISNAN */
   21048                 :   testcase( rc );
   21049           59823 :   return rc;
   21050                 : }
   21051                 : #endif /* SQLITE_OMIT_FLOATING_POINT */
   21052                 : 
   21053                 : /*
   21054                 : ** Compute a string length that is limited to what can be stored in
   21055                 : ** lower 30 bits of a 32-bit signed integer.
   21056                 : **
   21057                 : ** The value returned will never be negative.  Nor will it ever be greater
   21058                 : ** than the actual length of the string.  For very long strings (greater
   21059                 : ** than 1GiB) the value returned might be less than the true string length.
   21060                 : */
   21061         4030145 : SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
   21062         4030145 :   const char *z2 = z;
   21063         4030145 :   if( z==0 ) return 0;
   21064         4030145 :   while( *z2 ){ z2++; }
   21065         4030145 :   return 0x3fffffff & (int)(z2 - z);
   21066                 : }
   21067                 : 
   21068                 : /*
   21069                 : ** Set the most recent error code and error string for the sqlite
   21070                 : ** handle "db". The error code is set to "err_code".
   21071                 : **
   21072                 : ** If it is not NULL, string zFormat specifies the format of the
   21073                 : ** error string in the style of the printf functions: The following
   21074                 : ** format characters are allowed:
   21075                 : **
   21076                 : **      %s      Insert a string
   21077                 : **      %z      A string that should be freed after use
   21078                 : **      %d      Insert an integer
   21079                 : **      %T      Insert a token
   21080                 : **      %S      Insert the first element of a SrcList
   21081                 : **
   21082                 : ** zFormat and any string tokens that follow it are assumed to be
   21083                 : ** encoded in UTF-8.
   21084                 : **
   21085                 : ** To clear the most recent error for sqlite handle "db", sqlite3Error
   21086                 : ** should be called with err_code set to SQLITE_OK and zFormat set
   21087                 : ** to NULL.
   21088                 : */
   21089         1105575 : SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
   21090         1105575 :   if( db && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
   21091         1105568 :     db->errCode = err_code;
   21092         1105568 :     if( zFormat ){
   21093                 :       char *z;
   21094                 :       va_list ap;
   21095              89 :       va_start(ap, zFormat);
   21096              89 :       z = sqlite3VMPrintf(db, zFormat, ap);
   21097              89 :       va_end(ap);
   21098              89 :       sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
   21099                 :     }else{
   21100         1105479 :       sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
   21101                 :     }
   21102                 :   }
   21103         1105575 : }
   21104                 : 
   21105                 : /*
   21106                 : ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
   21107                 : ** The following formatting characters are allowed:
   21108                 : **
   21109                 : **      %s      Insert a string
   21110                 : **      %z      A string that should be freed after use
   21111                 : **      %d      Insert an integer
   21112                 : **      %T      Insert a token
   21113                 : **      %S      Insert the first element of a SrcList
   21114                 : **
   21115                 : ** This function should be used to report any error that occurs whilst
   21116                 : ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
   21117                 : ** last thing the sqlite3_prepare() function does is copy the error
   21118                 : ** stored by this function into the database handle using sqlite3Error().
   21119                 : ** Function sqlite3Error() should be used during statement execution
   21120                 : ** (sqlite3_step() etc.).
   21121                 : */
   21122              86 : SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
   21123                 :   char *zMsg;
   21124                 :   va_list ap;
   21125              86 :   sqlite3 *db = pParse->db;
   21126              86 :   va_start(ap, zFormat);
   21127              86 :   zMsg = sqlite3VMPrintf(db, zFormat, ap);
   21128              86 :   va_end(ap);
   21129              86 :   if( db->suppressErr ){
   21130               7 :     sqlite3DbFree(db, zMsg);
   21131                 :   }else{
   21132              79 :     pParse->nErr++;
   21133              79 :     sqlite3DbFree(db, pParse->zErrMsg);
   21134              79 :     pParse->zErrMsg = zMsg;
   21135              79 :     pParse->rc = SQLITE_ERROR;
   21136                 :   }
   21137              86 : }
   21138                 : 
   21139                 : /*
   21140                 : ** Convert an SQL-style quoted string into a normal string by removing
   21141                 : ** the quote characters.  The conversion is done in-place.  If the
   21142                 : ** input does not begin with a quote character, then this routine
   21143                 : ** is a no-op.
   21144                 : **
   21145                 : ** The input string must be zero-terminated.  A new zero-terminator
   21146                 : ** is added to the dequoted string.
   21147                 : **
   21148                 : ** The return value is -1 if no dequoting occurs or the length of the
   21149                 : ** dequoted string, exclusive of the zero terminator, if dequoting does
   21150                 : ** occur.
   21151                 : **
   21152                 : ** 2002-Feb-14: This routine is extended to remove MS-Access style
   21153                 : ** brackets from around identifers.  For example:  "[a-b-c]" becomes
   21154                 : ** "a-b-c".
   21155                 : */
   21156         1046909 : SQLITE_PRIVATE int sqlite3Dequote(char *z){
   21157                 :   char quote;
   21158                 :   int i, j;
   21159         1046909 :   if( z==0 ) return -1;
   21160          961506 :   quote = z[0];
   21161          961506 :   switch( quote ){
   21162          154775 :     case '\'':  break;
   21163             142 :     case '"':   break;
   21164               0 :     case '`':   break;                /* For MySQL compatibility */
   21165               0 :     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
   21166          806589 :     default:    return -1;
   21167                 :   }
   21168         3939463 :   for(i=1, j=0; ALWAYS(z[i]); i++){
   21169         3939463 :     if( z[i]==quote ){
   21170          159173 :       if( z[i+1]==quote ){
   21171            4256 :         z[j++] = quote;
   21172            4256 :         i++;
   21173                 :       }else{
   21174          154917 :         break;
   21175                 :       }
   21176                 :     }else{
   21177         3780290 :       z[j++] = z[i];
   21178                 :     }
   21179                 :   }
   21180          154917 :   z[j] = 0;
   21181          154917 :   return j;
   21182                 : }
   21183                 : 
   21184                 : /* Convenient short-hand */
   21185                 : #define UpperToLower sqlite3UpperToLower
   21186                 : 
   21187                 : /*
   21188                 : ** Some systems have stricmp().  Others have strcasecmp().  Because
   21189                 : ** there is no consistency, we will define our own.
   21190                 : **
   21191                 : ** IMPLEMENTATION-OF: R-20522-24639 The sqlite3_strnicmp() API allows
   21192                 : ** applications and extensions to compare the contents of two buffers
   21193                 : ** containing UTF-8 strings in a case-independent fashion, using the same
   21194                 : ** definition of case independence that SQLite uses internally when
   21195                 : ** comparing identifiers.
   21196                 : */
   21197         5248717 : SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
   21198                 :   register unsigned char *a, *b;
   21199         5248717 :   a = (unsigned char *)zLeft;
   21200         5248717 :   b = (unsigned char *)zRight;
   21201         5248717 :   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
   21202         5248717 :   return UpperToLower[*a] - UpperToLower[*b];
   21203                 : }
   21204         3432467 : SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
   21205                 :   register unsigned char *a, *b;
   21206         3432467 :   a = (unsigned char *)zLeft;
   21207         3432467 :   b = (unsigned char *)zRight;
   21208         3432467 :   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
   21209         3432467 :   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
   21210                 : }
   21211                 : 
   21212                 : /*
   21213                 : ** The string z[] is an text representation of a real number.
   21214                 : ** Convert this string to a double and write it into *pResult.
   21215                 : **
   21216                 : ** The string z[] is length bytes in length (bytes, not characters) and
   21217                 : ** uses the encoding enc.  The string is not necessarily zero-terminated.
   21218                 : **
   21219                 : ** Return TRUE if the result is a valid real number (or integer) and FALSE
   21220                 : ** if the string is empty or contains extraneous text.  Valid numbers
   21221                 : ** are in one of these formats:
   21222                 : **
   21223                 : **    [+-]digits[E[+-]digits]
   21224                 : **    [+-]digits.[digits][E[+-]digits]
   21225                 : **    [+-].digits[E[+-]digits]
   21226                 : **
   21227                 : ** Leading and trailing whitespace is ignored for the purpose of determining
   21228                 : ** validity.
   21229                 : **
   21230                 : ** If some prefix of the input string is a valid number, this routine
   21231                 : ** returns FALSE but it still converts the prefix and writes the result
   21232                 : ** into *pResult.
   21233                 : */
   21234           13234 : SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
   21235                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   21236           13234 :   int incr = (enc==SQLITE_UTF8?1:2);
   21237           13234 :   const char *zEnd = z + length;
   21238                 :   /* sign * significand * (10 ^ (esign * exponent)) */
   21239           13234 :   int sign = 1;    /* sign of significand */
   21240           13234 :   i64 s = 0;       /* significand */
   21241           13234 :   int d = 0;       /* adjust exponent for shifting decimal point */
   21242           13234 :   int esign = 1;   /* sign of exponent */
   21243           13234 :   int e = 0;       /* exponent */
   21244           13234 :   int eValid = 1;  /* True exponent is either not used or is well-formed */
   21245                 :   double result;
   21246           13234 :   int nDigits = 0;
   21247                 : 
   21248           13234 :   *pResult = 0.0;   /* Default return value, in case of an error */
   21249                 : 
   21250           13234 :   if( enc==SQLITE_UTF16BE ) z++;
   21251                 : 
   21252                 :   /* skip leading spaces */
   21253           13234 :   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
   21254           13234 :   if( z>=zEnd ) return 0;
   21255                 : 
   21256                 :   /* get sign of significand */
   21257           13234 :   if( *z=='-' ){
   21258            1194 :     sign = -1;
   21259            1194 :     z+=incr;
   21260           12040 :   }else if( *z=='+' ){
   21261             239 :     z+=incr;
   21262                 :   }
   21263                 : 
   21264                 :   /* skip leading zeroes */
   21265           13234 :   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
   21266                 : 
   21267                 :   /* copy max significant digits to significand */
   21268          136666 :   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
   21269          110198 :     s = s*10 + (*z - '0');
   21270          110198 :     z+=incr, nDigits++;
   21271                 :   }
   21272                 : 
   21273                 :   /* skip non-significant significand digits
   21274                 :   ** (increase exponent by d to shift decimal left) */
   21275           13234 :   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
   21276           13234 :   if( z>=zEnd ) goto do_atof_calc;
   21277                 : 
   21278                 :   /* if decimal point is present */
   21279             946 :   if( *z=='.' ){
   21280             868 :     z+=incr;
   21281                 :     /* copy digits from after decimal to significand
   21282                 :     ** (decrease exponent by d to shift decimal right) */
   21283            3767 :     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
   21284            2031 :       s = s*10 + (*z - '0');
   21285            2031 :       z+=incr, nDigits++, d--;
   21286                 :     }
   21287                 :     /* skip non-significant digits */
   21288             868 :     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
   21289                 :   }
   21290             946 :   if( z>=zEnd ) goto do_atof_calc;
   21291                 : 
   21292                 :   /* if exponent is present */
   21293              80 :   if( *z=='e' || *z=='E' ){
   21294               2 :     z+=incr;
   21295               2 :     eValid = 0;
   21296               2 :     if( z>=zEnd ) goto do_atof_calc;
   21297                 :     /* get sign of exponent */
   21298               2 :     if( *z=='-' ){
   21299               2 :       esign = -1;
   21300               2 :       z+=incr;
   21301               0 :     }else if( *z=='+' ){
   21302               0 :       z+=incr;
   21303                 :     }
   21304                 :     /* copy digits to exponent */
   21305               8 :     while( z<zEnd && sqlite3Isdigit(*z) ){
   21306               4 :       e = e<10000 ? (e*10 + (*z - '0')) : 10000;
   21307               4 :       z+=incr;
   21308               4 :       eValid = 1;
   21309                 :     }
   21310                 :   }
   21311                 : 
   21312                 :   /* skip trailing spaces */
   21313              80 :   if( nDigits && eValid ){
   21314              66 :     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
   21315                 :   }
   21316                 : 
   21317                 : do_atof_calc:
   21318                 :   /* adjust exponent by d, and update sign */
   21319           13234 :   e = (e*esign) + d;
   21320           13234 :   if( e<0 ) {
   21321             868 :     esign = -1;
   21322             868 :     e *= -1;
   21323                 :   } else {
   21324           12366 :     esign = 1;
   21325                 :   }
   21326                 : 
   21327                 :   /* if 0 significand */
   21328           13234 :   if( !s ) {
   21329                 :     /* In the IEEE 754 standard, zero is signed.
   21330                 :     ** Add the sign if we've seen at least one digit */
   21331             189 :     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
   21332                 :   } else {
   21333                 :     /* attempt to reduce exponent */
   21334           13045 :     if( esign>0 ){
   21335           12177 :       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
   21336                 :     }else{
   21337             868 :       while( !(s%10) && e>0 ) e--,s/=10;
   21338                 :     }
   21339                 : 
   21340                 :     /* adjust the sign of significand */
   21341           13045 :     s = sign<0 ? -s : s;
   21342                 : 
   21343                 :     /* if exponent, scale significand as appropriate
   21344                 :     ** and store in result. */
   21345           13045 :     if( e ){
   21346             448 :       double scale = 1.0;
   21347                 :       /* attempt to handle extremely small/large numbers better */
   21348             448 :       if( e>307 && e<342 ){
   21349               0 :         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
   21350               0 :         if( esign<0 ){
   21351               0 :           result = s / scale;
   21352               0 :           result /= 1.0e+308;
   21353                 :         }else{
   21354               0 :           result = s * scale;
   21355               0 :           result *= 1.0e+308;
   21356                 :         }
   21357             448 :       }else if( e>=342 ){
   21358               0 :         if( esign<0 ){
   21359               0 :           result = 0.0*s;
   21360                 :         }else{
   21361               0 :           result = 1e308*1e308*s;  /* Infinity */
   21362                 :         }
   21363                 :       }else{
   21364                 :         /* 1.0e+22 is the largest power of 10 than can be 
   21365                 :         ** represented exactly. */
   21366             448 :         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
   21367             448 :         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
   21368             448 :         if( esign<0 ){
   21369             448 :           result = s / scale;
   21370                 :         }else{
   21371               0 :           result = s * scale;
   21372                 :         }
   21373                 :       }
   21374                 :     } else {
   21375           12597 :       result = (double)s;
   21376                 :     }
   21377                 :   }
   21378                 : 
   21379                 :   /* store the result */
   21380           13234 :   *pResult = result;
   21381                 : 
   21382                 :   /* return true if number and no extra non-whitespace chracters after */
   21383           13234 :   return z>=zEnd && nDigits>0 && eValid;
   21384                 : #else
   21385                 :   return !sqlite3Atoi64(z, pResult, length, enc);
   21386                 : #endif /* SQLITE_OMIT_FLOATING_POINT */
   21387                 : }
   21388                 : 
   21389                 : /*
   21390                 : ** Compare the 19-character string zNum against the text representation
   21391                 : ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
   21392                 : ** if zNum is less than, equal to, or greater than the string.
   21393                 : ** Note that zNum must contain exactly 19 characters.
   21394                 : **
   21395                 : ** Unlike memcmp() this routine is guaranteed to return the difference
   21396                 : ** in the values of the last digit if the only difference is in the
   21397                 : ** last digit.  So, for example,
   21398                 : **
   21399                 : **      compare2pow63("9223372036854775800", 1)
   21400                 : **
   21401                 : ** will return -8.
   21402                 : */
   21403               0 : static int compare2pow63(const char *zNum, int incr){
   21404               0 :   int c = 0;
   21405                 :   int i;
   21406                 :                     /* 012345678901234567 */
   21407               0 :   const char *pow63 = "922337203685477580";
   21408               0 :   for(i=0; c==0 && i<18; i++){
   21409               0 :     c = (zNum[i*incr]-pow63[i])*10;
   21410                 :   }
   21411               0 :   if( c==0 ){
   21412               0 :     c = zNum[18*incr] - '8';
   21413                 :     testcase( c==(-1) );
   21414                 :     testcase( c==0 );
   21415                 :     testcase( c==(+1) );
   21416                 :   }
   21417               0 :   return c;
   21418                 : }
   21419                 : 
   21420                 : 
   21421                 : /*
   21422                 : ** Convert zNum to a 64-bit signed integer.
   21423                 : **
   21424                 : ** If the zNum value is representable as a 64-bit twos-complement 
   21425                 : ** integer, then write that value into *pNum and return 0.
   21426                 : **
   21427                 : ** If zNum is exactly 9223372036854665808, return 2.  This special
   21428                 : ** case is broken out because while 9223372036854665808 cannot be a 
   21429                 : ** signed 64-bit integer, its negative -9223372036854665808 can be.
   21430                 : **
   21431                 : ** If zNum is too big for a 64-bit integer and is not
   21432                 : ** 9223372036854665808 then return 1.
   21433                 : **
   21434                 : ** length is the number of bytes in the string (bytes, not characters).
   21435                 : ** The string is not necessarily zero-terminated.  The encoding is
   21436                 : ** given by enc.
   21437                 : */
   21438           23636 : SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
   21439           23636 :   int incr = (enc==SQLITE_UTF8?1:2);
   21440           23636 :   u64 u = 0;
   21441           23636 :   int neg = 0; /* assume positive */
   21442                 :   int i;
   21443           23636 :   int c = 0;
   21444                 :   const char *zStart;
   21445           23636 :   const char *zEnd = zNum + length;
   21446           23636 :   if( enc==SQLITE_UTF16BE ) zNum++;
   21447           23636 :   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
   21448           23636 :   if( zNum<zEnd ){
   21449           23636 :     if( *zNum=='-' ){
   21450               9 :       neg = 1;
   21451               9 :       zNum+=incr;
   21452           23627 :     }else if( *zNum=='+' ){
   21453               0 :       zNum+=incr;
   21454                 :     }
   21455                 :   }
   21456           23636 :   zStart = zNum;
   21457           23636 :   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
   21458          146511 :   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
   21459          122875 :     u = u*10 + c - '0';
   21460                 :   }
   21461           23636 :   if( u>LARGEST_INT64 ){
   21462               0 :     *pNum = SMALLEST_INT64;
   21463           23636 :   }else if( neg ){
   21464               9 :     *pNum = -(i64)u;
   21465                 :   }else{
   21466           23627 :     *pNum = (i64)u;
   21467                 :   }
   21468                 :   testcase( i==18 );
   21469                 :   testcase( i==19 );
   21470                 :   testcase( i==20 );
   21471           23636 :   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
   21472                 :     /* zNum is empty or contains non-numeric text or is longer
   21473                 :     ** than 19 digits (thus guaranteeing that it is too large) */
   21474               0 :     return 1;
   21475           23636 :   }else if( i<19*incr ){
   21476                 :     /* Less than 19 digits, so we know that it fits in 64 bits */
   21477           23636 :     assert( u<=LARGEST_INT64 );
   21478           23636 :     return 0;
   21479                 :   }else{
   21480                 :     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
   21481               0 :     c = compare2pow63(zNum, incr);
   21482               0 :     if( c<0 ){
   21483                 :       /* zNum is less than 9223372036854775808 so it fits */
   21484               0 :       assert( u<=LARGEST_INT64 );
   21485               0 :       return 0;
   21486               0 :     }else if( c>0 ){
   21487                 :       /* zNum is greater than 9223372036854775808 so it overflows */
   21488               0 :       return 1;
   21489                 :     }else{
   21490                 :       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
   21491                 :       ** special case 2 overflow if positive */
   21492               0 :       assert( u-1==LARGEST_INT64 );
   21493               0 :       assert( (*pNum)==SMALLEST_INT64 );
   21494               0 :       return neg ? 0 : 2;
   21495                 :     }
   21496                 :   }
   21497                 : }
   21498                 : 
   21499                 : /*
   21500                 : ** If zNum represents an integer that will fit in 32-bits, then set
   21501                 : ** *pValue to that integer and return true.  Otherwise return false.
   21502                 : **
   21503                 : ** Any non-numeric characters that following zNum are ignored.
   21504                 : ** This is different from sqlite3Atoi64() which requires the
   21505                 : ** input number to be zero-terminated.
   21506                 : */
   21507          162572 : SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
   21508          162572 :   sqlite_int64 v = 0;
   21509                 :   int i, c;
   21510          162572 :   int neg = 0;
   21511          162572 :   if( zNum[0]=='-' ){
   21512              26 :     neg = 1;
   21513              26 :     zNum++;
   21514          162546 :   }else if( zNum[0]=='+' ){
   21515               0 :     zNum++;
   21516                 :   }
   21517          162572 :   while( zNum[0]=='0' ) zNum++;
   21518          343583 :   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
   21519          181011 :     v = v*10 + c;
   21520                 :   }
   21521                 : 
   21522                 :   /* The longest decimal representation of a 32 bit integer is 10 digits:
   21523                 :   **
   21524                 :   **             1234567890
   21525                 :   **     2^31 -> 2147483648
   21526                 :   */
   21527                 :   testcase( i==10 );
   21528          162572 :   if( i>10 ){
   21529             175 :     return 0;
   21530                 :   }
   21531                 :   testcase( v-neg==2147483647 );
   21532          162397 :   if( v-neg>2147483647 ){
   21533               0 :     return 0;
   21534                 :   }
   21535          162397 :   if( neg ){
   21536              26 :     v = -v;
   21537                 :   }
   21538          162397 :   *pValue = (int)v;
   21539          162397 :   return 1;
   21540                 : }
   21541                 : 
   21542                 : /*
   21543                 : ** Return a 32-bit integer value extracted from a string.  If the
   21544                 : ** string is not an integer, just return 0.
   21545                 : */
   21546           58756 : SQLITE_PRIVATE int sqlite3Atoi(const char *z){
   21547           58756 :   int x = 0;
   21548           58756 :   if( z ) sqlite3GetInt32(z, &x);
   21549           58756 :   return x;
   21550                 : }
   21551                 : 
   21552                 : /*
   21553                 : ** The variable-length integer encoding is as follows:
   21554                 : **
   21555                 : ** KEY:
   21556                 : **         A = 0xxxxxxx    7 bits of data and one flag bit
   21557                 : **         B = 1xxxxxxx    7 bits of data and one flag bit
   21558                 : **         C = xxxxxxxx    8 bits of data
   21559                 : **
   21560                 : **  7 bits - A
   21561                 : ** 14 bits - BA
   21562                 : ** 21 bits - BBA
   21563                 : ** 28 bits - BBBA
   21564                 : ** 35 bits - BBBBA
   21565                 : ** 42 bits - BBBBBA
   21566                 : ** 49 bits - BBBBBBA
   21567                 : ** 56 bits - BBBBBBBA
   21568                 : ** 64 bits - BBBBBBBBC
   21569                 : */
   21570                 : 
   21571                 : /*
   21572                 : ** Write a 64-bit variable-length integer to memory starting at p[0].
   21573                 : ** The length of data write will be between 1 and 9 bytes.  The number
   21574                 : ** of bytes written is returned.
   21575                 : **
   21576                 : ** A variable-length integer consists of the lower 7 bits of each byte
   21577                 : ** for all bytes that have the 8th bit set and one byte with the 8th
   21578                 : ** bit clear.  Except, if we get to the 9th byte, it stores the full
   21579                 : ** 8 bits and is the last byte.
   21580                 : */
   21581          330123 : SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
   21582                 :   int i, j, n;
   21583                 :   u8 buf[10];
   21584          330123 :   if( v & (((u64)0xff000000)<<32) ){
   21585               0 :     p[8] = (u8)v;
   21586               0 :     v >>= 8;
   21587               0 :     for(i=7; i>=0; i--){
   21588               0 :       p[i] = (u8)((v & 0x7f) | 0x80);
   21589               0 :       v >>= 7;
   21590                 :     }
   21591               0 :     return 9;
   21592                 :   }    
   21593          330123 :   n = 0;
   21594                 :   do{
   21595          367907 :     buf[n++] = (u8)((v & 0x7f) | 0x80);
   21596          367907 :     v >>= 7;
   21597          367907 :   }while( v!=0 );
   21598          330123 :   buf[0] &= 0x7f;
   21599          330123 :   assert( n<=9 );
   21600          698030 :   for(i=0, j=n-1; j>=0; j--, i++){
   21601          367907 :     p[i] = buf[j];
   21602                 :   }
   21603          330123 :   return n;
   21604                 : }
   21605                 : 
   21606                 : /*
   21607                 : ** This routine is a faster version of sqlite3PutVarint() that only
   21608                 : ** works for 32-bit positive integers and which is optimized for
   21609                 : ** the common case of small integers.  A MACRO version, putVarint32,
   21610                 : ** is provided which inlines the single-byte case.  All code should use
   21611                 : ** the MACRO version as this function assumes the single-byte case has
   21612                 : ** already been handled.
   21613                 : */
   21614           21353 : SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
   21615                 : #ifndef putVarint32
   21616                 :   if( (v & ~0x7f)==0 ){
   21617                 :     p[0] = v;
   21618                 :     return 1;
   21619                 :   }
   21620                 : #endif
   21621           21353 :   if( (v & ~0x3fff)==0 ){
   21622           21353 :     p[0] = (u8)((v>>7) | 0x80);
   21623           21353 :     p[1] = (u8)(v & 0x7f);
   21624           21353 :     return 2;
   21625                 :   }
   21626               0 :   return sqlite3PutVarint(p, v);
   21627                 : }
   21628                 : 
   21629                 : /*
   21630                 : ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
   21631                 : ** are defined here rather than simply putting the constant expressions
   21632                 : ** inline in order to work around bugs in the RVT compiler.
   21633                 : **
   21634                 : ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
   21635                 : **
   21636                 : ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
   21637                 : */
   21638                 : #define SLOT_2_0     0x001fc07f
   21639                 : #define SLOT_4_2_0   0xf01fc07f
   21640                 : 
   21641                 : 
   21642                 : /*
   21643                 : ** Read a 64-bit variable-length integer from memory starting at p[0].
   21644                 : ** Return the number of bytes read.  The value is stored in *v.
   21645                 : */
   21646         1761093 : SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
   21647                 :   u32 a,b,s;
   21648                 : 
   21649         1761093 :   a = *p;
   21650                 :   /* a: p0 (unmasked) */
   21651         1761093 :   if (!(a&0x80))
   21652                 :   {
   21653         1501056 :     *v = a;
   21654         1501056 :     return 1;
   21655                 :   }
   21656                 : 
   21657          260037 :   p++;
   21658          260037 :   b = *p;
   21659                 :   /* b: p1 (unmasked) */
   21660          260037 :   if (!(b&0x80))
   21661                 :   {
   21662          251891 :     a &= 0x7f;
   21663          251891 :     a = a<<7;
   21664          251891 :     a |= b;
   21665          251891 :     *v = a;
   21666          251891 :     return 2;
   21667                 :   }
   21668                 : 
   21669                 :   /* Verify that constants are precomputed correctly */
   21670                 :   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
   21671                 :   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
   21672                 : 
   21673            8146 :   p++;
   21674            8146 :   a = a<<14;
   21675            8146 :   a |= *p;
   21676                 :   /* a: p0<<14 | p2 (unmasked) */
   21677            8146 :   if (!(a&0x80))
   21678                 :   {
   21679               0 :     a &= SLOT_2_0;
   21680               0 :     b &= 0x7f;
   21681               0 :     b = b<<7;
   21682               0 :     a |= b;
   21683               0 :     *v = a;
   21684               0 :     return 3;
   21685                 :   }
   21686                 : 
   21687                 :   /* CSE1 from below */
   21688            8146 :   a &= SLOT_2_0;
   21689            8146 :   p++;
   21690            8146 :   b = b<<14;
   21691            8146 :   b |= *p;
   21692                 :   /* b: p1<<14 | p3 (unmasked) */
   21693            8146 :   if (!(b&0x80))
   21694                 :   {
   21695               0 :     b &= SLOT_2_0;
   21696                 :     /* moved CSE1 up */
   21697                 :     /* a &= (0x7f<<14)|(0x7f); */
   21698               0 :     a = a<<7;
   21699               0 :     a |= b;
   21700               0 :     *v = a;
   21701               0 :     return 4;
   21702                 :   }
   21703                 : 
   21704                 :   /* a: p0<<14 | p2 (masked) */
   21705                 :   /* b: p1<<14 | p3 (unmasked) */
   21706                 :   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
   21707                 :   /* moved CSE1 up */
   21708                 :   /* a &= (0x7f<<14)|(0x7f); */
   21709            8146 :   b &= SLOT_2_0;
   21710            8146 :   s = a;
   21711                 :   /* s: p0<<14 | p2 (masked) */
   21712                 : 
   21713            8146 :   p++;
   21714            8146 :   a = a<<14;
   21715            8146 :   a |= *p;
   21716                 :   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
   21717            8146 :   if (!(a&0x80))
   21718                 :   {
   21719                 :     /* we can skip these cause they were (effectively) done above in calc'ing s */
   21720                 :     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
   21721                 :     /* b &= (0x7f<<14)|(0x7f); */
   21722               0 :     b = b<<7;
   21723               0 :     a |= b;
   21724               0 :     s = s>>18;
   21725               0 :     *v = ((u64)s)<<32 | a;
   21726               0 :     return 5;
   21727                 :   }
   21728                 : 
   21729                 :   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
   21730            8146 :   s = s<<7;
   21731            8146 :   s |= b;
   21732                 :   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
   21733                 : 
   21734            8146 :   p++;
   21735            8146 :   b = b<<14;
   21736            8146 :   b |= *p;
   21737                 :   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
   21738            8146 :   if (!(b&0x80))
   21739                 :   {
   21740                 :     /* we can skip this cause it was (effectively) done above in calc'ing s */
   21741                 :     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
   21742               0 :     a &= SLOT_2_0;
   21743               0 :     a = a<<7;
   21744               0 :     a |= b;
   21745               0 :     s = s>>18;
   21746               0 :     *v = ((u64)s)<<32 | a;
   21747               0 :     return 6;
   21748                 :   }
   21749                 : 
   21750            8146 :   p++;
   21751            8146 :   a = a<<14;
   21752            8146 :   a |= *p;
   21753                 :   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
   21754            8146 :   if (!(a&0x80))
   21755                 :   {
   21756               0 :     a &= SLOT_4_2_0;
   21757               0 :     b &= SLOT_2_0;
   21758               0 :     b = b<<7;
   21759               0 :     a |= b;
   21760               0 :     s = s>>11;
   21761               0 :     *v = ((u64)s)<<32 | a;
   21762               0 :     return 7;
   21763                 :   }
   21764                 : 
   21765                 :   /* CSE2 from below */
   21766            8146 :   a &= SLOT_2_0;
   21767            8146 :   p++;
   21768            8146 :   b = b<<14;
   21769            8146 :   b |= *p;
   21770                 :   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
   21771            8146 :   if (!(b&0x80))
   21772                 :   {
   21773            8146 :     b &= SLOT_4_2_0;
   21774                 :     /* moved CSE2 up */
   21775                 :     /* a &= (0x7f<<14)|(0x7f); */
   21776            8146 :     a = a<<7;
   21777            8146 :     a |= b;
   21778            8146 :     s = s>>4;
   21779            8146 :     *v = ((u64)s)<<32 | a;
   21780            8146 :     return 8;
   21781                 :   }
   21782                 : 
   21783               0 :   p++;
   21784               0 :   a = a<<15;
   21785               0 :   a |= *p;
   21786                 :   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
   21787                 : 
   21788                 :   /* moved CSE2 up */
   21789                 :   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
   21790               0 :   b &= SLOT_2_0;
   21791               0 :   b = b<<8;
   21792               0 :   a |= b;
   21793                 : 
   21794               0 :   s = s<<4;
   21795               0 :   b = p[-4];
   21796               0 :   b &= 0x7f;
   21797               0 :   b = b>>3;
   21798               0 :   s |= b;
   21799                 : 
   21800               0 :   *v = ((u64)s)<<32 | a;
   21801                 : 
   21802               0 :   return 9;
   21803                 : }
   21804                 : 
   21805                 : /*
   21806                 : ** Read a 32-bit variable-length integer from memory starting at p[0].
   21807                 : ** Return the number of bytes read.  The value is stored in *v.
   21808                 : **
   21809                 : ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
   21810                 : ** integer, then set *v to 0xffffffff.
   21811                 : **
   21812                 : ** A MACRO version, getVarint32, is provided which inlines the 
   21813                 : ** single-byte case.  All code should use the MACRO version as 
   21814                 : ** this function assumes the single-byte case has already been handled.
   21815                 : */
   21816          484947 : SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
   21817                 :   u32 a,b;
   21818                 : 
   21819                 :   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
   21820                 :   ** by the getVarin32() macro */
   21821          484947 :   a = *p;
   21822                 :   /* a: p0 (unmasked) */
   21823                 : #ifndef getVarint32
   21824                 :   if (!(a&0x80))
   21825                 :   {
   21826                 :     /* Values between 0 and 127 */
   21827                 :     *v = a;
   21828                 :     return 1;
   21829                 :   }
   21830                 : #endif
   21831                 : 
   21832                 :   /* The 2-byte case */
   21833          484947 :   p++;
   21834          484947 :   b = *p;
   21835                 :   /* b: p1 (unmasked) */
   21836          484947 :   if (!(b&0x80))
   21837                 :   {
   21838                 :     /* Values between 128 and 16383 */
   21839          484947 :     a &= 0x7f;
   21840          484947 :     a = a<<7;
   21841          484947 :     *v = a | b;
   21842          484947 :     return 2;
   21843                 :   }
   21844                 : 
   21845                 :   /* The 3-byte case */
   21846               0 :   p++;
   21847               0 :   a = a<<14;
   21848               0 :   a |= *p;
   21849                 :   /* a: p0<<14 | p2 (unmasked) */
   21850               0 :   if (!(a&0x80))
   21851                 :   {
   21852                 :     /* Values between 16384 and 2097151 */
   21853               0 :     a &= (0x7f<<14)|(0x7f);
   21854               0 :     b &= 0x7f;
   21855               0 :     b = b<<7;
   21856               0 :     *v = a | b;
   21857               0 :     return 3;
   21858                 :   }
   21859                 : 
   21860                 :   /* A 32-bit varint is used to store size information in btrees.
   21861                 :   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
   21862                 :   ** A 3-byte varint is sufficient, for example, to record the size
   21863                 :   ** of a 1048569-byte BLOB or string.
   21864                 :   **
   21865                 :   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
   21866                 :   ** rare larger cases can be handled by the slower 64-bit varint
   21867                 :   ** routine.
   21868                 :   */
   21869                 : #if 1
   21870                 :   {
   21871                 :     u64 v64;
   21872                 :     u8 n;
   21873                 : 
   21874               0 :     p -= 2;
   21875               0 :     n = sqlite3GetVarint(p, &v64);
   21876               0 :     assert( n>3 && n<=9 );
   21877               0 :     if( (v64 & SQLITE_MAX_U32)!=v64 ){
   21878               0 :       *v = 0xffffffff;
   21879                 :     }else{
   21880               0 :       *v = (u32)v64;
   21881                 :     }
   21882               0 :     return n;
   21883                 :   }
   21884                 : 
   21885                 : #else
   21886                 :   /* For following code (kept for historical record only) shows an
   21887                 :   ** unrolling for the 3- and 4-byte varint cases.  This code is
   21888                 :   ** slightly faster, but it is also larger and much harder to test.
   21889                 :   */
   21890                 :   p++;
   21891                 :   b = b<<14;
   21892                 :   b |= *p;
   21893                 :   /* b: p1<<14 | p3 (unmasked) */
   21894                 :   if (!(b&0x80))
   21895                 :   {
   21896                 :     /* Values between 2097152 and 268435455 */
   21897                 :     b &= (0x7f<<14)|(0x7f);
   21898                 :     a &= (0x7f<<14)|(0x7f);
   21899                 :     a = a<<7;
   21900                 :     *v = a | b;
   21901                 :     return 4;
   21902                 :   }
   21903                 : 
   21904                 :   p++;
   21905                 :   a = a<<14;
   21906                 :   a |= *p;
   21907                 :   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
   21908                 :   if (!(a&0x80))
   21909                 :   {
   21910                 :     /* Values  between 268435456 and 34359738367 */
   21911                 :     a &= SLOT_4_2_0;
   21912                 :     b &= SLOT_4_2_0;
   21913                 :     b = b<<7;
   21914                 :     *v = a | b;
   21915                 :     return 5;
   21916                 :   }
   21917                 : 
   21918                 :   /* We can only reach this point when reading a corrupt database
   21919                 :   ** file.  In that case we are not in any hurry.  Use the (relatively
   21920                 :   ** slow) general-purpose sqlite3GetVarint() routine to extract the
   21921                 :   ** value. */
   21922                 :   {
   21923                 :     u64 v64;
   21924                 :     u8 n;
   21925                 : 
   21926                 :     p -= 4;
   21927                 :     n = sqlite3GetVarint(p, &v64);
   21928                 :     assert( n>5 && n<=9 );
   21929                 :     *v = (u32)v64;
   21930                 :     return n;
   21931                 :   }
   21932                 : #endif
   21933                 : }
   21934                 : 
   21935                 : /*
   21936                 : ** Return the number of bytes that will be needed to store the given
   21937                 : ** 64-bit integer.
   21938                 : */
   21939         1606976 : SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
   21940         1606976 :   int i = 0;
   21941                 :   do{
   21942         1628507 :     i++;
   21943         1628507 :     v >>= 7;
   21944         1628507 :   }while( v!=0 && ALWAYS(i<9) );
   21945         1606976 :   return i;
   21946                 : }
   21947                 : 
   21948                 : 
   21949                 : /*
   21950                 : ** Read or write a four-byte big-endian integer value.
   21951                 : */
   21952          637305 : SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
   21953          637305 :   return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
   21954                 : }
   21955          726884 : SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
   21956          726884 :   p[0] = (u8)(v>>24);
   21957          726884 :   p[1] = (u8)(v>>16);
   21958          726884 :   p[2] = (u8)(v>>8);
   21959          726884 :   p[3] = (u8)v;
   21960          726884 : }
   21961                 : 
   21962                 : 
   21963                 : 
   21964                 : /*
   21965                 : ** Translate a single byte of Hex into an integer.
   21966                 : ** This routine only works if h really is a valid hexadecimal
   21967                 : ** character:  0..9a..fA..F
   21968                 : */
   21969             116 : SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
   21970             116 :   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
   21971                 : #ifdef SQLITE_ASCII
   21972             116 :   h += 9*(1&(h>>6));
   21973                 : #endif
   21974                 : #ifdef SQLITE_EBCDIC
   21975                 :   h += 9*(1&~(h>>4));
   21976                 : #endif
   21977             116 :   return (u8)(h & 0xf);
   21978                 : }
   21979                 : 
   21980                 : #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
   21981                 : /*
   21982                 : ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
   21983                 : ** value.  Return a pointer to its binary value.  Space to hold the
   21984                 : ** binary value has been obtained from malloc and must be freed by
   21985                 : ** the calling routine.
   21986                 : */
   21987              29 : SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
   21988                 :   char *zBlob;
   21989                 :   int i;
   21990                 : 
   21991              29 :   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
   21992              29 :   n--;
   21993              29 :   if( zBlob ){
   21994              87 :     for(i=0; i<n; i+=2){
   21995              58 :       zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
   21996                 :     }
   21997              29 :     zBlob[i/2] = 0;
   21998                 :   }
   21999              29 :   return zBlob;
   22000                 : }
   22001                 : #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
   22002                 : 
   22003                 : /*
   22004                 : ** Log an error that is an API call on a connection pointer that should
   22005                 : ** not have been used.  The "type" of connection pointer is given as the
   22006                 : ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
   22007                 : */
   22008               0 : static void logBadConnection(const char *zType){
   22009               0 :   sqlite3_log(SQLITE_MISUSE, 
   22010                 :      "API call with %s database connection pointer",
   22011                 :      zType
   22012                 :   );
   22013               0 : }
   22014                 : 
   22015                 : /*
   22016                 : ** Check to make sure we have a valid db pointer.  This test is not
   22017                 : ** foolproof but it does provide some measure of protection against
   22018                 : ** misuse of the interface such as passing in db pointers that are
   22019                 : ** NULL or which have been previously closed.  If this routine returns
   22020                 : ** 1 it means that the db pointer is valid and 0 if it should not be
   22021                 : ** dereferenced for any reason.  The calling function should invoke
   22022                 : ** SQLITE_MISUSE immediately.
   22023                 : **
   22024                 : ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
   22025                 : ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
   22026                 : ** open properly and is not fit for general use but which can be
   22027                 : ** used as an argument to sqlite3_errmsg() or sqlite3_close().
   22028                 : */
   22029          243510 : SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
   22030                 :   u32 magic;
   22031          243510 :   if( db==0 ){
   22032               0 :     logBadConnection("NULL");
   22033               0 :     return 0;
   22034                 :   }
   22035          243510 :   magic = db->magic;
   22036          243510 :   if( magic!=SQLITE_MAGIC_OPEN ){
   22037               0 :     if( sqlite3SafetyCheckSickOrOk(db) ){
   22038                 :       testcase( sqlite3GlobalConfig.xLog!=0 );
   22039               0 :       logBadConnection("unopened");
   22040                 :     }
   22041               0 :     return 0;
   22042                 :   }else{
   22043          243510 :     return 1;
   22044                 :   }
   22045                 : }
   22046           16643 : SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
   22047                 :   u32 magic;
   22048           16643 :   magic = db->magic;
   22049           16643 :   if( magic!=SQLITE_MAGIC_SICK &&
   22050               4 :       magic!=SQLITE_MAGIC_OPEN &&
   22051                 :       magic!=SQLITE_MAGIC_BUSY ){
   22052                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   22053               0 :     logBadConnection("invalid");
   22054               0 :     return 0;
   22055                 :   }else{
   22056           16643 :     return 1;
   22057                 :   }
   22058                 : }
   22059                 : 
   22060                 : /*
   22061                 : ** Attempt to add, substract, or multiply the 64-bit signed value iB against
   22062                 : ** the other 64-bit signed integer at *pA and store the result in *pA.
   22063                 : ** Return 0 on success.  Or if the operation would have resulted in an
   22064                 : ** overflow, leave *pA unchanged and return 1.
   22065                 : */
   22066           57878 : SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
   22067           57878 :   i64 iA = *pA;
   22068                 :   testcase( iA==0 ); testcase( iA==1 );
   22069                 :   testcase( iB==-1 ); testcase( iB==0 );
   22070           57878 :   if( iB>=0 ){
   22071                 :     testcase( iA>0 && LARGEST_INT64 - iA == iB );
   22072                 :     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
   22073           43681 :     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
   22074           43681 :     *pA += iB;
   22075                 :   }else{
   22076                 :     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
   22077                 :     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
   22078           14197 :     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
   22079           14197 :     *pA += iB;
   22080                 :   }
   22081           57878 :   return 0; 
   22082                 : }
   22083           27729 : SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
   22084                 :   testcase( iB==SMALLEST_INT64+1 );
   22085           27729 :   if( iB==SMALLEST_INT64 ){
   22086                 :     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
   22087               0 :     if( (*pA)>=0 ) return 1;
   22088               0 :     *pA -= iB;
   22089               0 :     return 0;
   22090                 :   }else{
   22091           27729 :     return sqlite3AddInt64(pA, -iB);
   22092                 :   }
   22093                 : }
   22094                 : #define TWOPOWER32 (((i64)1)<<32)
   22095                 : #define TWOPOWER31 (((i64)1)<<31)
   22096           12484 : SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
   22097           12484 :   i64 iA = *pA;
   22098                 :   i64 iA1, iA0, iB1, iB0, r;
   22099                 : 
   22100           12484 :   iA1 = iA/TWOPOWER32;
   22101           12484 :   iA0 = iA % TWOPOWER32;
   22102           12484 :   iB1 = iB/TWOPOWER32;
   22103           12484 :   iB0 = iB % TWOPOWER32;
   22104           12484 :   if( iA1*iB1 != 0 ) return 1;
   22105           12484 :   assert( iA1*iB0==0 || iA0*iB1==0 );
   22106           12484 :   r = iA1*iB0 + iA0*iB1;
   22107                 :   testcase( r==(-TWOPOWER31)-1 );
   22108                 :   testcase( r==(-TWOPOWER31) );
   22109                 :   testcase( r==TWOPOWER31 );
   22110                 :   testcase( r==TWOPOWER31-1 );
   22111           12484 :   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
   22112           12484 :   r *= TWOPOWER32;
   22113           12484 :   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
   22114           12484 :   *pA = r;
   22115           12484 :   return 0;
   22116                 : }
   22117                 : 
   22118                 : /*
   22119                 : ** Compute the absolute value of a 32-bit signed integer, of possible.  Or 
   22120                 : ** if the integer has a value of -2147483648, return +2147483647
   22121                 : */
   22122            3746 : SQLITE_PRIVATE int sqlite3AbsInt32(int x){
   22123            3746 :   if( x>=0 ) return x;
   22124               0 :   if( x==(int)0x80000000 ) return 0x7fffffff;
   22125               0 :   return -x;
   22126                 : }
   22127                 : 
   22128                 : #ifdef SQLITE_ENABLE_8_3_NAMES
   22129                 : /*
   22130                 : ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
   22131                 : ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
   22132                 : ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
   22133                 : ** three characters, then shorten the suffix on z[] to be the last three
   22134                 : ** characters of the original suffix.
   22135                 : **
   22136                 : ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
   22137                 : ** do the suffix shortening regardless of URI parameter.
   22138                 : **
   22139                 : ** Examples:
   22140                 : **
   22141                 : **     test.db-journal    =>   test.nal
   22142                 : **     test.db-wal        =>   test.wal
   22143                 : **     test.db-shm        =>   test.shm
   22144                 : **     test.db-mj7f3319fa =>   test.9fa
   22145                 : */
   22146                 : SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
   22147                 : #if SQLITE_ENABLE_8_3_NAMES<2
   22148                 :   if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
   22149                 : #endif
   22150                 :   {
   22151                 :     int i, sz;
   22152                 :     sz = sqlite3Strlen30(z);
   22153                 :     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
   22154                 :     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
   22155                 :   }
   22156                 : }
   22157                 : #endif
   22158                 : 
   22159                 : /************** End of util.c ************************************************/
   22160                 : /************** Begin file hash.c ********************************************/
   22161                 : /*
   22162                 : ** 2001 September 22
   22163                 : **
   22164                 : ** The author disclaims copyright to this source code.  In place of
   22165                 : ** a legal notice, here is a blessing:
   22166                 : **
   22167                 : **    May you do good and not evil.
   22168                 : **    May you find forgiveness for yourself and forgive others.
   22169                 : **    May you share freely, never taking more than you give.
   22170                 : **
   22171                 : *************************************************************************
   22172                 : ** This is the implementation of generic hash-tables
   22173                 : ** used in SQLite.
   22174                 : */
   22175                 : /* #include <assert.h> */
   22176                 : 
   22177                 : /* Turn bulk memory into a hash table object by initializing the
   22178                 : ** fields of the Hash structure.
   22179                 : **
   22180                 : ** "pNew" is a pointer to the hash table that is to be initialized.
   22181                 : */
   22182           59178 : SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
   22183           59178 :   assert( pNew!=0 );
   22184           59178 :   pNew->first = 0;
   22185           59178 :   pNew->count = 0;
   22186           59178 :   pNew->htsize = 0;
   22187           59178 :   pNew->ht = 0;
   22188           59178 : }
   22189                 : 
   22190                 : /* Remove all entries from a hash table.  Reclaim all memory.
   22191                 : ** Call this routine to delete a hash table or to reset a hash table
   22192                 : ** to the empty state.
   22193                 : */
   22194           61471 : SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
   22195                 :   HashElem *elem;         /* For looping over all elements of the table */
   22196                 : 
   22197           61471 :   assert( pH!=0 );
   22198           61471 :   elem = pH->first;
   22199           61471 :   pH->first = 0;
   22200           61471 :   sqlite3_free(pH->ht);
   22201           61471 :   pH->ht = 0;
   22202           61471 :   pH->htsize = 0;
   22203          218033 :   while( elem ){
   22204           95091 :     HashElem *next_elem = elem->next;
   22205           95091 :     sqlite3_free(elem);
   22206           95091 :     elem = next_elem;
   22207                 :   }
   22208           61471 :   pH->count = 0;
   22209           61471 : }
   22210                 : 
   22211                 : /*
   22212                 : ** The hashing function.
   22213                 : */
   22214          121949 : static unsigned int strHash(const char *z, int nKey){
   22215          121949 :   int h = 0;
   22216          121949 :   assert( nKey>=0 );
   22217         2365789 :   while( nKey > 0  ){
   22218         2121891 :     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
   22219         2121891 :     nKey--;
   22220                 :   }
   22221          121949 :   return h;
   22222                 : }
   22223                 : 
   22224                 : 
   22225                 : /* Link pNew element into the hash table pH.  If pEntry!=0 then also
   22226                 : ** insert pNew into the pEntry hash bucket.
   22227                 : */
   22228          108133 : static void insertElement(
   22229                 :   Hash *pH,              /* The complete hash table */
   22230                 :   struct _ht *pEntry,    /* The entry into which pNew is inserted */
   22231                 :   HashElem *pNew         /* The element to be inserted */
   22232                 : ){
   22233                 :   HashElem *pHead;       /* First element already in pEntry */
   22234          108133 :   if( pEntry ){
   22235           21131 :     pHead = pEntry->count ? pEntry->chain : 0;
   22236           21131 :     pEntry->count++;
   22237           21131 :     pEntry->chain = pNew;
   22238                 :   }else{
   22239           87002 :     pHead = 0;
   22240                 :   }
   22241          108133 :   if( pHead ){
   22242            7635 :     pNew->next = pHead;
   22243            7635 :     pNew->prev = pHead->prev;
   22244            7635 :     if( pHead->prev ){ pHead->prev->next = pNew; }
   22245            1737 :     else             { pH->first = pNew; }
   22246            7635 :     pHead->prev = pNew;
   22247                 :   }else{
   22248          100498 :     pNew->next = pH->first;
   22249          100498 :     if( pH->first ){ pH->first->prev = pNew; }
   22250          100498 :     pNew->prev = 0;
   22251          100498 :     pH->first = pNew;
   22252                 :   }
   22253          108133 : }
   22254                 : 
   22255                 : 
   22256                 : /* Resize the hash table so that it cantains "new_size" buckets.
   22257                 : **
   22258                 : ** The hash table might fail to resize if sqlite3_malloc() fails or
   22259                 : ** if the new size is the same as the prior size.
   22260                 : ** Return TRUE if the resize occurs and false if not.
   22261                 : */
   22262            1172 : static int rehash(Hash *pH, unsigned int new_size){
   22263                 :   struct _ht *new_ht;            /* The new hash table */
   22264                 :   HashElem *elem, *next_elem;    /* For looping over existing elements */
   22265                 : 
   22266                 : #if SQLITE_MALLOC_SOFT_LIMIT>0
   22267            1172 :   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
   22268               0 :     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
   22269                 :   }
   22270            1172 :   if( new_size==pH->htsize ) return 0;
   22271                 : #endif
   22272                 : 
   22273                 :   /* The inability to allocates space for a larger hash table is
   22274                 :   ** a performance hit but it is not a fatal error.  So mark the
   22275                 :   ** allocation as a benign.
   22276                 :   */
   22277            1172 :   sqlite3BeginBenignMalloc();
   22278            1172 :   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
   22279            1172 :   sqlite3EndBenignMalloc();
   22280                 : 
   22281            1172 :   if( new_ht==0 ) return 0;
   22282            1172 :   sqlite3_free(pH->ht);
   22283            1172 :   pH->ht = new_ht;
   22284            1172 :   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
   22285            1172 :   memset(new_ht, 0, new_size*sizeof(struct _ht));
   22286           11720 :   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
   22287           10548 :     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
   22288           10548 :     next_elem = elem->next;
   22289           10548 :     insertElement(pH, &new_ht[h], elem);
   22290                 :   }
   22291            1172 :   return 1;
   22292                 : }
   22293                 : 
   22294                 : /* This function (for internal use only) locates an element in an
   22295                 : ** hash table that matches the given key.  The hash for this key has
   22296                 : ** already been computed and is passed as the 4th parameter.
   22297                 : */
   22298          761754 : static HashElem *findElementGivenHash(
   22299                 :   const Hash *pH,     /* The pH to be searched */
   22300                 :   const char *pKey,   /* The key we are searching for */
   22301                 :   int nKey,           /* Bytes in key (not counting zero terminator) */
   22302                 :   unsigned int h      /* The hash for this key. */
   22303                 : ){
   22304                 :   HashElem *elem;                /* Used to loop thru the element list */
   22305                 :   int count;                     /* Number of elements left to test */
   22306                 : 
   22307          761754 :   if( pH->ht ){
   22308          110229 :     struct _ht *pEntry = &pH->ht[h];
   22309          110229 :     elem = pEntry->chain;
   22310          110229 :     count = pEntry->count;
   22311                 :   }else{
   22312          651525 :     elem = pH->first;
   22313          651525 :     count = pH->count;
   22314                 :   }
   22315         3315874 :   while( count-- && ALWAYS(elem) ){
   22316         2176449 :     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){ 
   22317          384083 :       return elem;
   22318                 :     }
   22319         1792366 :     elem = elem->next;
   22320                 :   }
   22321          377671 :   return 0;
   22322                 : }
   22323                 : 
   22324                 : /* Remove a single entry from the hash table given a pointer to that
   22325                 : ** element and a hash on the element's key.
   22326                 : */
   22327            2482 : static void removeElementGivenHash(
   22328                 :   Hash *pH,         /* The pH containing "elem" */
   22329                 :   HashElem* elem,   /* The element to be removed from the pH */
   22330                 :   unsigned int h    /* Hash value for the element */
   22331                 : ){
   22332                 :   struct _ht *pEntry;
   22333            2482 :   if( elem->prev ){
   22334              57 :     elem->prev->next = elem->next; 
   22335                 :   }else{
   22336            2425 :     pH->first = elem->next;
   22337                 :   }
   22338            2482 :   if( elem->next ){
   22339            1720 :     elem->next->prev = elem->prev;
   22340                 :   }
   22341            2482 :   if( pH->ht ){
   22342              58 :     pEntry = &pH->ht[h];
   22343              58 :     if( pEntry->chain==elem ){
   22344              47 :       pEntry->chain = elem->next;
   22345                 :     }
   22346              58 :     pEntry->count--;
   22347              58 :     assert( pEntry->count>=0 );
   22348                 :   }
   22349            2482 :   sqlite3_free( elem );
   22350            2482 :   pH->count--;
   22351            2482 :   if( pH->count<=0 ){
   22352             757 :     assert( pH->first==0 );
   22353             757 :     assert( pH->count==0 );
   22354             757 :     sqlite3HashClear(pH);
   22355                 :   }
   22356            2482 : }
   22357                 : 
   22358                 : /* Attempt to locate an element of the hash table pH with a key
   22359                 : ** that matches pKey,nKey.  Return the data for this element if it is
   22360                 : ** found, or NULL if there is no match.
   22361                 : */
   22362          629341 : SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
   22363                 :   HashElem *elem;    /* The element that matches key */
   22364                 :   unsigned int h;    /* A hash on key */
   22365                 : 
   22366          629341 :   assert( pH!=0 );
   22367          629341 :   assert( pKey!=0 );
   22368          629341 :   assert( nKey>=0 );
   22369          629341 :   if( pH->ht ){
   22370           99195 :     h = strHash(pKey, nKey) % pH->htsize;
   22371                 :   }else{
   22372          530146 :     h = 0;
   22373                 :   }
   22374          629341 :   elem = findElementGivenHash(pH, pKey, nKey, h);
   22375          629341 :   return elem ? elem->data : 0;
   22376                 : }
   22377                 : 
   22378                 : /* Insert an element into the hash table pH.  The key is pKey,nKey
   22379                 : ** and the data is "data".
   22380                 : **
   22381                 : ** If no element exists with a matching key, then a new
   22382                 : ** element is created and NULL is returned.
   22383                 : **
   22384                 : ** If another element already exists with the same key, then the
   22385                 : ** new data replaces the old data and the old data is returned.
   22386                 : ** The key is not copied in this instance.  If a malloc fails, then
   22387                 : ** the new data is returned and the hash table is unchanged.
   22388                 : **
   22389                 : ** If the "data" parameter to this function is NULL, then the
   22390                 : ** element corresponding to "key" is removed from the hash table.
   22391                 : */
   22392          132413 : SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
   22393                 :   unsigned int h;       /* the hash of the key modulo hash table size */
   22394                 :   HashElem *elem;       /* Used to loop thru the element list */
   22395                 :   HashElem *new_elem;   /* New element added to the pH */
   22396                 : 
   22397          132413 :   assert( pH!=0 );
   22398          132413 :   assert( pKey!=0 );
   22399          132413 :   assert( nKey>=0 );
   22400          132413 :   if( pH->htsize ){
   22401           11034 :     h = strHash(pKey, nKey) % pH->htsize;
   22402                 :   }else{
   22403          121379 :     h = 0;
   22404                 :   }
   22405          132413 :   elem = findElementGivenHash(pH,pKey,nKey,h);
   22406          132413 :   if( elem ){
   22407            6274 :     void *old_data = elem->data;
   22408            6274 :     if( data==0 ){
   22409            2482 :       removeElementGivenHash(pH,elem,h);
   22410                 :     }else{
   22411            3792 :       elem->data = data;
   22412            3792 :       elem->pKey = pKey;
   22413            3792 :       assert(nKey==elem->nKey);
   22414                 :     }
   22415            6274 :     return old_data;
   22416                 :   }
   22417          126139 :   if( data==0 ) return 0;
   22418           97585 :   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
   22419           97585 :   if( new_elem==0 ) return data;
   22420           97585 :   new_elem->pKey = pKey;
   22421           97585 :   new_elem->nKey = nKey;
   22422           97585 :   new_elem->data = data;
   22423           97585 :   pH->count++;
   22424           97585 :   if( pH->count>=10 && pH->count > 2*pH->htsize ){
   22425            1172 :     if( rehash(pH, pH->count*2) ){
   22426            1172 :       assert( pH->htsize>0 );
   22427            1172 :       h = strHash(pKey, nKey) % pH->htsize;
   22428                 :     }
   22429                 :   }
   22430           97585 :   if( pH->ht ){
   22431           10583 :     insertElement(pH, &pH->ht[h], new_elem);
   22432                 :   }else{
   22433           87002 :     insertElement(pH, 0, new_elem);
   22434                 :   }
   22435           97585 :   return 0;
   22436                 : }
   22437                 : 
   22438                 : /************** End of hash.c ************************************************/
   22439                 : /************** Begin file opcodes.c *****************************************/
   22440                 : /* Automatically generated.  Do not edit */
   22441                 : /* See the mkopcodec.awk script for details. */
   22442                 : #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
   22443               0 : SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
   22444                 :  static const char *const azName[] = { "?",
   22445                 :      /*   1 */ "Goto",
   22446                 :      /*   2 */ "Gosub",
   22447                 :      /*   3 */ "Return",
   22448                 :      /*   4 */ "Yield",
   22449                 :      /*   5 */ "HaltIfNull",
   22450                 :      /*   6 */ "Halt",
   22451                 :      /*   7 */ "Integer",
   22452                 :      /*   8 */ "Int64",
   22453                 :      /*   9 */ "String",
   22454                 :      /*  10 */ "Null",
   22455                 :      /*  11 */ "Blob",
   22456                 :      /*  12 */ "Variable",
   22457                 :      /*  13 */ "Move",
   22458                 :      /*  14 */ "Copy",
   22459                 :      /*  15 */ "SCopy",
   22460                 :      /*  16 */ "ResultRow",
   22461                 :      /*  17 */ "CollSeq",
   22462                 :      /*  18 */ "Function",
   22463                 :      /*  19 */ "Not",
   22464                 :      /*  20 */ "AddImm",
   22465                 :      /*  21 */ "MustBeInt",
   22466                 :      /*  22 */ "RealAffinity",
   22467                 :      /*  23 */ "Permutation",
   22468                 :      /*  24 */ "Compare",
   22469                 :      /*  25 */ "Jump",
   22470                 :      /*  26 */ "Once",
   22471                 :      /*  27 */ "If",
   22472                 :      /*  28 */ "IfNot",
   22473                 :      /*  29 */ "Column",
   22474                 :      /*  30 */ "Affinity",
   22475                 :      /*  31 */ "MakeRecord",
   22476                 :      /*  32 */ "Count",
   22477                 :      /*  33 */ "Savepoint",
   22478                 :      /*  34 */ "AutoCommit",
   22479                 :      /*  35 */ "Transaction",
   22480                 :      /*  36 */ "ReadCookie",
   22481                 :      /*  37 */ "SetCookie",
   22482                 :      /*  38 */ "VerifyCookie",
   22483                 :      /*  39 */ "OpenRead",
   22484                 :      /*  40 */ "OpenWrite",
   22485                 :      /*  41 */ "OpenAutoindex",
   22486                 :      /*  42 */ "OpenEphemeral",
   22487                 :      /*  43 */ "SorterOpen",
   22488                 :      /*  44 */ "OpenPseudo",
   22489                 :      /*  45 */ "Close",
   22490                 :      /*  46 */ "SeekLt",
   22491                 :      /*  47 */ "SeekLe",
   22492                 :      /*  48 */ "SeekGe",
   22493                 :      /*  49 */ "SeekGt",
   22494                 :      /*  50 */ "Seek",
   22495                 :      /*  51 */ "NotFound",
   22496                 :      /*  52 */ "Found",
   22497                 :      /*  53 */ "IsUnique",
   22498                 :      /*  54 */ "NotExists",
   22499                 :      /*  55 */ "Sequence",
   22500                 :      /*  56 */ "NewRowid",
   22501                 :      /*  57 */ "Insert",
   22502                 :      /*  58 */ "InsertInt",
   22503                 :      /*  59 */ "Delete",
   22504                 :      /*  60 */ "ResetCount",
   22505                 :      /*  61 */ "SorterCompare",
   22506                 :      /*  62 */ "SorterData",
   22507                 :      /*  63 */ "RowKey",
   22508                 :      /*  64 */ "RowData",
   22509                 :      /*  65 */ "Rowid",
   22510                 :      /*  66 */ "NullRow",
   22511                 :      /*  67 */ "Last",
   22512                 :      /*  68 */ "Or",
   22513                 :      /*  69 */ "And",
   22514                 :      /*  70 */ "SorterSort",
   22515                 :      /*  71 */ "Sort",
   22516                 :      /*  72 */ "Rewind",
   22517                 :      /*  73 */ "IsNull",
   22518                 :      /*  74 */ "NotNull",
   22519                 :      /*  75 */ "Ne",
   22520                 :      /*  76 */ "Eq",
   22521                 :      /*  77 */ "Gt",
   22522                 :      /*  78 */ "Le",
   22523                 :      /*  79 */ "Lt",
   22524                 :      /*  80 */ "Ge",
   22525                 :      /*  81 */ "SorterNext",
   22526                 :      /*  82 */ "BitAnd",
   22527                 :      /*  83 */ "BitOr",
   22528                 :      /*  84 */ "ShiftLeft",
   22529                 :      /*  85 */ "ShiftRight",
   22530                 :      /*  86 */ "Add",
   22531                 :      /*  87 */ "Subtract",
   22532                 :      /*  88 */ "Multiply",
   22533                 :      /*  89 */ "Divide",
   22534                 :      /*  90 */ "Remainder",
   22535                 :      /*  91 */ "Concat",
   22536                 :      /*  92 */ "Prev",
   22537                 :      /*  93 */ "BitNot",
   22538                 :      /*  94 */ "String8",
   22539                 :      /*  95 */ "Next",
   22540                 :      /*  96 */ "SorterInsert",
   22541                 :      /*  97 */ "IdxInsert",
   22542                 :      /*  98 */ "IdxDelete",
   22543                 :      /*  99 */ "IdxRowid",
   22544                 :      /* 100 */ "IdxLT",
   22545                 :      /* 101 */ "IdxGE",
   22546                 :      /* 102 */ "Destroy",
   22547                 :      /* 103 */ "Clear",
   22548                 :      /* 104 */ "CreateIndex",
   22549                 :      /* 105 */ "CreateTable",
   22550                 :      /* 106 */ "ParseSchema",
   22551                 :      /* 107 */ "LoadAnalysis",
   22552                 :      /* 108 */ "DropTable",
   22553                 :      /* 109 */ "DropIndex",
   22554                 :      /* 110 */ "DropTrigger",
   22555                 :      /* 111 */ "IntegrityCk",
   22556                 :      /* 112 */ "RowSetAdd",
   22557                 :      /* 113 */ "RowSetRead",
   22558                 :      /* 114 */ "RowSetTest",
   22559                 :      /* 115 */ "Program",
   22560                 :      /* 116 */ "Param",
   22561                 :      /* 117 */ "FkCounter",
   22562                 :      /* 118 */ "FkIfZero",
   22563                 :      /* 119 */ "MemMax",
   22564                 :      /* 120 */ "IfPos",
   22565                 :      /* 121 */ "IfNeg",
   22566                 :      /* 122 */ "IfZero",
   22567                 :      /* 123 */ "AggStep",
   22568                 :      /* 124 */ "AggFinal",
   22569                 :      /* 125 */ "Checkpoint",
   22570                 :      /* 126 */ "JournalMode",
   22571                 :      /* 127 */ "Vacuum",
   22572                 :      /* 128 */ "IncrVacuum",
   22573                 :      /* 129 */ "Expire",
   22574                 :      /* 130 */ "Real",
   22575                 :      /* 131 */ "TableLock",
   22576                 :      /* 132 */ "VBegin",
   22577                 :      /* 133 */ "VCreate",
   22578                 :      /* 134 */ "VDestroy",
   22579                 :      /* 135 */ "VOpen",
   22580                 :      /* 136 */ "VFilter",
   22581                 :      /* 137 */ "VColumn",
   22582                 :      /* 138 */ "VNext",
   22583                 :      /* 139 */ "VRename",
   22584                 :      /* 140 */ "VUpdate",
   22585                 :      /* 141 */ "ToText",
   22586                 :      /* 142 */ "ToBlob",
   22587                 :      /* 143 */ "ToNumeric",
   22588                 :      /* 144 */ "ToInt",
   22589                 :      /* 145 */ "ToReal",
   22590                 :      /* 146 */ "Pagecount",
   22591                 :      /* 147 */ "MaxPgcnt",
   22592                 :      /* 148 */ "Trace",
   22593                 :      /* 149 */ "Noop",
   22594                 :      /* 150 */ "Explain",
   22595                 :   };
   22596               0 :   return azName[i];
   22597                 : }
   22598                 : #endif
   22599                 : 
   22600                 : /************** End of opcodes.c *********************************************/
   22601                 : /************** Begin file os_os2.c ******************************************/
   22602                 : /*
   22603                 : ** 2006 Feb 14
   22604                 : **
   22605                 : ** The author disclaims copyright to this source code.  In place of
   22606                 : ** a legal notice, here is a blessing:
   22607                 : **
   22608                 : **    May you do good and not evil.
   22609                 : **    May you find forgiveness for yourself and forgive others.
   22610                 : **    May you share freely, never taking more than you give.
   22611                 : **
   22612                 : ******************************************************************************
   22613                 : **
   22614                 : ** This file contains code that is specific to OS/2.
   22615                 : */
   22616                 : 
   22617                 : 
   22618                 : #if SQLITE_OS_OS2
   22619                 : 
   22620                 : /*
   22621                 : ** A Note About Memory Allocation:
   22622                 : **
   22623                 : ** This driver uses malloc()/free() directly rather than going through
   22624                 : ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free().  Those wrappers
   22625                 : ** are designed for use on embedded systems where memory is scarce and
   22626                 : ** malloc failures happen frequently.  OS/2 does not typically run on
   22627                 : ** embedded systems, and when it does the developers normally have bigger
   22628                 : ** problems to worry about than running out of memory.  So there is not
   22629                 : ** a compelling need to use the wrappers.
   22630                 : **
   22631                 : ** But there is a good reason to not use the wrappers.  If we use the
   22632                 : ** wrappers then we will get simulated malloc() failures within this
   22633                 : ** driver.  And that causes all kinds of problems for our tests.  We
   22634                 : ** could enhance SQLite to deal with simulated malloc failures within
   22635                 : ** the OS driver, but the code to deal with those failure would not
   22636                 : ** be exercised on Linux (which does not need to malloc() in the driver)
   22637                 : ** and so we would have difficulty writing coverage tests for that
   22638                 : ** code.  Better to leave the code out, we think.
   22639                 : **
   22640                 : ** The point of this discussion is as follows:  When creating a new
   22641                 : ** OS layer for an embedded system, if you use this file as an example,
   22642                 : ** avoid the use of malloc()/free().  Those routines work ok on OS/2
   22643                 : ** desktops but not so well in embedded systems.
   22644                 : */
   22645                 : 
   22646                 : /*
   22647                 : ** Macros used to determine whether or not to use threads.
   22648                 : */
   22649                 : #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE
   22650                 : # define SQLITE_OS2_THREADS 1
   22651                 : #endif
   22652                 : 
   22653                 : /*
   22654                 : ** Include code that is common to all os_*.c files
   22655                 : */
   22656                 : /************** Include os_common.h in the middle of os_os2.c ****************/
   22657                 : /************** Begin file os_common.h ***************************************/
   22658                 : /*
   22659                 : ** 2004 May 22
   22660                 : **
   22661                 : ** The author disclaims copyright to this source code.  In place of
   22662                 : ** a legal notice, here is a blessing:
   22663                 : **
   22664                 : **    May you do good and not evil.
   22665                 : **    May you find forgiveness for yourself and forgive others.
   22666                 : **    May you share freely, never taking more than you give.
   22667                 : **
   22668                 : ******************************************************************************
   22669                 : **
   22670                 : ** This file contains macros and a little bit of code that is common to
   22671                 : ** all of the platform-specific files (os_*.c) and is #included into those
   22672                 : ** files.
   22673                 : **
   22674                 : ** This file should be #included by the os_*.c files only.  It is not a
   22675                 : ** general purpose header file.
   22676                 : */
   22677                 : #ifndef _OS_COMMON_H_
   22678                 : #define _OS_COMMON_H_
   22679                 : 
   22680                 : /*
   22681                 : ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
   22682                 : ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
   22683                 : ** switch.  The following code should catch this problem at compile-time.
   22684                 : */
   22685                 : #ifdef MEMORY_DEBUG
   22686                 : # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
   22687                 : #endif
   22688                 : 
   22689                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   22690                 : # ifndef SQLITE_DEBUG_OS_TRACE
   22691                 : #   define SQLITE_DEBUG_OS_TRACE 0
   22692                 : # endif
   22693                 :   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
   22694                 : # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
   22695                 : #else
   22696                 : # define OSTRACE(X)
   22697                 : #endif
   22698                 : 
   22699                 : /*
   22700                 : ** Macros for performance tracing.  Normally turned off.  Only works
   22701                 : ** on i486 hardware.
   22702                 : */
   22703                 : #ifdef SQLITE_PERFORMANCE_TRACE
   22704                 : 
   22705                 : /* 
   22706                 : ** hwtime.h contains inline assembler code for implementing 
   22707                 : ** high-performance timing routines.
   22708                 : */
   22709                 : /************** Include hwtime.h in the middle of os_common.h ****************/
   22710                 : /************** Begin file hwtime.h ******************************************/
   22711                 : /*
   22712                 : ** 2008 May 27
   22713                 : **
   22714                 : ** The author disclaims copyright to this source code.  In place of
   22715                 : ** a legal notice, here is a blessing:
   22716                 : **
   22717                 : **    May you do good and not evil.
   22718                 : **    May you find forgiveness for yourself and forgive others.
   22719                 : **    May you share freely, never taking more than you give.
   22720                 : **
   22721                 : ******************************************************************************
   22722                 : **
   22723                 : ** This file contains inline asm code for retrieving "high-performance"
   22724                 : ** counters for x86 class CPUs.
   22725                 : */
   22726                 : #ifndef _HWTIME_H_
   22727                 : #define _HWTIME_H_
   22728                 : 
   22729                 : /*
   22730                 : ** The following routine only works on pentium-class (or newer) processors.
   22731                 : ** It uses the RDTSC opcode to read the cycle count value out of the
   22732                 : ** processor and returns that value.  This can be used for high-res
   22733                 : ** profiling.
   22734                 : */
   22735                 : #if (defined(__GNUC__) || defined(_MSC_VER)) && \
   22736                 :       (defined(i386) || defined(__i386__) || defined(_M_IX86))
   22737                 : 
   22738                 :   #if defined(__GNUC__)
   22739                 : 
   22740                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   22741                 :      unsigned int lo, hi;
   22742                 :      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   22743                 :      return (sqlite_uint64)hi << 32 | lo;
   22744                 :   }
   22745                 : 
   22746                 :   #elif defined(_MSC_VER)
   22747                 : 
   22748                 :   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
   22749                 :      __asm {
   22750                 :         rdtsc
   22751                 :         ret       ; return value at EDX:EAX
   22752                 :      }
   22753                 :   }
   22754                 : 
   22755                 :   #endif
   22756                 : 
   22757                 : #elif (defined(__GNUC__) && defined(__x86_64__))
   22758                 : 
   22759                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   22760                 :       unsigned long val;
   22761                 :       __asm__ __volatile__ ("rdtsc" : "=A" (val));
   22762                 :       return val;
   22763                 :   }
   22764                 :  
   22765                 : #elif (defined(__GNUC__) && defined(__ppc__))
   22766                 : 
   22767                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   22768                 :       unsigned long long retval;
   22769                 :       unsigned long junk;
   22770                 :       __asm__ __volatile__ ("\n\
   22771                 :           1:      mftbu   %1\n\
   22772                 :                   mftb    %L0\n\
   22773                 :                   mftbu   %0\n\
   22774                 :                   cmpw    %0,%1\n\
   22775                 :                   bne     1b"
   22776                 :                   : "=r" (retval), "=r" (junk));
   22777                 :       return retval;
   22778                 :   }
   22779                 : 
   22780                 : #else
   22781                 : 
   22782                 :   #error Need implementation of sqlite3Hwtime() for your platform.
   22783                 : 
   22784                 :   /*
   22785                 :   ** To compile without implementing sqlite3Hwtime() for your platform,
   22786                 :   ** you can remove the above #error and use the following
   22787                 :   ** stub function.  You will lose timing support for many
   22788                 :   ** of the debugging and testing utilities, but it should at
   22789                 :   ** least compile and run.
   22790                 :   */
   22791                 : SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
   22792                 : 
   22793                 : #endif
   22794                 : 
   22795                 : #endif /* !defined(_HWTIME_H_) */
   22796                 : 
   22797                 : /************** End of hwtime.h **********************************************/
   22798                 : /************** Continuing where we left off in os_common.h ******************/
   22799                 : 
   22800                 : static sqlite_uint64 g_start;
   22801                 : static sqlite_uint64 g_elapsed;
   22802                 : #define TIMER_START       g_start=sqlite3Hwtime()
   22803                 : #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
   22804                 : #define TIMER_ELAPSED     g_elapsed
   22805                 : #else
   22806                 : #define TIMER_START
   22807                 : #define TIMER_END
   22808                 : #define TIMER_ELAPSED     ((sqlite_uint64)0)
   22809                 : #endif
   22810                 : 
   22811                 : /*
   22812                 : ** If we compile with the SQLITE_TEST macro set, then the following block
   22813                 : ** of code will give us the ability to simulate a disk I/O error.  This
   22814                 : ** is used for testing the I/O recovery logic.
   22815                 : */
   22816                 : #ifdef SQLITE_TEST
   22817                 : SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
   22818                 : SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
   22819                 : SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
   22820                 : SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
   22821                 : SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
   22822                 : SQLITE_API int sqlite3_diskfull_pending = 0;
   22823                 : SQLITE_API int sqlite3_diskfull = 0;
   22824                 : #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
   22825                 : #define SimulateIOError(CODE)  \
   22826                 :   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
   22827                 :        || sqlite3_io_error_pending-- == 1 )  \
   22828                 :               { local_ioerr(); CODE; }
   22829                 : static void local_ioerr(){
   22830                 :   IOTRACE(("IOERR\n"));
   22831                 :   sqlite3_io_error_hit++;
   22832                 :   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
   22833                 : }
   22834                 : #define SimulateDiskfullError(CODE) \
   22835                 :    if( sqlite3_diskfull_pending ){ \
   22836                 :      if( sqlite3_diskfull_pending == 1 ){ \
   22837                 :        local_ioerr(); \
   22838                 :        sqlite3_diskfull = 1; \
   22839                 :        sqlite3_io_error_hit = 1; \
   22840                 :        CODE; \
   22841                 :      }else{ \
   22842                 :        sqlite3_diskfull_pending--; \
   22843                 :      } \
   22844                 :    }
   22845                 : #else
   22846                 : #define SimulateIOErrorBenign(X)
   22847                 : #define SimulateIOError(A)
   22848                 : #define SimulateDiskfullError(A)
   22849                 : #endif
   22850                 : 
   22851                 : /*
   22852                 : ** When testing, keep a count of the number of open files.
   22853                 : */
   22854                 : #ifdef SQLITE_TEST
   22855                 : SQLITE_API int sqlite3_open_file_count = 0;
   22856                 : #define OpenCounter(X)  sqlite3_open_file_count+=(X)
   22857                 : #else
   22858                 : #define OpenCounter(X)
   22859                 : #endif
   22860                 : 
   22861                 : #endif /* !defined(_OS_COMMON_H_) */
   22862                 : 
   22863                 : /************** End of os_common.h *******************************************/
   22864                 : /************** Continuing where we left off in os_os2.c *********************/
   22865                 : 
   22866                 : /* Forward references */
   22867                 : typedef struct os2File os2File;         /* The file structure */
   22868                 : typedef struct os2ShmNode os2ShmNode;   /* A shared descritive memory node */
   22869                 : typedef struct os2ShmLink os2ShmLink;   /* A connection to shared-memory */
   22870                 : 
   22871                 : /*
   22872                 : ** The os2File structure is subclass of sqlite3_file specific for the OS/2
   22873                 : ** protability layer.
   22874                 : */
   22875                 : struct os2File {
   22876                 :   const sqlite3_io_methods *pMethod;  /* Always the first entry */
   22877                 :   HFILE h;                  /* Handle for accessing the file */
   22878                 :   int flags;                /* Flags provided to os2Open() */
   22879                 :   int locktype;             /* Type of lock currently held on this file */
   22880                 :   int szChunk;              /* Chunk size configured by FCNTL_CHUNK_SIZE */
   22881                 :   char *zFullPathCp;        /* Full path name of this file */
   22882                 :   os2ShmLink *pShmLink;     /* Instance of shared memory on this file */
   22883                 : };
   22884                 : 
   22885                 : #define LOCK_TIMEOUT 10L /* the default locking timeout */
   22886                 : 
   22887                 : /*
   22888                 : ** Missing from some versions of the OS/2 toolkit -
   22889                 : ** used to allocate from high memory if possible
   22890                 : */
   22891                 : #ifndef OBJ_ANY
   22892                 : # define OBJ_ANY 0x00000400
   22893                 : #endif
   22894                 : 
   22895                 : /*****************************************************************************
   22896                 : ** The next group of routines implement the I/O methods specified
   22897                 : ** by the sqlite3_io_methods object.
   22898                 : ******************************************************************************/
   22899                 : 
   22900                 : /*
   22901                 : ** Close a file.
   22902                 : */
   22903                 : static int os2Close( sqlite3_file *id ){
   22904                 :   APIRET rc;
   22905                 :   os2File *pFile = (os2File*)id;
   22906                 : 
   22907                 :   assert( id!=0 );
   22908                 :   OSTRACE(( "CLOSE %d (%s)\n", pFile->h, pFile->zFullPathCp ));
   22909                 : 
   22910                 :   rc = DosClose( pFile->h );
   22911                 : 
   22912                 :   if( pFile->flags & SQLITE_OPEN_DELETEONCLOSE )
   22913                 :     DosForceDelete( (PSZ)pFile->zFullPathCp );
   22914                 : 
   22915                 :   free( pFile->zFullPathCp );
   22916                 :   pFile->zFullPathCp = NULL;
   22917                 :   pFile->locktype = NO_LOCK;
   22918                 :   pFile->h = (HFILE)-1;
   22919                 :   pFile->flags = 0;
   22920                 : 
   22921                 :   OpenCounter( -1 );
   22922                 :   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   22923                 : }
   22924                 : 
   22925                 : /*
   22926                 : ** Read data from a file into a buffer.  Return SQLITE_OK if all
   22927                 : ** bytes were read successfully and SQLITE_IOERR if anything goes
   22928                 : ** wrong.
   22929                 : */
   22930                 : static int os2Read(
   22931                 :   sqlite3_file *id,               /* File to read from */
   22932                 :   void *pBuf,                     /* Write content into this buffer */
   22933                 :   int amt,                        /* Number of bytes to read */
   22934                 :   sqlite3_int64 offset            /* Begin reading at this offset */
   22935                 : ){
   22936                 :   ULONG fileLocation = 0L;
   22937                 :   ULONG got;
   22938                 :   os2File *pFile = (os2File*)id;
   22939                 :   assert( id!=0 );
   22940                 :   SimulateIOError( return SQLITE_IOERR_READ );
   22941                 :   OSTRACE(( "READ %d lock=%d\n", pFile->h, pFile->locktype ));
   22942                 :   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
   22943                 :     return SQLITE_IOERR;
   22944                 :   }
   22945                 :   if( DosRead( pFile->h, pBuf, amt, &got ) != NO_ERROR ){
   22946                 :     return SQLITE_IOERR_READ;
   22947                 :   }
   22948                 :   if( got == (ULONG)amt )
   22949                 :     return SQLITE_OK;
   22950                 :   else {
   22951                 :     /* Unread portions of the input buffer must be zero-filled */
   22952                 :     memset(&((char*)pBuf)[got], 0, amt-got);
   22953                 :     return SQLITE_IOERR_SHORT_READ;
   22954                 :   }
   22955                 : }
   22956                 : 
   22957                 : /*
   22958                 : ** Write data from a buffer into a file.  Return SQLITE_OK on success
   22959                 : ** or some other error code on failure.
   22960                 : */
   22961                 : static int os2Write(
   22962                 :   sqlite3_file *id,               /* File to write into */
   22963                 :   const void *pBuf,               /* The bytes to be written */
   22964                 :   int amt,                        /* Number of bytes to write */
   22965                 :   sqlite3_int64 offset            /* Offset into the file to begin writing at */
   22966                 : ){
   22967                 :   ULONG fileLocation = 0L;
   22968                 :   APIRET rc = NO_ERROR;
   22969                 :   ULONG wrote;
   22970                 :   os2File *pFile = (os2File*)id;
   22971                 :   assert( id!=0 );
   22972                 :   SimulateIOError( return SQLITE_IOERR_WRITE );
   22973                 :   SimulateDiskfullError( return SQLITE_FULL );
   22974                 :   OSTRACE(( "WRITE %d lock=%d\n", pFile->h, pFile->locktype ));
   22975                 :   if( DosSetFilePtr(pFile->h, offset, FILE_BEGIN, &fileLocation) != NO_ERROR ){
   22976                 :     return SQLITE_IOERR;
   22977                 :   }
   22978                 :   assert( amt>0 );
   22979                 :   while( amt > 0 &&
   22980                 :          ( rc = DosWrite( pFile->h, (PVOID)pBuf, amt, &wrote ) ) == NO_ERROR &&
   22981                 :          wrote > 0
   22982                 :   ){
   22983                 :     amt -= wrote;
   22984                 :     pBuf = &((char*)pBuf)[wrote];
   22985                 :   }
   22986                 : 
   22987                 :   return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
   22988                 : }
   22989                 : 
   22990                 : /*
   22991                 : ** Truncate an open file to a specified size
   22992                 : */
   22993                 : static int os2Truncate( sqlite3_file *id, i64 nByte ){
   22994                 :   APIRET rc;
   22995                 :   os2File *pFile = (os2File*)id;
   22996                 :   assert( id!=0 );
   22997                 :   OSTRACE(( "TRUNCATE %d %lld\n", pFile->h, nByte ));
   22998                 :   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
   22999                 : 
   23000                 :   /* If the user has configured a chunk-size for this file, truncate the
   23001                 :   ** file so that it consists of an integer number of chunks (i.e. the
   23002                 :   ** actual file size after the operation may be larger than the requested
   23003                 :   ** size).
   23004                 :   */
   23005                 :   if( pFile->szChunk ){
   23006                 :     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
   23007                 :   }
   23008                 :   
   23009                 :   rc = DosSetFileSize( pFile->h, nByte );
   23010                 :   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
   23011                 : }
   23012                 : 
   23013                 : #ifdef SQLITE_TEST
   23014                 : /*
   23015                 : ** Count the number of fullsyncs and normal syncs.  This is used to test
   23016                 : ** that syncs and fullsyncs are occuring at the right times.
   23017                 : */
   23018                 : SQLITE_API int sqlite3_sync_count = 0;
   23019                 : SQLITE_API int sqlite3_fullsync_count = 0;
   23020                 : #endif
   23021                 : 
   23022                 : /*
   23023                 : ** Make sure all writes to a particular file are committed to disk.
   23024                 : */
   23025                 : static int os2Sync( sqlite3_file *id, int flags ){
   23026                 :   os2File *pFile = (os2File*)id;
   23027                 :   OSTRACE(( "SYNC %d lock=%d\n", pFile->h, pFile->locktype ));
   23028                 : #ifdef SQLITE_TEST
   23029                 :   if( flags & SQLITE_SYNC_FULL){
   23030                 :     sqlite3_fullsync_count++;
   23031                 :   }
   23032                 :   sqlite3_sync_count++;
   23033                 : #endif
   23034                 :   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
   23035                 :   ** no-op
   23036                 :   */
   23037                 : #ifdef SQLITE_NO_SYNC
   23038                 :   UNUSED_PARAMETER(pFile);
   23039                 :   return SQLITE_OK;
   23040                 : #else
   23041                 :   return DosResetBuffer( pFile->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   23042                 : #endif
   23043                 : }
   23044                 : 
   23045                 : /*
   23046                 : ** Determine the current size of a file in bytes
   23047                 : */
   23048                 : static int os2FileSize( sqlite3_file *id, sqlite3_int64 *pSize ){
   23049                 :   APIRET rc = NO_ERROR;
   23050                 :   FILESTATUS3 fsts3FileInfo;
   23051                 :   memset(&fsts3FileInfo, 0, sizeof(fsts3FileInfo));
   23052                 :   assert( id!=0 );
   23053                 :   SimulateIOError( return SQLITE_IOERR_FSTAT );
   23054                 :   rc = DosQueryFileInfo( ((os2File*)id)->h, FIL_STANDARD, &fsts3FileInfo, sizeof(FILESTATUS3) );
   23055                 :   if( rc == NO_ERROR ){
   23056                 :     *pSize = fsts3FileInfo.cbFile;
   23057                 :     return SQLITE_OK;
   23058                 :   }else{
   23059                 :     return SQLITE_IOERR_FSTAT;
   23060                 :   }
   23061                 : }
   23062                 : 
   23063                 : /*
   23064                 : ** Acquire a reader lock.
   23065                 : */
   23066                 : static int getReadLock( os2File *pFile ){
   23067                 :   FILELOCK  LockArea,
   23068                 :             UnlockArea;
   23069                 :   APIRET res;
   23070                 :   memset(&LockArea, 0, sizeof(LockArea));
   23071                 :   memset(&UnlockArea, 0, sizeof(UnlockArea));
   23072                 :   LockArea.lOffset = SHARED_FIRST;
   23073                 :   LockArea.lRange = SHARED_SIZE;
   23074                 :   UnlockArea.lOffset = 0L;
   23075                 :   UnlockArea.lRange = 0L;
   23076                 :   res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
   23077                 :   OSTRACE(( "GETREADLOCK %d res=%d\n", pFile->h, res ));
   23078                 :   return res;
   23079                 : }
   23080                 : 
   23081                 : /*
   23082                 : ** Undo a readlock
   23083                 : */
   23084                 : static int unlockReadLock( os2File *id ){
   23085                 :   FILELOCK  LockArea,
   23086                 :             UnlockArea;
   23087                 :   APIRET res;
   23088                 :   memset(&LockArea, 0, sizeof(LockArea));
   23089                 :   memset(&UnlockArea, 0, sizeof(UnlockArea));
   23090                 :   LockArea.lOffset = 0L;
   23091                 :   LockArea.lRange = 0L;
   23092                 :   UnlockArea.lOffset = SHARED_FIRST;
   23093                 :   UnlockArea.lRange = SHARED_SIZE;
   23094                 :   res = DosSetFileLocks( id->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 1L );
   23095                 :   OSTRACE(( "UNLOCK-READLOCK file handle=%d res=%d?\n", id->h, res ));
   23096                 :   return res;
   23097                 : }
   23098                 : 
   23099                 : /*
   23100                 : ** Lock the file with the lock specified by parameter locktype - one
   23101                 : ** of the following:
   23102                 : **
   23103                 : **     (1) SHARED_LOCK
   23104                 : **     (2) RESERVED_LOCK
   23105                 : **     (3) PENDING_LOCK
   23106                 : **     (4) EXCLUSIVE_LOCK
   23107                 : **
   23108                 : ** Sometimes when requesting one lock state, additional lock states
   23109                 : ** are inserted in between.  The locking might fail on one of the later
   23110                 : ** transitions leaving the lock state different from what it started but
   23111                 : ** still short of its goal.  The following chart shows the allowed
   23112                 : ** transitions and the inserted intermediate states:
   23113                 : **
   23114                 : **    UNLOCKED -> SHARED
   23115                 : **    SHARED -> RESERVED
   23116                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   23117                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   23118                 : **    PENDING -> EXCLUSIVE
   23119                 : **
   23120                 : ** This routine will only increase a lock.  The os2Unlock() routine
   23121                 : ** erases all locks at once and returns us immediately to locking level 0.
   23122                 : ** It is not possible to lower the locking level one step at a time.  You
   23123                 : ** must go straight to locking level 0.
   23124                 : */
   23125                 : static int os2Lock( sqlite3_file *id, int locktype ){
   23126                 :   int rc = SQLITE_OK;       /* Return code from subroutines */
   23127                 :   APIRET res = NO_ERROR;    /* Result of an OS/2 lock call */
   23128                 :   int newLocktype;       /* Set pFile->locktype to this value before exiting */
   23129                 :   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
   23130                 :   FILELOCK  LockArea,
   23131                 :             UnlockArea;
   23132                 :   os2File *pFile = (os2File*)id;
   23133                 :   memset(&LockArea, 0, sizeof(LockArea));
   23134                 :   memset(&UnlockArea, 0, sizeof(UnlockArea));
   23135                 :   assert( pFile!=0 );
   23136                 :   OSTRACE(( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype ));
   23137                 : 
   23138                 :   /* If there is already a lock of this type or more restrictive on the
   23139                 :   ** os2File, do nothing. Don't use the end_lock: exit path, as
   23140                 :   ** sqlite3_mutex_enter() hasn't been called yet.
   23141                 :   */
   23142                 :   if( pFile->locktype>=locktype ){
   23143                 :     OSTRACE(( "LOCK %d %d ok (already held)\n", pFile->h, locktype ));
   23144                 :     return SQLITE_OK;
   23145                 :   }
   23146                 : 
   23147                 :   /* Make sure the locking sequence is correct
   23148                 :   */
   23149                 :   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
   23150                 :   assert( locktype!=PENDING_LOCK );
   23151                 :   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
   23152                 : 
   23153                 :   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
   23154                 :   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
   23155                 :   ** the PENDING_LOCK byte is temporary.
   23156                 :   */
   23157                 :   newLocktype = pFile->locktype;
   23158                 :   if( pFile->locktype==NO_LOCK
   23159                 :       || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
   23160                 :   ){
   23161                 :     LockArea.lOffset = PENDING_BYTE;
   23162                 :     LockArea.lRange = 1L;
   23163                 :     UnlockArea.lOffset = 0L;
   23164                 :     UnlockArea.lRange = 0L;
   23165                 : 
   23166                 :     /* wait longer than LOCK_TIMEOUT here not to have to try multiple times */
   23167                 :     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 100L, 0L );
   23168                 :     if( res == NO_ERROR ){
   23169                 :       gotPendingLock = 1;
   23170                 :       OSTRACE(( "LOCK %d pending lock boolean set.  res=%d\n", pFile->h, res ));
   23171                 :     }
   23172                 :   }
   23173                 : 
   23174                 :   /* Acquire a shared lock
   23175                 :   */
   23176                 :   if( locktype==SHARED_LOCK && res == NO_ERROR ){
   23177                 :     assert( pFile->locktype==NO_LOCK );
   23178                 :     res = getReadLock(pFile);
   23179                 :     if( res == NO_ERROR ){
   23180                 :       newLocktype = SHARED_LOCK;
   23181                 :     }
   23182                 :     OSTRACE(( "LOCK %d acquire shared lock. res=%d\n", pFile->h, res ));
   23183                 :   }
   23184                 : 
   23185                 :   /* Acquire a RESERVED lock
   23186                 :   */
   23187                 :   if( locktype==RESERVED_LOCK && res == NO_ERROR ){
   23188                 :     assert( pFile->locktype==SHARED_LOCK );
   23189                 :     LockArea.lOffset = RESERVED_BYTE;
   23190                 :     LockArea.lRange = 1L;
   23191                 :     UnlockArea.lOffset = 0L;
   23192                 :     UnlockArea.lRange = 0L;
   23193                 :     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23194                 :     if( res == NO_ERROR ){
   23195                 :       newLocktype = RESERVED_LOCK;
   23196                 :     }
   23197                 :     OSTRACE(( "LOCK %d acquire reserved lock. res=%d\n", pFile->h, res ));
   23198                 :   }
   23199                 : 
   23200                 :   /* Acquire a PENDING lock
   23201                 :   */
   23202                 :   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
   23203                 :     newLocktype = PENDING_LOCK;
   23204                 :     gotPendingLock = 0;
   23205                 :     OSTRACE(( "LOCK %d acquire pending lock. pending lock boolean unset.\n",
   23206                 :                pFile->h ));
   23207                 :   }
   23208                 : 
   23209                 :   /* Acquire an EXCLUSIVE lock
   23210                 :   */
   23211                 :   if( locktype==EXCLUSIVE_LOCK && res == NO_ERROR ){
   23212                 :     assert( pFile->locktype>=SHARED_LOCK );
   23213                 :     res = unlockReadLock(pFile);
   23214                 :     OSTRACE(( "unreadlock = %d\n", res ));
   23215                 :     LockArea.lOffset = SHARED_FIRST;
   23216                 :     LockArea.lRange = SHARED_SIZE;
   23217                 :     UnlockArea.lOffset = 0L;
   23218                 :     UnlockArea.lRange = 0L;
   23219                 :     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23220                 :     if( res == NO_ERROR ){
   23221                 :       newLocktype = EXCLUSIVE_LOCK;
   23222                 :     }else{
   23223                 :       OSTRACE(( "OS/2 error-code = %d\n", res ));
   23224                 :       getReadLock(pFile);
   23225                 :     }
   23226                 :     OSTRACE(( "LOCK %d acquire exclusive lock.  res=%d\n", pFile->h, res ));
   23227                 :   }
   23228                 : 
   23229                 :   /* If we are holding a PENDING lock that ought to be released, then
   23230                 :   ** release it now.
   23231                 :   */
   23232                 :   if( gotPendingLock && locktype==SHARED_LOCK ){
   23233                 :     int r;
   23234                 :     LockArea.lOffset = 0L;
   23235                 :     LockArea.lRange = 0L;
   23236                 :     UnlockArea.lOffset = PENDING_BYTE;
   23237                 :     UnlockArea.lRange = 1L;
   23238                 :     r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23239                 :     OSTRACE(( "LOCK %d unlocking pending/is shared. r=%d\n", pFile->h, r ));
   23240                 :   }
   23241                 : 
   23242                 :   /* Update the state of the lock has held in the file descriptor then
   23243                 :   ** return the appropriate result code.
   23244                 :   */
   23245                 :   if( res == NO_ERROR ){
   23246                 :     rc = SQLITE_OK;
   23247                 :   }else{
   23248                 :     OSTRACE(( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
   23249                 :               locktype, newLocktype ));
   23250                 :     rc = SQLITE_BUSY;
   23251                 :   }
   23252                 :   pFile->locktype = newLocktype;
   23253                 :   OSTRACE(( "LOCK %d now %d\n", pFile->h, pFile->locktype ));
   23254                 :   return rc;
   23255                 : }
   23256                 : 
   23257                 : /*
   23258                 : ** This routine checks if there is a RESERVED lock held on the specified
   23259                 : ** file by this or any other process. If such a lock is held, return
   23260                 : ** non-zero, otherwise zero.
   23261                 : */
   23262                 : static int os2CheckReservedLock( sqlite3_file *id, int *pOut ){
   23263                 :   int r = 0;
   23264                 :   os2File *pFile = (os2File*)id;
   23265                 :   assert( pFile!=0 );
   23266                 :   if( pFile->locktype>=RESERVED_LOCK ){
   23267                 :     r = 1;
   23268                 :     OSTRACE(( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ));
   23269                 :   }else{
   23270                 :     FILELOCK  LockArea,
   23271                 :               UnlockArea;
   23272                 :     APIRET rc = NO_ERROR;
   23273                 :     memset(&LockArea, 0, sizeof(LockArea));
   23274                 :     memset(&UnlockArea, 0, sizeof(UnlockArea));
   23275                 :     LockArea.lOffset = RESERVED_BYTE;
   23276                 :     LockArea.lRange = 1L;
   23277                 :     UnlockArea.lOffset = 0L;
   23278                 :     UnlockArea.lRange = 0L;
   23279                 :     rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23280                 :     OSTRACE(( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ));
   23281                 :     if( rc == NO_ERROR ){
   23282                 :       APIRET rcu = NO_ERROR; /* return code for unlocking */
   23283                 :       LockArea.lOffset = 0L;
   23284                 :       LockArea.lRange = 0L;
   23285                 :       UnlockArea.lOffset = RESERVED_BYTE;
   23286                 :       UnlockArea.lRange = 1L;
   23287                 :       rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23288                 :       OSTRACE(( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ));
   23289                 :     }
   23290                 :     r = !(rc == NO_ERROR);
   23291                 :     OSTRACE(( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ));
   23292                 :   }
   23293                 :   *pOut = r;
   23294                 :   return SQLITE_OK;
   23295                 : }
   23296                 : 
   23297                 : /*
   23298                 : ** Lower the locking level on file descriptor id to locktype.  locktype
   23299                 : ** must be either NO_LOCK or SHARED_LOCK.
   23300                 : **
   23301                 : ** If the locking level of the file descriptor is already at or below
   23302                 : ** the requested locking level, this routine is a no-op.
   23303                 : **
   23304                 : ** It is not possible for this routine to fail if the second argument
   23305                 : ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
   23306                 : ** might return SQLITE_IOERR;
   23307                 : */
   23308                 : static int os2Unlock( sqlite3_file *id, int locktype ){
   23309                 :   int type;
   23310                 :   os2File *pFile = (os2File*)id;
   23311                 :   APIRET rc = SQLITE_OK;
   23312                 :   APIRET res = NO_ERROR;
   23313                 :   FILELOCK  LockArea,
   23314                 :             UnlockArea;
   23315                 :   memset(&LockArea, 0, sizeof(LockArea));
   23316                 :   memset(&UnlockArea, 0, sizeof(UnlockArea));
   23317                 :   assert( pFile!=0 );
   23318                 :   assert( locktype<=SHARED_LOCK );
   23319                 :   OSTRACE(( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype ));
   23320                 :   type = pFile->locktype;
   23321                 :   if( type>=EXCLUSIVE_LOCK ){
   23322                 :     LockArea.lOffset = 0L;
   23323                 :     LockArea.lRange = 0L;
   23324                 :     UnlockArea.lOffset = SHARED_FIRST;
   23325                 :     UnlockArea.lRange = SHARED_SIZE;
   23326                 :     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23327                 :     OSTRACE(( "UNLOCK %d exclusive lock res=%d\n", pFile->h, res ));
   23328                 :     if( locktype==SHARED_LOCK && getReadLock(pFile) != NO_ERROR ){
   23329                 :       /* This should never happen.  We should always be able to
   23330                 :       ** reacquire the read lock */
   23331                 :       OSTRACE(( "UNLOCK %d to %d getReadLock() failed\n", pFile->h, locktype ));
   23332                 :       rc = SQLITE_IOERR_UNLOCK;
   23333                 :     }
   23334                 :   }
   23335                 :   if( type>=RESERVED_LOCK ){
   23336                 :     LockArea.lOffset = 0L;
   23337                 :     LockArea.lRange = 0L;
   23338                 :     UnlockArea.lOffset = RESERVED_BYTE;
   23339                 :     UnlockArea.lRange = 1L;
   23340                 :     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23341                 :     OSTRACE(( "UNLOCK %d reserved res=%d\n", pFile->h, res ));
   23342                 :   }
   23343                 :   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
   23344                 :     res = unlockReadLock(pFile);
   23345                 :     OSTRACE(( "UNLOCK %d is %d want %d res=%d\n",
   23346                 :               pFile->h, type, locktype, res ));
   23347                 :   }
   23348                 :   if( type>=PENDING_LOCK ){
   23349                 :     LockArea.lOffset = 0L;
   23350                 :     LockArea.lRange = 0L;
   23351                 :     UnlockArea.lOffset = PENDING_BYTE;
   23352                 :     UnlockArea.lRange = 1L;
   23353                 :     res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, LOCK_TIMEOUT, 0L );
   23354                 :     OSTRACE(( "UNLOCK %d pending res=%d\n", pFile->h, res ));
   23355                 :   }
   23356                 :   pFile->locktype = locktype;
   23357                 :   OSTRACE(( "UNLOCK %d now %d\n", pFile->h, pFile->locktype ));
   23358                 :   return rc;
   23359                 : }
   23360                 : 
   23361                 : /*
   23362                 : ** Control and query of the open file handle.
   23363                 : */
   23364                 : static int os2FileControl(sqlite3_file *id, int op, void *pArg){
   23365                 :   switch( op ){
   23366                 :     case SQLITE_FCNTL_LOCKSTATE: {
   23367                 :       *(int*)pArg = ((os2File*)id)->locktype;
   23368                 :       OSTRACE(( "FCNTL_LOCKSTATE %d lock=%d\n",
   23369                 :                 ((os2File*)id)->h, ((os2File*)id)->locktype ));
   23370                 :       return SQLITE_OK;
   23371                 :     }
   23372                 :     case SQLITE_FCNTL_CHUNK_SIZE: {
   23373                 :       ((os2File*)id)->szChunk = *(int*)pArg;
   23374                 :       return SQLITE_OK;
   23375                 :     }
   23376                 :     case SQLITE_FCNTL_SIZE_HINT: {
   23377                 :       sqlite3_int64 sz = *(sqlite3_int64*)pArg;
   23378                 :       SimulateIOErrorBenign(1);
   23379                 :       os2Truncate(id, sz);
   23380                 :       SimulateIOErrorBenign(0);
   23381                 :       return SQLITE_OK;
   23382                 :     }
   23383                 :     case SQLITE_FCNTL_SYNC_OMITTED: {
   23384                 :       return SQLITE_OK;
   23385                 :     }
   23386                 :   }
   23387                 :   return SQLITE_NOTFOUND;
   23388                 : }
   23389                 : 
   23390                 : /*
   23391                 : ** Return the sector size in bytes of the underlying block device for
   23392                 : ** the specified file. This is almost always 512 bytes, but may be
   23393                 : ** larger for some devices.
   23394                 : **
   23395                 : ** SQLite code assumes this function cannot fail. It also assumes that
   23396                 : ** if two files are created in the same file-system directory (i.e.
   23397                 : ** a database and its journal file) that the sector size will be the
   23398                 : ** same for both.
   23399                 : */
   23400                 : static int os2SectorSize(sqlite3_file *id){
   23401                 :   UNUSED_PARAMETER(id);
   23402                 :   return SQLITE_DEFAULT_SECTOR_SIZE;
   23403                 : }
   23404                 : 
   23405                 : /*
   23406                 : ** Return a vector of device characteristics.
   23407                 : */
   23408                 : static int os2DeviceCharacteristics(sqlite3_file *id){
   23409                 :   UNUSED_PARAMETER(id);
   23410                 :   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
   23411                 : }
   23412                 : 
   23413                 : 
   23414                 : /*
   23415                 : ** Character set conversion objects used by conversion routines.
   23416                 : */
   23417                 : static UconvObject ucUtf8 = NULL; /* convert between UTF-8 and UCS-2 */
   23418                 : static UconvObject uclCp = NULL;  /* convert between local codepage and UCS-2 */
   23419                 : 
   23420                 : /*
   23421                 : ** Helper function to initialize the conversion objects from and to UTF-8.
   23422                 : */
   23423                 : static void initUconvObjects( void ){
   23424                 :   if( UniCreateUconvObject( UTF_8, &ucUtf8 ) != ULS_SUCCESS )
   23425                 :     ucUtf8 = NULL;
   23426                 :   if ( UniCreateUconvObject( (UniChar *)L"@path=yes", &uclCp ) != ULS_SUCCESS )
   23427                 :     uclCp = NULL;
   23428                 : }
   23429                 : 
   23430                 : /*
   23431                 : ** Helper function to free the conversion objects from and to UTF-8.
   23432                 : */
   23433                 : static void freeUconvObjects( void ){
   23434                 :   if ( ucUtf8 )
   23435                 :     UniFreeUconvObject( ucUtf8 );
   23436                 :   if ( uclCp )
   23437                 :     UniFreeUconvObject( uclCp );
   23438                 :   ucUtf8 = NULL;
   23439                 :   uclCp = NULL;
   23440                 : }
   23441                 : 
   23442                 : /*
   23443                 : ** Helper function to convert UTF-8 filenames to local OS/2 codepage.
   23444                 : ** The two-step process: first convert the incoming UTF-8 string
   23445                 : ** into UCS-2 and then from UCS-2 to the current codepage.
   23446                 : ** The returned char pointer has to be freed.
   23447                 : */
   23448                 : static char *convertUtf8PathToCp( const char *in ){
   23449                 :   UniChar tempPath[CCHMAXPATH];
   23450                 :   char *out = (char *)calloc( CCHMAXPATH, 1 );
   23451                 : 
   23452                 :   if( !out )
   23453                 :     return NULL;
   23454                 : 
   23455                 :   if( !ucUtf8 || !uclCp )
   23456                 :     initUconvObjects();
   23457                 : 
   23458                 :   /* determine string for the conversion of UTF-8 which is CP1208 */
   23459                 :   if( UniStrToUcs( ucUtf8, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
   23460                 :     return out; /* if conversion fails, return the empty string */
   23461                 : 
   23462                 :   /* conversion for current codepage which can be used for paths */
   23463                 :   UniStrFromUcs( uclCp, out, tempPath, CCHMAXPATH );
   23464                 : 
   23465                 :   return out;
   23466                 : }
   23467                 : 
   23468                 : /*
   23469                 : ** Helper function to convert filenames from local codepage to UTF-8.
   23470                 : ** The two-step process: first convert the incoming codepage-specific
   23471                 : ** string into UCS-2 and then from UCS-2 to the codepage of UTF-8.
   23472                 : ** The returned char pointer has to be freed.
   23473                 : **
   23474                 : ** This function is non-static to be able to use this in shell.c and
   23475                 : ** similar applications that take command line arguments.
   23476                 : */
   23477                 : char *convertCpPathToUtf8( const char *in ){
   23478                 :   UniChar tempPath[CCHMAXPATH];
   23479                 :   char *out = (char *)calloc( CCHMAXPATH, 1 );
   23480                 : 
   23481                 :   if( !out )
   23482                 :     return NULL;
   23483                 : 
   23484                 :   if( !ucUtf8 || !uclCp )
   23485                 :     initUconvObjects();
   23486                 : 
   23487                 :   /* conversion for current codepage which can be used for paths */
   23488                 :   if( UniStrToUcs( uclCp, tempPath, (char *)in, CCHMAXPATH ) != ULS_SUCCESS )
   23489                 :     return out; /* if conversion fails, return the empty string */
   23490                 : 
   23491                 :   /* determine string for the conversion of UTF-8 which is CP1208 */
   23492                 :   UniStrFromUcs( ucUtf8, out, tempPath, CCHMAXPATH );
   23493                 : 
   23494                 :   return out;
   23495                 : }
   23496                 : 
   23497                 : 
   23498                 : #ifndef SQLITE_OMIT_WAL
   23499                 : 
   23500                 : /*
   23501                 : ** Use main database file for interprocess locking. If un-defined
   23502                 : ** a separate file is created for this purpose. The file will be
   23503                 : ** used only to set file locks. There will be no data written to it.
   23504                 : */
   23505                 : #define SQLITE_OS2_NO_WAL_LOCK_FILE     
   23506                 : 
   23507                 : #if 0
   23508                 : static void _ERR_TRACE( const char *fmt, ... ) {
   23509                 :   va_list  ap;
   23510                 :   va_start(ap, fmt);
   23511                 :   vfprintf(stderr, fmt, ap);
   23512                 :   fflush(stderr);
   23513                 : }
   23514                 : #define ERR_TRACE(rc, msg)        \
   23515                 :         if( (rc) != SQLITE_OK ) _ERR_TRACE msg;
   23516                 : #else
   23517                 : #define ERR_TRACE(rc, msg)
   23518                 : #endif
   23519                 : 
   23520                 : /*
   23521                 : ** Helper functions to obtain and relinquish the global mutex. The
   23522                 : ** global mutex is used to protect os2ShmNodeList.
   23523                 : **
   23524                 : ** Function os2ShmMutexHeld() is used to assert() that the global mutex 
   23525                 : ** is held when required. This function is only used as part of assert() 
   23526                 : ** statements. e.g.
   23527                 : **
   23528                 : **   os2ShmEnterMutex()
   23529                 : **     assert( os2ShmMutexHeld() );
   23530                 : **   os2ShmLeaveMutex()
   23531                 : */
   23532                 : static void os2ShmEnterMutex(void){
   23533                 :   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   23534                 : }
   23535                 : static void os2ShmLeaveMutex(void){
   23536                 :   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   23537                 : }
   23538                 : #ifdef SQLITE_DEBUG
   23539                 : static int os2ShmMutexHeld(void) {
   23540                 :   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   23541                 : }
   23542                 : int GetCurrentProcessId(void) {
   23543                 :   PPIB pib;
   23544                 :   DosGetInfoBlocks(NULL, &pib);
   23545                 :   return (int)pib->pib_ulpid;
   23546                 : }
   23547                 : #endif
   23548                 : 
   23549                 : /*
   23550                 : ** Object used to represent a the shared memory area for a single log file.
   23551                 : ** When multiple threads all reference the same log-summary, each thread has
   23552                 : ** its own os2File object, but they all point to a single instance of this 
   23553                 : ** object.  In other words, each log-summary is opened only once per process.
   23554                 : **
   23555                 : ** os2ShmMutexHeld() must be true when creating or destroying
   23556                 : ** this object or while reading or writing the following fields:
   23557                 : **
   23558                 : **      nRef
   23559                 : **      pNext 
   23560                 : **
   23561                 : ** The following fields are read-only after the object is created:
   23562                 : ** 
   23563                 : **      szRegion
   23564                 : **      hLockFile
   23565                 : **      shmBaseName
   23566                 : **
   23567                 : ** Either os2ShmNode.mutex must be held or os2ShmNode.nRef==0 and
   23568                 : ** os2ShmMutexHeld() is true when reading or writing any other field
   23569                 : ** in this structure.
   23570                 : **
   23571                 : */
   23572                 : struct os2ShmNode {
   23573                 :   sqlite3_mutex *mutex;      /* Mutex to access this object */
   23574                 :   os2ShmNode *pNext;         /* Next in list of all os2ShmNode objects */
   23575                 : 
   23576                 :   int szRegion;              /* Size of shared-memory regions */
   23577                 : 
   23578                 :   int nRegion;               /* Size of array apRegion */
   23579                 :   void **apRegion;           /* Array of pointers to shared-memory regions */
   23580                 : 
   23581                 :   int nRef;                  /* Number of os2ShmLink objects pointing to this */
   23582                 :   os2ShmLink *pFirst;        /* First os2ShmLink object pointing to this */
   23583                 : 
   23584                 :   HFILE hLockFile;           /* File used for inter-process memory locking */
   23585                 :   char shmBaseName[1];       /* Name of the memory object !!! must last !!! */
   23586                 : };
   23587                 : 
   23588                 : 
   23589                 : /*
   23590                 : ** Structure used internally by this VFS to record the state of an
   23591                 : ** open shared memory connection.
   23592                 : **
   23593                 : ** The following fields are initialized when this object is created and
   23594                 : ** are read-only thereafter:
   23595                 : **
   23596                 : **    os2Shm.pShmNode
   23597                 : **    os2Shm.id
   23598                 : **
   23599                 : ** All other fields are read/write.  The os2Shm.pShmNode->mutex must be held
   23600                 : ** while accessing any read/write fields.
   23601                 : */
   23602                 : struct os2ShmLink {
   23603                 :   os2ShmNode *pShmNode;      /* The underlying os2ShmNode object */
   23604                 :   os2ShmLink *pNext;         /* Next os2Shm with the same os2ShmNode */
   23605                 :   u32 sharedMask;            /* Mask of shared locks held */
   23606                 :   u32 exclMask;              /* Mask of exclusive locks held */
   23607                 : #ifdef SQLITE_DEBUG
   23608                 :   u8 id;                     /* Id of this connection with its os2ShmNode */
   23609                 : #endif
   23610                 : };
   23611                 : 
   23612                 : 
   23613                 : /*
   23614                 : ** A global list of all os2ShmNode objects.
   23615                 : **
   23616                 : ** The os2ShmMutexHeld() must be true while reading or writing this list.
   23617                 : */
   23618                 : static os2ShmNode *os2ShmNodeList = NULL;
   23619                 : 
   23620                 : /*
   23621                 : ** Constants used for locking
   23622                 : */
   23623                 : #ifdef  SQLITE_OS2_NO_WAL_LOCK_FILE
   23624                 : #define OS2_SHM_BASE   (PENDING_BYTE + 0x10000)         /* first lock byte */
   23625                 : #else
   23626                 : #define OS2_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
   23627                 : #endif
   23628                 : 
   23629                 : #define OS2_SHM_DMS    (OS2_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
   23630                 : 
   23631                 : /*
   23632                 : ** Apply advisory locks for all n bytes beginning at ofst.
   23633                 : */
   23634                 : #define _SHM_UNLCK  1   /* no lock */
   23635                 : #define _SHM_RDLCK  2   /* shared lock, no wait */
   23636                 : #define _SHM_WRLCK  3   /* exlusive lock, no wait */
   23637                 : #define _SHM_WRLCK_WAIT 4 /* exclusive lock, wait */
   23638                 : static int os2ShmSystemLock(
   23639                 :   os2ShmNode *pNode,    /* Apply locks to this open shared-memory segment */
   23640                 :   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, _SHM_WRLCK or _SHM_WRLCK_WAIT */
   23641                 :   int ofst,             /* Offset to first byte to be locked/unlocked */
   23642                 :   int nByte             /* Number of bytes to lock or unlock */
   23643                 : ){
   23644                 :   APIRET rc;
   23645                 :   FILELOCK area;
   23646                 :   ULONG mode, timeout;
   23647                 : 
   23648                 :   /* Access to the os2ShmNode object is serialized by the caller */
   23649                 :   assert( sqlite3_mutex_held(pNode->mutex) || pNode->nRef==0 );
   23650                 : 
   23651                 :   mode = 1;     /* shared lock */
   23652                 :   timeout = 0;  /* no wait */
   23653                 :   area.lOffset = ofst;
   23654                 :   area.lRange = nByte;
   23655                 : 
   23656                 :   switch( lockType ) {
   23657                 :     case _SHM_WRLCK_WAIT:
   23658                 :       timeout = (ULONG)-1;      /* wait forever */
   23659                 :     case _SHM_WRLCK:
   23660                 :       mode = 0;                 /* exclusive lock */
   23661                 :     case _SHM_RDLCK:
   23662                 :       rc = DosSetFileLocks(pNode->hLockFile, 
   23663                 :                            NULL, &area, timeout, mode);
   23664                 :       break;
   23665                 :     /* case _SHM_UNLCK: */
   23666                 :     default:
   23667                 :       rc = DosSetFileLocks(pNode->hLockFile, 
   23668                 :                            &area, NULL, 0, 0);
   23669                 :       break;
   23670                 :   }
   23671                 :                           
   23672                 :   OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
   23673                 :            pNode->hLockFile,
   23674                 :            rc==SQLITE_OK ? "ok" : "failed",
   23675                 :            lockType==_SHM_UNLCK ? "Unlock" : "Lock",
   23676                 :            rc));
   23677                 : 
   23678                 :   ERR_TRACE(rc, ("os2ShmSystemLock: %d %s\n", rc, pNode->shmBaseName))
   23679                 : 
   23680                 :   return ( rc == 0 ) ?  SQLITE_OK : SQLITE_BUSY;
   23681                 : }
   23682                 : 
   23683                 : /*
   23684                 : ** Find an os2ShmNode in global list or allocate a new one, if not found.
   23685                 : **
   23686                 : ** This is not a VFS shared-memory method; it is a utility function called
   23687                 : ** by VFS shared-memory methods.
   23688                 : */
   23689                 : static int os2OpenSharedMemory( os2File *fd, int szRegion ) {
   23690                 :   os2ShmLink *pLink;
   23691                 :   os2ShmNode *pNode;
   23692                 :   int cbShmName, rc = SQLITE_OK;
   23693                 :   char shmName[CCHMAXPATH + 30];
   23694                 : #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
   23695                 :   ULONG action;
   23696                 : #endif
   23697                 :   
   23698                 :   /* We need some additional space at the end to append the region number */
   23699                 :   cbShmName = sprintf(shmName, "\\SHAREMEM\\%s", fd->zFullPathCp );
   23700                 :   if( cbShmName >= CCHMAXPATH-8 )
   23701                 :     return SQLITE_IOERR_SHMOPEN; 
   23702                 : 
   23703                 :   /* Replace colon in file name to form a valid shared memory name */
   23704                 :   shmName[10+1] = '!';
   23705                 : 
   23706                 :   /* Allocate link object (we free it later in case of failure) */
   23707                 :   pLink = sqlite3_malloc( sizeof(*pLink) );
   23708                 :   if( !pLink )
   23709                 :     return SQLITE_NOMEM;
   23710                 : 
   23711                 :   /* Access node list */
   23712                 :   os2ShmEnterMutex();
   23713                 : 
   23714                 :   /* Find node by it's shared memory base name */
   23715                 :   for( pNode = os2ShmNodeList; 
   23716                 :        pNode && stricmp(shmName, pNode->shmBaseName) != 0; 
   23717                 :        pNode = pNode->pNext )   ;
   23718                 : 
   23719                 :   /* Not found: allocate a new node */
   23720                 :   if( !pNode ) {
   23721                 :     pNode = sqlite3_malloc( sizeof(*pNode) + cbShmName );
   23722                 :     if( pNode ) {
   23723                 :       memset(pNode, 0, sizeof(*pNode) );
   23724                 :       pNode->szRegion = szRegion;
   23725                 :       pNode->hLockFile = (HFILE)-1;      
   23726                 :       strcpy(pNode->shmBaseName, shmName);
   23727                 : 
   23728                 : #ifdef SQLITE_OS2_NO_WAL_LOCK_FILE
   23729                 :       if( DosDupHandle(fd->h, &pNode->hLockFile) != 0 ) {
   23730                 : #else
   23731                 :       sprintf(shmName, "%s-lck", fd->zFullPathCp);
   23732                 :       if( DosOpen((PSZ)shmName, &pNode->hLockFile, &action, 0, FILE_NORMAL, 
   23733                 :                   OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
   23734                 :                   OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | 
   23735                 :                   OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR,
   23736                 :                   NULL) != 0 ) {
   23737                 : #endif
   23738                 :         sqlite3_free(pNode);  
   23739                 :         rc = SQLITE_IOERR;
   23740                 :       } else {
   23741                 :         pNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
   23742                 :         if( !pNode->mutex ) {
   23743                 :           sqlite3_free(pNode);  
   23744                 :           rc = SQLITE_NOMEM;
   23745                 :         }
   23746                 :       }   
   23747                 :     } else {
   23748                 :       rc = SQLITE_NOMEM;
   23749                 :     }
   23750                 :     
   23751                 :     if( rc == SQLITE_OK ) {
   23752                 :       pNode->pNext = os2ShmNodeList;
   23753                 :       os2ShmNodeList = pNode;
   23754                 :     } else {
   23755                 :       pNode = NULL;
   23756                 :     }
   23757                 :   } else if( pNode->szRegion != szRegion ) {
   23758                 :     rc = SQLITE_IOERR_SHMSIZE;
   23759                 :     pNode = NULL;
   23760                 :   }
   23761                 : 
   23762                 :   if( pNode ) {
   23763                 :     sqlite3_mutex_enter(pNode->mutex);
   23764                 : 
   23765                 :     memset(pLink, 0, sizeof(*pLink));
   23766                 : 
   23767                 :     pLink->pShmNode = pNode;
   23768                 :     pLink->pNext = pNode->pFirst;
   23769                 :     pNode->pFirst = pLink;
   23770                 :     pNode->nRef++;
   23771                 : 
   23772                 :     fd->pShmLink = pLink;
   23773                 : 
   23774                 :     sqlite3_mutex_leave(pNode->mutex);
   23775                 :     
   23776                 :   } else {
   23777                 :     /* Error occured. Free our link object. */
   23778                 :     sqlite3_free(pLink);  
   23779                 :   }
   23780                 : 
   23781                 :   os2ShmLeaveMutex();
   23782                 : 
   23783                 :   ERR_TRACE(rc, ("os2OpenSharedMemory: %d  %s\n", rc, fd->zFullPathCp))  
   23784                 :   
   23785                 :   return rc;
   23786                 : }
   23787                 : 
   23788                 : /*
   23789                 : ** Purge the os2ShmNodeList list of all entries with nRef==0.
   23790                 : **
   23791                 : ** This is not a VFS shared-memory method; it is a utility function called
   23792                 : ** by VFS shared-memory methods.
   23793                 : */
   23794                 : static void os2PurgeShmNodes( int deleteFlag ) {
   23795                 :   os2ShmNode *pNode;
   23796                 :   os2ShmNode **ppNode;
   23797                 : 
   23798                 :   os2ShmEnterMutex();
   23799                 :   
   23800                 :   ppNode = &os2ShmNodeList;
   23801                 : 
   23802                 :   while( *ppNode ) {
   23803                 :     pNode = *ppNode;
   23804                 : 
   23805                 :     if( pNode->nRef == 0 ) {
   23806                 :       *ppNode = pNode->pNext;   
   23807                 :      
   23808                 :       if( pNode->apRegion ) {
   23809                 :         /* Prevent other processes from resizing the shared memory */
   23810                 :         os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
   23811                 : 
   23812                 :         while( pNode->nRegion-- ) {
   23813                 : #ifdef SQLITE_DEBUG
   23814                 :           int rc = 
   23815                 : #endif          
   23816                 :           DosFreeMem(pNode->apRegion[pNode->nRegion]);
   23817                 : 
   23818                 :           OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
   23819                 :                   (int)GetCurrentProcessId(), pNode->nRegion,
   23820                 :                   rc == 0 ? "ok" : "failed"));
   23821                 :         }
   23822                 : 
   23823                 :         /* Allow other processes to resize the shared memory */
   23824                 :         os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
   23825                 : 
   23826                 :         sqlite3_free(pNode->apRegion);
   23827                 :       }  
   23828                 : 
   23829                 :       DosClose(pNode->hLockFile);
   23830                 :       
   23831                 : #ifndef SQLITE_OS2_NO_WAL_LOCK_FILE
   23832                 :       if( deleteFlag ) {
   23833                 :          char fileName[CCHMAXPATH];
   23834                 :          /* Skip "\\SHAREMEM\\" */
   23835                 :          sprintf(fileName, "%s-lck", pNode->shmBaseName + 10);
   23836                 :          /* restore colon */
   23837                 :          fileName[1] = ':';
   23838                 :          
   23839                 :          DosForceDelete(fileName); 
   23840                 :       }
   23841                 : #endif
   23842                 : 
   23843                 :       sqlite3_mutex_free(pNode->mutex);
   23844                 : 
   23845                 :       sqlite3_free(pNode);
   23846                 :       
   23847                 :     } else {
   23848                 :       ppNode = &pNode->pNext;
   23849                 :     }
   23850                 :   } 
   23851                 : 
   23852                 :   os2ShmLeaveMutex();
   23853                 : }
   23854                 : 
   23855                 : /*
   23856                 : ** This function is called to obtain a pointer to region iRegion of the
   23857                 : ** shared-memory associated with the database file id. Shared-memory regions
   23858                 : ** are numbered starting from zero. Each shared-memory region is szRegion
   23859                 : ** bytes in size.
   23860                 : **
   23861                 : ** If an error occurs, an error code is returned and *pp is set to NULL.
   23862                 : **
   23863                 : ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
   23864                 : ** region has not been allocated (by any client, including one running in a
   23865                 : ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
   23866                 : ** bExtend is non-zero and the requested shared-memory region has not yet
   23867                 : ** been allocated, it is allocated by this function.
   23868                 : **
   23869                 : ** If the shared-memory region has already been allocated or is allocated by
   23870                 : ** this call as described above, then it is mapped into this processes
   23871                 : ** address space (if it is not already), *pp is set to point to the mapped
   23872                 : ** memory and SQLITE_OK returned.
   23873                 : */
   23874                 : static int os2ShmMap(
   23875                 :   sqlite3_file *id,               /* Handle open on database file */
   23876                 :   int iRegion,                    /* Region to retrieve */
   23877                 :   int szRegion,                   /* Size of regions */
   23878                 :   int bExtend,                    /* True to extend block if necessary */
   23879                 :   void volatile **pp              /* OUT: Mapped memory */
   23880                 : ){
   23881                 :   PVOID pvTemp;
   23882                 :   void **apRegion;
   23883                 :   os2ShmNode *pNode;
   23884                 :   int n, rc = SQLITE_OK;
   23885                 :   char shmName[CCHMAXPATH];
   23886                 :   os2File *pFile = (os2File*)id;
   23887                 :   
   23888                 :   *pp = NULL;
   23889                 : 
   23890                 :   if( !pFile->pShmLink )
   23891                 :     rc = os2OpenSharedMemory( pFile, szRegion );
   23892                 :   
   23893                 :   if( rc == SQLITE_OK ) {
   23894                 :     pNode = pFile->pShmLink->pShmNode ;
   23895                 :     
   23896                 :     sqlite3_mutex_enter(pNode->mutex);
   23897                 :     
   23898                 :     assert( szRegion==pNode->szRegion );
   23899                 : 
   23900                 :     /* Unmapped region ? */
   23901                 :     if( iRegion >= pNode->nRegion ) {
   23902                 :       /* Prevent other processes from resizing the shared memory */
   23903                 :       os2ShmSystemLock(pNode, _SHM_WRLCK_WAIT, OS2_SHM_DMS, 1);
   23904                 : 
   23905                 :       apRegion = sqlite3_realloc(
   23906                 :         pNode->apRegion, (iRegion + 1) * sizeof(apRegion[0]));
   23907                 : 
   23908                 :       if( apRegion ) {
   23909                 :         pNode->apRegion = apRegion;
   23910                 : 
   23911                 :         while( pNode->nRegion <= iRegion ) {
   23912                 :           sprintf(shmName, "%s-%u", 
   23913                 :                   pNode->shmBaseName, pNode->nRegion);
   23914                 : 
   23915                 :           if( DosGetNamedSharedMem(&pvTemp, (PSZ)shmName, 
   23916                 :                 PAG_READ | PAG_WRITE) != NO_ERROR ) {
   23917                 :             if( !bExtend )
   23918                 :               break;
   23919                 : 
   23920                 :             if( DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
   23921                 :                   PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_ANY) != NO_ERROR && 
   23922                 :                 DosAllocSharedMem(&pvTemp, (PSZ)shmName, szRegion,
   23923                 :                   PAG_READ | PAG_WRITE | PAG_COMMIT) != NO_ERROR ) { 
   23924                 :               rc = SQLITE_NOMEM;
   23925                 :               break;
   23926                 :             }
   23927                 :           }
   23928                 : 
   23929                 :           apRegion[pNode->nRegion++] = pvTemp;
   23930                 :         }
   23931                 : 
   23932                 :         /* zero out remaining entries */ 
   23933                 :         for( n = pNode->nRegion; n <= iRegion; n++ )
   23934                 :           pNode->apRegion[n] = NULL;
   23935                 : 
   23936                 :         /* Return this region (maybe zero) */
   23937                 :         *pp = pNode->apRegion[iRegion];
   23938                 :       } else {
   23939                 :         rc = SQLITE_NOMEM;
   23940                 :       }
   23941                 : 
   23942                 :       /* Allow other processes to resize the shared memory */
   23943                 :       os2ShmSystemLock(pNode, _SHM_UNLCK, OS2_SHM_DMS, 1);
   23944                 :       
   23945                 :     } else {
   23946                 :       /* Region has been mapped previously */
   23947                 :       *pp = pNode->apRegion[iRegion];
   23948                 :     }
   23949                 : 
   23950                 :     sqlite3_mutex_leave(pNode->mutex);
   23951                 :   } 
   23952                 : 
   23953                 :   ERR_TRACE(rc, ("os2ShmMap: %s iRgn = %d, szRgn = %d, bExt = %d : %d\n", 
   23954                 :                  pFile->zFullPathCp, iRegion, szRegion, bExtend, rc))
   23955                 :           
   23956                 :   return rc;
   23957                 : }
   23958                 : 
   23959                 : /*
   23960                 : ** Close a connection to shared-memory.  Delete the underlying
   23961                 : ** storage if deleteFlag is true.
   23962                 : **
   23963                 : ** If there is no shared memory associated with the connection then this
   23964                 : ** routine is a harmless no-op.
   23965                 : */
   23966                 : static int os2ShmUnmap(
   23967                 :   sqlite3_file *id,               /* The underlying database file */
   23968                 :   int deleteFlag                  /* Delete shared-memory if true */
   23969                 : ){
   23970                 :   os2File *pFile = (os2File*)id;
   23971                 :   os2ShmLink *pLink = pFile->pShmLink;
   23972                 :   
   23973                 :   if( pLink ) {
   23974                 :     int nRef = -1;
   23975                 :     os2ShmLink **ppLink;
   23976                 :     os2ShmNode *pNode = pLink->pShmNode;
   23977                 : 
   23978                 :     sqlite3_mutex_enter(pNode->mutex);
   23979                 :     
   23980                 :     for( ppLink = &pNode->pFirst;
   23981                 :          *ppLink && *ppLink != pLink;
   23982                 :          ppLink = &(*ppLink)->pNext )   ;
   23983                 :          
   23984                 :     assert(*ppLink);
   23985                 : 
   23986                 :     if( *ppLink ) {
   23987                 :       *ppLink = pLink->pNext;
   23988                 :       nRef = --pNode->nRef;
   23989                 :     } else {
   23990                 :       ERR_TRACE(1, ("os2ShmUnmap: link not found ! %s\n", 
   23991                 :                     pNode->shmBaseName))
   23992                 :     }
   23993                 :     
   23994                 :     pFile->pShmLink = NULL;
   23995                 :     sqlite3_free(pLink);
   23996                 : 
   23997                 :     sqlite3_mutex_leave(pNode->mutex);
   23998                 :     
   23999                 :     if( nRef == 0 )
   24000                 :       os2PurgeShmNodes( deleteFlag );
   24001                 :   }
   24002                 : 
   24003                 :   return SQLITE_OK;
   24004                 : }
   24005                 : 
   24006                 : /*
   24007                 : ** Change the lock state for a shared-memory segment.
   24008                 : **
   24009                 : ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
   24010                 : ** different here than in posix.  In xShmLock(), one can go from unlocked
   24011                 : ** to shared and back or from unlocked to exclusive and back.  But one may
   24012                 : ** not go from shared to exclusive or from exclusive to shared.
   24013                 : */
   24014                 : static int os2ShmLock(
   24015                 :   sqlite3_file *id,          /* Database file holding the shared memory */
   24016                 :   int ofst,                  /* First lock to acquire or release */
   24017                 :   int n,                     /* Number of locks to acquire or release */
   24018                 :   int flags                  /* What to do with the lock */
   24019                 : ){
   24020                 :   u32 mask;                             /* Mask of locks to take or release */
   24021                 :   int rc = SQLITE_OK;                   /* Result code */
   24022                 :   os2File *pFile = (os2File*)id;
   24023                 :   os2ShmLink *p = pFile->pShmLink;      /* The shared memory being locked */
   24024                 :   os2ShmLink *pX;                       /* For looping over all siblings */
   24025                 :   os2ShmNode *pShmNode = p->pShmNode;   /* Our node */
   24026                 :   
   24027                 :   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
   24028                 :   assert( n>=1 );
   24029                 :   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
   24030                 :        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
   24031                 :        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
   24032                 :        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
   24033                 :   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
   24034                 : 
   24035                 :   mask = (u32)((1U<<(ofst+n)) - (1U<<ofst));
   24036                 :   assert( n>1 || mask==(1<<ofst) );
   24037                 : 
   24038                 : 
   24039                 :   sqlite3_mutex_enter(pShmNode->mutex);
   24040                 : 
   24041                 :   if( flags & SQLITE_SHM_UNLOCK ){
   24042                 :     u32 allMask = 0; /* Mask of locks held by siblings */
   24043                 : 
   24044                 :     /* See if any siblings hold this same lock */
   24045                 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   24046                 :       if( pX==p ) continue;
   24047                 :       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
   24048                 :       allMask |= pX->sharedMask;
   24049                 :     }
   24050                 : 
   24051                 :     /* Unlock the system-level locks */
   24052                 :     if( (mask & allMask)==0 ){
   24053                 :       rc = os2ShmSystemLock(pShmNode, _SHM_UNLCK, ofst+OS2_SHM_BASE, n);
   24054                 :     }else{
   24055                 :       rc = SQLITE_OK;
   24056                 :     }
   24057                 : 
   24058                 :     /* Undo the local locks */
   24059                 :     if( rc==SQLITE_OK ){
   24060                 :       p->exclMask &= ~mask;
   24061                 :       p->sharedMask &= ~mask;
   24062                 :     } 
   24063                 :   }else if( flags & SQLITE_SHM_SHARED ){
   24064                 :     u32 allShared = 0;  /* Union of locks held by connections other than "p" */
   24065                 : 
   24066                 :     /* Find out which shared locks are already held by sibling connections.
   24067                 :     ** If any sibling already holds an exclusive lock, go ahead and return
   24068                 :     ** SQLITE_BUSY.
   24069                 :     */
   24070                 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   24071                 :       if( (pX->exclMask & mask)!=0 ){
   24072                 :         rc = SQLITE_BUSY;
   24073                 :         break;
   24074                 :       }
   24075                 :       allShared |= pX->sharedMask;
   24076                 :     }
   24077                 : 
   24078                 :     /* Get shared locks at the system level, if necessary */
   24079                 :     if( rc==SQLITE_OK ){
   24080                 :       if( (allShared & mask)==0 ){
   24081                 :         rc = os2ShmSystemLock(pShmNode, _SHM_RDLCK, ofst+OS2_SHM_BASE, n);
   24082                 :       }else{
   24083                 :         rc = SQLITE_OK;
   24084                 :       }
   24085                 :     }
   24086                 : 
   24087                 :     /* Get the local shared locks */
   24088                 :     if( rc==SQLITE_OK ){
   24089                 :       p->sharedMask |= mask;
   24090                 :     }
   24091                 :   }else{
   24092                 :     /* Make sure no sibling connections hold locks that will block this
   24093                 :     ** lock.  If any do, return SQLITE_BUSY right away.
   24094                 :     */
   24095                 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   24096                 :       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
   24097                 :         rc = SQLITE_BUSY;
   24098                 :         break;
   24099                 :       }
   24100                 :     }
   24101                 :   
   24102                 :     /* Get the exclusive locks at the system level.  Then if successful
   24103                 :     ** also mark the local connection as being locked.
   24104                 :     */
   24105                 :     if( rc==SQLITE_OK ){
   24106                 :       rc = os2ShmSystemLock(pShmNode, _SHM_WRLCK, ofst+OS2_SHM_BASE, n);
   24107                 :       if( rc==SQLITE_OK ){
   24108                 :         assert( (p->sharedMask & mask)==0 );
   24109                 :         p->exclMask |= mask;
   24110                 :       }
   24111                 :     }
   24112                 :   }
   24113                 : 
   24114                 :   sqlite3_mutex_leave(pShmNode->mutex);
   24115                 :   
   24116                 :   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
   24117                 :            p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
   24118                 :            rc ? "failed" : "ok"));
   24119                 : 
   24120                 :   ERR_TRACE(rc, ("os2ShmLock: ofst = %d, n = %d, flags = 0x%x -> %d \n", 
   24121                 :                  ofst, n, flags, rc))
   24122                 :                   
   24123                 :   return rc; 
   24124                 : }
   24125                 : 
   24126                 : /*
   24127                 : ** Implement a memory barrier or memory fence on shared memory.
   24128                 : **
   24129                 : ** All loads and stores begun before the barrier must complete before
   24130                 : ** any load or store begun after the barrier.
   24131                 : */
   24132                 : static void os2ShmBarrier(
   24133                 :   sqlite3_file *id                /* Database file holding the shared memory */
   24134                 : ){
   24135                 :   UNUSED_PARAMETER(id);
   24136                 :   os2ShmEnterMutex();
   24137                 :   os2ShmLeaveMutex();
   24138                 : }
   24139                 : 
   24140                 : #else
   24141                 : # define os2ShmMap     0
   24142                 : # define os2ShmLock    0
   24143                 : # define os2ShmBarrier 0
   24144                 : # define os2ShmUnmap   0
   24145                 : #endif /* #ifndef SQLITE_OMIT_WAL */
   24146                 : 
   24147                 : 
   24148                 : /*
   24149                 : ** This vector defines all the methods that can operate on an
   24150                 : ** sqlite3_file for os2.
   24151                 : */
   24152                 : static const sqlite3_io_methods os2IoMethod = {
   24153                 :   2,                              /* iVersion */
   24154                 :   os2Close,                       /* xClose */
   24155                 :   os2Read,                        /* xRead */
   24156                 :   os2Write,                       /* xWrite */
   24157                 :   os2Truncate,                    /* xTruncate */
   24158                 :   os2Sync,                        /* xSync */
   24159                 :   os2FileSize,                    /* xFileSize */
   24160                 :   os2Lock,                        /* xLock */
   24161                 :   os2Unlock,                      /* xUnlock */
   24162                 :   os2CheckReservedLock,           /* xCheckReservedLock */
   24163                 :   os2FileControl,                 /* xFileControl */
   24164                 :   os2SectorSize,                  /* xSectorSize */
   24165                 :   os2DeviceCharacteristics,       /* xDeviceCharacteristics */
   24166                 :   os2ShmMap,                      /* xShmMap */
   24167                 :   os2ShmLock,                     /* xShmLock */
   24168                 :   os2ShmBarrier,                  /* xShmBarrier */
   24169                 :   os2ShmUnmap                     /* xShmUnmap */
   24170                 : };
   24171                 : 
   24172                 : 
   24173                 : /***************************************************************************
   24174                 : ** Here ends the I/O methods that form the sqlite3_io_methods object.
   24175                 : **
   24176                 : ** The next block of code implements the VFS methods.
   24177                 : ****************************************************************************/
   24178                 : 
   24179                 : /*
   24180                 : ** Create a temporary file name in zBuf.  zBuf must be big enough to
   24181                 : ** hold at pVfs->mxPathname characters.
   24182                 : */
   24183                 : static int getTempname(int nBuf, char *zBuf ){
   24184                 :   static const char zChars[] =
   24185                 :     "abcdefghijklmnopqrstuvwxyz"
   24186                 :     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   24187                 :     "0123456789";
   24188                 :   int i, j;
   24189                 :   PSZ zTempPathCp;      
   24190                 :   char zTempPath[CCHMAXPATH];
   24191                 :   ULONG ulDriveNum, ulDriveMap;
   24192                 :   
   24193                 :   /* It's odd to simulate an io-error here, but really this is just
   24194                 :   ** using the io-error infrastructure to test that SQLite handles this
   24195                 :   ** function failing. 
   24196                 :   */
   24197                 :   SimulateIOError( return SQLITE_IOERR );
   24198                 : 
   24199                 :   if( sqlite3_temp_directory ) {
   24200                 :     sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", sqlite3_temp_directory);
   24201                 :   } else if( DosScanEnv( (PSZ)"TEMP",   &zTempPathCp ) == NO_ERROR ||
   24202                 :              DosScanEnv( (PSZ)"TMP",    &zTempPathCp ) == NO_ERROR ||
   24203                 :              DosScanEnv( (PSZ)"TMPDIR", &zTempPathCp ) == NO_ERROR ) {
   24204                 :     char *zTempPathUTF = convertCpPathToUtf8( (char *)zTempPathCp );
   24205                 :     sqlite3_snprintf(CCHMAXPATH-30, zTempPath, "%s", zTempPathUTF);
   24206                 :     free( zTempPathUTF );
   24207                 :   } else if( DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ) == NO_ERROR ) {
   24208                 :     zTempPath[0] = (char)('A' + ulDriveNum - 1);
   24209                 :     zTempPath[1] = ':'; 
   24210                 :     zTempPath[2] = '\0'; 
   24211                 :   } else {
   24212                 :     zTempPath[0] = '\0'; 
   24213                 :   }
   24214                 :   
   24215                 :   /* Strip off a trailing slashes or backslashes, otherwise we would get *
   24216                 :    * multiple (back)slashes which causes DosOpen() to fail.              *
   24217                 :    * Trailing spaces are not allowed, either.                            */
   24218                 :   j = sqlite3Strlen30(zTempPath);
   24219                 :   while( j > 0 && ( zTempPath[j-1] == '\\' || zTempPath[j-1] == '/' || 
   24220                 :                     zTempPath[j-1] == ' ' ) ){
   24221                 :     j--;
   24222                 :   }
   24223                 :   zTempPath[j] = '\0';
   24224                 :   
   24225                 :   /* We use 20 bytes to randomize the name */
   24226                 :   sqlite3_snprintf(nBuf-22, zBuf,
   24227                 :                    "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
   24228                 :   j = sqlite3Strlen30(zBuf);
   24229                 :   sqlite3_randomness( 20, &zBuf[j] );
   24230                 :   for( i = 0; i < 20; i++, j++ ){
   24231                 :     zBuf[j] = zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   24232                 :   }
   24233                 :   zBuf[j] = 0;
   24234                 : 
   24235                 :   OSTRACE(( "TEMP FILENAME: %s\n", zBuf ));
   24236                 :   return SQLITE_OK;
   24237                 : }
   24238                 : 
   24239                 : 
   24240                 : /*
   24241                 : ** Turn a relative pathname into a full pathname.  Write the full
   24242                 : ** pathname into zFull[].  zFull[] will be at least pVfs->mxPathname
   24243                 : ** bytes in size.
   24244                 : */
   24245                 : static int os2FullPathname(
   24246                 :   sqlite3_vfs *pVfs,          /* Pointer to vfs object */
   24247                 :   const char *zRelative,      /* Possibly relative input path */
   24248                 :   int nFull,                  /* Size of output buffer in bytes */
   24249                 :   char *zFull                 /* Output buffer */
   24250                 : ){
   24251                 :   char *zRelativeCp = convertUtf8PathToCp( zRelative );
   24252                 :   char zFullCp[CCHMAXPATH] = "\0";
   24253                 :   char *zFullUTF;
   24254                 :   APIRET rc = DosQueryPathInfo( (PSZ)zRelativeCp, FIL_QUERYFULLNAME, 
   24255                 :                                 zFullCp, CCHMAXPATH );
   24256                 :   free( zRelativeCp );
   24257                 :   zFullUTF = convertCpPathToUtf8( zFullCp );
   24258                 :   sqlite3_snprintf( nFull, zFull, zFullUTF );
   24259                 :   free( zFullUTF );
   24260                 :   return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
   24261                 : }
   24262                 : 
   24263                 : 
   24264                 : /*
   24265                 : ** Open a file.
   24266                 : */
   24267                 : static int os2Open(
   24268                 :   sqlite3_vfs *pVfs,            /* Not used */
   24269                 :   const char *zName,            /* Name of the file (UTF-8) */
   24270                 :   sqlite3_file *id,             /* Write the SQLite file handle here */
   24271                 :   int flags,                    /* Open mode flags */
   24272                 :   int *pOutFlags                /* Status return flags */
   24273                 : ){
   24274                 :   HFILE h;
   24275                 :   ULONG ulOpenFlags = 0;
   24276                 :   ULONG ulOpenMode = 0;
   24277                 :   ULONG ulAction = 0;
   24278                 :   ULONG rc;
   24279                 :   os2File *pFile = (os2File*)id;
   24280                 :   const char *zUtf8Name = zName;
   24281                 :   char *zNameCp;
   24282                 :   char  zTmpname[CCHMAXPATH];
   24283                 : 
   24284                 :   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
   24285                 :   int isCreate     = (flags & SQLITE_OPEN_CREATE);
   24286                 :   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
   24287                 : #ifndef NDEBUG
   24288                 :   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
   24289                 :   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
   24290                 :   int eType        = (flags & 0xFFFFFF00);
   24291                 :   int isOpenJournal = (isCreate && (
   24292                 :         eType==SQLITE_OPEN_MASTER_JOURNAL 
   24293                 :      || eType==SQLITE_OPEN_MAIN_JOURNAL 
   24294                 :      || eType==SQLITE_OPEN_WAL
   24295                 :   ));
   24296                 : #endif
   24297                 : 
   24298                 :   UNUSED_PARAMETER(pVfs);
   24299                 :   assert( id!=0 );
   24300                 : 
   24301                 :   /* Check the following statements are true: 
   24302                 :   **
   24303                 :   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
   24304                 :   **   (b) if CREATE is set, then READWRITE must also be set, and
   24305                 :   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
   24306                 :   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
   24307                 :   */
   24308                 :   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
   24309                 :   assert(isCreate==0 || isReadWrite);
   24310                 :   assert(isExclusive==0 || isCreate);
   24311                 :   assert(isDelete==0 || isCreate);
   24312                 : 
   24313                 :   /* The main DB, main journal, WAL file and master journal are never 
   24314                 :   ** automatically deleted. Nor are they ever temporary files.  */
   24315                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
   24316                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
   24317                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
   24318                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
   24319                 : 
   24320                 :   /* Assert that the upper layer has set one of the "file-type" flags. */
   24321                 :   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
   24322                 :        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
   24323                 :        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
   24324                 :        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
   24325                 :   );
   24326                 : 
   24327                 :   memset( pFile, 0, sizeof(*pFile) );
   24328                 :   pFile->h = (HFILE)-1;
   24329                 : 
   24330                 :   /* If the second argument to this function is NULL, generate a 
   24331                 :   ** temporary file name to use 
   24332                 :   */
   24333                 :   if( !zUtf8Name ){
   24334                 :     assert(isDelete && !isOpenJournal);
   24335                 :     rc = getTempname(CCHMAXPATH, zTmpname);
   24336                 :     if( rc!=SQLITE_OK ){
   24337                 :       return rc;
   24338                 :     }
   24339                 :     zUtf8Name = zTmpname;
   24340                 :   }
   24341                 : 
   24342                 :   if( isReadWrite ){
   24343                 :     ulOpenMode |= OPEN_ACCESS_READWRITE;
   24344                 :   }else{
   24345                 :     ulOpenMode |= OPEN_ACCESS_READONLY;
   24346                 :   }
   24347                 : 
   24348                 :   /* Open in random access mode for possibly better speed.  Allow full
   24349                 :   ** sharing because file locks will provide exclusive access when needed.
   24350                 :   ** The handle should not be inherited by child processes and we don't 
   24351                 :   ** want popups from the critical error handler.
   24352                 :   */
   24353                 :   ulOpenMode |= OPEN_FLAGS_RANDOM | OPEN_SHARE_DENYNONE | 
   24354                 :                 OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_FAIL_ON_ERROR;
   24355                 : 
   24356                 :   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
   24357                 :   ** created. SQLite doesn't use it to indicate "exclusive access" 
   24358                 :   ** as it is usually understood.
   24359                 :   */
   24360                 :   if( isExclusive ){
   24361                 :     /* Creates a new file, only if it does not already exist. */
   24362                 :     /* If the file exists, it fails. */
   24363                 :     ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
   24364                 :   }else if( isCreate ){
   24365                 :     /* Open existing file, or create if it doesn't exist */
   24366                 :     ulOpenFlags |= OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
   24367                 :   }else{
   24368                 :     /* Opens a file, only if it exists. */
   24369                 :     ulOpenFlags |= OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
   24370                 :   }
   24371                 : 
   24372                 :   zNameCp = convertUtf8PathToCp( zUtf8Name );
   24373                 :   rc = DosOpen( (PSZ)zNameCp,
   24374                 :                 &h,
   24375                 :                 &ulAction,
   24376                 :                 0L,
   24377                 :                 FILE_NORMAL,
   24378                 :                 ulOpenFlags,
   24379                 :                 ulOpenMode,
   24380                 :                 (PEAOP2)NULL );
   24381                 :   free( zNameCp );
   24382                 : 
   24383                 :   if( rc != NO_ERROR ){
   24384                 :     OSTRACE(( "OPEN Invalid handle rc=%d: zName=%s, ulAction=%#lx, ulFlags=%#lx, ulMode=%#lx\n",
   24385                 :               rc, zUtf8Name, ulAction, ulOpenFlags, ulOpenMode ));
   24386                 : 
   24387                 :     if( isReadWrite ){
   24388                 :       return os2Open( pVfs, zName, id,
   24389                 :                       ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
   24390                 :                       pOutFlags );
   24391                 :     }else{
   24392                 :       return SQLITE_CANTOPEN;
   24393                 :     }
   24394                 :   }
   24395                 : 
   24396                 :   if( pOutFlags ){
   24397                 :     *pOutFlags = isReadWrite ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
   24398                 :   }
   24399                 : 
   24400                 :   os2FullPathname( pVfs, zUtf8Name, sizeof( zTmpname ), zTmpname );
   24401                 :   pFile->zFullPathCp = convertUtf8PathToCp( zTmpname );
   24402                 :   pFile->pMethod = &os2IoMethod;
   24403                 :   pFile->flags = flags;
   24404                 :   pFile->h = h;
   24405                 : 
   24406                 :   OpenCounter(+1);
   24407                 :   OSTRACE(( "OPEN %d pOutFlags=%d\n", pFile->h, pOutFlags ));
   24408                 :   return SQLITE_OK;
   24409                 : }
   24410                 : 
   24411                 : /*
   24412                 : ** Delete the named file.
   24413                 : */
   24414                 : static int os2Delete(
   24415                 :   sqlite3_vfs *pVfs,                     /* Not used on os2 */
   24416                 :   const char *zFilename,                 /* Name of file to delete */
   24417                 :   int syncDir                            /* Not used on os2 */
   24418                 : ){
   24419                 :   APIRET rc;
   24420                 :   char *zFilenameCp;
   24421                 :   SimulateIOError( return SQLITE_IOERR_DELETE );
   24422                 :   zFilenameCp = convertUtf8PathToCp( zFilename );
   24423                 :   rc = DosDelete( (PSZ)zFilenameCp );
   24424                 :   free( zFilenameCp );
   24425                 :   OSTRACE(( "DELETE \"%s\"\n", zFilename ));
   24426                 :   return (rc == NO_ERROR ||
   24427                 :           rc == ERROR_FILE_NOT_FOUND ||
   24428                 :           rc == ERROR_PATH_NOT_FOUND ) ? SQLITE_OK : SQLITE_IOERR_DELETE;
   24429                 : }
   24430                 : 
   24431                 : /*
   24432                 : ** Check the existance and status of a file.
   24433                 : */
   24434                 : static int os2Access(
   24435                 :   sqlite3_vfs *pVfs,        /* Not used on os2 */
   24436                 :   const char *zFilename,    /* Name of file to check */
   24437                 :   int flags,                /* Type of test to make on this file */
   24438                 :   int *pOut                 /* Write results here */
   24439                 : ){
   24440                 :   APIRET rc;
   24441                 :   FILESTATUS3 fsts3ConfigInfo;
   24442                 :   char *zFilenameCp;
   24443                 : 
   24444                 :   UNUSED_PARAMETER(pVfs);
   24445                 :   SimulateIOError( return SQLITE_IOERR_ACCESS; );
   24446                 :   
   24447                 :   zFilenameCp = convertUtf8PathToCp( zFilename );
   24448                 :   rc = DosQueryPathInfo( (PSZ)zFilenameCp, FIL_STANDARD,
   24449                 :                          &fsts3ConfigInfo, sizeof(FILESTATUS3) );
   24450                 :   free( zFilenameCp );
   24451                 :   OSTRACE(( "ACCESS fsts3ConfigInfo.attrFile=%d flags=%d rc=%d\n",
   24452                 :             fsts3ConfigInfo.attrFile, flags, rc ));
   24453                 : 
   24454                 :   switch( flags ){
   24455                 :     case SQLITE_ACCESS_EXISTS:
   24456                 :       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
   24457                 :       ** as if it does not exist.
   24458                 :       */
   24459                 :       if( fsts3ConfigInfo.cbFile == 0 ) 
   24460                 :         rc = ERROR_FILE_NOT_FOUND;
   24461                 :       break;
   24462                 :     case SQLITE_ACCESS_READ:
   24463                 :       break;
   24464                 :     case SQLITE_ACCESS_READWRITE:
   24465                 :       if( fsts3ConfigInfo.attrFile & FILE_READONLY )
   24466                 :         rc = ERROR_ACCESS_DENIED;
   24467                 :       break;
   24468                 :     default:
   24469                 :       rc = ERROR_FILE_NOT_FOUND;
   24470                 :       assert( !"Invalid flags argument" );
   24471                 :   }
   24472                 : 
   24473                 :   *pOut = (rc == NO_ERROR);
   24474                 :   OSTRACE(( "ACCESS %s flags %d: rc=%d\n", zFilename, flags, *pOut ));
   24475                 : 
   24476                 :   return SQLITE_OK;
   24477                 : }
   24478                 : 
   24479                 : 
   24480                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
   24481                 : /*
   24482                 : ** Interfaces for opening a shared library, finding entry points
   24483                 : ** within the shared library, and closing the shared library.
   24484                 : */
   24485                 : /*
   24486                 : ** Interfaces for opening a shared library, finding entry points
   24487                 : ** within the shared library, and closing the shared library.
   24488                 : */
   24489                 : static void *os2DlOpen(sqlite3_vfs *pVfs, const char *zFilename){
   24490                 :   HMODULE hmod;
   24491                 :   APIRET rc;
   24492                 :   char *zFilenameCp = convertUtf8PathToCp(zFilename);
   24493                 :   rc = DosLoadModule(NULL, 0, (PSZ)zFilenameCp, &hmod);
   24494                 :   free(zFilenameCp);
   24495                 :   return rc != NO_ERROR ? 0 : (void*)hmod;
   24496                 : }
   24497                 : /*
   24498                 : ** A no-op since the error code is returned on the DosLoadModule call.
   24499                 : ** os2Dlopen returns zero if DosLoadModule is not successful.
   24500                 : */
   24501                 : static void os2DlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
   24502                 : /* no-op */
   24503                 : }
   24504                 : static void (*os2DlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
   24505                 :   PFN pfn;
   24506                 :   APIRET rc;
   24507                 :   rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)zSymbol, &pfn);
   24508                 :   if( rc != NO_ERROR ){
   24509                 :     /* if the symbol itself was not found, search again for the same
   24510                 :      * symbol with an extra underscore, that might be needed depending
   24511                 :      * on the calling convention */
   24512                 :     char _zSymbol[256] = "_";
   24513                 :     strncat(_zSymbol, zSymbol, 254);
   24514                 :     rc = DosQueryProcAddr((HMODULE)pHandle, 0L, (PSZ)_zSymbol, &pfn);
   24515                 :   }
   24516                 :   return rc != NO_ERROR ? 0 : (void(*)(void))pfn;
   24517                 : }
   24518                 : static void os2DlClose(sqlite3_vfs *pVfs, void *pHandle){
   24519                 :   DosFreeModule((HMODULE)pHandle);
   24520                 : }
   24521                 : #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
   24522                 :   #define os2DlOpen 0
   24523                 :   #define os2DlError 0
   24524                 :   #define os2DlSym 0
   24525                 :   #define os2DlClose 0
   24526                 : #endif
   24527                 : 
   24528                 : 
   24529                 : /*
   24530                 : ** Write up to nBuf bytes of randomness into zBuf.
   24531                 : */
   24532                 : static int os2Randomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf ){
   24533                 :   int n = 0;
   24534                 : #if defined(SQLITE_TEST)
   24535                 :   n = nBuf;
   24536                 :   memset(zBuf, 0, nBuf);
   24537                 : #else
   24538                 :   int i;                           
   24539                 :   PPIB ppib;
   24540                 :   PTIB ptib;
   24541                 :   DATETIME dt; 
   24542                 :   static unsigned c = 0;
   24543                 :   /* Ordered by variation probability */
   24544                 :   static ULONG svIdx[6] = { QSV_MS_COUNT, QSV_TIME_LOW,
   24545                 :                             QSV_MAXPRMEM, QSV_MAXSHMEM,
   24546                 :                             QSV_TOTAVAILMEM, QSV_TOTRESMEM };
   24547                 : 
   24548                 :   /* 8 bytes; timezone and weekday don't increase the randomness much */
   24549                 :   if( (int)sizeof(dt)-3 <= nBuf - n ){
   24550                 :     c += 0x0100;
   24551                 :     DosGetDateTime(&dt);
   24552                 :     dt.year = (USHORT)((dt.year - 1900) | c);
   24553                 :     memcpy(&zBuf[n], &dt, sizeof(dt)-3);
   24554                 :     n += sizeof(dt)-3;
   24555                 :   }
   24556                 : 
   24557                 :   /* 4 bytes; PIDs and TIDs are 16 bit internally, so combine them */
   24558                 :   if( (int)sizeof(ULONG) <= nBuf - n ){
   24559                 :     DosGetInfoBlocks(&ptib, &ppib);
   24560                 :     *(PULONG)&zBuf[n] = MAKELONG(ppib->pib_ulpid,
   24561                 :                                  ptib->tib_ptib2->tib2_ultid);
   24562                 :     n += sizeof(ULONG);
   24563                 :   }
   24564                 : 
   24565                 :   /* Up to 6 * 4 bytes; variables depend on the system state */
   24566                 :   for( i = 0; i < 6 && (int)sizeof(ULONG) <= nBuf - n; i++ ){
   24567                 :     DosQuerySysInfo(svIdx[i], svIdx[i], 
   24568                 :                     (PULONG)&zBuf[n], sizeof(ULONG));
   24569                 :     n += sizeof(ULONG);
   24570                 :   } 
   24571                 : #endif
   24572                 : 
   24573                 :   return n;
   24574                 : }
   24575                 : 
   24576                 : /*
   24577                 : ** Sleep for a little while.  Return the amount of time slept.
   24578                 : ** The argument is the number of microseconds we want to sleep.
   24579                 : ** The return value is the number of microseconds of sleep actually
   24580                 : ** requested from the underlying operating system, a number which
   24581                 : ** might be greater than or equal to the argument, but not less
   24582                 : ** than the argument.
   24583                 : */
   24584                 : static int os2Sleep( sqlite3_vfs *pVfs, int microsec ){
   24585                 :   DosSleep( (microsec/1000) );
   24586                 :   return microsec;
   24587                 : }
   24588                 : 
   24589                 : /*
   24590                 : ** The following variable, if set to a non-zero value, becomes the result
   24591                 : ** returned from sqlite3OsCurrentTime().  This is used for testing.
   24592                 : */
   24593                 : #ifdef SQLITE_TEST
   24594                 : SQLITE_API int sqlite3_current_time = 0;
   24595                 : #endif
   24596                 : 
   24597                 : /*
   24598                 : ** Find the current time (in Universal Coordinated Time).  Write into *piNow
   24599                 : ** the current time and date as a Julian Day number times 86_400_000.  In
   24600                 : ** other words, write into *piNow the number of milliseconds since the Julian
   24601                 : ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
   24602                 : ** proleptic Gregorian calendar.
   24603                 : **
   24604                 : ** On success, return 0.  Return 1 if the time and date cannot be found.
   24605                 : */
   24606                 : static int os2CurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
   24607                 : #ifdef SQLITE_TEST
   24608                 :   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
   24609                 : #endif
   24610                 :   int year, month, datepart, timepart;
   24611                 :  
   24612                 :   DATETIME dt;
   24613                 :   DosGetDateTime( &dt );
   24614                 : 
   24615                 :   year = dt.year;
   24616                 :   month = dt.month;
   24617                 : 
   24618                 :   /* Calculations from http://www.astro.keele.ac.uk/~rno/Astronomy/hjd.html
   24619                 :   ** http://www.astro.keele.ac.uk/~rno/Astronomy/hjd-0.1.c
   24620                 :   ** Calculate the Julian days
   24621                 :   */
   24622                 :   datepart = (int)dt.day - 32076 +
   24623                 :     1461*(year + 4800 + (month - 14)/12)/4 +
   24624                 :     367*(month - 2 - (month - 14)/12*12)/12 -
   24625                 :     3*((year + 4900 + (month - 14)/12)/100)/4;
   24626                 : 
   24627                 :   /* Time in milliseconds, hours to noon added */
   24628                 :   timepart = 12*3600*1000 + dt.hundredths*10 + dt.seconds*1000 +
   24629                 :     ((int)dt.minutes + dt.timezone)*60*1000 + dt.hours*3600*1000;
   24630                 : 
   24631                 :   *piNow = (sqlite3_int64)datepart*86400*1000 + timepart;
   24632                 :    
   24633                 : #ifdef SQLITE_TEST
   24634                 :   if( sqlite3_current_time ){
   24635                 :     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
   24636                 :   }
   24637                 : #endif
   24638                 : 
   24639                 :   UNUSED_PARAMETER(pVfs);
   24640                 :   return 0;
   24641                 : }
   24642                 : 
   24643                 : /*
   24644                 : ** Find the current time (in Universal Coordinated Time).  Write the
   24645                 : ** current time and date as a Julian Day number into *prNow and
   24646                 : ** return 0.  Return 1 if the time and date cannot be found.
   24647                 : */
   24648                 : static int os2CurrentTime( sqlite3_vfs *pVfs, double *prNow ){
   24649                 :   int rc;
   24650                 :   sqlite3_int64 i;
   24651                 :   rc = os2CurrentTimeInt64(pVfs, &i);
   24652                 :   if( !rc ){
   24653                 :     *prNow = i/86400000.0;
   24654                 :   }
   24655                 :   return rc;
   24656                 : }
   24657                 : 
   24658                 : /*
   24659                 : ** The idea is that this function works like a combination of
   24660                 : ** GetLastError() and FormatMessage() on windows (or errno and
   24661                 : ** strerror_r() on unix). After an error is returned by an OS
   24662                 : ** function, SQLite calls this function with zBuf pointing to
   24663                 : ** a buffer of nBuf bytes. The OS layer should populate the
   24664                 : ** buffer with a nul-terminated UTF-8 encoded error message
   24665                 : ** describing the last IO error to have occurred within the calling
   24666                 : ** thread.
   24667                 : **
   24668                 : ** If the error message is too large for the supplied buffer,
   24669                 : ** it should be truncated. The return value of xGetLastError
   24670                 : ** is zero if the error message fits in the buffer, or non-zero
   24671                 : ** otherwise (if the message was truncated). If non-zero is returned,
   24672                 : ** then it is not necessary to include the nul-terminator character
   24673                 : ** in the output buffer.
   24674                 : **
   24675                 : ** Not supplying an error message will have no adverse effect
   24676                 : ** on SQLite. It is fine to have an implementation that never
   24677                 : ** returns an error message:
   24678                 : **
   24679                 : **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   24680                 : **     assert(zBuf[0]=='\0');
   24681                 : **     return 0;
   24682                 : **   }
   24683                 : **
   24684                 : ** However if an error message is supplied, it will be incorporated
   24685                 : ** by sqlite into the error message available to the user using
   24686                 : ** sqlite3_errmsg(), possibly making IO errors easier to debug.
   24687                 : */
   24688                 : static int os2GetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   24689                 :   assert(zBuf[0]=='\0');
   24690                 :   return 0;
   24691                 : }
   24692                 : 
   24693                 : /*
   24694                 : ** Initialize and deinitialize the operating system interface.
   24695                 : */
   24696                 : SQLITE_API int sqlite3_os_init(void){
   24697                 :   static sqlite3_vfs os2Vfs = {
   24698                 :     3,                 /* iVersion */
   24699                 :     sizeof(os2File),   /* szOsFile */
   24700                 :     CCHMAXPATH,        /* mxPathname */
   24701                 :     0,                 /* pNext */
   24702                 :     "os2",             /* zName */
   24703                 :     0,                 /* pAppData */
   24704                 : 
   24705                 :     os2Open,           /* xOpen */
   24706                 :     os2Delete,         /* xDelete */
   24707                 :     os2Access,         /* xAccess */
   24708                 :     os2FullPathname,   /* xFullPathname */
   24709                 :     os2DlOpen,         /* xDlOpen */
   24710                 :     os2DlError,        /* xDlError */
   24711                 :     os2DlSym,          /* xDlSym */
   24712                 :     os2DlClose,        /* xDlClose */
   24713                 :     os2Randomness,     /* xRandomness */
   24714                 :     os2Sleep,          /* xSleep */
   24715                 :     os2CurrentTime,    /* xCurrentTime */
   24716                 :     os2GetLastError,   /* xGetLastError */
   24717                 :     os2CurrentTimeInt64, /* xCurrentTimeInt64 */
   24718                 :     0,                 /* xSetSystemCall */
   24719                 :     0,                 /* xGetSystemCall */
   24720                 :     0                  /* xNextSystemCall */
   24721                 :   };
   24722                 :   sqlite3_vfs_register(&os2Vfs, 1);
   24723                 :   initUconvObjects();
   24724                 : /*  sqlite3OSTrace = 1; */
   24725                 :   return SQLITE_OK;
   24726                 : }
   24727                 : SQLITE_API int sqlite3_os_end(void){
   24728                 :   freeUconvObjects();
   24729                 :   return SQLITE_OK;
   24730                 : }
   24731                 : 
   24732                 : #endif /* SQLITE_OS_OS2 */
   24733                 : 
   24734                 : /************** End of os_os2.c **********************************************/
   24735                 : /************** Begin file os_unix.c *****************************************/
   24736                 : /*
   24737                 : ** 2004 May 22
   24738                 : **
   24739                 : ** The author disclaims copyright to this source code.  In place of
   24740                 : ** a legal notice, here is a blessing:
   24741                 : **
   24742                 : **    May you do good and not evil.
   24743                 : **    May you find forgiveness for yourself and forgive others.
   24744                 : **    May you share freely, never taking more than you give.
   24745                 : **
   24746                 : ******************************************************************************
   24747                 : **
   24748                 : ** This file contains the VFS implementation for unix-like operating systems
   24749                 : ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
   24750                 : **
   24751                 : ** There are actually several different VFS implementations in this file.
   24752                 : ** The differences are in the way that file locking is done.  The default
   24753                 : ** implementation uses Posix Advisory Locks.  Alternative implementations
   24754                 : ** use flock(), dot-files, various proprietary locking schemas, or simply
   24755                 : ** skip locking all together.
   24756                 : **
   24757                 : ** This source file is organized into divisions where the logic for various
   24758                 : ** subfunctions is contained within the appropriate division.  PLEASE
   24759                 : ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
   24760                 : ** in the correct division and should be clearly labeled.
   24761                 : **
   24762                 : ** The layout of divisions is as follows:
   24763                 : **
   24764                 : **   *  General-purpose declarations and utility functions.
   24765                 : **   *  Unique file ID logic used by VxWorks.
   24766                 : **   *  Various locking primitive implementations (all except proxy locking):
   24767                 : **      + for Posix Advisory Locks
   24768                 : **      + for no-op locks
   24769                 : **      + for dot-file locks
   24770                 : **      + for flock() locking
   24771                 : **      + for named semaphore locks (VxWorks only)
   24772                 : **      + for AFP filesystem locks (MacOSX only)
   24773                 : **   *  sqlite3_file methods not associated with locking.
   24774                 : **   *  Definitions of sqlite3_io_methods objects for all locking
   24775                 : **      methods plus "finder" functions for each locking method.
   24776                 : **   *  sqlite3_vfs method implementations.
   24777                 : **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
   24778                 : **   *  Definitions of sqlite3_vfs objects for all locking methods
   24779                 : **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
   24780                 : */
   24781                 : #if SQLITE_OS_UNIX              /* This file is used on unix only */
   24782                 : 
   24783                 : /*
   24784                 : ** There are various methods for file locking used for concurrency
   24785                 : ** control:
   24786                 : **
   24787                 : **   1. POSIX locking (the default),
   24788                 : **   2. No locking,
   24789                 : **   3. Dot-file locking,
   24790                 : **   4. flock() locking,
   24791                 : **   5. AFP locking (OSX only),
   24792                 : **   6. Named POSIX semaphores (VXWorks only),
   24793                 : **   7. proxy locking. (OSX only)
   24794                 : **
   24795                 : ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
   24796                 : ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
   24797                 : ** selection of the appropriate locking style based on the filesystem
   24798                 : ** where the database is located.  
   24799                 : */
   24800                 : #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
   24801                 : #  if defined(__APPLE__)
   24802                 : #    define SQLITE_ENABLE_LOCKING_STYLE 1
   24803                 : #  else
   24804                 : #    define SQLITE_ENABLE_LOCKING_STYLE 0
   24805                 : #  endif
   24806                 : #endif
   24807                 : 
   24808                 : /*
   24809                 : ** Define the OS_VXWORKS pre-processor macro to 1 if building on 
   24810                 : ** vxworks, or 0 otherwise.
   24811                 : */
   24812                 : #ifndef OS_VXWORKS
   24813                 : #  if defined(__RTP__) || defined(_WRS_KERNEL)
   24814                 : #    define OS_VXWORKS 1
   24815                 : #  else
   24816                 : #    define OS_VXWORKS 0
   24817                 : #  endif
   24818                 : #endif
   24819                 : 
   24820                 : /*
   24821                 : ** These #defines should enable >2GB file support on Posix if the
   24822                 : ** underlying operating system supports it.  If the OS lacks
   24823                 : ** large file support, these should be no-ops.
   24824                 : **
   24825                 : ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
   24826                 : ** on the compiler command line.  This is necessary if you are compiling
   24827                 : ** on a recent machine (ex: RedHat 7.2) but you want your code to work
   24828                 : ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
   24829                 : ** without this option, LFS is enable.  But LFS does not exist in the kernel
   24830                 : ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
   24831                 : ** portability you should omit LFS.
   24832                 : **
   24833                 : ** The previous paragraph was written in 2005.  (This paragraph is written
   24834                 : ** on 2008-11-28.) These days, all Linux kernels support large files, so
   24835                 : ** you should probably leave LFS enabled.  But some embedded platforms might
   24836                 : ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
   24837                 : */
   24838                 : #ifndef SQLITE_DISABLE_LFS
   24839                 : # define _LARGE_FILE       1
   24840                 : # ifndef _FILE_OFFSET_BITS
   24841                 : #   define _FILE_OFFSET_BITS 64
   24842                 : # endif
   24843                 : # define _LARGEFILE_SOURCE 1
   24844                 : #endif
   24845                 : 
   24846                 : /*
   24847                 : ** standard include files.
   24848                 : */
   24849                 : #include <sys/types.h>
   24850                 : #include <sys/stat.h>
   24851                 : #include <fcntl.h>
   24852                 : #include <unistd.h>
   24853                 : /* #include <time.h> */
   24854                 : #include <sys/time.h>
   24855                 : #include <errno.h>
   24856                 : #ifndef SQLITE_OMIT_WAL
   24857                 : #include <sys/mman.h>
   24858                 : #endif
   24859                 : 
   24860                 : 
   24861                 : #if SQLITE_ENABLE_LOCKING_STYLE
   24862                 : # include <sys/ioctl.h>
   24863                 : # if OS_VXWORKS
   24864                 : #  include <semaphore.h>
   24865                 : #  include <limits.h>
   24866                 : # else
   24867                 : #  include <sys/file.h>
   24868                 : #  include <sys/param.h>
   24869                 : # endif
   24870                 : #endif /* SQLITE_ENABLE_LOCKING_STYLE */
   24871                 : 
   24872                 : #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
   24873                 : # include <sys/mount.h>
   24874                 : #endif
   24875                 : 
   24876                 : #ifdef HAVE_UTIME
   24877                 : # include <utime.h>
   24878                 : #endif
   24879                 : 
   24880                 : /*
   24881                 : ** Allowed values of unixFile.fsFlags
   24882                 : */
   24883                 : #define SQLITE_FSFLAGS_IS_MSDOS     0x1
   24884                 : 
   24885                 : /*
   24886                 : ** If we are to be thread-safe, include the pthreads header and define
   24887                 : ** the SQLITE_UNIX_THREADS macro.
   24888                 : */
   24889                 : #if SQLITE_THREADSAFE
   24890                 : /* # include <pthread.h> */
   24891                 : # define SQLITE_UNIX_THREADS 1
   24892                 : #endif
   24893                 : 
   24894                 : /*
   24895                 : ** Default permissions when creating a new file
   24896                 : */
   24897                 : #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
   24898                 : # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
   24899                 : #endif
   24900                 : 
   24901                 : /*
   24902                 :  ** Default permissions when creating auto proxy dir
   24903                 :  */
   24904                 : #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
   24905                 : # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
   24906                 : #endif
   24907                 : 
   24908                 : /*
   24909                 : ** Maximum supported path-length.
   24910                 : */
   24911                 : #define MAX_PATHNAME 512
   24912                 : 
   24913                 : /*
   24914                 : ** Only set the lastErrno if the error code is a real error and not 
   24915                 : ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
   24916                 : */
   24917                 : #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
   24918                 : 
   24919                 : /* Forward references */
   24920                 : typedef struct unixShm unixShm;               /* Connection shared memory */
   24921                 : typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
   24922                 : typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
   24923                 : typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
   24924                 : 
   24925                 : /*
   24926                 : ** Sometimes, after a file handle is closed by SQLite, the file descriptor
   24927                 : ** cannot be closed immediately. In these cases, instances of the following
   24928                 : ** structure are used to store the file descriptor while waiting for an
   24929                 : ** opportunity to either close or reuse it.
   24930                 : */
   24931                 : struct UnixUnusedFd {
   24932                 :   int fd;                   /* File descriptor to close */
   24933                 :   int flags;                /* Flags this file descriptor was opened with */
   24934                 :   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
   24935                 : };
   24936                 : 
   24937                 : /*
   24938                 : ** The unixFile structure is subclass of sqlite3_file specific to the unix
   24939                 : ** VFS implementations.
   24940                 : */
   24941                 : typedef struct unixFile unixFile;
   24942                 : struct unixFile {
   24943                 :   sqlite3_io_methods const *pMethod;  /* Always the first entry */
   24944                 :   sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
   24945                 :   unixInodeInfo *pInode;              /* Info about locks on this inode */
   24946                 :   int h;                              /* The file descriptor */
   24947                 :   unsigned char eFileLock;            /* The type of lock held on this fd */
   24948                 :   unsigned char ctrlFlags;            /* Behavioral bits.  UNIXFILE_* flags */
   24949                 :   int lastErrno;                      /* The unix errno from last I/O error */
   24950                 :   void *lockingContext;               /* Locking style specific state */
   24951                 :   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
   24952                 :   const char *zPath;                  /* Name of the file */
   24953                 :   unixShm *pShm;                      /* Shared memory segment information */
   24954                 :   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
   24955                 : #if SQLITE_ENABLE_LOCKING_STYLE
   24956                 :   int openFlags;                      /* The flags specified at open() */
   24957                 : #endif
   24958                 : #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
   24959                 :   unsigned fsFlags;                   /* cached details from statfs() */
   24960                 : #endif
   24961                 : #if OS_VXWORKS
   24962                 :   struct vxworksFileId *pId;          /* Unique file ID */
   24963                 : #endif
   24964                 : #ifndef NDEBUG
   24965                 :   /* The next group of variables are used to track whether or not the
   24966                 :   ** transaction counter in bytes 24-27 of database files are updated
   24967                 :   ** whenever any part of the database changes.  An assertion fault will
   24968                 :   ** occur if a file is updated without also updating the transaction
   24969                 :   ** counter.  This test is made to avoid new problems similar to the
   24970                 :   ** one described by ticket #3584. 
   24971                 :   */
   24972                 :   unsigned char transCntrChng;   /* True if the transaction counter changed */
   24973                 :   unsigned char dbUpdate;        /* True if any part of database file changed */
   24974                 :   unsigned char inNormalWrite;   /* True if in a normal write operation */
   24975                 : #endif
   24976                 : #ifdef SQLITE_TEST
   24977                 :   /* In test mode, increase the size of this structure a bit so that 
   24978                 :   ** it is larger than the struct CrashFile defined in test6.c.
   24979                 :   */
   24980                 :   char aPadding[32];
   24981                 : #endif
   24982                 : };
   24983                 : 
   24984                 : /*
   24985                 : ** Allowed values for the unixFile.ctrlFlags bitmask:
   24986                 : */
   24987                 : #define UNIXFILE_EXCL        0x01     /* Connections from one process only */
   24988                 : #define UNIXFILE_RDONLY      0x02     /* Connection is read only */
   24989                 : #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
   24990                 : #ifndef SQLITE_DISABLE_DIRSYNC
   24991                 : # define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
   24992                 : #else
   24993                 : # define UNIXFILE_DIRSYNC    0x00
   24994                 : #endif
   24995                 : #define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
   24996                 : #define UNIXFILE_DELETE      0x20     /* Delete on close */
   24997                 : #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
   24998                 : #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
   24999                 : 
   25000                 : /*
   25001                 : ** Include code that is common to all os_*.c files
   25002                 : */
   25003                 : /************** Include os_common.h in the middle of os_unix.c ***************/
   25004                 : /************** Begin file os_common.h ***************************************/
   25005                 : /*
   25006                 : ** 2004 May 22
   25007                 : **
   25008                 : ** The author disclaims copyright to this source code.  In place of
   25009                 : ** a legal notice, here is a blessing:
   25010                 : **
   25011                 : **    May you do good and not evil.
   25012                 : **    May you find forgiveness for yourself and forgive others.
   25013                 : **    May you share freely, never taking more than you give.
   25014                 : **
   25015                 : ******************************************************************************
   25016                 : **
   25017                 : ** This file contains macros and a little bit of code that is common to
   25018                 : ** all of the platform-specific files (os_*.c) and is #included into those
   25019                 : ** files.
   25020                 : **
   25021                 : ** This file should be #included by the os_*.c files only.  It is not a
   25022                 : ** general purpose header file.
   25023                 : */
   25024                 : #ifndef _OS_COMMON_H_
   25025                 : #define _OS_COMMON_H_
   25026                 : 
   25027                 : /*
   25028                 : ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
   25029                 : ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
   25030                 : ** switch.  The following code should catch this problem at compile-time.
   25031                 : */
   25032                 : #ifdef MEMORY_DEBUG
   25033                 : # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
   25034                 : #endif
   25035                 : 
   25036                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   25037                 : # ifndef SQLITE_DEBUG_OS_TRACE
   25038                 : #   define SQLITE_DEBUG_OS_TRACE 0
   25039                 : # endif
   25040                 :   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
   25041                 : # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
   25042                 : #else
   25043                 : # define OSTRACE(X)
   25044                 : #endif
   25045                 : 
   25046                 : /*
   25047                 : ** Macros for performance tracing.  Normally turned off.  Only works
   25048                 : ** on i486 hardware.
   25049                 : */
   25050                 : #ifdef SQLITE_PERFORMANCE_TRACE
   25051                 : 
   25052                 : /* 
   25053                 : ** hwtime.h contains inline assembler code for implementing 
   25054                 : ** high-performance timing routines.
   25055                 : */
   25056                 : /************** Include hwtime.h in the middle of os_common.h ****************/
   25057                 : /************** Begin file hwtime.h ******************************************/
   25058                 : /*
   25059                 : ** 2008 May 27
   25060                 : **
   25061                 : ** The author disclaims copyright to this source code.  In place of
   25062                 : ** a legal notice, here is a blessing:
   25063                 : **
   25064                 : **    May you do good and not evil.
   25065                 : **    May you find forgiveness for yourself and forgive others.
   25066                 : **    May you share freely, never taking more than you give.
   25067                 : **
   25068                 : ******************************************************************************
   25069                 : **
   25070                 : ** This file contains inline asm code for retrieving "high-performance"
   25071                 : ** counters for x86 class CPUs.
   25072                 : */
   25073                 : #ifndef _HWTIME_H_
   25074                 : #define _HWTIME_H_
   25075                 : 
   25076                 : /*
   25077                 : ** The following routine only works on pentium-class (or newer) processors.
   25078                 : ** It uses the RDTSC opcode to read the cycle count value out of the
   25079                 : ** processor and returns that value.  This can be used for high-res
   25080                 : ** profiling.
   25081                 : */
   25082                 : #if (defined(__GNUC__) || defined(_MSC_VER)) && \
   25083                 :       (defined(i386) || defined(__i386__) || defined(_M_IX86))
   25084                 : 
   25085                 :   #if defined(__GNUC__)
   25086                 : 
   25087                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   25088                 :      unsigned int lo, hi;
   25089                 :      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   25090                 :      return (sqlite_uint64)hi << 32 | lo;
   25091                 :   }
   25092                 : 
   25093                 :   #elif defined(_MSC_VER)
   25094                 : 
   25095                 :   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
   25096                 :      __asm {
   25097                 :         rdtsc
   25098                 :         ret       ; return value at EDX:EAX
   25099                 :      }
   25100                 :   }
   25101                 : 
   25102                 :   #endif
   25103                 : 
   25104                 : #elif (defined(__GNUC__) && defined(__x86_64__))
   25105                 : 
   25106                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   25107                 :       unsigned long val;
   25108                 :       __asm__ __volatile__ ("rdtsc" : "=A" (val));
   25109                 :       return val;
   25110                 :   }
   25111                 :  
   25112                 : #elif (defined(__GNUC__) && defined(__ppc__))
   25113                 : 
   25114                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   25115                 :       unsigned long long retval;
   25116                 :       unsigned long junk;
   25117                 :       __asm__ __volatile__ ("\n\
   25118                 :           1:      mftbu   %1\n\
   25119                 :                   mftb    %L0\n\
   25120                 :                   mftbu   %0\n\
   25121                 :                   cmpw    %0,%1\n\
   25122                 :                   bne     1b"
   25123                 :                   : "=r" (retval), "=r" (junk));
   25124                 :       return retval;
   25125                 :   }
   25126                 : 
   25127                 : #else
   25128                 : 
   25129                 :   #error Need implementation of sqlite3Hwtime() for your platform.
   25130                 : 
   25131                 :   /*
   25132                 :   ** To compile without implementing sqlite3Hwtime() for your platform,
   25133                 :   ** you can remove the above #error and use the following
   25134                 :   ** stub function.  You will lose timing support for many
   25135                 :   ** of the debugging and testing utilities, but it should at
   25136                 :   ** least compile and run.
   25137                 :   */
   25138                 : SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
   25139                 : 
   25140                 : #endif
   25141                 : 
   25142                 : #endif /* !defined(_HWTIME_H_) */
   25143                 : 
   25144                 : /************** End of hwtime.h **********************************************/
   25145                 : /************** Continuing where we left off in os_common.h ******************/
   25146                 : 
   25147                 : static sqlite_uint64 g_start;
   25148                 : static sqlite_uint64 g_elapsed;
   25149                 : #define TIMER_START       g_start=sqlite3Hwtime()
   25150                 : #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
   25151                 : #define TIMER_ELAPSED     g_elapsed
   25152                 : #else
   25153                 : #define TIMER_START
   25154                 : #define TIMER_END
   25155                 : #define TIMER_ELAPSED     ((sqlite_uint64)0)
   25156                 : #endif
   25157                 : 
   25158                 : /*
   25159                 : ** If we compile with the SQLITE_TEST macro set, then the following block
   25160                 : ** of code will give us the ability to simulate a disk I/O error.  This
   25161                 : ** is used for testing the I/O recovery logic.
   25162                 : */
   25163                 : #ifdef SQLITE_TEST
   25164                 : SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
   25165                 : SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
   25166                 : SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
   25167                 : SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
   25168                 : SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
   25169                 : SQLITE_API int sqlite3_diskfull_pending = 0;
   25170                 : SQLITE_API int sqlite3_diskfull = 0;
   25171                 : #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
   25172                 : #define SimulateIOError(CODE)  \
   25173                 :   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
   25174                 :        || sqlite3_io_error_pending-- == 1 )  \
   25175                 :               { local_ioerr(); CODE; }
   25176                 : static void local_ioerr(){
   25177                 :   IOTRACE(("IOERR\n"));
   25178                 :   sqlite3_io_error_hit++;
   25179                 :   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
   25180                 : }
   25181                 : #define SimulateDiskfullError(CODE) \
   25182                 :    if( sqlite3_diskfull_pending ){ \
   25183                 :      if( sqlite3_diskfull_pending == 1 ){ \
   25184                 :        local_ioerr(); \
   25185                 :        sqlite3_diskfull = 1; \
   25186                 :        sqlite3_io_error_hit = 1; \
   25187                 :        CODE; \
   25188                 :      }else{ \
   25189                 :        sqlite3_diskfull_pending--; \
   25190                 :      } \
   25191                 :    }
   25192                 : #else
   25193                 : #define SimulateIOErrorBenign(X)
   25194                 : #define SimulateIOError(A)
   25195                 : #define SimulateDiskfullError(A)
   25196                 : #endif
   25197                 : 
   25198                 : /*
   25199                 : ** When testing, keep a count of the number of open files.
   25200                 : */
   25201                 : #ifdef SQLITE_TEST
   25202                 : SQLITE_API int sqlite3_open_file_count = 0;
   25203                 : #define OpenCounter(X)  sqlite3_open_file_count+=(X)
   25204                 : #else
   25205                 : #define OpenCounter(X)
   25206                 : #endif
   25207                 : 
   25208                 : #endif /* !defined(_OS_COMMON_H_) */
   25209                 : 
   25210                 : /************** End of os_common.h *******************************************/
   25211                 : /************** Continuing where we left off in os_unix.c ********************/
   25212                 : 
   25213                 : /*
   25214                 : ** Define various macros that are missing from some systems.
   25215                 : */
   25216                 : #ifndef O_LARGEFILE
   25217                 : # define O_LARGEFILE 0
   25218                 : #endif
   25219                 : #ifdef SQLITE_DISABLE_LFS
   25220                 : # undef O_LARGEFILE
   25221                 : # define O_LARGEFILE 0
   25222                 : #endif
   25223                 : #ifndef O_NOFOLLOW
   25224                 : # define O_NOFOLLOW 0
   25225                 : #endif
   25226                 : #ifndef O_BINARY
   25227                 : # define O_BINARY 0
   25228                 : #endif
   25229                 : 
   25230                 : /*
   25231                 : ** The threadid macro resolves to the thread-id or to 0.  Used for
   25232                 : ** testing and debugging only.
   25233                 : */
   25234                 : #if SQLITE_THREADSAFE
   25235                 : #define threadid pthread_self()
   25236                 : #else
   25237                 : #define threadid 0
   25238                 : #endif
   25239                 : 
   25240                 : /*
   25241                 : ** Different Unix systems declare open() in different ways.  Same use
   25242                 : ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
   25243                 : ** The difference is important when using a pointer to the function.
   25244                 : **
   25245                 : ** The safest way to deal with the problem is to always use this wrapper
   25246                 : ** which always has the same well-defined interface.
   25247                 : */
   25248           17307 : static int posixOpen(const char *zFile, int flags, int mode){
   25249           17307 :   return open(zFile, flags, mode);
   25250                 : }
   25251                 : 
   25252                 : /* Forward reference */
   25253                 : static int openDirectory(const char*, int*);
   25254                 : 
   25255                 : /*
   25256                 : ** Many system calls are accessed through pointer-to-functions so that
   25257                 : ** they may be overridden at runtime to facilitate fault injection during
   25258                 : ** testing and sandboxing.  The following array holds the names and pointers
   25259                 : ** to all overrideable system calls.
   25260                 : */
   25261                 : static struct unix_syscall {
   25262                 :   const char *zName;            /* Name of the sytem call */
   25263                 :   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
   25264                 :   sqlite3_syscall_ptr pDefault; /* Default value */
   25265                 : } aSyscall[] = {
   25266                 :   { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
   25267                 : #define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
   25268                 : 
   25269                 :   { "close",        (sqlite3_syscall_ptr)close,      0  },
   25270                 : #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
   25271                 : 
   25272                 :   { "access",       (sqlite3_syscall_ptr)access,     0  },
   25273                 : #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
   25274                 : 
   25275                 :   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
   25276                 : #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
   25277                 : 
   25278                 :   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
   25279                 : #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
   25280                 : 
   25281                 : /*
   25282                 : ** The DJGPP compiler environment looks mostly like Unix, but it
   25283                 : ** lacks the fcntl() system call.  So redefine fcntl() to be something
   25284                 : ** that always succeeds.  This means that locking does not occur under
   25285                 : ** DJGPP.  But it is DOS - what did you expect?
   25286                 : */
   25287                 : #ifdef __DJGPP__
   25288                 :   { "fstat",        0,                 0  },
   25289                 : #define osFstat(a,b,c)    0
   25290                 : #else     
   25291                 :   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
   25292                 : #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
   25293                 : #endif
   25294                 : 
   25295                 :   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
   25296                 : #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
   25297                 : 
   25298                 :   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
   25299                 : #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
   25300                 : 
   25301                 :   { "read",         (sqlite3_syscall_ptr)read,       0  },
   25302                 : #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
   25303                 : 
   25304                 : #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
   25305                 :   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
   25306                 : #else
   25307                 :   { "pread",        (sqlite3_syscall_ptr)0,          0  },
   25308                 : #endif
   25309                 : #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
   25310                 : 
   25311                 : #if defined(USE_PREAD64)
   25312                 :   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
   25313                 : #else
   25314                 :   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
   25315                 : #endif
   25316                 : #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
   25317                 : 
   25318                 :   { "write",        (sqlite3_syscall_ptr)write,      0  },
   25319                 : #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
   25320                 : 
   25321                 : #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
   25322                 :   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
   25323                 : #else
   25324                 :   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
   25325                 : #endif
   25326                 : #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
   25327                 :                     aSyscall[12].pCurrent)
   25328                 : 
   25329                 : #if defined(USE_PREAD64)
   25330                 :   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
   25331                 : #else
   25332                 :   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
   25333                 : #endif
   25334                 : #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
   25335                 :                     aSyscall[13].pCurrent)
   25336                 : 
   25337                 : #if SQLITE_ENABLE_LOCKING_STYLE
   25338                 :   { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
   25339                 : #else
   25340                 :   { "fchmod",       (sqlite3_syscall_ptr)0,          0  },
   25341                 : #endif
   25342                 : #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
   25343                 : 
   25344                 : #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
   25345                 :   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
   25346                 : #else
   25347                 :   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
   25348                 : #endif
   25349                 : #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
   25350                 : 
   25351                 :   { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
   25352                 : #define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
   25353                 : 
   25354                 :   { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
   25355                 : #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
   25356                 : 
   25357                 :   { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
   25358                 : #define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
   25359                 : 
   25360                 :   { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
   25361                 : #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
   25362                 : 
   25363                 : }; /* End of the overrideable system calls */
   25364                 : 
   25365                 : /*
   25366                 : ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
   25367                 : ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
   25368                 : ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
   25369                 : ** system call named zName.
   25370                 : */
   25371               0 : static int unixSetSystemCall(
   25372                 :   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
   25373                 :   const char *zName,            /* Name of system call to override */
   25374                 :   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
   25375                 : ){
   25376                 :   unsigned int i;
   25377               0 :   int rc = SQLITE_NOTFOUND;
   25378                 : 
   25379                 :   UNUSED_PARAMETER(pNotUsed);
   25380               0 :   if( zName==0 ){
   25381                 :     /* If no zName is given, restore all system calls to their default
   25382                 :     ** settings and return NULL
   25383                 :     */
   25384               0 :     rc = SQLITE_OK;
   25385               0 :     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   25386               0 :       if( aSyscall[i].pDefault ){
   25387               0 :         aSyscall[i].pCurrent = aSyscall[i].pDefault;
   25388                 :       }
   25389                 :     }
   25390                 :   }else{
   25391                 :     /* If zName is specified, operate on only the one system call
   25392                 :     ** specified.
   25393                 :     */
   25394               0 :     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   25395               0 :       if( strcmp(zName, aSyscall[i].zName)==0 ){
   25396               0 :         if( aSyscall[i].pDefault==0 ){
   25397               0 :           aSyscall[i].pDefault = aSyscall[i].pCurrent;
   25398                 :         }
   25399               0 :         rc = SQLITE_OK;
   25400               0 :         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
   25401               0 :         aSyscall[i].pCurrent = pNewFunc;
   25402               0 :         break;
   25403                 :       }
   25404                 :     }
   25405                 :   }
   25406               0 :   return rc;
   25407                 : }
   25408                 : 
   25409                 : /*
   25410                 : ** Return the value of a system call.  Return NULL if zName is not a
   25411                 : ** recognized system call name.  NULL is also returned if the system call
   25412                 : ** is currently undefined.
   25413                 : */
   25414               0 : static sqlite3_syscall_ptr unixGetSystemCall(
   25415                 :   sqlite3_vfs *pNotUsed,
   25416                 :   const char *zName
   25417                 : ){
   25418                 :   unsigned int i;
   25419                 : 
   25420                 :   UNUSED_PARAMETER(pNotUsed);
   25421               0 :   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   25422               0 :     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
   25423                 :   }
   25424               0 :   return 0;
   25425                 : }
   25426                 : 
   25427                 : /*
   25428                 : ** Return the name of the first system call after zName.  If zName==NULL
   25429                 : ** then return the name of the first system call.  Return NULL if zName
   25430                 : ** is the last system call or if zName is not the name of a valid
   25431                 : ** system call.
   25432                 : */
   25433               0 : static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
   25434               0 :   int i = -1;
   25435                 : 
   25436                 :   UNUSED_PARAMETER(p);
   25437               0 :   if( zName ){
   25438               0 :     for(i=0; i<ArraySize(aSyscall)-1; i++){
   25439               0 :       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
   25440                 :     }
   25441                 :   }
   25442               0 :   for(i++; i<ArraySize(aSyscall); i++){
   25443               0 :     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
   25444                 :   }
   25445               0 :   return 0;
   25446                 : }
   25447                 : 
   25448                 : /*
   25449                 : ** Retry open() calls that fail due to EINTR
   25450                 : */
   25451           17307 : static int robust_open(const char *z, int f, int m){
   25452                 :   int rc;
   25453           17307 :   do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR );
   25454           17307 :   return rc;
   25455                 : }
   25456                 : 
   25457                 : /*
   25458                 : ** Helper functions to obtain and relinquish the global mutex. The
   25459                 : ** global mutex is used to protect the unixInodeInfo and
   25460                 : ** vxworksFileId objects used by this file, all of which may be 
   25461                 : ** shared by multiple threads.
   25462                 : **
   25463                 : ** Function unixMutexHeld() is used to assert() that the global mutex 
   25464                 : ** is held when required. This function is only used as part of assert() 
   25465                 : ** statements. e.g.
   25466                 : **
   25467                 : **   unixEnterMutex()
   25468                 : **     assert( unixMutexHeld() );
   25469                 : **   unixEnterLeave()
   25470                 : */
   25471          185568 : static void unixEnterMutex(void){
   25472          185568 :   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   25473          185568 : }
   25474          185568 : static void unixLeaveMutex(void){
   25475          185568 :   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   25476          185568 : }
   25477                 : #ifdef SQLITE_DEBUG
   25478          109339 : static int unixMutexHeld(void) {
   25479          109339 :   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   25480                 : }
   25481                 : #endif
   25482                 : 
   25483                 : 
   25484                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   25485                 : /*
   25486                 : ** Helper function for printing out trace information from debugging
   25487                 : ** binaries. This returns the string represetation of the supplied
   25488                 : ** integer lock-type.
   25489                 : */
   25490                 : static const char *azFileLock(int eFileLock){
   25491                 :   switch( eFileLock ){
   25492                 :     case NO_LOCK: return "NONE";
   25493                 :     case SHARED_LOCK: return "SHARED";
   25494                 :     case RESERVED_LOCK: return "RESERVED";
   25495                 :     case PENDING_LOCK: return "PENDING";
   25496                 :     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
   25497                 :   }
   25498                 :   return "ERROR";
   25499                 : }
   25500                 : #endif
   25501                 : 
   25502                 : #ifdef SQLITE_LOCK_TRACE
   25503                 : /*
   25504                 : ** Print out information about all locking operations.
   25505                 : **
   25506                 : ** This routine is used for troubleshooting locks on multithreaded
   25507                 : ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
   25508                 : ** command-line option on the compiler.  This code is normally
   25509                 : ** turned off.
   25510                 : */
   25511                 : static int lockTrace(int fd, int op, struct flock *p){
   25512                 :   char *zOpName, *zType;
   25513                 :   int s;
   25514                 :   int savedErrno;
   25515                 :   if( op==F_GETLK ){
   25516                 :     zOpName = "GETLK";
   25517                 :   }else if( op==F_SETLK ){
   25518                 :     zOpName = "SETLK";
   25519                 :   }else{
   25520                 :     s = osFcntl(fd, op, p);
   25521                 :     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
   25522                 :     return s;
   25523                 :   }
   25524                 :   if( p->l_type==F_RDLCK ){
   25525                 :     zType = "RDLCK";
   25526                 :   }else if( p->l_type==F_WRLCK ){
   25527                 :     zType = "WRLCK";
   25528                 :   }else if( p->l_type==F_UNLCK ){
   25529                 :     zType = "UNLCK";
   25530                 :   }else{
   25531                 :     assert( 0 );
   25532                 :   }
   25533                 :   assert( p->l_whence==SEEK_SET );
   25534                 :   s = osFcntl(fd, op, p);
   25535                 :   savedErrno = errno;
   25536                 :   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
   25537                 :      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
   25538                 :      (int)p->l_pid, s);
   25539                 :   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
   25540                 :     struct flock l2;
   25541                 :     l2 = *p;
   25542                 :     osFcntl(fd, F_GETLK, &l2);
   25543                 :     if( l2.l_type==F_RDLCK ){
   25544                 :       zType = "RDLCK";
   25545                 :     }else if( l2.l_type==F_WRLCK ){
   25546                 :       zType = "WRLCK";
   25547                 :     }else if( l2.l_type==F_UNLCK ){
   25548                 :       zType = "UNLCK";
   25549                 :     }else{
   25550                 :       assert( 0 );
   25551                 :     }
   25552                 :     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
   25553                 :        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
   25554                 :   }
   25555                 :   errno = savedErrno;
   25556                 :   return s;
   25557                 : }
   25558                 : #undef osFcntl
   25559                 : #define osFcntl lockTrace
   25560                 : #endif /* SQLITE_LOCK_TRACE */
   25561                 : 
   25562                 : /*
   25563                 : ** Retry ftruncate() calls that fail due to EINTR
   25564                 : */
   25565            8401 : static int robust_ftruncate(int h, sqlite3_int64 sz){
   25566                 :   int rc;
   25567            8401 :   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
   25568            8401 :   return rc;
   25569                 : }
   25570                 : 
   25571                 : /*
   25572                 : ** This routine translates a standard POSIX errno code into something
   25573                 : ** useful to the clients of the sqlite3 functions.  Specifically, it is
   25574                 : ** intended to translate a variety of "try again" errors into SQLITE_BUSY
   25575                 : ** and a variety of "please close the file descriptor NOW" errors into 
   25576                 : ** SQLITE_IOERR
   25577                 : ** 
   25578                 : ** Errors during initialization of locks, or file system support for locks,
   25579                 : ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
   25580                 : */
   25581              30 : static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
   25582              30 :   switch (posixError) {
   25583                 : #if 0
   25584                 :   /* At one point this code was not commented out. In theory, this branch
   25585                 :   ** should never be hit, as this function should only be called after
   25586                 :   ** a locking-related function (i.e. fcntl()) has returned non-zero with
   25587                 :   ** the value of errno as the first argument. Since a system call has failed,
   25588                 :   ** errno should be non-zero.
   25589                 :   **
   25590                 :   ** Despite this, if errno really is zero, we still don't want to return
   25591                 :   ** SQLITE_OK. The system call failed, and *some* SQLite error should be
   25592                 :   ** propagated back to the caller. Commenting this branch out means errno==0
   25593                 :   ** will be handled by the "default:" case below.
   25594                 :   */
   25595                 :   case 0: 
   25596                 :     return SQLITE_OK;
   25597                 : #endif
   25598                 : 
   25599                 :   case EAGAIN:
   25600                 :   case ETIMEDOUT:
   25601                 :   case EBUSY:
   25602                 :   case EINTR:
   25603                 :   case ENOLCK:  
   25604                 :     /* random NFS retry error, unless during file system support 
   25605                 :      * introspection, in which it actually means what it says */
   25606               0 :     return SQLITE_BUSY;
   25607                 :     
   25608                 :   case EACCES: 
   25609                 :     /* EACCES is like EAGAIN during locking operations, but not any other time*/
   25610               0 :     if( (sqliteIOErr == SQLITE_IOERR_LOCK) || 
   25611               0 :         (sqliteIOErr == SQLITE_IOERR_UNLOCK) || 
   25612               0 :         (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
   25613                 :         (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
   25614               0 :       return SQLITE_BUSY;
   25615                 :     }
   25616                 :     /* else fall through */
   25617                 :   case EPERM: 
   25618               0 :     return SQLITE_PERM;
   25619                 :     
   25620                 :   /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
   25621                 :   ** this module never makes such a call. And the code in SQLite itself 
   25622                 :   ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
   25623                 :   ** this case is also commented out. If the system does set errno to EDEADLK,
   25624                 :   ** the default SQLITE_IOERR_XXX code will be returned. */
   25625                 : #if 0
   25626                 :   case EDEADLK:
   25627                 :     return SQLITE_IOERR_BLOCKED;
   25628                 : #endif
   25629                 :     
   25630                 : #if EOPNOTSUPP!=ENOTSUP
   25631                 :   case EOPNOTSUPP: 
   25632                 :     /* something went terribly awry, unless during file system support 
   25633                 :      * introspection, in which it actually means what it says */
   25634                 : #endif
   25635                 : #ifdef ENOTSUP
   25636                 :   case ENOTSUP: 
   25637                 :     /* invalid fd, unless during file system support introspection, in which 
   25638                 :      * it actually means what it says */
   25639                 : #endif
   25640                 :   case EIO:
   25641                 :   case EBADF:
   25642                 :   case EINVAL:
   25643                 :   case ENOTCONN:
   25644                 :   case ENODEV:
   25645                 :   case ENXIO:
   25646                 :   case ENOENT:
   25647                 : #ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
   25648                 :   case ESTALE:
   25649                 : #endif
   25650                 :   case ENOSYS:
   25651                 :     /* these should force the client to close the file and reconnect */
   25652                 :     
   25653                 :   default: 
   25654              30 :     return sqliteIOErr;
   25655                 :   }
   25656                 : }
   25657                 : 
   25658                 : 
   25659                 : 
   25660                 : /******************************************************************************
   25661                 : ****************** Begin Unique File ID Utility Used By VxWorks ***************
   25662                 : **
   25663                 : ** On most versions of unix, we can get a unique ID for a file by concatenating
   25664                 : ** the device number and the inode number.  But this does not work on VxWorks.
   25665                 : ** On VxWorks, a unique file id must be based on the canonical filename.
   25666                 : **
   25667                 : ** A pointer to an instance of the following structure can be used as a
   25668                 : ** unique file ID in VxWorks.  Each instance of this structure contains
   25669                 : ** a copy of the canonical filename.  There is also a reference count.  
   25670                 : ** The structure is reclaimed when the number of pointers to it drops to
   25671                 : ** zero.
   25672                 : **
   25673                 : ** There are never very many files open at one time and lookups are not
   25674                 : ** a performance-critical path, so it is sufficient to put these
   25675                 : ** structures on a linked list.
   25676                 : */
   25677                 : struct vxworksFileId {
   25678                 :   struct vxworksFileId *pNext;  /* Next in a list of them all */
   25679                 :   int nRef;                     /* Number of references to this one */
   25680                 :   int nName;                    /* Length of the zCanonicalName[] string */
   25681                 :   char *zCanonicalName;         /* Canonical filename */
   25682                 : };
   25683                 : 
   25684                 : #if OS_VXWORKS
   25685                 : /* 
   25686                 : ** All unique filenames are held on a linked list headed by this
   25687                 : ** variable:
   25688                 : */
   25689                 : static struct vxworksFileId *vxworksFileList = 0;
   25690                 : 
   25691                 : /*
   25692                 : ** Simplify a filename into its canonical form
   25693                 : ** by making the following changes:
   25694                 : **
   25695                 : **  * removing any trailing and duplicate /
   25696                 : **  * convert /./ into just /
   25697                 : **  * convert /A/../ where A is any simple name into just /
   25698                 : **
   25699                 : ** Changes are made in-place.  Return the new name length.
   25700                 : **
   25701                 : ** The original filename is in z[0..n-1].  Return the number of
   25702                 : ** characters in the simplified name.
   25703                 : */
   25704                 : static int vxworksSimplifyName(char *z, int n){
   25705                 :   int i, j;
   25706                 :   while( n>1 && z[n-1]=='/' ){ n--; }
   25707                 :   for(i=j=0; i<n; i++){
   25708                 :     if( z[i]=='/' ){
   25709                 :       if( z[i+1]=='/' ) continue;
   25710                 :       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
   25711                 :         i += 1;
   25712                 :         continue;
   25713                 :       }
   25714                 :       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
   25715                 :         while( j>0 && z[j-1]!='/' ){ j--; }
   25716                 :         if( j>0 ){ j--; }
   25717                 :         i += 2;
   25718                 :         continue;
   25719                 :       }
   25720                 :     }
   25721                 :     z[j++] = z[i];
   25722                 :   }
   25723                 :   z[j] = 0;
   25724                 :   return j;
   25725                 : }
   25726                 : 
   25727                 : /*
   25728                 : ** Find a unique file ID for the given absolute pathname.  Return
   25729                 : ** a pointer to the vxworksFileId object.  This pointer is the unique
   25730                 : ** file ID.
   25731                 : **
   25732                 : ** The nRef field of the vxworksFileId object is incremented before
   25733                 : ** the object is returned.  A new vxworksFileId object is created
   25734                 : ** and added to the global list if necessary.
   25735                 : **
   25736                 : ** If a memory allocation error occurs, return NULL.
   25737                 : */
   25738                 : static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
   25739                 :   struct vxworksFileId *pNew;         /* search key and new file ID */
   25740                 :   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
   25741                 :   int n;                              /* Length of zAbsoluteName string */
   25742                 : 
   25743                 :   assert( zAbsoluteName[0]=='/' );
   25744                 :   n = (int)strlen(zAbsoluteName);
   25745                 :   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
   25746                 :   if( pNew==0 ) return 0;
   25747                 :   pNew->zCanonicalName = (char*)&pNew[1];
   25748                 :   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
   25749                 :   n = vxworksSimplifyName(pNew->zCanonicalName, n);
   25750                 : 
   25751                 :   /* Search for an existing entry that matching the canonical name.
   25752                 :   ** If found, increment the reference count and return a pointer to
   25753                 :   ** the existing file ID.
   25754                 :   */
   25755                 :   unixEnterMutex();
   25756                 :   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
   25757                 :     if( pCandidate->nName==n 
   25758                 :      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
   25759                 :     ){
   25760                 :        sqlite3_free(pNew);
   25761                 :        pCandidate->nRef++;
   25762                 :        unixLeaveMutex();
   25763                 :        return pCandidate;
   25764                 :     }
   25765                 :   }
   25766                 : 
   25767                 :   /* No match was found.  We will make a new file ID */
   25768                 :   pNew->nRef = 1;
   25769                 :   pNew->nName = n;
   25770                 :   pNew->pNext = vxworksFileList;
   25771                 :   vxworksFileList = pNew;
   25772                 :   unixLeaveMutex();
   25773                 :   return pNew;
   25774                 : }
   25775                 : 
   25776                 : /*
   25777                 : ** Decrement the reference count on a vxworksFileId object.  Free
   25778                 : ** the object when the reference count reaches zero.
   25779                 : */
   25780                 : static void vxworksReleaseFileId(struct vxworksFileId *pId){
   25781                 :   unixEnterMutex();
   25782                 :   assert( pId->nRef>0 );
   25783                 :   pId->nRef--;
   25784                 :   if( pId->nRef==0 ){
   25785                 :     struct vxworksFileId **pp;
   25786                 :     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
   25787                 :     assert( *pp==pId );
   25788                 :     *pp = pId->pNext;
   25789                 :     sqlite3_free(pId);
   25790                 :   }
   25791                 :   unixLeaveMutex();
   25792                 : }
   25793                 : #endif /* OS_VXWORKS */
   25794                 : /*************** End of Unique File ID Utility Used By VxWorks ****************
   25795                 : ******************************************************************************/
   25796                 : 
   25797                 : 
   25798                 : /******************************************************************************
   25799                 : *************************** Posix Advisory Locking ****************************
   25800                 : **
   25801                 : ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
   25802                 : ** section 6.5.2.2 lines 483 through 490 specify that when a process
   25803                 : ** sets or clears a lock, that operation overrides any prior locks set
   25804                 : ** by the same process.  It does not explicitly say so, but this implies
   25805                 : ** that it overrides locks set by the same process using a different
   25806                 : ** file descriptor.  Consider this test case:
   25807                 : **
   25808                 : **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
   25809                 : **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
   25810                 : **
   25811                 : ** Suppose ./file1 and ./file2 are really the same file (because
   25812                 : ** one is a hard or symbolic link to the other) then if you set
   25813                 : ** an exclusive lock on fd1, then try to get an exclusive lock
   25814                 : ** on fd2, it works.  I would have expected the second lock to
   25815                 : ** fail since there was already a lock on the file due to fd1.
   25816                 : ** But not so.  Since both locks came from the same process, the
   25817                 : ** second overrides the first, even though they were on different
   25818                 : ** file descriptors opened on different file names.
   25819                 : **
   25820                 : ** This means that we cannot use POSIX locks to synchronize file access
   25821                 : ** among competing threads of the same process.  POSIX locks will work fine
   25822                 : ** to synchronize access for threads in separate processes, but not
   25823                 : ** threads within the same process.
   25824                 : **
   25825                 : ** To work around the problem, SQLite has to manage file locks internally
   25826                 : ** on its own.  Whenever a new database is opened, we have to find the
   25827                 : ** specific inode of the database file (the inode is determined by the
   25828                 : ** st_dev and st_ino fields of the stat structure that fstat() fills in)
   25829                 : ** and check for locks already existing on that inode.  When locks are
   25830                 : ** created or removed, we have to look at our own internal record of the
   25831                 : ** locks to see if another thread has previously set a lock on that same
   25832                 : ** inode.
   25833                 : **
   25834                 : ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
   25835                 : ** For VxWorks, we have to use the alternative unique ID system based on
   25836                 : ** canonical filename and implemented in the previous division.)
   25837                 : **
   25838                 : ** The sqlite3_file structure for POSIX is no longer just an integer file
   25839                 : ** descriptor.  It is now a structure that holds the integer file
   25840                 : ** descriptor and a pointer to a structure that describes the internal
   25841                 : ** locks on the corresponding inode.  There is one locking structure
   25842                 : ** per inode, so if the same inode is opened twice, both unixFile structures
   25843                 : ** point to the same locking structure.  The locking structure keeps
   25844                 : ** a reference count (so we will know when to delete it) and a "cnt"
   25845                 : ** field that tells us its internal lock status.  cnt==0 means the
   25846                 : ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
   25847                 : ** cnt>0 means there are cnt shared locks on the file.
   25848                 : **
   25849                 : ** Any attempt to lock or unlock a file first checks the locking
   25850                 : ** structure.  The fcntl() system call is only invoked to set a 
   25851                 : ** POSIX lock if the internal lock structure transitions between
   25852                 : ** a locked and an unlocked state.
   25853                 : **
   25854                 : ** But wait:  there are yet more problems with POSIX advisory locks.
   25855                 : **
   25856                 : ** If you close a file descriptor that points to a file that has locks,
   25857                 : ** all locks on that file that are owned by the current process are
   25858                 : ** released.  To work around this problem, each unixInodeInfo object
   25859                 : ** maintains a count of the number of pending locks on tha inode.
   25860                 : ** When an attempt is made to close an unixFile, if there are
   25861                 : ** other unixFile open on the same inode that are holding locks, the call
   25862                 : ** to close() the file descriptor is deferred until all of the locks clear.
   25863                 : ** The unixInodeInfo structure keeps a list of file descriptors that need to
   25864                 : ** be closed and that list is walked (and cleared) when the last lock
   25865                 : ** clears.
   25866                 : **
   25867                 : ** Yet another problem:  LinuxThreads do not play well with posix locks.
   25868                 : **
   25869                 : ** Many older versions of linux use the LinuxThreads library which is
   25870                 : ** not posix compliant.  Under LinuxThreads, a lock created by thread
   25871                 : ** A cannot be modified or overridden by a different thread B.
   25872                 : ** Only thread A can modify the lock.  Locking behavior is correct
   25873                 : ** if the appliation uses the newer Native Posix Thread Library (NPTL)
   25874                 : ** on linux - with NPTL a lock created by thread A can override locks
   25875                 : ** in thread B.  But there is no way to know at compile-time which
   25876                 : ** threading library is being used.  So there is no way to know at
   25877                 : ** compile-time whether or not thread A can override locks on thread B.
   25878                 : ** One has to do a run-time check to discover the behavior of the
   25879                 : ** current process.
   25880                 : **
   25881                 : ** SQLite used to support LinuxThreads.  But support for LinuxThreads
   25882                 : ** was dropped beginning with version 3.7.0.  SQLite will still work with
   25883                 : ** LinuxThreads provided that (1) there is no more than one connection 
   25884                 : ** per database file in the same process and (2) database connections
   25885                 : ** do not move across threads.
   25886                 : */
   25887                 : 
   25888                 : /*
   25889                 : ** An instance of the following structure serves as the key used
   25890                 : ** to locate a particular unixInodeInfo object.
   25891                 : */
   25892                 : struct unixFileId {
   25893                 :   dev_t dev;                  /* Device number */
   25894                 : #if OS_VXWORKS
   25895                 :   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
   25896                 : #else
   25897                 :   ino_t ino;                  /* Inode number */
   25898                 : #endif
   25899                 : };
   25900                 : 
   25901                 : /*
   25902                 : ** An instance of the following structure is allocated for each open
   25903                 : ** inode.  Or, on LinuxThreads, there is one of these structures for
   25904                 : ** each inode opened by each thread.
   25905                 : **
   25906                 : ** A single inode can have multiple file descriptors, so each unixFile
   25907                 : ** structure contains a pointer to an instance of this object and this
   25908                 : ** object keeps a count of the number of unixFile pointing to it.
   25909                 : */
   25910                 : struct unixInodeInfo {
   25911                 :   struct unixFileId fileId;       /* The lookup key */
   25912                 :   int nShared;                    /* Number of SHARED locks held */
   25913                 :   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
   25914                 :   unsigned char bProcessLock;     /* An exclusive process lock is held */
   25915                 :   int nRef;                       /* Number of pointers to this structure */
   25916                 :   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
   25917                 :   int nLock;                      /* Number of outstanding file locks */
   25918                 :   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
   25919                 :   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
   25920                 :   unixInodeInfo *pPrev;           /*    .... doubly linked */
   25921                 : #if SQLITE_ENABLE_LOCKING_STYLE
   25922                 :   unsigned long long sharedByte;  /* for AFP simulated shared lock */
   25923                 : #endif
   25924                 : #if OS_VXWORKS
   25925                 :   sem_t *pSem;                    /* Named POSIX semaphore */
   25926                 :   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
   25927                 : #endif
   25928                 : };
   25929                 : 
   25930                 : /*
   25931                 : ** A lists of all unixInodeInfo objects.
   25932                 : */
   25933                 : static unixInodeInfo *inodeList = 0;
   25934                 : 
   25935                 : /*
   25936                 : **
   25937                 : ** This function - unixLogError_x(), is only ever called via the macro
   25938                 : ** unixLogError().
   25939                 : **
   25940                 : ** It is invoked after an error occurs in an OS function and errno has been
   25941                 : ** set. It logs a message using sqlite3_log() containing the current value of
   25942                 : ** errno and, if possible, the human-readable equivalent from strerror() or
   25943                 : ** strerror_r().
   25944                 : **
   25945                 : ** The first argument passed to the macro should be the error code that
   25946                 : ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
   25947                 : ** The two subsequent arguments should be the name of the OS function that
   25948                 : ** failed (e.g. "unlink", "open") and the the associated file-system path,
   25949                 : ** if any.
   25950                 : */
   25951                 : #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
   25952               4 : static int unixLogErrorAtLine(
   25953                 :   int errcode,                    /* SQLite error code */
   25954                 :   const char *zFunc,              /* Name of OS function that failed */
   25955                 :   const char *zPath,              /* File path associated with error */
   25956                 :   int iLine                       /* Source line number where error occurred */
   25957                 : ){
   25958                 :   char *zErr;                     /* Message from strerror() or equivalent */
   25959               4 :   int iErrno = errno;             /* Saved syscall error number */
   25960                 : 
   25961                 :   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
   25962                 :   ** the strerror() function to obtain the human-readable error message
   25963                 :   ** equivalent to errno. Otherwise, use strerror_r().
   25964                 :   */ 
   25965                 : #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
   25966                 :   char aErr[80];
   25967                 :   memset(aErr, 0, sizeof(aErr));
   25968                 :   zErr = aErr;
   25969                 : 
   25970                 :   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
   25971                 :   ** assume that the system provides the the GNU version of strerror_r() that 
   25972                 :   ** returns a pointer to a buffer containing the error message. That pointer 
   25973                 :   ** may point to aErr[], or it may point to some static storage somewhere. 
   25974                 :   ** Otherwise, assume that the system provides the POSIX version of 
   25975                 :   ** strerror_r(), which always writes an error message into aErr[].
   25976                 :   **
   25977                 :   ** If the code incorrectly assumes that it is the POSIX version that is
   25978                 :   ** available, the error message will often be an empty string. Not a
   25979                 :   ** huge problem. Incorrectly concluding that the GNU version is available 
   25980                 :   ** could lead to a segfault though.
   25981                 :   */
   25982                 : #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
   25983                 :   zErr = 
   25984                 : # endif
   25985                 :   strerror_r(iErrno, aErr, sizeof(aErr)-1);
   25986                 : 
   25987                 : #elif SQLITE_THREADSAFE
   25988                 :   /* This is a threadsafe build, but strerror_r() is not available. */
   25989               4 :   zErr = "";
   25990                 : #else
   25991                 :   /* Non-threadsafe build, use strerror(). */
   25992                 :   zErr = strerror(iErrno);
   25993                 : #endif
   25994                 : 
   25995               4 :   assert( errcode!=SQLITE_OK );
   25996               4 :   if( zPath==0 ) zPath = "";
   25997               4 :   sqlite3_log(errcode,
   25998                 :       "os_unix.c:%d: (%d) %s(%s) - %s",
   25999                 :       iLine, iErrno, zFunc, zPath, zErr
   26000                 :   );
   26001                 : 
   26002               4 :   return errcode;
   26003                 : }
   26004                 : 
   26005                 : /*
   26006                 : ** Close a file descriptor.
   26007                 : **
   26008                 : ** We assume that close() almost always works, since it is only in a
   26009                 : ** very sick application or on a very sick platform that it might fail.
   26010                 : ** If it does fail, simply leak the file descriptor, but do log the
   26011                 : ** error.
   26012                 : **
   26013                 : ** Note that it is not safe to retry close() after EINTR since the
   26014                 : ** file descriptor might have already been reused by another thread.
   26015                 : ** So we don't even try to recover from an EINTR.  Just log the error
   26016                 : ** and move on.
   26017                 : */
   26018           17303 : static void robust_close(unixFile *pFile, int h, int lineno){
   26019           17303 :   if( osClose(h) ){
   26020               0 :     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
   26021                 :                        pFile ? pFile->zPath : 0, lineno);
   26022                 :   }
   26023           17303 : }
   26024                 : 
   26025                 : /*
   26026                 : ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
   26027                 : */ 
   26028           19028 : static void closePendingFds(unixFile *pFile){
   26029           19028 :   unixInodeInfo *pInode = pFile->pInode;
   26030                 :   UnixUnusedFd *p;
   26031                 :   UnixUnusedFd *pNext;
   26032           19100 :   for(p=pInode->pUnused; p; p=pNext){
   26033              72 :     pNext = p->pNext;
   26034              72 :     robust_close(pFile, p->fd, __LINE__);
   26035              72 :     sqlite3_free(p);
   26036                 :   }
   26037           19028 :   pInode->pUnused = 0;
   26038           19028 : }
   26039                 : 
   26040                 : /*
   26041                 : ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
   26042                 : **
   26043                 : ** The mutex entered using the unixEnterMutex() function must be held
   26044                 : ** when this function is called.
   26045                 : */
   26046            3052 : static void releaseInodeInfo(unixFile *pFile){
   26047            3052 :   unixInodeInfo *pInode = pFile->pInode;
   26048            3052 :   assert( unixMutexHeld() );
   26049            3052 :   if( ALWAYS(pInode) ){
   26050            3052 :     pInode->nRef--;
   26051            3052 :     if( pInode->nRef==0 ){
   26052            2950 :       assert( pInode->pShmNode==0 );
   26053            2950 :       closePendingFds(pFile);
   26054            2950 :       if( pInode->pPrev ){
   26055             391 :         assert( pInode->pPrev->pNext==pInode );
   26056             391 :         pInode->pPrev->pNext = pInode->pNext;
   26057                 :       }else{
   26058            2559 :         assert( inodeList==pInode );
   26059            2559 :         inodeList = pInode->pNext;
   26060                 :       }
   26061            2950 :       if( pInode->pNext ){
   26062            1389 :         assert( pInode->pNext->pPrev==pInode );
   26063            1389 :         pInode->pNext->pPrev = pInode->pPrev;
   26064                 :       }
   26065            2950 :       sqlite3_free(pInode);
   26066                 :     }
   26067                 :   }
   26068            3052 : }
   26069                 : 
   26070                 : /*
   26071                 : ** Given a file descriptor, locate the unixInodeInfo object that
   26072                 : ** describes that file descriptor.  Create a new one if necessary.  The
   26073                 : ** return value might be uninitialized if an error occurs.
   26074                 : **
   26075                 : ** The mutex entered using the unixEnterMutex() function must be held
   26076                 : ** when this function is called.
   26077                 : **
   26078                 : ** Return an appropriate error code.
   26079                 : */
   26080            3052 : static int findInodeInfo(
   26081                 :   unixFile *pFile,               /* Unix file with file desc used in the key */
   26082                 :   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
   26083                 : ){
   26084                 :   int rc;                        /* System call return code */
   26085                 :   int fd;                        /* The file descriptor for pFile */
   26086                 :   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
   26087                 :   struct stat statbuf;           /* Low-level file information */
   26088            3052 :   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
   26089                 : 
   26090            3052 :   assert( unixMutexHeld() );
   26091                 : 
   26092                 :   /* Get low-level information about the file that we can used to
   26093                 :   ** create a unique name for the file.
   26094                 :   */
   26095            3052 :   fd = pFile->h;
   26096            3052 :   rc = osFstat(fd, &statbuf);
   26097            3052 :   if( rc!=0 ){
   26098               0 :     pFile->lastErrno = errno;
   26099                 : #ifdef EOVERFLOW
   26100               0 :     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
   26101                 : #endif
   26102               0 :     return SQLITE_IOERR;
   26103                 :   }
   26104                 : 
   26105                 : #ifdef __APPLE__
   26106                 :   /* On OS X on an msdos filesystem, the inode number is reported
   26107                 :   ** incorrectly for zero-size files.  See ticket #3260.  To work
   26108                 :   ** around this problem (we consider it a bug in OS X, not SQLite)
   26109                 :   ** we always increase the file size to 1 by writing a single byte
   26110                 :   ** prior to accessing the inode number.  The one byte written is
   26111                 :   ** an ASCII 'S' character which also happens to be the first byte
   26112                 :   ** in the header of every SQLite database.  In this way, if there
   26113                 :   ** is a race condition such that another thread has already populated
   26114                 :   ** the first page of the database, no damage is done.
   26115                 :   */
   26116                 :   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
   26117                 :     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
   26118                 :     if( rc!=1 ){
   26119                 :       pFile->lastErrno = errno;
   26120                 :       return SQLITE_IOERR;
   26121                 :     }
   26122                 :     rc = osFstat(fd, &statbuf);
   26123                 :     if( rc!=0 ){
   26124                 :       pFile->lastErrno = errno;
   26125                 :       return SQLITE_IOERR;
   26126                 :     }
   26127                 :   }
   26128                 : #endif
   26129                 : 
   26130            3052 :   memset(&fileId, 0, sizeof(fileId));
   26131            3052 :   fileId.dev = statbuf.st_dev;
   26132                 : #if OS_VXWORKS
   26133                 :   fileId.pId = pFile->pId;
   26134                 : #else
   26135            3052 :   fileId.ino = statbuf.st_ino;
   26136                 : #endif
   26137            3052 :   pInode = inodeList;
   26138            9093 :   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
   26139            2989 :     pInode = pInode->pNext;
   26140                 :   }
   26141            3052 :   if( pInode==0 ){
   26142            2950 :     pInode = sqlite3_malloc( sizeof(*pInode) );
   26143            2950 :     if( pInode==0 ){
   26144               0 :       return SQLITE_NOMEM;
   26145                 :     }
   26146            2950 :     memset(pInode, 0, sizeof(*pInode));
   26147            2950 :     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
   26148            2950 :     pInode->nRef = 1;
   26149            2950 :     pInode->pNext = inodeList;
   26150            2950 :     pInode->pPrev = 0;
   26151            2950 :     if( inodeList ) inodeList->pPrev = pInode;
   26152            2950 :     inodeList = pInode;
   26153                 :   }else{
   26154             102 :     pInode->nRef++;
   26155                 :   }
   26156            3052 :   *ppInode = pInode;
   26157            3052 :   return SQLITE_OK;
   26158                 : }
   26159                 : 
   26160                 : 
   26161                 : /*
   26162                 : ** This routine checks if there is a RESERVED lock held on the specified
   26163                 : ** file by this or any other process. If such a lock is held, set *pResOut
   26164                 : ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   26165                 : ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   26166                 : */
   26167               0 : static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
   26168               0 :   int rc = SQLITE_OK;
   26169               0 :   int reserved = 0;
   26170               0 :   unixFile *pFile = (unixFile*)id;
   26171                 : 
   26172                 :   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   26173                 : 
   26174               0 :   assert( pFile );
   26175               0 :   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
   26176                 : 
   26177                 :   /* Check if a thread in this process holds such a lock */
   26178               0 :   if( pFile->pInode->eFileLock>SHARED_LOCK ){
   26179               0 :     reserved = 1;
   26180                 :   }
   26181                 : 
   26182                 :   /* Otherwise see if some other process holds it.
   26183                 :   */
   26184                 : #ifndef __DJGPP__
   26185               0 :   if( !reserved && !pFile->pInode->bProcessLock ){
   26186                 :     struct flock lock;
   26187               0 :     lock.l_whence = SEEK_SET;
   26188               0 :     lock.l_start = RESERVED_BYTE;
   26189               0 :     lock.l_len = 1;
   26190               0 :     lock.l_type = F_WRLCK;
   26191               0 :     if( osFcntl(pFile->h, F_GETLK, &lock) ){
   26192               0 :       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
   26193               0 :       pFile->lastErrno = errno;
   26194               0 :     } else if( lock.l_type!=F_UNLCK ){
   26195               0 :       reserved = 1;
   26196                 :     }
   26197                 :   }
   26198                 : #endif
   26199                 :   
   26200               0 :   unixLeaveMutex();
   26201                 :   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
   26202                 : 
   26203               0 :   *pResOut = reserved;
   26204               0 :   return rc;
   26205                 : }
   26206                 : 
   26207                 : /*
   26208                 : ** Attempt to set a system-lock on the file pFile.  The lock is 
   26209                 : ** described by pLock.
   26210                 : **
   26211                 : ** If the pFile was opened read/write from unix-excl, then the only lock
   26212                 : ** ever obtained is an exclusive lock, and it is obtained exactly once
   26213                 : ** the first time any lock is attempted.  All subsequent system locking
   26214                 : ** operations become no-ops.  Locking operations still happen internally,
   26215                 : ** in order to coordinate access between separate database connections
   26216                 : ** within this process, but all of that is handled in memory and the
   26217                 : ** operating system does not participate.
   26218                 : **
   26219                 : ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
   26220                 : ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
   26221                 : ** and is read-only.
   26222                 : **
   26223                 : ** Zero is returned if the call completes successfully, or -1 if a call
   26224                 : ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
   26225                 : */
   26226          102882 : static int unixFileLock(unixFile *pFile, struct flock *pLock){
   26227                 :   int rc;
   26228          102882 :   unixInodeInfo *pInode = pFile->pInode;
   26229          102882 :   assert( unixMutexHeld() );
   26230          102882 :   assert( pInode!=0 );
   26231          102882 :   if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
   26232               0 :    && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
   26233                 :   ){
   26234               0 :     if( pInode->bProcessLock==0 ){
   26235                 :       struct flock lock;
   26236               0 :       assert( pInode->nLock==0 );
   26237               0 :       lock.l_whence = SEEK_SET;
   26238               0 :       lock.l_start = SHARED_FIRST;
   26239               0 :       lock.l_len = SHARED_SIZE;
   26240               0 :       lock.l_type = F_WRLCK;
   26241               0 :       rc = osFcntl(pFile->h, F_SETLK, &lock);
   26242               0 :       if( rc<0 ) return rc;
   26243               0 :       pInode->bProcessLock = 1;
   26244               0 :       pInode->nLock++;
   26245                 :     }else{
   26246               0 :       rc = 0;
   26247                 :     }
   26248                 :   }else{
   26249          102882 :     rc = osFcntl(pFile->h, F_SETLK, pLock);
   26250                 :   }
   26251          102882 :   return rc;
   26252                 : }
   26253                 : 
   26254                 : /*
   26255                 : ** Lock the file with the lock specified by parameter eFileLock - one
   26256                 : ** of the following:
   26257                 : **
   26258                 : **     (1) SHARED_LOCK
   26259                 : **     (2) RESERVED_LOCK
   26260                 : **     (3) PENDING_LOCK
   26261                 : **     (4) EXCLUSIVE_LOCK
   26262                 : **
   26263                 : ** Sometimes when requesting one lock state, additional lock states
   26264                 : ** are inserted in between.  The locking might fail on one of the later
   26265                 : ** transitions leaving the lock state different from what it started but
   26266                 : ** still short of its goal.  The following chart shows the allowed
   26267                 : ** transitions and the inserted intermediate states:
   26268                 : **
   26269                 : **    UNLOCKED -> SHARED
   26270                 : **    SHARED -> RESERVED
   26271                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   26272                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   26273                 : **    PENDING -> EXCLUSIVE
   26274                 : **
   26275                 : ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   26276                 : ** routine to lower a locking level.
   26277                 : */
   26278           31534 : static int unixLock(sqlite3_file *id, int eFileLock){
   26279                 :   /* The following describes the implementation of the various locks and
   26280                 :   ** lock transitions in terms of the POSIX advisory shared and exclusive
   26281                 :   ** lock primitives (called read-locks and write-locks below, to avoid
   26282                 :   ** confusion with SQLite lock names). The algorithms are complicated
   26283                 :   ** slightly in order to be compatible with windows systems simultaneously
   26284                 :   ** accessing the same database file, in case that is ever required.
   26285                 :   **
   26286                 :   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
   26287                 :   ** byte', each single bytes at well known offsets, and the 'shared byte
   26288                 :   ** range', a range of 510 bytes at a well known offset.
   26289                 :   **
   26290                 :   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
   26291                 :   ** byte'.  If this is successful, a random byte from the 'shared byte
   26292                 :   ** range' is read-locked and the lock on the 'pending byte' released.
   26293                 :   **
   26294                 :   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
   26295                 :   ** A RESERVED lock is implemented by grabbing a write-lock on the
   26296                 :   ** 'reserved byte'. 
   26297                 :   **
   26298                 :   ** A process may only obtain a PENDING lock after it has obtained a
   26299                 :   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
   26300                 :   ** on the 'pending byte'. This ensures that no new SHARED locks can be
   26301                 :   ** obtained, but existing SHARED locks are allowed to persist. A process
   26302                 :   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
   26303                 :   ** This property is used by the algorithm for rolling back a journal file
   26304                 :   ** after a crash.
   26305                 :   **
   26306                 :   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
   26307                 :   ** implemented by obtaining a write-lock on the entire 'shared byte
   26308                 :   ** range'. Since all other locks require a read-lock on one of the bytes
   26309                 :   ** within this range, this ensures that no other locks are held on the
   26310                 :   ** database. 
   26311                 :   **
   26312                 :   ** The reason a single byte cannot be used instead of the 'shared byte
   26313                 :   ** range' is that some versions of windows do not support read-locks. By
   26314                 :   ** locking a random byte from a range, concurrent SHARED locks may exist
   26315                 :   ** even if the locking primitive used is always a write-lock.
   26316                 :   */
   26317           31534 :   int rc = SQLITE_OK;
   26318           31534 :   unixFile *pFile = (unixFile*)id;
   26319                 :   unixInodeInfo *pInode;
   26320                 :   struct flock lock;
   26321           31534 :   int tErrno = 0;
   26322                 : 
   26323           31534 :   assert( pFile );
   26324                 :   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
   26325                 :       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
   26326                 :       azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
   26327                 : 
   26328                 :   /* If there is already a lock of this type or more restrictive on the
   26329                 :   ** unixFile, do nothing. Don't use the end_lock: exit path, as
   26330                 :   ** unixEnterMutex() hasn't been called yet.
   26331                 :   */
   26332           31534 :   if( pFile->eFileLock>=eFileLock ){
   26333                 :     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
   26334                 :             azFileLock(eFileLock)));
   26335               0 :     return SQLITE_OK;
   26336                 :   }
   26337                 : 
   26338                 :   /* Make sure the locking sequence is correct.
   26339                 :   **  (1) We never move from unlocked to anything higher than shared lock.
   26340                 :   **  (2) SQLite never explicitly requests a pendig lock.
   26341                 :   **  (3) A shared lock is always held when a reserve lock is requested.
   26342                 :   */
   26343           31534 :   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
   26344           31534 :   assert( eFileLock!=PENDING_LOCK );
   26345           31534 :   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
   26346                 : 
   26347                 :   /* This mutex is needed because pFile->pInode is shared across threads
   26348                 :   */
   26349           31534 :   unixEnterMutex();
   26350           31534 :   pInode = pFile->pInode;
   26351                 : 
   26352                 :   /* If some thread using this PID has a lock via a different unixFile*
   26353                 :   ** handle that precludes the requested lock, return BUSY.
   26354                 :   */
   26355           31608 :   if( (pFile->eFileLock!=pInode->eFileLock && 
   26356             147 :           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
   26357                 :   ){
   26358               1 :     rc = SQLITE_BUSY;
   26359               1 :     goto end_lock;
   26360                 :   }
   26361                 : 
   26362                 :   /* If a SHARED lock is requested, and some thread using this PID already
   26363                 :   ** has a SHARED or RESERVED lock, then increment reference counts and
   26364                 :   ** return SQLITE_OK.
   26365                 :   */
   26366           47684 :   if( eFileLock==SHARED_LOCK && 
   26367           32229 :       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
   26368              73 :     assert( eFileLock==SHARED_LOCK );
   26369              73 :     assert( pFile->eFileLock==0 );
   26370              73 :     assert( pInode->nShared>0 );
   26371              73 :     pFile->eFileLock = SHARED_LOCK;
   26372              73 :     pInode->nShared++;
   26373              73 :     pInode->nLock++;
   26374              73 :     goto end_lock;
   26375                 :   }
   26376                 : 
   26377                 : 
   26378                 :   /* A PENDING lock is needed before acquiring a SHARED lock and before
   26379                 :   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
   26380                 :   ** be released.
   26381                 :   */
   26382           31460 :   lock.l_len = 1L;
   26383           31460 :   lock.l_whence = SEEK_SET;
   26384           31460 :   if( eFileLock==SHARED_LOCK 
   26385           15382 :       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
   26386                 :   ){
   26387           23977 :     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
   26388           23977 :     lock.l_start = PENDING_BYTE;
   26389           23977 :     if( unixFileLock(pFile, &lock) ){
   26390              30 :       tErrno = errno;
   26391              30 :       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   26392              30 :       if( rc!=SQLITE_BUSY ){
   26393              30 :         pFile->lastErrno = tErrno;
   26394                 :       }
   26395              30 :       goto end_lock;
   26396                 :     }
   26397                 :   }
   26398                 : 
   26399                 : 
   26400                 :   /* If control gets to this point, then actually go ahead and make
   26401                 :   ** operating system calls for the specified lock.
   26402                 :   */
   26403           31430 :   if( eFileLock==SHARED_LOCK ){
   26404           16078 :     assert( pInode->nShared==0 );
   26405           16078 :     assert( pInode->eFileLock==0 );
   26406           16078 :     assert( rc==SQLITE_OK );
   26407                 : 
   26408                 :     /* Now get the read-lock */
   26409           16078 :     lock.l_start = SHARED_FIRST;
   26410           16078 :     lock.l_len = SHARED_SIZE;
   26411           16078 :     if( unixFileLock(pFile, &lock) ){
   26412               0 :       tErrno = errno;
   26413               0 :       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   26414                 :     }
   26415                 : 
   26416                 :     /* Drop the temporary PENDING lock */
   26417           16078 :     lock.l_start = PENDING_BYTE;
   26418           16078 :     lock.l_len = 1L;
   26419           16078 :     lock.l_type = F_UNLCK;
   26420           16078 :     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
   26421                 :       /* This could happen with a network mount */
   26422               0 :       tErrno = errno;
   26423               0 :       rc = SQLITE_IOERR_UNLOCK; 
   26424                 :     }
   26425                 : 
   26426           16078 :     if( rc ){
   26427               0 :       if( rc!=SQLITE_BUSY ){
   26428               0 :         pFile->lastErrno = tErrno;
   26429                 :       }
   26430               0 :       goto end_lock;
   26431                 :     }else{
   26432           16078 :       pFile->eFileLock = SHARED_LOCK;
   26433           16078 :       pInode->nLock++;
   26434           16078 :       pInode->nShared = 1;
   26435                 :     }
   26436           15352 :   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
   26437                 :     /* We are trying for an exclusive lock but another thread in this
   26438                 :     ** same process is still holding a shared lock. */
   26439              43 :     rc = SQLITE_BUSY;
   26440                 :   }else{
   26441                 :     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
   26442                 :     ** assumed that there is a SHARED or greater lock on the file
   26443                 :     ** already.
   26444                 :     */
   26445           15309 :     assert( 0!=pFile->eFileLock );
   26446           15309 :     lock.l_type = F_WRLCK;
   26447                 : 
   26448           15309 :     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
   26449           15309 :     if( eFileLock==RESERVED_LOCK ){
   26450            7483 :       lock.l_start = RESERVED_BYTE;
   26451            7483 :       lock.l_len = 1L;
   26452                 :     }else{
   26453            7826 :       lock.l_start = SHARED_FIRST;
   26454            7826 :       lock.l_len = SHARED_SIZE;
   26455                 :     }
   26456                 : 
   26457           15309 :     if( unixFileLock(pFile, &lock) ){
   26458               0 :       tErrno = errno;
   26459               0 :       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   26460               0 :       if( rc!=SQLITE_BUSY ){
   26461               0 :         pFile->lastErrno = tErrno;
   26462                 :       }
   26463                 :     }
   26464                 :   }
   26465                 :   
   26466                 : 
   26467                 : #ifndef NDEBUG
   26468                 :   /* Set up the transaction-counter change checking flags when
   26469                 :   ** transitioning from a SHARED to a RESERVED lock.  The change
   26470                 :   ** from SHARED to RESERVED marks the beginning of a normal
   26471                 :   ** write operation (not a hot journal rollback).
   26472                 :   */
   26473           31430 :   if( rc==SQLITE_OK
   26474           31387 :    && pFile->eFileLock<=SHARED_LOCK
   26475           23914 :    && eFileLock==RESERVED_LOCK
   26476                 :   ){
   26477            7483 :     pFile->transCntrChng = 0;
   26478            7483 :     pFile->dbUpdate = 0;
   26479            7483 :     pFile->inNormalWrite = 1;
   26480                 :   }
   26481                 : #endif
   26482                 : 
   26483                 : 
   26484           31430 :   if( rc==SQLITE_OK ){
   26485           31387 :     pFile->eFileLock = eFileLock;
   26486           31387 :     pInode->eFileLock = eFileLock;
   26487              43 :   }else if( eFileLock==EXCLUSIVE_LOCK ){
   26488              43 :     pFile->eFileLock = PENDING_LOCK;
   26489              43 :     pInode->eFileLock = PENDING_LOCK;
   26490                 :   }
   26491                 : 
   26492                 : end_lock:
   26493           31534 :   unixLeaveMutex();
   26494                 :   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), 
   26495                 :       rc==SQLITE_OK ? "ok" : "failed"));
   26496           31534 :   return rc;
   26497                 : }
   26498                 : 
   26499                 : /*
   26500                 : ** Add the file descriptor used by file handle pFile to the corresponding
   26501                 : ** pUnused list.
   26502                 : */
   26503              74 : static void setPendingFd(unixFile *pFile){
   26504              74 :   unixInodeInfo *pInode = pFile->pInode;
   26505              74 :   UnixUnusedFd *p = pFile->pUnused;
   26506              74 :   p->pNext = pInode->pUnused;
   26507              74 :   pInode->pUnused = p;
   26508              74 :   pFile->h = -1;
   26509              74 :   pFile->pUnused = 0;
   26510              74 : }
   26511                 : 
   26512                 : /*
   26513                 : ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   26514                 : ** must be either NO_LOCK or SHARED_LOCK.
   26515                 : **
   26516                 : ** If the locking level of the file descriptor is already at or below
   26517                 : ** the requested locking level, this routine is a no-op.
   26518                 : ** 
   26519                 : ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
   26520                 : ** the byte range is divided into 2 parts and the first part is unlocked then
   26521                 : ** set to a read lock, then the other part is simply unlocked.  This works 
   26522                 : ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to 
   26523                 : ** remove the write lock on a region when a read lock is set.
   26524                 : */
   26525           28253 : static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
   26526           28253 :   unixFile *pFile = (unixFile*)id;
   26527                 :   unixInodeInfo *pInode;
   26528                 :   struct flock lock;
   26529           28253 :   int rc = SQLITE_OK;
   26530                 : 
   26531           28253 :   assert( pFile );
   26532                 :   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
   26533                 :       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
   26534                 :       getpid()));
   26535                 : 
   26536           28253 :   assert( eFileLock<=SHARED_LOCK );
   26537           28253 :   if( pFile->eFileLock<=eFileLock ){
   26538            4619 :     return SQLITE_OK;
   26539                 :   }
   26540           23634 :   unixEnterMutex();
   26541           23634 :   pInode = pFile->pInode;
   26542           23634 :   assert( pInode->nShared!=0 );
   26543           23634 :   if( pFile->eFileLock>SHARED_LOCK ){
   26544            7879 :     assert( pInode->eFileLock==pFile->eFileLock );
   26545                 : 
   26546                 : #ifndef NDEBUG
   26547                 :     /* When reducing a lock such that other processes can start
   26548                 :     ** reading the database file again, make sure that the
   26549                 :     ** transaction counter was updated if any part of the database
   26550                 :     ** file changed.  If the transaction counter is not updated,
   26551                 :     ** other connections to the same file might not realize that
   26552                 :     ** the file has changed and hence might not know to flush their
   26553                 :     ** cache.  The use of a stale cache can lead to database corruption.
   26554                 :     */
   26555            7879 :     pFile->inNormalWrite = 0;
   26556                 : #endif
   26557                 : 
   26558                 :     /* downgrading to a shared lock on NFS involves clearing the write lock
   26559                 :     ** before establishing the readlock - to avoid a race condition we downgrade
   26560                 :     ** the lock in 2 blocks, so that part of the range will be covered by a 
   26561                 :     ** write lock until the rest is covered by a read lock:
   26562                 :     **  1:   [WWWWW]
   26563                 :     **  2:   [....W]
   26564                 :     **  3:   [RRRRW]
   26565                 :     **  4:   [RRRR.]
   26566                 :     */
   26567            7879 :     if( eFileLock==SHARED_LOCK ){
   26568                 : 
   26569                 : #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
   26570                 :       (void)handleNFSUnlock;
   26571            7483 :       assert( handleNFSUnlock==0 );
   26572                 : #endif
   26573                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   26574                 :       if( handleNFSUnlock ){
   26575                 :         int tErrno;               /* Error code from system call errors */
   26576                 :         off_t divSize = SHARED_SIZE - 1;
   26577                 :         
   26578                 :         lock.l_type = F_UNLCK;
   26579                 :         lock.l_whence = SEEK_SET;
   26580                 :         lock.l_start = SHARED_FIRST;
   26581                 :         lock.l_len = divSize;
   26582                 :         if( unixFileLock(pFile, &lock)==(-1) ){
   26583                 :           tErrno = errno;
   26584                 :           rc = SQLITE_IOERR_UNLOCK;
   26585                 :           if( IS_LOCK_ERROR(rc) ){
   26586                 :             pFile->lastErrno = tErrno;
   26587                 :           }
   26588                 :           goto end_unlock;
   26589                 :         }
   26590                 :         lock.l_type = F_RDLCK;
   26591                 :         lock.l_whence = SEEK_SET;
   26592                 :         lock.l_start = SHARED_FIRST;
   26593                 :         lock.l_len = divSize;
   26594                 :         if( unixFileLock(pFile, &lock)==(-1) ){
   26595                 :           tErrno = errno;
   26596                 :           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
   26597                 :           if( IS_LOCK_ERROR(rc) ){
   26598                 :             pFile->lastErrno = tErrno;
   26599                 :           }
   26600                 :           goto end_unlock;
   26601                 :         }
   26602                 :         lock.l_type = F_UNLCK;
   26603                 :         lock.l_whence = SEEK_SET;
   26604                 :         lock.l_start = SHARED_FIRST+divSize;
   26605                 :         lock.l_len = SHARED_SIZE-divSize;
   26606                 :         if( unixFileLock(pFile, &lock)==(-1) ){
   26607                 :           tErrno = errno;
   26608                 :           rc = SQLITE_IOERR_UNLOCK;
   26609                 :           if( IS_LOCK_ERROR(rc) ){
   26610                 :             pFile->lastErrno = tErrno;
   26611                 :           }
   26612                 :           goto end_unlock;
   26613                 :         }
   26614                 :       }else
   26615                 : #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   26616                 :       {
   26617            7483 :         lock.l_type = F_RDLCK;
   26618            7483 :         lock.l_whence = SEEK_SET;
   26619            7483 :         lock.l_start = SHARED_FIRST;
   26620            7483 :         lock.l_len = SHARED_SIZE;
   26621            7483 :         if( unixFileLock(pFile, &lock) ){
   26622                 :           /* In theory, the call to unixFileLock() cannot fail because another
   26623                 :           ** process is holding an incompatible lock. If it does, this 
   26624                 :           ** indicates that the other process is not following the locking
   26625                 :           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
   26626                 :           ** SQLITE_BUSY would confuse the upper layer (in practice it causes 
   26627                 :           ** an assert to fail). */ 
   26628               0 :           rc = SQLITE_IOERR_RDLOCK;
   26629               0 :           pFile->lastErrno = errno;
   26630               0 :           goto end_unlock;
   26631                 :         }
   26632                 :       }
   26633                 :     }
   26634            7879 :     lock.l_type = F_UNLCK;
   26635            7879 :     lock.l_whence = SEEK_SET;
   26636            7879 :     lock.l_start = PENDING_BYTE;
   26637            7879 :     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
   26638            7879 :     if( unixFileLock(pFile, &lock)==0 ){
   26639            7879 :       pInode->eFileLock = SHARED_LOCK;
   26640                 :     }else{
   26641               0 :       rc = SQLITE_IOERR_UNLOCK;
   26642               0 :       pFile->lastErrno = errno;
   26643               0 :       goto end_unlock;
   26644                 :     }
   26645                 :   }
   26646           23634 :   if( eFileLock==NO_LOCK ){
   26647                 :     /* Decrement the shared lock counter.  Release the lock using an
   26648                 :     ** OS call only when all threads in this same process have released
   26649                 :     ** the lock.
   26650                 :     */
   26651           16151 :     pInode->nShared--;
   26652           16151 :     if( pInode->nShared==0 ){
   26653           16078 :       lock.l_type = F_UNLCK;
   26654           16078 :       lock.l_whence = SEEK_SET;
   26655           16078 :       lock.l_start = lock.l_len = 0L;
   26656           16078 :       if( unixFileLock(pFile, &lock)==0 ){
   26657           16078 :         pInode->eFileLock = NO_LOCK;
   26658                 :       }else{
   26659               0 :         rc = SQLITE_IOERR_UNLOCK;
   26660               0 :         pFile->lastErrno = errno;
   26661               0 :         pInode->eFileLock = NO_LOCK;
   26662               0 :         pFile->eFileLock = NO_LOCK;
   26663                 :       }
   26664                 :     }
   26665                 : 
   26666                 :     /* Decrement the count of locks against this same file.  When the
   26667                 :     ** count reaches zero, close any other file descriptors whose close
   26668                 :     ** was deferred because of outstanding locks.
   26669                 :     */
   26670           16151 :     pInode->nLock--;
   26671           16151 :     assert( pInode->nLock>=0 );
   26672           16151 :     if( pInode->nLock==0 ){
   26673           16078 :       closePendingFds(pFile);
   26674                 :     }
   26675                 :   }
   26676                 :         
   26677                 : end_unlock:
   26678           23634 :   unixLeaveMutex();
   26679           23634 :   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
   26680           23634 :   return rc;
   26681                 : }
   26682                 : 
   26683                 : /*
   26684                 : ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   26685                 : ** must be either NO_LOCK or SHARED_LOCK.
   26686                 : **
   26687                 : ** If the locking level of the file descriptor is already at or below
   26688                 : ** the requested locking level, this routine is a no-op.
   26689                 : */
   26690           28253 : static int unixUnlock(sqlite3_file *id, int eFileLock){
   26691           28253 :   return posixUnlock(id, eFileLock, 0);
   26692                 : }
   26693                 : 
   26694                 : /*
   26695                 : ** This function performs the parts of the "close file" operation 
   26696                 : ** common to all locking schemes. It closes the directory and file
   26697                 : ** handles, if they are valid, and sets all fields of the unixFile
   26698                 : ** structure to 0.
   26699                 : **
   26700                 : ** It is *not* necessary to hold the mutex when this routine is called,
   26701                 : ** even on VxWorks.  A mutex will be acquired on VxWorks by the
   26702                 : ** vxworksReleaseFileId() routine.
   26703                 : */
   26704           11040 : static int closeUnixFile(sqlite3_file *id){
   26705           11040 :   unixFile *pFile = (unixFile*)id;
   26706           11040 :   if( pFile->h>=0 ){
   26707           10966 :     robust_close(pFile, pFile->h, __LINE__);
   26708           10966 :     pFile->h = -1;
   26709                 :   }
   26710                 : #if OS_VXWORKS
   26711                 :   if( pFile->pId ){
   26712                 :     if( pFile->ctrlFlags & UNIXFILE_DELETE ){
   26713                 :       osUnlink(pFile->pId->zCanonicalName);
   26714                 :     }
   26715                 :     vxworksReleaseFileId(pFile->pId);
   26716                 :     pFile->pId = 0;
   26717                 :   }
   26718                 : #endif
   26719                 :   OSTRACE(("CLOSE   %-3d\n", pFile->h));
   26720                 :   OpenCounter(-1);
   26721           11040 :   sqlite3_free(pFile->pUnused);
   26722           11040 :   memset(pFile, 0, sizeof(unixFile));
   26723           11040 :   return SQLITE_OK;
   26724                 : }
   26725                 : 
   26726                 : /*
   26727                 : ** Close a file.
   26728                 : */
   26729            3052 : static int unixClose(sqlite3_file *id){
   26730            3052 :   int rc = SQLITE_OK;
   26731            3052 :   unixFile *pFile = (unixFile *)id;
   26732            3052 :   unixUnlock(id, NO_LOCK);
   26733            3052 :   unixEnterMutex();
   26734                 : 
   26735                 :   /* unixFile.pInode is always valid here. Otherwise, a different close
   26736                 :   ** routine (e.g. nolockClose()) would be called instead.
   26737                 :   */
   26738            3052 :   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
   26739            3052 :   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
   26740                 :     /* If there are outstanding locks, do not actually close the file just
   26741                 :     ** yet because that would clear those locks.  Instead, add the file
   26742                 :     ** descriptor to pInode->pUnused list.  It will be automatically closed 
   26743                 :     ** when the last lock is cleared.
   26744                 :     */
   26745              74 :     setPendingFd(pFile);
   26746                 :   }
   26747            3052 :   releaseInodeInfo(pFile);
   26748            3052 :   rc = closeUnixFile(id);
   26749            3052 :   unixLeaveMutex();
   26750            3052 :   return rc;
   26751                 : }
   26752                 : 
   26753                 : /************** End of the posix advisory lock implementation *****************
   26754                 : ******************************************************************************/
   26755                 : 
   26756                 : /******************************************************************************
   26757                 : ****************************** No-op Locking **********************************
   26758                 : **
   26759                 : ** Of the various locking implementations available, this is by far the
   26760                 : ** simplest:  locking is ignored.  No attempt is made to lock the database
   26761                 : ** file for reading or writing.
   26762                 : **
   26763                 : ** This locking mode is appropriate for use on read-only databases
   26764                 : ** (ex: databases that are burned into CD-ROM, for example.)  It can
   26765                 : ** also be used if the application employs some external mechanism to
   26766                 : ** prevent simultaneous access of the same database by two or more
   26767                 : ** database connections.  But there is a serious risk of database
   26768                 : ** corruption if this locking mode is used in situations where multiple
   26769                 : ** database connections are accessing the same database file at the same
   26770                 : ** time and one or more of those connections are writing.
   26771                 : */
   26772                 : 
   26773               0 : static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
   26774                 :   UNUSED_PARAMETER(NotUsed);
   26775               0 :   *pResOut = 0;
   26776               0 :   return SQLITE_OK;
   26777                 : }
   26778               0 : static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
   26779                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   26780               0 :   return SQLITE_OK;
   26781                 : }
   26782              42 : static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
   26783                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   26784              42 :   return SQLITE_OK;
   26785                 : }
   26786                 : 
   26787                 : /*
   26788                 : ** Close the file.
   26789                 : */
   26790            7988 : static int nolockClose(sqlite3_file *id) {
   26791            7988 :   return closeUnixFile(id);
   26792                 : }
   26793                 : 
   26794                 : /******************* End of the no-op lock implementation *********************
   26795                 : ******************************************************************************/
   26796                 : 
   26797                 : /******************************************************************************
   26798                 : ************************* Begin dot-file Locking ******************************
   26799                 : **
   26800                 : ** The dotfile locking implementation uses the existance of separate lock
   26801                 : ** files (really a directory) to control access to the database.  This works
   26802                 : ** on just about every filesystem imaginable.  But there are serious downsides:
   26803                 : **
   26804                 : **    (1)  There is zero concurrency.  A single reader blocks all other
   26805                 : **         connections from reading or writing the database.
   26806                 : **
   26807                 : **    (2)  An application crash or power loss can leave stale lock files
   26808                 : **         sitting around that need to be cleared manually.
   26809                 : **
   26810                 : ** Nevertheless, a dotlock is an appropriate locking mode for use if no
   26811                 : ** other locking strategy is available.
   26812                 : **
   26813                 : ** Dotfile locking works by creating a subdirectory in the same directory as
   26814                 : ** the database and with the same name but with a ".lock" extension added.
   26815                 : ** The existance of a lock directory implies an EXCLUSIVE lock.  All other
   26816                 : ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
   26817                 : */
   26818                 : 
   26819                 : /*
   26820                 : ** The file suffix added to the data base filename in order to create the
   26821                 : ** lock directory.
   26822                 : */
   26823                 : #define DOTLOCK_SUFFIX ".lock"
   26824                 : 
   26825                 : /*
   26826                 : ** This routine checks if there is a RESERVED lock held on the specified
   26827                 : ** file by this or any other process. If such a lock is held, set *pResOut
   26828                 : ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   26829                 : ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   26830                 : **
   26831                 : ** In dotfile locking, either a lock exists or it does not.  So in this
   26832                 : ** variation of CheckReservedLock(), *pResOut is set to true if any lock
   26833                 : ** is held on the file and false if the file is unlocked.
   26834                 : */
   26835               0 : static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
   26836               0 :   int rc = SQLITE_OK;
   26837               0 :   int reserved = 0;
   26838               0 :   unixFile *pFile = (unixFile*)id;
   26839                 : 
   26840                 :   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   26841                 :   
   26842               0 :   assert( pFile );
   26843                 : 
   26844                 :   /* Check if a thread in this process holds such a lock */
   26845               0 :   if( pFile->eFileLock>SHARED_LOCK ){
   26846                 :     /* Either this connection or some other connection in the same process
   26847                 :     ** holds a lock on the file.  No need to check further. */
   26848               0 :     reserved = 1;
   26849                 :   }else{
   26850                 :     /* The lock is held if and only if the lockfile exists */
   26851               0 :     const char *zLockFile = (const char*)pFile->lockingContext;
   26852               0 :     reserved = osAccess(zLockFile, 0)==0;
   26853                 :   }
   26854                 :   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
   26855               0 :   *pResOut = reserved;
   26856               0 :   return rc;
   26857                 : }
   26858                 : 
   26859                 : /*
   26860                 : ** Lock the file with the lock specified by parameter eFileLock - one
   26861                 : ** of the following:
   26862                 : **
   26863                 : **     (1) SHARED_LOCK
   26864                 : **     (2) RESERVED_LOCK
   26865                 : **     (3) PENDING_LOCK
   26866                 : **     (4) EXCLUSIVE_LOCK
   26867                 : **
   26868                 : ** Sometimes when requesting one lock state, additional lock states
   26869                 : ** are inserted in between.  The locking might fail on one of the later
   26870                 : ** transitions leaving the lock state different from what it started but
   26871                 : ** still short of its goal.  The following chart shows the allowed
   26872                 : ** transitions and the inserted intermediate states:
   26873                 : **
   26874                 : **    UNLOCKED -> SHARED
   26875                 : **    SHARED -> RESERVED
   26876                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   26877                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   26878                 : **    PENDING -> EXCLUSIVE
   26879                 : **
   26880                 : ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   26881                 : ** routine to lower a locking level.
   26882                 : **
   26883                 : ** With dotfile locking, we really only support state (4): EXCLUSIVE.
   26884                 : ** But we track the other locking levels internally.
   26885                 : */
   26886               0 : static int dotlockLock(sqlite3_file *id, int eFileLock) {
   26887               0 :   unixFile *pFile = (unixFile*)id;
   26888               0 :   char *zLockFile = (char *)pFile->lockingContext;
   26889               0 :   int rc = SQLITE_OK;
   26890                 : 
   26891                 : 
   26892                 :   /* If we have any lock, then the lock file already exists.  All we have
   26893                 :   ** to do is adjust our internal record of the lock level.
   26894                 :   */
   26895               0 :   if( pFile->eFileLock > NO_LOCK ){
   26896               0 :     pFile->eFileLock = eFileLock;
   26897                 :     /* Always update the timestamp on the old file */
   26898                 : #ifdef HAVE_UTIME
   26899                 :     utime(zLockFile, NULL);
   26900                 : #else
   26901               0 :     utimes(zLockFile, NULL);
   26902                 : #endif
   26903               0 :     return SQLITE_OK;
   26904                 :   }
   26905                 :   
   26906                 :   /* grab an exclusive lock */
   26907               0 :   rc = osMkdir(zLockFile, 0777);
   26908               0 :   if( rc<0 ){
   26909                 :     /* failed to open/create the lock directory */
   26910               0 :     int tErrno = errno;
   26911               0 :     if( EEXIST == tErrno ){
   26912               0 :       rc = SQLITE_BUSY;
   26913                 :     } else {
   26914               0 :       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   26915               0 :       if( IS_LOCK_ERROR(rc) ){
   26916               0 :         pFile->lastErrno = tErrno;
   26917                 :       }
   26918                 :     }
   26919               0 :     return rc;
   26920                 :   } 
   26921                 :   
   26922                 :   /* got it, set the type and return ok */
   26923               0 :   pFile->eFileLock = eFileLock;
   26924               0 :   return rc;
   26925                 : }
   26926                 : 
   26927                 : /*
   26928                 : ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   26929                 : ** must be either NO_LOCK or SHARED_LOCK.
   26930                 : **
   26931                 : ** If the locking level of the file descriptor is already at or below
   26932                 : ** the requested locking level, this routine is a no-op.
   26933                 : **
   26934                 : ** When the locking level reaches NO_LOCK, delete the lock file.
   26935                 : */
   26936               0 : static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
   26937               0 :   unixFile *pFile = (unixFile*)id;
   26938               0 :   char *zLockFile = (char *)pFile->lockingContext;
   26939                 :   int rc;
   26940                 : 
   26941               0 :   assert( pFile );
   26942                 :   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
   26943                 :            pFile->eFileLock, getpid()));
   26944               0 :   assert( eFileLock<=SHARED_LOCK );
   26945                 :   
   26946                 :   /* no-op if possible */
   26947               0 :   if( pFile->eFileLock==eFileLock ){
   26948               0 :     return SQLITE_OK;
   26949                 :   }
   26950                 : 
   26951                 :   /* To downgrade to shared, simply update our internal notion of the
   26952                 :   ** lock state.  No need to mess with the file on disk.
   26953                 :   */
   26954               0 :   if( eFileLock==SHARED_LOCK ){
   26955               0 :     pFile->eFileLock = SHARED_LOCK;
   26956               0 :     return SQLITE_OK;
   26957                 :   }
   26958                 :   
   26959                 :   /* To fully unlock the database, delete the lock file */
   26960               0 :   assert( eFileLock==NO_LOCK );
   26961               0 :   rc = osRmdir(zLockFile);
   26962               0 :   if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
   26963               0 :   if( rc<0 ){
   26964               0 :     int tErrno = errno;
   26965               0 :     rc = 0;
   26966               0 :     if( ENOENT != tErrno ){
   26967               0 :       rc = SQLITE_IOERR_UNLOCK;
   26968                 :     }
   26969               0 :     if( IS_LOCK_ERROR(rc) ){
   26970               0 :       pFile->lastErrno = tErrno;
   26971                 :     }
   26972               0 :     return rc; 
   26973                 :   }
   26974               0 :   pFile->eFileLock = NO_LOCK;
   26975               0 :   return SQLITE_OK;
   26976                 : }
   26977                 : 
   26978                 : /*
   26979                 : ** Close a file.  Make sure the lock has been released before closing.
   26980                 : */
   26981               0 : static int dotlockClose(sqlite3_file *id) {
   26982                 :   int rc;
   26983               0 :   if( id ){
   26984               0 :     unixFile *pFile = (unixFile*)id;
   26985               0 :     dotlockUnlock(id, NO_LOCK);
   26986               0 :     sqlite3_free(pFile->lockingContext);
   26987                 :   }
   26988               0 :   rc = closeUnixFile(id);
   26989               0 :   return rc;
   26990                 : }
   26991                 : /****************** End of the dot-file lock implementation *******************
   26992                 : ******************************************************************************/
   26993                 : 
   26994                 : /******************************************************************************
   26995                 : ************************** Begin flock Locking ********************************
   26996                 : **
   26997                 : ** Use the flock() system call to do file locking.
   26998                 : **
   26999                 : ** flock() locking is like dot-file locking in that the various
   27000                 : ** fine-grain locking levels supported by SQLite are collapsed into
   27001                 : ** a single exclusive lock.  In other words, SHARED, RESERVED, and
   27002                 : ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
   27003                 : ** still works when you do this, but concurrency is reduced since
   27004                 : ** only a single process can be reading the database at a time.
   27005                 : **
   27006                 : ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
   27007                 : ** compiling for VXWORKS.
   27008                 : */
   27009                 : #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
   27010                 : 
   27011                 : /*
   27012                 : ** Retry flock() calls that fail with EINTR
   27013                 : */
   27014                 : #ifdef EINTR
   27015                 : static int robust_flock(int fd, int op){
   27016                 :   int rc;
   27017                 :   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
   27018                 :   return rc;
   27019                 : }
   27020                 : #else
   27021                 : # define robust_flock(a,b) flock(a,b)
   27022                 : #endif
   27023                 :      
   27024                 : 
   27025                 : /*
   27026                 : ** This routine checks if there is a RESERVED lock held on the specified
   27027                 : ** file by this or any other process. If such a lock is held, set *pResOut
   27028                 : ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   27029                 : ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   27030                 : */
   27031                 : static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
   27032                 :   int rc = SQLITE_OK;
   27033                 :   int reserved = 0;
   27034                 :   unixFile *pFile = (unixFile*)id;
   27035                 :   
   27036                 :   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   27037                 :   
   27038                 :   assert( pFile );
   27039                 :   
   27040                 :   /* Check if a thread in this process holds such a lock */
   27041                 :   if( pFile->eFileLock>SHARED_LOCK ){
   27042                 :     reserved = 1;
   27043                 :   }
   27044                 :   
   27045                 :   /* Otherwise see if some other process holds it. */
   27046                 :   if( !reserved ){
   27047                 :     /* attempt to get the lock */
   27048                 :     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
   27049                 :     if( !lrc ){
   27050                 :       /* got the lock, unlock it */
   27051                 :       lrc = robust_flock(pFile->h, LOCK_UN);
   27052                 :       if ( lrc ) {
   27053                 :         int tErrno = errno;
   27054                 :         /* unlock failed with an error */
   27055                 :         lrc = SQLITE_IOERR_UNLOCK; 
   27056                 :         if( IS_LOCK_ERROR(lrc) ){
   27057                 :           pFile->lastErrno = tErrno;
   27058                 :           rc = lrc;
   27059                 :         }
   27060                 :       }
   27061                 :     } else {
   27062                 :       int tErrno = errno;
   27063                 :       reserved = 1;
   27064                 :       /* someone else might have it reserved */
   27065                 :       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); 
   27066                 :       if( IS_LOCK_ERROR(lrc) ){
   27067                 :         pFile->lastErrno = tErrno;
   27068                 :         rc = lrc;
   27069                 :       }
   27070                 :     }
   27071                 :   }
   27072                 :   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
   27073                 : 
   27074                 : #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   27075                 :   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
   27076                 :     rc = SQLITE_OK;
   27077                 :     reserved=1;
   27078                 :   }
   27079                 : #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
   27080                 :   *pResOut = reserved;
   27081                 :   return rc;
   27082                 : }
   27083                 : 
   27084                 : /*
   27085                 : ** Lock the file with the lock specified by parameter eFileLock - one
   27086                 : ** of the following:
   27087                 : **
   27088                 : **     (1) SHARED_LOCK
   27089                 : **     (2) RESERVED_LOCK
   27090                 : **     (3) PENDING_LOCK
   27091                 : **     (4) EXCLUSIVE_LOCK
   27092                 : **
   27093                 : ** Sometimes when requesting one lock state, additional lock states
   27094                 : ** are inserted in between.  The locking might fail on one of the later
   27095                 : ** transitions leaving the lock state different from what it started but
   27096                 : ** still short of its goal.  The following chart shows the allowed
   27097                 : ** transitions and the inserted intermediate states:
   27098                 : **
   27099                 : **    UNLOCKED -> SHARED
   27100                 : **    SHARED -> RESERVED
   27101                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   27102                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   27103                 : **    PENDING -> EXCLUSIVE
   27104                 : **
   27105                 : ** flock() only really support EXCLUSIVE locks.  We track intermediate
   27106                 : ** lock states in the sqlite3_file structure, but all locks SHARED or
   27107                 : ** above are really EXCLUSIVE locks and exclude all other processes from
   27108                 : ** access the file.
   27109                 : **
   27110                 : ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   27111                 : ** routine to lower a locking level.
   27112                 : */
   27113                 : static int flockLock(sqlite3_file *id, int eFileLock) {
   27114                 :   int rc = SQLITE_OK;
   27115                 :   unixFile *pFile = (unixFile*)id;
   27116                 : 
   27117                 :   assert( pFile );
   27118                 : 
   27119                 :   /* if we already have a lock, it is exclusive.  
   27120                 :   ** Just adjust level and punt on outta here. */
   27121                 :   if (pFile->eFileLock > NO_LOCK) {
   27122                 :     pFile->eFileLock = eFileLock;
   27123                 :     return SQLITE_OK;
   27124                 :   }
   27125                 :   
   27126                 :   /* grab an exclusive lock */
   27127                 :   
   27128                 :   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
   27129                 :     int tErrno = errno;
   27130                 :     /* didn't get, must be busy */
   27131                 :     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
   27132                 :     if( IS_LOCK_ERROR(rc) ){
   27133                 :       pFile->lastErrno = tErrno;
   27134                 :     }
   27135                 :   } else {
   27136                 :     /* got it, set the type and return ok */
   27137                 :     pFile->eFileLock = eFileLock;
   27138                 :   }
   27139                 :   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
   27140                 :            rc==SQLITE_OK ? "ok" : "failed"));
   27141                 : #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   27142                 :   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
   27143                 :     rc = SQLITE_BUSY;
   27144                 :   }
   27145                 : #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
   27146                 :   return rc;
   27147                 : }
   27148                 : 
   27149                 : 
   27150                 : /*
   27151                 : ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   27152                 : ** must be either NO_LOCK or SHARED_LOCK.
   27153                 : **
   27154                 : ** If the locking level of the file descriptor is already at or below
   27155                 : ** the requested locking level, this routine is a no-op.
   27156                 : */
   27157                 : static int flockUnlock(sqlite3_file *id, int eFileLock) {
   27158                 :   unixFile *pFile = (unixFile*)id;
   27159                 :   
   27160                 :   assert( pFile );
   27161                 :   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
   27162                 :            pFile->eFileLock, getpid()));
   27163                 :   assert( eFileLock<=SHARED_LOCK );
   27164                 :   
   27165                 :   /* no-op if possible */
   27166                 :   if( pFile->eFileLock==eFileLock ){
   27167                 :     return SQLITE_OK;
   27168                 :   }
   27169                 :   
   27170                 :   /* shared can just be set because we always have an exclusive */
   27171                 :   if (eFileLock==SHARED_LOCK) {
   27172                 :     pFile->eFileLock = eFileLock;
   27173                 :     return SQLITE_OK;
   27174                 :   }
   27175                 :   
   27176                 :   /* no, really, unlock. */
   27177                 :   if( robust_flock(pFile->h, LOCK_UN) ){
   27178                 : #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
   27179                 :     return SQLITE_OK;
   27180                 : #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
   27181                 :     return SQLITE_IOERR_UNLOCK;
   27182                 :   }else{
   27183                 :     pFile->eFileLock = NO_LOCK;
   27184                 :     return SQLITE_OK;
   27185                 :   }
   27186                 : }
   27187                 : 
   27188                 : /*
   27189                 : ** Close a file.
   27190                 : */
   27191                 : static int flockClose(sqlite3_file *id) {
   27192                 :   if( id ){
   27193                 :     flockUnlock(id, NO_LOCK);
   27194                 :   }
   27195                 :   return closeUnixFile(id);
   27196                 : }
   27197                 : 
   27198                 : #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
   27199                 : 
   27200                 : /******************* End of the flock lock implementation *********************
   27201                 : ******************************************************************************/
   27202                 : 
   27203                 : /******************************************************************************
   27204                 : ************************ Begin Named Semaphore Locking ************************
   27205                 : **
   27206                 : ** Named semaphore locking is only supported on VxWorks.
   27207                 : **
   27208                 : ** Semaphore locking is like dot-lock and flock in that it really only
   27209                 : ** supports EXCLUSIVE locking.  Only a single process can read or write
   27210                 : ** the database file at a time.  This reduces potential concurrency, but
   27211                 : ** makes the lock implementation much easier.
   27212                 : */
   27213                 : #if OS_VXWORKS
   27214                 : 
   27215                 : /*
   27216                 : ** This routine checks if there is a RESERVED lock held on the specified
   27217                 : ** file by this or any other process. If such a lock is held, set *pResOut
   27218                 : ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   27219                 : ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   27220                 : */
   27221                 : static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
   27222                 :   int rc = SQLITE_OK;
   27223                 :   int reserved = 0;
   27224                 :   unixFile *pFile = (unixFile*)id;
   27225                 : 
   27226                 :   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   27227                 :   
   27228                 :   assert( pFile );
   27229                 : 
   27230                 :   /* Check if a thread in this process holds such a lock */
   27231                 :   if( pFile->eFileLock>SHARED_LOCK ){
   27232                 :     reserved = 1;
   27233                 :   }
   27234                 :   
   27235                 :   /* Otherwise see if some other process holds it. */
   27236                 :   if( !reserved ){
   27237                 :     sem_t *pSem = pFile->pInode->pSem;
   27238                 :     struct stat statBuf;
   27239                 : 
   27240                 :     if( sem_trywait(pSem)==-1 ){
   27241                 :       int tErrno = errno;
   27242                 :       if( EAGAIN != tErrno ){
   27243                 :         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
   27244                 :         pFile->lastErrno = tErrno;
   27245                 :       } else {
   27246                 :         /* someone else has the lock when we are in NO_LOCK */
   27247                 :         reserved = (pFile->eFileLock < SHARED_LOCK);
   27248                 :       }
   27249                 :     }else{
   27250                 :       /* we could have it if we want it */
   27251                 :       sem_post(pSem);
   27252                 :     }
   27253                 :   }
   27254                 :   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
   27255                 : 
   27256                 :   *pResOut = reserved;
   27257                 :   return rc;
   27258                 : }
   27259                 : 
   27260                 : /*
   27261                 : ** Lock the file with the lock specified by parameter eFileLock - one
   27262                 : ** of the following:
   27263                 : **
   27264                 : **     (1) SHARED_LOCK
   27265                 : **     (2) RESERVED_LOCK
   27266                 : **     (3) PENDING_LOCK
   27267                 : **     (4) EXCLUSIVE_LOCK
   27268                 : **
   27269                 : ** Sometimes when requesting one lock state, additional lock states
   27270                 : ** are inserted in between.  The locking might fail on one of the later
   27271                 : ** transitions leaving the lock state different from what it started but
   27272                 : ** still short of its goal.  The following chart shows the allowed
   27273                 : ** transitions and the inserted intermediate states:
   27274                 : **
   27275                 : **    UNLOCKED -> SHARED
   27276                 : **    SHARED -> RESERVED
   27277                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   27278                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   27279                 : **    PENDING -> EXCLUSIVE
   27280                 : **
   27281                 : ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
   27282                 : ** lock states in the sqlite3_file structure, but all locks SHARED or
   27283                 : ** above are really EXCLUSIVE locks and exclude all other processes from
   27284                 : ** access the file.
   27285                 : **
   27286                 : ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   27287                 : ** routine to lower a locking level.
   27288                 : */
   27289                 : static int semLock(sqlite3_file *id, int eFileLock) {
   27290                 :   unixFile *pFile = (unixFile*)id;
   27291                 :   int fd;
   27292                 :   sem_t *pSem = pFile->pInode->pSem;
   27293                 :   int rc = SQLITE_OK;
   27294                 : 
   27295                 :   /* if we already have a lock, it is exclusive.  
   27296                 :   ** Just adjust level and punt on outta here. */
   27297                 :   if (pFile->eFileLock > NO_LOCK) {
   27298                 :     pFile->eFileLock = eFileLock;
   27299                 :     rc = SQLITE_OK;
   27300                 :     goto sem_end_lock;
   27301                 :   }
   27302                 :   
   27303                 :   /* lock semaphore now but bail out when already locked. */
   27304                 :   if( sem_trywait(pSem)==-1 ){
   27305                 :     rc = SQLITE_BUSY;
   27306                 :     goto sem_end_lock;
   27307                 :   }
   27308                 : 
   27309                 :   /* got it, set the type and return ok */
   27310                 :   pFile->eFileLock = eFileLock;
   27311                 : 
   27312                 :  sem_end_lock:
   27313                 :   return rc;
   27314                 : }
   27315                 : 
   27316                 : /*
   27317                 : ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   27318                 : ** must be either NO_LOCK or SHARED_LOCK.
   27319                 : **
   27320                 : ** If the locking level of the file descriptor is already at or below
   27321                 : ** the requested locking level, this routine is a no-op.
   27322                 : */
   27323                 : static int semUnlock(sqlite3_file *id, int eFileLock) {
   27324                 :   unixFile *pFile = (unixFile*)id;
   27325                 :   sem_t *pSem = pFile->pInode->pSem;
   27326                 : 
   27327                 :   assert( pFile );
   27328                 :   assert( pSem );
   27329                 :   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
   27330                 :            pFile->eFileLock, getpid()));
   27331                 :   assert( eFileLock<=SHARED_LOCK );
   27332                 :   
   27333                 :   /* no-op if possible */
   27334                 :   if( pFile->eFileLock==eFileLock ){
   27335                 :     return SQLITE_OK;
   27336                 :   }
   27337                 :   
   27338                 :   /* shared can just be set because we always have an exclusive */
   27339                 :   if (eFileLock==SHARED_LOCK) {
   27340                 :     pFile->eFileLock = eFileLock;
   27341                 :     return SQLITE_OK;
   27342                 :   }
   27343                 :   
   27344                 :   /* no, really unlock. */
   27345                 :   if ( sem_post(pSem)==-1 ) {
   27346                 :     int rc, tErrno = errno;
   27347                 :     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
   27348                 :     if( IS_LOCK_ERROR(rc) ){
   27349                 :       pFile->lastErrno = tErrno;
   27350                 :     }
   27351                 :     return rc; 
   27352                 :   }
   27353                 :   pFile->eFileLock = NO_LOCK;
   27354                 :   return SQLITE_OK;
   27355                 : }
   27356                 : 
   27357                 : /*
   27358                 :  ** Close a file.
   27359                 :  */
   27360                 : static int semClose(sqlite3_file *id) {
   27361                 :   if( id ){
   27362                 :     unixFile *pFile = (unixFile*)id;
   27363                 :     semUnlock(id, NO_LOCK);
   27364                 :     assert( pFile );
   27365                 :     unixEnterMutex();
   27366                 :     releaseInodeInfo(pFile);
   27367                 :     unixLeaveMutex();
   27368                 :     closeUnixFile(id);
   27369                 :   }
   27370                 :   return SQLITE_OK;
   27371                 : }
   27372                 : 
   27373                 : #endif /* OS_VXWORKS */
   27374                 : /*
   27375                 : ** Named semaphore locking is only available on VxWorks.
   27376                 : **
   27377                 : *************** End of the named semaphore lock implementation ****************
   27378                 : ******************************************************************************/
   27379                 : 
   27380                 : 
   27381                 : /******************************************************************************
   27382                 : *************************** Begin AFP Locking *********************************
   27383                 : **
   27384                 : ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
   27385                 : ** on Apple Macintosh computers - both OS9 and OSX.
   27386                 : **
   27387                 : ** Third-party implementations of AFP are available.  But this code here
   27388                 : ** only works on OSX.
   27389                 : */
   27390                 : 
   27391                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   27392                 : /*
   27393                 : ** The afpLockingContext structure contains all afp lock specific state
   27394                 : */
   27395                 : typedef struct afpLockingContext afpLockingContext;
   27396                 : struct afpLockingContext {
   27397                 :   int reserved;
   27398                 :   const char *dbPath;             /* Name of the open file */
   27399                 : };
   27400                 : 
   27401                 : struct ByteRangeLockPB2
   27402                 : {
   27403                 :   unsigned long long offset;        /* offset to first byte to lock */
   27404                 :   unsigned long long length;        /* nbr of bytes to lock */
   27405                 :   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
   27406                 :   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
   27407                 :   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
   27408                 :   int fd;                           /* file desc to assoc this lock with */
   27409                 : };
   27410                 : 
   27411                 : #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
   27412                 : 
   27413                 : /*
   27414                 : ** This is a utility for setting or clearing a bit-range lock on an
   27415                 : ** AFP filesystem.
   27416                 : ** 
   27417                 : ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
   27418                 : */
   27419                 : static int afpSetLock(
   27420                 :   const char *path,              /* Name of the file to be locked or unlocked */
   27421                 :   unixFile *pFile,               /* Open file descriptor on path */
   27422                 :   unsigned long long offset,     /* First byte to be locked */
   27423                 :   unsigned long long length,     /* Number of bytes to lock */
   27424                 :   int setLockFlag                /* True to set lock.  False to clear lock */
   27425                 : ){
   27426                 :   struct ByteRangeLockPB2 pb;
   27427                 :   int err;
   27428                 :   
   27429                 :   pb.unLockFlag = setLockFlag ? 0 : 1;
   27430                 :   pb.startEndFlag = 0;
   27431                 :   pb.offset = offset;
   27432                 :   pb.length = length; 
   27433                 :   pb.fd = pFile->h;
   27434                 :   
   27435                 :   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", 
   27436                 :     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
   27437                 :     offset, length));
   27438                 :   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
   27439                 :   if ( err==-1 ) {
   27440                 :     int rc;
   27441                 :     int tErrno = errno;
   27442                 :     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
   27443                 :              path, tErrno, strerror(tErrno)));
   27444                 : #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
   27445                 :     rc = SQLITE_BUSY;
   27446                 : #else
   27447                 :     rc = sqliteErrorFromPosixError(tErrno,
   27448                 :                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
   27449                 : #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
   27450                 :     if( IS_LOCK_ERROR(rc) ){
   27451                 :       pFile->lastErrno = tErrno;
   27452                 :     }
   27453                 :     return rc;
   27454                 :   } else {
   27455                 :     return SQLITE_OK;
   27456                 :   }
   27457                 : }
   27458                 : 
   27459                 : /*
   27460                 : ** This routine checks if there is a RESERVED lock held on the specified
   27461                 : ** file by this or any other process. If such a lock is held, set *pResOut
   27462                 : ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   27463                 : ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   27464                 : */
   27465                 : static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
   27466                 :   int rc = SQLITE_OK;
   27467                 :   int reserved = 0;
   27468                 :   unixFile *pFile = (unixFile*)id;
   27469                 :   afpLockingContext *context;
   27470                 :   
   27471                 :   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   27472                 :   
   27473                 :   assert( pFile );
   27474                 :   context = (afpLockingContext *) pFile->lockingContext;
   27475                 :   if( context->reserved ){
   27476                 :     *pResOut = 1;
   27477                 :     return SQLITE_OK;
   27478                 :   }
   27479                 :   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
   27480                 :   
   27481                 :   /* Check if a thread in this process holds such a lock */
   27482                 :   if( pFile->pInode->eFileLock>SHARED_LOCK ){
   27483                 :     reserved = 1;
   27484                 :   }
   27485                 :   
   27486                 :   /* Otherwise see if some other process holds it.
   27487                 :    */
   27488                 :   if( !reserved ){
   27489                 :     /* lock the RESERVED byte */
   27490                 :     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);  
   27491                 :     if( SQLITE_OK==lrc ){
   27492                 :       /* if we succeeded in taking the reserved lock, unlock it to restore
   27493                 :       ** the original state */
   27494                 :       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
   27495                 :     } else {
   27496                 :       /* if we failed to get the lock then someone else must have it */
   27497                 :       reserved = 1;
   27498                 :     }
   27499                 :     if( IS_LOCK_ERROR(lrc) ){
   27500                 :       rc=lrc;
   27501                 :     }
   27502                 :   }
   27503                 :   
   27504                 :   unixLeaveMutex();
   27505                 :   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
   27506                 :   
   27507                 :   *pResOut = reserved;
   27508                 :   return rc;
   27509                 : }
   27510                 : 
   27511                 : /*
   27512                 : ** Lock the file with the lock specified by parameter eFileLock - one
   27513                 : ** of the following:
   27514                 : **
   27515                 : **     (1) SHARED_LOCK
   27516                 : **     (2) RESERVED_LOCK
   27517                 : **     (3) PENDING_LOCK
   27518                 : **     (4) EXCLUSIVE_LOCK
   27519                 : **
   27520                 : ** Sometimes when requesting one lock state, additional lock states
   27521                 : ** are inserted in between.  The locking might fail on one of the later
   27522                 : ** transitions leaving the lock state different from what it started but
   27523                 : ** still short of its goal.  The following chart shows the allowed
   27524                 : ** transitions and the inserted intermediate states:
   27525                 : **
   27526                 : **    UNLOCKED -> SHARED
   27527                 : **    SHARED -> RESERVED
   27528                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   27529                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   27530                 : **    PENDING -> EXCLUSIVE
   27531                 : **
   27532                 : ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   27533                 : ** routine to lower a locking level.
   27534                 : */
   27535                 : static int afpLock(sqlite3_file *id, int eFileLock){
   27536                 :   int rc = SQLITE_OK;
   27537                 :   unixFile *pFile = (unixFile*)id;
   27538                 :   unixInodeInfo *pInode = pFile->pInode;
   27539                 :   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
   27540                 :   
   27541                 :   assert( pFile );
   27542                 :   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
   27543                 :            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
   27544                 :            azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
   27545                 : 
   27546                 :   /* If there is already a lock of this type or more restrictive on the
   27547                 :   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
   27548                 :   ** unixEnterMutex() hasn't been called yet.
   27549                 :   */
   27550                 :   if( pFile->eFileLock>=eFileLock ){
   27551                 :     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
   27552                 :            azFileLock(eFileLock)));
   27553                 :     return SQLITE_OK;
   27554                 :   }
   27555                 : 
   27556                 :   /* Make sure the locking sequence is correct
   27557                 :   **  (1) We never move from unlocked to anything higher than shared lock.
   27558                 :   **  (2) SQLite never explicitly requests a pendig lock.
   27559                 :   **  (3) A shared lock is always held when a reserve lock is requested.
   27560                 :   */
   27561                 :   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
   27562                 :   assert( eFileLock!=PENDING_LOCK );
   27563                 :   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
   27564                 :   
   27565                 :   /* This mutex is needed because pFile->pInode is shared across threads
   27566                 :   */
   27567                 :   unixEnterMutex();
   27568                 :   pInode = pFile->pInode;
   27569                 : 
   27570                 :   /* If some thread using this PID has a lock via a different unixFile*
   27571                 :   ** handle that precludes the requested lock, return BUSY.
   27572                 :   */
   27573                 :   if( (pFile->eFileLock!=pInode->eFileLock && 
   27574                 :        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
   27575                 :      ){
   27576                 :     rc = SQLITE_BUSY;
   27577                 :     goto afp_end_lock;
   27578                 :   }
   27579                 :   
   27580                 :   /* If a SHARED lock is requested, and some thread using this PID already
   27581                 :   ** has a SHARED or RESERVED lock, then increment reference counts and
   27582                 :   ** return SQLITE_OK.
   27583                 :   */
   27584                 :   if( eFileLock==SHARED_LOCK && 
   27585                 :      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
   27586                 :     assert( eFileLock==SHARED_LOCK );
   27587                 :     assert( pFile->eFileLock==0 );
   27588                 :     assert( pInode->nShared>0 );
   27589                 :     pFile->eFileLock = SHARED_LOCK;
   27590                 :     pInode->nShared++;
   27591                 :     pInode->nLock++;
   27592                 :     goto afp_end_lock;
   27593                 :   }
   27594                 :     
   27595                 :   /* A PENDING lock is needed before acquiring a SHARED lock and before
   27596                 :   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
   27597                 :   ** be released.
   27598                 :   */
   27599                 :   if( eFileLock==SHARED_LOCK 
   27600                 :       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
   27601                 :   ){
   27602                 :     int failed;
   27603                 :     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
   27604                 :     if (failed) {
   27605                 :       rc = failed;
   27606                 :       goto afp_end_lock;
   27607                 :     }
   27608                 :   }
   27609                 :   
   27610                 :   /* If control gets to this point, then actually go ahead and make
   27611                 :   ** operating system calls for the specified lock.
   27612                 :   */
   27613                 :   if( eFileLock==SHARED_LOCK ){
   27614                 :     int lrc1, lrc2, lrc1Errno = 0;
   27615                 :     long lk, mask;
   27616                 :     
   27617                 :     assert( pInode->nShared==0 );
   27618                 :     assert( pInode->eFileLock==0 );
   27619                 :         
   27620                 :     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
   27621                 :     /* Now get the read-lock SHARED_LOCK */
   27622                 :     /* note that the quality of the randomness doesn't matter that much */
   27623                 :     lk = random(); 
   27624                 :     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
   27625                 :     lrc1 = afpSetLock(context->dbPath, pFile, 
   27626                 :           SHARED_FIRST+pInode->sharedByte, 1, 1);
   27627                 :     if( IS_LOCK_ERROR(lrc1) ){
   27628                 :       lrc1Errno = pFile->lastErrno;
   27629                 :     }
   27630                 :     /* Drop the temporary PENDING lock */
   27631                 :     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
   27632                 :     
   27633                 :     if( IS_LOCK_ERROR(lrc1) ) {
   27634                 :       pFile->lastErrno = lrc1Errno;
   27635                 :       rc = lrc1;
   27636                 :       goto afp_end_lock;
   27637                 :     } else if( IS_LOCK_ERROR(lrc2) ){
   27638                 :       rc = lrc2;
   27639                 :       goto afp_end_lock;
   27640                 :     } else if( lrc1 != SQLITE_OK ) {
   27641                 :       rc = lrc1;
   27642                 :     } else {
   27643                 :       pFile->eFileLock = SHARED_LOCK;
   27644                 :       pInode->nLock++;
   27645                 :       pInode->nShared = 1;
   27646                 :     }
   27647                 :   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
   27648                 :     /* We are trying for an exclusive lock but another thread in this
   27649                 :      ** same process is still holding a shared lock. */
   27650                 :     rc = SQLITE_BUSY;
   27651                 :   }else{
   27652                 :     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
   27653                 :     ** assumed that there is a SHARED or greater lock on the file
   27654                 :     ** already.
   27655                 :     */
   27656                 :     int failed = 0;
   27657                 :     assert( 0!=pFile->eFileLock );
   27658                 :     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
   27659                 :         /* Acquire a RESERVED lock */
   27660                 :         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
   27661                 :       if( !failed ){
   27662                 :         context->reserved = 1;
   27663                 :       }
   27664                 :     }
   27665                 :     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
   27666                 :       /* Acquire an EXCLUSIVE lock */
   27667                 :         
   27668                 :       /* Remove the shared lock before trying the range.  we'll need to 
   27669                 :       ** reestablish the shared lock if we can't get the  afpUnlock
   27670                 :       */
   27671                 :       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
   27672                 :                          pInode->sharedByte, 1, 0)) ){
   27673                 :         int failed2 = SQLITE_OK;
   27674                 :         /* now attemmpt to get the exclusive lock range */
   27675                 :         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, 
   27676                 :                                SHARED_SIZE, 1);
   27677                 :         if( failed && (failed2 = afpSetLock(context->dbPath, pFile, 
   27678                 :                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
   27679                 :           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
   27680                 :           ** a critical I/O error
   27681                 :           */
   27682                 :           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : 
   27683                 :                SQLITE_IOERR_LOCK;
   27684                 :           goto afp_end_lock;
   27685                 :         } 
   27686                 :       }else{
   27687                 :         rc = failed; 
   27688                 :       }
   27689                 :     }
   27690                 :     if( failed ){
   27691                 :       rc = failed;
   27692                 :     }
   27693                 :   }
   27694                 :   
   27695                 :   if( rc==SQLITE_OK ){
   27696                 :     pFile->eFileLock = eFileLock;
   27697                 :     pInode->eFileLock = eFileLock;
   27698                 :   }else if( eFileLock==EXCLUSIVE_LOCK ){
   27699                 :     pFile->eFileLock = PENDING_LOCK;
   27700                 :     pInode->eFileLock = PENDING_LOCK;
   27701                 :   }
   27702                 :   
   27703                 : afp_end_lock:
   27704                 :   unixLeaveMutex();
   27705                 :   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), 
   27706                 :          rc==SQLITE_OK ? "ok" : "failed"));
   27707                 :   return rc;
   27708                 : }
   27709                 : 
   27710                 : /*
   27711                 : ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   27712                 : ** must be either NO_LOCK or SHARED_LOCK.
   27713                 : **
   27714                 : ** If the locking level of the file descriptor is already at or below
   27715                 : ** the requested locking level, this routine is a no-op.
   27716                 : */
   27717                 : static int afpUnlock(sqlite3_file *id, int eFileLock) {
   27718                 :   int rc = SQLITE_OK;
   27719                 :   unixFile *pFile = (unixFile*)id;
   27720                 :   unixInodeInfo *pInode;
   27721                 :   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
   27722                 :   int skipShared = 0;
   27723                 : #ifdef SQLITE_TEST
   27724                 :   int h = pFile->h;
   27725                 : #endif
   27726                 : 
   27727                 :   assert( pFile );
   27728                 :   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
   27729                 :            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
   27730                 :            getpid()));
   27731                 : 
   27732                 :   assert( eFileLock<=SHARED_LOCK );
   27733                 :   if( pFile->eFileLock<=eFileLock ){
   27734                 :     return SQLITE_OK;
   27735                 :   }
   27736                 :   unixEnterMutex();
   27737                 :   pInode = pFile->pInode;
   27738                 :   assert( pInode->nShared!=0 );
   27739                 :   if( pFile->eFileLock>SHARED_LOCK ){
   27740                 :     assert( pInode->eFileLock==pFile->eFileLock );
   27741                 :     SimulateIOErrorBenign(1);
   27742                 :     SimulateIOError( h=(-1) )
   27743                 :     SimulateIOErrorBenign(0);
   27744                 :     
   27745                 : #ifndef NDEBUG
   27746                 :     /* When reducing a lock such that other processes can start
   27747                 :     ** reading the database file again, make sure that the
   27748                 :     ** transaction counter was updated if any part of the database
   27749                 :     ** file changed.  If the transaction counter is not updated,
   27750                 :     ** other connections to the same file might not realize that
   27751                 :     ** the file has changed and hence might not know to flush their
   27752                 :     ** cache.  The use of a stale cache can lead to database corruption.
   27753                 :     */
   27754                 :     assert( pFile->inNormalWrite==0
   27755                 :            || pFile->dbUpdate==0
   27756                 :            || pFile->transCntrChng==1 );
   27757                 :     pFile->inNormalWrite = 0;
   27758                 : #endif
   27759                 :     
   27760                 :     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
   27761                 :       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
   27762                 :       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
   27763                 :         /* only re-establish the shared lock if necessary */
   27764                 :         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
   27765                 :         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
   27766                 :       } else {
   27767                 :         skipShared = 1;
   27768                 :       }
   27769                 :     }
   27770                 :     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
   27771                 :       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
   27772                 :     } 
   27773                 :     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
   27774                 :       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
   27775                 :       if( !rc ){ 
   27776                 :         context->reserved = 0; 
   27777                 :       }
   27778                 :     }
   27779                 :     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
   27780                 :       pInode->eFileLock = SHARED_LOCK;
   27781                 :     }
   27782                 :   }
   27783                 :   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
   27784                 : 
   27785                 :     /* Decrement the shared lock counter.  Release the lock using an
   27786                 :     ** OS call only when all threads in this same process have released
   27787                 :     ** the lock.
   27788                 :     */
   27789                 :     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
   27790                 :     pInode->nShared--;
   27791                 :     if( pInode->nShared==0 ){
   27792                 :       SimulateIOErrorBenign(1);
   27793                 :       SimulateIOError( h=(-1) )
   27794                 :       SimulateIOErrorBenign(0);
   27795                 :       if( !skipShared ){
   27796                 :         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
   27797                 :       }
   27798                 :       if( !rc ){
   27799                 :         pInode->eFileLock = NO_LOCK;
   27800                 :         pFile->eFileLock = NO_LOCK;
   27801                 :       }
   27802                 :     }
   27803                 :     if( rc==SQLITE_OK ){
   27804                 :       pInode->nLock--;
   27805                 :       assert( pInode->nLock>=0 );
   27806                 :       if( pInode->nLock==0 ){
   27807                 :         closePendingFds(pFile);
   27808                 :       }
   27809                 :     }
   27810                 :   }
   27811                 :   
   27812                 :   unixLeaveMutex();
   27813                 :   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
   27814                 :   return rc;
   27815                 : }
   27816                 : 
   27817                 : /*
   27818                 : ** Close a file & cleanup AFP specific locking context 
   27819                 : */
   27820                 : static int afpClose(sqlite3_file *id) {
   27821                 :   int rc = SQLITE_OK;
   27822                 :   if( id ){
   27823                 :     unixFile *pFile = (unixFile*)id;
   27824                 :     afpUnlock(id, NO_LOCK);
   27825                 :     unixEnterMutex();
   27826                 :     if( pFile->pInode && pFile->pInode->nLock ){
   27827                 :       /* If there are outstanding locks, do not actually close the file just
   27828                 :       ** yet because that would clear those locks.  Instead, add the file
   27829                 :       ** descriptor to pInode->aPending.  It will be automatically closed when
   27830                 :       ** the last lock is cleared.
   27831                 :       */
   27832                 :       setPendingFd(pFile);
   27833                 :     }
   27834                 :     releaseInodeInfo(pFile);
   27835                 :     sqlite3_free(pFile->lockingContext);
   27836                 :     rc = closeUnixFile(id);
   27837                 :     unixLeaveMutex();
   27838                 :   }
   27839                 :   return rc;
   27840                 : }
   27841                 : 
   27842                 : #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   27843                 : /*
   27844                 : ** The code above is the AFP lock implementation.  The code is specific
   27845                 : ** to MacOSX and does not work on other unix platforms.  No alternative
   27846                 : ** is available.  If you don't compile for a mac, then the "unix-afp"
   27847                 : ** VFS is not available.
   27848                 : **
   27849                 : ********************* End of the AFP lock implementation **********************
   27850                 : ******************************************************************************/
   27851                 : 
   27852                 : /******************************************************************************
   27853                 : *************************** Begin NFS Locking ********************************/
   27854                 : 
   27855                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   27856                 : /*
   27857                 :  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   27858                 :  ** must be either NO_LOCK or SHARED_LOCK.
   27859                 :  **
   27860                 :  ** If the locking level of the file descriptor is already at or below
   27861                 :  ** the requested locking level, this routine is a no-op.
   27862                 :  */
   27863                 : static int nfsUnlock(sqlite3_file *id, int eFileLock){
   27864                 :   return posixUnlock(id, eFileLock, 1);
   27865                 : }
   27866                 : 
   27867                 : #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   27868                 : /*
   27869                 : ** The code above is the NFS lock implementation.  The code is specific
   27870                 : ** to MacOSX and does not work on other unix platforms.  No alternative
   27871                 : ** is available.  
   27872                 : **
   27873                 : ********************* End of the NFS lock implementation **********************
   27874                 : ******************************************************************************/
   27875                 : 
   27876                 : /******************************************************************************
   27877                 : **************** Non-locking sqlite3_file methods *****************************
   27878                 : **
   27879                 : ** The next division contains implementations for all methods of the 
   27880                 : ** sqlite3_file object other than the locking methods.  The locking
   27881                 : ** methods were defined in divisions above (one locking method per
   27882                 : ** division).  Those methods that are common to all locking modes
   27883                 : ** are gather together into this division.
   27884                 : */
   27885                 : 
   27886                 : /*
   27887                 : ** Seek to the offset passed as the second argument, then read cnt 
   27888                 : ** bytes into pBuf. Return the number of bytes actually read.
   27889                 : **
   27890                 : ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
   27891                 : ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
   27892                 : ** one system to another.  Since SQLite does not define USE_PREAD
   27893                 : ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
   27894                 : ** See tickets #2741 and #2681.
   27895                 : **
   27896                 : ** To avoid stomping the errno value on a failed read the lastErrno value
   27897                 : ** is set before returning.
   27898                 : */
   27899           85694 : static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
   27900                 :   int got;
   27901           85694 :   int prior = 0;
   27902                 : #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
   27903                 :   i64 newOffset;
   27904                 : #endif
   27905                 :   TIMER_START;
   27906                 :   do{
   27907                 : #if defined(USE_PREAD)
   27908                 :     got = osPread(id->h, pBuf, cnt, offset);
   27909                 :     SimulateIOError( got = -1 );
   27910                 : #elif defined(USE_PREAD64)
   27911                 :     got = osPread64(id->h, pBuf, cnt, offset);
   27912                 :     SimulateIOError( got = -1 );
   27913                 : #else
   27914           85702 :     newOffset = lseek(id->h, offset, SEEK_SET);
   27915                 :     SimulateIOError( newOffset-- );
   27916           85702 :     if( newOffset!=offset ){
   27917               0 :       if( newOffset == -1 ){
   27918               0 :         ((unixFile*)id)->lastErrno = errno;
   27919                 :       }else{
   27920               0 :         ((unixFile*)id)->lastErrno = 0;                      
   27921                 :       }
   27922               0 :       return -1;
   27923                 :     }
   27924           85702 :     got = osRead(id->h, pBuf, cnt);
   27925                 : #endif
   27926           85702 :     if( got==cnt ) break;
   27927            7739 :     if( got<0 ){
   27928               0 :       if( errno==EINTR ){ got = 1; continue; }
   27929               0 :       prior = 0;
   27930               0 :       ((unixFile*)id)->lastErrno = errno;
   27931               0 :       break;
   27932            7739 :     }else if( got>0 ){
   27933               8 :       cnt -= got;
   27934               8 :       offset += got;
   27935               8 :       prior += got;
   27936               8 :       pBuf = (void*)(got + (char*)pBuf);
   27937                 :     }
   27938            7739 :   }while( got>0 );
   27939                 :   TIMER_END;
   27940                 :   OSTRACE(("READ    %-3d %5d %7lld %llu\n",
   27941                 :             id->h, got+prior, offset-prior, TIMER_ELAPSED));
   27942           85694 :   return got+prior;
   27943                 : }
   27944                 : 
   27945                 : /*
   27946                 : ** Read data from a file into a buffer.  Return SQLITE_OK if all
   27947                 : ** bytes were read successfully and SQLITE_IOERR if anything goes
   27948                 : ** wrong.
   27949                 : */
   27950           78549 : static int unixRead(
   27951                 :   sqlite3_file *id, 
   27952                 :   void *pBuf, 
   27953                 :   int amt,
   27954                 :   sqlite3_int64 offset
   27955                 : ){
   27956           78549 :   unixFile *pFile = (unixFile *)id;
   27957                 :   int got;
   27958           78549 :   assert( id );
   27959                 : 
   27960                 :   /* If this is a database file (not a journal, master-journal or temp
   27961                 :   ** file), the bytes in the locking range should never be read or written. */
   27962                 : #if 0
   27963                 :   assert( pFile->pUnused==0
   27964                 :        || offset>=PENDING_BYTE+512
   27965                 :        || offset+amt<=PENDING_BYTE 
   27966                 :   );
   27967                 : #endif
   27968                 : 
   27969           78549 :   got = seekAndRead(pFile, offset, pBuf, amt);
   27970           78549 :   if( got==amt ){
   27971           71909 :     return SQLITE_OK;
   27972            6640 :   }else if( got<0 ){
   27973                 :     /* lastErrno set by seekAndRead */
   27974               0 :     return SQLITE_IOERR_READ;
   27975                 :   }else{
   27976            6640 :     pFile->lastErrno = 0; /* not a system error */
   27977                 :     /* Unread parts of the buffer must be zero-filled */
   27978            6640 :     memset(&((char*)pBuf)[got], 0, amt-got);
   27979            6640 :     return SQLITE_IOERR_SHORT_READ;
   27980                 :   }
   27981                 : }
   27982                 : 
   27983                 : /*
   27984                 : ** Seek to the offset in id->offset then read cnt bytes into pBuf.
   27985                 : ** Return the number of bytes actually read.  Update the offset.
   27986                 : **
   27987                 : ** To avoid stomping the errno value on a failed write the lastErrno value
   27988                 : ** is set before returning.
   27989                 : */
   27990          384007 : static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
   27991                 :   int got;
   27992                 : #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
   27993                 :   i64 newOffset;
   27994                 : #endif
   27995                 :   TIMER_START;
   27996                 : #if defined(USE_PREAD)
   27997                 :   do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
   27998                 : #elif defined(USE_PREAD64)
   27999                 :   do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
   28000                 : #else
   28001                 :   do{
   28002          384007 :     newOffset = lseek(id->h, offset, SEEK_SET);
   28003                 :     SimulateIOError( newOffset-- );
   28004          384007 :     if( newOffset!=offset ){
   28005               0 :       if( newOffset == -1 ){
   28006               0 :         ((unixFile*)id)->lastErrno = errno;
   28007                 :       }else{
   28008               0 :         ((unixFile*)id)->lastErrno = 0;                      
   28009                 :       }
   28010               0 :       return -1;
   28011                 :     }
   28012          384007 :     got = osWrite(id->h, pBuf, cnt);
   28013          384007 :   }while( got<0 && errno==EINTR );
   28014                 : #endif
   28015                 :   TIMER_END;
   28016          384007 :   if( got<0 ){
   28017               0 :     ((unixFile*)id)->lastErrno = errno;
   28018                 :   }
   28019                 : 
   28020                 :   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
   28021          384007 :   return got;
   28022                 : }
   28023                 : 
   28024                 : 
   28025                 : /*
   28026                 : ** Write data from a buffer into a file.  Return SQLITE_OK on success
   28027                 : ** or some other error code on failure.
   28028                 : */
   28029          384007 : static int unixWrite(
   28030                 :   sqlite3_file *id, 
   28031                 :   const void *pBuf, 
   28032                 :   int amt,
   28033                 :   sqlite3_int64 offset 
   28034                 : ){
   28035          384007 :   unixFile *pFile = (unixFile*)id;
   28036          384007 :   int wrote = 0;
   28037          384007 :   assert( id );
   28038          384007 :   assert( amt>0 );
   28039                 : 
   28040                 :   /* If this is a database file (not a journal, master-journal or temp
   28041                 :   ** file), the bytes in the locking range should never be read or written. */
   28042                 : #if 0
   28043                 :   assert( pFile->pUnused==0
   28044                 :        || offset>=PENDING_BYTE+512
   28045                 :        || offset+amt<=PENDING_BYTE 
   28046                 :   );
   28047                 : #endif
   28048                 : 
   28049                 : #ifndef NDEBUG
   28050                 :   /* If we are doing a normal write to a database file (as opposed to
   28051                 :   ** doing a hot-journal rollback or a write to some file other than a
   28052                 :   ** normal database file) then record the fact that the database
   28053                 :   ** has changed.  If the transaction counter is modified, record that
   28054                 :   ** fact too.
   28055                 :   */
   28056          384007 :   if( pFile->inNormalWrite ){
   28057           26800 :     pFile->dbUpdate = 1;  /* The database has been modified */
   28058           26800 :     if( offset<=24 && offset+amt>=27 ){
   28059                 :       int rc;
   28060                 :       char oldCntr[4];
   28061                 :       SimulateIOErrorBenign(1);
   28062            7145 :       rc = seekAndRead(pFile, 24, oldCntr, 4);
   28063                 :       SimulateIOErrorBenign(0);
   28064            7145 :       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
   28065            7145 :         pFile->transCntrChng = 1;  /* The transaction counter has changed */
   28066                 :       }
   28067                 :     }
   28068                 :   }
   28069                 : #endif
   28070                 : 
   28071         1152021 :   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
   28072          384007 :     amt -= wrote;
   28073          384007 :     offset += wrote;
   28074          384007 :     pBuf = &((char*)pBuf)[wrote];
   28075                 :   }
   28076                 :   SimulateIOError(( wrote=(-1), amt=1 ));
   28077                 :   SimulateDiskfullError(( wrote=0, amt=1 ));
   28078                 : 
   28079          384007 :   if( amt>0 ){
   28080               0 :     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
   28081                 :       /* lastErrno set by seekAndWrite */
   28082               0 :       return SQLITE_IOERR_WRITE;
   28083                 :     }else{
   28084               0 :       pFile->lastErrno = 0; /* not a system error */
   28085               0 :       return SQLITE_FULL;
   28086                 :     }
   28087                 :   }
   28088                 : 
   28089          384007 :   return SQLITE_OK;
   28090                 : }
   28091                 : 
   28092                 : #ifdef SQLITE_TEST
   28093                 : /*
   28094                 : ** Count the number of fullsyncs and normal syncs.  This is used to test
   28095                 : ** that syncs and fullsyncs are occurring at the right times.
   28096                 : */
   28097                 : SQLITE_API int sqlite3_sync_count = 0;
   28098                 : SQLITE_API int sqlite3_fullsync_count = 0;
   28099                 : #endif
   28100                 : 
   28101                 : /*
   28102                 : ** We do not trust systems to provide a working fdatasync().  Some do.
   28103                 : ** Others do no.  To be safe, we will stick with the (slightly slower)
   28104                 : ** fsync(). If you know that your system does support fdatasync() correctly,
   28105                 : ** then simply compile with -Dfdatasync=fdatasync
   28106                 : */
   28107                 : #if !defined(fdatasync)
   28108                 : # define fdatasync fsync
   28109                 : #endif
   28110                 : 
   28111                 : /*
   28112                 : ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
   28113                 : ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
   28114                 : ** only available on Mac OS X.  But that could change.
   28115                 : */
   28116                 : #ifdef F_FULLFSYNC
   28117                 : # define HAVE_FULLFSYNC 1
   28118                 : #else
   28119                 : # define HAVE_FULLFSYNC 0
   28120                 : #endif
   28121                 : 
   28122                 : 
   28123                 : /*
   28124                 : ** The fsync() system call does not work as advertised on many
   28125                 : ** unix systems.  The following procedure is an attempt to make
   28126                 : ** it work better.
   28127                 : **
   28128                 : ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
   28129                 : ** for testing when we want to run through the test suite quickly.
   28130                 : ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
   28131                 : ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
   28132                 : ** or power failure will likely corrupt the database file.
   28133                 : **
   28134                 : ** SQLite sets the dataOnly flag if the size of the file is unchanged.
   28135                 : ** The idea behind dataOnly is that it should only write the file content
   28136                 : ** to disk, not the inode.  We only set dataOnly if the file size is 
   28137                 : ** unchanged since the file size is part of the inode.  However, 
   28138                 : ** Ted Ts'o tells us that fdatasync() will also write the inode if the
   28139                 : ** file size has changed.  The only real difference between fdatasync()
   28140                 : ** and fsync(), Ted tells us, is that fdatasync() will not flush the
   28141                 : ** inode if the mtime or owner or other inode attributes have changed.
   28142                 : ** We only care about the file size, not the other file attributes, so
   28143                 : ** as far as SQLite is concerned, an fdatasync() is always adequate.
   28144                 : ** So, we always use fdatasync() if it is available, regardless of
   28145                 : ** the value of the dataOnly flag.
   28146                 : */
   28147           32980 : static int full_fsync(int fd, int fullSync, int dataOnly){
   28148                 :   int rc;
   28149                 : 
   28150                 :   /* The following "ifdef/elif/else/" block has the same structure as
   28151                 :   ** the one below. It is replicated here solely to avoid cluttering 
   28152                 :   ** up the real code with the UNUSED_PARAMETER() macros.
   28153                 :   */
   28154                 : #ifdef SQLITE_NO_SYNC
   28155                 :   UNUSED_PARAMETER(fd);
   28156                 :   UNUSED_PARAMETER(fullSync);
   28157                 :   UNUSED_PARAMETER(dataOnly);
   28158                 : #elif HAVE_FULLFSYNC
   28159                 :   UNUSED_PARAMETER(dataOnly);
   28160                 : #else
   28161                 :   UNUSED_PARAMETER(fullSync);
   28162                 :   UNUSED_PARAMETER(dataOnly);
   28163                 : #endif
   28164                 : 
   28165                 :   /* Record the number of times that we do a normal fsync() and 
   28166                 :   ** FULLSYNC.  This is used during testing to verify that this procedure
   28167                 :   ** gets called with the correct arguments.
   28168                 :   */
   28169                 : #ifdef SQLITE_TEST
   28170                 :   if( fullSync ) sqlite3_fullsync_count++;
   28171                 :   sqlite3_sync_count++;
   28172                 : #endif
   28173                 : 
   28174                 :   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
   28175                 :   ** no-op
   28176                 :   */
   28177                 : #ifdef SQLITE_NO_SYNC
   28178                 :   rc = SQLITE_OK;
   28179                 : #elif HAVE_FULLFSYNC
   28180                 :   if( fullSync ){
   28181                 :     rc = osFcntl(fd, F_FULLFSYNC, 0);
   28182                 :   }else{
   28183                 :     rc = 1;
   28184                 :   }
   28185                 :   /* If the FULLFSYNC failed, fall back to attempting an fsync().
   28186                 :   ** It shouldn't be possible for fullfsync to fail on the local 
   28187                 :   ** file system (on OSX), so failure indicates that FULLFSYNC
   28188                 :   ** isn't supported for this file system. So, attempt an fsync 
   28189                 :   ** and (for now) ignore the overhead of a superfluous fcntl call.  
   28190                 :   ** It'd be better to detect fullfsync support once and avoid 
   28191                 :   ** the fcntl call every time sync is called.
   28192                 :   */
   28193                 :   if( rc ) rc = fsync(fd);
   28194                 : 
   28195                 : #elif defined(__APPLE__)
   28196                 :   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
   28197                 :   ** so currently we default to the macro that redefines fdatasync to fsync
   28198                 :   */
   28199                 :   rc = fsync(fd);
   28200                 : #else 
   28201           32980 :   rc = fdatasync(fd);
   28202                 : #if OS_VXWORKS
   28203                 :   if( rc==-1 && errno==ENOTSUP ){
   28204                 :     rc = fsync(fd);
   28205                 :   }
   28206                 : #endif /* OS_VXWORKS */
   28207                 : #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
   28208                 : 
   28209                 :   if( OS_VXWORKS && rc!= -1 ){
   28210                 :     rc = 0;
   28211                 :   }
   28212           32980 :   return rc;
   28213                 : }
   28214                 : 
   28215                 : /*
   28216                 : ** Open a file descriptor to the directory containing file zFilename.
   28217                 : ** If successful, *pFd is set to the opened file descriptor and
   28218                 : ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
   28219                 : ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
   28220                 : ** value.
   28221                 : **
   28222                 : ** The directory file descriptor is used for only one thing - to
   28223                 : ** fsync() a directory to make sure file creation and deletion events
   28224                 : ** are flushed to disk.  Such fsyncs are not needed on newer
   28225                 : ** journaling filesystems, but are required on older filesystems.
   28226                 : **
   28227                 : ** This routine can be overridden using the xSetSysCall interface.
   28228                 : ** The ability to override this routine was added in support of the
   28229                 : ** chromium sandbox.  Opening a directory is a security risk (we are
   28230                 : ** told) so making it overrideable allows the chromium sandbox to
   28231                 : ** replace this routine with a harmless no-op.  To make this routine
   28232                 : ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
   28233                 : ** *pFd set to a negative number.
   28234                 : **
   28235                 : ** If SQLITE_OK is returned, the caller is responsible for closing
   28236                 : ** the file descriptor *pFd using close().
   28237                 : */
   28238            5223 : static int openDirectory(const char *zFilename, int *pFd){
   28239                 :   int ii;
   28240            5223 :   int fd = -1;
   28241                 :   char zDirname[MAX_PATHNAME+1];
   28242                 : 
   28243            5223 :   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
   28244            5223 :   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
   28245            5223 :   if( ii>0 ){
   28246            5223 :     zDirname[ii] = '\0';
   28247            5223 :     fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
   28248            5223 :     if( fd>=0 ){
   28249                 : #ifdef FD_CLOEXEC
   28250            5223 :       osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
   28251                 : #endif
   28252                 :       OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
   28253                 :     }
   28254                 :   }
   28255            5223 :   *pFd = fd;
   28256            5223 :   return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
   28257                 : }
   28258                 : 
   28259                 : /*
   28260                 : ** Make sure all writes to a particular file are committed to disk.
   28261                 : **
   28262                 : ** If dataOnly==0 then both the file itself and its metadata (file
   28263                 : ** size, access time, etc) are synced.  If dataOnly!=0 then only the
   28264                 : ** file data is synced.
   28265                 : **
   28266                 : ** Under Unix, also make sure that the directory entry for the file
   28267                 : ** has been created by fsync-ing the directory that contains the file.
   28268                 : ** If we do not do this and we encounter a power failure, the directory
   28269                 : ** entry for the journal might not exist after we reboot.  The next
   28270                 : ** SQLite to access the file will not know that the journal exists (because
   28271                 : ** the directory entry for the journal was never created) and the transaction
   28272                 : ** will not roll back - possibly leading to database corruption.
   28273                 : */
   28274           27757 : static int unixSync(sqlite3_file *id, int flags){
   28275                 :   int rc;
   28276           27757 :   unixFile *pFile = (unixFile*)id;
   28277                 : 
   28278           27757 :   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
   28279           27757 :   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
   28280                 : 
   28281                 :   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
   28282           27757 :   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
   28283                 :       || (flags&0x0F)==SQLITE_SYNC_FULL
   28284                 :   );
   28285                 : 
   28286                 :   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
   28287                 :   ** line is to test that doing so does not cause any problems.
   28288                 :   */
   28289                 :   SimulateDiskfullError( return SQLITE_FULL );
   28290                 : 
   28291           27757 :   assert( pFile );
   28292                 :   OSTRACE(("SYNC    %-3d\n", pFile->h));
   28293           27757 :   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
   28294                 :   SimulateIOError( rc=1 );
   28295           27757 :   if( rc ){
   28296               0 :     pFile->lastErrno = errno;
   28297               0 :     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
   28298                 :   }
   28299                 : 
   28300                 :   /* Also fsync the directory containing the file if the DIRSYNC flag
   28301                 :   ** is set.  This is a one-time occurrance.  Many systems (examples: AIX)
   28302                 :   ** are unable to fsync a directory, so ignore errors on the fsync.
   28303                 :   */
   28304           27757 :   if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
   28305                 :     int dirfd;
   28306                 :     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
   28307                 :             HAVE_FULLFSYNC, isFullsync));
   28308            5223 :     rc = osOpenDirectory(pFile->zPath, &dirfd);
   28309            5223 :     if( rc==SQLITE_OK && dirfd>=0 ){
   28310            5223 :       full_fsync(dirfd, 0, 0);
   28311            5223 :       robust_close(pFile, dirfd, __LINE__);
   28312               0 :     }else if( rc==SQLITE_CANTOPEN ){
   28313               0 :       rc = SQLITE_OK;
   28314                 :     }
   28315            5223 :     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
   28316                 :   }
   28317           27757 :   return rc;
   28318                 : }
   28319                 : 
   28320                 : /*
   28321                 : ** Truncate an open file to a specified size
   28322                 : */
   28323            7695 : static int unixTruncate(sqlite3_file *id, i64 nByte){
   28324            7695 :   unixFile *pFile = (unixFile *)id;
   28325                 :   int rc;
   28326            7695 :   assert( pFile );
   28327                 :   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
   28328                 : 
   28329                 :   /* If the user has configured a chunk-size for this file, truncate the
   28330                 :   ** file so that it consists of an integer number of chunks (i.e. the
   28331                 :   ** actual file size after the operation may be larger than the requested
   28332                 :   ** size).
   28333                 :   */
   28334            7695 :   if( pFile->szChunk ){
   28335            5309 :     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
   28336                 :   }
   28337                 : 
   28338            7695 :   rc = robust_ftruncate(pFile->h, (off_t)nByte);
   28339            7695 :   if( rc ){
   28340               0 :     pFile->lastErrno = errno;
   28341               0 :     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
   28342                 :   }else{
   28343                 : #ifndef NDEBUG
   28344                 :     /* If we are doing a normal write to a database file (as opposed to
   28345                 :     ** doing a hot-journal rollback or a write to some file other than a
   28346                 :     ** normal database file) and we truncate the file to zero length,
   28347                 :     ** that effectively updates the change counter.  This might happen
   28348                 :     ** when restoring a database using the backup API from a zero-length
   28349                 :     ** source.
   28350                 :     */
   28351            7695 :     if( pFile->inNormalWrite && nByte==0 ){
   28352               0 :       pFile->transCntrChng = 1;
   28353                 :     }
   28354                 : #endif
   28355                 : 
   28356            7695 :     return SQLITE_OK;
   28357                 :   }
   28358                 : }
   28359                 : 
   28360                 : /*
   28361                 : ** Determine the current size of a file in bytes
   28362                 : */
   28363           55651 : static int unixFileSize(sqlite3_file *id, i64 *pSize){
   28364                 :   int rc;
   28365                 :   struct stat buf;
   28366           55651 :   assert( id );
   28367           55651 :   rc = osFstat(((unixFile*)id)->h, &buf);
   28368                 :   SimulateIOError( rc=1 );
   28369           55651 :   if( rc!=0 ){
   28370               0 :     ((unixFile*)id)->lastErrno = errno;
   28371               0 :     return SQLITE_IOERR_FSTAT;
   28372                 :   }
   28373           55651 :   *pSize = buf.st_size;
   28374                 : 
   28375                 :   /* When opening a zero-size database, the findInodeInfo() procedure
   28376                 :   ** writes a single byte into that file in order to work around a bug
   28377                 :   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
   28378                 :   ** layers, we need to report this file size as zero even though it is
   28379                 :   ** really 1.   Ticket #3260.
   28380                 :   */
   28381           55651 :   if( *pSize==1 ) *pSize = 0;
   28382                 : 
   28383                 : 
   28384           55651 :   return SQLITE_OK;
   28385                 : }
   28386                 : 
   28387                 : #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   28388                 : /*
   28389                 : ** Handler for proxy-locking file-control verbs.  Defined below in the
   28390                 : ** proxying locking division.
   28391                 : */
   28392                 : static int proxyFileControl(sqlite3_file*,int,void*);
   28393                 : #endif
   28394                 : 
   28395                 : /* 
   28396                 : ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 
   28397                 : ** file-control operation.  Enlarge the database to nBytes in size
   28398                 : ** (rounded up to the next chunk-size).  If the database is already
   28399                 : ** nBytes or larger, this routine is a no-op.
   28400                 : */
   28401            2824 : static int fcntlSizeHint(unixFile *pFile, i64 nByte){
   28402            2824 :   if( pFile->szChunk>0 ){
   28403                 :     i64 nSize;                    /* Required file size */
   28404                 :     struct stat buf;              /* Used to hold return values of fstat() */
   28405                 :    
   28406             530 :     if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
   28407                 : 
   28408             530 :     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
   28409             530 :     if( nSize>(i64)buf.st_size ){
   28410                 : 
   28411                 : #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
   28412                 :       /* The code below is handling the return value of osFallocate() 
   28413                 :       ** correctly. posix_fallocate() is defined to "returns zero on success, 
   28414                 :       ** or an error number on  failure". See the manpage for details. */
   28415                 :       int err;
   28416                 :       do{
   28417             530 :         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
   28418             530 :       }while( err==EINTR );
   28419             530 :       if( err ) return SQLITE_IOERR_WRITE;
   28420                 : #else
   28421                 :       /* If the OS does not have posix_fallocate(), fake it. First use
   28422                 :       ** ftruncate() to set the file size, then write a single byte to
   28423                 :       ** the last byte in each block within the extended region. This
   28424                 :       ** is the same technique used by glibc to implement posix_fallocate()
   28425                 :       ** on systems that do not have a real fallocate() system call.
   28426                 :       */
   28427                 :       int nBlk = buf.st_blksize;  /* File-system block size */
   28428                 :       i64 iWrite;                 /* Next offset to write to */
   28429                 : 
   28430                 :       if( robust_ftruncate(pFile->h, nSize) ){
   28431                 :         pFile->lastErrno = errno;
   28432                 :         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
   28433                 :       }
   28434                 :       iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
   28435                 :       while( iWrite<nSize ){
   28436                 :         int nWrite = seekAndWrite(pFile, iWrite, "", 1);
   28437                 :         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
   28438                 :         iWrite += nBlk;
   28439                 :       }
   28440                 : #endif
   28441                 :     }
   28442                 :   }
   28443                 : 
   28444            2824 :   return SQLITE_OK;
   28445                 : }
   28446                 : 
   28447                 : /*
   28448                 : ** If *pArg is inititially negative then this is a query.  Set *pArg to
   28449                 : ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
   28450                 : **
   28451                 : ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
   28452                 : */
   28453             353 : static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
   28454             353 :   if( *pArg<0 ){
   28455             353 :     *pArg = (pFile->ctrlFlags & mask)!=0;
   28456               0 :   }else if( (*pArg)==0 ){
   28457               0 :     pFile->ctrlFlags &= ~mask;
   28458                 :   }else{
   28459               0 :     pFile->ctrlFlags |= mask;
   28460                 :   }
   28461             353 : }
   28462                 : 
   28463                 : /*
   28464                 : ** Information and control of an open file handle.
   28465                 : */
   28466            6080 : static int unixFileControl(sqlite3_file *id, int op, void *pArg){
   28467            6080 :   unixFile *pFile = (unixFile*)id;
   28468            6080 :   switch( op ){
   28469                 :     case SQLITE_FCNTL_LOCKSTATE: {
   28470               0 :       *(int*)pArg = pFile->eFileLock;
   28471               0 :       return SQLITE_OK;
   28472                 :     }
   28473                 :     case SQLITE_LAST_ERRNO: {
   28474               0 :       *(int*)pArg = pFile->lastErrno;
   28475               0 :       return SQLITE_OK;
   28476                 :     }
   28477                 :     case SQLITE_FCNTL_CHUNK_SIZE: {
   28478             659 :       pFile->szChunk = *(int *)pArg;
   28479             659 :       return SQLITE_OK;
   28480                 :     }
   28481                 :     case SQLITE_FCNTL_SIZE_HINT: {
   28482                 :       int rc;
   28483                 :       SimulateIOErrorBenign(1);
   28484            2824 :       rc = fcntlSizeHint(pFile, *(i64 *)pArg);
   28485                 :       SimulateIOErrorBenign(0);
   28486            2824 :       return rc;
   28487                 :     }
   28488                 :     case SQLITE_FCNTL_PERSIST_WAL: {
   28489             353 :       unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
   28490             353 :       return SQLITE_OK;
   28491                 :     }
   28492                 :     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
   28493               0 :       unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
   28494               0 :       return SQLITE_OK;
   28495                 :     }
   28496                 :     case SQLITE_FCNTL_VFSNAME: {
   28497               0 :       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
   28498               0 :       return SQLITE_OK;
   28499                 :     }
   28500                 : #ifndef NDEBUG
   28501                 :     /* The pager calls this method to signal that it has done
   28502                 :     ** a rollback and that the database is therefore unchanged and
   28503                 :     ** it hence it is OK for the transaction change counter to be
   28504                 :     ** unchanged.
   28505                 :     */
   28506                 :     case SQLITE_FCNTL_DB_UNCHANGED: {
   28507               6 :       ((unixFile*)id)->dbUpdate = 0;
   28508               6 :       return SQLITE_OK;
   28509                 :     }
   28510                 : #endif
   28511                 : #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   28512                 :     case SQLITE_SET_LOCKPROXYFILE:
   28513                 :     case SQLITE_GET_LOCKPROXYFILE: {
   28514                 :       return proxyFileControl(id,op,pArg);
   28515                 :     }
   28516                 : #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
   28517                 :   }
   28518            2238 :   return SQLITE_NOTFOUND;
   28519                 : }
   28520                 : 
   28521                 : /*
   28522                 : ** Return the sector size in bytes of the underlying block device for
   28523                 : ** the specified file. This is almost always 512 bytes, but may be
   28524                 : ** larger for some devices.
   28525                 : **
   28526                 : ** SQLite code assumes this function cannot fail. It also assumes that
   28527                 : ** if two files are created in the same file-system directory (i.e.
   28528                 : ** a database and its journal file) that the sector size will be the
   28529                 : ** same for both.
   28530                 : */
   28531               0 : static int unixSectorSize(sqlite3_file *pFile){
   28532                 :   (void)pFile;
   28533               0 :   return SQLITE_DEFAULT_SECTOR_SIZE;
   28534                 : }
   28535                 : 
   28536                 : /*
   28537                 : ** Return the device characteristics for the file.
   28538                 : **
   28539                 : ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
   28540                 : ** However, that choice is contraversial since technically the underlying
   28541                 : ** file system does not always provide powersafe overwrites.  (In other
   28542                 : ** words, after a power-loss event, parts of the file that were never
   28543                 : ** written might end up being altered.)  However, non-PSOW behavior is very,
   28544                 : ** very rare.  And asserting PSOW makes a large reduction in the amount
   28545                 : ** of required I/O for journaling, since a lot of padding is eliminated.
   28546                 : **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
   28547                 : ** available to turn it off and URI query parameter available to turn it off.
   28548                 : */
   28549           37131 : static int unixDeviceCharacteristics(sqlite3_file *id){
   28550           37131 :   unixFile *p = (unixFile*)id;
   28551           37131 :   if( p->ctrlFlags & UNIXFILE_PSOW ){
   28552           37131 :     return SQLITE_IOCAP_POWERSAFE_OVERWRITE;
   28553                 :   }else{
   28554               0 :     return 0;
   28555                 :   }
   28556                 : }
   28557                 : 
   28558                 : #ifndef SQLITE_OMIT_WAL
   28559                 : 
   28560                 : 
   28561                 : /*
   28562                 : ** Object used to represent an shared memory buffer.  
   28563                 : **
   28564                 : ** When multiple threads all reference the same wal-index, each thread
   28565                 : ** has its own unixShm object, but they all point to a single instance
   28566                 : ** of this unixShmNode object.  In other words, each wal-index is opened
   28567                 : ** only once per process.
   28568                 : **
   28569                 : ** Each unixShmNode object is connected to a single unixInodeInfo object.
   28570                 : ** We could coalesce this object into unixInodeInfo, but that would mean
   28571                 : ** every open file that does not use shared memory (in other words, most
   28572                 : ** open files) would have to carry around this extra information.  So
   28573                 : ** the unixInodeInfo object contains a pointer to this unixShmNode object
   28574                 : ** and the unixShmNode object is created only when needed.
   28575                 : **
   28576                 : ** unixMutexHeld() must be true when creating or destroying
   28577                 : ** this object or while reading or writing the following fields:
   28578                 : **
   28579                 : **      nRef
   28580                 : **
   28581                 : ** The following fields are read-only after the object is created:
   28582                 : ** 
   28583                 : **      fid
   28584                 : **      zFilename
   28585                 : **
   28586                 : ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
   28587                 : ** unixMutexHeld() is true when reading or writing any other field
   28588                 : ** in this structure.
   28589                 : */
   28590                 : struct unixShmNode {
   28591                 :   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
   28592                 :   sqlite3_mutex *mutex;      /* Mutex to access this object */
   28593                 :   char *zFilename;           /* Name of the mmapped file */
   28594                 :   int h;                     /* Open file descriptor */
   28595                 :   int szRegion;              /* Size of shared-memory regions */
   28596                 :   u16 nRegion;               /* Size of array apRegion */
   28597                 :   u8 isReadonly;             /* True if read-only */
   28598                 :   char **apRegion;           /* Array of mapped shared-memory regions */
   28599                 :   int nRef;                  /* Number of unixShm objects pointing to this */
   28600                 :   unixShm *pFirst;           /* All unixShm objects pointing to this */
   28601                 : #ifdef SQLITE_DEBUG
   28602                 :   u8 exclMask;               /* Mask of exclusive locks held */
   28603                 :   u8 sharedMask;             /* Mask of shared locks held */
   28604                 :   u8 nextShmId;              /* Next available unixShm.id value */
   28605                 : #endif
   28606                 : };
   28607                 : 
   28608                 : /*
   28609                 : ** Structure used internally by this VFS to record the state of an
   28610                 : ** open shared memory connection.
   28611                 : **
   28612                 : ** The following fields are initialized when this object is created and
   28613                 : ** are read-only thereafter:
   28614                 : **
   28615                 : **    unixShm.pFile
   28616                 : **    unixShm.id
   28617                 : **
   28618                 : ** All other fields are read/write.  The unixShm.pFile->mutex must be held
   28619                 : ** while accessing any read/write fields.
   28620                 : */
   28621                 : struct unixShm {
   28622                 :   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
   28623                 :   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
   28624                 :   u8 hasMutex;               /* True if holding the unixShmNode mutex */
   28625                 :   u8 id;                     /* Id of this connection within its unixShmNode */
   28626                 :   u16 sharedMask;            /* Mask of shared locks held */
   28627                 :   u16 exclMask;              /* Mask of exclusive locks held */
   28628                 : };
   28629                 : 
   28630                 : /*
   28631                 : ** Constants used for locking
   28632                 : */
   28633                 : #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
   28634                 : #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
   28635                 : 
   28636                 : /*
   28637                 : ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
   28638                 : **
   28639                 : ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
   28640                 : ** otherwise.
   28641                 : */
   28642          237412 : static int unixShmSystemLock(
   28643                 :   unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
   28644                 :   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
   28645                 :   int ofst,              /* First byte of the locking range */
   28646                 :   int n                  /* Number of bytes to lock */
   28647                 : ){
   28648                 :   struct flock f;       /* The posix advisory locking structure */
   28649          237412 :   int rc = SQLITE_OK;   /* Result code form fcntl() */
   28650                 : 
   28651                 :   /* Access to the unixShmNode object is serialized by the caller */
   28652          237412 :   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
   28653                 : 
   28654                 :   /* Shared locks never span more than one byte */
   28655          237412 :   assert( n==1 || lockType!=F_RDLCK );
   28656                 : 
   28657                 :   /* Locks are within range */
   28658          237412 :   assert( n>=1 && n<SQLITE_SHM_NLOCK );
   28659                 : 
   28660          237412 :   if( pShmNode->h>=0 ){
   28661                 :     /* Initialize the locking parameters */
   28662          237412 :     memset(&f, 0, sizeof(f));
   28663          237412 :     f.l_type = lockType;
   28664          237412 :     f.l_whence = SEEK_SET;
   28665          237412 :     f.l_start = ofst;
   28666          237412 :     f.l_len = n;
   28667                 : 
   28668          237412 :     rc = osFcntl(pShmNode->h, F_SETLK, &f);
   28669          237412 :     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
   28670                 :   }
   28671                 : 
   28672                 :   /* Update the global lock state and do debug tracing */
   28673                 : #ifdef SQLITE_DEBUG
   28674                 :   { u16 mask;
   28675                 :   OSTRACE(("SHM-LOCK "));
   28676          237412 :   mask = (1<<(ofst+n)) - (1<<ofst);
   28677          237412 :   if( rc==SQLITE_OK ){
   28678          237412 :     if( lockType==F_UNLCK ){
   28679                 :       OSTRACE(("unlock %d ok", ofst));
   28680          118353 :       pShmNode->exclMask &= ~mask;
   28681          118353 :       pShmNode->sharedMask &= ~mask;
   28682          119059 :     }else if( lockType==F_RDLCK ){
   28683                 :       OSTRACE(("read-lock %d ok", ofst));
   28684           45546 :       pShmNode->exclMask &= ~mask;
   28685           45546 :       pShmNode->sharedMask |= mask;
   28686                 :     }else{
   28687           73513 :       assert( lockType==F_WRLCK );
   28688                 :       OSTRACE(("write-lock %d ok", ofst));
   28689           73513 :       pShmNode->exclMask |= mask;
   28690           73513 :       pShmNode->sharedMask &= ~mask;
   28691                 :     }
   28692                 :   }else{
   28693               0 :     if( lockType==F_UNLCK ){
   28694                 :       OSTRACE(("unlock %d failed", ofst));
   28695               0 :     }else if( lockType==F_RDLCK ){
   28696                 :       OSTRACE(("read-lock failed"));
   28697                 :     }else{
   28698               0 :       assert( lockType==F_WRLCK );
   28699                 :       OSTRACE(("write-lock %d failed", ofst));
   28700                 :     }
   28701                 :   }
   28702                 :   OSTRACE((" - afterwards %03x,%03x\n",
   28703                 :            pShmNode->sharedMask, pShmNode->exclMask));
   28704                 :   }
   28705                 : #endif
   28706                 : 
   28707          237412 :   return rc;        
   28708                 : }
   28709                 : 
   28710                 : 
   28711                 : /*
   28712                 : ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
   28713                 : **
   28714                 : ** This is not a VFS shared-memory method; it is a utility function called
   28715                 : ** by VFS shared-memory methods.
   28716                 : */
   28717             353 : static void unixShmPurge(unixFile *pFd){
   28718             353 :   unixShmNode *p = pFd->pInode->pShmNode;
   28719             353 :   assert( unixMutexHeld() );
   28720             353 :   if( p && p->nRef==0 ){
   28721                 :     int i;
   28722             353 :     assert( p->pInode==pFd->pInode );
   28723             353 :     sqlite3_mutex_free(p->mutex);
   28724             706 :     for(i=0; i<p->nRegion; i++){
   28725             353 :       if( p->h>=0 ){
   28726             353 :         munmap(p->apRegion[i], p->szRegion);
   28727                 :       }else{
   28728               0 :         sqlite3_free(p->apRegion[i]);
   28729                 :       }
   28730                 :     }
   28731             353 :     sqlite3_free(p->apRegion);
   28732             353 :     if( p->h>=0 ){
   28733             353 :       robust_close(pFd, p->h, __LINE__);
   28734             353 :       p->h = -1;
   28735                 :     }
   28736             353 :     p->pInode->pShmNode = 0;
   28737             353 :     sqlite3_free(p);
   28738                 :   }
   28739             353 : }
   28740                 : 
   28741                 : /*
   28742                 : ** Open a shared-memory area associated with open database file pDbFd.  
   28743                 : ** This particular implementation uses mmapped files.
   28744                 : **
   28745                 : ** The file used to implement shared-memory is in the same directory
   28746                 : ** as the open database file and has the same name as the open database
   28747                 : ** file with the "-shm" suffix added.  For example, if the database file
   28748                 : ** is "/home/user1/config.db" then the file that is created and mmapped
   28749                 : ** for shared memory will be called "/home/user1/config.db-shm".  
   28750                 : **
   28751                 : ** Another approach to is to use files in /dev/shm or /dev/tmp or an
   28752                 : ** some other tmpfs mount. But if a file in a different directory
   28753                 : ** from the database file is used, then differing access permissions
   28754                 : ** or a chroot() might cause two different processes on the same
   28755                 : ** database to end up using different files for shared memory - 
   28756                 : ** meaning that their memory would not really be shared - resulting
   28757                 : ** in database corruption.  Nevertheless, this tmpfs file usage
   28758                 : ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
   28759                 : ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
   28760                 : ** option results in an incompatible build of SQLite;  builds of SQLite
   28761                 : ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
   28762                 : ** same database file at the same time, database corruption will likely
   28763                 : ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
   28764                 : ** "unsupported" and may go away in a future SQLite release.
   28765                 : **
   28766                 : ** When opening a new shared-memory file, if no other instances of that
   28767                 : ** file are currently open, in this process or in other processes, then
   28768                 : ** the file must be truncated to zero length or have its header cleared.
   28769                 : **
   28770                 : ** If the original database file (pDbFd) is using the "unix-excl" VFS
   28771                 : ** that means that an exclusive lock is held on the database file and
   28772                 : ** that no other processes are able to read or write the database.  In
   28773                 : ** that case, we do not really need shared memory.  No shared memory
   28774                 : ** file is created.  The shared memory will be simulated with heap memory.
   28775                 : */
   28776             426 : static int unixOpenSharedMemory(unixFile *pDbFd){
   28777             426 :   struct unixShm *p = 0;          /* The connection to be opened */
   28778                 :   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
   28779                 :   int rc;                         /* Result code */
   28780                 :   unixInodeInfo *pInode;          /* The inode of fd */
   28781                 :   char *zShmFilename;             /* Name of the file used for SHM */
   28782                 :   int nShmFilename;               /* Size of the SHM filename in bytes */
   28783                 : 
   28784                 :   /* Allocate space for the new unixShm object. */
   28785             426 :   p = sqlite3_malloc( sizeof(*p) );
   28786             426 :   if( p==0 ) return SQLITE_NOMEM;
   28787             426 :   memset(p, 0, sizeof(*p));
   28788             426 :   assert( pDbFd->pShm==0 );
   28789                 : 
   28790                 :   /* Check to see if a unixShmNode object already exists. Reuse an existing
   28791                 :   ** one if present. Create a new one if necessary.
   28792                 :   */
   28793             426 :   unixEnterMutex();
   28794             426 :   pInode = pDbFd->pInode;
   28795             426 :   pShmNode = pInode->pShmNode;
   28796             426 :   if( pShmNode==0 ){
   28797                 :     struct stat sStat;                 /* fstat() info for database file */
   28798                 : 
   28799                 :     /* Call fstat() to figure out the permissions on the database file. If
   28800                 :     ** a new *-shm file is created, an attempt will be made to create it
   28801                 :     ** with the same permissions. The actual permissions the file is created
   28802                 :     ** with are subject to the current umask setting.
   28803                 :     */
   28804             353 :     if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
   28805               0 :       rc = SQLITE_IOERR_FSTAT;
   28806               0 :       goto shm_open_err;
   28807                 :     }
   28808                 : 
   28809                 : #ifdef SQLITE_SHM_DIRECTORY
   28810                 :     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
   28811                 : #else
   28812             353 :     nShmFilename = 6 + (int)strlen(pDbFd->zPath);
   28813                 : #endif
   28814             353 :     pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
   28815             353 :     if( pShmNode==0 ){
   28816               0 :       rc = SQLITE_NOMEM;
   28817               0 :       goto shm_open_err;
   28818                 :     }
   28819             353 :     memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
   28820             353 :     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
   28821                 : #ifdef SQLITE_SHM_DIRECTORY
   28822                 :     sqlite3_snprintf(nShmFilename, zShmFilename, 
   28823                 :                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
   28824                 :                      (u32)sStat.st_ino, (u32)sStat.st_dev);
   28825                 : #else
   28826             353 :     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
   28827                 :     sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
   28828                 : #endif
   28829             353 :     pShmNode->h = -1;
   28830             353 :     pDbFd->pInode->pShmNode = pShmNode;
   28831             353 :     pShmNode->pInode = pDbFd->pInode;
   28832             353 :     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
   28833             353 :     if( pShmNode->mutex==0 ){
   28834               0 :       rc = SQLITE_NOMEM;
   28835               0 :       goto shm_open_err;
   28836                 :     }
   28837                 : 
   28838             353 :     if( pInode->bProcessLock==0 ){
   28839             353 :       int openFlags = O_RDWR | O_CREAT;
   28840             353 :       if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
   28841               0 :         openFlags = O_RDONLY;
   28842               0 :         pShmNode->isReadonly = 1;
   28843                 :       }
   28844             353 :       pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
   28845             353 :       if( pShmNode->h<0 ){
   28846               0 :         if( pShmNode->h<0 ){
   28847               0 :           rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
   28848               0 :           goto shm_open_err;
   28849                 :         }
   28850                 :       }
   28851                 :   
   28852                 :       /* Check to see if another process is holding the dead-man switch.
   28853                 :       ** If not, truncate the file to zero length. 
   28854                 :       */
   28855             353 :       rc = SQLITE_OK;
   28856             353 :       if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
   28857             353 :         if( robust_ftruncate(pShmNode->h, 0) ){
   28858               0 :           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
   28859                 :         }
   28860                 :       }
   28861             353 :       if( rc==SQLITE_OK ){
   28862             353 :         rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
   28863                 :       }
   28864             353 :       if( rc ) goto shm_open_err;
   28865                 :     }
   28866                 :   }
   28867                 : 
   28868                 :   /* Make the new connection a child of the unixShmNode */
   28869             426 :   p->pShmNode = pShmNode;
   28870                 : #ifdef SQLITE_DEBUG
   28871             426 :   p->id = pShmNode->nextShmId++;
   28872                 : #endif
   28873             426 :   pShmNode->nRef++;
   28874             426 :   pDbFd->pShm = p;
   28875             426 :   unixLeaveMutex();
   28876                 : 
   28877                 :   /* The reference count on pShmNode has already been incremented under
   28878                 :   ** the cover of the unixEnterMutex() mutex and the pointer from the
   28879                 :   ** new (struct unixShm) object to the pShmNode has been set. All that is
   28880                 :   ** left to do is to link the new object into the linked list starting
   28881                 :   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
   28882                 :   ** mutex.
   28883                 :   */
   28884             426 :   sqlite3_mutex_enter(pShmNode->mutex);
   28885             426 :   p->pNext = pShmNode->pFirst;
   28886             426 :   pShmNode->pFirst = p;
   28887             426 :   sqlite3_mutex_leave(pShmNode->mutex);
   28888             426 :   return SQLITE_OK;
   28889                 : 
   28890                 :   /* Jump here on any error */
   28891                 : shm_open_err:
   28892               0 :   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
   28893               0 :   sqlite3_free(p);
   28894               0 :   unixLeaveMutex();
   28895               0 :   return rc;
   28896                 : }
   28897                 : 
   28898                 : /*
   28899                 : ** This function is called to obtain a pointer to region iRegion of the 
   28900                 : ** shared-memory associated with the database file fd. Shared-memory regions 
   28901                 : ** are numbered starting from zero. Each shared-memory region is szRegion 
   28902                 : ** bytes in size.
   28903                 : **
   28904                 : ** If an error occurs, an error code is returned and *pp is set to NULL.
   28905                 : **
   28906                 : ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
   28907                 : ** region has not been allocated (by any client, including one running in a
   28908                 : ** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
   28909                 : ** bExtend is non-zero and the requested shared-memory region has not yet 
   28910                 : ** been allocated, it is allocated by this function.
   28911                 : **
   28912                 : ** If the shared-memory region has already been allocated or is allocated by
   28913                 : ** this call as described above, then it is mapped into this processes 
   28914                 : ** address space (if it is not already), *pp is set to point to the mapped 
   28915                 : ** memory and SQLITE_OK returned.
   28916                 : */
   28917             779 : static int unixShmMap(
   28918                 :   sqlite3_file *fd,               /* Handle open on database file */
   28919                 :   int iRegion,                    /* Region to retrieve */
   28920                 :   int szRegion,                   /* Size of regions */
   28921                 :   int bExtend,                    /* True to extend file if necessary */
   28922                 :   void volatile **pp              /* OUT: Mapped memory */
   28923                 : ){
   28924             779 :   unixFile *pDbFd = (unixFile*)fd;
   28925                 :   unixShm *p;
   28926                 :   unixShmNode *pShmNode;
   28927             779 :   int rc = SQLITE_OK;
   28928                 : 
   28929                 :   /* If the shared-memory file has not yet been opened, open it now. */
   28930             779 :   if( pDbFd->pShm==0 ){
   28931             426 :     rc = unixOpenSharedMemory(pDbFd);
   28932             426 :     if( rc!=SQLITE_OK ) return rc;
   28933                 :   }
   28934                 : 
   28935             779 :   p = pDbFd->pShm;
   28936             779 :   pShmNode = p->pShmNode;
   28937             779 :   sqlite3_mutex_enter(pShmNode->mutex);
   28938             779 :   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
   28939             779 :   assert( pShmNode->pInode==pDbFd->pInode );
   28940             779 :   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
   28941             779 :   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
   28942                 : 
   28943             779 :   if( pShmNode->nRegion<=iRegion ){
   28944                 :     char **apNew;                      /* New apRegion[] array */
   28945             706 :     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
   28946                 :     struct stat sStat;                 /* Used by fstat() */
   28947                 : 
   28948             706 :     pShmNode->szRegion = szRegion;
   28949                 : 
   28950             706 :     if( pShmNode->h>=0 ){
   28951                 :       /* The requested region is not mapped into this processes address space.
   28952                 :       ** Check to see if it has been allocated (i.e. if the wal-index file is
   28953                 :       ** large enough to contain the requested region).
   28954                 :       */
   28955             706 :       if( osFstat(pShmNode->h, &sStat) ){
   28956               0 :         rc = SQLITE_IOERR_SHMSIZE;
   28957               0 :         goto shmpage_out;
   28958                 :       }
   28959                 :   
   28960             706 :       if( sStat.st_size<nByte ){
   28961                 :         /* The requested memory region does not exist. If bExtend is set to
   28962                 :         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
   28963                 :         **
   28964                 :         ** Alternatively, if bExtend is true, use ftruncate() to allocate
   28965                 :         ** the requested memory region.
   28966                 :         */
   28967             706 :         if( !bExtend ) goto shmpage_out;
   28968             353 :         if( robust_ftruncate(pShmNode->h, nByte) ){
   28969               0 :           rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate",
   28970                 :                             pShmNode->zFilename);
   28971               0 :           goto shmpage_out;
   28972                 :         }
   28973                 :       }
   28974                 :     }
   28975                 : 
   28976                 :     /* Map the requested memory region into this processes address space. */
   28977             706 :     apNew = (char **)sqlite3_realloc(
   28978             353 :         pShmNode->apRegion, (iRegion+1)*sizeof(char *)
   28979                 :     );
   28980             353 :     if( !apNew ){
   28981               0 :       rc = SQLITE_IOERR_NOMEM;
   28982               0 :       goto shmpage_out;
   28983                 :     }
   28984             353 :     pShmNode->apRegion = apNew;
   28985            1059 :     while(pShmNode->nRegion<=iRegion){
   28986                 :       void *pMem;
   28987             353 :       if( pShmNode->h>=0 ){
   28988             706 :         pMem = mmap(0, szRegion,
   28989             353 :             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
   28990             353 :             MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion
   28991                 :         );
   28992             353 :         if( pMem==MAP_FAILED ){
   28993               0 :           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
   28994               0 :           goto shmpage_out;
   28995                 :         }
   28996                 :       }else{
   28997               0 :         pMem = sqlite3_malloc(szRegion);
   28998               0 :         if( pMem==0 ){
   28999               0 :           rc = SQLITE_NOMEM;
   29000               0 :           goto shmpage_out;
   29001                 :         }
   29002               0 :         memset(pMem, 0, szRegion);
   29003                 :       }
   29004             353 :       pShmNode->apRegion[pShmNode->nRegion] = pMem;
   29005             353 :       pShmNode->nRegion++;
   29006                 :     }
   29007                 :   }
   29008                 : 
   29009                 : shmpage_out:
   29010             779 :   if( pShmNode->nRegion>iRegion ){
   29011             426 :     *pp = pShmNode->apRegion[iRegion];
   29012                 :   }else{
   29013             353 :     *pp = 0;
   29014                 :   }
   29015             779 :   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
   29016             779 :   sqlite3_mutex_leave(pShmNode->mutex);
   29017             779 :   return rc;
   29018                 : }
   29019                 : 
   29020                 : /*
   29021                 : ** Change the lock state for a shared-memory segment.
   29022                 : **
   29023                 : ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
   29024                 : ** different here than in posix.  In xShmLock(), one can go from unlocked
   29025                 : ** to shared and back or from unlocked to exclusive and back.  But one may
   29026                 : ** not go from shared to exclusive or from exclusive to shared.
   29027                 : */
   29028          237317 : static int unixShmLock(
   29029                 :   sqlite3_file *fd,          /* Database file holding the shared memory */
   29030                 :   int ofst,                  /* First lock to acquire or release */
   29031                 :   int n,                     /* Number of locks to acquire or release */
   29032                 :   int flags                  /* What to do with the lock */
   29033                 : ){
   29034          237317 :   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
   29035          237317 :   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
   29036                 :   unixShm *pX;                          /* For looping over all siblings */
   29037          237317 :   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
   29038          237317 :   int rc = SQLITE_OK;                   /* Result code */
   29039                 :   u16 mask;                             /* Mask of locks to take or release */
   29040                 : 
   29041          237317 :   assert( pShmNode==pDbFd->pInode->pShmNode );
   29042          237317 :   assert( pShmNode->pInode==pDbFd->pInode );
   29043          237317 :   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
   29044          237317 :   assert( n>=1 );
   29045          237317 :   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
   29046                 :        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
   29047                 :        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
   29048                 :        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
   29049          237317 :   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
   29050          237317 :   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
   29051          237317 :   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
   29052                 : 
   29053          237317 :   mask = (1<<(ofst+n)) - (1<<ofst);
   29054          237317 :   assert( n>1 || mask==(1<<ofst) );
   29055          237317 :   sqlite3_mutex_enter(pShmNode->mutex);
   29056          237317 :   if( flags & SQLITE_SHM_UNLOCK ){
   29057          118570 :     u16 allMask = 0; /* Mask of locks held by siblings */
   29058                 : 
   29059                 :     /* See if any siblings hold this same lock */
   29060          246133 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   29061          127563 :       if( pX==p ) continue;
   29062            8993 :       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
   29063            8993 :       allMask |= pX->sharedMask;
   29064                 :     }
   29065                 : 
   29066                 :     /* Unlock the system-level locks */
   29067          118570 :     if( (mask & allMask)==0 ){
   29068          118353 :       rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
   29069                 :     }else{
   29070             217 :       rc = SQLITE_OK;
   29071                 :     }
   29072                 : 
   29073                 :     /* Undo the local locks */
   29074          118570 :     if( rc==SQLITE_OK ){
   29075          118570 :       p->exclMask &= ~mask;
   29076          118570 :       p->sharedMask &= ~mask;
   29077                 :     } 
   29078          118747 :   }else if( flags & SQLITE_SHM_SHARED ){
   29079           45410 :     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
   29080                 : 
   29081                 :     /* Find out which shared locks are already held by sibling connections.
   29082                 :     ** If any sibling already holds an exclusive lock, go ahead and return
   29083                 :     ** SQLITE_BUSY.
   29084                 :     */
   29085           95388 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   29086           49978 :       if( (pX->exclMask & mask)!=0 ){
   29087               0 :         rc = SQLITE_BUSY;
   29088               0 :         break;
   29089                 :       }
   29090           49978 :       allShared |= pX->sharedMask;
   29091                 :     }
   29092                 : 
   29093                 :     /* Get shared locks at the system level, if necessary */
   29094           45410 :     if( rc==SQLITE_OK ){
   29095           45410 :       if( (allShared & mask)==0 ){
   29096           45193 :         rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
   29097                 :       }else{
   29098             217 :         rc = SQLITE_OK;
   29099                 :       }
   29100                 :     }
   29101                 : 
   29102                 :     /* Get the local shared locks */
   29103           45410 :     if( rc==SQLITE_OK ){
   29104           45410 :       p->sharedMask |= mask;
   29105                 :     }
   29106                 :   }else{
   29107                 :     /* Make sure no sibling connections hold locks that will block this
   29108                 :     ** lock.  If any do, return SQLITE_BUSY right away.
   29109                 :     */
   29110          150919 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   29111           77759 :       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
   29112             177 :         rc = SQLITE_BUSY;
   29113             177 :         break;
   29114                 :       }
   29115                 :     }
   29116                 :   
   29117                 :     /* Get the exclusive locks at the system level.  Then if successful
   29118                 :     ** also mark the local connection as being locked.
   29119                 :     */
   29120           73337 :     if( rc==SQLITE_OK ){
   29121           73160 :       rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
   29122           73160 :       if( rc==SQLITE_OK ){
   29123           73160 :         assert( (p->sharedMask & mask)==0 );
   29124           73160 :         p->exclMask |= mask;
   29125                 :       }
   29126                 :     }
   29127                 :   }
   29128          237317 :   sqlite3_mutex_leave(pShmNode->mutex);
   29129                 :   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
   29130                 :            p->id, getpid(), p->sharedMask, p->exclMask));
   29131          237317 :   return rc;
   29132                 : }
   29133                 : 
   29134                 : /*
   29135                 : ** Implement a memory barrier or memory fence on shared memory.  
   29136                 : **
   29137                 : ** All loads and stores begun before the barrier must complete before
   29138                 : ** any load or store begun after the barrier.
   29139                 : */
   29140          121778 : static void unixShmBarrier(
   29141                 :   sqlite3_file *fd                /* Database file holding the shared memory */
   29142                 : ){
   29143                 :   UNUSED_PARAMETER(fd);
   29144          121778 :   unixEnterMutex();
   29145          121778 :   unixLeaveMutex();
   29146          121778 : }
   29147                 : 
   29148                 : /*
   29149                 : ** Close a connection to shared-memory.  Delete the underlying 
   29150                 : ** storage if deleteFlag is true.
   29151                 : **
   29152                 : ** If there is no shared memory associated with the connection then this
   29153                 : ** routine is a harmless no-op.
   29154                 : */
   29155             426 : static int unixShmUnmap(
   29156                 :   sqlite3_file *fd,               /* The underlying database file */
   29157                 :   int deleteFlag                  /* Delete shared-memory if true */
   29158                 : ){
   29159                 :   unixShm *p;                     /* The connection to be closed */
   29160                 :   unixShmNode *pShmNode;          /* The underlying shared-memory file */
   29161                 :   unixShm **pp;                   /* For looping over sibling connections */
   29162                 :   unixFile *pDbFd;                /* The underlying database file */
   29163                 : 
   29164             426 :   pDbFd = (unixFile*)fd;
   29165             426 :   p = pDbFd->pShm;
   29166             426 :   if( p==0 ) return SQLITE_OK;
   29167             426 :   pShmNode = p->pShmNode;
   29168                 : 
   29169             426 :   assert( pShmNode==pDbFd->pInode->pShmNode );
   29170             426 :   assert( pShmNode->pInode==pDbFd->pInode );
   29171                 : 
   29172                 :   /* Remove connection p from the set of connections associated
   29173                 :   ** with pShmNode */
   29174             426 :   sqlite3_mutex_enter(pShmNode->mutex);
   29175             426 :   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
   29176             426 :   *pp = p->pNext;
   29177                 : 
   29178                 :   /* Free the connection p */
   29179             426 :   sqlite3_free(p);
   29180             426 :   pDbFd->pShm = 0;
   29181             426 :   sqlite3_mutex_leave(pShmNode->mutex);
   29182                 : 
   29183                 :   /* If pShmNode->nRef has reached 0, then close the underlying
   29184                 :   ** shared-memory file, too */
   29185             426 :   unixEnterMutex();
   29186             426 :   assert( pShmNode->nRef>0 );
   29187             426 :   pShmNode->nRef--;
   29188             426 :   if( pShmNode->nRef==0 ){
   29189             353 :     if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
   29190             353 :     unixShmPurge(pDbFd);
   29191                 :   }
   29192             426 :   unixLeaveMutex();
   29193                 : 
   29194             426 :   return SQLITE_OK;
   29195                 : }
   29196                 : 
   29197                 : 
   29198                 : #else
   29199                 : # define unixShmMap     0
   29200                 : # define unixShmLock    0
   29201                 : # define unixShmBarrier 0
   29202                 : # define unixShmUnmap   0
   29203                 : #endif /* #ifndef SQLITE_OMIT_WAL */
   29204                 : 
   29205                 : /*
   29206                 : ** Here ends the implementation of all sqlite3_file methods.
   29207                 : **
   29208                 : ********************** End sqlite3_file Methods *******************************
   29209                 : ******************************************************************************/
   29210                 : 
   29211                 : /*
   29212                 : ** This division contains definitions of sqlite3_io_methods objects that
   29213                 : ** implement various file locking strategies.  It also contains definitions
   29214                 : ** of "finder" functions.  A finder-function is used to locate the appropriate
   29215                 : ** sqlite3_io_methods object for a particular database file.  The pAppData
   29216                 : ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
   29217                 : ** the correct finder-function for that VFS.
   29218                 : **
   29219                 : ** Most finder functions return a pointer to a fixed sqlite3_io_methods
   29220                 : ** object.  The only interesting finder-function is autolockIoFinder, which
   29221                 : ** looks at the filesystem type and tries to guess the best locking
   29222                 : ** strategy from that.
   29223                 : **
   29224                 : ** For finder-funtion F, two objects are created:
   29225                 : **
   29226                 : **    (1) The real finder-function named "FImpt()".
   29227                 : **
   29228                 : **    (2) A constant pointer to this function named just "F".
   29229                 : **
   29230                 : **
   29231                 : ** A pointer to the F pointer is used as the pAppData value for VFS
   29232                 : ** objects.  We have to do this instead of letting pAppData point
   29233                 : ** directly at the finder-function since C90 rules prevent a void*
   29234                 : ** from be cast into a function pointer.
   29235                 : **
   29236                 : **
   29237                 : ** Each instance of this macro generates two objects:
   29238                 : **
   29239                 : **   *  A constant sqlite3_io_methods object call METHOD that has locking
   29240                 : **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
   29241                 : **
   29242                 : **   *  An I/O method finder function called FINDER that returns a pointer
   29243                 : **      to the METHOD object in the previous bullet.
   29244                 : */
   29245                 : #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
   29246                 : static const sqlite3_io_methods METHOD = {                                   \
   29247                 :    VERSION,                    /* iVersion */                                \
   29248                 :    CLOSE,                      /* xClose */                                  \
   29249                 :    unixRead,                   /* xRead */                                   \
   29250                 :    unixWrite,                  /* xWrite */                                  \
   29251                 :    unixTruncate,               /* xTruncate */                               \
   29252                 :    unixSync,                   /* xSync */                                   \
   29253                 :    unixFileSize,               /* xFileSize */                               \
   29254                 :    LOCK,                       /* xLock */                                   \
   29255                 :    UNLOCK,                     /* xUnlock */                                 \
   29256                 :    CKLOCK,                     /* xCheckReservedLock */                      \
   29257                 :    unixFileControl,            /* xFileControl */                            \
   29258                 :    unixSectorSize,             /* xSectorSize */                             \
   29259                 :    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
   29260                 :    unixShmMap,                 /* xShmMap */                                 \
   29261                 :    unixShmLock,                /* xShmLock */                                \
   29262                 :    unixShmBarrier,             /* xShmBarrier */                             \
   29263                 :    unixShmUnmap                /* xShmUnmap */                               \
   29264                 : };                                                                           \
   29265                 : static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
   29266                 :   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
   29267                 :   return &METHOD;                                                            \
   29268                 : }                                                                            \
   29269                 : static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
   29270                 :     = FINDER##Impl;
   29271                 : 
   29272                 : /*
   29273                 : ** Here are all of the sqlite3_io_methods objects for each of the
   29274                 : ** locking strategies.  Functions that return pointers to these methods
   29275                 : ** are also created.
   29276                 : */
   29277            3052 : IOMETHODS(
   29278                 :   posixIoFinder,            /* Finder function name */
   29279                 :   posixIoMethods,           /* sqlite3_io_methods object name */
   29280                 :   2,                        /* shared memory is enabled */
   29281                 :   unixClose,                /* xClose method */
   29282                 :   unixLock,                 /* xLock method */
   29283                 :   unixUnlock,               /* xUnlock method */
   29284                 :   unixCheckReservedLock     /* xCheckReservedLock method */
   29285            3052 : )
   29286               0 : IOMETHODS(
   29287                 :   nolockIoFinder,           /* Finder function name */
   29288                 :   nolockIoMethods,          /* sqlite3_io_methods object name */
   29289                 :   1,                        /* shared memory is disabled */
   29290                 :   nolockClose,              /* xClose method */
   29291                 :   nolockLock,               /* xLock method */
   29292                 :   nolockUnlock,             /* xUnlock method */
   29293                 :   nolockCheckReservedLock   /* xCheckReservedLock method */
   29294               0 : )
   29295               0 : IOMETHODS(
   29296                 :   dotlockIoFinder,          /* Finder function name */
   29297                 :   dotlockIoMethods,         /* sqlite3_io_methods object name */
   29298                 :   1,                        /* shared memory is disabled */
   29299                 :   dotlockClose,             /* xClose method */
   29300                 :   dotlockLock,              /* xLock method */
   29301                 :   dotlockUnlock,            /* xUnlock method */
   29302                 :   dotlockCheckReservedLock  /* xCheckReservedLock method */
   29303               0 : )
   29304                 : 
   29305                 : #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
   29306                 : IOMETHODS(
   29307                 :   flockIoFinder,            /* Finder function name */
   29308                 :   flockIoMethods,           /* sqlite3_io_methods object name */
   29309                 :   1,                        /* shared memory is disabled */
   29310                 :   flockClose,               /* xClose method */
   29311                 :   flockLock,                /* xLock method */
   29312                 :   flockUnlock,              /* xUnlock method */
   29313                 :   flockCheckReservedLock    /* xCheckReservedLock method */
   29314                 : )
   29315                 : #endif
   29316                 : 
   29317                 : #if OS_VXWORKS
   29318                 : IOMETHODS(
   29319                 :   semIoFinder,              /* Finder function name */
   29320                 :   semIoMethods,             /* sqlite3_io_methods object name */
   29321                 :   1,                        /* shared memory is disabled */
   29322                 :   semClose,                 /* xClose method */
   29323                 :   semLock,                  /* xLock method */
   29324                 :   semUnlock,                /* xUnlock method */
   29325                 :   semCheckReservedLock      /* xCheckReservedLock method */
   29326                 : )
   29327                 : #endif
   29328                 : 
   29329                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   29330                 : IOMETHODS(
   29331                 :   afpIoFinder,              /* Finder function name */
   29332                 :   afpIoMethods,             /* sqlite3_io_methods object name */
   29333                 :   1,                        /* shared memory is disabled */
   29334                 :   afpClose,                 /* xClose method */
   29335                 :   afpLock,                  /* xLock method */
   29336                 :   afpUnlock,                /* xUnlock method */
   29337                 :   afpCheckReservedLock      /* xCheckReservedLock method */
   29338                 : )
   29339                 : #endif
   29340                 : 
   29341                 : /*
   29342                 : ** The proxy locking method is a "super-method" in the sense that it
   29343                 : ** opens secondary file descriptors for the conch and lock files and
   29344                 : ** it uses proxy, dot-file, AFP, and flock() locking methods on those
   29345                 : ** secondary files.  For this reason, the division that implements
   29346                 : ** proxy locking is located much further down in the file.  But we need
   29347                 : ** to go ahead and define the sqlite3_io_methods and finder function
   29348                 : ** for proxy locking here.  So we forward declare the I/O methods.
   29349                 : */
   29350                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   29351                 : static int proxyClose(sqlite3_file*);
   29352                 : static int proxyLock(sqlite3_file*, int);
   29353                 : static int proxyUnlock(sqlite3_file*, int);
   29354                 : static int proxyCheckReservedLock(sqlite3_file*, int*);
   29355                 : IOMETHODS(
   29356                 :   proxyIoFinder,            /* Finder function name */
   29357                 :   proxyIoMethods,           /* sqlite3_io_methods object name */
   29358                 :   1,                        /* shared memory is disabled */
   29359                 :   proxyClose,               /* xClose method */
   29360                 :   proxyLock,                /* xLock method */
   29361                 :   proxyUnlock,              /* xUnlock method */
   29362                 :   proxyCheckReservedLock    /* xCheckReservedLock method */
   29363                 : )
   29364                 : #endif
   29365                 : 
   29366                 : /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
   29367                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   29368                 : IOMETHODS(
   29369                 :   nfsIoFinder,               /* Finder function name */
   29370                 :   nfsIoMethods,              /* sqlite3_io_methods object name */
   29371                 :   1,                         /* shared memory is disabled */
   29372                 :   unixClose,                 /* xClose method */
   29373                 :   unixLock,                  /* xLock method */
   29374                 :   nfsUnlock,                 /* xUnlock method */
   29375                 :   unixCheckReservedLock      /* xCheckReservedLock method */
   29376                 : )
   29377                 : #endif
   29378                 : 
   29379                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   29380                 : /* 
   29381                 : ** This "finder" function attempts to determine the best locking strategy 
   29382                 : ** for the database file "filePath".  It then returns the sqlite3_io_methods
   29383                 : ** object that implements that strategy.
   29384                 : **
   29385                 : ** This is for MacOSX only.
   29386                 : */
   29387                 : static const sqlite3_io_methods *autolockIoFinderImpl(
   29388                 :   const char *filePath,    /* name of the database file */
   29389                 :   unixFile *pNew           /* open file object for the database file */
   29390                 : ){
   29391                 :   static const struct Mapping {
   29392                 :     const char *zFilesystem;              /* Filesystem type name */
   29393                 :     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
   29394                 :   } aMap[] = {
   29395                 :     { "hfs",    &posixIoMethods },
   29396                 :     { "ufs",    &posixIoMethods },
   29397                 :     { "afpfs",  &afpIoMethods },
   29398                 :     { "smbfs",  &afpIoMethods },
   29399                 :     { "webdav", &nolockIoMethods },
   29400                 :     { 0, 0 }
   29401                 :   };
   29402                 :   int i;
   29403                 :   struct statfs fsInfo;
   29404                 :   struct flock lockInfo;
   29405                 : 
   29406                 :   if( !filePath ){
   29407                 :     /* If filePath==NULL that means we are dealing with a transient file
   29408                 :     ** that does not need to be locked. */
   29409                 :     return &nolockIoMethods;
   29410                 :   }
   29411                 :   if( statfs(filePath, &fsInfo) != -1 ){
   29412                 :     if( fsInfo.f_flags & MNT_RDONLY ){
   29413                 :       return &nolockIoMethods;
   29414                 :     }
   29415                 :     for(i=0; aMap[i].zFilesystem; i++){
   29416                 :       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
   29417                 :         return aMap[i].pMethods;
   29418                 :       }
   29419                 :     }
   29420                 :   }
   29421                 : 
   29422                 :   /* Default case. Handles, amongst others, "nfs".
   29423                 :   ** Test byte-range lock using fcntl(). If the call succeeds, 
   29424                 :   ** assume that the file-system supports POSIX style locks. 
   29425                 :   */
   29426                 :   lockInfo.l_len = 1;
   29427                 :   lockInfo.l_start = 0;
   29428                 :   lockInfo.l_whence = SEEK_SET;
   29429                 :   lockInfo.l_type = F_RDLCK;
   29430                 :   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
   29431                 :     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
   29432                 :       return &nfsIoMethods;
   29433                 :     } else {
   29434                 :       return &posixIoMethods;
   29435                 :     }
   29436                 :   }else{
   29437                 :     return &dotlockIoMethods;
   29438                 :   }
   29439                 : }
   29440                 : static const sqlite3_io_methods 
   29441                 :   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
   29442                 : 
   29443                 : #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   29444                 : 
   29445                 : #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
   29446                 : /* 
   29447                 : ** This "finder" function attempts to determine the best locking strategy 
   29448                 : ** for the database file "filePath".  It then returns the sqlite3_io_methods
   29449                 : ** object that implements that strategy.
   29450                 : **
   29451                 : ** This is for VXWorks only.
   29452                 : */
   29453                 : static const sqlite3_io_methods *autolockIoFinderImpl(
   29454                 :   const char *filePath,    /* name of the database file */
   29455                 :   unixFile *pNew           /* the open file object */
   29456                 : ){
   29457                 :   struct flock lockInfo;
   29458                 : 
   29459                 :   if( !filePath ){
   29460                 :     /* If filePath==NULL that means we are dealing with a transient file
   29461                 :     ** that does not need to be locked. */
   29462                 :     return &nolockIoMethods;
   29463                 :   }
   29464                 : 
   29465                 :   /* Test if fcntl() is supported and use POSIX style locks.
   29466                 :   ** Otherwise fall back to the named semaphore method.
   29467                 :   */
   29468                 :   lockInfo.l_len = 1;
   29469                 :   lockInfo.l_start = 0;
   29470                 :   lockInfo.l_whence = SEEK_SET;
   29471                 :   lockInfo.l_type = F_RDLCK;
   29472                 :   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
   29473                 :     return &posixIoMethods;
   29474                 :   }else{
   29475                 :     return &semIoMethods;
   29476                 :   }
   29477                 : }
   29478                 : static const sqlite3_io_methods 
   29479                 :   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
   29480                 : 
   29481                 : #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
   29482                 : 
   29483                 : /*
   29484                 : ** An abstract type for a pointer to a IO method finder function:
   29485                 : */
   29486                 : typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
   29487                 : 
   29488                 : 
   29489                 : /****************************************************************************
   29490                 : **************************** sqlite3_vfs methods ****************************
   29491                 : **
   29492                 : ** This division contains the implementation of methods on the
   29493                 : ** sqlite3_vfs object.
   29494                 : */
   29495                 : 
   29496                 : /*
   29497                 : ** Initialize the contents of the unixFile structure pointed to by pId.
   29498                 : */
   29499           11040 : static int fillInUnixFile(
   29500                 :   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
   29501                 :   int h,                  /* Open file descriptor of file being opened */
   29502                 :   sqlite3_file *pId,      /* Write to the unixFile structure here */
   29503                 :   const char *zFilename,  /* Name of the file being opened */
   29504                 :   int ctrlFlags           /* Zero or more UNIXFILE_* values */
   29505                 : ){
   29506                 :   const sqlite3_io_methods *pLockingStyle;
   29507           11040 :   unixFile *pNew = (unixFile *)pId;
   29508           11040 :   int rc = SQLITE_OK;
   29509                 : 
   29510           11040 :   assert( pNew->pInode==NULL );
   29511                 : 
   29512                 :   /* Usually the path zFilename should not be a relative pathname. The
   29513                 :   ** exception is when opening the proxy "conch" file in builds that
   29514                 :   ** include the special Apple locking styles.
   29515                 :   */
   29516                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   29517                 :   assert( zFilename==0 || zFilename[0]=='/' 
   29518                 :     || pVfs->pAppData==(void*)&autolockIoFinder );
   29519                 : #else
   29520           11040 :   assert( zFilename==0 || zFilename[0]=='/' );
   29521                 : #endif
   29522                 : 
   29523                 :   /* No locking occurs in temporary files */
   29524           11040 :   assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
   29525                 : 
   29526                 :   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
   29527           11040 :   pNew->h = h;
   29528           11040 :   pNew->pVfs = pVfs;
   29529           11040 :   pNew->zPath = zFilename;
   29530           11040 :   pNew->ctrlFlags = (u8)ctrlFlags;
   29531           11040 :   if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
   29532                 :                            "psow", SQLITE_POWERSAFE_OVERWRITE) ){
   29533           11040 :     pNew->ctrlFlags |= UNIXFILE_PSOW;
   29534                 :   }
   29535           11040 :   if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
   29536               0 :     pNew->ctrlFlags |= UNIXFILE_EXCL;
   29537                 :   }
   29538                 : 
   29539                 : #if OS_VXWORKS
   29540                 :   pNew->pId = vxworksFindFileId(zFilename);
   29541                 :   if( pNew->pId==0 ){
   29542                 :     ctrlFlags |= UNIXFILE_NOLOCK;
   29543                 :     rc = SQLITE_NOMEM;
   29544                 :   }
   29545                 : #endif
   29546                 : 
   29547           11040 :   if( ctrlFlags & UNIXFILE_NOLOCK ){
   29548            7988 :     pLockingStyle = &nolockIoMethods;
   29549                 :   }else{
   29550            3052 :     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
   29551                 : #if SQLITE_ENABLE_LOCKING_STYLE
   29552                 :     /* Cache zFilename in the locking context (AFP and dotlock override) for
   29553                 :     ** proxyLock activation is possible (remote proxy is based on db name)
   29554                 :     ** zFilename remains valid until file is closed, to support */
   29555                 :     pNew->lockingContext = (void*)zFilename;
   29556                 : #endif
   29557                 :   }
   29558                 : 
   29559           11040 :   if( pLockingStyle == &posixIoMethods
   29560                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   29561                 :     || pLockingStyle == &nfsIoMethods
   29562                 : #endif
   29563                 :   ){
   29564            3052 :     unixEnterMutex();
   29565            3052 :     rc = findInodeInfo(pNew, &pNew->pInode);
   29566            3052 :     if( rc!=SQLITE_OK ){
   29567                 :       /* If an error occured in findInodeInfo(), close the file descriptor
   29568                 :       ** immediately, before releasing the mutex. findInodeInfo() may fail
   29569                 :       ** in two scenarios:
   29570                 :       **
   29571                 :       **   (a) A call to fstat() failed.
   29572                 :       **   (b) A malloc failed.
   29573                 :       **
   29574                 :       ** Scenario (b) may only occur if the process is holding no other
   29575                 :       ** file descriptors open on the same file. If there were other file
   29576                 :       ** descriptors on this file, then no malloc would be required by
   29577                 :       ** findInodeInfo(). If this is the case, it is quite safe to close
   29578                 :       ** handle h - as it is guaranteed that no posix locks will be released
   29579                 :       ** by doing so.
   29580                 :       **
   29581                 :       ** If scenario (a) caused the error then things are not so safe. The
   29582                 :       ** implicit assumption here is that if fstat() fails, things are in
   29583                 :       ** such bad shape that dropping a lock or two doesn't matter much.
   29584                 :       */
   29585               0 :       robust_close(pNew, h, __LINE__);
   29586               0 :       h = -1;
   29587                 :     }
   29588            3052 :     unixLeaveMutex();
   29589                 :   }
   29590                 : 
   29591                 : #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   29592                 :   else if( pLockingStyle == &afpIoMethods ){
   29593                 :     /* AFP locking uses the file path so it needs to be included in
   29594                 :     ** the afpLockingContext.
   29595                 :     */
   29596                 :     afpLockingContext *pCtx;
   29597                 :     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
   29598                 :     if( pCtx==0 ){
   29599                 :       rc = SQLITE_NOMEM;
   29600                 :     }else{
   29601                 :       /* NB: zFilename exists and remains valid until the file is closed
   29602                 :       ** according to requirement F11141.  So we do not need to make a
   29603                 :       ** copy of the filename. */
   29604                 :       pCtx->dbPath = zFilename;
   29605                 :       pCtx->reserved = 0;
   29606                 :       srandomdev();
   29607                 :       unixEnterMutex();
   29608                 :       rc = findInodeInfo(pNew, &pNew->pInode);
   29609                 :       if( rc!=SQLITE_OK ){
   29610                 :         sqlite3_free(pNew->lockingContext);
   29611                 :         robust_close(pNew, h, __LINE__);
   29612                 :         h = -1;
   29613                 :       }
   29614                 :       unixLeaveMutex();        
   29615                 :     }
   29616                 :   }
   29617                 : #endif
   29618                 : 
   29619            7988 :   else if( pLockingStyle == &dotlockIoMethods ){
   29620                 :     /* Dotfile locking uses the file path so it needs to be included in
   29621                 :     ** the dotlockLockingContext 
   29622                 :     */
   29623                 :     char *zLockFile;
   29624                 :     int nFilename;
   29625               0 :     assert( zFilename!=0 );
   29626               0 :     nFilename = (int)strlen(zFilename) + 6;
   29627               0 :     zLockFile = (char *)sqlite3_malloc(nFilename);
   29628               0 :     if( zLockFile==0 ){
   29629               0 :       rc = SQLITE_NOMEM;
   29630                 :     }else{
   29631               0 :       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
   29632                 :     }
   29633               0 :     pNew->lockingContext = zLockFile;
   29634                 :   }
   29635                 : 
   29636                 : #if OS_VXWORKS
   29637                 :   else if( pLockingStyle == &semIoMethods ){
   29638                 :     /* Named semaphore locking uses the file path so it needs to be
   29639                 :     ** included in the semLockingContext
   29640                 :     */
   29641                 :     unixEnterMutex();
   29642                 :     rc = findInodeInfo(pNew, &pNew->pInode);
   29643                 :     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
   29644                 :       char *zSemName = pNew->pInode->aSemName;
   29645                 :       int n;
   29646                 :       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
   29647                 :                        pNew->pId->zCanonicalName);
   29648                 :       for( n=1; zSemName[n]; n++ )
   29649                 :         if( zSemName[n]=='/' ) zSemName[n] = '_';
   29650                 :       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
   29651                 :       if( pNew->pInode->pSem == SEM_FAILED ){
   29652                 :         rc = SQLITE_NOMEM;
   29653                 :         pNew->pInode->aSemName[0] = '\0';
   29654                 :       }
   29655                 :     }
   29656                 :     unixLeaveMutex();
   29657                 :   }
   29658                 : #endif
   29659                 :   
   29660           11040 :   pNew->lastErrno = 0;
   29661                 : #if OS_VXWORKS
   29662                 :   if( rc!=SQLITE_OK ){
   29663                 :     if( h>=0 ) robust_close(pNew, h, __LINE__);
   29664                 :     h = -1;
   29665                 :     osUnlink(zFilename);
   29666                 :     isDelete = 0;
   29667                 :   }
   29668                 :   if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
   29669                 : #endif
   29670           11040 :   if( rc!=SQLITE_OK ){
   29671               0 :     if( h>=0 ) robust_close(pNew, h, __LINE__);
   29672                 :   }else{
   29673           11040 :     pNew->pMethod = pLockingStyle;
   29674                 :     OpenCounter(+1);
   29675                 :   }
   29676           11040 :   return rc;
   29677                 : }
   29678                 : 
   29679                 : /*
   29680                 : ** Return the name of a directory in which to put temporary files.
   29681                 : ** If no suitable temporary file directory can be found, return NULL.
   29682                 : */
   29683             411 : static const char *unixTempFileDir(void){
   29684                 :   static const char *azDirs[] = {
   29685                 :      0,
   29686                 :      0,
   29687                 :      "/var/tmp",
   29688                 :      "/usr/tmp",
   29689                 :      "/tmp",
   29690                 :      0        /* List terminator */
   29691                 :   };
   29692                 :   unsigned int i;
   29693                 :   struct stat buf;
   29694             411 :   const char *zDir = 0;
   29695                 : 
   29696             411 :   azDirs[0] = sqlite3_temp_directory;
   29697             411 :   if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
   29698            1644 :   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
   29699            1644 :     if( zDir==0 ) continue;
   29700             411 :     if( osStat(zDir, &buf) ) continue;
   29701             411 :     if( !S_ISDIR(buf.st_mode) ) continue;
   29702             411 :     if( osAccess(zDir, 07) ) continue;
   29703             411 :     break;
   29704                 :   }
   29705             411 :   return zDir;
   29706                 : }
   29707                 : 
   29708                 : /*
   29709                 : ** Create a temporary file name in zBuf.  zBuf must be allocated
   29710                 : ** by the calling process and must be big enough to hold at least
   29711                 : ** pVfs->mxPathname bytes.
   29712                 : */
   29713             411 : static int unixGetTempname(int nBuf, char *zBuf){
   29714                 :   static const unsigned char zChars[] =
   29715                 :     "abcdefghijklmnopqrstuvwxyz"
   29716                 :     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   29717                 :     "0123456789";
   29718                 :   unsigned int i, j;
   29719                 :   const char *zDir;
   29720                 : 
   29721                 :   /* It's odd to simulate an io-error here, but really this is just
   29722                 :   ** using the io-error infrastructure to test that SQLite handles this
   29723                 :   ** function failing. 
   29724                 :   */
   29725                 :   SimulateIOError( return SQLITE_IOERR );
   29726                 : 
   29727             411 :   zDir = unixTempFileDir();
   29728             411 :   if( zDir==0 ) zDir = ".";
   29729                 : 
   29730                 :   /* Check that the output buffer is large enough for the temporary file 
   29731                 :   ** name. If it is not, return SQLITE_ERROR.
   29732                 :   */
   29733             411 :   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
   29734               0 :     return SQLITE_ERROR;
   29735                 :   }
   29736                 : 
   29737                 :   do{
   29738             411 :     sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
   29739             411 :     j = (int)strlen(zBuf);
   29740             411 :     sqlite3_randomness(15, &zBuf[j]);
   29741            6576 :     for(i=0; i<15; i++, j++){
   29742            6165 :       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   29743                 :     }
   29744             411 :     zBuf[j] = 0;
   29745             411 :     zBuf[j+1] = 0;
   29746             411 :   }while( osAccess(zBuf,0)==0 );
   29747             411 :   return SQLITE_OK;
   29748                 : }
   29749                 : 
   29750                 : #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   29751                 : /*
   29752                 : ** Routine to transform a unixFile into a proxy-locking unixFile.
   29753                 : ** Implementation in the proxy-lock division, but used by unixOpen()
   29754                 : ** if SQLITE_PREFER_PROXY_LOCKING is defined.
   29755                 : */
   29756                 : static int proxyTransformUnixFile(unixFile*, const char*);
   29757                 : #endif
   29758                 : 
   29759                 : /*
   29760                 : ** Search for an unused file descriptor that was opened on the database 
   29761                 : ** file (not a journal or master-journal file) identified by pathname
   29762                 : ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
   29763                 : ** argument to this function.
   29764                 : **
   29765                 : ** Such a file descriptor may exist if a database connection was closed
   29766                 : ** but the associated file descriptor could not be closed because some
   29767                 : ** other file descriptor open on the same file is holding a file-lock.
   29768                 : ** Refer to comments in the unixClose() function and the lengthy comment
   29769                 : ** describing "Posix Advisory Locking" at the start of this file for 
   29770                 : ** further details. Also, ticket #4018.
   29771                 : **
   29772                 : ** If a suitable file descriptor is found, then it is returned. If no
   29773                 : ** such file descriptor is located, -1 is returned.
   29774                 : */
   29775            3056 : static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
   29776            3056 :   UnixUnusedFd *pUnused = 0;
   29777                 : 
   29778                 :   /* Do not search for an unused file descriptor on vxworks. Not because
   29779                 :   ** vxworks would not benefit from the change (it might, we're not sure),
   29780                 :   ** but because no way to test it is currently available. It is better 
   29781                 :   ** not to risk breaking vxworks support for the sake of such an obscure 
   29782                 :   ** feature.  */
   29783                 : #if !OS_VXWORKS
   29784                 :   struct stat sStat;                   /* Results of stat() call */
   29785                 : 
   29786                 :   /* A stat() call may fail for various reasons. If this happens, it is
   29787                 :   ** almost certain that an open() call on the same path will also fail.
   29788                 :   ** For this reason, if an error occurs in the stat() call here, it is
   29789                 :   ** ignored and -1 is returned. The caller will try to open a new file
   29790                 :   ** descriptor on the same path, fail, and return an error to SQLite.
   29791                 :   **
   29792                 :   ** Even if a subsequent open() call does succeed, the consequences of
   29793                 :   ** not searching for a resusable file descriptor are not dire.  */
   29794            3056 :   if( 0==osStat(zPath, &sStat) ){
   29795                 :     unixInodeInfo *pInode;
   29796                 : 
   29797            1666 :     unixEnterMutex();
   29798            1666 :     pInode = inodeList;
   29799            5097 :     while( pInode && (pInode->fileId.dev!=sStat.st_dev
   29800            1867 :                      || pInode->fileId.ino!=sStat.st_ino) ){
   29801            1765 :        pInode = pInode->pNext;
   29802                 :     }
   29803            1666 :     if( pInode ){
   29804                 :       UnixUnusedFd **pp;
   29805             102 :       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
   29806             102 :       pUnused = *pp;
   29807             102 :       if( pUnused ){
   29808               2 :         *pp = pUnused->pNext;
   29809                 :       }
   29810                 :     }
   29811            1666 :     unixLeaveMutex();
   29812                 :   }
   29813                 : #endif    /* if !OS_VXWORKS */
   29814            3056 :   return pUnused;
   29815                 : }
   29816                 : 
   29817                 : /*
   29818                 : ** This function is called by unixOpen() to determine the unix permissions
   29819                 : ** to create new files with. If no error occurs, then SQLITE_OK is returned
   29820                 : ** and a value suitable for passing as the third argument to open(2) is
   29821                 : ** written to *pMode. If an IO error occurs, an SQLite error code is 
   29822                 : ** returned and the value of *pMode is not modified.
   29823                 : **
   29824                 : ** If the file being opened is a temporary file, it is always created with
   29825                 : ** the octal permissions 0600 (read/writable by owner only). If the file
   29826                 : ** is a database or master journal file, it is created with the permissions 
   29827                 : ** mask SQLITE_DEFAULT_FILE_PERMISSIONS.
   29828                 : **
   29829                 : ** Finally, if the file being opened is a WAL or regular journal file, then 
   29830                 : ** this function queries the file-system for the permissions on the 
   29831                 : ** corresponding database file and sets *pMode to this value. Whenever 
   29832                 : ** possible, WAL and journal files are created using the same permissions 
   29833                 : ** as the associated database file.
   29834                 : **
   29835                 : ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
   29836                 : ** original filename is unavailable.  But 8_3_NAMES is only used for
   29837                 : ** FAT filesystems and permissions do not matter there, so just use
   29838                 : ** the default permissions.
   29839                 : */
   29840           11042 : static int findCreateFileMode(
   29841                 :   const char *zPath,              /* Path of file (possibly) being created */
   29842                 :   int flags,                      /* Flags passed as 4th argument to xOpen() */
   29843                 :   mode_t *pMode                   /* OUT: Permissions to open file with */
   29844                 : ){
   29845           11042 :   int rc = SQLITE_OK;             /* Return Code */
   29846           11042 :   *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS;
   29847           11042 :   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
   29848                 :     char zDb[MAX_PATHNAME+1];     /* Database file path */
   29849                 :     int nDb;                      /* Number of valid bytes in zDb */
   29850                 :     struct stat sStat;            /* Output of stat() on database file */
   29851                 : 
   29852                 :     /* zPath is a path to a WAL or journal file. The following block derives
   29853                 :     ** the path to the associated database file from zPath. This block handles
   29854                 :     ** the following naming conventions:
   29855                 :     **
   29856                 :     **   "<path to db>-journal"
   29857                 :     **   "<path to db>-wal"
   29858                 :     **   "<path to db>-journalNN"
   29859                 :     **   "<path to db>-walNN"
   29860                 :     **
   29861                 :     ** where NN is a decimal number. The NN naming schemes are 
   29862                 :     ** used by the test_multiplex.c module.
   29863                 :     */
   29864            7577 :     nDb = sqlite3Strlen30(zPath) - 1; 
   29865                 : #ifdef SQLITE_ENABLE_8_3_NAMES
   29866                 :     while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
   29867                 :     if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
   29868                 : #else
   29869           66489 :     while( zPath[nDb]!='-' ){
   29870           51335 :       assert( nDb>0 );
   29871           51335 :       assert( zPath[nDb]!='\n' );
   29872           51335 :       nDb--;
   29873                 :     }
   29874                 : #endif
   29875            7577 :     memcpy(zDb, zPath, nDb);
   29876            7577 :     zDb[nDb] = '\0';
   29877                 : 
   29878            7577 :     if( 0==osStat(zDb, &sStat) ){
   29879            7577 :       *pMode = sStat.st_mode & 0777;
   29880                 :     }else{
   29881               0 :       rc = SQLITE_IOERR_FSTAT;
   29882                 :     }
   29883            3465 :   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
   29884             411 :     *pMode = 0600;
   29885                 :   }
   29886           11042 :   return rc;
   29887                 : }
   29888                 : 
   29889                 : /*
   29890                 : ** Open the file zPath.
   29891                 : ** 
   29892                 : ** Previously, the SQLite OS layer used three functions in place of this
   29893                 : ** one:
   29894                 : **
   29895                 : **     sqlite3OsOpenReadWrite();
   29896                 : **     sqlite3OsOpenReadOnly();
   29897                 : **     sqlite3OsOpenExclusive();
   29898                 : **
   29899                 : ** These calls correspond to the following combinations of flags:
   29900                 : **
   29901                 : **     ReadWrite() ->     (READWRITE | CREATE)
   29902                 : **     ReadOnly()  ->     (READONLY) 
   29903                 : **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
   29904                 : **
   29905                 : ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
   29906                 : ** true, the file was configured to be automatically deleted when the
   29907                 : ** file handle closed. To achieve the same effect using this new 
   29908                 : ** interface, add the DELETEONCLOSE flag to those specified above for 
   29909                 : ** OpenExclusive().
   29910                 : */
   29911           11044 : static int unixOpen(
   29912                 :   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
   29913                 :   const char *zPath,           /* Pathname of file to be opened */
   29914                 :   sqlite3_file *pFile,         /* The file descriptor to be filled in */
   29915                 :   int flags,                   /* Input flags to control the opening */
   29916                 :   int *pOutFlags               /* Output flags returned to SQLite core */
   29917                 : ){
   29918           11044 :   unixFile *p = (unixFile *)pFile;
   29919           11044 :   int fd = -1;                   /* File descriptor returned by open() */
   29920           11044 :   int openFlags = 0;             /* Flags to pass to open() */
   29921           11044 :   int eType = flags&0xFFFFFF00;  /* Type of file to open */
   29922                 :   int noLock;                    /* True to omit locking primitives */
   29923           11044 :   int rc = SQLITE_OK;            /* Function Return Code */
   29924           11044 :   int ctrlFlags = 0;             /* UNIXFILE_* flags */
   29925                 : 
   29926           11044 :   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
   29927           11044 :   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
   29928           11044 :   int isCreate     = (flags & SQLITE_OPEN_CREATE);
   29929           11044 :   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
   29930           11044 :   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
   29931                 : #if SQLITE_ENABLE_LOCKING_STYLE
   29932                 :   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
   29933                 : #endif
   29934                 : #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
   29935                 :   struct statfs fsInfo;
   29936                 : #endif
   29937                 : 
   29938                 :   /* If creating a master or main-file journal, this function will open
   29939                 :   ** a file-descriptor on the directory too. The first time unixSync()
   29940                 :   ** is called the directory file descriptor will be fsync()ed and close()d.
   29941                 :   */
   29942           18621 :   int syncDir = (isCreate && (
   29943                 :         eType==SQLITE_OPEN_MASTER_JOURNAL 
   29944           11008 :      || eType==SQLITE_OPEN_MAIN_JOURNAL 
   29945            3857 :      || eType==SQLITE_OPEN_WAL
   29946                 :   ));
   29947                 : 
   29948                 :   /* If argument zPath is a NULL pointer, this function is required to open
   29949                 :   ** a temporary file. Use this buffer to store the file name in.
   29950                 :   */
   29951                 :   char zTmpname[MAX_PATHNAME+2];
   29952           11044 :   const char *zName = zPath;
   29953                 : 
   29954                 :   /* Check the following statements are true: 
   29955                 :   **
   29956                 :   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
   29957                 :   **   (b) if CREATE is set, then READWRITE must also be set, and
   29958                 :   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
   29959                 :   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
   29960                 :   */
   29961           11044 :   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
   29962           11044 :   assert(isCreate==0 || isReadWrite);
   29963           11044 :   assert(isExclusive==0 || isCreate);
   29964           11044 :   assert(isDelete==0 || isCreate);
   29965                 : 
   29966                 :   /* The main DB, main journal, WAL file and master journal are never 
   29967                 :   ** automatically deleted. Nor are they ever temporary files.  */
   29968           11044 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
   29969           11044 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
   29970           11044 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
   29971           11044 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
   29972                 : 
   29973                 :   /* Assert that the upper layer has set one of the "file-type" flags. */
   29974           11044 :   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
   29975                 :        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
   29976                 :        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
   29977                 :        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
   29978                 :   );
   29979                 : 
   29980           11044 :   memset(p, 0, sizeof(unixFile));
   29981                 : 
   29982           11044 :   if( eType==SQLITE_OPEN_MAIN_DB ){
   29983                 :     UnixUnusedFd *pUnused;
   29984            3056 :     pUnused = findReusableFd(zName, flags);
   29985            3056 :     if( pUnused ){
   29986               2 :       fd = pUnused->fd;
   29987                 :     }else{
   29988            3054 :       pUnused = sqlite3_malloc(sizeof(*pUnused));
   29989            3054 :       if( !pUnused ){
   29990               0 :         return SQLITE_NOMEM;
   29991                 :       }
   29992                 :     }
   29993            3056 :     p->pUnused = pUnused;
   29994                 : 
   29995                 :     /* Database filenames are double-zero terminated if they are not
   29996                 :     ** URIs with parameters.  Hence, they can always be passed into
   29997                 :     ** sqlite3_uri_parameter(). */
   29998            3056 :     assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
   29999                 : 
   30000            7988 :   }else if( !zName ){
   30001                 :     /* If zName is NULL, the upper layer is requesting a temp file. */
   30002             411 :     assert(isDelete && !syncDir);
   30003             411 :     rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
   30004             411 :     if( rc!=SQLITE_OK ){
   30005               0 :       return rc;
   30006                 :     }
   30007             411 :     zName = zTmpname;
   30008                 : 
   30009                 :     /* Generated temporary filenames are always double-zero terminated
   30010                 :     ** for use by sqlite3_uri_parameter(). */
   30011             411 :     assert( zName[strlen(zName)+1]==0 );
   30012                 :   }
   30013                 : 
   30014                 :   /* Determine the value of the flags parameter passed to POSIX function
   30015                 :   ** open(). These must be calculated even if open() is not called, as
   30016                 :   ** they may be stored as part of the file handle and used by the 
   30017                 :   ** 'conch file' locking functions later on.  */
   30018           11044 :   if( isReadonly )  openFlags |= O_RDONLY;
   30019           11044 :   if( isReadWrite ) openFlags |= O_RDWR;
   30020           11044 :   if( isCreate )    openFlags |= O_CREAT;
   30021           11044 :   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
   30022           11044 :   openFlags |= (O_LARGEFILE|O_BINARY);
   30023                 : 
   30024           11044 :   if( fd<0 ){
   30025                 :     mode_t openMode;              /* Permissions to create file with */
   30026           11042 :     rc = findCreateFileMode(zName, flags, &openMode);
   30027           11042 :     if( rc!=SQLITE_OK ){
   30028               0 :       assert( !p->pUnused );
   30029               0 :       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
   30030               0 :       return rc;
   30031                 :     }
   30032           11042 :     fd = robust_open(zName, openFlags, openMode);
   30033                 :     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
   30034           11042 :     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
   30035                 :       /* Failed to open the file for read/write access. Try read-only. */
   30036               0 :       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
   30037               0 :       openFlags &= ~(O_RDWR|O_CREAT);
   30038               0 :       flags |= SQLITE_OPEN_READONLY;
   30039               0 :       openFlags |= O_RDONLY;
   30040               0 :       isReadonly = 1;
   30041               0 :       fd = robust_open(zName, openFlags, openMode);
   30042                 :     }
   30043           11042 :     if( fd<0 ){
   30044               4 :       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
   30045               4 :       goto open_finished;
   30046                 :     }
   30047                 :   }
   30048           11040 :   assert( fd>=0 );
   30049           11040 :   if( pOutFlags ){
   30050            3478 :     *pOutFlags = flags;
   30051                 :   }
   30052                 : 
   30053           11040 :   if( p->pUnused ){
   30054            3052 :     p->pUnused->fd = fd;
   30055            3052 :     p->pUnused->flags = flags;
   30056                 :   }
   30057                 : 
   30058           11040 :   if( isDelete ){
   30059                 : #if OS_VXWORKS
   30060                 :     zPath = zName;
   30061                 : #else
   30062             411 :     osUnlink(zName);
   30063                 : #endif
   30064                 :   }
   30065                 : #if SQLITE_ENABLE_LOCKING_STYLE
   30066                 :   else{
   30067                 :     p->openFlags = openFlags;
   30068                 :   }
   30069                 : #endif
   30070                 : 
   30071                 : #ifdef FD_CLOEXEC
   30072           11040 :   osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
   30073                 : #endif
   30074                 : 
   30075           11040 :   noLock = eType!=SQLITE_OPEN_MAIN_DB;
   30076                 : 
   30077                 :   
   30078                 : #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
   30079                 :   if( fstatfs(fd, &fsInfo) == -1 ){
   30080                 :     ((unixFile*)pFile)->lastErrno = errno;
   30081                 :     robust_close(p, fd, __LINE__);
   30082                 :     return SQLITE_IOERR_ACCESS;
   30083                 :   }
   30084                 :   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
   30085                 :     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
   30086                 :   }
   30087                 : #endif
   30088                 : 
   30089                 :   /* Set up appropriate ctrlFlags */
   30090           11040 :   if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
   30091           11040 :   if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
   30092           11040 :   if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
   30093           11040 :   if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
   30094           11040 :   if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
   30095                 : 
   30096                 : #if SQLITE_ENABLE_LOCKING_STYLE
   30097                 : #if SQLITE_PREFER_PROXY_LOCKING
   30098                 :   isAutoProxy = 1;
   30099                 : #endif
   30100                 :   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
   30101                 :     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
   30102                 :     int useProxy = 0;
   30103                 : 
   30104                 :     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
   30105                 :     ** never use proxy, NULL means use proxy for non-local files only.  */
   30106                 :     if( envforce!=NULL ){
   30107                 :       useProxy = atoi(envforce)>0;
   30108                 :     }else{
   30109                 :       if( statfs(zPath, &fsInfo) == -1 ){
   30110                 :         /* In theory, the close(fd) call is sub-optimal. If the file opened
   30111                 :         ** with fd is a database file, and there are other connections open
   30112                 :         ** on that file that are currently holding advisory locks on it,
   30113                 :         ** then the call to close() will cancel those locks. In practice,
   30114                 :         ** we're assuming that statfs() doesn't fail very often. At least
   30115                 :         ** not while other file descriptors opened by the same process on
   30116                 :         ** the same file are working.  */
   30117                 :         p->lastErrno = errno;
   30118                 :         robust_close(p, fd, __LINE__);
   30119                 :         rc = SQLITE_IOERR_ACCESS;
   30120                 :         goto open_finished;
   30121                 :       }
   30122                 :       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
   30123                 :     }
   30124                 :     if( useProxy ){
   30125                 :       rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
   30126                 :       if( rc==SQLITE_OK ){
   30127                 :         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
   30128                 :         if( rc!=SQLITE_OK ){
   30129                 :           /* Use unixClose to clean up the resources added in fillInUnixFile 
   30130                 :           ** and clear all the structure's references.  Specifically, 
   30131                 :           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op 
   30132                 :           */
   30133                 :           unixClose(pFile);
   30134                 :           return rc;
   30135                 :         }
   30136                 :       }
   30137                 :       goto open_finished;
   30138                 :     }
   30139                 :   }
   30140                 : #endif
   30141                 :   
   30142           11040 :   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
   30143                 : 
   30144                 : open_finished:
   30145           11044 :   if( rc!=SQLITE_OK ){
   30146               4 :     sqlite3_free(p->pUnused);
   30147                 :   }
   30148           11044 :   return rc;
   30149                 : }
   30150                 : 
   30151                 : 
   30152                 : /*
   30153                 : ** Delete the file at zPath. If the dirSync argument is true, fsync()
   30154                 : ** the directory after deleting the file.
   30155                 : */
   30156           10941 : static int unixDelete(
   30157                 :   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
   30158                 :   const char *zPath,        /* Name of file to be deleted */
   30159                 :   int dirSync               /* If true, fsync() directory after deleting file */
   30160                 : ){
   30161           10941 :   int rc = SQLITE_OK;
   30162                 :   UNUSED_PARAMETER(NotUsed);
   30163                 :   SimulateIOError(return SQLITE_IOERR_DELETE);
   30164           10941 :   if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
   30165               0 :     return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
   30166                 :   }
   30167                 : #ifndef SQLITE_DISABLE_DIRSYNC
   30168           10941 :   if( (dirSync & 1)!=0 ){
   30169                 :     int fd;
   30170               0 :     rc = osOpenDirectory(zPath, &fd);
   30171               0 :     if( rc==SQLITE_OK ){
   30172                 : #if OS_VXWORKS
   30173                 :       if( fsync(fd)==-1 )
   30174                 : #else
   30175               0 :       if( fsync(fd) )
   30176                 : #endif
   30177                 :       {
   30178               0 :         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
   30179                 :       }
   30180               0 :       robust_close(0, fd, __LINE__);
   30181               0 :     }else if( rc==SQLITE_CANTOPEN ){
   30182               0 :       rc = SQLITE_OK;
   30183                 :     }
   30184                 :   }
   30185                 : #endif
   30186           10941 :   return rc;
   30187                 : }
   30188                 : 
   30189                 : /*
   30190                 : ** Test the existance of or access permissions of file zPath. The
   30191                 : ** test performed depends on the value of flags:
   30192                 : **
   30193                 : **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
   30194                 : **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
   30195                 : **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
   30196                 : **
   30197                 : ** Otherwise return 0.
   30198                 : */
   30199           28865 : static int unixAccess(
   30200                 :   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
   30201                 :   const char *zPath,      /* Path of the file to examine */
   30202                 :   int flags,              /* What do we want to learn about the zPath file? */
   30203                 :   int *pResOut            /* Write result boolean here */
   30204                 : ){
   30205           28865 :   int amode = 0;
   30206                 :   UNUSED_PARAMETER(NotUsed);
   30207                 :   SimulateIOError( return SQLITE_IOERR_ACCESS; );
   30208           28865 :   switch( flags ){
   30209                 :     case SQLITE_ACCESS_EXISTS:
   30210           28865 :       amode = F_OK;
   30211           28865 :       break;
   30212                 :     case SQLITE_ACCESS_READWRITE:
   30213               0 :       amode = W_OK|R_OK;
   30214               0 :       break;
   30215                 :     case SQLITE_ACCESS_READ:
   30216               0 :       amode = R_OK;
   30217               0 :       break;
   30218                 : 
   30219                 :     default:
   30220               0 :       assert(!"Invalid flags argument");
   30221                 :   }
   30222           28865 :   *pResOut = (osAccess(zPath, amode)==0);
   30223           28865 :   if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
   30224                 :     struct stat buf;
   30225              73 :     if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
   30226              40 :       *pResOut = 0;
   30227                 :     }
   30228                 :   }
   30229           28865 :   return SQLITE_OK;
   30230                 : }
   30231                 : 
   30232                 : 
   30233                 : /*
   30234                 : ** Turn a relative pathname into a full pathname. The relative path
   30235                 : ** is stored as a nul-terminated string in the buffer pointed to by
   30236                 : ** zPath. 
   30237                 : **
   30238                 : ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes 
   30239                 : ** (in this case, MAX_PATHNAME bytes). The full-path is written to
   30240                 : ** this buffer before returning.
   30241                 : */
   30242            4512 : static int unixFullPathname(
   30243                 :   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
   30244                 :   const char *zPath,            /* Possibly relative input path */
   30245                 :   int nOut,                     /* Size of output buffer in bytes */
   30246                 :   char *zOut                    /* Output buffer */
   30247                 : ){
   30248                 : 
   30249                 :   /* It's odd to simulate an io-error here, but really this is just
   30250                 :   ** using the io-error infrastructure to test that SQLite handles this
   30251                 :   ** function failing. This function could fail if, for example, the
   30252                 :   ** current working directory has been unlinked.
   30253                 :   */
   30254                 :   SimulateIOError( return SQLITE_ERROR );
   30255                 : 
   30256            4512 :   assert( pVfs->mxPathname==MAX_PATHNAME );
   30257                 :   UNUSED_PARAMETER(pVfs);
   30258                 : 
   30259            4512 :   zOut[nOut-1] = '\0';
   30260            4512 :   if( zPath[0]=='/' ){
   30261            4512 :     sqlite3_snprintf(nOut, zOut, "%s", zPath);
   30262                 :   }else{
   30263                 :     int nCwd;
   30264               0 :     if( osGetcwd(zOut, nOut-1)==0 ){
   30265               0 :       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
   30266                 :     }
   30267               0 :     nCwd = (int)strlen(zOut);
   30268               0 :     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
   30269                 :   }
   30270            4512 :   return SQLITE_OK;
   30271                 : }
   30272                 : 
   30273                 : 
   30274                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
   30275                 : /*
   30276                 : ** Interfaces for opening a shared library, finding entry points
   30277                 : ** within the shared library, and closing the shared library.
   30278                 : */
   30279                 : #include <dlfcn.h>
   30280               0 : static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
   30281                 :   UNUSED_PARAMETER(NotUsed);
   30282               0 :   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
   30283                 : }
   30284                 : 
   30285                 : /*
   30286                 : ** SQLite calls this function immediately after a call to unixDlSym() or
   30287                 : ** unixDlOpen() fails (returns a null pointer). If a more detailed error
   30288                 : ** message is available, it is written to zBufOut. If no error message
   30289                 : ** is available, zBufOut is left unmodified and SQLite uses a default
   30290                 : ** error message.
   30291                 : */
   30292               0 : static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
   30293                 :   const char *zErr;
   30294                 :   UNUSED_PARAMETER(NotUsed);
   30295               0 :   unixEnterMutex();
   30296               0 :   zErr = dlerror();
   30297               0 :   if( zErr ){
   30298               0 :     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
   30299                 :   }
   30300               0 :   unixLeaveMutex();
   30301               0 : }
   30302               0 : static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
   30303                 :   /* 
   30304                 :   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
   30305                 :   ** cast into a pointer to a function.  And yet the library dlsym() routine
   30306                 :   ** returns a void* which is really a pointer to a function.  So how do we
   30307                 :   ** use dlsym() with -pedantic-errors?
   30308                 :   **
   30309                 :   ** Variable x below is defined to be a pointer to a function taking
   30310                 :   ** parameters void* and const char* and returning a pointer to a function.
   30311                 :   ** We initialize x by assigning it a pointer to the dlsym() function.
   30312                 :   ** (That assignment requires a cast.)  Then we call the function that
   30313                 :   ** x points to.  
   30314                 :   **
   30315                 :   ** This work-around is unlikely to work correctly on any system where
   30316                 :   ** you really cannot cast a function pointer into void*.  But then, on the
   30317                 :   ** other hand, dlsym() will not work on such a system either, so we have
   30318                 :   ** not really lost anything.
   30319                 :   */
   30320                 :   void (*(*x)(void*,const char*))(void);
   30321                 :   UNUSED_PARAMETER(NotUsed);
   30322               0 :   x = (void(*(*)(void*,const char*))(void))dlsym;
   30323               0 :   return (*x)(p, zSym);
   30324                 : }
   30325               0 : static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
   30326                 :   UNUSED_PARAMETER(NotUsed);
   30327               0 :   dlclose(pHandle);
   30328               0 : }
   30329                 : #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
   30330                 :   #define unixDlOpen  0
   30331                 :   #define unixDlError 0
   30332                 :   #define unixDlSym   0
   30333                 :   #define unixDlClose 0
   30334                 : #endif
   30335                 : 
   30336                 : /*
   30337                 : ** Write nBuf bytes of random data to the supplied buffer zBuf.
   30338                 : */
   30339             689 : static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
   30340                 :   UNUSED_PARAMETER(NotUsed);
   30341             689 :   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
   30342                 : 
   30343                 :   /* We have to initialize zBuf to prevent valgrind from reporting
   30344                 :   ** errors.  The reports issued by valgrind are incorrect - we would
   30345                 :   ** prefer that the randomness be increased by making use of the
   30346                 :   ** uninitialized space in zBuf - but valgrind errors tend to worry
   30347                 :   ** some users.  Rather than argue, it seems easier just to initialize
   30348                 :   ** the whole array and silence valgrind, even if that means less randomness
   30349                 :   ** in the random seed.
   30350                 :   **
   30351                 :   ** When testing, initializing zBuf[] to zero is all we do.  That means
   30352                 :   ** that we always use the same random number sequence.  This makes the
   30353                 :   ** tests repeatable.
   30354                 :   */
   30355             689 :   memset(zBuf, 0, nBuf);
   30356                 : #if !defined(SQLITE_TEST)
   30357                 :   {
   30358                 :     int pid, fd;
   30359             689 :     fd = robust_open("/dev/urandom", O_RDONLY, 0);
   30360             689 :     if( fd<0 ){
   30361                 :       time_t t;
   30362               0 :       time(&t);
   30363               0 :       memcpy(zBuf, &t, sizeof(t));
   30364               0 :       pid = getpid();
   30365               0 :       memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
   30366               0 :       assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf );
   30367               0 :       nBuf = sizeof(t) + sizeof(pid);
   30368                 :     }else{
   30369             689 :       do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR );
   30370             689 :       robust_close(0, fd, __LINE__);
   30371                 :     }
   30372                 :   }
   30373                 : #endif
   30374             689 :   return nBuf;
   30375                 : }
   30376                 : 
   30377                 : 
   30378                 : /*
   30379                 : ** Sleep for a little while.  Return the amount of time slept.
   30380                 : ** The argument is the number of microseconds we want to sleep.
   30381                 : ** The return value is the number of microseconds of sleep actually
   30382                 : ** requested from the underlying operating system, a number which
   30383                 : ** might be greater than or equal to the argument, but not less
   30384                 : ** than the argument.
   30385                 : */
   30386               0 : static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
   30387                 : #if OS_VXWORKS
   30388                 :   struct timespec sp;
   30389                 : 
   30390                 :   sp.tv_sec = microseconds / 1000000;
   30391                 :   sp.tv_nsec = (microseconds % 1000000) * 1000;
   30392                 :   nanosleep(&sp, NULL);
   30393                 :   UNUSED_PARAMETER(NotUsed);
   30394                 :   return microseconds;
   30395                 : #elif defined(HAVE_USLEEP) && HAVE_USLEEP
   30396                 :   usleep(microseconds);
   30397                 :   UNUSED_PARAMETER(NotUsed);
   30398                 :   return microseconds;
   30399                 : #else
   30400               0 :   int seconds = (microseconds+999999)/1000000;
   30401               0 :   sleep(seconds);
   30402                 :   UNUSED_PARAMETER(NotUsed);
   30403               0 :   return seconds*1000000;
   30404                 : #endif
   30405                 : }
   30406                 : 
   30407                 : /*
   30408                 : ** The following variable, if set to a non-zero value, is interpreted as
   30409                 : ** the number of seconds since 1970 and is used to set the result of
   30410                 : ** sqlite3OsCurrentTime() during testing.
   30411                 : */
   30412                 : #ifdef SQLITE_TEST
   30413                 : SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
   30414                 : #endif
   30415                 : 
   30416                 : /*
   30417                 : ** Find the current time (in Universal Coordinated Time).  Write into *piNow
   30418                 : ** the current time and date as a Julian Day number times 86_400_000.  In
   30419                 : ** other words, write into *piNow the number of milliseconds since the Julian
   30420                 : ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
   30421                 : ** proleptic Gregorian calendar.
   30422                 : **
   30423                 : ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
   30424                 : ** cannot be found.
   30425                 : */
   30426           10655 : static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
   30427                 :   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
   30428           10655 :   int rc = SQLITE_OK;
   30429                 : #if defined(NO_GETTOD)
   30430                 :   time_t t;
   30431                 :   time(&t);
   30432                 :   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
   30433                 : #elif OS_VXWORKS
   30434                 :   struct timespec sNow;
   30435                 :   clock_gettime(CLOCK_REALTIME, &sNow);
   30436                 :   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
   30437                 : #else
   30438                 :   struct timeval sNow;
   30439           10655 :   if( gettimeofday(&sNow, 0)==0 ){
   30440           10655 :     *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
   30441                 :   }else{
   30442               0 :     rc = SQLITE_ERROR;
   30443                 :   }
   30444                 : #endif
   30445                 : 
   30446                 : #ifdef SQLITE_TEST
   30447                 :   if( sqlite3_current_time ){
   30448                 :     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
   30449                 :   }
   30450                 : #endif
   30451                 :   UNUSED_PARAMETER(NotUsed);
   30452           10655 :   return rc;
   30453                 : }
   30454                 : 
   30455                 : /*
   30456                 : ** Find the current time (in Universal Coordinated Time).  Write the
   30457                 : ** current time and date as a Julian Day number into *prNow and
   30458                 : ** return 0.  Return 1 if the time and date cannot be found.
   30459                 : */
   30460               0 : static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
   30461               0 :   sqlite3_int64 i = 0;
   30462                 :   int rc;
   30463                 :   UNUSED_PARAMETER(NotUsed);
   30464               0 :   rc = unixCurrentTimeInt64(0, &i);
   30465               0 :   *prNow = i/86400000.0;
   30466               0 :   return rc;
   30467                 : }
   30468                 : 
   30469                 : /*
   30470                 : ** We added the xGetLastError() method with the intention of providing
   30471                 : ** better low-level error messages when operating-system problems come up
   30472                 : ** during SQLite operation.  But so far, none of that has been implemented
   30473                 : ** in the core.  So this routine is never called.  For now, it is merely
   30474                 : ** a place-holder.
   30475                 : */
   30476               0 : static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
   30477                 :   UNUSED_PARAMETER(NotUsed);
   30478                 :   UNUSED_PARAMETER(NotUsed2);
   30479                 :   UNUSED_PARAMETER(NotUsed3);
   30480               0 :   return 0;
   30481                 : }
   30482                 : 
   30483                 : 
   30484                 : /*
   30485                 : ************************ End of sqlite3_vfs methods ***************************
   30486                 : ******************************************************************************/
   30487                 : 
   30488                 : /******************************************************************************
   30489                 : ************************** Begin Proxy Locking ********************************
   30490                 : **
   30491                 : ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
   30492                 : ** other locking methods on secondary lock files.  Proxy locking is a
   30493                 : ** meta-layer over top of the primitive locking implemented above.  For
   30494                 : ** this reason, the division that implements of proxy locking is deferred
   30495                 : ** until late in the file (here) after all of the other I/O methods have
   30496                 : ** been defined - so that the primitive locking methods are available
   30497                 : ** as services to help with the implementation of proxy locking.
   30498                 : **
   30499                 : ****
   30500                 : **
   30501                 : ** The default locking schemes in SQLite use byte-range locks on the
   30502                 : ** database file to coordinate safe, concurrent access by multiple readers
   30503                 : ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
   30504                 : ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
   30505                 : ** as POSIX read & write locks over fixed set of locations (via fsctl),
   30506                 : ** on AFP and SMB only exclusive byte-range locks are available via fsctl
   30507                 : ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
   30508                 : ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
   30509                 : ** address in the shared range is taken for a SHARED lock, the entire
   30510                 : ** shared range is taken for an EXCLUSIVE lock):
   30511                 : **
   30512                 : **      PENDING_BYTE        0x40000000                  
   30513                 : **      RESERVED_BYTE       0x40000001
   30514                 : **      SHARED_RANGE        0x40000002 -> 0x40000200
   30515                 : **
   30516                 : ** This works well on the local file system, but shows a nearly 100x
   30517                 : ** slowdown in read performance on AFP because the AFP client disables
   30518                 : ** the read cache when byte-range locks are present.  Enabling the read
   30519                 : ** cache exposes a cache coherency problem that is present on all OS X
   30520                 : ** supported network file systems.  NFS and AFP both observe the
   30521                 : ** close-to-open semantics for ensuring cache coherency
   30522                 : ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
   30523                 : ** address the requirements for concurrent database access by multiple
   30524                 : ** readers and writers
   30525                 : ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
   30526                 : **
   30527                 : ** To address the performance and cache coherency issues, proxy file locking
   30528                 : ** changes the way database access is controlled by limiting access to a
   30529                 : ** single host at a time and moving file locks off of the database file
   30530                 : ** and onto a proxy file on the local file system.  
   30531                 : **
   30532                 : **
   30533                 : ** Using proxy locks
   30534                 : ** -----------------
   30535                 : **
   30536                 : ** C APIs
   30537                 : **
   30538                 : **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
   30539                 : **                       <proxy_path> | ":auto:");
   30540                 : **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
   30541                 : **
   30542                 : **
   30543                 : ** SQL pragmas
   30544                 : **
   30545                 : **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
   30546                 : **  PRAGMA [database.]lock_proxy_file
   30547                 : **
   30548                 : ** Specifying ":auto:" means that if there is a conch file with a matching
   30549                 : ** host ID in it, the proxy path in the conch file will be used, otherwise
   30550                 : ** a proxy path based on the user's temp dir
   30551                 : ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
   30552                 : ** actual proxy file name is generated from the name and path of the
   30553                 : ** database file.  For example:
   30554                 : **
   30555                 : **       For database path "/Users/me/foo.db" 
   30556                 : **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
   30557                 : **
   30558                 : ** Once a lock proxy is configured for a database connection, it can not
   30559                 : ** be removed, however it may be switched to a different proxy path via
   30560                 : ** the above APIs (assuming the conch file is not being held by another
   30561                 : ** connection or process). 
   30562                 : **
   30563                 : **
   30564                 : ** How proxy locking works
   30565                 : ** -----------------------
   30566                 : **
   30567                 : ** Proxy file locking relies primarily on two new supporting files: 
   30568                 : **
   30569                 : **   *  conch file to limit access to the database file to a single host
   30570                 : **      at a time
   30571                 : **
   30572                 : **   *  proxy file to act as a proxy for the advisory locks normally
   30573                 : **      taken on the database
   30574                 : **
   30575                 : ** The conch file - to use a proxy file, sqlite must first "hold the conch"
   30576                 : ** by taking an sqlite-style shared lock on the conch file, reading the
   30577                 : ** contents and comparing the host's unique host ID (see below) and lock
   30578                 : ** proxy path against the values stored in the conch.  The conch file is
   30579                 : ** stored in the same directory as the database file and the file name
   30580                 : ** is patterned after the database file name as ".<databasename>-conch".
   30581                 : ** If the conch file does not exist, or it's contents do not match the
   30582                 : ** host ID and/or proxy path, then the lock is escalated to an exclusive
   30583                 : ** lock and the conch file contents is updated with the host ID and proxy
   30584                 : ** path and the lock is downgraded to a shared lock again.  If the conch
   30585                 : ** is held by another process (with a shared lock), the exclusive lock
   30586                 : ** will fail and SQLITE_BUSY is returned.
   30587                 : **
   30588                 : ** The proxy file - a single-byte file used for all advisory file locks
   30589                 : ** normally taken on the database file.   This allows for safe sharing
   30590                 : ** of the database file for multiple readers and writers on the same
   30591                 : ** host (the conch ensures that they all use the same local lock file).
   30592                 : **
   30593                 : ** Requesting the lock proxy does not immediately take the conch, it is
   30594                 : ** only taken when the first request to lock database file is made.  
   30595                 : ** This matches the semantics of the traditional locking behavior, where
   30596                 : ** opening a connection to a database file does not take a lock on it.
   30597                 : ** The shared lock and an open file descriptor are maintained until 
   30598                 : ** the connection to the database is closed. 
   30599                 : **
   30600                 : ** The proxy file and the lock file are never deleted so they only need
   30601                 : ** to be created the first time they are used.
   30602                 : **
   30603                 : ** Configuration options
   30604                 : ** ---------------------
   30605                 : **
   30606                 : **  SQLITE_PREFER_PROXY_LOCKING
   30607                 : **
   30608                 : **       Database files accessed on non-local file systems are
   30609                 : **       automatically configured for proxy locking, lock files are
   30610                 : **       named automatically using the same logic as
   30611                 : **       PRAGMA lock_proxy_file=":auto:"
   30612                 : **    
   30613                 : **  SQLITE_PROXY_DEBUG
   30614                 : **
   30615                 : **       Enables the logging of error messages during host id file
   30616                 : **       retrieval and creation
   30617                 : **
   30618                 : **  LOCKPROXYDIR
   30619                 : **
   30620                 : **       Overrides the default directory used for lock proxy files that
   30621                 : **       are named automatically via the ":auto:" setting
   30622                 : **
   30623                 : **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
   30624                 : **
   30625                 : **       Permissions to use when creating a directory for storing the
   30626                 : **       lock proxy files, only used when LOCKPROXYDIR is not set.
   30627                 : **    
   30628                 : **    
   30629                 : ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
   30630                 : ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
   30631                 : ** force proxy locking to be used for every database file opened, and 0
   30632                 : ** will force automatic proxy locking to be disabled for all database
   30633                 : ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
   30634                 : ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
   30635                 : */
   30636                 : 
   30637                 : /*
   30638                 : ** Proxy locking is only available on MacOSX 
   30639                 : */
   30640                 : #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
   30641                 : 
   30642                 : /*
   30643                 : ** The proxyLockingContext has the path and file structures for the remote 
   30644                 : ** and local proxy files in it
   30645                 : */
   30646                 : typedef struct proxyLockingContext proxyLockingContext;
   30647                 : struct proxyLockingContext {
   30648                 :   unixFile *conchFile;         /* Open conch file */
   30649                 :   char *conchFilePath;         /* Name of the conch file */
   30650                 :   unixFile *lockProxy;         /* Open proxy lock file */
   30651                 :   char *lockProxyPath;         /* Name of the proxy lock file */
   30652                 :   char *dbPath;                /* Name of the open file */
   30653                 :   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
   30654                 :   void *oldLockingContext;     /* Original lockingcontext to restore on close */
   30655                 :   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
   30656                 : };
   30657                 : 
   30658                 : /* 
   30659                 : ** The proxy lock file path for the database at dbPath is written into lPath, 
   30660                 : ** which must point to valid, writable memory large enough for a maxLen length
   30661                 : ** file path. 
   30662                 : */
   30663                 : static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
   30664                 :   int len;
   30665                 :   int dbLen;
   30666                 :   int i;
   30667                 : 
   30668                 : #ifdef LOCKPROXYDIR
   30669                 :   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
   30670                 : #else
   30671                 : # ifdef _CS_DARWIN_USER_TEMP_DIR
   30672                 :   {
   30673                 :     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
   30674                 :       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
   30675                 :                lPath, errno, getpid()));
   30676                 :       return SQLITE_IOERR_LOCK;
   30677                 :     }
   30678                 :     len = strlcat(lPath, "sqliteplocks", maxLen);    
   30679                 :   }
   30680                 : # else
   30681                 :   len = strlcpy(lPath, "/tmp/", maxLen);
   30682                 : # endif
   30683                 : #endif
   30684                 : 
   30685                 :   if( lPath[len-1]!='/' ){
   30686                 :     len = strlcat(lPath, "/", maxLen);
   30687                 :   }
   30688                 :   
   30689                 :   /* transform the db path to a unique cache name */
   30690                 :   dbLen = (int)strlen(dbPath);
   30691                 :   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
   30692                 :     char c = dbPath[i];
   30693                 :     lPath[i+len] = (c=='/')?'_':c;
   30694                 :   }
   30695                 :   lPath[i+len]='\0';
   30696                 :   strlcat(lPath, ":auto:", maxLen);
   30697                 :   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
   30698                 :   return SQLITE_OK;
   30699                 : }
   30700                 : 
   30701                 : /* 
   30702                 :  ** Creates the lock file and any missing directories in lockPath
   30703                 :  */
   30704                 : static int proxyCreateLockPath(const char *lockPath){
   30705                 :   int i, len;
   30706                 :   char buf[MAXPATHLEN];
   30707                 :   int start = 0;
   30708                 :   
   30709                 :   assert(lockPath!=NULL);
   30710                 :   /* try to create all the intermediate directories */
   30711                 :   len = (int)strlen(lockPath);
   30712                 :   buf[0] = lockPath[0];
   30713                 :   for( i=1; i<len; i++ ){
   30714                 :     if( lockPath[i] == '/' && (i - start > 0) ){
   30715                 :       /* only mkdir if leaf dir != "." or "/" or ".." */
   30716                 :       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') 
   30717                 :          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
   30718                 :         buf[i]='\0';
   30719                 :         if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
   30720                 :           int err=errno;
   30721                 :           if( err!=EEXIST ) {
   30722                 :             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
   30723                 :                      "'%s' proxy lock path=%s pid=%d\n",
   30724                 :                      buf, strerror(err), lockPath, getpid()));
   30725                 :             return err;
   30726                 :           }
   30727                 :         }
   30728                 :       }
   30729                 :       start=i+1;
   30730                 :     }
   30731                 :     buf[i] = lockPath[i];
   30732                 :   }
   30733                 :   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
   30734                 :   return 0;
   30735                 : }
   30736                 : 
   30737                 : /*
   30738                 : ** Create a new VFS file descriptor (stored in memory obtained from
   30739                 : ** sqlite3_malloc) and open the file named "path" in the file descriptor.
   30740                 : **
   30741                 : ** The caller is responsible not only for closing the file descriptor
   30742                 : ** but also for freeing the memory associated with the file descriptor.
   30743                 : */
   30744                 : static int proxyCreateUnixFile(
   30745                 :     const char *path,        /* path for the new unixFile */
   30746                 :     unixFile **ppFile,       /* unixFile created and returned by ref */
   30747                 :     int islockfile           /* if non zero missing dirs will be created */
   30748                 : ) {
   30749                 :   int fd = -1;
   30750                 :   unixFile *pNew;
   30751                 :   int rc = SQLITE_OK;
   30752                 :   int openFlags = O_RDWR | O_CREAT;
   30753                 :   sqlite3_vfs dummyVfs;
   30754                 :   int terrno = 0;
   30755                 :   UnixUnusedFd *pUnused = NULL;
   30756                 : 
   30757                 :   /* 1. first try to open/create the file
   30758                 :   ** 2. if that fails, and this is a lock file (not-conch), try creating
   30759                 :   ** the parent directories and then try again.
   30760                 :   ** 3. if that fails, try to open the file read-only
   30761                 :   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
   30762                 :   */
   30763                 :   pUnused = findReusableFd(path, openFlags);
   30764                 :   if( pUnused ){
   30765                 :     fd = pUnused->fd;
   30766                 :   }else{
   30767                 :     pUnused = sqlite3_malloc(sizeof(*pUnused));
   30768                 :     if( !pUnused ){
   30769                 :       return SQLITE_NOMEM;
   30770                 :     }
   30771                 :   }
   30772                 :   if( fd<0 ){
   30773                 :     fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
   30774                 :     terrno = errno;
   30775                 :     if( fd<0 && errno==ENOENT && islockfile ){
   30776                 :       if( proxyCreateLockPath(path) == SQLITE_OK ){
   30777                 :         fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
   30778                 :       }
   30779                 :     }
   30780                 :   }
   30781                 :   if( fd<0 ){
   30782                 :     openFlags = O_RDONLY;
   30783                 :     fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS);
   30784                 :     terrno = errno;
   30785                 :   }
   30786                 :   if( fd<0 ){
   30787                 :     if( islockfile ){
   30788                 :       return SQLITE_BUSY;
   30789                 :     }
   30790                 :     switch (terrno) {
   30791                 :       case EACCES:
   30792                 :         return SQLITE_PERM;
   30793                 :       case EIO: 
   30794                 :         return SQLITE_IOERR_LOCK; /* even though it is the conch */
   30795                 :       default:
   30796                 :         return SQLITE_CANTOPEN_BKPT;
   30797                 :     }
   30798                 :   }
   30799                 :   
   30800                 :   pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
   30801                 :   if( pNew==NULL ){
   30802                 :     rc = SQLITE_NOMEM;
   30803                 :     goto end_create_proxy;
   30804                 :   }
   30805                 :   memset(pNew, 0, sizeof(unixFile));
   30806                 :   pNew->openFlags = openFlags;
   30807                 :   memset(&dummyVfs, 0, sizeof(dummyVfs));
   30808                 :   dummyVfs.pAppData = (void*)&autolockIoFinder;
   30809                 :   dummyVfs.zName = "dummy";
   30810                 :   pUnused->fd = fd;
   30811                 :   pUnused->flags = openFlags;
   30812                 :   pNew->pUnused = pUnused;
   30813                 :   
   30814                 :   rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
   30815                 :   if( rc==SQLITE_OK ){
   30816                 :     *ppFile = pNew;
   30817                 :     return SQLITE_OK;
   30818                 :   }
   30819                 : end_create_proxy:    
   30820                 :   robust_close(pNew, fd, __LINE__);
   30821                 :   sqlite3_free(pNew);
   30822                 :   sqlite3_free(pUnused);
   30823                 :   return rc;
   30824                 : }
   30825                 : 
   30826                 : #ifdef SQLITE_TEST
   30827                 : /* simulate multiple hosts by creating unique hostid file paths */
   30828                 : SQLITE_API int sqlite3_hostid_num = 0;
   30829                 : #endif
   30830                 : 
   30831                 : #define PROXY_HOSTIDLEN    16  /* conch file host id length */
   30832                 : 
   30833                 : /* Not always defined in the headers as it ought to be */
   30834                 : extern int gethostuuid(uuid_t id, const struct timespec *wait);
   30835                 : 
   30836                 : /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
   30837                 : ** bytes of writable memory.
   30838                 : */
   30839                 : static int proxyGetHostID(unsigned char *pHostID, int *pError){
   30840                 :   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
   30841                 :   memset(pHostID, 0, PROXY_HOSTIDLEN);
   30842                 : #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
   30843                 :                && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
   30844                 :   {
   30845                 :     static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
   30846                 :     if( gethostuuid(pHostID, &timeout) ){
   30847                 :       int err = errno;
   30848                 :       if( pError ){
   30849                 :         *pError = err;
   30850                 :       }
   30851                 :       return SQLITE_IOERR;
   30852                 :     }
   30853                 :   }
   30854                 : #else
   30855                 :   UNUSED_PARAMETER(pError);
   30856                 : #endif
   30857                 : #ifdef SQLITE_TEST
   30858                 :   /* simulate multiple hosts by creating unique hostid file paths */
   30859                 :   if( sqlite3_hostid_num != 0){
   30860                 :     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
   30861                 :   }
   30862                 : #endif
   30863                 :   
   30864                 :   return SQLITE_OK;
   30865                 : }
   30866                 : 
   30867                 : /* The conch file contains the header, host id and lock file path
   30868                 :  */
   30869                 : #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
   30870                 : #define PROXY_HEADERLEN    1   /* conch file header length */
   30871                 : #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
   30872                 : #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
   30873                 : 
   30874                 : /* 
   30875                 : ** Takes an open conch file, copies the contents to a new path and then moves 
   30876                 : ** it back.  The newly created file's file descriptor is assigned to the
   30877                 : ** conch file structure and finally the original conch file descriptor is 
   30878                 : ** closed.  Returns zero if successful.
   30879                 : */
   30880                 : static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
   30881                 :   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
   30882                 :   unixFile *conchFile = pCtx->conchFile;
   30883                 :   char tPath[MAXPATHLEN];
   30884                 :   char buf[PROXY_MAXCONCHLEN];
   30885                 :   char *cPath = pCtx->conchFilePath;
   30886                 :   size_t readLen = 0;
   30887                 :   size_t pathLen = 0;
   30888                 :   char errmsg[64] = "";
   30889                 :   int fd = -1;
   30890                 :   int rc = -1;
   30891                 :   UNUSED_PARAMETER(myHostID);
   30892                 : 
   30893                 :   /* create a new path by replace the trailing '-conch' with '-break' */
   30894                 :   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
   30895                 :   if( pathLen>MAXPATHLEN || pathLen<6 || 
   30896                 :      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
   30897                 :     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
   30898                 :     goto end_breaklock;
   30899                 :   }
   30900                 :   /* read the conch content */
   30901                 :   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
   30902                 :   if( readLen<PROXY_PATHINDEX ){
   30903                 :     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
   30904                 :     goto end_breaklock;
   30905                 :   }
   30906                 :   /* write it out to the temporary break file */
   30907                 :   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL),
   30908                 :                    SQLITE_DEFAULT_FILE_PERMISSIONS);
   30909                 :   if( fd<0 ){
   30910                 :     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
   30911                 :     goto end_breaklock;
   30912                 :   }
   30913                 :   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
   30914                 :     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
   30915                 :     goto end_breaklock;
   30916                 :   }
   30917                 :   if( rename(tPath, cPath) ){
   30918                 :     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
   30919                 :     goto end_breaklock;
   30920                 :   }
   30921                 :   rc = 0;
   30922                 :   fprintf(stderr, "broke stale lock on %s\n", cPath);
   30923                 :   robust_close(pFile, conchFile->h, __LINE__);
   30924                 :   conchFile->h = fd;
   30925                 :   conchFile->openFlags = O_RDWR | O_CREAT;
   30926                 : 
   30927                 : end_breaklock:
   30928                 :   if( rc ){
   30929                 :     if( fd>=0 ){
   30930                 :       osUnlink(tPath);
   30931                 :       robust_close(pFile, fd, __LINE__);
   30932                 :     }
   30933                 :     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
   30934                 :   }
   30935                 :   return rc;
   30936                 : }
   30937                 : 
   30938                 : /* Take the requested lock on the conch file and break a stale lock if the 
   30939                 : ** host id matches.
   30940                 : */
   30941                 : static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
   30942                 :   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
   30943                 :   unixFile *conchFile = pCtx->conchFile;
   30944                 :   int rc = SQLITE_OK;
   30945                 :   int nTries = 0;
   30946                 :   struct timespec conchModTime;
   30947                 :   
   30948                 :   memset(&conchModTime, 0, sizeof(conchModTime));
   30949                 :   do {
   30950                 :     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
   30951                 :     nTries ++;
   30952                 :     if( rc==SQLITE_BUSY ){
   30953                 :       /* If the lock failed (busy):
   30954                 :        * 1st try: get the mod time of the conch, wait 0.5s and try again. 
   30955                 :        * 2nd try: fail if the mod time changed or host id is different, wait 
   30956                 :        *           10 sec and try again
   30957                 :        * 3rd try: break the lock unless the mod time has changed.
   30958                 :        */
   30959                 :       struct stat buf;
   30960                 :       if( osFstat(conchFile->h, &buf) ){
   30961                 :         pFile->lastErrno = errno;
   30962                 :         return SQLITE_IOERR_LOCK;
   30963                 :       }
   30964                 :       
   30965                 :       if( nTries==1 ){
   30966                 :         conchModTime = buf.st_mtimespec;
   30967                 :         usleep(500000); /* wait 0.5 sec and try the lock again*/
   30968                 :         continue;  
   30969                 :       }
   30970                 : 
   30971                 :       assert( nTries>1 );
   30972                 :       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
   30973                 :          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
   30974                 :         return SQLITE_BUSY;
   30975                 :       }
   30976                 :       
   30977                 :       if( nTries==2 ){  
   30978                 :         char tBuf[PROXY_MAXCONCHLEN];
   30979                 :         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
   30980                 :         if( len<0 ){
   30981                 :           pFile->lastErrno = errno;
   30982                 :           return SQLITE_IOERR_LOCK;
   30983                 :         }
   30984                 :         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
   30985                 :           /* don't break the lock if the host id doesn't match */
   30986                 :           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
   30987                 :             return SQLITE_BUSY;
   30988                 :           }
   30989                 :         }else{
   30990                 :           /* don't break the lock on short read or a version mismatch */
   30991                 :           return SQLITE_BUSY;
   30992                 :         }
   30993                 :         usleep(10000000); /* wait 10 sec and try the lock again */
   30994                 :         continue; 
   30995                 :       }
   30996                 :       
   30997                 :       assert( nTries==3 );
   30998                 :       if( 0==proxyBreakConchLock(pFile, myHostID) ){
   30999                 :         rc = SQLITE_OK;
   31000                 :         if( lockType==EXCLUSIVE_LOCK ){
   31001                 :           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);          
   31002                 :         }
   31003                 :         if( !rc ){
   31004                 :           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
   31005                 :         }
   31006                 :       }
   31007                 :     }
   31008                 :   } while( rc==SQLITE_BUSY && nTries<3 );
   31009                 :   
   31010                 :   return rc;
   31011                 : }
   31012                 : 
   31013                 : /* Takes the conch by taking a shared lock and read the contents conch, if 
   31014                 : ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL 
   31015                 : ** lockPath means that the lockPath in the conch file will be used if the 
   31016                 : ** host IDs match, or a new lock path will be generated automatically 
   31017                 : ** and written to the conch file.
   31018                 : */
   31019                 : static int proxyTakeConch(unixFile *pFile){
   31020                 :   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
   31021                 :   
   31022                 :   if( pCtx->conchHeld!=0 ){
   31023                 :     return SQLITE_OK;
   31024                 :   }else{
   31025                 :     unixFile *conchFile = pCtx->conchFile;
   31026                 :     uuid_t myHostID;
   31027                 :     int pError = 0;
   31028                 :     char readBuf[PROXY_MAXCONCHLEN];
   31029                 :     char lockPath[MAXPATHLEN];
   31030                 :     char *tempLockPath = NULL;
   31031                 :     int rc = SQLITE_OK;
   31032                 :     int createConch = 0;
   31033                 :     int hostIdMatch = 0;
   31034                 :     int readLen = 0;
   31035                 :     int tryOldLockPath = 0;
   31036                 :     int forceNewLockPath = 0;
   31037                 :     
   31038                 :     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
   31039                 :              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
   31040                 : 
   31041                 :     rc = proxyGetHostID(myHostID, &pError);
   31042                 :     if( (rc&0xff)==SQLITE_IOERR ){
   31043                 :       pFile->lastErrno = pError;
   31044                 :       goto end_takeconch;
   31045                 :     }
   31046                 :     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
   31047                 :     if( rc!=SQLITE_OK ){
   31048                 :       goto end_takeconch;
   31049                 :     }
   31050                 :     /* read the existing conch file */
   31051                 :     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
   31052                 :     if( readLen<0 ){
   31053                 :       /* I/O error: lastErrno set by seekAndRead */
   31054                 :       pFile->lastErrno = conchFile->lastErrno;
   31055                 :       rc = SQLITE_IOERR_READ;
   31056                 :       goto end_takeconch;
   31057                 :     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || 
   31058                 :              readBuf[0]!=(char)PROXY_CONCHVERSION ){
   31059                 :       /* a short read or version format mismatch means we need to create a new 
   31060                 :       ** conch file. 
   31061                 :       */
   31062                 :       createConch = 1;
   31063                 :     }
   31064                 :     /* if the host id matches and the lock path already exists in the conch
   31065                 :     ** we'll try to use the path there, if we can't open that path, we'll 
   31066                 :     ** retry with a new auto-generated path 
   31067                 :     */
   31068                 :     do { /* in case we need to try again for an :auto: named lock file */
   31069                 : 
   31070                 :       if( !createConch && !forceNewLockPath ){
   31071                 :         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, 
   31072                 :                                   PROXY_HOSTIDLEN);
   31073                 :         /* if the conch has data compare the contents */
   31074                 :         if( !pCtx->lockProxyPath ){
   31075                 :           /* for auto-named local lock file, just check the host ID and we'll
   31076                 :            ** use the local lock file path that's already in there
   31077                 :            */
   31078                 :           if( hostIdMatch ){
   31079                 :             size_t pathLen = (readLen - PROXY_PATHINDEX);
   31080                 :             
   31081                 :             if( pathLen>=MAXPATHLEN ){
   31082                 :               pathLen=MAXPATHLEN-1;
   31083                 :             }
   31084                 :             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
   31085                 :             lockPath[pathLen] = 0;
   31086                 :             tempLockPath = lockPath;
   31087                 :             tryOldLockPath = 1;
   31088                 :             /* create a copy of the lock path if the conch is taken */
   31089                 :             goto end_takeconch;
   31090                 :           }
   31091                 :         }else if( hostIdMatch
   31092                 :                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
   31093                 :                            readLen-PROXY_PATHINDEX)
   31094                 :         ){
   31095                 :           /* conch host and lock path match */
   31096                 :           goto end_takeconch; 
   31097                 :         }
   31098                 :       }
   31099                 :       
   31100                 :       /* if the conch isn't writable and doesn't match, we can't take it */
   31101                 :       if( (conchFile->openFlags&O_RDWR) == 0 ){
   31102                 :         rc = SQLITE_BUSY;
   31103                 :         goto end_takeconch;
   31104                 :       }
   31105                 :       
   31106                 :       /* either the conch didn't match or we need to create a new one */
   31107                 :       if( !pCtx->lockProxyPath ){
   31108                 :         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
   31109                 :         tempLockPath = lockPath;
   31110                 :         /* create a copy of the lock path _only_ if the conch is taken */
   31111                 :       }
   31112                 :       
   31113                 :       /* update conch with host and path (this will fail if other process
   31114                 :       ** has a shared lock already), if the host id matches, use the big
   31115                 :       ** stick.
   31116                 :       */
   31117                 :       futimes(conchFile->h, NULL);
   31118                 :       if( hostIdMatch && !createConch ){
   31119                 :         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
   31120                 :           /* We are trying for an exclusive lock but another thread in this
   31121                 :            ** same process is still holding a shared lock. */
   31122                 :           rc = SQLITE_BUSY;
   31123                 :         } else {          
   31124                 :           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
   31125                 :         }
   31126                 :       }else{
   31127                 :         rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
   31128                 :       }
   31129                 :       if( rc==SQLITE_OK ){
   31130                 :         char writeBuffer[PROXY_MAXCONCHLEN];
   31131                 :         int writeSize = 0;
   31132                 :         
   31133                 :         writeBuffer[0] = (char)PROXY_CONCHVERSION;
   31134                 :         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
   31135                 :         if( pCtx->lockProxyPath!=NULL ){
   31136                 :           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
   31137                 :         }else{
   31138                 :           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
   31139                 :         }
   31140                 :         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
   31141                 :         robust_ftruncate(conchFile->h, writeSize);
   31142                 :         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
   31143                 :         fsync(conchFile->h);
   31144                 :         /* If we created a new conch file (not just updated the contents of a 
   31145                 :          ** valid conch file), try to match the permissions of the database 
   31146                 :          */
   31147                 :         if( rc==SQLITE_OK && createConch ){
   31148                 :           struct stat buf;
   31149                 :           int err = osFstat(pFile->h, &buf);
   31150                 :           if( err==0 ){
   31151                 :             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
   31152                 :                                         S_IROTH|S_IWOTH);
   31153                 :             /* try to match the database file R/W permissions, ignore failure */
   31154                 : #ifndef SQLITE_PROXY_DEBUG
   31155                 :             osFchmod(conchFile->h, cmode);
   31156                 : #else
   31157                 :             do{
   31158                 :               rc = osFchmod(conchFile->h, cmode);
   31159                 :             }while( rc==(-1) && errno==EINTR );
   31160                 :             if( rc!=0 ){
   31161                 :               int code = errno;
   31162                 :               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
   31163                 :                       cmode, code, strerror(code));
   31164                 :             } else {
   31165                 :               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
   31166                 :             }
   31167                 :           }else{
   31168                 :             int code = errno;
   31169                 :             fprintf(stderr, "STAT FAILED[%d] with %d %s\n", 
   31170                 :                     err, code, strerror(code));
   31171                 : #endif
   31172                 :           }
   31173                 :         }
   31174                 :       }
   31175                 :       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
   31176                 :       
   31177                 :     end_takeconch:
   31178                 :       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
   31179                 :       if( rc==SQLITE_OK && pFile->openFlags ){
   31180                 :         int fd;
   31181                 :         if( pFile->h>=0 ){
   31182                 :           robust_close(pFile, pFile->h, __LINE__);
   31183                 :         }
   31184                 :         pFile->h = -1;
   31185                 :         fd = robust_open(pCtx->dbPath, pFile->openFlags,
   31186                 :                       SQLITE_DEFAULT_FILE_PERMISSIONS);
   31187                 :         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
   31188                 :         if( fd>=0 ){
   31189                 :           pFile->h = fd;
   31190                 :         }else{
   31191                 :           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
   31192                 :            during locking */
   31193                 :         }
   31194                 :       }
   31195                 :       if( rc==SQLITE_OK && !pCtx->lockProxy ){
   31196                 :         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
   31197                 :         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
   31198                 :         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
   31199                 :           /* we couldn't create the proxy lock file with the old lock file path
   31200                 :            ** so try again via auto-naming 
   31201                 :            */
   31202                 :           forceNewLockPath = 1;
   31203                 :           tryOldLockPath = 0;
   31204                 :           continue; /* go back to the do {} while start point, try again */
   31205                 :         }
   31206                 :       }
   31207                 :       if( rc==SQLITE_OK ){
   31208                 :         /* Need to make a copy of path if we extracted the value
   31209                 :          ** from the conch file or the path was allocated on the stack
   31210                 :          */
   31211                 :         if( tempLockPath ){
   31212                 :           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
   31213                 :           if( !pCtx->lockProxyPath ){
   31214                 :             rc = SQLITE_NOMEM;
   31215                 :           }
   31216                 :         }
   31217                 :       }
   31218                 :       if( rc==SQLITE_OK ){
   31219                 :         pCtx->conchHeld = 1;
   31220                 :         
   31221                 :         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
   31222                 :           afpLockingContext *afpCtx;
   31223                 :           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
   31224                 :           afpCtx->dbPath = pCtx->lockProxyPath;
   31225                 :         }
   31226                 :       } else {
   31227                 :         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
   31228                 :       }
   31229                 :       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
   31230                 :                rc==SQLITE_OK?"ok":"failed"));
   31231                 :       return rc;
   31232                 :     } while (1); /* in case we need to retry the :auto: lock file - 
   31233                 :                  ** we should never get here except via the 'continue' call. */
   31234                 :   }
   31235                 : }
   31236                 : 
   31237                 : /*
   31238                 : ** If pFile holds a lock on a conch file, then release that lock.
   31239                 : */
   31240                 : static int proxyReleaseConch(unixFile *pFile){
   31241                 :   int rc = SQLITE_OK;         /* Subroutine return code */
   31242                 :   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
   31243                 :   unixFile *conchFile;        /* Name of the conch file */
   31244                 : 
   31245                 :   pCtx = (proxyLockingContext *)pFile->lockingContext;
   31246                 :   conchFile = pCtx->conchFile;
   31247                 :   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
   31248                 :            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), 
   31249                 :            getpid()));
   31250                 :   if( pCtx->conchHeld>0 ){
   31251                 :     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
   31252                 :   }
   31253                 :   pCtx->conchHeld = 0;
   31254                 :   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
   31255                 :            (rc==SQLITE_OK ? "ok" : "failed")));
   31256                 :   return rc;
   31257                 : }
   31258                 : 
   31259                 : /*
   31260                 : ** Given the name of a database file, compute the name of its conch file.
   31261                 : ** Store the conch filename in memory obtained from sqlite3_malloc().
   31262                 : ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
   31263                 : ** or SQLITE_NOMEM if unable to obtain memory.
   31264                 : **
   31265                 : ** The caller is responsible for ensuring that the allocated memory
   31266                 : ** space is eventually freed.
   31267                 : **
   31268                 : ** *pConchPath is set to NULL if a memory allocation error occurs.
   31269                 : */
   31270                 : static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
   31271                 :   int i;                        /* Loop counter */
   31272                 :   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
   31273                 :   char *conchPath;              /* buffer in which to construct conch name */
   31274                 : 
   31275                 :   /* Allocate space for the conch filename and initialize the name to
   31276                 :   ** the name of the original database file. */  
   31277                 :   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
   31278                 :   if( conchPath==0 ){
   31279                 :     return SQLITE_NOMEM;
   31280                 :   }
   31281                 :   memcpy(conchPath, dbPath, len+1);
   31282                 :   
   31283                 :   /* now insert a "." before the last / character */
   31284                 :   for( i=(len-1); i>=0; i-- ){
   31285                 :     if( conchPath[i]=='/' ){
   31286                 :       i++;
   31287                 :       break;
   31288                 :     }
   31289                 :   }
   31290                 :   conchPath[i]='.';
   31291                 :   while ( i<len ){
   31292                 :     conchPath[i+1]=dbPath[i];
   31293                 :     i++;
   31294                 :   }
   31295                 : 
   31296                 :   /* append the "-conch" suffix to the file */
   31297                 :   memcpy(&conchPath[i+1], "-conch", 7);
   31298                 :   assert( (int)strlen(conchPath) == len+7 );
   31299                 : 
   31300                 :   return SQLITE_OK;
   31301                 : }
   31302                 : 
   31303                 : 
   31304                 : /* Takes a fully configured proxy locking-style unix file and switches
   31305                 : ** the local lock file path 
   31306                 : */
   31307                 : static int switchLockProxyPath(unixFile *pFile, const char *path) {
   31308                 :   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
   31309                 :   char *oldPath = pCtx->lockProxyPath;
   31310                 :   int rc = SQLITE_OK;
   31311                 : 
   31312                 :   if( pFile->eFileLock!=NO_LOCK ){
   31313                 :     return SQLITE_BUSY;
   31314                 :   }  
   31315                 : 
   31316                 :   /* nothing to do if the path is NULL, :auto: or matches the existing path */
   31317                 :   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
   31318                 :     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
   31319                 :     return SQLITE_OK;
   31320                 :   }else{
   31321                 :     unixFile *lockProxy = pCtx->lockProxy;
   31322                 :     pCtx->lockProxy=NULL;
   31323                 :     pCtx->conchHeld = 0;
   31324                 :     if( lockProxy!=NULL ){
   31325                 :       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
   31326                 :       if( rc ) return rc;
   31327                 :       sqlite3_free(lockProxy);
   31328                 :     }
   31329                 :     sqlite3_free(oldPath);
   31330                 :     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
   31331                 :   }
   31332                 :   
   31333                 :   return rc;
   31334                 : }
   31335                 : 
   31336                 : /*
   31337                 : ** pFile is a file that has been opened by a prior xOpen call.  dbPath
   31338                 : ** is a string buffer at least MAXPATHLEN+1 characters in size.
   31339                 : **
   31340                 : ** This routine find the filename associated with pFile and writes it
   31341                 : ** int dbPath.
   31342                 : */
   31343                 : static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
   31344                 : #if defined(__APPLE__)
   31345                 :   if( pFile->pMethod == &afpIoMethods ){
   31346                 :     /* afp style keeps a reference to the db path in the filePath field 
   31347                 :     ** of the struct */
   31348                 :     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
   31349                 :     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
   31350                 :   } else
   31351                 : #endif
   31352                 :   if( pFile->pMethod == &dotlockIoMethods ){
   31353                 :     /* dot lock style uses the locking context to store the dot lock
   31354                 :     ** file path */
   31355                 :     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
   31356                 :     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
   31357                 :   }else{
   31358                 :     /* all other styles use the locking context to store the db file path */
   31359                 :     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
   31360                 :     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
   31361                 :   }
   31362                 :   return SQLITE_OK;
   31363                 : }
   31364                 : 
   31365                 : /*
   31366                 : ** Takes an already filled in unix file and alters it so all file locking 
   31367                 : ** will be performed on the local proxy lock file.  The following fields
   31368                 : ** are preserved in the locking context so that they can be restored and 
   31369                 : ** the unix structure properly cleaned up at close time:
   31370                 : **  ->lockingContext
   31371                 : **  ->pMethod
   31372                 : */
   31373                 : static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
   31374                 :   proxyLockingContext *pCtx;
   31375                 :   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
   31376                 :   char *lockPath=NULL;
   31377                 :   int rc = SQLITE_OK;
   31378                 :   
   31379                 :   if( pFile->eFileLock!=NO_LOCK ){
   31380                 :     return SQLITE_BUSY;
   31381                 :   }
   31382                 :   proxyGetDbPathForUnixFile(pFile, dbPath);
   31383                 :   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
   31384                 :     lockPath=NULL;
   31385                 :   }else{
   31386                 :     lockPath=(char *)path;
   31387                 :   }
   31388                 :   
   31389                 :   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
   31390                 :            (lockPath ? lockPath : ":auto:"), getpid()));
   31391                 : 
   31392                 :   pCtx = sqlite3_malloc( sizeof(*pCtx) );
   31393                 :   if( pCtx==0 ){
   31394                 :     return SQLITE_NOMEM;
   31395                 :   }
   31396                 :   memset(pCtx, 0, sizeof(*pCtx));
   31397                 : 
   31398                 :   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
   31399                 :   if( rc==SQLITE_OK ){
   31400                 :     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
   31401                 :     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
   31402                 :       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
   31403                 :       ** (c) the file system is read-only, then enable no-locking access.
   31404                 :       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
   31405                 :       ** that openFlags will have only one of O_RDONLY or O_RDWR.
   31406                 :       */
   31407                 :       struct statfs fsInfo;
   31408                 :       struct stat conchInfo;
   31409                 :       int goLockless = 0;
   31410                 : 
   31411                 :       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
   31412                 :         int err = errno;
   31413                 :         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
   31414                 :           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
   31415                 :         }
   31416                 :       }
   31417                 :       if( goLockless ){
   31418                 :         pCtx->conchHeld = -1; /* read only FS/ lockless */
   31419                 :         rc = SQLITE_OK;
   31420                 :       }
   31421                 :     }
   31422                 :   }  
   31423                 :   if( rc==SQLITE_OK && lockPath ){
   31424                 :     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
   31425                 :   }
   31426                 : 
   31427                 :   if( rc==SQLITE_OK ){
   31428                 :     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
   31429                 :     if( pCtx->dbPath==NULL ){
   31430                 :       rc = SQLITE_NOMEM;
   31431                 :     }
   31432                 :   }
   31433                 :   if( rc==SQLITE_OK ){
   31434                 :     /* all memory is allocated, proxys are created and assigned, 
   31435                 :     ** switch the locking context and pMethod then return.
   31436                 :     */
   31437                 :     pCtx->oldLockingContext = pFile->lockingContext;
   31438                 :     pFile->lockingContext = pCtx;
   31439                 :     pCtx->pOldMethod = pFile->pMethod;
   31440                 :     pFile->pMethod = &proxyIoMethods;
   31441                 :   }else{
   31442                 :     if( pCtx->conchFile ){ 
   31443                 :       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
   31444                 :       sqlite3_free(pCtx->conchFile);
   31445                 :     }
   31446                 :     sqlite3DbFree(0, pCtx->lockProxyPath);
   31447                 :     sqlite3_free(pCtx->conchFilePath); 
   31448                 :     sqlite3_free(pCtx);
   31449                 :   }
   31450                 :   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
   31451                 :            (rc==SQLITE_OK ? "ok" : "failed")));
   31452                 :   return rc;
   31453                 : }
   31454                 : 
   31455                 : 
   31456                 : /*
   31457                 : ** This routine handles sqlite3_file_control() calls that are specific
   31458                 : ** to proxy locking.
   31459                 : */
   31460                 : static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
   31461                 :   switch( op ){
   31462                 :     case SQLITE_GET_LOCKPROXYFILE: {
   31463                 :       unixFile *pFile = (unixFile*)id;
   31464                 :       if( pFile->pMethod == &proxyIoMethods ){
   31465                 :         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
   31466                 :         proxyTakeConch(pFile);
   31467                 :         if( pCtx->lockProxyPath ){
   31468                 :           *(const char **)pArg = pCtx->lockProxyPath;
   31469                 :         }else{
   31470                 :           *(const char **)pArg = ":auto: (not held)";
   31471                 :         }
   31472                 :       } else {
   31473                 :         *(const char **)pArg = NULL;
   31474                 :       }
   31475                 :       return SQLITE_OK;
   31476                 :     }
   31477                 :     case SQLITE_SET_LOCKPROXYFILE: {
   31478                 :       unixFile *pFile = (unixFile*)id;
   31479                 :       int rc = SQLITE_OK;
   31480                 :       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
   31481                 :       if( pArg==NULL || (const char *)pArg==0 ){
   31482                 :         if( isProxyStyle ){
   31483                 :           /* turn off proxy locking - not supported */
   31484                 :           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
   31485                 :         }else{
   31486                 :           /* turn off proxy locking - already off - NOOP */
   31487                 :           rc = SQLITE_OK;
   31488                 :         }
   31489                 :       }else{
   31490                 :         const char *proxyPath = (const char *)pArg;
   31491                 :         if( isProxyStyle ){
   31492                 :           proxyLockingContext *pCtx = 
   31493                 :             (proxyLockingContext*)pFile->lockingContext;
   31494                 :           if( !strcmp(pArg, ":auto:") 
   31495                 :            || (pCtx->lockProxyPath &&
   31496                 :                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
   31497                 :           ){
   31498                 :             rc = SQLITE_OK;
   31499                 :           }else{
   31500                 :             rc = switchLockProxyPath(pFile, proxyPath);
   31501                 :           }
   31502                 :         }else{
   31503                 :           /* turn on proxy file locking */
   31504                 :           rc = proxyTransformUnixFile(pFile, proxyPath);
   31505                 :         }
   31506                 :       }
   31507                 :       return rc;
   31508                 :     }
   31509                 :     default: {
   31510                 :       assert( 0 );  /* The call assures that only valid opcodes are sent */
   31511                 :     }
   31512                 :   }
   31513                 :   /*NOTREACHED*/
   31514                 :   return SQLITE_ERROR;
   31515                 : }
   31516                 : 
   31517                 : /*
   31518                 : ** Within this division (the proxying locking implementation) the procedures
   31519                 : ** above this point are all utilities.  The lock-related methods of the
   31520                 : ** proxy-locking sqlite3_io_method object follow.
   31521                 : */
   31522                 : 
   31523                 : 
   31524                 : /*
   31525                 : ** This routine checks if there is a RESERVED lock held on the specified
   31526                 : ** file by this or any other process. If such a lock is held, set *pResOut
   31527                 : ** to a non-zero value otherwise *pResOut is set to zero.  The return value
   31528                 : ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
   31529                 : */
   31530                 : static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
   31531                 :   unixFile *pFile = (unixFile*)id;
   31532                 :   int rc = proxyTakeConch(pFile);
   31533                 :   if( rc==SQLITE_OK ){
   31534                 :     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   31535                 :     if( pCtx->conchHeld>0 ){
   31536                 :       unixFile *proxy = pCtx->lockProxy;
   31537                 :       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
   31538                 :     }else{ /* conchHeld < 0 is lockless */
   31539                 :       pResOut=0;
   31540                 :     }
   31541                 :   }
   31542                 :   return rc;
   31543                 : }
   31544                 : 
   31545                 : /*
   31546                 : ** Lock the file with the lock specified by parameter eFileLock - one
   31547                 : ** of the following:
   31548                 : **
   31549                 : **     (1) SHARED_LOCK
   31550                 : **     (2) RESERVED_LOCK
   31551                 : **     (3) PENDING_LOCK
   31552                 : **     (4) EXCLUSIVE_LOCK
   31553                 : **
   31554                 : ** Sometimes when requesting one lock state, additional lock states
   31555                 : ** are inserted in between.  The locking might fail on one of the later
   31556                 : ** transitions leaving the lock state different from what it started but
   31557                 : ** still short of its goal.  The following chart shows the allowed
   31558                 : ** transitions and the inserted intermediate states:
   31559                 : **
   31560                 : **    UNLOCKED -> SHARED
   31561                 : **    SHARED -> RESERVED
   31562                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   31563                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   31564                 : **    PENDING -> EXCLUSIVE
   31565                 : **
   31566                 : ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
   31567                 : ** routine to lower a locking level.
   31568                 : */
   31569                 : static int proxyLock(sqlite3_file *id, int eFileLock) {
   31570                 :   unixFile *pFile = (unixFile*)id;
   31571                 :   int rc = proxyTakeConch(pFile);
   31572                 :   if( rc==SQLITE_OK ){
   31573                 :     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   31574                 :     if( pCtx->conchHeld>0 ){
   31575                 :       unixFile *proxy = pCtx->lockProxy;
   31576                 :       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
   31577                 :       pFile->eFileLock = proxy->eFileLock;
   31578                 :     }else{
   31579                 :       /* conchHeld < 0 is lockless */
   31580                 :     }
   31581                 :   }
   31582                 :   return rc;
   31583                 : }
   31584                 : 
   31585                 : 
   31586                 : /*
   31587                 : ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
   31588                 : ** must be either NO_LOCK or SHARED_LOCK.
   31589                 : **
   31590                 : ** If the locking level of the file descriptor is already at or below
   31591                 : ** the requested locking level, this routine is a no-op.
   31592                 : */
   31593                 : static int proxyUnlock(sqlite3_file *id, int eFileLock) {
   31594                 :   unixFile *pFile = (unixFile*)id;
   31595                 :   int rc = proxyTakeConch(pFile);
   31596                 :   if( rc==SQLITE_OK ){
   31597                 :     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   31598                 :     if( pCtx->conchHeld>0 ){
   31599                 :       unixFile *proxy = pCtx->lockProxy;
   31600                 :       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
   31601                 :       pFile->eFileLock = proxy->eFileLock;
   31602                 :     }else{
   31603                 :       /* conchHeld < 0 is lockless */
   31604                 :     }
   31605                 :   }
   31606                 :   return rc;
   31607                 : }
   31608                 : 
   31609                 : /*
   31610                 : ** Close a file that uses proxy locks.
   31611                 : */
   31612                 : static int proxyClose(sqlite3_file *id) {
   31613                 :   if( id ){
   31614                 :     unixFile *pFile = (unixFile*)id;
   31615                 :     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
   31616                 :     unixFile *lockProxy = pCtx->lockProxy;
   31617                 :     unixFile *conchFile = pCtx->conchFile;
   31618                 :     int rc = SQLITE_OK;
   31619                 :     
   31620                 :     if( lockProxy ){
   31621                 :       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
   31622                 :       if( rc ) return rc;
   31623                 :       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
   31624                 :       if( rc ) return rc;
   31625                 :       sqlite3_free(lockProxy);
   31626                 :       pCtx->lockProxy = 0;
   31627                 :     }
   31628                 :     if( conchFile ){
   31629                 :       if( pCtx->conchHeld ){
   31630                 :         rc = proxyReleaseConch(pFile);
   31631                 :         if( rc ) return rc;
   31632                 :       }
   31633                 :       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
   31634                 :       if( rc ) return rc;
   31635                 :       sqlite3_free(conchFile);
   31636                 :     }
   31637                 :     sqlite3DbFree(0, pCtx->lockProxyPath);
   31638                 :     sqlite3_free(pCtx->conchFilePath);
   31639                 :     sqlite3DbFree(0, pCtx->dbPath);
   31640                 :     /* restore the original locking context and pMethod then close it */
   31641                 :     pFile->lockingContext = pCtx->oldLockingContext;
   31642                 :     pFile->pMethod = pCtx->pOldMethod;
   31643                 :     sqlite3_free(pCtx);
   31644                 :     return pFile->pMethod->xClose(id);
   31645                 :   }
   31646                 :   return SQLITE_OK;
   31647                 : }
   31648                 : 
   31649                 : 
   31650                 : 
   31651                 : #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
   31652                 : /*
   31653                 : ** The proxy locking style is intended for use with AFP filesystems.
   31654                 : ** And since AFP is only supported on MacOSX, the proxy locking is also
   31655                 : ** restricted to MacOSX.
   31656                 : ** 
   31657                 : **
   31658                 : ******************* End of the proxy lock implementation **********************
   31659                 : ******************************************************************************/
   31660                 : 
   31661                 : /*
   31662                 : ** Initialize the operating system interface.
   31663                 : **
   31664                 : ** This routine registers all VFS implementations for unix-like operating
   31665                 : ** systems.  This routine, and the sqlite3_os_end() routine that follows,
   31666                 : ** should be the only routines in this file that are visible from other
   31667                 : ** files.
   31668                 : **
   31669                 : ** This routine is called once during SQLite initialization and by a
   31670                 : ** single thread.  The memory allocation and mutex subsystems have not
   31671                 : ** necessarily been initialized when this routine is called, and so they
   31672                 : ** should not be used.
   31673                 : */
   31674             806 : SQLITE_API int sqlite3_os_init(void){ 
   31675                 :   /* 
   31676                 :   ** The following macro defines an initializer for an sqlite3_vfs object.
   31677                 :   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
   31678                 :   ** to the "finder" function.  (pAppData is a pointer to a pointer because
   31679                 :   ** silly C90 rules prohibit a void* from being cast to a function pointer
   31680                 :   ** and so we have to go through the intermediate pointer to avoid problems
   31681                 :   ** when compiling with -pedantic-errors on GCC.)
   31682                 :   **
   31683                 :   ** The FINDER parameter to this macro is the name of the pointer to the
   31684                 :   ** finder-function.  The finder-function returns a pointer to the
   31685                 :   ** sqlite_io_methods object that implements the desired locking
   31686                 :   ** behaviors.  See the division above that contains the IOMETHODS
   31687                 :   ** macro for addition information on finder-functions.
   31688                 :   **
   31689                 :   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
   31690                 :   ** object.  But the "autolockIoFinder" available on MacOSX does a little
   31691                 :   ** more than that; it looks at the filesystem type that hosts the 
   31692                 :   ** database file and tries to choose an locking method appropriate for
   31693                 :   ** that filesystem time.
   31694                 :   */
   31695                 :   #define UNIXVFS(VFSNAME, FINDER) {                        \
   31696                 :     3,                    /* iVersion */                    \
   31697                 :     sizeof(unixFile),     /* szOsFile */                    \
   31698                 :     MAX_PATHNAME,         /* mxPathname */                  \
   31699                 :     0,                    /* pNext */                       \
   31700                 :     VFSNAME,              /* zName */                       \
   31701                 :     (void*)&FINDER,       /* pAppData */                    \
   31702                 :     unixOpen,             /* xOpen */                       \
   31703                 :     unixDelete,           /* xDelete */                     \
   31704                 :     unixAccess,           /* xAccess */                     \
   31705                 :     unixFullPathname,     /* xFullPathname */               \
   31706                 :     unixDlOpen,           /* xDlOpen */                     \
   31707                 :     unixDlError,          /* xDlError */                    \
   31708                 :     unixDlSym,            /* xDlSym */                      \
   31709                 :     unixDlClose,          /* xDlClose */                    \
   31710                 :     unixRandomness,       /* xRandomness */                 \
   31711                 :     unixSleep,            /* xSleep */                      \
   31712                 :     unixCurrentTime,      /* xCurrentTime */                \
   31713                 :     unixGetLastError,     /* xGetLastError */               \
   31714                 :     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
   31715                 :     unixSetSystemCall,    /* xSetSystemCall */              \
   31716                 :     unixGetSystemCall,    /* xGetSystemCall */              \
   31717                 :     unixNextSystemCall,   /* xNextSystemCall */             \
   31718                 :   }
   31719                 : 
   31720                 :   /*
   31721                 :   ** All default VFSes for unix are contained in the following array.
   31722                 :   **
   31723                 :   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
   31724                 :   ** by the SQLite core when the VFS is registered.  So the following
   31725                 :   ** array cannot be const.
   31726                 :   */
   31727                 :   static sqlite3_vfs aVfs[] = {
   31728                 : #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
   31729                 :     UNIXVFS("unix",          autolockIoFinder ),
   31730                 : #else
   31731                 :     UNIXVFS("unix",          posixIoFinder ),
   31732                 : #endif
   31733                 :     UNIXVFS("unix-none",     nolockIoFinder ),
   31734                 :     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
   31735                 :     UNIXVFS("unix-excl",     posixIoFinder ),
   31736                 : #if OS_VXWORKS
   31737                 :     UNIXVFS("unix-namedsem", semIoFinder ),
   31738                 : #endif
   31739                 : #if SQLITE_ENABLE_LOCKING_STYLE
   31740                 :     UNIXVFS("unix-posix",    posixIoFinder ),
   31741                 : #if !OS_VXWORKS
   31742                 :     UNIXVFS("unix-flock",    flockIoFinder ),
   31743                 : #endif
   31744                 : #endif
   31745                 : #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
   31746                 :     UNIXVFS("unix-afp",      afpIoFinder ),
   31747                 :     UNIXVFS("unix-nfs",      nfsIoFinder ),
   31748                 :     UNIXVFS("unix-proxy",    proxyIoFinder ),
   31749                 : #endif
   31750                 :   };
   31751                 :   unsigned int i;          /* Loop counter */
   31752                 : 
   31753                 :   /* Double-check that the aSyscall[] array has been constructed
   31754                 :   ** correctly.  See ticket [bb3a86e890c8e96ab] */
   31755                 :   assert( ArraySize(aSyscall)==20 );
   31756                 : 
   31757                 :   /* Register all VFSes defined in the aVfs[] array */
   31758            4030 :   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
   31759            3224 :     sqlite3_vfs_register(&aVfs[i], i==0);
   31760                 :   }
   31761             806 :   return SQLITE_OK; 
   31762                 : }
   31763                 : 
   31764                 : /*
   31765                 : ** Shutdown the operating system interface.
   31766                 : **
   31767                 : ** Some operating systems might need to do some cleanup in this routine,
   31768                 : ** to release dynamically allocated objects.  But not on unix.
   31769                 : ** This routine is a no-op for unix.
   31770                 : */
   31771             795 : SQLITE_API int sqlite3_os_end(void){ 
   31772             795 :   return SQLITE_OK; 
   31773                 : }
   31774                 :  
   31775                 : #endif /* SQLITE_OS_UNIX */
   31776                 : 
   31777                 : /************** End of os_unix.c *********************************************/
   31778                 : /************** Begin file os_win.c ******************************************/
   31779                 : /*
   31780                 : ** 2004 May 22
   31781                 : **
   31782                 : ** The author disclaims copyright to this source code.  In place of
   31783                 : ** a legal notice, here is a blessing:
   31784                 : **
   31785                 : **    May you do good and not evil.
   31786                 : **    May you find forgiveness for yourself and forgive others.
   31787                 : **    May you share freely, never taking more than you give.
   31788                 : **
   31789                 : ******************************************************************************
   31790                 : **
   31791                 : ** This file contains code that is specific to Windows.
   31792                 : */
   31793                 : #if SQLITE_OS_WIN               /* This file is used for Windows only */
   31794                 : 
   31795                 : #ifdef __CYGWIN__
   31796                 : # include <sys/cygwin.h>
   31797                 : #endif
   31798                 : 
   31799                 : /*
   31800                 : ** Include code that is common to all os_*.c files
   31801                 : */
   31802                 : /************** Include os_common.h in the middle of os_win.c ****************/
   31803                 : /************** Begin file os_common.h ***************************************/
   31804                 : /*
   31805                 : ** 2004 May 22
   31806                 : **
   31807                 : ** The author disclaims copyright to this source code.  In place of
   31808                 : ** a legal notice, here is a blessing:
   31809                 : **
   31810                 : **    May you do good and not evil.
   31811                 : **    May you find forgiveness for yourself and forgive others.
   31812                 : **    May you share freely, never taking more than you give.
   31813                 : **
   31814                 : ******************************************************************************
   31815                 : **
   31816                 : ** This file contains macros and a little bit of code that is common to
   31817                 : ** all of the platform-specific files (os_*.c) and is #included into those
   31818                 : ** files.
   31819                 : **
   31820                 : ** This file should be #included by the os_*.c files only.  It is not a
   31821                 : ** general purpose header file.
   31822                 : */
   31823                 : #ifndef _OS_COMMON_H_
   31824                 : #define _OS_COMMON_H_
   31825                 : 
   31826                 : /*
   31827                 : ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
   31828                 : ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
   31829                 : ** switch.  The following code should catch this problem at compile-time.
   31830                 : */
   31831                 : #ifdef MEMORY_DEBUG
   31832                 : # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
   31833                 : #endif
   31834                 : 
   31835                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   31836                 : # ifndef SQLITE_DEBUG_OS_TRACE
   31837                 : #   define SQLITE_DEBUG_OS_TRACE 0
   31838                 : # endif
   31839                 :   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
   31840                 : # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
   31841                 : #else
   31842                 : # define OSTRACE(X)
   31843                 : #endif
   31844                 : 
   31845                 : /*
   31846                 : ** Macros for performance tracing.  Normally turned off.  Only works
   31847                 : ** on i486 hardware.
   31848                 : */
   31849                 : #ifdef SQLITE_PERFORMANCE_TRACE
   31850                 : 
   31851                 : /* 
   31852                 : ** hwtime.h contains inline assembler code for implementing 
   31853                 : ** high-performance timing routines.
   31854                 : */
   31855                 : /************** Include hwtime.h in the middle of os_common.h ****************/
   31856                 : /************** Begin file hwtime.h ******************************************/
   31857                 : /*
   31858                 : ** 2008 May 27
   31859                 : **
   31860                 : ** The author disclaims copyright to this source code.  In place of
   31861                 : ** a legal notice, here is a blessing:
   31862                 : **
   31863                 : **    May you do good and not evil.
   31864                 : **    May you find forgiveness for yourself and forgive others.
   31865                 : **    May you share freely, never taking more than you give.
   31866                 : **
   31867                 : ******************************************************************************
   31868                 : **
   31869                 : ** This file contains inline asm code for retrieving "high-performance"
   31870                 : ** counters for x86 class CPUs.
   31871                 : */
   31872                 : #ifndef _HWTIME_H_
   31873                 : #define _HWTIME_H_
   31874                 : 
   31875                 : /*
   31876                 : ** The following routine only works on pentium-class (or newer) processors.
   31877                 : ** It uses the RDTSC opcode to read the cycle count value out of the
   31878                 : ** processor and returns that value.  This can be used for high-res
   31879                 : ** profiling.
   31880                 : */
   31881                 : #if (defined(__GNUC__) || defined(_MSC_VER)) && \
   31882                 :       (defined(i386) || defined(__i386__) || defined(_M_IX86))
   31883                 : 
   31884                 :   #if defined(__GNUC__)
   31885                 : 
   31886                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   31887                 :      unsigned int lo, hi;
   31888                 :      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   31889                 :      return (sqlite_uint64)hi << 32 | lo;
   31890                 :   }
   31891                 : 
   31892                 :   #elif defined(_MSC_VER)
   31893                 : 
   31894                 :   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
   31895                 :      __asm {
   31896                 :         rdtsc
   31897                 :         ret       ; return value at EDX:EAX
   31898                 :      }
   31899                 :   }
   31900                 : 
   31901                 :   #endif
   31902                 : 
   31903                 : #elif (defined(__GNUC__) && defined(__x86_64__))
   31904                 : 
   31905                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   31906                 :       unsigned long val;
   31907                 :       __asm__ __volatile__ ("rdtsc" : "=A" (val));
   31908                 :       return val;
   31909                 :   }
   31910                 :  
   31911                 : #elif (defined(__GNUC__) && defined(__ppc__))
   31912                 : 
   31913                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   31914                 :       unsigned long long retval;
   31915                 :       unsigned long junk;
   31916                 :       __asm__ __volatile__ ("\n\
   31917                 :           1:      mftbu   %1\n\
   31918                 :                   mftb    %L0\n\
   31919                 :                   mftbu   %0\n\
   31920                 :                   cmpw    %0,%1\n\
   31921                 :                   bne     1b"
   31922                 :                   : "=r" (retval), "=r" (junk));
   31923                 :       return retval;
   31924                 :   }
   31925                 : 
   31926                 : #else
   31927                 : 
   31928                 :   #error Need implementation of sqlite3Hwtime() for your platform.
   31929                 : 
   31930                 :   /*
   31931                 :   ** To compile without implementing sqlite3Hwtime() for your platform,
   31932                 :   ** you can remove the above #error and use the following
   31933                 :   ** stub function.  You will lose timing support for many
   31934                 :   ** of the debugging and testing utilities, but it should at
   31935                 :   ** least compile and run.
   31936                 :   */
   31937                 : SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
   31938                 : 
   31939                 : #endif
   31940                 : 
   31941                 : #endif /* !defined(_HWTIME_H_) */
   31942                 : 
   31943                 : /************** End of hwtime.h **********************************************/
   31944                 : /************** Continuing where we left off in os_common.h ******************/
   31945                 : 
   31946                 : static sqlite_uint64 g_start;
   31947                 : static sqlite_uint64 g_elapsed;
   31948                 : #define TIMER_START       g_start=sqlite3Hwtime()
   31949                 : #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
   31950                 : #define TIMER_ELAPSED     g_elapsed
   31951                 : #else
   31952                 : #define TIMER_START
   31953                 : #define TIMER_END
   31954                 : #define TIMER_ELAPSED     ((sqlite_uint64)0)
   31955                 : #endif
   31956                 : 
   31957                 : /*
   31958                 : ** If we compile with the SQLITE_TEST macro set, then the following block
   31959                 : ** of code will give us the ability to simulate a disk I/O error.  This
   31960                 : ** is used for testing the I/O recovery logic.
   31961                 : */
   31962                 : #ifdef SQLITE_TEST
   31963                 : SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
   31964                 : SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
   31965                 : SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
   31966                 : SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
   31967                 : SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
   31968                 : SQLITE_API int sqlite3_diskfull_pending = 0;
   31969                 : SQLITE_API int sqlite3_diskfull = 0;
   31970                 : #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
   31971                 : #define SimulateIOError(CODE)  \
   31972                 :   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
   31973                 :        || sqlite3_io_error_pending-- == 1 )  \
   31974                 :               { local_ioerr(); CODE; }
   31975                 : static void local_ioerr(){
   31976                 :   IOTRACE(("IOERR\n"));
   31977                 :   sqlite3_io_error_hit++;
   31978                 :   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
   31979                 : }
   31980                 : #define SimulateDiskfullError(CODE) \
   31981                 :    if( sqlite3_diskfull_pending ){ \
   31982                 :      if( sqlite3_diskfull_pending == 1 ){ \
   31983                 :        local_ioerr(); \
   31984                 :        sqlite3_diskfull = 1; \
   31985                 :        sqlite3_io_error_hit = 1; \
   31986                 :        CODE; \
   31987                 :      }else{ \
   31988                 :        sqlite3_diskfull_pending--; \
   31989                 :      } \
   31990                 :    }
   31991                 : #else
   31992                 : #define SimulateIOErrorBenign(X)
   31993                 : #define SimulateIOError(A)
   31994                 : #define SimulateDiskfullError(A)
   31995                 : #endif
   31996                 : 
   31997                 : /*
   31998                 : ** When testing, keep a count of the number of open files.
   31999                 : */
   32000                 : #ifdef SQLITE_TEST
   32001                 : SQLITE_API int sqlite3_open_file_count = 0;
   32002                 : #define OpenCounter(X)  sqlite3_open_file_count+=(X)
   32003                 : #else
   32004                 : #define OpenCounter(X)
   32005                 : #endif
   32006                 : 
   32007                 : #endif /* !defined(_OS_COMMON_H_) */
   32008                 : 
   32009                 : /************** End of os_common.h *******************************************/
   32010                 : /************** Continuing where we left off in os_win.c *********************/
   32011                 : 
   32012                 : /*
   32013                 : ** Some Microsoft compilers lack this definition.
   32014                 : */
   32015                 : #ifndef INVALID_FILE_ATTRIBUTES
   32016                 : # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
   32017                 : #endif
   32018                 : 
   32019                 : /* Forward references */
   32020                 : typedef struct winShm winShm;           /* A connection to shared-memory */
   32021                 : typedef struct winShmNode winShmNode;   /* A region of shared-memory */
   32022                 : 
   32023                 : /*
   32024                 : ** WinCE lacks native support for file locking so we have to fake it
   32025                 : ** with some code of our own.
   32026                 : */
   32027                 : #if SQLITE_OS_WINCE
   32028                 : typedef struct winceLock {
   32029                 :   int nReaders;       /* Number of reader locks obtained */
   32030                 :   BOOL bPending;      /* Indicates a pending lock has been obtained */
   32031                 :   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
   32032                 :   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
   32033                 : } winceLock;
   32034                 : #endif
   32035                 : 
   32036                 : /*
   32037                 : ** The winFile structure is a subclass of sqlite3_file* specific to the win32
   32038                 : ** portability layer.
   32039                 : */
   32040                 : typedef struct winFile winFile;
   32041                 : struct winFile {
   32042                 :   const sqlite3_io_methods *pMethod; /*** Must be first ***/
   32043                 :   sqlite3_vfs *pVfs;      /* The VFS used to open this file */
   32044                 :   HANDLE h;               /* Handle for accessing the file */
   32045                 :   u8 locktype;            /* Type of lock currently held on this file */
   32046                 :   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
   32047                 :   u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
   32048                 :   DWORD lastErrno;        /* The Windows errno from the last I/O error */
   32049                 :   winShm *pShm;           /* Instance of shared memory on this file */
   32050                 :   const char *zPath;      /* Full pathname of this file */
   32051                 :   int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
   32052                 : #if SQLITE_OS_WINCE
   32053                 :   LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
   32054                 :   HANDLE hMutex;          /* Mutex used to control access to shared lock */  
   32055                 :   HANDLE hShared;         /* Shared memory segment used for locking */
   32056                 :   winceLock local;        /* Locks obtained by this instance of winFile */
   32057                 :   winceLock *shared;      /* Global shared lock memory for the file  */
   32058                 : #endif
   32059                 : };
   32060                 : 
   32061                 : /*
   32062                 : ** Allowed values for winFile.ctrlFlags
   32063                 : */
   32064                 : #define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
   32065                 : #define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
   32066                 : 
   32067                 : /*
   32068                 :  * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
   32069                 :  * various Win32 API heap functions instead of our own.
   32070                 :  */
   32071                 : #ifdef SQLITE_WIN32_MALLOC
   32072                 : /*
   32073                 :  * The initial size of the Win32-specific heap.  This value may be zero.
   32074                 :  */
   32075                 : #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
   32076                 : #  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
   32077                 :                                        (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
   32078                 : #endif
   32079                 : 
   32080                 : /*
   32081                 :  * The maximum size of the Win32-specific heap.  This value may be zero.
   32082                 :  */
   32083                 : #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
   32084                 : #  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
   32085                 : #endif
   32086                 : 
   32087                 : /*
   32088                 :  * The extra flags to use in calls to the Win32 heap APIs.  This value may be
   32089                 :  * zero for the default behavior.
   32090                 :  */
   32091                 : #ifndef SQLITE_WIN32_HEAP_FLAGS
   32092                 : #  define SQLITE_WIN32_HEAP_FLAGS     (0)
   32093                 : #endif
   32094                 : 
   32095                 : /*
   32096                 : ** The winMemData structure stores information required by the Win32-specific
   32097                 : ** sqlite3_mem_methods implementation.
   32098                 : */
   32099                 : typedef struct winMemData winMemData;
   32100                 : struct winMemData {
   32101                 : #ifndef NDEBUG
   32102                 :   u32 magic;    /* Magic number to detect structure corruption. */
   32103                 : #endif
   32104                 :   HANDLE hHeap; /* The handle to our heap. */
   32105                 :   BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
   32106                 : };
   32107                 : 
   32108                 : #ifndef NDEBUG
   32109                 : #define WINMEM_MAGIC     0x42b2830b
   32110                 : #endif
   32111                 : 
   32112                 : static struct winMemData win_mem_data = {
   32113                 : #ifndef NDEBUG
   32114                 :   WINMEM_MAGIC,
   32115                 : #endif
   32116                 :   NULL, FALSE
   32117                 : };
   32118                 : 
   32119                 : #ifndef NDEBUG
   32120                 : #define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
   32121                 : #else
   32122                 : #define winMemAssertMagic()
   32123                 : #endif
   32124                 : 
   32125                 : #define winMemGetHeap() win_mem_data.hHeap
   32126                 : 
   32127                 : static void *winMemMalloc(int nBytes);
   32128                 : static void winMemFree(void *pPrior);
   32129                 : static void *winMemRealloc(void *pPrior, int nBytes);
   32130                 : static int winMemSize(void *p);
   32131                 : static int winMemRoundup(int n);
   32132                 : static int winMemInit(void *pAppData);
   32133                 : static void winMemShutdown(void *pAppData);
   32134                 : 
   32135                 : SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
   32136                 : #endif /* SQLITE_WIN32_MALLOC */
   32137                 : 
   32138                 : /*
   32139                 : ** The following variable is (normally) set once and never changes
   32140                 : ** thereafter.  It records whether the operating system is Win9x
   32141                 : ** or WinNT.
   32142                 : **
   32143                 : ** 0:   Operating system unknown.
   32144                 : ** 1:   Operating system is Win9x.
   32145                 : ** 2:   Operating system is WinNT.
   32146                 : **
   32147                 : ** In order to facilitate testing on a WinNT system, the test fixture
   32148                 : ** can manually set this value to 1 to emulate Win98 behavior.
   32149                 : */
   32150                 : #ifdef SQLITE_TEST
   32151                 : SQLITE_API int sqlite3_os_type = 0;
   32152                 : #else
   32153                 : static int sqlite3_os_type = 0;
   32154                 : #endif
   32155                 : 
   32156                 : /*
   32157                 : ** Many system calls are accessed through pointer-to-functions so that
   32158                 : ** they may be overridden at runtime to facilitate fault injection during
   32159                 : ** testing and sandboxing.  The following array holds the names and pointers
   32160                 : ** to all overrideable system calls.
   32161                 : */
   32162                 : #if !SQLITE_OS_WINCE
   32163                 : #  define SQLITE_WIN32_HAS_ANSI
   32164                 : #endif
   32165                 : 
   32166                 : #if SQLITE_OS_WINCE || SQLITE_OS_WINNT
   32167                 : #  define SQLITE_WIN32_HAS_WIDE
   32168                 : #endif
   32169                 : 
   32170                 : #ifndef SYSCALL
   32171                 : #  define SYSCALL sqlite3_syscall_ptr
   32172                 : #endif
   32173                 : 
   32174                 : #if SQLITE_OS_WINCE
   32175                 : /*
   32176                 : ** These macros are necessary because Windows CE does not natively support the
   32177                 : ** Win32 APIs LockFile, UnlockFile, and LockFileEx.
   32178                 :  */
   32179                 : 
   32180                 : #  define LockFile(a,b,c,d,e)       winceLockFile(&a, b, c, d, e)
   32181                 : #  define UnlockFile(a,b,c,d,e)     winceUnlockFile(&a, b, c, d, e)
   32182                 : #  define LockFileEx(a,b,c,d,e,f)   winceLockFileEx(&a, b, c, d, e, f)
   32183                 : 
   32184                 : /*
   32185                 : ** These are the special syscall hacks for Windows CE.  The locking related
   32186                 : ** defines here refer to the macros defined just above.
   32187                 :  */
   32188                 : 
   32189                 : #  define osAreFileApisANSI()       1
   32190                 : #  define osLockFile                LockFile
   32191                 : #  define osUnlockFile              UnlockFile
   32192                 : #  define osLockFileEx              LockFileEx
   32193                 : #endif
   32194                 : 
   32195                 : static struct win_syscall {
   32196                 :   const char *zName;            /* Name of the sytem call */
   32197                 :   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
   32198                 :   sqlite3_syscall_ptr pDefault; /* Default value */
   32199                 : } aSyscall[] = {
   32200                 : #if !SQLITE_OS_WINCE
   32201                 :   { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
   32202                 : 
   32203                 : #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
   32204                 : #else
   32205                 :   { "AreFileApisANSI",         (SYSCALL)0,                       0 },
   32206                 : #endif
   32207                 : 
   32208                 : #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
   32209                 :   { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
   32210                 : #else
   32211                 :   { "CharLowerW",              (SYSCALL)0,                       0 },
   32212                 : #endif
   32213                 : 
   32214                 : #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
   32215                 : 
   32216                 : #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
   32217                 :   { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
   32218                 : #else
   32219                 :   { "CharUpperW",              (SYSCALL)0,                       0 },
   32220                 : #endif
   32221                 : 
   32222                 : #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
   32223                 : 
   32224                 :   { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
   32225                 : 
   32226                 : #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
   32227                 : 
   32228                 : #if defined(SQLITE_WIN32_HAS_ANSI)
   32229                 :   { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
   32230                 : #else
   32231                 :   { "CreateFileA",             (SYSCALL)0,                       0 },
   32232                 : #endif
   32233                 : 
   32234                 : #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
   32235                 :         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
   32236                 : 
   32237                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32238                 :   { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
   32239                 : #else
   32240                 :   { "CreateFileW",             (SYSCALL)0,                       0 },
   32241                 : #endif
   32242                 : 
   32243                 : #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
   32244                 :         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
   32245                 : 
   32246                 :   { "CreateFileMapping",       (SYSCALL)CreateFileMapping,       0 },
   32247                 : 
   32248                 : #define osCreateFileMapping ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
   32249                 :         DWORD,DWORD,DWORD,LPCTSTR))aSyscall[6].pCurrent)
   32250                 : 
   32251                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32252                 :   { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
   32253                 : #else
   32254                 :   { "CreateFileMappingW",      (SYSCALL)0,                       0 },
   32255                 : #endif
   32256                 : 
   32257                 : #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
   32258                 :         DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
   32259                 : 
   32260                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32261                 :   { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
   32262                 : #else
   32263                 :   { "CreateMutexW",            (SYSCALL)0,                       0 },
   32264                 : #endif
   32265                 : 
   32266                 : #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
   32267                 :         LPCWSTR))aSyscall[8].pCurrent)
   32268                 : 
   32269                 : #if defined(SQLITE_WIN32_HAS_ANSI)
   32270                 :   { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
   32271                 : #else
   32272                 :   { "DeleteFileA",             (SYSCALL)0,                       0 },
   32273                 : #endif
   32274                 : 
   32275                 : #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
   32276                 : 
   32277                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32278                 :   { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
   32279                 : #else
   32280                 :   { "DeleteFileW",             (SYSCALL)0,                       0 },
   32281                 : #endif
   32282                 : 
   32283                 : #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
   32284                 : 
   32285                 : #if SQLITE_OS_WINCE
   32286                 :   { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
   32287                 : #else
   32288                 :   { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
   32289                 : #endif
   32290                 : 
   32291                 : #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
   32292                 :         LPFILETIME))aSyscall[11].pCurrent)
   32293                 : 
   32294                 : #if SQLITE_OS_WINCE
   32295                 :   { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
   32296                 : #else
   32297                 :   { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
   32298                 : #endif
   32299                 : 
   32300                 : #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
   32301                 :         LPSYSTEMTIME))aSyscall[12].pCurrent)
   32302                 : 
   32303                 :   { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
   32304                 : 
   32305                 : #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
   32306                 : 
   32307                 : #if defined(SQLITE_WIN32_HAS_ANSI)
   32308                 :   { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
   32309                 : #else
   32310                 :   { "FormatMessageA",          (SYSCALL)0,                       0 },
   32311                 : #endif
   32312                 : 
   32313                 : #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
   32314                 :         DWORD,va_list*))aSyscall[14].pCurrent)
   32315                 : 
   32316                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32317                 :   { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
   32318                 : #else
   32319                 :   { "FormatMessageW",          (SYSCALL)0,                       0 },
   32320                 : #endif
   32321                 : 
   32322                 : #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
   32323                 :         DWORD,va_list*))aSyscall[15].pCurrent)
   32324                 : 
   32325                 :   { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
   32326                 : 
   32327                 : #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
   32328                 : 
   32329                 :   { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
   32330                 : 
   32331                 : #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
   32332                 : 
   32333                 : #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
   32334                 :   { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
   32335                 : #else
   32336                 :   { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
   32337                 : #endif
   32338                 : 
   32339                 : #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
   32340                 :         LPDWORD))aSyscall[18].pCurrent)
   32341                 : 
   32342                 : #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
   32343                 :   { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
   32344                 : #else
   32345                 :   { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
   32346                 : #endif
   32347                 : 
   32348                 : #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
   32349                 :         LPDWORD))aSyscall[19].pCurrent)
   32350                 : 
   32351                 : #if defined(SQLITE_WIN32_HAS_ANSI)
   32352                 :   { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
   32353                 : #else
   32354                 :   { "GetFileAttributesA",      (SYSCALL)0,                       0 },
   32355                 : #endif
   32356                 : 
   32357                 : #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
   32358                 : 
   32359                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32360                 :   { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
   32361                 : #else
   32362                 :   { "GetFileAttributesW",      (SYSCALL)0,                       0 },
   32363                 : #endif
   32364                 : 
   32365                 : #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
   32366                 : 
   32367                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32368                 :   { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
   32369                 : #else
   32370                 :   { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
   32371                 : #endif
   32372                 : 
   32373                 : #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
   32374                 :         LPVOID))aSyscall[22].pCurrent)
   32375                 : 
   32376                 :   { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
   32377                 : 
   32378                 : #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
   32379                 : 
   32380                 : #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
   32381                 :   { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
   32382                 : #else
   32383                 :   { "GetFullPathNameA",        (SYSCALL)0,                       0 },
   32384                 : #endif
   32385                 : 
   32386                 : #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
   32387                 :         LPSTR*))aSyscall[24].pCurrent)
   32388                 : 
   32389                 : #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
   32390                 :   { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
   32391                 : #else
   32392                 :   { "GetFullPathNameW",        (SYSCALL)0,                       0 },
   32393                 : #endif
   32394                 : 
   32395                 : #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
   32396                 :         LPWSTR*))aSyscall[25].pCurrent)
   32397                 : 
   32398                 :   { "GetLastError",            (SYSCALL)GetLastError,            0 },
   32399                 : 
   32400                 : #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
   32401                 : 
   32402                 : #if SQLITE_OS_WINCE
   32403                 :   /* The GetProcAddressA() routine is only available on Windows CE. */
   32404                 :   { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
   32405                 : #else
   32406                 :   /* All other Windows platforms expect GetProcAddress() to take
   32407                 :   ** an ANSI string regardless of the _UNICODE setting */
   32408                 :   { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
   32409                 : #endif
   32410                 : 
   32411                 : #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
   32412                 :         LPCSTR))aSyscall[27].pCurrent)
   32413                 : 
   32414                 :   { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
   32415                 : 
   32416                 : #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
   32417                 : 
   32418                 :   { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
   32419                 : 
   32420                 : #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
   32421                 : 
   32422                 : #if !SQLITE_OS_WINCE
   32423                 :   { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
   32424                 : #else
   32425                 :   { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
   32426                 : #endif
   32427                 : 
   32428                 : #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
   32429                 :         LPFILETIME))aSyscall[30].pCurrent)
   32430                 : 
   32431                 : #if defined(SQLITE_WIN32_HAS_ANSI)
   32432                 :   { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
   32433                 : #else
   32434                 :   { "GetTempPathA",            (SYSCALL)0,                       0 },
   32435                 : #endif
   32436                 : 
   32437                 : #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
   32438                 : 
   32439                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32440                 :   { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
   32441                 : #else
   32442                 :   { "GetTempPathW",            (SYSCALL)0,                       0 },
   32443                 : #endif
   32444                 : 
   32445                 : #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
   32446                 : 
   32447                 :   { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
   32448                 : 
   32449                 : #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
   32450                 : 
   32451                 : #if defined(SQLITE_WIN32_HAS_ANSI)
   32452                 :   { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
   32453                 : #else
   32454                 :   { "GetVersionExA",           (SYSCALL)0,                       0 },
   32455                 : #endif
   32456                 : 
   32457                 : #define osGetVersionExA ((BOOL(WINAPI*)( \
   32458                 :         LPOSVERSIONINFOA))aSyscall[34].pCurrent)
   32459                 : 
   32460                 :   { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
   32461                 : 
   32462                 : #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
   32463                 :         SIZE_T))aSyscall[35].pCurrent)
   32464                 : 
   32465                 :   { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
   32466                 : 
   32467                 : #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
   32468                 :         SIZE_T))aSyscall[36].pCurrent)
   32469                 : 
   32470                 :   { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
   32471                 : 
   32472                 : #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
   32473                 : 
   32474                 :   { "HeapFree",                (SYSCALL)HeapFree,                0 },
   32475                 : 
   32476                 : #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
   32477                 : 
   32478                 :   { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
   32479                 : 
   32480                 : #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
   32481                 :         SIZE_T))aSyscall[39].pCurrent)
   32482                 : 
   32483                 :   { "HeapSize",                (SYSCALL)HeapSize,                0 },
   32484                 : 
   32485                 : #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
   32486                 :         LPCVOID))aSyscall[40].pCurrent)
   32487                 : 
   32488                 :   { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
   32489                 : 
   32490                 : #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
   32491                 :         LPCVOID))aSyscall[41].pCurrent)
   32492                 : 
   32493                 : #if defined(SQLITE_WIN32_HAS_ANSI)
   32494                 :   { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
   32495                 : #else
   32496                 :   { "LoadLibraryA",            (SYSCALL)0,                       0 },
   32497                 : #endif
   32498                 : 
   32499                 : #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
   32500                 : 
   32501                 : #if defined(SQLITE_WIN32_HAS_WIDE)
   32502                 :   { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
   32503                 : #else
   32504                 :   { "LoadLibraryW",            (SYSCALL)0,                       0 },
   32505                 : #endif
   32506                 : 
   32507                 : #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
   32508                 : 
   32509                 :   { "LocalFree",               (SYSCALL)LocalFree,               0 },
   32510                 : 
   32511                 : #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
   32512                 : 
   32513                 : #if !SQLITE_OS_WINCE
   32514                 :   { "LockFile",                (SYSCALL)LockFile,                0 },
   32515                 : 
   32516                 : #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   32517                 :         DWORD))aSyscall[45].pCurrent)
   32518                 : #else
   32519                 :   { "LockFile",                (SYSCALL)0,                       0 },
   32520                 : #endif
   32521                 : 
   32522                 : #if !SQLITE_OS_WINCE
   32523                 :   { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
   32524                 : 
   32525                 : #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
   32526                 :         LPOVERLAPPED))aSyscall[46].pCurrent)
   32527                 : #else
   32528                 :   { "LockFileEx",              (SYSCALL)0,                       0 },
   32529                 : #endif
   32530                 : 
   32531                 :   { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
   32532                 : 
   32533                 : #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   32534                 :         SIZE_T))aSyscall[47].pCurrent)
   32535                 : 
   32536                 :   { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
   32537                 : 
   32538                 : #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
   32539                 :         int))aSyscall[48].pCurrent)
   32540                 : 
   32541                 :   { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
   32542                 : 
   32543                 : #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
   32544                 :         LARGE_INTEGER*))aSyscall[49].pCurrent)
   32545                 : 
   32546                 :   { "ReadFile",                (SYSCALL)ReadFile,                0 },
   32547                 : 
   32548                 : #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
   32549                 :         LPOVERLAPPED))aSyscall[50].pCurrent)
   32550                 : 
   32551                 :   { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
   32552                 : 
   32553                 : #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
   32554                 : 
   32555                 :   { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
   32556                 : 
   32557                 : #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
   32558                 :         DWORD))aSyscall[52].pCurrent)
   32559                 : 
   32560                 :   { "Sleep",                   (SYSCALL)Sleep,                   0 },
   32561                 : 
   32562                 : #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
   32563                 : 
   32564                 :   { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
   32565                 : 
   32566                 : #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
   32567                 :         LPFILETIME))aSyscall[54].pCurrent)
   32568                 : 
   32569                 : #if !SQLITE_OS_WINCE
   32570                 :   { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
   32571                 : 
   32572                 : #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   32573                 :         DWORD))aSyscall[55].pCurrent)
   32574                 : #else
   32575                 :   { "UnlockFile",              (SYSCALL)0,                       0 },
   32576                 : #endif
   32577                 : 
   32578                 : #if !SQLITE_OS_WINCE
   32579                 :   { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
   32580                 : 
   32581                 : #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   32582                 :         LPOVERLAPPED))aSyscall[56].pCurrent)
   32583                 : #else
   32584                 :   { "UnlockFileEx",            (SYSCALL)0,                       0 },
   32585                 : #endif
   32586                 : 
   32587                 :   { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
   32588                 : 
   32589                 : #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
   32590                 : 
   32591                 :   { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
   32592                 : 
   32593                 : #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
   32594                 :         LPCSTR,LPBOOL))aSyscall[58].pCurrent)
   32595                 : 
   32596                 :   { "WriteFile",               (SYSCALL)WriteFile,               0 },
   32597                 : 
   32598                 : #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
   32599                 :         LPOVERLAPPED))aSyscall[59].pCurrent)
   32600                 : 
   32601                 : }; /* End of the overrideable system calls */
   32602                 : 
   32603                 : /*
   32604                 : ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
   32605                 : ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
   32606                 : ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
   32607                 : ** system call named zName.
   32608                 : */
   32609                 : static int winSetSystemCall(
   32610                 :   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
   32611                 :   const char *zName,            /* Name of system call to override */
   32612                 :   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
   32613                 : ){
   32614                 :   unsigned int i;
   32615                 :   int rc = SQLITE_NOTFOUND;
   32616                 : 
   32617                 :   UNUSED_PARAMETER(pNotUsed);
   32618                 :   if( zName==0 ){
   32619                 :     /* If no zName is given, restore all system calls to their default
   32620                 :     ** settings and return NULL
   32621                 :     */
   32622                 :     rc = SQLITE_OK;
   32623                 :     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   32624                 :       if( aSyscall[i].pDefault ){
   32625                 :         aSyscall[i].pCurrent = aSyscall[i].pDefault;
   32626                 :       }
   32627                 :     }
   32628                 :   }else{
   32629                 :     /* If zName is specified, operate on only the one system call
   32630                 :     ** specified.
   32631                 :     */
   32632                 :     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   32633                 :       if( strcmp(zName, aSyscall[i].zName)==0 ){
   32634                 :         if( aSyscall[i].pDefault==0 ){
   32635                 :           aSyscall[i].pDefault = aSyscall[i].pCurrent;
   32636                 :         }
   32637                 :         rc = SQLITE_OK;
   32638                 :         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
   32639                 :         aSyscall[i].pCurrent = pNewFunc;
   32640                 :         break;
   32641                 :       }
   32642                 :     }
   32643                 :   }
   32644                 :   return rc;
   32645                 : }
   32646                 : 
   32647                 : /*
   32648                 : ** Return the value of a system call.  Return NULL if zName is not a
   32649                 : ** recognized system call name.  NULL is also returned if the system call
   32650                 : ** is currently undefined.
   32651                 : */
   32652                 : static sqlite3_syscall_ptr winGetSystemCall(
   32653                 :   sqlite3_vfs *pNotUsed,
   32654                 :   const char *zName
   32655                 : ){
   32656                 :   unsigned int i;
   32657                 : 
   32658                 :   UNUSED_PARAMETER(pNotUsed);
   32659                 :   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
   32660                 :     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
   32661                 :   }
   32662                 :   return 0;
   32663                 : }
   32664                 : 
   32665                 : /*
   32666                 : ** Return the name of the first system call after zName.  If zName==NULL
   32667                 : ** then return the name of the first system call.  Return NULL if zName
   32668                 : ** is the last system call or if zName is not the name of a valid
   32669                 : ** system call.
   32670                 : */
   32671                 : static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
   32672                 :   int i = -1;
   32673                 : 
   32674                 :   UNUSED_PARAMETER(p);
   32675                 :   if( zName ){
   32676                 :     for(i=0; i<ArraySize(aSyscall)-1; i++){
   32677                 :       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
   32678                 :     }
   32679                 :   }
   32680                 :   for(i++; i<ArraySize(aSyscall); i++){
   32681                 :     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
   32682                 :   }
   32683                 :   return 0;
   32684                 : }
   32685                 : 
   32686                 : /*
   32687                 : ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
   32688                 : ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
   32689                 : **
   32690                 : ** Here is an interesting observation:  Win95, Win98, and WinME lack
   32691                 : ** the LockFileEx() API.  But we can still statically link against that
   32692                 : ** API as long as we don't call it when running Win95/98/ME.  A call to
   32693                 : ** this routine is used to determine if the host is Win95/98/ME or
   32694                 : ** WinNT/2K/XP so that we will know whether or not we can safely call
   32695                 : ** the LockFileEx() API.
   32696                 : */
   32697                 : #if SQLITE_OS_WINCE
   32698                 : # define isNT()  (1)
   32699                 : #else
   32700                 :   static int isNT(void){
   32701                 :     if( sqlite3_os_type==0 ){
   32702                 :       OSVERSIONINFOA sInfo;
   32703                 :       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
   32704                 :       osGetVersionExA(&sInfo);
   32705                 :       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
   32706                 :     }
   32707                 :     return sqlite3_os_type==2;
   32708                 :   }
   32709                 : #endif /* SQLITE_OS_WINCE */
   32710                 : 
   32711                 : #ifdef SQLITE_WIN32_MALLOC
   32712                 : /*
   32713                 : ** Allocate nBytes of memory.
   32714                 : */
   32715                 : static void *winMemMalloc(int nBytes){
   32716                 :   HANDLE hHeap;
   32717                 :   void *p;
   32718                 : 
   32719                 :   winMemAssertMagic();
   32720                 :   hHeap = winMemGetHeap();
   32721                 :   assert( hHeap!=0 );
   32722                 :   assert( hHeap!=INVALID_HANDLE_VALUE );
   32723                 : #ifdef SQLITE_WIN32_MALLOC_VALIDATE
   32724                 :   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
   32725                 : #endif
   32726                 :   assert( nBytes>=0 );
   32727                 :   p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
   32728                 :   if( !p ){
   32729                 :     sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
   32730                 :                 nBytes, osGetLastError(), (void*)hHeap);
   32731                 :   }
   32732                 :   return p;
   32733                 : }
   32734                 : 
   32735                 : /*
   32736                 : ** Free memory.
   32737                 : */
   32738                 : static void winMemFree(void *pPrior){
   32739                 :   HANDLE hHeap;
   32740                 : 
   32741                 :   winMemAssertMagic();
   32742                 :   hHeap = winMemGetHeap();
   32743                 :   assert( hHeap!=0 );
   32744                 :   assert( hHeap!=INVALID_HANDLE_VALUE );
   32745                 : #ifdef SQLITE_WIN32_MALLOC_VALIDATE
   32746                 :   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
   32747                 : #endif
   32748                 :   if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
   32749                 :   if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
   32750                 :     sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
   32751                 :                 pPrior, osGetLastError(), (void*)hHeap);
   32752                 :   }
   32753                 : }
   32754                 : 
   32755                 : /*
   32756                 : ** Change the size of an existing memory allocation
   32757                 : */
   32758                 : static void *winMemRealloc(void *pPrior, int nBytes){
   32759                 :   HANDLE hHeap;
   32760                 :   void *p;
   32761                 : 
   32762                 :   winMemAssertMagic();
   32763                 :   hHeap = winMemGetHeap();
   32764                 :   assert( hHeap!=0 );
   32765                 :   assert( hHeap!=INVALID_HANDLE_VALUE );
   32766                 : #ifdef SQLITE_WIN32_MALLOC_VALIDATE
   32767                 :   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
   32768                 : #endif
   32769                 :   assert( nBytes>=0 );
   32770                 :   if( !pPrior ){
   32771                 :     p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
   32772                 :   }else{
   32773                 :     p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
   32774                 :   }
   32775                 :   if( !p ){
   32776                 :     sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
   32777                 :                 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
   32778                 :                 (void*)hHeap);
   32779                 :   }
   32780                 :   return p;
   32781                 : }
   32782                 : 
   32783                 : /*
   32784                 : ** Return the size of an outstanding allocation, in bytes.
   32785                 : */
   32786                 : static int winMemSize(void *p){
   32787                 :   HANDLE hHeap;
   32788                 :   SIZE_T n;
   32789                 : 
   32790                 :   winMemAssertMagic();
   32791                 :   hHeap = winMemGetHeap();
   32792                 :   assert( hHeap!=0 );
   32793                 :   assert( hHeap!=INVALID_HANDLE_VALUE );
   32794                 : #ifdef SQLITE_WIN32_MALLOC_VALIDATE
   32795                 :   assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
   32796                 : #endif
   32797                 :   if( !p ) return 0;
   32798                 :   n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
   32799                 :   if( n==(SIZE_T)-1 ){
   32800                 :     sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
   32801                 :                 p, osGetLastError(), (void*)hHeap);
   32802                 :     return 0;
   32803                 :   }
   32804                 :   return (int)n;
   32805                 : }
   32806                 : 
   32807                 : /*
   32808                 : ** Round up a request size to the next valid allocation size.
   32809                 : */
   32810                 : static int winMemRoundup(int n){
   32811                 :   return n;
   32812                 : }
   32813                 : 
   32814                 : /*
   32815                 : ** Initialize this module.
   32816                 : */
   32817                 : static int winMemInit(void *pAppData){
   32818                 :   winMemData *pWinMemData = (winMemData *)pAppData;
   32819                 : 
   32820                 :   if( !pWinMemData ) return SQLITE_ERROR;
   32821                 :   assert( pWinMemData->magic==WINMEM_MAGIC );
   32822                 :   if( !pWinMemData->hHeap ){
   32823                 :     pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
   32824                 :                                       SQLITE_WIN32_HEAP_INIT_SIZE,
   32825                 :                                       SQLITE_WIN32_HEAP_MAX_SIZE);
   32826                 :     if( !pWinMemData->hHeap ){
   32827                 :       sqlite3_log(SQLITE_NOMEM,
   32828                 :           "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
   32829                 :           osGetLastError(), SQLITE_WIN32_HEAP_FLAGS,
   32830                 :           SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE);
   32831                 :       return SQLITE_NOMEM;
   32832                 :     }
   32833                 :     pWinMemData->bOwned = TRUE;
   32834                 :   }
   32835                 :   assert( pWinMemData->hHeap!=0 );
   32836                 :   assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
   32837                 : #ifdef SQLITE_WIN32_MALLOC_VALIDATE
   32838                 :   assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
   32839                 : #endif
   32840                 :   return SQLITE_OK;
   32841                 : }
   32842                 : 
   32843                 : /*
   32844                 : ** Deinitialize this module.
   32845                 : */
   32846                 : static void winMemShutdown(void *pAppData){
   32847                 :   winMemData *pWinMemData = (winMemData *)pAppData;
   32848                 : 
   32849                 :   if( !pWinMemData ) return;
   32850                 :   if( pWinMemData->hHeap ){
   32851                 :     assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
   32852                 : #ifdef SQLITE_WIN32_MALLOC_VALIDATE
   32853                 :     assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
   32854                 : #endif
   32855                 :     if( pWinMemData->bOwned ){
   32856                 :       if( !osHeapDestroy(pWinMemData->hHeap) ){
   32857                 :         sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
   32858                 :                     osGetLastError(), (void*)pWinMemData->hHeap);
   32859                 :       }
   32860                 :       pWinMemData->bOwned = FALSE;
   32861                 :     }
   32862                 :     pWinMemData->hHeap = NULL;
   32863                 :   }
   32864                 : }
   32865                 : 
   32866                 : /*
   32867                 : ** Populate the low-level memory allocation function pointers in
   32868                 : ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
   32869                 : ** arguments specify the block of memory to manage.
   32870                 : **
   32871                 : ** This routine is only called by sqlite3_config(), and therefore
   32872                 : ** is not required to be threadsafe (it is not).
   32873                 : */
   32874                 : SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
   32875                 :   static const sqlite3_mem_methods winMemMethods = {
   32876                 :     winMemMalloc,
   32877                 :     winMemFree,
   32878                 :     winMemRealloc,
   32879                 :     winMemSize,
   32880                 :     winMemRoundup,
   32881                 :     winMemInit,
   32882                 :     winMemShutdown,
   32883                 :     &win_mem_data
   32884                 :   };
   32885                 :   return &winMemMethods;
   32886                 : }
   32887                 : 
   32888                 : SQLITE_PRIVATE void sqlite3MemSetDefault(void){
   32889                 :   sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
   32890                 : }
   32891                 : #endif /* SQLITE_WIN32_MALLOC */
   32892                 : 
   32893                 : /*
   32894                 : ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). 
   32895                 : **
   32896                 : ** Space to hold the returned string is obtained from malloc.
   32897                 : */
   32898                 : static LPWSTR utf8ToUnicode(const char *zFilename){
   32899                 :   int nChar;
   32900                 :   LPWSTR zWideFilename;
   32901                 : 
   32902                 :   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
   32903                 :   if( nChar==0 ){
   32904                 :     return 0;
   32905                 :   }
   32906                 :   zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
   32907                 :   if( zWideFilename==0 ){
   32908                 :     return 0;
   32909                 :   }
   32910                 :   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
   32911                 :                                 nChar);
   32912                 :   if( nChar==0 ){
   32913                 :     sqlite3_free(zWideFilename);
   32914                 :     zWideFilename = 0;
   32915                 :   }
   32916                 :   return zWideFilename;
   32917                 : }
   32918                 : 
   32919                 : /*
   32920                 : ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
   32921                 : ** obtained from sqlite3_malloc().
   32922                 : */
   32923                 : static char *unicodeToUtf8(LPCWSTR zWideFilename){
   32924                 :   int nByte;
   32925                 :   char *zFilename;
   32926                 : 
   32927                 :   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
   32928                 :   if( nByte == 0 ){
   32929                 :     return 0;
   32930                 :   }
   32931                 :   zFilename = sqlite3_malloc( nByte );
   32932                 :   if( zFilename==0 ){
   32933                 :     return 0;
   32934                 :   }
   32935                 :   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
   32936                 :                                 0, 0);
   32937                 :   if( nByte == 0 ){
   32938                 :     sqlite3_free(zFilename);
   32939                 :     zFilename = 0;
   32940                 :   }
   32941                 :   return zFilename;
   32942                 : }
   32943                 : 
   32944                 : /*
   32945                 : ** Convert an ANSI string to Microsoft Unicode, based on the
   32946                 : ** current codepage settings for file apis.
   32947                 : ** 
   32948                 : ** Space to hold the returned string is obtained
   32949                 : ** from sqlite3_malloc.
   32950                 : */
   32951                 : static LPWSTR mbcsToUnicode(const char *zFilename){
   32952                 :   int nByte;
   32953                 :   LPWSTR zMbcsFilename;
   32954                 :   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
   32955                 : 
   32956                 :   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
   32957                 :                                 0)*sizeof(WCHAR);
   32958                 :   if( nByte==0 ){
   32959                 :     return 0;
   32960                 :   }
   32961                 :   zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
   32962                 :   if( zMbcsFilename==0 ){
   32963                 :     return 0;
   32964                 :   }
   32965                 :   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
   32966                 :                                 nByte);
   32967                 :   if( nByte==0 ){
   32968                 :     sqlite3_free(zMbcsFilename);
   32969                 :     zMbcsFilename = 0;
   32970                 :   }
   32971                 :   return zMbcsFilename;
   32972                 : }
   32973                 : 
   32974                 : /*
   32975                 : ** Convert Microsoft Unicode to multi-byte character string, based on the
   32976                 : ** user's ANSI codepage.
   32977                 : **
   32978                 : ** Space to hold the returned string is obtained from
   32979                 : ** sqlite3_malloc().
   32980                 : */
   32981                 : static char *unicodeToMbcs(LPCWSTR zWideFilename){
   32982                 :   int nByte;
   32983                 :   char *zFilename;
   32984                 :   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
   32985                 : 
   32986                 :   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
   32987                 :   if( nByte == 0 ){
   32988                 :     return 0;
   32989                 :   }
   32990                 :   zFilename = sqlite3_malloc( nByte );
   32991                 :   if( zFilename==0 ){
   32992                 :     return 0;
   32993                 :   }
   32994                 :   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
   32995                 :                                 nByte, 0, 0);
   32996                 :   if( nByte == 0 ){
   32997                 :     sqlite3_free(zFilename);
   32998                 :     zFilename = 0;
   32999                 :   }
   33000                 :   return zFilename;
   33001                 : }
   33002                 : 
   33003                 : /*
   33004                 : ** Convert multibyte character string to UTF-8.  Space to hold the
   33005                 : ** returned string is obtained from sqlite3_malloc().
   33006                 : */
   33007                 : SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
   33008                 :   char *zFilenameUtf8;
   33009                 :   LPWSTR zTmpWide;
   33010                 : 
   33011                 :   zTmpWide = mbcsToUnicode(zFilename);
   33012                 :   if( zTmpWide==0 ){
   33013                 :     return 0;
   33014                 :   }
   33015                 :   zFilenameUtf8 = unicodeToUtf8(zTmpWide);
   33016                 :   sqlite3_free(zTmpWide);
   33017                 :   return zFilenameUtf8;
   33018                 : }
   33019                 : 
   33020                 : /*
   33021                 : ** Convert UTF-8 to multibyte character string.  Space to hold the 
   33022                 : ** returned string is obtained from sqlite3_malloc().
   33023                 : */
   33024                 : SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
   33025                 :   char *zFilenameMbcs;
   33026                 :   LPWSTR zTmpWide;
   33027                 : 
   33028                 :   zTmpWide = utf8ToUnicode(zFilename);
   33029                 :   if( zTmpWide==0 ){
   33030                 :     return 0;
   33031                 :   }
   33032                 :   zFilenameMbcs = unicodeToMbcs(zTmpWide);
   33033                 :   sqlite3_free(zTmpWide);
   33034                 :   return zFilenameMbcs;
   33035                 : }
   33036                 : 
   33037                 : 
   33038                 : /*
   33039                 : ** The return value of getLastErrorMsg
   33040                 : ** is zero if the error message fits in the buffer, or non-zero
   33041                 : ** otherwise (if the message was truncated).
   33042                 : */
   33043                 : static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
   33044                 :   /* FormatMessage returns 0 on failure.  Otherwise it
   33045                 :   ** returns the number of TCHARs written to the output
   33046                 :   ** buffer, excluding the terminating null char.
   33047                 :   */
   33048                 :   DWORD dwLen = 0;
   33049                 :   char *zOut = 0;
   33050                 : 
   33051                 :   if( isNT() ){
   33052                 :     LPWSTR zTempWide = NULL;
   33053                 :     dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
   33054                 :                              FORMAT_MESSAGE_FROM_SYSTEM |
   33055                 :                              FORMAT_MESSAGE_IGNORE_INSERTS,
   33056                 :                              NULL,
   33057                 :                              lastErrno,
   33058                 :                              0,
   33059                 :                              (LPWSTR) &zTempWide,
   33060                 :                              0,
   33061                 :                              0);
   33062                 :     if( dwLen > 0 ){
   33063                 :       /* allocate a buffer and convert to UTF8 */
   33064                 :       sqlite3BeginBenignMalloc();
   33065                 :       zOut = unicodeToUtf8(zTempWide);
   33066                 :       sqlite3EndBenignMalloc();
   33067                 :       /* free the system buffer allocated by FormatMessage */
   33068                 :       osLocalFree(zTempWide);
   33069                 :     }
   33070                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   33071                 : ** Since the ANSI version of these Windows API do not exist for WINCE,
   33072                 : ** it's important to not reference them for WINCE builds.
   33073                 : */
   33074                 : #if SQLITE_OS_WINCE==0
   33075                 :   }else{
   33076                 :     char *zTemp = NULL;
   33077                 :     dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
   33078                 :                              FORMAT_MESSAGE_FROM_SYSTEM |
   33079                 :                              FORMAT_MESSAGE_IGNORE_INSERTS,
   33080                 :                              NULL,
   33081                 :                              lastErrno,
   33082                 :                              0,
   33083                 :                              (LPSTR) &zTemp,
   33084                 :                              0,
   33085                 :                              0);
   33086                 :     if( dwLen > 0 ){
   33087                 :       /* allocate a buffer and convert to UTF8 */
   33088                 :       sqlite3BeginBenignMalloc();
   33089                 :       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
   33090                 :       sqlite3EndBenignMalloc();
   33091                 :       /* free the system buffer allocated by FormatMessage */
   33092                 :       osLocalFree(zTemp);
   33093                 :     }
   33094                 : #endif
   33095                 :   }
   33096                 :   if( 0 == dwLen ){
   33097                 :     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
   33098                 :   }else{
   33099                 :     /* copy a maximum of nBuf chars to output buffer */
   33100                 :     sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
   33101                 :     /* free the UTF8 buffer */
   33102                 :     sqlite3_free(zOut);
   33103                 :   }
   33104                 :   return 0;
   33105                 : }
   33106                 : 
   33107                 : /*
   33108                 : **
   33109                 : ** This function - winLogErrorAtLine() - is only ever called via the macro
   33110                 : ** winLogError().
   33111                 : **
   33112                 : ** This routine is invoked after an error occurs in an OS function.
   33113                 : ** It logs a message using sqlite3_log() containing the current value of
   33114                 : ** error code and, if possible, the human-readable equivalent from 
   33115                 : ** FormatMessage.
   33116                 : **
   33117                 : ** The first argument passed to the macro should be the error code that
   33118                 : ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). 
   33119                 : ** The two subsequent arguments should be the name of the OS function that
   33120                 : ** failed and the the associated file-system path, if any.
   33121                 : */
   33122                 : #define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
   33123                 : static int winLogErrorAtLine(
   33124                 :   int errcode,                    /* SQLite error code */
   33125                 :   DWORD lastErrno,                /* Win32 last error */
   33126                 :   const char *zFunc,              /* Name of OS function that failed */
   33127                 :   const char *zPath,              /* File path associated with error */
   33128                 :   int iLine                       /* Source line number where error occurred */
   33129                 : ){
   33130                 :   char zMsg[500];                 /* Human readable error text */
   33131                 :   int i;                          /* Loop counter */
   33132                 : 
   33133                 :   zMsg[0] = 0;
   33134                 :   getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
   33135                 :   assert( errcode!=SQLITE_OK );
   33136                 :   if( zPath==0 ) zPath = "";
   33137                 :   for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
   33138                 :   zMsg[i] = 0;
   33139                 :   sqlite3_log(errcode,
   33140                 :       "os_win.c:%d: (%d) %s(%s) - %s",
   33141                 :       iLine, lastErrno, zFunc, zPath, zMsg
   33142                 :   );
   33143                 : 
   33144                 :   return errcode;
   33145                 : }
   33146                 : 
   33147                 : /*
   33148                 : ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
   33149                 : ** will be retried following a locking error - probably caused by 
   33150                 : ** antivirus software.  Also the initial delay before the first retry.
   33151                 : ** The delay increases linearly with each retry.
   33152                 : */
   33153                 : #ifndef SQLITE_WIN32_IOERR_RETRY
   33154                 : # define SQLITE_WIN32_IOERR_RETRY 10
   33155                 : #endif
   33156                 : #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
   33157                 : # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
   33158                 : #endif
   33159                 : static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
   33160                 : static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
   33161                 : 
   33162                 : /*
   33163                 : ** If a ReadFile() or WriteFile() error occurs, invoke this routine
   33164                 : ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
   33165                 : ** to give up with an error.
   33166                 : */
   33167                 : static int retryIoerr(int *pnRetry, DWORD *pError){
   33168                 :   DWORD e = osGetLastError();
   33169                 :   if( *pnRetry>=win32IoerrRetry ){
   33170                 :     if( pError ){
   33171                 :       *pError = e;
   33172                 :     }
   33173                 :     return 0;
   33174                 :   }
   33175                 :   if( e==ERROR_ACCESS_DENIED ||
   33176                 :       e==ERROR_LOCK_VIOLATION ||
   33177                 :       e==ERROR_SHARING_VIOLATION ){
   33178                 :     osSleep(win32IoerrRetryDelay*(1+*pnRetry));
   33179                 :     ++*pnRetry;
   33180                 :     return 1;
   33181                 :   }
   33182                 :   if( pError ){
   33183                 :     *pError = e;
   33184                 :   }
   33185                 :   return 0;
   33186                 : }
   33187                 : 
   33188                 : /*
   33189                 : ** Log a I/O error retry episode.
   33190                 : */
   33191                 : static void logIoerr(int nRetry){
   33192                 :   if( nRetry ){
   33193                 :     sqlite3_log(SQLITE_IOERR, 
   33194                 :       "delayed %dms for lock/sharing conflict",
   33195                 :       win32IoerrRetryDelay*nRetry*(nRetry+1)/2
   33196                 :     );
   33197                 :   }
   33198                 : }
   33199                 : 
   33200                 : #if SQLITE_OS_WINCE
   33201                 : /*************************************************************************
   33202                 : ** This section contains code for WinCE only.
   33203                 : */
   33204                 : /*
   33205                 : ** Windows CE does not have a localtime() function.  So create a
   33206                 : ** substitute.
   33207                 : */
   33208                 : /* #include <time.h> */
   33209                 : struct tm *__cdecl localtime(const time_t *t)
   33210                 : {
   33211                 :   static struct tm y;
   33212                 :   FILETIME uTm, lTm;
   33213                 :   SYSTEMTIME pTm;
   33214                 :   sqlite3_int64 t64;
   33215                 :   t64 = *t;
   33216                 :   t64 = (t64 + 11644473600)*10000000;
   33217                 :   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
   33218                 :   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
   33219                 :   osFileTimeToLocalFileTime(&uTm,&lTm);
   33220                 :   osFileTimeToSystemTime(&lTm,&pTm);
   33221                 :   y.tm_year = pTm.wYear - 1900;
   33222                 :   y.tm_mon = pTm.wMonth - 1;
   33223                 :   y.tm_wday = pTm.wDayOfWeek;
   33224                 :   y.tm_mday = pTm.wDay;
   33225                 :   y.tm_hour = pTm.wHour;
   33226                 :   y.tm_min = pTm.wMinute;
   33227                 :   y.tm_sec = pTm.wSecond;
   33228                 :   return &y;
   33229                 : }
   33230                 : 
   33231                 : #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
   33232                 : 
   33233                 : /*
   33234                 : ** Acquire a lock on the handle h
   33235                 : */
   33236                 : static void winceMutexAcquire(HANDLE h){
   33237                 :    DWORD dwErr;
   33238                 :    do {
   33239                 :      dwErr = WaitForSingleObject(h, INFINITE);
   33240                 :    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
   33241                 : }
   33242                 : /*
   33243                 : ** Release a lock acquired by winceMutexAcquire()
   33244                 : */
   33245                 : #define winceMutexRelease(h) ReleaseMutex(h)
   33246                 : 
   33247                 : /*
   33248                 : ** Create the mutex and shared memory used for locking in the file
   33249                 : ** descriptor pFile
   33250                 : */
   33251                 : static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
   33252                 :   LPWSTR zTok;
   33253                 :   LPWSTR zName;
   33254                 :   BOOL bInit = TRUE;
   33255                 : 
   33256                 :   zName = utf8ToUnicode(zFilename);
   33257                 :   if( zName==0 ){
   33258                 :     /* out of memory */
   33259                 :     return FALSE;
   33260                 :   }
   33261                 : 
   33262                 :   /* Initialize the local lockdata */
   33263                 :   memset(&pFile->local, 0, sizeof(pFile->local));
   33264                 : 
   33265                 :   /* Replace the backslashes from the filename and lowercase it
   33266                 :   ** to derive a mutex name. */
   33267                 :   zTok = osCharLowerW(zName);
   33268                 :   for (;*zTok;zTok++){
   33269                 :     if (*zTok == '\\') *zTok = '_';
   33270                 :   }
   33271                 : 
   33272                 :   /* Create/open the named mutex */
   33273                 :   pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
   33274                 :   if (!pFile->hMutex){
   33275                 :     pFile->lastErrno = osGetLastError();
   33276                 :     winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
   33277                 :     sqlite3_free(zName);
   33278                 :     return FALSE;
   33279                 :   }
   33280                 : 
   33281                 :   /* Acquire the mutex before continuing */
   33282                 :   winceMutexAcquire(pFile->hMutex);
   33283                 :   
   33284                 :   /* Since the names of named mutexes, semaphores, file mappings etc are 
   33285                 :   ** case-sensitive, take advantage of that by uppercasing the mutex name
   33286                 :   ** and using that as the shared filemapping name.
   33287                 :   */
   33288                 :   osCharUpperW(zName);
   33289                 :   pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
   33290                 :                                         PAGE_READWRITE, 0, sizeof(winceLock),
   33291                 :                                         zName);  
   33292                 : 
   33293                 :   /* Set a flag that indicates we're the first to create the memory so it 
   33294                 :   ** must be zero-initialized */
   33295                 :   if (osGetLastError() == ERROR_ALREADY_EXISTS){
   33296                 :     bInit = FALSE;
   33297                 :   }
   33298                 : 
   33299                 :   sqlite3_free(zName);
   33300                 : 
   33301                 :   /* If we succeeded in making the shared memory handle, map it. */
   33302                 :   if (pFile->hShared){
   33303                 :     pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, 
   33304                 :              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
   33305                 :     /* If mapping failed, close the shared memory handle and erase it */
   33306                 :     if (!pFile->shared){
   33307                 :       pFile->lastErrno = osGetLastError();
   33308                 :       winLogError(SQLITE_ERROR, pFile->lastErrno,
   33309                 :                "winceCreateLock2", zFilename);
   33310                 :       osCloseHandle(pFile->hShared);
   33311                 :       pFile->hShared = NULL;
   33312                 :     }
   33313                 :   }
   33314                 : 
   33315                 :   /* If shared memory could not be created, then close the mutex and fail */
   33316                 :   if (pFile->hShared == NULL){
   33317                 :     winceMutexRelease(pFile->hMutex);
   33318                 :     osCloseHandle(pFile->hMutex);
   33319                 :     pFile->hMutex = NULL;
   33320                 :     return FALSE;
   33321                 :   }
   33322                 :   
   33323                 :   /* Initialize the shared memory if we're supposed to */
   33324                 :   if (bInit) {
   33325                 :     memset(pFile->shared, 0, sizeof(winceLock));
   33326                 :   }
   33327                 : 
   33328                 :   winceMutexRelease(pFile->hMutex);
   33329                 :   return TRUE;
   33330                 : }
   33331                 : 
   33332                 : /*
   33333                 : ** Destroy the part of winFile that deals with wince locks
   33334                 : */
   33335                 : static void winceDestroyLock(winFile *pFile){
   33336                 :   if (pFile->hMutex){
   33337                 :     /* Acquire the mutex */
   33338                 :     winceMutexAcquire(pFile->hMutex);
   33339                 : 
   33340                 :     /* The following blocks should probably assert in debug mode, but they
   33341                 :        are to cleanup in case any locks remained open */
   33342                 :     if (pFile->local.nReaders){
   33343                 :       pFile->shared->nReaders --;
   33344                 :     }
   33345                 :     if (pFile->local.bReserved){
   33346                 :       pFile->shared->bReserved = FALSE;
   33347                 :     }
   33348                 :     if (pFile->local.bPending){
   33349                 :       pFile->shared->bPending = FALSE;
   33350                 :     }
   33351                 :     if (pFile->local.bExclusive){
   33352                 :       pFile->shared->bExclusive = FALSE;
   33353                 :     }
   33354                 : 
   33355                 :     /* De-reference and close our copy of the shared memory handle */
   33356                 :     osUnmapViewOfFile(pFile->shared);
   33357                 :     osCloseHandle(pFile->hShared);
   33358                 : 
   33359                 :     /* Done with the mutex */
   33360                 :     winceMutexRelease(pFile->hMutex);    
   33361                 :     osCloseHandle(pFile->hMutex);
   33362                 :     pFile->hMutex = NULL;
   33363                 :   }
   33364                 : }
   33365                 : 
   33366                 : /* 
   33367                 : ** An implementation of the LockFile() API of Windows for CE
   33368                 : */
   33369                 : static BOOL winceLockFile(
   33370                 :   HANDLE *phFile,
   33371                 :   DWORD dwFileOffsetLow,
   33372                 :   DWORD dwFileOffsetHigh,
   33373                 :   DWORD nNumberOfBytesToLockLow,
   33374                 :   DWORD nNumberOfBytesToLockHigh
   33375                 : ){
   33376                 :   winFile *pFile = HANDLE_TO_WINFILE(phFile);
   33377                 :   BOOL bReturn = FALSE;
   33378                 : 
   33379                 :   UNUSED_PARAMETER(dwFileOffsetHigh);
   33380                 :   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
   33381                 : 
   33382                 :   if (!pFile->hMutex) return TRUE;
   33383                 :   winceMutexAcquire(pFile->hMutex);
   33384                 : 
   33385                 :   /* Wanting an exclusive lock? */
   33386                 :   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
   33387                 :        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
   33388                 :     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
   33389                 :        pFile->shared->bExclusive = TRUE;
   33390                 :        pFile->local.bExclusive = TRUE;
   33391                 :        bReturn = TRUE;
   33392                 :     }
   33393                 :   }
   33394                 : 
   33395                 :   /* Want a read-only lock? */
   33396                 :   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
   33397                 :            nNumberOfBytesToLockLow == 1){
   33398                 :     if (pFile->shared->bExclusive == 0){
   33399                 :       pFile->local.nReaders ++;
   33400                 :       if (pFile->local.nReaders == 1){
   33401                 :         pFile->shared->nReaders ++;
   33402                 :       }
   33403                 :       bReturn = TRUE;
   33404                 :     }
   33405                 :   }
   33406                 : 
   33407                 :   /* Want a pending lock? */
   33408                 :   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToLockLow == 1){
   33409                 :     /* If no pending lock has been acquired, then acquire it */
   33410                 :     if (pFile->shared->bPending == 0) {
   33411                 :       pFile->shared->bPending = TRUE;
   33412                 :       pFile->local.bPending = TRUE;
   33413                 :       bReturn = TRUE;
   33414                 :     }
   33415                 :   }
   33416                 : 
   33417                 :   /* Want a reserved lock? */
   33418                 :   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToLockLow == 1){
   33419                 :     if (pFile->shared->bReserved == 0) {
   33420                 :       pFile->shared->bReserved = TRUE;
   33421                 :       pFile->local.bReserved = TRUE;
   33422                 :       bReturn = TRUE;
   33423                 :     }
   33424                 :   }
   33425                 : 
   33426                 :   winceMutexRelease(pFile->hMutex);
   33427                 :   return bReturn;
   33428                 : }
   33429                 : 
   33430                 : /*
   33431                 : ** An implementation of the UnlockFile API of Windows for CE
   33432                 : */
   33433                 : static BOOL winceUnlockFile(
   33434                 :   HANDLE *phFile,
   33435                 :   DWORD dwFileOffsetLow,
   33436                 :   DWORD dwFileOffsetHigh,
   33437                 :   DWORD nNumberOfBytesToUnlockLow,
   33438                 :   DWORD nNumberOfBytesToUnlockHigh
   33439                 : ){
   33440                 :   winFile *pFile = HANDLE_TO_WINFILE(phFile);
   33441                 :   BOOL bReturn = FALSE;
   33442                 : 
   33443                 :   UNUSED_PARAMETER(dwFileOffsetHigh);
   33444                 :   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
   33445                 : 
   33446                 :   if (!pFile->hMutex) return TRUE;
   33447                 :   winceMutexAcquire(pFile->hMutex);
   33448                 : 
   33449                 :   /* Releasing a reader lock or an exclusive lock */
   33450                 :   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
   33451                 :     /* Did we have an exclusive lock? */
   33452                 :     if (pFile->local.bExclusive){
   33453                 :       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
   33454                 :       pFile->local.bExclusive = FALSE;
   33455                 :       pFile->shared->bExclusive = FALSE;
   33456                 :       bReturn = TRUE;
   33457                 :     }
   33458                 : 
   33459                 :     /* Did we just have a reader lock? */
   33460                 :     else if (pFile->local.nReaders){
   33461                 :       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE || nNumberOfBytesToUnlockLow == 1);
   33462                 :       pFile->local.nReaders --;
   33463                 :       if (pFile->local.nReaders == 0)
   33464                 :       {
   33465                 :         pFile->shared->nReaders --;
   33466                 :       }
   33467                 :       bReturn = TRUE;
   33468                 :     }
   33469                 :   }
   33470                 : 
   33471                 :   /* Releasing a pending lock */
   33472                 :   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE && nNumberOfBytesToUnlockLow == 1){
   33473                 :     if (pFile->local.bPending){
   33474                 :       pFile->local.bPending = FALSE;
   33475                 :       pFile->shared->bPending = FALSE;
   33476                 :       bReturn = TRUE;
   33477                 :     }
   33478                 :   }
   33479                 :   /* Releasing a reserved lock */
   33480                 :   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE && nNumberOfBytesToUnlockLow == 1){
   33481                 :     if (pFile->local.bReserved) {
   33482                 :       pFile->local.bReserved = FALSE;
   33483                 :       pFile->shared->bReserved = FALSE;
   33484                 :       bReturn = TRUE;
   33485                 :     }
   33486                 :   }
   33487                 : 
   33488                 :   winceMutexRelease(pFile->hMutex);
   33489                 :   return bReturn;
   33490                 : }
   33491                 : 
   33492                 : /*
   33493                 : ** An implementation of the LockFileEx() API of Windows for CE
   33494                 : */
   33495                 : static BOOL winceLockFileEx(
   33496                 :   HANDLE *phFile,
   33497                 :   DWORD dwFlags,
   33498                 :   DWORD dwReserved,
   33499                 :   DWORD nNumberOfBytesToLockLow,
   33500                 :   DWORD nNumberOfBytesToLockHigh,
   33501                 :   LPOVERLAPPED lpOverlapped
   33502                 : ){
   33503                 :   UNUSED_PARAMETER(dwReserved);
   33504                 :   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
   33505                 : 
   33506                 :   /* If the caller wants a shared read lock, forward this call
   33507                 :   ** to winceLockFile */
   33508                 :   if (lpOverlapped->Offset == (DWORD)SHARED_FIRST &&
   33509                 :       dwFlags == 1 &&
   33510                 :       nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
   33511                 :     return winceLockFile(phFile, SHARED_FIRST, 0, 1, 0);
   33512                 :   }
   33513                 :   return FALSE;
   33514                 : }
   33515                 : /*
   33516                 : ** End of the special code for wince
   33517                 : *****************************************************************************/
   33518                 : #endif /* SQLITE_OS_WINCE */
   33519                 : 
   33520                 : /*****************************************************************************
   33521                 : ** The next group of routines implement the I/O methods specified
   33522                 : ** by the sqlite3_io_methods object.
   33523                 : ******************************************************************************/
   33524                 : 
   33525                 : /*
   33526                 : ** Some Microsoft compilers lack this definition.
   33527                 : */
   33528                 : #ifndef INVALID_SET_FILE_POINTER
   33529                 : # define INVALID_SET_FILE_POINTER ((DWORD)-1)
   33530                 : #endif
   33531                 : 
   33532                 : /*
   33533                 : ** Move the current position of the file handle passed as the first 
   33534                 : ** argument to offset iOffset within the file. If successful, return 0. 
   33535                 : ** Otherwise, set pFile->lastErrno and return non-zero.
   33536                 : */
   33537                 : static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
   33538                 :   LONG upperBits;                 /* Most sig. 32 bits of new offset */
   33539                 :   LONG lowerBits;                 /* Least sig. 32 bits of new offset */
   33540                 :   DWORD dwRet;                    /* Value returned by SetFilePointer() */
   33541                 :   DWORD lastErrno;                /* Value returned by GetLastError() */
   33542                 : 
   33543                 :   upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
   33544                 :   lowerBits = (LONG)(iOffset & 0xffffffff);
   33545                 : 
   33546                 :   /* API oddity: If successful, SetFilePointer() returns a dword 
   33547                 :   ** containing the lower 32-bits of the new file-offset. Or, if it fails,
   33548                 :   ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, 
   33549                 :   ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine 
   33550                 :   ** whether an error has actually occured, it is also necessary to call 
   33551                 :   ** GetLastError().
   33552                 :   */
   33553                 :   dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
   33554                 : 
   33555                 :   if( (dwRet==INVALID_SET_FILE_POINTER
   33556                 :       && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
   33557                 :     pFile->lastErrno = lastErrno;
   33558                 :     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
   33559                 :              "seekWinFile", pFile->zPath);
   33560                 :     return 1;
   33561                 :   }
   33562                 : 
   33563                 :   return 0;
   33564                 : }
   33565                 : 
   33566                 : /*
   33567                 : ** Close a file.
   33568                 : **
   33569                 : ** It is reported that an attempt to close a handle might sometimes
   33570                 : ** fail.  This is a very unreasonable result, but Windows is notorious
   33571                 : ** for being unreasonable so I do not doubt that it might happen.  If
   33572                 : ** the close fails, we pause for 100 milliseconds and try again.  As
   33573                 : ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
   33574                 : ** giving up and returning an error.
   33575                 : */
   33576                 : #define MX_CLOSE_ATTEMPT 3
   33577                 : static int winClose(sqlite3_file *id){
   33578                 :   int rc, cnt = 0;
   33579                 :   winFile *pFile = (winFile*)id;
   33580                 : 
   33581                 :   assert( id!=0 );
   33582                 :   assert( pFile->pShm==0 );
   33583                 :   OSTRACE(("CLOSE %d\n", pFile->h));
   33584                 :   do{
   33585                 :     rc = osCloseHandle(pFile->h);
   33586                 :     /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
   33587                 :   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (osSleep(100), 1) );
   33588                 : #if SQLITE_OS_WINCE
   33589                 : #define WINCE_DELETION_ATTEMPTS 3
   33590                 :   winceDestroyLock(pFile);
   33591                 :   if( pFile->zDeleteOnClose ){
   33592                 :     int cnt = 0;
   33593                 :     while(
   33594                 :            osDeleteFileW(pFile->zDeleteOnClose)==0
   33595                 :         && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff 
   33596                 :         && cnt++ < WINCE_DELETION_ATTEMPTS
   33597                 :     ){
   33598                 :        osSleep(100);  /* Wait a little before trying again */
   33599                 :     }
   33600                 :     sqlite3_free(pFile->zDeleteOnClose);
   33601                 :   }
   33602                 : #endif
   33603                 :   OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
   33604                 :   OpenCounter(-1);
   33605                 :   return rc ? SQLITE_OK
   33606                 :             : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
   33607                 :                           "winClose", pFile->zPath);
   33608                 : }
   33609                 : 
   33610                 : /*
   33611                 : ** Read data from a file into a buffer.  Return SQLITE_OK if all
   33612                 : ** bytes were read successfully and SQLITE_IOERR if anything goes
   33613                 : ** wrong.
   33614                 : */
   33615                 : static int winRead(
   33616                 :   sqlite3_file *id,          /* File to read from */
   33617                 :   void *pBuf,                /* Write content into this buffer */
   33618                 :   int amt,                   /* Number of bytes to read */
   33619                 :   sqlite3_int64 offset       /* Begin reading at this offset */
   33620                 : ){
   33621                 :   winFile *pFile = (winFile*)id;  /* file handle */
   33622                 :   DWORD nRead;                    /* Number of bytes actually read from file */
   33623                 :   int nRetry = 0;                 /* Number of retrys */
   33624                 : 
   33625                 :   assert( id!=0 );
   33626                 :   SimulateIOError(return SQLITE_IOERR_READ);
   33627                 :   OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
   33628                 : 
   33629                 :   if( seekWinFile(pFile, offset) ){
   33630                 :     return SQLITE_FULL;
   33631                 :   }
   33632                 :   while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
   33633                 :     DWORD lastErrno;
   33634                 :     if( retryIoerr(&nRetry, &lastErrno) ) continue;
   33635                 :     pFile->lastErrno = lastErrno;
   33636                 :     return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
   33637                 :              "winRead", pFile->zPath);
   33638                 :   }
   33639                 :   logIoerr(nRetry);
   33640                 :   if( nRead<(DWORD)amt ){
   33641                 :     /* Unread parts of the buffer must be zero-filled */
   33642                 :     memset(&((char*)pBuf)[nRead], 0, amt-nRead);
   33643                 :     return SQLITE_IOERR_SHORT_READ;
   33644                 :   }
   33645                 : 
   33646                 :   return SQLITE_OK;
   33647                 : }
   33648                 : 
   33649                 : /*
   33650                 : ** Write data from a buffer into a file.  Return SQLITE_OK on success
   33651                 : ** or some other error code on failure.
   33652                 : */
   33653                 : static int winWrite(
   33654                 :   sqlite3_file *id,               /* File to write into */
   33655                 :   const void *pBuf,               /* The bytes to be written */
   33656                 :   int amt,                        /* Number of bytes to write */
   33657                 :   sqlite3_int64 offset            /* Offset into the file to begin writing at */
   33658                 : ){
   33659                 :   int rc;                         /* True if error has occured, else false */
   33660                 :   winFile *pFile = (winFile*)id;  /* File handle */
   33661                 :   int nRetry = 0;                 /* Number of retries */
   33662                 : 
   33663                 :   assert( amt>0 );
   33664                 :   assert( pFile );
   33665                 :   SimulateIOError(return SQLITE_IOERR_WRITE);
   33666                 :   SimulateDiskfullError(return SQLITE_FULL);
   33667                 : 
   33668                 :   OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
   33669                 : 
   33670                 :   rc = seekWinFile(pFile, offset);
   33671                 :   if( rc==0 ){
   33672                 :     u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
   33673                 :     int nRem = amt;               /* Number of bytes yet to be written */
   33674                 :     DWORD nWrite;                 /* Bytes written by each WriteFile() call */
   33675                 :     DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
   33676                 : 
   33677                 :     while( nRem>0 ){
   33678                 :       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
   33679                 :         if( retryIoerr(&nRetry, &lastErrno) ) continue;
   33680                 :         break;
   33681                 :       }
   33682                 :       if( nWrite<=0 ) break;
   33683                 :       aRem += nWrite;
   33684                 :       nRem -= nWrite;
   33685                 :     }
   33686                 :     if( nRem>0 ){
   33687                 :       pFile->lastErrno = lastErrno;
   33688                 :       rc = 1;
   33689                 :     }
   33690                 :   }
   33691                 : 
   33692                 :   if( rc ){
   33693                 :     if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
   33694                 :        || ( pFile->lastErrno==ERROR_DISK_FULL )){
   33695                 :       return SQLITE_FULL;
   33696                 :     }
   33697                 :     return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
   33698                 :              "winWrite", pFile->zPath);
   33699                 :   }else{
   33700                 :     logIoerr(nRetry);
   33701                 :   }
   33702                 :   return SQLITE_OK;
   33703                 : }
   33704                 : 
   33705                 : /*
   33706                 : ** Truncate an open file to a specified size
   33707                 : */
   33708                 : static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
   33709                 :   winFile *pFile = (winFile*)id;  /* File handle object */
   33710                 :   int rc = SQLITE_OK;             /* Return code for this function */
   33711                 : 
   33712                 :   assert( pFile );
   33713                 : 
   33714                 :   OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
   33715                 :   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
   33716                 : 
   33717                 :   /* If the user has configured a chunk-size for this file, truncate the
   33718                 :   ** file so that it consists of an integer number of chunks (i.e. the
   33719                 :   ** actual file size after the operation may be larger than the requested
   33720                 :   ** size).
   33721                 :   */
   33722                 :   if( pFile->szChunk>0 ){
   33723                 :     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
   33724                 :   }
   33725                 : 
   33726                 :   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
   33727                 :   if( seekWinFile(pFile, nByte) ){
   33728                 :     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
   33729                 :              "winTruncate1", pFile->zPath);
   33730                 :   }else if( 0==osSetEndOfFile(pFile->h) ){
   33731                 :     pFile->lastErrno = osGetLastError();
   33732                 :     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
   33733                 :              "winTruncate2", pFile->zPath);
   33734                 :   }
   33735                 : 
   33736                 :   OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
   33737                 :   return rc;
   33738                 : }
   33739                 : 
   33740                 : #ifdef SQLITE_TEST
   33741                 : /*
   33742                 : ** Count the number of fullsyncs and normal syncs.  This is used to test
   33743                 : ** that syncs and fullsyncs are occuring at the right times.
   33744                 : */
   33745                 : SQLITE_API int sqlite3_sync_count = 0;
   33746                 : SQLITE_API int sqlite3_fullsync_count = 0;
   33747                 : #endif
   33748                 : 
   33749                 : /*
   33750                 : ** Make sure all writes to a particular file are committed to disk.
   33751                 : */
   33752                 : static int winSync(sqlite3_file *id, int flags){
   33753                 : #ifndef SQLITE_NO_SYNC
   33754                 :   /*
   33755                 :   ** Used only when SQLITE_NO_SYNC is not defined.
   33756                 :    */
   33757                 :   BOOL rc;
   33758                 : #endif
   33759                 : #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
   33760                 :     (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
   33761                 :   /*
   33762                 :   ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
   33763                 :   ** OSTRACE() macros.
   33764                 :    */
   33765                 :   winFile *pFile = (winFile*)id;
   33766                 : #else
   33767                 :   UNUSED_PARAMETER(id);
   33768                 : #endif
   33769                 : 
   33770                 :   assert( pFile );
   33771                 :   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
   33772                 :   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
   33773                 :       || (flags&0x0F)==SQLITE_SYNC_FULL
   33774                 :   );
   33775                 : 
   33776                 :   OSTRACE(("SYNC %d lock=%d\n", pFile->h, pFile->locktype));
   33777                 : 
   33778                 :   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
   33779                 :   ** line is to test that doing so does not cause any problems.
   33780                 :   */
   33781                 :   SimulateDiskfullError( return SQLITE_FULL );
   33782                 : 
   33783                 : #ifndef SQLITE_TEST
   33784                 :   UNUSED_PARAMETER(flags);
   33785                 : #else
   33786                 :   if( (flags&0x0F)==SQLITE_SYNC_FULL ){
   33787                 :     sqlite3_fullsync_count++;
   33788                 :   }
   33789                 :   sqlite3_sync_count++;
   33790                 : #endif
   33791                 : 
   33792                 :   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
   33793                 :   ** no-op
   33794                 :   */
   33795                 : #ifdef SQLITE_NO_SYNC
   33796                 :   return SQLITE_OK;
   33797                 : #else
   33798                 :   rc = osFlushFileBuffers(pFile->h);
   33799                 :   SimulateIOError( rc=FALSE );
   33800                 :   if( rc ){
   33801                 :     return SQLITE_OK;
   33802                 :   }else{
   33803                 :     pFile->lastErrno = osGetLastError();
   33804                 :     return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
   33805                 :              "winSync", pFile->zPath);
   33806                 :   }
   33807                 : #endif
   33808                 : }
   33809                 : 
   33810                 : /*
   33811                 : ** Determine the current size of a file in bytes
   33812                 : */
   33813                 : static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
   33814                 :   DWORD upperBits;
   33815                 :   DWORD lowerBits;
   33816                 :   winFile *pFile = (winFile*)id;
   33817                 :   DWORD lastErrno;
   33818                 : 
   33819                 :   assert( id!=0 );
   33820                 :   SimulateIOError(return SQLITE_IOERR_FSTAT);
   33821                 :   lowerBits = osGetFileSize(pFile->h, &upperBits);
   33822                 :   if(   (lowerBits == INVALID_FILE_SIZE)
   33823                 :      && ((lastErrno = osGetLastError())!=NO_ERROR) )
   33824                 :   {
   33825                 :     pFile->lastErrno = lastErrno;
   33826                 :     return winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
   33827                 :              "winFileSize", pFile->zPath);
   33828                 :   }
   33829                 :   *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
   33830                 :   return SQLITE_OK;
   33831                 : }
   33832                 : 
   33833                 : /*
   33834                 : ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
   33835                 : */
   33836                 : #ifndef LOCKFILE_FAIL_IMMEDIATELY
   33837                 : # define LOCKFILE_FAIL_IMMEDIATELY 1
   33838                 : #endif
   33839                 : 
   33840                 : /*
   33841                 : ** Acquire a reader lock.
   33842                 : ** Different API routines are called depending on whether or not this
   33843                 : ** is Win9x or WinNT.
   33844                 : */
   33845                 : static int getReadLock(winFile *pFile){
   33846                 :   int res;
   33847                 :   if( isNT() ){
   33848                 :     OVERLAPPED ovlp;
   33849                 :     ovlp.Offset = SHARED_FIRST;
   33850                 :     ovlp.OffsetHigh = 0;
   33851                 :     ovlp.hEvent = 0;
   33852                 :     res = osLockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
   33853                 :                        0, SHARED_SIZE, 0, &ovlp);
   33854                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   33855                 : */
   33856                 : #if SQLITE_OS_WINCE==0
   33857                 :   }else{
   33858                 :     int lk;
   33859                 :     sqlite3_randomness(sizeof(lk), &lk);
   33860                 :     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
   33861                 :     res = osLockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
   33862                 : #endif
   33863                 :   }
   33864                 :   if( res == 0 ){
   33865                 :     pFile->lastErrno = osGetLastError();
   33866                 :     /* No need to log a failure to lock */
   33867                 :   }
   33868                 :   return res;
   33869                 : }
   33870                 : 
   33871                 : /*
   33872                 : ** Undo a readlock
   33873                 : */
   33874                 : static int unlockReadLock(winFile *pFile){
   33875                 :   int res;
   33876                 :   DWORD lastErrno;
   33877                 :   if( isNT() ){
   33878                 :     res = osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   33879                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   33880                 : */
   33881                 : #if SQLITE_OS_WINCE==0
   33882                 :   }else{
   33883                 :     res = osUnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
   33884                 : #endif
   33885                 :   }
   33886                 :   if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
   33887                 :     pFile->lastErrno = lastErrno;
   33888                 :     winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
   33889                 :              "unlockReadLock", pFile->zPath);
   33890                 :   }
   33891                 :   return res;
   33892                 : }
   33893                 : 
   33894                 : /*
   33895                 : ** Lock the file with the lock specified by parameter locktype - one
   33896                 : ** of the following:
   33897                 : **
   33898                 : **     (1) SHARED_LOCK
   33899                 : **     (2) RESERVED_LOCK
   33900                 : **     (3) PENDING_LOCK
   33901                 : **     (4) EXCLUSIVE_LOCK
   33902                 : **
   33903                 : ** Sometimes when requesting one lock state, additional lock states
   33904                 : ** are inserted in between.  The locking might fail on one of the later
   33905                 : ** transitions leaving the lock state different from what it started but
   33906                 : ** still short of its goal.  The following chart shows the allowed
   33907                 : ** transitions and the inserted intermediate states:
   33908                 : **
   33909                 : **    UNLOCKED -> SHARED
   33910                 : **    SHARED -> RESERVED
   33911                 : **    SHARED -> (PENDING) -> EXCLUSIVE
   33912                 : **    RESERVED -> (PENDING) -> EXCLUSIVE
   33913                 : **    PENDING -> EXCLUSIVE
   33914                 : **
   33915                 : ** This routine will only increase a lock.  The winUnlock() routine
   33916                 : ** erases all locks at once and returns us immediately to locking level 0.
   33917                 : ** It is not possible to lower the locking level one step at a time.  You
   33918                 : ** must go straight to locking level 0.
   33919                 : */
   33920                 : static int winLock(sqlite3_file *id, int locktype){
   33921                 :   int rc = SQLITE_OK;    /* Return code from subroutines */
   33922                 :   int res = 1;           /* Result of a Windows lock call */
   33923                 :   int newLocktype;       /* Set pFile->locktype to this value before exiting */
   33924                 :   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
   33925                 :   winFile *pFile = (winFile*)id;
   33926                 :   DWORD lastErrno = NO_ERROR;
   33927                 : 
   33928                 :   assert( id!=0 );
   33929                 :   OSTRACE(("LOCK %d %d was %d(%d)\n",
   33930                 :            pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
   33931                 : 
   33932                 :   /* If there is already a lock of this type or more restrictive on the
   33933                 :   ** OsFile, do nothing. Don't use the end_lock: exit path, as
   33934                 :   ** sqlite3OsEnterMutex() hasn't been called yet.
   33935                 :   */
   33936                 :   if( pFile->locktype>=locktype ){
   33937                 :     return SQLITE_OK;
   33938                 :   }
   33939                 : 
   33940                 :   /* Make sure the locking sequence is correct
   33941                 :   */
   33942                 :   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
   33943                 :   assert( locktype!=PENDING_LOCK );
   33944                 :   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
   33945                 : 
   33946                 :   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
   33947                 :   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
   33948                 :   ** the PENDING_LOCK byte is temporary.
   33949                 :   */
   33950                 :   newLocktype = pFile->locktype;
   33951                 :   if(   (pFile->locktype==NO_LOCK)
   33952                 :      || (   (locktype==EXCLUSIVE_LOCK)
   33953                 :          && (pFile->locktype==RESERVED_LOCK))
   33954                 :   ){
   33955                 :     int cnt = 3;
   33956                 :     while( cnt-->0 && (res = osLockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
   33957                 :       /* Try 3 times to get the pending lock.  This is needed to work
   33958                 :       ** around problems caused by indexing and/or anti-virus software on
   33959                 :       ** Windows systems.
   33960                 :       ** If you are using this code as a model for alternative VFSes, do not
   33961                 :       ** copy this retry logic.  It is a hack intended for Windows only.
   33962                 :       */
   33963                 :       OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
   33964                 :       if( cnt ) osSleep(1);
   33965                 :     }
   33966                 :     gotPendingLock = res;
   33967                 :     if( !res ){
   33968                 :       lastErrno = osGetLastError();
   33969                 :     }
   33970                 :   }
   33971                 : 
   33972                 :   /* Acquire a shared lock
   33973                 :   */
   33974                 :   if( locktype==SHARED_LOCK && res ){
   33975                 :     assert( pFile->locktype==NO_LOCK );
   33976                 :     res = getReadLock(pFile);
   33977                 :     if( res ){
   33978                 :       newLocktype = SHARED_LOCK;
   33979                 :     }else{
   33980                 :       lastErrno = osGetLastError();
   33981                 :     }
   33982                 :   }
   33983                 : 
   33984                 :   /* Acquire a RESERVED lock
   33985                 :   */
   33986                 :   if( locktype==RESERVED_LOCK && res ){
   33987                 :     assert( pFile->locktype==SHARED_LOCK );
   33988                 :     res = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   33989                 :     if( res ){
   33990                 :       newLocktype = RESERVED_LOCK;
   33991                 :     }else{
   33992                 :       lastErrno = osGetLastError();
   33993                 :     }
   33994                 :   }
   33995                 : 
   33996                 :   /* Acquire a PENDING lock
   33997                 :   */
   33998                 :   if( locktype==EXCLUSIVE_LOCK && res ){
   33999                 :     newLocktype = PENDING_LOCK;
   34000                 :     gotPendingLock = 0;
   34001                 :   }
   34002                 : 
   34003                 :   /* Acquire an EXCLUSIVE lock
   34004                 :   */
   34005                 :   if( locktype==EXCLUSIVE_LOCK && res ){
   34006                 :     assert( pFile->locktype>=SHARED_LOCK );
   34007                 :     res = unlockReadLock(pFile);
   34008                 :     OSTRACE(("unreadlock = %d\n", res));
   34009                 :     res = osLockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   34010                 :     if( res ){
   34011                 :       newLocktype = EXCLUSIVE_LOCK;
   34012                 :     }else{
   34013                 :       lastErrno = osGetLastError();
   34014                 :       OSTRACE(("error-code = %d\n", lastErrno));
   34015                 :       getReadLock(pFile);
   34016                 :     }
   34017                 :   }
   34018                 : 
   34019                 :   /* If we are holding a PENDING lock that ought to be released, then
   34020                 :   ** release it now.
   34021                 :   */
   34022                 :   if( gotPendingLock && locktype==SHARED_LOCK ){
   34023                 :     osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
   34024                 :   }
   34025                 : 
   34026                 :   /* Update the state of the lock has held in the file descriptor then
   34027                 :   ** return the appropriate result code.
   34028                 :   */
   34029                 :   if( res ){
   34030                 :     rc = SQLITE_OK;
   34031                 :   }else{
   34032                 :     OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
   34033                 :            locktype, newLocktype));
   34034                 :     pFile->lastErrno = lastErrno;
   34035                 :     rc = SQLITE_BUSY;
   34036                 :   }
   34037                 :   pFile->locktype = (u8)newLocktype;
   34038                 :   return rc;
   34039                 : }
   34040                 : 
   34041                 : /*
   34042                 : ** This routine checks if there is a RESERVED lock held on the specified
   34043                 : ** file by this or any other process. If such a lock is held, return
   34044                 : ** non-zero, otherwise zero.
   34045                 : */
   34046                 : static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
   34047                 :   int rc;
   34048                 :   winFile *pFile = (winFile*)id;
   34049                 : 
   34050                 :   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
   34051                 : 
   34052                 :   assert( id!=0 );
   34053                 :   if( pFile->locktype>=RESERVED_LOCK ){
   34054                 :     rc = 1;
   34055                 :     OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
   34056                 :   }else{
   34057                 :     rc = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   34058                 :     if( rc ){
   34059                 :       osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   34060                 :     }
   34061                 :     rc = !rc;
   34062                 :     OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
   34063                 :   }
   34064                 :   *pResOut = rc;
   34065                 :   return SQLITE_OK;
   34066                 : }
   34067                 : 
   34068                 : /*
   34069                 : ** Lower the locking level on file descriptor id to locktype.  locktype
   34070                 : ** must be either NO_LOCK or SHARED_LOCK.
   34071                 : **
   34072                 : ** If the locking level of the file descriptor is already at or below
   34073                 : ** the requested locking level, this routine is a no-op.
   34074                 : **
   34075                 : ** It is not possible for this routine to fail if the second argument
   34076                 : ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
   34077                 : ** might return SQLITE_IOERR;
   34078                 : */
   34079                 : static int winUnlock(sqlite3_file *id, int locktype){
   34080                 :   int type;
   34081                 :   winFile *pFile = (winFile*)id;
   34082                 :   int rc = SQLITE_OK;
   34083                 :   assert( pFile!=0 );
   34084                 :   assert( locktype<=SHARED_LOCK );
   34085                 :   OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
   34086                 :           pFile->locktype, pFile->sharedLockByte));
   34087                 :   type = pFile->locktype;
   34088                 :   if( type>=EXCLUSIVE_LOCK ){
   34089                 :     osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
   34090                 :     if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
   34091                 :       /* This should never happen.  We should always be able to
   34092                 :       ** reacquire the read lock */
   34093                 :       rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
   34094                 :                "winUnlock", pFile->zPath);
   34095                 :     }
   34096                 :   }
   34097                 :   if( type>=RESERVED_LOCK ){
   34098                 :     osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
   34099                 :   }
   34100                 :   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
   34101                 :     unlockReadLock(pFile);
   34102                 :   }
   34103                 :   if( type>=PENDING_LOCK ){
   34104                 :     osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
   34105                 :   }
   34106                 :   pFile->locktype = (u8)locktype;
   34107                 :   return rc;
   34108                 : }
   34109                 : 
   34110                 : /*
   34111                 : ** If *pArg is inititially negative then this is a query.  Set *pArg to
   34112                 : ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
   34113                 : **
   34114                 : ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
   34115                 : */
   34116                 : static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
   34117                 :   if( *pArg<0 ){
   34118                 :     *pArg = (pFile->ctrlFlags & mask)!=0;
   34119                 :   }else if( (*pArg)==0 ){
   34120                 :     pFile->ctrlFlags &= ~mask;
   34121                 :   }else{
   34122                 :     pFile->ctrlFlags |= mask;
   34123                 :   }
   34124                 : }
   34125                 : 
   34126                 : /*
   34127                 : ** Control and query of the open file handle.
   34128                 : */
   34129                 : static int winFileControl(sqlite3_file *id, int op, void *pArg){
   34130                 :   winFile *pFile = (winFile*)id;
   34131                 :   switch( op ){
   34132                 :     case SQLITE_FCNTL_LOCKSTATE: {
   34133                 :       *(int*)pArg = pFile->locktype;
   34134                 :       return SQLITE_OK;
   34135                 :     }
   34136                 :     case SQLITE_LAST_ERRNO: {
   34137                 :       *(int*)pArg = (int)pFile->lastErrno;
   34138                 :       return SQLITE_OK;
   34139                 :     }
   34140                 :     case SQLITE_FCNTL_CHUNK_SIZE: {
   34141                 :       pFile->szChunk = *(int *)pArg;
   34142                 :       return SQLITE_OK;
   34143                 :     }
   34144                 :     case SQLITE_FCNTL_SIZE_HINT: {
   34145                 :       if( pFile->szChunk>0 ){
   34146                 :         sqlite3_int64 oldSz;
   34147                 :         int rc = winFileSize(id, &oldSz);
   34148                 :         if( rc==SQLITE_OK ){
   34149                 :           sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
   34150                 :           if( newSz>oldSz ){
   34151                 :             SimulateIOErrorBenign(1);
   34152                 :             rc = winTruncate(id, newSz);
   34153                 :             SimulateIOErrorBenign(0);
   34154                 :           }
   34155                 :         }
   34156                 :         return rc;
   34157                 :       }
   34158                 :       return SQLITE_OK;
   34159                 :     }
   34160                 :     case SQLITE_FCNTL_PERSIST_WAL: {
   34161                 :       winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
   34162                 :       return SQLITE_OK;
   34163                 :     }
   34164                 :     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
   34165                 :       winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
   34166                 :       return SQLITE_OK;
   34167                 :     }
   34168                 :     case SQLITE_FCNTL_VFSNAME: {
   34169                 :       *(char**)pArg = sqlite3_mprintf("win32");
   34170                 :       return SQLITE_OK;
   34171                 :     }
   34172                 :     case SQLITE_FCNTL_WIN32_AV_RETRY: {
   34173                 :       int *a = (int*)pArg;
   34174                 :       if( a[0]>0 ){
   34175                 :         win32IoerrRetry = a[0];
   34176                 :       }else{
   34177                 :         a[0] = win32IoerrRetry;
   34178                 :       }
   34179                 :       if( a[1]>0 ){
   34180                 :         win32IoerrRetryDelay = a[1];
   34181                 :       }else{
   34182                 :         a[1] = win32IoerrRetryDelay;
   34183                 :       }
   34184                 :       return SQLITE_OK;
   34185                 :     }
   34186                 :   }
   34187                 :   return SQLITE_NOTFOUND;
   34188                 : }
   34189                 : 
   34190                 : /*
   34191                 : ** Return the sector size in bytes of the underlying block device for
   34192                 : ** the specified file. This is almost always 512 bytes, but may be
   34193                 : ** larger for some devices.
   34194                 : **
   34195                 : ** SQLite code assumes this function cannot fail. It also assumes that
   34196                 : ** if two files are created in the same file-system directory (i.e.
   34197                 : ** a database and its journal file) that the sector size will be the
   34198                 : ** same for both.
   34199                 : */
   34200                 : static int winSectorSize(sqlite3_file *id){
   34201                 :   (void)id;
   34202                 :   return SQLITE_DEFAULT_SECTOR_SIZE;
   34203                 : }
   34204                 : 
   34205                 : /*
   34206                 : ** Return a vector of device characteristics.
   34207                 : */
   34208                 : static int winDeviceCharacteristics(sqlite3_file *id){
   34209                 :   winFile *p = (winFile*)id;
   34210                 :   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
   34211                 :          ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
   34212                 : }
   34213                 : 
   34214                 : #ifndef SQLITE_OMIT_WAL
   34215                 : 
   34216                 : /* 
   34217                 : ** Windows will only let you create file view mappings
   34218                 : ** on allocation size granularity boundaries.
   34219                 : ** During sqlite3_os_init() we do a GetSystemInfo()
   34220                 : ** to get the granularity size.
   34221                 : */
   34222                 : SYSTEM_INFO winSysInfo;
   34223                 : 
   34224                 : /*
   34225                 : ** Helper functions to obtain and relinquish the global mutex. The
   34226                 : ** global mutex is used to protect the winLockInfo objects used by 
   34227                 : ** this file, all of which may be shared by multiple threads.
   34228                 : **
   34229                 : ** Function winShmMutexHeld() is used to assert() that the global mutex 
   34230                 : ** is held when required. This function is only used as part of assert() 
   34231                 : ** statements. e.g.
   34232                 : **
   34233                 : **   winShmEnterMutex()
   34234                 : **     assert( winShmMutexHeld() );
   34235                 : **   winShmLeaveMutex()
   34236                 : */
   34237                 : static void winShmEnterMutex(void){
   34238                 :   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   34239                 : }
   34240                 : static void winShmLeaveMutex(void){
   34241                 :   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   34242                 : }
   34243                 : #ifdef SQLITE_DEBUG
   34244                 : static int winShmMutexHeld(void) {
   34245                 :   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
   34246                 : }
   34247                 : #endif
   34248                 : 
   34249                 : /*
   34250                 : ** Object used to represent a single file opened and mmapped to provide
   34251                 : ** shared memory.  When multiple threads all reference the same
   34252                 : ** log-summary, each thread has its own winFile object, but they all
   34253                 : ** point to a single instance of this object.  In other words, each
   34254                 : ** log-summary is opened only once per process.
   34255                 : **
   34256                 : ** winShmMutexHeld() must be true when creating or destroying
   34257                 : ** this object or while reading or writing the following fields:
   34258                 : **
   34259                 : **      nRef
   34260                 : **      pNext 
   34261                 : **
   34262                 : ** The following fields are read-only after the object is created:
   34263                 : ** 
   34264                 : **      fid
   34265                 : **      zFilename
   34266                 : **
   34267                 : ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
   34268                 : ** winShmMutexHeld() is true when reading or writing any other field
   34269                 : ** in this structure.
   34270                 : **
   34271                 : */
   34272                 : struct winShmNode {
   34273                 :   sqlite3_mutex *mutex;      /* Mutex to access this object */
   34274                 :   char *zFilename;           /* Name of the file */
   34275                 :   winFile hFile;             /* File handle from winOpen */
   34276                 : 
   34277                 :   int szRegion;              /* Size of shared-memory regions */
   34278                 :   int nRegion;               /* Size of array apRegion */
   34279                 :   struct ShmRegion {
   34280                 :     HANDLE hMap;             /* File handle from CreateFileMapping */
   34281                 :     void *pMap;
   34282                 :   } *aRegion;
   34283                 :   DWORD lastErrno;           /* The Windows errno from the last I/O error */
   34284                 : 
   34285                 :   int nRef;                  /* Number of winShm objects pointing to this */
   34286                 :   winShm *pFirst;            /* All winShm objects pointing to this */
   34287                 :   winShmNode *pNext;         /* Next in list of all winShmNode objects */
   34288                 : #ifdef SQLITE_DEBUG
   34289                 :   u8 nextShmId;              /* Next available winShm.id value */
   34290                 : #endif
   34291                 : };
   34292                 : 
   34293                 : /*
   34294                 : ** A global array of all winShmNode objects.
   34295                 : **
   34296                 : ** The winShmMutexHeld() must be true while reading or writing this list.
   34297                 : */
   34298                 : static winShmNode *winShmNodeList = 0;
   34299                 : 
   34300                 : /*
   34301                 : ** Structure used internally by this VFS to record the state of an
   34302                 : ** open shared memory connection.
   34303                 : **
   34304                 : ** The following fields are initialized when this object is created and
   34305                 : ** are read-only thereafter:
   34306                 : **
   34307                 : **    winShm.pShmNode
   34308                 : **    winShm.id
   34309                 : **
   34310                 : ** All other fields are read/write.  The winShm.pShmNode->mutex must be held
   34311                 : ** while accessing any read/write fields.
   34312                 : */
   34313                 : struct winShm {
   34314                 :   winShmNode *pShmNode;      /* The underlying winShmNode object */
   34315                 :   winShm *pNext;             /* Next winShm with the same winShmNode */
   34316                 :   u8 hasMutex;               /* True if holding the winShmNode mutex */
   34317                 :   u16 sharedMask;            /* Mask of shared locks held */
   34318                 :   u16 exclMask;              /* Mask of exclusive locks held */
   34319                 : #ifdef SQLITE_DEBUG
   34320                 :   u8 id;                     /* Id of this connection with its winShmNode */
   34321                 : #endif
   34322                 : };
   34323                 : 
   34324                 : /*
   34325                 : ** Constants used for locking
   34326                 : */
   34327                 : #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
   34328                 : #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
   34329                 : 
   34330                 : /*
   34331                 : ** Apply advisory locks for all n bytes beginning at ofst.
   34332                 : */
   34333                 : #define _SHM_UNLCK  1
   34334                 : #define _SHM_RDLCK  2
   34335                 : #define _SHM_WRLCK  3
   34336                 : static int winShmSystemLock(
   34337                 :   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
   34338                 :   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
   34339                 :   int ofst,             /* Offset to first byte to be locked/unlocked */
   34340                 :   int nByte             /* Number of bytes to lock or unlock */
   34341                 : ){
   34342                 :   OVERLAPPED ovlp;
   34343                 :   DWORD dwFlags;
   34344                 :   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
   34345                 : 
   34346                 :   /* Access to the winShmNode object is serialized by the caller */
   34347                 :   assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
   34348                 : 
   34349                 :   /* Initialize the locking parameters */
   34350                 :   dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
   34351                 :   if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
   34352                 : 
   34353                 :   memset(&ovlp, 0, sizeof(OVERLAPPED));
   34354                 :   ovlp.Offset = ofst;
   34355                 : 
   34356                 :   /* Release/Acquire the system-level lock */
   34357                 :   if( lockType==_SHM_UNLCK ){
   34358                 :     rc = osUnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
   34359                 :   }else{
   34360                 :     rc = osLockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
   34361                 :   }
   34362                 :   
   34363                 :   if( rc!= 0 ){
   34364                 :     rc = SQLITE_OK;
   34365                 :   }else{
   34366                 :     pFile->lastErrno =  osGetLastError();
   34367                 :     rc = SQLITE_BUSY;
   34368                 :   }
   34369                 : 
   34370                 :   OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n", 
   34371                 :            pFile->hFile.h,
   34372                 :            rc==SQLITE_OK ? "ok" : "failed",
   34373                 :            lockType==_SHM_UNLCK ? "UnlockFileEx" : "LockFileEx",
   34374                 :            pFile->lastErrno));
   34375                 : 
   34376                 :   return rc;
   34377                 : }
   34378                 : 
   34379                 : /* Forward references to VFS methods */
   34380                 : static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
   34381                 : static int winDelete(sqlite3_vfs *,const char*,int);
   34382                 : 
   34383                 : /*
   34384                 : ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
   34385                 : **
   34386                 : ** This is not a VFS shared-memory method; it is a utility function called
   34387                 : ** by VFS shared-memory methods.
   34388                 : */
   34389                 : static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
   34390                 :   winShmNode **pp;
   34391                 :   winShmNode *p;
   34392                 :   BOOL bRc;
   34393                 :   assert( winShmMutexHeld() );
   34394                 :   pp = &winShmNodeList;
   34395                 :   while( (p = *pp)!=0 ){
   34396                 :     if( p->nRef==0 ){
   34397                 :       int i;
   34398                 :       if( p->mutex ) sqlite3_mutex_free(p->mutex);
   34399                 :       for(i=0; i<p->nRegion; i++){
   34400                 :         bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
   34401                 :         OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
   34402                 :                  (int)osGetCurrentProcessId(), i,
   34403                 :                  bRc ? "ok" : "failed"));
   34404                 :         bRc = osCloseHandle(p->aRegion[i].hMap);
   34405                 :         OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
   34406                 :                  (int)osGetCurrentProcessId(), i,
   34407                 :                  bRc ? "ok" : "failed"));
   34408                 :       }
   34409                 :       if( p->hFile.h != INVALID_HANDLE_VALUE ){
   34410                 :         SimulateIOErrorBenign(1);
   34411                 :         winClose((sqlite3_file *)&p->hFile);
   34412                 :         SimulateIOErrorBenign(0);
   34413                 :       }
   34414                 :       if( deleteFlag ){
   34415                 :         SimulateIOErrorBenign(1);
   34416                 :         sqlite3BeginBenignMalloc();
   34417                 :         winDelete(pVfs, p->zFilename, 0);
   34418                 :         sqlite3EndBenignMalloc();
   34419                 :         SimulateIOErrorBenign(0);
   34420                 :       }
   34421                 :       *pp = p->pNext;
   34422                 :       sqlite3_free(p->aRegion);
   34423                 :       sqlite3_free(p);
   34424                 :     }else{
   34425                 :       pp = &p->pNext;
   34426                 :     }
   34427                 :   }
   34428                 : }
   34429                 : 
   34430                 : /*
   34431                 : ** Open the shared-memory area associated with database file pDbFd.
   34432                 : **
   34433                 : ** When opening a new shared-memory file, if no other instances of that
   34434                 : ** file are currently open, in this process or in other processes, then
   34435                 : ** the file must be truncated to zero length or have its header cleared.
   34436                 : */
   34437                 : static int winOpenSharedMemory(winFile *pDbFd){
   34438                 :   struct winShm *p;                  /* The connection to be opened */
   34439                 :   struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
   34440                 :   int rc;                            /* Result code */
   34441                 :   struct winShmNode *pNew;           /* Newly allocated winShmNode */
   34442                 :   int nName;                         /* Size of zName in bytes */
   34443                 : 
   34444                 :   assert( pDbFd->pShm==0 );    /* Not previously opened */
   34445                 : 
   34446                 :   /* Allocate space for the new sqlite3_shm object.  Also speculatively
   34447                 :   ** allocate space for a new winShmNode and filename.
   34448                 :   */
   34449                 :   p = sqlite3_malloc( sizeof(*p) );
   34450                 :   if( p==0 ) return SQLITE_IOERR_NOMEM;
   34451                 :   memset(p, 0, sizeof(*p));
   34452                 :   nName = sqlite3Strlen30(pDbFd->zPath);
   34453                 :   pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
   34454                 :   if( pNew==0 ){
   34455                 :     sqlite3_free(p);
   34456                 :     return SQLITE_IOERR_NOMEM;
   34457                 :   }
   34458                 :   memset(pNew, 0, sizeof(*pNew) + nName + 17);
   34459                 :   pNew->zFilename = (char*)&pNew[1];
   34460                 :   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
   34461                 :   sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
   34462                 : 
   34463                 :   /* Look to see if there is an existing winShmNode that can be used.
   34464                 :   ** If no matching winShmNode currently exists, create a new one.
   34465                 :   */
   34466                 :   winShmEnterMutex();
   34467                 :   for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
   34468                 :     /* TBD need to come up with better match here.  Perhaps
   34469                 :     ** use FILE_ID_BOTH_DIR_INFO Structure.
   34470                 :     */
   34471                 :     if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
   34472                 :   }
   34473                 :   if( pShmNode ){
   34474                 :     sqlite3_free(pNew);
   34475                 :   }else{
   34476                 :     pShmNode = pNew;
   34477                 :     pNew = 0;
   34478                 :     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
   34479                 :     pShmNode->pNext = winShmNodeList;
   34480                 :     winShmNodeList = pShmNode;
   34481                 : 
   34482                 :     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
   34483                 :     if( pShmNode->mutex==0 ){
   34484                 :       rc = SQLITE_IOERR_NOMEM;
   34485                 :       goto shm_open_err;
   34486                 :     }
   34487                 : 
   34488                 :     rc = winOpen(pDbFd->pVfs,
   34489                 :                  pShmNode->zFilename,             /* Name of the file (UTF-8) */
   34490                 :                  (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
   34491                 :                  SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
   34492                 :                  0);
   34493                 :     if( SQLITE_OK!=rc ){
   34494                 :       goto shm_open_err;
   34495                 :     }
   34496                 : 
   34497                 :     /* Check to see if another process is holding the dead-man switch.
   34498                 :     ** If not, truncate the file to zero length. 
   34499                 :     */
   34500                 :     if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
   34501                 :       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
   34502                 :       if( rc!=SQLITE_OK ){
   34503                 :         rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
   34504                 :                  "winOpenShm", pDbFd->zPath);
   34505                 :       }
   34506                 :     }
   34507                 :     if( rc==SQLITE_OK ){
   34508                 :       winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
   34509                 :       rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
   34510                 :     }
   34511                 :     if( rc ) goto shm_open_err;
   34512                 :   }
   34513                 : 
   34514                 :   /* Make the new connection a child of the winShmNode */
   34515                 :   p->pShmNode = pShmNode;
   34516                 : #ifdef SQLITE_DEBUG
   34517                 :   p->id = pShmNode->nextShmId++;
   34518                 : #endif
   34519                 :   pShmNode->nRef++;
   34520                 :   pDbFd->pShm = p;
   34521                 :   winShmLeaveMutex();
   34522                 : 
   34523                 :   /* The reference count on pShmNode has already been incremented under
   34524                 :   ** the cover of the winShmEnterMutex() mutex and the pointer from the
   34525                 :   ** new (struct winShm) object to the pShmNode has been set. All that is
   34526                 :   ** left to do is to link the new object into the linked list starting
   34527                 :   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex 
   34528                 :   ** mutex.
   34529                 :   */
   34530                 :   sqlite3_mutex_enter(pShmNode->mutex);
   34531                 :   p->pNext = pShmNode->pFirst;
   34532                 :   pShmNode->pFirst = p;
   34533                 :   sqlite3_mutex_leave(pShmNode->mutex);
   34534                 :   return SQLITE_OK;
   34535                 : 
   34536                 :   /* Jump here on any error */
   34537                 : shm_open_err:
   34538                 :   winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
   34539                 :   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
   34540                 :   sqlite3_free(p);
   34541                 :   sqlite3_free(pNew);
   34542                 :   winShmLeaveMutex();
   34543                 :   return rc;
   34544                 : }
   34545                 : 
   34546                 : /*
   34547                 : ** Close a connection to shared-memory.  Delete the underlying 
   34548                 : ** storage if deleteFlag is true.
   34549                 : */
   34550                 : static int winShmUnmap(
   34551                 :   sqlite3_file *fd,          /* Database holding shared memory */
   34552                 :   int deleteFlag             /* Delete after closing if true */
   34553                 : ){
   34554                 :   winFile *pDbFd;       /* Database holding shared-memory */
   34555                 :   winShm *p;            /* The connection to be closed */
   34556                 :   winShmNode *pShmNode; /* The underlying shared-memory file */
   34557                 :   winShm **pp;          /* For looping over sibling connections */
   34558                 : 
   34559                 :   pDbFd = (winFile*)fd;
   34560                 :   p = pDbFd->pShm;
   34561                 :   if( p==0 ) return SQLITE_OK;
   34562                 :   pShmNode = p->pShmNode;
   34563                 : 
   34564                 :   /* Remove connection p from the set of connections associated
   34565                 :   ** with pShmNode */
   34566                 :   sqlite3_mutex_enter(pShmNode->mutex);
   34567                 :   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
   34568                 :   *pp = p->pNext;
   34569                 : 
   34570                 :   /* Free the connection p */
   34571                 :   sqlite3_free(p);
   34572                 :   pDbFd->pShm = 0;
   34573                 :   sqlite3_mutex_leave(pShmNode->mutex);
   34574                 : 
   34575                 :   /* If pShmNode->nRef has reached 0, then close the underlying
   34576                 :   ** shared-memory file, too */
   34577                 :   winShmEnterMutex();
   34578                 :   assert( pShmNode->nRef>0 );
   34579                 :   pShmNode->nRef--;
   34580                 :   if( pShmNode->nRef==0 ){
   34581                 :     winShmPurge(pDbFd->pVfs, deleteFlag);
   34582                 :   }
   34583                 :   winShmLeaveMutex();
   34584                 : 
   34585                 :   return SQLITE_OK;
   34586                 : }
   34587                 : 
   34588                 : /*
   34589                 : ** Change the lock state for a shared-memory segment.
   34590                 : */
   34591                 : static int winShmLock(
   34592                 :   sqlite3_file *fd,          /* Database file holding the shared memory */
   34593                 :   int ofst,                  /* First lock to acquire or release */
   34594                 :   int n,                     /* Number of locks to acquire or release */
   34595                 :   int flags                  /* What to do with the lock */
   34596                 : ){
   34597                 :   winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
   34598                 :   winShm *p = pDbFd->pShm;              /* The shared memory being locked */
   34599                 :   winShm *pX;                           /* For looping over all siblings */
   34600                 :   winShmNode *pShmNode = p->pShmNode;
   34601                 :   int rc = SQLITE_OK;                   /* Result code */
   34602                 :   u16 mask;                             /* Mask of locks to take or release */
   34603                 : 
   34604                 :   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
   34605                 :   assert( n>=1 );
   34606                 :   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
   34607                 :        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
   34608                 :        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
   34609                 :        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
   34610                 :   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
   34611                 : 
   34612                 :   mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
   34613                 :   assert( n>1 || mask==(1<<ofst) );
   34614                 :   sqlite3_mutex_enter(pShmNode->mutex);
   34615                 :   if( flags & SQLITE_SHM_UNLOCK ){
   34616                 :     u16 allMask = 0; /* Mask of locks held by siblings */
   34617                 : 
   34618                 :     /* See if any siblings hold this same lock */
   34619                 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   34620                 :       if( pX==p ) continue;
   34621                 :       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
   34622                 :       allMask |= pX->sharedMask;
   34623                 :     }
   34624                 : 
   34625                 :     /* Unlock the system-level locks */
   34626                 :     if( (mask & allMask)==0 ){
   34627                 :       rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
   34628                 :     }else{
   34629                 :       rc = SQLITE_OK;
   34630                 :     }
   34631                 : 
   34632                 :     /* Undo the local locks */
   34633                 :     if( rc==SQLITE_OK ){
   34634                 :       p->exclMask &= ~mask;
   34635                 :       p->sharedMask &= ~mask;
   34636                 :     } 
   34637                 :   }else if( flags & SQLITE_SHM_SHARED ){
   34638                 :     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
   34639                 : 
   34640                 :     /* Find out which shared locks are already held by sibling connections.
   34641                 :     ** If any sibling already holds an exclusive lock, go ahead and return
   34642                 :     ** SQLITE_BUSY.
   34643                 :     */
   34644                 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   34645                 :       if( (pX->exclMask & mask)!=0 ){
   34646                 :         rc = SQLITE_BUSY;
   34647                 :         break;
   34648                 :       }
   34649                 :       allShared |= pX->sharedMask;
   34650                 :     }
   34651                 : 
   34652                 :     /* Get shared locks at the system level, if necessary */
   34653                 :     if( rc==SQLITE_OK ){
   34654                 :       if( (allShared & mask)==0 ){
   34655                 :         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
   34656                 :       }else{
   34657                 :         rc = SQLITE_OK;
   34658                 :       }
   34659                 :     }
   34660                 : 
   34661                 :     /* Get the local shared locks */
   34662                 :     if( rc==SQLITE_OK ){
   34663                 :       p->sharedMask |= mask;
   34664                 :     }
   34665                 :   }else{
   34666                 :     /* Make sure no sibling connections hold locks that will block this
   34667                 :     ** lock.  If any do, return SQLITE_BUSY right away.
   34668                 :     */
   34669                 :     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
   34670                 :       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
   34671                 :         rc = SQLITE_BUSY;
   34672                 :         break;
   34673                 :       }
   34674                 :     }
   34675                 :   
   34676                 :     /* Get the exclusive locks at the system level.  Then if successful
   34677                 :     ** also mark the local connection as being locked.
   34678                 :     */
   34679                 :     if( rc==SQLITE_OK ){
   34680                 :       rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
   34681                 :       if( rc==SQLITE_OK ){
   34682                 :         assert( (p->sharedMask & mask)==0 );
   34683                 :         p->exclMask |= mask;
   34684                 :       }
   34685                 :     }
   34686                 :   }
   34687                 :   sqlite3_mutex_leave(pShmNode->mutex);
   34688                 :   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
   34689                 :            p->id, (int)osGetCurrentProcessId(), p->sharedMask, p->exclMask,
   34690                 :            rc ? "failed" : "ok"));
   34691                 :   return rc;
   34692                 : }
   34693                 : 
   34694                 : /*
   34695                 : ** Implement a memory barrier or memory fence on shared memory.  
   34696                 : **
   34697                 : ** All loads and stores begun before the barrier must complete before
   34698                 : ** any load or store begun after the barrier.
   34699                 : */
   34700                 : static void winShmBarrier(
   34701                 :   sqlite3_file *fd          /* Database holding the shared memory */
   34702                 : ){
   34703                 :   UNUSED_PARAMETER(fd);
   34704                 :   /* MemoryBarrier(); // does not work -- do not know why not */
   34705                 :   winShmEnterMutex();
   34706                 :   winShmLeaveMutex();
   34707                 : }
   34708                 : 
   34709                 : /*
   34710                 : ** This function is called to obtain a pointer to region iRegion of the 
   34711                 : ** shared-memory associated with the database file fd. Shared-memory regions 
   34712                 : ** are numbered starting from zero. Each shared-memory region is szRegion 
   34713                 : ** bytes in size.
   34714                 : **
   34715                 : ** If an error occurs, an error code is returned and *pp is set to NULL.
   34716                 : **
   34717                 : ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
   34718                 : ** region has not been allocated (by any client, including one running in a
   34719                 : ** separate process), then *pp is set to NULL and SQLITE_OK returned. If 
   34720                 : ** isWrite is non-zero and the requested shared-memory region has not yet 
   34721                 : ** been allocated, it is allocated by this function.
   34722                 : **
   34723                 : ** If the shared-memory region has already been allocated or is allocated by
   34724                 : ** this call as described above, then it is mapped into this processes 
   34725                 : ** address space (if it is not already), *pp is set to point to the mapped 
   34726                 : ** memory and SQLITE_OK returned.
   34727                 : */
   34728                 : static int winShmMap(
   34729                 :   sqlite3_file *fd,               /* Handle open on database file */
   34730                 :   int iRegion,                    /* Region to retrieve */
   34731                 :   int szRegion,                   /* Size of regions */
   34732                 :   int isWrite,                    /* True to extend file if necessary */
   34733                 :   void volatile **pp              /* OUT: Mapped memory */
   34734                 : ){
   34735                 :   winFile *pDbFd = (winFile*)fd;
   34736                 :   winShm *p = pDbFd->pShm;
   34737                 :   winShmNode *pShmNode;
   34738                 :   int rc = SQLITE_OK;
   34739                 : 
   34740                 :   if( !p ){
   34741                 :     rc = winOpenSharedMemory(pDbFd);
   34742                 :     if( rc!=SQLITE_OK ) return rc;
   34743                 :     p = pDbFd->pShm;
   34744                 :   }
   34745                 :   pShmNode = p->pShmNode;
   34746                 : 
   34747                 :   sqlite3_mutex_enter(pShmNode->mutex);
   34748                 :   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
   34749                 : 
   34750                 :   if( pShmNode->nRegion<=iRegion ){
   34751                 :     struct ShmRegion *apNew;           /* New aRegion[] array */
   34752                 :     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
   34753                 :     sqlite3_int64 sz;                  /* Current size of wal-index file */
   34754                 : 
   34755                 :     pShmNode->szRegion = szRegion;
   34756                 : 
   34757                 :     /* The requested region is not mapped into this processes address space.
   34758                 :     ** Check to see if it has been allocated (i.e. if the wal-index file is
   34759                 :     ** large enough to contain the requested region).
   34760                 :     */
   34761                 :     rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
   34762                 :     if( rc!=SQLITE_OK ){
   34763                 :       rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
   34764                 :                "winShmMap1", pDbFd->zPath);
   34765                 :       goto shmpage_out;
   34766                 :     }
   34767                 : 
   34768                 :     if( sz<nByte ){
   34769                 :       /* The requested memory region does not exist. If isWrite is set to
   34770                 :       ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
   34771                 :       **
   34772                 :       ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
   34773                 :       ** the requested memory region.
   34774                 :       */
   34775                 :       if( !isWrite ) goto shmpage_out;
   34776                 :       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
   34777                 :       if( rc!=SQLITE_OK ){
   34778                 :         rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
   34779                 :                  "winShmMap2", pDbFd->zPath);
   34780                 :         goto shmpage_out;
   34781                 :       }
   34782                 :     }
   34783                 : 
   34784                 :     /* Map the requested memory region into this processes address space. */
   34785                 :     apNew = (struct ShmRegion *)sqlite3_realloc(
   34786                 :         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
   34787                 :     );
   34788                 :     if( !apNew ){
   34789                 :       rc = SQLITE_IOERR_NOMEM;
   34790                 :       goto shmpage_out;
   34791                 :     }
   34792                 :     pShmNode->aRegion = apNew;
   34793                 : 
   34794                 :     while( pShmNode->nRegion<=iRegion ){
   34795                 :       HANDLE hMap;                /* file-mapping handle */
   34796                 :       void *pMap = 0;             /* Mapped memory region */
   34797                 :      
   34798                 :       hMap = osCreateFileMapping(pShmNode->hFile.h, 
   34799                 :           NULL, PAGE_READWRITE, 0, nByte, NULL
   34800                 :       );
   34801                 :       OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
   34802                 :                (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
   34803                 :                hMap ? "ok" : "failed"));
   34804                 :       if( hMap ){
   34805                 :         int iOffset = pShmNode->nRegion*szRegion;
   34806                 :         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
   34807                 :         pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
   34808                 :             0, iOffset - iOffsetShift, szRegion + iOffsetShift
   34809                 :         );
   34810                 :         OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
   34811                 :                  (int)osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
   34812                 :                  szRegion, pMap ? "ok" : "failed"));
   34813                 :       }
   34814                 :       if( !pMap ){
   34815                 :         pShmNode->lastErrno = osGetLastError();
   34816                 :         rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
   34817                 :                  "winShmMap3", pDbFd->zPath);
   34818                 :         if( hMap ) osCloseHandle(hMap);
   34819                 :         goto shmpage_out;
   34820                 :       }
   34821                 : 
   34822                 :       pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
   34823                 :       pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
   34824                 :       pShmNode->nRegion++;
   34825                 :     }
   34826                 :   }
   34827                 : 
   34828                 : shmpage_out:
   34829                 :   if( pShmNode->nRegion>iRegion ){
   34830                 :     int iOffset = iRegion*szRegion;
   34831                 :     int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
   34832                 :     char *p = (char *)pShmNode->aRegion[iRegion].pMap;
   34833                 :     *pp = (void *)&p[iOffsetShift];
   34834                 :   }else{
   34835                 :     *pp = 0;
   34836                 :   }
   34837                 :   sqlite3_mutex_leave(pShmNode->mutex);
   34838                 :   return rc;
   34839                 : }
   34840                 : 
   34841                 : #else
   34842                 : # define winShmMap     0
   34843                 : # define winShmLock    0
   34844                 : # define winShmBarrier 0
   34845                 : # define winShmUnmap   0
   34846                 : #endif /* #ifndef SQLITE_OMIT_WAL */
   34847                 : 
   34848                 : /*
   34849                 : ** Here ends the implementation of all sqlite3_file methods.
   34850                 : **
   34851                 : ********************** End sqlite3_file Methods *******************************
   34852                 : ******************************************************************************/
   34853                 : 
   34854                 : /*
   34855                 : ** This vector defines all the methods that can operate on an
   34856                 : ** sqlite3_file for win32.
   34857                 : */
   34858                 : static const sqlite3_io_methods winIoMethod = {
   34859                 :   2,                              /* iVersion */
   34860                 :   winClose,                       /* xClose */
   34861                 :   winRead,                        /* xRead */
   34862                 :   winWrite,                       /* xWrite */
   34863                 :   winTruncate,                    /* xTruncate */
   34864                 :   winSync,                        /* xSync */
   34865                 :   winFileSize,                    /* xFileSize */
   34866                 :   winLock,                        /* xLock */
   34867                 :   winUnlock,                      /* xUnlock */
   34868                 :   winCheckReservedLock,           /* xCheckReservedLock */
   34869                 :   winFileControl,                 /* xFileControl */
   34870                 :   winSectorSize,                  /* xSectorSize */
   34871                 :   winDeviceCharacteristics,       /* xDeviceCharacteristics */
   34872                 :   winShmMap,                      /* xShmMap */
   34873                 :   winShmLock,                     /* xShmLock */
   34874                 :   winShmBarrier,                  /* xShmBarrier */
   34875                 :   winShmUnmap                     /* xShmUnmap */
   34876                 : };
   34877                 : 
   34878                 : /****************************************************************************
   34879                 : **************************** sqlite3_vfs methods ****************************
   34880                 : **
   34881                 : ** This division contains the implementation of methods on the
   34882                 : ** sqlite3_vfs object.
   34883                 : */
   34884                 : 
   34885                 : /*
   34886                 : ** Convert a UTF-8 filename into whatever form the underlying
   34887                 : ** operating system wants filenames in.  Space to hold the result
   34888                 : ** is obtained from malloc and must be freed by the calling
   34889                 : ** function.
   34890                 : */
   34891                 : static void *convertUtf8Filename(const char *zFilename){
   34892                 :   void *zConverted = 0;
   34893                 :   if( isNT() ){
   34894                 :     zConverted = utf8ToUnicode(zFilename);
   34895                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   34896                 : */
   34897                 : #if SQLITE_OS_WINCE==0
   34898                 :   }else{
   34899                 :     zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
   34900                 : #endif
   34901                 :   }
   34902                 :   /* caller will handle out of memory */
   34903                 :   return zConverted;
   34904                 : }
   34905                 : 
   34906                 : /*
   34907                 : ** Create a temporary file name in zBuf.  zBuf must be big enough to
   34908                 : ** hold at pVfs->mxPathname characters.
   34909                 : */
   34910                 : static int getTempname(int nBuf, char *zBuf){
   34911                 :   static char zChars[] =
   34912                 :     "abcdefghijklmnopqrstuvwxyz"
   34913                 :     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   34914                 :     "0123456789";
   34915                 :   size_t i, j;
   34916                 :   char zTempPath[MAX_PATH+2];
   34917                 : 
   34918                 :   /* It's odd to simulate an io-error here, but really this is just
   34919                 :   ** using the io-error infrastructure to test that SQLite handles this
   34920                 :   ** function failing. 
   34921                 :   */
   34922                 :   SimulateIOError( return SQLITE_IOERR );
   34923                 : 
   34924                 :   if( sqlite3_temp_directory ){
   34925                 :     sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
   34926                 :   }else if( isNT() ){
   34927                 :     char *zMulti;
   34928                 :     WCHAR zWidePath[MAX_PATH];
   34929                 :     osGetTempPathW(MAX_PATH-30, zWidePath);
   34930                 :     zMulti = unicodeToUtf8(zWidePath);
   34931                 :     if( zMulti ){
   34932                 :       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
   34933                 :       sqlite3_free(zMulti);
   34934                 :     }else{
   34935                 :       return SQLITE_IOERR_NOMEM;
   34936                 :     }
   34937                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   34938                 : ** Since the ANSI version of these Windows API do not exist for WINCE,
   34939                 : ** it's important to not reference them for WINCE builds.
   34940                 : */
   34941                 : #if SQLITE_OS_WINCE==0
   34942                 :   }else{
   34943                 :     char *zUtf8;
   34944                 :     char zMbcsPath[MAX_PATH];
   34945                 :     osGetTempPathA(MAX_PATH-30, zMbcsPath);
   34946                 :     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
   34947                 :     if( zUtf8 ){
   34948                 :       sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
   34949                 :       sqlite3_free(zUtf8);
   34950                 :     }else{
   34951                 :       return SQLITE_IOERR_NOMEM;
   34952                 :     }
   34953                 : #endif
   34954                 :   }
   34955                 : 
   34956                 :   /* Check that the output buffer is large enough for the temporary file 
   34957                 :   ** name. If it is not, return SQLITE_ERROR.
   34958                 :   */
   34959                 :   if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
   34960                 :     return SQLITE_ERROR;
   34961                 :   }
   34962                 : 
   34963                 :   for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
   34964                 :   zTempPath[i] = 0;
   34965                 : 
   34966                 :   sqlite3_snprintf(nBuf-18, zBuf,
   34967                 :                    "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
   34968                 :   j = sqlite3Strlen30(zBuf);
   34969                 :   sqlite3_randomness(15, &zBuf[j]);
   34970                 :   for(i=0; i<15; i++, j++){
   34971                 :     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
   34972                 :   }
   34973                 :   zBuf[j] = 0;
   34974                 :   zBuf[j+1] = 0;
   34975                 : 
   34976                 :   OSTRACE(("TEMP FILENAME: %s\n", zBuf));
   34977                 :   return SQLITE_OK; 
   34978                 : }
   34979                 : 
   34980                 : /*
   34981                 : ** Open a file.
   34982                 : */
   34983                 : static int winOpen(
   34984                 :   sqlite3_vfs *pVfs,        /* Not used */
   34985                 :   const char *zName,        /* Name of the file (UTF-8) */
   34986                 :   sqlite3_file *id,         /* Write the SQLite file handle here */
   34987                 :   int flags,                /* Open mode flags */
   34988                 :   int *pOutFlags            /* Status return flags */
   34989                 : ){
   34990                 :   HANDLE h;
   34991                 :   DWORD lastErrno;
   34992                 :   DWORD dwDesiredAccess;
   34993                 :   DWORD dwShareMode;
   34994                 :   DWORD dwCreationDisposition;
   34995                 :   DWORD dwFlagsAndAttributes = 0;
   34996                 : #if SQLITE_OS_WINCE
   34997                 :   int isTemp = 0;
   34998                 : #endif
   34999                 :   winFile *pFile = (winFile*)id;
   35000                 :   void *zConverted;              /* Filename in OS encoding */
   35001                 :   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
   35002                 :   int cnt = 0;
   35003                 : 
   35004                 :   /* If argument zPath is a NULL pointer, this function is required to open
   35005                 :   ** a temporary file. Use this buffer to store the file name in.
   35006                 :   */
   35007                 :   char zTmpname[MAX_PATH+2];     /* Buffer used to create temp filename */
   35008                 : 
   35009                 :   int rc = SQLITE_OK;            /* Function Return Code */
   35010                 : #if !defined(NDEBUG) || SQLITE_OS_WINCE
   35011                 :   int eType = flags&0xFFFFFF00;  /* Type of file to open */
   35012                 : #endif
   35013                 : 
   35014                 :   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
   35015                 :   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
   35016                 :   int isCreate     = (flags & SQLITE_OPEN_CREATE);
   35017                 : #ifndef NDEBUG
   35018                 :   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
   35019                 : #endif
   35020                 :   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
   35021                 : 
   35022                 : #ifndef NDEBUG
   35023                 :   int isOpenJournal = (isCreate && (
   35024                 :         eType==SQLITE_OPEN_MASTER_JOURNAL 
   35025                 :      || eType==SQLITE_OPEN_MAIN_JOURNAL 
   35026                 :      || eType==SQLITE_OPEN_WAL
   35027                 :   ));
   35028                 : #endif
   35029                 : 
   35030                 :   /* Check the following statements are true: 
   35031                 :   **
   35032                 :   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and 
   35033                 :   **   (b) if CREATE is set, then READWRITE must also be set, and
   35034                 :   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
   35035                 :   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
   35036                 :   */
   35037                 :   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
   35038                 :   assert(isCreate==0 || isReadWrite);
   35039                 :   assert(isExclusive==0 || isCreate);
   35040                 :   assert(isDelete==0 || isCreate);
   35041                 : 
   35042                 :   /* The main DB, main journal, WAL file and master journal are never 
   35043                 :   ** automatically deleted. Nor are they ever temporary files.  */
   35044                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
   35045                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
   35046                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
   35047                 :   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
   35048                 : 
   35049                 :   /* Assert that the upper layer has set one of the "file-type" flags. */
   35050                 :   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
   35051                 :        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
   35052                 :        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
   35053                 :        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
   35054                 :   );
   35055                 : 
   35056                 :   assert( id!=0 );
   35057                 :   UNUSED_PARAMETER(pVfs);
   35058                 : 
   35059                 :   pFile->h = INVALID_HANDLE_VALUE;
   35060                 : 
   35061                 :   /* If the second argument to this function is NULL, generate a 
   35062                 :   ** temporary file name to use 
   35063                 :   */
   35064                 :   if( !zUtf8Name ){
   35065                 :     assert(isDelete && !isOpenJournal);
   35066                 :     rc = getTempname(MAX_PATH+2, zTmpname);
   35067                 :     if( rc!=SQLITE_OK ){
   35068                 :       return rc;
   35069                 :     }
   35070                 :     zUtf8Name = zTmpname;
   35071                 :   }
   35072                 : 
   35073                 :   /* Database filenames are double-zero terminated if they are not
   35074                 :   ** URIs with parameters.  Hence, they can always be passed into
   35075                 :   ** sqlite3_uri_parameter().
   35076                 :   */
   35077                 :   assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
   35078                 :         zUtf8Name[strlen(zUtf8Name)+1]==0 );
   35079                 : 
   35080                 :   /* Convert the filename to the system encoding. */
   35081                 :   zConverted = convertUtf8Filename(zUtf8Name);
   35082                 :   if( zConverted==0 ){
   35083                 :     return SQLITE_IOERR_NOMEM;
   35084                 :   }
   35085                 : 
   35086                 :   if( isReadWrite ){
   35087                 :     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
   35088                 :   }else{
   35089                 :     dwDesiredAccess = GENERIC_READ;
   35090                 :   }
   35091                 : 
   35092                 :   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is 
   35093                 :   ** created. SQLite doesn't use it to indicate "exclusive access" 
   35094                 :   ** as it is usually understood.
   35095                 :   */
   35096                 :   if( isExclusive ){
   35097                 :     /* Creates a new file, only if it does not already exist. */
   35098                 :     /* If the file exists, it fails. */
   35099                 :     dwCreationDisposition = CREATE_NEW;
   35100                 :   }else if( isCreate ){
   35101                 :     /* Open existing file, or create if it doesn't exist */
   35102                 :     dwCreationDisposition = OPEN_ALWAYS;
   35103                 :   }else{
   35104                 :     /* Opens a file, only if it exists. */
   35105                 :     dwCreationDisposition = OPEN_EXISTING;
   35106                 :   }
   35107                 : 
   35108                 :   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
   35109                 : 
   35110                 :   if( isDelete ){
   35111                 : #if SQLITE_OS_WINCE
   35112                 :     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
   35113                 :     isTemp = 1;
   35114                 : #else
   35115                 :     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
   35116                 :                                | FILE_ATTRIBUTE_HIDDEN
   35117                 :                                | FILE_FLAG_DELETE_ON_CLOSE;
   35118                 : #endif
   35119                 :   }else{
   35120                 :     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
   35121                 :   }
   35122                 :   /* Reports from the internet are that performance is always
   35123                 :   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
   35124                 : #if SQLITE_OS_WINCE
   35125                 :   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
   35126                 : #endif
   35127                 : 
   35128                 :   if( isNT() ){
   35129                 :     while( (h = osCreateFileW((LPCWSTR)zConverted,
   35130                 :                               dwDesiredAccess,
   35131                 :                               dwShareMode, NULL,
   35132                 :                               dwCreationDisposition,
   35133                 :                               dwFlagsAndAttributes,
   35134                 :                               NULL))==INVALID_HANDLE_VALUE &&
   35135                 :                               retryIoerr(&cnt, &lastErrno) ){}
   35136                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   35137                 : ** Since the ANSI version of these Windows API do not exist for WINCE,
   35138                 : ** it's important to not reference them for WINCE builds.
   35139                 : */
   35140                 : #if SQLITE_OS_WINCE==0
   35141                 :   }else{
   35142                 :     while( (h = osCreateFileA((LPCSTR)zConverted,
   35143                 :                               dwDesiredAccess,
   35144                 :                               dwShareMode, NULL,
   35145                 :                               dwCreationDisposition,
   35146                 :                               dwFlagsAndAttributes,
   35147                 :                               NULL))==INVALID_HANDLE_VALUE &&
   35148                 :                               retryIoerr(&cnt, &lastErrno) ){}
   35149                 : #endif
   35150                 :   }
   35151                 : 
   35152                 :   logIoerr(cnt);
   35153                 : 
   35154                 :   OSTRACE(("OPEN %d %s 0x%lx %s\n", 
   35155                 :            h, zName, dwDesiredAccess, 
   35156                 :            h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
   35157                 : 
   35158                 :   if( h==INVALID_HANDLE_VALUE ){
   35159                 :     pFile->lastErrno = lastErrno;
   35160                 :     winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
   35161                 :     sqlite3_free(zConverted);
   35162                 :     if( isReadWrite && !isExclusive ){
   35163                 :       return winOpen(pVfs, zName, id, 
   35164                 :              ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
   35165                 :     }else{
   35166                 :       return SQLITE_CANTOPEN_BKPT;
   35167                 :     }
   35168                 :   }
   35169                 : 
   35170                 :   if( pOutFlags ){
   35171                 :     if( isReadWrite ){
   35172                 :       *pOutFlags = SQLITE_OPEN_READWRITE;
   35173                 :     }else{
   35174                 :       *pOutFlags = SQLITE_OPEN_READONLY;
   35175                 :     }
   35176                 :   }
   35177                 : 
   35178                 :   memset(pFile, 0, sizeof(*pFile));
   35179                 :   pFile->pMethod = &winIoMethod;
   35180                 :   pFile->h = h;
   35181                 :   pFile->lastErrno = NO_ERROR;
   35182                 :   pFile->pVfs = pVfs;
   35183                 :   pFile->pShm = 0;
   35184                 :   pFile->zPath = zName;
   35185                 :   if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
   35186                 :     pFile->ctrlFlags |= WINFILE_PSOW;
   35187                 :   }
   35188                 : 
   35189                 : #if SQLITE_OS_WINCE
   35190                 :   if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
   35191                 :        && !winceCreateLock(zName, pFile)
   35192                 :   ){
   35193                 :     osCloseHandle(h);
   35194                 :     sqlite3_free(zConverted);
   35195                 :     return SQLITE_CANTOPEN_BKPT;
   35196                 :   }
   35197                 :   if( isTemp ){
   35198                 :     pFile->zDeleteOnClose = zConverted;
   35199                 :   }else
   35200                 : #endif
   35201                 :   {
   35202                 :     sqlite3_free(zConverted);
   35203                 :   }
   35204                 : 
   35205                 :   OpenCounter(+1);
   35206                 :   return rc;
   35207                 : }
   35208                 : 
   35209                 : /*
   35210                 : ** Delete the named file.
   35211                 : **
   35212                 : ** Note that Windows does not allow a file to be deleted if some other
   35213                 : ** process has it open.  Sometimes a virus scanner or indexing program
   35214                 : ** will open a journal file shortly after it is created in order to do
   35215                 : ** whatever it does.  While this other process is holding the
   35216                 : ** file open, we will be unable to delete it.  To work around this
   35217                 : ** problem, we delay 100 milliseconds and try to delete again.  Up
   35218                 : ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
   35219                 : ** up and returning an error.
   35220                 : */
   35221                 : static int winDelete(
   35222                 :   sqlite3_vfs *pVfs,          /* Not used on win32 */
   35223                 :   const char *zFilename,      /* Name of file to delete */
   35224                 :   int syncDir                 /* Not used on win32 */
   35225                 : ){
   35226                 :   int cnt = 0;
   35227                 :   int rc;
   35228                 :   DWORD lastErrno;
   35229                 :   void *zConverted;
   35230                 :   UNUSED_PARAMETER(pVfs);
   35231                 :   UNUSED_PARAMETER(syncDir);
   35232                 : 
   35233                 :   SimulateIOError(return SQLITE_IOERR_DELETE);
   35234                 :   zConverted = convertUtf8Filename(zFilename);
   35235                 :   if( zConverted==0 ){
   35236                 :     return SQLITE_IOERR_NOMEM;
   35237                 :   }
   35238                 :   if( isNT() ){
   35239                 :     rc = 1;
   35240                 :     while( osGetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
   35241                 :          (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
   35242                 :     rc = rc ? SQLITE_OK : SQLITE_ERROR;
   35243                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   35244                 : ** Since the ANSI version of these Windows API do not exist for WINCE,
   35245                 : ** it's important to not reference them for WINCE builds.
   35246                 : */
   35247                 : #if SQLITE_OS_WINCE==0
   35248                 :   }else{
   35249                 :     rc = 1;
   35250                 :     while( osGetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
   35251                 :          (rc = osDeleteFileA(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
   35252                 :     rc = rc ? SQLITE_OK : SQLITE_ERROR;
   35253                 : #endif
   35254                 :   }
   35255                 :   if( rc ){
   35256                 :     rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
   35257                 :              "winDelete", zFilename);
   35258                 :   }else{
   35259                 :     logIoerr(cnt);
   35260                 :   }
   35261                 :   sqlite3_free(zConverted);
   35262                 :   OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
   35263                 :   return rc;
   35264                 : }
   35265                 : 
   35266                 : /*
   35267                 : ** Check the existance and status of a file.
   35268                 : */
   35269                 : static int winAccess(
   35270                 :   sqlite3_vfs *pVfs,         /* Not used on win32 */
   35271                 :   const char *zFilename,     /* Name of file to check */
   35272                 :   int flags,                 /* Type of test to make on this file */
   35273                 :   int *pResOut               /* OUT: Result */
   35274                 : ){
   35275                 :   DWORD attr;
   35276                 :   int rc = 0;
   35277                 :   DWORD lastErrno;
   35278                 :   void *zConverted;
   35279                 :   UNUSED_PARAMETER(pVfs);
   35280                 : 
   35281                 :   SimulateIOError( return SQLITE_IOERR_ACCESS; );
   35282                 :   zConverted = convertUtf8Filename(zFilename);
   35283                 :   if( zConverted==0 ){
   35284                 :     return SQLITE_IOERR_NOMEM;
   35285                 :   }
   35286                 :   if( isNT() ){
   35287                 :     int cnt = 0;
   35288                 :     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
   35289                 :     memset(&sAttrData, 0, sizeof(sAttrData));
   35290                 :     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
   35291                 :                              GetFileExInfoStandard, 
   35292                 :                              &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
   35293                 :     if( rc ){
   35294                 :       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
   35295                 :       ** as if it does not exist.
   35296                 :       */
   35297                 :       if(    flags==SQLITE_ACCESS_EXISTS
   35298                 :           && sAttrData.nFileSizeHigh==0 
   35299                 :           && sAttrData.nFileSizeLow==0 ){
   35300                 :         attr = INVALID_FILE_ATTRIBUTES;
   35301                 :       }else{
   35302                 :         attr = sAttrData.dwFileAttributes;
   35303                 :       }
   35304                 :     }else{
   35305                 :       logIoerr(cnt);
   35306                 :       if( lastErrno!=ERROR_FILE_NOT_FOUND ){
   35307                 :         winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
   35308                 :         sqlite3_free(zConverted);
   35309                 :         return SQLITE_IOERR_ACCESS;
   35310                 :       }else{
   35311                 :         attr = INVALID_FILE_ATTRIBUTES;
   35312                 :       }
   35313                 :     }
   35314                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   35315                 : ** Since the ANSI version of these Windows API do not exist for WINCE,
   35316                 : ** it's important to not reference them for WINCE builds.
   35317                 : */
   35318                 : #if SQLITE_OS_WINCE==0
   35319                 :   }else{
   35320                 :     attr = osGetFileAttributesA((char*)zConverted);
   35321                 : #endif
   35322                 :   }
   35323                 :   sqlite3_free(zConverted);
   35324                 :   switch( flags ){
   35325                 :     case SQLITE_ACCESS_READ:
   35326                 :     case SQLITE_ACCESS_EXISTS:
   35327                 :       rc = attr!=INVALID_FILE_ATTRIBUTES;
   35328                 :       break;
   35329                 :     case SQLITE_ACCESS_READWRITE:
   35330                 :       rc = attr!=INVALID_FILE_ATTRIBUTES &&
   35331                 :              (attr & FILE_ATTRIBUTE_READONLY)==0;
   35332                 :       break;
   35333                 :     default:
   35334                 :       assert(!"Invalid flags argument");
   35335                 :   }
   35336                 :   *pResOut = rc;
   35337                 :   return SQLITE_OK;
   35338                 : }
   35339                 : 
   35340                 : 
   35341                 : /*
   35342                 : ** Turn a relative pathname into a full pathname.  Write the full
   35343                 : ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
   35344                 : ** bytes in size.
   35345                 : */
   35346                 : static int winFullPathname(
   35347                 :   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
   35348                 :   const char *zRelative,        /* Possibly relative input path */
   35349                 :   int nFull,                    /* Size of output buffer in bytes */
   35350                 :   char *zFull                   /* Output buffer */
   35351                 : ){
   35352                 :   
   35353                 : #if defined(__CYGWIN__)
   35354                 :   SimulateIOError( return SQLITE_ERROR );
   35355                 :   UNUSED_PARAMETER(nFull);
   35356                 :   cygwin_conv_to_full_win32_path(zRelative, zFull);
   35357                 :   return SQLITE_OK;
   35358                 : #endif
   35359                 : 
   35360                 : #if SQLITE_OS_WINCE
   35361                 :   SimulateIOError( return SQLITE_ERROR );
   35362                 :   UNUSED_PARAMETER(nFull);
   35363                 :   /* WinCE has no concept of a relative pathname, or so I am told. */
   35364                 :   sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
   35365                 :   return SQLITE_OK;
   35366                 : #endif
   35367                 : 
   35368                 : #if !SQLITE_OS_WINCE && !defined(__CYGWIN__)
   35369                 :   int nByte;
   35370                 :   void *zConverted;
   35371                 :   char *zOut;
   35372                 : 
   35373                 :   /* If this path name begins with "/X:", where "X" is any alphabetic
   35374                 :   ** character, discard the initial "/" from the pathname.
   35375                 :   */
   35376                 :   if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
   35377                 :     zRelative++;
   35378                 :   }
   35379                 : 
   35380                 :   /* It's odd to simulate an io-error here, but really this is just
   35381                 :   ** using the io-error infrastructure to test that SQLite handles this
   35382                 :   ** function failing. This function could fail if, for example, the
   35383                 :   ** current working directory has been unlinked.
   35384                 :   */
   35385                 :   SimulateIOError( return SQLITE_ERROR );
   35386                 :   UNUSED_PARAMETER(nFull);
   35387                 :   zConverted = convertUtf8Filename(zRelative);
   35388                 :   if( zConverted==0 ){
   35389                 :     return SQLITE_IOERR_NOMEM;
   35390                 :   }
   35391                 :   if( isNT() ){
   35392                 :     LPWSTR zTemp;
   35393                 :     nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
   35394                 :     zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
   35395                 :     if( zTemp==0 ){
   35396                 :       sqlite3_free(zConverted);
   35397                 :       return SQLITE_IOERR_NOMEM;
   35398                 :     }
   35399                 :     osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
   35400                 :     sqlite3_free(zConverted);
   35401                 :     zOut = unicodeToUtf8(zTemp);
   35402                 :     sqlite3_free(zTemp);
   35403                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   35404                 : ** Since the ANSI version of these Windows API do not exist for WINCE,
   35405                 : ** it's important to not reference them for WINCE builds.
   35406                 : */
   35407                 : #if SQLITE_OS_WINCE==0
   35408                 :   }else{
   35409                 :     char *zTemp;
   35410                 :     nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
   35411                 :     zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
   35412                 :     if( zTemp==0 ){
   35413                 :       sqlite3_free(zConverted);
   35414                 :       return SQLITE_IOERR_NOMEM;
   35415                 :     }
   35416                 :     osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
   35417                 :     sqlite3_free(zConverted);
   35418                 :     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
   35419                 :     sqlite3_free(zTemp);
   35420                 : #endif
   35421                 :   }
   35422                 :   if( zOut ){
   35423                 :     sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
   35424                 :     sqlite3_free(zOut);
   35425                 :     return SQLITE_OK;
   35426                 :   }else{
   35427                 :     return SQLITE_IOERR_NOMEM;
   35428                 :   }
   35429                 : #endif
   35430                 : }
   35431                 : 
   35432                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
   35433                 : /*
   35434                 : ** Interfaces for opening a shared library, finding entry points
   35435                 : ** within the shared library, and closing the shared library.
   35436                 : */
   35437                 : /*
   35438                 : ** Interfaces for opening a shared library, finding entry points
   35439                 : ** within the shared library, and closing the shared library.
   35440                 : */
   35441                 : static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
   35442                 :   HANDLE h;
   35443                 :   void *zConverted = convertUtf8Filename(zFilename);
   35444                 :   UNUSED_PARAMETER(pVfs);
   35445                 :   if( zConverted==0 ){
   35446                 :     return 0;
   35447                 :   }
   35448                 :   if( isNT() ){
   35449                 :     h = osLoadLibraryW((LPCWSTR)zConverted);
   35450                 : /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed. 
   35451                 : ** Since the ANSI version of these Windows API do not exist for WINCE,
   35452                 : ** it's important to not reference them for WINCE builds.
   35453                 : */
   35454                 : #if SQLITE_OS_WINCE==0
   35455                 :   }else{
   35456                 :     h = osLoadLibraryA((char*)zConverted);
   35457                 : #endif
   35458                 :   }
   35459                 :   sqlite3_free(zConverted);
   35460                 :   return (void*)h;
   35461                 : }
   35462                 : static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
   35463                 :   UNUSED_PARAMETER(pVfs);
   35464                 :   getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
   35465                 : }
   35466                 : static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
   35467                 :   UNUSED_PARAMETER(pVfs);
   35468                 :   return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol);
   35469                 : }
   35470                 : static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
   35471                 :   UNUSED_PARAMETER(pVfs);
   35472                 :   osFreeLibrary((HANDLE)pHandle);
   35473                 : }
   35474                 : #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
   35475                 :   #define winDlOpen  0
   35476                 :   #define winDlError 0
   35477                 :   #define winDlSym   0
   35478                 :   #define winDlClose 0
   35479                 : #endif
   35480                 : 
   35481                 : 
   35482                 : /*
   35483                 : ** Write up to nBuf bytes of randomness into zBuf.
   35484                 : */
   35485                 : static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   35486                 :   int n = 0;
   35487                 :   UNUSED_PARAMETER(pVfs);
   35488                 : #if defined(SQLITE_TEST)
   35489                 :   n = nBuf;
   35490                 :   memset(zBuf, 0, nBuf);
   35491                 : #else
   35492                 :   if( sizeof(SYSTEMTIME)<=nBuf-n ){
   35493                 :     SYSTEMTIME x;
   35494                 :     osGetSystemTime(&x);
   35495                 :     memcpy(&zBuf[n], &x, sizeof(x));
   35496                 :     n += sizeof(x);
   35497                 :   }
   35498                 :   if( sizeof(DWORD)<=nBuf-n ){
   35499                 :     DWORD pid = osGetCurrentProcessId();
   35500                 :     memcpy(&zBuf[n], &pid, sizeof(pid));
   35501                 :     n += sizeof(pid);
   35502                 :   }
   35503                 :   if( sizeof(DWORD)<=nBuf-n ){
   35504                 :     DWORD cnt = osGetTickCount();
   35505                 :     memcpy(&zBuf[n], &cnt, sizeof(cnt));
   35506                 :     n += sizeof(cnt);
   35507                 :   }
   35508                 :   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
   35509                 :     LARGE_INTEGER i;
   35510                 :     osQueryPerformanceCounter(&i);
   35511                 :     memcpy(&zBuf[n], &i, sizeof(i));
   35512                 :     n += sizeof(i);
   35513                 :   }
   35514                 : #endif
   35515                 :   return n;
   35516                 : }
   35517                 : 
   35518                 : 
   35519                 : /*
   35520                 : ** Sleep for a little while.  Return the amount of time slept.
   35521                 : */
   35522                 : static int winSleep(sqlite3_vfs *pVfs, int microsec){
   35523                 :   osSleep((microsec+999)/1000);
   35524                 :   UNUSED_PARAMETER(pVfs);
   35525                 :   return ((microsec+999)/1000)*1000;
   35526                 : }
   35527                 : 
   35528                 : /*
   35529                 : ** The following variable, if set to a non-zero value, is interpreted as
   35530                 : ** the number of seconds since 1970 and is used to set the result of
   35531                 : ** sqlite3OsCurrentTime() during testing.
   35532                 : */
   35533                 : #ifdef SQLITE_TEST
   35534                 : SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
   35535                 : #endif
   35536                 : 
   35537                 : /*
   35538                 : ** Find the current time (in Universal Coordinated Time).  Write into *piNow
   35539                 : ** the current time and date as a Julian Day number times 86_400_000.  In
   35540                 : ** other words, write into *piNow the number of milliseconds since the Julian
   35541                 : ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
   35542                 : ** proleptic Gregorian calendar.
   35543                 : **
   35544                 : ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date 
   35545                 : ** cannot be found.
   35546                 : */
   35547                 : static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
   35548                 :   /* FILETIME structure is a 64-bit value representing the number of 
   35549                 :      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). 
   35550                 :   */
   35551                 :   FILETIME ft;
   35552                 :   static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
   35553                 : #ifdef SQLITE_TEST
   35554                 :   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
   35555                 : #endif
   35556                 :   /* 2^32 - to avoid use of LL and warnings in gcc */
   35557                 :   static const sqlite3_int64 max32BitValue = 
   35558                 :       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
   35559                 : 
   35560                 : #if SQLITE_OS_WINCE
   35561                 :   SYSTEMTIME time;
   35562                 :   osGetSystemTime(&time);
   35563                 :   /* if SystemTimeToFileTime() fails, it returns zero. */
   35564                 :   if (!osSystemTimeToFileTime(&time,&ft)){
   35565                 :     return SQLITE_ERROR;
   35566                 :   }
   35567                 : #else
   35568                 :   osGetSystemTimeAsFileTime( &ft );
   35569                 : #endif
   35570                 : 
   35571                 :   *piNow = winFiletimeEpoch +
   35572                 :             ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + 
   35573                 :                (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
   35574                 : 
   35575                 : #ifdef SQLITE_TEST
   35576                 :   if( sqlite3_current_time ){
   35577                 :     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
   35578                 :   }
   35579                 : #endif
   35580                 :   UNUSED_PARAMETER(pVfs);
   35581                 :   return SQLITE_OK;
   35582                 : }
   35583                 : 
   35584                 : /*
   35585                 : ** Find the current time (in Universal Coordinated Time).  Write the
   35586                 : ** current time and date as a Julian Day number into *prNow and
   35587                 : ** return 0.  Return 1 if the time and date cannot be found.
   35588                 : */
   35589                 : static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
   35590                 :   int rc;
   35591                 :   sqlite3_int64 i;
   35592                 :   rc = winCurrentTimeInt64(pVfs, &i);
   35593                 :   if( !rc ){
   35594                 :     *prNow = i/86400000.0;
   35595                 :   }
   35596                 :   return rc;
   35597                 : }
   35598                 : 
   35599                 : /*
   35600                 : ** The idea is that this function works like a combination of
   35601                 : ** GetLastError() and FormatMessage() on Windows (or errno and
   35602                 : ** strerror_r() on Unix). After an error is returned by an OS
   35603                 : ** function, SQLite calls this function with zBuf pointing to
   35604                 : ** a buffer of nBuf bytes. The OS layer should populate the
   35605                 : ** buffer with a nul-terminated UTF-8 encoded error message
   35606                 : ** describing the last IO error to have occurred within the calling
   35607                 : ** thread.
   35608                 : **
   35609                 : ** If the error message is too large for the supplied buffer,
   35610                 : ** it should be truncated. The return value of xGetLastError
   35611                 : ** is zero if the error message fits in the buffer, or non-zero
   35612                 : ** otherwise (if the message was truncated). If non-zero is returned,
   35613                 : ** then it is not necessary to include the nul-terminator character
   35614                 : ** in the output buffer.
   35615                 : **
   35616                 : ** Not supplying an error message will have no adverse effect
   35617                 : ** on SQLite. It is fine to have an implementation that never
   35618                 : ** returns an error message:
   35619                 : **
   35620                 : **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   35621                 : **     assert(zBuf[0]=='\0');
   35622                 : **     return 0;
   35623                 : **   }
   35624                 : **
   35625                 : ** However if an error message is supplied, it will be incorporated
   35626                 : ** by sqlite into the error message available to the user using
   35627                 : ** sqlite3_errmsg(), possibly making IO errors easier to debug.
   35628                 : */
   35629                 : static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
   35630                 :   UNUSED_PARAMETER(pVfs);
   35631                 :   return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
   35632                 : }
   35633                 : 
   35634                 : /*
   35635                 : ** Initialize and deinitialize the operating system interface.
   35636                 : */
   35637                 : SQLITE_API int sqlite3_os_init(void){
   35638                 :   static sqlite3_vfs winVfs = {
   35639                 :     3,                   /* iVersion */
   35640                 :     sizeof(winFile),     /* szOsFile */
   35641                 :     MAX_PATH,            /* mxPathname */
   35642                 :     0,                   /* pNext */
   35643                 :     "win32",             /* zName */
   35644                 :     0,                   /* pAppData */
   35645                 :     winOpen,             /* xOpen */
   35646                 :     winDelete,           /* xDelete */
   35647                 :     winAccess,           /* xAccess */
   35648                 :     winFullPathname,     /* xFullPathname */
   35649                 :     winDlOpen,           /* xDlOpen */
   35650                 :     winDlError,          /* xDlError */
   35651                 :     winDlSym,            /* xDlSym */
   35652                 :     winDlClose,          /* xDlClose */
   35653                 :     winRandomness,       /* xRandomness */
   35654                 :     winSleep,            /* xSleep */
   35655                 :     winCurrentTime,      /* xCurrentTime */
   35656                 :     winGetLastError,     /* xGetLastError */
   35657                 :     winCurrentTimeInt64, /* xCurrentTimeInt64 */
   35658                 :     winSetSystemCall,    /* xSetSystemCall */
   35659                 :     winGetSystemCall,    /* xGetSystemCall */
   35660                 :     winNextSystemCall,   /* xNextSystemCall */
   35661                 :   };
   35662                 : 
   35663                 :   /* Double-check that the aSyscall[] array has been constructed
   35664                 :   ** correctly.  See ticket [bb3a86e890c8e96ab] */
   35665                 :   assert( ArraySize(aSyscall)==60 );
   35666                 : 
   35667                 : #ifndef SQLITE_OMIT_WAL
   35668                 :   /* get memory map allocation granularity */
   35669                 :   memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
   35670                 :   osGetSystemInfo(&winSysInfo);
   35671                 :   assert(winSysInfo.dwAllocationGranularity > 0);
   35672                 : #endif
   35673                 : 
   35674                 :   sqlite3_vfs_register(&winVfs, 1);
   35675                 :   return SQLITE_OK; 
   35676                 : }
   35677                 : 
   35678                 : SQLITE_API int sqlite3_os_end(void){ 
   35679                 :   return SQLITE_OK;
   35680                 : }
   35681                 : 
   35682                 : #endif /* SQLITE_OS_WIN */
   35683                 : 
   35684                 : /************** End of os_win.c **********************************************/
   35685                 : /************** Begin file bitvec.c ******************************************/
   35686                 : /*
   35687                 : ** 2008 February 16
   35688                 : **
   35689                 : ** The author disclaims copyright to this source code.  In place of
   35690                 : ** a legal notice, here is a blessing:
   35691                 : **
   35692                 : **    May you do good and not evil.
   35693                 : **    May you find forgiveness for yourself and forgive others.
   35694                 : **    May you share freely, never taking more than you give.
   35695                 : **
   35696                 : *************************************************************************
   35697                 : ** This file implements an object that represents a fixed-length
   35698                 : ** bitmap.  Bits are numbered starting with 1.
   35699                 : **
   35700                 : ** A bitmap is used to record which pages of a database file have been
   35701                 : ** journalled during a transaction, or which pages have the "dont-write"
   35702                 : ** property.  Usually only a few pages are meet either condition.
   35703                 : ** So the bitmap is usually sparse and has low cardinality.
   35704                 : ** But sometimes (for example when during a DROP of a large table) most
   35705                 : ** or all of the pages in a database can get journalled.  In those cases, 
   35706                 : ** the bitmap becomes dense with high cardinality.  The algorithm needs 
   35707                 : ** to handle both cases well.
   35708                 : **
   35709                 : ** The size of the bitmap is fixed when the object is created.
   35710                 : **
   35711                 : ** All bits are clear when the bitmap is created.  Individual bits
   35712                 : ** may be set or cleared one at a time.
   35713                 : **
   35714                 : ** Test operations are about 100 times more common that set operations.
   35715                 : ** Clear operations are exceedingly rare.  There are usually between
   35716                 : ** 5 and 500 set operations per Bitvec object, though the number of sets can
   35717                 : ** sometimes grow into tens of thousands or larger.  The size of the
   35718                 : ** Bitvec object is the number of pages in the database file at the
   35719                 : ** start of a transaction, and is thus usually less than a few thousand,
   35720                 : ** but can be as large as 2 billion for a really big database.
   35721                 : */
   35722                 : 
   35723                 : /* Size of the Bitvec structure in bytes. */
   35724                 : #define BITVEC_SZ        512
   35725                 : 
   35726                 : /* Round the union size down to the nearest pointer boundary, since that's how 
   35727                 : ** it will be aligned within the Bitvec struct. */
   35728                 : #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
   35729                 : 
   35730                 : /* Type of the array "element" for the bitmap representation. 
   35731                 : ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. 
   35732                 : ** Setting this to the "natural word" size of your CPU may improve
   35733                 : ** performance. */
   35734                 : #define BITVEC_TELEM     u8
   35735                 : /* Size, in bits, of the bitmap element. */
   35736                 : #define BITVEC_SZELEM    8
   35737                 : /* Number of elements in a bitmap array. */
   35738                 : #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
   35739                 : /* Number of bits in the bitmap array. */
   35740                 : #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
   35741                 : 
   35742                 : /* Number of u32 values in hash table. */
   35743                 : #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
   35744                 : /* Maximum number of entries in hash table before 
   35745                 : ** sub-dividing and re-hashing. */
   35746                 : #define BITVEC_MXHASH    (BITVEC_NINT/2)
   35747                 : /* Hashing function for the aHash representation.
   35748                 : ** Empirical testing showed that the *37 multiplier 
   35749                 : ** (an arbitrary prime)in the hash function provided 
   35750                 : ** no fewer collisions than the no-op *1. */
   35751                 : #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
   35752                 : 
   35753                 : #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
   35754                 : 
   35755                 : 
   35756                 : /*
   35757                 : ** A bitmap is an instance of the following structure.
   35758                 : **
   35759                 : ** This bitmap records the existance of zero or more bits
   35760                 : ** with values between 1 and iSize, inclusive.
   35761                 : **
   35762                 : ** There are three possible representations of the bitmap.
   35763                 : ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
   35764                 : ** bitmap.  The least significant bit is bit 1.
   35765                 : **
   35766                 : ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
   35767                 : ** a hash table that will hold up to BITVEC_MXHASH distinct values.
   35768                 : **
   35769                 : ** Otherwise, the value i is redirected into one of BITVEC_NPTR
   35770                 : ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
   35771                 : ** handles up to iDivisor separate values of i.  apSub[0] holds
   35772                 : ** values between 1 and iDivisor.  apSub[1] holds values between
   35773                 : ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
   35774                 : ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
   35775                 : ** to hold deal with values between 1 and iDivisor.
   35776                 : */
   35777                 : struct Bitvec {
   35778                 :   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
   35779                 :   u32 nSet;       /* Number of bits that are set - only valid for aHash
   35780                 :                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
   35781                 :                   ** this would be 125. */
   35782                 :   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
   35783                 :                   /* Should >=0 for apSub element. */
   35784                 :                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
   35785                 :                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
   35786                 :   union {
   35787                 :     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
   35788                 :     u32 aHash[BITVEC_NINT];      /* Hash table representation */
   35789                 :     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
   35790                 :   } u;
   35791                 : };
   35792                 : 
   35793                 : /*
   35794                 : ** Create a new bitmap object able to handle bits between 0 and iSize,
   35795                 : ** inclusive.  Return a pointer to the new object.  Return NULL if 
   35796                 : ** malloc fails.
   35797                 : */
   35798           33630 : SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
   35799                 :   Bitvec *p;
   35800                 :   assert( sizeof(*p)==BITVEC_SZ );
   35801           33630 :   p = sqlite3MallocZero( sizeof(*p) );
   35802           33630 :   if( p ){
   35803           33630 :     p->iSize = iSize;
   35804                 :   }
   35805           33630 :   return p;
   35806                 : }
   35807                 : 
   35808                 : /*
   35809                 : ** Check to see if the i-th bit is set.  Return true or false.
   35810                 : ** If p is NULL (if the bitmap has not been created) or if
   35811                 : ** i is out of range, then return false.
   35812                 : */
   35813         1158596 : SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
   35814         1158596 :   if( p==0 ) return 0;
   35815          507827 :   if( i>p->iSize || i==0 ) return 0;
   35816          438858 :   i--;
   35817          877716 :   while( p->iDivisor ){
   35818               0 :     u32 bin = i/p->iDivisor;
   35819               0 :     i = i%p->iDivisor;
   35820               0 :     p = p->u.apSub[bin];
   35821               0 :     if (!p) {
   35822               0 :       return 0;
   35823                 :     }
   35824                 :   }
   35825          438858 :   if( p->iSize<=BITVEC_NBIT ){
   35826          438858 :     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
   35827                 :   } else{
   35828               0 :     u32 h = BITVEC_HASH(i++);
   35829               0 :     while( p->u.aHash[h] ){
   35830               0 :       if( p->u.aHash[h]==i ) return 1;
   35831               0 :       h = (h+1) % BITVEC_NINT;
   35832                 :     }
   35833               0 :     return 0;
   35834                 :   }
   35835                 : }
   35836                 : 
   35837                 : /*
   35838                 : ** Set the i-th bit.  Return 0 on success and an error code if
   35839                 : ** anything goes wrong.
   35840                 : **
   35841                 : ** This routine might cause sub-bitmaps to be allocated.  Failing
   35842                 : ** to get the memory needed to hold the sub-bitmap is the only
   35843                 : ** that can go wrong with an insert, assuming p and i are valid.
   35844                 : **
   35845                 : ** The calling function must ensure that p is a valid Bitvec object
   35846                 : ** and that the value for "i" is within range of the Bitvec object.
   35847                 : ** Otherwise the behavior is undefined.
   35848                 : */
   35849           90130 : SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
   35850                 :   u32 h;
   35851           90130 :   if( p==0 ) return SQLITE_OK;
   35852           90073 :   assert( i>0 );
   35853           90073 :   assert( i<=p->iSize );
   35854           90073 :   i--;
   35855          180146 :   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
   35856               0 :     u32 bin = i/p->iDivisor;
   35857               0 :     i = i%p->iDivisor;
   35858               0 :     if( p->u.apSub[bin]==0 ){
   35859               0 :       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
   35860               0 :       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
   35861                 :     }
   35862               0 :     p = p->u.apSub[bin];
   35863                 :   }
   35864           90073 :   if( p->iSize<=BITVEC_NBIT ){
   35865           90073 :     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
   35866           90073 :     return SQLITE_OK;
   35867                 :   }
   35868               0 :   h = BITVEC_HASH(i++);
   35869                 :   /* if there wasn't a hash collision, and this doesn't */
   35870                 :   /* completely fill the hash, then just add it without */
   35871                 :   /* worring about sub-dividing and re-hashing. */
   35872               0 :   if( !p->u.aHash[h] ){
   35873               0 :     if (p->nSet<(BITVEC_NINT-1)) {
   35874               0 :       goto bitvec_set_end;
   35875                 :     } else {
   35876               0 :       goto bitvec_set_rehash;
   35877                 :     }
   35878                 :   }
   35879                 :   /* there was a collision, check to see if it's already */
   35880                 :   /* in hash, if not, try to find a spot for it */
   35881                 :   do {
   35882               0 :     if( p->u.aHash[h]==i ) return SQLITE_OK;
   35883               0 :     h++;
   35884               0 :     if( h>=BITVEC_NINT ) h = 0;
   35885               0 :   } while( p->u.aHash[h] );
   35886                 :   /* we didn't find it in the hash.  h points to the first */
   35887                 :   /* available free spot. check to see if this is going to */
   35888                 :   /* make our hash too "full".  */
   35889                 : bitvec_set_rehash:
   35890               0 :   if( p->nSet>=BITVEC_MXHASH ){
   35891                 :     unsigned int j;
   35892                 :     int rc;
   35893               0 :     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
   35894               0 :     if( aiValues==0 ){
   35895               0 :       return SQLITE_NOMEM;
   35896                 :     }else{
   35897               0 :       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
   35898               0 :       memset(p->u.apSub, 0, sizeof(p->u.apSub));
   35899               0 :       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
   35900               0 :       rc = sqlite3BitvecSet(p, i);
   35901               0 :       for(j=0; j<BITVEC_NINT; j++){
   35902               0 :         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
   35903                 :       }
   35904               0 :       sqlite3StackFree(0, aiValues);
   35905               0 :       return rc;
   35906                 :     }
   35907                 :   }
   35908                 : bitvec_set_end:
   35909               0 :   p->nSet++;
   35910               0 :   p->u.aHash[h] = i;
   35911               0 :   return SQLITE_OK;
   35912                 : }
   35913                 : 
   35914                 : /*
   35915                 : ** Clear the i-th bit.
   35916                 : **
   35917                 : ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
   35918                 : ** that BitvecClear can use to rebuilt its hash table.
   35919                 : */
   35920               0 : SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
   35921               0 :   if( p==0 ) return;
   35922               0 :   assert( i>0 );
   35923               0 :   i--;
   35924               0 :   while( p->iDivisor ){
   35925               0 :     u32 bin = i/p->iDivisor;
   35926               0 :     i = i%p->iDivisor;
   35927               0 :     p = p->u.apSub[bin];
   35928               0 :     if (!p) {
   35929               0 :       return;
   35930                 :     }
   35931                 :   }
   35932               0 :   if( p->iSize<=BITVEC_NBIT ){
   35933               0 :     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
   35934                 :   }else{
   35935                 :     unsigned int j;
   35936               0 :     u32 *aiValues = pBuf;
   35937               0 :     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
   35938               0 :     memset(p->u.aHash, 0, sizeof(p->u.aHash));
   35939               0 :     p->nSet = 0;
   35940               0 :     for(j=0; j<BITVEC_NINT; j++){
   35941               0 :       if( aiValues[j] && aiValues[j]!=(i+1) ){
   35942               0 :         u32 h = BITVEC_HASH(aiValues[j]-1);
   35943               0 :         p->nSet++;
   35944               0 :         while( p->u.aHash[h] ){
   35945               0 :           h++;
   35946               0 :           if( h>=BITVEC_NINT ) h = 0;
   35947                 :         }
   35948               0 :         p->u.aHash[h] = aiValues[j];
   35949                 :       }
   35950                 :     }
   35951                 :   }
   35952                 : }
   35953                 : 
   35954                 : /*
   35955                 : ** Destroy a bitmap object.  Reclaim all memory used.
   35956                 : */
   35957          305211 : SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
   35958          305211 :   if( p==0 ) return;
   35959           33630 :   if( p->iDivisor ){
   35960                 :     unsigned int i;
   35961               0 :     for(i=0; i<BITVEC_NPTR; i++){
   35962               0 :       sqlite3BitvecDestroy(p->u.apSub[i]);
   35963                 :     }
   35964                 :   }
   35965           33630 :   sqlite3_free(p);
   35966                 : }
   35967                 : 
   35968                 : /*
   35969                 : ** Return the value of the iSize parameter specified when Bitvec *p
   35970                 : ** was created.
   35971                 : */
   35972              70 : SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
   35973              70 :   return p->iSize;
   35974                 : }
   35975                 : 
   35976                 : #ifndef SQLITE_OMIT_BUILTIN_TEST
   35977                 : /*
   35978                 : ** Let V[] be an array of unsigned characters sufficient to hold
   35979                 : ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
   35980                 : ** Then the following macros can be used to set, clear, or test
   35981                 : ** individual bits within V.
   35982                 : */
   35983                 : #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
   35984                 : #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
   35985                 : #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
   35986                 : 
   35987                 : /*
   35988                 : ** This routine runs an extensive test of the Bitvec code.
   35989                 : **
   35990                 : ** The input is an array of integers that acts as a program
   35991                 : ** to test the Bitvec.  The integers are opcodes followed
   35992                 : ** by 0, 1, or 3 operands, depending on the opcode.  Another
   35993                 : ** opcode follows immediately after the last operand.
   35994                 : **
   35995                 : ** There are 6 opcodes numbered from 0 through 5.  0 is the
   35996                 : ** "halt" opcode and causes the test to end.
   35997                 : **
   35998                 : **    0          Halt and return the number of errors
   35999                 : **    1 N S X    Set N bits beginning with S and incrementing by X
   36000                 : **    2 N S X    Clear N bits beginning with S and incrementing by X
   36001                 : **    3 N        Set N randomly chosen bits
   36002                 : **    4 N        Clear N randomly chosen bits
   36003                 : **    5 N S X    Set N bits from S increment X in array only, not in bitvec
   36004                 : **
   36005                 : ** The opcodes 1 through 4 perform set and clear operations are performed
   36006                 : ** on both a Bitvec object and on a linear array of bits obtained from malloc.
   36007                 : ** Opcode 5 works on the linear array only, not on the Bitvec.
   36008                 : ** Opcode 5 is used to deliberately induce a fault in order to
   36009                 : ** confirm that error detection works.
   36010                 : **
   36011                 : ** At the conclusion of the test the linear array is compared
   36012                 : ** against the Bitvec object.  If there are any differences,
   36013                 : ** an error is returned.  If they are the same, zero is returned.
   36014                 : **
   36015                 : ** If a memory allocation error occurs, return -1.
   36016                 : */
   36017               0 : SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
   36018               0 :   Bitvec *pBitvec = 0;
   36019               0 :   unsigned char *pV = 0;
   36020               0 :   int rc = -1;
   36021                 :   int i, nx, pc, op;
   36022                 :   void *pTmpSpace;
   36023                 : 
   36024                 :   /* Allocate the Bitvec to be tested and a linear array of
   36025                 :   ** bits to act as the reference */
   36026               0 :   pBitvec = sqlite3BitvecCreate( sz );
   36027               0 :   pV = sqlite3_malloc( (sz+7)/8 + 1 );
   36028               0 :   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
   36029               0 :   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
   36030               0 :   memset(pV, 0, (sz+7)/8 + 1);
   36031                 : 
   36032                 :   /* NULL pBitvec tests */
   36033               0 :   sqlite3BitvecSet(0, 1);
   36034               0 :   sqlite3BitvecClear(0, 1, pTmpSpace);
   36035                 : 
   36036                 :   /* Run the program */
   36037               0 :   pc = 0;
   36038               0 :   while( (op = aOp[pc])!=0 ){
   36039               0 :     switch( op ){
   36040                 :       case 1:
   36041                 :       case 2:
   36042                 :       case 5: {
   36043               0 :         nx = 4;
   36044               0 :         i = aOp[pc+2] - 1;
   36045               0 :         aOp[pc+2] += aOp[pc+3];
   36046               0 :         break;
   36047                 :       }
   36048                 :       case 3:
   36049                 :       case 4: 
   36050                 :       default: {
   36051               0 :         nx = 2;
   36052               0 :         sqlite3_randomness(sizeof(i), &i);
   36053               0 :         break;
   36054                 :       }
   36055                 :     }
   36056               0 :     if( (--aOp[pc+1]) > 0 ) nx = 0;
   36057               0 :     pc += nx;
   36058               0 :     i = (i & 0x7fffffff)%sz;
   36059               0 :     if( (op & 1)!=0 ){
   36060               0 :       SETBIT(pV, (i+1));
   36061               0 :       if( op!=5 ){
   36062               0 :         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
   36063                 :       }
   36064                 :     }else{
   36065               0 :       CLEARBIT(pV, (i+1));
   36066               0 :       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
   36067                 :     }
   36068                 :   }
   36069                 : 
   36070                 :   /* Test to make sure the linear array exactly matches the
   36071                 :   ** Bitvec object.  Start with the assumption that they do
   36072                 :   ** match (rc==0).  Change rc to non-zero if a discrepancy
   36073                 :   ** is found.
   36074                 :   */
   36075               0 :   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
   36076               0 :           + sqlite3BitvecTest(pBitvec, 0)
   36077               0 :           + (sqlite3BitvecSize(pBitvec) - sz);
   36078               0 :   for(i=1; i<=sz; i++){
   36079               0 :     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
   36080               0 :       rc = i;
   36081               0 :       break;
   36082                 :     }
   36083                 :   }
   36084                 : 
   36085                 :   /* Free allocated structure */
   36086                 : bitvec_end:
   36087               0 :   sqlite3_free(pTmpSpace);
   36088               0 :   sqlite3_free(pV);
   36089               0 :   sqlite3BitvecDestroy(pBitvec);
   36090               0 :   return rc;
   36091                 : }
   36092                 : #endif /* SQLITE_OMIT_BUILTIN_TEST */
   36093                 : 
   36094                 : /************** End of bitvec.c **********************************************/
   36095                 : /************** Begin file pcache.c ******************************************/
   36096                 : /*
   36097                 : ** 2008 August 05
   36098                 : **
   36099                 : ** The author disclaims copyright to this source code.  In place of
   36100                 : ** a legal notice, here is a blessing:
   36101                 : **
   36102                 : **    May you do good and not evil.
   36103                 : **    May you find forgiveness for yourself and forgive others.
   36104                 : **    May you share freely, never taking more than you give.
   36105                 : **
   36106                 : *************************************************************************
   36107                 : ** This file implements that page cache.
   36108                 : */
   36109                 : 
   36110                 : /*
   36111                 : ** A complete page cache is an instance of this structure.
   36112                 : */
   36113                 : struct PCache {
   36114                 :   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
   36115                 :   PgHdr *pSynced;                     /* Last synced page in dirty page list */
   36116                 :   int nRef;                           /* Number of referenced pages */
   36117                 :   int szCache;                        /* Configured cache size */
   36118                 :   int szPage;                         /* Size of every page in this cache */
   36119                 :   int szExtra;                        /* Size of extra space for each page */
   36120                 :   int bPurgeable;                     /* True if pages are on backing store */
   36121                 :   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
   36122                 :   void *pStress;                      /* Argument to xStress */
   36123                 :   sqlite3_pcache *pCache;             /* Pluggable cache module */
   36124                 :   PgHdr *pPage1;                      /* Reference to page 1 */
   36125                 : };
   36126                 : 
   36127                 : /*
   36128                 : ** Some of the assert() macros in this code are too expensive to run
   36129                 : ** even during normal debugging.  Use them only rarely on long-running
   36130                 : ** tests.  Enable the expensive asserts using the
   36131                 : ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
   36132                 : */
   36133                 : #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
   36134                 : # define expensive_assert(X)  assert(X)
   36135                 : #else
   36136                 : # define expensive_assert(X)
   36137                 : #endif
   36138                 : 
   36139                 : /********************************** Linked List Management ********************/
   36140                 : 
   36141                 : #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
   36142                 : /*
   36143                 : ** Check that the pCache->pSynced variable is set correctly. If it
   36144                 : ** is not, either fail an assert or return zero. Otherwise, return
   36145                 : ** non-zero. This is only used in debugging builds, as follows:
   36146                 : **
   36147                 : **   expensive_assert( pcacheCheckSynced(pCache) );
   36148                 : */
   36149                 : static int pcacheCheckSynced(PCache *pCache){
   36150                 :   PgHdr *p;
   36151                 :   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
   36152                 :     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
   36153                 :   }
   36154                 :   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
   36155                 : }
   36156                 : #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
   36157                 : 
   36158                 : /*
   36159                 : ** Remove page pPage from the list of dirty pages.
   36160                 : */
   36161          439433 : static void pcacheRemoveFromDirtyList(PgHdr *pPage){
   36162          439433 :   PCache *p = pPage->pCache;
   36163                 : 
   36164          439433 :   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
   36165          439433 :   assert( pPage->pDirtyPrev || pPage==p->pDirty );
   36166                 : 
   36167                 :   /* Update the PCache1.pSynced variable if necessary. */
   36168          439433 :   if( p->pSynced==pPage ){
   36169           97937 :     PgHdr *pSynced = pPage->pDirtyPrev;
   36170          202123 :     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
   36171            6249 :       pSynced = pSynced->pDirtyPrev;
   36172                 :     }
   36173           97937 :     p->pSynced = pSynced;
   36174                 :   }
   36175                 : 
   36176          439433 :   if( pPage->pDirtyNext ){
   36177          335709 :     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
   36178                 :   }else{
   36179          103724 :     assert( pPage==p->pDirtyTail );
   36180          103724 :     p->pDirtyTail = pPage->pDirtyPrev;
   36181                 :   }
   36182          439433 :   if( pPage->pDirtyPrev ){
   36183          181609 :     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
   36184                 :   }else{
   36185          257824 :     assert( pPage==p->pDirty );
   36186          257824 :     p->pDirty = pPage->pDirtyNext;
   36187                 :   }
   36188          439433 :   pPage->pDirtyNext = 0;
   36189          439433 :   pPage->pDirtyPrev = 0;
   36190                 : 
   36191                 :   expensive_assert( pcacheCheckSynced(p) );
   36192          439433 : }
   36193                 : 
   36194                 : /*
   36195                 : ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
   36196                 : ** pPage).
   36197                 : */
   36198          439433 : static void pcacheAddToDirtyList(PgHdr *pPage){
   36199          439433 :   PCache *p = pPage->pCache;
   36200                 : 
   36201          439433 :   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
   36202                 : 
   36203          439433 :   pPage->pDirtyNext = p->pDirty;
   36204          439433 :   if( pPage->pDirtyNext ){
   36205          382625 :     assert( pPage->pDirtyNext->pDirtyPrev==0 );
   36206          382625 :     pPage->pDirtyNext->pDirtyPrev = pPage;
   36207                 :   }
   36208          439433 :   p->pDirty = pPage;
   36209          439433 :   if( !p->pDirtyTail ){
   36210           56808 :     p->pDirtyTail = pPage;
   36211                 :   }
   36212          439433 :   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
   36213           58786 :     p->pSynced = pPage;
   36214                 :   }
   36215                 :   expensive_assert( pcacheCheckSynced(p) );
   36216          439433 : }
   36217                 : 
   36218                 : /*
   36219                 : ** Wrapper around the pluggable caches xUnpin method. If the cache is
   36220                 : ** being used for an in-memory database, this function is a no-op.
   36221                 : */
   36222          407635 : static void pcacheUnpin(PgHdr *p){
   36223          407635 :   PCache *pCache = p->pCache;
   36224          407635 :   if( pCache->bPurgeable ){
   36225          370039 :     if( p->pgno==1 ){
   36226           78362 :       pCache->pPage1 = 0;
   36227                 :     }
   36228          370039 :     sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
   36229                 :   }
   36230          407635 : }
   36231                 : 
   36232                 : /*************************************************** General Interfaces ******
   36233                 : **
   36234                 : ** Initialize and shutdown the page cache subsystem. Neither of these 
   36235                 : ** functions are threadsafe.
   36236                 : */
   36237             806 : SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
   36238             806 :   if( sqlite3GlobalConfig.pcache2.xInit==0 ){
   36239                 :     /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
   36240                 :     ** built-in default page cache is used instead of the application defined
   36241                 :     ** page cache. */
   36242             805 :     sqlite3PCacheSetDefault();
   36243                 :   }
   36244             806 :   return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
   36245                 : }
   36246             795 : SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
   36247             795 :   if( sqlite3GlobalConfig.pcache2.xShutdown ){
   36248                 :     /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
   36249             795 :     sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
   36250                 :   }
   36251             795 : }
   36252                 : 
   36253                 : /*
   36254                 : ** Return the size in bytes of a PCache object.
   36255                 : */
   36256           19521 : SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
   36257                 : 
   36258                 : /*
   36259                 : ** Create a new PCache object. Storage space to hold the object
   36260                 : ** has already been allocated and is passed in as the p pointer. 
   36261                 : ** The caller discovers how much space needs to be allocated by 
   36262                 : ** calling sqlite3PcacheSize().
   36263                 : */
   36264           19517 : SQLITE_PRIVATE void sqlite3PcacheOpen(
   36265                 :   int szPage,                  /* Size of every page */
   36266                 :   int szExtra,                 /* Extra space associated with each page */
   36267                 :   int bPurgeable,              /* True if pages are on backing store */
   36268                 :   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
   36269                 :   void *pStress,               /* Argument to xStress */
   36270                 :   PCache *p                    /* Preallocated space for the PCache */
   36271                 : ){
   36272           19517 :   memset(p, 0, sizeof(PCache));
   36273           19517 :   p->szPage = szPage;
   36274           19517 :   p->szExtra = szExtra;
   36275           19517 :   p->bPurgeable = bPurgeable;
   36276           19517 :   p->xStress = xStress;
   36277           19517 :   p->pStress = pStress;
   36278           19517 :   p->szCache = 100;
   36279           19517 : }
   36280                 : 
   36281                 : /*
   36282                 : ** Change the page size for PCache object. The caller must ensure that there
   36283                 : ** are no outstanding page references when this function is called.
   36284                 : */
   36285           19584 : SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
   36286           19584 :   assert( pCache->nRef==0 && pCache->pDirty==0 );
   36287           19584 :   if( pCache->pCache ){
   36288               7 :     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   36289               7 :     pCache->pCache = 0;
   36290               7 :     pCache->pPage1 = 0;
   36291                 :   }
   36292           19584 :   pCache->szPage = szPage;
   36293           19584 : }
   36294                 : 
   36295                 : /*
   36296                 : ** Compute the number of pages of cache requested.
   36297                 : */
   36298           26455 : static int numberOfCachePages(PCache *p){
   36299           26455 :   if( p->szCache>=0 ){
   36300           26431 :     return p->szCache;
   36301                 :   }else{
   36302              24 :     return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
   36303                 :   }
   36304                 : }
   36305                 : 
   36306                 : /*
   36307                 : ** Try to obtain a page from the cache.
   36308                 : */
   36309          638798 : SQLITE_PRIVATE int sqlite3PcacheFetch(
   36310                 :   PCache *pCache,       /* Obtain the page from this cache */
   36311                 :   Pgno pgno,            /* Page number to obtain */
   36312                 :   int createFlag,       /* If true, create page if it does not exist already */
   36313                 :   PgHdr **ppPage        /* Write the page here */
   36314                 : ){
   36315          638798 :   sqlite3_pcache_page *pPage = 0;
   36316          638798 :   PgHdr *pPgHdr = 0;
   36317                 :   int eCreate;
   36318                 : 
   36319          638798 :   assert( pCache!=0 );
   36320          638798 :   assert( createFlag==1 || createFlag==0 );
   36321          638798 :   assert( pgno>0 );
   36322                 : 
   36323                 :   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
   36324                 :   ** allocate it now.
   36325                 :   */
   36326          638798 :   if( !pCache->pCache && createFlag ){
   36327                 :     sqlite3_pcache *p;
   36328           39046 :     p = sqlite3GlobalConfig.pcache2.xCreate(
   36329           19523 :         pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
   36330                 :     );
   36331           19523 :     if( !p ){
   36332               0 :       return SQLITE_NOMEM;
   36333                 :     }
   36334           19523 :     sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
   36335           19523 :     pCache->pCache = p;
   36336                 :   }
   36337                 : 
   36338          638798 :   eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
   36339          638798 :   if( pCache->pCache ){
   36340          638798 :     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
   36341                 :   }
   36342                 : 
   36343          638798 :   if( !pPage && eCreate==1 ){
   36344                 :     PgHdr *pPg;
   36345                 : 
   36346                 :     /* Find a dirty page to write-out and recycle. First try to find a 
   36347                 :     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   36348                 :     ** cleared), but if that is not possible settle for any other 
   36349                 :     ** unreferenced dirty page.
   36350                 :     */
   36351                 :     expensive_assert( pcacheCheckSynced(pCache) );
   36352               0 :     for(pPg=pCache->pSynced; 
   36353               0 :         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
   36354               0 :         pPg=pPg->pDirtyPrev
   36355                 :     );
   36356               0 :     pCache->pSynced = pPg;
   36357               0 :     if( !pPg ){
   36358               0 :       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
   36359                 :     }
   36360               0 :     if( pPg ){
   36361                 :       int rc;
   36362                 : #ifdef SQLITE_LOG_CACHE_SPILL
   36363                 :       sqlite3_log(SQLITE_FULL, 
   36364                 :                   "spill page %d making room for %d - cache used: %d/%d",
   36365                 :                   pPg->pgno, pgno,
   36366                 :                   sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
   36367                 :                   numberOfCachePages(pCache));
   36368                 : #endif
   36369               0 :       rc = pCache->xStress(pCache->pStress, pPg);
   36370               0 :       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   36371               0 :         return rc;
   36372                 :       }
   36373                 :     }
   36374                 : 
   36375               0 :     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
   36376                 :   }
   36377                 : 
   36378          638798 :   if( pPage ){
   36379          638798 :     pPgHdr = (PgHdr *)pPage->pExtra;
   36380                 : 
   36381          638798 :     if( !pPgHdr->pPage ){
   36382           61032 :       memset(pPgHdr, 0, sizeof(PgHdr));
   36383           61032 :       pPgHdr->pPage = pPage;
   36384           61032 :       pPgHdr->pData = pPage->pBuf;
   36385           61032 :       pPgHdr->pExtra = (void *)&pPgHdr[1];
   36386           61032 :       memset(pPgHdr->pExtra, 0, pCache->szExtra);
   36387           61032 :       pPgHdr->pCache = pCache;
   36388           61032 :       pPgHdr->pgno = pgno;
   36389                 :     }
   36390          638798 :     assert( pPgHdr->pCache==pCache );
   36391          638798 :     assert( pPgHdr->pgno==pgno );
   36392          638798 :     assert( pPgHdr->pData==pPage->pBuf );
   36393          638798 :     assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
   36394                 : 
   36395          638798 :     if( 0==pPgHdr->nRef ){
   36396          548279 :       pCache->nRef++;
   36397                 :     }
   36398          638798 :     pPgHdr->nRef++;
   36399          638798 :     if( pgno==1 ){
   36400          167422 :       pCache->pPage1 = pPgHdr;
   36401                 :     }
   36402                 :   }
   36403          638798 :   *ppPage = pPgHdr;
   36404          638798 :   return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
   36405                 : }
   36406                 : 
   36407                 : /*
   36408                 : ** Decrement the reference count on a page. If the page is clean and the
   36409                 : ** reference count drops to 0, then it is made elible for recycling.
   36410                 : */
   36411          638789 : SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
   36412          638789 :   assert( p->nRef>0 );
   36413          638789 :   p->nRef--;
   36414          638789 :   if( p->nRef==0 ){
   36415          548177 :     PCache *pCache = p->pCache;
   36416          548177 :     pCache->nRef--;
   36417          548177 :     if( (p->flags&PGHDR_DIRTY)==0 ){
   36418          271134 :       pcacheUnpin(p);
   36419                 :     }else{
   36420                 :       /* Move the page to the head of the dirty list. */
   36421          277043 :       pcacheRemoveFromDirtyList(p);
   36422          277043 :       pcacheAddToDirtyList(p);
   36423                 :     }
   36424                 :   }
   36425          638789 : }
   36426                 : 
   36427                 : /*
   36428                 : ** Increase the reference count of a supplied page by 1.
   36429                 : */
   36430              93 : SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
   36431              93 :   assert(p->nRef>0);
   36432              93 :   p->nRef++;
   36433              93 : }
   36434                 : 
   36435                 : /*
   36436                 : ** Drop a page from the cache. There must be exactly one reference to the
   36437                 : ** page. This function deletes that reference, so after it returns the
   36438                 : ** page pointed to by p is invalid.
   36439                 : */
   36440              11 : SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
   36441                 :   PCache *pCache;
   36442              11 :   assert( p->nRef==1 );
   36443              11 :   if( p->flags&PGHDR_DIRTY ){
   36444              11 :     pcacheRemoveFromDirtyList(p);
   36445                 :   }
   36446              11 :   pCache = p->pCache;
   36447              11 :   pCache->nRef--;
   36448              11 :   if( p->pgno==1 ){
   36449               0 :     pCache->pPage1 = 0;
   36450                 :   }
   36451              11 :   sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
   36452              11 : }
   36453                 : 
   36454                 : /*
   36455                 : ** Make sure the page is marked as dirty. If it isn't dirty already,
   36456                 : ** make it so.
   36457                 : */
   36458          432942 : SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
   36459          432942 :   p->flags &= ~PGHDR_DONT_WRITE;
   36460          432942 :   assert( p->nRef>0 );
   36461          432942 :   if( 0==(p->flags & PGHDR_DIRTY) ){
   36462          162390 :     p->flags |= PGHDR_DIRTY;
   36463          162390 :     pcacheAddToDirtyList( p);
   36464                 :   }
   36465          432941 : }
   36466                 : 
   36467                 : /*
   36468                 : ** Make sure the page is marked as clean. If it isn't clean already,
   36469                 : ** make it so.
   36470                 : */
   36471          162379 : SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
   36472          162379 :   if( (p->flags & PGHDR_DIRTY) ){
   36473          162379 :     pcacheRemoveFromDirtyList(p);
   36474          162379 :     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
   36475          162379 :     if( p->nRef==0 ){
   36476          136501 :       pcacheUnpin(p);
   36477                 :     }
   36478                 :   }
   36479          162379 : }
   36480                 : 
   36481                 : /*
   36482                 : ** Make every page in the cache clean.
   36483                 : */
   36484           91457 : SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
   36485                 :   PgHdr *p;
   36486          345276 :   while( (p = pCache->pDirty)!=0 ){
   36487          162362 :     sqlite3PcacheMakeClean(p);
   36488                 :   }
   36489           91457 : }
   36490                 : 
   36491                 : /*
   36492                 : ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
   36493                 : */
   36494            8675 : SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
   36495                 :   PgHdr *p;
   36496           35545 :   for(p=pCache->pDirty; p; p=p->pDirtyNext){
   36497           26870 :     p->flags &= ~PGHDR_NEED_SYNC;
   36498                 :   }
   36499            8675 :   pCache->pSynced = pCache->pDirtyTail;
   36500            8675 : }
   36501                 : 
   36502                 : /*
   36503                 : ** Change the page number of page p to newPgno. 
   36504                 : */
   36505               0 : SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
   36506               0 :   PCache *pCache = p->pCache;
   36507               0 :   assert( p->nRef>0 );
   36508               0 :   assert( newPgno>0 );
   36509               0 :   sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
   36510               0 :   p->pgno = newPgno;
   36511               0 :   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
   36512               0 :     pcacheRemoveFromDirtyList(p);
   36513               0 :     pcacheAddToDirtyList(p);
   36514                 :   }
   36515               0 : }
   36516                 : 
   36517                 : /*
   36518                 : ** Drop every cache entry whose page number is greater than "pgno". The
   36519                 : ** caller must ensure that there are no outstanding references to any pages
   36520                 : ** other than page 1 with a page number greater than pgno.
   36521                 : **
   36522                 : ** If there is a reference to page 1 and the pgno parameter passed to this
   36523                 : ** function is 0, then the data area associated with page 1 is zeroed, but
   36524                 : ** the page object is not dropped.
   36525                 : */
   36526           98371 : SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
   36527           98371 :   if( pCache->pCache ){
   36528                 :     PgHdr *p;
   36529                 :     PgHdr *pNext;
   36530           78760 :     for(p=pCache->pDirty; p; p=pNext){
   36531               0 :       pNext = p->pDirtyNext;
   36532                 :       /* This routine never gets call with a positive pgno except right
   36533                 :       ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
   36534                 :       ** it must be that pgno==0.
   36535                 :       */
   36536               0 :       assert( p->pgno>0 );
   36537               0 :       if( ALWAYS(p->pgno>pgno) ){
   36538               0 :         assert( p->flags&PGHDR_DIRTY );
   36539               0 :         sqlite3PcacheMakeClean(p);
   36540                 :       }
   36541                 :     }
   36542           78760 :     if( pgno==0 && pCache->pPage1 ){
   36543           15583 :       memset(pCache->pPage1->pData, 0, pCache->szPage);
   36544           15583 :       pgno = 1;
   36545                 :     }
   36546           78760 :     sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
   36547                 :   }
   36548           98371 : }
   36549                 : 
   36550                 : /*
   36551                 : ** Close a cache.
   36552                 : */
   36553           19517 : SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
   36554           19517 :   if( pCache->pCache ){
   36555           19516 :     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   36556                 :   }
   36557           19517 : }
   36558                 : 
   36559                 : /* 
   36560                 : ** Discard the contents of the cache.
   36561                 : */
   36562           40629 : SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
   36563           40629 :   sqlite3PcacheTruncate(pCache, 0);
   36564           40629 : }
   36565                 : 
   36566                 : /*
   36567                 : ** Merge two lists of pages connected by pDirty and in pgno order.
   36568                 : ** Do not both fixing the pDirtyPrev pointers.
   36569                 : */
   36570         1123108 : static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
   36571                 :   PgHdr result, *pTail;
   36572         1123108 :   pTail = &result;
   36573         2431456 :   while( pA && pB ){
   36574          185240 :     if( pA->pgno<pB->pgno ){
   36575           81720 :       pTail->pDirty = pA;
   36576           81720 :       pTail = pA;
   36577           81720 :       pA = pA->pDirty;
   36578                 :     }else{
   36579          103520 :       pTail->pDirty = pB;
   36580          103520 :       pTail = pB;
   36581          103520 :       pB = pB->pDirty;
   36582                 :     }
   36583                 :   }
   36584         1123108 :   if( pA ){
   36585         1050721 :     pTail->pDirty = pA;
   36586           72387 :   }else if( pB ){
   36587           60657 :     pTail->pDirty = pB;
   36588                 :   }else{
   36589           11730 :     pTail->pDirty = 0;
   36590                 :   }
   36591         1123108 :   return result.pDirty;
   36592                 : }
   36593                 : 
   36594                 : /*
   36595                 : ** Sort the list of pages in accending order by pgno.  Pages are
   36596                 : ** connected by pDirty pointers.  The pDirtyPrev pointers are
   36597                 : ** corrupted by this sort.
   36598                 : **
   36599                 : ** Since there cannot be more than 2^31 distinct pages in a database,
   36600                 : ** there cannot be more than 31 buckets required by the merge sorter.
   36601                 : ** One extra bucket is added to catch overflow in case something
   36602                 : ** ever changes to make the previous sentence incorrect.
   36603                 : */
   36604                 : #define N_SORT_BUCKET  32
   36605           34019 : static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
   36606                 :   PgHdr *a[N_SORT_BUCKET], *p;
   36607                 :   int i;
   36608           34019 :   memset(a, 0, sizeof(a));
   36609          196608 :   while( pIn ){
   36610          128570 :     p = pIn;
   36611          128570 :     pIn = p->pDirty;
   36612          128570 :     p->pDirty = 0;
   36613          197089 :     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
   36614          197089 :       if( a[i]==0 ){
   36615          128570 :         a[i] = p;
   36616          128570 :         break;
   36617                 :       }else{
   36618           68519 :         p = pcacheMergeDirtyList(a[i], p);
   36619           68519 :         a[i] = 0;
   36620                 :       }
   36621                 :     }
   36622          128570 :     if( NEVER(i==N_SORT_BUCKET-1) ){
   36623                 :       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
   36624                 :       ** the input list.  But that is impossible.
   36625                 :       */
   36626               0 :       a[i] = pcacheMergeDirtyList(a[i], p);
   36627                 :     }
   36628                 :   }
   36629           34019 :   p = a[0];
   36630         1088608 :   for(i=1; i<N_SORT_BUCKET; i++){
   36631         1054589 :     p = pcacheMergeDirtyList(p, a[i]);
   36632                 :   }
   36633           34019 :   return p;
   36634                 : }
   36635                 : 
   36636                 : /*
   36637                 : ** Return a list of all dirty pages in the cache, sorted by page number.
   36638                 : */
   36639           34019 : SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
   36640                 :   PgHdr *p;
   36641          162589 :   for(p=pCache->pDirty; p; p=p->pDirtyNext){
   36642          128570 :     p->pDirty = p->pDirtyNext;
   36643                 :   }
   36644           34019 :   return pcacheSortDirtyList(pCache->pDirty);
   36645                 : }
   36646                 : 
   36647                 : /* 
   36648                 : ** Return the total number of referenced pages held by the cache.
   36649                 : */
   36650          941417 : SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
   36651          941417 :   return pCache->nRef;
   36652                 : }
   36653                 : 
   36654                 : /*
   36655                 : ** Return the number of references to the page supplied as an argument.
   36656                 : */
   36657           30765 : SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
   36658           30765 :   return p->nRef;
   36659                 : }
   36660                 : 
   36661                 : /* 
   36662                 : ** Return the total number of pages in the cache.
   36663                 : */
   36664           16160 : SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
   36665           16160 :   int nPage = 0;
   36666           16160 :   if( pCache->pCache ){
   36667           13103 :     nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
   36668                 :   }
   36669           16160 :   return nPage;
   36670                 : }
   36671                 : 
   36672                 : #ifdef SQLITE_TEST
   36673                 : /*
   36674                 : ** Get the suggested cache-size value.
   36675                 : */
   36676                 : SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
   36677                 :   return numberOfCachePages(pCache);
   36678                 : }
   36679                 : #endif
   36680                 : 
   36681                 : /*
   36682                 : ** Set the suggested cache-size value.
   36683                 : */
   36684           26450 : SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
   36685           26450 :   pCache->szCache = mxPage;
   36686           26450 :   if( pCache->pCache ){
   36687            6932 :     sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
   36688                 :                                            numberOfCachePages(pCache));
   36689                 :   }
   36690           26450 : }
   36691                 : 
   36692                 : /*
   36693                 : ** Free up as much memory as possible from the page cache.
   36694                 : */
   36695               0 : SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
   36696               0 :   if( pCache->pCache ){
   36697               0 :     sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
   36698                 :   }
   36699               0 : }
   36700                 : 
   36701                 : #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
   36702                 : /*
   36703                 : ** For all dirty pages currently in the cache, invoke the specified
   36704                 : ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
   36705                 : ** defined.
   36706                 : */
   36707           22640 : SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
   36708                 :   PgHdr *pDirty;
   36709          205285 :   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
   36710          182645 :     xIter(pDirty);
   36711                 :   }
   36712           22640 : }
   36713                 : #endif
   36714                 : 
   36715                 : /************** End of pcache.c **********************************************/
   36716                 : /************** Begin file pcache1.c *****************************************/
   36717                 : /*
   36718                 : ** 2008 November 05
   36719                 : **
   36720                 : ** The author disclaims copyright to this source code.  In place of
   36721                 : ** a legal notice, here is a blessing:
   36722                 : **
   36723                 : **    May you do good and not evil.
   36724                 : **    May you find forgiveness for yourself and forgive others.
   36725                 : **    May you share freely, never taking more than you give.
   36726                 : **
   36727                 : *************************************************************************
   36728                 : **
   36729                 : ** This file implements the default page cache implementation (the
   36730                 : ** sqlite3_pcache interface). It also contains part of the implementation
   36731                 : ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
   36732                 : ** If the default page cache implementation is overriden, then neither of
   36733                 : ** these two features are available.
   36734                 : */
   36735                 : 
   36736                 : 
   36737                 : typedef struct PCache1 PCache1;
   36738                 : typedef struct PgHdr1 PgHdr1;
   36739                 : typedef struct PgFreeslot PgFreeslot;
   36740                 : typedef struct PGroup PGroup;
   36741                 : 
   36742                 : /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
   36743                 : ** of one or more PCaches that are able to recycle each others unpinned
   36744                 : ** pages when they are under memory pressure.  A PGroup is an instance of
   36745                 : ** the following object.
   36746                 : **
   36747                 : ** This page cache implementation works in one of two modes:
   36748                 : **
   36749                 : **   (1)  Every PCache is the sole member of its own PGroup.  There is
   36750                 : **        one PGroup per PCache.
   36751                 : **
   36752                 : **   (2)  There is a single global PGroup that all PCaches are a member
   36753                 : **        of.
   36754                 : **
   36755                 : ** Mode 1 uses more memory (since PCache instances are not able to rob
   36756                 : ** unused pages from other PCaches) but it also operates without a mutex,
   36757                 : ** and is therefore often faster.  Mode 2 requires a mutex in order to be
   36758                 : ** threadsafe, but recycles pages more efficiently.
   36759                 : **
   36760                 : ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
   36761                 : ** PGroup which is the pcache1.grp global variable and its mutex is
   36762                 : ** SQLITE_MUTEX_STATIC_LRU.
   36763                 : */
   36764                 : struct PGroup {
   36765                 :   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
   36766                 :   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
   36767                 :   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
   36768                 :   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
   36769                 :   unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
   36770                 :   PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
   36771                 : };
   36772                 : 
   36773                 : /* Each page cache is an instance of the following object.  Every
   36774                 : ** open database file (including each in-memory database and each
   36775                 : ** temporary or transient database) has a single page cache which
   36776                 : ** is an instance of this object.
   36777                 : **
   36778                 : ** Pointers to structures of this type are cast and returned as 
   36779                 : ** opaque sqlite3_pcache* handles.
   36780                 : */
   36781                 : struct PCache1 {
   36782                 :   /* Cache configuration parameters. Page size (szPage) and the purgeable
   36783                 :   ** flag (bPurgeable) are set when the cache is created. nMax may be 
   36784                 :   ** modified at any time by a call to the pcache1Cachesize() method.
   36785                 :   ** The PGroup mutex must be held when accessing nMax.
   36786                 :   */
   36787                 :   PGroup *pGroup;                     /* PGroup this cache belongs to */
   36788                 :   int szPage;                         /* Size of allocated pages in bytes */
   36789                 :   int szExtra;                        /* Size of extra space in bytes */
   36790                 :   int bPurgeable;                     /* True if cache is purgeable */
   36791                 :   unsigned int nMin;                  /* Minimum number of pages reserved */
   36792                 :   unsigned int nMax;                  /* Configured "cache_size" value */
   36793                 :   unsigned int n90pct;                /* nMax*9/10 */
   36794                 : 
   36795                 :   /* Hash table of all pages. The following variables may only be accessed
   36796                 :   ** when the accessor is holding the PGroup mutex.
   36797                 :   */
   36798                 :   unsigned int nRecyclable;           /* Number of pages in the LRU list */
   36799                 :   unsigned int nPage;                 /* Total number of pages in apHash */
   36800                 :   unsigned int nHash;                 /* Number of slots in apHash[] */
   36801                 :   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
   36802                 : 
   36803                 :   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
   36804                 : };
   36805                 : 
   36806                 : /*
   36807                 : ** Each cache entry is represented by an instance of the following 
   36808                 : ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
   36809                 : ** PgHdr1.pCache->szPage bytes is allocated directly before this structure 
   36810                 : ** in memory.
   36811                 : */
   36812                 : struct PgHdr1 {
   36813                 :   sqlite3_pcache_page page;
   36814                 :   unsigned int iKey;             /* Key value (page number) */
   36815                 :   PgHdr1 *pNext;                 /* Next in hash table chain */
   36816                 :   PCache1 *pCache;               /* Cache that currently owns this page */
   36817                 :   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
   36818                 :   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
   36819                 : };
   36820                 : 
   36821                 : /*
   36822                 : ** Free slots in the allocator used to divide up the buffer provided using
   36823                 : ** the SQLITE_CONFIG_PAGECACHE mechanism.
   36824                 : */
   36825                 : struct PgFreeslot {
   36826                 :   PgFreeslot *pNext;  /* Next free slot */
   36827                 : };
   36828                 : 
   36829                 : /*
   36830                 : ** Global data used by this cache.
   36831                 : */
   36832                 : static SQLITE_WSD struct PCacheGlobal {
   36833                 :   PGroup grp;                    /* The global PGroup for mode (2) */
   36834                 : 
   36835                 :   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
   36836                 :   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
   36837                 :   ** fixed at sqlite3_initialize() time and do not require mutex protection.
   36838                 :   ** The nFreeSlot and pFree values do require mutex protection.
   36839                 :   */
   36840                 :   int isInit;                    /* True if initialized */
   36841                 :   int szSlot;                    /* Size of each free slot */
   36842                 :   int nSlot;                     /* The number of pcache slots */
   36843                 :   int nReserve;                  /* Try to keep nFreeSlot above this */
   36844                 :   void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
   36845                 :   /* Above requires no mutex.  Use mutex below for variable that follow. */
   36846                 :   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
   36847                 :   int nFreeSlot;                 /* Number of unused pcache slots */
   36848                 :   PgFreeslot *pFree;             /* Free page blocks */
   36849                 :   /* The following value requires a mutex to change.  We skip the mutex on
   36850                 :   ** reading because (1) most platforms read a 32-bit integer atomically and
   36851                 :   ** (2) even if an incorrect value is read, no great harm is done since this
   36852                 :   ** is really just an optimization. */
   36853                 :   int bUnderPressure;            /* True if low on PAGECACHE memory */
   36854                 : } pcache1_g;
   36855                 : 
   36856                 : /*
   36857                 : ** All code in this file should access the global structure above via the
   36858                 : ** alias "pcache1". This ensures that the WSD emulation is used when
   36859                 : ** compiling for systems that do not support real WSD.
   36860                 : */
   36861                 : #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
   36862                 : 
   36863                 : /*
   36864                 : ** Macros to enter and leave the PCache LRU mutex.
   36865                 : */
   36866                 : #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
   36867                 : #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
   36868                 : 
   36869                 : /******************************************************************************/
   36870                 : /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
   36871                 : 
   36872                 : /*
   36873                 : ** This function is called during initialization if a static buffer is 
   36874                 : ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
   36875                 : ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
   36876                 : ** enough to contain 'n' buffers of 'sz' bytes each.
   36877                 : **
   36878                 : ** This routine is called from sqlite3_initialize() and so it is guaranteed
   36879                 : ** to be serialized already.  There is no need for further mutexing.
   36880                 : */
   36881             806 : SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
   36882             806 :   if( pcache1.isInit ){
   36883                 :     PgFreeslot *p;
   36884             806 :     sz = ROUNDDOWN8(sz);
   36885             806 :     pcache1.szSlot = sz;
   36886             806 :     pcache1.nSlot = pcache1.nFreeSlot = n;
   36887             806 :     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
   36888             806 :     pcache1.pStart = pBuf;
   36889             806 :     pcache1.pFree = 0;
   36890             806 :     pcache1.bUnderPressure = 0;
   36891            1612 :     while( n-- ){
   36892               0 :       p = (PgFreeslot*)pBuf;
   36893               0 :       p->pNext = pcache1.pFree;
   36894               0 :       pcache1.pFree = p;
   36895               0 :       pBuf = (void*)&((char*)pBuf)[sz];
   36896                 :     }
   36897             806 :     pcache1.pEnd = pBuf;
   36898                 :   }
   36899             806 : }
   36900                 : 
   36901                 : /*
   36902                 : ** Malloc function used within this file to allocate space from the buffer
   36903                 : ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no 
   36904                 : ** such buffer exists or there is no space left in it, this function falls 
   36905                 : ** back to sqlite3Malloc().
   36906                 : **
   36907                 : ** Multiple threads can run this routine at the same time.  Global variables
   36908                 : ** in pcache1 need to be protected via mutex.
   36909                 : */
   36910           95912 : static void *pcache1Alloc(int nByte){
   36911           95912 :   void *p = 0;
   36912           95912 :   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
   36913           95912 :   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
   36914           95912 :   if( nByte<=pcache1.szSlot ){
   36915               0 :     sqlite3_mutex_enter(pcache1.mutex);
   36916               0 :     p = (PgHdr1 *)pcache1.pFree;
   36917               0 :     if( p ){
   36918               0 :       pcache1.pFree = pcache1.pFree->pNext;
   36919               0 :       pcache1.nFreeSlot--;
   36920               0 :       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
   36921               0 :       assert( pcache1.nFreeSlot>=0 );
   36922               0 :       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
   36923                 :     }
   36924               0 :     sqlite3_mutex_leave(pcache1.mutex);
   36925                 :   }
   36926           95912 :   if( p==0 ){
   36927                 :     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
   36928                 :     ** it from sqlite3Malloc instead.
   36929                 :     */
   36930           95912 :     p = sqlite3Malloc(nByte);
   36931           95912 :     if( p ){
   36932           95912 :       int sz = sqlite3MallocSize(p);
   36933           95912 :       sqlite3_mutex_enter(pcache1.mutex);
   36934           95912 :       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
   36935           95912 :       sqlite3_mutex_leave(pcache1.mutex);
   36936                 :     }
   36937                 :     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
   36938                 :   }
   36939           95912 :   return p;
   36940                 : }
   36941                 : 
   36942                 : /*
   36943                 : ** Free an allocated buffer obtained from pcache1Alloc().
   36944                 : */
   36945          122709 : static int pcache1Free(void *p){
   36946          122709 :   int nFreed = 0;
   36947          122709 :   if( p==0 ) return 0;
   36948           95912 :   if( p>=pcache1.pStart && p<pcache1.pEnd ){
   36949                 :     PgFreeslot *pSlot;
   36950               0 :     sqlite3_mutex_enter(pcache1.mutex);
   36951               0 :     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
   36952               0 :     pSlot = (PgFreeslot*)p;
   36953               0 :     pSlot->pNext = pcache1.pFree;
   36954               0 :     pcache1.pFree = pSlot;
   36955               0 :     pcache1.nFreeSlot++;
   36956               0 :     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
   36957               0 :     assert( pcache1.nFreeSlot<=pcache1.nSlot );
   36958               0 :     sqlite3_mutex_leave(pcache1.mutex);
   36959                 :   }else{
   36960                 :     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
   36961                 :     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   36962           95912 :     nFreed = sqlite3MallocSize(p);
   36963           95912 :     sqlite3_mutex_enter(pcache1.mutex);
   36964           95912 :     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
   36965           95912 :     sqlite3_mutex_leave(pcache1.mutex);
   36966           95912 :     sqlite3_free(p);
   36967                 :   }
   36968           95912 :   return nFreed;
   36969                 : }
   36970                 : 
   36971                 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   36972                 : /*
   36973                 : ** Return the size of a pcache allocation
   36974                 : */
   36975                 : static int pcache1MemSize(void *p){
   36976                 :   if( p>=pcache1.pStart && p<pcache1.pEnd ){
   36977                 :     return pcache1.szSlot;
   36978                 :   }else{
   36979                 :     int iSize;
   36980                 :     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
   36981                 :     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   36982                 :     iSize = sqlite3MallocSize(p);
   36983                 :     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
   36984                 :     return iSize;
   36985                 :   }
   36986                 : }
   36987                 : #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
   36988                 : 
   36989                 : /*
   36990                 : ** Allocate a new page object initially associated with cache pCache.
   36991                 : */
   36992           61032 : static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
   36993           61032 :   PgHdr1 *p = 0;
   36994                 :   void *pPg;
   36995                 : 
   36996                 :   /* The group mutex must be released before pcache1Alloc() is called. This
   36997                 :   ** is because it may call sqlite3_release_memory(), which assumes that 
   36998                 :   ** this mutex is not held. */
   36999           61032 :   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   37000           61032 :   pcache1LeaveMutex(pCache->pGroup);
   37001                 : #ifdef SQLITE_PCACHE_SEPARATE_HEADER
   37002                 :   pPg = pcache1Alloc(pCache->szPage);
   37003                 :   p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
   37004                 :   if( !pPg || !p ){
   37005                 :     pcache1Free(pPg);
   37006                 :     sqlite3_free(p);
   37007                 :     pPg = 0;
   37008                 :   }
   37009                 : #else
   37010           61032 :   pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
   37011           61032 :   p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
   37012                 : #endif
   37013           61032 :   pcache1EnterMutex(pCache->pGroup);
   37014                 : 
   37015           61032 :   if( pPg ){
   37016           61032 :     p->page.pBuf = pPg;
   37017           61032 :     p->page.pExtra = &p[1];
   37018           61032 :     if( pCache->bPurgeable ){
   37019           31128 :       pCache->pGroup->nCurrentPage++;
   37020                 :     }
   37021           61032 :     return p;
   37022                 :   }
   37023               0 :   return 0;
   37024                 : }
   37025                 : 
   37026                 : /*
   37027                 : ** Free a page object allocated by pcache1AllocPage().
   37028                 : **
   37029                 : ** The pointer is allowed to be NULL, which is prudent.  But it turns out
   37030                 : ** that the current implementation happens to never call this routine
   37031                 : ** with a NULL pointer, so we mark the NULL test with ALWAYS().
   37032                 : */
   37033           61032 : static void pcache1FreePage(PgHdr1 *p){
   37034           61032 :   if( ALWAYS(p) ){
   37035           61032 :     PCache1 *pCache = p->pCache;
   37036           61032 :     assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
   37037           61032 :     pcache1Free(p->page.pBuf);
   37038                 : #ifdef SQLITE_PCACHE_SEPARATE_HEADER
   37039                 :     sqlite3_free(p);
   37040                 : #endif
   37041           61032 :     if( pCache->bPurgeable ){
   37042           31128 :       pCache->pGroup->nCurrentPage--;
   37043                 :     }
   37044                 :   }
   37045           61032 : }
   37046                 : 
   37047                 : /*
   37048                 : ** Malloc function used by SQLite to obtain space from the buffer configured
   37049                 : ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
   37050                 : ** exists, this function falls back to sqlite3Malloc().
   37051                 : */
   37052           34880 : SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
   37053           34880 :   return pcache1Alloc(sz);
   37054                 : }
   37055                 : 
   37056                 : /*
   37057                 : ** Free an allocated buffer obtained from sqlite3PageMalloc().
   37058                 : */
   37059           61677 : SQLITE_PRIVATE void sqlite3PageFree(void *p){
   37060           61677 :   pcache1Free(p);
   37061           61677 : }
   37062                 : 
   37063                 : 
   37064                 : /*
   37065                 : ** Return true if it desirable to avoid allocating a new page cache
   37066                 : ** entry.
   37067                 : **
   37068                 : ** If memory was allocated specifically to the page cache using
   37069                 : ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
   37070                 : ** it is desirable to avoid allocating a new page cache entry because
   37071                 : ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
   37072                 : ** for all page cache needs and we should not need to spill the
   37073                 : ** allocation onto the heap.
   37074                 : **
   37075                 : ** Or, the heap is used for all page cache memory but the heap is
   37076                 : ** under memory pressure, then again it is desirable to avoid
   37077                 : ** allocating a new page cache entry in order to avoid stressing
   37078                 : ** the heap even further.
   37079                 : */
   37080           27552 : static int pcache1UnderMemoryPressure(PCache1 *pCache){
   37081           27552 :   if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
   37082               0 :     return pcache1.bUnderPressure;
   37083                 :   }else{
   37084           27552 :     return sqlite3HeapNearlyFull();
   37085                 :   }
   37086                 : }
   37087                 : 
   37088                 : /******************************************************************************/
   37089                 : /******** General Implementation Functions ************************************/
   37090                 : 
   37091                 : /*
   37092                 : ** This function is used to resize the hash table used by the cache passed
   37093                 : ** as the first argument.
   37094                 : **
   37095                 : ** The PCache mutex must be held when this function is called.
   37096                 : */
   37097           19523 : static int pcache1ResizeHash(PCache1 *p){
   37098                 :   PgHdr1 **apNew;
   37099                 :   unsigned int nNew;
   37100                 :   unsigned int i;
   37101                 : 
   37102           19523 :   assert( sqlite3_mutex_held(p->pGroup->mutex) );
   37103                 : 
   37104           19523 :   nNew = p->nHash*2;
   37105           19523 :   if( nNew<256 ){
   37106           19523 :     nNew = 256;
   37107                 :   }
   37108                 : 
   37109           19523 :   pcache1LeaveMutex(p->pGroup);
   37110           19523 :   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
   37111           19523 :   apNew = (PgHdr1 **)sqlite3_malloc(sizeof(PgHdr1 *)*nNew);
   37112           19523 :   if( p->nHash ){ sqlite3EndBenignMalloc(); }
   37113           19523 :   pcache1EnterMutex(p->pGroup);
   37114           19523 :   if( apNew ){
   37115           19523 :     memset(apNew, 0, sizeof(PgHdr1 *)*nNew);
   37116           19523 :     for(i=0; i<p->nHash; i++){
   37117                 :       PgHdr1 *pPage;
   37118               0 :       PgHdr1 *pNext = p->apHash[i];
   37119               0 :       while( (pPage = pNext)!=0 ){
   37120               0 :         unsigned int h = pPage->iKey % nNew;
   37121               0 :         pNext = pPage->pNext;
   37122               0 :         pPage->pNext = apNew[h];
   37123               0 :         apNew[h] = pPage;
   37124                 :       }
   37125                 :     }
   37126           19523 :     sqlite3_free(p->apHash);
   37127           19523 :     p->apHash = apNew;
   37128           19523 :     p->nHash = nNew;
   37129                 :   }
   37130                 : 
   37131           19523 :   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
   37132                 : }
   37133                 : 
   37134                 : /*
   37135                 : ** This function is used internally to remove the page pPage from the 
   37136                 : ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
   37137                 : ** LRU list, then this function is a no-op.
   37138                 : **
   37139                 : ** The PGroup mutex must be held when this function is called.
   37140                 : **
   37141                 : ** If pPage is NULL then this routine is a no-op.
   37142                 : */
   37143          638787 : static void pcache1PinPage(PgHdr1 *pPage){
   37144                 :   PCache1 *pCache;
   37145                 :   PGroup *pGroup;
   37146                 : 
   37147          638787 :   if( pPage==0 ) return;
   37148          638787 :   pCache = pPage->pCache;
   37149          638787 :   pGroup = pCache->pGroup;
   37150          638787 :   assert( sqlite3_mutex_held(pGroup->mutex) );
   37151          638787 :   if( pPage->pLruNext || pPage==pGroup->pLruTail ){
   37152          370039 :     if( pPage->pLruPrev ){
   37153          190362 :       pPage->pLruPrev->pLruNext = pPage->pLruNext;
   37154                 :     }
   37155          370039 :     if( pPage->pLruNext ){
   37156          327411 :       pPage->pLruNext->pLruPrev = pPage->pLruPrev;
   37157                 :     }
   37158          370039 :     if( pGroup->pLruHead==pPage ){
   37159          179677 :       pGroup->pLruHead = pPage->pLruNext;
   37160                 :     }
   37161          370039 :     if( pGroup->pLruTail==pPage ){
   37162           42628 :       pGroup->pLruTail = pPage->pLruPrev;
   37163                 :     }
   37164          370039 :     pPage->pLruNext = 0;
   37165          370039 :     pPage->pLruPrev = 0;
   37166          370039 :     pPage->pCache->nRecyclable--;
   37167                 :   }
   37168                 : }
   37169                 : 
   37170                 : 
   37171                 : /*
   37172                 : ** Remove the page supplied as an argument from the hash table 
   37173                 : ** (PCache1.apHash structure) that it is currently stored in.
   37174                 : **
   37175                 : ** The PGroup mutex must be held when this function is called.
   37176                 : */
   37177              11 : static void pcache1RemoveFromHash(PgHdr1 *pPage){
   37178                 :   unsigned int h;
   37179              11 :   PCache1 *pCache = pPage->pCache;
   37180                 :   PgHdr1 **pp;
   37181                 : 
   37182              11 :   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   37183              11 :   h = pPage->iKey % pCache->nHash;
   37184              11 :   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
   37185              11 :   *pp = (*pp)->pNext;
   37186                 : 
   37187              11 :   pCache->nPage--;
   37188              11 : }
   37189                 : 
   37190                 : /*
   37191                 : ** If there are currently more than nMaxPage pages allocated, try
   37192                 : ** to recycle pages to reduce the number allocated to nMaxPage.
   37193                 : */
   37194           30156 : static void pcache1EnforceMaxPage(PGroup *pGroup){
   37195           30156 :   assert( sqlite3_mutex_held(pGroup->mutex) );
   37196           60312 :   while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
   37197               0 :     PgHdr1 *p = pGroup->pLruTail;
   37198               0 :     assert( p->pCache->pGroup==pGroup );
   37199               0 :     pcache1PinPage(p);
   37200               0 :     pcache1RemoveFromHash(p);
   37201               0 :     pcache1FreePage(p);
   37202                 :   }
   37203           30156 : }
   37204                 : 
   37205                 : /*
   37206                 : ** Discard all pages from cache pCache with a page number (key value) 
   37207                 : ** greater than or equal to iLimit. Any pinned pages that meet this 
   37208                 : ** criteria are unpinned before they are discarded.
   37209                 : **
   37210                 : ** The PCache mutex must be held when this function is called.
   37211                 : */
   37212           38402 : static void pcache1TruncateUnsafe(
   37213                 :   PCache1 *pCache,             /* The cache to truncate */
   37214                 :   unsigned int iLimit          /* Drop pages with this pgno or larger */
   37215                 : ){
   37216           38402 :   TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
   37217                 :   unsigned int h;
   37218           38402 :   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   37219         9869314 :   for(h=0; h<pCache->nHash; h++){
   37220         9830912 :     PgHdr1 **pp = &pCache->apHash[h]; 
   37221                 :     PgHdr1 *pPage;
   37222        19737217 :     while( (pPage = *pp)!=0 ){
   37223           75393 :       if( pPage->iKey>=iLimit ){
   37224           61021 :         pCache->nPage--;
   37225           61021 :         *pp = pPage->pNext;
   37226           61021 :         pcache1PinPage(pPage);
   37227           61021 :         pcache1FreePage(pPage);
   37228                 :       }else{
   37229           14372 :         pp = &pPage->pNext;
   37230           14372 :         TESTONLY( nPage++; )
   37231                 :       }
   37232                 :     }
   37233                 :   }
   37234           38402 :   assert( pCache->nPage==nPage );
   37235           38402 : }
   37236                 : 
   37237                 : /******************************************************************************/
   37238                 : /******** sqlite3_pcache Methods **********************************************/
   37239                 : 
   37240                 : /*
   37241                 : ** Implementation of the sqlite3_pcache.xInit method.
   37242                 : */
   37243             806 : static int pcache1Init(void *NotUsed){
   37244                 :   UNUSED_PARAMETER(NotUsed);
   37245             806 :   assert( pcache1.isInit==0 );
   37246             806 :   memset(&pcache1, 0, sizeof(pcache1));
   37247             806 :   if( sqlite3GlobalConfig.bCoreMutex ){
   37248             806 :     pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
   37249             806 :     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
   37250                 :   }
   37251             806 :   pcache1.grp.mxPinned = 10;
   37252             806 :   pcache1.isInit = 1;
   37253             806 :   return SQLITE_OK;
   37254                 : }
   37255                 : 
   37256                 : /*
   37257                 : ** Implementation of the sqlite3_pcache.xShutdown method.
   37258                 : ** Note that the static mutex allocated in xInit does 
   37259                 : ** not need to be freed.
   37260                 : */
   37261             795 : static void pcache1Shutdown(void *NotUsed){
   37262                 :   UNUSED_PARAMETER(NotUsed);
   37263             795 :   assert( pcache1.isInit!=0 );
   37264             795 :   memset(&pcache1, 0, sizeof(pcache1));
   37265             795 : }
   37266                 : 
   37267                 : /*
   37268                 : ** Implementation of the sqlite3_pcache.xCreate method.
   37269                 : **
   37270                 : ** Allocate a new cache.
   37271                 : */
   37272           19523 : static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
   37273                 :   PCache1 *pCache;      /* The newly created page cache */
   37274                 :   PGroup *pGroup;       /* The group the new page cache will belong to */
   37275                 :   int sz;               /* Bytes of memory required to allocate the new cache */
   37276                 : 
   37277                 :   /*
   37278                 :   ** The seperateCache variable is true if each PCache has its own private
   37279                 :   ** PGroup.  In other words, separateCache is true for mode (1) where no
   37280                 :   ** mutexing is required.
   37281                 :   **
   37282                 :   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
   37283                 :   **
   37284                 :   **   *  Always use a unified cache in single-threaded applications
   37285                 :   **
   37286                 :   **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
   37287                 :   **      use separate caches (mode-1)
   37288                 :   */
   37289                 : #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
   37290                 :   const int separateCache = 0;
   37291                 : #else
   37292           19523 :   int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
   37293                 : #endif
   37294                 : 
   37295           19523 :   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
   37296           19523 :   assert( szExtra < 300 );
   37297                 : 
   37298           19523 :   sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
   37299           19523 :   pCache = (PCache1 *)sqlite3_malloc(sz);
   37300           19523 :   if( pCache ){
   37301           19523 :     memset(pCache, 0, sz);
   37302           19523 :     if( separateCache ){
   37303           19523 :       pGroup = (PGroup*)&pCache[1];
   37304           19523 :       pGroup->mxPinned = 10;
   37305                 :     }else{
   37306               0 :       pGroup = &pcache1.grp;
   37307                 :     }
   37308           19523 :     pCache->pGroup = pGroup;
   37309           19523 :     pCache->szPage = szPage;
   37310           19523 :     pCache->szExtra = szExtra;
   37311           19523 :     pCache->bPurgeable = (bPurgeable ? 1 : 0);
   37312           19523 :     if( bPurgeable ){
   37313            4065 :       pCache->nMin = 10;
   37314            4065 :       pcache1EnterMutex(pGroup);
   37315            4065 :       pGroup->nMinPage += pCache->nMin;
   37316            4065 :       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
   37317            4065 :       pcache1LeaveMutex(pGroup);
   37318                 :     }
   37319                 :   }
   37320           19523 :   return (sqlite3_pcache *)pCache;
   37321                 : }
   37322                 : 
   37323                 : /*
   37324                 : ** Implementation of the sqlite3_pcache.xCachesize method. 
   37325                 : **
   37326                 : ** Configure the cache_size limit for a cache.
   37327                 : */
   37328           26455 : static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
   37329           26455 :   PCache1 *pCache = (PCache1 *)p;
   37330           26455 :   if( pCache->bPurgeable ){
   37331           10633 :     PGroup *pGroup = pCache->pGroup;
   37332           10633 :     pcache1EnterMutex(pGroup);
   37333           10633 :     pGroup->nMaxPage += (nMax - pCache->nMax);
   37334           10633 :     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
   37335           10633 :     pCache->nMax = nMax;
   37336           10633 :     pCache->n90pct = pCache->nMax*9/10;
   37337           10633 :     pcache1EnforceMaxPage(pGroup);
   37338           10633 :     pcache1LeaveMutex(pGroup);
   37339                 :   }
   37340           26455 : }
   37341                 : 
   37342                 : /*
   37343                 : ** Implementation of the sqlite3_pcache.xShrink method. 
   37344                 : **
   37345                 : ** Free up as much memory as possible.
   37346                 : */
   37347               0 : static void pcache1Shrink(sqlite3_pcache *p){
   37348               0 :   PCache1 *pCache = (PCache1*)p;
   37349               0 :   if( pCache->bPurgeable ){
   37350               0 :     PGroup *pGroup = pCache->pGroup;
   37351                 :     int savedMaxPage;
   37352               0 :     pcache1EnterMutex(pGroup);
   37353               0 :     savedMaxPage = pGroup->nMaxPage;
   37354               0 :     pGroup->nMaxPage = 0;
   37355               0 :     pcache1EnforceMaxPage(pGroup);
   37356               0 :     pGroup->nMaxPage = savedMaxPage;
   37357               0 :     pcache1LeaveMutex(pGroup);
   37358                 :   }
   37359               0 : }
   37360                 : 
   37361                 : /*
   37362                 : ** Implementation of the sqlite3_pcache.xPagecount method. 
   37363                 : */
   37364           13103 : static int pcache1Pagecount(sqlite3_pcache *p){
   37365                 :   int n;
   37366           13103 :   PCache1 *pCache = (PCache1*)p;
   37367           13103 :   pcache1EnterMutex(pCache->pGroup);
   37368           13103 :   n = pCache->nPage;
   37369           13103 :   pcache1LeaveMutex(pCache->pGroup);
   37370           13103 :   return n;
   37371                 : }
   37372                 : 
   37373                 : /*
   37374                 : ** Implementation of the sqlite3_pcache.xFetch method. 
   37375                 : **
   37376                 : ** Fetch a page by key value.
   37377                 : **
   37378                 : ** Whether or not a new page may be allocated by this function depends on
   37379                 : ** the value of the createFlag argument.  0 means do not allocate a new
   37380                 : ** page.  1 means allocate a new page if space is easily available.  2 
   37381                 : ** means to try really hard to allocate a new page.
   37382                 : **
   37383                 : ** For a non-purgeable cache (a cache used as the storage for an in-memory
   37384                 : ** database) there is really no difference between createFlag 1 and 2.  So
   37385                 : ** the calling function (pcache.c) will never have a createFlag of 1 on
   37386                 : ** a non-purgeable cache.
   37387                 : **
   37388                 : ** There are three different approaches to obtaining space for a page,
   37389                 : ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
   37390                 : **
   37391                 : **   1. Regardless of the value of createFlag, the cache is searched for a 
   37392                 : **      copy of the requested page. If one is found, it is returned.
   37393                 : **
   37394                 : **   2. If createFlag==0 and the page is not already in the cache, NULL is
   37395                 : **      returned.
   37396                 : **
   37397                 : **   3. If createFlag is 1, and the page is not already in the cache, then
   37398                 : **      return NULL (do not allocate a new page) if any of the following
   37399                 : **      conditions are true:
   37400                 : **
   37401                 : **       (a) the number of pages pinned by the cache is greater than
   37402                 : **           PCache1.nMax, or
   37403                 : **
   37404                 : **       (b) the number of pages pinned by the cache is greater than
   37405                 : **           the sum of nMax for all purgeable caches, less the sum of 
   37406                 : **           nMin for all other purgeable caches, or
   37407                 : **
   37408                 : **   4. If none of the first three conditions apply and the cache is marked
   37409                 : **      as purgeable, and if one of the following is true:
   37410                 : **
   37411                 : **       (a) The number of pages allocated for the cache is already 
   37412                 : **           PCache1.nMax, or
   37413                 : **
   37414                 : **       (b) The number of pages allocated for all purgeable caches is
   37415                 : **           already equal to or greater than the sum of nMax for all
   37416                 : **           purgeable caches,
   37417                 : **
   37418                 : **       (c) The system is under memory pressure and wants to avoid
   37419                 : **           unnecessary pages cache entry allocations
   37420                 : **
   37421                 : **      then attempt to recycle a page from the LRU list. If it is the right
   37422                 : **      size, return the recycled buffer. Otherwise, free the buffer and
   37423                 : **      proceed to step 5. 
   37424                 : **
   37425                 : **   5. Otherwise, allocate and return a new page buffer.
   37426                 : */
   37427          638798 : static sqlite3_pcache_page *pcache1Fetch(
   37428                 :   sqlite3_pcache *p, 
   37429                 :   unsigned int iKey, 
   37430                 :   int createFlag
   37431                 : ){
   37432                 :   unsigned int nPinned;
   37433          638798 :   PCache1 *pCache = (PCache1 *)p;
   37434                 :   PGroup *pGroup;
   37435          638798 :   PgHdr1 *pPage = 0;
   37436                 : 
   37437          638798 :   assert( pCache->bPurgeable || createFlag!=1 );
   37438          638798 :   assert( pCache->bPurgeable || pCache->nMin==0 );
   37439          638798 :   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
   37440          638798 :   assert( pCache->nMin==0 || pCache->bPurgeable );
   37441          638798 :   pcache1EnterMutex(pGroup = pCache->pGroup);
   37442                 : 
   37443                 :   /* Step 1: Search the hash table for an existing entry. */
   37444          638798 :   if( pCache->nHash>0 ){
   37445          619275 :     unsigned int h = iKey % pCache->nHash;
   37446          619275 :     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
   37447                 :   }
   37448                 : 
   37449                 :   /* Step 2: Abort if no existing page is found and createFlag is 0 */
   37450          638798 :   if( pPage || createFlag==0 ){
   37451          577766 :     pcache1PinPage(pPage);
   37452          577766 :     goto fetch_out;
   37453                 :   }
   37454                 : 
   37455                 :   /* The pGroup local variable will normally be initialized by the
   37456                 :   ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
   37457                 :   ** then pcache1EnterMutex() is a no-op, so we have to initialize the
   37458                 :   ** local variable here.  Delaying the initialization of pGroup is an
   37459                 :   ** optimization:  The common case is to exit the module before reaching
   37460                 :   ** this point.
   37461                 :   */
   37462                 : #ifdef SQLITE_MUTEX_OMIT
   37463                 :   pGroup = pCache->pGroup;
   37464                 : #endif
   37465                 : 
   37466                 :   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
   37467           61032 :   assert( pCache->nPage >= pCache->nRecyclable );
   37468           61032 :   nPinned = pCache->nPage - pCache->nRecyclable;
   37469           61032 :   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
   37470           61032 :   assert( pCache->n90pct == pCache->nMax*9/10 );
   37471           80893 :   if( createFlag==1 && (
   37472           19861 :         nPinned>=pGroup->mxPinned
   37473           19861 :      || nPinned>=pCache->n90pct
   37474           19861 :      || pcache1UnderMemoryPressure(pCache)
   37475                 :   )){
   37476                 :     goto fetch_out;
   37477                 :   }
   37478                 : 
   37479           61032 :   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
   37480               0 :     goto fetch_out;
   37481                 :   }
   37482                 : 
   37483                 :   /* Step 4. Try to recycle a page. */
   37484           68723 :   if( pCache->bPurgeable && pGroup->pLruTail && (
   37485            7691 :          (pCache->nPage+1>=pCache->nMax)
   37486            7691 :       || pGroup->nCurrentPage>=pGroup->nMaxPage
   37487            7691 :       || pcache1UnderMemoryPressure(pCache)
   37488                 :   )){
   37489                 :     PCache1 *pOther;
   37490               0 :     pPage = pGroup->pLruTail;
   37491               0 :     pcache1RemoveFromHash(pPage);
   37492               0 :     pcache1PinPage(pPage);
   37493               0 :     pOther = pPage->pCache;
   37494                 : 
   37495                 :     /* We want to verify that szPage and szExtra are the same for pOther
   37496                 :     ** and pCache.  Assert that we can verify this by comparing sums. */
   37497               0 :     assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
   37498               0 :     assert( pCache->szExtra<512 );
   37499               0 :     assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
   37500               0 :     assert( pOther->szExtra<512 );
   37501                 : 
   37502               0 :     if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
   37503               0 :       pcache1FreePage(pPage);
   37504               0 :       pPage = 0;
   37505                 :     }else{
   37506               0 :       pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
   37507                 :     }
   37508                 :   }
   37509                 : 
   37510                 :   /* Step 5. If a usable page buffer has still not been found, 
   37511                 :   ** attempt to allocate a new one. 
   37512                 :   */
   37513           61032 :   if( !pPage ){
   37514           61032 :     if( createFlag==1 ) sqlite3BeginBenignMalloc();
   37515           61032 :     pPage = pcache1AllocPage(pCache);
   37516           61032 :     if( createFlag==1 ) sqlite3EndBenignMalloc();
   37517                 :   }
   37518                 : 
   37519           61032 :   if( pPage ){
   37520           61032 :     unsigned int h = iKey % pCache->nHash;
   37521           61032 :     pCache->nPage++;
   37522           61032 :     pPage->iKey = iKey;
   37523           61032 :     pPage->pNext = pCache->apHash[h];
   37524           61032 :     pPage->pCache = pCache;
   37525           61032 :     pPage->pLruPrev = 0;
   37526           61032 :     pPage->pLruNext = 0;
   37527           61032 :     *(void **)pPage->page.pExtra = 0;
   37528           61032 :     pCache->apHash[h] = pPage;
   37529                 :   }
   37530                 : 
   37531                 : fetch_out:
   37532          638798 :   if( pPage && iKey>pCache->iMaxKey ){
   37533           56653 :     pCache->iMaxKey = iKey;
   37534                 :   }
   37535          638798 :   pcache1LeaveMutex(pGroup);
   37536          638798 :   return &pPage->page;
   37537                 : }
   37538                 : 
   37539                 : 
   37540                 : /*
   37541                 : ** Implementation of the sqlite3_pcache.xUnpin method.
   37542                 : **
   37543                 : ** Mark a page as unpinned (eligible for asynchronous recycling).
   37544                 : */
   37545          370050 : static void pcache1Unpin(
   37546                 :   sqlite3_pcache *p, 
   37547                 :   sqlite3_pcache_page *pPg, 
   37548                 :   int reuseUnlikely
   37549                 : ){
   37550          370050 :   PCache1 *pCache = (PCache1 *)p;
   37551          370050 :   PgHdr1 *pPage = (PgHdr1 *)pPg;
   37552          370050 :   PGroup *pGroup = pCache->pGroup;
   37553                 :  
   37554          370050 :   assert( pPage->pCache==pCache );
   37555          370050 :   pcache1EnterMutex(pGroup);
   37556                 : 
   37557                 :   /* It is an error to call this function if the page is already 
   37558                 :   ** part of the PGroup LRU list.
   37559                 :   */
   37560          370050 :   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
   37561          370050 :   assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
   37562                 : 
   37563          370050 :   if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
   37564              11 :     pcache1RemoveFromHash(pPage);
   37565              11 :     pcache1FreePage(pPage);
   37566                 :   }else{
   37567                 :     /* Add the page to the PGroup LRU list. */
   37568          370039 :     if( pGroup->pLruHead ){
   37569          349661 :       pGroup->pLruHead->pLruPrev = pPage;
   37570          349661 :       pPage->pLruNext = pGroup->pLruHead;
   37571          349661 :       pGroup->pLruHead = pPage;
   37572                 :     }else{
   37573           20378 :       pGroup->pLruTail = pPage;
   37574           20378 :       pGroup->pLruHead = pPage;
   37575                 :     }
   37576          370039 :     pCache->nRecyclable++;
   37577                 :   }
   37578                 : 
   37579          370050 :   pcache1LeaveMutex(pCache->pGroup);
   37580          370050 : }
   37581                 : 
   37582                 : /*
   37583                 : ** Implementation of the sqlite3_pcache.xRekey method. 
   37584                 : */
   37585               0 : static void pcache1Rekey(
   37586                 :   sqlite3_pcache *p,
   37587                 :   sqlite3_pcache_page *pPg,
   37588                 :   unsigned int iOld,
   37589                 :   unsigned int iNew
   37590                 : ){
   37591               0 :   PCache1 *pCache = (PCache1 *)p;
   37592               0 :   PgHdr1 *pPage = (PgHdr1 *)pPg;
   37593                 :   PgHdr1 **pp;
   37594                 :   unsigned int h; 
   37595               0 :   assert( pPage->iKey==iOld );
   37596               0 :   assert( pPage->pCache==pCache );
   37597                 : 
   37598               0 :   pcache1EnterMutex(pCache->pGroup);
   37599                 : 
   37600               0 :   h = iOld%pCache->nHash;
   37601               0 :   pp = &pCache->apHash[h];
   37602               0 :   while( (*pp)!=pPage ){
   37603               0 :     pp = &(*pp)->pNext;
   37604                 :   }
   37605               0 :   *pp = pPage->pNext;
   37606                 : 
   37607               0 :   h = iNew%pCache->nHash;
   37608               0 :   pPage->iKey = iNew;
   37609               0 :   pPage->pNext = pCache->apHash[h];
   37610               0 :   pCache->apHash[h] = pPage;
   37611               0 :   if( iNew>pCache->iMaxKey ){
   37612               0 :     pCache->iMaxKey = iNew;
   37613                 :   }
   37614                 : 
   37615               0 :   pcache1LeaveMutex(pCache->pGroup);
   37616               0 : }
   37617                 : 
   37618                 : /*
   37619                 : ** Implementation of the sqlite3_pcache.xTruncate method. 
   37620                 : **
   37621                 : ** Discard all unpinned pages in the cache with a page number equal to
   37622                 : ** or greater than parameter iLimit. Any pinned pages with a page number
   37623                 : ** equal to or greater than iLimit are implicitly unpinned.
   37624                 : */
   37625           78760 : static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
   37626           78760 :   PCache1 *pCache = (PCache1 *)p;
   37627           78760 :   pcache1EnterMutex(pCache->pGroup);
   37628           78760 :   if( iLimit<=pCache->iMaxKey ){
   37629           18879 :     pcache1TruncateUnsafe(pCache, iLimit);
   37630           18879 :     pCache->iMaxKey = iLimit-1;
   37631                 :   }
   37632           78760 :   pcache1LeaveMutex(pCache->pGroup);
   37633           78760 : }
   37634                 : 
   37635                 : /*
   37636                 : ** Implementation of the sqlite3_pcache.xDestroy method. 
   37637                 : **
   37638                 : ** Destroy a cache allocated using pcache1Create().
   37639                 : */
   37640           19523 : static void pcache1Destroy(sqlite3_pcache *p){
   37641           19523 :   PCache1 *pCache = (PCache1 *)p;
   37642           19523 :   PGroup *pGroup = pCache->pGroup;
   37643           19523 :   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
   37644           19523 :   pcache1EnterMutex(pGroup);
   37645           19523 :   pcache1TruncateUnsafe(pCache, 0);
   37646           19523 :   assert( pGroup->nMaxPage >= pCache->nMax );
   37647           19523 :   pGroup->nMaxPage -= pCache->nMax;
   37648           19523 :   assert( pGroup->nMinPage >= pCache->nMin );
   37649           19523 :   pGroup->nMinPage -= pCache->nMin;
   37650           19523 :   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
   37651           19523 :   pcache1EnforceMaxPage(pGroup);
   37652           19523 :   pcache1LeaveMutex(pGroup);
   37653           19523 :   sqlite3_free(pCache->apHash);
   37654           19523 :   sqlite3_free(pCache);
   37655           19523 : }
   37656                 : 
   37657                 : /*
   37658                 : ** This function is called during initialization (sqlite3_initialize()) to
   37659                 : ** install the default pluggable cache module, assuming the user has not
   37660                 : ** already provided an alternative.
   37661                 : */
   37662             805 : SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
   37663                 :   static const sqlite3_pcache_methods2 defaultMethods = {
   37664                 :     1,                       /* iVersion */
   37665                 :     0,                       /* pArg */
   37666                 :     pcache1Init,             /* xInit */
   37667                 :     pcache1Shutdown,         /* xShutdown */
   37668                 :     pcache1Create,           /* xCreate */
   37669                 :     pcache1Cachesize,        /* xCachesize */
   37670                 :     pcache1Pagecount,        /* xPagecount */
   37671                 :     pcache1Fetch,            /* xFetch */
   37672                 :     pcache1Unpin,            /* xUnpin */
   37673                 :     pcache1Rekey,            /* xRekey */
   37674                 :     pcache1Truncate,         /* xTruncate */
   37675                 :     pcache1Destroy,          /* xDestroy */
   37676                 :     pcache1Shrink            /* xShrink */
   37677                 :   };
   37678             805 :   sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
   37679             805 : }
   37680                 : 
   37681                 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   37682                 : /*
   37683                 : ** This function is called to free superfluous dynamically allocated memory
   37684                 : ** held by the pager system. Memory in use by any SQLite pager allocated
   37685                 : ** by the current thread may be sqlite3_free()ed.
   37686                 : **
   37687                 : ** nReq is the number of bytes of memory required. Once this much has
   37688                 : ** been released, the function returns. The return value is the total number 
   37689                 : ** of bytes of memory released.
   37690                 : */
   37691                 : SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
   37692                 :   int nFree = 0;
   37693                 :   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
   37694                 :   assert( sqlite3_mutex_notheld(pcache1.mutex) );
   37695                 :   if( pcache1.pStart==0 ){
   37696                 :     PgHdr1 *p;
   37697                 :     pcache1EnterMutex(&pcache1.grp);
   37698                 :     while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
   37699                 :       nFree += pcache1MemSize(p->page.pBuf);
   37700                 : #ifdef SQLITE_PCACHE_SEPARATE_HEADER
   37701                 :       nFree += sqlite3MemSize(p);
   37702                 : #endif
   37703                 :       pcache1PinPage(p);
   37704                 :       pcache1RemoveFromHash(p);
   37705                 :       pcache1FreePage(p);
   37706                 :     }
   37707                 :     pcache1LeaveMutex(&pcache1.grp);
   37708                 :   }
   37709                 :   return nFree;
   37710                 : }
   37711                 : #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
   37712                 : 
   37713                 : #ifdef SQLITE_TEST
   37714                 : /*
   37715                 : ** This function is used by test procedures to inspect the internal state
   37716                 : ** of the global cache.
   37717                 : */
   37718                 : SQLITE_PRIVATE void sqlite3PcacheStats(
   37719                 :   int *pnCurrent,      /* OUT: Total number of pages cached */
   37720                 :   int *pnMax,          /* OUT: Global maximum cache size */
   37721                 :   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
   37722                 :   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
   37723                 : ){
   37724                 :   PgHdr1 *p;
   37725                 :   int nRecyclable = 0;
   37726                 :   for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
   37727                 :     nRecyclable++;
   37728                 :   }
   37729                 :   *pnCurrent = pcache1.grp.nCurrentPage;
   37730                 :   *pnMax = (int)pcache1.grp.nMaxPage;
   37731                 :   *pnMin = (int)pcache1.grp.nMinPage;
   37732                 :   *pnRecyclable = nRecyclable;
   37733                 : }
   37734                 : #endif
   37735                 : 
   37736                 : /************** End of pcache1.c *********************************************/
   37737                 : /************** Begin file rowset.c ******************************************/
   37738                 : /*
   37739                 : ** 2008 December 3
   37740                 : **
   37741                 : ** The author disclaims copyright to this source code.  In place of
   37742                 : ** a legal notice, here is a blessing:
   37743                 : **
   37744                 : **    May you do good and not evil.
   37745                 : **    May you find forgiveness for yourself and forgive others.
   37746                 : **    May you share freely, never taking more than you give.
   37747                 : **
   37748                 : *************************************************************************
   37749                 : **
   37750                 : ** This module implements an object we call a "RowSet".
   37751                 : **
   37752                 : ** The RowSet object is a collection of rowids.  Rowids
   37753                 : ** are inserted into the RowSet in an arbitrary order.  Inserts
   37754                 : ** can be intermixed with tests to see if a given rowid has been
   37755                 : ** previously inserted into the RowSet.
   37756                 : **
   37757                 : ** After all inserts are finished, it is possible to extract the
   37758                 : ** elements of the RowSet in sorted order.  Once this extraction
   37759                 : ** process has started, no new elements may be inserted.
   37760                 : **
   37761                 : ** Hence, the primitive operations for a RowSet are:
   37762                 : **
   37763                 : **    CREATE
   37764                 : **    INSERT
   37765                 : **    TEST
   37766                 : **    SMALLEST
   37767                 : **    DESTROY
   37768                 : **
   37769                 : ** The CREATE and DESTROY primitives are the constructor and destructor,
   37770                 : ** obviously.  The INSERT primitive adds a new element to the RowSet.
   37771                 : ** TEST checks to see if an element is already in the RowSet.  SMALLEST
   37772                 : ** extracts the least value from the RowSet.
   37773                 : **
   37774                 : ** The INSERT primitive might allocate additional memory.  Memory is
   37775                 : ** allocated in chunks so most INSERTs do no allocation.  There is an 
   37776                 : ** upper bound on the size of allocated memory.  No memory is freed
   37777                 : ** until DESTROY.
   37778                 : **
   37779                 : ** The TEST primitive includes a "batch" number.  The TEST primitive
   37780                 : ** will only see elements that were inserted before the last change
   37781                 : ** in the batch number.  In other words, if an INSERT occurs between
   37782                 : ** two TESTs where the TESTs have the same batch nubmer, then the
   37783                 : ** value added by the INSERT will not be visible to the second TEST.
   37784                 : ** The initial batch number is zero, so if the very first TEST contains
   37785                 : ** a non-zero batch number, it will see all prior INSERTs.
   37786                 : **
   37787                 : ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
   37788                 : ** that is attempted.
   37789                 : **
   37790                 : ** The cost of an INSERT is roughly constant.  (Sometime new memory
   37791                 : ** has to be allocated on an INSERT.)  The cost of a TEST with a new
   37792                 : ** batch number is O(NlogN) where N is the number of elements in the RowSet.
   37793                 : ** The cost of a TEST using the same batch number is O(logN).  The cost
   37794                 : ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
   37795                 : ** primitives are constant time.  The cost of DESTROY is O(N).
   37796                 : **
   37797                 : ** There is an added cost of O(N) when switching between TEST and
   37798                 : ** SMALLEST primitives.
   37799                 : */
   37800                 : 
   37801                 : 
   37802                 : /*
   37803                 : ** Target size for allocation chunks.
   37804                 : */
   37805                 : #define ROWSET_ALLOCATION_SIZE 1024
   37806                 : 
   37807                 : /*
   37808                 : ** The number of rowset entries per allocation chunk.
   37809                 : */
   37810                 : #define ROWSET_ENTRY_PER_CHUNK  \
   37811                 :                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
   37812                 : 
   37813                 : /*
   37814                 : ** Each entry in a RowSet is an instance of the following object.
   37815                 : */
   37816                 : struct RowSetEntry {            
   37817                 :   i64 v;                        /* ROWID value for this entry */
   37818                 :   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
   37819                 :   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
   37820                 : };
   37821                 : 
   37822                 : /*
   37823                 : ** RowSetEntry objects are allocated in large chunks (instances of the
   37824                 : ** following structure) to reduce memory allocation overhead.  The
   37825                 : ** chunks are kept on a linked list so that they can be deallocated
   37826                 : ** when the RowSet is destroyed.
   37827                 : */
   37828                 : struct RowSetChunk {
   37829                 :   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
   37830                 :   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
   37831                 : };
   37832                 : 
   37833                 : /*
   37834                 : ** A RowSet in an instance of the following structure.
   37835                 : **
   37836                 : ** A typedef of this structure if found in sqliteInt.h.
   37837                 : */
   37838                 : struct RowSet {
   37839                 :   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
   37840                 :   sqlite3 *db;                   /* The database connection */
   37841                 :   struct RowSetEntry *pEntry;    /* List of entries using pRight */
   37842                 :   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
   37843                 :   struct RowSetEntry *pFresh;    /* Source of new entry objects */
   37844                 :   struct RowSetEntry *pTree;     /* Binary tree of entries */
   37845                 :   u16 nFresh;                    /* Number of objects on pFresh */
   37846                 :   u8 isSorted;                   /* True if pEntry is sorted */
   37847                 :   u8 iBatch;                     /* Current insert batch */
   37848                 : };
   37849                 : 
   37850                 : /*
   37851                 : ** Turn bulk memory into a RowSet object.  N bytes of memory
   37852                 : ** are available at pSpace.  The db pointer is used as a memory context
   37853                 : ** for any subsequent allocations that need to occur.
   37854                 : ** Return a pointer to the new RowSet object.
   37855                 : **
   37856                 : ** It must be the case that N is sufficient to make a Rowset.  If not
   37857                 : ** an assertion fault occurs.
   37858                 : ** 
   37859                 : ** If N is larger than the minimum, use the surplus as an initial
   37860                 : ** allocation of entries available to be filled.
   37861                 : */
   37862            6092 : SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
   37863                 :   RowSet *p;
   37864            6092 :   assert( N >= ROUND8(sizeof(*p)) );
   37865            6092 :   p = pSpace;
   37866            6092 :   p->pChunk = 0;
   37867            6092 :   p->db = db;
   37868            6092 :   p->pEntry = 0;
   37869            6092 :   p->pLast = 0;
   37870            6092 :   p->pTree = 0;
   37871            6092 :   p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
   37872            6092 :   p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
   37873            6092 :   p->isSorted = 1;
   37874            6092 :   p->iBatch = 0;
   37875            6092 :   return p;
   37876                 : }
   37877                 : 
   37878                 : /*
   37879                 : ** Deallocate all chunks from a RowSet.  This frees all memory that
   37880                 : ** the RowSet has allocated over its lifetime.  This routine is
   37881                 : ** the destructor for the RowSet.
   37882                 : */
   37883           12177 : SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
   37884                 :   struct RowSetChunk *pChunk, *pNextChunk;
   37885           12687 :   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
   37886             510 :     pNextChunk = pChunk->pNextChunk;
   37887             510 :     sqlite3DbFree(p->db, pChunk);
   37888                 :   }
   37889           12177 :   p->pChunk = 0;
   37890           12177 :   p->nFresh = 0;
   37891           12177 :   p->pEntry = 0;
   37892           12177 :   p->pLast = 0;
   37893           12177 :   p->pTree = 0;
   37894           12177 :   p->isSorted = 1;
   37895           12177 : }
   37896                 : 
   37897                 : /*
   37898                 : ** Insert a new value into a RowSet.
   37899                 : **
   37900                 : ** The mallocFailed flag of the database connection is set if a
   37901                 : ** memory allocation fails.
   37902                 : */
   37903           14893 : SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
   37904                 :   struct RowSetEntry *pEntry;  /* The new entry */
   37905                 :   struct RowSetEntry *pLast;   /* The last prior entry */
   37906           14893 :   assert( p!=0 );
   37907           14893 :   if( p->nFresh==0 ){
   37908                 :     struct RowSetChunk *pNew;
   37909             510 :     pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
   37910             510 :     if( pNew==0 ){
   37911               0 :       return;
   37912                 :     }
   37913             510 :     pNew->pNextChunk = p->pChunk;
   37914             510 :     p->pChunk = pNew;
   37915             510 :     p->pFresh = pNew->aEntry;
   37916             510 :     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
   37917                 :   }
   37918           14893 :   pEntry = p->pFresh++;
   37919           14893 :   p->nFresh--;
   37920           14893 :   pEntry->v = rowid;
   37921           14893 :   pEntry->pRight = 0;
   37922           14893 :   pLast = p->pLast;
   37923           14893 :   if( pLast ){
   37924            8801 :     if( p->isSorted && rowid<=pLast->v ){
   37925             141 :       p->isSorted = 0;
   37926                 :     }
   37927            8801 :     pLast->pRight = pEntry;
   37928                 :   }else{
   37929            6092 :     assert( p->pEntry==0 ); /* Fires if INSERT after SMALLEST */
   37930            6092 :     p->pEntry = pEntry;
   37931                 :   }
   37932           14893 :   p->pLast = pEntry;
   37933                 : }
   37934                 : 
   37935                 : /*
   37936                 : ** Merge two lists of RowSetEntry objects.  Remove duplicates.
   37937                 : **
   37938                 : ** The input lists are connected via pRight pointers and are 
   37939                 : ** assumed to each already be in sorted order.
   37940                 : */
   37941            6095 : static struct RowSetEntry *rowSetMerge(
   37942                 :   struct RowSetEntry *pA,    /* First sorted list to be merged */
   37943                 :   struct RowSetEntry *pB     /* Second sorted list to be merged */
   37944                 : ){
   37945                 :   struct RowSetEntry head;
   37946                 :   struct RowSetEntry *pTail;
   37947                 : 
   37948            6095 :   pTail = &head;
   37949           13994 :   while( pA && pB ){
   37950            1804 :     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
   37951            1804 :     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
   37952            1804 :     if( pA->v<pB->v ){
   37953             775 :       pTail->pRight = pA;
   37954             775 :       pA = pA->pRight;
   37955             775 :       pTail = pTail->pRight;
   37956            1029 :     }else if( pB->v<pA->v ){
   37957            1029 :       pTail->pRight = pB;
   37958            1029 :       pB = pB->pRight;
   37959            1029 :       pTail = pTail->pRight;
   37960                 :     }else{
   37961               0 :       pA = pA->pRight;
   37962                 :     }
   37963                 :   }
   37964            6095 :   if( pA ){
   37965            5497 :     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
   37966            5497 :     pTail->pRight = pA;
   37967                 :   }else{
   37968             598 :     assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
   37969             598 :     pTail->pRight = pB;
   37970                 :   }
   37971            6095 :   return head.pRight;
   37972                 : }
   37973                 : 
   37974                 : /*
   37975                 : ** Sort all elements on the pEntry list of the RowSet into ascending order.
   37976                 : */ 
   37977             137 : static void rowSetSort(RowSet *p){
   37978                 :   unsigned int i;
   37979                 :   struct RowSetEntry *pEntry;
   37980                 :   struct RowSetEntry *aBucket[40];
   37981                 : 
   37982             137 :   assert( p->isSorted==0 );
   37983             137 :   memset(aBucket, 0, sizeof(aBucket));
   37984            1137 :   while( p->pEntry ){
   37985             863 :     pEntry = p->pEntry;
   37986             863 :     p->pEntry = pEntry->pRight;
   37987             863 :     pEntry->pRight = 0;
   37988            1478 :     for(i=0; aBucket[i]; i++){
   37989             615 :       pEntry = rowSetMerge(aBucket[i], pEntry);
   37990             615 :       aBucket[i] = 0;
   37991                 :     }
   37992             863 :     aBucket[i] = pEntry;
   37993                 :   }
   37994             137 :   pEntry = 0;
   37995            5617 :   for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
   37996            5480 :     pEntry = rowSetMerge(pEntry, aBucket[i]);
   37997                 :   }
   37998             137 :   p->pEntry = pEntry;
   37999             137 :   p->pLast = 0;
   38000             137 :   p->isSorted = 1;
   38001             137 : }
   38002                 : 
   38003                 : 
   38004                 : /*
   38005                 : ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
   38006                 : ** Convert this tree into a linked list connected by the pRight pointers
   38007                 : ** and return pointers to the first and last elements of the new list.
   38008                 : */
   38009               0 : static void rowSetTreeToList(
   38010                 :   struct RowSetEntry *pIn,         /* Root of the input tree */
   38011                 :   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
   38012                 :   struct RowSetEntry **ppLast      /* Write tail of the output list here */
   38013                 : ){
   38014               0 :   assert( pIn!=0 );
   38015               0 :   if( pIn->pLeft ){
   38016                 :     struct RowSetEntry *p;
   38017               0 :     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
   38018               0 :     p->pRight = pIn;
   38019                 :   }else{
   38020               0 :     *ppFirst = pIn;
   38021                 :   }
   38022               0 :   if( pIn->pRight ){
   38023               0 :     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
   38024                 :   }else{
   38025               0 :     *ppLast = pIn;
   38026                 :   }
   38027               0 :   assert( (*ppLast)->pRight==0 );
   38028               0 : }
   38029                 : 
   38030                 : 
   38031                 : /*
   38032                 : ** Convert a sorted list of elements (connected by pRight) into a binary
   38033                 : ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
   38034                 : ** node taken from the head of *ppList.  A depth of 2 means a tree with
   38035                 : ** three nodes.  And so forth.
   38036                 : **
   38037                 : ** Use as many entries from the input list as required and update the
   38038                 : ** *ppList to point to the unused elements of the list.  If the input
   38039                 : ** list contains too few elements, then construct an incomplete tree
   38040                 : ** and leave *ppList set to NULL.
   38041                 : **
   38042                 : ** Return a pointer to the root of the constructed binary tree.
   38043                 : */
   38044               0 : static struct RowSetEntry *rowSetNDeepTree(
   38045                 :   struct RowSetEntry **ppList,
   38046                 :   int iDepth
   38047                 : ){
   38048                 :   struct RowSetEntry *p;         /* Root of the new tree */
   38049                 :   struct RowSetEntry *pLeft;     /* Left subtree */
   38050               0 :   if( *ppList==0 ){
   38051               0 :     return 0;
   38052                 :   }
   38053               0 :   if( iDepth==1 ){
   38054               0 :     p = *ppList;
   38055               0 :     *ppList = p->pRight;
   38056               0 :     p->pLeft = p->pRight = 0;
   38057               0 :     return p;
   38058                 :   }
   38059               0 :   pLeft = rowSetNDeepTree(ppList, iDepth-1);
   38060               0 :   p = *ppList;
   38061               0 :   if( p==0 ){
   38062               0 :     return pLeft;
   38063                 :   }
   38064               0 :   p->pLeft = pLeft;
   38065               0 :   *ppList = p->pRight;
   38066               0 :   p->pRight = rowSetNDeepTree(ppList, iDepth-1);
   38067               0 :   return p;
   38068                 : }
   38069                 : 
   38070                 : /*
   38071                 : ** Convert a sorted list of elements into a binary tree. Make the tree
   38072                 : ** as deep as it needs to be in order to contain the entire list.
   38073                 : */
   38074               2 : static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
   38075                 :   int iDepth;           /* Depth of the tree so far */
   38076                 :   struct RowSetEntry *p;       /* Current tree root */
   38077                 :   struct RowSetEntry *pLeft;   /* Left subtree */
   38078                 : 
   38079               2 :   assert( pList!=0 );
   38080               2 :   p = pList;
   38081               2 :   pList = p->pRight;
   38082               2 :   p->pLeft = p->pRight = 0;
   38083               2 :   for(iDepth=1; pList; iDepth++){
   38084               0 :     pLeft = p;
   38085               0 :     p = pList;
   38086               0 :     pList = p->pRight;
   38087               0 :     p->pLeft = pLeft;
   38088               0 :     p->pRight = rowSetNDeepTree(&pList, iDepth);
   38089                 :   }
   38090               2 :   return p;
   38091                 : }
   38092                 : 
   38093                 : /*
   38094                 : ** Convert the list in p->pEntry into a sorted list if it is not
   38095                 : ** sorted already.  If there is a binary tree on p->pTree, then
   38096                 : ** convert it into a list too and merge it into the p->pEntry list.
   38097                 : */
   38098           20947 : static void rowSetToList(RowSet *p){
   38099           20947 :   if( !p->isSorted ){
   38100             137 :     rowSetSort(p);
   38101                 :   }
   38102           20947 :   if( p->pTree ){
   38103                 :     struct RowSetEntry *pHead, *pTail;
   38104               0 :     rowSetTreeToList(p->pTree, &pHead, &pTail);
   38105               0 :     p->pTree = 0;
   38106               0 :     p->pEntry = rowSetMerge(p->pEntry, pHead);
   38107                 :   }
   38108           20947 : }
   38109                 : 
   38110                 : /*
   38111                 : ** Extract the smallest element from the RowSet.
   38112                 : ** Write the element into *pRowid.  Return 1 on success.  Return
   38113                 : ** 0 if the RowSet is already empty.
   38114                 : **
   38115                 : ** After this routine has been called, the sqlite3RowSetInsert()
   38116                 : ** routine may not be called again.  
   38117                 : */
   38118           20945 : SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
   38119           20945 :   rowSetToList(p);
   38120           20945 :   if( p->pEntry ){
   38121           14860 :     *pRowid = p->pEntry->v;
   38122           14860 :     p->pEntry = p->pEntry->pRight;
   38123           14860 :     if( p->pEntry==0 ){
   38124            6085 :       sqlite3RowSetClear(p);
   38125                 :     }
   38126           14860 :     return 1;
   38127                 :   }else{
   38128            6085 :     return 0;
   38129                 :   }
   38130                 : }
   38131                 : 
   38132                 : /*
   38133                 : ** Check to see if element iRowid was inserted into the the rowset as
   38134                 : ** part of any insert batch prior to iBatch.  Return 1 or 0.
   38135                 : */
   38136               2 : SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
   38137                 :   struct RowSetEntry *p;
   38138               2 :   if( iBatch!=pRowSet->iBatch ){
   38139               2 :     if( pRowSet->pEntry ){
   38140               2 :       rowSetToList(pRowSet);
   38141               2 :       pRowSet->pTree = rowSetListToTree(pRowSet->pEntry);
   38142               2 :       pRowSet->pEntry = 0;
   38143               2 :       pRowSet->pLast = 0;
   38144                 :     }
   38145               2 :     pRowSet->iBatch = iBatch;
   38146                 :   }
   38147               2 :   p = pRowSet->pTree;
   38148               5 :   while( p ){
   38149               2 :     if( p->v<iRowid ){
   38150               1 :       p = p->pRight;
   38151               1 :     }else if( p->v>iRowid ){
   38152               0 :       p = p->pLeft;
   38153                 :     }else{
   38154               1 :       return 1;
   38155                 :     }
   38156                 :   }
   38157               1 :   return 0;
   38158                 : }
   38159                 : 
   38160                 : /************** End of rowset.c **********************************************/
   38161                 : /************** Begin file pager.c *******************************************/
   38162                 : /*
   38163                 : ** 2001 September 15
   38164                 : **
   38165                 : ** The author disclaims copyright to this source code.  In place of
   38166                 : ** a legal notice, here is a blessing:
   38167                 : **
   38168                 : **    May you do good and not evil.
   38169                 : **    May you find forgiveness for yourself and forgive others.
   38170                 : **    May you share freely, never taking more than you give.
   38171                 : **
   38172                 : *************************************************************************
   38173                 : ** This is the implementation of the page cache subsystem or "pager".
   38174                 : ** 
   38175                 : ** The pager is used to access a database disk file.  It implements
   38176                 : ** atomic commit and rollback through the use of a journal file that
   38177                 : ** is separate from the database file.  The pager also implements file
   38178                 : ** locking to prevent two processes from writing the same database
   38179                 : ** file simultaneously, or one process from reading the database while
   38180                 : ** another is writing.
   38181                 : */
   38182                 : #ifndef SQLITE_OMIT_DISKIO
   38183                 : /************** Include wal.h in the middle of pager.c ***********************/
   38184                 : /************** Begin file wal.h *********************************************/
   38185                 : /*
   38186                 : ** 2010 February 1
   38187                 : **
   38188                 : ** The author disclaims copyright to this source code.  In place of
   38189                 : ** a legal notice, here is a blessing:
   38190                 : **
   38191                 : **    May you do good and not evil.
   38192                 : **    May you find forgiveness for yourself and forgive others.
   38193                 : **    May you share freely, never taking more than you give.
   38194                 : **
   38195                 : *************************************************************************
   38196                 : ** This header file defines the interface to the write-ahead logging 
   38197                 : ** system. Refer to the comments below and the header comment attached to 
   38198                 : ** the implementation of each function in log.c for further details.
   38199                 : */
   38200                 : 
   38201                 : #ifndef _WAL_H_
   38202                 : #define _WAL_H_
   38203                 : 
   38204                 : 
   38205                 : /* Additional values that can be added to the sync_flags argument of
   38206                 : ** sqlite3WalFrames():
   38207                 : */
   38208                 : #define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
   38209                 : #define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
   38210                 : 
   38211                 : #ifdef SQLITE_OMIT_WAL
   38212                 : # define sqlite3WalOpen(x,y,z)                   0
   38213                 : # define sqlite3WalLimit(x,y)
   38214                 : # define sqlite3WalClose(w,x,y,z)                0
   38215                 : # define sqlite3WalBeginReadTransaction(y,z)     0
   38216                 : # define sqlite3WalEndReadTransaction(z)
   38217                 : # define sqlite3WalRead(v,w,x,y,z)               0
   38218                 : # define sqlite3WalDbsize(y)                     0
   38219                 : # define sqlite3WalBeginWriteTransaction(y)      0
   38220                 : # define sqlite3WalEndWriteTransaction(x)        0
   38221                 : # define sqlite3WalUndo(x,y,z)                   0
   38222                 : # define sqlite3WalSavepoint(y,z)
   38223                 : # define sqlite3WalSavepointUndo(y,z)            0
   38224                 : # define sqlite3WalFrames(u,v,w,x,y,z)           0
   38225                 : # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
   38226                 : # define sqlite3WalCallback(z)                   0
   38227                 : # define sqlite3WalExclusiveMode(y,z)            0
   38228                 : # define sqlite3WalHeapMemory(z)                 0
   38229                 : #else
   38230                 : 
   38231                 : #define WAL_SAVEPOINT_NDATA 4
   38232                 : 
   38233                 : /* Connection to a write-ahead log (WAL) file. 
   38234                 : ** There is one object of this type for each pager. 
   38235                 : */
   38236                 : typedef struct Wal Wal;
   38237                 : 
   38238                 : /* Open and close a connection to a write-ahead log. */
   38239                 : SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
   38240                 : SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
   38241                 : 
   38242                 : /* Set the limiting size of a WAL file. */
   38243                 : SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
   38244                 : 
   38245                 : /* Used by readers to open (lock) and close (unlock) a snapshot.  A 
   38246                 : ** snapshot is like a read-transaction.  It is the state of the database
   38247                 : ** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
   38248                 : ** preserves the current state even if the other threads or processes
   38249                 : ** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
   38250                 : ** transaction and releases the lock.
   38251                 : */
   38252                 : SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
   38253                 : SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
   38254                 : 
   38255                 : /* Read a page from the write-ahead log, if it is present. */
   38256                 : SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
   38257                 : 
   38258                 : /* If the WAL is not empty, return the size of the database. */
   38259                 : SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
   38260                 : 
   38261                 : /* Obtain or release the WRITER lock. */
   38262                 : SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
   38263                 : SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
   38264                 : 
   38265                 : /* Undo any frames written (but not committed) to the log */
   38266                 : SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
   38267                 : 
   38268                 : /* Return an integer that records the current (uncommitted) write
   38269                 : ** position in the WAL */
   38270                 : SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
   38271                 : 
   38272                 : /* Move the write position of the WAL back to iFrame.  Called in
   38273                 : ** response to a ROLLBACK TO command. */
   38274                 : SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
   38275                 : 
   38276                 : /* Write a frame or frames to the log. */
   38277                 : SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
   38278                 : 
   38279                 : /* Copy pages from the log to the database file */ 
   38280                 : SQLITE_PRIVATE int sqlite3WalCheckpoint(
   38281                 :   Wal *pWal,                      /* Write-ahead log connection */
   38282                 :   int eMode,                      /* One of PASSIVE, FULL and RESTART */
   38283                 :   int (*xBusy)(void*),            /* Function to call when busy */
   38284                 :   void *pBusyArg,                 /* Context argument for xBusyHandler */
   38285                 :   int sync_flags,                 /* Flags to sync db file with (or 0) */
   38286                 :   int nBuf,                       /* Size of buffer nBuf */
   38287                 :   u8 *zBuf,                       /* Temporary buffer to use */
   38288                 :   int *pnLog,                     /* OUT: Number of frames in WAL */
   38289                 :   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
   38290                 : );
   38291                 : 
   38292                 : /* Return the value to pass to a sqlite3_wal_hook callback, the
   38293                 : ** number of frames in the WAL at the point of the last commit since
   38294                 : ** sqlite3WalCallback() was called.  If no commits have occurred since
   38295                 : ** the last call, then return 0.
   38296                 : */
   38297                 : SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
   38298                 : 
   38299                 : /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
   38300                 : ** by the pager layer on the database file.
   38301                 : */
   38302                 : SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
   38303                 : 
   38304                 : /* Return true if the argument is non-NULL and the WAL module is using
   38305                 : ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
   38306                 : ** WAL module is using shared-memory, return false. 
   38307                 : */
   38308                 : SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
   38309                 : 
   38310                 : #endif /* ifndef SQLITE_OMIT_WAL */
   38311                 : #endif /* _WAL_H_ */
   38312                 : 
   38313                 : /************** End of wal.h *************************************************/
   38314                 : /************** Continuing where we left off in pager.c **********************/
   38315                 : 
   38316                 : 
   38317                 : /******************* NOTES ON THE DESIGN OF THE PAGER ************************
   38318                 : **
   38319                 : ** This comment block describes invariants that hold when using a rollback
   38320                 : ** journal.  These invariants do not apply for journal_mode=WAL,
   38321                 : ** journal_mode=MEMORY, or journal_mode=OFF.
   38322                 : **
   38323                 : ** Within this comment block, a page is deemed to have been synced
   38324                 : ** automatically as soon as it is written when PRAGMA synchronous=OFF.
   38325                 : ** Otherwise, the page is not synced until the xSync method of the VFS
   38326                 : ** is called successfully on the file containing the page.
   38327                 : **
   38328                 : ** Definition:  A page of the database file is said to be "overwriteable" if
   38329                 : ** one or more of the following are true about the page:
   38330                 : ** 
   38331                 : **     (a)  The original content of the page as it was at the beginning of
   38332                 : **          the transaction has been written into the rollback journal and
   38333                 : **          synced.
   38334                 : ** 
   38335                 : **     (b)  The page was a freelist leaf page at the start of the transaction.
   38336                 : ** 
   38337                 : **     (c)  The page number is greater than the largest page that existed in
   38338                 : **          the database file at the start of the transaction.
   38339                 : ** 
   38340                 : ** (1) A page of the database file is never overwritten unless one of the
   38341                 : **     following are true:
   38342                 : ** 
   38343                 : **     (a) The page and all other pages on the same sector are overwriteable.
   38344                 : ** 
   38345                 : **     (b) The atomic page write optimization is enabled, and the entire
   38346                 : **         transaction other than the update of the transaction sequence
   38347                 : **         number consists of a single page change.
   38348                 : ** 
   38349                 : ** (2) The content of a page written into the rollback journal exactly matches
   38350                 : **     both the content in the database when the rollback journal was written
   38351                 : **     and the content in the database at the beginning of the current
   38352                 : **     transaction.
   38353                 : ** 
   38354                 : ** (3) Writes to the database file are an integer multiple of the page size
   38355                 : **     in length and are aligned on a page boundary.
   38356                 : ** 
   38357                 : ** (4) Reads from the database file are either aligned on a page boundary and
   38358                 : **     an integer multiple of the page size in length or are taken from the
   38359                 : **     first 100 bytes of the database file.
   38360                 : ** 
   38361                 : ** (5) All writes to the database file are synced prior to the rollback journal
   38362                 : **     being deleted, truncated, or zeroed.
   38363                 : ** 
   38364                 : ** (6) If a master journal file is used, then all writes to the database file
   38365                 : **     are synced prior to the master journal being deleted.
   38366                 : ** 
   38367                 : ** Definition: Two databases (or the same database at two points it time)
   38368                 : ** are said to be "logically equivalent" if they give the same answer to
   38369                 : ** all queries.  Note in particular the the content of freelist leaf
   38370                 : ** pages can be changed arbitarily without effecting the logical equivalence
   38371                 : ** of the database.
   38372                 : ** 
   38373                 : ** (7) At any time, if any subset, including the empty set and the total set,
   38374                 : **     of the unsynced changes to a rollback journal are removed and the 
   38375                 : **     journal is rolled back, the resulting database file will be logical
   38376                 : **     equivalent to the database file at the beginning of the transaction.
   38377                 : ** 
   38378                 : ** (8) When a transaction is rolled back, the xTruncate method of the VFS
   38379                 : **     is called to restore the database file to the same size it was at
   38380                 : **     the beginning of the transaction.  (In some VFSes, the xTruncate
   38381                 : **     method is a no-op, but that does not change the fact the SQLite will
   38382                 : **     invoke it.)
   38383                 : ** 
   38384                 : ** (9) Whenever the database file is modified, at least one bit in the range
   38385                 : **     of bytes from 24 through 39 inclusive will be changed prior to releasing
   38386                 : **     the EXCLUSIVE lock, thus signaling other connections on the same
   38387                 : **     database to flush their caches.
   38388                 : **
   38389                 : ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
   38390                 : **      than one billion transactions.
   38391                 : **
   38392                 : ** (11) A database file is well-formed at the beginning and at the conclusion
   38393                 : **      of every transaction.
   38394                 : **
   38395                 : ** (12) An EXCLUSIVE lock is held on the database file when writing to
   38396                 : **      the database file.
   38397                 : **
   38398                 : ** (13) A SHARED lock is held on the database file while reading any
   38399                 : **      content out of the database file.
   38400                 : **
   38401                 : ******************************************************************************/
   38402                 : 
   38403                 : /*
   38404                 : ** Macros for troubleshooting.  Normally turned off
   38405                 : */
   38406                 : #if 0
   38407                 : int sqlite3PagerTrace=1;  /* True to enable tracing */
   38408                 : #define sqlite3DebugPrintf printf
   38409                 : #define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
   38410                 : #else
   38411                 : #define PAGERTRACE(X)
   38412                 : #endif
   38413                 : 
   38414                 : /*
   38415                 : ** The following two macros are used within the PAGERTRACE() macros above
   38416                 : ** to print out file-descriptors. 
   38417                 : **
   38418                 : ** PAGERID() takes a pointer to a Pager struct as its argument. The
   38419                 : ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
   38420                 : ** struct as its argument.
   38421                 : */
   38422                 : #define PAGERID(p) ((int)(p->fd))
   38423                 : #define FILEHANDLEID(fd) ((int)fd)
   38424                 : 
   38425                 : /*
   38426                 : ** The Pager.eState variable stores the current 'state' of a pager. A
   38427                 : ** pager may be in any one of the seven states shown in the following
   38428                 : ** state diagram.
   38429                 : **
   38430                 : **                            OPEN <------+------+
   38431                 : **                              |         |      |
   38432                 : **                              V         |      |
   38433                 : **               +---------> READER-------+      |
   38434                 : **               |              |                |
   38435                 : **               |              V                |
   38436                 : **               |<-------WRITER_LOCKED------> ERROR
   38437                 : **               |              |                ^  
   38438                 : **               |              V                |
   38439                 : **               |<------WRITER_CACHEMOD-------->|
   38440                 : **               |              |                |
   38441                 : **               |              V                |
   38442                 : **               |<-------WRITER_DBMOD---------->|
   38443                 : **               |              |                |
   38444                 : **               |              V                |
   38445                 : **               +<------WRITER_FINISHED-------->+
   38446                 : **
   38447                 : **
   38448                 : ** List of state transitions and the C [function] that performs each:
   38449                 : ** 
   38450                 : **   OPEN              -> READER              [sqlite3PagerSharedLock]
   38451                 : **   READER            -> OPEN                [pager_unlock]
   38452                 : **
   38453                 : **   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
   38454                 : **   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
   38455                 : **   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
   38456                 : **   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
   38457                 : **   WRITER_***        -> READER              [pager_end_transaction]
   38458                 : **
   38459                 : **   WRITER_***        -> ERROR               [pager_error]
   38460                 : **   ERROR             -> OPEN                [pager_unlock]
   38461                 : ** 
   38462                 : **
   38463                 : **  OPEN:
   38464                 : **
   38465                 : **    The pager starts up in this state. Nothing is guaranteed in this
   38466                 : **    state - the file may or may not be locked and the database size is
   38467                 : **    unknown. The database may not be read or written.
   38468                 : **
   38469                 : **    * No read or write transaction is active.
   38470                 : **    * Any lock, or no lock at all, may be held on the database file.
   38471                 : **    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
   38472                 : **
   38473                 : **  READER:
   38474                 : **
   38475                 : **    In this state all the requirements for reading the database in 
   38476                 : **    rollback (non-WAL) mode are met. Unless the pager is (or recently
   38477                 : **    was) in exclusive-locking mode, a user-level read transaction is 
   38478                 : **    open. The database size is known in this state.
   38479                 : **
   38480                 : **    A connection running with locking_mode=normal enters this state when
   38481                 : **    it opens a read-transaction on the database and returns to state
   38482                 : **    OPEN after the read-transaction is completed. However a connection
   38483                 : **    running in locking_mode=exclusive (including temp databases) remains in
   38484                 : **    this state even after the read-transaction is closed. The only way
   38485                 : **    a locking_mode=exclusive connection can transition from READER to OPEN
   38486                 : **    is via the ERROR state (see below).
   38487                 : ** 
   38488                 : **    * A read transaction may be active (but a write-transaction cannot).
   38489                 : **    * A SHARED or greater lock is held on the database file.
   38490                 : **    * The dbSize variable may be trusted (even if a user-level read 
   38491                 : **      transaction is not active). The dbOrigSize and dbFileSize variables
   38492                 : **      may not be trusted at this point.
   38493                 : **    * If the database is a WAL database, then the WAL connection is open.
   38494                 : **    * Even if a read-transaction is not open, it is guaranteed that 
   38495                 : **      there is no hot-journal in the file-system.
   38496                 : **
   38497                 : **  WRITER_LOCKED:
   38498                 : **
   38499                 : **    The pager moves to this state from READER when a write-transaction
   38500                 : **    is first opened on the database. In WRITER_LOCKED state, all locks 
   38501                 : **    required to start a write-transaction are held, but no actual 
   38502                 : **    modifications to the cache or database have taken place.
   38503                 : **
   38504                 : **    In rollback mode, a RESERVED or (if the transaction was opened with 
   38505                 : **    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
   38506                 : **    moving to this state, but the journal file is not written to or opened 
   38507                 : **    to in this state. If the transaction is committed or rolled back while 
   38508                 : **    in WRITER_LOCKED state, all that is required is to unlock the database 
   38509                 : **    file.
   38510                 : **
   38511                 : **    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
   38512                 : **    If the connection is running with locking_mode=exclusive, an attempt
   38513                 : **    is made to obtain an EXCLUSIVE lock on the database file.
   38514                 : **
   38515                 : **    * A write transaction is active.
   38516                 : **    * If the connection is open in rollback-mode, a RESERVED or greater 
   38517                 : **      lock is held on the database file.
   38518                 : **    * If the connection is open in WAL-mode, a WAL write transaction
   38519                 : **      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
   38520                 : **      called).
   38521                 : **    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
   38522                 : **    * The contents of the pager cache have not been modified.
   38523                 : **    * The journal file may or may not be open.
   38524                 : **    * Nothing (not even the first header) has been written to the journal.
   38525                 : **
   38526                 : **  WRITER_CACHEMOD:
   38527                 : **
   38528                 : **    A pager moves from WRITER_LOCKED state to this state when a page is
   38529                 : **    first modified by the upper layer. In rollback mode the journal file
   38530                 : **    is opened (if it is not already open) and a header written to the
   38531                 : **    start of it. The database file on disk has not been modified.
   38532                 : **
   38533                 : **    * A write transaction is active.
   38534                 : **    * A RESERVED or greater lock is held on the database file.
   38535                 : **    * The journal file is open and the first header has been written 
   38536                 : **      to it, but the header has not been synced to disk.
   38537                 : **    * The contents of the page cache have been modified.
   38538                 : **
   38539                 : **  WRITER_DBMOD:
   38540                 : **
   38541                 : **    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
   38542                 : **    when it modifies the contents of the database file. WAL connections
   38543                 : **    never enter this state (since they do not modify the database file,
   38544                 : **    just the log file).
   38545                 : **
   38546                 : **    * A write transaction is active.
   38547                 : **    * An EXCLUSIVE or greater lock is held on the database file.
   38548                 : **    * The journal file is open and the first header has been written 
   38549                 : **      and synced to disk.
   38550                 : **    * The contents of the page cache have been modified (and possibly
   38551                 : **      written to disk).
   38552                 : **
   38553                 : **  WRITER_FINISHED:
   38554                 : **
   38555                 : **    It is not possible for a WAL connection to enter this state.
   38556                 : **
   38557                 : **    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
   38558                 : **    state after the entire transaction has been successfully written into the
   38559                 : **    database file. In this state the transaction may be committed simply
   38560                 : **    by finalizing the journal file. Once in WRITER_FINISHED state, it is 
   38561                 : **    not possible to modify the database further. At this point, the upper 
   38562                 : **    layer must either commit or rollback the transaction.
   38563                 : **
   38564                 : **    * A write transaction is active.
   38565                 : **    * An EXCLUSIVE or greater lock is held on the database file.
   38566                 : **    * All writing and syncing of journal and database data has finished.
   38567                 : **      If no error occured, all that remains is to finalize the journal to
   38568                 : **      commit the transaction. If an error did occur, the caller will need
   38569                 : **      to rollback the transaction. 
   38570                 : **
   38571                 : **  ERROR:
   38572                 : **
   38573                 : **    The ERROR state is entered when an IO or disk-full error (including
   38574                 : **    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it 
   38575                 : **    difficult to be sure that the in-memory pager state (cache contents, 
   38576                 : **    db size etc.) are consistent with the contents of the file-system.
   38577                 : **
   38578                 : **    Temporary pager files may enter the ERROR state, but in-memory pagers
   38579                 : **    cannot.
   38580                 : **
   38581                 : **    For example, if an IO error occurs while performing a rollback, 
   38582                 : **    the contents of the page-cache may be left in an inconsistent state.
   38583                 : **    At this point it would be dangerous to change back to READER state
   38584                 : **    (as usually happens after a rollback). Any subsequent readers might
   38585                 : **    report database corruption (due to the inconsistent cache), and if
   38586                 : **    they upgrade to writers, they may inadvertently corrupt the database
   38587                 : **    file. To avoid this hazard, the pager switches into the ERROR state
   38588                 : **    instead of READER following such an error.
   38589                 : **
   38590                 : **    Once it has entered the ERROR state, any attempt to use the pager
   38591                 : **    to read or write data returns an error. Eventually, once all 
   38592                 : **    outstanding transactions have been abandoned, the pager is able to
   38593                 : **    transition back to OPEN state, discarding the contents of the 
   38594                 : **    page-cache and any other in-memory state at the same time. Everything
   38595                 : **    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
   38596                 : **    when a read-transaction is next opened on the pager (transitioning
   38597                 : **    the pager into READER state). At that point the system has recovered 
   38598                 : **    from the error.
   38599                 : **
   38600                 : **    Specifically, the pager jumps into the ERROR state if:
   38601                 : **
   38602                 : **      1. An error occurs while attempting a rollback. This happens in
   38603                 : **         function sqlite3PagerRollback().
   38604                 : **
   38605                 : **      2. An error occurs while attempting to finalize a journal file
   38606                 : **         following a commit in function sqlite3PagerCommitPhaseTwo().
   38607                 : **
   38608                 : **      3. An error occurs while attempting to write to the journal or
   38609                 : **         database file in function pagerStress() in order to free up
   38610                 : **         memory.
   38611                 : **
   38612                 : **    In other cases, the error is returned to the b-tree layer. The b-tree
   38613                 : **    layer then attempts a rollback operation. If the error condition 
   38614                 : **    persists, the pager enters the ERROR state via condition (1) above.
   38615                 : **
   38616                 : **    Condition (3) is necessary because it can be triggered by a read-only
   38617                 : **    statement executed within a transaction. In this case, if the error
   38618                 : **    code were simply returned to the user, the b-tree layer would not
   38619                 : **    automatically attempt a rollback, as it assumes that an error in a
   38620                 : **    read-only statement cannot leave the pager in an internally inconsistent 
   38621                 : **    state.
   38622                 : **
   38623                 : **    * The Pager.errCode variable is set to something other than SQLITE_OK.
   38624                 : **    * There are one or more outstanding references to pages (after the
   38625                 : **      last reference is dropped the pager should move back to OPEN state).
   38626                 : **    * The pager is not an in-memory pager.
   38627                 : **    
   38628                 : **
   38629                 : ** Notes:
   38630                 : **
   38631                 : **   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
   38632                 : **     connection is open in WAL mode. A WAL connection is always in one
   38633                 : **     of the first four states.
   38634                 : **
   38635                 : **   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
   38636                 : **     state. There are two exceptions: immediately after exclusive-mode has
   38637                 : **     been turned on (and before any read or write transactions are 
   38638                 : **     executed), and when the pager is leaving the "error state".
   38639                 : **
   38640                 : **   * See also: assert_pager_state().
   38641                 : */
   38642                 : #define PAGER_OPEN                  0
   38643                 : #define PAGER_READER                1
   38644                 : #define PAGER_WRITER_LOCKED         2
   38645                 : #define PAGER_WRITER_CACHEMOD       3
   38646                 : #define PAGER_WRITER_DBMOD          4
   38647                 : #define PAGER_WRITER_FINISHED       5
   38648                 : #define PAGER_ERROR                 6
   38649                 : 
   38650                 : /*
   38651                 : ** The Pager.eLock variable is almost always set to one of the 
   38652                 : ** following locking-states, according to the lock currently held on
   38653                 : ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
   38654                 : ** This variable is kept up to date as locks are taken and released by
   38655                 : ** the pagerLockDb() and pagerUnlockDb() wrappers.
   38656                 : **
   38657                 : ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
   38658                 : ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
   38659                 : ** the operation was successful. In these circumstances pagerLockDb() and
   38660                 : ** pagerUnlockDb() take a conservative approach - eLock is always updated
   38661                 : ** when unlocking the file, and only updated when locking the file if the
   38662                 : ** VFS call is successful. This way, the Pager.eLock variable may be set
   38663                 : ** to a less exclusive (lower) value than the lock that is actually held
   38664                 : ** at the system level, but it is never set to a more exclusive value.
   38665                 : **
   38666                 : ** This is usually safe. If an xUnlock fails or appears to fail, there may 
   38667                 : ** be a few redundant xLock() calls or a lock may be held for longer than
   38668                 : ** required, but nothing really goes wrong.
   38669                 : **
   38670                 : ** The exception is when the database file is unlocked as the pager moves
   38671                 : ** from ERROR to OPEN state. At this point there may be a hot-journal file 
   38672                 : ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
   38673                 : ** transition, by the same pager or any other). If the call to xUnlock()
   38674                 : ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
   38675                 : ** can confuse the call to xCheckReservedLock() call made later as part
   38676                 : ** of hot-journal detection.
   38677                 : **
   38678                 : ** xCheckReservedLock() is defined as returning true "if there is a RESERVED 
   38679                 : ** lock held by this process or any others". So xCheckReservedLock may 
   38680                 : ** return true because the caller itself is holding an EXCLUSIVE lock (but
   38681                 : ** doesn't know it because of a previous error in xUnlock). If this happens
   38682                 : ** a hot-journal may be mistaken for a journal being created by an active
   38683                 : ** transaction in another process, causing SQLite to read from the database
   38684                 : ** without rolling it back.
   38685                 : **
   38686                 : ** To work around this, if a call to xUnlock() fails when unlocking the
   38687                 : ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
   38688                 : ** is only changed back to a real locking state after a successful call
   38689                 : ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
   38690                 : ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK 
   38691                 : ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
   38692                 : ** lock on the database file before attempting to roll it back. See function
   38693                 : ** PagerSharedLock() for more detail.
   38694                 : **
   38695                 : ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in 
   38696                 : ** PAGER_OPEN state.
   38697                 : */
   38698                 : #define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
   38699                 : 
   38700                 : /*
   38701                 : ** A macro used for invoking the codec if there is one
   38702                 : */
   38703                 : #ifdef SQLITE_HAS_CODEC
   38704                 : # define CODEC1(P,D,N,X,E) \
   38705                 :     if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
   38706                 : # define CODEC2(P,D,N,X,E,O) \
   38707                 :     if( P->xCodec==0 ){ O=(char*)D; }else \
   38708                 :     if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
   38709                 : #else
   38710                 : # define CODEC1(P,D,N,X,E)   /* NO-OP */
   38711                 : # define CODEC2(P,D,N,X,E,O) O=(char*)D
   38712                 : #endif
   38713                 : 
   38714                 : /*
   38715                 : ** The maximum allowed sector size. 64KiB. If the xSectorsize() method 
   38716                 : ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
   38717                 : ** This could conceivably cause corruption following a power failure on
   38718                 : ** such a system. This is currently an undocumented limit.
   38719                 : */
   38720                 : #define MAX_SECTOR_SIZE 0x10000
   38721                 : 
   38722                 : /*
   38723                 : ** An instance of the following structure is allocated for each active
   38724                 : ** savepoint and statement transaction in the system. All such structures
   38725                 : ** are stored in the Pager.aSavepoint[] array, which is allocated and
   38726                 : ** resized using sqlite3Realloc().
   38727                 : **
   38728                 : ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
   38729                 : ** set to 0. If a journal-header is written into the main journal while
   38730                 : ** the savepoint is active, then iHdrOffset is set to the byte offset 
   38731                 : ** immediately following the last journal record written into the main
   38732                 : ** journal before the journal-header. This is required during savepoint
   38733                 : ** rollback (see pagerPlaybackSavepoint()).
   38734                 : */
   38735                 : typedef struct PagerSavepoint PagerSavepoint;
   38736                 : struct PagerSavepoint {
   38737                 :   i64 iOffset;                 /* Starting offset in main journal */
   38738                 :   i64 iHdrOffset;              /* See above */
   38739                 :   Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
   38740                 :   Pgno nOrig;                  /* Original number of pages in file */
   38741                 :   Pgno iSubRec;                /* Index of first record in sub-journal */
   38742                 : #ifndef SQLITE_OMIT_WAL
   38743                 :   u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
   38744                 : #endif
   38745                 : };
   38746                 : 
   38747                 : /*
   38748                 : ** A open page cache is an instance of struct Pager. A description of
   38749                 : ** some of the more important member variables follows:
   38750                 : **
   38751                 : ** eState
   38752                 : **
   38753                 : **   The current 'state' of the pager object. See the comment and state
   38754                 : **   diagram above for a description of the pager state.
   38755                 : **
   38756                 : ** eLock
   38757                 : **
   38758                 : **   For a real on-disk database, the current lock held on the database file -
   38759                 : **   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
   38760                 : **
   38761                 : **   For a temporary or in-memory database (neither of which require any
   38762                 : **   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
   38763                 : **   databases always have Pager.exclusiveMode==1, this tricks the pager
   38764                 : **   logic into thinking that it already has all the locks it will ever
   38765                 : **   need (and no reason to release them).
   38766                 : **
   38767                 : **   In some (obscure) circumstances, this variable may also be set to
   38768                 : **   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
   38769                 : **   details.
   38770                 : **
   38771                 : ** changeCountDone
   38772                 : **
   38773                 : **   This boolean variable is used to make sure that the change-counter 
   38774                 : **   (the 4-byte header field at byte offset 24 of the database file) is 
   38775                 : **   not updated more often than necessary. 
   38776                 : **
   38777                 : **   It is set to true when the change-counter field is updated, which 
   38778                 : **   can only happen if an exclusive lock is held on the database file.
   38779                 : **   It is cleared (set to false) whenever an exclusive lock is 
   38780                 : **   relinquished on the database file. Each time a transaction is committed,
   38781                 : **   The changeCountDone flag is inspected. If it is true, the work of
   38782                 : **   updating the change-counter is omitted for the current transaction.
   38783                 : **
   38784                 : **   This mechanism means that when running in exclusive mode, a connection 
   38785                 : **   need only update the change-counter once, for the first transaction
   38786                 : **   committed.
   38787                 : **
   38788                 : ** setMaster
   38789                 : **
   38790                 : **   When PagerCommitPhaseOne() is called to commit a transaction, it may
   38791                 : **   (or may not) specify a master-journal name to be written into the 
   38792                 : **   journal file before it is synced to disk.
   38793                 : **
   38794                 : **   Whether or not a journal file contains a master-journal pointer affects 
   38795                 : **   the way in which the journal file is finalized after the transaction is 
   38796                 : **   committed or rolled back when running in "journal_mode=PERSIST" mode.
   38797                 : **   If a journal file does not contain a master-journal pointer, it is
   38798                 : **   finalized by overwriting the first journal header with zeroes. If
   38799                 : **   it does contain a master-journal pointer the journal file is finalized 
   38800                 : **   by truncating it to zero bytes, just as if the connection were 
   38801                 : **   running in "journal_mode=truncate" mode.
   38802                 : **
   38803                 : **   Journal files that contain master journal pointers cannot be finalized
   38804                 : **   simply by overwriting the first journal-header with zeroes, as the
   38805                 : **   master journal pointer could interfere with hot-journal rollback of any
   38806                 : **   subsequently interrupted transaction that reuses the journal file.
   38807                 : **
   38808                 : **   The flag is cleared as soon as the journal file is finalized (either
   38809                 : **   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
   38810                 : **   journal file from being successfully finalized, the setMaster flag
   38811                 : **   is cleared anyway (and the pager will move to ERROR state).
   38812                 : **
   38813                 : ** doNotSpill, doNotSyncSpill
   38814                 : **
   38815                 : **   These two boolean variables control the behaviour of cache-spills
   38816                 : **   (calls made by the pcache module to the pagerStress() routine to
   38817                 : **   write cached data to the file-system in order to free up memory).
   38818                 : **
   38819                 : **   When doNotSpill is non-zero, writing to the database from pagerStress()
   38820                 : **   is disabled altogether. This is done in a very obscure case that
   38821                 : **   comes up during savepoint rollback that requires the pcache module
   38822                 : **   to allocate a new page to prevent the journal file from being written
   38823                 : **   while it is being traversed by code in pager_playback().
   38824                 : ** 
   38825                 : **   If doNotSyncSpill is non-zero, writing to the database from pagerStress()
   38826                 : **   is permitted, but syncing the journal file is not. This flag is set
   38827                 : **   by sqlite3PagerWrite() when the file-system sector-size is larger than
   38828                 : **   the database page-size in order to prevent a journal sync from happening 
   38829                 : **   in between the journalling of two pages on the same sector. 
   38830                 : **
   38831                 : ** subjInMemory
   38832                 : **
   38833                 : **   This is a boolean variable. If true, then any required sub-journal
   38834                 : **   is opened as an in-memory journal file. If false, then in-memory
   38835                 : **   sub-journals are only used for in-memory pager files.
   38836                 : **
   38837                 : **   This variable is updated by the upper layer each time a new 
   38838                 : **   write-transaction is opened.
   38839                 : **
   38840                 : ** dbSize, dbOrigSize, dbFileSize
   38841                 : **
   38842                 : **   Variable dbSize is set to the number of pages in the database file.
   38843                 : **   It is valid in PAGER_READER and higher states (all states except for
   38844                 : **   OPEN and ERROR). 
   38845                 : **
   38846                 : **   dbSize is set based on the size of the database file, which may be 
   38847                 : **   larger than the size of the database (the value stored at offset
   38848                 : **   28 of the database header by the btree). If the size of the file
   38849                 : **   is not an integer multiple of the page-size, the value stored in
   38850                 : **   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
   38851                 : **   Except, any file that is greater than 0 bytes in size is considered
   38852                 : **   to have at least one page. (i.e. a 1KB file with 2K page-size leads
   38853                 : **   to dbSize==1).
   38854                 : **
   38855                 : **   During a write-transaction, if pages with page-numbers greater than
   38856                 : **   dbSize are modified in the cache, dbSize is updated accordingly.
   38857                 : **   Similarly, if the database is truncated using PagerTruncateImage(), 
   38858                 : **   dbSize is updated.
   38859                 : **
   38860                 : **   Variables dbOrigSize and dbFileSize are valid in states 
   38861                 : **   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
   38862                 : **   variable at the start of the transaction. It is used during rollback,
   38863                 : **   and to determine whether or not pages need to be journalled before
   38864                 : **   being modified.
   38865                 : **
   38866                 : **   Throughout a write-transaction, dbFileSize contains the size of
   38867                 : **   the file on disk in pages. It is set to a copy of dbSize when the
   38868                 : **   write-transaction is first opened, and updated when VFS calls are made
   38869                 : **   to write or truncate the database file on disk. 
   38870                 : **
   38871                 : **   The only reason the dbFileSize variable is required is to suppress 
   38872                 : **   unnecessary calls to xTruncate() after committing a transaction. If, 
   38873                 : **   when a transaction is committed, the dbFileSize variable indicates 
   38874                 : **   that the database file is larger than the database image (Pager.dbSize), 
   38875                 : **   pager_truncate() is called. The pager_truncate() call uses xFilesize()
   38876                 : **   to measure the database file on disk, and then truncates it if required.
   38877                 : **   dbFileSize is not used when rolling back a transaction. In this case
   38878                 : **   pager_truncate() is called unconditionally (which means there may be
   38879                 : **   a call to xFilesize() that is not strictly required). In either case,
   38880                 : **   pager_truncate() may cause the file to become smaller or larger.
   38881                 : **
   38882                 : ** dbHintSize
   38883                 : **
   38884                 : **   The dbHintSize variable is used to limit the number of calls made to
   38885                 : **   the VFS xFileControl(FCNTL_SIZE_HINT) method. 
   38886                 : **
   38887                 : **   dbHintSize is set to a copy of the dbSize variable when a
   38888                 : **   write-transaction is opened (at the same time as dbFileSize and
   38889                 : **   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
   38890                 : **   dbHintSize is increased to the number of pages that correspond to the
   38891                 : **   size-hint passed to the method call. See pager_write_pagelist() for 
   38892                 : **   details.
   38893                 : **
   38894                 : ** errCode
   38895                 : **
   38896                 : **   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
   38897                 : **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode 
   38898                 : **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX 
   38899                 : **   sub-codes.
   38900                 : */
   38901                 : struct Pager {
   38902                 :   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
   38903                 :   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
   38904                 :   u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
   38905                 :   u8 useJournal;              /* Use a rollback journal on this file */
   38906                 :   u8 noReadlock;              /* Do not bother to obtain readlocks */
   38907                 :   u8 noSync;                  /* Do not sync the journal if true */
   38908                 :   u8 fullSync;                /* Do extra syncs of the journal for robustness */
   38909                 :   u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
   38910                 :   u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
   38911                 :   u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
   38912                 :   u8 tempFile;                /* zFilename is a temporary file */
   38913                 :   u8 readOnly;                /* True for a read-only database */
   38914                 :   u8 memDb;                   /* True to inhibit all file I/O */
   38915                 : 
   38916                 :   /**************************************************************************
   38917                 :   ** The following block contains those class members that change during
   38918                 :   ** routine opertion.  Class members not in this block are either fixed
   38919                 :   ** when the pager is first created or else only change when there is a
   38920                 :   ** significant mode change (such as changing the page_size, locking_mode,
   38921                 :   ** or the journal_mode).  From another view, these class members describe
   38922                 :   ** the "state" of the pager, while other class members describe the
   38923                 :   ** "configuration" of the pager.
   38924                 :   */
   38925                 :   u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
   38926                 :   u8 eLock;                   /* Current lock held on database file */
   38927                 :   u8 changeCountDone;         /* Set after incrementing the change-counter */
   38928                 :   u8 setMaster;               /* True if a m-j name has been written to jrnl */
   38929                 :   u8 doNotSpill;              /* Do not spill the cache when non-zero */
   38930                 :   u8 doNotSyncSpill;          /* Do not do a spill that requires jrnl sync */
   38931                 :   u8 subjInMemory;            /* True to use in-memory sub-journals */
   38932                 :   Pgno dbSize;                /* Number of pages in the database */
   38933                 :   Pgno dbOrigSize;            /* dbSize before the current transaction */
   38934                 :   Pgno dbFileSize;            /* Number of pages in the database file */
   38935                 :   Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
   38936                 :   int errCode;                /* One of several kinds of errors */
   38937                 :   int nRec;                   /* Pages journalled since last j-header written */
   38938                 :   u32 cksumInit;              /* Quasi-random value added to every checksum */
   38939                 :   u32 nSubRec;                /* Number of records written to sub-journal */
   38940                 :   Bitvec *pInJournal;         /* One bit for each page in the database file */
   38941                 :   sqlite3_file *fd;           /* File descriptor for database */
   38942                 :   sqlite3_file *jfd;          /* File descriptor for main journal */
   38943                 :   sqlite3_file *sjfd;         /* File descriptor for sub-journal */
   38944                 :   i64 journalOff;             /* Current write offset in the journal file */
   38945                 :   i64 journalHdr;             /* Byte offset to previous journal header */
   38946                 :   sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
   38947                 :   PagerSavepoint *aSavepoint; /* Array of active savepoints */
   38948                 :   int nSavepoint;             /* Number of elements in aSavepoint[] */
   38949                 :   char dbFileVers[16];        /* Changes whenever database file changes */
   38950                 :   /*
   38951                 :   ** End of the routinely-changing class members
   38952                 :   ***************************************************************************/
   38953                 : 
   38954                 :   u16 nExtra;                 /* Add this many bytes to each in-memory page */
   38955                 :   i16 nReserve;               /* Number of unused bytes at end of each page */
   38956                 :   u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
   38957                 :   u32 sectorSize;             /* Assumed sector size during rollback */
   38958                 :   int pageSize;               /* Number of bytes in a page */
   38959                 :   Pgno mxPgno;                /* Maximum allowed size of the database */
   38960                 :   i64 journalSizeLimit;       /* Size limit for persistent journal files */
   38961                 :   char *zFilename;            /* Name of the database file */
   38962                 :   char *zJournal;             /* Name of the journal file */
   38963                 :   int (*xBusyHandler)(void*); /* Function to call when busy */
   38964                 :   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
   38965                 :   int nHit, nMiss;            /* Total cache hits and misses */
   38966                 : #ifdef SQLITE_TEST
   38967                 :   int nRead, nWrite;          /* Database pages read/written */
   38968                 : #endif
   38969                 :   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
   38970                 : #ifdef SQLITE_HAS_CODEC
   38971                 :   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
   38972                 :   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
   38973                 :   void (*xCodecFree)(void*);             /* Destructor for the codec */
   38974                 :   void *pCodec;               /* First argument to xCodec... methods */
   38975                 : #endif
   38976                 :   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
   38977                 :   PCache *pPCache;            /* Pointer to page cache object */
   38978                 : #ifndef SQLITE_OMIT_WAL
   38979                 :   Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
   38980                 :   char *zWal;                 /* File name for write-ahead log */
   38981                 : #endif
   38982                 : };
   38983                 : 
   38984                 : /*
   38985                 : ** The following global variables hold counters used for
   38986                 : ** testing purposes only.  These variables do not exist in
   38987                 : ** a non-testing build.  These variables are not thread-safe.
   38988                 : */
   38989                 : #ifdef SQLITE_TEST
   38990                 : SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
   38991                 : SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
   38992                 : SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
   38993                 : # define PAGER_INCR(v)  v++
   38994                 : #else
   38995                 : # define PAGER_INCR(v)
   38996                 : #endif
   38997                 : 
   38998                 : 
   38999                 : 
   39000                 : /*
   39001                 : ** Journal files begin with the following magic string.  The data
   39002                 : ** was obtained from /dev/random.  It is used only as a sanity check.
   39003                 : **
   39004                 : ** Since version 2.8.0, the journal format contains additional sanity
   39005                 : ** checking information.  If the power fails while the journal is being
   39006                 : ** written, semi-random garbage data might appear in the journal
   39007                 : ** file after power is restored.  If an attempt is then made
   39008                 : ** to roll the journal back, the database could be corrupted.  The additional
   39009                 : ** sanity checking data is an attempt to discover the garbage in the
   39010                 : ** journal and ignore it.
   39011                 : **
   39012                 : ** The sanity checking information for the new journal format consists
   39013                 : ** of a 32-bit checksum on each page of data.  The checksum covers both
   39014                 : ** the page number and the pPager->pageSize bytes of data for the page.
   39015                 : ** This cksum is initialized to a 32-bit random value that appears in the
   39016                 : ** journal file right after the header.  The random initializer is important,
   39017                 : ** because garbage data that appears at the end of a journal is likely
   39018                 : ** data that was once in other files that have now been deleted.  If the
   39019                 : ** garbage data came from an obsolete journal file, the checksums might
   39020                 : ** be correct.  But by initializing the checksum to random value which
   39021                 : ** is different for every journal, we minimize that risk.
   39022                 : */
   39023                 : static const unsigned char aJournalMagic[] = {
   39024                 :   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
   39025                 : };
   39026                 : 
   39027                 : /*
   39028                 : ** The size of the of each page record in the journal is given by
   39029                 : ** the following macro.
   39030                 : */
   39031                 : #define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
   39032                 : 
   39033                 : /*
   39034                 : ** The journal header size for this pager. This is usually the same 
   39035                 : ** size as a single disk sector. See also setSectorSize().
   39036                 : */
   39037                 : #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
   39038                 : 
   39039                 : /*
   39040                 : ** The macro MEMDB is true if we are dealing with an in-memory database.
   39041                 : ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
   39042                 : ** the value of MEMDB will be a constant and the compiler will optimize
   39043                 : ** out code that would never execute.
   39044                 : */
   39045                 : #ifdef SQLITE_OMIT_MEMORYDB
   39046                 : # define MEMDB 0
   39047                 : #else
   39048                 : # define MEMDB pPager->memDb
   39049                 : #endif
   39050                 : 
   39051                 : /*
   39052                 : ** The maximum legal page number is (2^31 - 1).
   39053                 : */
   39054                 : #define PAGER_MAX_PGNO 2147483647
   39055                 : 
   39056                 : /*
   39057                 : ** The argument to this macro is a file descriptor (type sqlite3_file*).
   39058                 : ** Return 0 if it is not open, or non-zero (but not 1) if it is.
   39059                 : **
   39060                 : ** This is so that expressions can be written as:
   39061                 : **
   39062                 : **   if( isOpen(pPager->jfd) ){ ...
   39063                 : **
   39064                 : ** instead of
   39065                 : **
   39066                 : **   if( pPager->jfd->pMethods ){ ...
   39067                 : */
   39068                 : #define isOpen(pFd) ((pFd)->pMethods)
   39069                 : 
   39070                 : /*
   39071                 : ** Return true if this pager uses a write-ahead log instead of the usual
   39072                 : ** rollback journal. Otherwise false.
   39073                 : */
   39074                 : #ifndef SQLITE_OMIT_WAL
   39075         4183598 : static int pagerUseWal(Pager *pPager){
   39076         4183598 :   return (pPager->pWal!=0);
   39077                 : }
   39078                 : #else
   39079                 : # define pagerUseWal(x) 0
   39080                 : # define pagerRollbackWal(x) 0
   39081                 : # define pagerWalFrames(v,w,x,y) 0
   39082                 : # define pagerOpenWalIfPresent(z) SQLITE_OK
   39083                 : # define pagerBeginReadTransaction(z) SQLITE_OK
   39084                 : #endif
   39085                 : 
   39086                 : #ifndef NDEBUG 
   39087                 : /*
   39088                 : ** Usage:
   39089                 : **
   39090                 : **   assert( assert_pager_state(pPager) );
   39091                 : **
   39092                 : ** This function runs many asserts to try to find inconsistencies in
   39093                 : ** the internal state of the Pager object.
   39094                 : */
   39095         2722454 : static int assert_pager_state(Pager *p){
   39096         2722454 :   Pager *pPager = p;
   39097                 : 
   39098                 :   /* State must be valid. */
   39099         2722454 :   assert( p->eState==PAGER_OPEN
   39100                 :        || p->eState==PAGER_READER
   39101                 :        || p->eState==PAGER_WRITER_LOCKED
   39102                 :        || p->eState==PAGER_WRITER_CACHEMOD
   39103                 :        || p->eState==PAGER_WRITER_DBMOD
   39104                 :        || p->eState==PAGER_WRITER_FINISHED
   39105                 :        || p->eState==PAGER_ERROR
   39106                 :   );
   39107                 : 
   39108                 :   /* Regardless of the current state, a temp-file connection always behaves
   39109                 :   ** as if it has an exclusive lock on the database file. It never updates
   39110                 :   ** the change-counter field, so the changeCountDone flag is always set.
   39111                 :   */
   39112         2722454 :   assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
   39113         2722454 :   assert( p->tempFile==0 || pPager->changeCountDone );
   39114                 : 
   39115                 :   /* If the useJournal flag is clear, the journal-mode must be "OFF". 
   39116                 :   ** And if the journal-mode is "OFF", the journal file must not be open.
   39117                 :   */
   39118         2722454 :   assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
   39119         2722454 :   assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
   39120                 : 
   39121                 :   /* Check that MEMDB implies noSync. And an in-memory journal. Since 
   39122                 :   ** this means an in-memory pager performs no IO at all, it cannot encounter 
   39123                 :   ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing 
   39124                 :   ** a journal file. (although the in-memory journal implementation may 
   39125                 :   ** return SQLITE_IOERR_NOMEM while the journal file is being written). It 
   39126                 :   ** is therefore not possible for an in-memory pager to enter the ERROR 
   39127                 :   ** state.
   39128                 :   */
   39129         2722454 :   if( MEMDB ){
   39130          493219 :     assert( p->noSync );
   39131          493219 :     assert( p->journalMode==PAGER_JOURNALMODE_OFF 
   39132                 :          || p->journalMode==PAGER_JOURNALMODE_MEMORY 
   39133                 :     );
   39134          493219 :     assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
   39135          493219 :     assert( pagerUseWal(p)==0 );
   39136                 :   }
   39137                 : 
   39138                 :   /* If changeCountDone is set, a RESERVED lock or greater must be held
   39139                 :   ** on the file.
   39140                 :   */
   39141         2722454 :   assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
   39142         2722454 :   assert( p->eLock!=PENDING_LOCK );
   39143                 : 
   39144         2722454 :   switch( p->eState ){
   39145                 :     case PAGER_OPEN:
   39146           59683 :       assert( !MEMDB );
   39147           59683 :       assert( pPager->errCode==SQLITE_OK );
   39148           59683 :       assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
   39149           59683 :       break;
   39150                 : 
   39151                 :     case PAGER_READER:
   39152          467539 :       assert( pPager->errCode==SQLITE_OK );
   39153          467539 :       assert( p->eLock!=UNKNOWN_LOCK );
   39154          467539 :       assert( p->eLock>=SHARED_LOCK || p->noReadlock );
   39155          467539 :       break;
   39156                 : 
   39157                 :     case PAGER_WRITER_LOCKED:
   39158          385987 :       assert( p->eLock!=UNKNOWN_LOCK );
   39159          385987 :       assert( pPager->errCode==SQLITE_OK );
   39160          385987 :       if( !pagerUseWal(pPager) ){
   39161          151770 :         assert( p->eLock>=RESERVED_LOCK );
   39162                 :       }
   39163          385987 :       assert( pPager->dbSize==pPager->dbOrigSize );
   39164          385987 :       assert( pPager->dbOrigSize==pPager->dbFileSize );
   39165          385987 :       assert( pPager->dbOrigSize==pPager->dbHintSize );
   39166          385987 :       assert( pPager->setMaster==0 );
   39167          385987 :       break;
   39168                 : 
   39169                 :     case PAGER_WRITER_CACHEMOD:
   39170         1777678 :       assert( p->eLock!=UNKNOWN_LOCK );
   39171         1777678 :       assert( pPager->errCode==SQLITE_OK );
   39172         1777678 :       if( !pagerUseWal(pPager) ){
   39173                 :         /* It is possible that if journal_mode=wal here that neither the
   39174                 :         ** journal file nor the WAL file are open. This happens during
   39175                 :         ** a rollback transaction that switches from journal_mode=off
   39176                 :         ** to journal_mode=wal.
   39177                 :         */
   39178          735351 :         assert( p->eLock>=RESERVED_LOCK );
   39179          735351 :         assert( isOpen(p->jfd) 
   39180                 :              || p->journalMode==PAGER_JOURNALMODE_OFF 
   39181                 :              || p->journalMode==PAGER_JOURNALMODE_WAL 
   39182                 :         );
   39183                 :       }
   39184         1777679 :       assert( pPager->dbOrigSize==pPager->dbFileSize );
   39185         1777679 :       assert( pPager->dbOrigSize==pPager->dbHintSize );
   39186         1777679 :       break;
   39187                 : 
   39188                 :     case PAGER_WRITER_DBMOD:
   39189            8675 :       assert( p->eLock==EXCLUSIVE_LOCK );
   39190            8675 :       assert( pPager->errCode==SQLITE_OK );
   39191            8675 :       assert( !pagerUseWal(pPager) );
   39192            8675 :       assert( p->eLock>=EXCLUSIVE_LOCK );
   39193            8675 :       assert( isOpen(p->jfd) 
   39194                 :            || p->journalMode==PAGER_JOURNALMODE_OFF 
   39195                 :            || p->journalMode==PAGER_JOURNALMODE_WAL 
   39196                 :       );
   39197            8675 :       assert( pPager->dbOrigSize<=pPager->dbHintSize );
   39198            8675 :       break;
   39199                 : 
   39200                 :     case PAGER_WRITER_FINISHED:
   39201           21844 :       assert( p->eLock==EXCLUSIVE_LOCK );
   39202           21844 :       assert( pPager->errCode==SQLITE_OK );
   39203           21844 :       assert( !pagerUseWal(pPager) );
   39204           21844 :       assert( isOpen(p->jfd) 
   39205                 :            || p->journalMode==PAGER_JOURNALMODE_OFF 
   39206                 :            || p->journalMode==PAGER_JOURNALMODE_WAL 
   39207                 :       );
   39208           21844 :       break;
   39209                 : 
   39210                 :     case PAGER_ERROR:
   39211                 :       /* There must be at least one outstanding reference to the pager if
   39212                 :       ** in ERROR state. Otherwise the pager should have already dropped
   39213                 :       ** back to OPEN state.
   39214                 :       */
   39215            1047 :       assert( pPager->errCode!=SQLITE_OK );
   39216            1047 :       assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
   39217            1047 :       break;
   39218                 :   }
   39219                 : 
   39220         2722455 :   return 1;
   39221                 : }
   39222                 : #endif /* ifndef NDEBUG */
   39223                 : 
   39224                 : #ifdef SQLITE_DEBUG 
   39225                 : /*
   39226                 : ** Return a pointer to a human readable string in a static buffer
   39227                 : ** containing the state of the Pager object passed as an argument. This
   39228                 : ** is intended to be used within debuggers. For example, as an alternative
   39229                 : ** to "print *pPager" in gdb:
   39230                 : **
   39231                 : ** (gdb) printf "%s", print_pager_state(pPager)
   39232                 : */
   39233             517 : static char *print_pager_state(Pager *p){
   39234                 :   static char zRet[1024];
   39235                 : 
   39236            6677 :   sqlite3_snprintf(1024, zRet,
   39237                 :       "Filename:      %s\n"
   39238                 :       "State:         %s errCode=%d\n"
   39239                 :       "Lock:          %s\n"
   39240                 :       "Locking mode:  locking_mode=%s\n"
   39241                 :       "Journal mode:  journal_mode=%s\n"
   39242                 :       "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
   39243                 :       "Journal:       journalOff=%lld journalHdr=%lld\n"
   39244                 :       "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
   39245                 :       , p->zFilename
   39246             517 :       , p->eState==PAGER_OPEN            ? "OPEN" :
   39247             952 :         p->eState==PAGER_READER          ? "READER" :
   39248             952 :         p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
   39249             476 :         p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
   39250               0 :         p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
   39251               0 :         p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
   39252               0 :         p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
   39253                 :       , (int)p->errCode
   39254             517 :       , p->eLock==NO_LOCK         ? "NO_LOCK" :
   39255            1028 :         p->eLock==RESERVED_LOCK   ? "RESERVED" :
   39256             552 :         p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
   39257              38 :         p->eLock==SHARED_LOCK     ? "SHARED" :
   39258               0 :         p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
   39259             517 :       , p->exclusiveMode ? "exclusive" : "normal"
   39260             517 :       , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
   39261            1034 :         p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
   39262             555 :         p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
   39263              76 :         p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
   39264              76 :         p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
   39265              38 :         p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
   39266            1551 :       , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
   39267                 :       , p->journalOff, p->journalHdr
   39268            1551 :       , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
   39269                 :   );
   39270                 : 
   39271             517 :   return zRet;
   39272                 : }
   39273                 : #endif
   39274                 : 
   39275                 : /*
   39276                 : ** Return true if it is necessary to write page *pPg into the sub-journal.
   39277                 : ** A page needs to be written into the sub-journal if there exists one
   39278                 : ** or more open savepoints for which:
   39279                 : **
   39280                 : **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
   39281                 : **   * The bit corresponding to the page-number is not set in
   39282                 : **     PagerSavepoint.pInSavepoint.
   39283                 : */
   39284          627930 : static int subjRequiresPage(PgHdr *pPg){
   39285          627930 :   Pgno pgno = pPg->pgno;
   39286          627930 :   Pager *pPager = pPg->pPager;
   39287                 :   int i;
   39288          700384 :   for(i=0; i<pPager->nSavepoint; i++){
   39289          321061 :     PagerSavepoint *p = &pPager->aSavepoint[i];
   39290          321061 :     if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
   39291          248607 :       return 1;
   39292                 :     }
   39293                 :   }
   39294          379323 :   return 0;
   39295                 : }
   39296                 : 
   39297                 : /*
   39298                 : ** Return true if the page is already in the journal file.
   39299                 : */
   39300          839611 : static int pageInJournal(PgHdr *pPg){
   39301          839611 :   return sqlite3BitvecTest(pPg->pPager->pInJournal, pPg->pgno);
   39302                 : }
   39303                 : 
   39304                 : /*
   39305                 : ** Read a 32-bit integer from the given file descriptor.  Store the integer
   39306                 : ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
   39307                 : ** error code is something goes wrong.
   39308                 : **
   39309                 : ** All values are stored on disk as big-endian.
   39310                 : */
   39311             123 : static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
   39312                 :   unsigned char ac[4];
   39313             123 :   int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
   39314             123 :   if( rc==SQLITE_OK ){
   39315             123 :     *pRes = sqlite3Get4byte(ac);
   39316                 :   }
   39317             123 :   return rc;
   39318                 : }
   39319                 : 
   39320                 : /*
   39321                 : ** Write a 32-bit integer into a string buffer in big-endian byte order.
   39322                 : */
   39323                 : #define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
   39324                 : 
   39325                 : 
   39326                 : /*
   39327                 : ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
   39328                 : ** on success or an error code is something goes wrong.
   39329                 : */
   39330           96476 : static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
   39331                 :   char ac[4];
   39332           96476 :   put32bits(ac, val);
   39333           96476 :   return sqlite3OsWrite(fd, ac, 4, offset);
   39334                 : }
   39335                 : 
   39336                 : /*
   39337                 : ** Unlock the database file to level eLock, which must be either NO_LOCK
   39338                 : ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
   39339                 : ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
   39340                 : **
   39341                 : ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
   39342                 : ** called, do not modify it. See the comment above the #define of 
   39343                 : ** UNKNOWN_LOCK for an explanation of this.
   39344                 : */
   39345           41687 : static int pagerUnlockDb(Pager *pPager, int eLock){
   39346           41687 :   int rc = SQLITE_OK;
   39347                 : 
   39348           41687 :   assert( !pPager->exclusiveMode || pPager->eLock==eLock );
   39349           41687 :   assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
   39350           41687 :   assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
   39351           41687 :   if( isOpen(pPager->fd) ){
   39352           25243 :     assert( pPager->eLock>=eLock );
   39353           25243 :     rc = sqlite3OsUnlock(pPager->fd, eLock);
   39354           25243 :     if( pPager->eLock!=UNKNOWN_LOCK ){
   39355           25243 :       pPager->eLock = (u8)eLock;
   39356                 :     }
   39357                 :     IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
   39358                 :   }
   39359           41687 :   return rc;
   39360                 : }
   39361                 : 
   39362                 : /*
   39363                 : ** Lock the database file to level eLock, which must be either SHARED_LOCK,
   39364                 : ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
   39365                 : ** Pager.eLock variable to the new locking state. 
   39366                 : **
   39367                 : ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is 
   39368                 : ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK. 
   39369                 : ** See the comment above the #define of UNKNOWN_LOCK for an explanation 
   39370                 : ** of this.
   39371                 : */
   39372           70595 : static int pagerLockDb(Pager *pPager, int eLock){
   39373           70595 :   int rc = SQLITE_OK;
   39374                 : 
   39375           70595 :   assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
   39376           70595 :   if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
   39377           31108 :     rc = sqlite3OsLock(pPager->fd, eLock);
   39378           31108 :     if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
   39379           31107 :       pPager->eLock = (u8)eLock;
   39380                 :       IOTRACE(("LOCK %p %d\n", pPager, eLock))
   39381                 :     }
   39382                 :   }
   39383           70595 :   return rc;
   39384                 : }
   39385                 : 
   39386                 : /*
   39387                 : ** This function determines whether or not the atomic-write optimization
   39388                 : ** can be used with this pager. The optimization can be used if:
   39389                 : **
   39390                 : **  (a) the value returned by OsDeviceCharacteristics() indicates that
   39391                 : **      a database page may be written atomically, and
   39392                 : **  (b) the value returned by OsSectorSize() is less than or equal
   39393                 : **      to the page size.
   39394                 : **
   39395                 : ** The optimization is also always enabled for temporary files. It is
   39396                 : ** an error to call this function if pPager is opened on an in-memory
   39397                 : ** database.
   39398                 : **
   39399                 : ** If the optimization cannot be used, 0 is returned. If it can be used,
   39400                 : ** then the value returned is the size of the journal file when it
   39401                 : ** contains rollback data for exactly one page.
   39402                 : */
   39403                 : #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   39404                 : static int jrnlBufferSize(Pager *pPager){
   39405                 :   assert( !MEMDB );
   39406                 :   if( !pPager->tempFile ){
   39407                 :     int dc;                           /* Device characteristics */
   39408                 :     int nSector;                      /* Sector size */
   39409                 :     int szPage;                       /* Page size */
   39410                 : 
   39411                 :     assert( isOpen(pPager->fd) );
   39412                 :     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
   39413                 :     nSector = pPager->sectorSize;
   39414                 :     szPage = pPager->pageSize;
   39415                 : 
   39416                 :     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
   39417                 :     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
   39418                 :     if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
   39419                 :       return 0;
   39420                 :     }
   39421                 :   }
   39422                 : 
   39423                 :   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
   39424                 : }
   39425                 : #endif
   39426                 : 
   39427                 : /*
   39428                 : ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
   39429                 : ** on the cache using a hash function.  This is used for testing
   39430                 : ** and debugging only.
   39431                 : */
   39432                 : #ifdef SQLITE_CHECK_PAGES
   39433                 : /*
   39434                 : ** Return a 32-bit hash of the page data for pPage.
   39435                 : */
   39436                 : static u32 pager_datahash(int nByte, unsigned char *pData){
   39437                 :   u32 hash = 0;
   39438                 :   int i;
   39439                 :   for(i=0; i<nByte; i++){
   39440                 :     hash = (hash*1039) + pData[i];
   39441                 :   }
   39442                 :   return hash;
   39443                 : }
   39444                 : static u32 pager_pagehash(PgHdr *pPage){
   39445                 :   return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
   39446                 : }
   39447                 : static void pager_set_pagehash(PgHdr *pPage){
   39448                 :   pPage->pageHash = pager_pagehash(pPage);
   39449                 : }
   39450                 : 
   39451                 : /*
   39452                 : ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
   39453                 : ** is defined, and NDEBUG is not defined, an assert() statement checks
   39454                 : ** that the page is either dirty or still matches the calculated page-hash.
   39455                 : */
   39456                 : #define CHECK_PAGE(x) checkPage(x)
   39457                 : static void checkPage(PgHdr *pPg){
   39458                 :   Pager *pPager = pPg->pPager;
   39459                 :   assert( pPager->eState!=PAGER_ERROR );
   39460                 :   assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
   39461                 : }
   39462                 : 
   39463                 : #else
   39464                 : #define pager_datahash(X,Y)  0
   39465                 : #define pager_pagehash(X)  0
   39466                 : #define pager_set_pagehash(X)
   39467                 : #define CHECK_PAGE(x)
   39468                 : #endif  /* SQLITE_CHECK_PAGES */
   39469                 : 
   39470                 : /*
   39471                 : ** When this is called the journal file for pager pPager must be open.
   39472                 : ** This function attempts to read a master journal file name from the 
   39473                 : ** end of the file and, if successful, copies it into memory supplied 
   39474                 : ** by the caller. See comments above writeMasterJournal() for the format
   39475                 : ** used to store a master journal file name at the end of a journal file.
   39476                 : **
   39477                 : ** zMaster must point to a buffer of at least nMaster bytes allocated by
   39478                 : ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
   39479                 : ** enough space to write the master journal name). If the master journal
   39480                 : ** name in the journal is longer than nMaster bytes (including a
   39481                 : ** nul-terminator), then this is handled as if no master journal name
   39482                 : ** were present in the journal.
   39483                 : **
   39484                 : ** If a master journal file name is present at the end of the journal
   39485                 : ** file, then it is copied into the buffer pointed to by zMaster. A
   39486                 : ** nul-terminator byte is appended to the buffer following the master
   39487                 : ** journal file name.
   39488                 : **
   39489                 : ** If it is determined that no master journal file name is present 
   39490                 : ** zMaster[0] is set to 0 and SQLITE_OK returned.
   39491                 : **
   39492                 : ** If an error occurs while reading from the journal file, an SQLite
   39493                 : ** error code is returned.
   39494                 : */
   39495              18 : static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
   39496                 :   int rc;                    /* Return code */
   39497                 :   u32 len;                   /* Length in bytes of master journal name */
   39498                 :   i64 szJ;                   /* Total size in bytes of journal file pJrnl */
   39499                 :   u32 cksum;                 /* MJ checksum value read from journal */
   39500                 :   u32 u;                     /* Unsigned loop counter */
   39501                 :   unsigned char aMagic[8];   /* A buffer to hold the magic header */
   39502              18 :   zMaster[0] = '\0';
   39503                 : 
   39504              18 :   if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
   39505              18 :    || szJ<16
   39506              18 :    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
   39507              18 :    || len>=nMaster 
   39508              10 :    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
   39509              10 :    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
   39510              10 :    || memcmp(aMagic, aJournalMagic, 8)
   39511               0 :    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
   39512                 :   ){
   39513              18 :     return rc;
   39514                 :   }
   39515                 : 
   39516                 :   /* See if the checksum matches the master journal name */
   39517               0 :   for(u=0; u<len; u++){
   39518               0 :     cksum -= zMaster[u];
   39519                 :   }
   39520               0 :   if( cksum ){
   39521                 :     /* If the checksum doesn't add up, then one or more of the disk sectors
   39522                 :     ** containing the master journal filename is corrupted. This means
   39523                 :     ** definitely roll back, so just return SQLITE_OK and report a (nul)
   39524                 :     ** master-journal filename.
   39525                 :     */
   39526               0 :     len = 0;
   39527                 :   }
   39528               0 :   zMaster[len] = '\0';
   39529                 :    
   39530               0 :   return SQLITE_OK;
   39531                 : }
   39532                 : 
   39533                 : /*
   39534                 : ** Return the offset of the sector boundary at or immediately 
   39535                 : ** following the value in pPager->journalOff, assuming a sector 
   39536                 : ** size of pPager->sectorSize bytes.
   39537                 : **
   39538                 : ** i.e for a sector size of 512:
   39539                 : **
   39540                 : **   Pager.journalOff          Return value
   39541                 : **   ---------------------------------------
   39542                 : **   0                         0
   39543                 : **   512                       512
   39544                 : **   100                       512
   39545                 : **   2000                      2048
   39546                 : ** 
   39547                 : */
   39548           17392 : static i64 journalHdrOffset(Pager *pPager){
   39549           17392 :   i64 offset = 0;
   39550           17392 :   i64 c = pPager->journalOff;
   39551           17392 :   if( c ){
   39552            6452 :     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
   39553                 :   }
   39554           17392 :   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
   39555           17392 :   assert( offset>=c );
   39556           17392 :   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
   39557           17392 :   return offset;
   39558                 : }
   39559                 : 
   39560                 : /*
   39561                 : ** The journal file must be open when this function is called.
   39562                 : **
   39563                 : ** This function is a no-op if the journal file has not been written to
   39564                 : ** within the current transaction (i.e. if Pager.journalOff==0).
   39565                 : **
   39566                 : ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
   39567                 : ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
   39568                 : ** zero the 28-byte header at the start of the journal file. In either case, 
   39569                 : ** if the pager is not in no-sync mode, sync the journal file immediately 
   39570                 : ** after writing or truncating it.
   39571                 : **
   39572                 : ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
   39573                 : ** following the truncation or zeroing described above the size of the 
   39574                 : ** journal file in bytes is larger than this value, then truncate the
   39575                 : ** journal file to Pager.journalSizeLimit bytes. The journal file does
   39576                 : ** not need to be synced following this operation.
   39577                 : **
   39578                 : ** If an IO error occurs, abandon processing and return the IO error code.
   39579                 : ** Otherwise, return SQLITE_OK.
   39580                 : */
   39581            2376 : static int zeroJournalHdr(Pager *pPager, int doTruncate){
   39582            2376 :   int rc = SQLITE_OK;                               /* Return code */
   39583            2376 :   assert( isOpen(pPager->jfd) );
   39584            2376 :   if( pPager->journalOff ){
   39585            2356 :     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
   39586                 : 
   39587                 :     IOTRACE(("JZEROHDR %p\n", pPager))
   39588            2356 :     if( doTruncate || iLimit==0 ){
   39589               0 :       rc = sqlite3OsTruncate(pPager->jfd, 0);
   39590                 :     }else{
   39591                 :       static const char zeroHdr[28] = {0};
   39592            2356 :       rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
   39593                 :     }
   39594            2356 :     if( rc==SQLITE_OK && !pPager->noSync ){
   39595            2318 :       rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
   39596                 :     }
   39597                 : 
   39598                 :     /* At this point the transaction is committed but the write lock 
   39599                 :     ** is still held on the file. If there is a size limit configured for 
   39600                 :     ** the persistent journal and the journal file currently consumes more
   39601                 :     ** space than that limit allows for, truncate it now. There is no need
   39602                 :     ** to sync the file following this operation.
   39603                 :     */
   39604            2356 :     if( rc==SQLITE_OK && iLimit>0 ){
   39605                 :       i64 sz;
   39606               0 :       rc = sqlite3OsFileSize(pPager->jfd, &sz);
   39607               0 :       if( rc==SQLITE_OK && sz>iLimit ){
   39608               0 :         rc = sqlite3OsTruncate(pPager->jfd, iLimit);
   39609                 :       }
   39610                 :     }
   39611                 :   }
   39612            2376 :   return rc;
   39613                 : }
   39614                 : 
   39615                 : /*
   39616                 : ** The journal file must be open when this routine is called. A journal
   39617                 : ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
   39618                 : ** current location.
   39619                 : **
   39620                 : ** The format for the journal header is as follows:
   39621                 : ** - 8 bytes: Magic identifying journal format.
   39622                 : ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
   39623                 : ** - 4 bytes: Random number used for page hash.
   39624                 : ** - 4 bytes: Initial database page count.
   39625                 : ** - 4 bytes: Sector size used by the process that wrote this journal.
   39626                 : ** - 4 bytes: Database page size.
   39627                 : ** 
   39628                 : ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
   39629                 : */
   39630           10931 : static int writeJournalHdr(Pager *pPager){
   39631           10931 :   int rc = SQLITE_OK;                 /* Return code */
   39632           10931 :   char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
   39633           10931 :   u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
   39634                 :   u32 nWrite;                         /* Bytes of header sector written */
   39635                 :   int ii;                             /* Loop counter */
   39636                 : 
   39637           10931 :   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
   39638                 : 
   39639           10931 :   if( nHeader>JOURNAL_HDR_SZ(pPager) ){
   39640           10931 :     nHeader = JOURNAL_HDR_SZ(pPager);
   39641                 :   }
   39642                 : 
   39643                 :   /* If there are active savepoints and any of them were created 
   39644                 :   ** since the most recent journal header was written, update the 
   39645                 :   ** PagerSavepoint.iHdrOffset fields now.
   39646                 :   */
   39647           12107 :   for(ii=0; ii<pPager->nSavepoint; ii++){
   39648            1176 :     if( pPager->aSavepoint[ii].iHdrOffset==0 ){
   39649            1176 :       pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
   39650                 :     }
   39651                 :   }
   39652                 : 
   39653           10931 :   pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
   39654                 : 
   39655                 :   /* 
   39656                 :   ** Write the nRec Field - the number of page records that follow this
   39657                 :   ** journal header. Normally, zero is written to this value at this time.
   39658                 :   ** After the records are added to the journal (and the journal synced, 
   39659                 :   ** if in full-sync mode), the zero is overwritten with the true number
   39660                 :   ** of records (see syncJournal()).
   39661                 :   **
   39662                 :   ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
   39663                 :   ** reading the journal this value tells SQLite to assume that the
   39664                 :   ** rest of the journal file contains valid page records. This assumption
   39665                 :   ** is dangerous, as if a failure occurred whilst writing to the journal
   39666                 :   ** file it may contain some garbage data. There are two scenarios
   39667                 :   ** where this risk can be ignored:
   39668                 :   **
   39669                 :   **   * When the pager is in no-sync mode. Corruption can follow a
   39670                 :   **     power failure in this case anyway.
   39671                 :   **
   39672                 :   **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
   39673                 :   **     that garbage data is never appended to the journal file.
   39674                 :   */
   39675           10931 :   assert( isOpen(pPager->fd) || pPager->noSync );
   39676           10931 :   if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
   39677            6449 :    || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
   39678                 :   ){
   39679            4482 :     memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
   39680            4482 :     put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
   39681                 :   }else{
   39682            6449 :     memset(zHeader, 0, sizeof(aJournalMagic)+4);
   39683                 :   }
   39684                 : 
   39685                 :   /* The random check-hash initialiser */ 
   39686           10931 :   sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
   39687           10931 :   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
   39688                 :   /* The initial database size */
   39689           10931 :   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
   39690                 :   /* The assumed sector size for this process */
   39691           10931 :   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
   39692                 : 
   39693                 :   /* The page size */
   39694           10931 :   put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
   39695                 : 
   39696                 :   /* Initializing the tail of the buffer is not necessary.  Everything
   39697                 :   ** works find if the following memset() is omitted.  But initializing
   39698                 :   ** the memory prevents valgrind from complaining, so we are willing to
   39699                 :   ** take the performance hit.
   39700                 :   */
   39701           10931 :   memset(&zHeader[sizeof(aJournalMagic)+20], 0,
   39702                 :          nHeader-(sizeof(aJournalMagic)+20));
   39703                 : 
   39704                 :   /* In theory, it is only necessary to write the 28 bytes that the 
   39705                 :   ** journal header consumes to the journal file here. Then increment the 
   39706                 :   ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next 
   39707                 :   ** record is written to the following sector (leaving a gap in the file
   39708                 :   ** that will be implicitly filled in by the OS).
   39709                 :   **
   39710                 :   ** However it has been discovered that on some systems this pattern can 
   39711                 :   ** be significantly slower than contiguously writing data to the file,
   39712                 :   ** even if that means explicitly writing data to the block of 
   39713                 :   ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
   39714                 :   ** is done. 
   39715                 :   **
   39716                 :   ** The loop is required here in case the sector-size is larger than the 
   39717                 :   ** database page size. Since the zHeader buffer is only Pager.pageSize
   39718                 :   ** bytes in size, more than one call to sqlite3OsWrite() may be required
   39719                 :   ** to populate the entire journal header sector.
   39720                 :   */ 
   39721           21862 :   for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
   39722                 :     IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
   39723           10931 :     rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
   39724           10931 :     assert( pPager->journalHdr <= pPager->journalOff );
   39725           10931 :     pPager->journalOff += nHeader;
   39726                 :   }
   39727                 : 
   39728           10931 :   return rc;
   39729                 : }
   39730                 : 
   39731                 : /*
   39732                 : ** The journal file must be open when this is called. A journal header file
   39733                 : ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
   39734                 : ** file. The current location in the journal file is given by
   39735                 : ** pPager->journalOff. See comments above function writeJournalHdr() for
   39736                 : ** a description of the journal header format.
   39737                 : **
   39738                 : ** If the header is read successfully, *pNRec is set to the number of
   39739                 : ** page records following this header and *pDbSize is set to the size of the
   39740                 : ** database before the transaction began, in pages. Also, pPager->cksumInit
   39741                 : ** is set to the value read from the journal header. SQLITE_OK is returned
   39742                 : ** in this case.
   39743                 : **
   39744                 : ** If the journal header file appears to be corrupted, SQLITE_DONE is
   39745                 : ** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
   39746                 : ** cannot be read from the journal file an error code is returned.
   39747                 : */
   39748              18 : static int readJournalHdr(
   39749                 :   Pager *pPager,               /* Pager object */
   39750                 :   int isHot,
   39751                 :   i64 journalSize,             /* Size of the open journal file in bytes */
   39752                 :   u32 *pNRec,                  /* OUT: Value read from the nRec field */
   39753                 :   u32 *pDbSize                 /* OUT: Value of original database size field */
   39754                 : ){
   39755                 :   int rc;                      /* Return code */
   39756                 :   unsigned char aMagic[8];     /* A buffer to hold the magic header */
   39757                 :   i64 iHdrOff;                 /* Offset of journal header being read */
   39758                 : 
   39759              18 :   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
   39760                 : 
   39761                 :   /* Advance Pager.journalOff to the start of the next sector. If the
   39762                 :   ** journal file is too small for there to be a header stored at this
   39763                 :   ** point, return SQLITE_DONE.
   39764                 :   */
   39765              18 :   pPager->journalOff = journalHdrOffset(pPager);
   39766              18 :   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
   39767               9 :     return SQLITE_DONE;
   39768                 :   }
   39769               9 :   iHdrOff = pPager->journalOff;
   39770                 : 
   39771                 :   /* Read in the first 8 bytes of the journal header. If they do not match
   39772                 :   ** the  magic string found at the start of each journal header, return
   39773                 :   ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
   39774                 :   ** proceed.
   39775                 :   */
   39776               9 :   if( isHot || iHdrOff!=pPager->journalHdr ){
   39777               0 :     rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
   39778               0 :     if( rc ){
   39779               0 :       return rc;
   39780                 :     }
   39781               0 :     if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
   39782               0 :       return SQLITE_DONE;
   39783                 :     }
   39784                 :   }
   39785                 : 
   39786                 :   /* Read the first three 32-bit fields of the journal header: The nRec
   39787                 :   ** field, the checksum-initializer and the database size at the start
   39788                 :   ** of the transaction. Return an error code if anything goes wrong.
   39789                 :   */
   39790               9 :   if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
   39791               9 :    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
   39792               9 :    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
   39793                 :   ){
   39794               0 :     return rc;
   39795                 :   }
   39796                 : 
   39797               9 :   if( pPager->journalOff==0 ){
   39798                 :     u32 iPageSize;               /* Page-size field of journal header */
   39799                 :     u32 iSectorSize;             /* Sector-size field of journal header */
   39800                 : 
   39801                 :     /* Read the page-size and sector-size journal header fields. */
   39802               9 :     if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
   39803               9 :      || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
   39804                 :     ){
   39805               0 :       return rc;
   39806                 :     }
   39807                 : 
   39808                 :     /* Versions of SQLite prior to 3.5.8 set the page-size field of the
   39809                 :     ** journal header to zero. In this case, assume that the Pager.pageSize
   39810                 :     ** variable is already set to the correct page size.
   39811                 :     */
   39812               9 :     if( iPageSize==0 ){
   39813               0 :       iPageSize = pPager->pageSize;
   39814                 :     }
   39815                 : 
   39816                 :     /* Check that the values read from the page-size and sector-size fields
   39817                 :     ** are within range. To be 'in range', both values need to be a power
   39818                 :     ** of two greater than or equal to 512 or 32, and not greater than their 
   39819                 :     ** respective compile time maximum limits.
   39820                 :     */
   39821               9 :     if( iPageSize<512                  || iSectorSize<32
   39822               9 :      || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
   39823               9 :      || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0 
   39824                 :     ){
   39825                 :       /* If the either the page-size or sector-size in the journal-header is 
   39826                 :       ** invalid, then the process that wrote the journal-header must have 
   39827                 :       ** crashed before the header was synced. In this case stop reading 
   39828                 :       ** the journal file here.
   39829                 :       */
   39830               0 :       return SQLITE_DONE;
   39831                 :     }
   39832                 : 
   39833                 :     /* Update the page-size to match the value read from the journal. 
   39834                 :     ** Use a testcase() macro to make sure that malloc failure within 
   39835                 :     ** PagerSetPagesize() is tested.
   39836                 :     */
   39837               9 :     rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
   39838                 :     testcase( rc!=SQLITE_OK );
   39839                 : 
   39840                 :     /* Update the assumed sector-size to match the value used by 
   39841                 :     ** the process that created this journal. If this journal was
   39842                 :     ** created by a process other than this one, then this routine
   39843                 :     ** is being called from within pager_playback(). The local value
   39844                 :     ** of Pager.sectorSize is restored at the end of that routine.
   39845                 :     */
   39846               9 :     pPager->sectorSize = iSectorSize;
   39847                 :   }
   39848                 : 
   39849               9 :   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
   39850               9 :   return rc;
   39851                 : }
   39852                 : 
   39853                 : 
   39854                 : /*
   39855                 : ** Write the supplied master journal name into the journal file for pager
   39856                 : ** pPager at the current location. The master journal name must be the last
   39857                 : ** thing written to a journal file. If the pager is in full-sync mode, the
   39858                 : ** journal file descriptor is advanced to the next sector boundary before
   39859                 : ** anything is written. The format is:
   39860                 : **
   39861                 : **   + 4 bytes: PAGER_MJ_PGNO.
   39862                 : **   + N bytes: Master journal filename in utf-8.
   39863                 : **   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
   39864                 : **   + 4 bytes: Master journal name checksum.
   39865                 : **   + 8 bytes: aJournalMagic[].
   39866                 : **
   39867                 : ** The master journal page checksum is the sum of the bytes in the master
   39868                 : ** journal name, where each byte is interpreted as a signed 8-bit integer.
   39869                 : **
   39870                 : ** If zMaster is a NULL pointer (occurs for a single database transaction), 
   39871                 : ** this call is a no-op.
   39872                 : */
   39873            8675 : static int writeMasterJournal(Pager *pPager, const char *zMaster){
   39874                 :   int rc;                          /* Return code */
   39875                 :   int nMaster;                     /* Length of string zMaster */
   39876                 :   i64 iHdrOff;                     /* Offset of header in journal file */
   39877                 :   i64 jrnlSize;                    /* Size of journal file on disk */
   39878            8675 :   u32 cksum = 0;                   /* Checksum of string zMaster */
   39879                 : 
   39880            8675 :   assert( pPager->setMaster==0 );
   39881            8675 :   assert( !pagerUseWal(pPager) );
   39882                 : 
   39883            8675 :   if( !zMaster 
   39884               0 :    || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
   39885               0 :    || pPager->journalMode==PAGER_JOURNALMODE_OFF 
   39886                 :   ){
   39887            8675 :     return SQLITE_OK;
   39888                 :   }
   39889               0 :   pPager->setMaster = 1;
   39890               0 :   assert( isOpen(pPager->jfd) );
   39891               0 :   assert( pPager->journalHdr <= pPager->journalOff );
   39892                 : 
   39893                 :   /* Calculate the length in bytes and the checksum of zMaster */
   39894               0 :   for(nMaster=0; zMaster[nMaster]; nMaster++){
   39895               0 :     cksum += zMaster[nMaster];
   39896                 :   }
   39897                 : 
   39898                 :   /* If in full-sync mode, advance to the next disk sector before writing
   39899                 :   ** the master journal name. This is in case the previous page written to
   39900                 :   ** the journal has already been synced.
   39901                 :   */
   39902               0 :   if( pPager->fullSync ){
   39903               0 :     pPager->journalOff = journalHdrOffset(pPager);
   39904                 :   }
   39905               0 :   iHdrOff = pPager->journalOff;
   39906                 : 
   39907                 :   /* Write the master journal data to the end of the journal file. If
   39908                 :   ** an error occurs, return the error code to the caller.
   39909                 :   */
   39910               0 :   if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
   39911               0 :    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
   39912               0 :    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
   39913               0 :    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
   39914               0 :    || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
   39915                 :   ){
   39916               0 :     return rc;
   39917                 :   }
   39918               0 :   pPager->journalOff += (nMaster+20);
   39919                 : 
   39920                 :   /* If the pager is in peristent-journal mode, then the physical 
   39921                 :   ** journal-file may extend past the end of the master-journal name
   39922                 :   ** and 8 bytes of magic data just written to the file. This is 
   39923                 :   ** dangerous because the code to rollback a hot-journal file
   39924                 :   ** will not be able to find the master-journal name to determine 
   39925                 :   ** whether or not the journal is hot. 
   39926                 :   **
   39927                 :   ** Easiest thing to do in this scenario is to truncate the journal 
   39928                 :   ** file to the required size.
   39929                 :   */ 
   39930               0 :   if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
   39931               0 :    && jrnlSize>pPager->journalOff
   39932                 :   ){
   39933               0 :     rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
   39934                 :   }
   39935               0 :   return rc;
   39936                 : }
   39937                 : 
   39938                 : /*
   39939                 : ** Find a page in the hash table given its page number. Return
   39940                 : ** a pointer to the page or NULL if the requested page is not 
   39941                 : ** already in memory.
   39942                 : */
   39943              31 : static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
   39944                 :   PgHdr *p;                         /* Return value */
   39945                 : 
   39946                 :   /* It is not possible for a call to PcacheFetch() with createFlag==0 to
   39947                 :   ** fail, since no attempt to allocate dynamic memory will be made.
   39948                 :   */
   39949              31 :   (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
   39950              31 :   return p;
   39951                 : }
   39952                 : 
   39953                 : /*
   39954                 : ** Discard the entire contents of the in-memory page-cache.
   39955                 : */
   39956           40629 : static void pager_reset(Pager *pPager){
   39957           40629 :   sqlite3BackupRestart(pPager->pBackup);
   39958           40629 :   sqlite3PcacheClear(pPager->pPCache);
   39959           40629 : }
   39960                 : 
   39961                 : /*
   39962                 : ** Free all structures in the Pager.aSavepoint[] array and set both
   39963                 : ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
   39964                 : ** if it is open and the pager is not in exclusive mode.
   39965                 : */
   39966          177381 : static void releaseAllSavepoints(Pager *pPager){
   39967                 :   int ii;               /* Iterator for looping through Pager.aSavepoint */
   39968          177405 :   for(ii=0; ii<pPager->nSavepoint; ii++){
   39969              24 :     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
   39970                 :   }
   39971          177381 :   if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
   39972          108661 :     sqlite3OsClose(pPager->sjfd);
   39973                 :   }
   39974          177381 :   sqlite3_free(pPager->aSavepoint);
   39975          177381 :   pPager->aSavepoint = 0;
   39976          177381 :   pPager->nSavepoint = 0;
   39977          177381 :   pPager->nSubRec = 0;
   39978          177381 : }
   39979                 : 
   39980                 : /*
   39981                 : ** Set the bit number pgno in the PagerSavepoint.pInSavepoint 
   39982                 : ** bitvecs of all open savepoints. Return SQLITE_OK if successful
   39983                 : ** or SQLITE_NOMEM if a malloc failure occurs.
   39984                 : */
   39985          105837 : static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
   39986                 :   int ii;                   /* Loop counter */
   39987          105837 :   int rc = SQLITE_OK;       /* Result code */
   39988                 : 
   39989          175033 :   for(ii=0; ii<pPager->nSavepoint; ii++){
   39990           69196 :     PagerSavepoint *p = &pPager->aSavepoint[ii];
   39991           69196 :     if( pgno<=p->nOrig ){
   39992           67693 :       rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
   39993                 :       testcase( rc==SQLITE_NOMEM );
   39994           67693 :       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
   39995                 :     }
   39996                 :   }
   39997          105837 :   return rc;
   39998                 : }
   39999                 : 
   40000                 : /*
   40001                 : ** This function is a no-op if the pager is in exclusive mode and not
   40002                 : ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
   40003                 : ** state.
   40004                 : **
   40005                 : ** If the pager is not in exclusive-access mode, the database file is
   40006                 : ** completely unlocked. If the file is unlocked and the file-system does
   40007                 : ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
   40008                 : ** closed (if it is open).
   40009                 : **
   40010                 : ** If the pager is in ERROR state when this function is called, the 
   40011                 : ** contents of the pager cache are discarded before switching back to 
   40012                 : ** the OPEN state. Regardless of whether the pager is in exclusive-mode
   40013                 : ** or not, any journal file left in the file-system will be treated
   40014                 : ** as a hot-journal and rolled back the next time a read-transaction
   40015                 : ** is opened (by this or by any other connection).
   40016                 : */
   40017          119639 : static void pager_unlock(Pager *pPager){
   40018                 : 
   40019          119639 :   assert( pPager->eState==PAGER_READER 
   40020                 :        || pPager->eState==PAGER_OPEN 
   40021                 :        || pPager->eState==PAGER_ERROR 
   40022                 :   );
   40023                 : 
   40024          119639 :   sqlite3BitvecDestroy(pPager->pInJournal);
   40025          119639 :   pPager->pInJournal = 0;
   40026          119639 :   releaseAllSavepoints(pPager);
   40027                 : 
   40028          119639 :   if( pagerUseWal(pPager) ){
   40029           40491 :     assert( !isOpen(pPager->jfd) );
   40030           40491 :     sqlite3WalEndReadTransaction(pPager->pWal);
   40031           40491 :     pPager->eState = PAGER_OPEN;
   40032           79148 :   }else if( !pPager->exclusiveMode ){
   40033                 :     int rc;                       /* Error code returned by pagerUnlockDb() */
   40034           34183 :     int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
   40035                 : 
   40036                 :     /* If the operating system support deletion of open files, then
   40037                 :     ** close the journal file when dropping the database lock.  Otherwise
   40038                 :     ** another connection with journal_mode=delete might delete the file
   40039                 :     ** out from under us.
   40040                 :     */
   40041                 :     assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
   40042                 :     assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
   40043                 :     assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
   40044                 :     assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
   40045                 :     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
   40046                 :     assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
   40047           34183 :     if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
   40048               0 :      || 1!=(pPager->journalMode & 5)
   40049                 :     ){
   40050           34183 :       sqlite3OsClose(pPager->jfd);
   40051                 :     }
   40052                 : 
   40053                 :     /* If the pager is in the ERROR state and the call to unlock the database
   40054                 :     ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
   40055                 :     ** above the #define for UNKNOWN_LOCK for an explanation of why this
   40056                 :     ** is necessary.
   40057                 :     */
   40058           34183 :     rc = pagerUnlockDb(pPager, NO_LOCK);
   40059           34183 :     if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
   40060               0 :       pPager->eLock = UNKNOWN_LOCK;
   40061                 :     }
   40062                 : 
   40063                 :     /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
   40064                 :     ** without clearing the error code. This is intentional - the error
   40065                 :     ** code is cleared and the cache reset in the block below.
   40066                 :     */
   40067           34183 :     assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
   40068           34183 :     pPager->changeCountDone = 0;
   40069           34183 :     pPager->eState = PAGER_OPEN;
   40070                 :   }
   40071                 : 
   40072                 :   /* If Pager.errCode is set, the contents of the pager cache cannot be
   40073                 :   ** trusted. Now that there are no outstanding references to the pager,
   40074                 :   ** it can safely move back to PAGER_OPEN state. This happens in both
   40075                 :   ** normal and exclusive-locking mode.
   40076                 :   */
   40077          119639 :   if( pPager->errCode ){
   40078             986 :     assert( !MEMDB );
   40079             986 :     pager_reset(pPager);
   40080             986 :     pPager->changeCountDone = pPager->tempFile;
   40081             986 :     pPager->eState = PAGER_OPEN;
   40082             986 :     pPager->errCode = SQLITE_OK;
   40083                 :   }
   40084                 : 
   40085          119639 :   pPager->journalOff = 0;
   40086          119639 :   pPager->journalHdr = 0;
   40087          119639 :   pPager->setMaster = 0;
   40088          119639 : }
   40089                 : 
   40090                 : /*
   40091                 : ** This function is called whenever an IOERR or FULL error that requires
   40092                 : ** the pager to transition into the ERROR state may ahve occurred.
   40093                 : ** The first argument is a pointer to the pager structure, the second 
   40094                 : ** the error-code about to be returned by a pager API function. The 
   40095                 : ** value returned is a copy of the second argument to this function. 
   40096                 : **
   40097                 : ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
   40098                 : ** IOERR sub-codes, the pager enters the ERROR state and the error code
   40099                 : ** is stored in Pager.errCode. While the pager remains in the ERROR state,
   40100                 : ** all major API calls on the Pager will immediately return Pager.errCode.
   40101                 : **
   40102                 : ** The ERROR state indicates that the contents of the pager-cache 
   40103                 : ** cannot be trusted. This state can be cleared by completely discarding 
   40104                 : ** the contents of the pager-cache. If a transaction was active when
   40105                 : ** the persistent error occurred, then the rollback journal may need
   40106                 : ** to be replayed to restore the contents of the database file (as if
   40107                 : ** it were a hot-journal).
   40108                 : */
   40109           56754 : static int pager_error(Pager *pPager, int rc){
   40110           56754 :   int rc2 = rc & 0xff;
   40111           56754 :   assert( rc==SQLITE_OK || !MEMDB );
   40112           56754 :   assert(
   40113                 :        pPager->errCode==SQLITE_FULL ||
   40114                 :        pPager->errCode==SQLITE_OK ||
   40115                 :        (pPager->errCode & 0xff)==SQLITE_IOERR
   40116                 :   );
   40117           56754 :   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
   40118               0 :     pPager->errCode = rc;
   40119               0 :     pPager->eState = PAGER_ERROR;
   40120                 :   }
   40121           56754 :   return rc;
   40122                 : }
   40123                 : 
   40124                 : /*
   40125                 : ** This routine ends a transaction. A transaction is usually ended by 
   40126                 : ** either a COMMIT or a ROLLBACK operation. This routine may be called 
   40127                 : ** after rollback of a hot-journal, or if an error occurs while opening
   40128                 : ** the journal file or writing the very first journal-header of a
   40129                 : ** database transaction.
   40130                 : ** 
   40131                 : ** This routine is never called in PAGER_ERROR state. If it is called
   40132                 : ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
   40133                 : ** exclusive than a RESERVED lock, it is a no-op.
   40134                 : **
   40135                 : ** Otherwise, any active savepoints are released.
   40136                 : **
   40137                 : ** If the journal file is open, then it is "finalized". Once a journal 
   40138                 : ** file has been finalized it is not possible to use it to roll back a 
   40139                 : ** transaction. Nor will it be considered to be a hot-journal by this
   40140                 : ** or any other database connection. Exactly how a journal is finalized
   40141                 : ** depends on whether or not the pager is running in exclusive mode and
   40142                 : ** the current journal-mode (Pager.journalMode value), as follows:
   40143                 : **
   40144                 : **   journalMode==MEMORY
   40145                 : **     Journal file descriptor is simply closed. This destroys an 
   40146                 : **     in-memory journal.
   40147                 : **
   40148                 : **   journalMode==TRUNCATE
   40149                 : **     Journal file is truncated to zero bytes in size.
   40150                 : **
   40151                 : **   journalMode==PERSIST
   40152                 : **     The first 28 bytes of the journal file are zeroed. This invalidates
   40153                 : **     the first journal header in the file, and hence the entire journal
   40154                 : **     file. An invalid journal file cannot be rolled back.
   40155                 : **
   40156                 : **   journalMode==DELETE
   40157                 : **     The journal file is closed and deleted using sqlite3OsDelete().
   40158                 : **
   40159                 : **     If the pager is running in exclusive mode, this method of finalizing
   40160                 : **     the journal file is never used. Instead, if the journalMode is
   40161                 : **     DELETE and the pager is in exclusive mode, the method described under
   40162                 : **     journalMode==PERSIST is used instead.
   40163                 : **
   40164                 : ** After the journal is finalized, the pager moves to PAGER_READER state.
   40165                 : ** If running in non-exclusive rollback mode, the lock on the file is 
   40166                 : ** downgraded to a SHARED_LOCK.
   40167                 : **
   40168                 : ** SQLITE_OK is returned if no error occurs. If an error occurs during
   40169                 : ** any of the IO operations to finalize the journal file or unlock the
   40170                 : ** database then the IO error code is returned to the user. If the 
   40171                 : ** operation to finalize the journal file fails, then the code still
   40172                 : ** tries to unlock the database file if not in exclusive mode. If the
   40173                 : ** unlock operation fails as well, then the first error code related
   40174                 : ** to the first error encountered (the journal finalization one) is
   40175                 : ** returned.
   40176                 : */
   40177          112737 : static int pager_end_transaction(Pager *pPager, int hasMaster){
   40178          112737 :   int rc = SQLITE_OK;      /* Error code from journal finalization operation */
   40179          112737 :   int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
   40180                 : 
   40181                 :   /* Do nothing if the pager does not have an open write transaction
   40182                 :   ** or at least a RESERVED lock. This function may be called when there
   40183                 :   ** is no write-transaction active but a RESERVED or greater lock is
   40184                 :   ** held under two circumstances:
   40185                 :   **
   40186                 :   **   1. After a successful hot-journal rollback, it is called with
   40187                 :   **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
   40188                 :   **
   40189                 :   **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE 
   40190                 :   **      lock switches back to locking_mode=normal and then executes a
   40191                 :   **      read-transaction, this function is called with eState==PAGER_READER 
   40192                 :   **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
   40193                 :   */
   40194          112737 :   assert( assert_pager_state(pPager) );
   40195          112737 :   assert( pPager->eState!=PAGER_ERROR );
   40196          112737 :   if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
   40197           54995 :     return SQLITE_OK;
   40198                 :   }
   40199                 : 
   40200           57742 :   releaseAllSavepoints(pPager);
   40201           57742 :   assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
   40202           57742 :   if( isOpen(pPager->jfd) ){
   40203           11798 :     assert( !pagerUseWal(pPager) );
   40204                 : 
   40205                 :     /* Finalize the journal file. */
   40206           11798 :     if( sqlite3IsMemJournal(pPager->jfd) ){
   40207            2250 :       assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
   40208            2250 :       sqlite3OsClose(pPager->jfd);
   40209            9548 :     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
   40210               0 :       if( pPager->journalOff==0 ){
   40211               0 :         rc = SQLITE_OK;
   40212                 :       }else{
   40213               0 :         rc = sqlite3OsTruncate(pPager->jfd, 0);
   40214                 :       }
   40215               0 :       pPager->journalOff = 0;
   40216            9548 :     }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
   40217            9548 :       || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
   40218                 :     ){
   40219            2376 :       rc = zeroJournalHdr(pPager, hasMaster);
   40220            2376 :       pPager->journalOff = 0;
   40221                 :     }else{
   40222                 :       /* This branch may be executed with Pager.journalMode==MEMORY if
   40223                 :       ** a hot-journal was just rolled back. In this case the journal
   40224                 :       ** file should be closed and deleted. If this connection writes to
   40225                 :       ** the database file, it will do so using an in-memory journal. 
   40226                 :       */
   40227            7172 :       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE 
   40228                 :            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
   40229                 :            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
   40230                 :       );
   40231            7172 :       sqlite3OsClose(pPager->jfd);
   40232            7172 :       if( !pPager->tempFile ){
   40233            7151 :         rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
   40234                 :       }
   40235                 :     }
   40236                 :   }
   40237                 : 
   40238                 : #ifdef SQLITE_CHECK_PAGES
   40239                 :   sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
   40240                 :   if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
   40241                 :     PgHdr *p = pager_lookup(pPager, 1);
   40242                 :     if( p ){
   40243                 :       p->pageHash = 0;
   40244                 :       sqlite3PagerUnref(p);
   40245                 :     }
   40246                 :   }
   40247                 : #endif
   40248                 : 
   40249           57742 :   sqlite3BitvecDestroy(pPager->pInJournal);
   40250           57742 :   pPager->pInJournal = 0;
   40251           57742 :   pPager->nRec = 0;
   40252           57742 :   sqlite3PcacheCleanAll(pPager->pPCache);
   40253           57742 :   sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
   40254                 : 
   40255           57742 :   if( pagerUseWal(pPager) ){
   40256                 :     /* Drop the WAL write-lock, if any. Also, if the connection was in 
   40257                 :     ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE 
   40258                 :     ** lock held on the database file.
   40259                 :     */
   40260           26364 :     rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
   40261           26364 :     assert( rc2==SQLITE_OK );
   40262                 :   }
   40263           57742 :   if( !pPager->exclusiveMode 
   40264           33868 :    && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
   40265                 :   ){
   40266            7504 :     rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
   40267            7504 :     pPager->changeCountDone = 0;
   40268                 :   }
   40269           57742 :   pPager->eState = PAGER_READER;
   40270           57742 :   pPager->setMaster = 0;
   40271                 : 
   40272           57742 :   return (rc==SQLITE_OK?rc2:rc);
   40273                 : }
   40274                 : 
   40275                 : /*
   40276                 : ** Execute a rollback if a transaction is active and unlock the 
   40277                 : ** database file. 
   40278                 : **
   40279                 : ** If the pager has already entered the ERROR state, do not attempt 
   40280                 : ** the rollback at this time. Instead, pager_unlock() is called. The
   40281                 : ** call to pager_unlock() will discard all in-memory pages, unlock
   40282                 : ** the database file and move the pager back to OPEN state. If this 
   40283                 : ** means that there is a hot-journal left in the file-system, the next 
   40284                 : ** connection to obtain a shared lock on the pager (which may be this one) 
   40285                 : ** will roll it back.
   40286                 : **
   40287                 : ** If the pager has not already entered the ERROR state, but an IO or
   40288                 : ** malloc error occurs during a rollback, then this will itself cause 
   40289                 : ** the pager to enter the ERROR state. Which will be cleared by the
   40290                 : ** call to pager_unlock(), as described above.
   40291                 : */
   40292          104180 : static void pagerUnlockAndRollback(Pager *pPager){
   40293          104180 :   if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
   40294           99884 :     assert( assert_pager_state(pPager) );
   40295           99884 :     if( pPager->eState>=PAGER_WRITER_LOCKED ){
   40296               0 :       sqlite3BeginBenignMalloc();
   40297               0 :       sqlite3PagerRollback(pPager);
   40298               0 :       sqlite3EndBenignMalloc();
   40299           99884 :     }else if( !pPager->exclusiveMode ){
   40300           55844 :       assert( pPager->eState==PAGER_READER );
   40301           55844 :       pager_end_transaction(pPager, 0);
   40302                 :     }
   40303                 :   }
   40304          104180 :   pager_unlock(pPager);
   40305          104180 : }
   40306                 : 
   40307                 : /*
   40308                 : ** Parameter aData must point to a buffer of pPager->pageSize bytes
   40309                 : ** of data. Compute and return a checksum based ont the contents of the 
   40310                 : ** page of data and the current value of pPager->cksumInit.
   40311                 : **
   40312                 : ** This is not a real checksum. It is really just the sum of the 
   40313                 : ** random initial value (pPager->cksumInit) and every 200th byte
   40314                 : ** of the page data, starting with byte offset (pPager->pageSize%200).
   40315                 : ** Each byte is interpreted as an 8-bit unsigned integer.
   40316                 : **
   40317                 : ** Changing the formula used to compute this checksum results in an
   40318                 : ** incompatible journal file format.
   40319                 : **
   40320                 : ** If journal corruption occurs due to a power failure, the most likely 
   40321                 : ** scenario is that one end or the other of the record will be changed. 
   40322                 : ** It is much less likely that the two ends of the journal record will be
   40323                 : ** correct and the middle be corrupt.  Thus, this "checksum" scheme,
   40324                 : ** though fast and simple, catches the mostly likely kind of corruption.
   40325                 : */
   40326           21294 : static u32 pager_cksum(Pager *pPager, const u8 *aData){
   40327           21294 :   u32 cksum = pPager->cksumInit;         /* Checksum value to return */
   40328           21294 :   int i = pPager->pageSize-200;          /* Loop counter */
   40329         3438821 :   while( i>0 ){
   40330         3396233 :     cksum += aData[i];
   40331         3396233 :     i -= 200;
   40332                 :   }
   40333           21294 :   return cksum;
   40334                 : }
   40335                 : 
   40336                 : /*
   40337                 : ** Report the current page size and number of reserved bytes back
   40338                 : ** to the codec.
   40339                 : */
   40340                 : #ifdef SQLITE_HAS_CODEC
   40341                 : static void pagerReportSize(Pager *pPager){
   40342                 :   if( pPager->xCodecSizeChng ){
   40343                 :     pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
   40344                 :                            (int)pPager->nReserve);
   40345                 :   }
   40346                 : }
   40347                 : #else
   40348                 : # define pagerReportSize(X)     /* No-op if we do not support a codec */
   40349                 : #endif
   40350                 : 
   40351                 : /*
   40352                 : ** Read a single page from either the journal file (if isMainJrnl==1) or
   40353                 : ** from the sub-journal (if isMainJrnl==0) and playback that page.
   40354                 : ** The page begins at offset *pOffset into the file. The *pOffset
   40355                 : ** value is increased to the start of the next page in the journal.
   40356                 : **
   40357                 : ** The main rollback journal uses checksums - the statement journal does 
   40358                 : ** not.
   40359                 : **
   40360                 : ** If the page number of the page record read from the (sub-)journal file
   40361                 : ** is greater than the current value of Pager.dbSize, then playback is
   40362                 : ** skipped and SQLITE_OK is returned.
   40363                 : **
   40364                 : ** If pDone is not NULL, then it is a record of pages that have already
   40365                 : ** been played back.  If the page at *pOffset has already been played back
   40366                 : ** (if the corresponding pDone bit is set) then skip the playback.
   40367                 : ** Make sure the pDone bit corresponding to the *pOffset page is set
   40368                 : ** prior to returning.
   40369                 : **
   40370                 : ** If the page record is successfully read from the (sub-)journal file
   40371                 : ** and played back, then SQLITE_OK is returned. If an IO error occurs
   40372                 : ** while reading the record from the (sub-)journal file or while writing
   40373                 : ** to the database file, then the IO error code is returned. If data
   40374                 : ** is successfully read from the (sub-)journal file but appears to be
   40375                 : ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
   40376                 : ** two circumstances:
   40377                 : ** 
   40378                 : **   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
   40379                 : **   * If the record is being rolled back from the main journal file
   40380                 : **     and the checksum field does not match the record content.
   40381                 : **
   40382                 : ** Neither of these two scenarios are possible during a savepoint rollback.
   40383                 : **
   40384                 : ** If this is a savepoint rollback, then memory may have to be dynamically
   40385                 : ** allocated by this function. If this is the case and an allocation fails,
   40386                 : ** SQLITE_NOMEM is returned.
   40387                 : */
   40388              31 : static int pager_playback_one_page(
   40389                 :   Pager *pPager,                /* The pager being played back */
   40390                 :   i64 *pOffset,                 /* Offset of record to playback */
   40391                 :   Bitvec *pDone,                /* Bitvec of pages already played back */
   40392                 :   int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
   40393                 :   int isSavepnt                 /* True for a savepoint rollback */
   40394                 : ){
   40395                 :   int rc;
   40396                 :   PgHdr *pPg;                   /* An existing page in the cache */
   40397                 :   Pgno pgno;                    /* The page number of a page in journal */
   40398                 :   u32 cksum;                    /* Checksum used for sanity checking */
   40399                 :   char *aData;                  /* Temporary storage for the page */
   40400                 :   sqlite3_file *jfd;            /* The file descriptor for the journal file */
   40401                 :   int isSynced;                 /* True if journal page is synced */
   40402                 : 
   40403              31 :   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
   40404              31 :   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
   40405              31 :   assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
   40406              31 :   assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
   40407                 : 
   40408              31 :   aData = pPager->pTmpSpace;
   40409              31 :   assert( aData );         /* Temp storage must have already been allocated */
   40410              31 :   assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
   40411                 : 
   40412                 :   /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction 
   40413                 :   ** or savepoint rollback done at the request of the caller) or this is
   40414                 :   ** a hot-journal rollback. If it is a hot-journal rollback, the pager
   40415                 :   ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
   40416                 :   ** only reads from the main journal, not the sub-journal.
   40417                 :   */
   40418              31 :   assert( pPager->eState>=PAGER_WRITER_CACHEMOD
   40419                 :        || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
   40420                 :   );
   40421              31 :   assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
   40422                 : 
   40423                 :   /* Read the page number and page data from the journal or sub-journal
   40424                 :   ** file. Return an error code to the caller if an IO error occurs.
   40425                 :   */
   40426              31 :   jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
   40427              31 :   rc = read32bits(jfd, *pOffset, &pgno);
   40428              31 :   if( rc!=SQLITE_OK ) return rc;
   40429              31 :   rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
   40430              31 :   if( rc!=SQLITE_OK ) return rc;
   40431              31 :   *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
   40432                 : 
   40433                 :   /* Sanity checking on the page.  This is more important that I originally
   40434                 :   ** thought.  If a power failure occurs while the journal is being written,
   40435                 :   ** it could cause invalid data to be written into the journal.  We need to
   40436                 :   ** detect this invalid data (with high probability) and ignore it.
   40437                 :   */
   40438              31 :   if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
   40439               0 :     assert( !isSavepnt );
   40440               0 :     return SQLITE_DONE;
   40441                 :   }
   40442              31 :   if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
   40443               0 :     return SQLITE_OK;
   40444                 :   }
   40445              31 :   if( isMainJrnl ){
   40446              19 :     rc = read32bits(jfd, (*pOffset)-4, &cksum);
   40447              19 :     if( rc ) return rc;
   40448              19 :     if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
   40449               0 :       return SQLITE_DONE;
   40450                 :     }
   40451                 :   }
   40452                 : 
   40453                 :   /* If this page has already been played by before during the current
   40454                 :   ** rollback, then don't bother to play it back again.
   40455                 :   */
   40456              31 :   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
   40457               0 :     return rc;
   40458                 :   }
   40459                 : 
   40460                 :   /* When playing back page 1, restore the nReserve setting
   40461                 :   */
   40462              31 :   if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
   40463               0 :     pPager->nReserve = ((u8*)aData)[20];
   40464                 :     pagerReportSize(pPager);
   40465                 :   }
   40466                 : 
   40467                 :   /* If the pager is in CACHEMOD state, then there must be a copy of this
   40468                 :   ** page in the pager cache. In this case just update the pager cache,
   40469                 :   ** not the database file. The page is left marked dirty in this case.
   40470                 :   **
   40471                 :   ** An exception to the above rule: If the database is in no-sync mode
   40472                 :   ** and a page is moved during an incremental vacuum then the page may
   40473                 :   ** not be in the pager cache. Later: if a malloc() or IO error occurs
   40474                 :   ** during a Movepage() call, then the page may not be in the cache
   40475                 :   ** either. So the condition described in the above paragraph is not
   40476                 :   ** assert()able.
   40477                 :   **
   40478                 :   ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
   40479                 :   ** pager cache if it exists and the main file. The page is then marked 
   40480                 :   ** not dirty. Since this code is only executed in PAGER_OPEN state for
   40481                 :   ** a hot-journal rollback, it is guaranteed that the page-cache is empty
   40482                 :   ** if the pager is in OPEN state.
   40483                 :   **
   40484                 :   ** Ticket #1171:  The statement journal might contain page content that is
   40485                 :   ** different from the page content at the start of the transaction.
   40486                 :   ** This occurs when a page is changed prior to the start of a statement
   40487                 :   ** then changed again within the statement.  When rolling back such a
   40488                 :   ** statement we must not write to the original database unless we know
   40489                 :   ** for certain that original page contents are synced into the main rollback
   40490                 :   ** journal.  Otherwise, a power loss might leave modified data in the
   40491                 :   ** database file without an entry in the rollback journal that can
   40492                 :   ** restore the database to its original form.  Two conditions must be
   40493                 :   ** met before writing to the database files. (1) the database must be
   40494                 :   ** locked.  (2) we know that the original page content is fully synced
   40495                 :   ** in the main journal either because the page is not in cache or else
   40496                 :   ** the page is marked as needSync==0.
   40497                 :   **
   40498                 :   ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
   40499                 :   ** is possible to fail a statement on a database that does not yet exist.
   40500                 :   ** Do not attempt to write if database file has never been opened.
   40501                 :   */
   40502              31 :   if( pagerUseWal(pPager) ){
   40503               0 :     pPg = 0;
   40504                 :   }else{
   40505              31 :     pPg = pager_lookup(pPager, pgno);
   40506                 :   }
   40507              31 :   assert( pPg || !MEMDB );
   40508              31 :   assert( pPager->eState!=PAGER_OPEN || pPg==0 );
   40509                 :   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
   40510                 :            PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
   40511                 :            (isMainJrnl?"main-journal":"sub-journal")
   40512                 :   ));
   40513              31 :   if( isMainJrnl ){
   40514              19 :     isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
   40515                 :   }else{
   40516              12 :     isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
   40517                 :   }
   40518              31 :   if( isOpen(pPager->fd)
   40519              31 :    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
   40520               0 :    && isSynced
   40521               0 :   ){
   40522               0 :     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
   40523                 :     testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
   40524               0 :     assert( !pagerUseWal(pPager) );
   40525               0 :     rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
   40526               0 :     if( pgno>pPager->dbFileSize ){
   40527               0 :       pPager->dbFileSize = pgno;
   40528                 :     }
   40529               0 :     if( pPager->pBackup ){
   40530                 :       CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
   40531               0 :       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
   40532               0 :       CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
   40533                 :     }
   40534              31 :   }else if( !isMainJrnl && pPg==0 ){
   40535                 :     /* If this is a rollback of a savepoint and data was not written to
   40536                 :     ** the database and the page is not in-memory, there is a potential
   40537                 :     ** problem. When the page is next fetched by the b-tree layer, it 
   40538                 :     ** will be read from the database file, which may or may not be 
   40539                 :     ** current. 
   40540                 :     **
   40541                 :     ** There are a couple of different ways this can happen. All are quite
   40542                 :     ** obscure. When running in synchronous mode, this can only happen 
   40543                 :     ** if the page is on the free-list at the start of the transaction, then
   40544                 :     ** populated, then moved using sqlite3PagerMovepage().
   40545                 :     **
   40546                 :     ** The solution is to add an in-memory page to the cache containing
   40547                 :     ** the data just read from the sub-journal. Mark the page as dirty 
   40548                 :     ** and if the pager requires a journal-sync, then mark the page as 
   40549                 :     ** requiring a journal-sync before it is written.
   40550                 :     */
   40551               0 :     assert( isSavepnt );
   40552               0 :     assert( pPager->doNotSpill==0 );
   40553               0 :     pPager->doNotSpill++;
   40554               0 :     rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
   40555               0 :     assert( pPager->doNotSpill==1 );
   40556               0 :     pPager->doNotSpill--;
   40557               0 :     if( rc!=SQLITE_OK ) return rc;
   40558               0 :     pPg->flags &= ~PGHDR_NEED_READ;
   40559               0 :     sqlite3PcacheMakeDirty(pPg);
   40560                 :   }
   40561              31 :   if( pPg ){
   40562                 :     /* No page should ever be explicitly rolled back that is in use, except
   40563                 :     ** for page 1 which is held in use in order to keep the lock on the
   40564                 :     ** database active. However such a page may be rolled back as a result
   40565                 :     ** of an internal error resulting in an automatic call to
   40566                 :     ** sqlite3PagerRollback().
   40567                 :     */
   40568                 :     void *pData;
   40569              31 :     pData = pPg->pData;
   40570              31 :     memcpy(pData, (u8*)aData, pPager->pageSize);
   40571              31 :     pPager->xReiniter(pPg);
   40572              31 :     if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
   40573                 :       /* If the contents of this page were just restored from the main 
   40574                 :       ** journal file, then its content must be as they were when the 
   40575                 :       ** transaction was first opened. In this case we can mark the page
   40576                 :       ** as clean, since there will be no need to write it out to the
   40577                 :       ** database.
   40578                 :       **
   40579                 :       ** There is one exception to this rule. If the page is being rolled
   40580                 :       ** back as part of a savepoint (or statement) rollback from an 
   40581                 :       ** unsynced portion of the main journal file, then it is not safe
   40582                 :       ** to mark the page as clean. This is because marking the page as
   40583                 :       ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
   40584                 :       ** already in the journal file (recorded in Pager.pInJournal) and
   40585                 :       ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
   40586                 :       ** again within this transaction, it will be marked as dirty but
   40587                 :       ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
   40588                 :       ** be written out into the database file before its journal file
   40589                 :       ** segment is synced. If a crash occurs during or following this,
   40590                 :       ** database corruption may ensue.
   40591                 :       */
   40592              17 :       assert( !pagerUseWal(pPager) );
   40593              17 :       sqlite3PcacheMakeClean(pPg);
   40594                 :     }
   40595                 :     pager_set_pagehash(pPg);
   40596                 : 
   40597                 :     /* If this was page 1, then restore the value of Pager.dbFileVers.
   40598                 :     ** Do this before any decoding. */
   40599              31 :     if( pgno==1 ){
   40600               2 :       memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
   40601                 :     }
   40602                 : 
   40603                 :     /* Decode the page just read from disk */
   40604                 :     CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
   40605              31 :     sqlite3PcacheRelease(pPg);
   40606                 :   }
   40607              31 :   return rc;
   40608                 : }
   40609                 : 
   40610                 : /*
   40611                 : ** Parameter zMaster is the name of a master journal file. A single journal
   40612                 : ** file that referred to the master journal file has just been rolled back.
   40613                 : ** This routine checks if it is possible to delete the master journal file,
   40614                 : ** and does so if it is.
   40615                 : **
   40616                 : ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
   40617                 : ** available for use within this function.
   40618                 : **
   40619                 : ** When a master journal file is created, it is populated with the names 
   40620                 : ** of all of its child journals, one after another, formatted as utf-8 
   40621                 : ** encoded text. The end of each child journal file is marked with a 
   40622                 : ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
   40623                 : ** file for a transaction involving two databases might be:
   40624                 : **
   40625                 : **   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
   40626                 : **
   40627                 : ** A master journal file may only be deleted once all of its child 
   40628                 : ** journals have been rolled back.
   40629                 : **
   40630                 : ** This function reads the contents of the master-journal file into 
   40631                 : ** memory and loops through each of the child journal names. For
   40632                 : ** each child journal, it checks if:
   40633                 : **
   40634                 : **   * if the child journal exists, and if so
   40635                 : **   * if the child journal contains a reference to master journal 
   40636                 : **     file zMaster
   40637                 : **
   40638                 : ** If a child journal can be found that matches both of the criteria
   40639                 : ** above, this function returns without doing anything. Otherwise, if
   40640                 : ** no such child journal can be found, file zMaster is deleted from
   40641                 : ** the file-system using sqlite3OsDelete().
   40642                 : **
   40643                 : ** If an IO error within this function, an error code is returned. This
   40644                 : ** function allocates memory by calling sqlite3Malloc(). If an allocation
   40645                 : ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors 
   40646                 : ** occur, SQLITE_OK is returned.
   40647                 : **
   40648                 : ** TODO: This function allocates a single block of memory to load
   40649                 : ** the entire contents of the master journal file. This could be
   40650                 : ** a couple of kilobytes or so - potentially larger than the page 
   40651                 : ** size.
   40652                 : */
   40653               0 : static int pager_delmaster(Pager *pPager, const char *zMaster){
   40654               0 :   sqlite3_vfs *pVfs = pPager->pVfs;
   40655                 :   int rc;                   /* Return code */
   40656                 :   sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
   40657                 :   sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
   40658               0 :   char *zMasterJournal = 0; /* Contents of master journal file */
   40659                 :   i64 nMasterJournal;       /* Size of master journal file */
   40660                 :   char *zJournal;           /* Pointer to one journal within MJ file */
   40661                 :   char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
   40662                 :   int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
   40663                 : 
   40664                 :   /* Allocate space for both the pJournal and pMaster file descriptors.
   40665                 :   ** If successful, open the master journal file for reading.
   40666                 :   */
   40667               0 :   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
   40668               0 :   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
   40669               0 :   if( !pMaster ){
   40670               0 :     rc = SQLITE_NOMEM;
   40671                 :   }else{
   40672               0 :     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
   40673               0 :     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
   40674                 :   }
   40675               0 :   if( rc!=SQLITE_OK ) goto delmaster_out;
   40676                 : 
   40677                 :   /* Load the entire master journal file into space obtained from
   40678                 :   ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
   40679                 :   ** sufficient space (in zMasterPtr) to hold the names of master
   40680                 :   ** journal files extracted from regular rollback-journals.
   40681                 :   */
   40682               0 :   rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
   40683               0 :   if( rc!=SQLITE_OK ) goto delmaster_out;
   40684               0 :   nMasterPtr = pVfs->mxPathname+1;
   40685               0 :   zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
   40686               0 :   if( !zMasterJournal ){
   40687               0 :     rc = SQLITE_NOMEM;
   40688               0 :     goto delmaster_out;
   40689                 :   }
   40690               0 :   zMasterPtr = &zMasterJournal[nMasterJournal+1];
   40691               0 :   rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
   40692               0 :   if( rc!=SQLITE_OK ) goto delmaster_out;
   40693               0 :   zMasterJournal[nMasterJournal] = 0;
   40694                 : 
   40695               0 :   zJournal = zMasterJournal;
   40696               0 :   while( (zJournal-zMasterJournal)<nMasterJournal ){
   40697                 :     int exists;
   40698               0 :     rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
   40699               0 :     if( rc!=SQLITE_OK ){
   40700               0 :       goto delmaster_out;
   40701                 :     }
   40702               0 :     if( exists ){
   40703                 :       /* One of the journals pointed to by the master journal exists.
   40704                 :       ** Open it and check if it points at the master journal. If
   40705                 :       ** so, return without deleting the master journal file.
   40706                 :       */
   40707                 :       int c;
   40708               0 :       int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
   40709               0 :       rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
   40710               0 :       if( rc!=SQLITE_OK ){
   40711               0 :         goto delmaster_out;
   40712                 :       }
   40713                 : 
   40714               0 :       rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
   40715               0 :       sqlite3OsClose(pJournal);
   40716               0 :       if( rc!=SQLITE_OK ){
   40717               0 :         goto delmaster_out;
   40718                 :       }
   40719                 : 
   40720               0 :       c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
   40721               0 :       if( c ){
   40722                 :         /* We have a match. Do not delete the master journal file. */
   40723               0 :         goto delmaster_out;
   40724                 :       }
   40725                 :     }
   40726               0 :     zJournal += (sqlite3Strlen30(zJournal)+1);
   40727                 :   }
   40728                 :  
   40729               0 :   sqlite3OsClose(pMaster);
   40730               0 :   rc = sqlite3OsDelete(pVfs, zMaster, 0);
   40731                 : 
   40732                 : delmaster_out:
   40733               0 :   sqlite3_free(zMasterJournal);
   40734               0 :   if( pMaster ){
   40735               0 :     sqlite3OsClose(pMaster);
   40736               0 :     assert( !isOpen(pJournal) );
   40737               0 :     sqlite3_free(pMaster);
   40738                 :   }
   40739               0 :   return rc;
   40740                 : }
   40741                 : 
   40742                 : 
   40743                 : /*
   40744                 : ** This function is used to change the actual size of the database 
   40745                 : ** file in the file-system. This only happens when committing a transaction,
   40746                 : ** or rolling back a transaction (including rolling back a hot-journal).
   40747                 : **
   40748                 : ** If the main database file is not open, or the pager is not in either
   40749                 : ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size 
   40750                 : ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes). 
   40751                 : ** If the file on disk is currently larger than nPage pages, then use the VFS
   40752                 : ** xTruncate() method to truncate it.
   40753                 : **
   40754                 : ** Or, it might might be the case that the file on disk is smaller than 
   40755                 : ** nPage pages. Some operating system implementations can get confused if 
   40756                 : ** you try to truncate a file to some size that is larger than it 
   40757                 : ** currently is, so detect this case and write a single zero byte to 
   40758                 : ** the end of the new file instead.
   40759                 : **
   40760                 : ** If successful, return SQLITE_OK. If an IO error occurs while modifying
   40761                 : ** the database file, return the error code to the caller.
   40762                 : */
   40763              11 : static int pager_truncate(Pager *pPager, Pgno nPage){
   40764              11 :   int rc = SQLITE_OK;
   40765              11 :   assert( pPager->eState!=PAGER_ERROR );
   40766              11 :   assert( pPager->eState!=PAGER_READER );
   40767                 :   
   40768              11 :   if( isOpen(pPager->fd) 
   40769               8 :    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN) 
   40770                 :   ){
   40771                 :     i64 currentSize, newSize;
   40772               2 :     int szPage = pPager->pageSize;
   40773               2 :     assert( pPager->eLock==EXCLUSIVE_LOCK );
   40774                 :     /* TODO: Is it safe to use Pager.dbFileSize here? */
   40775               2 :     rc = sqlite3OsFileSize(pPager->fd, &currentSize);
   40776               2 :     newSize = szPage*(i64)nPage;
   40777               2 :     if( rc==SQLITE_OK && currentSize!=newSize ){
   40778               2 :       if( currentSize>newSize ){
   40779               2 :         rc = sqlite3OsTruncate(pPager->fd, newSize);
   40780               0 :       }else if( (currentSize+szPage)<=newSize ){
   40781               0 :         char *pTmp = pPager->pTmpSpace;
   40782               0 :         memset(pTmp, 0, szPage);
   40783                 :         testcase( (newSize-szPage) == currentSize );
   40784                 :         testcase( (newSize-szPage) >  currentSize );
   40785               0 :         rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
   40786                 :       }
   40787               2 :       if( rc==SQLITE_OK ){
   40788               2 :         pPager->dbFileSize = nPage;
   40789                 :       }
   40790                 :     }
   40791                 :   }
   40792              11 :   return rc;
   40793                 : }
   40794                 : 
   40795                 : /*
   40796                 : ** Set the value of the Pager.sectorSize variable for the given
   40797                 : ** pager based on the value returned by the xSectorSize method
   40798                 : ** of the open database file. The sector size will be used used 
   40799                 : ** to determine the size and alignment of journal header and 
   40800                 : ** master journal pointers within created journal files.
   40801                 : **
   40802                 : ** For temporary files the effective sector size is always 512 bytes.
   40803                 : **
   40804                 : ** Otherwise, for non-temporary files, the effective sector size is
   40805                 : ** the value returned by the xSectorSize() method rounded up to 32 if
   40806                 : ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
   40807                 : ** is greater than MAX_SECTOR_SIZE.
   40808                 : **
   40809                 : ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
   40810                 : ** the effective sector size to its minimum value (512).  The purpose of
   40811                 : ** pPager->sectorSize is to define the "blast radius" of bytes that
   40812                 : ** might change if a crash occurs while writing to a single byte in
   40813                 : ** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
   40814                 : ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
   40815                 : ** size.  For backwards compatibility of the rollback journal file format,
   40816                 : ** we cannot reduce the effective sector size below 512.
   40817                 : */
   40818           22542 : static void setSectorSize(Pager *pPager){
   40819           22542 :   assert( isOpen(pPager->fd) || pPager->tempFile );
   40820                 : 
   40821           22542 :   if( pPager->tempFile
   40822            6074 :    || (sqlite3OsDeviceCharacteristics(pPager->fd) & 
   40823                 :               SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
   40824                 :   ){
   40825                 :     /* Sector size doesn't matter for temporary files. Also, the file
   40826                 :     ** may not have been opened yet, in which case the OsSectorSize()
   40827                 :     ** call will segfault. */
   40828           22542 :     pPager->sectorSize = 512;
   40829                 :   }else{
   40830               0 :     pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
   40831               0 :     if( pPager->sectorSize<32 ){
   40832               0 :       pPager->sectorSize = 512;
   40833                 :     }
   40834               0 :     if( pPager->sectorSize>MAX_SECTOR_SIZE ){
   40835                 :       assert( MAX_SECTOR_SIZE>=512 );
   40836               0 :       pPager->sectorSize = MAX_SECTOR_SIZE;
   40837                 :     }
   40838                 :   }
   40839           22542 : }
   40840                 : 
   40841                 : /*
   40842                 : ** Playback the journal and thus restore the database file to
   40843                 : ** the state it was in before we started making changes.  
   40844                 : **
   40845                 : ** The journal file format is as follows: 
   40846                 : **
   40847                 : **  (1)  8 byte prefix.  A copy of aJournalMagic[].
   40848                 : **  (2)  4 byte big-endian integer which is the number of valid page records
   40849                 : **       in the journal.  If this value is 0xffffffff, then compute the
   40850                 : **       number of page records from the journal size.
   40851                 : **  (3)  4 byte big-endian integer which is the initial value for the 
   40852                 : **       sanity checksum.
   40853                 : **  (4)  4 byte integer which is the number of pages to truncate the
   40854                 : **       database to during a rollback.
   40855                 : **  (5)  4 byte big-endian integer which is the sector size.  The header
   40856                 : **       is this many bytes in size.
   40857                 : **  (6)  4 byte big-endian integer which is the page size.
   40858                 : **  (7)  zero padding out to the next sector size.
   40859                 : **  (8)  Zero or more pages instances, each as follows:
   40860                 : **        +  4 byte page number.
   40861                 : **        +  pPager->pageSize bytes of data.
   40862                 : **        +  4 byte checksum
   40863                 : **
   40864                 : ** When we speak of the journal header, we mean the first 7 items above.
   40865                 : ** Each entry in the journal is an instance of the 8th item.
   40866                 : **
   40867                 : ** Call the value from the second bullet "nRec".  nRec is the number of
   40868                 : ** valid page entries in the journal.  In most cases, you can compute the
   40869                 : ** value of nRec from the size of the journal file.  But if a power
   40870                 : ** failure occurred while the journal was being written, it could be the
   40871                 : ** case that the size of the journal file had already been increased but
   40872                 : ** the extra entries had not yet made it safely to disk.  In such a case,
   40873                 : ** the value of nRec computed from the file size would be too large.  For
   40874                 : ** that reason, we always use the nRec value in the header.
   40875                 : **
   40876                 : ** If the nRec value is 0xffffffff it means that nRec should be computed
   40877                 : ** from the file size.  This value is used when the user selects the
   40878                 : ** no-sync option for the journal.  A power failure could lead to corruption
   40879                 : ** in this case.  But for things like temporary table (which will be
   40880                 : ** deleted when the power is restored) we don't care.  
   40881                 : **
   40882                 : ** If the file opened as the journal file is not a well-formed
   40883                 : ** journal file then all pages up to the first corrupted page are rolled
   40884                 : ** back (or no pages if the journal header is corrupted). The journal file
   40885                 : ** is then deleted and SQLITE_OK returned, just as if no corruption had
   40886                 : ** been encountered.
   40887                 : **
   40888                 : ** If an I/O or malloc() error occurs, the journal-file is not deleted
   40889                 : ** and an error code is returned.
   40890                 : **
   40891                 : ** The isHot parameter indicates that we are trying to rollback a journal
   40892                 : ** that might be a hot journal.  Or, it could be that the journal is 
   40893                 : ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
   40894                 : ** If the journal really is hot, reset the pager cache prior rolling
   40895                 : ** back any content.  If the journal is merely persistent, no reset is
   40896                 : ** needed.
   40897                 : */
   40898               9 : static int pager_playback(Pager *pPager, int isHot){
   40899               9 :   sqlite3_vfs *pVfs = pPager->pVfs;
   40900                 :   i64 szJ;                 /* Size of the journal file in bytes */
   40901                 :   u32 nRec;                /* Number of Records in the journal */
   40902                 :   u32 u;                   /* Unsigned loop counter */
   40903               9 :   Pgno mxPg = 0;           /* Size of the original file in pages */
   40904                 :   int rc;                  /* Result code of a subroutine */
   40905               9 :   int res = 1;             /* Value returned by sqlite3OsAccess() */
   40906               9 :   char *zMaster = 0;       /* Name of master journal file if any */
   40907                 :   int needPagerReset;      /* True to reset page prior to first page rollback */
   40908                 : 
   40909                 :   /* Figure out how many records are in the journal.  Abort early if
   40910                 :   ** the journal is empty.
   40911                 :   */
   40912               9 :   assert( isOpen(pPager->jfd) );
   40913               9 :   rc = sqlite3OsFileSize(pPager->jfd, &szJ);
   40914               9 :   if( rc!=SQLITE_OK ){
   40915               0 :     goto end_playback;
   40916                 :   }
   40917                 : 
   40918                 :   /* Read the master journal name from the journal, if it is present.
   40919                 :   ** If a master journal file name is specified, but the file is not
   40920                 :   ** present on disk, then the journal is not hot and does not need to be
   40921                 :   ** played back.
   40922                 :   **
   40923                 :   ** TODO: Technically the following is an error because it assumes that
   40924                 :   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
   40925                 :   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
   40926                 :   **  mxPathname is 512, which is the same as the minimum allowable value
   40927                 :   ** for pageSize.
   40928                 :   */
   40929               9 :   zMaster = pPager->pTmpSpace;
   40930               9 :   rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
   40931               9 :   if( rc==SQLITE_OK && zMaster[0] ){
   40932               0 :     rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
   40933                 :   }
   40934               9 :   zMaster = 0;
   40935               9 :   if( rc!=SQLITE_OK || !res ){
   40936                 :     goto end_playback;
   40937                 :   }
   40938               9 :   pPager->journalOff = 0;
   40939               9 :   needPagerReset = isHot;
   40940                 : 
   40941                 :   /* This loop terminates either when a readJournalHdr() or 
   40942                 :   ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 
   40943                 :   ** occurs. 
   40944                 :   */
   40945                 :   while( 1 ){
   40946                 :     /* Read the next journal header from the journal file.  If there are
   40947                 :     ** not enough bytes left in the journal file for a complete header, or
   40948                 :     ** it is corrupted, then a process must have failed while writing it.
   40949                 :     ** This indicates nothing more needs to be rolled back.
   40950                 :     */
   40951              18 :     rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
   40952              18 :     if( rc!=SQLITE_OK ){ 
   40953               9 :       if( rc==SQLITE_DONE ){
   40954               9 :         rc = SQLITE_OK;
   40955                 :       }
   40956               9 :       goto end_playback;
   40957                 :     }
   40958                 : 
   40959                 :     /* If nRec is 0xffffffff, then this journal was created by a process
   40960                 :     ** working in no-sync mode. This means that the rest of the journal
   40961                 :     ** file consists of pages, there are no more journal headers. Compute
   40962                 :     ** the value of nRec based on this assumption.
   40963                 :     */
   40964               9 :     if( nRec==0xffffffff ){
   40965               3 :       assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
   40966               3 :       nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
   40967                 :     }
   40968                 : 
   40969                 :     /* If nRec is 0 and this rollback is of a transaction created by this
   40970                 :     ** process and if this is the final header in the journal, then it means
   40971                 :     ** that this part of the journal was being filled but has not yet been
   40972                 :     ** synced to disk.  Compute the number of pages based on the remaining
   40973                 :     ** size of the file.
   40974                 :     **
   40975                 :     ** The third term of the test was added to fix ticket #2565.
   40976                 :     ** When rolling back a hot journal, nRec==0 always means that the next
   40977                 :     ** chunk of the journal contains zero pages to be rolled back.  But
   40978                 :     ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
   40979                 :     ** the journal, it means that the journal might contain additional
   40980                 :     ** pages that need to be rolled back and that the number of pages 
   40981                 :     ** should be computed based on the journal file size.
   40982                 :     */
   40983              18 :     if( nRec==0 && !isHot &&
   40984               9 :         pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
   40985               9 :       nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
   40986                 :     }
   40987                 : 
   40988                 :     /* If this is the first header read from the journal, truncate the
   40989                 :     ** database file back to its original size.
   40990                 :     */
   40991               9 :     if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
   40992               9 :       rc = pager_truncate(pPager, mxPg);
   40993               9 :       if( rc!=SQLITE_OK ){
   40994               0 :         goto end_playback;
   40995                 :       }
   40996               9 :       pPager->dbSize = mxPg;
   40997                 :     }
   40998                 : 
   40999                 :     /* Copy original pages out of the journal and back into the 
   41000                 :     ** database file and/or page cache.
   41001                 :     */
   41002              26 :     for(u=0; u<nRec; u++){
   41003              17 :       if( needPagerReset ){
   41004               0 :         pager_reset(pPager);
   41005               0 :         needPagerReset = 0;
   41006                 :       }
   41007              17 :       rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
   41008              17 :       if( rc!=SQLITE_OK ){
   41009               0 :         if( rc==SQLITE_DONE ){
   41010               0 :           pPager->journalOff = szJ;
   41011               0 :           break;
   41012               0 :         }else if( rc==SQLITE_IOERR_SHORT_READ ){
   41013                 :           /* If the journal has been truncated, simply stop reading and
   41014                 :           ** processing the journal. This might happen if the journal was
   41015                 :           ** not completely written and synced prior to a crash.  In that
   41016                 :           ** case, the database should have never been written in the
   41017                 :           ** first place so it is OK to simply abandon the rollback. */
   41018               0 :           rc = SQLITE_OK;
   41019               0 :           goto end_playback;
   41020                 :         }else{
   41021                 :           /* If we are unable to rollback, quit and return the error
   41022                 :           ** code.  This will cause the pager to enter the error state
   41023                 :           ** so that no further harm will be done.  Perhaps the next
   41024                 :           ** process to come along will be able to rollback the database.
   41025                 :           */
   41026               0 :           goto end_playback;
   41027                 :         }
   41028                 :       }
   41029                 :     }
   41030               9 :   }
   41031                 :   /*NOTREACHED*/
   41032                 :   assert( 0 );
   41033                 : 
   41034                 : end_playback:
   41035                 :   /* Following a rollback, the database file should be back in its original
   41036                 :   ** state prior to the start of the transaction, so invoke the
   41037                 :   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
   41038                 :   ** assertion that the transaction counter was modified.
   41039                 :   */
   41040                 : #ifdef SQLITE_DEBUG
   41041               9 :   if( pPager->fd->pMethods ){
   41042               6 :     sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
   41043                 :   }
   41044                 : #endif
   41045                 : 
   41046                 :   /* If this playback is happening automatically as a result of an IO or 
   41047                 :   ** malloc error that occurred after the change-counter was updated but 
   41048                 :   ** before the transaction was committed, then the change-counter 
   41049                 :   ** modification may just have been reverted. If this happens in exclusive 
   41050                 :   ** mode, then subsequent transactions performed by the connection will not
   41051                 :   ** update the change-counter at all. This may lead to cache inconsistency
   41052                 :   ** problems for other processes at some point in the future. So, just
   41053                 :   ** in case this has happened, clear the changeCountDone flag now.
   41054                 :   */
   41055               9 :   pPager->changeCountDone = pPager->tempFile;
   41056                 : 
   41057               9 :   if( rc==SQLITE_OK ){
   41058               9 :     zMaster = pPager->pTmpSpace;
   41059               9 :     rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
   41060                 :     testcase( rc!=SQLITE_OK );
   41061                 :   }
   41062               9 :   if( rc==SQLITE_OK
   41063               9 :    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
   41064                 :   ){
   41065               0 :     rc = sqlite3PagerSync(pPager);
   41066                 :   }
   41067               9 :   if( rc==SQLITE_OK ){
   41068               9 :     rc = pager_end_transaction(pPager, zMaster[0]!='\0');
   41069                 :     testcase( rc!=SQLITE_OK );
   41070                 :   }
   41071               9 :   if( rc==SQLITE_OK && zMaster[0] && res ){
   41072                 :     /* If there was a master journal and this routine will return success,
   41073                 :     ** see if it is possible to delete the master journal.
   41074                 :     */
   41075               0 :     rc = pager_delmaster(pPager, zMaster);
   41076                 :     testcase( rc!=SQLITE_OK );
   41077                 :   }
   41078                 : 
   41079                 :   /* The Pager.sectorSize variable may have been updated while rolling
   41080                 :   ** back a journal created by a process with a different sector size
   41081                 :   ** value. Reset it to the correct value for this process.
   41082                 :   */
   41083               9 :   setSectorSize(pPager);
   41084               9 :   return rc;
   41085                 : }
   41086                 : 
   41087                 : 
   41088                 : /*
   41089                 : ** Read the content for page pPg out of the database file and into 
   41090                 : ** pPg->pData. A shared lock or greater must be held on the database
   41091                 : ** file before this function is called.
   41092                 : **
   41093                 : ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
   41094                 : ** the value read from the database file.
   41095                 : **
   41096                 : ** If an IO error occurs, then the IO error is returned to the caller.
   41097                 : ** Otherwise, SQLITE_OK is returned.
   41098                 : */
   41099           12392 : static int readDbPage(PgHdr *pPg){
   41100           12392 :   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
   41101           12392 :   Pgno pgno = pPg->pgno;       /* Page number to read */
   41102           12392 :   int rc = SQLITE_OK;          /* Return code */
   41103           12392 :   int isInWal = 0;             /* True if page is in log file */
   41104           12392 :   int pgsz = pPager->pageSize; /* Number of bytes to read */
   41105                 : 
   41106           12392 :   assert( pPager->eState>=PAGER_READER && !MEMDB );
   41107           12392 :   assert( isOpen(pPager->fd) );
   41108                 : 
   41109           12392 :   if( NEVER(!isOpen(pPager->fd)) ){
   41110               0 :     assert( pPager->tempFile );
   41111               0 :     memset(pPg->pData, 0, pPager->pageSize);
   41112               0 :     return SQLITE_OK;
   41113                 :   }
   41114                 : 
   41115           12392 :   if( pagerUseWal(pPager) ){
   41116                 :     /* Try to pull the page from the write-ahead log. */
   41117            1476 :     rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
   41118                 :   }
   41119           12392 :   if( rc==SQLITE_OK && !isInWal ){
   41120           12183 :     i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
   41121           12183 :     rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
   41122           12183 :     if( rc==SQLITE_IOERR_SHORT_READ ){
   41123               4 :       rc = SQLITE_OK;
   41124                 :     }
   41125                 :   }
   41126                 : 
   41127           12392 :   if( pgno==1 ){
   41128            2164 :     if( rc ){
   41129                 :       /* If the read is unsuccessful, set the dbFileVers[] to something
   41130                 :       ** that will never be a valid file version.  dbFileVers[] is a copy
   41131                 :       ** of bytes 24..39 of the database.  Bytes 28..31 should always be
   41132                 :       ** zero or the size of the database in page. Bytes 32..35 and 35..39
   41133                 :       ** should be page numbers which are never 0xffffffff.  So filling
   41134                 :       ** pPager->dbFileVers[] with all 0xff bytes should suffice.
   41135                 :       **
   41136                 :       ** For an encrypted database, the situation is more complex:  bytes
   41137                 :       ** 24..39 of the database are white noise.  But the probability of
   41138                 :       ** white noising equaling 16 bytes of 0xff is vanishingly small so
   41139                 :       ** we should still be ok.
   41140                 :       */
   41141               0 :       memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
   41142                 :     }else{
   41143            2164 :       u8 *dbFileVers = &((u8*)pPg->pData)[24];
   41144            2164 :       memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
   41145                 :     }
   41146                 :   }
   41147                 :   CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
   41148                 : 
   41149                 :   PAGER_INCR(sqlite3_pager_readdb_count);
   41150                 :   PAGER_INCR(pPager->nRead);
   41151                 :   IOTRACE(("PGIN %p %d\n", pPager, pgno));
   41152                 :   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
   41153                 :                PAGERID(pPager), pgno, pager_pagehash(pPg)));
   41154                 : 
   41155           12392 :   return rc;
   41156                 : }
   41157                 : 
   41158                 : /*
   41159                 : ** Update the value of the change-counter at offsets 24 and 92 in
   41160                 : ** the header and the sqlite version number at offset 96.
   41161                 : **
   41162                 : ** This is an unconditional update.  See also the pager_incr_changecounter()
   41163                 : ** routine which only updates the change-counter if the update is actually
   41164                 : ** needed, as determined by the pPager->changeCountDone state variable.
   41165                 : */
   41166           14900 : static void pager_write_changecounter(PgHdr *pPg){
   41167                 :   u32 change_counter;
   41168                 : 
   41169                 :   /* Increment the value just read and write it back to byte 24. */
   41170           14900 :   change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
   41171           14900 :   put32bits(((char*)pPg->pData)+24, change_counter);
   41172                 : 
   41173                 :   /* Also store the SQLite version number in bytes 96..99 and in
   41174                 :   ** bytes 92..95 store the change counter for which the version number
   41175                 :   ** is valid. */
   41176           14900 :   put32bits(((char*)pPg->pData)+92, change_counter);
   41177           14900 :   put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
   41178           14900 : }
   41179                 : 
   41180                 : #ifndef SQLITE_OMIT_WAL
   41181                 : /*
   41182                 : ** This function is invoked once for each page that has already been 
   41183                 : ** written into the log file when a WAL transaction is rolled back.
   41184                 : ** Parameter iPg is the page number of said page. The pCtx argument 
   41185                 : ** is actually a pointer to the Pager structure.
   41186                 : **
   41187                 : ** If page iPg is present in the cache, and has no outstanding references,
   41188                 : ** it is discarded. Otherwise, if there are one or more outstanding
   41189                 : ** references, the page content is reloaded from the database. If the
   41190                 : ** attempt to reload content from the database is required and fails, 
   41191                 : ** return an SQLite error code. Otherwise, SQLITE_OK.
   41192                 : */
   41193              11 : static int pagerUndoCallback(void *pCtx, Pgno iPg){
   41194              11 :   int rc = SQLITE_OK;
   41195              11 :   Pager *pPager = (Pager *)pCtx;
   41196                 :   PgHdr *pPg;
   41197                 : 
   41198              11 :   pPg = sqlite3PagerLookup(pPager, iPg);
   41199              11 :   if( pPg ){
   41200              11 :     if( sqlite3PcachePageRefcount(pPg)==1 ){
   41201              11 :       sqlite3PcacheDrop(pPg);
   41202                 :     }else{
   41203               0 :       rc = readDbPage(pPg);
   41204               0 :       if( rc==SQLITE_OK ){
   41205               0 :         pPager->xReiniter(pPg);
   41206                 :       }
   41207               0 :       sqlite3PagerUnref(pPg);
   41208                 :     }
   41209                 :   }
   41210                 : 
   41211                 :   /* Normally, if a transaction is rolled back, any backup processes are
   41212                 :   ** updated as data is copied out of the rollback journal and into the
   41213                 :   ** database. This is not generally possible with a WAL database, as
   41214                 :   ** rollback involves simply truncating the log file. Therefore, if one
   41215                 :   ** or more frames have already been written to the log (and therefore 
   41216                 :   ** also copied into the backup databases) as part of this transaction,
   41217                 :   ** the backups must be restarted.
   41218                 :   */
   41219              11 :   sqlite3BackupRestart(pPager->pBackup);
   41220                 : 
   41221              11 :   return rc;
   41222                 : }
   41223                 : 
   41224                 : /*
   41225                 : ** This function is called to rollback a transaction on a WAL database.
   41226                 : */
   41227             304 : static int pagerRollbackWal(Pager *pPager){
   41228                 :   int rc;                         /* Return Code */
   41229                 :   PgHdr *pList;                   /* List of dirty pages to revert */
   41230                 : 
   41231                 :   /* For all pages in the cache that are currently dirty or have already
   41232                 :   ** been written (but not committed) to the log file, do one of the 
   41233                 :   ** following:
   41234                 :   **
   41235                 :   **   + Discard the cached page (if refcount==0), or
   41236                 :   **   + Reload page content from the database (if refcount>0).
   41237                 :   */
   41238             304 :   pPager->dbSize = pPager->dbOrigSize;
   41239             304 :   rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
   41240             304 :   pList = sqlite3PcacheDirtyList(pPager->pPCache);
   41241             619 :   while( pList && rc==SQLITE_OK ){
   41242              11 :     PgHdr *pNext = pList->pDirty;
   41243              11 :     rc = pagerUndoCallback((void *)pPager, pList->pgno);
   41244              11 :     pList = pNext;
   41245                 :   }
   41246                 : 
   41247             304 :   return rc;
   41248                 : }
   41249                 : 
   41250                 : /*
   41251                 : ** This function is a wrapper around sqlite3WalFrames(). As well as logging
   41252                 : ** the contents of the list of pages headed by pList (connected by pDirty),
   41253                 : ** this function notifies any active backup processes that the pages have
   41254                 : ** changed. 
   41255                 : **
   41256                 : ** The list of pages passed into this routine is always sorted by page number.
   41257                 : ** Hence, if page 1 appears anywhere on the list, it will be the first page.
   41258                 : */ 
   41259           25040 : static int pagerWalFrames(
   41260                 :   Pager *pPager,                  /* Pager object */
   41261                 :   PgHdr *pList,                   /* List of frames to log */
   41262                 :   Pgno nTruncate,                 /* Database size after this commit */
   41263                 :   int isCommit                    /* True if this is a commit */
   41264                 : ){
   41265                 :   int rc;                         /* Return code */
   41266                 : #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
   41267                 :   PgHdr *p;                       /* For looping over pages */
   41268                 : #endif
   41269                 : 
   41270           25040 :   assert( pPager->pWal );
   41271           25040 :   assert( pList );
   41272                 : #ifdef SQLITE_DEBUG
   41273                 :   /* Verify that the page list is in accending order */
   41274          101689 :   for(p=pList; p && p->pDirty; p=p->pDirty){
   41275           76649 :     assert( p->pgno < p->pDirty->pgno );
   41276                 :   }
   41277                 : #endif
   41278                 : 
   41279           25040 :   if( isCommit ){
   41280                 :     /* If a WAL transaction is being committed, there is no point in writing
   41281                 :     ** any pages with page numbers greater than nTruncate into the WAL file.
   41282                 :     ** They will never be read by any client. So remove them from the pDirty
   41283                 :     ** list here. */
   41284                 :     PgHdr *p;
   41285           25040 :     PgHdr **ppNext = &pList;
   41286          126729 :     for(p=pList; (*ppNext = p); p=p->pDirty){
   41287          101689 :       if( p->pgno<=nTruncate ) ppNext = &p->pDirty;
   41288                 :     }
   41289           25040 :     assert( pList );
   41290                 :   }
   41291                 : 
   41292           25040 :   if( pList->pgno==1 ) pager_write_changecounter(pList);
   41293           25040 :   rc = sqlite3WalFrames(pPager->pWal, 
   41294           25040 :       pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
   41295                 :   );
   41296           25040 :   if( rc==SQLITE_OK && pPager->pBackup ){
   41297                 :     PgHdr *p;
   41298               0 :     for(p=pList; p; p=p->pDirty){
   41299               0 :       sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
   41300                 :     }
   41301                 :   }
   41302                 : 
   41303                 : #ifdef SQLITE_CHECK_PAGES
   41304                 :   pList = sqlite3PcacheDirtyList(pPager->pPCache);
   41305                 :   for(p=pList; p; p=p->pDirty){
   41306                 :     pager_set_pagehash(p);
   41307                 :   }
   41308                 : #endif
   41309                 : 
   41310           25040 :   return rc;
   41311                 : }
   41312                 : 
   41313                 : /*
   41314                 : ** Begin a read transaction on the WAL.
   41315                 : **
   41316                 : ** This routine used to be called "pagerOpenSnapshot()" because it essentially
   41317                 : ** makes a snapshot of the database at the current point in time and preserves
   41318                 : ** that snapshot for use by the reader in spite of concurrently changes by
   41319                 : ** other writers or checkpointers.
   41320                 : */
   41321           40098 : static int pagerBeginReadTransaction(Pager *pPager){
   41322                 :   int rc;                         /* Return code */
   41323           40098 :   int changed = 0;                /* True if cache must be reset */
   41324                 : 
   41325           40098 :   assert( pagerUseWal(pPager) );
   41326           40098 :   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
   41327                 : 
   41328                 :   /* sqlite3WalEndReadTransaction() was not called for the previous
   41329                 :   ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
   41330                 :   ** are in locking_mode=NORMAL and EndRead() was previously called,
   41331                 :   ** the duplicate call is harmless.
   41332                 :   */
   41333           40098 :   sqlite3WalEndReadTransaction(pPager->pWal);
   41334                 : 
   41335           40098 :   rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
   41336           40098 :   if( rc!=SQLITE_OK || changed ){
   41337             541 :     pager_reset(pPager);
   41338                 :   }
   41339                 : 
   41340           40098 :   return rc;
   41341                 : }
   41342                 : #endif
   41343                 : 
   41344                 : /*
   41345                 : ** This function is called as part of the transition from PAGER_OPEN
   41346                 : ** to PAGER_READER state to determine the size of the database file
   41347                 : ** in pages (assuming the page size currently stored in Pager.pageSize).
   41348                 : **
   41349                 : ** If no error occurs, SQLITE_OK is returned and the size of the database
   41350                 : ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
   41351                 : ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
   41352                 : */
   41353           85461 : static int pagerPagecount(Pager *pPager, Pgno *pnPage){
   41354                 :   Pgno nPage;                     /* Value to return via *pnPage */
   41355                 : 
   41356                 :   /* Query the WAL sub-system for the database size. The WalDbsize()
   41357                 :   ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
   41358                 :   ** if the database size is not available. The database size is not
   41359                 :   ** available from the WAL sub-system if the log file is empty or
   41360                 :   ** contains no valid committed transactions.
   41361                 :   */
   41362           85461 :   assert( pPager->eState==PAGER_OPEN );
   41363           85461 :   assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
   41364           85461 :   nPage = sqlite3WalDbsize(pPager->pWal);
   41365                 : 
   41366                 :   /* If the database size was not available from the WAL sub-system,
   41367                 :   ** determine it based on the size of the database file. If the size
   41368                 :   ** of the database file is not an integer multiple of the page-size,
   41369                 :   ** round down to the nearest page. Except, any file larger than 0
   41370                 :   ** bytes in size is considered to contain at least one page.
   41371                 :   */
   41372           85461 :   if( nPage==0 ){
   41373           46465 :     i64 n = 0;                    /* Size of db file in bytes */
   41374           46465 :     assert( isOpen(pPager->fd) || pPager->tempFile );
   41375           46465 :     if( isOpen(pPager->fd) ){
   41376           46465 :       int rc = sqlite3OsFileSize(pPager->fd, &n);
   41377           46465 :       if( rc!=SQLITE_OK ){
   41378               0 :         return rc;
   41379                 :       }
   41380                 :     }
   41381           46465 :     nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
   41382                 :   }
   41383                 : 
   41384                 :   /* If the current number of pages in the file is greater than the
   41385                 :   ** configured maximum pager number, increase the allowed limit so
   41386                 :   ** that the file can be read.
   41387                 :   */
   41388           85461 :   if( nPage>pPager->mxPgno ){
   41389               0 :     pPager->mxPgno = (Pgno)nPage;
   41390                 :   }
   41391                 : 
   41392           85461 :   *pnPage = nPage;
   41393           85461 :   return SQLITE_OK;
   41394                 : }
   41395                 : 
   41396                 : #ifndef SQLITE_OMIT_WAL
   41397                 : /*
   41398                 : ** Check if the *-wal file that corresponds to the database opened by pPager
   41399                 : ** exists if the database is not empy, or verify that the *-wal file does
   41400                 : ** not exist (by deleting it) if the database file is empty.
   41401                 : **
   41402                 : ** If the database is not empty and the *-wal file exists, open the pager
   41403                 : ** in WAL mode.  If the database is empty or if no *-wal file exists and
   41404                 : ** if no error occurs, make sure Pager.journalMode is not set to
   41405                 : ** PAGER_JOURNALMODE_WAL.
   41406                 : **
   41407                 : ** Return SQLITE_OK or an error code.
   41408                 : **
   41409                 : ** The caller must hold a SHARED lock on the database file to call this
   41410                 : ** function. Because an EXCLUSIVE lock on the db file is required to delete 
   41411                 : ** a WAL on a none-empty database, this ensures there is no race condition 
   41412                 : ** between the xAccess() below and an xDelete() being executed by some 
   41413                 : ** other connection.
   41414                 : */
   41415           16151 : static int pagerOpenWalIfPresent(Pager *pPager){
   41416           16151 :   int rc = SQLITE_OK;
   41417           16151 :   assert( pPager->eState==PAGER_OPEN );
   41418           16151 :   assert( pPager->eLock>=SHARED_LOCK || pPager->noReadlock );
   41419                 : 
   41420           16151 :   if( !pPager->tempFile ){
   41421                 :     int isWal;                    /* True if WAL file exists */
   41422                 :     Pgno nPage;                   /* Size of the database file */
   41423                 : 
   41424           16151 :     rc = pagerPagecount(pPager, &nPage);
   41425           16151 :     if( rc ) return rc;
   41426           16151 :     if( nPage==0 ){
   41427            3437 :       rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
   41428            3437 :       isWal = 0;
   41429                 :     }else{
   41430           12714 :       rc = sqlite3OsAccess(
   41431           12714 :           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
   41432                 :       );
   41433                 :     }
   41434           16151 :     if( rc==SQLITE_OK ){
   41435           16151 :       if( isWal ){
   41436                 :         testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
   41437              33 :         rc = sqlite3PagerOpenWal(pPager, 0);
   41438           16118 :       }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
   41439             293 :         pPager->journalMode = PAGER_JOURNALMODE_DELETE;
   41440                 :       }
   41441                 :     }
   41442                 :   }
   41443           16151 :   return rc;
   41444                 : }
   41445                 : #endif
   41446                 : 
   41447                 : /*
   41448                 : ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
   41449                 : ** the entire master journal file. The case pSavepoint==NULL occurs when 
   41450                 : ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction 
   41451                 : ** savepoint.
   41452                 : **
   41453                 : ** When pSavepoint is not NULL (meaning a non-transaction savepoint is 
   41454                 : ** being rolled back), then the rollback consists of up to three stages,
   41455                 : ** performed in the order specified:
   41456                 : **
   41457                 : **   * Pages are played back from the main journal starting at byte
   41458                 : **     offset PagerSavepoint.iOffset and continuing to 
   41459                 : **     PagerSavepoint.iHdrOffset, or to the end of the main journal
   41460                 : **     file if PagerSavepoint.iHdrOffset is zero.
   41461                 : **
   41462                 : **   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
   41463                 : **     back starting from the journal header immediately following 
   41464                 : **     PagerSavepoint.iHdrOffset to the end of the main journal file.
   41465                 : **
   41466                 : **   * Pages are then played back from the sub-journal file, starting
   41467                 : **     with the PagerSavepoint.iSubRec and continuing to the end of
   41468                 : **     the journal file.
   41469                 : **
   41470                 : ** Throughout the rollback process, each time a page is rolled back, the
   41471                 : ** corresponding bit is set in a bitvec structure (variable pDone in the
   41472                 : ** implementation below). This is used to ensure that a page is only
   41473                 : ** rolled back the first time it is encountered in either journal.
   41474                 : **
   41475                 : ** If pSavepoint is NULL, then pages are only played back from the main
   41476                 : ** journal file. There is no need for a bitvec in this case.
   41477                 : **
   41478                 : ** In either case, before playback commences the Pager.dbSize variable
   41479                 : ** is reset to the value that it held at the start of the savepoint 
   41480                 : ** (or transaction). No page with a page-number greater than this value
   41481                 : ** is played back. If one is encountered it is simply skipped.
   41482                 : */
   41483             337 : static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
   41484                 :   i64 szJ;                 /* Effective size of the main journal */
   41485                 :   i64 iHdrOff;             /* End of first segment of main-journal records */
   41486             337 :   int rc = SQLITE_OK;      /* Return code */
   41487             337 :   Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
   41488                 : 
   41489             337 :   assert( pPager->eState!=PAGER_ERROR );
   41490             337 :   assert( pPager->eState>=PAGER_WRITER_LOCKED );
   41491                 : 
   41492                 :   /* Allocate a bitvec to use to store the set of pages rolled back */
   41493             337 :   if( pSavepoint ){
   41494              33 :     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
   41495              33 :     if( !pDone ){
   41496               0 :       return SQLITE_NOMEM;
   41497                 :     }
   41498                 :   }
   41499                 : 
   41500                 :   /* Set the database size back to the value it was before the savepoint 
   41501                 :   ** being reverted was opened.
   41502                 :   */
   41503             337 :   pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
   41504             337 :   pPager->changeCountDone = pPager->tempFile;
   41505                 : 
   41506             337 :   if( !pSavepoint && pagerUseWal(pPager) ){
   41507             304 :     return pagerRollbackWal(pPager);
   41508                 :   }
   41509                 : 
   41510                 :   /* Use pPager->journalOff as the effective size of the main rollback
   41511                 :   ** journal.  The actual file might be larger than this in
   41512                 :   ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
   41513                 :   ** past pPager->journalOff is off-limits to us.
   41514                 :   */
   41515              33 :   szJ = pPager->journalOff;
   41516              33 :   assert( pagerUseWal(pPager)==0 || szJ==0 );
   41517                 : 
   41518                 :   /* Begin by rolling back records from the main journal starting at
   41519                 :   ** PagerSavepoint.iOffset and continuing to the next journal header.
   41520                 :   ** There might be records in the main journal that have a page number
   41521                 :   ** greater than the current database size (pPager->dbSize) but those
   41522                 :   ** will be skipped automatically.  Pages are added to pDone as they
   41523                 :   ** are played back.
   41524                 :   */
   41525              33 :   if( pSavepoint && !pagerUseWal(pPager) ){
   41526              29 :     iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
   41527              29 :     pPager->journalOff = pSavepoint->iOffset;
   41528              60 :     while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
   41529               2 :       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
   41530                 :     }
   41531              29 :     assert( rc!=SQLITE_DONE );
   41532                 :   }else{
   41533               4 :     pPager->journalOff = 0;
   41534                 :   }
   41535                 : 
   41536                 :   /* Continue rolling back records out of the main journal starting at
   41537                 :   ** the first journal header seen and continuing until the effective end
   41538                 :   ** of the main journal file.  Continue to skip out-of-range pages and
   41539                 :   ** continue adding pages rolled back to pDone.
   41540                 :   */
   41541              66 :   while( rc==SQLITE_OK && pPager->journalOff<szJ ){
   41542                 :     u32 ii;            /* Loop counter */
   41543               0 :     u32 nJRec = 0;     /* Number of Journal Records */
   41544                 :     u32 dummy;
   41545               0 :     rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
   41546               0 :     assert( rc!=SQLITE_DONE );
   41547                 : 
   41548                 :     /*
   41549                 :     ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
   41550                 :     ** test is related to ticket #2565.  See the discussion in the
   41551                 :     ** pager_playback() function for additional information.
   41552                 :     */
   41553               0 :     if( nJRec==0 
   41554               0 :      && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
   41555                 :     ){
   41556               0 :       nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
   41557                 :     }
   41558               0 :     for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
   41559               0 :       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
   41560                 :     }
   41561               0 :     assert( rc!=SQLITE_DONE );
   41562                 :   }
   41563              33 :   assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
   41564                 : 
   41565                 :   /* Finally,  rollback pages from the sub-journal.  Page that were
   41566                 :   ** previously rolled back out of the main journal (and are hence in pDone)
   41567                 :   ** will be skipped.  Out-of-range pages are also skipped.
   41568                 :   */
   41569              33 :   if( pSavepoint ){
   41570                 :     u32 ii;            /* Loop counter */
   41571              33 :     i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
   41572                 : 
   41573              33 :     if( pagerUseWal(pPager) ){
   41574               4 :       rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
   41575                 :     }
   41576              45 :     for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
   41577              12 :       assert( offset==(i64)ii*(4+pPager->pageSize) );
   41578              12 :       rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
   41579                 :     }
   41580              33 :     assert( rc!=SQLITE_DONE );
   41581                 :   }
   41582                 : 
   41583              33 :   sqlite3BitvecDestroy(pDone);
   41584              33 :   if( rc==SQLITE_OK ){
   41585              33 :     pPager->journalOff = szJ;
   41586                 :   }
   41587                 : 
   41588              33 :   return rc;
   41589                 : }
   41590                 : 
   41591                 : /*
   41592                 : ** Change the maximum number of in-memory pages that are allowed.
   41593                 : */
   41594           26450 : SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
   41595           26450 :   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
   41596           26450 : }
   41597                 : 
   41598                 : /*
   41599                 : ** Free as much memory as possible from the pager.
   41600                 : */
   41601               0 : SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
   41602               0 :   sqlite3PcacheShrink(pPager->pPCache);
   41603               0 : }
   41604                 : 
   41605                 : /*
   41606                 : ** Adjust the robustness of the database to damage due to OS crashes
   41607                 : ** or power failures by changing the number of syncs()s when writing
   41608                 : ** the rollback journal.  There are three levels:
   41609                 : **
   41610                 : **    OFF       sqlite3OsSync() is never called.  This is the default
   41611                 : **              for temporary and transient files.
   41612                 : **
   41613                 : **    NORMAL    The journal is synced once before writes begin on the
   41614                 : **              database.  This is normally adequate protection, but
   41615                 : **              it is theoretically possible, though very unlikely,
   41616                 : **              that an inopertune power failure could leave the journal
   41617                 : **              in a state which would cause damage to the database
   41618                 : **              when it is rolled back.
   41619                 : **
   41620                 : **    FULL      The journal is synced twice before writes begin on the
   41621                 : **              database (with some additional information - the nRec field
   41622                 : **              of the journal header - being written in between the two
   41623                 : **              syncs).  If we assume that writing a
   41624                 : **              single disk sector is atomic, then this mode provides
   41625                 : **              assurance that the journal will not be corrupted to the
   41626                 : **              point of causing damage to the database during rollback.
   41627                 : **
   41628                 : ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
   41629                 : ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
   41630                 : ** prior to the start of checkpoint and that the database file is synced
   41631                 : ** at the conclusion of the checkpoint if the entire content of the WAL
   41632                 : ** was written back into the database.  But no sync operations occur for
   41633                 : ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
   41634                 : ** file is synced following each commit operation, in addition to the
   41635                 : ** syncs associated with NORMAL.
   41636                 : **
   41637                 : ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
   41638                 : ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
   41639                 : ** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
   41640                 : ** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
   41641                 : ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
   41642                 : ** synchronous=FULL versus synchronous=NORMAL setting determines when
   41643                 : ** the xSync primitive is called and is relevant to all platforms.
   41644                 : **
   41645                 : ** Numeric values associated with these states are OFF==1, NORMAL=2,
   41646                 : ** and FULL=3.
   41647                 : */
   41648                 : #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   41649           21800 : SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
   41650                 :   Pager *pPager,        /* The pager to set safety level for */
   41651                 :   int level,            /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */  
   41652                 :   int bFullFsync,       /* PRAGMA fullfsync */
   41653                 :   int bCkptFullFsync    /* PRAGMA checkpoint_fullfsync */
   41654                 : ){
   41655           21800 :   assert( level>=1 && level<=3 );
   41656           21800 :   pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
   41657           21800 :   pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
   41658           21800 :   if( pPager->noSync ){
   41659            1755 :     pPager->syncFlags = 0;
   41660            1755 :     pPager->ckptSyncFlags = 0;
   41661           20045 :   }else if( bFullFsync ){
   41662               0 :     pPager->syncFlags = SQLITE_SYNC_FULL;
   41663               0 :     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
   41664           20045 :   }else if( bCkptFullFsync ){
   41665               0 :     pPager->syncFlags = SQLITE_SYNC_NORMAL;
   41666               0 :     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
   41667                 :   }else{
   41668           20045 :     pPager->syncFlags = SQLITE_SYNC_NORMAL;
   41669           20045 :     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
   41670                 :   }
   41671           21800 :   pPager->walSyncFlags = pPager->syncFlags;
   41672           21800 :   if( pPager->fullSync ){
   41673           12652 :     pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
   41674                 :   }
   41675           21800 : }
   41676                 : #endif
   41677                 : 
   41678                 : /*
   41679                 : ** The following global variable is incremented whenever the library
   41680                 : ** attempts to open a temporary file.  This information is used for
   41681                 : ** testing and analysis only.  
   41682                 : */
   41683                 : #ifdef SQLITE_TEST
   41684                 : SQLITE_API int sqlite3_opentemp_count = 0;
   41685                 : #endif
   41686                 : 
   41687                 : /*
   41688                 : ** Open a temporary file.
   41689                 : **
   41690                 : ** Write the file descriptor into *pFile. Return SQLITE_OK on success 
   41691                 : ** or some other error code if we fail. The OS will automatically 
   41692                 : ** delete the temporary file when it is closed.
   41693                 : **
   41694                 : ** The flags passed to the VFS layer xOpen() call are those specified
   41695                 : ** by parameter vfsFlags ORed with the following:
   41696                 : **
   41697                 : **     SQLITE_OPEN_READWRITE
   41698                 : **     SQLITE_OPEN_CREATE
   41699                 : **     SQLITE_OPEN_EXCLUSIVE
   41700                 : **     SQLITE_OPEN_DELETEONCLOSE
   41701                 : */
   41702             390 : static int pagerOpentemp(
   41703                 :   Pager *pPager,        /* The pager object */
   41704                 :   sqlite3_file *pFile,  /* Write the file descriptor here */
   41705                 :   int vfsFlags          /* Flags passed through to the VFS */
   41706                 : ){
   41707                 :   int rc;               /* Return code */
   41708                 : 
   41709                 : #ifdef SQLITE_TEST
   41710                 :   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
   41711                 : #endif
   41712                 : 
   41713             390 :   vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
   41714                 :             SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
   41715             390 :   rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
   41716             390 :   assert( rc!=SQLITE_OK || isOpen(pFile) );
   41717             390 :   return rc;
   41718                 : }
   41719                 : 
   41720                 : /*
   41721                 : ** Set the busy handler function.
   41722                 : **
   41723                 : ** The pager invokes the busy-handler if sqlite3OsLock() returns 
   41724                 : ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
   41725                 : ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE 
   41726                 : ** lock. It does *not* invoke the busy handler when upgrading from
   41727                 : ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
   41728                 : ** (which occurs during hot-journal rollback). Summary:
   41729                 : **
   41730                 : **   Transition                        | Invokes xBusyHandler
   41731                 : **   --------------------------------------------------------
   41732                 : **   NO_LOCK       -> SHARED_LOCK      | Yes
   41733                 : **   SHARED_LOCK   -> RESERVED_LOCK    | No
   41734                 : **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
   41735                 : **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
   41736                 : **
   41737                 : ** If the busy-handler callback returns non-zero, the lock is 
   41738                 : ** retried. If it returns zero, then the SQLITE_BUSY error is
   41739                 : ** returned to the caller of the pager API function.
   41740                 : */
   41741           19517 : SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
   41742                 :   Pager *pPager,                       /* Pager object */
   41743                 :   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
   41744                 :   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
   41745                 : ){  
   41746           19517 :   pPager->xBusyHandler = xBusyHandler;
   41747           19517 :   pPager->pBusyHandlerArg = pBusyHandlerArg;
   41748           19517 : }
   41749                 : 
   41750                 : /*
   41751                 : ** Change the page size used by the Pager object. The new page size 
   41752                 : ** is passed in *pPageSize.
   41753                 : **
   41754                 : ** If the pager is in the error state when this function is called, it
   41755                 : ** is a no-op. The value returned is the error state error code (i.e. 
   41756                 : ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
   41757                 : **
   41758                 : ** Otherwise, if all of the following are true:
   41759                 : **
   41760                 : **   * the new page size (value of *pPageSize) is valid (a power 
   41761                 : **     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
   41762                 : **
   41763                 : **   * there are no outstanding page references, and
   41764                 : **
   41765                 : **   * the database is either not an in-memory database or it is
   41766                 : **     an in-memory database that currently consists of zero pages.
   41767                 : **
   41768                 : ** then the pager object page size is set to *pPageSize.
   41769                 : **
   41770                 : ** If the page size is changed, then this function uses sqlite3PagerMalloc() 
   41771                 : ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt 
   41772                 : ** fails, SQLITE_NOMEM is returned and the page size remains unchanged. 
   41773                 : ** In all other cases, SQLITE_OK is returned.
   41774                 : **
   41775                 : ** If the page size is not changed, either because one of the enumerated
   41776                 : ** conditions above is not true, the pager was in error state when this
   41777                 : ** function was called, or because the memory allocation attempt failed, 
   41778                 : ** then *pPageSize is set to the old, retained page size before returning.
   41779                 : */
   41780           40911 : SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
   41781           40911 :   int rc = SQLITE_OK;
   41782                 : 
   41783                 :   /* It is not possible to do a full assert_pager_state() here, as this
   41784                 :   ** function may be called from within PagerOpen(), before the state
   41785                 :   ** of the Pager object is internally consistent.
   41786                 :   **
   41787                 :   ** At one point this function returned an error if the pager was in 
   41788                 :   ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
   41789                 :   ** there is at least one outstanding page reference, this function
   41790                 :   ** is a no-op for that case anyhow.
   41791                 :   */
   41792                 : 
   41793           40911 :   u32 pageSize = *pPageSize;
   41794           40911 :   assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
   41795           40911 :   if( (pPager->memDb==0 || pPager->dbSize==0)
   41796           40908 :    && sqlite3PcacheRefCount(pPager->pPCache)==0 
   41797           40902 :    && pageSize && pageSize!=(u32)pPager->pageSize 
   41798                 :   ){
   41799           19584 :     char *pNew = NULL;             /* New temp space */
   41800           19584 :     i64 nByte = 0;
   41801                 : 
   41802           19584 :     if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
   41803               0 :       rc = sqlite3OsFileSize(pPager->fd, &nByte);
   41804                 :     }
   41805           19584 :     if( rc==SQLITE_OK ){
   41806           19584 :       pNew = (char *)sqlite3PageMalloc(pageSize);
   41807           19584 :       if( !pNew ) rc = SQLITE_NOMEM;
   41808                 :     }
   41809                 : 
   41810           19584 :     if( rc==SQLITE_OK ){
   41811           19584 :       pager_reset(pPager);
   41812           19584 :       pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
   41813           19584 :       pPager->pageSize = pageSize;
   41814           19584 :       sqlite3PageFree(pPager->pTmpSpace);
   41815           19584 :       pPager->pTmpSpace = pNew;
   41816           19584 :       sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
   41817                 :     }
   41818                 :   }
   41819                 : 
   41820           40911 :   *pPageSize = pPager->pageSize;
   41821           40911 :   if( rc==SQLITE_OK ){
   41822           40911 :     if( nReserve<0 ) nReserve = pPager->nReserve;
   41823           40911 :     assert( nReserve>=0 && nReserve<1000 );
   41824           40911 :     pPager->nReserve = (i16)nReserve;
   41825                 :     pagerReportSize(pPager);
   41826                 :   }
   41827           40911 :   return rc;
   41828                 : }
   41829                 : 
   41830                 : /*
   41831                 : ** Return a pointer to the "temporary page" buffer held internally
   41832                 : ** by the pager.  This is a buffer that is big enough to hold the
   41833                 : ** entire content of a database page.  This buffer is used internally
   41834                 : ** during rollback and will be overwritten whenever a rollback
   41835                 : ** occurs.  But other modules are free to use it too, as long as
   41836                 : ** no rollbacks are happening.
   41837                 : */
   41838             301 : SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
   41839             301 :   return pPager->pTmpSpace;
   41840                 : }
   41841                 : 
   41842                 : /*
   41843                 : ** Attempt to set the maximum database page count if mxPage is positive. 
   41844                 : ** Make no changes if mxPage is zero or negative.  And never reduce the
   41845                 : ** maximum page count below the current size of the database.
   41846                 : **
   41847                 : ** Regardless of mxPage, return the current maximum page count.
   41848                 : */
   41849               0 : SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
   41850               0 :   if( mxPage>0 ){
   41851               0 :     pPager->mxPgno = mxPage;
   41852                 :   }
   41853               0 :   assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
   41854               0 :   assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
   41855               0 :   return pPager->mxPgno;
   41856                 : }
   41857                 : 
   41858                 : /*
   41859                 : ** The following set of routines are used to disable the simulated
   41860                 : ** I/O error mechanism.  These routines are used to avoid simulated
   41861                 : ** errors in places where we do not care about errors.
   41862                 : **
   41863                 : ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
   41864                 : ** and generate no code.
   41865                 : */
   41866                 : #ifdef SQLITE_TEST
   41867                 : SQLITE_API extern int sqlite3_io_error_pending;
   41868                 : SQLITE_API extern int sqlite3_io_error_hit;
   41869                 : static int saved_cnt;
   41870                 : void disable_simulated_io_errors(void){
   41871                 :   saved_cnt = sqlite3_io_error_pending;
   41872                 :   sqlite3_io_error_pending = -1;
   41873                 : }
   41874                 : void enable_simulated_io_errors(void){
   41875                 :   sqlite3_io_error_pending = saved_cnt;
   41876                 : }
   41877                 : #else
   41878                 : # define disable_simulated_io_errors()
   41879                 : # define enable_simulated_io_errors()
   41880                 : #endif
   41881                 : 
   41882                 : /*
   41883                 : ** Read the first N bytes from the beginning of the file into memory
   41884                 : ** that pDest points to. 
   41885                 : **
   41886                 : ** If the pager was opened on a transient file (zFilename==""), or
   41887                 : ** opened on a file less than N bytes in size, the output buffer is
   41888                 : ** zeroed and SQLITE_OK returned. The rationale for this is that this 
   41889                 : ** function is used to read database headers, and a new transient or
   41890                 : ** zero sized database has a header than consists entirely of zeroes.
   41891                 : **
   41892                 : ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
   41893                 : ** the error code is returned to the caller and the contents of the
   41894                 : ** output buffer undefined.
   41895                 : */
   41896           19517 : SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
   41897           19517 :   int rc = SQLITE_OK;
   41898           19517 :   memset(pDest, 0, N);
   41899           19517 :   assert( isOpen(pPager->fd) || pPager->tempFile );
   41900                 : 
   41901                 :   /* This routine is only called by btree immediately after creating
   41902                 :   ** the Pager object.  There has not been an opportunity to transition
   41903                 :   ** to WAL mode yet.
   41904                 :   */
   41905           19517 :   assert( !pagerUseWal(pPager) );
   41906                 : 
   41907           19517 :   if( isOpen(pPager->fd) ){
   41908                 :     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
   41909            3052 :     rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
   41910            3052 :     if( rc==SQLITE_IOERR_SHORT_READ ){
   41911            1401 :       rc = SQLITE_OK;
   41912                 :     }
   41913                 :   }
   41914           19517 :   return rc;
   41915                 : }
   41916                 : 
   41917                 : /*
   41918                 : ** This function may only be called when a read-transaction is open on
   41919                 : ** the pager. It returns the total number of pages in the database.
   41920                 : **
   41921                 : ** However, if the file is between 1 and <page-size> bytes in size, then 
   41922                 : ** this is considered a 1 page file.
   41923                 : */
   41924          100215 : SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
   41925          100215 :   assert( pPager->eState>=PAGER_READER );
   41926          100215 :   assert( pPager->eState!=PAGER_WRITER_FINISHED );
   41927          100215 :   *pnPage = (int)pPager->dbSize;
   41928          100215 : }
   41929                 : 
   41930                 : 
   41931                 : /*
   41932                 : ** Try to obtain a lock of type locktype on the database file. If
   41933                 : ** a similar or greater lock is already held, this function is a no-op
   41934                 : ** (returning SQLITE_OK immediately).
   41935                 : **
   41936                 : ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke 
   41937                 : ** the busy callback if the lock is currently not available. Repeat 
   41938                 : ** until the busy callback returns false or until the attempt to 
   41939                 : ** obtain the lock succeeds.
   41940                 : **
   41941                 : ** Return SQLITE_OK on success and an error code if we cannot obtain
   41942                 : ** the lock. If the lock is obtained successfully, set the Pager.state 
   41943                 : ** variable to locktype before returning.
   41944                 : */
   41945           40066 : static int pager_wait_on_lock(Pager *pPager, int locktype){
   41946                 :   int rc;                              /* Return code */
   41947                 : 
   41948                 :   /* Check that this is either a no-op (because the requested lock is 
   41949                 :   ** already held, or one of the transistions that the busy-handler
   41950                 :   ** may be invoked during, according to the comment above
   41951                 :   ** sqlite3PagerSetBusyhandler().
   41952                 :   */
   41953           40066 :   assert( (pPager->eLock>=locktype)
   41954                 :        || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
   41955                 :        || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
   41956                 :   );
   41957                 : 
   41958                 :   do {
   41959           40066 :     rc = pagerLockDb(pPager, locktype);
   41960           40066 :   }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
   41961           40066 :   return rc;
   41962                 : }
   41963                 : 
   41964                 : /*
   41965                 : ** Function assertTruncateConstraint(pPager) checks that one of the 
   41966                 : ** following is true for all dirty pages currently in the page-cache:
   41967                 : **
   41968                 : **   a) The page number is less than or equal to the size of the 
   41969                 : **      current database image, in pages, OR
   41970                 : **
   41971                 : **   b) if the page content were written at this time, it would not
   41972                 : **      be necessary to write the current content out to the sub-journal
   41973                 : **      (as determined by function subjRequiresPage()).
   41974                 : **
   41975                 : ** If the condition asserted by this function were not true, and the
   41976                 : ** dirty page were to be discarded from the cache via the pagerStress()
   41977                 : ** routine, pagerStress() would not write the current page content to
   41978                 : ** the database file. If a savepoint transaction were rolled back after
   41979                 : ** this happened, the correct behaviour would be to restore the current
   41980                 : ** content of the page. However, since this content is not present in either
   41981                 : ** the database file or the portion of the rollback journal and 
   41982                 : ** sub-journal rolled back the content could not be restored and the
   41983                 : ** database image would become corrupt. It is therefore fortunate that 
   41984                 : ** this circumstance cannot arise.
   41985                 : */
   41986                 : #if defined(SQLITE_DEBUG)
   41987          182645 : static void assertTruncateConstraintCb(PgHdr *pPg){
   41988          182645 :   assert( pPg->flags&PGHDR_DIRTY );
   41989          182645 :   assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
   41990          182645 : }
   41991           22640 : static void assertTruncateConstraint(Pager *pPager){
   41992           22640 :   sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
   41993           22640 : }
   41994                 : #else
   41995                 : # define assertTruncateConstraint(pPager)
   41996                 : #endif
   41997                 : 
   41998                 : /*
   41999                 : ** Truncate the in-memory database file image to nPage pages. This 
   42000                 : ** function does not actually modify the database file on disk. It 
   42001                 : ** just sets the internal state of the pager object so that the 
   42002                 : ** truncation will be done when the current transaction is committed.
   42003                 : */
   42004               6 : SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
   42005               6 :   assert( pPager->dbSize>=nPage );
   42006               6 :   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
   42007               6 :   pPager->dbSize = nPage;
   42008               6 :   assertTruncateConstraint(pPager);
   42009               6 : }
   42010                 : 
   42011                 : 
   42012                 : /*
   42013                 : ** This function is called before attempting a hot-journal rollback. It
   42014                 : ** syncs the journal file to disk, then sets pPager->journalHdr to the
   42015                 : ** size of the journal file so that the pager_playback() routine knows
   42016                 : ** that the entire journal file has been synced.
   42017                 : **
   42018                 : ** Syncing a hot-journal to disk before attempting to roll it back ensures 
   42019                 : ** that if a power-failure occurs during the rollback, the process that
   42020                 : ** attempts rollback following system recovery sees the same journal
   42021                 : ** content as this process.
   42022                 : **
   42023                 : ** If everything goes as planned, SQLITE_OK is returned. Otherwise, 
   42024                 : ** an SQLite error code.
   42025                 : */
   42026             847 : static int pagerSyncHotJournal(Pager *pPager){
   42027             847 :   int rc = SQLITE_OK;
   42028             847 :   if( !pPager->noSync ){
   42029             826 :     rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
   42030                 :   }
   42031             847 :   if( rc==SQLITE_OK ){
   42032             847 :     rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
   42033                 :   }
   42034             847 :   return rc;
   42035                 : }
   42036                 : 
   42037                 : /*
   42038                 : ** Shutdown the page cache.  Free all memory and close all files.
   42039                 : **
   42040                 : ** If a transaction was in progress when this routine is called, that
   42041                 : ** transaction is rolled back.  All outstanding pages are invalidated
   42042                 : ** and their memory is freed.  Any attempt to use a page associated
   42043                 : ** with this page cache after this function returns will likely
   42044                 : ** result in a coredump.
   42045                 : **
   42046                 : ** This function always succeeds. If a transaction is active an attempt
   42047                 : ** is made to roll it back. If an error occurs during the rollback 
   42048                 : ** a hot journal may be left in the filesystem but no error is returned
   42049                 : ** to the caller.
   42050                 : */
   42051           19517 : SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
   42052           19517 :   u8 *pTmp = (u8 *)pPager->pTmpSpace;
   42053                 : 
   42054           19517 :   assert( assert_pager_state(pPager) );
   42055                 :   disable_simulated_io_errors();
   42056           19517 :   sqlite3BeginBenignMalloc();
   42057                 :   /* pPager->errCode = 0; */
   42058           19517 :   pPager->exclusiveMode = 0;
   42059                 : #ifndef SQLITE_OMIT_WAL
   42060           19517 :   sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
   42061           19517 :   pPager->pWal = 0;
   42062                 : #endif
   42063           19517 :   pager_reset(pPager);
   42064           19517 :   if( MEMDB ){
   42065           15458 :     pager_unlock(pPager);
   42066                 :   }else{
   42067                 :     /* If it is open, sync the journal file before calling UnlockAndRollback.
   42068                 :     ** If this is not done, then an unsynced portion of the open journal 
   42069                 :     ** file may be played back into the database. If a power failure occurs 
   42070                 :     ** while this is happening, the database could become corrupt.
   42071                 :     **
   42072                 :     ** If an error occurs while trying to sync the journal, shift the pager
   42073                 :     ** into the ERROR state. This causes UnlockAndRollback to unlock the
   42074                 :     ** database and close the journal file without attempting to roll it
   42075                 :     ** back or finalize it. The next database user will have to do hot-journal
   42076                 :     ** rollback before accessing the database file.
   42077                 :     */
   42078            4059 :     if( isOpen(pPager->jfd) ){
   42079             847 :       pager_error(pPager, pagerSyncHotJournal(pPager));
   42080                 :     }
   42081            4059 :     pagerUnlockAndRollback(pPager);
   42082                 :   }
   42083           19517 :   sqlite3EndBenignMalloc();
   42084                 :   enable_simulated_io_errors();
   42085                 :   PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
   42086                 :   IOTRACE(("CLOSE %p\n", pPager))
   42087           19517 :   sqlite3OsClose(pPager->jfd);
   42088           19517 :   sqlite3OsClose(pPager->fd);
   42089           19517 :   sqlite3PageFree(pTmp);
   42090           19517 :   sqlite3PcacheClose(pPager->pPCache);
   42091                 : 
   42092                 : #ifdef SQLITE_HAS_CODEC
   42093                 :   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
   42094                 : #endif
   42095                 : 
   42096           19517 :   assert( !pPager->aSavepoint && !pPager->pInJournal );
   42097           19517 :   assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
   42098                 : 
   42099           19517 :   sqlite3_free(pPager);
   42100           19517 :   return SQLITE_OK;
   42101                 : }
   42102                 : 
   42103                 : #if !defined(NDEBUG) || defined(SQLITE_TEST)
   42104                 : /*
   42105                 : ** Return the page number for page pPg.
   42106                 : */
   42107          534498 : SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
   42108          534498 :   return pPg->pgno;
   42109                 : }
   42110                 : #endif
   42111                 : 
   42112                 : /*
   42113                 : ** Increment the reference count for page pPg.
   42114                 : */
   42115              93 : SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
   42116              93 :   sqlite3PcacheRef(pPg);
   42117              93 : }
   42118                 : 
   42119                 : /*
   42120                 : ** Sync the journal. In other words, make sure all the pages that have
   42121                 : ** been written to the journal have actually reached the surface of the
   42122                 : ** disk and can be restored in the event of a hot-journal rollback.
   42123                 : **
   42124                 : ** If the Pager.noSync flag is set, then this function is a no-op.
   42125                 : ** Otherwise, the actions required depend on the journal-mode and the 
   42126                 : ** device characteristics of the the file-system, as follows:
   42127                 : **
   42128                 : **   * If the journal file is an in-memory journal file, no action need
   42129                 : **     be taken.
   42130                 : **
   42131                 : **   * Otherwise, if the device does not support the SAFE_APPEND property,
   42132                 : **     then the nRec field of the most recently written journal header
   42133                 : **     is updated to contain the number of journal records that have
   42134                 : **     been written following it. If the pager is operating in full-sync
   42135                 : **     mode, then the journal file is synced before this field is updated.
   42136                 : **
   42137                 : **   * If the device does not support the SEQUENTIAL property, then 
   42138                 : **     journal file is synced.
   42139                 : **
   42140                 : ** Or, in pseudo-code:
   42141                 : **
   42142                 : **   if( NOT <in-memory journal> ){
   42143                 : **     if( NOT SAFE_APPEND ){
   42144                 : **       if( <full-sync mode> ) xSync(<journal file>);
   42145                 : **       <update nRec field>
   42146                 : **     } 
   42147                 : **     if( NOT SEQUENTIAL ) xSync(<journal file>);
   42148                 : **   }
   42149                 : **
   42150                 : ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every 
   42151                 : ** page currently held in memory before returning SQLITE_OK. If an IO
   42152                 : ** error is encountered, then the IO error code is returned to the caller.
   42153                 : */
   42154            8675 : static int syncJournal(Pager *pPager, int newHdr){
   42155                 :   int rc;                         /* Return code */
   42156                 : 
   42157            8675 :   assert( pPager->eState==PAGER_WRITER_CACHEMOD
   42158                 :        || pPager->eState==PAGER_WRITER_DBMOD
   42159                 :   );
   42160            8675 :   assert( assert_pager_state(pPager) );
   42161            8675 :   assert( !pagerUseWal(pPager) );
   42162                 : 
   42163            8675 :   rc = sqlite3PagerExclusiveLock(pPager);
   42164            8675 :   if( rc!=SQLITE_OK ) return rc;
   42165                 : 
   42166            8675 :   if( !pPager->noSync ){
   42167            6443 :     assert( !pPager->tempFile );
   42168           12886 :     if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
   42169            6443 :       const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
   42170            6443 :       assert( isOpen(pPager->jfd) );
   42171                 : 
   42172            6443 :       if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
   42173                 :         /* This block deals with an obscure problem. If the last connection
   42174                 :         ** that wrote to this database was operating in persistent-journal
   42175                 :         ** mode, then the journal file may at this point actually be larger
   42176                 :         ** than Pager.journalOff bytes. If the next thing in the journal
   42177                 :         ** file happens to be a journal-header (written as part of the
   42178                 :         ** previous connection's transaction), and a crash or power-failure 
   42179                 :         ** occurs after nRec is updated but before this connection writes 
   42180                 :         ** anything else to the journal file (or commits/rolls back its 
   42181                 :         ** transaction), then SQLite may become confused when doing the 
   42182                 :         ** hot-journal rollback following recovery. It may roll back all
   42183                 :         ** of this connections data, then proceed to rolling back the old,
   42184                 :         ** out-of-date data that follows it. Database corruption.
   42185                 :         **
   42186                 :         ** To work around this, if the journal file does appear to contain
   42187                 :         ** a valid header following Pager.journalOff, then write a 0x00
   42188                 :         ** byte to the start of it to prevent it from being recognized.
   42189                 :         **
   42190                 :         ** Variable iNextHdrOffset is set to the offset at which this
   42191                 :         ** problematic header will occur, if it exists. aMagic is used 
   42192                 :         ** as a temporary buffer to inspect the first couple of bytes of
   42193                 :         ** the potential journal header.
   42194                 :         */
   42195                 :         i64 iNextHdrOffset;
   42196                 :         u8 aMagic[8];
   42197                 :         u8 zHeader[sizeof(aJournalMagic)+4];
   42198                 : 
   42199            6443 :         memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
   42200            6443 :         put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
   42201                 : 
   42202            6443 :         iNextHdrOffset = journalHdrOffset(pPager);
   42203            6443 :         rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
   42204            6443 :         if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
   42205                 :           static const u8 zerobyte = 0;
   42206               0 :           rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
   42207                 :         }
   42208            6443 :         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
   42209               0 :           return rc;
   42210                 :         }
   42211                 : 
   42212                 :         /* Write the nRec value into the journal file header. If in
   42213                 :         ** full-synchronous mode, sync the journal first. This ensures that
   42214                 :         ** all data has really hit the disk before nRec is updated to mark
   42215                 :         ** it as a candidate for rollback.
   42216                 :         **
   42217                 :         ** This is not required if the persistent media supports the
   42218                 :         ** SAFE_APPEND property. Because in this case it is not possible 
   42219                 :         ** for garbage data to be appended to the file, the nRec field
   42220                 :         ** is populated with 0xFFFFFFFF when the journal header is written
   42221                 :         ** and never needs to be updated.
   42222                 :         */
   42223            6443 :         if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
   42224                 :           PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
   42225                 :           IOTRACE(("JSYNC %p\n", pPager))
   42226            2243 :           rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
   42227            2243 :           if( rc!=SQLITE_OK ) return rc;
   42228                 :         }
   42229                 :         IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
   42230            6443 :         rc = sqlite3OsWrite(
   42231                 :             pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
   42232                 :         );
   42233            6443 :         if( rc!=SQLITE_OK ) return rc;
   42234                 :       }
   42235            6443 :       if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
   42236                 :         PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
   42237                 :         IOTRACE(("JSYNC %p\n", pPager))
   42238           12886 :         rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags| 
   42239            6443 :           (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
   42240                 :         );
   42241            6443 :         if( rc!=SQLITE_OK ) return rc;
   42242                 :       }
   42243                 : 
   42244            6443 :       pPager->journalHdr = pPager->journalOff;
   42245            6443 :       if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
   42246               0 :         pPager->nRec = 0;
   42247               0 :         rc = writeJournalHdr(pPager);
   42248               0 :         if( rc!=SQLITE_OK ) return rc;
   42249                 :       }
   42250                 :     }else{
   42251               0 :       pPager->journalHdr = pPager->journalOff;
   42252                 :     }
   42253                 :   }
   42254                 : 
   42255                 :   /* Unless the pager is in noSync mode, the journal file was just 
   42256                 :   ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on 
   42257                 :   ** all pages.
   42258                 :   */
   42259            8675 :   sqlite3PcacheClearSyncFlags(pPager->pPCache);
   42260            8675 :   pPager->eState = PAGER_WRITER_DBMOD;
   42261            8675 :   assert( assert_pager_state(pPager) );
   42262            8675 :   return SQLITE_OK;
   42263                 : }
   42264                 : 
   42265                 : /*
   42266                 : ** The argument is the first in a linked list of dirty pages connected
   42267                 : ** by the PgHdr.pDirty pointer. This function writes each one of the
   42268                 : ** in-memory pages in the list to the database file. The argument may
   42269                 : ** be NULL, representing an empty list. In this case this function is
   42270                 : ** a no-op.
   42271                 : **
   42272                 : ** The pager must hold at least a RESERVED lock when this function
   42273                 : ** is called. Before writing anything to the database file, this lock
   42274                 : ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
   42275                 : ** SQLITE_BUSY is returned and no data is written to the database file.
   42276                 : ** 
   42277                 : ** If the pager is a temp-file pager and the actual file-system file
   42278                 : ** is not yet open, it is created and opened before any data is 
   42279                 : ** written out.
   42280                 : **
   42281                 : ** Once the lock has been upgraded and, if necessary, the file opened,
   42282                 : ** the pages are written out to the database file in list order. Writing
   42283                 : ** a page is skipped if it meets either of the following criteria:
   42284                 : **
   42285                 : **   * The page number is greater than Pager.dbSize, or
   42286                 : **   * The PGHDR_DONT_WRITE flag is set on the page.
   42287                 : **
   42288                 : ** If writing out a page causes the database file to grow, Pager.dbFileSize
   42289                 : ** is updated accordingly. If page 1 is written out, then the value cached
   42290                 : ** in Pager.dbFileVers[] is updated to match the new value stored in
   42291                 : ** the database file.
   42292                 : **
   42293                 : ** If everything is successful, SQLITE_OK is returned. If an IO error 
   42294                 : ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
   42295                 : ** be obtained, SQLITE_BUSY is returned.
   42296                 : */
   42297            8675 : static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
   42298            8675 :   int rc = SQLITE_OK;                  /* Return code */
   42299                 : 
   42300                 :   /* This function is only called for rollback pagers in WRITER_DBMOD state. */
   42301            8675 :   assert( !pagerUseWal(pPager) );
   42302            8675 :   assert( pPager->eState==PAGER_WRITER_DBMOD );
   42303            8675 :   assert( pPager->eLock==EXCLUSIVE_LOCK );
   42304                 : 
   42305                 :   /* If the file is a temp-file has not yet been opened, open it now. It
   42306                 :   ** is not possible for rc to be other than SQLITE_OK if this branch
   42307                 :   ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
   42308                 :   */
   42309            8675 :   if( !isOpen(pPager->fd) ){
   42310              21 :     assert( pPager->tempFile && rc==SQLITE_OK );
   42311              21 :     rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
   42312                 :   }
   42313                 : 
   42314                 :   /* Before the first write, give the VFS a hint of what the final
   42315                 :   ** file size will be.
   42316                 :   */
   42317            8675 :   assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
   42318            8675 :   if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
   42319            2559 :     sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
   42320            2559 :     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
   42321            2559 :     pPager->dbHintSize = pPager->dbSize;
   42322                 :   }
   42323                 : 
   42324           44220 :   while( rc==SQLITE_OK && pList ){
   42325           26870 :     Pgno pgno = pList->pgno;
   42326                 : 
   42327                 :     /* If there are dirty pages in the page cache with page numbers greater
   42328                 :     ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
   42329                 :     ** make the file smaller (presumably by auto-vacuum code). Do not write
   42330                 :     ** any such pages to the file.
   42331                 :     **
   42332                 :     ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
   42333                 :     ** set (set by sqlite3PagerDontWrite()).
   42334                 :     */
   42335           26870 :     if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
   42336           26854 :       i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
   42337                 :       char *pData;                                   /* Data to write */    
   42338                 : 
   42339           26854 :       assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
   42340           26854 :       if( pList->pgno==1 ) pager_write_changecounter(pList);
   42341                 : 
   42342                 :       /* Encode the database */
   42343           26854 :       CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
   42344                 : 
   42345                 :       /* Write out the page data. */
   42346           26854 :       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
   42347                 : 
   42348                 :       /* If page 1 was just written, update Pager.dbFileVers to match
   42349                 :       ** the value now stored in the database file. If writing this 
   42350                 :       ** page caused the database file to grow, update dbFileSize. 
   42351                 :       */
   42352           26854 :       if( pgno==1 ){
   42353            7183 :         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
   42354                 :       }
   42355           26854 :       if( pgno>pPager->dbFileSize ){
   42356            6518 :         pPager->dbFileSize = pgno;
   42357                 :       }
   42358                 : 
   42359                 :       /* Update any backup objects copying the contents of this pager. */
   42360           26854 :       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
   42361                 : 
   42362                 :       PAGERTRACE(("STORE %d page %d hash(%08x)\n",
   42363                 :                    PAGERID(pPager), pgno, pager_pagehash(pList)));
   42364                 :       IOTRACE(("PGOUT %p %d\n", pPager, pgno));
   42365                 :       PAGER_INCR(sqlite3_pager_writedb_count);
   42366                 :       PAGER_INCR(pPager->nWrite);
   42367                 :     }else{
   42368                 :       PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
   42369                 :     }
   42370                 :     pager_set_pagehash(pList);
   42371           26870 :     pList = pList->pDirty;
   42372                 :   }
   42373                 : 
   42374            8675 :   return rc;
   42375                 : }
   42376                 : 
   42377                 : /*
   42378                 : ** Ensure that the sub-journal file is open. If it is already open, this 
   42379                 : ** function is a no-op.
   42380                 : **
   42381                 : ** SQLITE_OK is returned if everything goes according to plan. An 
   42382                 : ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen() 
   42383                 : ** fails.
   42384                 : */
   42385           53922 : static int openSubJournal(Pager *pPager){
   42386           53922 :   int rc = SQLITE_OK;
   42387           53922 :   if( !isOpen(pPager->sjfd) ){
   42388            5329 :     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
   42389            4960 :       sqlite3MemJournalOpen(pPager->sjfd);
   42390                 :     }else{
   42391             369 :       rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
   42392                 :     }
   42393                 :   }
   42394           53922 :   return rc;
   42395                 : }
   42396                 : 
   42397                 : /*
   42398                 : ** Append a record of the current state of page pPg to the sub-journal. 
   42399                 : ** It is the callers responsibility to use subjRequiresPage() to check 
   42400                 : ** that it is really required before calling this function.
   42401                 : **
   42402                 : ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
   42403                 : ** for all open savepoints before returning.
   42404                 : **
   42405                 : ** This function returns SQLITE_OK if everything is successful, an IO
   42406                 : ** error code if the attempt to write to the sub-journal fails, or 
   42407                 : ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
   42408                 : ** bitvec.
   42409                 : */
   42410           53922 : static int subjournalPage(PgHdr *pPg){
   42411           53922 :   int rc = SQLITE_OK;
   42412           53922 :   Pager *pPager = pPg->pPager;
   42413           53922 :   if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
   42414                 : 
   42415                 :     /* Open the sub-journal, if it has not already been opened */
   42416           53922 :     assert( pPager->useJournal );
   42417           53922 :     assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
   42418           53922 :     assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
   42419           53922 :     assert( pagerUseWal(pPager) 
   42420                 :          || pageInJournal(pPg) 
   42421                 :          || pPg->pgno>pPager->dbOrigSize 
   42422                 :     );
   42423           53922 :     rc = openSubJournal(pPager);
   42424                 : 
   42425                 :     /* If the sub-journal was opened successfully (or was already open),
   42426                 :     ** write the journal record into the file.  */
   42427           53922 :     if( rc==SQLITE_OK ){
   42428           53922 :       void *pData = pPg->pData;
   42429           53922 :       i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
   42430                 :       char *pData2;
   42431                 :   
   42432           53922 :       CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
   42433                 :       PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
   42434           53922 :       rc = write32bits(pPager->sjfd, offset, pPg->pgno);
   42435           53922 :       if( rc==SQLITE_OK ){
   42436           53922 :         rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
   42437                 :       }
   42438                 :     }
   42439                 :   }
   42440           53922 :   if( rc==SQLITE_OK ){
   42441           53922 :     pPager->nSubRec++;
   42442           53922 :     assert( pPager->nSavepoint>0 );
   42443           53922 :     rc = addToSavepointBitvecs(pPager, pPg->pgno);
   42444                 :   }
   42445           53922 :   return rc;
   42446                 : }
   42447                 : 
   42448                 : /*
   42449                 : ** This function is called by the pcache layer when it has reached some
   42450                 : ** soft memory limit. The first argument is a pointer to a Pager object
   42451                 : ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
   42452                 : ** database). The second argument is a reference to a page that is 
   42453                 : ** currently dirty but has no outstanding references. The page
   42454                 : ** is always associated with the Pager object passed as the first 
   42455                 : ** argument.
   42456                 : **
   42457                 : ** The job of this function is to make pPg clean by writing its contents
   42458                 : ** out to the database file, if possible. This may involve syncing the
   42459                 : ** journal file. 
   42460                 : **
   42461                 : ** If successful, sqlite3PcacheMakeClean() is called on the page and
   42462                 : ** SQLITE_OK returned. If an IO error occurs while trying to make the
   42463                 : ** page clean, the IO error code is returned. If the page cannot be
   42464                 : ** made clean for some other reason, but no error occurs, then SQLITE_OK
   42465                 : ** is returned by sqlite3PcacheMakeClean() is not called.
   42466                 : */
   42467               0 : static int pagerStress(void *p, PgHdr *pPg){
   42468               0 :   Pager *pPager = (Pager *)p;
   42469               0 :   int rc = SQLITE_OK;
   42470                 : 
   42471               0 :   assert( pPg->pPager==pPager );
   42472               0 :   assert( pPg->flags&PGHDR_DIRTY );
   42473                 : 
   42474                 :   /* The doNotSyncSpill flag is set during times when doing a sync of
   42475                 :   ** journal (and adding a new header) is not allowed.  This occurs
   42476                 :   ** during calls to sqlite3PagerWrite() while trying to journal multiple
   42477                 :   ** pages belonging to the same sector.
   42478                 :   **
   42479                 :   ** The doNotSpill flag inhibits all cache spilling regardless of whether
   42480                 :   ** or not a sync is required.  This is set during a rollback.
   42481                 :   **
   42482                 :   ** Spilling is also prohibited when in an error state since that could
   42483                 :   ** lead to database corruption.   In the current implementaton it 
   42484                 :   ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
   42485                 :   ** while in the error state, hence it is impossible for this routine to
   42486                 :   ** be called in the error state.  Nevertheless, we include a NEVER()
   42487                 :   ** test for the error state as a safeguard against future changes.
   42488                 :   */
   42489               0 :   if( NEVER(pPager->errCode) ) return SQLITE_OK;
   42490               0 :   if( pPager->doNotSpill ) return SQLITE_OK;
   42491               0 :   if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
   42492               0 :     return SQLITE_OK;
   42493                 :   }
   42494                 : 
   42495               0 :   pPg->pDirty = 0;
   42496               0 :   if( pagerUseWal(pPager) ){
   42497                 :     /* Write a single frame for this page to the log. */
   42498               0 :     if( subjRequiresPage(pPg) ){ 
   42499               0 :       rc = subjournalPage(pPg); 
   42500                 :     }
   42501               0 :     if( rc==SQLITE_OK ){
   42502               0 :       rc = pagerWalFrames(pPager, pPg, 0, 0);
   42503                 :     }
   42504                 :   }else{
   42505                 :   
   42506                 :     /* Sync the journal file if required. */
   42507               0 :     if( pPg->flags&PGHDR_NEED_SYNC 
   42508               0 :      || pPager->eState==PAGER_WRITER_CACHEMOD
   42509                 :     ){
   42510               0 :       rc = syncJournal(pPager, 1);
   42511                 :     }
   42512                 :   
   42513                 :     /* If the page number of this page is larger than the current size of
   42514                 :     ** the database image, it may need to be written to the sub-journal.
   42515                 :     ** This is because the call to pager_write_pagelist() below will not
   42516                 :     ** actually write data to the file in this case.
   42517                 :     **
   42518                 :     ** Consider the following sequence of events:
   42519                 :     **
   42520                 :     **   BEGIN;
   42521                 :     **     <journal page X>
   42522                 :     **     <modify page X>
   42523                 :     **     SAVEPOINT sp;
   42524                 :     **       <shrink database file to Y pages>
   42525                 :     **       pagerStress(page X)
   42526                 :     **     ROLLBACK TO sp;
   42527                 :     **
   42528                 :     ** If (X>Y), then when pagerStress is called page X will not be written
   42529                 :     ** out to the database file, but will be dropped from the cache. Then,
   42530                 :     ** following the "ROLLBACK TO sp" statement, reading page X will read
   42531                 :     ** data from the database file. This will be the copy of page X as it
   42532                 :     ** was when the transaction started, not as it was when "SAVEPOINT sp"
   42533                 :     ** was executed.
   42534                 :     **
   42535                 :     ** The solution is to write the current data for page X into the 
   42536                 :     ** sub-journal file now (if it is not already there), so that it will
   42537                 :     ** be restored to its current value when the "ROLLBACK TO sp" is 
   42538                 :     ** executed.
   42539                 :     */
   42540               0 :     if( NEVER(
   42541                 :         rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
   42542                 :     ) ){
   42543               0 :       rc = subjournalPage(pPg);
   42544                 :     }
   42545                 :   
   42546                 :     /* Write the contents of the page out to the database file. */
   42547               0 :     if( rc==SQLITE_OK ){
   42548               0 :       assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
   42549               0 :       rc = pager_write_pagelist(pPager, pPg);
   42550                 :     }
   42551                 :   }
   42552                 : 
   42553                 :   /* Mark the page as clean. */
   42554               0 :   if( rc==SQLITE_OK ){
   42555                 :     PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
   42556               0 :     sqlite3PcacheMakeClean(pPg);
   42557                 :   }
   42558                 : 
   42559               0 :   return pager_error(pPager, rc); 
   42560                 : }
   42561                 : 
   42562                 : 
   42563                 : /*
   42564                 : ** Allocate and initialize a new Pager object and put a pointer to it
   42565                 : ** in *ppPager. The pager should eventually be freed by passing it
   42566                 : ** to sqlite3PagerClose().
   42567                 : **
   42568                 : ** The zFilename argument is the path to the database file to open.
   42569                 : ** If zFilename is NULL then a randomly-named temporary file is created
   42570                 : ** and used as the file to be cached. Temporary files are be deleted
   42571                 : ** automatically when they are closed. If zFilename is ":memory:" then 
   42572                 : ** all information is held in cache. It is never written to disk. 
   42573                 : ** This can be used to implement an in-memory database.
   42574                 : **
   42575                 : ** The nExtra parameter specifies the number of bytes of space allocated
   42576                 : ** along with each page reference. This space is available to the user
   42577                 : ** via the sqlite3PagerGetExtra() API.
   42578                 : **
   42579                 : ** The flags argument is used to specify properties that affect the
   42580                 : ** operation of the pager. It should be passed some bitwise combination
   42581                 : ** of the PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK flags.
   42582                 : **
   42583                 : ** The vfsFlags parameter is a bitmask to pass to the flags parameter
   42584                 : ** of the xOpen() method of the supplied VFS when opening files. 
   42585                 : **
   42586                 : ** If the pager object is allocated and the specified file opened 
   42587                 : ** successfully, SQLITE_OK is returned and *ppPager set to point to
   42588                 : ** the new pager object. If an error occurs, *ppPager is set to NULL
   42589                 : ** and error code returned. This function may return SQLITE_NOMEM
   42590                 : ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or 
   42591                 : ** various SQLITE_IO_XXX errors.
   42592                 : */
   42593           19521 : SQLITE_PRIVATE int sqlite3PagerOpen(
   42594                 :   sqlite3_vfs *pVfs,       /* The virtual file system to use */
   42595                 :   Pager **ppPager,         /* OUT: Return the Pager structure here */
   42596                 :   const char *zFilename,   /* Name of the database file to open */
   42597                 :   int nExtra,              /* Extra bytes append to each in-memory page */
   42598                 :   int flags,               /* flags controlling this file */
   42599                 :   int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
   42600                 :   void (*xReinit)(DbPage*) /* Function to reinitialize pages */
   42601                 : ){
   42602                 :   u8 *pPtr;
   42603           19521 :   Pager *pPager = 0;       /* Pager object to allocate and return */
   42604           19521 :   int rc = SQLITE_OK;      /* Return code */
   42605           19521 :   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
   42606           19521 :   int memDb = 0;           /* True if this is an in-memory file */
   42607           19521 :   int readOnly = 0;        /* True if this is a read-only file */
   42608                 :   int journalFileSize;     /* Bytes to allocate for each journal fd */
   42609           19521 :   char *zPathname = 0;     /* Full path to database file */
   42610           19521 :   int nPathname = 0;       /* Number of bytes in zPathname */
   42611           19521 :   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
   42612           19521 :   int noReadlock = (flags & PAGER_NO_READLOCK)!=0;  /* True to omit read-lock */
   42613           19521 :   int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
   42614           19521 :   u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
   42615           19521 :   const char *zUri = 0;    /* URI args to copy */
   42616           19521 :   int nUri = 0;            /* Number of bytes of URI args at *zUri */
   42617                 : 
   42618                 :   /* Figure out how much space is required for each journal file-handle
   42619                 :   ** (there are two of them, the main journal and the sub-journal). This
   42620                 :   ** is the maximum space required for an in-memory journal file handle 
   42621                 :   ** and a regular journal file-handle. Note that a "regular journal-handle"
   42622                 :   ** may be a wrapper capable of caching the first portion of the journal
   42623                 :   ** file in memory to implement the atomic-write optimization (see 
   42624                 :   ** source file journal.c).
   42625                 :   */
   42626           19521 :   if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
   42627           19521 :     journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
   42628                 :   }else{
   42629               0 :     journalFileSize = ROUND8(sqlite3MemJournalSize());
   42630                 :   }
   42631                 : 
   42632                 :   /* Set the output variable to NULL in case an error occurs. */
   42633           19521 :   *ppPager = 0;
   42634                 : 
   42635                 : #ifndef SQLITE_OMIT_MEMORYDB
   42636           19521 :   if( flags & PAGER_MEMORY ){
   42637           15458 :     memDb = 1;
   42638           15458 :     zFilename = 0;
   42639                 :   }
   42640                 : #endif
   42641                 : 
   42642                 :   /* Compute and store the full pathname in an allocated buffer pointed
   42643                 :   ** to by zPathname, length nPathname. Or, if this is a temporary file,
   42644                 :   ** leave both nPathname and zPathname set to 0.
   42645                 :   */
   42646           19521 :   if( zFilename && zFilename[0] ){
   42647                 :     const char *z;
   42648            3056 :     nPathname = pVfs->mxPathname+1;
   42649            3056 :     zPathname = sqlite3Malloc(nPathname*2);
   42650            3056 :     if( zPathname==0 ){
   42651               0 :       return SQLITE_NOMEM;
   42652                 :     }
   42653            3056 :     zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
   42654            3056 :     rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
   42655            3056 :     nPathname = sqlite3Strlen30(zPathname);
   42656            3056 :     z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
   42657            6112 :     while( *z ){
   42658               0 :       z += sqlite3Strlen30(z)+1;
   42659               0 :       z += sqlite3Strlen30(z)+1;
   42660                 :     }
   42661            3056 :     nUri = (int)(&z[1] - zUri);
   42662            3056 :     assert( nUri>=0 );
   42663            3056 :     if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
   42664                 :       /* This branch is taken when the journal path required by
   42665                 :       ** the database being opened will be more than pVfs->mxPathname
   42666                 :       ** bytes in length. This means the database cannot be opened,
   42667                 :       ** as it will not be possible to open the journal file or even
   42668                 :       ** check for a hot-journal before reading.
   42669                 :       */
   42670               0 :       rc = SQLITE_CANTOPEN_BKPT;
   42671                 :     }
   42672            3056 :     if( rc!=SQLITE_OK ){
   42673               0 :       sqlite3_free(zPathname);
   42674               0 :       return rc;
   42675                 :     }
   42676                 :   }
   42677                 : 
   42678                 :   /* Allocate memory for the Pager structure, PCache object, the
   42679                 :   ** three file descriptors, the database file name and the journal 
   42680                 :   ** file name. The layout in memory is as follows:
   42681                 :   **
   42682                 :   **     Pager object                    (sizeof(Pager) bytes)
   42683                 :   **     PCache object                   (sqlite3PcacheSize() bytes)
   42684                 :   **     Database file handle            (pVfs->szOsFile bytes)
   42685                 :   **     Sub-journal file handle         (journalFileSize bytes)
   42686                 :   **     Main journal file handle        (journalFileSize bytes)
   42687                 :   **     Database file name              (nPathname+1 bytes)
   42688                 :   **     Journal file name               (nPathname+8+1 bytes)
   42689                 :   */
   42690           19521 :   pPtr = (u8 *)sqlite3MallocZero(
   42691                 :     ROUND8(sizeof(*pPager)) +      /* Pager structure */
   42692           39042 :     ROUND8(pcacheSize) +           /* PCache object */
   42693           39042 :     ROUND8(pVfs->szOsFile) +       /* The main db file */
   42694           39042 :     journalFileSize * 2 +          /* The two journal files */ 
   42695           19521 :     nPathname + 1 + nUri +         /* zFilename */
   42696                 :     nPathname + 8 + 2              /* zJournal */
   42697                 : #ifndef SQLITE_OMIT_WAL
   42698           19521 :     + nPathname + 4 + 2            /* zWal */
   42699                 : #endif
   42700                 :   );
   42701           19521 :   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
   42702           19521 :   if( !pPtr ){
   42703               0 :     sqlite3_free(zPathname);
   42704               0 :     return SQLITE_NOMEM;
   42705                 :   }
   42706           19521 :   pPager =              (Pager*)(pPtr);
   42707           19521 :   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
   42708           19521 :   pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
   42709           19521 :   pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
   42710           19521 :   pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
   42711           19521 :   pPager->zFilename =    (char*)(pPtr += journalFileSize);
   42712           19521 :   assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
   42713                 : 
   42714                 :   /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
   42715           19521 :   if( zPathname ){
   42716            3056 :     assert( nPathname>0 );
   42717            3056 :     pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
   42718            3056 :     memcpy(pPager->zFilename, zPathname, nPathname);
   42719            3056 :     memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
   42720            3056 :     memcpy(pPager->zJournal, zPathname, nPathname);
   42721            3056 :     memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
   42722                 :     sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
   42723                 : #ifndef SQLITE_OMIT_WAL
   42724            3056 :     pPager->zWal = &pPager->zJournal[nPathname+8+1];
   42725            3056 :     memcpy(pPager->zWal, zPathname, nPathname);
   42726            3056 :     memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
   42727                 :     sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
   42728                 : #endif
   42729            3056 :     sqlite3_free(zPathname);
   42730                 :   }
   42731           19521 :   pPager->pVfs = pVfs;
   42732           19521 :   pPager->vfsFlags = vfsFlags;
   42733                 : 
   42734                 :   /* Open the pager file.
   42735                 :   */
   42736           22577 :   if( zFilename && zFilename[0] ){
   42737            3056 :     int fout = 0;                    /* VFS flags returned by xOpen() */
   42738            3056 :     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
   42739            3056 :     assert( !memDb );
   42740            3056 :     readOnly = (fout&SQLITE_OPEN_READONLY);
   42741                 : 
   42742                 :     /* If the file was successfully opened for read/write access,
   42743                 :     ** choose a default page size in case we have to create the
   42744                 :     ** database file. The default page size is the maximum of:
   42745                 :     **
   42746                 :     **    + SQLITE_DEFAULT_PAGE_SIZE,
   42747                 :     **    + The value returned by sqlite3OsSectorSize()
   42748                 :     **    + The largest page size that can be written atomically.
   42749                 :     */
   42750            3056 :     if( rc==SQLITE_OK && !readOnly ){
   42751            3016 :       setSectorSize(pPager);
   42752                 :       assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
   42753            3016 :       if( szPageDflt<pPager->sectorSize ){
   42754               0 :         if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
   42755               0 :           szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
   42756                 :         }else{
   42757               0 :           szPageDflt = (u32)pPager->sectorSize;
   42758                 :         }
   42759                 :       }
   42760                 : #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   42761                 :       {
   42762                 :         int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
   42763                 :         int ii;
   42764                 :         assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
   42765                 :         assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
   42766                 :         assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
   42767                 :         for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
   42768                 :           if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
   42769                 :             szPageDflt = ii;
   42770                 :           }
   42771                 :         }
   42772                 :       }
   42773                 : #endif
   42774                 :     }
   42775                 :   }else{
   42776                 :     /* If a temporary file is requested, it is not opened immediately.
   42777                 :     ** In this case we accept the default page size and delay actually
   42778                 :     ** opening the file until the first call to OsWrite().
   42779                 :     **
   42780                 :     ** This branch is also run for an in-memory database. An in-memory
   42781                 :     ** database is the same as a temp-file that is never written out to
   42782                 :     ** disk and uses an in-memory rollback journal.
   42783                 :     */ 
   42784           16465 :     tempFile = 1;
   42785           16465 :     pPager->eState = PAGER_READER;
   42786           16465 :     pPager->eLock = EXCLUSIVE_LOCK;
   42787           16465 :     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
   42788                 :   }
   42789                 : 
   42790                 :   /* The following call to PagerSetPagesize() serves to set the value of 
   42791                 :   ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
   42792                 :   */
   42793           19521 :   if( rc==SQLITE_OK ){
   42794           19517 :     assert( pPager->memDb==0 );
   42795           19517 :     rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
   42796                 :     testcase( rc!=SQLITE_OK );
   42797                 :   }
   42798                 : 
   42799                 :   /* If an error occurred in either of the blocks above, free the 
   42800                 :   ** Pager structure and close the file.
   42801                 :   */
   42802           19521 :   if( rc!=SQLITE_OK ){
   42803               4 :     assert( !pPager->pTmpSpace );
   42804               4 :     sqlite3OsClose(pPager->fd);
   42805               4 :     sqlite3_free(pPager);
   42806               4 :     return rc;
   42807                 :   }
   42808                 : 
   42809                 :   /* Initialize the PCache object. */
   42810           19517 :   assert( nExtra<1000 );
   42811           19517 :   nExtra = ROUND8(nExtra);
   42812           19517 :   sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
   42813                 :                     !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
   42814                 : 
   42815                 :   PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
   42816                 :   IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
   42817                 : 
   42818           19517 :   pPager->useJournal = (u8)useJournal;
   42819           19517 :   pPager->noReadlock = (noReadlock && readOnly) ?1:0;
   42820                 :   /* pPager->stmtOpen = 0; */
   42821                 :   /* pPager->stmtInUse = 0; */
   42822                 :   /* pPager->nRef = 0; */
   42823                 :   /* pPager->stmtSize = 0; */
   42824                 :   /* pPager->stmtJSize = 0; */
   42825                 :   /* pPager->nPage = 0; */
   42826           19517 :   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
   42827                 :   /* pPager->state = PAGER_UNLOCK; */
   42828                 : #if 0
   42829                 :   assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
   42830                 : #endif
   42831                 :   /* pPager->errMask = 0; */
   42832           19517 :   pPager->tempFile = (u8)tempFile;
   42833           19517 :   assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
   42834                 :           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
   42835                 :   assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
   42836           19517 :   pPager->exclusiveMode = (u8)tempFile; 
   42837           19517 :   pPager->changeCountDone = pPager->tempFile;
   42838           19517 :   pPager->memDb = (u8)memDb;
   42839           19517 :   pPager->readOnly = (u8)readOnly;
   42840           19517 :   assert( useJournal || pPager->tempFile );
   42841           19517 :   pPager->noSync = pPager->tempFile;
   42842           19517 :   if( pPager->noSync ){
   42843           16465 :     assert( pPager->fullSync==0 );
   42844           16465 :     assert( pPager->syncFlags==0 );
   42845           16465 :     assert( pPager->walSyncFlags==0 );
   42846           16465 :     assert( pPager->ckptSyncFlags==0 );
   42847                 :   }else{
   42848            3052 :     pPager->fullSync = 1;
   42849            3052 :     pPager->syncFlags = SQLITE_SYNC_NORMAL;
   42850            3052 :     pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
   42851            3052 :     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
   42852                 :   }
   42853                 :   /* pPager->pFirst = 0; */
   42854                 :   /* pPager->pFirstSynced = 0; */
   42855                 :   /* pPager->pLast = 0; */
   42856           19517 :   pPager->nExtra = (u16)nExtra;
   42857           19517 :   pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
   42858           19517 :   assert( isOpen(pPager->fd) || tempFile );
   42859           19517 :   setSectorSize(pPager);
   42860           19517 :   if( !useJournal ){
   42861           16027 :     pPager->journalMode = PAGER_JOURNALMODE_OFF;
   42862            3490 :   }else if( memDb ){
   42863             417 :     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
   42864                 :   }
   42865                 :   /* pPager->xBusyHandler = 0; */
   42866                 :   /* pPager->pBusyHandlerArg = 0; */
   42867           19517 :   pPager->xReiniter = xReinit;
   42868                 :   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
   42869                 : 
   42870           19517 :   *ppPager = pPager;
   42871           19517 :   return SQLITE_OK;
   42872                 : }
   42873                 : 
   42874                 : 
   42875                 : 
   42876                 : /*
   42877                 : ** This function is called after transitioning from PAGER_UNLOCK to
   42878                 : ** PAGER_SHARED state. It tests if there is a hot journal present in
   42879                 : ** the file-system for the given pager. A hot journal is one that 
   42880                 : ** needs to be played back. According to this function, a hot-journal
   42881                 : ** file exists if the following criteria are met:
   42882                 : **
   42883                 : **   * The journal file exists in the file system, and
   42884                 : **   * No process holds a RESERVED or greater lock on the database file, and
   42885                 : **   * The database file itself is greater than 0 bytes in size, and
   42886                 : **   * The first byte of the journal file exists and is not 0x00.
   42887                 : **
   42888                 : ** If the current size of the database file is 0 but a journal file
   42889                 : ** exists, that is probably an old journal left over from a prior
   42890                 : ** database with the same name. In this case the journal file is
   42891                 : ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
   42892                 : ** is returned.
   42893                 : **
   42894                 : ** This routine does not check if there is a master journal filename
   42895                 : ** at the end of the file. If there is, and that master journal file
   42896                 : ** does not exist, then the journal file is not really hot. In this
   42897                 : ** case this routine will return a false-positive. The pager_playback()
   42898                 : ** routine will discover that the journal file is not really hot and 
   42899                 : ** will not roll it back. 
   42900                 : **
   42901                 : ** If a hot-journal file is found to exist, *pExists is set to 1 and 
   42902                 : ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
   42903                 : ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
   42904                 : ** to determine whether or not a hot-journal file exists, the IO error
   42905                 : ** code is returned and the value of *pExists is undefined.
   42906                 : */
   42907           16151 : static int hasHotJournal(Pager *pPager, int *pExists){
   42908           16151 :   sqlite3_vfs * const pVfs = pPager->pVfs;
   42909           16151 :   int rc = SQLITE_OK;           /* Return code */
   42910           16151 :   int exists = 1;               /* True if a journal file is present */
   42911           16151 :   int jrnlOpen = !!isOpen(pPager->jfd);
   42912                 : 
   42913           16151 :   assert( pPager->useJournal );
   42914           16151 :   assert( isOpen(pPager->fd) );
   42915           16151 :   assert( pPager->eState==PAGER_OPEN );
   42916                 : 
   42917           16151 :   assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
   42918                 :     SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
   42919                 :   ));
   42920                 : 
   42921           16151 :   *pExists = 0;
   42922           16151 :   if( !jrnlOpen ){
   42923           16151 :     rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
   42924                 :   }
   42925           16151 :   if( rc==SQLITE_OK && exists ){
   42926               0 :     int locked = 0;             /* True if some process holds a RESERVED lock */
   42927                 : 
   42928                 :     /* Race condition here:  Another process might have been holding the
   42929                 :     ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 
   42930                 :     ** call above, but then delete the journal and drop the lock before
   42931                 :     ** we get to the following sqlite3OsCheckReservedLock() call.  If that
   42932                 :     ** is the case, this routine might think there is a hot journal when
   42933                 :     ** in fact there is none.  This results in a false-positive which will
   42934                 :     ** be dealt with by the playback routine.  Ticket #3883.
   42935                 :     */
   42936               0 :     rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
   42937               0 :     if( rc==SQLITE_OK && !locked ){
   42938                 :       Pgno nPage;                 /* Number of pages in database file */
   42939                 : 
   42940                 :       /* Check the size of the database file. If it consists of 0 pages,
   42941                 :       ** then delete the journal file. See the header comment above for 
   42942                 :       ** the reasoning here.  Delete the obsolete journal file under
   42943                 :       ** a RESERVED lock to avoid race conditions and to avoid violating
   42944                 :       ** [H33020].
   42945                 :       */
   42946               0 :       rc = pagerPagecount(pPager, &nPage);
   42947               0 :       if( rc==SQLITE_OK ){
   42948               0 :         if( nPage==0 ){
   42949               0 :           sqlite3BeginBenignMalloc();
   42950               0 :           if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
   42951               0 :             sqlite3OsDelete(pVfs, pPager->zJournal, 0);
   42952               0 :             if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
   42953                 :           }
   42954               0 :           sqlite3EndBenignMalloc();
   42955                 :         }else{
   42956                 :           /* The journal file exists and no other connection has a reserved
   42957                 :           ** or greater lock on the database file. Now check that there is
   42958                 :           ** at least one non-zero bytes at the start of the journal file.
   42959                 :           ** If there is, then we consider this journal to be hot. If not, 
   42960                 :           ** it can be ignored.
   42961                 :           */
   42962               0 :           if( !jrnlOpen ){
   42963               0 :             int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
   42964               0 :             rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
   42965                 :           }
   42966               0 :           if( rc==SQLITE_OK ){
   42967               0 :             u8 first = 0;
   42968               0 :             rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
   42969               0 :             if( rc==SQLITE_IOERR_SHORT_READ ){
   42970               0 :               rc = SQLITE_OK;
   42971                 :             }
   42972               0 :             if( !jrnlOpen ){
   42973               0 :               sqlite3OsClose(pPager->jfd);
   42974                 :             }
   42975               0 :             *pExists = (first!=0);
   42976               0 :           }else if( rc==SQLITE_CANTOPEN ){
   42977                 :             /* If we cannot open the rollback journal file in order to see if
   42978                 :             ** its has a zero header, that might be due to an I/O error, or
   42979                 :             ** it might be due to the race condition described above and in
   42980                 :             ** ticket #3883.  Either way, assume that the journal is hot.
   42981                 :             ** This might be a false positive.  But if it is, then the
   42982                 :             ** automatic journal playback and recovery mechanism will deal
   42983                 :             ** with it under an EXCLUSIVE lock where we do not need to
   42984                 :             ** worry so much with race conditions.
   42985                 :             */
   42986               0 :             *pExists = 1;
   42987               0 :             rc = SQLITE_OK;
   42988                 :           }
   42989                 :         }
   42990                 :       }
   42991                 :     }
   42992                 :   }
   42993                 : 
   42994           16151 :   return rc;
   42995                 : }
   42996                 : 
   42997                 : /*
   42998                 : ** This function is called to obtain a shared lock on the database file.
   42999                 : ** It is illegal to call sqlite3PagerAcquire() until after this function
   43000                 : ** has been successfully called. If a shared-lock is already held when
   43001                 : ** this function is called, it is a no-op.
   43002                 : **
   43003                 : ** The following operations are also performed by this function.
   43004                 : **
   43005                 : **   1) If the pager is currently in PAGER_OPEN state (no lock held
   43006                 : **      on the database file), then an attempt is made to obtain a
   43007                 : **      SHARED lock on the database file. Immediately after obtaining
   43008                 : **      the SHARED lock, the file-system is checked for a hot-journal,
   43009                 : **      which is played back if present. Following any hot-journal 
   43010                 : **      rollback, the contents of the cache are validated by checking
   43011                 : **      the 'change-counter' field of the database file header and
   43012                 : **      discarded if they are found to be invalid.
   43013                 : **
   43014                 : **   2) If the pager is running in exclusive-mode, and there are currently
   43015                 : **      no outstanding references to any pages, and is in the error state,
   43016                 : **      then an attempt is made to clear the error state by discarding
   43017                 : **      the contents of the page cache and rolling back any open journal
   43018                 : **      file.
   43019                 : **
   43020                 : ** If everything is successful, SQLITE_OK is returned. If an IO error 
   43021                 : ** occurs while locking the database, checking for a hot-journal file or 
   43022                 : ** rolling back a journal file, the IO error code is returned.
   43023                 : */
   43024          100213 : SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
   43025          100213 :   int rc = SQLITE_OK;                /* Return code */
   43026                 : 
   43027                 :   /* This routine is only called from b-tree and only when there are no
   43028                 :   ** outstanding pages. This implies that the pager state should either
   43029                 :   ** be OPEN or READER. READER is only possible if the pager is or was in 
   43030                 :   ** exclusive access mode.
   43031                 :   */
   43032          100213 :   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
   43033          100213 :   assert( assert_pager_state(pPager) );
   43034          100209 :   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
   43035          100209 :   if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
   43036                 : 
   43037          100209 :   if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
   43038           16152 :     int bHotJournal = 1;          /* True if there exists a hot journal-file */
   43039                 : 
   43040           16152 :     assert( !MEMDB );
   43041           16152 :     assert( pPager->noReadlock==0 || pPager->readOnly );
   43042                 : 
   43043           16152 :     if( pPager->noReadlock==0 ){
   43044           16152 :       rc = pager_wait_on_lock(pPager, SHARED_LOCK);
   43045           16152 :       if( rc!=SQLITE_OK ){
   43046               1 :         assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
   43047               1 :         goto failed;
   43048                 :       }
   43049                 :     }
   43050                 : 
   43051                 :     /* If a journal file exists, and there is no RESERVED lock on the
   43052                 :     ** database file, then it either needs to be played back or deleted.
   43053                 :     */
   43054           16151 :     if( pPager->eLock<=SHARED_LOCK ){
   43055           16151 :       rc = hasHotJournal(pPager, &bHotJournal);
   43056                 :     }
   43057           16151 :     if( rc!=SQLITE_OK ){
   43058               0 :       goto failed;
   43059                 :     }
   43060           16151 :     if( bHotJournal ){
   43061                 :       /* Get an EXCLUSIVE lock on the database file. At this point it is
   43062                 :       ** important that a RESERVED lock is not obtained on the way to the
   43063                 :       ** EXCLUSIVE lock. If it were, another process might open the
   43064                 :       ** database file, detect the RESERVED lock, and conclude that the
   43065                 :       ** database is safe to read while this process is still rolling the 
   43066                 :       ** hot-journal back.
   43067                 :       ** 
   43068                 :       ** Because the intermediate RESERVED lock is not requested, any
   43069                 :       ** other process attempting to access the database file will get to 
   43070                 :       ** this point in the code and fail to obtain its own EXCLUSIVE lock 
   43071                 :       ** on the database file.
   43072                 :       **
   43073                 :       ** Unless the pager is in locking_mode=exclusive mode, the lock is
   43074                 :       ** downgraded to SHARED_LOCK before this function returns.
   43075                 :       */
   43076               0 :       rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
   43077               0 :       if( rc!=SQLITE_OK ){
   43078               0 :         goto failed;
   43079                 :       }
   43080                 :  
   43081                 :       /* If it is not already open and the file exists on disk, open the 
   43082                 :       ** journal for read/write access. Write access is required because 
   43083                 :       ** in exclusive-access mode the file descriptor will be kept open 
   43084                 :       ** and possibly used for a transaction later on. Also, write-access 
   43085                 :       ** is usually required to finalize the journal in journal_mode=persist 
   43086                 :       ** mode (and also for journal_mode=truncate on some systems).
   43087                 :       **
   43088                 :       ** If the journal does not exist, it usually means that some 
   43089                 :       ** other connection managed to get in and roll it back before 
   43090                 :       ** this connection obtained the exclusive lock above. Or, it 
   43091                 :       ** may mean that the pager was in the error-state when this
   43092                 :       ** function was called and the journal file does not exist.
   43093                 :       */
   43094               0 :       if( !isOpen(pPager->jfd) ){
   43095               0 :         sqlite3_vfs * const pVfs = pPager->pVfs;
   43096                 :         int bExists;              /* True if journal file exists */
   43097               0 :         rc = sqlite3OsAccess(
   43098               0 :             pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
   43099               0 :         if( rc==SQLITE_OK && bExists ){
   43100               0 :           int fout = 0;
   43101               0 :           int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
   43102               0 :           assert( !pPager->tempFile );
   43103               0 :           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
   43104               0 :           assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
   43105               0 :           if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
   43106               0 :             rc = SQLITE_CANTOPEN_BKPT;
   43107               0 :             sqlite3OsClose(pPager->jfd);
   43108                 :           }
   43109                 :         }
   43110                 :       }
   43111                 :  
   43112                 :       /* Playback and delete the journal.  Drop the database write
   43113                 :       ** lock and reacquire the read lock. Purge the cache before
   43114                 :       ** playing back the hot-journal so that we don't end up with
   43115                 :       ** an inconsistent cache.  Sync the hot journal before playing
   43116                 :       ** it back since the process that crashed and left the hot journal
   43117                 :       ** probably did not sync it and we are required to always sync
   43118                 :       ** the journal before playing it back.
   43119                 :       */
   43120               0 :       if( isOpen(pPager->jfd) ){
   43121               0 :         assert( rc==SQLITE_OK );
   43122               0 :         rc = pagerSyncHotJournal(pPager);
   43123               0 :         if( rc==SQLITE_OK ){
   43124               0 :           rc = pager_playback(pPager, 1);
   43125               0 :           pPager->eState = PAGER_OPEN;
   43126                 :         }
   43127               0 :       }else if( !pPager->exclusiveMode ){
   43128               0 :         pagerUnlockDb(pPager, SHARED_LOCK);
   43129                 :       }
   43130                 : 
   43131               0 :       if( rc!=SQLITE_OK ){
   43132                 :         /* This branch is taken if an error occurs while trying to open
   43133                 :         ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
   43134                 :         ** pager_unlock() routine will be called before returning to unlock
   43135                 :         ** the file. If the unlock attempt fails, then Pager.eLock must be
   43136                 :         ** set to UNKNOWN_LOCK (see the comment above the #define for 
   43137                 :         ** UNKNOWN_LOCK above for an explanation). 
   43138                 :         **
   43139                 :         ** In order to get pager_unlock() to do this, set Pager.eState to
   43140                 :         ** PAGER_ERROR now. This is not actually counted as a transition
   43141                 :         ** to ERROR state in the state diagram at the top of this file,
   43142                 :         ** since we know that the same call to pager_unlock() will very
   43143                 :         ** shortly transition the pager object to the OPEN state. Calling
   43144                 :         ** assert_pager_state() would fail now, as it should not be possible
   43145                 :         ** to be in ERROR state when there are zero outstanding page 
   43146                 :         ** references.
   43147                 :         */
   43148               0 :         pager_error(pPager, rc);
   43149               0 :         goto failed;
   43150                 :       }
   43151                 : 
   43152               0 :       assert( pPager->eState==PAGER_OPEN );
   43153               0 :       assert( (pPager->eLock==SHARED_LOCK)
   43154                 :            || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
   43155                 :       );
   43156                 :     }
   43157                 : 
   43158           16151 :     if( !pPager->tempFile 
   43159           16151 :      && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0) 
   43160                 :     ){
   43161                 :       /* The shared-lock has just been acquired on the database file
   43162                 :       ** and there are already pages in the cache (from a previous
   43163                 :       ** read or write transaction).  Check to see if the database
   43164                 :       ** has been modified.  If the database has changed, flush the
   43165                 :       ** cache.
   43166                 :       **
   43167                 :       ** Database changes is detected by looking at 15 bytes beginning
   43168                 :       ** at offset 24 into the file.  The first 4 of these 16 bytes are
   43169                 :       ** a 32-bit counter that is incremented with each change.  The
   43170                 :       ** other bytes change randomly with each file change when
   43171                 :       ** a codec is in use.
   43172                 :       ** 
   43173                 :       ** There is a vanishingly small chance that a change will not be 
   43174                 :       ** detected.  The chance of an undetected change is so small that
   43175                 :       ** it can be neglected.
   43176                 :       */
   43177           13094 :       Pgno nPage = 0;
   43178                 :       char dbFileVers[sizeof(pPager->dbFileVers)];
   43179                 : 
   43180           13094 :       rc = pagerPagecount(pPager, &nPage);
   43181           13094 :       if( rc ) goto failed;
   43182                 : 
   43183           13094 :       if( nPage>0 ){
   43184                 :         IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
   43185           11059 :         rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
   43186           11059 :         if( rc!=SQLITE_OK ){
   43187               0 :           goto failed;
   43188                 :         }
   43189                 :       }else{
   43190            2035 :         memset(dbFileVers, 0, sizeof(dbFileVers));
   43191                 :       }
   43192                 : 
   43193           13094 :       if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
   43194               1 :         pager_reset(pPager);
   43195                 :       }
   43196                 :     }
   43197                 : 
   43198                 :     /* If there is a WAL file in the file-system, open this database in WAL
   43199                 :     ** mode. Otherwise, the following function call is a no-op.
   43200                 :     */
   43201           16151 :     rc = pagerOpenWalIfPresent(pPager);
   43202                 : #ifndef SQLITE_OMIT_WAL
   43203           16151 :     assert( pPager->pWal==0 || rc==SQLITE_OK );
   43204                 : #endif
   43205                 :   }
   43206                 : 
   43207          100212 :   if( pagerUseWal(pPager) ){
   43208           40098 :     assert( rc==SQLITE_OK );
   43209           40098 :     rc = pagerBeginReadTransaction(pPager);
   43210                 :   }
   43211                 : 
   43212          100212 :   if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
   43213           56216 :     rc = pagerPagecount(pPager, &pPager->dbSize);
   43214                 :   }
   43215                 : 
   43216                 :  failed:
   43217          100213 :   if( rc!=SQLITE_OK ){
   43218               1 :     assert( !MEMDB );
   43219               1 :     pager_unlock(pPager);
   43220               1 :     assert( pPager->eState==PAGER_OPEN );
   43221                 :   }else{
   43222          100212 :     pPager->eState = PAGER_READER;
   43223                 :   }
   43224          100213 :   return rc;
   43225                 : }
   43226                 : 
   43227                 : /*
   43228                 : ** If the reference count has reached zero, rollback any active
   43229                 : ** transaction and unlock the pager.
   43230                 : **
   43231                 : ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
   43232                 : ** the rollback journal, the unlock is not performed and there is
   43233                 : ** nothing to rollback, so this routine is a no-op.
   43234                 : */ 
   43235          639744 : static void pagerUnlockIfUnused(Pager *pPager){
   43236          639744 :   if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
   43237          100121 :     pagerUnlockAndRollback(pPager);
   43238                 :   }
   43239          639744 : }
   43240                 : 
   43241                 : /*
   43242                 : ** Acquire a reference to page number pgno in pager pPager (a page
   43243                 : ** reference has type DbPage*). If the requested reference is 
   43244                 : ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
   43245                 : **
   43246                 : ** If the requested page is already in the cache, it is returned. 
   43247                 : ** Otherwise, a new page object is allocated and populated with data
   43248                 : ** read from the database file. In some cases, the pcache module may
   43249                 : ** choose not to allocate a new page object and may reuse an existing
   43250                 : ** object with no outstanding references.
   43251                 : **
   43252                 : ** The extra data appended to a page is always initialized to zeros the 
   43253                 : ** first time a page is loaded into memory. If the page requested is 
   43254                 : ** already in the cache when this function is called, then the extra
   43255                 : ** data is left as it was when the page object was last used.
   43256                 : **
   43257                 : ** If the database image is smaller than the requested page or if a 
   43258                 : ** non-zero value is passed as the noContent parameter and the 
   43259                 : ** requested page is not already stored in the cache, then no 
   43260                 : ** actual disk read occurs. In this case the memory image of the 
   43261                 : ** page is initialized to all zeros. 
   43262                 : **
   43263                 : ** If noContent is true, it means that we do not care about the contents
   43264                 : ** of the page. This occurs in two seperate scenarios:
   43265                 : **
   43266                 : **   a) When reading a free-list leaf page from the database, and
   43267                 : **
   43268                 : **   b) When a savepoint is being rolled back and we need to load
   43269                 : **      a new page into the cache to be filled with the data read
   43270                 : **      from the savepoint journal.
   43271                 : **
   43272                 : ** If noContent is true, then the data returned is zeroed instead of
   43273                 : ** being read from the database. Additionally, the bits corresponding
   43274                 : ** to pgno in Pager.pInJournal (bitvec of pages already written to the
   43275                 : ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
   43276                 : ** savepoints are set. This means if the page is made writable at any
   43277                 : ** point in the future, using a call to sqlite3PagerWrite(), its contents
   43278                 : ** will not be journaled. This saves IO.
   43279                 : **
   43280                 : ** The acquisition might fail for several reasons.  In all cases,
   43281                 : ** an appropriate error code is returned and *ppPage is set to NULL.
   43282                 : **
   43283                 : ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
   43284                 : ** to find a page in the in-memory cache first.  If the page is not already
   43285                 : ** in memory, this routine goes to disk to read it in whereas Lookup()
   43286                 : ** just returns 0.  This routine acquires a read-lock the first time it
   43287                 : ** has to go to disk, and could also playback an old journal if necessary.
   43288                 : ** Since Lookup() never goes to disk, it never has to deal with locks
   43289                 : ** or journal files.
   43290                 : */
   43291          639742 : SQLITE_PRIVATE int sqlite3PagerAcquire(
   43292                 :   Pager *pPager,      /* The pager open on the database file */
   43293                 :   Pgno pgno,          /* Page number to fetch */
   43294                 :   DbPage **ppPage,    /* Write a pointer to the page here */
   43295                 :   int noContent       /* Do not bother reading content from disk if true */
   43296                 : ){
   43297                 :   int rc;
   43298                 :   PgHdr *pPg;
   43299                 : 
   43300          639742 :   assert( pPager->eState>=PAGER_READER );
   43301          639742 :   assert( assert_pager_state(pPager) );
   43302                 : 
   43303          639742 :   if( pgno==0 ){
   43304               0 :     return SQLITE_CORRUPT_BKPT;
   43305                 :   }
   43306                 : 
   43307                 :   /* If the pager is in the error state, return an error immediately. 
   43308                 :   ** Otherwise, request the page from the PCache layer. */
   43309          639742 :   if( pPager->errCode!=SQLITE_OK ){
   43310             986 :     rc = pPager->errCode;
   43311                 :   }else{
   43312          638756 :     rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
   43313                 :   }
   43314                 : 
   43315          639742 :   if( rc!=SQLITE_OK ){
   43316                 :     /* Either the call to sqlite3PcacheFetch() returned an error or the
   43317                 :     ** pager was already in the error-state when this function was called.
   43318                 :     ** Set pPg to 0 and jump to the exception handler.  */
   43319             986 :     pPg = 0;
   43320             986 :     goto pager_acquire_err;
   43321                 :   }
   43322          638756 :   assert( (*ppPage)->pgno==pgno );
   43323          638756 :   assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
   43324                 : 
   43325          638756 :   if( (*ppPage)->pPager && !noContent ){
   43326                 :     /* In this case the pcache already contains an initialized copy of
   43327                 :     ** the page. Return without further ado.  */
   43328          577699 :     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
   43329          577699 :     pPager->nHit++;
   43330          577699 :     return SQLITE_OK;
   43331                 : 
   43332                 :   }else{
   43333                 :     /* The pager cache has created a new page. Its content needs to 
   43334                 :     ** be initialized.  */
   43335                 : 
   43336           61057 :     pPg = *ppPage;
   43337           61057 :     pPg->pPager = pPager;
   43338                 : 
   43339                 :     /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
   43340                 :     ** number greater than this, or the unused locking-page, is requested. */
   43341           61057 :     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
   43342               0 :       rc = SQLITE_CORRUPT_BKPT;
   43343               0 :       goto pager_acquire_err;
   43344                 :     }
   43345                 : 
   43346           61057 :     if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
   43347           48665 :       if( pgno>pPager->mxPgno ){
   43348               0 :         rc = SQLITE_FULL;
   43349               0 :         goto pager_acquire_err;
   43350                 :       }
   43351           48665 :       if( noContent ){
   43352                 :         /* Failure to set the bits in the InJournal bit-vectors is benign.
   43353                 :         ** It merely means that we might do some extra work to journal a 
   43354                 :         ** page that does not need to be journaled.  Nevertheless, be sure 
   43355                 :         ** to test the case where a malloc error occurs while trying to set 
   43356                 :         ** a bit in a bit vector.
   43357                 :         */
   43358           30638 :         sqlite3BeginBenignMalloc();
   43359           30638 :         if( pgno<=pPager->dbOrigSize ){
   43360            1095 :           TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
   43361                 :           testcase( rc==SQLITE_NOMEM );
   43362                 :         }
   43363           30638 :         TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
   43364                 :         testcase( rc==SQLITE_NOMEM );
   43365           30638 :         sqlite3EndBenignMalloc();
   43366                 :       }
   43367           48665 :       memset(pPg->pData, 0, pPager->pageSize);
   43368                 :       IOTRACE(("ZERO %p %d\n", pPager, pgno));
   43369                 :     }else{
   43370           12392 :       assert( pPg->pPager==pPager );
   43371           12392 :       pPager->nMiss++;
   43372           12392 :       rc = readDbPage(pPg);
   43373           12392 :       if( rc!=SQLITE_OK ){
   43374               0 :         goto pager_acquire_err;
   43375                 :       }
   43376                 :     }
   43377                 :     pager_set_pagehash(pPg);
   43378                 :   }
   43379                 : 
   43380           61057 :   return SQLITE_OK;
   43381                 : 
   43382                 : pager_acquire_err:
   43383             986 :   assert( rc!=SQLITE_OK );
   43384             986 :   if( pPg ){
   43385               0 :     sqlite3PcacheDrop(pPg);
   43386                 :   }
   43387             986 :   pagerUnlockIfUnused(pPager);
   43388                 : 
   43389             986 :   *ppPage = 0;
   43390             986 :   return rc;
   43391                 : }
   43392                 : 
   43393                 : /*
   43394                 : ** Acquire a page if it is already in the in-memory cache.  Do
   43395                 : ** not read the page from disk.  Return a pointer to the page,
   43396                 : ** or 0 if the page is not in cache. 
   43397                 : **
   43398                 : ** See also sqlite3PagerGet().  The difference between this routine
   43399                 : ** and sqlite3PagerGet() is that _get() will go to the disk and read
   43400                 : ** in the page if the page is not already in cache.  This routine
   43401                 : ** returns NULL if the page is not in cache or if a disk I/O error 
   43402                 : ** has ever happened.
   43403                 : */
   43404              11 : SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
   43405              11 :   PgHdr *pPg = 0;
   43406              11 :   assert( pPager!=0 );
   43407              11 :   assert( pgno!=0 );
   43408              11 :   assert( pPager->pPCache!=0 );
   43409              11 :   assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
   43410              11 :   sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
   43411              11 :   return pPg;
   43412                 : }
   43413                 : 
   43414                 : /*
   43415                 : ** Release a page reference.
   43416                 : **
   43417                 : ** If the number of references to the page drop to zero, then the
   43418                 : ** page is added to the LRU list.  When all references to all pages
   43419                 : ** are released, a rollback occurs and the lock on the database is
   43420                 : ** removed.
   43421                 : */
   43422          663798 : SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
   43423          663798 :   if( pPg ){
   43424          638758 :     Pager *pPager = pPg->pPager;
   43425          638758 :     sqlite3PcacheRelease(pPg);
   43426          638758 :     pagerUnlockIfUnused(pPager);
   43427                 :   }
   43428          663798 : }
   43429                 : 
   43430                 : /*
   43431                 : ** This function is called at the start of every write transaction.
   43432                 : ** There must already be a RESERVED or EXCLUSIVE lock on the database 
   43433                 : ** file when this routine is called.
   43434                 : **
   43435                 : ** Open the journal file for pager pPager and write a journal header
   43436                 : ** to the start of it. If there are active savepoints, open the sub-journal
   43437                 : ** as well. This function is only used when the journal file is being 
   43438                 : ** opened to write a rollback log for a transaction. It is not used 
   43439                 : ** when opening a hot journal file to roll it back.
   43440                 : **
   43441                 : ** If the journal file is already open (as it may be in exclusive mode),
   43442                 : ** then this function just writes a journal header to the start of the
   43443                 : ** already open file. 
   43444                 : **
   43445                 : ** Whether or not the journal file is opened by this function, the
   43446                 : ** Pager.pInJournal bitvec structure is allocated.
   43447                 : **
   43448                 : ** Return SQLITE_OK if everything is successful. Otherwise, return 
   43449                 : ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or 
   43450                 : ** an IO error code if opening or writing the journal file fails.
   43451                 : */
   43452           52003 : static int pager_open_journal(Pager *pPager){
   43453           52003 :   int rc = SQLITE_OK;                        /* Return code */
   43454           52003 :   sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
   43455                 : 
   43456           52003 :   assert( pPager->eState==PAGER_WRITER_LOCKED );
   43457           52003 :   assert( assert_pager_state(pPager) );
   43458           52003 :   assert( pPager->pInJournal==0 );
   43459                 :   
   43460                 :   /* If already in the error state, this function is a no-op.  But on
   43461                 :   ** the other hand, this routine is never called if we are already in
   43462                 :   ** an error state. */
   43463           52003 :   if( NEVER(pPager->errCode) ) return pPager->errCode;
   43464                 : 
   43465           52003 :   if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
   43466           10931 :     pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
   43467           10931 :     if( pPager->pInJournal==0 ){
   43468               0 :       return SQLITE_NOMEM;
   43469                 :     }
   43470                 :   
   43471                 :     /* Open the journal file if it is not already open. */
   43472           10931 :     if( !isOpen(pPager->jfd) ){
   43473            9422 :       if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
   43474            2250 :         sqlite3MemJournalOpen(pPager->jfd);
   43475                 :       }else{
   43476            7172 :         const int flags =                   /* VFS flags to open journal file */
   43477            7172 :           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
   43478            7172 :           (pPager->tempFile ? 
   43479                 :             (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
   43480                 :             (SQLITE_OPEN_MAIN_JOURNAL)
   43481                 :           );
   43482                 :   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   43483                 :         rc = sqlite3JournalOpen(
   43484                 :             pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
   43485                 :         );
   43486                 :   #else
   43487            7172 :         rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
   43488                 :   #endif
   43489                 :       }
   43490            9422 :       assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
   43491                 :     }
   43492                 :   
   43493                 :   
   43494                 :     /* Write the first journal header to the journal file and open 
   43495                 :     ** the sub-journal if necessary.
   43496                 :     */
   43497           10931 :     if( rc==SQLITE_OK ){
   43498                 :       /* TODO: Check if all of these are really required. */
   43499           10931 :       pPager->nRec = 0;
   43500           10931 :       pPager->journalOff = 0;
   43501           10931 :       pPager->setMaster = 0;
   43502           10931 :       pPager->journalHdr = 0;
   43503           10931 :       rc = writeJournalHdr(pPager);
   43504                 :     }
   43505                 :   }
   43506                 : 
   43507           52003 :   if( rc!=SQLITE_OK ){
   43508               0 :     sqlite3BitvecDestroy(pPager->pInJournal);
   43509               0 :     pPager->pInJournal = 0;
   43510                 :   }else{
   43511           52003 :     assert( pPager->eState==PAGER_WRITER_LOCKED );
   43512           52003 :     pPager->eState = PAGER_WRITER_CACHEMOD;
   43513                 :   }
   43514                 : 
   43515           52003 :   return rc;
   43516                 : }
   43517                 : 
   43518                 : /*
   43519                 : ** Begin a write-transaction on the specified pager object. If a 
   43520                 : ** write-transaction has already been opened, this function is a no-op.
   43521                 : **
   43522                 : ** If the exFlag argument is false, then acquire at least a RESERVED
   43523                 : ** lock on the database file. If exFlag is true, then acquire at least
   43524                 : ** an EXCLUSIVE lock. If such a lock is already held, no locking 
   43525                 : ** functions need be called.
   43526                 : **
   43527                 : ** If the subjInMemory argument is non-zero, then any sub-journal opened
   43528                 : ** within this transaction will be opened as an in-memory file. This
   43529                 : ** has no effect if the sub-journal is already opened (as it may be when
   43530                 : ** running in exclusive mode) or if the transaction does not require a
   43531                 : ** sub-journal. If the subjInMemory argument is zero, then any required
   43532                 : ** sub-journal is implemented in-memory if pPager is an in-memory database, 
   43533                 : ** or using a temporary file otherwise.
   43534                 : */
   43535           56893 : SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
   43536           56893 :   int rc = SQLITE_OK;
   43537                 : 
   43538           56893 :   if( pPager->errCode ) return pPager->errCode;
   43539           56893 :   assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
   43540           56893 :   pPager->subjInMemory = (u8)subjInMemory;
   43541                 : 
   43542           56893 :   if( ALWAYS(pPager->eState==PAGER_READER) ){
   43543           56893 :     assert( pPager->pInJournal==0 );
   43544                 : 
   43545           56893 :     if( pagerUseWal(pPager) ){
   43546                 :       /* If the pager is configured to use locking_mode=exclusive, and an
   43547                 :       ** exclusive lock on the database is not already held, obtain it now.
   43548                 :       */
   43549           26364 :       if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
   43550               0 :         rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
   43551               0 :         if( rc!=SQLITE_OK ){
   43552               0 :           return rc;
   43553                 :         }
   43554               0 :         sqlite3WalExclusiveMode(pPager->pWal, 1);
   43555                 :       }
   43556                 : 
   43557                 :       /* Grab the write lock on the log file. If successful, upgrade to
   43558                 :       ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
   43559                 :       ** The busy-handler is not invoked if another connection already
   43560                 :       ** holds the write-lock. If possible, the upper layer will call it.
   43561                 :       */
   43562           26364 :       rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
   43563                 :     }else{
   43564                 :       /* Obtain a RESERVED lock on the database file. If the exFlag parameter
   43565                 :       ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
   43566                 :       ** busy-handler callback can be used when upgrading to the EXCLUSIVE
   43567                 :       ** lock, but not when obtaining the RESERVED lock.
   43568                 :       */
   43569           30529 :       rc = pagerLockDb(pPager, RESERVED_LOCK);
   43570           30529 :       if( rc==SQLITE_OK && exFlag ){
   43571             771 :         rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
   43572                 :       }
   43573                 :     }
   43574                 : 
   43575           56893 :     if( rc==SQLITE_OK ){
   43576                 :       /* Change to WRITER_LOCKED state.
   43577                 :       **
   43578                 :       ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
   43579                 :       ** when it has an open transaction, but never to DBMOD or FINISHED.
   43580                 :       ** This is because in those states the code to roll back savepoint 
   43581                 :       ** transactions may copy data from the sub-journal into the database 
   43582                 :       ** file as well as into the page cache. Which would be incorrect in 
   43583                 :       ** WAL mode.
   43584                 :       */
   43585           56893 :       pPager->eState = PAGER_WRITER_LOCKED;
   43586           56893 :       pPager->dbHintSize = pPager->dbSize;
   43587           56893 :       pPager->dbFileSize = pPager->dbSize;
   43588           56893 :       pPager->dbOrigSize = pPager->dbSize;
   43589           56893 :       pPager->journalOff = 0;
   43590                 :     }
   43591                 : 
   43592           56893 :     assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
   43593           56893 :     assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
   43594           56893 :     assert( assert_pager_state(pPager) );
   43595                 :   }
   43596                 : 
   43597                 :   PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
   43598           56893 :   return rc;
   43599                 : }
   43600                 : 
   43601                 : /*
   43602                 : ** Mark a single data page as writeable. The page is written into the 
   43603                 : ** main journal or sub-journal as required. If the page is written into
   43604                 : ** one of the journals, the corresponding bit is set in the 
   43605                 : ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
   43606                 : ** of any open savepoints as appropriate.
   43607                 : */
   43608          432942 : static int pager_write(PgHdr *pPg){
   43609          432942 :   void *pData = pPg->pData;
   43610          432942 :   Pager *pPager = pPg->pPager;
   43611          432942 :   int rc = SQLITE_OK;
   43612                 : 
   43613                 :   /* This routine is not called unless a write-transaction has already 
   43614                 :   ** been started. The journal file may or may not be open at this point.
   43615                 :   ** It is never called in the ERROR state.
   43616                 :   */
   43617          432942 :   assert( pPager->eState==PAGER_WRITER_LOCKED
   43618                 :        || pPager->eState==PAGER_WRITER_CACHEMOD
   43619                 :        || pPager->eState==PAGER_WRITER_DBMOD
   43620                 :   );
   43621          432942 :   assert( assert_pager_state(pPager) );
   43622                 : 
   43623                 :   /* If an error has been previously detected, report the same error
   43624                 :   ** again. This should not happen, but the check provides robustness. */
   43625          432942 :   if( NEVER(pPager->errCode) )  return pPager->errCode;
   43626                 : 
   43627                 :   /* Higher-level routines never call this function if database is not
   43628                 :   ** writable.  But check anyway, just for robustness. */
   43629          432942 :   if( NEVER(pPager->readOnly) ) return SQLITE_PERM;
   43630                 : 
   43631                 :   CHECK_PAGE(pPg);
   43632                 : 
   43633                 :   /* The journal file needs to be opened. Higher level routines have already
   43634                 :   ** obtained the necessary locks to begin the write-transaction, but the
   43635                 :   ** rollback journal might not yet be open. Open it now if this is the case.
   43636                 :   **
   43637                 :   ** This is done before calling sqlite3PcacheMakeDirty() on the page. 
   43638                 :   ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
   43639                 :   ** an error might occur and the pager would end up in WRITER_LOCKED state
   43640                 :   ** with pages marked as dirty in the cache.
   43641                 :   */
   43642          432942 :   if( pPager->eState==PAGER_WRITER_LOCKED ){
   43643           52003 :     rc = pager_open_journal(pPager);
   43644           52003 :     if( rc!=SQLITE_OK ) return rc;
   43645                 :   }
   43646          432942 :   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
   43647          432942 :   assert( assert_pager_state(pPager) );
   43648                 : 
   43649                 :   /* Mark the page as dirty.  If the page has already been written
   43650                 :   ** to the journal then we can return right away.
   43651                 :   */
   43652          432942 :   sqlite3PcacheMakeDirty(pPg);
   43653          432941 :   if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
   43654           40305 :     assert( !pagerUseWal(pPager) );
   43655                 :   }else{
   43656                 :   
   43657                 :     /* The transaction journal now exists and we have a RESERVED or an
   43658                 :     ** EXCLUSIVE lock on the main database file.  Write the current page to
   43659                 :     ** the transaction journal if it is not there already.
   43660                 :     */
   43661          392637 :     if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
   43662          133478 :       assert( pagerUseWal(pPager)==0 );
   43663          154755 :       if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
   43664                 :         u32 cksum;
   43665                 :         char *pData2;
   43666           21277 :         i64 iOff = pPager->journalOff;
   43667                 : 
   43668                 :         /* We should never write to the journal file the page that
   43669                 :         ** contains the database locks.  The following assert verifies
   43670                 :         ** that we do not. */
   43671           21277 :         assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
   43672                 : 
   43673           21277 :         assert( pPager->journalHdr<=pPager->journalOff );
   43674           21277 :         CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
   43675           21277 :         cksum = pager_cksum(pPager, (u8*)pData2);
   43676                 : 
   43677                 :         /* Even if an IO or diskfull error occurs while journalling the
   43678                 :         ** page in the block above, set the need-sync flag for the page.
   43679                 :         ** Otherwise, when the transaction is rolled back, the logic in
   43680                 :         ** playback_one_page() will think that the page needs to be restored
   43681                 :         ** in the database file. And if an IO error occurs while doing so,
   43682                 :         ** then corruption may follow.
   43683                 :         */
   43684           21277 :         pPg->flags |= PGHDR_NEED_SYNC;
   43685                 : 
   43686           21277 :         rc = write32bits(pPager->jfd, iOff, pPg->pgno);
   43687           21277 :         if( rc!=SQLITE_OK ) return rc;
   43688           21277 :         rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
   43689           21277 :         if( rc!=SQLITE_OK ) return rc;
   43690           21277 :         rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
   43691           21277 :         if( rc!=SQLITE_OK ) return rc;
   43692                 : 
   43693                 :         IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
   43694                 :                  pPager->journalOff, pPager->pageSize));
   43695                 :         PAGER_INCR(sqlite3_pager_writej_count);
   43696                 :         PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
   43697                 :              PAGERID(pPager), pPg->pgno, 
   43698                 :              ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
   43699                 : 
   43700           21277 :         pPager->journalOff += 8 + pPager->pageSize;
   43701           21277 :         pPager->nRec++;
   43702           21277 :         assert( pPager->pInJournal!=0 );
   43703           21277 :         rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
   43704                 :         testcase( rc==SQLITE_NOMEM );
   43705           21277 :         assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
   43706           21277 :         rc |= addToSavepointBitvecs(pPager, pPg->pgno);
   43707           21277 :         if( rc!=SQLITE_OK ){
   43708               0 :           assert( rc==SQLITE_NOMEM );
   43709               0 :           return rc;
   43710                 :         }
   43711                 :       }else{
   43712          112201 :         if( pPager->eState!=PAGER_WRITER_DBMOD ){
   43713          112201 :           pPg->flags |= PGHDR_NEED_SYNC;
   43714                 :         }
   43715                 :         PAGERTRACE(("APPEND %d page %d needSync=%d\n",
   43716                 :                 PAGERID(pPager), pPg->pgno,
   43717                 :                ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
   43718                 :       }
   43719                 :     }
   43720                 :   
   43721                 :     /* If the statement journal is open and the page is not in it,
   43722                 :     ** then write the current page to the statement journal.  Note that
   43723                 :     ** the statement journal format differs from the standard journal format
   43724                 :     ** in that it omits the checksums and the header.
   43725                 :     */
   43726          392637 :     if( subjRequiresPage(pPg) ){
   43727           53922 :       rc = subjournalPage(pPg);
   43728                 :     }
   43729                 :   }
   43730                 : 
   43731                 :   /* Update the database size and return.
   43732                 :   */
   43733          432941 :   if( pPager->dbSize<pPg->pgno ){
   43734           47500 :     pPager->dbSize = pPg->pgno;
   43735                 :   }
   43736          432941 :   return rc;
   43737                 : }
   43738                 : 
   43739                 : /*
   43740                 : ** Mark a data page as writeable. This routine must be called before 
   43741                 : ** making changes to a page. The caller must check the return value 
   43742                 : ** of this function and be careful not to change any page data unless 
   43743                 : ** this routine returns SQLITE_OK.
   43744                 : **
   43745                 : ** The difference between this function and pager_write() is that this
   43746                 : ** function also deals with the special case where 2 or more pages
   43747                 : ** fit on a single disk sector. In this case all co-resident pages
   43748                 : ** must have been written to the journal file before returning.
   43749                 : **
   43750                 : ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
   43751                 : ** as appropriate. Otherwise, SQLITE_OK.
   43752                 : */
   43753          432942 : SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
   43754          432942 :   int rc = SQLITE_OK;
   43755                 : 
   43756          432942 :   PgHdr *pPg = pDbPage;
   43757          432942 :   Pager *pPager = pPg->pPager;
   43758          432942 :   Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
   43759                 : 
   43760          432942 :   assert( pPager->eState>=PAGER_WRITER_LOCKED );
   43761          432942 :   assert( pPager->eState!=PAGER_ERROR );
   43762          432942 :   assert( assert_pager_state(pPager) );
   43763                 : 
   43764          432942 :   if( nPagePerSector>1 ){
   43765                 :     Pgno nPageCount;          /* Total number of pages in database file */
   43766                 :     Pgno pg1;                 /* First page of the sector pPg is located on. */
   43767               0 :     int nPage = 0;            /* Number of pages starting at pg1 to journal */
   43768                 :     int ii;                   /* Loop counter */
   43769               0 :     int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
   43770                 : 
   43771                 :     /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
   43772                 :     ** a journal header to be written between the pages journaled by
   43773                 :     ** this function.
   43774                 :     */
   43775               0 :     assert( !MEMDB );
   43776               0 :     assert( pPager->doNotSyncSpill==0 );
   43777               0 :     pPager->doNotSyncSpill++;
   43778                 : 
   43779                 :     /* This trick assumes that both the page-size and sector-size are
   43780                 :     ** an integer power of 2. It sets variable pg1 to the identifier
   43781                 :     ** of the first page of the sector pPg is located on.
   43782                 :     */
   43783               0 :     pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
   43784                 : 
   43785               0 :     nPageCount = pPager->dbSize;
   43786               0 :     if( pPg->pgno>nPageCount ){
   43787               0 :       nPage = (pPg->pgno - pg1)+1;
   43788               0 :     }else if( (pg1+nPagePerSector-1)>nPageCount ){
   43789               0 :       nPage = nPageCount+1-pg1;
   43790                 :     }else{
   43791               0 :       nPage = nPagePerSector;
   43792                 :     }
   43793               0 :     assert(nPage>0);
   43794               0 :     assert(pg1<=pPg->pgno);
   43795               0 :     assert((pg1+nPage)>pPg->pgno);
   43796                 : 
   43797               0 :     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
   43798               0 :       Pgno pg = pg1+ii;
   43799                 :       PgHdr *pPage;
   43800               0 :       if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
   43801               0 :         if( pg!=PAGER_MJ_PGNO(pPager) ){
   43802               0 :           rc = sqlite3PagerGet(pPager, pg, &pPage);
   43803               0 :           if( rc==SQLITE_OK ){
   43804               0 :             rc = pager_write(pPage);
   43805               0 :             if( pPage->flags&PGHDR_NEED_SYNC ){
   43806               0 :               needSync = 1;
   43807                 :             }
   43808               0 :             sqlite3PagerUnref(pPage);
   43809                 :           }
   43810                 :         }
   43811               0 :       }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
   43812               0 :         if( pPage->flags&PGHDR_NEED_SYNC ){
   43813               0 :           needSync = 1;
   43814                 :         }
   43815               0 :         sqlite3PagerUnref(pPage);
   43816                 :       }
   43817                 :     }
   43818                 : 
   43819                 :     /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
   43820                 :     ** starting at pg1, then it needs to be set for all of them. Because
   43821                 :     ** writing to any of these nPage pages may damage the others, the
   43822                 :     ** journal file must contain sync()ed copies of all of them
   43823                 :     ** before any of them can be written out to the database file.
   43824                 :     */
   43825               0 :     if( rc==SQLITE_OK && needSync ){
   43826               0 :       assert( !MEMDB );
   43827               0 :       for(ii=0; ii<nPage; ii++){
   43828               0 :         PgHdr *pPage = pager_lookup(pPager, pg1+ii);
   43829               0 :         if( pPage ){
   43830               0 :           pPage->flags |= PGHDR_NEED_SYNC;
   43831               0 :           sqlite3PagerUnref(pPage);
   43832                 :         }
   43833                 :       }
   43834                 :     }
   43835                 : 
   43836               0 :     assert( pPager->doNotSyncSpill==1 );
   43837               0 :     pPager->doNotSyncSpill--;
   43838                 :   }else{
   43839          432942 :     rc = pager_write(pDbPage);
   43840                 :   }
   43841          432941 :   return rc;
   43842                 : }
   43843                 : 
   43844                 : /*
   43845                 : ** Return TRUE if the page given in the argument was previously passed
   43846                 : ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
   43847                 : ** to change the content of the page.
   43848                 : */
   43849                 : #ifndef NDEBUG
   43850          854958 : SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
   43851          854958 :   return pPg->flags&PGHDR_DIRTY;
   43852                 : }
   43853                 : #endif
   43854                 : 
   43855                 : /*
   43856                 : ** A call to this routine tells the pager that it is not necessary to
   43857                 : ** write the information on page pPg back to the disk, even though
   43858                 : ** that page might be marked as dirty.  This happens, for example, when
   43859                 : ** the page has been added as a leaf of the freelist and so its
   43860                 : ** content no longer matters.
   43861                 : **
   43862                 : ** The overlying software layer calls this routine when all of the data
   43863                 : ** on the given page is unused. The pager marks the page as clean so
   43864                 : ** that it does not get written to disk.
   43865                 : **
   43866                 : ** Tests show that this optimization can quadruple the speed of large 
   43867                 : ** DELETE operations.
   43868                 : */
   43869               0 : SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
   43870               0 :   Pager *pPager = pPg->pPager;
   43871               0 :   if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
   43872                 :     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
   43873                 :     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
   43874               0 :     pPg->flags |= PGHDR_DONT_WRITE;
   43875                 :     pager_set_pagehash(pPg);
   43876                 :   }
   43877               0 : }
   43878                 : 
   43879                 : /*
   43880                 : ** This routine is called to increment the value of the database file 
   43881                 : ** change-counter, stored as a 4-byte big-endian integer starting at 
   43882                 : ** byte offset 24 of the pager file.  The secondary change counter at
   43883                 : ** 92 is also updated, as is the SQLite version number at offset 96.
   43884                 : **
   43885                 : ** But this only happens if the pPager->changeCountDone flag is false.
   43886                 : ** To avoid excess churning of page 1, the update only happens once.
   43887                 : ** See also the pager_write_changecounter() routine that does an 
   43888                 : ** unconditional update of the change counters.
   43889                 : **
   43890                 : ** If the isDirectMode flag is zero, then this is done by calling 
   43891                 : ** sqlite3PagerWrite() on page 1, then modifying the contents of the
   43892                 : ** page data. In this case the file will be updated when the current
   43893                 : ** transaction is committed.
   43894                 : **
   43895                 : ** The isDirectMode flag may only be non-zero if the library was compiled
   43896                 : ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
   43897                 : ** if isDirect is non-zero, then the database file is updated directly
   43898                 : ** by writing an updated version of page 1 using a call to the 
   43899                 : ** sqlite3OsWrite() function.
   43900                 : */
   43901            8675 : static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
   43902            8675 :   int rc = SQLITE_OK;
   43903                 : 
   43904            8675 :   assert( pPager->eState==PAGER_WRITER_CACHEMOD
   43905                 :        || pPager->eState==PAGER_WRITER_DBMOD
   43906                 :   );
   43907            8675 :   assert( assert_pager_state(pPager) );
   43908                 : 
   43909                 :   /* Declare and initialize constant integer 'isDirect'. If the
   43910                 :   ** atomic-write optimization is enabled in this build, then isDirect
   43911                 :   ** is initialized to the value passed as the isDirectMode parameter
   43912                 :   ** to this function. Otherwise, it is always set to zero.
   43913                 :   **
   43914                 :   ** The idea is that if the atomic-write optimization is not
   43915                 :   ** enabled at compile time, the compiler can omit the tests of
   43916                 :   ** 'isDirect' below, as well as the block enclosed in the
   43917                 :   ** "if( isDirect )" condition.
   43918                 :   */
   43919                 : #ifndef SQLITE_ENABLE_ATOMIC_WRITE
   43920                 : # define DIRECT_MODE 0
   43921            8675 :   assert( isDirectMode==0 );
   43922                 :   UNUSED_PARAMETER(isDirectMode);
   43923                 : #else
   43924                 : # define DIRECT_MODE isDirectMode
   43925                 : #endif
   43926                 : 
   43927            8675 :   if( !pPager->changeCountDone && pPager->dbSize>0 ){
   43928                 :     PgHdr *pPgHdr;                /* Reference to page 1 */
   43929                 : 
   43930            7145 :     assert( !pPager->tempFile && isOpen(pPager->fd) );
   43931                 : 
   43932                 :     /* Open page 1 of the file for writing. */
   43933            7145 :     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
   43934            7145 :     assert( pPgHdr==0 || rc==SQLITE_OK );
   43935                 : 
   43936                 :     /* If page one was fetched successfully, and this function is not
   43937                 :     ** operating in direct-mode, make page 1 writable.  When not in 
   43938                 :     ** direct mode, page 1 is always held in cache and hence the PagerGet()
   43939                 :     ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
   43940                 :     */
   43941            7145 :     if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
   43942            7145 :       rc = sqlite3PagerWrite(pPgHdr);
   43943                 :     }
   43944                 : 
   43945            7145 :     if( rc==SQLITE_OK ){
   43946                 :       /* Actually do the update of the change counter */
   43947            7145 :       pager_write_changecounter(pPgHdr);
   43948                 : 
   43949                 :       /* If running in direct mode, write the contents of page 1 to the file. */
   43950                 :       if( DIRECT_MODE ){
   43951                 :         const void *zBuf;
   43952                 :         assert( pPager->dbFileSize>0 );
   43953                 :         CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
   43954                 :         if( rc==SQLITE_OK ){
   43955                 :           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
   43956                 :         }
   43957                 :         if( rc==SQLITE_OK ){
   43958                 :           pPager->changeCountDone = 1;
   43959                 :         }
   43960                 :       }else{
   43961            7145 :         pPager->changeCountDone = 1;
   43962                 :       }
   43963                 :     }
   43964                 : 
   43965                 :     /* Release the page reference. */
   43966            7145 :     sqlite3PagerUnref(pPgHdr);
   43967                 :   }
   43968            8675 :   return rc;
   43969                 : }
   43970                 : 
   43971                 : /*
   43972                 : ** Sync the database file to disk. This is a no-op for in-memory databases
   43973                 : ** or pages with the Pager.noSync flag set.
   43974                 : **
   43975                 : ** If successful, or if called on a pager for which it is a no-op, this
   43976                 : ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
   43977                 : */
   43978            8675 : SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager){
   43979            8675 :   int rc = SQLITE_OK;
   43980            8675 :   if( !pPager->noSync ){
   43981            6443 :     assert( !MEMDB );
   43982            6443 :     rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
   43983            2232 :   }else if( isOpen(pPager->fd) ){
   43984            2232 :     assert( !MEMDB );
   43985            2232 :     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
   43986            2232 :     if( rc==SQLITE_NOTFOUND ){
   43987            2232 :       rc = SQLITE_OK;
   43988                 :     }
   43989                 :   }
   43990            8675 :   return rc;
   43991                 : }
   43992                 : 
   43993                 : /*
   43994                 : ** This function may only be called while a write-transaction is active in
   43995                 : ** rollback. If the connection is in WAL mode, this call is a no-op. 
   43996                 : ** Otherwise, if the connection does not already have an EXCLUSIVE lock on 
   43997                 : ** the database file, an attempt is made to obtain one.
   43998                 : **
   43999                 : ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
   44000                 : ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
   44001                 : ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is 
   44002                 : ** returned.
   44003                 : */
   44004           49202 : SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
   44005           49202 :   int rc = SQLITE_OK;
   44006           49202 :   assert( pPager->eState==PAGER_WRITER_CACHEMOD 
   44007                 :        || pPager->eState==PAGER_WRITER_DBMOD 
   44008                 :        || pPager->eState==PAGER_WRITER_LOCKED 
   44009                 :   );
   44010           49202 :   assert( assert_pager_state(pPager) );
   44011           49202 :   if( 0==pagerUseWal(pPager) ){
   44012           23143 :     rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
   44013                 :   }
   44014           49202 :   return rc;
   44015                 : }
   44016                 : 
   44017                 : /*
   44018                 : ** Sync the database file for the pager pPager. zMaster points to the name
   44019                 : ** of a master journal file that should be written into the individual
   44020                 : ** journal file. zMaster may be NULL, which is interpreted as no master
   44021                 : ** journal (a single database transaction).
   44022                 : **
   44023                 : ** This routine ensures that:
   44024                 : **
   44025                 : **   * The database file change-counter is updated,
   44026                 : **   * the journal is synced (unless the atomic-write optimization is used),
   44027                 : **   * all dirty pages are written to the database file, 
   44028                 : **   * the database file is truncated (if required), and
   44029                 : **   * the database file synced. 
   44030                 : **
   44031                 : ** The only thing that remains to commit the transaction is to finalize 
   44032                 : ** (delete, truncate or zero the first part of) the journal file (or 
   44033                 : ** delete the master journal file if specified).
   44034                 : **
   44035                 : ** Note that if zMaster==NULL, this does not overwrite a previous value
   44036                 : ** passed to an sqlite3PagerCommitPhaseOne() call.
   44037                 : **
   44038                 : ** If the final parameter - noSync - is true, then the database file itself
   44039                 : ** is not synced. The caller must call sqlite3PagerSync() directly to
   44040                 : ** sync the database file before calling CommitPhaseTwo() to delete the
   44041                 : ** journal file in this case.
   44042                 : */
   44043           40539 : SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
   44044                 :   Pager *pPager,                  /* Pager object */
   44045                 :   const char *zMaster,            /* If not NULL, the master journal name */
   44046                 :   int noSync                      /* True to omit the xSync on the db file */
   44047                 : ){
   44048           40539 :   int rc = SQLITE_OK;             /* Return code */
   44049                 : 
   44050           40539 :   assert( pPager->eState==PAGER_WRITER_LOCKED
   44051                 :        || pPager->eState==PAGER_WRITER_CACHEMOD
   44052                 :        || pPager->eState==PAGER_WRITER_DBMOD
   44053                 :        || pPager->eState==PAGER_ERROR
   44054                 :   );
   44055           40539 :   assert( assert_pager_state(pPager) );
   44056                 : 
   44057                 :   /* If a prior error occurred, report that error again. */
   44058           40539 :   if( NEVER(pPager->errCode) ) return pPager->errCode;
   44059                 : 
   44060                 :   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n", 
   44061                 :       pPager->zFilename, zMaster, pPager->dbSize));
   44062                 : 
   44063                 :   /* If no database changes have been made, return early. */
   44064           40539 :   if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
   44065                 : 
   44066           35962 :   if( MEMDB ){
   44067                 :     /* If this is an in-memory db, or no pages have been written to, or this
   44068                 :     ** function has already been called, it is mostly a no-op.  However, any
   44069                 :     ** backup in progress needs to be restarted.
   44070                 :     */
   44071            2247 :     sqlite3BackupRestart(pPager->pBackup);
   44072                 :   }else{
   44073           33715 :     if( pagerUseWal(pPager) ){
   44074           25040 :       PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
   44075           25040 :       PgHdr *pPageOne = 0;
   44076           25040 :       if( pList==0 ){
   44077                 :         /* Must have at least one page for the WAL commit flag.
   44078                 :         ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
   44079               0 :         rc = sqlite3PagerGet(pPager, 1, &pPageOne);
   44080               0 :         pList = pPageOne;
   44081               0 :         pList->pDirty = 0;
   44082                 :       }
   44083           25040 :       assert( rc==SQLITE_OK );
   44084           25040 :       if( ALWAYS(pList) ){
   44085           25040 :         rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
   44086                 :       }
   44087           25040 :       sqlite3PagerUnref(pPageOne);
   44088           25040 :       if( rc==SQLITE_OK ){
   44089           25040 :         sqlite3PcacheCleanAll(pPager->pPCache);
   44090                 :       }
   44091                 :     }else{
   44092                 :       /* The following block updates the change-counter. Exactly how it
   44093                 :       ** does this depends on whether or not the atomic-update optimization
   44094                 :       ** was enabled at compile time, and if this transaction meets the 
   44095                 :       ** runtime criteria to use the operation: 
   44096                 :       **
   44097                 :       **    * The file-system supports the atomic-write property for
   44098                 :       **      blocks of size page-size, and 
   44099                 :       **    * This commit is not part of a multi-file transaction, and
   44100                 :       **    * Exactly one page has been modified and store in the journal file.
   44101                 :       **
   44102                 :       ** If the optimization was not enabled at compile time, then the
   44103                 :       ** pager_incr_changecounter() function is called to update the change
   44104                 :       ** counter in 'indirect-mode'. If the optimization is compiled in but
   44105                 :       ** is not applicable to this transaction, call sqlite3JournalCreate()
   44106                 :       ** to make sure the journal file has actually been created, then call
   44107                 :       ** pager_incr_changecounter() to update the change-counter in indirect
   44108                 :       ** mode. 
   44109                 :       **
   44110                 :       ** Otherwise, if the optimization is both enabled and applicable,
   44111                 :       ** then call pager_incr_changecounter() to update the change-counter
   44112                 :       ** in 'direct' mode. In this case the journal file will never be
   44113                 :       ** created for this transaction.
   44114                 :       */
   44115                 :   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   44116                 :       PgHdr *pPg;
   44117                 :       assert( isOpen(pPager->jfd) 
   44118                 :            || pPager->journalMode==PAGER_JOURNALMODE_OFF 
   44119                 :            || pPager->journalMode==PAGER_JOURNALMODE_WAL 
   44120                 :       );
   44121                 :       if( !zMaster && isOpen(pPager->jfd) 
   44122                 :        && pPager->journalOff==jrnlBufferSize(pPager) 
   44123                 :        && pPager->dbSize>=pPager->dbOrigSize
   44124                 :        && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
   44125                 :       ){
   44126                 :         /* Update the db file change counter via the direct-write method. The 
   44127                 :         ** following call will modify the in-memory representation of page 1 
   44128                 :         ** to include the updated change counter and then write page 1 
   44129                 :         ** directly to the database file. Because of the atomic-write 
   44130                 :         ** property of the host file-system, this is safe.
   44131                 :         */
   44132                 :         rc = pager_incr_changecounter(pPager, 1);
   44133                 :       }else{
   44134                 :         rc = sqlite3JournalCreate(pPager->jfd);
   44135                 :         if( rc==SQLITE_OK ){
   44136                 :           rc = pager_incr_changecounter(pPager, 0);
   44137                 :         }
   44138                 :       }
   44139                 :   #else
   44140            8675 :       rc = pager_incr_changecounter(pPager, 0);
   44141                 :   #endif
   44142            8675 :       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
   44143                 :   
   44144                 :       /* If this transaction has made the database smaller, then all pages
   44145                 :       ** being discarded by the truncation must be written to the journal
   44146                 :       ** file. This can only happen in auto-vacuum mode.
   44147                 :       **
   44148                 :       ** Before reading the pages with page numbers larger than the 
   44149                 :       ** current value of Pager.dbSize, set dbSize back to the value
   44150                 :       ** that it took at the start of the transaction. Otherwise, the
   44151                 :       ** calls to sqlite3PagerGet() return zeroed pages instead of 
   44152                 :       ** reading data from the database file.
   44153                 :       */
   44154                 :   #ifndef SQLITE_OMIT_AUTOVACUUM
   44155            8675 :       if( pPager->dbSize<pPager->dbOrigSize 
   44156               2 :        && pPager->journalMode!=PAGER_JOURNALMODE_OFF
   44157                 :       ){
   44158                 :         Pgno i;                                   /* Iterator variable */
   44159               2 :         const Pgno iSkip = PAGER_MJ_PGNO(pPager); /* Pending lock page */
   44160               2 :         const Pgno dbSize = pPager->dbSize;       /* Database image size */ 
   44161               2 :         pPager->dbSize = pPager->dbOrigSize;
   44162              18 :         for( i=dbSize+1; i<=pPager->dbOrigSize; i++ ){
   44163              16 :           if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
   44164                 :             PgHdr *pPage;             /* Page to journal */
   44165              16 :             rc = sqlite3PagerGet(pPager, i, &pPage);
   44166              16 :             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
   44167              16 :             rc = sqlite3PagerWrite(pPage);
   44168              16 :             sqlite3PagerUnref(pPage);
   44169              16 :             if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
   44170                 :           }
   44171                 :         }
   44172               2 :         pPager->dbSize = dbSize;
   44173                 :       } 
   44174                 :   #endif
   44175                 :   
   44176                 :       /* Write the master journal name into the journal file. If a master 
   44177                 :       ** journal file name has already been written to the journal file, 
   44178                 :       ** or if zMaster is NULL (no master journal), then this call is a no-op.
   44179                 :       */
   44180            8675 :       rc = writeMasterJournal(pPager, zMaster);
   44181            8675 :       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
   44182                 :   
   44183                 :       /* Sync the journal file and write all dirty pages to the database.
   44184                 :       ** If the atomic-update optimization is being used, this sync will not 
   44185                 :       ** create the journal file or perform any real IO.
   44186                 :       **
   44187                 :       ** Because the change-counter page was just modified, unless the
   44188                 :       ** atomic-update optimization is used it is almost certain that the
   44189                 :       ** journal requires a sync here. However, in locking_mode=exclusive
   44190                 :       ** on a system under memory pressure it is just possible that this is 
   44191                 :       ** not the case. In this case it is likely enough that the redundant
   44192                 :       ** xSync() call will be changed to a no-op by the OS anyhow. 
   44193                 :       */
   44194            8675 :       rc = syncJournal(pPager, 0);
   44195            8675 :       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
   44196                 :   
   44197            8675 :       rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
   44198            8675 :       if( rc!=SQLITE_OK ){
   44199               0 :         assert( rc!=SQLITE_IOERR_BLOCKED );
   44200               0 :         goto commit_phase_one_exit;
   44201                 :       }
   44202            8675 :       sqlite3PcacheCleanAll(pPager->pPCache);
   44203                 :   
   44204                 :       /* If the file on disk is not the same size as the database image,
   44205                 :       ** then use pager_truncate to grow or shrink the file here.
   44206                 :       */
   44207            8675 :       if( pPager->dbSize!=pPager->dbFileSize ){
   44208               2 :         Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
   44209               2 :         assert( pPager->eState==PAGER_WRITER_DBMOD );
   44210               2 :         rc = pager_truncate(pPager, nNew);
   44211               2 :         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
   44212                 :       }
   44213                 :   
   44214                 :       /* Finally, sync the database file. */
   44215            8675 :       if( !noSync ){
   44216            8675 :         rc = sqlite3PagerSync(pPager);
   44217                 :       }
   44218                 :       IOTRACE(("DBSYNC %p\n", pPager))
   44219                 :     }
   44220                 :   }
   44221                 : 
   44222                 : commit_phase_one_exit:
   44223           35962 :   if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
   44224           10922 :     pPager->eState = PAGER_WRITER_FINISHED;
   44225                 :   }
   44226           35962 :   return rc;
   44227                 : }
   44228                 : 
   44229                 : 
   44230                 : /*
   44231                 : ** When this function is called, the database file has been completely
   44232                 : ** updated to reflect the changes made by the current transaction and
   44233                 : ** synced to disk. The journal file still exists in the file-system 
   44234                 : ** though, and if a failure occurs at this point it will eventually
   44235                 : ** be used as a hot-journal and the current transaction rolled back.
   44236                 : **
   44237                 : ** This function finalizes the journal file, either by deleting, 
   44238                 : ** truncating or partially zeroing it, so that it cannot be used 
   44239                 : ** for hot-journal rollback. Once this is done the transaction is
   44240                 : ** irrevocably committed.
   44241                 : **
   44242                 : ** If an error occurs, an IO error code is returned and the pager
   44243                 : ** moves into the error state. Otherwise, SQLITE_OK is returned.
   44244                 : */
   44245           40539 : SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
   44246           40539 :   int rc = SQLITE_OK;                  /* Return code */
   44247                 : 
   44248                 :   /* This routine should not be called if a prior error has occurred.
   44249                 :   ** But if (due to a coding error elsewhere in the system) it does get
   44250                 :   ** called, just return the same error code without doing anything. */
   44251           40539 :   if( NEVER(pPager->errCode) ) return pPager->errCode;
   44252                 : 
   44253           40539 :   assert( pPager->eState==PAGER_WRITER_LOCKED
   44254                 :        || pPager->eState==PAGER_WRITER_FINISHED
   44255                 :        || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
   44256                 :   );
   44257           40539 :   assert( assert_pager_state(pPager) );
   44258                 : 
   44259                 :   /* An optimization. If the database was not actually modified during
   44260                 :   ** this transaction, the pager is running in exclusive-mode and is
   44261                 :   ** using persistent journals, then this function is a no-op.
   44262                 :   **
   44263                 :   ** The start of the journal file currently contains a single journal 
   44264                 :   ** header with the nRec field set to 0. If such a journal is used as
   44265                 :   ** a hot-journal during hot-journal rollback, 0 changes will be made
   44266                 :   ** to the database file. So there is no need to zero the journal 
   44267                 :   ** header. Since the pager is in exclusive mode, there is no need
   44268                 :   ** to drop any locks either.
   44269                 :   */
   44270           40539 :   if( pPager->eState==PAGER_WRITER_LOCKED 
   44271            4577 :    && pPager->exclusiveMode 
   44272            3231 :    && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
   44273                 :   ){
   44274               0 :     assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
   44275               0 :     pPager->eState = PAGER_READER;
   44276               0 :     return SQLITE_OK;
   44277                 :   }
   44278                 : 
   44279                 :   PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
   44280           40539 :   rc = pager_end_transaction(pPager, pPager->setMaster);
   44281           40539 :   return pager_error(pPager, rc);
   44282                 : }
   44283                 : 
   44284                 : /*
   44285                 : ** If a write transaction is open, then all changes made within the 
   44286                 : ** transaction are reverted and the current write-transaction is closed.
   44287                 : ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
   44288                 : ** state if an error occurs.
   44289                 : **
   44290                 : ** If the pager is already in PAGER_ERROR state when this function is called,
   44291                 : ** it returns Pager.errCode immediately. No work is performed in this case.
   44292                 : **
   44293                 : ** Otherwise, in rollback mode, this function performs two functions:
   44294                 : **
   44295                 : **   1) It rolls back the journal file, restoring all database file and 
   44296                 : **      in-memory cache pages to the state they were in when the transaction
   44297                 : **      was opened, and
   44298                 : **
   44299                 : **   2) It finalizes the journal file, so that it is not used for hot
   44300                 : **      rollback at any point in the future.
   44301                 : **
   44302                 : ** Finalization of the journal file (task 2) is only performed if the 
   44303                 : ** rollback is successful.
   44304                 : **
   44305                 : ** In WAL mode, all cache-entries containing data modified within the
   44306                 : ** current transaction are either expelled from the cache or reverted to
   44307                 : ** their pre-transaction state by re-reading data from the database or
   44308                 : ** WAL files. The WAL transaction is then closed.
   44309                 : */
   44310           16354 : SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
   44311           16354 :   int rc = SQLITE_OK;                  /* Return code */
   44312                 :   PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
   44313                 : 
   44314                 :   /* PagerRollback() is a no-op if called in READER or OPEN state. If
   44315                 :   ** the pager is already in the ERROR state, the rollback is not 
   44316                 :   ** attempted here. Instead, the error code is returned to the caller.
   44317                 :   */
   44318           16354 :   assert( assert_pager_state(pPager) );
   44319           16354 :   if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
   44320           16354 :   if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
   44321                 : 
   44322           16354 :   if( pagerUseWal(pPager) ){
   44323                 :     int rc2;
   44324             304 :     rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
   44325             304 :     rc2 = pager_end_transaction(pPager, pPager->setMaster);
   44326             304 :     if( rc==SQLITE_OK ) rc = rc2;
   44327           31105 :   }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
   44328           16041 :     int eState = pPager->eState;
   44329           16041 :     rc = pager_end_transaction(pPager, 0);
   44330           16041 :     if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
   44331                 :       /* This can happen using journal_mode=off. Move the pager to the error 
   44332                 :       ** state to indicate that the contents of the cache may not be trusted.
   44333                 :       ** Any active readers will get SQLITE_ABORT.
   44334                 :       */
   44335             986 :       pPager->errCode = SQLITE_ABORT;
   44336             986 :       pPager->eState = PAGER_ERROR;
   44337             986 :       return rc;
   44338                 :     }
   44339                 :   }else{
   44340               9 :     rc = pager_playback(pPager, 0);
   44341                 :   }
   44342                 : 
   44343           15368 :   assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
   44344           15368 :   assert( rc==SQLITE_OK || rc==SQLITE_FULL
   44345                 :           || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
   44346                 : 
   44347                 :   /* If an error occurs during a ROLLBACK, we can no longer trust the pager
   44348                 :   ** cache. So call pager_error() on the way out to make any error persistent.
   44349                 :   */
   44350           15368 :   return pager_error(pPager, rc);
   44351                 : }
   44352                 : 
   44353                 : /*
   44354                 : ** Return TRUE if the database file is opened read-only.  Return FALSE
   44355                 : ** if the database is (in theory) writable.
   44356                 : */
   44357           19517 : SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
   44358           19517 :   return pPager->readOnly;
   44359                 : }
   44360                 : 
   44361                 : /*
   44362                 : ** Return the number of references to the pager.
   44363                 : */
   44364           99822 : SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
   44365           99822 :   return sqlite3PcacheRefCount(pPager->pPCache);
   44366                 : }
   44367                 : 
   44368                 : /*
   44369                 : ** Return the approximate number of bytes of memory currently
   44370                 : ** used by the pager and its associated cache.
   44371                 : */
   44372               9 : SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
   44373              18 :   int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
   44374               9 :                                      + 5*sizeof(void*);
   44375              18 :   return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
   44376               9 :            + sqlite3MallocSize(pPager)
   44377               9 :            + pPager->pageSize;
   44378                 : }
   44379                 : 
   44380                 : /*
   44381                 : ** Return the number of references to the specified page.
   44382                 : */
   44383           30754 : SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
   44384           30754 :   return sqlite3PcachePageRefcount(pPage);
   44385                 : }
   44386                 : 
   44387                 : #ifdef SQLITE_TEST
   44388                 : /*
   44389                 : ** This routine is used for testing and analysis only.
   44390                 : */
   44391                 : SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
   44392                 :   static int a[11];
   44393                 :   a[0] = sqlite3PcacheRefCount(pPager->pPCache);
   44394                 :   a[1] = sqlite3PcachePagecount(pPager->pPCache);
   44395                 :   a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
   44396                 :   a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
   44397                 :   a[4] = pPager->eState;
   44398                 :   a[5] = pPager->errCode;
   44399                 :   a[6] = pPager->nHit;
   44400                 :   a[7] = pPager->nMiss;
   44401                 :   a[8] = 0;  /* Used to be pPager->nOvfl */
   44402                 :   a[9] = pPager->nRead;
   44403                 :   a[10] = pPager->nWrite;
   44404                 :   return a;
   44405                 : }
   44406                 : #endif
   44407                 : 
   44408                 : /*
   44409                 : ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
   44410                 : ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
   44411                 : ** current cache hit or miss count, according to the value of eStat. If the 
   44412                 : ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
   44413                 : ** returning.
   44414                 : */
   44415               0 : SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
   44416                 :   int *piStat;
   44417                 : 
   44418               0 :   assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
   44419                 :        || eStat==SQLITE_DBSTATUS_CACHE_MISS
   44420                 :   );
   44421               0 :   if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){
   44422               0 :     piStat = &pPager->nHit;
   44423                 :   }else{
   44424               0 :     piStat = &pPager->nMiss;
   44425                 :   }
   44426                 : 
   44427               0 :   *pnVal += *piStat;
   44428               0 :   if( reset ){
   44429               0 :     *piStat = 0;
   44430                 :   }
   44431               0 : }
   44432                 : 
   44433                 : /*
   44434                 : ** Return true if this is an in-memory pager.
   44435                 : */
   44436              14 : SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
   44437              14 :   return MEMDB;
   44438                 : }
   44439                 : 
   44440                 : /*
   44441                 : ** Check that there are at least nSavepoint savepoints open. If there are
   44442                 : ** currently less than nSavepoints open, then open one or more savepoints
   44443                 : ** to make up the difference. If the number of savepoints is already
   44444                 : ** equal to nSavepoint, then this function is a no-op.
   44445                 : **
   44446                 : ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
   44447                 : ** occurs while opening the sub-journal file, then an IO error code is
   44448                 : ** returned. Otherwise, SQLITE_OK.
   44449                 : */
   44450          128940 : SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
   44451          128940 :   int rc = SQLITE_OK;                       /* Return code */
   44452          128940 :   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
   44453                 : 
   44454          128940 :   assert( pPager->eState>=PAGER_WRITER_LOCKED );
   44455          128940 :   assert( assert_pager_state(pPager) );
   44456                 : 
   44457          128940 :   if( nSavepoint>nCurrent && pPager->useJournal ){
   44458                 :     int ii;                                 /* Iterator variable */
   44459                 :     PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
   44460                 : 
   44461                 :     /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
   44462                 :     ** if the allocation fails. Otherwise, zero the new portion in case a 
   44463                 :     ** malloc failure occurs while populating it in the for(...) loop below.
   44464                 :     */
   44465           45268 :     aNew = (PagerSavepoint *)sqlite3Realloc(
   44466           45268 :         pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
   44467                 :     );
   44468           22634 :     if( !aNew ){
   44469               0 :       return SQLITE_NOMEM;
   44470                 :     }
   44471           22634 :     memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
   44472           22634 :     pPager->aSavepoint = aNew;
   44473                 : 
   44474                 :     /* Populate the PagerSavepoint structures just allocated. */
   44475           45268 :     for(ii=nCurrent; ii<nSavepoint; ii++){
   44476           22634 :       aNew[ii].nOrig = pPager->dbSize;
   44477           22634 :       if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
   44478            5884 :         aNew[ii].iOffset = pPager->journalOff;
   44479                 :       }else{
   44480           16750 :         aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
   44481                 :       }
   44482           22634 :       aNew[ii].iSubRec = pPager->nSubRec;
   44483           22634 :       aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
   44484           22634 :       if( !aNew[ii].pInSavepoint ){
   44485               0 :         return SQLITE_NOMEM;
   44486                 :       }
   44487           22634 :       if( pagerUseWal(pPager) ){
   44488           15551 :         sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
   44489                 :       }
   44490           22634 :       pPager->nSavepoint = ii+1;
   44491                 :     }
   44492           22634 :     assert( pPager->nSavepoint==nSavepoint );
   44493           22634 :     assertTruncateConstraint(pPager);
   44494                 :   }
   44495                 : 
   44496          128940 :   return rc;
   44497                 : }
   44498                 : 
   44499                 : /*
   44500                 : ** This function is called to rollback or release (commit) a savepoint.
   44501                 : ** The savepoint to release or rollback need not be the most recently 
   44502                 : ** created savepoint.
   44503                 : **
   44504                 : ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
   44505                 : ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
   44506                 : ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
   44507                 : ** that have occurred since the specified savepoint was created.
   44508                 : **
   44509                 : ** The savepoint to rollback or release is identified by parameter 
   44510                 : ** iSavepoint. A value of 0 means to operate on the outermost savepoint
   44511                 : ** (the first created). A value of (Pager.nSavepoint-1) means operate
   44512                 : ** on the most recently created savepoint. If iSavepoint is greater than
   44513                 : ** (Pager.nSavepoint-1), then this function is a no-op.
   44514                 : **
   44515                 : ** If a negative value is passed to this function, then the current
   44516                 : ** transaction is rolled back. This is different to calling 
   44517                 : ** sqlite3PagerRollback() because this function does not terminate
   44518                 : ** the transaction or unlock the database, it just restores the 
   44519                 : ** contents of the database to its original state. 
   44520                 : **
   44521                 : ** In any case, all savepoints with an index greater than iSavepoint 
   44522                 : ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
   44523                 : ** then savepoint iSavepoint is also destroyed.
   44524                 : **
   44525                 : ** This function may return SQLITE_NOMEM if a memory allocation fails,
   44526                 : ** or an IO error code if an IO error occurs while rolling back a 
   44527                 : ** savepoint. If no errors occur, SQLITE_OK is returned.
   44528                 : */ 
   44529           30934 : SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
   44530           30934 :   int rc = pPager->errCode;       /* Return code */
   44531                 : 
   44532           30934 :   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
   44533           30934 :   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
   44534                 : 
   44535           30934 :   if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
   44536                 :     int ii;            /* Iterator variable */
   44537                 :     int nNew;          /* Number of remaining savepoints after this op. */
   44538                 : 
   44539                 :     /* Figure out how many savepoints will still be active after this
   44540                 :     ** operation. Store this value in nNew. Then free resources associated 
   44541                 :     ** with any savepoints that are destroyed by this operation.
   44542                 :     */
   44543           22949 :     nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
   44544           45559 :     for(ii=nNew; ii<pPager->nSavepoint; ii++){
   44545           22610 :       sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
   44546                 :     }
   44547           22949 :     pPager->nSavepoint = nNew;
   44548                 : 
   44549                 :     /* If this is a release of the outermost savepoint, truncate 
   44550                 :     ** the sub-journal to zero bytes in size. */
   44551           22949 :     if( op==SAVEPOINT_RELEASE ){
   44552           22610 :       if( nNew==0 && isOpen(pPager->sjfd) ){
   44553                 :         /* Only truncate if it is an in-memory sub-journal. */
   44554           18695 :         if( sqlite3IsMemJournal(pPager->sjfd) ){
   44555           15692 :           rc = sqlite3OsTruncate(pPager->sjfd, 0);
   44556           15692 :           assert( rc==SQLITE_OK );
   44557                 :         }
   44558           18695 :         pPager->nSubRec = 0;
   44559                 :       }
   44560                 :     }
   44561                 :     /* Else this is a rollback operation, playback the specified savepoint.
   44562                 :     ** If this is a temp-file, it is possible that the journal file has
   44563                 :     ** not yet been opened. In this case there have been no changes to
   44564                 :     ** the database file, so the playback operation can be skipped.
   44565                 :     */
   44566             339 :     else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
   44567             337 :       PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
   44568             337 :       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
   44569             337 :       assert(rc!=SQLITE_DONE);
   44570                 :     }
   44571                 :   }
   44572                 : 
   44573           30934 :   return rc;
   44574                 : }
   44575                 : 
   44576                 : /*
   44577                 : ** Return the full pathname of the database file.
   44578                 : */
   44579          101518 : SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager){
   44580          101518 :   return pPager->zFilename;
   44581                 : }
   44582                 : 
   44583                 : /*
   44584                 : ** Return the VFS structure for the pager.
   44585                 : */
   44586             176 : SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
   44587             176 :   return pPager->pVfs;
   44588                 : }
   44589                 : 
   44590                 : /*
   44591                 : ** Return the file handle for the database file associated
   44592                 : ** with the pager.  This might return NULL if the file has
   44593                 : ** not yet been opened.
   44594                 : */
   44595             665 : SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
   44596             665 :   return pPager->fd;
   44597                 : }
   44598                 : 
   44599                 : /*
   44600                 : ** Return the full pathname of the journal file.
   44601                 : */
   44602               0 : SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
   44603               0 :   return pPager->zJournal;
   44604                 : }
   44605                 : 
   44606                 : /*
   44607                 : ** Return true if fsync() calls are disabled for this pager.  Return FALSE
   44608                 : ** if fsync()s are executed normally.
   44609                 : */
   44610               0 : SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
   44611               0 :   return pPager->noSync;
   44612                 : }
   44613                 : 
   44614                 : #ifdef SQLITE_HAS_CODEC
   44615                 : /*
   44616                 : ** Set or retrieve the codec for this pager
   44617                 : */
   44618                 : SQLITE_PRIVATE void sqlite3PagerSetCodec(
   44619                 :   Pager *pPager,
   44620                 :   void *(*xCodec)(void*,void*,Pgno,int),
   44621                 :   void (*xCodecSizeChng)(void*,int,int),
   44622                 :   void (*xCodecFree)(void*),
   44623                 :   void *pCodec
   44624                 : ){
   44625                 :   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
   44626                 :   pPager->xCodec = pPager->memDb ? 0 : xCodec;
   44627                 :   pPager->xCodecSizeChng = xCodecSizeChng;
   44628                 :   pPager->xCodecFree = xCodecFree;
   44629                 :   pPager->pCodec = pCodec;
   44630                 :   pagerReportSize(pPager);
   44631                 : }
   44632                 : SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
   44633                 :   return pPager->pCodec;
   44634                 : }
   44635                 : #endif
   44636                 : 
   44637                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   44638                 : /*
   44639                 : ** Move the page pPg to location pgno in the file.
   44640                 : **
   44641                 : ** There must be no references to the page previously located at
   44642                 : ** pgno (which we call pPgOld) though that page is allowed to be
   44643                 : ** in cache.  If the page previously located at pgno is not already
   44644                 : ** in the rollback journal, it is not put there by by this routine.
   44645                 : **
   44646                 : ** References to the page pPg remain valid. Updating any
   44647                 : ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
   44648                 : ** allocated along with the page) is the responsibility of the caller.
   44649                 : **
   44650                 : ** A transaction must be active when this routine is called. It used to be
   44651                 : ** required that a statement transaction was not active, but this restriction
   44652                 : ** has been removed (CREATE INDEX needs to move a page when a statement
   44653                 : ** transaction is active).
   44654                 : **
   44655                 : ** If the fourth argument, isCommit, is non-zero, then this page is being
   44656                 : ** moved as part of a database reorganization just before the transaction 
   44657                 : ** is being committed. In this case, it is guaranteed that the database page 
   44658                 : ** pPg refers to will not be written to again within this transaction.
   44659                 : **
   44660                 : ** This function may return SQLITE_NOMEM or an IO error code if an error
   44661                 : ** occurs. Otherwise, it returns SQLITE_OK.
   44662                 : */
   44663               0 : SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
   44664                 :   PgHdr *pPgOld;               /* The page being overwritten. */
   44665               0 :   Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
   44666                 :   int rc;                      /* Return code */
   44667                 :   Pgno origPgno;               /* The original page number */
   44668                 : 
   44669               0 :   assert( pPg->nRef>0 );
   44670               0 :   assert( pPager->eState==PAGER_WRITER_CACHEMOD
   44671                 :        || pPager->eState==PAGER_WRITER_DBMOD
   44672                 :   );
   44673               0 :   assert( assert_pager_state(pPager) );
   44674                 : 
   44675                 :   /* In order to be able to rollback, an in-memory database must journal
   44676                 :   ** the page we are moving from.
   44677                 :   */
   44678               0 :   if( MEMDB ){
   44679               0 :     rc = sqlite3PagerWrite(pPg);
   44680               0 :     if( rc ) return rc;
   44681                 :   }
   44682                 : 
   44683                 :   /* If the page being moved is dirty and has not been saved by the latest
   44684                 :   ** savepoint, then save the current contents of the page into the 
   44685                 :   ** sub-journal now. This is required to handle the following scenario:
   44686                 :   **
   44687                 :   **   BEGIN;
   44688                 :   **     <journal page X, then modify it in memory>
   44689                 :   **     SAVEPOINT one;
   44690                 :   **       <Move page X to location Y>
   44691                 :   **     ROLLBACK TO one;
   44692                 :   **
   44693                 :   ** If page X were not written to the sub-journal here, it would not
   44694                 :   ** be possible to restore its contents when the "ROLLBACK TO one"
   44695                 :   ** statement were is processed.
   44696                 :   **
   44697                 :   ** subjournalPage() may need to allocate space to store pPg->pgno into
   44698                 :   ** one or more savepoint bitvecs. This is the reason this function
   44699                 :   ** may return SQLITE_NOMEM.
   44700                 :   */
   44701               0 :   if( pPg->flags&PGHDR_DIRTY
   44702               0 :    && subjRequiresPage(pPg)
   44703               0 :    && SQLITE_OK!=(rc = subjournalPage(pPg))
   44704                 :   ){
   44705               0 :     return rc;
   44706                 :   }
   44707                 : 
   44708                 :   PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 
   44709                 :       PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
   44710                 :   IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
   44711                 : 
   44712                 :   /* If the journal needs to be sync()ed before page pPg->pgno can
   44713                 :   ** be written to, store pPg->pgno in local variable needSyncPgno.
   44714                 :   **
   44715                 :   ** If the isCommit flag is set, there is no need to remember that
   44716                 :   ** the journal needs to be sync()ed before database page pPg->pgno 
   44717                 :   ** can be written to. The caller has already promised not to write to it.
   44718                 :   */
   44719               0 :   if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
   44720               0 :     needSyncPgno = pPg->pgno;
   44721               0 :     assert( pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
   44722               0 :     assert( pPg->flags&PGHDR_DIRTY );
   44723                 :   }
   44724                 : 
   44725                 :   /* If the cache contains a page with page-number pgno, remove it
   44726                 :   ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
   44727                 :   ** page pgno before the 'move' operation, it needs to be retained 
   44728                 :   ** for the page moved there.
   44729                 :   */
   44730               0 :   pPg->flags &= ~PGHDR_NEED_SYNC;
   44731               0 :   pPgOld = pager_lookup(pPager, pgno);
   44732               0 :   assert( !pPgOld || pPgOld->nRef==1 );
   44733               0 :   if( pPgOld ){
   44734               0 :     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
   44735               0 :     if( MEMDB ){
   44736                 :       /* Do not discard pages from an in-memory database since we might
   44737                 :       ** need to rollback later.  Just move the page out of the way. */
   44738               0 :       sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
   44739                 :     }else{
   44740               0 :       sqlite3PcacheDrop(pPgOld);
   44741                 :     }
   44742                 :   }
   44743                 : 
   44744               0 :   origPgno = pPg->pgno;
   44745               0 :   sqlite3PcacheMove(pPg, pgno);
   44746               0 :   sqlite3PcacheMakeDirty(pPg);
   44747                 : 
   44748                 :   /* For an in-memory database, make sure the original page continues
   44749                 :   ** to exist, in case the transaction needs to roll back.  Use pPgOld
   44750                 :   ** as the original page since it has already been allocated.
   44751                 :   */
   44752               0 :   if( MEMDB ){
   44753               0 :     assert( pPgOld );
   44754               0 :     sqlite3PcacheMove(pPgOld, origPgno);
   44755               0 :     sqlite3PagerUnref(pPgOld);
   44756                 :   }
   44757                 : 
   44758               0 :   if( needSyncPgno ){
   44759                 :     /* If needSyncPgno is non-zero, then the journal file needs to be 
   44760                 :     ** sync()ed before any data is written to database file page needSyncPgno.
   44761                 :     ** Currently, no such page exists in the page-cache and the 
   44762                 :     ** "is journaled" bitvec flag has been set. This needs to be remedied by
   44763                 :     ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
   44764                 :     ** flag.
   44765                 :     **
   44766                 :     ** If the attempt to load the page into the page-cache fails, (due
   44767                 :     ** to a malloc() or IO failure), clear the bit in the pInJournal[]
   44768                 :     ** array. Otherwise, if the page is loaded and written again in
   44769                 :     ** this transaction, it may be written to the database file before
   44770                 :     ** it is synced into the journal file. This way, it may end up in
   44771                 :     ** the journal file twice, but that is not a problem.
   44772                 :     */
   44773                 :     PgHdr *pPgHdr;
   44774               0 :     rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
   44775               0 :     if( rc!=SQLITE_OK ){
   44776               0 :       if( needSyncPgno<=pPager->dbOrigSize ){
   44777               0 :         assert( pPager->pTmpSpace!=0 );
   44778               0 :         sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
   44779                 :       }
   44780               0 :       return rc;
   44781                 :     }
   44782               0 :     pPgHdr->flags |= PGHDR_NEED_SYNC;
   44783               0 :     sqlite3PcacheMakeDirty(pPgHdr);
   44784               0 :     sqlite3PagerUnref(pPgHdr);
   44785                 :   }
   44786                 : 
   44787               0 :   return SQLITE_OK;
   44788                 : }
   44789                 : #endif
   44790                 : 
   44791                 : /*
   44792                 : ** Return a pointer to the data for the specified page.
   44793                 : */
   44794         1766719 : SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
   44795         1766719 :   assert( pPg->nRef>0 || pPg->pPager->memDb );
   44796         1766719 :   return pPg->pData;
   44797                 : }
   44798                 : 
   44799                 : /*
   44800                 : ** Return a pointer to the Pager.nExtra bytes of "extra" space 
   44801                 : ** allocated along with the specified page.
   44802                 : */
   44803         1766654 : SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
   44804         1766654 :   return pPg->pExtra;
   44805                 : }
   44806                 : 
   44807                 : /*
   44808                 : ** Get/set the locking-mode for this pager. Parameter eMode must be one
   44809                 : ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
   44810                 : ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
   44811                 : ** the locking-mode is set to the value specified.
   44812                 : **
   44813                 : ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
   44814                 : ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
   44815                 : ** locking-mode.
   44816                 : */
   44817            1066 : SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
   44818            1066 :   assert( eMode==PAGER_LOCKINGMODE_QUERY
   44819                 :             || eMode==PAGER_LOCKINGMODE_NORMAL
   44820                 :             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
   44821                 :   assert( PAGER_LOCKINGMODE_QUERY<0 );
   44822                 :   assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
   44823            1066 :   assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
   44824            1066 :   if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
   44825            1060 :     pPager->exclusiveMode = (u8)eMode;
   44826                 :   }
   44827            1066 :   return (int)pPager->exclusiveMode;
   44828                 : }
   44829                 : 
   44830                 : /*
   44831                 : ** Set the journal-mode for this pager. Parameter eMode must be one of:
   44832                 : **
   44833                 : **    PAGER_JOURNALMODE_DELETE
   44834                 : **    PAGER_JOURNALMODE_TRUNCATE
   44835                 : **    PAGER_JOURNALMODE_PERSIST
   44836                 : **    PAGER_JOURNALMODE_OFF
   44837                 : **    PAGER_JOURNALMODE_MEMORY
   44838                 : **    PAGER_JOURNALMODE_WAL
   44839                 : **
   44840                 : ** The journalmode is set to the value specified if the change is allowed.
   44841                 : ** The change may be disallowed for the following reasons:
   44842                 : **
   44843                 : **   *  An in-memory database can only have its journal_mode set to _OFF
   44844                 : **      or _MEMORY.
   44845                 : **
   44846                 : **   *  Temporary databases cannot have _WAL journalmode.
   44847                 : **
   44848                 : ** The returned indicate the current (possibly updated) journal-mode.
   44849                 : */
   44850             517 : SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
   44851             517 :   u8 eOld = pPager->journalMode;    /* Prior journalmode */
   44852                 : 
   44853                 : #ifdef SQLITE_DEBUG
   44854                 :   /* The print_pager_state() routine is intended to be used by the debugger
   44855                 :   ** only.  We invoke it once here to suppress a compiler warning. */
   44856             517 :   print_pager_state(pPager);
   44857                 : #endif
   44858                 : 
   44859                 : 
   44860                 :   /* The eMode parameter is always valid */
   44861             517 :   assert(      eMode==PAGER_JOURNALMODE_DELETE
   44862                 :             || eMode==PAGER_JOURNALMODE_TRUNCATE
   44863                 :             || eMode==PAGER_JOURNALMODE_PERSIST
   44864                 :             || eMode==PAGER_JOURNALMODE_OFF 
   44865                 :             || eMode==PAGER_JOURNALMODE_WAL 
   44866                 :             || eMode==PAGER_JOURNALMODE_MEMORY );
   44867                 : 
   44868                 :   /* This routine is only called from the OP_JournalMode opcode, and
   44869                 :   ** the logic there will never allow a temporary file to be changed
   44870                 :   ** to WAL mode.
   44871                 :   */
   44872             517 :   assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
   44873                 : 
   44874                 :   /* Do allow the journalmode of an in-memory database to be set to
   44875                 :   ** anything other than MEMORY or OFF
   44876                 :   */
   44877             517 :   if( MEMDB ){
   44878               0 :     assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
   44879               0 :     if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
   44880               0 :       eMode = eOld;
   44881                 :     }
   44882                 :   }
   44883                 : 
   44884             517 :   if( eMode!=eOld ){
   44885                 : 
   44886                 :     /* Change the journal mode. */
   44887             476 :     assert( pPager->eState!=PAGER_ERROR );
   44888             476 :     pPager->journalMode = (u8)eMode;
   44889                 : 
   44890                 :     /* When transistioning from TRUNCATE or PERSIST to any other journal
   44891                 :     ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
   44892                 :     ** delete the journal file.
   44893                 :     */
   44894                 :     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
   44895                 :     assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
   44896                 :     assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
   44897                 :     assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
   44898                 :     assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
   44899                 :     assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
   44900                 : 
   44901             476 :     assert( isOpen(pPager->fd) || pPager->exclusiveMode );
   44902             476 :     if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
   44903                 : 
   44904                 :       /* In this case we would like to delete the journal file. If it is
   44905                 :       ** not possible, then that is not a problem. Deleting the journal file
   44906                 :       ** here is an optimization only.
   44907                 :       **
   44908                 :       ** Before deleting the journal file, obtain a RESERVED lock on the
   44909                 :       ** database file. This ensures that the journal file is not deleted
   44910                 :       ** while it is in use by some other client.
   44911                 :       */
   44912               0 :       sqlite3OsClose(pPager->jfd);
   44913               0 :       if( pPager->eLock>=RESERVED_LOCK ){
   44914               0 :         sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
   44915                 :       }else{
   44916               0 :         int rc = SQLITE_OK;
   44917               0 :         int state = pPager->eState;
   44918               0 :         assert( state==PAGER_OPEN || state==PAGER_READER );
   44919               0 :         if( state==PAGER_OPEN ){
   44920               0 :           rc = sqlite3PagerSharedLock(pPager);
   44921                 :         }
   44922               0 :         if( pPager->eState==PAGER_READER ){
   44923               0 :           assert( rc==SQLITE_OK );
   44924               0 :           rc = pagerLockDb(pPager, RESERVED_LOCK);
   44925                 :         }
   44926               0 :         if( rc==SQLITE_OK ){
   44927               0 :           sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
   44928                 :         }
   44929               0 :         if( rc==SQLITE_OK && state==PAGER_READER ){
   44930               0 :           pagerUnlockDb(pPager, SHARED_LOCK);
   44931               0 :         }else if( state==PAGER_OPEN ){
   44932               0 :           pager_unlock(pPager);
   44933                 :         }
   44934               0 :         assert( state==pPager->eState );
   44935                 :       }
   44936                 :     }
   44937                 :   }
   44938                 : 
   44939                 :   /* Return the new journal mode */
   44940             517 :   return (int)pPager->journalMode;
   44941                 : }
   44942                 : 
   44943                 : /*
   44944                 : ** Return the current journal mode.
   44945                 : */
   44946             529 : SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
   44947             529 :   return (int)pPager->journalMode;
   44948                 : }
   44949                 : 
   44950                 : /*
   44951                 : ** Return TRUE if the pager is in a state where it is OK to change the
   44952                 : ** journalmode.  Journalmode changes can only happen when the database
   44953                 : ** is unmodified.
   44954                 : */
   44955             517 : SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
   44956             517 :   assert( assert_pager_state(pPager) );
   44957             517 :   if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
   44958             517 :   if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
   44959             517 :   return 1;
   44960                 : }
   44961                 : 
   44962                 : /*
   44963                 : ** Get/set the size-limit used for persistent journal files.
   44964                 : **
   44965                 : ** Setting the size limit to -1 means no limit is enforced.
   44966                 : ** An attempt to set a limit smaller than -1 is a no-op.
   44967                 : */
   44968             293 : SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
   44969             293 :   if( iLimit>=-1 ){
   44970             279 :     pPager->journalSizeLimit = iLimit;
   44971             279 :     sqlite3WalLimit(pPager->pWal, iLimit);
   44972                 :   }
   44973             293 :   return pPager->journalSizeLimit;
   44974                 : }
   44975                 : 
   44976                 : /*
   44977                 : ** Return a pointer to the pPager->pBackup variable. The backup module
   44978                 : ** in backup.c maintains the content of this variable. This module
   44979                 : ** uses it opaquely as an argument to sqlite3BackupRestart() and
   44980                 : ** sqlite3BackupUpdate() only.
   44981                 : */
   44982               0 : SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
   44983               0 :   return &pPager->pBackup;
   44984                 : }
   44985                 : 
   44986                 : #ifndef SQLITE_OMIT_VACUUM
   44987                 : /*
   44988                 : ** Unless this is an in-memory or temporary database, clear the pager cache.
   44989                 : */
   44990               0 : SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
   44991               0 :   if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
   44992               0 : }
   44993                 : #endif
   44994                 : 
   44995                 : #ifndef SQLITE_OMIT_WAL
   44996                 : /*
   44997                 : ** This function is called when the user invokes "PRAGMA wal_checkpoint",
   44998                 : ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
   44999                 : ** or wal_blocking_checkpoint() API functions.
   45000                 : **
   45001                 : ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
   45002                 : */
   45003            5780 : SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
   45004            5780 :   int rc = SQLITE_OK;
   45005            5780 :   if( pPager->pWal ){
   45006           11032 :     rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
   45007                 :         pPager->xBusyHandler, pPager->pBusyHandlerArg,
   45008           11032 :         pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
   45009                 :         pnLog, pnCkpt
   45010                 :     );
   45011                 :   }
   45012            5780 :   return rc;
   45013                 : }
   45014                 : 
   45015          270810 : SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
   45016          270810 :   return sqlite3WalCallback(pPager->pWal);
   45017                 : }
   45018                 : 
   45019                 : /*
   45020                 : ** Return true if the underlying VFS for the given pager supports the
   45021                 : ** primitives necessary for write-ahead logging.
   45022                 : */
   45023             940 : SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
   45024             940 :   const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
   45025             940 :   return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
   45026                 : }
   45027                 : 
   45028                 : /*
   45029                 : ** Attempt to take an exclusive lock on the database file. If a PENDING lock
   45030                 : ** is obtained instead, immediately release it.
   45031                 : */
   45032               0 : static int pagerExclusiveLock(Pager *pPager){
   45033                 :   int rc;                         /* Return code */
   45034                 : 
   45035               0 :   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
   45036               0 :   rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
   45037               0 :   if( rc!=SQLITE_OK ){
   45038                 :     /* If the attempt to grab the exclusive lock failed, release the 
   45039                 :     ** pending lock that may have been obtained instead.  */
   45040               0 :     pagerUnlockDb(pPager, SHARED_LOCK);
   45041                 :   }
   45042                 : 
   45043               0 :   return rc;
   45044                 : }
   45045                 : 
   45046                 : /*
   45047                 : ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in 
   45048                 : ** exclusive-locking mode when this function is called, take an EXCLUSIVE
   45049                 : ** lock on the database file and use heap-memory to store the wal-index
   45050                 : ** in. Otherwise, use the normal shared-memory.
   45051                 : */
   45052             426 : static int pagerOpenWal(Pager *pPager){
   45053             426 :   int rc = SQLITE_OK;
   45054                 : 
   45055             426 :   assert( pPager->pWal==0 && pPager->tempFile==0 );
   45056             426 :   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK || pPager->noReadlock);
   45057                 : 
   45058                 :   /* If the pager is already in exclusive-mode, the WAL module will use 
   45059                 :   ** heap-memory for the wal-index instead of the VFS shared-memory 
   45060                 :   ** implementation. Take the exclusive lock now, before opening the WAL
   45061                 :   ** file, to make sure this is safe.
   45062                 :   */
   45063             426 :   if( pPager->exclusiveMode ){
   45064               0 :     rc = pagerExclusiveLock(pPager);
   45065                 :   }
   45066                 : 
   45067                 :   /* Open the connection to the log file. If this operation fails, 
   45068                 :   ** (e.g. due to malloc() failure), return an error code.
   45069                 :   */
   45070             426 :   if( rc==SQLITE_OK ){
   45071            1278 :     rc = sqlite3WalOpen(pPager->pVfs, 
   45072             852 :         pPager->fd, pPager->zWal, pPager->exclusiveMode,
   45073                 :         pPager->journalSizeLimit, &pPager->pWal
   45074                 :     );
   45075                 :   }
   45076                 : 
   45077             426 :   return rc;
   45078                 : }
   45079                 : 
   45080                 : 
   45081                 : /*
   45082                 : ** The caller must be holding a SHARED lock on the database file to call
   45083                 : ** this function.
   45084                 : **
   45085                 : ** If the pager passed as the first argument is open on a real database
   45086                 : ** file (not a temp file or an in-memory database), and the WAL file
   45087                 : ** is not already open, make an attempt to open it now. If successful,
   45088                 : ** return SQLITE_OK. If an error occurs or the VFS used by the pager does 
   45089                 : ** not support the xShmXXX() methods, return an error code. *pbOpen is
   45090                 : ** not modified in either case.
   45091                 : **
   45092                 : ** If the pager is open on a temp-file (or in-memory database), or if
   45093                 : ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
   45094                 : ** without doing anything.
   45095                 : */
   45096           40524 : SQLITE_PRIVATE int sqlite3PagerOpenWal(
   45097                 :   Pager *pPager,                  /* Pager object */
   45098                 :   int *pbOpen                     /* OUT: Set to true if call is a no-op */
   45099                 : ){
   45100           40524 :   int rc = SQLITE_OK;             /* Return code */
   45101                 : 
   45102           40524 :   assert( assert_pager_state(pPager) );
   45103           40524 :   assert( pPager->eState==PAGER_OPEN   || pbOpen );
   45104           40524 :   assert( pPager->eState==PAGER_READER || !pbOpen );
   45105           40524 :   assert( pbOpen==0 || *pbOpen==0 );
   45106           40524 :   assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
   45107                 : 
   45108           40524 :   if( !pPager->tempFile && !pPager->pWal ){
   45109             426 :     if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
   45110                 : 
   45111                 :     /* Close any rollback journal previously open */
   45112             426 :     sqlite3OsClose(pPager->jfd);
   45113                 : 
   45114             426 :     rc = pagerOpenWal(pPager);
   45115             852 :     if( rc==SQLITE_OK ){
   45116             426 :       pPager->journalMode = PAGER_JOURNALMODE_WAL;
   45117             426 :       pPager->eState = PAGER_OPEN;
   45118                 :     }
   45119                 :   }else{
   45120           40098 :     *pbOpen = 1;
   45121                 :   }
   45122                 : 
   45123           40524 :   return rc;
   45124                 : }
   45125                 : 
   45126                 : /*
   45127                 : ** This function is called to close the connection to the log file prior
   45128                 : ** to switching from WAL to rollback mode.
   45129                 : **
   45130                 : ** Before closing the log file, this function attempts to take an 
   45131                 : ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
   45132                 : ** error (SQLITE_BUSY) is returned and the log connection is not closed.
   45133                 : ** If successful, the EXCLUSIVE lock is not released before returning.
   45134                 : */
   45135               0 : SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
   45136               0 :   int rc = SQLITE_OK;
   45137                 : 
   45138               0 :   assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
   45139                 : 
   45140                 :   /* If the log file is not already open, but does exist in the file-system,
   45141                 :   ** it may need to be checkpointed before the connection can switch to
   45142                 :   ** rollback mode. Open it now so this can happen.
   45143                 :   */
   45144               0 :   if( !pPager->pWal ){
   45145               0 :     int logexists = 0;
   45146               0 :     rc = pagerLockDb(pPager, SHARED_LOCK);
   45147               0 :     if( rc==SQLITE_OK ){
   45148               0 :       rc = sqlite3OsAccess(
   45149               0 :           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
   45150                 :       );
   45151                 :     }
   45152               0 :     if( rc==SQLITE_OK && logexists ){
   45153               0 :       rc = pagerOpenWal(pPager);
   45154                 :     }
   45155                 :   }
   45156                 :     
   45157                 :   /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
   45158                 :   ** the database file, the log and log-summary files will be deleted.
   45159                 :   */
   45160               0 :   if( rc==SQLITE_OK && pPager->pWal ){
   45161               0 :     rc = pagerExclusiveLock(pPager);
   45162               0 :     if( rc==SQLITE_OK ){
   45163               0 :       rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
   45164               0 :                            pPager->pageSize, (u8*)pPager->pTmpSpace);
   45165               0 :       pPager->pWal = 0;
   45166                 :     }
   45167                 :   }
   45168               0 :   return rc;
   45169                 : }
   45170                 : 
   45171                 : #ifdef SQLITE_HAS_CODEC
   45172                 : /*
   45173                 : ** This function is called by the wal module when writing page content
   45174                 : ** into the log file.
   45175                 : **
   45176                 : ** This function returns a pointer to a buffer containing the encrypted
   45177                 : ** page content. If a malloc fails, this function may return NULL.
   45178                 : */
   45179                 : SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
   45180                 :   void *aData = 0;
   45181                 :   CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
   45182                 :   return aData;
   45183                 : }
   45184                 : #endif /* SQLITE_HAS_CODEC */
   45185                 : 
   45186                 : #endif /* !SQLITE_OMIT_WAL */
   45187                 : 
   45188                 : #endif /* SQLITE_OMIT_DISKIO */
   45189                 : 
   45190                 : /************** End of pager.c ***********************************************/
   45191                 : /************** Begin file wal.c *********************************************/
   45192                 : /*
   45193                 : ** 2010 February 1
   45194                 : **
   45195                 : ** The author disclaims copyright to this source code.  In place of
   45196                 : ** a legal notice, here is a blessing:
   45197                 : **
   45198                 : **    May you do good and not evil.
   45199                 : **    May you find forgiveness for yourself and forgive others.
   45200                 : **    May you share freely, never taking more than you give.
   45201                 : **
   45202                 : *************************************************************************
   45203                 : **
   45204                 : ** This file contains the implementation of a write-ahead log (WAL) used in 
   45205                 : ** "journal_mode=WAL" mode.
   45206                 : **
   45207                 : ** WRITE-AHEAD LOG (WAL) FILE FORMAT
   45208                 : **
   45209                 : ** A WAL file consists of a header followed by zero or more "frames".
   45210                 : ** Each frame records the revised content of a single page from the
   45211                 : ** database file.  All changes to the database are recorded by writing
   45212                 : ** frames into the WAL.  Transactions commit when a frame is written that
   45213                 : ** contains a commit marker.  A single WAL can and usually does record 
   45214                 : ** multiple transactions.  Periodically, the content of the WAL is
   45215                 : ** transferred back into the database file in an operation called a
   45216                 : ** "checkpoint".
   45217                 : **
   45218                 : ** A single WAL file can be used multiple times.  In other words, the
   45219                 : ** WAL can fill up with frames and then be checkpointed and then new
   45220                 : ** frames can overwrite the old ones.  A WAL always grows from beginning
   45221                 : ** toward the end.  Checksums and counters attached to each frame are
   45222                 : ** used to determine which frames within the WAL are valid and which
   45223                 : ** are leftovers from prior checkpoints.
   45224                 : **
   45225                 : ** The WAL header is 32 bytes in size and consists of the following eight
   45226                 : ** big-endian 32-bit unsigned integer values:
   45227                 : **
   45228                 : **     0: Magic number.  0x377f0682 or 0x377f0683
   45229                 : **     4: File format version.  Currently 3007000
   45230                 : **     8: Database page size.  Example: 1024
   45231                 : **    12: Checkpoint sequence number
   45232                 : **    16: Salt-1, random integer incremented with each checkpoint
   45233                 : **    20: Salt-2, a different random integer changing with each ckpt
   45234                 : **    24: Checksum-1 (first part of checksum for first 24 bytes of header).
   45235                 : **    28: Checksum-2 (second part of checksum for first 24 bytes of header).
   45236                 : **
   45237                 : ** Immediately following the wal-header are zero or more frames. Each
   45238                 : ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
   45239                 : ** of page data. The frame-header is six big-endian 32-bit unsigned 
   45240                 : ** integer values, as follows:
   45241                 : **
   45242                 : **     0: Page number.
   45243                 : **     4: For commit records, the size of the database image in pages 
   45244                 : **        after the commit. For all other records, zero.
   45245                 : **     8: Salt-1 (copied from the header)
   45246                 : **    12: Salt-2 (copied from the header)
   45247                 : **    16: Checksum-1.
   45248                 : **    20: Checksum-2.
   45249                 : **
   45250                 : ** A frame is considered valid if and only if the following conditions are
   45251                 : ** true:
   45252                 : **
   45253                 : **    (1) The salt-1 and salt-2 values in the frame-header match
   45254                 : **        salt values in the wal-header
   45255                 : **
   45256                 : **    (2) The checksum values in the final 8 bytes of the frame-header
   45257                 : **        exactly match the checksum computed consecutively on the
   45258                 : **        WAL header and the first 8 bytes and the content of all frames
   45259                 : **        up to and including the current frame.
   45260                 : **
   45261                 : ** The checksum is computed using 32-bit big-endian integers if the
   45262                 : ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
   45263                 : ** is computed using little-endian if the magic number is 0x377f0682.
   45264                 : ** The checksum values are always stored in the frame header in a
   45265                 : ** big-endian format regardless of which byte order is used to compute
   45266                 : ** the checksum.  The checksum is computed by interpreting the input as
   45267                 : ** an even number of unsigned 32-bit integers: x[0] through x[N].  The
   45268                 : ** algorithm used for the checksum is as follows:
   45269                 : ** 
   45270                 : **   for i from 0 to n-1 step 2:
   45271                 : **     s0 += x[i] + s1;
   45272                 : **     s1 += x[i+1] + s0;
   45273                 : **   endfor
   45274                 : **
   45275                 : ** Note that s0 and s1 are both weighted checksums using fibonacci weights
   45276                 : ** in reverse order (the largest fibonacci weight occurs on the first element
   45277                 : ** of the sequence being summed.)  The s1 value spans all 32-bit 
   45278                 : ** terms of the sequence whereas s0 omits the final term.
   45279                 : **
   45280                 : ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
   45281                 : ** WAL is transferred into the database, then the database is VFS.xSync-ed.
   45282                 : ** The VFS.xSync operations serve as write barriers - all writes launched
   45283                 : ** before the xSync must complete before any write that launches after the
   45284                 : ** xSync begins.
   45285                 : **
   45286                 : ** After each checkpoint, the salt-1 value is incremented and the salt-2
   45287                 : ** value is randomized.  This prevents old and new frames in the WAL from
   45288                 : ** being considered valid at the same time and being checkpointing together
   45289                 : ** following a crash.
   45290                 : **
   45291                 : ** READER ALGORITHM
   45292                 : **
   45293                 : ** To read a page from the database (call it page number P), a reader
   45294                 : ** first checks the WAL to see if it contains page P.  If so, then the
   45295                 : ** last valid instance of page P that is a followed by a commit frame
   45296                 : ** or is a commit frame itself becomes the value read.  If the WAL
   45297                 : ** contains no copies of page P that are valid and which are a commit
   45298                 : ** frame or are followed by a commit frame, then page P is read from
   45299                 : ** the database file.
   45300                 : **
   45301                 : ** To start a read transaction, the reader records the index of the last
   45302                 : ** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
   45303                 : ** for all subsequent read operations.  New transactions can be appended
   45304                 : ** to the WAL, but as long as the reader uses its original mxFrame value
   45305                 : ** and ignores the newly appended content, it will see a consistent snapshot
   45306                 : ** of the database from a single point in time.  This technique allows
   45307                 : ** multiple concurrent readers to view different versions of the database
   45308                 : ** content simultaneously.
   45309                 : **
   45310                 : ** The reader algorithm in the previous paragraphs works correctly, but 
   45311                 : ** because frames for page P can appear anywhere within the WAL, the
   45312                 : ** reader has to scan the entire WAL looking for page P frames.  If the
   45313                 : ** WAL is large (multiple megabytes is typical) that scan can be slow,
   45314                 : ** and read performance suffers.  To overcome this problem, a separate
   45315                 : ** data structure called the wal-index is maintained to expedite the
   45316                 : ** search for frames of a particular page.
   45317                 : ** 
   45318                 : ** WAL-INDEX FORMAT
   45319                 : **
   45320                 : ** Conceptually, the wal-index is shared memory, though VFS implementations
   45321                 : ** might choose to implement the wal-index using a mmapped file.  Because
   45322                 : ** the wal-index is shared memory, SQLite does not support journal_mode=WAL 
   45323                 : ** on a network filesystem.  All users of the database must be able to
   45324                 : ** share memory.
   45325                 : **
   45326                 : ** The wal-index is transient.  After a crash, the wal-index can (and should
   45327                 : ** be) reconstructed from the original WAL file.  In fact, the VFS is required
   45328                 : ** to either truncate or zero the header of the wal-index when the last
   45329                 : ** connection to it closes.  Because the wal-index is transient, it can
   45330                 : ** use an architecture-specific format; it does not have to be cross-platform.
   45331                 : ** Hence, unlike the database and WAL file formats which store all values
   45332                 : ** as big endian, the wal-index can store multi-byte values in the native
   45333                 : ** byte order of the host computer.
   45334                 : **
   45335                 : ** The purpose of the wal-index is to answer this question quickly:  Given
   45336                 : ** a page number P, return the index of the last frame for page P in the WAL,
   45337                 : ** or return NULL if there are no frames for page P in the WAL.
   45338                 : **
   45339                 : ** The wal-index consists of a header region, followed by an one or
   45340                 : ** more index blocks.  
   45341                 : **
   45342                 : ** The wal-index header contains the total number of frames within the WAL
   45343                 : ** in the the mxFrame field.  
   45344                 : **
   45345                 : ** Each index block except for the first contains information on 
   45346                 : ** HASHTABLE_NPAGE frames. The first index block contains information on
   45347                 : ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and 
   45348                 : ** HASHTABLE_NPAGE are selected so that together the wal-index header and
   45349                 : ** first index block are the same size as all other index blocks in the
   45350                 : ** wal-index.
   45351                 : **
   45352                 : ** Each index block contains two sections, a page-mapping that contains the
   45353                 : ** database page number associated with each wal frame, and a hash-table 
   45354                 : ** that allows readers to query an index block for a specific page number.
   45355                 : ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
   45356                 : ** for the first index block) 32-bit page numbers. The first entry in the 
   45357                 : ** first index-block contains the database page number corresponding to the
   45358                 : ** first frame in the WAL file. The first entry in the second index block
   45359                 : ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
   45360                 : ** the log, and so on.
   45361                 : **
   45362                 : ** The last index block in a wal-index usually contains less than the full
   45363                 : ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
   45364                 : ** depending on the contents of the WAL file. This does not change the
   45365                 : ** allocated size of the page-mapping array - the page-mapping array merely
   45366                 : ** contains unused entries.
   45367                 : **
   45368                 : ** Even without using the hash table, the last frame for page P
   45369                 : ** can be found by scanning the page-mapping sections of each index block
   45370                 : ** starting with the last index block and moving toward the first, and
   45371                 : ** within each index block, starting at the end and moving toward the
   45372                 : ** beginning.  The first entry that equals P corresponds to the frame
   45373                 : ** holding the content for that page.
   45374                 : **
   45375                 : ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
   45376                 : ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
   45377                 : ** hash table for each page number in the mapping section, so the hash 
   45378                 : ** table is never more than half full.  The expected number of collisions 
   45379                 : ** prior to finding a match is 1.  Each entry of the hash table is an
   45380                 : ** 1-based index of an entry in the mapping section of the same
   45381                 : ** index block.   Let K be the 1-based index of the largest entry in
   45382                 : ** the mapping section.  (For index blocks other than the last, K will
   45383                 : ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
   45384                 : ** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
   45385                 : ** contain a value of 0.
   45386                 : **
   45387                 : ** To look for page P in the hash table, first compute a hash iKey on
   45388                 : ** P as follows:
   45389                 : **
   45390                 : **      iKey = (P * 383) % HASHTABLE_NSLOT
   45391                 : **
   45392                 : ** Then start scanning entries of the hash table, starting with iKey
   45393                 : ** (wrapping around to the beginning when the end of the hash table is
   45394                 : ** reached) until an unused hash slot is found. Let the first unused slot
   45395                 : ** be at index iUnused.  (iUnused might be less than iKey if there was
   45396                 : ** wrap-around.) Because the hash table is never more than half full,
   45397                 : ** the search is guaranteed to eventually hit an unused entry.  Let 
   45398                 : ** iMax be the value between iKey and iUnused, closest to iUnused,
   45399                 : ** where aHash[iMax]==P.  If there is no iMax entry (if there exists
   45400                 : ** no hash slot such that aHash[i]==p) then page P is not in the
   45401                 : ** current index block.  Otherwise the iMax-th mapping entry of the
   45402                 : ** current index block corresponds to the last entry that references 
   45403                 : ** page P.
   45404                 : **
   45405                 : ** A hash search begins with the last index block and moves toward the
   45406                 : ** first index block, looking for entries corresponding to page P.  On
   45407                 : ** average, only two or three slots in each index block need to be
   45408                 : ** examined in order to either find the last entry for page P, or to
   45409                 : ** establish that no such entry exists in the block.  Each index block
   45410                 : ** holds over 4000 entries.  So two or three index blocks are sufficient
   45411                 : ** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
   45412                 : ** comparisons (on average) suffice to either locate a frame in the
   45413                 : ** WAL or to establish that the frame does not exist in the WAL.  This
   45414                 : ** is much faster than scanning the entire 10MB WAL.
   45415                 : **
   45416                 : ** Note that entries are added in order of increasing K.  Hence, one
   45417                 : ** reader might be using some value K0 and a second reader that started
   45418                 : ** at a later time (after additional transactions were added to the WAL
   45419                 : ** and to the wal-index) might be using a different value K1, where K1>K0.
   45420                 : ** Both readers can use the same hash table and mapping section to get
   45421                 : ** the correct result.  There may be entries in the hash table with
   45422                 : ** K>K0 but to the first reader, those entries will appear to be unused
   45423                 : ** slots in the hash table and so the first reader will get an answer as
   45424                 : ** if no values greater than K0 had ever been inserted into the hash table
   45425                 : ** in the first place - which is what reader one wants.  Meanwhile, the
   45426                 : ** second reader using K1 will see additional values that were inserted
   45427                 : ** later, which is exactly what reader two wants.  
   45428                 : **
   45429                 : ** When a rollback occurs, the value of K is decreased. Hash table entries
   45430                 : ** that correspond to frames greater than the new K value are removed
   45431                 : ** from the hash table at this point.
   45432                 : */
   45433                 : #ifndef SQLITE_OMIT_WAL
   45434                 : 
   45435                 : 
   45436                 : /*
   45437                 : ** Trace output macros
   45438                 : */
   45439                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   45440                 : SQLITE_PRIVATE int sqlite3WalTrace = 0;
   45441                 : # define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
   45442                 : #else
   45443                 : # define WALTRACE(X)
   45444                 : #endif
   45445                 : 
   45446                 : /*
   45447                 : ** The maximum (and only) versions of the wal and wal-index formats
   45448                 : ** that may be interpreted by this version of SQLite.
   45449                 : **
   45450                 : ** If a client begins recovering a WAL file and finds that (a) the checksum
   45451                 : ** values in the wal-header are correct and (b) the version field is not
   45452                 : ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
   45453                 : **
   45454                 : ** Similarly, if a client successfully reads a wal-index header (i.e. the 
   45455                 : ** checksum test is successful) and finds that the version field is not
   45456                 : ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
   45457                 : ** returns SQLITE_CANTOPEN.
   45458                 : */
   45459                 : #define WAL_MAX_VERSION      3007000
   45460                 : #define WALINDEX_MAX_VERSION 3007000
   45461                 : 
   45462                 : /*
   45463                 : ** Indices of various locking bytes.   WAL_NREADER is the number
   45464                 : ** of available reader locks and should be at least 3.
   45465                 : */
   45466                 : #define WAL_WRITE_LOCK         0
   45467                 : #define WAL_ALL_BUT_WRITE      1
   45468                 : #define WAL_CKPT_LOCK          1
   45469                 : #define WAL_RECOVER_LOCK       2
   45470                 : #define WAL_READ_LOCK(I)       (3+(I))
   45471                 : #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
   45472                 : 
   45473                 : 
   45474                 : /* Object declarations */
   45475                 : typedef struct WalIndexHdr WalIndexHdr;
   45476                 : typedef struct WalIterator WalIterator;
   45477                 : typedef struct WalCkptInfo WalCkptInfo;
   45478                 : 
   45479                 : 
   45480                 : /*
   45481                 : ** The following object holds a copy of the wal-index header content.
   45482                 : **
   45483                 : ** The actual header in the wal-index consists of two copies of this
   45484                 : ** object.
   45485                 : **
   45486                 : ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
   45487                 : ** Or it can be 1 to represent a 65536-byte page.  The latter case was
   45488                 : ** added in 3.7.1 when support for 64K pages was added.  
   45489                 : */
   45490                 : struct WalIndexHdr {
   45491                 :   u32 iVersion;                   /* Wal-index version */
   45492                 :   u32 unused;                     /* Unused (padding) field */
   45493                 :   u32 iChange;                    /* Counter incremented each transaction */
   45494                 :   u8 isInit;                      /* 1 when initialized */
   45495                 :   u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
   45496                 :   u16 szPage;                     /* Database page size in bytes. 1==64K */
   45497                 :   u32 mxFrame;                    /* Index of last valid frame in the WAL */
   45498                 :   u32 nPage;                      /* Size of database in pages */
   45499                 :   u32 aFrameCksum[2];             /* Checksum of last frame in log */
   45500                 :   u32 aSalt[2];                   /* Two salt values copied from WAL header */
   45501                 :   u32 aCksum[2];                  /* Checksum over all prior fields */
   45502                 : };
   45503                 : 
   45504                 : /*
   45505                 : ** A copy of the following object occurs in the wal-index immediately
   45506                 : ** following the second copy of the WalIndexHdr.  This object stores
   45507                 : ** information used by checkpoint.
   45508                 : **
   45509                 : ** nBackfill is the number of frames in the WAL that have been written
   45510                 : ** back into the database. (We call the act of moving content from WAL to
   45511                 : ** database "backfilling".)  The nBackfill number is never greater than
   45512                 : ** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
   45513                 : ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
   45514                 : ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
   45515                 : ** mxFrame back to zero when the WAL is reset.
   45516                 : **
   45517                 : ** There is one entry in aReadMark[] for each reader lock.  If a reader
   45518                 : ** holds read-lock K, then the value in aReadMark[K] is no greater than
   45519                 : ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
   45520                 : ** for any aReadMark[] means that entry is unused.  aReadMark[0] is 
   45521                 : ** a special case; its value is never used and it exists as a place-holder
   45522                 : ** to avoid having to offset aReadMark[] indexs by one.  Readers holding
   45523                 : ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
   45524                 : ** directly from the database.
   45525                 : **
   45526                 : ** The value of aReadMark[K] may only be changed by a thread that
   45527                 : ** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
   45528                 : ** aReadMark[K] cannot changed while there is a reader is using that mark
   45529                 : ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
   45530                 : **
   45531                 : ** The checkpointer may only transfer frames from WAL to database where
   45532                 : ** the frame numbers are less than or equal to every aReadMark[] that is
   45533                 : ** in use (that is, every aReadMark[j] for which there is a corresponding
   45534                 : ** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
   45535                 : ** largest value and will increase an unused aReadMark[] to mxFrame if there
   45536                 : ** is not already an aReadMark[] equal to mxFrame.  The exception to the
   45537                 : ** previous sentence is when nBackfill equals mxFrame (meaning that everything
   45538                 : ** in the WAL has been backfilled into the database) then new readers
   45539                 : ** will choose aReadMark[0] which has value 0 and hence such reader will
   45540                 : ** get all their all content directly from the database file and ignore 
   45541                 : ** the WAL.
   45542                 : **
   45543                 : ** Writers normally append new frames to the end of the WAL.  However,
   45544                 : ** if nBackfill equals mxFrame (meaning that all WAL content has been
   45545                 : ** written back into the database) and if no readers are using the WAL
   45546                 : ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
   45547                 : ** the writer will first "reset" the WAL back to the beginning and start
   45548                 : ** writing new content beginning at frame 1.
   45549                 : **
   45550                 : ** We assume that 32-bit loads are atomic and so no locks are needed in
   45551                 : ** order to read from any aReadMark[] entries.
   45552                 : */
   45553                 : struct WalCkptInfo {
   45554                 :   u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
   45555                 :   u32 aReadMark[WAL_NREADER];     /* Reader marks */
   45556                 : };
   45557                 : #define READMARK_NOT_USED  0xffffffff
   45558                 : 
   45559                 : 
   45560                 : /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
   45561                 : ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
   45562                 : ** only support mandatory file-locks, we do not read or write data
   45563                 : ** from the region of the file on which locks are applied.
   45564                 : */
   45565                 : #define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
   45566                 : #define WALINDEX_LOCK_RESERVED 16
   45567                 : #define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
   45568                 : 
   45569                 : /* Size of header before each frame in wal */
   45570                 : #define WAL_FRAME_HDRSIZE 24
   45571                 : 
   45572                 : /* Size of write ahead log header, including checksum. */
   45573                 : /* #define WAL_HDRSIZE 24 */
   45574                 : #define WAL_HDRSIZE 32
   45575                 : 
   45576                 : /* WAL magic value. Either this value, or the same value with the least
   45577                 : ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
   45578                 : ** big-endian format in the first 4 bytes of a WAL file.
   45579                 : **
   45580                 : ** If the LSB is set, then the checksums for each frame within the WAL
   45581                 : ** file are calculated by treating all data as an array of 32-bit 
   45582                 : ** big-endian words. Otherwise, they are calculated by interpreting 
   45583                 : ** all data as 32-bit little-endian words.
   45584                 : */
   45585                 : #define WAL_MAGIC 0x377f0682
   45586                 : 
   45587                 : /*
   45588                 : ** Return the offset of frame iFrame in the write-ahead log file, 
   45589                 : ** assuming a database page size of szPage bytes. The offset returned
   45590                 : ** is to the start of the write-ahead log frame-header.
   45591                 : */
   45592                 : #define walFrameOffset(iFrame, szPage) (                               \
   45593                 :   WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
   45594                 : )
   45595                 : 
   45596                 : /*
   45597                 : ** An open write-ahead log file is represented by an instance of the
   45598                 : ** following object.
   45599                 : */
   45600                 : struct Wal {
   45601                 :   sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
   45602                 :   sqlite3_file *pDbFd;       /* File handle for the database file */
   45603                 :   sqlite3_file *pWalFd;      /* File handle for WAL file */
   45604                 :   u32 iCallback;             /* Value to pass to log callback (or 0) */
   45605                 :   i64 mxWalSize;             /* Truncate WAL to this size upon reset */
   45606                 :   int nWiData;               /* Size of array apWiData */
   45607                 :   int szFirstBlock;          /* Size of first block written to WAL file */
   45608                 :   volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
   45609                 :   u32 szPage;                /* Database page size */
   45610                 :   i16 readLock;              /* Which read lock is being held.  -1 for none */
   45611                 :   u8 syncFlags;              /* Flags to use to sync header writes */
   45612                 :   u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
   45613                 :   u8 writeLock;              /* True if in a write transaction */
   45614                 :   u8 ckptLock;               /* True if holding a checkpoint lock */
   45615                 :   u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
   45616                 :   u8 truncateOnCommit;       /* True to truncate WAL file on commit */
   45617                 :   u8 syncHeader;             /* Fsync the WAL header if true */
   45618                 :   u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
   45619                 :   WalIndexHdr hdr;           /* Wal-index header for current transaction */
   45620                 :   const char *zWalName;      /* Name of WAL file */
   45621                 :   u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
   45622                 : #ifdef SQLITE_DEBUG
   45623                 :   u8 lockError;              /* True if a locking error has occurred */
   45624                 : #endif
   45625                 : };
   45626                 : 
   45627                 : /*
   45628                 : ** Candidate values for Wal.exclusiveMode.
   45629                 : */
   45630                 : #define WAL_NORMAL_MODE     0
   45631                 : #define WAL_EXCLUSIVE_MODE  1     
   45632                 : #define WAL_HEAPMEMORY_MODE 2
   45633                 : 
   45634                 : /*
   45635                 : ** Possible values for WAL.readOnly
   45636                 : */
   45637                 : #define WAL_RDWR        0    /* Normal read/write connection */
   45638                 : #define WAL_RDONLY      1    /* The WAL file is readonly */
   45639                 : #define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
   45640                 : 
   45641                 : /*
   45642                 : ** Each page of the wal-index mapping contains a hash-table made up of
   45643                 : ** an array of HASHTABLE_NSLOT elements of the following type.
   45644                 : */
   45645                 : typedef u16 ht_slot;
   45646                 : 
   45647                 : /*
   45648                 : ** This structure is used to implement an iterator that loops through
   45649                 : ** all frames in the WAL in database page order. Where two or more frames
   45650                 : ** correspond to the same database page, the iterator visits only the 
   45651                 : ** frame most recently written to the WAL (in other words, the frame with
   45652                 : ** the largest index).
   45653                 : **
   45654                 : ** The internals of this structure are only accessed by:
   45655                 : **
   45656                 : **   walIteratorInit() - Create a new iterator,
   45657                 : **   walIteratorNext() - Step an iterator,
   45658                 : **   walIteratorFree() - Free an iterator.
   45659                 : **
   45660                 : ** This functionality is used by the checkpoint code (see walCheckpoint()).
   45661                 : */
   45662                 : struct WalIterator {
   45663                 :   int iPrior;                     /* Last result returned from the iterator */
   45664                 :   int nSegment;                   /* Number of entries in aSegment[] */
   45665                 :   struct WalSegment {
   45666                 :     int iNext;                    /* Next slot in aIndex[] not yet returned */
   45667                 :     ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
   45668                 :     u32 *aPgno;                   /* Array of page numbers. */
   45669                 :     int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
   45670                 :     int iZero;                    /* Frame number associated with aPgno[0] */
   45671                 :   } aSegment[1];                  /* One for every 32KB page in the wal-index */
   45672                 : };
   45673                 : 
   45674                 : /*
   45675                 : ** Define the parameters of the hash tables in the wal-index file. There
   45676                 : ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
   45677                 : ** wal-index.
   45678                 : **
   45679                 : ** Changing any of these constants will alter the wal-index format and
   45680                 : ** create incompatibilities.
   45681                 : */
   45682                 : #define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
   45683                 : #define HASHTABLE_HASH_1     383                  /* Should be prime */
   45684                 : #define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
   45685                 : 
   45686                 : /* 
   45687                 : ** The block of page numbers associated with the first hash-table in a
   45688                 : ** wal-index is smaller than usual. This is so that there is a complete
   45689                 : ** hash-table on each aligned 32KB page of the wal-index.
   45690                 : */
   45691                 : #define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
   45692                 : 
   45693                 : /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
   45694                 : #define WALINDEX_PGSZ   (                                         \
   45695                 :     sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
   45696                 : )
   45697                 : 
   45698                 : /*
   45699                 : ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
   45700                 : ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
   45701                 : ** numbered from zero.
   45702                 : **
   45703                 : ** If this call is successful, *ppPage is set to point to the wal-index
   45704                 : ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
   45705                 : ** then an SQLite error code is returned and *ppPage is set to 0.
   45706                 : */
   45707          154271 : static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
   45708          154271 :   int rc = SQLITE_OK;
   45709                 : 
   45710                 :   /* Enlarge the pWal->apWiData[] array if required */
   45711          154271 :   if( pWal->nWiData<=iPage ){
   45712             426 :     int nByte = sizeof(u32*)*(iPage+1);
   45713                 :     volatile u32 **apNew;
   45714             426 :     apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
   45715             426 :     if( !apNew ){
   45716               0 :       *ppPage = 0;
   45717               0 :       return SQLITE_NOMEM;
   45718                 :     }
   45719             426 :     memset((void*)&apNew[pWal->nWiData], 0,
   45720             426 :            sizeof(u32*)*(iPage+1-pWal->nWiData));
   45721             426 :     pWal->apWiData = apNew;
   45722             426 :     pWal->nWiData = iPage+1;
   45723                 :   }
   45724                 : 
   45725                 :   /* Request a pointer to the required page from the VFS */
   45726          154271 :   if( pWal->apWiData[iPage]==0 ){
   45727             779 :     if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
   45728               0 :       pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
   45729               0 :       if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
   45730                 :     }else{
   45731            2337 :       rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
   45732            2337 :           pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
   45733                 :       );
   45734             779 :       if( rc==SQLITE_READONLY ){
   45735               0 :         pWal->readOnly |= WAL_SHM_RDONLY;
   45736               0 :         rc = SQLITE_OK;
   45737                 :       }
   45738                 :     }
   45739                 :   }
   45740                 : 
   45741          154271 :   *ppPage = pWal->apWiData[iPage];
   45742          154271 :   assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
   45743          154271 :   return rc;
   45744                 : }
   45745                 : 
   45746                 : /*
   45747                 : ** Return a pointer to the WalCkptInfo structure in the wal-index.
   45748                 : */
   45749           57209 : static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
   45750           57209 :   assert( pWal->nWiData>0 && pWal->apWiData[0] );
   45751           57209 :   return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
   45752                 : }
   45753                 : 
   45754                 : /*
   45755                 : ** Return a pointer to the WalIndexHdr structure in the wal-index.
   45756                 : */
   45757          153758 : static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
   45758          153758 :   assert( pWal->nWiData>0 && pWal->apWiData[0] );
   45759          153758 :   return (volatile WalIndexHdr*)pWal->apWiData[0];
   45760                 : }
   45761                 : 
   45762                 : /*
   45763                 : ** The argument to this macro must be of type u32. On a little-endian
   45764                 : ** architecture, it returns the u32 value that results from interpreting
   45765                 : ** the 4 bytes as a big-endian value. On a big-endian architecture, it
   45766                 : ** returns the value that would be produced by intepreting the 4 bytes
   45767                 : ** of the input value as a little-endian integer.
   45768                 : */
   45769                 : #define BYTESWAP32(x) ( \
   45770                 :     (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
   45771                 :   + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
   45772                 : )
   45773                 : 
   45774                 : /*
   45775                 : ** Generate or extend an 8 byte checksum based on the data in 
   45776                 : ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
   45777                 : ** initial values of 0 and 0 if aIn==NULL).
   45778                 : **
   45779                 : ** The checksum is written back into aOut[] before returning.
   45780                 : **
   45781                 : ** nByte must be a positive multiple of 8.
   45782                 : */
   45783          284705 : static void walChecksumBytes(
   45784                 :   int nativeCksum, /* True for native byte-order, false for non-native */
   45785                 :   u8 *a,           /* Content to be checksummed */
   45786                 :   int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
   45787                 :   const u32 *aIn,  /* Initial checksum value input */
   45788                 :   u32 *aOut        /* OUT: Final checksum value output */
   45789                 : ){
   45790                 :   u32 s1, s2;
   45791          284705 :   u32 *aData = (u32 *)a;
   45792          284705 :   u32 *aEnd = (u32 *)&a[nByte];
   45793                 : 
   45794          284705 :   if( aIn ){
   45795          203378 :     s1 = aIn[0];
   45796          203378 :     s2 = aIn[1];
   45797                 :   }else{
   45798           81327 :     s1 = s2 = 0;
   45799                 :   }
   45800                 : 
   45801          284705 :   assert( nByte>=8 );
   45802          284705 :   assert( (nByte&0x00000007)==0 );
   45803                 : 
   45804          284705 :   if( nativeCksum ){
   45805                 :     do {
   45806       416711202 :       s1 += *aData++ + s2;
   45807       416711202 :       s2 += *aData++ + s1;
   45808       416711202 :     }while( aData<aEnd );
   45809                 :   }else{
   45810                 :     do {
   45811               0 :       s1 += BYTESWAP32(aData[0]) + s2;
   45812               0 :       s2 += BYTESWAP32(aData[1]) + s1;
   45813               0 :       aData += 2;
   45814               0 :     }while( aData<aEnd );
   45815                 :   }
   45816                 : 
   45817          284705 :   aOut[0] = s1;
   45818          284705 :   aOut[1] = s2;
   45819          284705 : }
   45820                 : 
   45821          121778 : static void walShmBarrier(Wal *pWal){
   45822          121778 :   if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
   45823          121778 :     sqlite3OsShmBarrier(pWal->pDbFd);
   45824                 :   }
   45825          121778 : }
   45826                 : 
   45827                 : /*
   45828                 : ** Write the header information in pWal->hdr into the wal-index.
   45829                 : **
   45830                 : ** The checksum on pWal->hdr is updated before it is written.
   45831                 : */
   45832           30401 : static void walIndexWriteHdr(Wal *pWal){
   45833           30401 :   volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
   45834           30401 :   const int nCksum = offsetof(WalIndexHdr, aCksum);
   45835                 : 
   45836           30401 :   assert( pWal->writeLock );
   45837           30401 :   pWal->hdr.isInit = 1;
   45838           30401 :   pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
   45839           30401 :   walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
   45840           30401 :   memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
   45841           30401 :   walShmBarrier(pWal);
   45842           30401 :   memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
   45843           30401 : }
   45844                 : 
   45845                 : /*
   45846                 : ** This function encodes a single frame header and writes it to a buffer
   45847                 : ** supplied by the caller. A frame-header is made up of a series of 
   45848                 : ** 4-byte big-endian integers, as follows:
   45849                 : **
   45850                 : **     0: Page number.
   45851                 : **     4: For commit records, the size of the database image in pages 
   45852                 : **        after the commit. For all other records, zero.
   45853                 : **     8: Salt-1 (copied from the wal-header)
   45854                 : **    12: Salt-2 (copied from the wal-header)
   45855                 : **    16: Checksum-1.
   45856                 : **    20: Checksum-2.
   45857                 : */
   45858          101689 : static void walEncodeFrame(
   45859                 :   Wal *pWal,                      /* The write-ahead log */
   45860                 :   u32 iPage,                      /* Database page number for frame */
   45861                 :   u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
   45862                 :   u8 *aData,                      /* Pointer to page data */
   45863                 :   u8 *aFrame                      /* OUT: Write encoded frame here */
   45864                 : ){
   45865                 :   int nativeCksum;                /* True for native byte-order checksums */
   45866          101689 :   u32 *aCksum = pWal->hdr.aFrameCksum;
   45867                 :   assert( WAL_FRAME_HDRSIZE==24 );
   45868          101689 :   sqlite3Put4byte(&aFrame[0], iPage);
   45869          101689 :   sqlite3Put4byte(&aFrame[4], nTruncate);
   45870          101689 :   memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
   45871                 : 
   45872          101689 :   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
   45873          101689 :   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
   45874          101689 :   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
   45875                 : 
   45876          101689 :   sqlite3Put4byte(&aFrame[16], aCksum[0]);
   45877          101689 :   sqlite3Put4byte(&aFrame[20], aCksum[1]);
   45878          101689 : }
   45879                 : 
   45880                 : /*
   45881                 : ** Check to see if the frame with header in aFrame[] and content
   45882                 : ** in aData[] is valid.  If it is a valid frame, fill *piPage and
   45883                 : ** *pnTruncate and return true.  Return if the frame is not valid.
   45884                 : */
   45885               0 : static int walDecodeFrame(
   45886                 :   Wal *pWal,                      /* The write-ahead log */
   45887                 :   u32 *piPage,                    /* OUT: Database page number for frame */
   45888                 :   u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
   45889                 :   u8 *aData,                      /* Pointer to page data (for checksum) */
   45890                 :   u8 *aFrame                      /* Frame data */
   45891                 : ){
   45892                 :   int nativeCksum;                /* True for native byte-order checksums */
   45893               0 :   u32 *aCksum = pWal->hdr.aFrameCksum;
   45894                 :   u32 pgno;                       /* Page number of the frame */
   45895                 :   assert( WAL_FRAME_HDRSIZE==24 );
   45896                 : 
   45897                 :   /* A frame is only valid if the salt values in the frame-header
   45898                 :   ** match the salt values in the wal-header. 
   45899                 :   */
   45900               0 :   if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
   45901               0 :     return 0;
   45902                 :   }
   45903                 : 
   45904                 :   /* A frame is only valid if the page number is creater than zero.
   45905                 :   */
   45906               0 :   pgno = sqlite3Get4byte(&aFrame[0]);
   45907               0 :   if( pgno==0 ){
   45908               0 :     return 0;
   45909                 :   }
   45910                 : 
   45911                 :   /* A frame is only valid if a checksum of the WAL header,
   45912                 :   ** all prior frams, the first 16 bytes of this frame-header, 
   45913                 :   ** and the frame-data matches the checksum in the last 8 
   45914                 :   ** bytes of this frame-header.
   45915                 :   */
   45916               0 :   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
   45917               0 :   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
   45918               0 :   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
   45919               0 :   if( aCksum[0]!=sqlite3Get4byte(&aFrame[16]) 
   45920               0 :    || aCksum[1]!=sqlite3Get4byte(&aFrame[20]) 
   45921                 :   ){
   45922                 :     /* Checksum failed. */
   45923               0 :     return 0;
   45924                 :   }
   45925                 : 
   45926                 :   /* If we reach this point, the frame is valid.  Return the page number
   45927                 :   ** and the new database size.
   45928                 :   */
   45929               0 :   *piPage = pgno;
   45930               0 :   *pnTruncate = sqlite3Get4byte(&aFrame[4]);
   45931               0 :   return 1;
   45932                 : }
   45933                 : 
   45934                 : 
   45935                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   45936                 : /*
   45937                 : ** Names of locks.  This routine is used to provide debugging output and is not
   45938                 : ** a part of an ordinary build.
   45939                 : */
   45940                 : static const char *walLockName(int lockIdx){
   45941                 :   if( lockIdx==WAL_WRITE_LOCK ){
   45942                 :     return "WRITE-LOCK";
   45943                 :   }else if( lockIdx==WAL_CKPT_LOCK ){
   45944                 :     return "CKPT-LOCK";
   45945                 :   }else if( lockIdx==WAL_RECOVER_LOCK ){
   45946                 :     return "RECOVER-LOCK";
   45947                 :   }else{
   45948                 :     static char zName[15];
   45949                 :     sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
   45950                 :                      lockIdx-WAL_READ_LOCK(0));
   45951                 :     return zName;
   45952                 :   }
   45953                 : }
   45954                 : #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
   45955                 :     
   45956                 : 
   45957                 : /*
   45958                 : ** Set or release locks on the WAL.  Locks are either shared or exclusive.
   45959                 : ** A lock cannot be moved directly between shared and exclusive - it must go
   45960                 : ** through the unlocked state first.
   45961                 : **
   45962                 : ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
   45963                 : */
   45964           45410 : static int walLockShared(Wal *pWal, int lockIdx){
   45965                 :   int rc;
   45966           45410 :   if( pWal->exclusiveMode ) return SQLITE_OK;
   45967           45410 :   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
   45968                 :                         SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
   45969                 :   WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
   45970                 :             walLockName(lockIdx), rc ? "failed" : "ok"));
   45971           45410 :   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
   45972           45410 :   return rc;
   45973                 : }
   45974           45410 : static void walUnlockShared(Wal *pWal, int lockIdx){
   45975           45410 :   if( pWal->exclusiveMode ) return;
   45976           45410 :   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
   45977                 :                          SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
   45978                 :   WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
   45979                 : }
   45980           73959 : static int walLockExclusive(Wal *pWal, int lockIdx, int n){
   45981                 :   int rc;
   45982           73959 :   if( pWal->exclusiveMode ) return SQLITE_OK;
   45983           73337 :   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
   45984                 :                         SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
   45985                 :   WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
   45986                 :             walLockName(lockIdx), n, rc ? "failed" : "ok"));
   45987           73337 :   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
   45988           73337 :   return rc;
   45989                 : }
   45990           73782 : static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
   45991           73782 :   if( pWal->exclusiveMode ) return;
   45992           73160 :   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
   45993                 :                          SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
   45994                 :   WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
   45995                 :              walLockName(lockIdx), n));
   45996                 : }
   45997                 : 
   45998                 : /*
   45999                 : ** Compute a hash on a page number.  The resulting hash value must land
   46000                 : ** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
   46001                 : ** the hash to the next value in the event of a collision.
   46002                 : */
   46003          102158 : static int walHash(u32 iPage){
   46004          102158 :   assert( iPage>0 );
   46005                 :   assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
   46006          102158 :   return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
   46007                 : }
   46008          182623 : static int walNextHash(int iPriorHash){
   46009          182623 :   return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
   46010                 : }
   46011                 : 
   46012                 : /* 
   46013                 : ** Return pointers to the hash table and page number array stored on
   46014                 : ** page iHash of the wal-index. The wal-index is broken into 32KB pages
   46015                 : ** numbered starting from 0.
   46016                 : **
   46017                 : ** Set output variable *paHash to point to the start of the hash table
   46018                 : ** in the wal-index file. Set *piZero to one less than the frame 
   46019                 : ** number of the first frame indexed by this hash table. If a
   46020                 : ** slot in the hash table is set to N, it refers to frame number 
   46021                 : ** (*piZero+N) in the log.
   46022                 : **
   46023                 : ** Finally, set *paPgno so that *paPgno[1] is the page number of the
   46024                 : ** first frame indexed by the hash table, frame (*piZero+1).
   46025                 : */
   46026          107951 : static int walHashGet(
   46027                 :   Wal *pWal,                      /* WAL handle */
   46028                 :   int iHash,                      /* Find the iHash'th table */
   46029                 :   volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
   46030                 :   volatile u32 **paPgno,          /* OUT: Pointer to page number array */
   46031                 :   u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
   46032                 : ){
   46033                 :   int rc;                         /* Return code */
   46034                 :   volatile u32 *aPgno;
   46035                 : 
   46036          107951 :   rc = walIndexPage(pWal, iHash, &aPgno);
   46037          107951 :   assert( rc==SQLITE_OK || iHash>0 );
   46038                 : 
   46039          107951 :   if( rc==SQLITE_OK ){
   46040                 :     u32 iZero;
   46041                 :     volatile ht_slot *aHash;
   46042                 : 
   46043          107951 :     aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
   46044          107951 :     if( iHash==0 ){
   46045          107951 :       aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
   46046          107951 :       iZero = 0;
   46047                 :     }else{
   46048               0 :       iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
   46049                 :     }
   46050                 :   
   46051          107951 :     *paPgno = &aPgno[-1];
   46052          107951 :     *paHash = aHash;
   46053          107951 :     *piZero = iZero;
   46054                 :   }
   46055          107951 :   return rc;
   46056                 : }
   46057                 : 
   46058                 : /*
   46059                 : ** Return the number of the wal-index page that contains the hash-table
   46060                 : ** and page-number array that contain entries corresponding to WAL frame
   46061                 : ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages 
   46062                 : ** are numbered starting from 0.
   46063                 : */
   46064          154031 : static int walFramePage(u32 iFrame){
   46065          154031 :   int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
   46066          154031 :   assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
   46067                 :        && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
   46068                 :        && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
   46069                 :        && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
   46070                 :        && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
   46071                 :   );
   46072          154031 :   return iHash;
   46073                 : }
   46074                 : 
   46075                 : /*
   46076                 : ** Return the page number associated with frame iFrame in this WAL.
   46077                 : */
   46078           45472 : static u32 walFramePgno(Wal *pWal, u32 iFrame){
   46079           45472 :   int iHash = walFramePage(iFrame);
   46080           45472 :   if( iHash==0 ){
   46081           45472 :     return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
   46082                 :   }
   46083               0 :   return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
   46084                 : }
   46085                 : 
   46086                 : /*
   46087                 : ** Remove entries from the hash table that point to WAL slots greater
   46088                 : ** than pWal->hdr.mxFrame.
   46089                 : **
   46090                 : ** This function is called whenever pWal->hdr.mxFrame is decreased due
   46091                 : ** to a rollback or savepoint.
   46092                 : **
   46093                 : ** At most only the hash table containing pWal->hdr.mxFrame needs to be
   46094                 : ** updated.  Any later hash tables will be automatically cleared when
   46095                 : ** pWal->hdr.mxFrame advances to the point where those hash tables are
   46096                 : ** actually needed.
   46097                 : */
   46098             304 : static void walCleanupHash(Wal *pWal){
   46099             304 :   volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
   46100             304 :   volatile u32 *aPgno = 0;        /* Page number array for hash table */
   46101             304 :   u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
   46102             304 :   int iLimit = 0;                 /* Zero values greater than this */
   46103                 :   int nByte;                      /* Number of bytes to zero in aPgno[] */
   46104                 :   int i;                          /* Used to iterate through aHash[] */
   46105                 : 
   46106             304 :   assert( pWal->writeLock );
   46107                 :   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
   46108                 :   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
   46109                 :   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
   46110                 : 
   46111             304 :   if( pWal->hdr.mxFrame==0 ) return;
   46112                 : 
   46113                 :   /* Obtain pointers to the hash-table and page-number array containing 
   46114                 :   ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
   46115                 :   ** that the page said hash-table and array reside on is already mapped.
   46116                 :   */
   46117             304 :   assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
   46118             304 :   assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
   46119             304 :   walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
   46120                 : 
   46121                 :   /* Zero all hash-table entries that correspond to frame numbers greater
   46122                 :   ** than pWal->hdr.mxFrame.
   46123                 :   */
   46124             304 :   iLimit = pWal->hdr.mxFrame - iZero;
   46125             304 :   assert( iLimit>0 );
   46126         2490672 :   for(i=0; i<HASHTABLE_NSLOT; i++){
   46127         2490368 :     if( aHash[i]>iLimit ){
   46128               0 :       aHash[i] = 0;
   46129                 :     }
   46130                 :   }
   46131                 :   
   46132                 :   /* Zero the entries in the aPgno array that correspond to frames with
   46133                 :   ** frame numbers greater than pWal->hdr.mxFrame. 
   46134                 :   */
   46135             304 :   nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
   46136             304 :   memset((void *)&aPgno[iLimit+1], 0, nByte);
   46137                 : 
   46138                 : #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
   46139                 :   /* Verify that the every entry in the mapping region is still reachable
   46140                 :   ** via the hash table even after the cleanup.
   46141                 :   */
   46142                 :   if( iLimit ){
   46143                 :     int i;           /* Loop counter */
   46144                 :     int iKey;        /* Hash key */
   46145                 :     for(i=1; i<=iLimit; i++){
   46146                 :       for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
   46147                 :         if( aHash[iKey]==i ) break;
   46148                 :       }
   46149                 :       assert( aHash[iKey]==i );
   46150                 :     }
   46151                 :   }
   46152                 : #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
   46153                 : }
   46154                 : 
   46155                 : 
   46156                 : /*
   46157                 : ** Set an entry in the wal-index that will map database page number
   46158                 : ** pPage into WAL frame iFrame.
   46159                 : */
   46160          101689 : static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
   46161                 :   int rc;                         /* Return code */
   46162          101689 :   u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
   46163          101689 :   volatile u32 *aPgno = 0;        /* Page number array */
   46164          101689 :   volatile ht_slot *aHash = 0;    /* Hash table */
   46165                 : 
   46166          101689 :   rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
   46167                 : 
   46168                 :   /* Assuming the wal-index file was successfully mapped, populate the
   46169                 :   ** page number array and hash table entry.
   46170                 :   */
   46171          101689 :   if( rc==SQLITE_OK ){
   46172                 :     int iKey;                     /* Hash table key */
   46173                 :     int idx;                      /* Value to write to hash-table slot */
   46174                 :     int nCollide;                 /* Number of hash collisions */
   46175                 : 
   46176          101689 :     idx = iFrame - iZero;
   46177          101689 :     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
   46178                 :     
   46179                 :     /* If this is the first entry to be added to this hash-table, zero the
   46180                 :     ** entire hash table and aPgno[] array before proceding. 
   46181                 :     */
   46182          101689 :     if( idx==1 ){
   46183            5312 :       int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
   46184            5312 :       memset((void*)&aPgno[1], 0, nByte);
   46185                 :     }
   46186                 : 
   46187                 :     /* If the entry in aPgno[] is already set, then the previous writer
   46188                 :     ** must have exited unexpectedly in the middle of a transaction (after
   46189                 :     ** writing one or more dirty pages to the WAL to free up memory). 
   46190                 :     ** Remove the remnants of that writers uncommitted transaction from 
   46191                 :     ** the hash-table before writing any new entries.
   46192                 :     */
   46193          101689 :     if( aPgno[idx] ){
   46194               0 :       walCleanupHash(pWal);
   46195               0 :       assert( !aPgno[idx] );
   46196                 :     }
   46197                 : 
   46198                 :     /* Write the aPgno[] array entry and the hash-table slot. */
   46199          101689 :     nCollide = idx;
   46200          284049 :     for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
   46201          182360 :       if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
   46202                 :     }
   46203          101689 :     aPgno[idx] = iPage;
   46204          101689 :     aHash[iKey] = (ht_slot)idx;
   46205                 : 
   46206                 : #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
   46207                 :     /* Verify that the number of entries in the hash table exactly equals
   46208                 :     ** the number of entries in the mapping region.
   46209                 :     */
   46210                 :     {
   46211                 :       int i;           /* Loop counter */
   46212                 :       int nEntry = 0;  /* Number of entries in the hash table */
   46213                 :       for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
   46214                 :       assert( nEntry==idx );
   46215                 :     }
   46216                 : 
   46217                 :     /* Verify that the every entry in the mapping region is reachable
   46218                 :     ** via the hash table.  This turns out to be a really, really expensive
   46219                 :     ** thing to check, so only do this occasionally - not on every
   46220                 :     ** iteration.
   46221                 :     */
   46222                 :     if( (idx&0x3ff)==0 ){
   46223                 :       int i;           /* Loop counter */
   46224                 :       for(i=1; i<=idx; i++){
   46225                 :         for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
   46226                 :           if( aHash[iKey]==i ) break;
   46227                 :         }
   46228                 :         assert( aHash[iKey]==i );
   46229                 :       }
   46230                 :     }
   46231                 : #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
   46232                 :   }
   46233                 : 
   46234                 : 
   46235          101689 :   return rc;
   46236                 : }
   46237                 : 
   46238                 : 
   46239                 : /*
   46240                 : ** Recover the wal-index by reading the write-ahead log file. 
   46241                 : **
   46242                 : ** This routine first tries to establish an exclusive lock on the
   46243                 : ** wal-index to prevent other threads/processes from doing anything
   46244                 : ** with the WAL or wal-index while recovery is running.  The
   46245                 : ** WAL_RECOVER_LOCK is also held so that other threads will know
   46246                 : ** that this thread is running recovery.  If unable to establish
   46247                 : ** the necessary locks, this routine returns SQLITE_BUSY.
   46248                 : */
   46249             353 : static int walIndexRecover(Wal *pWal){
   46250                 :   int rc;                         /* Return Code */
   46251                 :   i64 nSize;                      /* Size of log file */
   46252             353 :   u32 aFrameCksum[2] = {0, 0};
   46253                 :   int iLock;                      /* Lock offset to lock for checkpoint */
   46254                 :   int nLock;                      /* Number of locks to hold */
   46255                 : 
   46256                 :   /* Obtain an exclusive lock on all byte in the locking range not already
   46257                 :   ** locked by the caller. The caller is guaranteed to have locked the
   46258                 :   ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
   46259                 :   ** If successful, the same bytes that are locked here are unlocked before
   46260                 :   ** this function returns.
   46261                 :   */
   46262             353 :   assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
   46263                 :   assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
   46264                 :   assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
   46265             353 :   assert( pWal->writeLock );
   46266             353 :   iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
   46267             353 :   nLock = SQLITE_SHM_NLOCK - iLock;
   46268             353 :   rc = walLockExclusive(pWal, iLock, nLock);
   46269             353 :   if( rc ){
   46270               0 :     return rc;
   46271                 :   }
   46272                 :   WALTRACE(("WAL%p: recovery begin...\n", pWal));
   46273                 : 
   46274             353 :   memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
   46275                 : 
   46276             353 :   rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
   46277             353 :   if( rc!=SQLITE_OK ){
   46278               0 :     goto recovery_error;
   46279                 :   }
   46280                 : 
   46281             353 :   if( nSize>WAL_HDRSIZE ){
   46282                 :     u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
   46283               0 :     u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
   46284                 :     int szFrame;                  /* Number of bytes in buffer aFrame[] */
   46285                 :     u8 *aData;                    /* Pointer to data part of aFrame buffer */
   46286                 :     int iFrame;                   /* Index of last frame read */
   46287                 :     i64 iOffset;                  /* Next offset to read from log file */
   46288                 :     int szPage;                   /* Page size according to the log */
   46289                 :     u32 magic;                    /* Magic value read from WAL header */
   46290                 :     u32 version;                  /* Magic value read from WAL header */
   46291                 :     int isValid;                  /* True if this frame is valid */
   46292                 : 
   46293                 :     /* Read in the WAL header. */
   46294               0 :     rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
   46295               0 :     if( rc!=SQLITE_OK ){
   46296               0 :       goto recovery_error;
   46297                 :     }
   46298                 : 
   46299                 :     /* If the database page size is not a power of two, or is greater than
   46300                 :     ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid 
   46301                 :     ** data. Similarly, if the 'magic' value is invalid, ignore the whole
   46302                 :     ** WAL file.
   46303                 :     */
   46304               0 :     magic = sqlite3Get4byte(&aBuf[0]);
   46305               0 :     szPage = sqlite3Get4byte(&aBuf[8]);
   46306               0 :     if( (magic&0xFFFFFFFE)!=WAL_MAGIC 
   46307               0 :      || szPage&(szPage-1) 
   46308               0 :      || szPage>SQLITE_MAX_PAGE_SIZE 
   46309               0 :      || szPage<512 
   46310                 :     ){
   46311                 :       goto finished;
   46312                 :     }
   46313               0 :     pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
   46314               0 :     pWal->szPage = szPage;
   46315               0 :     pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
   46316               0 :     memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
   46317                 : 
   46318                 :     /* Verify that the WAL header checksum is correct */
   46319               0 :     walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, 
   46320                 :         aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
   46321                 :     );
   46322               0 :     if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
   46323               0 :      || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
   46324                 :     ){
   46325                 :       goto finished;
   46326                 :     }
   46327                 : 
   46328                 :     /* Verify that the version number on the WAL format is one that
   46329                 :     ** are able to understand */
   46330               0 :     version = sqlite3Get4byte(&aBuf[4]);
   46331               0 :     if( version!=WAL_MAX_VERSION ){
   46332               0 :       rc = SQLITE_CANTOPEN_BKPT;
   46333               0 :       goto finished;
   46334                 :     }
   46335                 : 
   46336                 :     /* Malloc a buffer to read frames into. */
   46337               0 :     szFrame = szPage + WAL_FRAME_HDRSIZE;
   46338               0 :     aFrame = (u8 *)sqlite3_malloc(szFrame);
   46339               0 :     if( !aFrame ){
   46340               0 :       rc = SQLITE_NOMEM;
   46341               0 :       goto recovery_error;
   46342                 :     }
   46343               0 :     aData = &aFrame[WAL_FRAME_HDRSIZE];
   46344                 : 
   46345                 :     /* Read all frames from the log file. */
   46346               0 :     iFrame = 0;
   46347               0 :     for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
   46348                 :       u32 pgno;                   /* Database page number for frame */
   46349                 :       u32 nTruncate;              /* dbsize field from frame header */
   46350                 : 
   46351                 :       /* Read and decode the next log frame. */
   46352               0 :       iFrame++;
   46353               0 :       rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
   46354               0 :       if( rc!=SQLITE_OK ) break;
   46355               0 :       isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
   46356               0 :       if( !isValid ) break;
   46357               0 :       rc = walIndexAppend(pWal, iFrame, pgno);
   46358               0 :       if( rc!=SQLITE_OK ) break;
   46359                 : 
   46360                 :       /* If nTruncate is non-zero, this is a commit record. */
   46361               0 :       if( nTruncate ){
   46362               0 :         pWal->hdr.mxFrame = iFrame;
   46363               0 :         pWal->hdr.nPage = nTruncate;
   46364               0 :         pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
   46365                 :         testcase( szPage<=32768 );
   46366                 :         testcase( szPage>=65536 );
   46367               0 :         aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
   46368               0 :         aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
   46369                 :       }
   46370                 :     }
   46371                 : 
   46372               0 :     sqlite3_free(aFrame);
   46373                 :   }
   46374                 : 
   46375                 : finished:
   46376             353 :   if( rc==SQLITE_OK ){
   46377                 :     volatile WalCkptInfo *pInfo;
   46378                 :     int i;
   46379             353 :     pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
   46380             353 :     pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
   46381             353 :     walIndexWriteHdr(pWal);
   46382                 : 
   46383                 :     /* Reset the checkpoint-header. This is safe because this thread is 
   46384                 :     ** currently holding locks that exclude all other readers, writers and
   46385                 :     ** checkpointers.
   46386                 :     */
   46387             353 :     pInfo = walCkptInfo(pWal);
   46388             353 :     pInfo->nBackfill = 0;
   46389             353 :     pInfo->aReadMark[0] = 0;
   46390             353 :     for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
   46391                 : 
   46392                 :     /* If more than one frame was recovered from the log file, report an
   46393                 :     ** event via sqlite3_log(). This is to help with identifying performance
   46394                 :     ** problems caused by applications routinely shutting down without
   46395                 :     ** checkpointing the log file.
   46396                 :     */
   46397             353 :     if( pWal->hdr.nPage ){
   46398               0 :       sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
   46399                 :           pWal->hdr.nPage, pWal->zWalName
   46400                 :       );
   46401                 :     }
   46402                 :   }
   46403                 : 
   46404                 : recovery_error:
   46405                 :   WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
   46406             353 :   walUnlockExclusive(pWal, iLock, nLock);
   46407             353 :   return rc;
   46408                 : }
   46409                 : 
   46410                 : /*
   46411                 : ** Close an open wal-index.
   46412                 : */
   46413             426 : static void walIndexClose(Wal *pWal, int isDelete){
   46414             426 :   if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
   46415                 :     int i;
   46416               0 :     for(i=0; i<pWal->nWiData; i++){
   46417               0 :       sqlite3_free((void *)pWal->apWiData[i]);
   46418               0 :       pWal->apWiData[i] = 0;
   46419                 :     }
   46420                 :   }else{
   46421             426 :     sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
   46422                 :   }
   46423             426 : }
   46424                 : 
   46425                 : /* 
   46426                 : ** Open a connection to the WAL file zWalName. The database file must 
   46427                 : ** already be opened on connection pDbFd. The buffer that zWalName points
   46428                 : ** to must remain valid for the lifetime of the returned Wal* handle.
   46429                 : **
   46430                 : ** A SHARED lock should be held on the database file when this function
   46431                 : ** is called. The purpose of this SHARED lock is to prevent any other
   46432                 : ** client from unlinking the WAL or wal-index file. If another process
   46433                 : ** were to do this just after this client opened one of these files, the
   46434                 : ** system would be badly broken.
   46435                 : **
   46436                 : ** If the log file is successfully opened, SQLITE_OK is returned and 
   46437                 : ** *ppWal is set to point to a new WAL handle. If an error occurs,
   46438                 : ** an SQLite error code is returned and *ppWal is left unmodified.
   46439                 : */
   46440             426 : SQLITE_PRIVATE int sqlite3WalOpen(
   46441                 :   sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
   46442                 :   sqlite3_file *pDbFd,            /* The open database file */
   46443                 :   const char *zWalName,           /* Name of the WAL file */
   46444                 :   int bNoShm,                     /* True to run in heap-memory mode */
   46445                 :   i64 mxWalSize,                  /* Truncate WAL to this size on reset */
   46446                 :   Wal **ppWal                     /* OUT: Allocated Wal handle */
   46447                 : ){
   46448                 :   int rc;                         /* Return Code */
   46449                 :   Wal *pRet;                      /* Object to allocate and return */
   46450                 :   int flags;                      /* Flags passed to OsOpen() */
   46451                 : 
   46452             426 :   assert( zWalName && zWalName[0] );
   46453             426 :   assert( pDbFd );
   46454                 : 
   46455                 :   /* In the amalgamation, the os_unix.c and os_win.c source files come before
   46456                 :   ** this source file.  Verify that the #defines of the locking byte offsets
   46457                 :   ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
   46458                 :   */
   46459                 : #ifdef WIN_SHM_BASE
   46460                 :   assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
   46461                 : #endif
   46462                 : #ifdef UNIX_SHM_BASE
   46463                 :   assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
   46464                 : #endif
   46465                 : 
   46466                 : 
   46467                 :   /* Allocate an instance of struct Wal to return. */
   46468             426 :   *ppWal = 0;
   46469             426 :   pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
   46470             426 :   if( !pRet ){
   46471               0 :     return SQLITE_NOMEM;
   46472                 :   }
   46473                 : 
   46474             426 :   pRet->pVfs = pVfs;
   46475             426 :   pRet->pWalFd = (sqlite3_file *)&pRet[1];
   46476             426 :   pRet->pDbFd = pDbFd;
   46477             426 :   pRet->readLock = -1;
   46478             426 :   pRet->mxWalSize = mxWalSize;
   46479             426 :   pRet->zWalName = zWalName;
   46480             426 :   pRet->syncHeader = 1;
   46481             426 :   pRet->padToSectorBoundary = 1;
   46482             426 :   pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
   46483                 : 
   46484                 :   /* Open file handle on the write-ahead log file. */
   46485             426 :   flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
   46486             426 :   rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
   46487             426 :   if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
   46488               0 :     pRet->readOnly = WAL_RDONLY;
   46489                 :   }
   46490                 : 
   46491             426 :   if( rc!=SQLITE_OK ){
   46492               0 :     walIndexClose(pRet, 0);
   46493               0 :     sqlite3OsClose(pRet->pWalFd);
   46494               0 :     sqlite3_free(pRet);
   46495                 :   }else{
   46496             426 :     int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
   46497             426 :     if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
   46498             426 :     if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
   46499             426 :       pRet->padToSectorBoundary = 0;
   46500                 :     }
   46501             426 :     *ppWal = pRet;
   46502                 :     WALTRACE(("WAL%d: opened\n", pRet));
   46503                 :   }
   46504             426 :   return rc;
   46505                 : }
   46506                 : 
   46507                 : /*
   46508                 : ** Change the size to which the WAL file is trucated on each reset.
   46509                 : */
   46510             279 : SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
   46511             279 :   if( pWal ) pWal->mxWalSize = iLimit;
   46512             279 : }
   46513                 : 
   46514                 : /*
   46515                 : ** Find the smallest page number out of all pages held in the WAL that
   46516                 : ** has not been returned by any prior invocation of this method on the
   46517                 : ** same WalIterator object.   Write into *piFrame the frame index where
   46518                 : ** that page was last written into the WAL.  Write into *piPage the page
   46519                 : ** number.
   46520                 : **
   46521                 : ** Return 0 on success.  If there are no pages in the WAL with a page
   46522                 : ** number larger than *piPage, then return 1.
   46523                 : */
   46524           50784 : static int walIteratorNext(
   46525                 :   WalIterator *p,               /* Iterator */
   46526                 :   u32 *piPage,                  /* OUT: The page number of the next page */
   46527                 :   u32 *piFrame                  /* OUT: Wal frame index of next page */
   46528                 : ){
   46529                 :   u32 iMin;                     /* Result pgno must be greater than iMin */
   46530           50784 :   u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
   46531                 :   int i;                        /* For looping through segments */
   46532                 : 
   46533           50784 :   iMin = p->iPrior;
   46534           50784 :   assert( iMin<0xffffffff );
   46535          101568 :   for(i=p->nSegment-1; i>=0; i--){
   46536           50784 :     struct WalSegment *pSegment = &p->aSegment[i];
   46537          147040 :     while( pSegment->iNext<pSegment->nEntry ){
   46538           90944 :       u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
   46539           90944 :       if( iPg>iMin ){
   46540           45472 :         if( iPg<iRet ){
   46541           45472 :           iRet = iPg;
   46542           45472 :           *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
   46543                 :         }
   46544           45472 :         break;
   46545                 :       }
   46546           45472 :       pSegment->iNext++;
   46547                 :     }
   46548                 :   }
   46549                 : 
   46550           50784 :   *piPage = p->iPrior = iRet;
   46551           50784 :   return (iRet==0xFFFFFFFF);
   46552                 : }
   46553                 : 
   46554                 : /*
   46555                 : ** This function merges two sorted lists into a single sorted list.
   46556                 : **
   46557                 : ** aLeft[] and aRight[] are arrays of indices.  The sort key is
   46558                 : ** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
   46559                 : ** is guaranteed for all J<K:
   46560                 : **
   46561                 : **        aContent[aLeft[J]] < aContent[aLeft[K]]
   46562                 : **        aContent[aRight[J]] < aContent[aRight[K]]
   46563                 : **
   46564                 : ** This routine overwrites aRight[] with a new (probably longer) sequence
   46565                 : ** of indices such that the aRight[] contains every index that appears in
   46566                 : ** either aLeft[] or the old aRight[] and such that the second condition
   46567                 : ** above is still met.
   46568                 : **
   46569                 : ** The aContent[aLeft[X]] values will be unique for all X.  And the
   46570                 : ** aContent[aRight[X]] values will be unique too.  But there might be
   46571                 : ** one or more combinations of X and Y such that
   46572                 : **
   46573                 : **      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
   46574                 : **
   46575                 : ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
   46576                 : */
   46577          146357 : static void walMerge(
   46578                 :   const u32 *aContent,            /* Pages in wal - keys for the sort */
   46579                 :   ht_slot *aLeft,                 /* IN: Left hand input list */
   46580                 :   int nLeft,                      /* IN: Elements in array *paLeft */
   46581                 :   ht_slot **paRight,              /* IN/OUT: Right hand input list */
   46582                 :   int *pnRight,                   /* IN/OUT: Elements in *paRight */
   46583                 :   ht_slot *aTmp                   /* Temporary buffer */
   46584                 : ){
   46585          146357 :   int iLeft = 0;                  /* Current index in aLeft */
   46586          146357 :   int iRight = 0;                 /* Current index in aRight */
   46587          146357 :   int iOut = 0;                   /* Current index in output buffer */
   46588          146357 :   int nRight = *pnRight;
   46589          146357 :   ht_slot *aRight = *paRight;
   46590                 : 
   46591          146357 :   assert( nLeft>0 && nRight>0 );
   46592          757725 :   while( iRight<nRight || iLeft<nLeft ){
   46593                 :     ht_slot logpage;
   46594                 :     Pgno dbpage;
   46595                 : 
   46596          465011 :     if( (iLeft<nLeft) 
   46597          351480 :      && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
   46598                 :     ){
   46599          191690 :       logpage = aLeft[iLeft++];
   46600                 :     }else{
   46601          273321 :       logpage = aRight[iRight++];
   46602                 :     }
   46603          465011 :     dbpage = aContent[logpage];
   46604                 : 
   46605          465011 :     aTmp[iOut++] = logpage;
   46606          465011 :     if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
   46607                 : 
   46608          465011 :     assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
   46609          465011 :     assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
   46610                 :   }
   46611                 : 
   46612          146357 :   *paRight = aLeft;
   46613          146357 :   *pnRight = iOut;
   46614          146357 :   memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
   46615          146357 : }
   46616                 : 
   46617                 : /*
   46618                 : ** Sort the elements in list aList using aContent[] as the sort key.
   46619                 : ** Remove elements with duplicate keys, preferring to keep the
   46620                 : ** larger aList[] values.
   46621                 : **
   46622                 : ** The aList[] entries are indices into aContent[].  The values in
   46623                 : ** aList[] are to be sorted so that for all J<K:
   46624                 : **
   46625                 : **      aContent[aList[J]] < aContent[aList[K]]
   46626                 : **
   46627                 : ** For any X and Y such that
   46628                 : **
   46629                 : **      aContent[aList[X]] == aContent[aList[Y]]
   46630                 : **
   46631                 : ** Keep the larger of the two values aList[X] and aList[Y] and discard
   46632                 : ** the smaller.
   46633                 : */
   46634            5489 : static void walMergesort(
   46635                 :   const u32 *aContent,            /* Pages in wal */
   46636                 :   ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
   46637                 :   ht_slot *aList,                 /* IN/OUT: List to sort */
   46638                 :   int *pnList                     /* IN/OUT: Number of elements in aList[] */
   46639                 : ){
   46640                 :   struct Sublist {
   46641                 :     int nList;                    /* Number of elements in aList */
   46642                 :     ht_slot *aList;               /* Pointer to sub-list content */
   46643                 :   };
   46644                 : 
   46645            5489 :   const int nList = *pnList;      /* Size of input list */
   46646            5489 :   int nMerge = 0;                 /* Number of elements in list aMerge */
   46647            5489 :   ht_slot *aMerge = 0;            /* List to be merged */
   46648                 :   int iList;                      /* Index into input list */
   46649            5489 :   int iSub = 0;                   /* Index into aSub array */
   46650                 :   struct Sublist aSub[13];        /* Array of sub-lists */
   46651                 : 
   46652            5489 :   memset(aSub, 0, sizeof(aSub));
   46653            5489 :   assert( nList<=HASHTABLE_NPAGE && nList>0 );
   46654                 :   assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
   46655                 : 
   46656          157335 :   for(iList=0; iList<nList; iList++){
   46657          151846 :     nMerge = 1;
   46658          151846 :     aMerge = &aList[iList];
   46659          291414 :     for(iSub=0; iList & (1<<iSub); iSub++){
   46660          139568 :       struct Sublist *p = &aSub[iSub];
   46661          139568 :       assert( p->aList && p->nList<=(1<<iSub) );
   46662          139568 :       assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
   46663          139568 :       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
   46664                 :     }
   46665          151846 :     aSub[iSub].aList = aMerge;
   46666          151846 :     aSub[iSub].nList = nMerge;
   46667                 :   }
   46668                 : 
   46669           65514 :   for(iSub++; iSub<ArraySize(aSub); iSub++){
   46670           60025 :     if( nList & (1<<iSub) ){
   46671            6789 :       struct Sublist *p = &aSub[iSub];
   46672            6789 :       assert( p->nList<=(1<<iSub) );
   46673            6789 :       assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
   46674            6789 :       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
   46675                 :     }
   46676                 :   }
   46677            5489 :   assert( aMerge==aList );
   46678            5489 :   *pnList = nMerge;
   46679                 : 
   46680                 : #ifdef SQLITE_DEBUG
   46681                 :   {
   46682                 :     int i;
   46683           46536 :     for(i=1; i<*pnList; i++){
   46684           41047 :       assert( aContent[aList[i]] > aContent[aList[i-1]] );
   46685                 :     }
   46686                 :   }
   46687                 : #endif
   46688            5489 : }
   46689                 : 
   46690                 : /* 
   46691                 : ** Free an iterator allocated by walIteratorInit().
   46692                 : */
   46693            5489 : static void walIteratorFree(WalIterator *p){
   46694            5489 :   sqlite3ScratchFree(p);
   46695            5489 : }
   46696                 : 
   46697                 : /*
   46698                 : ** Construct a WalInterator object that can be used to loop over all 
   46699                 : ** pages in the WAL in ascending order. The caller must hold the checkpoint
   46700                 : ** lock.
   46701                 : **
   46702                 : ** On success, make *pp point to the newly allocated WalInterator object
   46703                 : ** return SQLITE_OK. Otherwise, return an error code. If this routine
   46704                 : ** returns an error, the value of *pp is undefined.
   46705                 : **
   46706                 : ** The calling routine should invoke walIteratorFree() to destroy the
   46707                 : ** WalIterator object when it has finished with it.
   46708                 : */
   46709            5489 : static int walIteratorInit(Wal *pWal, WalIterator **pp){
   46710                 :   WalIterator *p;                 /* Return value */
   46711                 :   int nSegment;                   /* Number of segments to merge */
   46712                 :   u32 iLast;                      /* Last frame in log */
   46713                 :   int nByte;                      /* Number of bytes to allocate */
   46714                 :   int i;                          /* Iterator variable */
   46715                 :   ht_slot *aTmp;                  /* Temp space used by merge-sort */
   46716            5489 :   int rc = SQLITE_OK;             /* Return Code */
   46717                 : 
   46718                 :   /* This routine only runs while holding the checkpoint lock. And
   46719                 :   ** it only runs if there is actually content in the log (mxFrame>0).
   46720                 :   */
   46721            5489 :   assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
   46722            5489 :   iLast = pWal->hdr.mxFrame;
   46723                 : 
   46724                 :   /* Allocate space for the WalIterator object. */
   46725            5489 :   nSegment = walFramePage(iLast) + 1;
   46726            5489 :   nByte = sizeof(WalIterator) 
   46727                 :         + (nSegment-1)*sizeof(struct WalSegment)
   46728            5489 :         + iLast*sizeof(ht_slot);
   46729            5489 :   p = (WalIterator *)sqlite3ScratchMalloc(nByte);
   46730            5489 :   if( !p ){
   46731               0 :     return SQLITE_NOMEM;
   46732                 :   }
   46733            5489 :   memset(p, 0, nByte);
   46734            5489 :   p->nSegment = nSegment;
   46735                 : 
   46736                 :   /* Allocate temporary space used by the merge-sort routine. This block
   46737                 :   ** of memory will be freed before this function returns.
   46738                 :   */
   46739            5489 :   aTmp = (ht_slot *)sqlite3ScratchMalloc(
   46740            5489 :       sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
   46741                 :   );
   46742            5489 :   if( !aTmp ){
   46743               0 :     rc = SQLITE_NOMEM;
   46744                 :   }
   46745                 : 
   46746           10978 :   for(i=0; rc==SQLITE_OK && i<nSegment; i++){
   46747                 :     volatile ht_slot *aHash;
   46748                 :     u32 iZero;
   46749                 :     volatile u32 *aPgno;
   46750                 : 
   46751            5489 :     rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
   46752            5489 :     if( rc==SQLITE_OK ){
   46753                 :       int j;                      /* Counter variable */
   46754                 :       int nEntry;                 /* Number of entries in this segment */
   46755                 :       ht_slot *aIndex;            /* Sorted index for this segment */
   46756                 : 
   46757            5489 :       aPgno++;
   46758            5489 :       if( (i+1)==nSegment ){
   46759            5489 :         nEntry = (int)(iLast - iZero);
   46760                 :       }else{
   46761               0 :         nEntry = (int)((u32*)aHash - (u32*)aPgno);
   46762                 :       }
   46763            5489 :       aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
   46764            5489 :       iZero++;
   46765                 :   
   46766          157335 :       for(j=0; j<nEntry; j++){
   46767          151846 :         aIndex[j] = (ht_slot)j;
   46768                 :       }
   46769            5489 :       walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
   46770            5489 :       p->aSegment[i].iZero = iZero;
   46771            5489 :       p->aSegment[i].nEntry = nEntry;
   46772            5489 :       p->aSegment[i].aIndex = aIndex;
   46773            5489 :       p->aSegment[i].aPgno = (u32 *)aPgno;
   46774                 :     }
   46775                 :   }
   46776            5489 :   sqlite3ScratchFree(aTmp);
   46777                 : 
   46778            5489 :   if( rc!=SQLITE_OK ){
   46779               0 :     walIteratorFree(p);
   46780                 :   }
   46781            5489 :   *pp = p;
   46782            5489 :   return rc;
   46783                 : }
   46784                 : 
   46785                 : /*
   46786                 : ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
   46787                 : ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
   46788                 : ** busy-handler function. Invoke it and retry the lock until either the
   46789                 : ** lock is successfully obtained or the busy-handler returns 0.
   46790                 : */
   46791           10781 : static int walBusyLock(
   46792                 :   Wal *pWal,                      /* WAL connection */
   46793                 :   int (*xBusy)(void*),            /* Function to call when busy */
   46794                 :   void *pBusyArg,                 /* Context argument for xBusyHandler */
   46795                 :   int lockIdx,                    /* Offset of first byte to lock */
   46796                 :   int n                           /* Number of bytes to lock */
   46797                 : ){
   46798                 :   int rc;
   46799                 :   do {
   46800           10781 :     rc = walLockExclusive(pWal, lockIdx, n);
   46801           10781 :   }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
   46802           10781 :   return rc;
   46803                 : }
   46804                 : 
   46805                 : /*
   46806                 : ** The cache of the wal-index header must be valid to call this function.
   46807                 : ** Return the page-size in bytes used by the database.
   46808                 : */
   46809           11689 : static int walPagesize(Wal *pWal){
   46810           11689 :   return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
   46811                 : }
   46812                 : 
   46813                 : /*
   46814                 : ** Copy as much content as we can from the WAL back into the database file
   46815                 : ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
   46816                 : **
   46817                 : ** The amount of information copies from WAL to database might be limited
   46818                 : ** by active readers.  This routine will never overwrite a database page
   46819                 : ** that a concurrent reader might be using.
   46820                 : **
   46821                 : ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
   46822                 : ** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if 
   46823                 : ** checkpoints are always run by a background thread or background 
   46824                 : ** process, foreground threads will never block on a lengthy fsync call.
   46825                 : **
   46826                 : ** Fsync is called on the WAL before writing content out of the WAL and
   46827                 : ** into the database.  This ensures that if the new content is persistent
   46828                 : ** in the WAL and can be recovered following a power-loss or hard reset.
   46829                 : **
   46830                 : ** Fsync is also called on the database file if (and only if) the entire
   46831                 : ** WAL content is copied into the database file.  This second fsync makes
   46832                 : ** it safe to delete the WAL since the new content will persist in the
   46833                 : ** database file.
   46834                 : **
   46835                 : ** This routine uses and updates the nBackfill field of the wal-index header.
   46836                 : ** This is the only routine tha will increase the value of nBackfill.  
   46837                 : ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
   46838                 : ** its value.)
   46839                 : **
   46840                 : ** The caller must be holding sufficient locks to ensure that no other
   46841                 : ** checkpoint is running (in any other thread or process) at the same
   46842                 : ** time.
   46843                 : */
   46844            5869 : static int walCheckpoint(
   46845                 :   Wal *pWal,                      /* Wal connection */
   46846                 :   int eMode,                      /* One of PASSIVE, FULL or RESTART */
   46847                 :   int (*xBusyCall)(void*),        /* Function to call when busy */
   46848                 :   void *pBusyArg,                 /* Context argument for xBusyHandler */
   46849                 :   int sync_flags,                 /* Flags for OsSync() (or 0) */
   46850                 :   u8 *zBuf                        /* Temporary buffer to use */
   46851                 : ){
   46852                 :   int rc;                         /* Return code */
   46853                 :   int szPage;                     /* Database page-size */
   46854            5869 :   WalIterator *pIter = 0;         /* Wal iterator context */
   46855            5869 :   u32 iDbpage = 0;                /* Next database page to write */
   46856            5869 :   u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
   46857                 :   u32 mxSafeFrame;                /* Max frame that can be backfilled */
   46858                 :   u32 mxPage;                     /* Max database page to write */
   46859                 :   int i;                          /* Loop counter */
   46860                 :   volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
   46861            5869 :   int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
   46862                 : 
   46863            5869 :   szPage = walPagesize(pWal);
   46864                 :   testcase( szPage<=32768 );
   46865                 :   testcase( szPage>=65536 );
   46866            5869 :   pInfo = walCkptInfo(pWal);
   46867            5869 :   if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
   46868                 : 
   46869                 :   /* Allocate the iterator */
   46870            5489 :   rc = walIteratorInit(pWal, &pIter);
   46871            5489 :   if( rc!=SQLITE_OK ){
   46872               0 :     return rc;
   46873                 :   }
   46874            5489 :   assert( pIter );
   46875                 : 
   46876            5489 :   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
   46877                 : 
   46878                 :   /* Compute in mxSafeFrame the index of the last frame of the WAL that is
   46879                 :   ** safe to write into the database.  Frames beyond mxSafeFrame might
   46880                 :   ** overwrite database pages that are in use by active readers and thus
   46881                 :   ** cannot be backfilled from the WAL.
   46882                 :   */
   46883            5489 :   mxSafeFrame = pWal->hdr.mxFrame;
   46884            5489 :   mxPage = pWal->hdr.nPage;
   46885           27445 :   for(i=1; i<WAL_NREADER; i++){
   46886           21956 :     u32 y = pInfo->aReadMark[i];
   46887           21956 :     if( mxSafeFrame>y ){
   46888            5292 :       assert( y<=pWal->hdr.mxFrame );
   46889            5292 :       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
   46890            5292 :       if( rc==SQLITE_OK ){
   46891            5292 :         pInfo->aReadMark[i] = READMARK_NOT_USED;
   46892            5292 :         walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
   46893               0 :       }else if( rc==SQLITE_BUSY ){
   46894               0 :         mxSafeFrame = y;
   46895               0 :         xBusy = 0;
   46896                 :       }else{
   46897               0 :         goto walcheckpoint_out;
   46898                 :       }
   46899                 :     }
   46900                 :   }
   46901                 : 
   46902            5489 :   if( pInfo->nBackfill<mxSafeFrame
   46903            5489 :    && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
   46904                 :   ){
   46905                 :     i64 nSize;                    /* Current size of database file */
   46906            5312 :     u32 nBackfill = pInfo->nBackfill;
   46907                 : 
   46908                 :     /* Sync the WAL to disk */
   46909            5312 :     if( sync_flags ){
   46910            3161 :       rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
   46911                 :     }
   46912                 : 
   46913                 :     /* If the database file may grow as a result of this checkpoint, hint
   46914                 :     ** about the eventual size of the db file to the VFS layer. 
   46915                 :     */
   46916            5312 :     if( rc==SQLITE_OK ){
   46917            5312 :       i64 nReq = ((i64)mxPage * szPage);
   46918            5312 :       rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
   46919            5312 :       if( rc==SQLITE_OK && nSize<nReq ){
   46920             265 :         sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
   46921                 :       }
   46922                 :     }
   46923                 : 
   46924                 :     /* Iterate through the contents of the WAL, copying data to the db file. */
   46925           56096 :     while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
   46926                 :       i64 iOffset;
   46927           45472 :       assert( walFramePgno(pWal, iFrame)==iDbpage );
   46928           45472 :       if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
   46929           45472 :       iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
   46930                 :       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
   46931           45472 :       rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
   46932           45472 :       if( rc!=SQLITE_OK ) break;
   46933           45472 :       iOffset = (iDbpage-1)*(i64)szPage;
   46934                 :       testcase( IS_BIG_INT(iOffset) );
   46935           45472 :       rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
   46936           45472 :       if( rc!=SQLITE_OK ) break;
   46937                 :     }
   46938                 : 
   46939                 :     /* If work was actually accomplished... */
   46940            5312 :     if( rc==SQLITE_OK ){
   46941            5312 :       if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
   46942            5312 :         i64 szDb = pWal->hdr.nPage*(i64)szPage;
   46943                 :         testcase( IS_BIG_INT(szDb) );
   46944            5312 :         rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
   46945            5312 :         if( rc==SQLITE_OK && sync_flags ){
   46946            3161 :           rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
   46947                 :         }
   46948                 :       }
   46949            5312 :       if( rc==SQLITE_OK ){
   46950            5312 :         pInfo->nBackfill = mxSafeFrame;
   46951                 :       }
   46952                 :     }
   46953                 : 
   46954                 :     /* Release the reader lock held while backfilling */
   46955            5312 :     walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
   46956                 :   }
   46957                 : 
   46958            5489 :   if( rc==SQLITE_BUSY ){
   46959                 :     /* Reset the return code so as not to report a checkpoint failure
   46960                 :     ** just because there are active readers.  */
   46961             177 :     rc = SQLITE_OK;
   46962                 :   }
   46963                 : 
   46964                 :   /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
   46965                 :   ** file has been copied into the database file, then block until all
   46966                 :   ** readers have finished using the wal file. This ensures that the next
   46967                 :   ** process to write to the database restarts the wal file.
   46968                 :   */
   46969            5489 :   if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
   46970               0 :     assert( pWal->writeLock );
   46971               0 :     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
   46972               0 :       rc = SQLITE_BUSY;
   46973               0 :     }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
   46974               0 :       assert( mxSafeFrame==pWal->hdr.mxFrame );
   46975               0 :       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
   46976               0 :       if( rc==SQLITE_OK ){
   46977               0 :         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
   46978                 :       }
   46979                 :     }
   46980                 :   }
   46981                 : 
   46982                 :  walcheckpoint_out:
   46983            5489 :   walIteratorFree(pIter);
   46984            5489 :   return rc;
   46985                 : }
   46986                 : 
   46987                 : /*
   46988                 : ** If the WAL file is currently larger than nMax bytes in size, truncate
   46989                 : ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
   46990                 : */
   46991            2654 : static void walLimitSize(Wal *pWal, i64 nMax){
   46992                 :   i64 sz;
   46993                 :   int rx;
   46994            2654 :   sqlite3BeginBenignMalloc();
   46995            2654 :   rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
   46996            2654 :   if( rx==SQLITE_OK && (sz > nMax ) ){
   46997            2381 :     rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
   46998                 :   }
   46999            2654 :   sqlite3EndBenignMalloc();
   47000            2654 :   if( rx ){
   47001               0 :     sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
   47002                 :   }
   47003            2654 : }
   47004                 : 
   47005                 : /*
   47006                 : ** Close a connection to a log file.
   47007                 : */
   47008           19517 : SQLITE_PRIVATE int sqlite3WalClose(
   47009                 :   Wal *pWal,                      /* Wal to close */
   47010                 :   int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
   47011                 :   int nBuf,
   47012                 :   u8 *zBuf                        /* Buffer of at least nBuf bytes */
   47013                 : ){
   47014           19517 :   int rc = SQLITE_OK;
   47015           19517 :   if( pWal ){
   47016             426 :     int isDelete = 0;             /* True to unlink wal and wal-index files */
   47017                 : 
   47018                 :     /* If an EXCLUSIVE lock can be obtained on the database file (using the
   47019                 :     ** ordinary, rollback-mode locking methods, this guarantees that the
   47020                 :     ** connection associated with this log file is the only connection to
   47021                 :     ** the database. In this case checkpoint the database and unlink both
   47022                 :     ** the wal and wal-index files.
   47023                 :     **
   47024                 :     ** The EXCLUSIVE lock is not released before returning.
   47025                 :     */
   47026             426 :     rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
   47027             426 :     if( rc==SQLITE_OK ){
   47028             353 :       if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
   47029             353 :         pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
   47030                 :       }
   47031             353 :       rc = sqlite3WalCheckpoint(
   47032                 :           pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
   47033                 :       );
   47034             353 :       if( rc==SQLITE_OK ){
   47035             353 :         int bPersist = -1;
   47036             353 :         sqlite3OsFileControlHint(
   47037                 :             pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
   47038                 :         );
   47039             353 :         if( bPersist!=1 ){
   47040                 :           /* Try to delete the WAL file if the checkpoint completed and
   47041                 :           ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
   47042                 :           ** mode (!bPersist) */
   47043             353 :           isDelete = 1;
   47044               0 :         }else if( pWal->mxWalSize>=0 ){
   47045                 :           /* Try to truncate the WAL file to zero bytes if the checkpoint
   47046                 :           ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
   47047                 :           ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
   47048                 :           ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
   47049                 :           ** to zero bytes as truncating to the journal_size_limit might
   47050                 :           ** leave a corrupt WAL file on disk. */
   47051               0 :           walLimitSize(pWal, 0);
   47052                 :         }
   47053                 :       }
   47054                 :     }
   47055                 : 
   47056             426 :     walIndexClose(pWal, isDelete);
   47057             426 :     sqlite3OsClose(pWal->pWalFd);
   47058             426 :     if( isDelete ){
   47059             353 :       sqlite3BeginBenignMalloc();
   47060             353 :       sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
   47061             353 :       sqlite3EndBenignMalloc();
   47062                 :     }
   47063                 :     WALTRACE(("WAL%p: closed\n", pWal));
   47064             426 :     sqlite3_free((void *)pWal->apWiData);
   47065             426 :     sqlite3_free(pWal);
   47066                 :   }
   47067           19517 :   return rc;
   47068                 : }
   47069                 : 
   47070                 : /*
   47071                 : ** Try to read the wal-index header.  Return 0 on success and 1 if
   47072                 : ** there is a problem.
   47073                 : **
   47074                 : ** The wal-index is in shared memory.  Another thread or process might
   47075                 : ** be writing the header at the same time this procedure is trying to
   47076                 : ** read it, which might result in inconsistency.  A dirty read is detected
   47077                 : ** by verifying that both copies of the header are the same and also by
   47078                 : ** a checksum on the header.
   47079                 : **
   47080                 : ** If and only if the read is consistent and the header is different from
   47081                 : ** pWal->hdr, then pWal->hdr is updated to the content of the new header
   47082                 : ** and *pChanged is set to 1.
   47083                 : **
   47084                 : ** If the checksum cannot be verified return non-zero. If the header
   47085                 : ** is read successfully and the checksum verified, return zero.
   47086                 : */
   47087           45967 : static int walIndexTryHdr(Wal *pWal, int *pChanged){
   47088                 :   u32 aCksum[2];                  /* Checksum on the header content */
   47089                 :   WalIndexHdr h1, h2;             /* Two copies of the header content */
   47090                 :   WalIndexHdr volatile *aHdr;     /* Header in shared memory */
   47091                 : 
   47092                 :   /* The first page of the wal-index must be mapped at this point. */
   47093           45967 :   assert( pWal->nWiData>0 && pWal->apWiData[0] );
   47094                 : 
   47095                 :   /* Read the header. This might happen concurrently with a write to the
   47096                 :   ** same area of shared memory on a different CPU in a SMP,
   47097                 :   ** meaning it is possible that an inconsistent snapshot is read
   47098                 :   ** from the file. If this happens, return non-zero.
   47099                 :   **
   47100                 :   ** There are two copies of the header at the beginning of the wal-index.
   47101                 :   ** When reading, read [0] first then [1].  Writes are in the reverse order.
   47102                 :   ** Memory barriers are used to prevent the compiler or the hardware from
   47103                 :   ** reordering the reads and writes.
   47104                 :   */
   47105           45967 :   aHdr = walIndexHdr(pWal);
   47106           45967 :   memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
   47107           45967 :   walShmBarrier(pWal);
   47108           45967 :   memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
   47109                 : 
   47110           45967 :   if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
   47111               0 :     return 1;   /* Dirty read */
   47112                 :   }  
   47113           45967 :   if( h1.isInit==0 ){
   47114             353 :     return 1;   /* Malformed header - probably all zeros */
   47115                 :   }
   47116           45614 :   walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
   47117           45614 :   if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
   47118               0 :     return 1;   /* Checksum does not match */
   47119                 :   }
   47120                 : 
   47121           45614 :   if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
   47122             188 :     *pChanged = 1;
   47123             188 :     memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
   47124             188 :     pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
   47125                 :     testcase( pWal->szPage<=32768 );
   47126                 :     testcase( pWal->szPage>=65536 );
   47127                 :   }
   47128                 : 
   47129                 :   /* The header was successfully read. Return zero. */
   47130           45614 :   return 0;
   47131                 : }
   47132                 : 
   47133                 : /*
   47134                 : ** Read the wal-index header from the wal-index and into pWal->hdr.
   47135                 : ** If the wal-header appears to be corrupt, try to reconstruct the
   47136                 : ** wal-index from the WAL before returning.
   47137                 : **
   47138                 : ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
   47139                 : ** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
   47140                 : ** to 0.
   47141                 : **
   47142                 : ** If the wal-index header is successfully read, return SQLITE_OK. 
   47143                 : ** Otherwise an SQLite error code.
   47144                 : */
   47145           45967 : static int walIndexReadHdr(Wal *pWal, int *pChanged){
   47146                 :   int rc;                         /* Return code */
   47147                 :   int badHdr;                     /* True if a header read failed */
   47148                 :   volatile u32 *page0;            /* Chunk of wal-index containing header */
   47149                 : 
   47150                 :   /* Ensure that page 0 of the wal-index (the page that contains the 
   47151                 :   ** wal-index header) is mapped. Return early if an error occurs here.
   47152                 :   */
   47153           45967 :   assert( pChanged );
   47154           45967 :   rc = walIndexPage(pWal, 0, &page0);
   47155           45967 :   if( rc!=SQLITE_OK ){
   47156               0 :     return rc;
   47157                 :   };
   47158           45967 :   assert( page0 || pWal->writeLock==0 );
   47159                 : 
   47160                 :   /* If the first page of the wal-index has been mapped, try to read the
   47161                 :   ** wal-index header immediately, without holding any lock. This usually
   47162                 :   ** works, but may fail if the wal-index header is corrupt or currently 
   47163                 :   ** being modified by another thread or process.
   47164                 :   */
   47165           45967 :   badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
   47166                 : 
   47167                 :   /* If the first attempt failed, it might have been due to a race
   47168                 :   ** with a writer.  So get a WRITE lock and try again.
   47169                 :   */
   47170           45967 :   assert( badHdr==0 || pWal->writeLock==0 );
   47171           45967 :   if( badHdr ){
   47172             353 :     if( pWal->readOnly & WAL_SHM_RDONLY ){
   47173               0 :       if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
   47174               0 :         walUnlockShared(pWal, WAL_WRITE_LOCK);
   47175               0 :         rc = SQLITE_READONLY_RECOVERY;
   47176                 :       }
   47177             353 :     }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
   47178             353 :       pWal->writeLock = 1;
   47179             353 :       if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
   47180             353 :         badHdr = walIndexTryHdr(pWal, pChanged);
   47181             353 :         if( badHdr ){
   47182                 :           /* If the wal-index header is still malformed even while holding
   47183                 :           ** a WRITE lock, it can only mean that the header is corrupted and
   47184                 :           ** needs to be reconstructed.  So run recovery to do exactly that.
   47185                 :           */
   47186             353 :           rc = walIndexRecover(pWal);
   47187             353 :           *pChanged = 1;
   47188                 :         }
   47189                 :       }
   47190             353 :       pWal->writeLock = 0;
   47191             353 :       walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
   47192                 :     }
   47193                 :   }
   47194                 : 
   47195                 :   /* If the header is read successfully, check the version number to make
   47196                 :   ** sure the wal-index was not constructed with some future format that
   47197                 :   ** this version of SQLite cannot understand.
   47198                 :   */
   47199           45967 :   if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
   47200               0 :     rc = SQLITE_CANTOPEN_BKPT;
   47201                 :   }
   47202                 : 
   47203           45967 :   return rc;
   47204                 : }
   47205                 : 
   47206                 : /*
   47207                 : ** This is the value that walTryBeginRead returns when it needs to
   47208                 : ** be retried.
   47209                 : */
   47210                 : #define WAL_RETRY  (-1)
   47211                 : 
   47212                 : /*
   47213                 : ** Attempt to start a read transaction.  This might fail due to a race or
   47214                 : ** other transient condition.  When that happens, it returns WAL_RETRY to
   47215                 : ** indicate to the caller that it is safe to retry immediately.
   47216                 : **
   47217                 : ** On success return SQLITE_OK.  On a permanent failure (such an
   47218                 : ** I/O error or an SQLITE_BUSY because another process is running
   47219                 : ** recovery) return a positive error code.
   47220                 : **
   47221                 : ** The useWal parameter is true to force the use of the WAL and disable
   47222                 : ** the case where the WAL is bypassed because it has been completely
   47223                 : ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
   47224                 : ** to make a copy of the wal-index header into pWal->hdr.  If the 
   47225                 : ** wal-index header has changed, *pChanged is set to 1 (as an indication 
   47226                 : ** to the caller that the local paget cache is obsolete and needs to be 
   47227                 : ** flushed.)  When useWal==1, the wal-index header is assumed to already
   47228                 : ** be loaded and the pChanged parameter is unused.
   47229                 : **
   47230                 : ** The caller must set the cnt parameter to the number of prior calls to
   47231                 : ** this routine during the current read attempt that returned WAL_RETRY.
   47232                 : ** This routine will start taking more aggressive measures to clear the
   47233                 : ** race conditions after multiple WAL_RETRY returns, and after an excessive
   47234                 : ** number of errors will ultimately return SQLITE_PROTOCOL.  The
   47235                 : ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
   47236                 : ** and is not honoring the locking protocol.  There is a vanishingly small
   47237                 : ** chance that SQLITE_PROTOCOL could be returned because of a run of really
   47238                 : ** bad luck when there is lots of contention for the wal-index, but that
   47239                 : ** possibility is so small that it can be safely neglected, we believe.
   47240                 : **
   47241                 : ** On success, this routine obtains a read lock on 
   47242                 : ** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
   47243                 : ** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
   47244                 : ** that means the Wal does not hold any read lock.  The reader must not
   47245                 : ** access any database page that is modified by a WAL frame up to and
   47246                 : ** including frame number aReadMark[pWal->readLock].  The reader will
   47247                 : ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
   47248                 : ** Or if pWal->readLock==0, then the reader will ignore the WAL
   47249                 : ** completely and get all content directly from the database file.
   47250                 : ** If the useWal parameter is 1 then the WAL will never be ignored and
   47251                 : ** this routine will always set pWal->readLock>0 on success.
   47252                 : ** When the read transaction is completed, the caller must release the
   47253                 : ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
   47254                 : **
   47255                 : ** This routine uses the nBackfill and aReadMark[] fields of the header
   47256                 : ** to select a particular WAL_READ_LOCK() that strives to let the
   47257                 : ** checkpoint process do as much work as possible.  This routine might
   47258                 : ** update values of the aReadMark[] array in the header, but if it does
   47259                 : ** so it takes care to hold an exclusive lock on the corresponding
   47260                 : ** WAL_READ_LOCK() while changing values.
   47261                 : */
   47262           45410 : static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
   47263                 :   volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
   47264                 :   u32 mxReadMark;                 /* Largest aReadMark[] value */
   47265                 :   int mxI;                        /* Index of largest aReadMark[] value */
   47266                 :   int i;                          /* Loop counter */
   47267           45410 :   int rc = SQLITE_OK;             /* Return code  */
   47268                 : 
   47269           45410 :   assert( pWal->readLock<0 );     /* Not currently locked */
   47270                 : 
   47271                 :   /* Take steps to avoid spinning forever if there is a protocol error.
   47272                 :   **
   47273                 :   ** Circumstances that cause a RETRY should only last for the briefest
   47274                 :   ** instances of time.  No I/O or other system calls are done while the
   47275                 :   ** locks are held, so the locks should not be held for very long. But 
   47276                 :   ** if we are unlucky, another process that is holding a lock might get
   47277                 :   ** paged out or take a page-fault that is time-consuming to resolve, 
   47278                 :   ** during the few nanoseconds that it is holding the lock.  In that case,
   47279                 :   ** it might take longer than normal for the lock to free.
   47280                 :   **
   47281                 :   ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
   47282                 :   ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
   47283                 :   ** is more of a scheduler yield than an actual delay.  But on the 10th
   47284                 :   ** an subsequent retries, the delays start becoming longer and longer, 
   47285                 :   ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
   47286                 :   ** The total delay time before giving up is less than 1 second.
   47287                 :   */
   47288           45410 :   if( cnt>5 ){
   47289               0 :     int nDelay = 1;                      /* Pause time in microseconds */
   47290               0 :     if( cnt>100 ){
   47291               0 :       VVA_ONLY( pWal->lockError = 1; )
   47292               0 :       return SQLITE_PROTOCOL;
   47293                 :     }
   47294               0 :     if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
   47295               0 :     sqlite3OsSleep(pWal->pVfs, nDelay);
   47296                 :   }
   47297                 : 
   47298           45410 :   if( !useWal ){
   47299           40098 :     rc = walIndexReadHdr(pWal, pChanged);
   47300           40098 :     if( rc==SQLITE_BUSY ){
   47301                 :       /* If there is not a recovery running in another thread or process
   47302                 :       ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
   47303                 :       ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
   47304                 :       ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
   47305                 :       ** would be technically correct.  But the race is benign since with
   47306                 :       ** WAL_RETRY this routine will be called again and will probably be
   47307                 :       ** right on the second iteration.
   47308                 :       */
   47309               0 :       if( pWal->apWiData[0]==0 ){
   47310                 :         /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
   47311                 :         ** We assume this is a transient condition, so return WAL_RETRY. The
   47312                 :         ** xShmMap() implementation used by the default unix and win32 VFS 
   47313                 :         ** modules may return SQLITE_BUSY due to a race condition in the 
   47314                 :         ** code that determines whether or not the shared-memory region 
   47315                 :         ** must be zeroed before the requested page is returned.
   47316                 :         */
   47317               0 :         rc = WAL_RETRY;
   47318               0 :       }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
   47319               0 :         walUnlockShared(pWal, WAL_RECOVER_LOCK);
   47320               0 :         rc = WAL_RETRY;
   47321               0 :       }else if( rc==SQLITE_BUSY ){
   47322               0 :         rc = SQLITE_BUSY_RECOVERY;
   47323                 :       }
   47324                 :     }
   47325           40098 :     if( rc!=SQLITE_OK ){
   47326               0 :       return rc;
   47327                 :     }
   47328                 :   }
   47329                 : 
   47330           45410 :   pInfo = walCkptInfo(pWal);
   47331           45410 :   if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
   47332                 :     /* The WAL has been completely backfilled (or it is empty).
   47333                 :     ** and can be safely ignored.
   47334                 :     */
   47335           10354 :     rc = walLockShared(pWal, WAL_READ_LOCK(0));
   47336           10354 :     walShmBarrier(pWal);
   47337           10354 :     if( rc==SQLITE_OK ){
   47338           10354 :       if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
   47339                 :         /* It is not safe to allow the reader to continue here if frames
   47340                 :         ** may have been appended to the log before READ_LOCK(0) was obtained.
   47341                 :         ** When holding READ_LOCK(0), the reader ignores the entire log file,
   47342                 :         ** which implies that the database file contains a trustworthy
   47343                 :         ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
   47344                 :         ** happening, this is usually correct.
   47345                 :         **
   47346                 :         ** However, if frames have been appended to the log (or if the log 
   47347                 :         ** is wrapped and written for that matter) before the READ_LOCK(0)
   47348                 :         ** is obtained, that is not necessarily true. A checkpointer may
   47349                 :         ** have started to backfill the appended frames but crashed before
   47350                 :         ** it finished. Leaving a corrupt image in the database file.
   47351                 :         */
   47352               0 :         walUnlockShared(pWal, WAL_READ_LOCK(0));
   47353               0 :         return WAL_RETRY;
   47354                 :       }
   47355           10354 :       pWal->readLock = 0;
   47356           10354 :       return SQLITE_OK;
   47357               0 :     }else if( rc!=SQLITE_BUSY ){
   47358               0 :       return rc;
   47359                 :     }
   47360                 :   }
   47361                 : 
   47362                 :   /* If we get this far, it means that the reader will want to use
   47363                 :   ** the WAL to get at content from recent commits.  The job now is
   47364                 :   ** to select one of the aReadMark[] entries that is closest to
   47365                 :   ** but not exceeding pWal->hdr.mxFrame and lock that entry.
   47366                 :   */
   47367           35056 :   mxReadMark = 0;
   47368           35056 :   mxI = 0;
   47369          175280 :   for(i=1; i<WAL_NREADER; i++){
   47370          140224 :     u32 thisMark = pInfo->aReadMark[i];
   47371          140224 :     if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
   47372           29566 :       assert( thisMark!=READMARK_NOT_USED );
   47373           29566 :       mxReadMark = thisMark;
   47374           29566 :       mxI = i;
   47375                 :     }
   47376                 :   }
   47377                 :   /* There was once an "if" here. The extra "{" is to preserve indentation. */
   47378                 :   {
   47379           35056 :     if( (pWal->readOnly & WAL_SHM_RDONLY)==0
   47380           35056 :      && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
   47381                 :     ){
   47382           25231 :       for(i=1; i<WAL_NREADER; i++){
   47383           25231 :         rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
   47384           25231 :         if( rc==SQLITE_OK ){
   47385           25231 :           mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
   47386           25231 :           mxI = i;
   47387           25231 :           walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
   47388           25231 :           break;
   47389               0 :         }else if( rc!=SQLITE_BUSY ){
   47390               0 :           return rc;
   47391                 :         }
   47392                 :       }
   47393                 :     }
   47394           35056 :     if( mxI==0 ){
   47395               0 :       assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
   47396               0 :       return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
   47397                 :     }
   47398                 : 
   47399           35056 :     rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
   47400           35056 :     if( rc ){
   47401               0 :       return rc==SQLITE_BUSY ? WAL_RETRY : rc;
   47402                 :     }
   47403                 :     /* Now that the read-lock has been obtained, check that neither the
   47404                 :     ** value in the aReadMark[] array or the contents of the wal-index
   47405                 :     ** header have changed.
   47406                 :     **
   47407                 :     ** It is necessary to check that the wal-index header did not change
   47408                 :     ** between the time it was read and when the shared-lock was obtained
   47409                 :     ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
   47410                 :     ** that the log file may have been wrapped by a writer, or that frames
   47411                 :     ** that occur later in the log than pWal->hdr.mxFrame may have been
   47412                 :     ** copied into the database by a checkpointer. If either of these things
   47413                 :     ** happened, then reading the database with the current value of
   47414                 :     ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
   47415                 :     ** instead.
   47416                 :     **
   47417                 :     ** This does not guarantee that the copy of the wal-index header is up to
   47418                 :     ** date before proceeding. That would not be possible without somehow
   47419                 :     ** blocking writers. It only guarantees that a dangerous checkpoint or 
   47420                 :     ** log-wrap (either of which would require an exclusive lock on
   47421                 :     ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
   47422                 :     */
   47423           35056 :     walShmBarrier(pWal);
   47424           35056 :     if( pInfo->aReadMark[mxI]!=mxReadMark
   47425           35056 :      || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
   47426                 :     ){
   47427               0 :       walUnlockShared(pWal, WAL_READ_LOCK(mxI));
   47428               0 :       return WAL_RETRY;
   47429                 :     }else{
   47430           35056 :       assert( mxReadMark<=pWal->hdr.mxFrame );
   47431           35056 :       pWal->readLock = (i16)mxI;
   47432                 :     }
   47433                 :   }
   47434           35056 :   return rc;
   47435                 : }
   47436                 : 
   47437                 : /*
   47438                 : ** Begin a read transaction on the database.
   47439                 : **
   47440                 : ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
   47441                 : ** it takes a snapshot of the state of the WAL and wal-index for the current
   47442                 : ** instant in time.  The current thread will continue to use this snapshot.
   47443                 : ** Other threads might append new content to the WAL and wal-index but
   47444                 : ** that extra content is ignored by the current thread.
   47445                 : **
   47446                 : ** If the database contents have changes since the previous read
   47447                 : ** transaction, then *pChanged is set to 1 before returning.  The
   47448                 : ** Pager layer will use this to know that is cache is stale and
   47449                 : ** needs to be flushed.
   47450                 : */
   47451           40098 : SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
   47452                 :   int rc;                         /* Return code */
   47453           40098 :   int cnt = 0;                    /* Number of TryBeginRead attempts */
   47454                 : 
   47455                 :   do{
   47456           40098 :     rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
   47457           40098 :   }while( rc==WAL_RETRY );
   47458                 :   testcase( (rc&0xff)==SQLITE_BUSY );
   47459                 :   testcase( (rc&0xff)==SQLITE_IOERR );
   47460                 :   testcase( rc==SQLITE_PROTOCOL );
   47461                 :   testcase( rc==SQLITE_OK );
   47462           40098 :   return rc;
   47463                 : }
   47464                 : 
   47465                 : /*
   47466                 : ** Finish with a read transaction.  All this does is release the
   47467                 : ** read-lock.
   47468                 : */
   47469           80589 : SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
   47470           80589 :   sqlite3WalEndWriteTransaction(pWal);
   47471           80589 :   if( pWal->readLock>=0 ){
   47472           40098 :     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
   47473           40098 :     pWal->readLock = -1;
   47474                 :   }
   47475           80589 : }
   47476                 : 
   47477                 : /*
   47478                 : ** Read a page from the WAL, if it is present in the WAL and if the 
   47479                 : ** current read transaction is configured to use the WAL.  
   47480                 : **
   47481                 : ** The *pInWal is set to 1 if the requested page is in the WAL and
   47482                 : ** has been loaded.  Or *pInWal is set to 0 if the page was not in 
   47483                 : ** the WAL and needs to be read out of the database.
   47484                 : */
   47485            1476 : SQLITE_PRIVATE int sqlite3WalRead(
   47486                 :   Wal *pWal,                      /* WAL handle */
   47487                 :   Pgno pgno,                      /* Database page number to read data for */
   47488                 :   int *pInWal,                    /* OUT: True if data is read from WAL */
   47489                 :   int nOut,                       /* Size of buffer pOut in bytes */
   47490                 :   u8 *pOut                        /* Buffer to write page data to */
   47491                 : ){
   47492            1476 :   u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
   47493            1476 :   u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
   47494                 :   int iHash;                      /* Used to loop through N hash tables */
   47495                 : 
   47496                 :   /* This routine is only be called from within a read transaction. */
   47497            1476 :   assert( pWal->readLock>=0 || pWal->lockError );
   47498                 : 
   47499                 :   /* If the "last page" field of the wal-index header snapshot is 0, then
   47500                 :   ** no data will be read from the wal under any circumstances. Return early
   47501                 :   ** in this case as an optimization.  Likewise, if pWal->readLock==0, 
   47502                 :   ** then the WAL is ignored by the reader so return early, as if the 
   47503                 :   ** WAL were empty.
   47504                 :   */
   47505            1476 :   if( iLast==0 || pWal->readLock==0 ){
   47506            1007 :     *pInWal = 0;
   47507            1007 :     return SQLITE_OK;
   47508                 :   }
   47509                 : 
   47510                 :   /* Search the hash table or tables for an entry matching page number
   47511                 :   ** pgno. Each iteration of the following for() loop searches one
   47512                 :   ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
   47513                 :   **
   47514                 :   ** This code might run concurrently to the code in walIndexAppend()
   47515                 :   ** that adds entries to the wal-index (and possibly to this hash 
   47516                 :   ** table). This means the value just read from the hash 
   47517                 :   ** slot (aHash[iKey]) may have been added before or after the 
   47518                 :   ** current read transaction was opened. Values added after the
   47519                 :   ** read transaction was opened may have been written incorrectly -
   47520                 :   ** i.e. these slots may contain garbage data. However, we assume
   47521                 :   ** that any slots written before the current read transaction was
   47522                 :   ** opened remain unmodified.
   47523                 :   **
   47524                 :   ** For the reasons above, the if(...) condition featured in the inner
   47525                 :   ** loop of the following block is more stringent that would be required 
   47526                 :   ** if we had exclusive access to the hash-table:
   47527                 :   **
   47528                 :   **   (aPgno[iFrame]==pgno): 
   47529                 :   **     This condition filters out normal hash-table collisions.
   47530                 :   **
   47531                 :   **   (iFrame<=iLast): 
   47532                 :   **     This condition filters out entries that were added to the hash
   47533                 :   **     table after the current read-transaction had started.
   47534                 :   */
   47535             938 :   for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
   47536                 :     volatile ht_slot *aHash;      /* Pointer to hash table */
   47537                 :     volatile u32 *aPgno;          /* Pointer to array of page numbers */
   47538                 :     u32 iZero;                    /* Frame number corresponding to aPgno[0] */
   47539                 :     int iKey;                     /* Hash slot index */
   47540                 :     int nCollide;                 /* Number of hash collisions remaining */
   47541                 :     int rc;                       /* Error code */
   47542                 : 
   47543             469 :     rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
   47544             469 :     if( rc!=SQLITE_OK ){
   47545               0 :       return rc;
   47546                 :     }
   47547             469 :     nCollide = HASHTABLE_NSLOT;
   47548             732 :     for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
   47549             263 :       u32 iFrame = aHash[iKey] + iZero;
   47550             263 :       if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
   47551                 :         /* assert( iFrame>iRead ); -- not true if there is corruption */
   47552             263 :         iRead = iFrame;
   47553                 :       }
   47554             263 :       if( (nCollide--)==0 ){
   47555               0 :         return SQLITE_CORRUPT_BKPT;
   47556                 :       }
   47557                 :     }
   47558                 :   }
   47559                 : 
   47560                 : #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
   47561                 :   /* If expensive assert() statements are available, do a linear search
   47562                 :   ** of the wal-index file content. Make sure the results agree with the
   47563                 :   ** result obtained using the hash indexes above.  */
   47564                 :   {
   47565                 :     u32 iRead2 = 0;
   47566                 :     u32 iTest;
   47567                 :     for(iTest=iLast; iTest>0; iTest--){
   47568                 :       if( walFramePgno(pWal, iTest)==pgno ){
   47569                 :         iRead2 = iTest;
   47570                 :         break;
   47571                 :       }
   47572                 :     }
   47573                 :     assert( iRead==iRead2 );
   47574                 :   }
   47575                 : #endif
   47576                 : 
   47577                 :   /* If iRead is non-zero, then it is the log frame number that contains the
   47578                 :   ** required page. Read and return data from the log file.
   47579                 :   */
   47580             469 :   if( iRead ){
   47581                 :     int sz;
   47582                 :     i64 iOffset;
   47583             209 :     sz = pWal->hdr.szPage;
   47584             209 :     sz = (sz&0xfe00) + ((sz&0x0001)<<16);
   47585                 :     testcase( sz<=32768 );
   47586                 :     testcase( sz>=65536 );
   47587             209 :     iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
   47588             209 :     *pInWal = 1;
   47589                 :     /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
   47590             209 :     return sqlite3OsRead(pWal->pWalFd, pOut, nOut, iOffset);
   47591                 :   }
   47592                 : 
   47593             260 :   *pInWal = 0;
   47594             260 :   return SQLITE_OK;
   47595                 : }
   47596                 : 
   47597                 : 
   47598                 : /* 
   47599                 : ** Return the size of the database in pages (or zero, if unknown).
   47600                 : */
   47601           85461 : SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
   47602           85461 :   if( pWal && ALWAYS(pWal->readLock>=0) ){
   47603           40098 :     return pWal->hdr.nPage;
   47604                 :   }
   47605           45363 :   return 0;
   47606                 : }
   47607                 : 
   47608                 : 
   47609                 : /* 
   47610                 : ** This function starts a write transaction on the WAL.
   47611                 : **
   47612                 : ** A read transaction must have already been started by a prior call
   47613                 : ** to sqlite3WalBeginReadTransaction().
   47614                 : **
   47615                 : ** If another thread or process has written into the database since
   47616                 : ** the read transaction was started, then it is not possible for this
   47617                 : ** thread to write as doing so would cause a fork.  So this routine
   47618                 : ** returns SQLITE_BUSY in that case and no write transaction is started.
   47619                 : **
   47620                 : ** There can only be a single writer active at a time.
   47621                 : */
   47622           26364 : SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
   47623                 :   int rc;
   47624                 : 
   47625                 :   /* Cannot start a write transaction without first holding a read
   47626                 :   ** transaction. */
   47627           26364 :   assert( pWal->readLock>=0 );
   47628                 : 
   47629           26364 :   if( pWal->readOnly ){
   47630               0 :     return SQLITE_READONLY;
   47631                 :   }
   47632                 : 
   47633                 :   /* Only one writer allowed at a time.  Get the write lock.  Return
   47634                 :   ** SQLITE_BUSY if unable.
   47635                 :   */
   47636           26364 :   rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
   47637           26364 :   if( rc ){
   47638               0 :     return rc;
   47639                 :   }
   47640           26364 :   pWal->writeLock = 1;
   47641                 : 
   47642                 :   /* If another connection has written to the database file since the
   47643                 :   ** time the read transaction on this connection was started, then
   47644                 :   ** the write is disallowed.
   47645                 :   */
   47646           26364 :   if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
   47647               0 :     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
   47648               0 :     pWal->writeLock = 0;
   47649               0 :     rc = SQLITE_BUSY;
   47650                 :   }
   47651                 : 
   47652           26364 :   return rc;
   47653                 : }
   47654                 : 
   47655                 : /*
   47656                 : ** End a write transaction.  The commit has already been done.  This
   47657                 : ** routine merely releases the lock.
   47658                 : */
   47659          112822 : SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
   47660          112822 :   if( pWal->writeLock ){
   47661           26364 :     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
   47662           26364 :     pWal->writeLock = 0;
   47663           26364 :     pWal->truncateOnCommit = 0;
   47664                 :   }
   47665          112822 :   return SQLITE_OK;
   47666                 : }
   47667                 : 
   47668                 : /*
   47669                 : ** If any data has been written (but not committed) to the log file, this
   47670                 : ** function moves the write-pointer back to the start of the transaction.
   47671                 : **
   47672                 : ** Additionally, the callback function is invoked for each frame written
   47673                 : ** to the WAL since the start of the transaction. If the callback returns
   47674                 : ** other than SQLITE_OK, it is not invoked again and the error code is
   47675                 : ** returned to the caller.
   47676                 : **
   47677                 : ** Otherwise, if the callback function does not return an error, this
   47678                 : ** function returns SQLITE_OK.
   47679                 : */
   47680             304 : SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
   47681             304 :   int rc = SQLITE_OK;
   47682             304 :   if( ALWAYS(pWal->writeLock) ){
   47683             304 :     Pgno iMax = pWal->hdr.mxFrame;
   47684                 :     Pgno iFrame;
   47685                 :   
   47686                 :     /* Restore the clients cache of the wal-index header to the state it
   47687                 :     ** was in before the client began writing to the database. 
   47688                 :     */
   47689             304 :     memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
   47690                 : 
   47691             608 :     for(iFrame=pWal->hdr.mxFrame+1; 
   47692               0 :         ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; 
   47693               0 :         iFrame++
   47694                 :     ){
   47695                 :       /* This call cannot fail. Unless the page for which the page number
   47696                 :       ** is passed as the second argument is (a) in the cache and 
   47697                 :       ** (b) has an outstanding reference, then xUndo is either a no-op
   47698                 :       ** (if (a) is false) or simply expels the page from the cache (if (b)
   47699                 :       ** is false).
   47700                 :       **
   47701                 :       ** If the upper layer is doing a rollback, it is guaranteed that there
   47702                 :       ** are no outstanding references to any page other than page 1. And
   47703                 :       ** page 1 is never written to the log until the transaction is
   47704                 :       ** committed. As a result, the call to xUndo may not fail.
   47705                 :       */
   47706               0 :       assert( walFramePgno(pWal, iFrame)!=1 );
   47707               0 :       rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
   47708                 :     }
   47709             304 :     walCleanupHash(pWal);
   47710                 :   }
   47711             304 :   assert( rc==SQLITE_OK );
   47712             304 :   return rc;
   47713                 : }
   47714                 : 
   47715                 : /* 
   47716                 : ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32 
   47717                 : ** values. This function populates the array with values required to 
   47718                 : ** "rollback" the write position of the WAL handle back to the current 
   47719                 : ** point in the event of a savepoint rollback (via WalSavepointUndo()).
   47720                 : */
   47721           15551 : SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
   47722           15551 :   assert( pWal->writeLock );
   47723           15551 :   aWalData[0] = pWal->hdr.mxFrame;
   47724           15551 :   aWalData[1] = pWal->hdr.aFrameCksum[0];
   47725           15551 :   aWalData[2] = pWal->hdr.aFrameCksum[1];
   47726           15551 :   aWalData[3] = pWal->nCkpt;
   47727           15551 : }
   47728                 : 
   47729                 : /* 
   47730                 : ** Move the write position of the WAL back to the point identified by
   47731                 : ** the values in the aWalData[] array. aWalData must point to an array
   47732                 : ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
   47733                 : ** by a call to WalSavepoint().
   47734                 : */
   47735               4 : SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
   47736               4 :   int rc = SQLITE_OK;
   47737                 : 
   47738               4 :   assert( pWal->writeLock );
   47739               4 :   assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
   47740                 : 
   47741               4 :   if( aWalData[3]!=pWal->nCkpt ){
   47742                 :     /* This savepoint was opened immediately after the write-transaction
   47743                 :     ** was started. Right after that, the writer decided to wrap around
   47744                 :     ** to the start of the log. Update the savepoint values to match.
   47745                 :     */
   47746               0 :     aWalData[0] = 0;
   47747               0 :     aWalData[3] = pWal->nCkpt;
   47748                 :   }
   47749                 : 
   47750               4 :   if( aWalData[0]<pWal->hdr.mxFrame ){
   47751               0 :     pWal->hdr.mxFrame = aWalData[0];
   47752               0 :     pWal->hdr.aFrameCksum[0] = aWalData[1];
   47753               0 :     pWal->hdr.aFrameCksum[1] = aWalData[2];
   47754               0 :     walCleanupHash(pWal);
   47755                 :   }
   47756                 : 
   47757               4 :   return rc;
   47758                 : }
   47759                 : 
   47760                 : 
   47761                 : /*
   47762                 : ** This function is called just before writing a set of frames to the log
   47763                 : ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
   47764                 : ** to the current log file, it is possible to overwrite the start of the
   47765                 : ** existing log file with the new frames (i.e. "reset" the log). If so,
   47766                 : ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
   47767                 : ** unchanged.
   47768                 : **
   47769                 : ** SQLITE_OK is returned if no error is encountered (regardless of whether
   47770                 : ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
   47771                 : ** if an error occurs.
   47772                 : */
   47773           25040 : static int walRestartLog(Wal *pWal){
   47774           25040 :   int rc = SQLITE_OK;
   47775                 :   int cnt;
   47776                 : 
   47777           25040 :   if( pWal->readLock==0 ){
   47778            5312 :     volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
   47779            5312 :     assert( pInfo->nBackfill==pWal->hdr.mxFrame );
   47780            5312 :     if( pInfo->nBackfill>0 ){
   47781                 :       u32 salt1;
   47782            5008 :       sqlite3_randomness(4, &salt1);
   47783            5008 :       rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
   47784            5008 :       if( rc==SQLITE_OK ){
   47785                 :         /* If all readers are using WAL_READ_LOCK(0) (in other words if no
   47786                 :         ** readers are currently using the WAL), then the transactions
   47787                 :         ** frames will overwrite the start of the existing log. Update the
   47788                 :         ** wal-index header to reflect this.
   47789                 :         **
   47790                 :         ** In theory it would be Ok to update the cache of the header only
   47791                 :         ** at this point. But updating the actual wal-index header is also
   47792                 :         ** safe and means there is no special case for sqlite3WalUndo()
   47793                 :         ** to handle if this transaction is rolled back.
   47794                 :         */
   47795                 :         int i;                    /* Loop counter */
   47796            5008 :         u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
   47797                 : 
   47798            5008 :         pWal->nCkpt++;
   47799            5008 :         pWal->hdr.mxFrame = 0;
   47800            5008 :         sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
   47801            5008 :         aSalt[1] = salt1;
   47802            5008 :         walIndexWriteHdr(pWal);
   47803            5008 :         pInfo->nBackfill = 0;
   47804            5008 :         for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
   47805            5008 :         assert( pInfo->aReadMark[0]==0 );
   47806            5008 :         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
   47807               0 :       }else if( rc!=SQLITE_BUSY ){
   47808               0 :         return rc;
   47809                 :       }
   47810                 :     }
   47811            5312 :     walUnlockShared(pWal, WAL_READ_LOCK(0));
   47812            5312 :     pWal->readLock = -1;
   47813            5312 :     cnt = 0;
   47814                 :     do{
   47815                 :       int notUsed;
   47816            5312 :       rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
   47817            5312 :     }while( rc==WAL_RETRY );
   47818            5312 :     assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
   47819                 :     testcase( (rc&0xff)==SQLITE_IOERR );
   47820                 :     testcase( rc==SQLITE_PROTOCOL );
   47821                 :     testcase( rc==SQLITE_OK );
   47822                 :   }
   47823           25040 :   return rc;
   47824                 : }
   47825                 : 
   47826                 : /*
   47827                 : ** Information about the current state of the WAL file and where
   47828                 : ** the next fsync should occur - passed from sqlite3WalFrames() into
   47829                 : ** walWriteToLog().
   47830                 : */
   47831                 : typedef struct WalWriter {
   47832                 :   Wal *pWal;                   /* The complete WAL information */
   47833                 :   sqlite3_file *pFd;           /* The WAL file to which we write */
   47834                 :   sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
   47835                 :   int syncFlags;               /* Flags for the fsync */
   47836                 :   int szPage;                  /* Size of one page */
   47837                 : } WalWriter;
   47838                 : 
   47839                 : /*
   47840                 : ** Write iAmt bytes of content into the WAL file beginning at iOffset.
   47841                 : ** Do a sync when crossing the p->iSyncPoint boundary.
   47842                 : **
   47843                 : ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
   47844                 : ** first write the part before iSyncPoint, then sync, then write the
   47845                 : ** rest.
   47846                 : */
   47847          203378 : static int walWriteToLog(
   47848                 :   WalWriter *p,              /* WAL to write to */
   47849                 :   void *pContent,            /* Content to be written */
   47850                 :   int iAmt,                  /* Number of bytes to write */
   47851                 :   sqlite3_int64 iOffset      /* Start writing at this offset */
   47852                 : ){
   47853                 :   int rc;
   47854          203378 :   if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
   47855               0 :     int iFirstAmt = (int)(p->iSyncPoint - iOffset);
   47856               0 :     rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
   47857               0 :     if( rc ) return rc;
   47858               0 :     iOffset += iFirstAmt;
   47859               0 :     iAmt -= iFirstAmt;
   47860               0 :     pContent = (void*)(iFirstAmt + (char*)pContent);
   47861               0 :     assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
   47862               0 :     rc = sqlite3OsSync(p->pFd, p->syncFlags);
   47863               0 :     if( iAmt==0 || rc ) return rc;
   47864                 :   }
   47865          203378 :   rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
   47866          203378 :   return rc;
   47867                 : }
   47868                 : 
   47869                 : /*
   47870                 : ** Write out a single frame of the WAL
   47871                 : */
   47872          101689 : static int walWriteOneFrame(
   47873                 :   WalWriter *p,               /* Where to write the frame */
   47874                 :   PgHdr *pPage,               /* The page of the frame to be written */
   47875                 :   int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
   47876                 :   sqlite3_int64 iOffset       /* Byte offset at which to write */
   47877                 : ){
   47878                 :   int rc;                         /* Result code from subfunctions */
   47879                 :   void *pData;                    /* Data actually written */
   47880                 :   u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
   47881                 : #if defined(SQLITE_HAS_CODEC)
   47882                 :   if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
   47883                 : #else
   47884          101689 :   pData = pPage->pData;
   47885                 : #endif
   47886          101689 :   walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
   47887          101689 :   rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
   47888          101689 :   if( rc ) return rc;
   47889                 :   /* Write the page data */
   47890          101689 :   rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
   47891          101689 :   return rc;
   47892                 : }
   47893                 : 
   47894                 : /* 
   47895                 : ** Write a set of frames to the log. The caller must hold the write-lock
   47896                 : ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
   47897                 : */
   47898           25040 : SQLITE_PRIVATE int sqlite3WalFrames(
   47899                 :   Wal *pWal,                      /* Wal handle to write to */
   47900                 :   int szPage,                     /* Database page-size in bytes */
   47901                 :   PgHdr *pList,                   /* List of dirty pages to write */
   47902                 :   Pgno nTruncate,                 /* Database size after this commit */
   47903                 :   int isCommit,                   /* True if this is a commit */
   47904                 :   int sync_flags                  /* Flags to pass to OsSync() (or 0) */
   47905                 : ){
   47906                 :   int rc;                         /* Used to catch return codes */
   47907                 :   u32 iFrame;                     /* Next frame address */
   47908                 :   PgHdr *p;                       /* Iterator to run through pList with. */
   47909           25040 :   PgHdr *pLast = 0;               /* Last frame in list */
   47910           25040 :   int nExtra = 0;                 /* Number of extra copies of last page */
   47911                 :   int szFrame;                    /* The size of a single frame */
   47912                 :   i64 iOffset;                    /* Next byte to write in WAL file */
   47913                 :   WalWriter w;                    /* The writer */
   47914                 : 
   47915           25040 :   assert( pList );
   47916           25040 :   assert( pWal->writeLock );
   47917                 : 
   47918                 :   /* If this frame set completes a transaction, then nTruncate>0.  If
   47919                 :   ** nTruncate==0 then this frame set does not complete the transaction. */
   47920           25040 :   assert( (isCommit!=0)==(nTruncate!=0) );
   47921                 : 
   47922                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
   47923                 :   { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
   47924                 :     WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
   47925                 :               pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
   47926                 :   }
   47927                 : #endif
   47928                 : 
   47929                 :   /* See if it is possible to write these frames into the start of the
   47930                 :   ** log file, instead of appending to it at pWal->hdr.mxFrame.
   47931                 :   */
   47932           25040 :   if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
   47933               0 :     return rc;
   47934                 :   }
   47935                 : 
   47936                 :   /* If this is the first frame written into the log, write the WAL
   47937                 :   ** header to the start of the WAL file. See comments at the top of
   47938                 :   ** this source file for a description of the WAL header format.
   47939                 :   */
   47940           25040 :   iFrame = pWal->hdr.mxFrame;
   47941           25040 :   if( iFrame==0 ){
   47942                 :     u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
   47943                 :     u32 aCksum[2];                /* Checksum for wal-header */
   47944                 : 
   47945            5312 :     sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
   47946            5312 :     sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
   47947            5312 :     sqlite3Put4byte(&aWalHdr[8], szPage);
   47948            5312 :     sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
   47949            5312 :     if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
   47950            5312 :     memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
   47951            5312 :     walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
   47952            5312 :     sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
   47953            5312 :     sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
   47954                 :     
   47955            5312 :     pWal->szPage = szPage;
   47956            5312 :     pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
   47957            5312 :     pWal->hdr.aFrameCksum[0] = aCksum[0];
   47958            5312 :     pWal->hdr.aFrameCksum[1] = aCksum[1];
   47959            5312 :     pWal->truncateOnCommit = 1;
   47960                 : 
   47961            5312 :     rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
   47962                 :     WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
   47963            5312 :     if( rc!=SQLITE_OK ){
   47964               0 :       return rc;
   47965                 :     }
   47966                 : 
   47967                 :     /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
   47968                 :     ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
   47969                 :     ** an out-of-order write following a WAL restart could result in
   47970                 :     ** database corruption.  See the ticket:
   47971                 :     **
   47972                 :     **     http://localhost:591/sqlite/info/ff5be73dee
   47973                 :     */
   47974            5312 :     if( pWal->syncHeader && sync_flags ){
   47975            3162 :       rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
   47976            3162 :       if( rc ) return rc;
   47977                 :     }
   47978                 :   }
   47979           25040 :   assert( (int)pWal->szPage==szPage );
   47980                 : 
   47981                 :   /* Setup information needed to write frames into the WAL */
   47982           25040 :   w.pWal = pWal;
   47983           25040 :   w.pFd = pWal->pWalFd;
   47984           25040 :   w.iSyncPoint = 0;
   47985           25040 :   w.syncFlags = sync_flags;
   47986           25040 :   w.szPage = szPage;
   47987           25040 :   iOffset = walFrameOffset(iFrame+1, szPage);
   47988           25040 :   szFrame = szPage + WAL_FRAME_HDRSIZE;
   47989                 : 
   47990                 :   /* Write all frames into the log file exactly once */
   47991          126729 :   for(p=pList; p; p=p->pDirty){
   47992                 :     int nDbSize;   /* 0 normally.  Positive == commit flag */
   47993          101689 :     iFrame++;
   47994          101689 :     assert( iOffset==walFrameOffset(iFrame, szPage) );
   47995          101689 :     nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
   47996          101689 :     rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
   47997          101689 :     if( rc ) return rc;
   47998          101689 :     pLast = p;
   47999          101689 :     iOffset += szFrame;
   48000                 :   }
   48001                 : 
   48002                 :   /* If this is the end of a transaction, then we might need to pad
   48003                 :   ** the transaction and/or sync the WAL file.
   48004                 :   **
   48005                 :   ** Padding and syncing only occur if this set of frames complete a
   48006                 :   ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
   48007                 :   ** or synchonous==OFF, then no padding or syncing are needed.
   48008                 :   **
   48009                 :   ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
   48010                 :   ** needed and only the sync is done.  If padding is needed, then the
   48011                 :   ** final frame is repeated (with its commit mark) until the next sector
   48012                 :   ** boundary is crossed.  Only the part of the WAL prior to the last
   48013                 :   ** sector boundary is synced; the part of the last frame that extends
   48014                 :   ** past the sector boundary is written after the sync.
   48015                 :   */
   48016           25040 :   if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
   48017               0 :     if( pWal->padToSectorBoundary ){
   48018               0 :       int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
   48019               0 :       w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
   48020               0 :       while( iOffset<w.iSyncPoint ){
   48021               0 :         rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
   48022               0 :         if( rc ) return rc;
   48023               0 :         iOffset += szFrame;
   48024               0 :         nExtra++;
   48025                 :       }
   48026                 :     }else{
   48027               0 :       rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
   48028                 :     }
   48029                 :   }
   48030                 : 
   48031                 :   /* If this frame set completes the first transaction in the WAL and
   48032                 :   ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
   48033                 :   ** journal size limit, if possible.
   48034                 :   */
   48035           25040 :   if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
   48036            2654 :     i64 sz = pWal->mxWalSize;
   48037            2654 :     if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
   48038            2654 :       sz = walFrameOffset(iFrame+nExtra+1, szPage);
   48039                 :     }
   48040            2654 :     walLimitSize(pWal, sz);
   48041            2654 :     pWal->truncateOnCommit = 0;
   48042                 :   }
   48043                 : 
   48044                 :   /* Append data to the wal-index. It is not necessary to lock the 
   48045                 :   ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
   48046                 :   ** guarantees that there are no other writers, and no data that may
   48047                 :   ** be in use by existing readers is being overwritten.
   48048                 :   */
   48049           25040 :   iFrame = pWal->hdr.mxFrame;
   48050          126729 :   for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
   48051          101689 :     iFrame++;
   48052          101689 :     rc = walIndexAppend(pWal, iFrame, p->pgno);
   48053                 :   }
   48054           50080 :   while( rc==SQLITE_OK && nExtra>0 ){
   48055               0 :     iFrame++;
   48056               0 :     nExtra--;
   48057               0 :     rc = walIndexAppend(pWal, iFrame, pLast->pgno);
   48058                 :   }
   48059                 : 
   48060           25040 :   if( rc==SQLITE_OK ){
   48061                 :     /* Update the private copy of the header. */
   48062           25040 :     pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
   48063                 :     testcase( szPage<=32768 );
   48064                 :     testcase( szPage>=65536 );
   48065           25040 :     pWal->hdr.mxFrame = iFrame;
   48066           25040 :     if( isCommit ){
   48067           25040 :       pWal->hdr.iChange++;
   48068           25040 :       pWal->hdr.nPage = nTruncate;
   48069                 :     }
   48070                 :     /* If this is a commit, update the wal-index header too. */
   48071           25040 :     if( isCommit ){
   48072           25040 :       walIndexWriteHdr(pWal);
   48073           25040 :       pWal->iCallback = iFrame;
   48074                 :     }
   48075                 :   }
   48076                 : 
   48077                 :   WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
   48078           25040 :   return rc;
   48079                 : }
   48080                 : 
   48081                 : /* 
   48082                 : ** This routine is called to implement sqlite3_wal_checkpoint() and
   48083                 : ** related interfaces.
   48084                 : **
   48085                 : ** Obtain a CHECKPOINT lock and then backfill as much information as
   48086                 : ** we can from WAL into the database.
   48087                 : **
   48088                 : ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
   48089                 : ** callback. In this case this function runs a blocking checkpoint.
   48090                 : */
   48091            5869 : SQLITE_PRIVATE int sqlite3WalCheckpoint(
   48092                 :   Wal *pWal,                      /* Wal connection */
   48093                 :   int eMode,                      /* PASSIVE, FULL or RESTART */
   48094                 :   int (*xBusy)(void*),            /* Function to call when busy */
   48095                 :   void *pBusyArg,                 /* Context argument for xBusyHandler */
   48096                 :   int sync_flags,                 /* Flags to sync db file with (or 0) */
   48097                 :   int nBuf,                       /* Size of temporary buffer */
   48098                 :   u8 *zBuf,                       /* Temporary buffer to use */
   48099                 :   int *pnLog,                     /* OUT: Number of frames in WAL */
   48100                 :   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
   48101                 : ){
   48102                 :   int rc;                         /* Return code */
   48103            5869 :   int isChanged = 0;              /* True if a new wal-index header is loaded */
   48104            5869 :   int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
   48105                 : 
   48106            5869 :   assert( pWal->ckptLock==0 );
   48107            5869 :   assert( pWal->writeLock==0 );
   48108                 : 
   48109            5869 :   if( pWal->readOnly ) return SQLITE_READONLY;
   48110                 :   WALTRACE(("WAL%p: checkpoint begins\n", pWal));
   48111            5869 :   rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
   48112            5869 :   if( rc ){
   48113                 :     /* Usually this is SQLITE_BUSY meaning that another thread or process
   48114                 :     ** is already running a checkpoint, or maybe a recovery.  But it might
   48115                 :     ** also be SQLITE_IOERR. */
   48116               0 :     return rc;
   48117                 :   }
   48118            5869 :   pWal->ckptLock = 1;
   48119                 : 
   48120                 :   /* If this is a blocking-checkpoint, then obtain the write-lock as well
   48121                 :   ** to prevent any writers from running while the checkpoint is underway.
   48122                 :   ** This has to be done before the call to walIndexReadHdr() below.
   48123                 :   **
   48124                 :   ** If the writer lock cannot be obtained, then a passive checkpoint is
   48125                 :   ** run instead. Since the checkpointer is not holding the writer lock,
   48126                 :   ** there is no point in blocking waiting for any readers. Assuming no 
   48127                 :   ** other error occurs, this function will return SQLITE_BUSY to the caller.
   48128                 :   */
   48129            5869 :   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
   48130               0 :     rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
   48131               0 :     if( rc==SQLITE_OK ){
   48132               0 :       pWal->writeLock = 1;
   48133               0 :     }else if( rc==SQLITE_BUSY ){
   48134               0 :       eMode2 = SQLITE_CHECKPOINT_PASSIVE;
   48135               0 :       rc = SQLITE_OK;
   48136                 :     }
   48137                 :   }
   48138                 : 
   48139                 :   /* Read the wal-index header. */
   48140            5869 :   if( rc==SQLITE_OK ){
   48141            5869 :     rc = walIndexReadHdr(pWal, &isChanged);
   48142                 :   }
   48143                 : 
   48144                 :   /* Copy data from the log to the database file. */
   48145            5869 :   if( rc==SQLITE_OK ){
   48146            5869 :     if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
   48147               0 :       rc = SQLITE_CORRUPT_BKPT;
   48148                 :     }else{
   48149            5869 :       rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
   48150                 :     }
   48151                 : 
   48152                 :     /* If no error occurred, set the output variables. */
   48153            5869 :     if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
   48154            5869 :       if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
   48155            5869 :       if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
   48156                 :     }
   48157                 :   }
   48158                 : 
   48159            5869 :   if( isChanged ){
   48160                 :     /* If a new wal-index header was loaded before the checkpoint was 
   48161                 :     ** performed, then the pager-cache associated with pWal is now
   48162                 :     ** out of date. So zero the cached wal-index header to ensure that
   48163                 :     ** next time the pager opens a snapshot on this database it knows that
   48164                 :     ** the cache needs to be reset.
   48165                 :     */
   48166               0 :     memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
   48167                 :   }
   48168                 : 
   48169                 :   /* Release the locks. */
   48170            5869 :   sqlite3WalEndWriteTransaction(pWal);
   48171            5869 :   walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
   48172            5869 :   pWal->ckptLock = 0;
   48173                 :   WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
   48174            5869 :   return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
   48175                 : }
   48176                 : 
   48177                 : /* Return the value to pass to a sqlite3_wal_hook callback, the
   48178                 : ** number of frames in the WAL at the point of the last commit since
   48179                 : ** sqlite3WalCallback() was called.  If no commits have occurred since
   48180                 : ** the last call, then return 0.
   48181                 : */
   48182          270810 : SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
   48183          270810 :   u32 ret = 0;
   48184          270810 :   if( pWal ){
   48185          108021 :     ret = pWal->iCallback;
   48186          108021 :     pWal->iCallback = 0;
   48187                 :   }
   48188          270810 :   return (int)ret;
   48189                 : }
   48190                 : 
   48191                 : /*
   48192                 : ** This function is called to change the WAL subsystem into or out
   48193                 : ** of locking_mode=EXCLUSIVE.
   48194                 : **
   48195                 : ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
   48196                 : ** into locking_mode=NORMAL.  This means that we must acquire a lock
   48197                 : ** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
   48198                 : ** or if the acquisition of the lock fails, then return 0.  If the
   48199                 : ** transition out of exclusive-mode is successful, return 1.  This
   48200                 : ** operation must occur while the pager is still holding the exclusive
   48201                 : ** lock on the main database file.
   48202                 : **
   48203                 : ** If op is one, then change from locking_mode=NORMAL into 
   48204                 : ** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
   48205                 : ** be released.  Return 1 if the transition is made and 0 if the
   48206                 : ** WAL is already in exclusive-locking mode - meaning that this
   48207                 : ** routine is a no-op.  The pager must already hold the exclusive lock
   48208                 : ** on the main database file before invoking this operation.
   48209                 : **
   48210                 : ** If op is negative, then do a dry-run of the op==1 case but do
   48211                 : ** not actually change anything. The pager uses this to see if it
   48212                 : ** should acquire the database exclusive lock prior to invoking
   48213                 : ** the op==1 case.
   48214                 : */
   48215           26364 : SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
   48216                 :   int rc;
   48217           26364 :   assert( pWal->writeLock==0 );
   48218           26364 :   assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
   48219                 : 
   48220                 :   /* pWal->readLock is usually set, but might be -1 if there was a 
   48221                 :   ** prior error while attempting to acquire are read-lock. This cannot 
   48222                 :   ** happen if the connection is actually in exclusive mode (as no xShmLock
   48223                 :   ** locks are taken in this case). Nor should the pager attempt to
   48224                 :   ** upgrade to exclusive-mode following such an error.
   48225                 :   */
   48226           26364 :   assert( pWal->readLock>=0 || pWal->lockError );
   48227           26364 :   assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
   48228                 : 
   48229           26364 :   if( op==0 ){
   48230           26364 :     if( pWal->exclusiveMode ){
   48231               0 :       pWal->exclusiveMode = 0;
   48232               0 :       if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
   48233               0 :         pWal->exclusiveMode = 1;
   48234                 :       }
   48235               0 :       rc = pWal->exclusiveMode==0;
   48236                 :     }else{
   48237                 :       /* Already in locking_mode=NORMAL */
   48238           26364 :       rc = 0;
   48239                 :     }
   48240               0 :   }else if( op>0 ){
   48241               0 :     assert( pWal->exclusiveMode==0 );
   48242               0 :     assert( pWal->readLock>=0 );
   48243               0 :     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
   48244               0 :     pWal->exclusiveMode = 1;
   48245               0 :     rc = 1;
   48246                 :   }else{
   48247               0 :     rc = pWal->exclusiveMode==0;
   48248                 :   }
   48249           26364 :   return rc;
   48250                 : }
   48251                 : 
   48252                 : /* 
   48253                 : ** Return true if the argument is non-NULL and the WAL module is using
   48254                 : ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
   48255                 : ** WAL module is using shared-memory, return false. 
   48256                 : */
   48257            2120 : SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
   48258            2120 :   return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
   48259                 : }
   48260                 : 
   48261                 : #endif /* #ifndef SQLITE_OMIT_WAL */
   48262                 : 
   48263                 : /************** End of wal.c *************************************************/
   48264                 : /************** Begin file btmutex.c *****************************************/
   48265                 : /*
   48266                 : ** 2007 August 27
   48267                 : **
   48268                 : ** The author disclaims copyright to this source code.  In place of
   48269                 : ** a legal notice, here is a blessing:
   48270                 : **
   48271                 : **    May you do good and not evil.
   48272                 : **    May you find forgiveness for yourself and forgive others.
   48273                 : **    May you share freely, never taking more than you give.
   48274                 : **
   48275                 : *************************************************************************
   48276                 : **
   48277                 : ** This file contains code used to implement mutexes on Btree objects.
   48278                 : ** This code really belongs in btree.c.  But btree.c is getting too
   48279                 : ** big and we want to break it down some.  This packaged seemed like
   48280                 : ** a good breakout.
   48281                 : */
   48282                 : /************** Include btreeInt.h in the middle of btmutex.c ****************/
   48283                 : /************** Begin file btreeInt.h ****************************************/
   48284                 : /*
   48285                 : ** 2004 April 6
   48286                 : **
   48287                 : ** The author disclaims copyright to this source code.  In place of
   48288                 : ** a legal notice, here is a blessing:
   48289                 : **
   48290                 : **    May you do good and not evil.
   48291                 : **    May you find forgiveness for yourself and forgive others.
   48292                 : **    May you share freely, never taking more than you give.
   48293                 : **
   48294                 : *************************************************************************
   48295                 : ** This file implements a external (disk-based) database using BTrees.
   48296                 : ** For a detailed discussion of BTrees, refer to
   48297                 : **
   48298                 : **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
   48299                 : **     "Sorting And Searching", pages 473-480. Addison-Wesley
   48300                 : **     Publishing Company, Reading, Massachusetts.
   48301                 : **
   48302                 : ** The basic idea is that each page of the file contains N database
   48303                 : ** entries and N+1 pointers to subpages.
   48304                 : **
   48305                 : **   ----------------------------------------------------------------
   48306                 : **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
   48307                 : **   ----------------------------------------------------------------
   48308                 : **
   48309                 : ** All of the keys on the page that Ptr(0) points to have values less
   48310                 : ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
   48311                 : ** values greater than Key(0) and less than Key(1).  All of the keys
   48312                 : ** on Ptr(N) and its subpages have values greater than Key(N-1).  And
   48313                 : ** so forth.
   48314                 : **
   48315                 : ** Finding a particular key requires reading O(log(M)) pages from the 
   48316                 : ** disk where M is the number of entries in the tree.
   48317                 : **
   48318                 : ** In this implementation, a single file can hold one or more separate 
   48319                 : ** BTrees.  Each BTree is identified by the index of its root page.  The
   48320                 : ** key and data for any entry are combined to form the "payload".  A
   48321                 : ** fixed amount of payload can be carried directly on the database
   48322                 : ** page.  If the payload is larger than the preset amount then surplus
   48323                 : ** bytes are stored on overflow pages.  The payload for an entry
   48324                 : ** and the preceding pointer are combined to form a "Cell".  Each 
   48325                 : ** page has a small header which contains the Ptr(N) pointer and other
   48326                 : ** information such as the size of key and data.
   48327                 : **
   48328                 : ** FORMAT DETAILS
   48329                 : **
   48330                 : ** The file is divided into pages.  The first page is called page 1,
   48331                 : ** the second is page 2, and so forth.  A page number of zero indicates
   48332                 : ** "no such page".  The page size can be any power of 2 between 512 and 65536.
   48333                 : ** Each page can be either a btree page, a freelist page, an overflow
   48334                 : ** page, or a pointer-map page.
   48335                 : **
   48336                 : ** The first page is always a btree page.  The first 100 bytes of the first
   48337                 : ** page contain a special header (the "file header") that describes the file.
   48338                 : ** The format of the file header is as follows:
   48339                 : **
   48340                 : **   OFFSET   SIZE    DESCRIPTION
   48341                 : **      0      16     Header string: "SQLite format 3\000"
   48342                 : **     16       2     Page size in bytes.  
   48343                 : **     18       1     File format write version
   48344                 : **     19       1     File format read version
   48345                 : **     20       1     Bytes of unused space at the end of each page
   48346                 : **     21       1     Max embedded payload fraction
   48347                 : **     22       1     Min embedded payload fraction
   48348                 : **     23       1     Min leaf payload fraction
   48349                 : **     24       4     File change counter
   48350                 : **     28       4     Reserved for future use
   48351                 : **     32       4     First freelist page
   48352                 : **     36       4     Number of freelist pages in the file
   48353                 : **     40      60     15 4-byte meta values passed to higher layers
   48354                 : **
   48355                 : **     40       4     Schema cookie
   48356                 : **     44       4     File format of schema layer
   48357                 : **     48       4     Size of page cache
   48358                 : **     52       4     Largest root-page (auto/incr_vacuum)
   48359                 : **     56       4     1=UTF-8 2=UTF16le 3=UTF16be
   48360                 : **     60       4     User version
   48361                 : **     64       4     Incremental vacuum mode
   48362                 : **     68       4     unused
   48363                 : **     72       4     unused
   48364                 : **     76       4     unused
   48365                 : **
   48366                 : ** All of the integer values are big-endian (most significant byte first).
   48367                 : **
   48368                 : ** The file change counter is incremented when the database is changed
   48369                 : ** This counter allows other processes to know when the file has changed
   48370                 : ** and thus when they need to flush their cache.
   48371                 : **
   48372                 : ** The max embedded payload fraction is the amount of the total usable
   48373                 : ** space in a page that can be consumed by a single cell for standard
   48374                 : ** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
   48375                 : ** is to limit the maximum cell size so that at least 4 cells will fit
   48376                 : ** on one page.  Thus the default max embedded payload fraction is 64.
   48377                 : **
   48378                 : ** If the payload for a cell is larger than the max payload, then extra
   48379                 : ** payload is spilled to overflow pages.  Once an overflow page is allocated,
   48380                 : ** as many bytes as possible are moved into the overflow pages without letting
   48381                 : ** the cell size drop below the min embedded payload fraction.
   48382                 : **
   48383                 : ** The min leaf payload fraction is like the min embedded payload fraction
   48384                 : ** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
   48385                 : ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
   48386                 : ** not specified in the header.
   48387                 : **
   48388                 : ** Each btree pages is divided into three sections:  The header, the
   48389                 : ** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
   48390                 : ** file header that occurs before the page header.
   48391                 : **
   48392                 : **      |----------------|
   48393                 : **      | file header    |   100 bytes.  Page 1 only.
   48394                 : **      |----------------|
   48395                 : **      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
   48396                 : **      |----------------|
   48397                 : **      | cell pointer   |   |  2 bytes per cell.  Sorted order.
   48398                 : **      | array          |   |  Grows downward
   48399                 : **      |                |   v
   48400                 : **      |----------------|
   48401                 : **      | unallocated    |
   48402                 : **      | space          |
   48403                 : **      |----------------|   ^  Grows upwards
   48404                 : **      | cell content   |   |  Arbitrary order interspersed with freeblocks.
   48405                 : **      | area           |   |  and free space fragments.
   48406                 : **      |----------------|
   48407                 : **
   48408                 : ** The page headers looks like this:
   48409                 : **
   48410                 : **   OFFSET   SIZE     DESCRIPTION
   48411                 : **      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
   48412                 : **      1       2      byte offset to the first freeblock
   48413                 : **      3       2      number of cells on this page
   48414                 : **      5       2      first byte of the cell content area
   48415                 : **      7       1      number of fragmented free bytes
   48416                 : **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
   48417                 : **
   48418                 : ** The flags define the format of this btree page.  The leaf flag means that
   48419                 : ** this page has no children.  The zerodata flag means that this page carries
   48420                 : ** only keys and no data.  The intkey flag means that the key is a integer
   48421                 : ** which is stored in the key size entry of the cell header rather than in
   48422                 : ** the payload area.
   48423                 : **
   48424                 : ** The cell pointer array begins on the first byte after the page header.
   48425                 : ** The cell pointer array contains zero or more 2-byte numbers which are
   48426                 : ** offsets from the beginning of the page to the cell content in the cell
   48427                 : ** content area.  The cell pointers occur in sorted order.  The system strives
   48428                 : ** to keep free space after the last cell pointer so that new cells can
   48429                 : ** be easily added without having to defragment the page.
   48430                 : **
   48431                 : ** Cell content is stored at the very end of the page and grows toward the
   48432                 : ** beginning of the page.
   48433                 : **
   48434                 : ** Unused space within the cell content area is collected into a linked list of
   48435                 : ** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
   48436                 : ** to the first freeblock is given in the header.  Freeblocks occur in
   48437                 : ** increasing order.  Because a freeblock must be at least 4 bytes in size,
   48438                 : ** any group of 3 or fewer unused bytes in the cell content area cannot
   48439                 : ** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
   48440                 : ** a fragment.  The total number of bytes in all fragments is recorded.
   48441                 : ** in the page header at offset 7.
   48442                 : **
   48443                 : **    SIZE    DESCRIPTION
   48444                 : **      2     Byte offset of the next freeblock
   48445                 : **      2     Bytes in this freeblock
   48446                 : **
   48447                 : ** Cells are of variable length.  Cells are stored in the cell content area at
   48448                 : ** the end of the page.  Pointers to the cells are in the cell pointer array
   48449                 : ** that immediately follows the page header.  Cells is not necessarily
   48450                 : ** contiguous or in order, but cell pointers are contiguous and in order.
   48451                 : **
   48452                 : ** Cell content makes use of variable length integers.  A variable
   48453                 : ** length integer is 1 to 9 bytes where the lower 7 bits of each 
   48454                 : ** byte are used.  The integer consists of all bytes that have bit 8 set and
   48455                 : ** the first byte with bit 8 clear.  The most significant byte of the integer
   48456                 : ** appears first.  A variable-length integer may not be more than 9 bytes long.
   48457                 : ** As a special case, all 8 bytes of the 9th byte are used as data.  This
   48458                 : ** allows a 64-bit integer to be encoded in 9 bytes.
   48459                 : **
   48460                 : **    0x00                      becomes  0x00000000
   48461                 : **    0x7f                      becomes  0x0000007f
   48462                 : **    0x81 0x00                 becomes  0x00000080
   48463                 : **    0x82 0x00                 becomes  0x00000100
   48464                 : **    0x80 0x7f                 becomes  0x0000007f
   48465                 : **    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
   48466                 : **    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
   48467                 : **
   48468                 : ** Variable length integers are used for rowids and to hold the number of
   48469                 : ** bytes of key and data in a btree cell.
   48470                 : **
   48471                 : ** The content of a cell looks like this:
   48472                 : **
   48473                 : **    SIZE    DESCRIPTION
   48474                 : **      4     Page number of the left child. Omitted if leaf flag is set.
   48475                 : **     var    Number of bytes of data. Omitted if the zerodata flag is set.
   48476                 : **     var    Number of bytes of key. Or the key itself if intkey flag is set.
   48477                 : **      *     Payload
   48478                 : **      4     First page of the overflow chain.  Omitted if no overflow
   48479                 : **
   48480                 : ** Overflow pages form a linked list.  Each page except the last is completely
   48481                 : ** filled with data (pagesize - 4 bytes).  The last page can have as little
   48482                 : ** as 1 byte of data.
   48483                 : **
   48484                 : **    SIZE    DESCRIPTION
   48485                 : **      4     Page number of next overflow page
   48486                 : **      *     Data
   48487                 : **
   48488                 : ** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
   48489                 : ** file header points to the first in a linked list of trunk page.  Each trunk
   48490                 : ** page points to multiple leaf pages.  The content of a leaf page is
   48491                 : ** unspecified.  A trunk page looks like this:
   48492                 : **
   48493                 : **    SIZE    DESCRIPTION
   48494                 : **      4     Page number of next trunk page
   48495                 : **      4     Number of leaf pointers on this page
   48496                 : **      *     zero or more pages numbers of leaves
   48497                 : */
   48498                 : 
   48499                 : 
   48500                 : /* The following value is the maximum cell size assuming a maximum page
   48501                 : ** size give above.
   48502                 : */
   48503                 : #define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
   48504                 : 
   48505                 : /* The maximum number of cells on a single page of the database.  This
   48506                 : ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
   48507                 : ** plus 2 bytes for the index to the cell in the page header).  Such
   48508                 : ** small cells will be rare, but they are possible.
   48509                 : */
   48510                 : #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
   48511                 : 
   48512                 : /* Forward declarations */
   48513                 : typedef struct MemPage MemPage;
   48514                 : typedef struct BtLock BtLock;
   48515                 : 
   48516                 : /*
   48517                 : ** This is a magic string that appears at the beginning of every
   48518                 : ** SQLite database in order to identify the file as a real database.
   48519                 : **
   48520                 : ** You can change this value at compile-time by specifying a
   48521                 : ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
   48522                 : ** header must be exactly 16 bytes including the zero-terminator so
   48523                 : ** the string itself should be 15 characters long.  If you change
   48524                 : ** the header, then your custom library will not be able to read 
   48525                 : ** databases generated by the standard tools and the standard tools
   48526                 : ** will not be able to read databases created by your custom library.
   48527                 : */
   48528                 : #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
   48529                 : #  define SQLITE_FILE_HEADER "SQLite format 3"
   48530                 : #endif
   48531                 : 
   48532                 : /*
   48533                 : ** Page type flags.  An ORed combination of these flags appear as the
   48534                 : ** first byte of on-disk image of every BTree page.
   48535                 : */
   48536                 : #define PTF_INTKEY    0x01
   48537                 : #define PTF_ZERODATA  0x02
   48538                 : #define PTF_LEAFDATA  0x04
   48539                 : #define PTF_LEAF      0x08
   48540                 : 
   48541                 : /*
   48542                 : ** As each page of the file is loaded into memory, an instance of the following
   48543                 : ** structure is appended and initialized to zero.  This structure stores
   48544                 : ** information about the page that is decoded from the raw file page.
   48545                 : **
   48546                 : ** The pParent field points back to the parent page.  This allows us to
   48547                 : ** walk up the BTree from any leaf to the root.  Care must be taken to
   48548                 : ** unref() the parent page pointer when this page is no longer referenced.
   48549                 : ** The pageDestructor() routine handles that chore.
   48550                 : **
   48551                 : ** Access to all fields of this structure is controlled by the mutex
   48552                 : ** stored in MemPage.pBt->mutex.
   48553                 : */
   48554                 : struct MemPage {
   48555                 :   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
   48556                 :   u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
   48557                 :   u8 intKey;           /* True if intkey flag is set */
   48558                 :   u8 leaf;             /* True if leaf flag is set */
   48559                 :   u8 hasData;          /* True if this page stores data */
   48560                 :   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
   48561                 :   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
   48562                 :   u8 max1bytePayload;  /* min(maxLocal,127) */
   48563                 :   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
   48564                 :   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
   48565                 :   u16 cellOffset;      /* Index in aData of first cell pointer */
   48566                 :   u16 nFree;           /* Number of free bytes on the page */
   48567                 :   u16 nCell;           /* Number of cells on this page, local and ovfl */
   48568                 :   u16 maskPage;        /* Mask for page offset */
   48569                 :   struct _OvflCell {   /* Cells that will not fit on aData[] */
   48570                 :     u8 *pCell;          /* Pointers to the body of the overflow cell */
   48571                 :     u16 idx;            /* Insert this cell before idx-th non-overflow cell */
   48572                 :   } aOvfl[5];
   48573                 :   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
   48574                 :   u8 *aData;           /* Pointer to disk image of the page data */
   48575                 :   u8 *aDataEnd;        /* One byte past the end of usable data */
   48576                 :   u8 *aCellIdx;        /* The cell index area */
   48577                 :   DbPage *pDbPage;     /* Pager page handle */
   48578                 :   Pgno pgno;           /* Page number for this page */
   48579                 : };
   48580                 : 
   48581                 : /*
   48582                 : ** The in-memory image of a disk page has the auxiliary information appended
   48583                 : ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
   48584                 : ** that extra information.
   48585                 : */
   48586                 : #define EXTRA_SIZE sizeof(MemPage)
   48587                 : 
   48588                 : /*
   48589                 : ** A linked list of the following structures is stored at BtShared.pLock.
   48590                 : ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
   48591                 : ** is opened on the table with root page BtShared.iTable. Locks are removed
   48592                 : ** from this list when a transaction is committed or rolled back, or when
   48593                 : ** a btree handle is closed.
   48594                 : */
   48595                 : struct BtLock {
   48596                 :   Btree *pBtree;        /* Btree handle holding this lock */
   48597                 :   Pgno iTable;          /* Root page of table */
   48598                 :   u8 eLock;             /* READ_LOCK or WRITE_LOCK */
   48599                 :   BtLock *pNext;        /* Next in BtShared.pLock list */
   48600                 : };
   48601                 : 
   48602                 : /* Candidate values for BtLock.eLock */
   48603                 : #define READ_LOCK     1
   48604                 : #define WRITE_LOCK    2
   48605                 : 
   48606                 : /* A Btree handle
   48607                 : **
   48608                 : ** A database connection contains a pointer to an instance of
   48609                 : ** this object for every database file that it has open.  This structure
   48610                 : ** is opaque to the database connection.  The database connection cannot
   48611                 : ** see the internals of this structure and only deals with pointers to
   48612                 : ** this structure.
   48613                 : **
   48614                 : ** For some database files, the same underlying database cache might be 
   48615                 : ** shared between multiple connections.  In that case, each connection
   48616                 : ** has it own instance of this object.  But each instance of this object
   48617                 : ** points to the same BtShared object.  The database cache and the
   48618                 : ** schema associated with the database file are all contained within
   48619                 : ** the BtShared object.
   48620                 : **
   48621                 : ** All fields in this structure are accessed under sqlite3.mutex.
   48622                 : ** The pBt pointer itself may not be changed while there exists cursors 
   48623                 : ** in the referenced BtShared that point back to this Btree since those
   48624                 : ** cursors have to go through this Btree to find their BtShared and
   48625                 : ** they often do so without holding sqlite3.mutex.
   48626                 : */
   48627                 : struct Btree {
   48628                 :   sqlite3 *db;       /* The database connection holding this btree */
   48629                 :   BtShared *pBt;     /* Sharable content of this btree */
   48630                 :   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
   48631                 :   u8 sharable;       /* True if we can share pBt with another db */
   48632                 :   u8 locked;         /* True if db currently has pBt locked */
   48633                 :   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
   48634                 :   int nBackup;       /* Number of backup operations reading this btree */
   48635                 :   Btree *pNext;      /* List of other sharable Btrees from the same db */
   48636                 :   Btree *pPrev;      /* Back pointer of the same list */
   48637                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   48638                 :   BtLock lock;       /* Object used to lock page 1 */
   48639                 : #endif
   48640                 : };
   48641                 : 
   48642                 : /*
   48643                 : ** Btree.inTrans may take one of the following values.
   48644                 : **
   48645                 : ** If the shared-data extension is enabled, there may be multiple users
   48646                 : ** of the Btree structure. At most one of these may open a write transaction,
   48647                 : ** but any number may have active read transactions.
   48648                 : */
   48649                 : #define TRANS_NONE  0
   48650                 : #define TRANS_READ  1
   48651                 : #define TRANS_WRITE 2
   48652                 : 
   48653                 : /*
   48654                 : ** An instance of this object represents a single database file.
   48655                 : ** 
   48656                 : ** A single database file can be in use at the same time by two
   48657                 : ** or more database connections.  When two or more connections are
   48658                 : ** sharing the same database file, each connection has it own
   48659                 : ** private Btree object for the file and each of those Btrees points
   48660                 : ** to this one BtShared object.  BtShared.nRef is the number of
   48661                 : ** connections currently sharing this database file.
   48662                 : **
   48663                 : ** Fields in this structure are accessed under the BtShared.mutex
   48664                 : ** mutex, except for nRef and pNext which are accessed under the
   48665                 : ** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
   48666                 : ** may not be modified once it is initially set as long as nRef>0.
   48667                 : ** The pSchema field may be set once under BtShared.mutex and
   48668                 : ** thereafter is unchanged as long as nRef>0.
   48669                 : **
   48670                 : ** isPending:
   48671                 : **
   48672                 : **   If a BtShared client fails to obtain a write-lock on a database
   48673                 : **   table (because there exists one or more read-locks on the table),
   48674                 : **   the shared-cache enters 'pending-lock' state and isPending is
   48675                 : **   set to true.
   48676                 : **
   48677                 : **   The shared-cache leaves the 'pending lock' state when either of
   48678                 : **   the following occur:
   48679                 : **
   48680                 : **     1) The current writer (BtShared.pWriter) concludes its transaction, OR
   48681                 : **     2) The number of locks held by other connections drops to zero.
   48682                 : **
   48683                 : **   while in the 'pending-lock' state, no connection may start a new
   48684                 : **   transaction.
   48685                 : **
   48686                 : **   This feature is included to help prevent writer-starvation.
   48687                 : */
   48688                 : struct BtShared {
   48689                 :   Pager *pPager;        /* The page cache */
   48690                 :   sqlite3 *db;          /* Database connection currently using this Btree */
   48691                 :   BtCursor *pCursor;    /* A list of all open cursors */
   48692                 :   MemPage *pPage1;      /* First page of the database */
   48693                 :   u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
   48694                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   48695                 :   u8 autoVacuum;        /* True if auto-vacuum is enabled */
   48696                 :   u8 incrVacuum;        /* True if incr-vacuum is enabled */
   48697                 : #endif
   48698                 :   u8 inTransaction;     /* Transaction state */
   48699                 :   u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
   48700                 :   u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
   48701                 :   u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
   48702                 :   u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
   48703                 :   u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
   48704                 :   u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
   48705                 :   u32 pageSize;         /* Total number of bytes on a page */
   48706                 :   u32 usableSize;       /* Number of usable bytes on each page */
   48707                 :   int nTransaction;     /* Number of open transactions (read + write) */
   48708                 :   u32 nPage;            /* Number of pages in the database */
   48709                 :   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
   48710                 :   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
   48711                 :   sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
   48712                 :   Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
   48713                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   48714                 :   int nRef;             /* Number of references to this structure */
   48715                 :   BtShared *pNext;      /* Next on a list of sharable BtShared structs */
   48716                 :   BtLock *pLock;        /* List of locks held on this shared-btree struct */
   48717                 :   Btree *pWriter;       /* Btree with currently open write transaction */
   48718                 : #endif
   48719                 :   u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
   48720                 : };
   48721                 : 
   48722                 : /*
   48723                 : ** Allowed values for BtShared.btsFlags
   48724                 : */
   48725                 : #define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
   48726                 : #define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
   48727                 : #define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
   48728                 : #define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
   48729                 : #define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
   48730                 : #define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
   48731                 : #define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
   48732                 : 
   48733                 : /*
   48734                 : ** An instance of the following structure is used to hold information
   48735                 : ** about a cell.  The parseCellPtr() function fills in this structure
   48736                 : ** based on information extract from the raw disk page.
   48737                 : */
   48738                 : typedef struct CellInfo CellInfo;
   48739                 : struct CellInfo {
   48740                 :   i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
   48741                 :   u8 *pCell;     /* Pointer to the start of cell content */
   48742                 :   u32 nData;     /* Number of bytes of data */
   48743                 :   u32 nPayload;  /* Total amount of payload */
   48744                 :   u16 nHeader;   /* Size of the cell content header in bytes */
   48745                 :   u16 nLocal;    /* Amount of payload held locally */
   48746                 :   u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
   48747                 :   u16 nSize;     /* Size of the cell content on the main b-tree page */
   48748                 : };
   48749                 : 
   48750                 : /*
   48751                 : ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
   48752                 : ** this will be declared corrupt. This value is calculated based on a
   48753                 : ** maximum database size of 2^31 pages a minimum fanout of 2 for a
   48754                 : ** root-node and 3 for all other internal nodes.
   48755                 : **
   48756                 : ** If a tree that appears to be taller than this is encountered, it is
   48757                 : ** assumed that the database is corrupt.
   48758                 : */
   48759                 : #define BTCURSOR_MAX_DEPTH 20
   48760                 : 
   48761                 : /*
   48762                 : ** A cursor is a pointer to a particular entry within a particular
   48763                 : ** b-tree within a database file.
   48764                 : **
   48765                 : ** The entry is identified by its MemPage and the index in
   48766                 : ** MemPage.aCell[] of the entry.
   48767                 : **
   48768                 : ** A single database file can be shared by two more database connections,
   48769                 : ** but cursors cannot be shared.  Each cursor is associated with a
   48770                 : ** particular database connection identified BtCursor.pBtree.db.
   48771                 : **
   48772                 : ** Fields in this structure are accessed under the BtShared.mutex
   48773                 : ** found at self->pBt->mutex. 
   48774                 : */
   48775                 : struct BtCursor {
   48776                 :   Btree *pBtree;            /* The Btree to which this cursor belongs */
   48777                 :   BtShared *pBt;            /* The BtShared this cursor points to */
   48778                 :   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
   48779                 :   struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
   48780                 :   Pgno pgnoRoot;            /* The root page of this tree */
   48781                 :   sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
   48782                 :   CellInfo info;            /* A parse of the cell we are pointing at */
   48783                 :   i64 nKey;        /* Size of pKey, or last integer key */
   48784                 :   void *pKey;      /* Saved key that was cursor's last known position */
   48785                 :   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
   48786                 :   u8 wrFlag;                /* True if writable */
   48787                 :   u8 atLast;                /* Cursor pointing to the last entry */
   48788                 :   u8 validNKey;             /* True if info.nKey is valid */
   48789                 :   u8 eState;                /* One of the CURSOR_XXX constants (see below) */
   48790                 : #ifndef SQLITE_OMIT_INCRBLOB
   48791                 :   Pgno *aOverflow;          /* Cache of overflow page locations */
   48792                 :   u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
   48793                 : #endif
   48794                 :   i16 iPage;                            /* Index of current page in apPage */
   48795                 :   u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
   48796                 :   MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
   48797                 : };
   48798                 : 
   48799                 : /*
   48800                 : ** Potential values for BtCursor.eState.
   48801                 : **
   48802                 : ** CURSOR_VALID:
   48803                 : **   Cursor points to a valid entry. getPayload() etc. may be called.
   48804                 : **
   48805                 : ** CURSOR_INVALID:
   48806                 : **   Cursor does not point to a valid entry. This can happen (for example) 
   48807                 : **   because the table is empty or because BtreeCursorFirst() has not been
   48808                 : **   called.
   48809                 : **
   48810                 : ** CURSOR_REQUIRESEEK:
   48811                 : **   The table that this cursor was opened on still exists, but has been 
   48812                 : **   modified since the cursor was last used. The cursor position is saved
   48813                 : **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
   48814                 : **   this state, restoreCursorPosition() can be called to attempt to
   48815                 : **   seek the cursor to the saved position.
   48816                 : **
   48817                 : ** CURSOR_FAULT:
   48818                 : **   A unrecoverable error (an I/O error or a malloc failure) has occurred
   48819                 : **   on a different connection that shares the BtShared cache with this
   48820                 : **   cursor.  The error has left the cache in an inconsistent state.
   48821                 : **   Do nothing else with this cursor.  Any attempt to use the cursor
   48822                 : **   should return the error code stored in BtCursor.skip
   48823                 : */
   48824                 : #define CURSOR_INVALID           0
   48825                 : #define CURSOR_VALID             1
   48826                 : #define CURSOR_REQUIRESEEK       2
   48827                 : #define CURSOR_FAULT             3
   48828                 : 
   48829                 : /* 
   48830                 : ** The database page the PENDING_BYTE occupies. This page is never used.
   48831                 : */
   48832                 : # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
   48833                 : 
   48834                 : /*
   48835                 : ** These macros define the location of the pointer-map entry for a 
   48836                 : ** database page. The first argument to each is the number of usable
   48837                 : ** bytes on each page of the database (often 1024). The second is the
   48838                 : ** page number to look up in the pointer map.
   48839                 : **
   48840                 : ** PTRMAP_PAGENO returns the database page number of the pointer-map
   48841                 : ** page that stores the required pointer. PTRMAP_PTROFFSET returns
   48842                 : ** the offset of the requested map entry.
   48843                 : **
   48844                 : ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
   48845                 : ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
   48846                 : ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
   48847                 : ** this test.
   48848                 : */
   48849                 : #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
   48850                 : #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
   48851                 : #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
   48852                 : 
   48853                 : /*
   48854                 : ** The pointer map is a lookup table that identifies the parent page for
   48855                 : ** each child page in the database file.  The parent page is the page that
   48856                 : ** contains a pointer to the child.  Every page in the database contains
   48857                 : ** 0 or 1 parent pages.  (In this context 'database page' refers
   48858                 : ** to any page that is not part of the pointer map itself.)  Each pointer map
   48859                 : ** entry consists of a single byte 'type' and a 4 byte parent page number.
   48860                 : ** The PTRMAP_XXX identifiers below are the valid types.
   48861                 : **
   48862                 : ** The purpose of the pointer map is to facility moving pages from one
   48863                 : ** position in the file to another as part of autovacuum.  When a page
   48864                 : ** is moved, the pointer in its parent must be updated to point to the
   48865                 : ** new location.  The pointer map is used to locate the parent page quickly.
   48866                 : **
   48867                 : ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
   48868                 : **                  used in this case.
   48869                 : **
   48870                 : ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
   48871                 : **                  is not used in this case.
   48872                 : **
   48873                 : ** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
   48874                 : **                   overflow pages. The page number identifies the page that
   48875                 : **                   contains the cell with a pointer to this overflow page.
   48876                 : **
   48877                 : ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
   48878                 : **                   overflow pages. The page-number identifies the previous
   48879                 : **                   page in the overflow page list.
   48880                 : **
   48881                 : ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
   48882                 : **               identifies the parent page in the btree.
   48883                 : */
   48884                 : #define PTRMAP_ROOTPAGE 1
   48885                 : #define PTRMAP_FREEPAGE 2
   48886                 : #define PTRMAP_OVERFLOW1 3
   48887                 : #define PTRMAP_OVERFLOW2 4
   48888                 : #define PTRMAP_BTREE 5
   48889                 : 
   48890                 : /* A bunch of assert() statements to check the transaction state variables
   48891                 : ** of handle p (type Btree*) are internally consistent.
   48892                 : */
   48893                 : #define btreeIntegrity(p) \
   48894                 :   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
   48895                 :   assert( p->pBt->inTransaction>=p->inTrans ); 
   48896                 : 
   48897                 : 
   48898                 : /*
   48899                 : ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
   48900                 : ** if the database supports auto-vacuum or not. Because it is used
   48901                 : ** within an expression that is an argument to another macro 
   48902                 : ** (sqliteMallocRaw), it is not possible to use conditional compilation.
   48903                 : ** So, this macro is defined instead.
   48904                 : */
   48905                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   48906                 : #define ISAUTOVACUUM (pBt->autoVacuum)
   48907                 : #else
   48908                 : #define ISAUTOVACUUM 0
   48909                 : #endif
   48910                 : 
   48911                 : 
   48912                 : /*
   48913                 : ** This structure is passed around through all the sanity checking routines
   48914                 : ** in order to keep track of some global state information.
   48915                 : */
   48916                 : typedef struct IntegrityCk IntegrityCk;
   48917                 : struct IntegrityCk {
   48918                 :   BtShared *pBt;    /* The tree being checked out */
   48919                 :   Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
   48920                 :   Pgno nPage;       /* Number of pages in the database */
   48921                 :   int *anRef;       /* Number of times each page is referenced */
   48922                 :   int mxErr;        /* Stop accumulating errors when this reaches zero */
   48923                 :   int nErr;         /* Number of messages written to zErrMsg so far */
   48924                 :   int mallocFailed; /* A memory allocation error has occurred */
   48925                 :   StrAccum errMsg;  /* Accumulate the error message text here */
   48926                 : };
   48927                 : 
   48928                 : /*
   48929                 : ** Routines to read or write a two- and four-byte big-endian integer values.
   48930                 : */
   48931                 : #define get2byte(x)   ((x)[0]<<8 | (x)[1])
   48932                 : #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
   48933                 : #define get4byte sqlite3Get4byte
   48934                 : #define put4byte sqlite3Put4byte
   48935                 : 
   48936                 : /************** End of btreeInt.h ********************************************/
   48937                 : /************** Continuing where we left off in btmutex.c ********************/
   48938                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   48939                 : #if SQLITE_THREADSAFE
   48940                 : 
   48941                 : /*
   48942                 : ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
   48943                 : ** set BtShared.db to the database handle associated with p and the
   48944                 : ** p->locked boolean to true.
   48945                 : */
   48946            1220 : static void lockBtreeMutex(Btree *p){
   48947            1220 :   assert( p->locked==0 );
   48948            1220 :   assert( sqlite3_mutex_notheld(p->pBt->mutex) );
   48949            1220 :   assert( sqlite3_mutex_held(p->db->mutex) );
   48950                 : 
   48951            1220 :   sqlite3_mutex_enter(p->pBt->mutex);
   48952            1220 :   p->pBt->db = p->db;
   48953            1220 :   p->locked = 1;
   48954            1220 : }
   48955                 : 
   48956                 : /*
   48957                 : ** Release the BtShared mutex associated with B-Tree handle p and
   48958                 : ** clear the p->locked boolean.
   48959                 : */
   48960          396479 : static void unlockBtreeMutex(Btree *p){
   48961          396479 :   BtShared *pBt = p->pBt;
   48962          396479 :   assert( p->locked==1 );
   48963          396479 :   assert( sqlite3_mutex_held(pBt->mutex) );
   48964          396479 :   assert( sqlite3_mutex_held(p->db->mutex) );
   48965          396479 :   assert( p->db==pBt->db );
   48966                 : 
   48967          396479 :   sqlite3_mutex_leave(pBt->mutex);
   48968          396479 :   p->locked = 0;
   48969          396479 : }
   48970                 : 
   48971                 : /*
   48972                 : ** Enter a mutex on the given BTree object.
   48973                 : **
   48974                 : ** If the object is not sharable, then no mutex is ever required
   48975                 : ** and this routine is a no-op.  The underlying mutex is non-recursive.
   48976                 : ** But we keep a reference count in Btree.wantToLock so the behavior
   48977                 : ** of this interface is recursive.
   48978                 : **
   48979                 : ** To avoid deadlocks, multiple Btrees are locked in the same order
   48980                 : ** by all database connections.  The p->pNext is a list of other
   48981                 : ** Btrees belonging to the same database connection as the p Btree
   48982                 : ** which need to be locked after p.  If we cannot get a lock on
   48983                 : ** p, then first unlock all of the others on p->pNext, then wait
   48984                 : ** for the lock to become available on p, then relock all of the
   48985                 : ** subsequent Btrees that desire a lock.
   48986                 : */
   48987         2944393 : SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
   48988                 :   Btree *pLater;
   48989                 : 
   48990                 :   /* Some basic sanity checking on the Btree.  The list of Btrees
   48991                 :   ** connected by pNext and pPrev should be in sorted order by
   48992                 :   ** Btree.pBt value. All elements of the list should belong to
   48993                 :   ** the same connection. Only shared Btrees are on the list. */
   48994         2944393 :   assert( p->pNext==0 || p->pNext->pBt>p->pBt );
   48995         2944393 :   assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
   48996         2944393 :   assert( p->pNext==0 || p->pNext->db==p->db );
   48997         2944393 :   assert( p->pPrev==0 || p->pPrev->db==p->db );
   48998         2944393 :   assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
   48999                 : 
   49000                 :   /* Check for locking consistency */
   49001         2944391 :   assert( !p->locked || p->wantToLock>0 );
   49002         2944391 :   assert( p->sharable || p->wantToLock==0 );
   49003                 : 
   49004                 :   /* We should already hold a lock on the database connection */
   49005         2944391 :   assert( sqlite3_mutex_held(p->db->mutex) );
   49006                 : 
   49007                 :   /* Unless the database is sharable and unlocked, then BtShared.db
   49008                 :   ** should already be set correctly. */
   49009         2944389 :   assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
   49010                 : 
   49011         2944389 :   if( !p->sharable ) return;
   49012         2675740 :   p->wantToLock++;
   49013         2675740 :   if( p->locked ) return;
   49014                 : 
   49015                 :   /* In most cases, we should be able to acquire the lock we
   49016                 :   ** want without having to go throught the ascending lock
   49017                 :   ** procedure that follows.  Just be sure not to block.
   49018                 :   */
   49019          396477 :   if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
   49020          395259 :     p->pBt->db = p->db;
   49021          395259 :     p->locked = 1;
   49022          395259 :     return;
   49023                 :   }
   49024                 : 
   49025                 :   /* To avoid deadlock, first release all locks with a larger
   49026                 :   ** BtShared address.  Then acquire our lock.  Then reacquire
   49027                 :   ** the other BtShared locks that we used to hold in ascending
   49028                 :   ** order.
   49029                 :   */
   49030            1220 :   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
   49031               0 :     assert( pLater->sharable );
   49032               0 :     assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
   49033               0 :     assert( !pLater->locked || pLater->wantToLock>0 );
   49034               0 :     if( pLater->locked ){
   49035               0 :       unlockBtreeMutex(pLater);
   49036                 :     }
   49037                 :   }
   49038            1220 :   lockBtreeMutex(p);
   49039            1220 :   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
   49040               0 :     if( pLater->wantToLock ){
   49041               0 :       lockBtreeMutex(pLater);
   49042                 :     }
   49043                 :   }
   49044                 : }
   49045                 : 
   49046                 : /*
   49047                 : ** Exit the recursive mutex on a Btree.
   49048                 : */
   49049         2944774 : SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
   49050         2944774 :   if( p->sharable ){
   49051         2675742 :     assert( p->wantToLock>0 );
   49052         2675742 :     p->wantToLock--;
   49053         2675742 :     if( p->wantToLock==0 ){
   49054          396477 :       unlockBtreeMutex(p);
   49055                 :     }
   49056                 :   }
   49057         2944776 : }
   49058                 : 
   49059                 : #ifndef NDEBUG
   49060                 : /*
   49061                 : ** Return true if the BtShared mutex is held on the btree, or if the
   49062                 : ** B-Tree is not marked as sharable.
   49063                 : **
   49064                 : ** This routine is used only from within assert() statements.
   49065                 : */
   49066         2329428 : SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
   49067         2329428 :   assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
   49068         2329428 :   assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
   49069         2329428 :   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
   49070         2329428 :   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
   49071                 : 
   49072         2329428 :   return (p->sharable==0 || p->locked);
   49073                 : }
   49074                 : #endif
   49075                 : 
   49076                 : 
   49077                 : #ifndef SQLITE_OMIT_INCRBLOB
   49078                 : /*
   49079                 : ** Enter and leave a mutex on a Btree given a cursor owned by that
   49080                 : ** Btree.  These entry points are used by incremental I/O and can be
   49081                 : ** omitted if that module is not used.
   49082                 : */
   49083               0 : SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
   49084               0 :   sqlite3BtreeEnter(pCur->pBtree);
   49085               0 : }
   49086               0 : SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
   49087               0 :   sqlite3BtreeLeave(pCur->pBtree);
   49088               0 : }
   49089                 : #endif /* SQLITE_OMIT_INCRBLOB */
   49090                 : 
   49091                 : 
   49092                 : /*
   49093                 : ** Enter the mutex on every Btree associated with a database
   49094                 : ** connection.  This is needed (for example) prior to parsing
   49095                 : ** a statement since we will be comparing table and column names
   49096                 : ** against all schemas and we do not want those schemas being
   49097                 : ** reset out from under us.
   49098                 : **
   49099                 : ** There is a corresponding leave-all procedures.
   49100                 : **
   49101                 : ** Enter the mutexes in accending order by BtShared pointer address
   49102                 : ** to avoid the possibility of deadlock when two threads with
   49103                 : ** two or more btrees in common both try to lock all their btrees
   49104                 : ** at the same instant.
   49105                 : */
   49106          179825 : SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
   49107                 :   int i;
   49108                 :   Btree *p;
   49109          179825 :   assert( sqlite3_mutex_held(db->mutex) );
   49110          539695 :   for(i=0; i<db->nDb; i++){
   49111          359870 :     p = db->aDb[i].pBt;
   49112          359870 :     if( p ) sqlite3BtreeEnter(p);
   49113                 :   }
   49114          179825 : }
   49115          179825 : SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
   49116                 :   int i;
   49117                 :   Btree *p;
   49118          179825 :   assert( sqlite3_mutex_held(db->mutex) );
   49119          539695 :   for(i=0; i<db->nDb; i++){
   49120          359870 :     p = db->aDb[i].pBt;
   49121          359870 :     if( p ) sqlite3BtreeLeave(p);
   49122                 :   }
   49123          179825 : }
   49124                 : 
   49125                 : /*
   49126                 : ** Return true if a particular Btree requires a lock.  Return FALSE if
   49127                 : ** no lock is ever required since it is not sharable.
   49128                 : */
   49129          104945 : SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
   49130          104945 :   return p->sharable;
   49131                 : }
   49132                 : 
   49133                 : #ifndef NDEBUG
   49134                 : /*
   49135                 : ** Return true if the current thread holds the database connection
   49136                 : ** mutex and all required BtShared mutexes.
   49137                 : **
   49138                 : ** This routine is used inside assert() statements only.
   49139                 : */
   49140          279663 : SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
   49141                 :   int i;
   49142          279663 :   if( !sqlite3_mutex_held(db->mutex) ){
   49143               0 :     return 0;
   49144                 :   }
   49145          839265 :   for(i=0; i<db->nDb; i++){
   49146                 :     Btree *p;
   49147          559602 :     p = db->aDb[i].pBt;
   49148          835224 :     if( p && p->sharable &&
   49149          551244 :          (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
   49150               0 :       return 0;
   49151                 :     }
   49152                 :   }
   49153          279663 :   return 1;
   49154                 : }
   49155                 : #endif /* NDEBUG */
   49156                 : 
   49157                 : #ifndef NDEBUG
   49158                 : /*
   49159                 : ** Return true if the correct mutexes are held for accessing the
   49160                 : ** db->aDb[iDb].pSchema structure.  The mutexes required for schema
   49161                 : ** access are:
   49162                 : **
   49163                 : **   (1) The mutex on db
   49164                 : **   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
   49165                 : **
   49166                 : ** If pSchema is not NULL, then iDb is computed from pSchema and
   49167                 : ** db using sqlite3SchemaToIndex().
   49168                 : */
   49169         1215982 : SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
   49170                 :   Btree *p;
   49171         1215982 :   assert( db!=0 );
   49172         1215982 :   if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
   49173         1215982 :   assert( iDb>=0 && iDb<db->nDb );
   49174         1215982 :   if( !sqlite3_mutex_held(db->mutex) ) return 0;
   49175         1215981 :   if( iDb==1 ) return 1;
   49176         1028027 :   p = db->aDb[iDb].pBt;
   49177         1028027 :   assert( p!=0 );
   49178         1028027 :   return p->sharable==0 || p->locked==1;
   49179                 : }
   49180                 : #endif /* NDEBUG */
   49181                 : 
   49182                 : #else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
   49183                 : /*
   49184                 : ** The following are special cases for mutex enter routines for use
   49185                 : ** in single threaded applications that use shared cache.  Except for
   49186                 : ** these two routines, all mutex operations are no-ops in that case and
   49187                 : ** are null #defines in btree.h.
   49188                 : **
   49189                 : ** If shared cache is disabled, then all btree mutex routines, including
   49190                 : ** the ones below, are no-ops and are null #defines in btree.h.
   49191                 : */
   49192                 : 
   49193                 : SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
   49194                 :   p->pBt->db = p->db;
   49195                 : }
   49196                 : SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
   49197                 :   int i;
   49198                 :   for(i=0; i<db->nDb; i++){
   49199                 :     Btree *p = db->aDb[i].pBt;
   49200                 :     if( p ){
   49201                 :       p->pBt->db = p->db;
   49202                 :     }
   49203                 :   }
   49204                 : }
   49205                 : #endif /* if SQLITE_THREADSAFE */
   49206                 : #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
   49207                 : 
   49208                 : /************** End of btmutex.c *********************************************/
   49209                 : /************** Begin file btree.c *******************************************/
   49210                 : /*
   49211                 : ** 2004 April 6
   49212                 : **
   49213                 : ** The author disclaims copyright to this source code.  In place of
   49214                 : ** a legal notice, here is a blessing:
   49215                 : **
   49216                 : **    May you do good and not evil.
   49217                 : **    May you find forgiveness for yourself and forgive others.
   49218                 : **    May you share freely, never taking more than you give.
   49219                 : **
   49220                 : *************************************************************************
   49221                 : ** This file implements a external (disk-based) database using BTrees.
   49222                 : ** See the header comment on "btreeInt.h" for additional information.
   49223                 : ** Including a description of file format and an overview of operation.
   49224                 : */
   49225                 : 
   49226                 : /*
   49227                 : ** The header string that appears at the beginning of every
   49228                 : ** SQLite database.
   49229                 : */
   49230                 : static const char zMagicHeader[] = SQLITE_FILE_HEADER;
   49231                 : 
   49232                 : /*
   49233                 : ** Set this global variable to 1 to enable tracing using the TRACE
   49234                 : ** macro.
   49235                 : */
   49236                 : #if 0
   49237                 : int sqlite3BtreeTrace=1;  /* True to enable tracing */
   49238                 : # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
   49239                 : #else
   49240                 : # define TRACE(X)
   49241                 : #endif
   49242                 : 
   49243                 : /*
   49244                 : ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
   49245                 : ** But if the value is zero, make it 65536.
   49246                 : **
   49247                 : ** This routine is used to extract the "offset to cell content area" value
   49248                 : ** from the header of a btree page.  If the page size is 65536 and the page
   49249                 : ** is empty, the offset should be 65536, but the 2-byte value stores zero.
   49250                 : ** This routine makes the necessary adjustment to 65536.
   49251                 : */
   49252                 : #define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
   49253                 : 
   49254                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   49255                 : /*
   49256                 : ** A list of BtShared objects that are eligible for participation
   49257                 : ** in shared cache.  This variable has file scope during normal builds,
   49258                 : ** but the test harness needs to access it so we make it global for 
   49259                 : ** test builds.
   49260                 : **
   49261                 : ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
   49262                 : */
   49263                 : #ifdef SQLITE_TEST
   49264                 : SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
   49265                 : #else
   49266                 : static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
   49267                 : #endif
   49268                 : #endif /* SQLITE_OMIT_SHARED_CACHE */
   49269                 : 
   49270                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   49271                 : /*
   49272                 : ** Enable or disable the shared pager and schema features.
   49273                 : **
   49274                 : ** This routine has no effect on existing database connections.
   49275                 : ** The shared cache setting effects only future calls to
   49276                 : ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
   49277                 : */
   49278               0 : SQLITE_API int sqlite3_enable_shared_cache(int enable){
   49279               0 :   sqlite3GlobalConfig.sharedCacheEnabled = enable;
   49280               0 :   return SQLITE_OK;
   49281                 : }
   49282                 : #endif
   49283                 : 
   49284                 : 
   49285                 : 
   49286                 : #ifdef SQLITE_OMIT_SHARED_CACHE
   49287                 :   /*
   49288                 :   ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
   49289                 :   ** and clearAllSharedCacheTableLocks()
   49290                 :   ** manipulate entries in the BtShared.pLock linked list used to store
   49291                 :   ** shared-cache table level locks. If the library is compiled with the
   49292                 :   ** shared-cache feature disabled, then there is only ever one user
   49293                 :   ** of each BtShared structure and so this locking is not necessary. 
   49294                 :   ** So define the lock related functions as no-ops.
   49295                 :   */
   49296                 :   #define querySharedCacheTableLock(a,b,c) SQLITE_OK
   49297                 :   #define setSharedCacheTableLock(a,b,c) SQLITE_OK
   49298                 :   #define clearAllSharedCacheTableLocks(a)
   49299                 :   #define downgradeAllSharedCacheTableLocks(a)
   49300                 :   #define hasSharedCacheTableLock(a,b,c,d) 1
   49301                 :   #define hasReadConflicts(a, b) 0
   49302                 : #endif
   49303                 : 
   49304                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   49305                 : 
   49306                 : #ifdef SQLITE_DEBUG
   49307                 : /*
   49308                 : **** This function is only used as part of an assert() statement. ***
   49309                 : **
   49310                 : ** Check to see if pBtree holds the required locks to read or write to the 
   49311                 : ** table with root page iRoot.   Return 1 if it does and 0 if not.
   49312                 : **
   49313                 : ** For example, when writing to a table with root-page iRoot via 
   49314                 : ** Btree connection pBtree:
   49315                 : **
   49316                 : **    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
   49317                 : **
   49318                 : ** When writing to an index that resides in a sharable database, the 
   49319                 : ** caller should have first obtained a lock specifying the root page of
   49320                 : ** the corresponding table. This makes things a bit more complicated,
   49321                 : ** as this module treats each table as a separate structure. To determine
   49322                 : ** the table corresponding to the index being written, this
   49323                 : ** function has to search through the database schema.
   49324                 : **
   49325                 : ** Instead of a lock on the table/index rooted at page iRoot, the caller may
   49326                 : ** hold a write-lock on the schema table (root page 1). This is also
   49327                 : ** acceptable.
   49328                 : */
   49329          816113 : static int hasSharedCacheTableLock(
   49330                 :   Btree *pBtree,         /* Handle that must hold lock */
   49331                 :   Pgno iRoot,            /* Root page of b-tree */
   49332                 :   int isIndex,           /* True if iRoot is the root of an index b-tree */
   49333                 :   int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
   49334                 : ){
   49335          816113 :   Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
   49336          816113 :   Pgno iTab = 0;
   49337                 :   BtLock *pLock;
   49338                 : 
   49339                 :   /* If this database is not shareable, or if the client is reading
   49340                 :   ** and has the read-uncommitted flag set, then no lock is required. 
   49341                 :   ** Return true immediately.
   49342                 :   */
   49343          816113 :   if( (pBtree->sharable==0)
   49344          752707 :    || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
   49345                 :   ){
   49346           63406 :     return 1;
   49347                 :   }
   49348                 : 
   49349                 :   /* If the client is reading  or writing an index and the schema is
   49350                 :   ** not loaded, then it is too difficult to actually check to see if
   49351                 :   ** the correct locks are held.  So do not bother - just return true.
   49352                 :   ** This case does not come up very often anyhow.
   49353                 :   */
   49354          752707 :   if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
   49355               0 :     return 1;
   49356                 :   }
   49357                 : 
   49358                 :   /* Figure out the root-page that the lock should be held on. For table
   49359                 :   ** b-trees, this is just the root page of the b-tree being read or
   49360                 :   ** written. For index b-trees, it is the root page of the associated
   49361                 :   ** table.  */
   49362          752707 :   if( isIndex ){
   49363                 :     HashElem *p;
   49364         6287759 :     for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
   49365         5903737 :       Index *pIdx = (Index *)sqliteHashData(p);
   49366         5903737 :       if( pIdx->tnum==(int)iRoot ){
   49367          377428 :         iTab = pIdx->pTable->tnum;
   49368                 :       }
   49369                 :     }
   49370                 :   }else{
   49371          368685 :     iTab = iRoot;
   49372                 :   }
   49373                 : 
   49374                 :   /* Search for the required lock. Either a write-lock on root-page iTab, a 
   49375                 :   ** write-lock on the schema table, or (if the client is reading) a
   49376                 :   ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
   49377         1657696 :   for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
   49378         1657696 :     if( pLock->pBtree==pBtree 
   49379         1657696 :      && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
   49380          752707 :      && pLock->eLock>=eLockType 
   49381                 :     ){
   49382          752707 :       return 1;
   49383                 :     }
   49384                 :   }
   49385                 : 
   49386                 :   /* Failed to find the required lock. */
   49387               0 :   return 0;
   49388                 : }
   49389                 : #endif /* SQLITE_DEBUG */
   49390                 : 
   49391                 : #ifdef SQLITE_DEBUG
   49392                 : /*
   49393                 : **** This function may be used as part of assert() statements only. ****
   49394                 : **
   49395                 : ** Return true if it would be illegal for pBtree to write into the
   49396                 : ** table or index rooted at iRoot because other shared connections are
   49397                 : ** simultaneously reading that same table or index.
   49398                 : **
   49399                 : ** It is illegal for pBtree to write if some other Btree object that
   49400                 : ** shares the same BtShared object is currently reading or writing
   49401                 : ** the iRoot table.  Except, if the other Btree object has the
   49402                 : ** read-uncommitted flag set, then it is OK for the other object to
   49403                 : ** have a read cursor.
   49404                 : **
   49405                 : ** For example, before writing to any part of the table or index
   49406                 : ** rooted at page iRoot, one should call:
   49407                 : **
   49408                 : **    assert( !hasReadConflicts(pBtree, iRoot) );
   49409                 : */
   49410          315292 : static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
   49411                 :   BtCursor *p;
   49412          904586 :   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
   49413          589294 :     if( p->pgnoRoot==iRoot 
   49414           52768 :      && p->pBtree!=pBtree
   49415               0 :      && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
   49416                 :     ){
   49417               0 :       return 1;
   49418                 :     }
   49419                 :   }
   49420          315292 :   return 0;
   49421                 : }
   49422                 : #endif    /* #ifdef SQLITE_DEBUG */
   49423                 : 
   49424                 : /*
   49425                 : ** Query to see if Btree handle p may obtain a lock of type eLock 
   49426                 : ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
   49427                 : ** SQLITE_OK if the lock may be obtained (by calling
   49428                 : ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
   49429                 : */
   49430          977433 : static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
   49431          977433 :   BtShared *pBt = p->pBt;
   49432                 :   BtLock *pIter;
   49433                 : 
   49434          977433 :   assert( sqlite3BtreeHoldsMutex(p) );
   49435          977433 :   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
   49436          977433 :   assert( p->db!=0 );
   49437          977433 :   assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
   49438                 :   
   49439                 :   /* If requesting a write-lock, then the Btree must have an open write
   49440                 :   ** transaction on this file. And, obviously, for this to be so there 
   49441                 :   ** must be an open write transaction on the file itself.
   49442                 :   */
   49443          977433 :   assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
   49444          977433 :   assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
   49445                 :   
   49446                 :   /* This routine is a no-op if the shared-cache is not enabled */
   49447          977433 :   if( !p->sharable ){
   49448           76708 :     return SQLITE_OK;
   49449                 :   }
   49450                 : 
   49451                 :   /* If some other connection is holding an exclusive lock, the
   49452                 :   ** requested lock may not be obtained.
   49453                 :   */
   49454          900725 :   if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
   49455               0 :     sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
   49456               0 :     return SQLITE_LOCKED_SHAREDCACHE;
   49457                 :   }
   49458                 : 
   49459         3260231 :   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
   49460                 :     /* The condition (pIter->eLock!=eLock) in the following if(...) 
   49461                 :     ** statement is a simplification of:
   49462                 :     **
   49463                 :     **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
   49464                 :     **
   49465                 :     ** since we know that if eLock==WRITE_LOCK, then no other connection
   49466                 :     ** may hold a WRITE_LOCK on any table in this file (since there can
   49467                 :     ** only be a single writer).
   49468                 :     */
   49469         2359804 :     assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
   49470         2359804 :     assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
   49471         2359804 :     if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
   49472             298 :       sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
   49473             298 :       if( eLock==WRITE_LOCK ){
   49474             298 :         assert( p==pBt->pWriter );
   49475             298 :         pBt->btsFlags |= BTS_PENDING;
   49476                 :       }
   49477             298 :       return SQLITE_LOCKED_SHAREDCACHE;
   49478                 :     }
   49479                 :   }
   49480          900427 :   return SQLITE_OK;
   49481                 : }
   49482                 : #endif /* !SQLITE_OMIT_SHARED_CACHE */
   49483                 : 
   49484                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   49485                 : /*
   49486                 : ** Add a lock on the table with root-page iTable to the shared-btree used
   49487                 : ** by Btree handle p. Parameter eLock must be either READ_LOCK or 
   49488                 : ** WRITE_LOCK.
   49489                 : **
   49490                 : ** This function assumes the following:
   49491                 : **
   49492                 : **   (a) The specified Btree object p is connected to a sharable
   49493                 : **       database (one with the BtShared.sharable flag set), and
   49494                 : **
   49495                 : **   (b) No other Btree objects hold a lock that conflicts
   49496                 : **       with the requested lock (i.e. querySharedCacheTableLock() has
   49497                 : **       already been called and returned SQLITE_OK).
   49498                 : **
   49499                 : ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 
   49500                 : ** is returned if a malloc attempt fails.
   49501                 : */
   49502          235386 : static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
   49503          235386 :   BtShared *pBt = p->pBt;
   49504          235386 :   BtLock *pLock = 0;
   49505                 :   BtLock *pIter;
   49506                 : 
   49507          235386 :   assert( sqlite3BtreeHoldsMutex(p) );
   49508          235386 :   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
   49509          235386 :   assert( p->db!=0 );
   49510                 : 
   49511                 :   /* A connection with the read-uncommitted flag set will never try to
   49512                 :   ** obtain a read-lock using this function. The only read-lock obtained
   49513                 :   ** by a connection in read-uncommitted mode is on the sqlite_master 
   49514                 :   ** table, and that lock is obtained in BtreeBeginTrans().  */
   49515          235386 :   assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
   49516                 : 
   49517                 :   /* This function should only be called on a sharable b-tree after it 
   49518                 :   ** has been determined that no other b-tree holds a conflicting lock.  */
   49519          235386 :   assert( p->sharable );
   49520          235386 :   assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
   49521                 : 
   49522                 :   /* First search the list for an existing lock on this table. */
   49523          640760 :   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
   49524          530587 :     if( pIter->iTable==iTable && pIter->pBtree==p ){
   49525          125213 :       pLock = pIter;
   49526          125213 :       break;
   49527                 :     }
   49528                 :   }
   49529                 : 
   49530                 :   /* If the above search did not find a BtLock struct associating Btree p
   49531                 :   ** with table iTable, allocate one and link it into the list.
   49532                 :   */
   49533          235386 :   if( !pLock ){
   49534          110173 :     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
   49535          110173 :     if( !pLock ){
   49536               0 :       return SQLITE_NOMEM;
   49537                 :     }
   49538          110173 :     pLock->iTable = iTable;
   49539          110173 :     pLock->pBtree = p;
   49540          110173 :     pLock->pNext = pBt->pLock;
   49541          110173 :     pBt->pLock = pLock;
   49542                 :   }
   49543                 : 
   49544                 :   /* Set the BtLock.eLock variable to the maximum of the current lock
   49545                 :   ** and the requested lock. This means if a write-lock was already held
   49546                 :   ** and a read-lock requested, we don't incorrectly downgrade the lock.
   49547                 :   */
   49548                 :   assert( WRITE_LOCK>READ_LOCK );
   49549          235386 :   if( eLock>pLock->eLock ){
   49550          118854 :     pLock->eLock = eLock;
   49551                 :   }
   49552                 : 
   49553          235386 :   return SQLITE_OK;
   49554                 : }
   49555                 : #endif /* !SQLITE_OMIT_SHARED_CACHE */
   49556                 : 
   49557                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   49558                 : /*
   49559                 : ** Release all the table locks (locks obtained via calls to
   49560                 : ** the setSharedCacheTableLock() procedure) held by Btree object p.
   49561                 : **
   49562                 : ** This function assumes that Btree p has an open read or write 
   49563                 : ** transaction. If it does not, then the BTS_PENDING flag
   49564                 : ** may be incorrectly cleared.
   49565                 : */
   49566          100195 : static void clearAllSharedCacheTableLocks(Btree *p){
   49567          100195 :   BtShared *pBt = p->pBt;
   49568          100195 :   BtLock **ppIter = &pBt->pLock;
   49569                 : 
   49570          100195 :   assert( sqlite3BtreeHoldsMutex(p) );
   49571          100195 :   assert( p->sharable || 0==*ppIter );
   49572          100195 :   assert( p->inTrans>0 );
   49573                 : 
   49574          389438 :   while( *ppIter ){
   49575          189048 :     BtLock *pLock = *ppIter;
   49576          189048 :     assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
   49577          189048 :     assert( pLock->pBtree->inTrans>=pLock->eLock );
   49578          189048 :     if( pLock->pBtree==p ){
   49579          187640 :       *ppIter = pLock->pNext;
   49580          187640 :       assert( pLock->iTable!=1 || pLock==&p->lock );
   49581          187640 :       if( pLock->iTable!=1 ){
   49582          110173 :         sqlite3_free(pLock);
   49583                 :       }
   49584                 :     }else{
   49585            1408 :       ppIter = &pLock->pNext;
   49586                 :     }
   49587                 :   }
   49588                 : 
   49589          100195 :   assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
   49590          100195 :   if( pBt->pWriter==p ){
   49591           56192 :     pBt->pWriter = 0;
   49592           56192 :     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
   49593           44003 :   }else if( pBt->nTransaction==2 ){
   49594                 :     /* This function is called when Btree p is concluding its 
   49595                 :     ** transaction. If there currently exists a writer, and p is not
   49596                 :     ** that writer, then the number of locks held by connections other
   49597                 :     ** than the writer must be about to drop to zero. In this case
   49598                 :     ** set the BTS_PENDING flag to 0.
   49599                 :     **
   49600                 :     ** If there is not currently a writer, then BTS_PENDING must
   49601                 :     ** be zero already. So this next line is harmless in that case.
   49602                 :     */
   49603             154 :     pBt->btsFlags &= ~BTS_PENDING;
   49604                 :   }
   49605          100195 : }
   49606                 : 
   49607                 : /*
   49608                 : ** This function changes all write-locks held by Btree p into read-locks.
   49609                 : */
   49610            1263 : static void downgradeAllSharedCacheTableLocks(Btree *p){
   49611            1263 :   BtShared *pBt = p->pBt;
   49612            1263 :   if( pBt->pWriter==p ){
   49613                 :     BtLock *pLock;
   49614             701 :     pBt->pWriter = 0;
   49615             701 :     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
   49616            2597 :     for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
   49617            1896 :       assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
   49618            1896 :       pLock->eLock = READ_LOCK;
   49619                 :     }
   49620                 :   }
   49621            1263 : }
   49622                 : 
   49623                 : #endif /* SQLITE_OMIT_SHARED_CACHE */
   49624                 : 
   49625                 : static void releasePage(MemPage *pPage);  /* Forward reference */
   49626                 : 
   49627                 : /*
   49628                 : ***** This routine is used inside of assert() only ****
   49629                 : **
   49630                 : ** Verify that the cursor holds the mutex on its BtShared
   49631                 : */
   49632                 : #ifdef SQLITE_DEBUG
   49633         6919177 : static int cursorHoldsMutex(BtCursor *p){
   49634         6919177 :   return sqlite3_mutex_held(p->pBt->mutex);
   49635                 : }
   49636                 : #endif
   49637                 : 
   49638                 : 
   49639                 : #ifndef SQLITE_OMIT_INCRBLOB
   49640                 : /*
   49641                 : ** Invalidate the overflow page-list cache for cursor pCur, if any.
   49642                 : */
   49643          533094 : static void invalidateOverflowCache(BtCursor *pCur){
   49644          533094 :   assert( cursorHoldsMutex(pCur) );
   49645          533094 :   sqlite3_free(pCur->aOverflow);
   49646          533094 :   pCur->aOverflow = 0;
   49647          533094 : }
   49648                 : 
   49649                 : /*
   49650                 : ** Invalidate the overflow page-list cache for all cursors opened
   49651                 : ** on the shared btree structure pBt.
   49652                 : */
   49653               0 : static void invalidateAllOverflowCache(BtShared *pBt){
   49654                 :   BtCursor *p;
   49655               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   49656               0 :   for(p=pBt->pCursor; p; p=p->pNext){
   49657               0 :     invalidateOverflowCache(p);
   49658                 :   }
   49659               0 : }
   49660                 : 
   49661                 : /*
   49662                 : ** This function is called before modifying the contents of a table
   49663                 : ** to invalidate any incrblob cursors that are open on the
   49664                 : ** row or one of the rows being modified.
   49665                 : **
   49666                 : ** If argument isClearTable is true, then the entire contents of the
   49667                 : ** table is about to be deleted. In this case invalidate all incrblob
   49668                 : ** cursors open on any row within the table with root-page pgnoRoot.
   49669                 : **
   49670                 : ** Otherwise, if argument isClearTable is false, then the row with
   49671                 : ** rowid iRow is being replaced or deleted. In this case invalidate
   49672                 : ** only those incrblob cursors open on that specific row.
   49673                 : */
   49674          109327 : static void invalidateIncrblobCursors(
   49675                 :   Btree *pBtree,          /* The database file to check */
   49676                 :   i64 iRow,               /* The rowid that might be changing */
   49677                 :   int isClearTable        /* True if all rows are being deleted */
   49678                 : ){
   49679                 :   BtCursor *p;
   49680          109327 :   BtShared *pBt = pBtree->pBt;
   49681          109327 :   assert( sqlite3BtreeHoldsMutex(pBtree) );
   49682          377655 :   for(p=pBt->pCursor; p; p=p->pNext){
   49683          268328 :     if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
   49684               0 :       p->eState = CURSOR_INVALID;
   49685                 :     }
   49686                 :   }
   49687          109327 : }
   49688                 : 
   49689                 : #else
   49690                 :   /* Stub functions when INCRBLOB is omitted */
   49691                 :   #define invalidateOverflowCache(x)
   49692                 :   #define invalidateAllOverflowCache(x)
   49693                 :   #define invalidateIncrblobCursors(x,y,z)
   49694                 : #endif /* SQLITE_OMIT_INCRBLOB */
   49695                 : 
   49696                 : /*
   49697                 : ** Set bit pgno of the BtShared.pHasContent bitvec. This is called 
   49698                 : ** when a page that previously contained data becomes a free-list leaf 
   49699                 : ** page.
   49700                 : **
   49701                 : ** The BtShared.pHasContent bitvec exists to work around an obscure
   49702                 : ** bug caused by the interaction of two useful IO optimizations surrounding
   49703                 : ** free-list leaf pages:
   49704                 : **
   49705                 : **   1) When all data is deleted from a page and the page becomes
   49706                 : **      a free-list leaf page, the page is not written to the database
   49707                 : **      (as free-list leaf pages contain no meaningful data). Sometimes
   49708                 : **      such a page is not even journalled (as it will not be modified,
   49709                 : **      why bother journalling it?).
   49710                 : **
   49711                 : **   2) When a free-list leaf page is reused, its content is not read
   49712                 : **      from the database or written to the journal file (why should it
   49713                 : **      be, if it is not at all meaningful?).
   49714                 : **
   49715                 : ** By themselves, these optimizations work fine and provide a handy
   49716                 : ** performance boost to bulk delete or insert operations. However, if
   49717                 : ** a page is moved to the free-list and then reused within the same
   49718                 : ** transaction, a problem comes up. If the page is not journalled when
   49719                 : ** it is moved to the free-list and it is also not journalled when it
   49720                 : ** is extracted from the free-list and reused, then the original data
   49721                 : ** may be lost. In the event of a rollback, it may not be possible
   49722                 : ** to restore the database to its original configuration.
   49723                 : **
   49724                 : ** The solution is the BtShared.pHasContent bitvec. Whenever a page is 
   49725                 : ** moved to become a free-list leaf page, the corresponding bit is
   49726                 : ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
   49727                 : ** optimization 2 above is omitted if the corresponding bit is already
   49728                 : ** set in BtShared.pHasContent. The contents of the bitvec are cleared
   49729                 : ** at the end of every transaction.
   49730                 : */
   49731              51 : static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
   49732              51 :   int rc = SQLITE_OK;
   49733              51 :   if( !pBt->pHasContent ){
   49734              32 :     assert( pgno<=pBt->nPage );
   49735              32 :     pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
   49736              32 :     if( !pBt->pHasContent ){
   49737               0 :       rc = SQLITE_NOMEM;
   49738                 :     }
   49739                 :   }
   49740              51 :   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
   49741              51 :     rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
   49742                 :   }
   49743              51 :   return rc;
   49744                 : }
   49745                 : 
   49746                 : /*
   49747                 : ** Query the BtShared.pHasContent vector.
   49748                 : **
   49749                 : ** This function is called when a free-list leaf page is removed from the
   49750                 : ** free-list for reuse. It returns false if it is safe to retrieve the
   49751                 : ** page from the pager layer with the 'no-content' flag set. True otherwise.
   49752                 : */
   49753              56 : static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
   49754              56 :   Bitvec *p = pBt->pHasContent;
   49755              56 :   return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
   49756                 : }
   49757                 : 
   49758                 : /*
   49759                 : ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
   49760                 : ** invoked at the conclusion of each write-transaction.
   49761                 : */
   49762          105163 : static void btreeClearHasContent(BtShared *pBt){
   49763          105163 :   sqlite3BitvecDestroy(pBt->pHasContent);
   49764          105163 :   pBt->pHasContent = 0;
   49765          105163 : }
   49766                 : 
   49767                 : /*
   49768                 : ** Save the current cursor position in the variables BtCursor.nKey 
   49769                 : ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
   49770                 : **
   49771                 : ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
   49772                 : ** prior to calling this routine.  
   49773                 : */
   49774             265 : static int saveCursorPosition(BtCursor *pCur){
   49775                 :   int rc;
   49776                 : 
   49777             265 :   assert( CURSOR_VALID==pCur->eState );
   49778             265 :   assert( 0==pCur->pKey );
   49779             265 :   assert( cursorHoldsMutex(pCur) );
   49780                 : 
   49781             265 :   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
   49782             265 :   assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
   49783                 : 
   49784                 :   /* If this is an intKey table, then the above call to BtreeKeySize()
   49785                 :   ** stores the integer key in pCur->nKey. In this case this value is
   49786                 :   ** all that is required. Otherwise, if pCur is not open on an intKey
   49787                 :   ** table, then malloc space for and store the pCur->nKey bytes of key 
   49788                 :   ** data.
   49789                 :   */
   49790             265 :   if( 0==pCur->apPage[0]->intKey ){
   49791              62 :     void *pKey = sqlite3Malloc( (int)pCur->nKey );
   49792              62 :     if( pKey ){
   49793              62 :       rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
   49794              62 :       if( rc==SQLITE_OK ){
   49795              62 :         pCur->pKey = pKey;
   49796                 :       }else{
   49797               0 :         sqlite3_free(pKey);
   49798                 :       }
   49799                 :     }else{
   49800               0 :       rc = SQLITE_NOMEM;
   49801                 :     }
   49802                 :   }
   49803             265 :   assert( !pCur->apPage[0]->intKey || !pCur->pKey );
   49804                 : 
   49805             265 :   if( rc==SQLITE_OK ){
   49806                 :     int i;
   49807             530 :     for(i=0; i<=pCur->iPage; i++){
   49808             265 :       releasePage(pCur->apPage[i]);
   49809             265 :       pCur->apPage[i] = 0;
   49810                 :     }
   49811             265 :     pCur->iPage = -1;
   49812             265 :     pCur->eState = CURSOR_REQUIRESEEK;
   49813                 :   }
   49814                 : 
   49815             265 :   invalidateOverflowCache(pCur);
   49816             265 :   return rc;
   49817                 : }
   49818                 : 
   49819                 : /*
   49820                 : ** Save the positions of all cursors (except pExcept) that are open on
   49821                 : ** the table  with root-page iRoot. Usually, this is called just before cursor
   49822                 : ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
   49823                 : */
   49824          304049 : static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
   49825                 :   BtCursor *p;
   49826          304049 :   assert( sqlite3_mutex_held(pBt->mutex) );
   49827          304049 :   assert( pExcept==0 || pExcept->pBt==pBt );
   49828         1204377 :   for(p=pBt->pCursor; p; p=p->pNext){
   49829          900664 :     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && 
   49830             336 :         p->eState==CURSOR_VALID ){
   49831             265 :       int rc = saveCursorPosition(p);
   49832             265 :       if( SQLITE_OK!=rc ){
   49833               0 :         return rc;
   49834                 :       }
   49835                 :     }
   49836                 :   }
   49837          304049 :   return SQLITE_OK;
   49838                 : }
   49839                 : 
   49840                 : /*
   49841                 : ** Clear the current cursor position.
   49842                 : */
   49843          555410 : SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
   49844          555410 :   assert( cursorHoldsMutex(pCur) );
   49845          555410 :   sqlite3_free(pCur->pKey);
   49846          555410 :   pCur->pKey = 0;
   49847          555410 :   pCur->eState = CURSOR_INVALID;
   49848          555410 : }
   49849                 : 
   49850                 : /*
   49851                 : ** In this version of BtreeMoveto, pKey is a packed index record
   49852                 : ** such as is generated by the OP_MakeRecord opcode.  Unpack the
   49853                 : ** record and then call BtreeMovetoUnpacked() to do the work.
   49854                 : */
   49855          194508 : static int btreeMoveto(
   49856                 :   BtCursor *pCur,     /* Cursor open on the btree to be searched */
   49857                 :   const void *pKey,   /* Packed key if the btree is an index */
   49858                 :   i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
   49859                 :   int bias,           /* Bias search to the high end */
   49860                 :   int *pRes           /* Write search results here */
   49861                 : ){
   49862                 :   int rc;                    /* Status code */
   49863                 :   UnpackedRecord *pIdxKey;   /* Unpacked index key */
   49864                 :   char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
   49865          194508 :   char *pFree = 0;
   49866                 : 
   49867          194508 :   if( pKey ){
   49868          100471 :     assert( nKey==(i64)(int)nKey );
   49869          100471 :     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
   49870          100471 :         pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
   49871                 :     );
   49872          100471 :     if( pIdxKey==0 ) return SQLITE_NOMEM;
   49873          100471 :     sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
   49874                 :   }else{
   49875           94037 :     pIdxKey = 0;
   49876                 :   }
   49877          194508 :   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
   49878          194508 :   if( pFree ){
   49879           20406 :     sqlite3DbFree(pCur->pKeyInfo->db, pFree);
   49880                 :   }
   49881          194508 :   return rc;
   49882                 : }
   49883                 : 
   49884                 : /*
   49885                 : ** Restore the cursor to the position it was in (or as close to as possible)
   49886                 : ** when saveCursorPosition() was called. Note that this call deletes the 
   49887                 : ** saved position info stored by saveCursorPosition(), so there can be
   49888                 : ** at most one effective restoreCursorPosition() call after each 
   49889                 : ** saveCursorPosition().
   49890                 : */
   49891             119 : static int btreeRestoreCursorPosition(BtCursor *pCur){
   49892                 :   int rc;
   49893             119 :   assert( cursorHoldsMutex(pCur) );
   49894             119 :   assert( pCur->eState>=CURSOR_REQUIRESEEK );
   49895             119 :   if( pCur->eState==CURSOR_FAULT ){
   49896               0 :     return pCur->skipNext;
   49897                 :   }
   49898             119 :   pCur->eState = CURSOR_INVALID;
   49899             119 :   rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
   49900             119 :   if( rc==SQLITE_OK ){
   49901             119 :     sqlite3_free(pCur->pKey);
   49902             119 :     pCur->pKey = 0;
   49903             119 :     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
   49904                 :   }
   49905             119 :   return rc;
   49906                 : }
   49907                 : 
   49908                 : #define restoreCursorPosition(p) \
   49909                 :   (p->eState>=CURSOR_REQUIRESEEK ? \
   49910                 :          btreeRestoreCursorPosition(p) : \
   49911                 :          SQLITE_OK)
   49912                 : 
   49913                 : /*
   49914                 : ** Determine whether or not a cursor has moved from the position it
   49915                 : ** was last placed at.  Cursors can move when the row they are pointing
   49916                 : ** at is deleted out from under them.
   49917                 : **
   49918                 : ** This routine returns an error code if something goes wrong.  The
   49919                 : ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
   49920                 : */
   49921         1994986 : SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
   49922                 :   int rc;
   49923                 : 
   49924         1994986 :   rc = restoreCursorPosition(pCur);
   49925         1994981 :   if( rc ){
   49926               0 :     *pHasMoved = 1;
   49927               0 :     return rc;
   49928                 :   }
   49929         1994981 :   if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
   49930           24188 :     *pHasMoved = 1;
   49931                 :   }else{
   49932         1970793 :     *pHasMoved = 0;
   49933                 :   }
   49934         1994981 :   return SQLITE_OK;
   49935                 : }
   49936                 : 
   49937                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   49938                 : /*
   49939                 : ** Given a page number of a regular database page, return the page
   49940                 : ** number for the pointer-map page that contains the entry for the
   49941                 : ** input page number.
   49942                 : **
   49943                 : ** Return 0 (not a valid page) for pgno==1 since there is
   49944                 : ** no pointer map associated with page 1.  The integrity_check logic
   49945                 : ** requires that ptrmapPageno(*,1)!=1.
   49946                 : */
   49947            1033 : static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
   49948                 :   int nPagesPerMapPage;
   49949                 :   Pgno iPtrMap, ret;
   49950            1033 :   assert( sqlite3_mutex_held(pBt->mutex) );
   49951            1033 :   if( pgno<2 ) return 0;
   49952             981 :   nPagesPerMapPage = (pBt->usableSize/5)+1;
   49953             981 :   iPtrMap = (pgno-2)/nPagesPerMapPage;
   49954             981 :   ret = (iPtrMap*nPagesPerMapPage) + 2; 
   49955             981 :   if( ret==PENDING_BYTE_PAGE(pBt) ){
   49956               0 :     ret++;
   49957                 :   }
   49958             981 :   return ret;
   49959                 : }
   49960                 : 
   49961                 : /*
   49962                 : ** Write an entry into the pointer map.
   49963                 : **
   49964                 : ** This routine updates the pointer map entry for page number 'key'
   49965                 : ** so that it maps to type 'eType' and parent page number 'pgno'.
   49966                 : **
   49967                 : ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
   49968                 : ** a no-op.  If an error occurs, the appropriate error code is written
   49969                 : ** into *pRC.
   49970                 : */
   49971               0 : static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
   49972                 :   DbPage *pDbPage;  /* The pointer map page */
   49973                 :   u8 *pPtrmap;      /* The pointer map data */
   49974                 :   Pgno iPtrmap;     /* The pointer map page number */
   49975                 :   int offset;       /* Offset in pointer map page */
   49976                 :   int rc;           /* Return code from subfunctions */
   49977                 : 
   49978               0 :   if( *pRC ) return;
   49979                 : 
   49980               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   49981                 :   /* The master-journal page number must never be used as a pointer map page */
   49982               0 :   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
   49983                 : 
   49984               0 :   assert( pBt->autoVacuum );
   49985               0 :   if( key==0 ){
   49986               0 :     *pRC = SQLITE_CORRUPT_BKPT;
   49987               0 :     return;
   49988                 :   }
   49989               0 :   iPtrmap = PTRMAP_PAGENO(pBt, key);
   49990               0 :   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
   49991               0 :   if( rc!=SQLITE_OK ){
   49992               0 :     *pRC = rc;
   49993               0 :     return;
   49994                 :   }
   49995               0 :   offset = PTRMAP_PTROFFSET(iPtrmap, key);
   49996               0 :   if( offset<0 ){
   49997               0 :     *pRC = SQLITE_CORRUPT_BKPT;
   49998               0 :     goto ptrmap_exit;
   49999                 :   }
   50000               0 :   assert( offset <= (int)pBt->usableSize-5 );
   50001               0 :   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
   50002                 : 
   50003               0 :   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
   50004                 :     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
   50005               0 :     *pRC= rc = sqlite3PagerWrite(pDbPage);
   50006               0 :     if( rc==SQLITE_OK ){
   50007               0 :       pPtrmap[offset] = eType;
   50008               0 :       put4byte(&pPtrmap[offset+1], parent);
   50009                 :     }
   50010                 :   }
   50011                 : 
   50012                 : ptrmap_exit:
   50013               0 :   sqlite3PagerUnref(pDbPage);
   50014                 : }
   50015                 : 
   50016                 : /*
   50017                 : ** Read an entry from the pointer map.
   50018                 : **
   50019                 : ** This routine retrieves the pointer map entry for page 'key', writing
   50020                 : ** the type and parent page number to *pEType and *pPgno respectively.
   50021                 : ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
   50022                 : */
   50023               0 : static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
   50024                 :   DbPage *pDbPage;   /* The pointer map page */
   50025                 :   int iPtrmap;       /* Pointer map page index */
   50026                 :   u8 *pPtrmap;       /* Pointer map page data */
   50027                 :   int offset;        /* Offset of entry in pointer map */
   50028                 :   int rc;
   50029                 : 
   50030               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   50031                 : 
   50032               0 :   iPtrmap = PTRMAP_PAGENO(pBt, key);
   50033               0 :   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
   50034               0 :   if( rc!=0 ){
   50035               0 :     return rc;
   50036                 :   }
   50037               0 :   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
   50038                 : 
   50039               0 :   offset = PTRMAP_PTROFFSET(iPtrmap, key);
   50040               0 :   if( offset<0 ){
   50041               0 :     sqlite3PagerUnref(pDbPage);
   50042               0 :     return SQLITE_CORRUPT_BKPT;
   50043                 :   }
   50044               0 :   assert( offset <= (int)pBt->usableSize-5 );
   50045               0 :   assert( pEType!=0 );
   50046               0 :   *pEType = pPtrmap[offset];
   50047               0 :   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
   50048                 : 
   50049               0 :   sqlite3PagerUnref(pDbPage);
   50050               0 :   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
   50051               0 :   return SQLITE_OK;
   50052                 : }
   50053                 : 
   50054                 : #else /* if defined SQLITE_OMIT_AUTOVACUUM */
   50055                 :   #define ptrmapPut(w,x,y,z,rc)
   50056                 :   #define ptrmapGet(w,x,y,z) SQLITE_OK
   50057                 :   #define ptrmapPutOvflPtr(x, y, rc)
   50058                 : #endif
   50059                 : 
   50060                 : /*
   50061                 : ** Given a btree page and a cell index (0 means the first cell on
   50062                 : ** the page, 1 means the second cell, and so forth) return a pointer
   50063                 : ** to the cell content.
   50064                 : **
   50065                 : ** This routine works only for pages that do not contain overflow cells.
   50066                 : */
   50067                 : #define findCell(P,I) \
   50068                 :   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
   50069                 : #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
   50070                 : 
   50071                 : 
   50072                 : /*
   50073                 : ** This a more complex version of findCell() that works for
   50074                 : ** pages that do contain overflow cells.
   50075                 : */
   50076           59782 : static u8 *findOverflowCell(MemPage *pPage, int iCell){
   50077                 :   int i;
   50078           59782 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50079          119370 :   for(i=pPage->nOverflow-1; i>=0; i--){
   50080                 :     int k;
   50081                 :     struct _OvflCell *pOvfl;
   50082           59782 :     pOvfl = &pPage->aOvfl[i];
   50083           59782 :     k = pOvfl->idx;
   50084           59782 :     if( k<=iCell ){
   50085           24376 :       if( k==iCell ){
   50086             194 :         return pOvfl->pCell;
   50087                 :       }
   50088           24182 :       iCell--;
   50089                 :     }
   50090                 :   }
   50091           59588 :   return findCell(pPage, iCell);
   50092                 : }
   50093                 : 
   50094                 : /*
   50095                 : ** Parse a cell content block and fill in the CellInfo structure.  There
   50096                 : ** are two versions of this function.  btreeParseCell() takes a 
   50097                 : ** cell index as the second argument and btreeParseCellPtr() 
   50098                 : ** takes a pointer to the body of the cell as its second argument.
   50099                 : **
   50100                 : ** Within this file, the parseCell() macro can be called instead of
   50101                 : ** btreeParseCellPtr(). Using some compilers, this will be faster.
   50102                 : */
   50103         2058183 : static void btreeParseCellPtr(
   50104                 :   MemPage *pPage,         /* Page containing the cell */
   50105                 :   u8 *pCell,              /* Pointer to the cell text. */
   50106                 :   CellInfo *pInfo         /* Fill in this structure */
   50107                 : ){
   50108                 :   u16 n;                  /* Number bytes in cell content header */
   50109                 :   u32 nPayload;           /* Number of bytes of cell payload */
   50110                 : 
   50111         2058183 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50112                 : 
   50113         2058184 :   pInfo->pCell = pCell;
   50114         2058184 :   assert( pPage->leaf==0 || pPage->leaf==1 );
   50115         2058184 :   n = pPage->childPtrSize;
   50116         2058184 :   assert( n==4-4*pPage->leaf );
   50117         2058184 :   if( pPage->intKey ){
   50118         1041388 :     if( pPage->hasData ){
   50119         1040991 :       n += getVarint32(&pCell[n], nPayload);
   50120                 :     }else{
   50121             397 :       nPayload = 0;
   50122                 :     }
   50123         1041388 :     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
   50124         1041385 :     pInfo->nData = nPayload;
   50125                 :   }else{
   50126         1016796 :     pInfo->nData = 0;
   50127         1016796 :     n += getVarint32(&pCell[n], nPayload);
   50128         1016796 :     pInfo->nKey = nPayload;
   50129                 :   }
   50130         2058181 :   pInfo->nPayload = nPayload;
   50131         2058181 :   pInfo->nHeader = n;
   50132                 :   testcase( nPayload==pPage->maxLocal );
   50133                 :   testcase( nPayload==pPage->maxLocal+1 );
   50134         2058181 :   if( likely(nPayload<=pPage->maxLocal) ){
   50135                 :     /* This is the (easy) common case where the entire payload fits
   50136                 :     ** on the local page.  No overflow is required.
   50137                 :     */
   50138         2058159 :     if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
   50139         2058159 :     pInfo->nLocal = (u16)nPayload;
   50140         2058159 :     pInfo->iOverflow = 0;
   50141                 :   }else{
   50142                 :     /* If the payload will not fit completely on the local page, we have
   50143                 :     ** to decide how much to store locally and how much to spill onto
   50144                 :     ** overflow pages.  The strategy is to minimize the amount of unused
   50145                 :     ** space on overflow pages while keeping the amount of local storage
   50146                 :     ** in between minLocal and maxLocal.
   50147                 :     **
   50148                 :     ** Warning:  changing the way overflow payload is distributed in any
   50149                 :     ** way will result in an incompatible file format.
   50150                 :     */
   50151                 :     int minLocal;  /* Minimum amount of payload held locally */
   50152                 :     int maxLocal;  /* Maximum amount of payload held locally */
   50153                 :     int surplus;   /* Overflow payload available for local storage */
   50154                 : 
   50155              22 :     minLocal = pPage->minLocal;
   50156              22 :     maxLocal = pPage->maxLocal;
   50157              22 :     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
   50158                 :     testcase( surplus==maxLocal );
   50159                 :     testcase( surplus==maxLocal+1 );
   50160              22 :     if( surplus <= maxLocal ){
   50161               0 :       pInfo->nLocal = (u16)surplus;
   50162                 :     }else{
   50163              22 :       pInfo->nLocal = (u16)minLocal;
   50164                 :     }
   50165              22 :     pInfo->iOverflow = (u16)(pInfo->nLocal + n);
   50166              22 :     pInfo->nSize = pInfo->iOverflow + 4;
   50167                 :   }
   50168         2058181 : }
   50169                 : #define parseCell(pPage, iCell, pInfo) \
   50170                 :   btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
   50171          963088 : static void btreeParseCell(
   50172                 :   MemPage *pPage,         /* Page containing the cell */
   50173                 :   int iCell,              /* The cell index.  First cell is 0 */
   50174                 :   CellInfo *pInfo         /* Fill in this structure */
   50175                 : ){
   50176          963088 :   parseCell(pPage, iCell, pInfo);
   50177          963087 : }
   50178                 : 
   50179                 : /*
   50180                 : ** Compute the total number of bytes that a Cell needs in the cell
   50181                 : ** data area of the btree-page.  The return number includes the cell
   50182                 : ** data header and the local payload, but not any overflow page or
   50183                 : ** the space used by the cell pointer.
   50184                 : */
   50185          765911 : static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
   50186          765911 :   u8 *pIter = &pCell[pPage->childPtrSize];
   50187                 :   u32 nSize;
   50188                 : 
   50189                 : #ifdef SQLITE_DEBUG
   50190                 :   /* The value returned by this function should always be the same as
   50191                 :   ** the (CellInfo.nSize) value found by doing a full parse of the
   50192                 :   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
   50193                 :   ** this function verifies that this invariant is not violated. */
   50194                 :   CellInfo debuginfo;
   50195          765911 :   btreeParseCellPtr(pPage, pCell, &debuginfo);
   50196                 : #endif
   50197                 : 
   50198          765911 :   if( pPage->intKey ){
   50199                 :     u8 *pEnd;
   50200          310461 :     if( pPage->hasData ){
   50201          310083 :       pIter += getVarint32(pIter, nSize);
   50202                 :     }else{
   50203             378 :       nSize = 0;
   50204                 :     }
   50205                 : 
   50206                 :     /* pIter now points at the 64-bit integer key value, a variable length 
   50207                 :     ** integer. The following block moves pIter to point at the first byte
   50208                 :     ** past the end of the key value. */
   50209          310461 :     pEnd = &pIter[9];
   50210          310461 :     while( (*pIter++)&0x80 && pIter<pEnd );
   50211                 :   }else{
   50212          455450 :     pIter += getVarint32(pIter, nSize);
   50213                 :   }
   50214                 : 
   50215                 :   testcase( nSize==pPage->maxLocal );
   50216                 :   testcase( nSize==pPage->maxLocal+1 );
   50217          765911 :   if( nSize>pPage->maxLocal ){
   50218               0 :     int minLocal = pPage->minLocal;
   50219               0 :     nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
   50220                 :     testcase( nSize==pPage->maxLocal );
   50221                 :     testcase( nSize==pPage->maxLocal+1 );
   50222               0 :     if( nSize>pPage->maxLocal ){
   50223               0 :       nSize = minLocal;
   50224                 :     }
   50225               0 :     nSize += 4;
   50226                 :   }
   50227          765911 :   nSize += (u32)(pIter - pCell);
   50228                 : 
   50229                 :   /* The minimum size of any cell is 4 bytes. */
   50230          765911 :   if( nSize<4 ){
   50231           37572 :     nSize = 4;
   50232                 :   }
   50233                 : 
   50234          765911 :   assert( nSize==debuginfo.nSize );
   50235          765911 :   return (u16)nSize;
   50236                 : }
   50237                 : 
   50238                 : #ifdef SQLITE_DEBUG
   50239                 : /* This variation on cellSizePtr() is used inside of assert() statements
   50240                 : ** only. */
   50241           93232 : static u16 cellSize(MemPage *pPage, int iCell){
   50242           93232 :   return cellSizePtr(pPage, findCell(pPage, iCell));
   50243                 : }
   50244                 : #endif
   50245                 : 
   50246                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   50247                 : /*
   50248                 : ** If the cell pCell, part of page pPage contains a pointer
   50249                 : ** to an overflow page, insert an entry into the pointer-map
   50250                 : ** for the overflow page.
   50251                 : */
   50252               0 : static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
   50253                 :   CellInfo info;
   50254               0 :   if( *pRC ) return;
   50255               0 :   assert( pCell!=0 );
   50256               0 :   btreeParseCellPtr(pPage, pCell, &info);
   50257               0 :   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
   50258               0 :   if( info.iOverflow ){
   50259               0 :     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
   50260               0 :     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
   50261                 :   }
   50262                 : }
   50263                 : #endif
   50264                 : 
   50265                 : 
   50266                 : /*
   50267                 : ** Defragment the page given.  All Cells are moved to the
   50268                 : ** end of the page and all free space is collected into one
   50269                 : ** big FreeBlk that occurs in between the header and cell
   50270                 : ** pointer array and the cell content area.
   50271                 : */
   50272             301 : static int defragmentPage(MemPage *pPage){
   50273                 :   int i;                     /* Loop counter */
   50274                 :   int pc;                    /* Address of a i-th cell */
   50275                 :   int hdr;                   /* Offset to the page header */
   50276                 :   int size;                  /* Size of a cell */
   50277                 :   int usableSize;            /* Number of usable bytes on a page */
   50278                 :   int cellOffset;            /* Offset to the cell pointer array */
   50279                 :   int cbrk;                  /* Offset to the cell content area */
   50280                 :   int nCell;                 /* Number of cells on the page */
   50281                 :   unsigned char *data;       /* The page data */
   50282                 :   unsigned char *temp;       /* Temp area for cell content */
   50283                 :   int iCellFirst;            /* First allowable cell index */
   50284                 :   int iCellLast;             /* Last possible cell index */
   50285                 : 
   50286                 : 
   50287             301 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   50288             301 :   assert( pPage->pBt!=0 );
   50289             301 :   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
   50290             301 :   assert( pPage->nOverflow==0 );
   50291             301 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50292             301 :   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
   50293             301 :   data = pPage->aData;
   50294             301 :   hdr = pPage->hdrOffset;
   50295             301 :   cellOffset = pPage->cellOffset;
   50296             301 :   nCell = pPage->nCell;
   50297             301 :   assert( nCell==get2byte(&data[hdr+3]) );
   50298             301 :   usableSize = pPage->pBt->usableSize;
   50299             301 :   cbrk = get2byte(&data[hdr+5]);
   50300             301 :   memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
   50301             301 :   cbrk = usableSize;
   50302             301 :   iCellFirst = cellOffset + 2*nCell;
   50303             301 :   iCellLast = usableSize - 4;
   50304            9793 :   for(i=0; i<nCell; i++){
   50305                 :     u8 *pAddr;     /* The i-th cell pointer */
   50306            9492 :     pAddr = &data[cellOffset + i*2];
   50307            9492 :     pc = get2byte(pAddr);
   50308                 :     testcase( pc==iCellFirst );
   50309                 :     testcase( pc==iCellLast );
   50310                 : #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
   50311                 :     /* These conditions have already been verified in btreeInitPage()
   50312                 :     ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined 
   50313                 :     */
   50314            9492 :     if( pc<iCellFirst || pc>iCellLast ){
   50315               0 :       return SQLITE_CORRUPT_BKPT;
   50316                 :     }
   50317                 : #endif
   50318            9492 :     assert( pc>=iCellFirst && pc<=iCellLast );
   50319            9492 :     size = cellSizePtr(pPage, &temp[pc]);
   50320            9492 :     cbrk -= size;
   50321                 : #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
   50322                 :     if( cbrk<iCellFirst ){
   50323                 :       return SQLITE_CORRUPT_BKPT;
   50324                 :     }
   50325                 : #else
   50326            9492 :     if( cbrk<iCellFirst || pc+size>usableSize ){
   50327               0 :       return SQLITE_CORRUPT_BKPT;
   50328                 :     }
   50329                 : #endif
   50330            9492 :     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
   50331                 :     testcase( cbrk+size==usableSize );
   50332                 :     testcase( pc+size==usableSize );
   50333            9492 :     memcpy(&data[cbrk], &temp[pc], size);
   50334            9492 :     put2byte(pAddr, cbrk);
   50335                 :   }
   50336             301 :   assert( cbrk>=iCellFirst );
   50337             301 :   put2byte(&data[hdr+5], cbrk);
   50338             301 :   data[hdr+1] = 0;
   50339             301 :   data[hdr+2] = 0;
   50340             301 :   data[hdr+7] = 0;
   50341             301 :   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
   50342             301 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   50343             301 :   if( cbrk-iCellFirst!=pPage->nFree ){
   50344               0 :     return SQLITE_CORRUPT_BKPT;
   50345                 :   }
   50346             301 :   return SQLITE_OK;
   50347                 : }
   50348                 : 
   50349                 : /*
   50350                 : ** Allocate nByte bytes of space from within the B-Tree page passed
   50351                 : ** as the first argument. Write into *pIdx the index into pPage->aData[]
   50352                 : ** of the first byte of allocated space. Return either SQLITE_OK or
   50353                 : ** an error code (usually SQLITE_CORRUPT).
   50354                 : **
   50355                 : ** The caller guarantees that there is sufficient space to make the
   50356                 : ** allocation.  This routine might need to defragment in order to bring
   50357                 : ** all the space together, however.  This routine will avoid using
   50358                 : ** the first two bytes past the cell pointer area since presumably this
   50359                 : ** allocation is being made in order to insert a new cell, so we will
   50360                 : ** also end up needing a new cell pointer.
   50361                 : */
   50362          231046 : static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
   50363          231046 :   const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
   50364          231046 :   u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
   50365                 :   int nFrag;                           /* Number of fragmented bytes on pPage */
   50366                 :   int top;                             /* First byte of cell content area */
   50367                 :   int gap;        /* First byte of gap between cell pointers and cell content */
   50368                 :   int rc;         /* Integer return code */
   50369                 :   int usableSize; /* Usable size of the page */
   50370                 :   
   50371          231046 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   50372          231046 :   assert( pPage->pBt );
   50373          231046 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50374          231046 :   assert( nByte>=0 );  /* Minimum cell size is 4 */
   50375          231046 :   assert( pPage->nFree>=nByte );
   50376          231046 :   assert( pPage->nOverflow==0 );
   50377          231046 :   usableSize = pPage->pBt->usableSize;
   50378          231046 :   assert( nByte < usableSize-8 );
   50379                 : 
   50380          231046 :   nFrag = data[hdr+7];
   50381          231046 :   assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
   50382          231046 :   gap = pPage->cellOffset + 2*pPage->nCell;
   50383          231046 :   top = get2byteNotZero(&data[hdr+5]);
   50384          231046 :   if( gap>top ) return SQLITE_CORRUPT_BKPT;
   50385                 :   testcase( gap+2==top );
   50386                 :   testcase( gap+1==top );
   50387                 :   testcase( gap==top );
   50388                 : 
   50389          231046 :   if( nFrag>=60 ){
   50390                 :     /* Always defragment highly fragmented pages */
   50391               9 :     rc = defragmentPage(pPage);
   50392               9 :     if( rc ) return rc;
   50393               9 :     top = get2byteNotZero(&data[hdr+5]);
   50394          231037 :   }else if( gap+2<=top ){
   50395                 :     /* Search the freelist looking for a free slot big enough to satisfy 
   50396                 :     ** the request. The allocation is made from the first free slot in 
   50397                 :     ** the list that is large enough to accomadate it.
   50398                 :     */
   50399                 :     int pc, addr;
   50400          254000 :     for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
   50401                 :       int size;            /* Size of the free slot */
   50402           51176 :       if( pc>usableSize-4 || pc<addr+4 ){
   50403               0 :         return SQLITE_CORRUPT_BKPT;
   50404                 :       }
   50405           51176 :       size = get2byte(&data[pc+2]);
   50406           51176 :       if( size>=nByte ){
   50407           28213 :         int x = size - nByte;
   50408                 :         testcase( x==4 );
   50409                 :         testcase( x==3 );
   50410           28213 :         if( x<4 ){
   50411                 :           /* Remove the slot from the free-list. Update the number of
   50412                 :           ** fragmented bytes within the page. */
   50413           21668 :           memcpy(&data[addr], &data[pc], 2);
   50414           21668 :           data[hdr+7] = (u8)(nFrag + x);
   50415            6545 :         }else if( size+pc > usableSize ){
   50416               0 :           return SQLITE_CORRUPT_BKPT;
   50417                 :         }else{
   50418                 :           /* The slot remains on the free-list. Reduce its size to account
   50419                 :           ** for the portion used by the new allocation. */
   50420            6545 :           put2byte(&data[pc+2], x);
   50421                 :         }
   50422           28213 :         *pIdx = pc + x;
   50423           28213 :         return SQLITE_OK;
   50424                 :       }
   50425                 :     }
   50426                 :   }
   50427                 : 
   50428                 :   /* Check to make sure there is enough space in the gap to satisfy
   50429                 :   ** the allocation.  If not, defragment.
   50430                 :   */
   50431                 :   testcase( gap+2+nByte==top );
   50432          202833 :   if( gap+2+nByte>top ){
   50433             292 :     rc = defragmentPage(pPage);
   50434             292 :     if( rc ) return rc;
   50435             292 :     top = get2byteNotZero(&data[hdr+5]);
   50436             292 :     assert( gap+nByte<=top );
   50437                 :   }
   50438                 : 
   50439                 : 
   50440                 :   /* Allocate memory from the gap in between the cell pointer array
   50441                 :   ** and the cell content area.  The btreeInitPage() call has already
   50442                 :   ** validated the freelist.  Given that the freelist is valid, there
   50443                 :   ** is no way that the allocation can extend off the end of the page.
   50444                 :   ** The assert() below verifies the previous sentence.
   50445                 :   */
   50446          202833 :   top -= nByte;
   50447          202833 :   put2byte(&data[hdr+5], top);
   50448          202833 :   assert( top+nByte <= (int)pPage->pBt->usableSize );
   50449          202833 :   *pIdx = top;
   50450          202833 :   return SQLITE_OK;
   50451                 : }
   50452                 : 
   50453                 : /*
   50454                 : ** Return a section of the pPage->aData to the freelist.
   50455                 : ** The first byte of the new free block is pPage->aDisk[start]
   50456                 : ** and the size of the block is "size" bytes.
   50457                 : **
   50458                 : ** Most of the effort here is involved in coalesing adjacent
   50459                 : ** free blocks into a single big free block.
   50460                 : */
   50461           93232 : static int freeSpace(MemPage *pPage, int start, int size){
   50462                 :   int addr, pbegin, hdr;
   50463                 :   int iLast;                        /* Largest possible freeblock offset */
   50464           93232 :   unsigned char *data = pPage->aData;
   50465                 : 
   50466           93232 :   assert( pPage->pBt!=0 );
   50467           93232 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   50468           93232 :   assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
   50469           93232 :   assert( (start + size) <= (int)pPage->pBt->usableSize );
   50470           93232 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50471           93232 :   assert( size>=0 );   /* Minimum cell size is 4 */
   50472                 : 
   50473           93232 :   if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
   50474                 :     /* Overwrite deleted information with zeros when the secure_delete
   50475                 :     ** option is enabled */
   50476           93232 :     memset(&data[start], 0, size);
   50477                 :   }
   50478                 : 
   50479                 :   /* Add the space back into the linked list of freeblocks.  Note that
   50480                 :   ** even though the freeblock list was checked by btreeInitPage(),
   50481                 :   ** btreeInitPage() did not detect overlapping cells or
   50482                 :   ** freeblocks that overlapped cells.   Nor does it detect when the
   50483                 :   ** cell content area exceeds the value in the page header.  If these
   50484                 :   ** situations arise, then subsequent insert operations might corrupt
   50485                 :   ** the freelist.  So we do need to check for corruption while scanning
   50486                 :   ** the freelist.
   50487                 :   */
   50488           93232 :   hdr = pPage->hdrOffset;
   50489           93232 :   addr = hdr + 1;
   50490           93232 :   iLast = pPage->pBt->usableSize - 4;
   50491           93232 :   assert( start<=iLast );
   50492          231801 :   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
   50493           45337 :     if( pbegin<addr+4 ){
   50494               0 :       return SQLITE_CORRUPT_BKPT;
   50495                 :     }
   50496           45337 :     addr = pbegin;
   50497                 :   }
   50498           93232 :   if( pbegin>iLast ){
   50499               0 :     return SQLITE_CORRUPT_BKPT;
   50500                 :   }
   50501           93232 :   assert( pbegin>addr || pbegin==0 );
   50502           93232 :   put2byte(&data[addr], start);
   50503           93232 :   put2byte(&data[start], pbegin);
   50504           93232 :   put2byte(&data[start+2], size);
   50505           93232 :   pPage->nFree = pPage->nFree + (u16)size;
   50506                 : 
   50507                 :   /* Coalesce adjacent free blocks */
   50508           93232 :   addr = hdr + 1;
   50509          413446 :   while( (pbegin = get2byte(&data[addr]))>0 ){
   50510                 :     int pnext, psize, x;
   50511          226982 :     assert( pbegin>addr );
   50512          226982 :     assert( pbegin <= (int)pPage->pBt->usableSize-4 );
   50513          226982 :     pnext = get2byte(&data[pbegin]);
   50514          226982 :     psize = get2byte(&data[pbegin+2]);
   50515          253466 :     if( pbegin + psize + 3 >= pnext && pnext>0 ){
   50516           26484 :       int frag = pnext - (pbegin+psize);
   50517           26484 :       if( (frag<0) || (frag>(int)data[hdr+7]) ){
   50518               0 :         return SQLITE_CORRUPT_BKPT;
   50519                 :       }
   50520           26484 :       data[hdr+7] -= (u8)frag;
   50521           26484 :       x = get2byte(&data[pnext]);
   50522           26484 :       put2byte(&data[pbegin], x);
   50523           26484 :       x = pnext + get2byte(&data[pnext+2]) - pbegin;
   50524           26484 :       put2byte(&data[pbegin+2], x);
   50525                 :     }else{
   50526          200498 :       addr = pbegin;
   50527                 :     }
   50528                 :   }
   50529                 : 
   50530                 :   /* If the cell content area begins with a freeblock, remove it. */
   50531           93232 :   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
   50532                 :     int top;
   50533           42515 :     pbegin = get2byte(&data[hdr+1]);
   50534           42515 :     memcpy(&data[hdr+1], &data[pbegin], 2);
   50535           42515 :     top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
   50536           42515 :     put2byte(&data[hdr+5], top);
   50537                 :   }
   50538           93232 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   50539           93232 :   return SQLITE_OK;
   50540                 : }
   50541                 : 
   50542                 : /*
   50543                 : ** Decode the flags byte (the first byte of the header) for a page
   50544                 : ** and initialize fields of the MemPage structure accordingly.
   50545                 : **
   50546                 : ** Only the following combinations are supported.  Anything different
   50547                 : ** indicates a corrupt database files:
   50548                 : **
   50549                 : **         PTF_ZERODATA
   50550                 : **         PTF_ZERODATA | PTF_LEAF
   50551                 : **         PTF_LEAFDATA | PTF_INTKEY
   50552                 : **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
   50553                 : */
   50554           62890 : static int decodeFlags(MemPage *pPage, int flagByte){
   50555                 :   BtShared *pBt;     /* A copy of pPage->pBt */
   50556                 : 
   50557           62890 :   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
   50558           62890 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50559           62890 :   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
   50560           62890 :   flagByte &= ~PTF_LEAF;
   50561           62890 :   pPage->childPtrSize = 4-4*pPage->leaf;
   50562           62890 :   pBt = pPage->pBt;
   50563           62890 :   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
   50564           32690 :     pPage->intKey = 1;
   50565           32690 :     pPage->hasData = pPage->leaf;
   50566           32690 :     pPage->maxLocal = pBt->maxLeaf;
   50567           32690 :     pPage->minLocal = pBt->minLeaf;
   50568           30200 :   }else if( flagByte==PTF_ZERODATA ){
   50569           30198 :     pPage->intKey = 0;
   50570           30198 :     pPage->hasData = 0;
   50571           30198 :     pPage->maxLocal = pBt->maxLocal;
   50572           30198 :     pPage->minLocal = pBt->minLocal;
   50573                 :   }else{
   50574               2 :     return SQLITE_CORRUPT_BKPT;
   50575                 :   }
   50576           62888 :   pPage->max1bytePayload = pBt->max1bytePayload;
   50577           62888 :   return SQLITE_OK;
   50578                 : }
   50579                 : 
   50580                 : /*
   50581                 : ** Initialize the auxiliary information for a disk block.
   50582                 : **
   50583                 : ** Return SQLITE_OK on success.  If we see that the page does
   50584                 : ** not contain a well-formed database page, then return 
   50585                 : ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
   50586                 : ** guarantee that the page is well-formed.  It only shows that
   50587                 : ** we failed to detect any corruption.
   50588                 : */
   50589          484782 : static int btreeInitPage(MemPage *pPage){
   50590                 : 
   50591          484782 :   assert( pPage->pBt!=0 );
   50592          484782 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50593          484782 :   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
   50594          484782 :   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
   50595          484782 :   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
   50596                 : 
   50597          484782 :   if( !pPage->isInit ){
   50598                 :     u16 pc;            /* Address of a freeblock within pPage->aData[] */
   50599                 :     u8 hdr;            /* Offset to beginning of page header */
   50600                 :     u8 *data;          /* Equal to pPage->aData */
   50601                 :     BtShared *pBt;        /* The main btree structure */
   50602                 :     int usableSize;    /* Amount of usable space on each page */
   50603                 :     u16 cellOffset;    /* Offset from start of page to first cell pointer */
   50604                 :     int nFree;         /* Number of unused bytes on the page */
   50605                 :     int top;           /* First byte of the cell content area */
   50606                 :     int iCellFirst;    /* First allowable cell or freeblock offset */
   50607                 :     int iCellLast;     /* Last possible cell or freeblock offset */
   50608                 : 
   50609           13174 :     pBt = pPage->pBt;
   50610                 : 
   50611           13174 :     hdr = pPage->hdrOffset;
   50612           13174 :     data = pPage->aData;
   50613           13174 :     if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
   50614           13172 :     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
   50615           13172 :     pPage->maskPage = (u16)(pBt->pageSize - 1);
   50616           13172 :     pPage->nOverflow = 0;
   50617           13172 :     usableSize = pBt->usableSize;
   50618           13172 :     pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
   50619           13172 :     pPage->aDataEnd = &data[usableSize];
   50620           13172 :     pPage->aCellIdx = &data[cellOffset];
   50621           13172 :     top = get2byteNotZero(&data[hdr+5]);
   50622           13172 :     pPage->nCell = get2byte(&data[hdr+3]);
   50623           13172 :     if( pPage->nCell>MX_CELL(pBt) ){
   50624                 :       /* To many cells for a single page.  The page must be corrupt */
   50625               0 :       return SQLITE_CORRUPT_BKPT;
   50626                 :     }
   50627                 :     testcase( pPage->nCell==MX_CELL(pBt) );
   50628                 : 
   50629                 :     /* A malformed database page might cause us to read past the end
   50630                 :     ** of page when parsing a cell.  
   50631                 :     **
   50632                 :     ** The following block of code checks early to see if a cell extends
   50633                 :     ** past the end of a page boundary and causes SQLITE_CORRUPT to be 
   50634                 :     ** returned if it does.
   50635                 :     */
   50636           13172 :     iCellFirst = cellOffset + 2*pPage->nCell;
   50637           13172 :     iCellLast = usableSize - 4;
   50638                 : #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
   50639                 :     {
   50640                 :       int i;            /* Index into the cell pointer array */
   50641                 :       int sz;           /* Size of a cell */
   50642                 : 
   50643                 :       if( !pPage->leaf ) iCellLast--;
   50644                 :       for(i=0; i<pPage->nCell; i++){
   50645                 :         pc = get2byte(&data[cellOffset+i*2]);
   50646                 :         testcase( pc==iCellFirst );
   50647                 :         testcase( pc==iCellLast );
   50648                 :         if( pc<iCellFirst || pc>iCellLast ){
   50649                 :           return SQLITE_CORRUPT_BKPT;
   50650                 :         }
   50651                 :         sz = cellSizePtr(pPage, &data[pc]);
   50652                 :         testcase( pc+sz==usableSize );
   50653                 :         if( pc+sz>usableSize ){
   50654                 :           return SQLITE_CORRUPT_BKPT;
   50655                 :         }
   50656                 :       }
   50657                 :       if( !pPage->leaf ) iCellLast++;
   50658                 :     }  
   50659                 : #endif
   50660                 : 
   50661                 :     /* Compute the total free space on the page */
   50662           13172 :     pc = get2byte(&data[hdr+1]);
   50663           13172 :     nFree = data[hdr+7] + top;
   50664           28929 :     while( pc>0 ){
   50665                 :       u16 next, size;
   50666            2585 :       if( pc<iCellFirst || pc>iCellLast ){
   50667                 :         /* Start of free block is off the page */
   50668               0 :         return SQLITE_CORRUPT_BKPT; 
   50669                 :       }
   50670            2585 :       next = get2byte(&data[pc]);
   50671            2585 :       size = get2byte(&data[pc+2]);
   50672            2585 :       if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
   50673                 :         /* Free blocks must be in ascending order. And the last byte of
   50674                 :         ** the free-block must lie on the database page.  */
   50675               0 :         return SQLITE_CORRUPT_BKPT; 
   50676                 :       }
   50677            2585 :       nFree = nFree + size;
   50678            2585 :       pc = next;
   50679                 :     }
   50680                 : 
   50681                 :     /* At this point, nFree contains the sum of the offset to the start
   50682                 :     ** of the cell-content area plus the number of free bytes within
   50683                 :     ** the cell-content area. If this is greater than the usable-size
   50684                 :     ** of the page, then the page must be corrupted. This check also
   50685                 :     ** serves to verify that the offset to the start of the cell-content
   50686                 :     ** area, according to the page header, lies within the page.
   50687                 :     */
   50688           13172 :     if( nFree>usableSize ){
   50689               0 :       return SQLITE_CORRUPT_BKPT; 
   50690                 :     }
   50691           13172 :     pPage->nFree = (u16)(nFree - iCellFirst);
   50692           13172 :     pPage->isInit = 1;
   50693                 :   }
   50694          484780 :   return SQLITE_OK;
   50695                 : }
   50696                 : 
   50697                 : /*
   50698                 : ** Set up a raw page so that it looks like a database page holding
   50699                 : ** no entries.
   50700                 : */
   50701           49716 : static void zeroPage(MemPage *pPage, int flags){
   50702           49716 :   unsigned char *data = pPage->aData;
   50703           49716 :   BtShared *pBt = pPage->pBt;
   50704           49716 :   u8 hdr = pPage->hdrOffset;
   50705                 :   u16 first;
   50706                 : 
   50707           49716 :   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
   50708           49716 :   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
   50709           49716 :   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
   50710           49716 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   50711           49716 :   assert( sqlite3_mutex_held(pBt->mutex) );
   50712           49716 :   if( pBt->btsFlags & BTS_SECURE_DELETE ){
   50713           49716 :     memset(&data[hdr], 0, pBt->usableSize - hdr);
   50714                 :   }
   50715           49716 :   data[hdr] = (char)flags;
   50716           49716 :   first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
   50717           49716 :   memset(&data[hdr+1], 0, 4);
   50718           49716 :   data[hdr+7] = 0;
   50719           49716 :   put2byte(&data[hdr+5], pBt->usableSize);
   50720           49716 :   pPage->nFree = (u16)(pBt->usableSize - first);
   50721           49716 :   decodeFlags(pPage, flags);
   50722           49716 :   pPage->hdrOffset = hdr;
   50723           49716 :   pPage->cellOffset = first;
   50724           49716 :   pPage->aDataEnd = &data[pBt->usableSize];
   50725           49716 :   pPage->aCellIdx = &data[first];
   50726           49716 :   pPage->nOverflow = 0;
   50727           49716 :   assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
   50728           49716 :   pPage->maskPage = (u16)(pBt->pageSize - 1);
   50729           49716 :   pPage->nCell = 0;
   50730           49716 :   pPage->isInit = 1;
   50731           49716 : }
   50732                 : 
   50733                 : 
   50734                 : /*
   50735                 : ** Convert a DbPage obtained from the pager into a MemPage used by
   50736                 : ** the btree layer.
   50737                 : */
   50738          631195 : static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
   50739          631195 :   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
   50740          631195 :   pPage->aData = sqlite3PagerGetData(pDbPage);
   50741          631195 :   pPage->pDbPage = pDbPage;
   50742          631195 :   pPage->pBt = pBt;
   50743          631195 :   pPage->pgno = pgno;
   50744          631195 :   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
   50745          631195 :   return pPage; 
   50746                 : }
   50747                 : 
   50748                 : /*
   50749                 : ** Get a page from the pager.  Initialize the MemPage.pBt and
   50750                 : ** MemPage.aData elements if needed.
   50751                 : **
   50752                 : ** If the noContent flag is set, it means that we do not care about
   50753                 : ** the content of the page at this time.  So do not go to the disk
   50754                 : ** to fetch the content.  Just fill in the content with zeros for now.
   50755                 : ** If in the future we call sqlite3PagerWrite() on this page, that
   50756                 : ** means we have started to be concerned about content and the disk
   50757                 : ** read should occur at that point.
   50758                 : */
   50759          632181 : static int btreeGetPage(
   50760                 :   BtShared *pBt,       /* The btree */
   50761                 :   Pgno pgno,           /* Number of the page to fetch */
   50762                 :   MemPage **ppPage,    /* Return the page in this parameter */
   50763                 :   int noContent        /* Do not load page content if true */
   50764                 : ){
   50765                 :   int rc;
   50766                 :   DbPage *pDbPage;
   50767                 : 
   50768          632181 :   assert( sqlite3_mutex_held(pBt->mutex) );
   50769          632181 :   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
   50770          632181 :   if( rc ) return rc;
   50771          631195 :   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
   50772          631195 :   return SQLITE_OK;
   50773                 : }
   50774                 : 
   50775                 : /*
   50776                 : ** Retrieve a page from the pager cache. If the requested page is not
   50777                 : ** already in the pager cache return NULL. Initialize the MemPage.pBt and
   50778                 : ** MemPage.aData elements if needed.
   50779                 : */
   50780               0 : static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
   50781                 :   DbPage *pDbPage;
   50782               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   50783               0 :   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
   50784               0 :   if( pDbPage ){
   50785               0 :     return btreePageFromDbPage(pDbPage, pgno, pBt);
   50786                 :   }
   50787               0 :   return 0;
   50788                 : }
   50789                 : 
   50790                 : /*
   50791                 : ** Return the size of the database file in pages. If there is any kind of
   50792                 : ** error, return ((unsigned int)-1).
   50793                 : */
   50794          561930 : static Pgno btreePagecount(BtShared *pBt){
   50795          561930 :   return pBt->nPage;
   50796                 : }
   50797              13 : SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
   50798              13 :   assert( sqlite3BtreeHoldsMutex(p) );
   50799              13 :   assert( ((p->pBt->nPage)&0x8000000)==0 );
   50800              13 :   return (int)btreePagecount(p->pBt);
   50801                 : }
   50802                 : 
   50803                 : /*
   50804                 : ** Get a page from the pager and initialize it.  This routine is just a
   50805                 : ** convenience wrapper around separate calls to btreeGetPage() and 
   50806                 : ** btreeInitPage().
   50807                 : **
   50808                 : ** If an error occurs, then the value *ppPage is set to is undefined. It
   50809                 : ** may remain unchanged, or it may be set to an invalid value.
   50810                 : */
   50811          483765 : static int getAndInitPage(
   50812                 :   BtShared *pBt,          /* The database file */
   50813                 :   Pgno pgno,           /* Number of the page to get */
   50814                 :   MemPage **ppPage     /* Write the page pointer here */
   50815                 : ){
   50816                 :   int rc;
   50817          483765 :   assert( sqlite3_mutex_held(pBt->mutex) );
   50818                 : 
   50819          483765 :   if( pgno>btreePagecount(pBt) ){
   50820               0 :     rc = SQLITE_CORRUPT_BKPT;
   50821                 :   }else{
   50822          483765 :     rc = btreeGetPage(pBt, pgno, ppPage, 0);
   50823          483765 :     if( rc==SQLITE_OK ){
   50824          483765 :       rc = btreeInitPage(*ppPage);
   50825          483765 :       if( rc!=SQLITE_OK ){
   50826               2 :         releasePage(*ppPage);
   50827                 :       }
   50828                 :     }
   50829                 :   }
   50830                 : 
   50831                 :   testcase( pgno==0 );
   50832          483765 :   assert( pgno!=0 || rc==SQLITE_CORRUPT );
   50833          483765 :   return rc;
   50834                 : }
   50835                 : 
   50836                 : /*
   50837                 : ** Release a MemPage.  This should be called once for each prior
   50838                 : ** call to btreeGetPage.
   50839                 : */
   50840          893495 : static void releasePage(MemPage *pPage){
   50841          893495 :   if( pPage ){
   50842          600626 :     assert( pPage->aData );
   50843          600626 :     assert( pPage->pBt );
   50844          600626 :     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
   50845          600626 :     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
   50846          600626 :     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50847          600626 :     sqlite3PagerUnref(pPage->pDbPage);
   50848                 :   }
   50849          893495 : }
   50850                 : 
   50851                 : /*
   50852                 : ** During a rollback, when the pager reloads information into the cache
   50853                 : ** so that the cache is restored to its original state at the start of
   50854                 : ** the transaction, for each page restored this routine is called.
   50855                 : **
   50856                 : ** This routine needs to reset the extra data section at the end of the
   50857                 : ** page to agree with the restored data.
   50858                 : */
   50859              31 : static void pageReinit(DbPage *pData){
   50860                 :   MemPage *pPage;
   50861              31 :   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
   50862              31 :   assert( sqlite3PagerPageRefcount(pData)>0 );
   50863              31 :   if( pPage->isInit ){
   50864              31 :     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   50865              31 :     pPage->isInit = 0;
   50866              31 :     if( sqlite3PagerPageRefcount(pData)>1 ){
   50867                 :       /* pPage might not be a btree page;  it might be an overflow page
   50868                 :       ** or ptrmap page or a free page.  In those cases, the following
   50869                 :       ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
   50870                 :       ** But no harm is done by this.  And it is very important that
   50871                 :       ** btreeInitPage() be called on every btree page so we make
   50872                 :       ** the call for every page that comes in for re-initing. */
   50873               2 :       btreeInitPage(pPage);
   50874                 :     }
   50875                 :   }
   50876              31 : }
   50877                 : 
   50878                 : /*
   50879                 : ** Invoke the busy handler for a btree.
   50880                 : */
   50881               2 : static int btreeInvokeBusyHandler(void *pArg){
   50882               2 :   BtShared *pBt = (BtShared*)pArg;
   50883               2 :   assert( pBt->db );
   50884               2 :   assert( sqlite3_mutex_held(pBt->db->mutex) );
   50885               2 :   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
   50886                 : }
   50887                 : 
   50888                 : /*
   50889                 : ** Open a database file.
   50890                 : ** 
   50891                 : ** zFilename is the name of the database file.  If zFilename is NULL
   50892                 : ** then an ephemeral database is created.  The ephemeral database might
   50893                 : ** be exclusively in memory, or it might use a disk-based memory cache.
   50894                 : ** Either way, the ephemeral database will be automatically deleted 
   50895                 : ** when sqlite3BtreeClose() is called.
   50896                 : **
   50897                 : ** If zFilename is ":memory:" then an in-memory database is created
   50898                 : ** that is automatically destroyed when it is closed.
   50899                 : **
   50900                 : ** The "flags" parameter is a bitmask that might contain bits
   50901                 : ** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK.  The BTREE_NO_READLOCK
   50902                 : ** bit is also set if the SQLITE_NoReadlock flags is set in db->flags.
   50903                 : ** These flags are passed through into sqlite3PagerOpen() and must
   50904                 : ** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK.
   50905                 : **
   50906                 : ** If the database is already opened in the same database connection
   50907                 : ** and we are in shared cache mode, then the open will fail with an
   50908                 : ** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
   50909                 : ** objects in the same database connection since doing so will lead
   50910                 : ** to problems with locking.
   50911                 : */
   50912           19697 : SQLITE_PRIVATE int sqlite3BtreeOpen(
   50913                 :   sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
   50914                 :   const char *zFilename,  /* Name of the file containing the BTree database */
   50915                 :   sqlite3 *db,            /* Associated database handle */
   50916                 :   Btree **ppBtree,        /* Pointer to new Btree object written here */
   50917                 :   int flags,              /* Options */
   50918                 :   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
   50919                 : ){
   50920           19697 :   BtShared *pBt = 0;             /* Shared part of btree structure */
   50921                 :   Btree *p;                      /* Handle to return */
   50922           19697 :   sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
   50923           19697 :   int rc = SQLITE_OK;            /* Result code from this function */
   50924                 :   u8 nReserve;                   /* Byte of unused space on each page */
   50925                 :   unsigned char zDbHeader[100];  /* Database header content */
   50926                 : 
   50927                 :   /* True if opening an ephemeral, temporary database */
   50928           19697 :   const int isTempDb = zFilename==0 || zFilename[0]==0;
   50929                 : 
   50930                 :   /* Set the variable isMemdb to true for an in-memory database, or 
   50931                 :   ** false for a file-based database.
   50932                 :   */
   50933                 : #ifdef SQLITE_OMIT_MEMORYDB
   50934                 :   const int isMemdb = 0;
   50935                 : #else
   50936           35155 :   const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
   50937           19647 :                        || (isTempDb && sqlite3TempInMemory(db));
   50938                 : #endif
   50939                 : 
   50940           19697 :   assert( db!=0 );
   50941           19697 :   assert( pVfs!=0 );
   50942           19697 :   assert( sqlite3_mutex_held(db->mutex) );
   50943           19697 :   assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
   50944                 : 
   50945                 :   /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
   50946           19697 :   assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
   50947                 : 
   50948                 :   /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
   50949           19697 :   assert( (flags & BTREE_SINGLE)==0 || isTempDb );
   50950                 : 
   50951           19697 :   if( db->flags & SQLITE_NoReadlock ){
   50952               0 :     flags |= BTREE_NO_READLOCK;
   50953                 :   }
   50954           19697 :   if( isMemdb ){
   50955           15458 :     flags |= BTREE_MEMORY;
   50956                 :   }
   50957           19697 :   if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
   50958              55 :     vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
   50959                 :   }
   50960           19697 :   p = sqlite3MallocZero(sizeof(Btree));
   50961           19697 :   if( !p ){
   50962               0 :     return SQLITE_NOMEM;
   50963                 :   }
   50964           19697 :   p->inTrans = TRANS_NONE;
   50965           19697 :   p->db = db;
   50966                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   50967           19697 :   p->lock.pBtree = p;
   50968           19697 :   p->lock.iTable = 1;
   50969                 : #endif
   50970                 : 
   50971                 : #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
   50972                 :   /*
   50973                 :   ** If this Btree is a candidate for shared cache, try to find an
   50974                 :   ** existing BtShared object that we can share with
   50975                 :   */
   50976           19697 :   if( isMemdb==0 && isTempDb==0 ){
   50977            3232 :     if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
   50978            1456 :       int nFullPathname = pVfs->mxPathname+1;
   50979            1456 :       char *zFullPathname = sqlite3Malloc(nFullPathname);
   50980                 :       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
   50981            1456 :       p->sharable = 1;
   50982            1456 :       if( !zFullPathname ){
   50983               0 :         sqlite3_free(p);
   50984               0 :         return SQLITE_NOMEM;
   50985                 :       }
   50986            1456 :       rc = sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
   50987            1456 :       if( rc ){
   50988               0 :         sqlite3_free(zFullPathname);
   50989               0 :         sqlite3_free(p);
   50990               0 :         return rc;
   50991                 :       }
   50992                 : #if SQLITE_THREADSAFE
   50993            1456 :       mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
   50994            1456 :       sqlite3_mutex_enter(mutexOpen);
   50995            1456 :       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   50996            1456 :       sqlite3_mutex_enter(mutexShared);
   50997                 : #endif
   50998            2150 :       for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
   50999             870 :         assert( pBt->nRef>0 );
   51000             870 :         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
   51001             176 :                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
   51002                 :           int iDb;
   51003             528 :           for(iDb=db->nDb-1; iDb>=0; iDb--){
   51004             352 :             Btree *pExisting = db->aDb[iDb].pBt;
   51005             352 :             if( pExisting && pExisting->pBt==pBt ){
   51006               0 :               sqlite3_mutex_leave(mutexShared);
   51007               0 :               sqlite3_mutex_leave(mutexOpen);
   51008               0 :               sqlite3_free(zFullPathname);
   51009               0 :               sqlite3_free(p);
   51010               0 :               return SQLITE_CONSTRAINT;
   51011                 :             }
   51012                 :           }
   51013             176 :           p->pBt = pBt;
   51014             176 :           pBt->nRef++;
   51015             176 :           break;
   51016                 :         }
   51017                 :       }
   51018            1456 :       sqlite3_mutex_leave(mutexShared);
   51019            1456 :       sqlite3_free(zFullPathname);
   51020                 :     }
   51021                 : #ifdef SQLITE_DEBUG
   51022                 :     else{
   51023                 :       /* In debug mode, we mark all persistent databases as sharable
   51024                 :       ** even when they are not.  This exercises the locking code and
   51025                 :       ** gives more opportunity for asserts(sqlite3_mutex_held())
   51026                 :       ** statements to find locking problems.
   51027                 :       */
   51028            1776 :       p->sharable = 1;
   51029                 :     }
   51030                 : #endif
   51031                 :   }
   51032                 : #endif
   51033           19697 :   if( pBt==0 ){
   51034                 :     /*
   51035                 :     ** The following asserts make sure that structures used by the btree are
   51036                 :     ** the right size.  This is to guard against size changes that result
   51037                 :     ** when compiling on a different architecture.
   51038                 :     */
   51039                 :     assert( sizeof(i64)==8 || sizeof(i64)==4 );
   51040                 :     assert( sizeof(u64)==8 || sizeof(u64)==4 );
   51041                 :     assert( sizeof(u32)==4 );
   51042                 :     assert( sizeof(u16)==2 );
   51043                 :     assert( sizeof(Pgno)==4 );
   51044                 :   
   51045           19521 :     pBt = sqlite3MallocZero( sizeof(*pBt) );
   51046           19521 :     if( pBt==0 ){
   51047               0 :       rc = SQLITE_NOMEM;
   51048               0 :       goto btree_open_out;
   51049                 :     }
   51050           19521 :     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
   51051                 :                           EXTRA_SIZE, flags, vfsFlags, pageReinit);
   51052           19521 :     if( rc==SQLITE_OK ){
   51053           19517 :       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
   51054                 :     }
   51055           19521 :     if( rc!=SQLITE_OK ){
   51056               4 :       goto btree_open_out;
   51057                 :     }
   51058           19517 :     pBt->openFlags = (u8)flags;
   51059           19517 :     pBt->db = db;
   51060           19517 :     sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
   51061           19517 :     p->pBt = pBt;
   51062                 :   
   51063           19517 :     pBt->pCursor = 0;
   51064           19517 :     pBt->pPage1 = 0;
   51065           19517 :     if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
   51066                 : #ifdef SQLITE_SECURE_DELETE
   51067           19517 :     pBt->btsFlags |= BTS_SECURE_DELETE;
   51068                 : #endif
   51069           19517 :     pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
   51070           19517 :     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
   51071            1646 :          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
   51072           17871 :       pBt->pageSize = 0;
   51073                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   51074                 :       /* If the magic name ":memory:" will create an in-memory database, then
   51075                 :       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
   51076                 :       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
   51077                 :       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
   51078                 :       ** regular file-name. In this case the auto-vacuum applies as per normal.
   51079                 :       */
   51080           17871 :       if( zFilename && !isMemdb ){
   51081            1411 :         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
   51082            1411 :         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
   51083                 :       }
   51084                 : #endif
   51085           17871 :       nReserve = 0;
   51086                 :     }else{
   51087            1646 :       nReserve = zDbHeader[20];
   51088            1646 :       pBt->btsFlags |= BTS_PAGESIZE_FIXED;
   51089                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   51090            1646 :       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
   51091            1646 :       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
   51092                 : #endif
   51093                 :     }
   51094           19517 :     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
   51095           19517 :     if( rc ) goto btree_open_out;
   51096           19517 :     pBt->usableSize = pBt->pageSize - nReserve;
   51097           19517 :     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
   51098                 :    
   51099                 : #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
   51100                 :     /* Add the new BtShared object to the linked list sharable BtShareds.
   51101                 :     */
   51102           19517 :     if( p->sharable ){
   51103                 :       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
   51104            3052 :       pBt->nRef = 1;
   51105            3052 :       MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
   51106            3052 :       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
   51107            3052 :         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
   51108            3052 :         if( pBt->mutex==0 ){
   51109               0 :           rc = SQLITE_NOMEM;
   51110               0 :           db->mallocFailed = 0;
   51111               0 :           goto btree_open_out;
   51112                 :         }
   51113                 :       }
   51114            3052 :       sqlite3_mutex_enter(mutexShared);
   51115            3052 :       pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
   51116            3052 :       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
   51117            3052 :       sqlite3_mutex_leave(mutexShared);
   51118                 :     }
   51119                 : #endif
   51120                 :   }
   51121                 : 
   51122                 : #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
   51123                 :   /* If the new Btree uses a sharable pBtShared, then link the new
   51124                 :   ** Btree into the list of all sharable Btrees for the same connection.
   51125                 :   ** The list is kept in ascending order by pBt address.
   51126                 :   */
   51127           19693 :   if( p->sharable ){
   51128                 :     int i;
   51129                 :     Btree *pSib;
   51130            9684 :     for(i=0; i<db->nDb; i++){
   51131            6456 :       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
   51132               0 :         while( pSib->pPrev ){ pSib = pSib->pPrev; }
   51133               0 :         if( p->pBt<pSib->pBt ){
   51134               0 :           p->pNext = pSib;
   51135               0 :           p->pPrev = 0;
   51136               0 :           pSib->pPrev = p;
   51137                 :         }else{
   51138               0 :           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
   51139               0 :             pSib = pSib->pNext;
   51140                 :           }
   51141               0 :           p->pNext = pSib->pNext;
   51142               0 :           p->pPrev = pSib;
   51143               0 :           if( p->pNext ){
   51144               0 :             p->pNext->pPrev = p;
   51145                 :           }
   51146               0 :           pSib->pNext = p;
   51147                 :         }
   51148               0 :         break;
   51149                 :       }
   51150                 :     }
   51151                 :   }
   51152                 : #endif
   51153           19693 :   *ppBtree = p;
   51154                 : 
   51155                 : btree_open_out:
   51156           19697 :   if( rc!=SQLITE_OK ){
   51157               4 :     if( pBt && pBt->pPager ){
   51158               0 :       sqlite3PagerClose(pBt->pPager);
   51159                 :     }
   51160               4 :     sqlite3_free(pBt);
   51161               4 :     sqlite3_free(p);
   51162               4 :     *ppBtree = 0;
   51163                 :   }else{
   51164                 :     /* If the B-Tree was successfully opened, set the pager-cache size to the
   51165                 :     ** default value. Except, when opening on an existing shared pager-cache,
   51166                 :     ** do not change the pager-cache size.
   51167                 :     */
   51168           19693 :     if( sqlite3BtreeSchema(p, 0, 0)==0 ){
   51169           19517 :       sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
   51170                 :     }
   51171                 :   }
   51172           19697 :   if( mutexOpen ){
   51173            1456 :     assert( sqlite3_mutex_held(mutexOpen) );
   51174            1456 :     sqlite3_mutex_leave(mutexOpen);
   51175                 :   }
   51176           19697 :   return rc;
   51177                 : }
   51178                 : 
   51179                 : /*
   51180                 : ** Decrement the BtShared.nRef counter.  When it reaches zero,
   51181                 : ** remove the BtShared structure from the sharing list.  Return
   51182                 : ** true if the BtShared.nRef counter reaches zero and return
   51183                 : ** false if it is still positive.
   51184                 : */
   51185            3228 : static int removeFromSharingList(BtShared *pBt){
   51186                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   51187                 :   MUTEX_LOGIC( sqlite3_mutex *pMaster; )
   51188                 :   BtShared *pList;
   51189            3228 :   int removed = 0;
   51190                 : 
   51191            3228 :   assert( sqlite3_mutex_notheld(pBt->mutex) );
   51192            3228 :   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
   51193            3228 :   sqlite3_mutex_enter(pMaster);
   51194            3228 :   pBt->nRef--;
   51195            3228 :   if( pBt->nRef<=0 ){
   51196            3052 :     if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
   51197            2643 :       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
   51198                 :     }else{
   51199             409 :       pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
   51200            1002 :       while( ALWAYS(pList) && pList->pNext!=pBt ){
   51201             184 :         pList=pList->pNext;
   51202                 :       }
   51203             409 :       if( ALWAYS(pList) ){
   51204             409 :         pList->pNext = pBt->pNext;
   51205                 :       }
   51206                 :     }
   51207                 :     if( SQLITE_THREADSAFE ){
   51208            3052 :       sqlite3_mutex_free(pBt->mutex);
   51209                 :     }
   51210            3052 :     removed = 1;
   51211                 :   }
   51212            3228 :   sqlite3_mutex_leave(pMaster);
   51213            3228 :   return removed;
   51214                 : #else
   51215                 :   return 1;
   51216                 : #endif
   51217                 : }
   51218                 : 
   51219                 : /*
   51220                 : ** Make sure pBt->pTmpSpace points to an allocation of 
   51221                 : ** MX_CELL_SIZE(pBt) bytes.
   51222                 : */
   51223          230859 : static void allocateTempSpace(BtShared *pBt){
   51224          230859 :   if( !pBt->pTmpSpace ){
   51225           14104 :     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
   51226                 :   }
   51227          230859 : }
   51228                 : 
   51229                 : /*
   51230                 : ** Free the pBt->pTmpSpace allocation
   51231                 : */
   51232           21384 : static void freeTempSpace(BtShared *pBt){
   51233           21384 :   sqlite3PageFree( pBt->pTmpSpace);
   51234           21384 :   pBt->pTmpSpace = 0;
   51235           21384 : }
   51236                 : 
   51237                 : /*
   51238                 : ** Close an open database and invalidate all cursors.
   51239                 : */
   51240           19693 : SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
   51241           19693 :   BtShared *pBt = p->pBt;
   51242                 :   BtCursor *pCur;
   51243                 : 
   51244                 :   /* Close all cursors opened via this handle.  */
   51245           19693 :   assert( sqlite3_mutex_held(p->db->mutex) );
   51246           19693 :   sqlite3BtreeEnter(p);
   51247           19693 :   pCur = pBt->pCursor;
   51248           55414 :   while( pCur ){
   51249           16028 :     BtCursor *pTmp = pCur;
   51250           16028 :     pCur = pCur->pNext;
   51251           16028 :     if( pTmp->pBtree==p ){
   51252           16027 :       sqlite3BtreeCloseCursor(pTmp);
   51253                 :     }
   51254                 :   }
   51255                 : 
   51256                 :   /* Rollback any active transaction and free the handle structure.
   51257                 :   ** The call to sqlite3BtreeRollback() drops any table-locks held by
   51258                 :   ** this handle.
   51259                 :   */
   51260           19693 :   sqlite3BtreeRollback(p);
   51261           19693 :   sqlite3BtreeLeave(p);
   51262                 : 
   51263                 :   /* If there are still other outstanding references to the shared-btree
   51264                 :   ** structure, return now. The remainder of this procedure cleans 
   51265                 :   ** up the shared-btree.
   51266                 :   */
   51267           19693 :   assert( p->wantToLock==0 && p->locked==0 );
   51268           19693 :   if( !p->sharable || removeFromSharingList(pBt) ){
   51269                 :     /* The pBt is no longer on the sharing list, so we can access
   51270                 :     ** it without having to hold the mutex.
   51271                 :     **
   51272                 :     ** Clean out and delete the BtShared object.
   51273                 :     */
   51274           19517 :     assert( !pBt->pCursor );
   51275           19517 :     sqlite3PagerClose(pBt->pPager);
   51276           19517 :     if( pBt->xFreeSchema && pBt->pSchema ){
   51277            3107 :       pBt->xFreeSchema(pBt->pSchema);
   51278                 :     }
   51279           19517 :     sqlite3DbFree(0, pBt->pSchema);
   51280           19517 :     freeTempSpace(pBt);
   51281           19517 :     sqlite3_free(pBt);
   51282                 :   }
   51283                 : 
   51284                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   51285           19693 :   assert( p->wantToLock==0 );
   51286           19693 :   assert( p->locked==0 );
   51287           19693 :   if( p->pPrev ) p->pPrev->pNext = p->pNext;
   51288           19693 :   if( p->pNext ) p->pNext->pPrev = p->pPrev;
   51289                 : #endif
   51290                 : 
   51291           19693 :   sqlite3_free(p);
   51292           19693 :   return SQLITE_OK;
   51293                 : }
   51294                 : 
   51295                 : /*
   51296                 : ** Change the limit on the number of pages allowed in the cache.
   51297                 : **
   51298                 : ** The maximum number of cache pages is set to the absolute
   51299                 : ** value of mxPage.  If mxPage is negative, the pager will
   51300                 : ** operate asynchronously - it will not stop to do fsync()s
   51301                 : ** to insure data is written to the disk surface before
   51302                 : ** continuing.  Transactions still work if synchronous is off,
   51303                 : ** and the database cannot be corrupted if this program
   51304                 : ** crashes.  But if the operating system crashes or there is
   51305                 : ** an abrupt power failure when synchronous is off, the database
   51306                 : ** could be left in an inconsistent and unrecoverable state.
   51307                 : ** Synchronous is on by default so database corruption is not
   51308                 : ** normally a worry.
   51309                 : */
   51310            6933 : SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
   51311            6933 :   BtShared *pBt = p->pBt;
   51312            6933 :   assert( sqlite3_mutex_held(p->db->mutex) );
   51313            6933 :   sqlite3BtreeEnter(p);
   51314            6933 :   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
   51315            6933 :   sqlite3BtreeLeave(p);
   51316            6933 :   return SQLITE_OK;
   51317                 : }
   51318                 : 
   51319                 : /*
   51320                 : ** Change the way data is synced to disk in order to increase or decrease
   51321                 : ** how well the database resists damage due to OS crashes and power
   51322                 : ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
   51323                 : ** there is a high probability of damage)  Level 2 is the default.  There
   51324                 : ** is a very low but non-zero probability of damage.  Level 3 reduces the
   51325                 : ** probability of damage to near zero but with a write performance reduction.
   51326                 : */
   51327                 : #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   51328           21800 : SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
   51329                 :   Btree *p,              /* The btree to set the safety level on */
   51330                 :   int level,             /* PRAGMA synchronous.  1=OFF, 2=NORMAL, 3=FULL */
   51331                 :   int fullSync,          /* PRAGMA fullfsync. */
   51332                 :   int ckptFullSync       /* PRAGMA checkpoint_fullfync */
   51333                 : ){
   51334           21800 :   BtShared *pBt = p->pBt;
   51335           21800 :   assert( sqlite3_mutex_held(p->db->mutex) );
   51336           21800 :   assert( level>=1 && level<=3 );
   51337           21800 :   sqlite3BtreeEnter(p);
   51338           21800 :   sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
   51339           21800 :   sqlite3BtreeLeave(p);
   51340           21800 :   return SQLITE_OK;
   51341                 : }
   51342                 : #endif
   51343                 : 
   51344                 : /*
   51345                 : ** Return TRUE if the given btree is set to safety level 1.  In other
   51346                 : ** words, return TRUE if no sync() occurs on the disk files.
   51347                 : */
   51348               0 : SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
   51349               0 :   BtShared *pBt = p->pBt;
   51350                 :   int rc;
   51351               0 :   assert( sqlite3_mutex_held(p->db->mutex) );  
   51352               0 :   sqlite3BtreeEnter(p);
   51353               0 :   assert( pBt && pBt->pPager );
   51354               0 :   rc = sqlite3PagerNosync(pBt->pPager);
   51355               0 :   sqlite3BtreeLeave(p);
   51356               0 :   return rc;
   51357                 : }
   51358                 : 
   51359                 : /*
   51360                 : ** Change the default pages size and the number of reserved bytes per page.
   51361                 : ** Or, if the page size has already been fixed, return SQLITE_READONLY 
   51362                 : ** without changing anything.
   51363                 : **
   51364                 : ** The page size must be a power of 2 between 512 and 65536.  If the page
   51365                 : ** size supplied does not meet this constraint then the page size is not
   51366                 : ** changed.
   51367                 : **
   51368                 : ** Page sizes are constrained to be a power of two so that the region
   51369                 : ** of the database file used for locking (beginning at PENDING_BYTE,
   51370                 : ** the first byte past the 1GB boundary, 0x40000000) needs to occur
   51371                 : ** at the beginning of a page.
   51372                 : **
   51373                 : ** If parameter nReserve is less than zero, then the number of reserved
   51374                 : ** bytes per page is left unchanged.
   51375                 : **
   51376                 : ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
   51377                 : ** and autovacuum mode can no longer be changed.
   51378                 : */
   51379            3691 : SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
   51380            3691 :   int rc = SQLITE_OK;
   51381            3691 :   BtShared *pBt = p->pBt;
   51382            3691 :   assert( nReserve>=-1 && nReserve<=255 );
   51383            3691 :   sqlite3BtreeEnter(p);
   51384            3691 :   if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
   51385            1823 :     sqlite3BtreeLeave(p);
   51386            1823 :     return SQLITE_READONLY;
   51387                 :   }
   51388            1868 :   if( nReserve<0 ){
   51389            1850 :     nReserve = pBt->pageSize - pBt->usableSize;
   51390                 :   }
   51391            1868 :   assert( nReserve>=0 && nReserve<=255 );
   51392            3735 :   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
   51393            1867 :         ((pageSize-1)&pageSize)==0 ){
   51394            1867 :     assert( (pageSize & 7)==0 );
   51395            1867 :     assert( !pBt->pPage1 && !pBt->pCursor );
   51396            1867 :     pBt->pageSize = (u32)pageSize;
   51397            1867 :     freeTempSpace(pBt);
   51398                 :   }
   51399            1868 :   rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
   51400            1868 :   pBt->usableSize = pBt->pageSize - (u16)nReserve;
   51401            1868 :   if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
   51402            1868 :   sqlite3BtreeLeave(p);
   51403            1868 :   return rc;
   51404                 : }
   51405                 : 
   51406                 : /*
   51407                 : ** Return the currently defined page size
   51408                 : */
   51409            5221 : SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
   51410            5221 :   return p->pBt->pageSize;
   51411                 : }
   51412                 : 
   51413                 : #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
   51414                 : /*
   51415                 : ** Return the number of bytes of space at the end of every page that
   51416                 : ** are intentually left unused.  This is the "reserved" space that is
   51417                 : ** sometimes used by extensions.
   51418                 : */
   51419               6 : SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
   51420                 :   int n;
   51421               6 :   sqlite3BtreeEnter(p);
   51422               6 :   n = p->pBt->pageSize - p->pBt->usableSize;
   51423               6 :   sqlite3BtreeLeave(p);
   51424               6 :   return n;
   51425                 : }
   51426                 : 
   51427                 : /*
   51428                 : ** Set the maximum page count for a database if mxPage is positive.
   51429                 : ** No changes are made if mxPage is 0 or negative.
   51430                 : ** Regardless of the value of mxPage, return the maximum page count.
   51431                 : */
   51432               0 : SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
   51433                 :   int n;
   51434               0 :   sqlite3BtreeEnter(p);
   51435               0 :   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
   51436               0 :   sqlite3BtreeLeave(p);
   51437               0 :   return n;
   51438                 : }
   51439                 : 
   51440                 : /*
   51441                 : ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
   51442                 : ** then make no changes.  Always return the value of the BTS_SECURE_DELETE
   51443                 : ** setting after the change.
   51444                 : */
   51445              12 : SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
   51446                 :   int b;
   51447              12 :   if( p==0 ) return 0;
   51448              12 :   sqlite3BtreeEnter(p);
   51449              12 :   if( newFlag>=0 ){
   51450               6 :     p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
   51451               6 :     if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
   51452                 :   } 
   51453              12 :   b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
   51454              12 :   sqlite3BtreeLeave(p);
   51455              12 :   return b;
   51456                 : }
   51457                 : #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
   51458                 : 
   51459                 : /*
   51460                 : ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
   51461                 : ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
   51462                 : ** is disabled. The default value for the auto-vacuum property is 
   51463                 : ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
   51464                 : */
   51465              12 : SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
   51466                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   51467                 :   return SQLITE_READONLY;
   51468                 : #else
   51469              12 :   BtShared *pBt = p->pBt;
   51470              12 :   int rc = SQLITE_OK;
   51471              12 :   u8 av = (u8)autoVacuum;
   51472                 : 
   51473              12 :   sqlite3BtreeEnter(p);
   51474              12 :   if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
   51475               0 :     rc = SQLITE_READONLY;
   51476                 :   }else{
   51477              12 :     pBt->autoVacuum = av ?1:0;
   51478              12 :     pBt->incrVacuum = av==2 ?1:0;
   51479                 :   }
   51480              12 :   sqlite3BtreeLeave(p);
   51481              12 :   return rc;
   51482                 : #endif
   51483                 : }
   51484                 : 
   51485                 : /*
   51486                 : ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
   51487                 : ** enabled 1 is returned. Otherwise 0.
   51488                 : */
   51489              12 : SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
   51490                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   51491                 :   return BTREE_AUTOVACUUM_NONE;
   51492                 : #else
   51493                 :   int rc;
   51494              12 :   sqlite3BtreeEnter(p);
   51495              12 :   rc = (
   51496              12 :     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
   51497               0 :     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
   51498                 :     BTREE_AUTOVACUUM_INCR
   51499                 :   );
   51500              12 :   sqlite3BtreeLeave(p);
   51501              12 :   return rc;
   51502                 : #endif
   51503                 : }
   51504                 : 
   51505                 : 
   51506                 : /*
   51507                 : ** Get a reference to pPage1 of the database file.  This will
   51508                 : ** also acquire a readlock on that file.
   51509                 : **
   51510                 : ** SQLITE_OK is returned on success.  If the file is not a
   51511                 : ** well-formed database file, then SQLITE_CORRUPT is returned.
   51512                 : ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
   51513                 : ** is returned if we run out of memory. 
   51514                 : */
   51515          100213 : static int lockBtree(BtShared *pBt){
   51516                 :   int rc;              /* Result code from subfunctions */
   51517                 :   MemPage *pPage1;     /* Page 1 of the database file */
   51518                 :   int nPage;           /* Number of pages in the database */
   51519          100213 :   int nPageFile = 0;   /* Number of pages in the database file */
   51520                 :   int nPageHeader;     /* Number of pages in the database according to hdr */
   51521                 : 
   51522          100213 :   assert( sqlite3_mutex_held(pBt->mutex) );
   51523          100213 :   assert( pBt->pPage1==0 );
   51524          100213 :   rc = sqlite3PagerSharedLock(pBt->pPager);
   51525          100213 :   if( rc!=SQLITE_OK ) return rc;
   51526          100212 :   rc = btreeGetPage(pBt, 1, &pPage1, 0);
   51527          100212 :   if( rc!=SQLITE_OK ) return rc;
   51528                 : 
   51529                 :   /* Do some checking to help insure the file we opened really is
   51530                 :   ** a valid database file. 
   51531                 :   */
   51532          100212 :   nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
   51533          100212 :   sqlite3PagerPagecount(pBt->pPager, &nPageFile);
   51534          100212 :   if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
   51535           20288 :     nPage = nPageFile;
   51536                 :   }
   51537          100212 :   if( nPage>0 ){
   51538                 :     u32 pageSize;
   51539                 :     u32 usableSize;
   51540           80081 :     u8 *page1 = pPage1->aData;
   51541           80081 :     rc = SQLITE_NOTADB;
   51542           80081 :     if( memcmp(page1, zMagicHeader, 16)!=0 ){
   51543              10 :       goto page1_init_failed;
   51544                 :     }
   51545                 : 
   51546                 : #ifdef SQLITE_OMIT_WAL
   51547                 :     if( page1[18]>1 ){
   51548                 :       pBt->btsFlags |= BTS_READ_ONLY;
   51549                 :     }
   51550                 :     if( page1[19]>1 ){
   51551                 :       goto page1_init_failed;
   51552                 :     }
   51553                 : #else
   51554           80071 :     if( page1[18]>2 ){
   51555               0 :       pBt->btsFlags |= BTS_READ_ONLY;
   51556                 :     }
   51557           80071 :     if( page1[19]>2 ){
   51558               0 :       goto page1_init_failed;
   51559                 :     }
   51560                 : 
   51561                 :     /* If the write version is set to 2, this database should be accessed
   51562                 :     ** in WAL mode. If the log is not already open, open it now. Then 
   51563                 :     ** return SQLITE_OK and return without populating BtShared.pPage1.
   51564                 :     ** The caller detects this and calls this function again. This is
   51565                 :     ** required as the version of page 1 currently in the page1 buffer
   51566                 :     ** may not be the latest version - there may be a newer one in the log
   51567                 :     ** file.
   51568                 :     */
   51569           80071 :     if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
   51570           40491 :       int isOpen = 0;
   51571           40491 :       rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
   51572           40491 :       if( rc!=SQLITE_OK ){
   51573               0 :         goto page1_init_failed;
   51574           40491 :       }else if( isOpen==0 ){
   51575             393 :         releasePage(pPage1);
   51576             393 :         return SQLITE_OK;
   51577                 :       }
   51578           40098 :       rc = SQLITE_NOTADB;
   51579                 :     }
   51580                 : #endif
   51581                 : 
   51582                 :     /* The maximum embedded fraction must be exactly 25%.  And the minimum
   51583                 :     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
   51584                 :     ** The original design allowed these amounts to vary, but as of
   51585                 :     ** version 3.6.0, we require them to be fixed.
   51586                 :     */
   51587           79678 :     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
   51588               0 :       goto page1_init_failed;
   51589                 :     }
   51590           79678 :     pageSize = (page1[16]<<8) | (page1[17]<<16);
   51591           79678 :     if( ((pageSize-1)&pageSize)!=0
   51592           79678 :      || pageSize>SQLITE_MAX_PAGE_SIZE 
   51593           79678 :      || pageSize<=256 
   51594                 :     ){
   51595                 :       goto page1_init_failed;
   51596                 :     }
   51597           79678 :     assert( (pageSize & 7)==0 );
   51598           79678 :     usableSize = pageSize - page1[20];
   51599           79678 :     if( (u32)pageSize!=pBt->pageSize ){
   51600                 :       /* After reading the first page of the database assuming a page size
   51601                 :       ** of BtShared.pageSize, we have discovered that the page-size is
   51602                 :       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
   51603                 :       ** zero and return SQLITE_OK. The caller will call this function
   51604                 :       ** again with the correct page-size.
   51605                 :       */
   51606               0 :       releasePage(pPage1);
   51607               0 :       pBt->usableSize = usableSize;
   51608               0 :       pBt->pageSize = pageSize;
   51609               0 :       freeTempSpace(pBt);
   51610               0 :       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
   51611               0 :                                    pageSize-usableSize);
   51612               0 :       return rc;
   51613                 :     }
   51614           79678 :     if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
   51615               0 :       rc = SQLITE_CORRUPT_BKPT;
   51616               0 :       goto page1_init_failed;
   51617                 :     }
   51618           79678 :     if( usableSize<480 ){
   51619               0 :       goto page1_init_failed;
   51620                 :     }
   51621           79678 :     pBt->pageSize = pageSize;
   51622           79678 :     pBt->usableSize = usableSize;
   51623                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   51624           79678 :     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
   51625           79678 :     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
   51626                 : #endif
   51627                 :   }
   51628                 : 
   51629                 :   /* maxLocal is the maximum amount of payload to store locally for
   51630                 :   ** a cell.  Make sure it is small enough so that at least minFanout
   51631                 :   ** cells can will fit on one page.  We assume a 10-byte page header.
   51632                 :   ** Besides the payload, the cell must store:
   51633                 :   **     2-byte pointer to the cell
   51634                 :   **     4-byte child pointer
   51635                 :   **     9-byte nKey value
   51636                 :   **     4-byte nData value
   51637                 :   **     4-byte overflow page pointer
   51638                 :   ** So a cell consists of a 2-byte pointer, a header which is as much as
   51639                 :   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
   51640                 :   ** page pointer.
   51641                 :   */
   51642           99809 :   pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
   51643           99809 :   pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
   51644           99809 :   pBt->maxLeaf = (u16)(pBt->usableSize - 35);
   51645           99809 :   pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
   51646           99809 :   if( pBt->maxLocal>127 ){
   51647           99809 :     pBt->max1bytePayload = 127;
   51648                 :   }else{
   51649               0 :     pBt->max1bytePayload = (u8)pBt->maxLocal;
   51650                 :   }
   51651           99809 :   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
   51652           99809 :   pBt->pPage1 = pPage1;
   51653           99809 :   pBt->nPage = nPage;
   51654           99809 :   return SQLITE_OK;
   51655                 : 
   51656                 : page1_init_failed:
   51657              10 :   releasePage(pPage1);
   51658              10 :   pBt->pPage1 = 0;
   51659              10 :   return rc;
   51660                 : }
   51661                 : 
   51662                 : /*
   51663                 : ** If there are no outstanding cursors and we are not in the middle
   51664                 : ** of a transaction but there is a read lock on the database, then
   51665                 : ** this routine unrefs the first page of the database file which 
   51666                 : ** has the effect of releasing the read lock.
   51667                 : **
   51668                 : ** If there is a transaction in progress, this routine is a no-op.
   51669                 : */
   51670          636740 : static void unlockBtreeIfUnused(BtShared *pBt){
   51671          636740 :   assert( sqlite3_mutex_held(pBt->mutex) );
   51672          636740 :   assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE );
   51673          636740 :   if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
   51674           99718 :     assert( pBt->pPage1->aData );
   51675           99718 :     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
   51676           99718 :     assert( pBt->pPage1->aData );
   51677           99718 :     releasePage(pBt->pPage1);
   51678           99718 :     pBt->pPage1 = 0;
   51679                 :   }
   51680          636740 : }
   51681                 : 
   51682                 : /*
   51683                 : ** If pBt points to an empty file then convert that empty file
   51684                 : ** into a new empty database by initializing the first page of
   51685                 : ** the database.
   51686                 : */
   51687           87523 : static int newDatabase(BtShared *pBt){
   51688                 :   MemPage *pP1;
   51689                 :   unsigned char *data;
   51690                 :   int rc;
   51691                 : 
   51692           87523 :   assert( sqlite3_mutex_held(pBt->mutex) );
   51693           87523 :   if( pBt->nPage>0 ){
   51694           69725 :     return SQLITE_OK;
   51695                 :   }
   51696           17798 :   pP1 = pBt->pPage1;
   51697           17798 :   assert( pP1!=0 );
   51698           17798 :   data = pP1->aData;
   51699           17798 :   rc = sqlite3PagerWrite(pP1->pDbPage);
   51700           17798 :   if( rc ) return rc;
   51701           17798 :   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
   51702                 :   assert( sizeof(zMagicHeader)==16 );
   51703           17798 :   data[16] = (u8)((pBt->pageSize>>8)&0xff);
   51704           17798 :   data[17] = (u8)((pBt->pageSize>>16)&0xff);
   51705           17798 :   data[18] = 1;
   51706           17798 :   data[19] = 1;
   51707           17798 :   assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
   51708           17798 :   data[20] = (u8)(pBt->pageSize - pBt->usableSize);
   51709           17798 :   data[21] = 64;
   51710           17798 :   data[22] = 32;
   51711           17798 :   data[23] = 32;
   51712           17798 :   memset(&data[24], 0, 100-24);
   51713           17798 :   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
   51714           17798 :   pBt->btsFlags |= BTS_PAGESIZE_FIXED;
   51715                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   51716           17798 :   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
   51717           17798 :   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
   51718           17798 :   put4byte(&data[36 + 4*4], pBt->autoVacuum);
   51719           17798 :   put4byte(&data[36 + 7*4], pBt->incrVacuum);
   51720                 : #endif
   51721           17798 :   pBt->nPage = 1;
   51722           17798 :   data[31] = 1;
   51723           17798 :   return SQLITE_OK;
   51724                 : }
   51725                 : 
   51726                 : /*
   51727                 : ** Attempt to start a new transaction. A write-transaction
   51728                 : ** is started if the second argument is nonzero, otherwise a read-
   51729                 : ** transaction.  If the second argument is 2 or more and exclusive
   51730                 : ** transaction is started, meaning that no other process is allowed
   51731                 : ** to access the database.  A preexisting transaction may not be
   51732                 : ** upgraded to exclusive by calling this routine a second time - the
   51733                 : ** exclusivity flag only works for a new transaction.
   51734                 : **
   51735                 : ** A write-transaction must be started before attempting any 
   51736                 : ** changes to the database.  None of the following routines 
   51737                 : ** will work unless a transaction is started first:
   51738                 : **
   51739                 : **      sqlite3BtreeCreateTable()
   51740                 : **      sqlite3BtreeCreateIndex()
   51741                 : **      sqlite3BtreeClearTable()
   51742                 : **      sqlite3BtreeDropTable()
   51743                 : **      sqlite3BtreeInsert()
   51744                 : **      sqlite3BtreeDelete()
   51745                 : **      sqlite3BtreeUpdateMeta()
   51746                 : **
   51747                 : ** If an initial attempt to acquire the lock fails because of lock contention
   51748                 : ** and the database was previously unlocked, then invoke the busy handler
   51749                 : ** if there is one.  But if there was previously a read-lock, do not
   51750                 : ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
   51751                 : ** returned when there is already a read-lock in order to avoid a deadlock.
   51752                 : **
   51753                 : ** Suppose there are two processes A and B.  A has a read lock and B has
   51754                 : ** a reserved lock.  B tries to promote to exclusive but is blocked because
   51755                 : ** of A's read lock.  A tries to promote to reserved but is blocked by B.
   51756                 : ** One or the other of the two processes must give way or there can be
   51757                 : ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
   51758                 : ** when A already has a read lock, we encourage A to give up and let B
   51759                 : ** proceed.
   51760                 : */
   51761          204538 : SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
   51762          204538 :   sqlite3 *pBlock = 0;
   51763          204538 :   BtShared *pBt = p->pBt;
   51764          204538 :   int rc = SQLITE_OK;
   51765                 : 
   51766          204538 :   sqlite3BtreeEnter(p);
   51767          204537 :   btreeIntegrity(p);
   51768                 : 
   51769                 :   /* If the btree is already in a write-transaction, or it
   51770                 :   ** is already in a read-transaction and a read-transaction
   51771                 :   ** is requested, this is a no-op.
   51772                 :   */
   51773          204537 :   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
   51774                 :     goto trans_begun;
   51775                 :   }
   51776                 : 
   51777                 :   /* Write transactions are not possible on a read-only database */
   51778          103661 :   if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
   51779               2 :     rc = SQLITE_READONLY;
   51780               2 :     goto trans_begun;
   51781                 :   }
   51782                 : 
   51783                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   51784                 :   /* If another database handle has already opened a write transaction 
   51785                 :   ** on this shared-btree structure and a second write transaction is
   51786                 :   ** requested, return SQLITE_LOCKED.
   51787                 :   */
   51788          103659 :   if( (wrflag && pBt->inTransaction==TRANS_WRITE)
   51789          103659 :    || (pBt->btsFlags & BTS_PENDING)!=0
   51790                 :   ){
   51791               0 :     pBlock = pBt->pWriter->db;
   51792          103659 :   }else if( wrflag>1 ){
   51793                 :     BtLock *pIter;
   51794            1531 :     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
   51795             476 :       if( pIter->pBtree!=p ){
   51796               0 :         pBlock = pIter->pBtree->db;
   51797               0 :         break;
   51798                 :       }
   51799                 :     }
   51800                 :   }
   51801          103659 :   if( pBlock ){
   51802               0 :     sqlite3ConnectionBlocked(p->db, pBlock);
   51803               0 :     rc = SQLITE_LOCKED_SHAREDCACHE;
   51804               0 :     goto trans_begun;
   51805                 :   }
   51806                 : #endif
   51807                 : 
   51808                 :   /* Any read-only or read-write transaction implies a read-lock on 
   51809                 :   ** page 1. So if some other shared-cache client already has a write-lock 
   51810                 :   ** on page 1, the transaction cannot be opened. */
   51811          103659 :   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
   51812          103661 :   if( SQLITE_OK!=rc ) goto trans_begun;
   51813                 : 
   51814          103661 :   pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
   51815          103661 :   if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
   51816                 :   do {
   51817                 :     /* Call lockBtree() until either pBt->pPage1 is populated or
   51818                 :     ** lockBtree() returns something other than SQLITE_OK. lockBtree()
   51819                 :     ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
   51820                 :     ** reading page 1 it discovers that the page-size of the database 
   51821                 :     ** file is not pBt->pageSize. In this case lockBtree() will update
   51822                 :     ** pBt->pageSize to the page-size of the file on disk.
   51823                 :     */
   51824          103661 :     while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
   51825                 : 
   51826          103661 :     if( rc==SQLITE_OK && wrflag ){
   51827           56893 :       if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
   51828               0 :         rc = SQLITE_READONLY;
   51829                 :       }else{
   51830           56893 :         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
   51831           56893 :         if( rc==SQLITE_OK ){
   51832           56893 :           rc = newDatabase(pBt);
   51833                 :         }
   51834                 :       }
   51835                 :     }
   51836                 :   
   51837          103660 :     if( rc!=SQLITE_OK ){
   51838              11 :       unlockBtreeIfUnused(pBt);
   51839                 :     }
   51840          103662 :   }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
   51841          103661 :           btreeInvokeBusyHandler(pBt) );
   51842                 : 
   51843          103660 :   if( rc==SQLITE_OK ){
   51844          103650 :     if( p->inTrans==TRANS_NONE ){
   51845          100286 :       pBt->nTransaction++;
   51846                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   51847          100286 :       if( p->sharable ){
   51848           77467 :         assert( p->lock.pBtree==p && p->lock.iTable==1 );
   51849           77467 :         p->lock.eLock = READ_LOCK;
   51850           77467 :         p->lock.pNext = pBt->pLock;
   51851           77467 :         pBt->pLock = &p->lock;
   51852                 :       }
   51853                 : #endif
   51854                 :     }
   51855          103650 :     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
   51856          103650 :     if( p->inTrans>pBt->inTransaction ){
   51857          103471 :       pBt->inTransaction = p->inTrans;
   51858                 :     }
   51859          103650 :     if( wrflag ){
   51860           56893 :       MemPage *pPage1 = pBt->pPage1;
   51861                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   51862           56893 :       assert( !pBt->pWriter );
   51863           56893 :       pBt->pWriter = p;
   51864           56893 :       pBt->btsFlags &= ~BTS_EXCLUSIVE;
   51865           56893 :       if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
   51866                 : #endif
   51867                 : 
   51868                 :       /* If the db-size header field is incorrect (as it may be if an old
   51869                 :       ** client has been writing the database file), update it now. Doing
   51870                 :       ** this sooner rather than later means the database size can safely 
   51871                 :       ** re-read the database size from page 1 if a savepoint or transaction
   51872                 :       ** rollback occurs within the transaction.
   51873                 :       */
   51874           56893 :       if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
   51875              30 :         rc = sqlite3PagerWrite(pPage1->pDbPage);
   51876              30 :         if( rc==SQLITE_OK ){
   51877              30 :           put4byte(&pPage1->aData[28], pBt->nPage);
   51878                 :         }
   51879                 :       }
   51880                 :     }
   51881                 :   }
   51882                 : 
   51883                 : 
   51884                 : trans_begun:
   51885          204538 :   if( rc==SQLITE_OK && wrflag ){
   51886                 :     /* This call makes sure that the pager has the correct number of
   51887                 :     ** open savepoints. If the second parameter is greater than 0 and
   51888                 :     ** the sub-journal is not already open, then it will be opened here.
   51889                 :     */
   51890          110145 :     rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
   51891                 :   }
   51892                 : 
   51893          204538 :   btreeIntegrity(p);
   51894          204538 :   sqlite3BtreeLeave(p);
   51895          204538 :   return rc;
   51896                 : }
   51897                 : 
   51898                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   51899                 : 
   51900                 : /*
   51901                 : ** Set the pointer-map entries for all children of page pPage. Also, if
   51902                 : ** pPage contains cells that point to overflow pages, set the pointer
   51903                 : ** map entries for the overflow pages as well.
   51904                 : */
   51905               0 : static int setChildPtrmaps(MemPage *pPage){
   51906                 :   int i;                             /* Counter variable */
   51907                 :   int nCell;                         /* Number of cells in page pPage */
   51908                 :   int rc;                            /* Return code */
   51909               0 :   BtShared *pBt = pPage->pBt;
   51910               0 :   u8 isInitOrig = pPage->isInit;
   51911               0 :   Pgno pgno = pPage->pgno;
   51912                 : 
   51913               0 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   51914               0 :   rc = btreeInitPage(pPage);
   51915               0 :   if( rc!=SQLITE_OK ){
   51916               0 :     goto set_child_ptrmaps_out;
   51917                 :   }
   51918               0 :   nCell = pPage->nCell;
   51919                 : 
   51920               0 :   for(i=0; i<nCell; i++){
   51921               0 :     u8 *pCell = findCell(pPage, i);
   51922                 : 
   51923               0 :     ptrmapPutOvflPtr(pPage, pCell, &rc);
   51924                 : 
   51925               0 :     if( !pPage->leaf ){
   51926               0 :       Pgno childPgno = get4byte(pCell);
   51927               0 :       ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
   51928                 :     }
   51929                 :   }
   51930                 : 
   51931               0 :   if( !pPage->leaf ){
   51932               0 :     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
   51933               0 :     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
   51934                 :   }
   51935                 : 
   51936                 : set_child_ptrmaps_out:
   51937               0 :   pPage->isInit = isInitOrig;
   51938               0 :   return rc;
   51939                 : }
   51940                 : 
   51941                 : /*
   51942                 : ** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
   51943                 : ** that it points to iTo. Parameter eType describes the type of pointer to
   51944                 : ** be modified, as  follows:
   51945                 : **
   51946                 : ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
   51947                 : **                   page of pPage.
   51948                 : **
   51949                 : ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
   51950                 : **                   page pointed to by one of the cells on pPage.
   51951                 : **
   51952                 : ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
   51953                 : **                   overflow page in the list.
   51954                 : */
   51955               0 : static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
   51956               0 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   51957               0 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   51958               0 :   if( eType==PTRMAP_OVERFLOW2 ){
   51959                 :     /* The pointer is always the first 4 bytes of the page in this case.  */
   51960               0 :     if( get4byte(pPage->aData)!=iFrom ){
   51961               0 :       return SQLITE_CORRUPT_BKPT;
   51962                 :     }
   51963               0 :     put4byte(pPage->aData, iTo);
   51964                 :   }else{
   51965               0 :     u8 isInitOrig = pPage->isInit;
   51966                 :     int i;
   51967                 :     int nCell;
   51968                 : 
   51969               0 :     btreeInitPage(pPage);
   51970               0 :     nCell = pPage->nCell;
   51971                 : 
   51972               0 :     for(i=0; i<nCell; i++){
   51973               0 :       u8 *pCell = findCell(pPage, i);
   51974               0 :       if( eType==PTRMAP_OVERFLOW1 ){
   51975                 :         CellInfo info;
   51976               0 :         btreeParseCellPtr(pPage, pCell, &info);
   51977               0 :         if( info.iOverflow
   51978               0 :          && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
   51979               0 :          && iFrom==get4byte(&pCell[info.iOverflow])
   51980                 :         ){
   51981               0 :           put4byte(&pCell[info.iOverflow], iTo);
   51982               0 :           break;
   51983                 :         }
   51984                 :       }else{
   51985               0 :         if( get4byte(pCell)==iFrom ){
   51986               0 :           put4byte(pCell, iTo);
   51987               0 :           break;
   51988                 :         }
   51989                 :       }
   51990                 :     }
   51991                 :   
   51992               0 :     if( i==nCell ){
   51993               0 :       if( eType!=PTRMAP_BTREE || 
   51994               0 :           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
   51995               0 :         return SQLITE_CORRUPT_BKPT;
   51996                 :       }
   51997               0 :       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
   51998                 :     }
   51999                 : 
   52000               0 :     pPage->isInit = isInitOrig;
   52001                 :   }
   52002               0 :   return SQLITE_OK;
   52003                 : }
   52004                 : 
   52005                 : 
   52006                 : /*
   52007                 : ** Move the open database page pDbPage to location iFreePage in the 
   52008                 : ** database. The pDbPage reference remains valid.
   52009                 : **
   52010                 : ** The isCommit flag indicates that there is no need to remember that
   52011                 : ** the journal needs to be sync()ed before database page pDbPage->pgno 
   52012                 : ** can be written to. The caller has already promised not to write to that
   52013                 : ** page.
   52014                 : */
   52015               0 : static int relocatePage(
   52016                 :   BtShared *pBt,           /* Btree */
   52017                 :   MemPage *pDbPage,        /* Open page to move */
   52018                 :   u8 eType,                /* Pointer map 'type' entry for pDbPage */
   52019                 :   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
   52020                 :   Pgno iFreePage,          /* The location to move pDbPage to */
   52021                 :   int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
   52022                 : ){
   52023                 :   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
   52024               0 :   Pgno iDbPage = pDbPage->pgno;
   52025               0 :   Pager *pPager = pBt->pPager;
   52026                 :   int rc;
   52027                 : 
   52028               0 :   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
   52029                 :       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
   52030               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   52031               0 :   assert( pDbPage->pBt==pBt );
   52032                 : 
   52033                 :   /* Move page iDbPage from its current location to page number iFreePage */
   52034                 :   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
   52035                 :       iDbPage, iFreePage, iPtrPage, eType));
   52036               0 :   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
   52037               0 :   if( rc!=SQLITE_OK ){
   52038               0 :     return rc;
   52039                 :   }
   52040               0 :   pDbPage->pgno = iFreePage;
   52041                 : 
   52042                 :   /* If pDbPage was a btree-page, then it may have child pages and/or cells
   52043                 :   ** that point to overflow pages. The pointer map entries for all these
   52044                 :   ** pages need to be changed.
   52045                 :   **
   52046                 :   ** If pDbPage is an overflow page, then the first 4 bytes may store a
   52047                 :   ** pointer to a subsequent overflow page. If this is the case, then
   52048                 :   ** the pointer map needs to be updated for the subsequent overflow page.
   52049                 :   */
   52050               0 :   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
   52051               0 :     rc = setChildPtrmaps(pDbPage);
   52052               0 :     if( rc!=SQLITE_OK ){
   52053               0 :       return rc;
   52054                 :     }
   52055                 :   }else{
   52056               0 :     Pgno nextOvfl = get4byte(pDbPage->aData);
   52057               0 :     if( nextOvfl!=0 ){
   52058               0 :       ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
   52059               0 :       if( rc!=SQLITE_OK ){
   52060               0 :         return rc;
   52061                 :       }
   52062                 :     }
   52063                 :   }
   52064                 : 
   52065                 :   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
   52066                 :   ** that it points at iFreePage. Also fix the pointer map entry for
   52067                 :   ** iPtrPage.
   52068                 :   */
   52069               0 :   if( eType!=PTRMAP_ROOTPAGE ){
   52070               0 :     rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
   52071               0 :     if( rc!=SQLITE_OK ){
   52072               0 :       return rc;
   52073                 :     }
   52074               0 :     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
   52075               0 :     if( rc!=SQLITE_OK ){
   52076               0 :       releasePage(pPtrPage);
   52077               0 :       return rc;
   52078                 :     }
   52079               0 :     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
   52080               0 :     releasePage(pPtrPage);
   52081               0 :     if( rc==SQLITE_OK ){
   52082               0 :       ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
   52083                 :     }
   52084                 :   }
   52085               0 :   return rc;
   52086                 : }
   52087                 : 
   52088                 : /* Forward declaration required by incrVacuumStep(). */
   52089                 : static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
   52090                 : 
   52091                 : /*
   52092                 : ** Perform a single step of an incremental-vacuum. If successful,
   52093                 : ** return SQLITE_OK. If there is no work to do (and therefore no
   52094                 : ** point in calling this function again), return SQLITE_DONE.
   52095                 : **
   52096                 : ** More specificly, this function attempts to re-organize the 
   52097                 : ** database so that the last page of the file currently in use
   52098                 : ** is no longer in use.
   52099                 : **
   52100                 : ** If the nFin parameter is non-zero, this function assumes
   52101                 : ** that the caller will keep calling incrVacuumStep() until
   52102                 : ** it returns SQLITE_DONE or an error, and that nFin is the
   52103                 : ** number of pages the database file will contain after this 
   52104                 : ** process is complete.  If nFin is zero, it is assumed that
   52105                 : ** incrVacuumStep() will be called a finite amount of times
   52106                 : ** which may or may not empty the freelist.  A full autovacuum
   52107                 : ** has nFin>0.  A "PRAGMA incremental_vacuum" has nFin==0.
   52108                 : */
   52109               0 : static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
   52110                 :   Pgno nFreeList;           /* Number of pages still on the free-list */
   52111                 :   int rc;
   52112                 : 
   52113               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   52114               0 :   assert( iLastPg>nFin );
   52115                 : 
   52116               0 :   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
   52117                 :     u8 eType;
   52118                 :     Pgno iPtrPage;
   52119                 : 
   52120               0 :     nFreeList = get4byte(&pBt->pPage1->aData[36]);
   52121               0 :     if( nFreeList==0 ){
   52122               0 :       return SQLITE_DONE;
   52123                 :     }
   52124                 : 
   52125               0 :     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
   52126               0 :     if( rc!=SQLITE_OK ){
   52127               0 :       return rc;
   52128                 :     }
   52129               0 :     if( eType==PTRMAP_ROOTPAGE ){
   52130               0 :       return SQLITE_CORRUPT_BKPT;
   52131                 :     }
   52132                 : 
   52133               0 :     if( eType==PTRMAP_FREEPAGE ){
   52134               0 :       if( nFin==0 ){
   52135                 :         /* Remove the page from the files free-list. This is not required
   52136                 :         ** if nFin is non-zero. In that case, the free-list will be
   52137                 :         ** truncated to zero after this function returns, so it doesn't 
   52138                 :         ** matter if it still contains some garbage entries.
   52139                 :         */
   52140                 :         Pgno iFreePg;
   52141                 :         MemPage *pFreePg;
   52142               0 :         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
   52143               0 :         if( rc!=SQLITE_OK ){
   52144               0 :           return rc;
   52145                 :         }
   52146               0 :         assert( iFreePg==iLastPg );
   52147               0 :         releasePage(pFreePg);
   52148                 :       }
   52149                 :     } else {
   52150                 :       Pgno iFreePg;             /* Index of free page to move pLastPg to */
   52151                 :       MemPage *pLastPg;
   52152                 : 
   52153               0 :       rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
   52154               0 :       if( rc!=SQLITE_OK ){
   52155               0 :         return rc;
   52156                 :       }
   52157                 : 
   52158                 :       /* If nFin is zero, this loop runs exactly once and page pLastPg
   52159                 :       ** is swapped with the first free page pulled off the free list.
   52160                 :       **
   52161                 :       ** On the other hand, if nFin is greater than zero, then keep
   52162                 :       ** looping until a free-page located within the first nFin pages
   52163                 :       ** of the file is found.
   52164                 :       */
   52165                 :       do {
   52166                 :         MemPage *pFreePg;
   52167               0 :         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
   52168               0 :         if( rc!=SQLITE_OK ){
   52169               0 :           releasePage(pLastPg);
   52170               0 :           return rc;
   52171                 :         }
   52172               0 :         releasePage(pFreePg);
   52173               0 :       }while( nFin!=0 && iFreePg>nFin );
   52174               0 :       assert( iFreePg<iLastPg );
   52175                 :       
   52176               0 :       rc = sqlite3PagerWrite(pLastPg->pDbPage);
   52177               0 :       if( rc==SQLITE_OK ){
   52178               0 :         rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
   52179                 :       }
   52180               0 :       releasePage(pLastPg);
   52181               0 :       if( rc!=SQLITE_OK ){
   52182               0 :         return rc;
   52183                 :       }
   52184                 :     }
   52185                 :   }
   52186                 : 
   52187               0 :   if( nFin==0 ){
   52188               0 :     iLastPg--;
   52189               0 :     while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
   52190               0 :       if( PTRMAP_ISPAGE(pBt, iLastPg) ){
   52191                 :         MemPage *pPg;
   52192               0 :         rc = btreeGetPage(pBt, iLastPg, &pPg, 0);
   52193               0 :         if( rc!=SQLITE_OK ){
   52194               0 :           return rc;
   52195                 :         }
   52196               0 :         rc = sqlite3PagerWrite(pPg->pDbPage);
   52197               0 :         releasePage(pPg);
   52198               0 :         if( rc!=SQLITE_OK ){
   52199               0 :           return rc;
   52200                 :         }
   52201                 :       }
   52202               0 :       iLastPg--;
   52203                 :     }
   52204               0 :     sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
   52205               0 :     pBt->nPage = iLastPg;
   52206                 :   }
   52207               0 :   return SQLITE_OK;
   52208                 : }
   52209                 : 
   52210                 : /*
   52211                 : ** A write-transaction must be opened before calling this function.
   52212                 : ** It performs a single unit of work towards an incremental vacuum.
   52213                 : **
   52214                 : ** If the incremental vacuum is finished after this function has run,
   52215                 : ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
   52216                 : ** SQLITE_OK is returned. Otherwise an SQLite error code. 
   52217                 : */
   52218               0 : SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
   52219                 :   int rc;
   52220               0 :   BtShared *pBt = p->pBt;
   52221                 : 
   52222               0 :   sqlite3BtreeEnter(p);
   52223               0 :   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
   52224               0 :   if( !pBt->autoVacuum ){
   52225               0 :     rc = SQLITE_DONE;
   52226                 :   }else{
   52227               0 :     invalidateAllOverflowCache(pBt);
   52228               0 :     rc = incrVacuumStep(pBt, 0, btreePagecount(pBt));
   52229               0 :     if( rc==SQLITE_OK ){
   52230               0 :       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
   52231               0 :       put4byte(&pBt->pPage1->aData[28], pBt->nPage);
   52232                 :     }
   52233                 :   }
   52234               0 :   sqlite3BtreeLeave(p);
   52235               0 :   return rc;
   52236                 : }
   52237                 : 
   52238                 : /*
   52239                 : ** This routine is called prior to sqlite3PagerCommit when a transaction
   52240                 : ** is commited for an auto-vacuum database.
   52241                 : **
   52242                 : ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
   52243                 : ** the database file should be truncated to during the commit process. 
   52244                 : ** i.e. the database has been reorganized so that only the first *pnTrunc
   52245                 : ** pages are in use.
   52246                 : */
   52247               0 : static int autoVacuumCommit(BtShared *pBt){
   52248               0 :   int rc = SQLITE_OK;
   52249               0 :   Pager *pPager = pBt->pPager;
   52250               0 :   VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
   52251                 : 
   52252               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   52253               0 :   invalidateAllOverflowCache(pBt);
   52254               0 :   assert(pBt->autoVacuum);
   52255               0 :   if( !pBt->incrVacuum ){
   52256                 :     Pgno nFin;         /* Number of pages in database after autovacuuming */
   52257                 :     Pgno nFree;        /* Number of pages on the freelist initially */
   52258                 :     Pgno nPtrmap;      /* Number of PtrMap pages to be freed */
   52259                 :     Pgno iFree;        /* The next page to be freed */
   52260                 :     int nEntry;        /* Number of entries on one ptrmap page */
   52261                 :     Pgno nOrig;        /* Database size before freeing */
   52262                 : 
   52263               0 :     nOrig = btreePagecount(pBt);
   52264               0 :     if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
   52265                 :       /* It is not possible to create a database for which the final page
   52266                 :       ** is either a pointer-map page or the pending-byte page. If one
   52267                 :       ** is encountered, this indicates corruption.
   52268                 :       */
   52269               0 :       return SQLITE_CORRUPT_BKPT;
   52270                 :     }
   52271                 : 
   52272               0 :     nFree = get4byte(&pBt->pPage1->aData[36]);
   52273               0 :     nEntry = pBt->usableSize/5;
   52274               0 :     nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
   52275               0 :     nFin = nOrig - nFree - nPtrmap;
   52276               0 :     if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
   52277               0 :       nFin--;
   52278                 :     }
   52279               0 :     while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
   52280               0 :       nFin--;
   52281                 :     }
   52282               0 :     if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
   52283                 : 
   52284               0 :     for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
   52285               0 :       rc = incrVacuumStep(pBt, nFin, iFree);
   52286                 :     }
   52287               0 :     if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
   52288               0 :       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
   52289               0 :       put4byte(&pBt->pPage1->aData[32], 0);
   52290               0 :       put4byte(&pBt->pPage1->aData[36], 0);
   52291               0 :       put4byte(&pBt->pPage1->aData[28], nFin);
   52292               0 :       sqlite3PagerTruncateImage(pBt->pPager, nFin);
   52293               0 :       pBt->nPage = nFin;
   52294                 :     }
   52295               0 :     if( rc!=SQLITE_OK ){
   52296               0 :       sqlite3PagerRollback(pPager);
   52297                 :     }
   52298                 :   }
   52299                 : 
   52300               0 :   assert( nRef==sqlite3PagerRefcount(pPager) );
   52301               0 :   return rc;
   52302                 : }
   52303                 : 
   52304                 : #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
   52305                 : # define setChildPtrmaps(x) SQLITE_OK
   52306                 : #endif
   52307                 : 
   52308                 : /*
   52309                 : ** This routine does the first phase of a two-phase commit.  This routine
   52310                 : ** causes a rollback journal to be created (if it does not already exist)
   52311                 : ** and populated with enough information so that if a power loss occurs
   52312                 : ** the database can be restored to its original state by playing back
   52313                 : ** the journal.  Then the contents of the journal are flushed out to
   52314                 : ** the disk.  After the journal is safely on oxide, the changes to the
   52315                 : ** database are written into the database file and flushed to oxide.
   52316                 : ** At the end of this call, the rollback journal still exists on the
   52317                 : ** disk and we are still holding all locks, so the transaction has not
   52318                 : ** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
   52319                 : ** commit process.
   52320                 : **
   52321                 : ** This call is a no-op if no write-transaction is currently active on pBt.
   52322                 : **
   52323                 : ** Otherwise, sync the database file for the btree pBt. zMaster points to
   52324                 : ** the name of a master journal file that should be written into the
   52325                 : ** individual journal file, or is NULL, indicating no master journal file 
   52326                 : ** (single database transaction).
   52327                 : **
   52328                 : ** When this is called, the master journal should already have been
   52329                 : ** created, populated with this journal pointer and synced to disk.
   52330                 : **
   52331                 : ** Once this is routine has returned, the only thing required to commit
   52332                 : ** the write-transaction for this database file is to delete the journal.
   52333                 : */
   52334          129267 : SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
   52335          129267 :   int rc = SQLITE_OK;
   52336          129267 :   if( p->inTrans==TRANS_WRITE ){
   52337           40533 :     BtShared *pBt = p->pBt;
   52338           40533 :     sqlite3BtreeEnter(p);
   52339                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   52340           40533 :     if( pBt->autoVacuum ){
   52341               0 :       rc = autoVacuumCommit(pBt);
   52342               0 :       if( rc!=SQLITE_OK ){
   52343               0 :         sqlite3BtreeLeave(p);
   52344               0 :         return rc;
   52345                 :       }
   52346                 :     }
   52347                 : #endif
   52348           40533 :     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
   52349           40533 :     sqlite3BtreeLeave(p);
   52350                 :   }
   52351          129267 :   return rc;
   52352                 : }
   52353                 : 
   52354                 : /*
   52355                 : ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
   52356                 : ** at the conclusion of a transaction.
   52357                 : */
   52358          105163 : static void btreeEndTransaction(Btree *p){
   52359          105163 :   BtShared *pBt = p->pBt;
   52360          105163 :   assert( sqlite3BtreeHoldsMutex(p) );
   52361                 : 
   52362          105163 :   btreeClearHasContent(pBt);
   52363          105163 :   if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){
   52364                 :     /* If there are other active statements that belong to this database
   52365                 :     ** handle, downgrade to a read-only transaction. The other statements
   52366                 :     ** may still be reading from the database.  */
   52367            1263 :     downgradeAllSharedCacheTableLocks(p);
   52368            1263 :     p->inTrans = TRANS_READ;
   52369                 :   }else{
   52370                 :     /* If the handle had any kind of transaction open, decrement the 
   52371                 :     ** transaction count of the shared btree. If the transaction count 
   52372                 :     ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
   52373                 :     ** call below will unlock the pager.  */
   52374          103900 :     if( p->inTrans!=TRANS_NONE ){
   52375          100195 :       clearAllSharedCacheTableLocks(p);
   52376          100195 :       pBt->nTransaction--;
   52377          100195 :       if( 0==pBt->nTransaction ){
   52378           99718 :         pBt->inTransaction = TRANS_NONE;
   52379                 :       }
   52380                 :     }
   52381                 : 
   52382                 :     /* Set the current transaction state to TRANS_NONE and unlock the 
   52383                 :     ** pager if this call closed the only read or write transaction.  */
   52384          103900 :     p->inTrans = TRANS_NONE;
   52385          103900 :     unlockBtreeIfUnused(pBt);
   52386                 :   }
   52387                 : 
   52388          105163 :   btreeIntegrity(p);
   52389          105163 : }
   52390                 : 
   52391                 : /*
   52392                 : ** Commit the transaction currently in progress.
   52393                 : **
   52394                 : ** This routine implements the second phase of a 2-phase commit.  The
   52395                 : ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
   52396                 : ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
   52397                 : ** routine did all the work of writing information out to disk and flushing the
   52398                 : ** contents so that they are written onto the disk platter.  All this
   52399                 : ** routine has to do is delete or truncate or zero the header in the
   52400                 : ** the rollback journal (which causes the transaction to commit) and
   52401                 : ** drop locks.
   52402                 : **
   52403                 : ** Normally, if an error occurs while the pager layer is attempting to 
   52404                 : ** finalize the underlying journal file, this function returns an error and
   52405                 : ** the upper layer will attempt a rollback. However, if the second argument
   52406                 : ** is non-zero then this b-tree transaction is part of a multi-file 
   52407                 : ** transaction. In this case, the transaction has already been committed 
   52408                 : ** (by deleting a master journal file) and the caller will ignore this 
   52409                 : ** functions return code. So, even if an error occurs in the pager layer,
   52410                 : ** reset the b-tree objects internal state to indicate that the write
   52411                 : ** transaction has been closed. This is quite safe, as the pager will have
   52412                 : ** transitioned to the error state.
   52413                 : **
   52414                 : ** This will release the write lock on the database file.  If there
   52415                 : ** are no active cursors, it also releases the read lock.
   52416                 : */
   52417          129273 : SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
   52418                 : 
   52419          129273 :   if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
   52420           85080 :   sqlite3BtreeEnter(p);
   52421           85080 :   btreeIntegrity(p);
   52422                 : 
   52423                 :   /* If the handle has a write-transaction open, commit the shared-btrees 
   52424                 :   ** transaction and set the shared state to TRANS_READ.
   52425                 :   */
   52426           85080 :   if( p->inTrans==TRANS_WRITE ){
   52427                 :     int rc;
   52428           40539 :     BtShared *pBt = p->pBt;
   52429           40539 :     assert( pBt->inTransaction==TRANS_WRITE );
   52430           40539 :     assert( pBt->nTransaction>0 );
   52431           40539 :     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
   52432           40539 :     if( rc!=SQLITE_OK && bCleanup==0 ){
   52433               0 :       sqlite3BtreeLeave(p);
   52434               0 :       return rc;
   52435                 :     }
   52436           40539 :     pBt->inTransaction = TRANS_READ;
   52437                 :   }
   52438                 : 
   52439           85080 :   btreeEndTransaction(p);
   52440           85080 :   sqlite3BtreeLeave(p);
   52441           85080 :   return SQLITE_OK;
   52442                 : }
   52443                 : 
   52444                 : /*
   52445                 : ** Do both phases of a commit.
   52446                 : */
   52447            3252 : SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
   52448                 :   int rc;
   52449            3252 :   sqlite3BtreeEnter(p);
   52450            3252 :   rc = sqlite3BtreeCommitPhaseOne(p, 0);
   52451            3252 :   if( rc==SQLITE_OK ){
   52452            3252 :     rc = sqlite3BtreeCommitPhaseTwo(p, 0);
   52453                 :   }
   52454            3252 :   sqlite3BtreeLeave(p);
   52455            3252 :   return rc;
   52456                 : }
   52457                 : 
   52458                 : #ifndef NDEBUG
   52459                 : /*
   52460                 : ** Return the number of write-cursors open on this handle. This is for use
   52461                 : ** in assert() expressions, so it is only compiled if NDEBUG is not
   52462                 : ** defined.
   52463                 : **
   52464                 : ** For the purposes of this routine, a write-cursor is any cursor that
   52465                 : ** is capable of writing to the databse.  That means the cursor was
   52466                 : ** originally opened for writing and the cursor has not be disabled
   52467                 : ** by having its state changed to CURSOR_FAULT.
   52468                 : */
   52469           16354 : static int countWriteCursors(BtShared *pBt){
   52470                 :   BtCursor *pCur;
   52471           16354 :   int r = 0;
   52472           16357 :   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
   52473               3 :     if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; 
   52474                 :   }
   52475           16354 :   return r;
   52476                 : }
   52477                 : #endif
   52478                 : 
   52479                 : /*
   52480                 : ** This routine sets the state to CURSOR_FAULT and the error
   52481                 : ** code to errCode for every cursor on BtShared that pBtree
   52482                 : ** references.
   52483                 : **
   52484                 : ** Every cursor is tripped, including cursors that belong
   52485                 : ** to other database connections that happen to be sharing
   52486                 : ** the cache with pBtree.
   52487                 : **
   52488                 : ** This routine gets called when a rollback occurs.
   52489                 : ** All cursors using the same cache must be tripped
   52490                 : ** to prevent them from trying to use the btree after
   52491                 : ** the rollback.  The rollback may have deleted tables
   52492                 : ** or moved root pages, so it is not sufficient to
   52493                 : ** save the state of the cursor.  The cursor must be
   52494                 : ** invalidated.
   52495                 : */
   52496               0 : SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
   52497                 :   BtCursor *p;
   52498               0 :   sqlite3BtreeEnter(pBtree);
   52499               0 :   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
   52500                 :     int i;
   52501               0 :     sqlite3BtreeClearCursor(p);
   52502               0 :     p->eState = CURSOR_FAULT;
   52503               0 :     p->skipNext = errCode;
   52504               0 :     for(i=0; i<=p->iPage; i++){
   52505               0 :       releasePage(p->apPage[i]);
   52506               0 :       p->apPage[i] = 0;
   52507                 :     }
   52508                 :   }
   52509               0 :   sqlite3BtreeLeave(pBtree);
   52510               0 : }
   52511                 : 
   52512                 : /*
   52513                 : ** Rollback the transaction in progress.  All cursors will be
   52514                 : ** invalided by this operation.  Any attempt to use a cursor
   52515                 : ** that was open at the beginning of this operation will result
   52516                 : ** in an error.
   52517                 : **
   52518                 : ** This will release the write lock on the database file.  If there
   52519                 : ** are no active cursors, it also releases the read lock.
   52520                 : */
   52521           20083 : SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p){
   52522                 :   int rc;
   52523           20083 :   BtShared *pBt = p->pBt;
   52524                 :   MemPage *pPage1;
   52525                 : 
   52526           20083 :   sqlite3BtreeEnter(p);
   52527           20083 :   rc = saveAllCursors(pBt, 0, 0);
   52528                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   52529           20083 :   if( rc!=SQLITE_OK ){
   52530                 :     /* This is a horrible situation. An IO or malloc() error occurred whilst
   52531                 :     ** trying to save cursor positions. If this is an automatic rollback (as
   52532                 :     ** the result of a constraint, malloc() failure or IO error) then 
   52533                 :     ** the cache may be internally inconsistent (not contain valid trees) so
   52534                 :     ** we cannot simply return the error to the caller. Instead, abort 
   52535                 :     ** all queries that may be using any of the cursors that failed to save.
   52536                 :     */
   52537               0 :     sqlite3BtreeTripAllCursors(p, rc);
   52538                 :   }
   52539                 : #endif
   52540           20083 :   btreeIntegrity(p);
   52541                 : 
   52542           20083 :   if( p->inTrans==TRANS_WRITE ){
   52543                 :     int rc2;
   52544                 : 
   52545           16354 :     assert( TRANS_WRITE==pBt->inTransaction );
   52546           16354 :     rc2 = sqlite3PagerRollback(pBt->pPager);
   52547           16354 :     if( rc2!=SQLITE_OK ){
   52548               0 :       rc = rc2;
   52549                 :     }
   52550                 : 
   52551                 :     /* The rollback may have destroyed the pPage1->aData value.  So
   52552                 :     ** call btreeGetPage() on page 1 again to make
   52553                 :     ** sure pPage1->aData is set correctly. */
   52554           16354 :     if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
   52555           15368 :       int nPage = get4byte(28+(u8*)pPage1->aData);
   52556                 :       testcase( nPage==0 );
   52557           15368 :       if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
   52558                 :       testcase( pBt->nPage!=nPage );
   52559           15368 :       pBt->nPage = nPage;
   52560           15368 :       releasePage(pPage1);
   52561                 :     }
   52562           16354 :     assert( countWriteCursors(pBt)==0 );
   52563           16354 :     pBt->inTransaction = TRANS_READ;
   52564                 :   }
   52565                 : 
   52566           20083 :   btreeEndTransaction(p);
   52567           20083 :   sqlite3BtreeLeave(p);
   52568           20083 :   return rc;
   52569                 : }
   52570                 : 
   52571                 : /*
   52572                 : ** Start a statement subtransaction. The subtransaction can can be rolled
   52573                 : ** back independently of the main transaction. You must start a transaction 
   52574                 : ** before starting a subtransaction. The subtransaction is ended automatically 
   52575                 : ** if the main transaction commits or rolls back.
   52576                 : **
   52577                 : ** Statement subtransactions are used around individual SQL statements
   52578                 : ** that are contained within a BEGIN...COMMIT block.  If a constraint
   52579                 : ** error occurs within the statement, the effect of that one statement
   52580                 : ** can be rolled back without having to rollback the entire transaction.
   52581                 : **
   52582                 : ** A statement sub-transaction is implemented as an anonymous savepoint. The
   52583                 : ** value passed as the second parameter is the total number of savepoints,
   52584                 : ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
   52585                 : ** are no active savepoints and no other statement-transactions open,
   52586                 : ** iStatement is 1. This anonymous savepoint can be released or rolled back
   52587                 : ** using the sqlite3BtreeSavepoint() function.
   52588                 : */
   52589           18795 : SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
   52590                 :   int rc;
   52591           18795 :   BtShared *pBt = p->pBt;
   52592           18795 :   sqlite3BtreeEnter(p);
   52593           18795 :   assert( p->inTrans==TRANS_WRITE );
   52594           18795 :   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
   52595           18795 :   assert( iStatement>0 );
   52596           18795 :   assert( iStatement>p->db->nSavepoint );
   52597           18795 :   assert( pBt->inTransaction==TRANS_WRITE );
   52598                 :   /* At the pager level, a statement transaction is a savepoint with
   52599                 :   ** an index greater than all savepoints created explicitly using
   52600                 :   ** SQL statements. It is illegal to open, release or rollback any
   52601                 :   ** such savepoints while the statement transaction savepoint is active.
   52602                 :   */
   52603           18795 :   rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
   52604           18795 :   sqlite3BtreeLeave(p);
   52605           18795 :   return rc;
   52606                 : }
   52607                 : 
   52608                 : /*
   52609                 : ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
   52610                 : ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
   52611                 : ** savepoint identified by parameter iSavepoint, depending on the value 
   52612                 : ** of op.
   52613                 : **
   52614                 : ** Normally, iSavepoint is greater than or equal to zero. However, if op is
   52615                 : ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 
   52616                 : ** contents of the entire transaction are rolled back. This is different
   52617                 : ** from a normal transaction rollback, as no locks are released and the
   52618                 : ** transaction remains open.
   52619                 : */
   52620           45504 : SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
   52621           45504 :   int rc = SQLITE_OK;
   52622           45504 :   if( p && p->inTrans==TRANS_WRITE ){
   52623           30630 :     BtShared *pBt = p->pBt;
   52624           30630 :     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
   52625           30630 :     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
   52626           30630 :     sqlite3BtreeEnter(p);
   52627           30630 :     rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
   52628           30630 :     if( rc==SQLITE_OK ){
   52629           30630 :       if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
   52630               0 :         pBt->nPage = 0;
   52631                 :       }
   52632           30630 :       rc = newDatabase(pBt);
   52633           30630 :       pBt->nPage = get4byte(28 + pBt->pPage1->aData);
   52634                 : 
   52635                 :       /* The database size was written into the offset 28 of the header
   52636                 :       ** when the transaction started, so we know that the value at offset
   52637                 :       ** 28 is nonzero. */
   52638           30630 :       assert( pBt->nPage>0 );
   52639                 :     }
   52640           30630 :     sqlite3BtreeLeave(p);
   52641                 :   }
   52642           45504 :   return rc;
   52643                 : }
   52644                 : 
   52645                 : /*
   52646                 : ** Create a new cursor for the BTree whose root is on the page
   52647                 : ** iTable. If a read-only cursor is requested, it is assumed that
   52648                 : ** the caller already has at least a read-only transaction open
   52649                 : ** on the database already. If a write-cursor is requested, then
   52650                 : ** the caller is assumed to have an open write transaction.
   52651                 : **
   52652                 : ** If wrFlag==0, then the cursor can only be used for reading.
   52653                 : ** If wrFlag==1, then the cursor can be used for reading or for
   52654                 : ** writing if other conditions for writing are also met.  These
   52655                 : ** are the conditions that must be met in order for writing to
   52656                 : ** be allowed:
   52657                 : **
   52658                 : ** 1:  The cursor must have been opened with wrFlag==1
   52659                 : **
   52660                 : ** 2:  Other database connections that share the same pager cache
   52661                 : **     but which are not in the READ_UNCOMMITTED state may not have
   52662                 : **     cursors open with wrFlag==0 on the same table.  Otherwise
   52663                 : **     the changes made by this write cursor would be visible to
   52664                 : **     the read cursors in the other database connection.
   52665                 : **
   52666                 : ** 3:  The database must be writable (not on read-only media)
   52667                 : **
   52668                 : ** 4:  There must be an active transaction.
   52669                 : **
   52670                 : ** No checking is done to make sure that page iTable really is the
   52671                 : ** root page of a b-tree.  If it is not, then the cursor acquired
   52672                 : ** will not work correctly.
   52673                 : **
   52674                 : ** It is assumed that the sqlite3BtreeCursorZero() has been called
   52675                 : ** on pCur to initialize the memory space prior to invoking this routine.
   52676                 : */
   52677          532829 : static int btreeCursor(
   52678                 :   Btree *p,                              /* The btree */
   52679                 :   int iTable,                            /* Root page of table to open */
   52680                 :   int wrFlag,                            /* 1 to write. 0 read-only */
   52681                 :   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
   52682                 :   BtCursor *pCur                         /* Space for new cursor */
   52683                 : ){
   52684          532829 :   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
   52685                 : 
   52686          532829 :   assert( sqlite3BtreeHoldsMutex(p) );
   52687          532829 :   assert( wrFlag==0 || wrFlag==1 );
   52688                 : 
   52689                 :   /* The following assert statements verify that if this is a sharable 
   52690                 :   ** b-tree database, the connection is holding the required table locks, 
   52691                 :   ** and that no other connection has any open cursor that conflicts with 
   52692                 :   ** this lock.  */
   52693          532829 :   assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
   52694          532829 :   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
   52695                 : 
   52696                 :   /* Assert that the caller has opened the required transaction. */
   52697          532829 :   assert( p->inTrans>TRANS_NONE );
   52698          532829 :   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
   52699          532829 :   assert( pBt->pPage1 && pBt->pPage1->aData );
   52700                 : 
   52701          532829 :   if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
   52702               0 :     return SQLITE_READONLY;
   52703                 :   }
   52704          532829 :   if( iTable==1 && btreePagecount(pBt)==0 ){
   52705            2004 :     assert( wrFlag==0 );
   52706            2004 :     iTable = 0;
   52707                 :   }
   52708                 : 
   52709                 :   /* Now that no other errors can occur, finish filling in the BtCursor
   52710                 :   ** variables and link the cursor into the BtShared list.  */
   52711          532829 :   pCur->pgnoRoot = (Pgno)iTable;
   52712          532829 :   pCur->iPage = -1;
   52713          532829 :   pCur->pKeyInfo = pKeyInfo;
   52714          532829 :   pCur->pBtree = p;
   52715          532829 :   pCur->pBt = pBt;
   52716          532829 :   pCur->wrFlag = (u8)wrFlag;
   52717          532829 :   pCur->pNext = pBt->pCursor;
   52718          532829 :   if( pCur->pNext ){
   52719          313967 :     pCur->pNext->pPrev = pCur;
   52720                 :   }
   52721          532829 :   pBt->pCursor = pCur;
   52722          532829 :   pCur->eState = CURSOR_INVALID;
   52723          532829 :   pCur->cachedRowid = 0;
   52724          532829 :   return SQLITE_OK;
   52725                 : }
   52726          532829 : SQLITE_PRIVATE int sqlite3BtreeCursor(
   52727                 :   Btree *p,                                   /* The btree */
   52728                 :   int iTable,                                 /* Root page of table to open */
   52729                 :   int wrFlag,                                 /* 1 to write. 0 read-only */
   52730                 :   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
   52731                 :   BtCursor *pCur                              /* Write new cursor here */
   52732                 : ){
   52733                 :   int rc;
   52734          532829 :   sqlite3BtreeEnter(p);
   52735          532829 :   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
   52736          532829 :   sqlite3BtreeLeave(p);
   52737          532829 :   return rc;
   52738                 : }
   52739                 : 
   52740                 : /*
   52741                 : ** Return the size of a BtCursor object in bytes.
   52742                 : **
   52743                 : ** This interfaces is needed so that users of cursors can preallocate
   52744                 : ** sufficient storage to hold a cursor.  The BtCursor object is opaque
   52745                 : ** to users so they cannot do the sizeof() themselves - they must call
   52746                 : ** this routine.
   52747                 : */
   52748          539715 : SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
   52749          539715 :   return ROUND8(sizeof(BtCursor));
   52750                 : }
   52751                 : 
   52752                 : /*
   52753                 : ** Initialize memory that will be converted into a BtCursor object.
   52754                 : **
   52755                 : ** The simple approach here would be to memset() the entire object
   52756                 : ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
   52757                 : ** do not need to be zeroed and they are large, so we can save a lot
   52758                 : ** of run-time by skipping the initialization of those elements.
   52759                 : */
   52760          539715 : SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
   52761          539715 :   memset(p, 0, offsetof(BtCursor, iPage));
   52762          539715 : }
   52763                 : 
   52764                 : /*
   52765                 : ** Set the cached rowid value of every cursor in the same database file
   52766                 : ** as pCur and having the same root page number as pCur.  The value is
   52767                 : ** set to iRowid.
   52768                 : **
   52769                 : ** Only positive rowid values are considered valid for this cache.
   52770                 : ** The cache is initialized to zero, indicating an invalid cache.
   52771                 : ** A btree will work fine with zero or negative rowids.  We just cannot
   52772                 : ** cache zero or negative rowids, which means tables that use zero or
   52773                 : ** negative rowids might run a little slower.  But in practice, zero
   52774                 : ** or negative rowids are very uncommon so this should not be a problem.
   52775                 : */
   52776          167037 : SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
   52777                 :   BtCursor *p;
   52778          578914 :   for(p=pCur->pBt->pCursor; p; p=p->pNext){
   52779          411877 :     if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
   52780                 :   }
   52781          167037 :   assert( pCur->cachedRowid==iRowid );
   52782          167037 : }
   52783                 : 
   52784                 : /*
   52785                 : ** Return the cached rowid for the given cursor.  A negative or zero
   52786                 : ** return value indicates that the rowid cache is invalid and should be
   52787                 : ** ignored.  If the rowid cache has never before been set, then a
   52788                 : ** zero is returned.
   52789                 : */
   52790           57443 : SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
   52791           57443 :   return pCur->cachedRowid;
   52792                 : }
   52793                 : 
   52794                 : /*
   52795                 : ** Close a cursor.  The read lock on the database file is released
   52796                 : ** when the last cursor is closed.
   52797                 : */
   52798          539715 : SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
   52799          539715 :   Btree *pBtree = pCur->pBtree;
   52800          539715 :   if( pBtree ){
   52801                 :     int i;
   52802          532829 :     BtShared *pBt = pCur->pBt;
   52803          532829 :     sqlite3BtreeEnter(pBtree);
   52804          532829 :     sqlite3BtreeClearCursor(pCur);
   52805          532829 :     if( pCur->pPrev ){
   52806          178594 :       pCur->pPrev->pNext = pCur->pNext;
   52807                 :     }else{
   52808          354235 :       pBt->pCursor = pCur->pNext;
   52809                 :     }
   52810          532829 :     if( pCur->pNext ){
   52811          186305 :       pCur->pNext->pPrev = pCur->pPrev;
   52812                 :     }
   52813         1006141 :     for(i=0; i<=pCur->iPage; i++){
   52814          473312 :       releasePage(pCur->apPage[i]);
   52815                 :     }
   52816          532829 :     unlockBtreeIfUnused(pBt);
   52817          532829 :     invalidateOverflowCache(pCur);
   52818                 :     /* sqlite3_free(pCur); */
   52819          532829 :     sqlite3BtreeLeave(pBtree);
   52820                 :   }
   52821          539715 :   return SQLITE_OK;
   52822                 : }
   52823                 : 
   52824                 : /*
   52825                 : ** Make sure the BtCursor* given in the argument has a valid
   52826                 : ** BtCursor.info structure.  If it is not already valid, call
   52827                 : ** btreeParseCell() to fill it in.
   52828                 : **
   52829                 : ** BtCursor.info is a cache of the information in the current cell.
   52830                 : ** Using this cache reduces the number of calls to btreeParseCell().
   52831                 : **
   52832                 : ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
   52833                 : ** compiler to crash when getCellInfo() is implemented as a macro.
   52834                 : ** But there is a measureable speed advantage to using the macro on gcc
   52835                 : ** (when less compiler optimizations like -Os or -O0 are used and the
   52836                 : ** compiler is not doing agressive inlining.)  So we use a real function
   52837                 : ** for MSVC and a macro for everything else.  Ticket #2457.
   52838                 : */
   52839                 : #ifndef NDEBUG
   52840          224100 :   static void assertCellInfo(BtCursor *pCur){
   52841                 :     CellInfo info;
   52842          224100 :     int iPage = pCur->iPage;
   52843          224100 :     memset(&info, 0, sizeof(info));
   52844          224100 :     btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
   52845          224100 :     assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
   52846          224100 :   }
   52847                 : #else
   52848                 :   #define assertCellInfo(x)
   52849                 : #endif
   52850                 : #ifdef _MSC_VER
   52851                 :   /* Use a real function in MSVC to work around bugs in that compiler. */
   52852                 :   static void getCellInfo(BtCursor *pCur){
   52853                 :     if( pCur->info.nSize==0 ){
   52854                 :       int iPage = pCur->iPage;
   52855                 :       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
   52856                 :       pCur->validNKey = 1;
   52857                 :     }else{
   52858                 :       assertCellInfo(pCur);
   52859                 :     }
   52860                 :   }
   52861                 : #else /* if not _MSC_VER */
   52862                 :   /* Use a macro in all other compilers so that the function is inlined */
   52863                 : #define getCellInfo(pCur)                                                      \
   52864                 :   if( pCur->info.nSize==0 ){                                                   \
   52865                 :     int iPage = pCur->iPage;                                                   \
   52866                 :     btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
   52867                 :     pCur->validNKey = 1;                                                       \
   52868                 :   }else{                                                                       \
   52869                 :     assertCellInfo(pCur);                                                      \
   52870                 :   }
   52871                 : #endif /* _MSC_VER */
   52872                 : 
   52873                 : #ifndef NDEBUG  /* The next routine used only within assert() statements */
   52874                 : /*
   52875                 : ** Return true if the given BtCursor is valid.  A valid cursor is one
   52876                 : ** that is currently pointing to a row in a (non-empty) table.
   52877                 : ** This is a verification routine is used only within assert() statements.
   52878                 : */
   52879         1262919 : SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
   52880         1262919 :   return pCur && pCur->eState==CURSOR_VALID;
   52881                 : }
   52882                 : #endif /* NDEBUG */
   52883                 : 
   52884                 : /*
   52885                 : ** Set *pSize to the size of the buffer needed to hold the value of
   52886                 : ** the key for the current entry.  If the cursor is not pointing
   52887                 : ** to a valid entry, *pSize is set to 0. 
   52888                 : **
   52889                 : ** For a table with the INTKEY flag set, this routine returns the key
   52890                 : ** itself, not the number of bytes in the key.
   52891                 : **
   52892                 : ** The caller must position the cursor prior to invoking this routine.
   52893                 : ** 
   52894                 : ** This routine cannot fail.  It always returns SQLITE_OK.  
   52895                 : */
   52896          454498 : SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
   52897          454498 :   assert( cursorHoldsMutex(pCur) );
   52898          454498 :   assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
   52899          454498 :   if( pCur->eState!=CURSOR_VALID ){
   52900               0 :     *pSize = 0;
   52901                 :   }else{
   52902          454498 :     getCellInfo(pCur);
   52903          454498 :     *pSize = pCur->info.nKey;
   52904                 :   }
   52905          454498 :   return SQLITE_OK;
   52906                 : }
   52907                 : 
   52908                 : /*
   52909                 : ** Set *pSize to the number of bytes of data in the entry the
   52910                 : ** cursor currently points to.
   52911                 : **
   52912                 : ** The caller must guarantee that the cursor is pointing to a non-NULL
   52913                 : ** valid entry.  In other words, the calling procedure must guarantee
   52914                 : ** that the cursor has Cursor.eState==CURSOR_VALID.
   52915                 : **
   52916                 : ** Failure is not possible.  This function always returns SQLITE_OK.
   52917                 : ** It might just as well be a procedure (returning void) but we continue
   52918                 : ** to return an integer result code for historical reasons.
   52919                 : */
   52920          506421 : SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
   52921          506421 :   assert( cursorHoldsMutex(pCur) );
   52922          506422 :   assert( pCur->eState==CURSOR_VALID );
   52923          506422 :   getCellInfo(pCur);
   52924          506422 :   *pSize = pCur->info.nData;
   52925          506422 :   return SQLITE_OK;
   52926                 : }
   52927                 : 
   52928                 : /*
   52929                 : ** Given the page number of an overflow page in the database (parameter
   52930                 : ** ovfl), this function finds the page number of the next page in the 
   52931                 : ** linked list of overflow pages. If possible, it uses the auto-vacuum
   52932                 : ** pointer-map data instead of reading the content of page ovfl to do so. 
   52933                 : **
   52934                 : ** If an error occurs an SQLite error code is returned. Otherwise:
   52935                 : **
   52936                 : ** The page number of the next overflow page in the linked list is 
   52937                 : ** written to *pPgnoNext. If page ovfl is the last page in its linked 
   52938                 : ** list, *pPgnoNext is set to zero. 
   52939                 : **
   52940                 : ** If ppPage is not NULL, and a reference to the MemPage object corresponding
   52941                 : ** to page number pOvfl was obtained, then *ppPage is set to point to that
   52942                 : ** reference. It is the responsibility of the caller to call releasePage()
   52943                 : ** on *ppPage to free the reference. In no reference was obtained (because
   52944                 : ** the pointer-map was used to obtain the value for *pPgnoNext), then
   52945                 : ** *ppPage is set to zero.
   52946                 : */
   52947               0 : static int getOverflowPage(
   52948                 :   BtShared *pBt,               /* The database file */
   52949                 :   Pgno ovfl,                   /* Current overflow page number */
   52950                 :   MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
   52951                 :   Pgno *pPgnoNext              /* OUT: Next overflow page number */
   52952                 : ){
   52953               0 :   Pgno next = 0;
   52954               0 :   MemPage *pPage = 0;
   52955               0 :   int rc = SQLITE_OK;
   52956                 : 
   52957               0 :   assert( sqlite3_mutex_held(pBt->mutex) );
   52958               0 :   assert(pPgnoNext);
   52959                 : 
   52960                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   52961                 :   /* Try to find the next page in the overflow list using the
   52962                 :   ** autovacuum pointer-map pages. Guess that the next page in 
   52963                 :   ** the overflow list is page number (ovfl+1). If that guess turns 
   52964                 :   ** out to be wrong, fall back to loading the data of page 
   52965                 :   ** number ovfl to determine the next page number.
   52966                 :   */
   52967               0 :   if( pBt->autoVacuum ){
   52968                 :     Pgno pgno;
   52969               0 :     Pgno iGuess = ovfl+1;
   52970                 :     u8 eType;
   52971                 : 
   52972               0 :     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
   52973               0 :       iGuess++;
   52974                 :     }
   52975                 : 
   52976               0 :     if( iGuess<=btreePagecount(pBt) ){
   52977               0 :       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
   52978               0 :       if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
   52979               0 :         next = iGuess;
   52980               0 :         rc = SQLITE_DONE;
   52981                 :       }
   52982                 :     }
   52983                 :   }
   52984                 : #endif
   52985                 : 
   52986               0 :   assert( next==0 || rc==SQLITE_DONE );
   52987               0 :   if( rc==SQLITE_OK ){
   52988               0 :     rc = btreeGetPage(pBt, ovfl, &pPage, 0);
   52989               0 :     assert( rc==SQLITE_OK || pPage==0 );
   52990               0 :     if( rc==SQLITE_OK ){
   52991               0 :       next = get4byte(pPage->aData);
   52992                 :     }
   52993                 :   }
   52994                 : 
   52995               0 :   *pPgnoNext = next;
   52996               0 :   if( ppPage ){
   52997               0 :     *ppPage = pPage;
   52998                 :   }else{
   52999               0 :     releasePage(pPage);
   53000                 :   }
   53001               0 :   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
   53002                 : }
   53003                 : 
   53004                 : /*
   53005                 : ** Copy data from a buffer to a page, or from a page to a buffer.
   53006                 : **
   53007                 : ** pPayload is a pointer to data stored on database page pDbPage.
   53008                 : ** If argument eOp is false, then nByte bytes of data are copied
   53009                 : ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
   53010                 : ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
   53011                 : ** of data are copied from the buffer pBuf to pPayload.
   53012                 : **
   53013                 : ** SQLITE_OK is returned on success, otherwise an error code.
   53014                 : */
   53015            2169 : static int copyPayload(
   53016                 :   void *pPayload,           /* Pointer to page data */
   53017                 :   void *pBuf,               /* Pointer to buffer */
   53018                 :   int nByte,                /* Number of bytes to copy */
   53019                 :   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
   53020                 :   DbPage *pDbPage           /* Page containing pPayload */
   53021                 : ){
   53022            2169 :   if( eOp ){
   53023                 :     /* Copy data from buffer to page (a write operation) */
   53024               0 :     int rc = sqlite3PagerWrite(pDbPage);
   53025               0 :     if( rc!=SQLITE_OK ){
   53026               0 :       return rc;
   53027                 :     }
   53028               0 :     memcpy(pPayload, pBuf, nByte);
   53029                 :   }else{
   53030                 :     /* Copy data from page to buffer (a read operation) */
   53031            2169 :     memcpy(pBuf, pPayload, nByte);
   53032                 :   }
   53033            2169 :   return SQLITE_OK;
   53034                 : }
   53035                 : 
   53036                 : /*
   53037                 : ** This function is used to read or overwrite payload information
   53038                 : ** for the entry that the pCur cursor is pointing to. If the eOp
   53039                 : ** parameter is 0, this is a read operation (data copied into
   53040                 : ** buffer pBuf). If it is non-zero, a write (data copied from
   53041                 : ** buffer pBuf).
   53042                 : **
   53043                 : ** A total of "amt" bytes are read or written beginning at "offset".
   53044                 : ** Data is read to or from the buffer pBuf.
   53045                 : **
   53046                 : ** The content being read or written might appear on the main page
   53047                 : ** or be scattered out on multiple overflow pages.
   53048                 : **
   53049                 : ** If the BtCursor.isIncrblobHandle flag is set, and the current
   53050                 : ** cursor entry uses one or more overflow pages, this function
   53051                 : ** allocates space for and lazily popluates the overflow page-list 
   53052                 : ** cache array (BtCursor.aOverflow). Subsequent calls use this
   53053                 : ** cache to make seeking to the supplied offset more efficient.
   53054                 : **
   53055                 : ** Once an overflow page-list cache has been allocated, it may be
   53056                 : ** invalidated if some other cursor writes to the same table, or if
   53057                 : ** the cursor is moved to a different row. Additionally, in auto-vacuum
   53058                 : ** mode, the following events may invalidate an overflow page-list cache.
   53059                 : **
   53060                 : **   * An incremental vacuum,
   53061                 : **   * A commit in auto_vacuum="full" mode,
   53062                 : **   * Creating a table (may require moving an overflow page).
   53063                 : */
   53064            2168 : static int accessPayload(
   53065                 :   BtCursor *pCur,      /* Cursor pointing to entry to read from */
   53066                 :   u32 offset,          /* Begin reading this far into payload */
   53067                 :   u32 amt,             /* Read this many bytes */
   53068                 :   unsigned char *pBuf, /* Write the bytes into this buffer */ 
   53069                 :   int eOp              /* zero to read. non-zero to write. */
   53070                 : ){
   53071                 :   unsigned char *aPayload;
   53072            2168 :   int rc = SQLITE_OK;
   53073                 :   u32 nKey;
   53074            2168 :   int iIdx = 0;
   53075            2168 :   MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
   53076            2168 :   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
   53077                 : 
   53078            2168 :   assert( pPage );
   53079            2168 :   assert( pCur->eState==CURSOR_VALID );
   53080            2168 :   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
   53081            2168 :   assert( cursorHoldsMutex(pCur) );
   53082                 : 
   53083            2168 :   getCellInfo(pCur);
   53084            2168 :   aPayload = pCur->info.pCell + pCur->info.nHeader;
   53085            2168 :   nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
   53086                 : 
   53087            2168 :   if( NEVER(offset+amt > nKey+pCur->info.nData) 
   53088            2168 :    || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
   53089                 :   ){
   53090                 :     /* Trying to read or write past the end of the data is an error */
   53091               0 :     return SQLITE_CORRUPT_BKPT;
   53092                 :   }
   53093                 : 
   53094                 :   /* Check if data must be read/written to/from the btree page itself. */
   53095            2168 :   if( offset<pCur->info.nLocal ){
   53096            2152 :     int a = amt;
   53097            2152 :     if( a+offset>pCur->info.nLocal ){
   53098               1 :       a = pCur->info.nLocal - offset;
   53099                 :     }
   53100            2152 :     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
   53101            2152 :     offset = 0;
   53102            2152 :     pBuf += a;
   53103            2152 :     amt -= a;
   53104                 :   }else{
   53105              16 :     offset -= pCur->info.nLocal;
   53106                 :   }
   53107                 : 
   53108            2168 :   if( rc==SQLITE_OK && amt>0 ){
   53109              17 :     const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
   53110                 :     Pgno nextPage;
   53111                 : 
   53112              17 :     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
   53113                 : 
   53114                 : #ifndef SQLITE_OMIT_INCRBLOB
   53115                 :     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
   53116                 :     ** has not been allocated, allocate it now. The array is sized at
   53117                 :     ** one entry for each overflow page in the overflow chain. The
   53118                 :     ** page number of the first overflow page is stored in aOverflow[0],
   53119                 :     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
   53120                 :     ** (the cache is lazily populated).
   53121                 :     */
   53122              17 :     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
   53123               0 :       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
   53124               0 :       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
   53125                 :       /* nOvfl is always positive.  If it were zero, fetchPayload would have
   53126                 :       ** been used instead of this routine. */
   53127               0 :       if( ALWAYS(nOvfl) && !pCur->aOverflow ){
   53128               0 :         rc = SQLITE_NOMEM;
   53129                 :       }
   53130                 :     }
   53131                 : 
   53132                 :     /* If the overflow page-list cache has been allocated and the
   53133                 :     ** entry for the first required overflow page is valid, skip
   53134                 :     ** directly to it.
   53135                 :     */
   53136              17 :     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
   53137               0 :       iIdx = (offset/ovflSize);
   53138               0 :       nextPage = pCur->aOverflow[iIdx];
   53139               0 :       offset = (offset%ovflSize);
   53140                 :     }
   53141                 : #endif
   53142                 : 
   53143              34 :     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
   53144                 : 
   53145                 : #ifndef SQLITE_OMIT_INCRBLOB
   53146                 :       /* If required, populate the overflow page-list cache. */
   53147              17 :       if( pCur->aOverflow ){
   53148               0 :         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
   53149               0 :         pCur->aOverflow[iIdx] = nextPage;
   53150                 :       }
   53151                 : #endif
   53152                 : 
   53153              17 :       if( offset>=ovflSize ){
   53154                 :         /* The only reason to read this page is to obtain the page
   53155                 :         ** number for the next page in the overflow chain. The page
   53156                 :         ** data is not required. So first try to lookup the overflow
   53157                 :         ** page-list cache, if any, then fall back to the getOverflowPage()
   53158                 :         ** function.
   53159                 :         */
   53160                 : #ifndef SQLITE_OMIT_INCRBLOB
   53161               0 :         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
   53162               0 :           nextPage = pCur->aOverflow[iIdx+1];
   53163                 :         } else 
   53164                 : #endif
   53165               0 :           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
   53166               0 :         offset -= ovflSize;
   53167                 :       }else{
   53168                 :         /* Need to read this page properly. It contains some of the
   53169                 :         ** range of data that is being read (eOp==0) or written (eOp!=0).
   53170                 :         */
   53171                 : #ifdef SQLITE_DIRECT_OVERFLOW_READ
   53172                 :         sqlite3_file *fd;
   53173                 : #endif
   53174              17 :         int a = amt;
   53175              17 :         if( a + offset > ovflSize ){
   53176               0 :           a = ovflSize - offset;
   53177                 :         }
   53178                 : 
   53179                 : #ifdef SQLITE_DIRECT_OVERFLOW_READ
   53180                 :         /* If all the following are true:
   53181                 :         **
   53182                 :         **   1) this is a read operation, and 
   53183                 :         **   2) data is required from the start of this overflow page, and
   53184                 :         **   3) the database is file-backed, and
   53185                 :         **   4) there is no open write-transaction, and
   53186                 :         **   5) the database is not a WAL database,
   53187                 :         **
   53188                 :         ** then data can be read directly from the database file into the
   53189                 :         ** output buffer, bypassing the page-cache altogether. This speeds
   53190                 :         ** up loading large records that span many overflow pages.
   53191                 :         */
   53192                 :         if( eOp==0                                             /* (1) */
   53193                 :          && offset==0                                          /* (2) */
   53194                 :          && pBt->inTransaction==TRANS_READ                     /* (4) */
   53195                 :          && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
   53196                 :          && pBt->pPage1->aData[19]==0x01                       /* (5) */
   53197                 :         ){
   53198                 :           u8 aSave[4];
   53199                 :           u8 *aWrite = &pBuf[-4];
   53200                 :           memcpy(aSave, aWrite, 4);
   53201                 :           rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
   53202                 :           nextPage = get4byte(aWrite);
   53203                 :           memcpy(aWrite, aSave, 4);
   53204                 :         }else
   53205                 : #endif
   53206                 : 
   53207                 :         {
   53208                 :           DbPage *pDbPage;
   53209              17 :           rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
   53210              17 :           if( rc==SQLITE_OK ){
   53211              17 :             aPayload = sqlite3PagerGetData(pDbPage);
   53212              17 :             nextPage = get4byte(aPayload);
   53213              17 :             rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
   53214              17 :             sqlite3PagerUnref(pDbPage);
   53215              17 :             offset = 0;
   53216                 :           }
   53217                 :         }
   53218              17 :         amt -= a;
   53219              17 :         pBuf += a;
   53220                 :       }
   53221                 :     }
   53222                 :   }
   53223                 : 
   53224            2168 :   if( rc==SQLITE_OK && amt>0 ){
   53225               0 :     return SQLITE_CORRUPT_BKPT;
   53226                 :   }
   53227            2168 :   return rc;
   53228                 : }
   53229                 : 
   53230                 : /*
   53231                 : ** Read part of the key associated with cursor pCur.  Exactly
   53232                 : ** "amt" bytes will be transfered into pBuf[].  The transfer
   53233                 : ** begins at "offset".
   53234                 : **
   53235                 : ** The caller must ensure that pCur is pointing to a valid row
   53236                 : ** in the table.
   53237                 : **
   53238                 : ** Return SQLITE_OK on success or an error code if anything goes
   53239                 : ** wrong.  An error is returned if "offset+amt" is larger than
   53240                 : ** the available payload.
   53241                 : */
   53242            1624 : SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
   53243            1624 :   assert( cursorHoldsMutex(pCur) );
   53244            1624 :   assert( pCur->eState==CURSOR_VALID );
   53245            1624 :   assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
   53246            1624 :   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
   53247            1624 :   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
   53248                 : }
   53249                 : 
   53250                 : /*
   53251                 : ** Read part of the data associated with cursor pCur.  Exactly
   53252                 : ** "amt" bytes will be transfered into pBuf[].  The transfer
   53253                 : ** begins at "offset".
   53254                 : **
   53255                 : ** Return SQLITE_OK on success or an error code if anything goes
   53256                 : ** wrong.  An error is returned if "offset+amt" is larger than
   53257                 : ** the available payload.
   53258                 : */
   53259             544 : SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
   53260                 :   int rc;
   53261                 : 
   53262                 : #ifndef SQLITE_OMIT_INCRBLOB
   53263             544 :   if ( pCur->eState==CURSOR_INVALID ){
   53264               0 :     return SQLITE_ABORT;
   53265                 :   }
   53266                 : #endif
   53267                 : 
   53268             544 :   assert( cursorHoldsMutex(pCur) );
   53269             544 :   rc = restoreCursorPosition(pCur);
   53270             544 :   if( rc==SQLITE_OK ){
   53271             544 :     assert( pCur->eState==CURSOR_VALID );
   53272             544 :     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
   53273             544 :     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
   53274             544 :     rc = accessPayload(pCur, offset, amt, pBuf, 0);
   53275                 :   }
   53276             544 :   return rc;
   53277                 : }
   53278                 : 
   53279                 : /*
   53280                 : ** Return a pointer to payload information from the entry that the 
   53281                 : ** pCur cursor is pointing to.  The pointer is to the beginning of
   53282                 : ** the key if skipKey==0 and it points to the beginning of data if
   53283                 : ** skipKey==1.  The number of bytes of available key/data is written
   53284                 : ** into *pAmt.  If *pAmt==0, then the value returned will not be
   53285                 : ** a valid pointer.
   53286                 : **
   53287                 : ** This routine is an optimization.  It is common for the entire key
   53288                 : ** and data to fit on the local page and for there to be no overflow
   53289                 : ** pages.  When that is so, this routine can be used to access the
   53290                 : ** key and data without making a copy.  If the key and/or data spills
   53291                 : ** onto overflow pages, then accessPayload() must be used to reassemble
   53292                 : ** the key/data and copy it into a preallocated buffer.
   53293                 : **
   53294                 : ** The pointer returned by this routine looks directly into the cached
   53295                 : ** page of the database.  The data might change or move the next time
   53296                 : ** any btree routine is called.
   53297                 : */
   53298          886721 : static const unsigned char *fetchPayload(
   53299                 :   BtCursor *pCur,      /* Cursor pointing to entry to read from */
   53300                 :   int *pAmt,           /* Write the number of available bytes here */
   53301                 :   int skipKey          /* read beginning at data if this is true */
   53302                 : ){
   53303                 :   unsigned char *aPayload;
   53304                 :   MemPage *pPage;
   53305                 :   u32 nKey;
   53306                 :   u32 nLocal;
   53307                 : 
   53308          886721 :   assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
   53309          886720 :   assert( pCur->eState==CURSOR_VALID );
   53310          886720 :   assert( cursorHoldsMutex(pCur) );
   53311          886720 :   pPage = pCur->apPage[pCur->iPage];
   53312          886720 :   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
   53313          886720 :   if( NEVER(pCur->info.nSize==0) ){
   53314               0 :     btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
   53315                 :                    &pCur->info);
   53316                 :   }
   53317          886721 :   aPayload = pCur->info.pCell;
   53318          886721 :   aPayload += pCur->info.nHeader;
   53319          886721 :   if( pPage->intKey ){
   53320          505913 :     nKey = 0;
   53321                 :   }else{
   53322          380808 :     nKey = (int)pCur->info.nKey;
   53323                 :   }
   53324          886721 :   if( skipKey ){
   53325          505913 :     aPayload += nKey;
   53326          505913 :     nLocal = pCur->info.nLocal - nKey;
   53327                 :   }else{
   53328          380808 :     nLocal = pCur->info.nLocal;
   53329          380808 :     assert( nLocal<=nKey );
   53330                 :   }
   53331          886721 :   *pAmt = nLocal;
   53332          886721 :   return aPayload;
   53333                 : }
   53334                 : 
   53335                 : 
   53336                 : /*
   53337                 : ** For the entry that cursor pCur is point to, return as
   53338                 : ** many bytes of the key or data as are available on the local
   53339                 : ** b-tree page.  Write the number of available bytes into *pAmt.
   53340                 : **
   53341                 : ** The pointer returned is ephemeral.  The key/data may move
   53342                 : ** or be destroyed on the next call to any Btree routine,
   53343                 : ** including calls from other threads against the same cache.
   53344                 : ** Hence, a mutex on the BtShared should be held prior to calling
   53345                 : ** this routine.
   53346                 : **
   53347                 : ** These routines is used to get quick access to key and data
   53348                 : ** in the common case where no overflow pages are used.
   53349                 : */
   53350          380808 : SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
   53351          380808 :   const void *p = 0;
   53352          380808 :   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
   53353          380808 :   assert( cursorHoldsMutex(pCur) );
   53354          380808 :   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
   53355          380808 :     p = (const void*)fetchPayload(pCur, pAmt, 0);
   53356                 :   }
   53357          380808 :   return p;
   53358                 : }
   53359          505912 : SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
   53360          505912 :   const void *p = 0;
   53361          505912 :   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
   53362          505913 :   assert( cursorHoldsMutex(pCur) );
   53363          505913 :   if( ALWAYS(pCur->eState==CURSOR_VALID) ){
   53364          505913 :     p = (const void*)fetchPayload(pCur, pAmt, 1);
   53365                 :   }
   53366          505913 :   return p;
   53367                 : }
   53368                 : 
   53369                 : 
   53370                 : /*
   53371                 : ** Move the cursor down to a new child page.  The newPgno argument is the
   53372                 : ** page number of the child page to move to.
   53373                 : **
   53374                 : ** This function returns SQLITE_CORRUPT if the page-header flags field of
   53375                 : ** the new child page does not match the flags field of the parent (i.e.
   53376                 : ** if an intkey page appears to be the parent of a non-intkey page, or
   53377                 : ** vice-versa).
   53378                 : */
   53379           36311 : static int moveToChild(BtCursor *pCur, u32 newPgno){
   53380                 :   int rc;
   53381           36311 :   int i = pCur->iPage;
   53382                 :   MemPage *pNewPage;
   53383           36311 :   BtShared *pBt = pCur->pBt;
   53384                 : 
   53385           36311 :   assert( cursorHoldsMutex(pCur) );
   53386           36311 :   assert( pCur->eState==CURSOR_VALID );
   53387           36311 :   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
   53388           36311 :   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
   53389               0 :     return SQLITE_CORRUPT_BKPT;
   53390                 :   }
   53391           36311 :   rc = getAndInitPage(pBt, newPgno, &pNewPage);
   53392           36311 :   if( rc ) return rc;
   53393           36309 :   pCur->apPage[i+1] = pNewPage;
   53394           36309 :   pCur->aiIdx[i+1] = 0;
   53395           36309 :   pCur->iPage++;
   53396                 : 
   53397           36309 :   pCur->info.nSize = 0;
   53398           36309 :   pCur->validNKey = 0;
   53399           36309 :   if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
   53400               0 :     return SQLITE_CORRUPT_BKPT;
   53401                 :   }
   53402           36309 :   return SQLITE_OK;
   53403                 : }
   53404                 : 
   53405                 : #if 0
   53406                 : /*
   53407                 : ** Page pParent is an internal (non-leaf) tree page. This function 
   53408                 : ** asserts that page number iChild is the left-child if the iIdx'th
   53409                 : ** cell in page pParent. Or, if iIdx is equal to the total number of
   53410                 : ** cells in pParent, that page number iChild is the right-child of
   53411                 : ** the page.
   53412                 : */
   53413                 : static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
   53414                 :   assert( iIdx<=pParent->nCell );
   53415                 :   if( iIdx==pParent->nCell ){
   53416                 :     assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
   53417                 :   }else{
   53418                 :     assert( get4byte(findCell(pParent, iIdx))==iChild );
   53419                 :   }
   53420                 : }
   53421                 : #else
   53422                 : #  define assertParentIndex(x,y,z) 
   53423                 : #endif
   53424                 : 
   53425                 : /*
   53426                 : ** Move the cursor up to the parent page.
   53427                 : **
   53428                 : ** pCur->idx is set to the cell index that contains the pointer
   53429                 : ** to the page we are coming from.  If we are coming from the
   53430                 : ** right-most child page then pCur->idx is set to one more than
   53431                 : ** the largest cell index.
   53432                 : */
   53433            2080 : static void moveToParent(BtCursor *pCur){
   53434            2080 :   assert( cursorHoldsMutex(pCur) );
   53435            2080 :   assert( pCur->eState==CURSOR_VALID );
   53436            2080 :   assert( pCur->iPage>0 );
   53437            2080 :   assert( pCur->apPage[pCur->iPage] );
   53438                 : 
   53439                 :   /* UPDATE: It is actually possible for the condition tested by the assert
   53440                 :   ** below to be untrue if the database file is corrupt. This can occur if
   53441                 :   ** one cursor has modified page pParent while a reference to it is held 
   53442                 :   ** by a second cursor. Which can only happen if a single page is linked
   53443                 :   ** into more than one b-tree structure in a corrupt database.  */
   53444                 : #if 0
   53445                 :   assertParentIndex(
   53446                 :     pCur->apPage[pCur->iPage-1], 
   53447                 :     pCur->aiIdx[pCur->iPage-1], 
   53448                 :     pCur->apPage[pCur->iPage]->pgno
   53449                 :   );
   53450                 : #endif
   53451                 :   testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
   53452                 : 
   53453            2080 :   releasePage(pCur->apPage[pCur->iPage]);
   53454            2080 :   pCur->iPage--;
   53455            2080 :   pCur->info.nSize = 0;
   53456            2080 :   pCur->validNKey = 0;
   53457            2080 : }
   53458                 : 
   53459                 : /*
   53460                 : ** Move the cursor to point to the root page of its b-tree structure.
   53461                 : **
   53462                 : ** If the table has a virtual root page, then the cursor is moved to point
   53463                 : ** to the virtual root page instead of the actual root page. A table has a
   53464                 : ** virtual root page when the actual root page contains no cells and a 
   53465                 : ** single child page. This can only happen with the table rooted at page 1.
   53466                 : **
   53467                 : ** If the b-tree structure is empty, the cursor state is set to 
   53468                 : ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
   53469                 : ** cell located on the root (or virtual root) page and the cursor state
   53470                 : ** is set to CURSOR_VALID.
   53471                 : **
   53472                 : ** If this function returns successfully, it may be assumed that the
   53473                 : ** page-header flags indicate that the [virtual] root-page is the expected 
   53474                 : ** kind of b-tree page (i.e. if when opening the cursor the caller did not
   53475                 : ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
   53476                 : ** indicating a table b-tree, or if the caller did specify a KeyInfo 
   53477                 : ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
   53478                 : ** b-tree).
   53479                 : */
   53480          679522 : static int moveToRoot(BtCursor *pCur){
   53481                 :   MemPage *pRoot;
   53482          679522 :   int rc = SQLITE_OK;
   53483          679522 :   Btree *p = pCur->pBtree;
   53484          679522 :   BtShared *pBt = p->pBt;
   53485                 : 
   53486          679522 :   assert( cursorHoldsMutex(pCur) );
   53487                 :   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
   53488                 :   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
   53489                 :   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
   53490          679522 :   if( pCur->eState>=CURSOR_REQUIRESEEK ){
   53491               2 :     if( pCur->eState==CURSOR_FAULT ){
   53492               0 :       assert( pCur->skipNext!=SQLITE_OK );
   53493               0 :       return pCur->skipNext;
   53494                 :     }
   53495               2 :     sqlite3BtreeClearCursor(pCur);
   53496                 :   }
   53497                 : 
   53498          679522 :   if( pCur->iPage>=0 ){
   53499                 :     int i;
   53500          237936 :     for(i=1; i<=pCur->iPage; i++){
   53501            6620 :       releasePage(pCur->apPage[i]);
   53502                 :     }
   53503          231316 :     pCur->iPage = 0;
   53504          448206 :   }else if( pCur->pgnoRoot==0 ){
   53505            2004 :     pCur->eState = CURSOR_INVALID;
   53506            2004 :     return SQLITE_OK;
   53507                 :   }else{
   53508          446202 :     rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
   53509          446202 :     if( rc!=SQLITE_OK ){
   53510               0 :       pCur->eState = CURSOR_INVALID;
   53511               0 :       return rc;
   53512                 :     }
   53513          446202 :     pCur->iPage = 0;
   53514                 : 
   53515                 :     /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
   53516                 :     ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
   53517                 :     ** NULL, the caller expects a table b-tree. If this is not the case,
   53518                 :     ** return an SQLITE_CORRUPT error.  */
   53519          446202 :     assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 );
   53520          446202 :     if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){
   53521               0 :       return SQLITE_CORRUPT_BKPT;
   53522                 :     }
   53523                 :   }
   53524                 : 
   53525                 :   /* Assert that the root page is of the correct type. This must be the
   53526                 :   ** case as the call to this function that loaded the root-page (either
   53527                 :   ** this call or a previous invocation) would have detected corruption 
   53528                 :   ** if the assumption were not true, and it is not possible for the flags 
   53529                 :   ** byte to have been modified while this cursor is holding a reference
   53530                 :   ** to the page.  */
   53531          677518 :   pRoot = pCur->apPage[0];
   53532          677518 :   assert( pRoot->pgno==pCur->pgnoRoot );
   53533          677518 :   assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey );
   53534                 : 
   53535          677518 :   pCur->aiIdx[0] = 0;
   53536          677518 :   pCur->info.nSize = 0;
   53537          677518 :   pCur->atLast = 0;
   53538          677518 :   pCur->validNKey = 0;
   53539                 : 
   53540          677525 :   if( pRoot->nCell==0 && !pRoot->leaf ){
   53541                 :     Pgno subpage;
   53542               7 :     if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
   53543               7 :     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
   53544               7 :     pCur->eState = CURSOR_VALID;
   53545               7 :     rc = moveToChild(pCur, subpage);
   53546                 :   }else{
   53547          677511 :     pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
   53548                 :   }
   53549          677518 :   return rc;
   53550                 : }
   53551                 : 
   53552                 : /*
   53553                 : ** Move the cursor down to the left-most leaf entry beneath the
   53554                 : ** entry to which it is currently pointing.
   53555                 : **
   53556                 : ** The left-most leaf is the one with the smallest key - the first
   53557                 : ** in ascending order.
   53558                 : */
   53559           44163 : static int moveToLeftmost(BtCursor *pCur){
   53560                 :   Pgno pgno;
   53561           44163 :   int rc = SQLITE_OK;
   53562                 :   MemPage *pPage;
   53563                 : 
   53564           44163 :   assert( cursorHoldsMutex(pCur) );
   53565           44163 :   assert( pCur->eState==CURSOR_VALID );
   53566           89890 :   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
   53567            1564 :     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
   53568            1564 :     pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
   53569            1564 :     rc = moveToChild(pCur, pgno);
   53570                 :   }
   53571           44163 :   return rc;
   53572                 : }
   53573                 : 
   53574                 : /*
   53575                 : ** Move the cursor down to the right-most leaf entry beneath the
   53576                 : ** page to which it is currently pointing.  Notice the difference
   53577                 : ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
   53578                 : ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
   53579                 : ** finds the right-most entry beneath the *page*.
   53580                 : **
   53581                 : ** The right-most entry is the one with the largest key - the last
   53582                 : ** key in ascending order.
   53583                 : */
   53584           52983 : static int moveToRightmost(BtCursor *pCur){
   53585                 :   Pgno pgno;
   53586           52983 :   int rc = SQLITE_OK;
   53587           52983 :   MemPage *pPage = 0;
   53588                 : 
   53589           52983 :   assert( cursorHoldsMutex(pCur) );
   53590           52983 :   assert( pCur->eState==CURSOR_VALID );
   53591          118634 :   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
   53592           12668 :     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
   53593           12668 :     pCur->aiIdx[pCur->iPage] = pPage->nCell;
   53594           12668 :     rc = moveToChild(pCur, pgno);
   53595                 :   }
   53596           52983 :   if( rc==SQLITE_OK ){
   53597           52983 :     pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
   53598           52983 :     pCur->info.nSize = 0;
   53599           52983 :     pCur->validNKey = 0;
   53600                 :   }
   53601           52983 :   return rc;
   53602                 : }
   53603                 : 
   53604                 : /* Move the cursor to the first entry in the table.  Return SQLITE_OK
   53605                 : ** on success.  Set *pRes to 0 if the cursor actually points to something
   53606                 : ** or set *pRes to 1 if the table is empty.
   53607                 : */
   53608           68049 : SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
   53609                 :   int rc;
   53610                 : 
   53611           68049 :   assert( cursorHoldsMutex(pCur) );
   53612           68049 :   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
   53613           68049 :   rc = moveToRoot(pCur);
   53614           68049 :   if( rc==SQLITE_OK ){
   53615           68049 :     if( pCur->eState==CURSOR_INVALID ){
   53616           25452 :       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
   53617           25452 :       *pRes = 1;
   53618                 :     }else{
   53619           42597 :       assert( pCur->apPage[pCur->iPage]->nCell>0 );
   53620           42597 :       *pRes = 0;
   53621           42597 :       rc = moveToLeftmost(pCur);
   53622                 :     }
   53623                 :   }
   53624           68049 :   return rc;
   53625                 : }
   53626                 : 
   53627                 : /* Move the cursor to the last entry in the table.  Return SQLITE_OK
   53628                 : ** on success.  Set *pRes to 0 if the cursor actually points to something
   53629                 : ** or set *pRes to 1 if the table is empty.
   53630                 : */
   53631           60914 : SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
   53632                 :   int rc;
   53633                 :  
   53634           60914 :   assert( cursorHoldsMutex(pCur) );
   53635           60914 :   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
   53636                 : 
   53637                 :   /* If the cursor already points to the last entry, this is a no-op. */
   53638           60914 :   if( CURSOR_VALID==pCur->eState && pCur->atLast ){
   53639                 : #ifdef SQLITE_DEBUG
   53640                 :     /* This block serves to assert() that the cursor really does point 
   53641                 :     ** to the last entry in the b-tree. */
   53642                 :     int ii;
   53643            1156 :     for(ii=0; ii<pCur->iPage; ii++){
   53644               0 :       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
   53645                 :     }
   53646            1156 :     assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
   53647            1156 :     assert( pCur->apPage[pCur->iPage]->leaf );
   53648                 : #endif
   53649            1156 :     return SQLITE_OK;
   53650                 :   }
   53651                 : 
   53652           59758 :   rc = moveToRoot(pCur);
   53653           59758 :   if( rc==SQLITE_OK ){
   53654           59758 :     if( CURSOR_INVALID==pCur->eState ){
   53655            6775 :       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
   53656            6775 :       *pRes = 1;
   53657                 :     }else{
   53658           52983 :       assert( pCur->eState==CURSOR_VALID );
   53659           52983 :       *pRes = 0;
   53660           52983 :       rc = moveToRightmost(pCur);
   53661           52983 :       pCur->atLast = rc==SQLITE_OK ?1:0;
   53662                 :     }
   53663                 :   }
   53664           59758 :   return rc;
   53665                 : }
   53666                 : 
   53667                 : /* Move the cursor so that it points to an entry near the key 
   53668                 : ** specified by pIdxKey or intKey.   Return a success code.
   53669                 : **
   53670                 : ** For INTKEY tables, the intKey parameter is used.  pIdxKey 
   53671                 : ** must be NULL.  For index tables, pIdxKey is used and intKey
   53672                 : ** is ignored.
   53673                 : **
   53674                 : ** If an exact match is not found, then the cursor is always
   53675                 : ** left pointing at a leaf page which would hold the entry if it
   53676                 : ** were present.  The cursor might point to an entry that comes
   53677                 : ** before or after the key.
   53678                 : **
   53679                 : ** An integer is written into *pRes which is the result of
   53680                 : ** comparing the key with the entry to which the cursor is 
   53681                 : ** pointing.  The meaning of the integer written into
   53682                 : ** *pRes is as follows:
   53683                 : **
   53684                 : **     *pRes<0      The cursor is left pointing at an entry that
   53685                 : **                  is smaller than intKey/pIdxKey or if the table is empty
   53686                 : **                  and the cursor is therefore left point to nothing.
   53687                 : **
   53688                 : **     *pRes==0     The cursor is left pointing at an entry that
   53689                 : **                  exactly matches intKey/pIdxKey.
   53690                 : **
   53691                 : **     *pRes>0      The cursor is left pointing at an entry that
   53692                 : **                  is larger than intKey/pIdxKey.
   53693                 : **
   53694                 : */
   53695          661194 : SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
   53696                 :   BtCursor *pCur,          /* The cursor to be moved */
   53697                 :   UnpackedRecord *pIdxKey, /* Unpacked index key */
   53698                 :   i64 intKey,              /* The table key */
   53699                 :   int biasRight,           /* If true, bias the search to the high end */
   53700                 :   int *pRes                /* Write search results here */
   53701                 : ){
   53702                 :   int rc;
   53703                 : 
   53704          661194 :   assert( cursorHoldsMutex(pCur) );
   53705          661194 :   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
   53706          661194 :   assert( pRes );
   53707          661194 :   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
   53708                 : 
   53709                 :   /* If the cursor is already positioned at the point we are trying
   53710                 :   ** to move to, then just return without doing any work */
   53711          661194 :   if( pCur->eState==CURSOR_VALID && pCur->validNKey 
   53712          230745 :    && pCur->apPage[0]->intKey 
   53713                 :   ){
   53714          228656 :     if( pCur->info.nKey==intKey ){
   53715          111777 :       *pRes = 0;
   53716          111777 :       return SQLITE_OK;
   53717                 :     }
   53718          116879 :     if( pCur->atLast && pCur->info.nKey<intKey ){
   53719           50758 :       *pRes = -1;
   53720           50758 :       return SQLITE_OK;
   53721                 :     }
   53722                 :   }
   53723                 : 
   53724          498659 :   rc = moveToRoot(pCur);
   53725          498659 :   if( rc ){
   53726               0 :     return rc;
   53727                 :   }
   53728          498659 :   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
   53729          498659 :   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
   53730          498659 :   assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
   53731          498659 :   if( pCur->eState==CURSOR_INVALID ){
   53732           54005 :     *pRes = -1;
   53733           54005 :     assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
   53734           54005 :     return SQLITE_OK;
   53735                 :   }
   53736          444654 :   assert( pCur->apPage[0]->intKey || pIdxKey );
   53737                 :   for(;;){
   53738                 :     int lwr, upr, idx;
   53739                 :     Pgno chldPg;
   53740          466193 :     MemPage *pPage = pCur->apPage[pCur->iPage];
   53741                 :     int c;
   53742                 : 
   53743                 :     /* pPage->nCell must be greater than zero. If this is the root-page
   53744                 :     ** the cursor would have been INVALID above and this for(;;) loop
   53745                 :     ** not run. If this is not the root-page, then the moveToChild() routine
   53746                 :     ** would have already detected db corruption. Similarly, pPage must
   53747                 :     ** be the right kind (index or table) of b-tree page. Otherwise
   53748                 :     ** a moveToChild() or moveToRoot() call would have detected corruption.  */
   53749          466193 :     assert( pPage->nCell>0 );
   53750          466193 :     assert( pPage->intKey==(pIdxKey==0) );
   53751          466193 :     lwr = 0;
   53752          466193 :     upr = pPage->nCell-1;
   53753          466193 :     if( biasRight ){
   53754            6512 :       pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
   53755                 :     }else{
   53756          459681 :       pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
   53757                 :     }
   53758                 :     for(;;){
   53759                 :       u8 *pCell;                          /* Pointer to current cell in pPage */
   53760                 : 
   53761         1750588 :       assert( idx==pCur->aiIdx[pCur->iPage] );
   53762         1750588 :       pCur->info.nSize = 0;
   53763         1750588 :       pCell = findCell(pPage, idx) + pPage->childPtrSize;
   53764         1750588 :       if( pPage->intKey ){
   53765                 :         i64 nCellKey;
   53766          719705 :         if( pPage->hasData ){
   53767                 :           u32 dummy;
   53768          704519 :           pCell += getVarint32(pCell, dummy);
   53769                 :         }
   53770          719705 :         getVarint(pCell, (u64*)&nCellKey);
   53771          719705 :         if( nCellKey==intKey ){
   53772          176937 :           c = 0;
   53773          542768 :         }else if( nCellKey<intKey ){
   53774          314860 :           c = -1;
   53775                 :         }else{
   53776          227908 :           assert( nCellKey>intKey );
   53777          227908 :           c = +1;
   53778                 :         }
   53779          719705 :         pCur->validNKey = 1;
   53780          719705 :         pCur->info.nKey = nCellKey;
   53781                 :       }else{
   53782                 :         /* The maximum supported page-size is 65536 bytes. This means that
   53783                 :         ** the maximum number of record bytes stored on an index B-Tree
   53784                 :         ** page is less than 16384 bytes and may be stored as a 2-byte
   53785                 :         ** varint. This information is used to attempt to avoid parsing 
   53786                 :         ** the entire cell by checking for the cases where the record is 
   53787                 :         ** stored entirely within the b-tree page by inspecting the first 
   53788                 :         ** 2 bytes of the cell.
   53789                 :         */
   53790         1030883 :         int nCell = pCell[0];
   53791         1030883 :         if( nCell<=pPage->max1bytePayload
   53792                 :          /* && (pCell+nCell)<pPage->aDataEnd */
   53793                 :         ){
   53794                 :           /* This branch runs if the record-size field of the cell is a
   53795                 :           ** single byte varint and the record fits entirely on the main
   53796                 :           ** b-tree page.  */
   53797                 :           testcase( pCell+nCell+1==pPage->aDataEnd );
   53798         1030852 :           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
   53799              31 :         }else if( !(pCell[1] & 0x80) 
   53800              31 :           && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
   53801                 :           /* && (pCell+nCell+2)<=pPage->aDataEnd */
   53802                 :         ){
   53803                 :           /* The record-size field is a 2 byte varint and the record 
   53804                 :           ** fits entirely on the main b-tree page.  */
   53805                 :           testcase( pCell+nCell+2==pPage->aDataEnd );
   53806              31 :           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
   53807                 :         }else{
   53808                 :           /* The record flows over onto one or more overflow pages. In
   53809                 :           ** this case the whole cell needs to be parsed, a buffer allocated
   53810                 :           ** and accessPayload() used to retrieve the record into the
   53811                 :           ** buffer before VdbeRecordCompare() can be called. */
   53812                 :           void *pCellKey;
   53813               0 :           u8 * const pCellBody = pCell - pPage->childPtrSize;
   53814               0 :           btreeParseCellPtr(pPage, pCellBody, &pCur->info);
   53815               0 :           nCell = (int)pCur->info.nKey;
   53816               0 :           pCellKey = sqlite3Malloc( nCell );
   53817               0 :           if( pCellKey==0 ){
   53818               0 :             rc = SQLITE_NOMEM;
   53819               0 :             goto moveto_finish;
   53820                 :           }
   53821               0 :           rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
   53822               0 :           if( rc ){
   53823               0 :             sqlite3_free(pCellKey);
   53824               0 :             goto moveto_finish;
   53825                 :           }
   53826               0 :           c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
   53827               0 :           sqlite3_free(pCellKey);
   53828                 :         }
   53829                 :       }
   53830         1750588 :       if( c==0 ){
   53831          222618 :         if( pPage->intKey && !pPage->leaf ){
   53832              99 :           lwr = idx;
   53833              99 :           break;
   53834                 :         }else{
   53835          222519 :           *pRes = 0;
   53836          222519 :           rc = SQLITE_OK;
   53837          222519 :           goto moveto_finish;
   53838                 :         }
   53839                 :       }
   53840         1527970 :       if( c<0 ){
   53841          923132 :         lwr = idx+1;
   53842                 :       }else{
   53843          604838 :         upr = idx-1;
   53844                 :       }
   53845         1527970 :       if( lwr>upr ){
   53846          243575 :         break;
   53847                 :       }
   53848         1284395 :       pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
   53849         1284395 :     }
   53850          243674 :     assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
   53851          243674 :     assert( pPage->isInit );
   53852          243674 :     if( pPage->leaf ){
   53853          222134 :       chldPg = 0;
   53854           21540 :     }else if( lwr>=pPage->nCell ){
   53855           10389 :       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
   53856                 :     }else{
   53857           11151 :       chldPg = get4byte(findCell(pPage, lwr));
   53858                 :     }
   53859          243674 :     if( chldPg==0 ){
   53860          222134 :       assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
   53861          222134 :       *pRes = c;
   53862          222134 :       rc = SQLITE_OK;
   53863          222134 :       goto moveto_finish;
   53864                 :     }
   53865           21540 :     pCur->aiIdx[pCur->iPage] = (u16)lwr;
   53866           21540 :     pCur->info.nSize = 0;
   53867           21540 :     pCur->validNKey = 0;
   53868           21540 :     rc = moveToChild(pCur, chldPg);
   53869           21540 :     if( rc ) goto moveto_finish;
   53870           21539 :   }
   53871                 : moveto_finish:
   53872          444654 :   return rc;
   53873                 : }
   53874                 : 
   53875                 : 
   53876                 : /*
   53877                 : ** Return TRUE if the cursor is not pointing at an entry of the table.
   53878                 : **
   53879                 : ** TRUE will be returned after a call to sqlite3BtreeNext() moves
   53880                 : ** past the last entry in the table or sqlite3BtreePrev() moves past
   53881                 : ** the first entry.  TRUE is also returned if the table is empty.
   53882                 : */
   53883            8548 : SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
   53884                 :   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
   53885                 :   ** have been deleted? This API will need to change to return an error code
   53886                 :   ** as well as the boolean result value.
   53887                 :   */
   53888            8548 :   return (CURSOR_VALID!=pCur->eState);
   53889                 : }
   53890                 : 
   53891                 : /*
   53892                 : ** Advance the cursor to the next entry in the database.  If
   53893                 : ** successful then set *pRes=0.  If the cursor
   53894                 : ** was already pointing to the last entry in the database before
   53895                 : ** this routine was called, then set *pRes=1.
   53896                 : */
   53897         1186505 : SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
   53898                 :   int rc;
   53899                 :   int idx;
   53900                 :   MemPage *pPage;
   53901                 : 
   53902         1186505 :   assert( cursorHoldsMutex(pCur) );
   53903         1186506 :   rc = restoreCursorPosition(pCur);
   53904         1186506 :   if( rc!=SQLITE_OK ){
   53905               0 :     return rc;
   53906                 :   }
   53907         1186506 :   assert( pRes!=0 );
   53908         1186506 :   if( CURSOR_INVALID==pCur->eState ){
   53909           24224 :     *pRes = 1;
   53910           24224 :     return SQLITE_OK;
   53911                 :   }
   53912         1162282 :   if( pCur->skipNext>0 ){
   53913               0 :     pCur->skipNext = 0;
   53914               0 :     *pRes = 0;
   53915               0 :     return SQLITE_OK;
   53916                 :   }
   53917         1162282 :   pCur->skipNext = 0;
   53918                 : 
   53919         1162282 :   pPage = pCur->apPage[pCur->iPage];
   53920         1162282 :   idx = ++pCur->aiIdx[pCur->iPage];
   53921         1162282 :   assert( pPage->isInit );
   53922                 : 
   53923                 :   /* If the database file is corrupt, it is possible for the value of idx 
   53924                 :   ** to be invalid here. This can only occur if a second cursor modifies
   53925                 :   ** the page while cursor pCur is holding a reference to it. Which can
   53926                 :   ** only happen if the database is corrupt in such a way as to link the
   53927                 :   ** page into more than one b-tree structure. */
   53928                 :   testcase( idx>pPage->nCell );
   53929                 : 
   53930         1162282 :   pCur->info.nSize = 0;
   53931         1162282 :   pCur->validNKey = 0;
   53932         1162282 :   if( idx>=pPage->nCell ){
   53933           67371 :     if( !pPage->leaf ){
   53934             518 :       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
   53935             518 :       if( rc ) return rc;
   53936             518 :       rc = moveToLeftmost(pCur);
   53937             518 :       *pRes = 0;
   53938             518 :       return rc;
   53939                 :     }
   53940                 :     do{
   53941           67360 :       if( pCur->iPage==0 ){
   53942           65294 :         *pRes = 1;
   53943           65294 :         pCur->eState = CURSOR_INVALID;
   53944           65294 :         return SQLITE_OK;
   53945                 :       }
   53946            2066 :       moveToParent(pCur);
   53947            2066 :       pPage = pCur->apPage[pCur->iPage];
   53948            2066 :     }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
   53949            1559 :     *pRes = 0;
   53950            1559 :     if( pPage->intKey ){
   53951            1527 :       rc = sqlite3BtreeNext(pCur, pRes);
   53952                 :     }else{
   53953              32 :       rc = SQLITE_OK;
   53954                 :     }
   53955            1559 :     return rc;
   53956                 :   }
   53957         1094911 :   *pRes = 0;
   53958         1094911 :   if( pPage->leaf ){
   53959         1093863 :     return SQLITE_OK;
   53960                 :   }
   53961            1048 :   rc = moveToLeftmost(pCur);
   53962            1048 :   return rc;
   53963                 : }
   53964                 : 
   53965                 : 
   53966                 : /*
   53967                 : ** Step the cursor to the back to the previous entry in the database.  If
   53968                 : ** successful then set *pRes=0.  If the cursor
   53969                 : ** was already pointing to the first entry in the database before
   53970                 : ** this routine was called, then set *pRes=1.
   53971                 : */
   53972           16598 : SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
   53973                 :   int rc;
   53974                 :   MemPage *pPage;
   53975                 : 
   53976           16598 :   assert( cursorHoldsMutex(pCur) );
   53977           16598 :   rc = restoreCursorPosition(pCur);
   53978           16598 :   if( rc!=SQLITE_OK ){
   53979               0 :     return rc;
   53980                 :   }
   53981           16598 :   pCur->atLast = 0;
   53982           16598 :   if( CURSOR_INVALID==pCur->eState ){
   53983               0 :     *pRes = 1;
   53984               0 :     return SQLITE_OK;
   53985                 :   }
   53986           16598 :   if( pCur->skipNext<0 ){
   53987               0 :     pCur->skipNext = 0;
   53988               0 :     *pRes = 0;
   53989               0 :     return SQLITE_OK;
   53990                 :   }
   53991           16598 :   pCur->skipNext = 0;
   53992                 : 
   53993           16598 :   pPage = pCur->apPage[pCur->iPage];
   53994           16598 :   assert( pPage->isInit );
   53995           16598 :   if( !pPage->leaf ){
   53996               0 :     int idx = pCur->aiIdx[pCur->iPage];
   53997               0 :     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
   53998               0 :     if( rc ){
   53999               0 :       return rc;
   54000                 :     }
   54001               0 :     rc = moveToRightmost(pCur);
   54002                 :   }else{
   54003           33196 :     while( pCur->aiIdx[pCur->iPage]==0 ){
   54004            1441 :       if( pCur->iPage==0 ){
   54005            1441 :         pCur->eState = CURSOR_INVALID;
   54006            1441 :         *pRes = 1;
   54007            1441 :         return SQLITE_OK;
   54008                 :       }
   54009               0 :       moveToParent(pCur);
   54010                 :     }
   54011           15157 :     pCur->info.nSize = 0;
   54012           15157 :     pCur->validNKey = 0;
   54013                 : 
   54014           15157 :     pCur->aiIdx[pCur->iPage]--;
   54015           15157 :     pPage = pCur->apPage[pCur->iPage];
   54016           15157 :     if( pPage->intKey && !pPage->leaf ){
   54017               0 :       rc = sqlite3BtreePrevious(pCur, pRes);
   54018                 :     }else{
   54019           15157 :       rc = SQLITE_OK;
   54020                 :     }
   54021                 :   }
   54022           15157 :   *pRes = 0;
   54023           15157 :   return rc;
   54024                 : }
   54025                 : 
   54026                 : /*
   54027                 : ** Allocate a new page from the database file.
   54028                 : **
   54029                 : ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
   54030                 : ** has already been called on the new page.)  The new page has also
   54031                 : ** been referenced and the calling routine is responsible for calling
   54032                 : ** sqlite3PagerUnref() on the new page when it is done.
   54033                 : **
   54034                 : ** SQLITE_OK is returned on success.  Any other return value indicates
   54035                 : ** an error.  *ppPage and *pPgno are undefined in the event of an error.
   54036                 : ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
   54037                 : **
   54038                 : ** If the "nearby" parameter is not 0, then a (feeble) effort is made to 
   54039                 : ** locate a page close to the page number "nearby".  This can be used in an
   54040                 : ** attempt to keep related pages close to each other in the database file,
   54041                 : ** which in turn can make database access faster.
   54042                 : **
   54043                 : ** If the "exact" parameter is not 0, and the page-number nearby exists 
   54044                 : ** anywhere on the free-list, then it is guarenteed to be returned. This
   54045                 : ** is only used by auto-vacuum databases when allocating a new table.
   54046                 : */
   54047           30692 : static int allocateBtreePage(
   54048                 :   BtShared *pBt, 
   54049                 :   MemPage **ppPage, 
   54050                 :   Pgno *pPgno, 
   54051                 :   Pgno nearby,
   54052                 :   u8 exact
   54053                 : ){
   54054                 :   MemPage *pPage1;
   54055                 :   int rc;
   54056                 :   u32 n;     /* Number of pages on the freelist */
   54057                 :   u32 k;     /* Number of leaves on the trunk of the freelist */
   54058           30692 :   MemPage *pTrunk = 0;
   54059           30692 :   MemPage *pPrevTrunk = 0;
   54060                 :   Pgno mxPage;     /* Total size of the database file */
   54061                 : 
   54062           30692 :   assert( sqlite3_mutex_held(pBt->mutex) );
   54063           30692 :   pPage1 = pBt->pPage1;
   54064           30692 :   mxPage = btreePagecount(pBt);
   54065           30692 :   n = get4byte(&pPage1->aData[36]);
   54066                 :   testcase( n==mxPage-1 );
   54067           30692 :   if( n>=mxPage ){
   54068               0 :     return SQLITE_CORRUPT_BKPT;
   54069                 :   }
   54070           30692 :   if( n>0 ){
   54071                 :     /* There are pages on the freelist.  Reuse one of those pages. */
   54072                 :     Pgno iTrunk;
   54073              91 :     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
   54074                 :     
   54075                 :     /* If the 'exact' parameter was true and a query of the pointer-map
   54076                 :     ** shows that the page 'nearby' is somewhere on the free-list, then
   54077                 :     ** the entire-list will be searched for that page.
   54078                 :     */
   54079                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   54080              91 :     if( exact && nearby<=mxPage ){
   54081                 :       u8 eType;
   54082               0 :       assert( nearby>0 );
   54083               0 :       assert( pBt->autoVacuum );
   54084               0 :       rc = ptrmapGet(pBt, nearby, &eType, 0);
   54085               0 :       if( rc ) return rc;
   54086               0 :       if( eType==PTRMAP_FREEPAGE ){
   54087               0 :         searchList = 1;
   54088                 :       }
   54089               0 :       *pPgno = nearby;
   54090                 :     }
   54091                 : #endif
   54092                 : 
   54093                 :     /* Decrement the free-list count by 1. Set iTrunk to the index of the
   54094                 :     ** first free-list trunk page. iPrevTrunk is initially 1.
   54095                 :     */
   54096              91 :     rc = sqlite3PagerWrite(pPage1->pDbPage);
   54097              91 :     if( rc ) return rc;
   54098              91 :     put4byte(&pPage1->aData[36], n-1);
   54099                 : 
   54100                 :     /* The code within this loop is run only once if the 'searchList' variable
   54101                 :     ** is not true. Otherwise, it runs once for each trunk-page on the
   54102                 :     ** free-list until the page 'nearby' is located.
   54103                 :     */
   54104                 :     do {
   54105              91 :       pPrevTrunk = pTrunk;
   54106              91 :       if( pPrevTrunk ){
   54107               0 :         iTrunk = get4byte(&pPrevTrunk->aData[0]);
   54108                 :       }else{
   54109              91 :         iTrunk = get4byte(&pPage1->aData[32]);
   54110                 :       }
   54111                 :       testcase( iTrunk==mxPage );
   54112              91 :       if( iTrunk>mxPage ){
   54113               0 :         rc = SQLITE_CORRUPT_BKPT;
   54114                 :       }else{
   54115              91 :         rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
   54116                 :       }
   54117              91 :       if( rc ){
   54118               0 :         pTrunk = 0;
   54119               0 :         goto end_allocate_page;
   54120                 :       }
   54121              91 :       assert( pTrunk!=0 );
   54122              91 :       assert( pTrunk->aData!=0 );
   54123                 : 
   54124              91 :       k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
   54125              91 :       if( k==0 && !searchList ){
   54126                 :         /* The trunk has no leaves and the list is not being searched. 
   54127                 :         ** So extract the trunk page itself and use it as the newly 
   54128                 :         ** allocated page */
   54129              35 :         assert( pPrevTrunk==0 );
   54130              35 :         rc = sqlite3PagerWrite(pTrunk->pDbPage);
   54131              35 :         if( rc ){
   54132               0 :           goto end_allocate_page;
   54133                 :         }
   54134              35 :         *pPgno = iTrunk;
   54135              35 :         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
   54136              35 :         *ppPage = pTrunk;
   54137              35 :         pTrunk = 0;
   54138                 :         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
   54139              56 :       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
   54140                 :         /* Value of k is out of range.  Database corruption */
   54141               0 :         rc = SQLITE_CORRUPT_BKPT;
   54142               0 :         goto end_allocate_page;
   54143                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   54144              56 :       }else if( searchList && nearby==iTrunk ){
   54145                 :         /* The list is being searched and this trunk page is the page
   54146                 :         ** to allocate, regardless of whether it has leaves.
   54147                 :         */
   54148               0 :         assert( *pPgno==iTrunk );
   54149               0 :         *ppPage = pTrunk;
   54150               0 :         searchList = 0;
   54151               0 :         rc = sqlite3PagerWrite(pTrunk->pDbPage);
   54152               0 :         if( rc ){
   54153               0 :           goto end_allocate_page;
   54154                 :         }
   54155               0 :         if( k==0 ){
   54156               0 :           if( !pPrevTrunk ){
   54157               0 :             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
   54158                 :           }else{
   54159               0 :             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
   54160               0 :             if( rc!=SQLITE_OK ){
   54161               0 :               goto end_allocate_page;
   54162                 :             }
   54163               0 :             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
   54164                 :           }
   54165                 :         }else{
   54166                 :           /* The trunk page is required by the caller but it contains 
   54167                 :           ** pointers to free-list leaves. The first leaf becomes a trunk
   54168                 :           ** page in this case.
   54169                 :           */
   54170                 :           MemPage *pNewTrunk;
   54171               0 :           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
   54172               0 :           if( iNewTrunk>mxPage ){ 
   54173               0 :             rc = SQLITE_CORRUPT_BKPT;
   54174               0 :             goto end_allocate_page;
   54175                 :           }
   54176                 :           testcase( iNewTrunk==mxPage );
   54177               0 :           rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
   54178               0 :           if( rc!=SQLITE_OK ){
   54179               0 :             goto end_allocate_page;
   54180                 :           }
   54181               0 :           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
   54182               0 :           if( rc!=SQLITE_OK ){
   54183               0 :             releasePage(pNewTrunk);
   54184               0 :             goto end_allocate_page;
   54185                 :           }
   54186               0 :           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
   54187               0 :           put4byte(&pNewTrunk->aData[4], k-1);
   54188               0 :           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
   54189               0 :           releasePage(pNewTrunk);
   54190               0 :           if( !pPrevTrunk ){
   54191               0 :             assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
   54192               0 :             put4byte(&pPage1->aData[32], iNewTrunk);
   54193                 :           }else{
   54194               0 :             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
   54195               0 :             if( rc ){
   54196               0 :               goto end_allocate_page;
   54197                 :             }
   54198               0 :             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
   54199                 :           }
   54200                 :         }
   54201               0 :         pTrunk = 0;
   54202                 :         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
   54203                 : #endif
   54204              56 :       }else if( k>0 ){
   54205                 :         /* Extract a leaf from the trunk */
   54206                 :         u32 closest;
   54207                 :         Pgno iPage;
   54208              56 :         unsigned char *aData = pTrunk->aData;
   54209              56 :         if( nearby>0 ){
   54210                 :           u32 i;
   54211                 :           int dist;
   54212              36 :           closest = 0;
   54213              36 :           dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
   54214             384 :           for(i=1; i<k; i++){
   54215             348 :             int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
   54216             348 :             if( d2<dist ){
   54217               9 :               closest = i;
   54218               9 :               dist = d2;
   54219                 :             }
   54220                 :           }
   54221                 :         }else{
   54222              20 :           closest = 0;
   54223                 :         }
   54224                 : 
   54225              56 :         iPage = get4byte(&aData[8+closest*4]);
   54226                 :         testcase( iPage==mxPage );
   54227              56 :         if( iPage>mxPage ){
   54228               0 :           rc = SQLITE_CORRUPT_BKPT;
   54229               0 :           goto end_allocate_page;
   54230                 :         }
   54231                 :         testcase( iPage==mxPage );
   54232              56 :         if( !searchList || iPage==nearby ){
   54233                 :           int noContent;
   54234              56 :           *pPgno = iPage;
   54235                 :           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
   54236                 :                  ": %d more free pages\n",
   54237                 :                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
   54238              56 :           rc = sqlite3PagerWrite(pTrunk->pDbPage);
   54239              56 :           if( rc ) goto end_allocate_page;
   54240              56 :           if( closest<k-1 ){
   54241              25 :             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
   54242                 :           }
   54243              56 :           put4byte(&aData[4], k-1);
   54244              56 :           noContent = !btreeGetHasContent(pBt, *pPgno);
   54245              56 :           rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
   54246              56 :           if( rc==SQLITE_OK ){
   54247              56 :             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
   54248              56 :             if( rc!=SQLITE_OK ){
   54249               0 :               releasePage(*ppPage);
   54250                 :             }
   54251                 :           }
   54252              56 :           searchList = 0;
   54253                 :         }
   54254                 :       }
   54255              91 :       releasePage(pPrevTrunk);
   54256              91 :       pPrevTrunk = 0;
   54257              91 :     }while( searchList );
   54258                 :   }else{
   54259                 :     /* There are no pages on the freelist, so create a new page at the
   54260                 :     ** end of the file */
   54261           30601 :     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
   54262           30601 :     if( rc ) return rc;
   54263           30601 :     pBt->nPage++;
   54264           30601 :     if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
   54265                 : 
   54266                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   54267           30601 :     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
   54268                 :       /* If *pPgno refers to a pointer-map page, allocate two new pages
   54269                 :       ** at the end of the file instead of one. The first allocated page
   54270                 :       ** becomes a new pointer-map page, the second is used by the caller.
   54271                 :       */
   54272               0 :       MemPage *pPg = 0;
   54273                 :       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
   54274               0 :       assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
   54275               0 :       rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1);
   54276               0 :       if( rc==SQLITE_OK ){
   54277               0 :         rc = sqlite3PagerWrite(pPg->pDbPage);
   54278               0 :         releasePage(pPg);
   54279                 :       }
   54280               0 :       if( rc ) return rc;
   54281               0 :       pBt->nPage++;
   54282               0 :       if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
   54283                 :     }
   54284                 : #endif
   54285           30601 :     put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
   54286           30601 :     *pPgno = pBt->nPage;
   54287                 : 
   54288           30601 :     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
   54289           30601 :     rc = btreeGetPage(pBt, *pPgno, ppPage, 1);
   54290           30601 :     if( rc ) return rc;
   54291           30601 :     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
   54292           30601 :     if( rc!=SQLITE_OK ){
   54293               0 :       releasePage(*ppPage);
   54294                 :     }
   54295                 :     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
   54296                 :   }
   54297                 : 
   54298           30692 :   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
   54299                 : 
   54300                 : end_allocate_page:
   54301           30692 :   releasePage(pTrunk);
   54302           30692 :   releasePage(pPrevTrunk);
   54303           30692 :   if( rc==SQLITE_OK ){
   54304           30692 :     if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
   54305               0 :       releasePage(*ppPage);
   54306               0 :       return SQLITE_CORRUPT_BKPT;
   54307                 :     }
   54308           30692 :     (*ppPage)->isInit = 0;
   54309                 :   }else{
   54310               0 :     *ppPage = 0;
   54311                 :   }
   54312           30692 :   assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
   54313           30692 :   return rc;
   54314                 : }
   54315                 : 
   54316                 : /*
   54317                 : ** This function is used to add page iPage to the database file free-list. 
   54318                 : ** It is assumed that the page is not already a part of the free-list.
   54319                 : **
   54320                 : ** The value passed as the second argument to this function is optional.
   54321                 : ** If the caller happens to have a pointer to the MemPage object 
   54322                 : ** corresponding to page iPage handy, it may pass it as the second value. 
   54323                 : ** Otherwise, it may pass NULL.
   54324                 : **
   54325                 : ** If a pointer to a MemPage object is passed as the second argument,
   54326                 : ** its reference count is not altered by this function.
   54327                 : */
   54328              93 : static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
   54329              93 :   MemPage *pTrunk = 0;                /* Free-list trunk page */
   54330              93 :   Pgno iTrunk = 0;                    /* Page number of free-list trunk page */ 
   54331              93 :   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
   54332                 :   MemPage *pPage;                     /* Page being freed. May be NULL. */
   54333                 :   int rc;                             /* Return Code */
   54334                 :   int nFree;                          /* Initial number of pages on free-list */
   54335                 : 
   54336              93 :   assert( sqlite3_mutex_held(pBt->mutex) );
   54337              93 :   assert( iPage>1 );
   54338              93 :   assert( !pMemPage || pMemPage->pgno==iPage );
   54339                 : 
   54340              93 :   if( pMemPage ){
   54341              93 :     pPage = pMemPage;
   54342              93 :     sqlite3PagerRef(pPage->pDbPage);
   54343                 :   }else{
   54344               0 :     pPage = btreePageLookup(pBt, iPage);
   54345                 :   }
   54346                 : 
   54347                 :   /* Increment the free page count on pPage1 */
   54348              93 :   rc = sqlite3PagerWrite(pPage1->pDbPage);
   54349              93 :   if( rc ) goto freepage_out;
   54350              93 :   nFree = get4byte(&pPage1->aData[36]);
   54351              93 :   put4byte(&pPage1->aData[36], nFree+1);
   54352                 : 
   54353              93 :   if( pBt->btsFlags & BTS_SECURE_DELETE ){
   54354                 :     /* If the secure_delete option is enabled, then
   54355                 :     ** always fully overwrite deleted information with zeros.
   54356                 :     */
   54357              93 :     if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
   54358              93 :      ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
   54359                 :     ){
   54360                 :       goto freepage_out;
   54361                 :     }
   54362              93 :     memset(pPage->aData, 0, pPage->pBt->pageSize);
   54363                 :   }
   54364                 : 
   54365                 :   /* If the database supports auto-vacuum, write an entry in the pointer-map
   54366                 :   ** to indicate that the page is free.
   54367                 :   */
   54368              93 :   if( ISAUTOVACUUM ){
   54369               0 :     ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
   54370               0 :     if( rc ) goto freepage_out;
   54371                 :   }
   54372                 : 
   54373                 :   /* Now manipulate the actual database free-list structure. There are two
   54374                 :   ** possibilities. If the free-list is currently empty, or if the first
   54375                 :   ** trunk page in the free-list is full, then this page will become a
   54376                 :   ** new free-list trunk page. Otherwise, it will become a leaf of the
   54377                 :   ** first trunk page in the current free-list. This block tests if it
   54378                 :   ** is possible to add the page as a new free-list leaf.
   54379                 :   */
   54380              93 :   if( nFree!=0 ){
   54381                 :     u32 nLeaf;                /* Initial number of leaf cells on trunk page */
   54382                 : 
   54383              51 :     iTrunk = get4byte(&pPage1->aData[32]);
   54384              51 :     rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
   54385              51 :     if( rc!=SQLITE_OK ){
   54386               0 :       goto freepage_out;
   54387                 :     }
   54388                 : 
   54389              51 :     nLeaf = get4byte(&pTrunk->aData[4]);
   54390              51 :     assert( pBt->usableSize>32 );
   54391              51 :     if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
   54392               0 :       rc = SQLITE_CORRUPT_BKPT;
   54393               0 :       goto freepage_out;
   54394                 :     }
   54395              51 :     if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
   54396                 :       /* In this case there is room on the trunk page to insert the page
   54397                 :       ** being freed as a new leaf.
   54398                 :       **
   54399                 :       ** Note that the trunk page is not really full until it contains
   54400                 :       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
   54401                 :       ** coded.  But due to a coding error in versions of SQLite prior to
   54402                 :       ** 3.6.0, databases with freelist trunk pages holding more than
   54403                 :       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
   54404                 :       ** to maintain backwards compatibility with older versions of SQLite,
   54405                 :       ** we will continue to restrict the number of entries to usableSize/4 - 8
   54406                 :       ** for now.  At some point in the future (once everyone has upgraded
   54407                 :       ** to 3.6.0 or later) we should consider fixing the conditional above
   54408                 :       ** to read "usableSize/4-2" instead of "usableSize/4-8".
   54409                 :       */
   54410              51 :       rc = sqlite3PagerWrite(pTrunk->pDbPage);
   54411              51 :       if( rc==SQLITE_OK ){
   54412              51 :         put4byte(&pTrunk->aData[4], nLeaf+1);
   54413              51 :         put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
   54414              51 :         if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
   54415               0 :           sqlite3PagerDontWrite(pPage->pDbPage);
   54416                 :         }
   54417              51 :         rc = btreeSetHasContent(pBt, iPage);
   54418                 :       }
   54419                 :       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
   54420              51 :       goto freepage_out;
   54421                 :     }
   54422                 :   }
   54423                 : 
   54424                 :   /* If control flows to this point, then it was not possible to add the
   54425                 :   ** the page being freed as a leaf page of the first trunk in the free-list.
   54426                 :   ** Possibly because the free-list is empty, or possibly because the 
   54427                 :   ** first trunk in the free-list is full. Either way, the page being freed
   54428                 :   ** will become the new first trunk page in the free-list.
   54429                 :   */
   54430              42 :   if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
   54431               0 :     goto freepage_out;
   54432                 :   }
   54433              42 :   rc = sqlite3PagerWrite(pPage->pDbPage);
   54434              42 :   if( rc!=SQLITE_OK ){
   54435               0 :     goto freepage_out;
   54436                 :   }
   54437              42 :   put4byte(pPage->aData, iTrunk);
   54438              42 :   put4byte(&pPage->aData[4], 0);
   54439              42 :   put4byte(&pPage1->aData[32], iPage);
   54440                 :   TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
   54441                 : 
   54442                 : freepage_out:
   54443              93 :   if( pPage ){
   54444              93 :     pPage->isInit = 0;
   54445                 :   }
   54446              93 :   releasePage(pPage);
   54447              93 :   releasePage(pTrunk);
   54448              93 :   return rc;
   54449                 : }
   54450              93 : static void freePage(MemPage *pPage, int *pRC){
   54451              93 :   if( (*pRC)==SQLITE_OK ){
   54452              93 :     *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
   54453                 :   }
   54454              93 : }
   54455                 : 
   54456                 : /*
   54457                 : ** Free any overflow pages associated with the given Cell.
   54458                 : */
   54459           95293 : static int clearCell(MemPage *pPage, unsigned char *pCell){
   54460           95293 :   BtShared *pBt = pPage->pBt;
   54461                 :   CellInfo info;
   54462                 :   Pgno ovflPgno;
   54463                 :   int rc;
   54464                 :   int nOvfl;
   54465                 :   u32 ovflPageSize;
   54466                 : 
   54467           95293 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   54468           95293 :   btreeParseCellPtr(pPage, pCell, &info);
   54469           95293 :   if( info.iOverflow==0 ){
   54470           95293 :     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
   54471                 :   }
   54472               0 :   if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
   54473               0 :     return SQLITE_CORRUPT;  /* Cell extends past end of page */
   54474                 :   }
   54475               0 :   ovflPgno = get4byte(&pCell[info.iOverflow]);
   54476               0 :   assert( pBt->usableSize > 4 );
   54477               0 :   ovflPageSize = pBt->usableSize - 4;
   54478               0 :   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
   54479               0 :   assert( ovflPgno==0 || nOvfl>0 );
   54480               0 :   while( nOvfl-- ){
   54481               0 :     Pgno iNext = 0;
   54482               0 :     MemPage *pOvfl = 0;
   54483               0 :     if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
   54484                 :       /* 0 is not a legal page number and page 1 cannot be an 
   54485                 :       ** overflow page. Therefore if ovflPgno<2 or past the end of the 
   54486                 :       ** file the database must be corrupt. */
   54487               0 :       return SQLITE_CORRUPT_BKPT;
   54488                 :     }
   54489               0 :     if( nOvfl ){
   54490               0 :       rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
   54491               0 :       if( rc ) return rc;
   54492                 :     }
   54493                 : 
   54494               0 :     if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
   54495               0 :      && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
   54496                 :     ){
   54497                 :       /* There is no reason any cursor should have an outstanding reference 
   54498                 :       ** to an overflow page belonging to a cell that is being deleted/updated.
   54499                 :       ** So if there exists more than one reference to this page, then it 
   54500                 :       ** must not really be an overflow page and the database must be corrupt. 
   54501                 :       ** It is helpful to detect this before calling freePage2(), as 
   54502                 :       ** freePage2() may zero the page contents if secure-delete mode is
   54503                 :       ** enabled. If this 'overflow' page happens to be a page that the
   54504                 :       ** caller is iterating through or using in some other way, this
   54505                 :       ** can be problematic.
   54506                 :       */
   54507               0 :       rc = SQLITE_CORRUPT_BKPT;
   54508                 :     }else{
   54509               0 :       rc = freePage2(pBt, pOvfl, ovflPgno);
   54510                 :     }
   54511                 : 
   54512               0 :     if( pOvfl ){
   54513               0 :       sqlite3PagerUnref(pOvfl->pDbPage);
   54514                 :     }
   54515               0 :     if( rc ) return rc;
   54516               0 :     ovflPgno = iNext;
   54517                 :   }
   54518               0 :   return SQLITE_OK;
   54519                 : }
   54520                 : 
   54521                 : /*
   54522                 : ** Create the byte sequence used to represent a cell on page pPage
   54523                 : ** and write that byte sequence into pCell[].  Overflow pages are
   54524                 : ** allocated and filled in as necessary.  The calling procedure
   54525                 : ** is responsible for making sure sufficient space has been allocated
   54526                 : ** for pCell[].
   54527                 : **
   54528                 : ** Note that pCell does not necessary need to point to the pPage->aData
   54529                 : ** area.  pCell might point to some temporary storage.  The cell will
   54530                 : ** be constructed in this temporary area then copied into pPage->aData
   54531                 : ** later.
   54532                 : */
   54533          230859 : static int fillInCell(
   54534                 :   MemPage *pPage,                /* The page that contains the cell */
   54535                 :   unsigned char *pCell,          /* Complete text of the cell */
   54536                 :   const void *pKey, i64 nKey,    /* The key */
   54537                 :   const void *pData,int nData,   /* The data */
   54538                 :   int nZero,                     /* Extra zero bytes to append to pData */
   54539                 :   int *pnSize                    /* Write cell size here */
   54540                 : ){
   54541                 :   int nPayload;
   54542                 :   const u8 *pSrc;
   54543                 :   int nSrc, n, rc;
   54544                 :   int spaceLeft;
   54545          230859 :   MemPage *pOvfl = 0;
   54546          230859 :   MemPage *pToRelease = 0;
   54547                 :   unsigned char *pPrior;
   54548                 :   unsigned char *pPayload;
   54549          230859 :   BtShared *pBt = pPage->pBt;
   54550          230859 :   Pgno pgnoOvfl = 0;
   54551                 :   int nHeader;
   54552                 :   CellInfo info;
   54553                 : 
   54554          230859 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   54555                 : 
   54556                 :   /* pPage is not necessarily writeable since pCell might be auxiliary
   54557                 :   ** buffer space that is separate from the pPage buffer area */
   54558          230859 :   assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
   54559                 :             || sqlite3PagerIswriteable(pPage->pDbPage) );
   54560                 : 
   54561                 :   /* Fill in the header. */
   54562          230859 :   nHeader = 0;
   54563          230859 :   if( !pPage->leaf ){
   54564               0 :     nHeader += 4;
   54565                 :   }
   54566          230859 :   if( pPage->hasData ){
   54567           99148 :     nHeader += putVarint(&pCell[nHeader], nData+nZero);
   54568                 :   }else{
   54569          131711 :     nData = nZero = 0;
   54570                 :   }
   54571          230859 :   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
   54572          230859 :   btreeParseCellPtr(pPage, pCell, &info);
   54573          230859 :   assert( info.nHeader==nHeader );
   54574          230859 :   assert( info.nKey==nKey );
   54575          230859 :   assert( info.nData==(u32)(nData+nZero) );
   54576                 :   
   54577                 :   /* Fill in the payload */
   54578          230859 :   nPayload = nData + nZero;
   54579          230859 :   if( pPage->intKey ){
   54580           99148 :     pSrc = pData;
   54581           99148 :     nSrc = nData;
   54582           99148 :     nData = 0;
   54583                 :   }else{ 
   54584          131711 :     if( NEVER(nKey>0x7fffffff || pKey==0) ){
   54585               0 :       return SQLITE_CORRUPT_BKPT;
   54586                 :     }
   54587          131711 :     nPayload += (int)nKey;
   54588          131711 :     pSrc = pKey;
   54589          131711 :     nSrc = (int)nKey;
   54590                 :   }
   54591          230859 :   *pnSize = info.nSize;
   54592          230859 :   spaceLeft = info.nLocal;
   54593          230859 :   pPayload = &pCell[nHeader];
   54594          230859 :   pPrior = &pCell[info.iOverflow];
   54595                 : 
   54596          685435 :   while( nPayload>0 ){
   54597          223717 :     if( spaceLeft==0 ){
   54598                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   54599               0 :       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
   54600               0 :       if( pBt->autoVacuum ){
   54601                 :         do{
   54602               0 :           pgnoOvfl++;
   54603                 :         } while( 
   54604               0 :           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
   54605               0 :         );
   54606                 :       }
   54607                 : #endif
   54608               0 :       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
   54609                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   54610                 :       /* If the database supports auto-vacuum, and the second or subsequent
   54611                 :       ** overflow page is being allocated, add an entry to the pointer-map
   54612                 :       ** for that page now. 
   54613                 :       **
   54614                 :       ** If this is the first overflow page, then write a partial entry 
   54615                 :       ** to the pointer-map. If we write nothing to this pointer-map slot,
   54616                 :       ** then the optimistic overflow chain processing in clearCell()
   54617                 :       ** may misinterpret the uninitialised values and delete the
   54618                 :       ** wrong pages from the database.
   54619                 :       */
   54620               0 :       if( pBt->autoVacuum && rc==SQLITE_OK ){
   54621               0 :         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
   54622               0 :         ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
   54623               0 :         if( rc ){
   54624               0 :           releasePage(pOvfl);
   54625                 :         }
   54626                 :       }
   54627                 : #endif
   54628               0 :       if( rc ){
   54629               0 :         releasePage(pToRelease);
   54630               0 :         return rc;
   54631                 :       }
   54632                 : 
   54633                 :       /* If pToRelease is not zero than pPrior points into the data area
   54634                 :       ** of pToRelease.  Make sure pToRelease is still writeable. */
   54635               0 :       assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
   54636                 : 
   54637                 :       /* If pPrior is part of the data area of pPage, then make sure pPage
   54638                 :       ** is still writeable */
   54639               0 :       assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
   54640                 :             || sqlite3PagerIswriteable(pPage->pDbPage) );
   54641                 : 
   54642               0 :       put4byte(pPrior, pgnoOvfl);
   54643               0 :       releasePage(pToRelease);
   54644               0 :       pToRelease = pOvfl;
   54645               0 :       pPrior = pOvfl->aData;
   54646               0 :       put4byte(pPrior, 0);
   54647               0 :       pPayload = &pOvfl->aData[4];
   54648               0 :       spaceLeft = pBt->usableSize - 4;
   54649                 :     }
   54650          223717 :     n = nPayload;
   54651          223717 :     if( n>spaceLeft ) n = spaceLeft;
   54652                 : 
   54653                 :     /* If pToRelease is not zero than pPayload points into the data area
   54654                 :     ** of pToRelease.  Make sure pToRelease is still writeable. */
   54655          223717 :     assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
   54656                 : 
   54657                 :     /* If pPayload is part of the data area of pPage, then make sure pPage
   54658                 :     ** is still writeable */
   54659          223717 :     assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
   54660                 :             || sqlite3PagerIswriteable(pPage->pDbPage) );
   54661                 : 
   54662          223717 :     if( nSrc>0 ){
   54663          223717 :       if( n>nSrc ) n = nSrc;
   54664          223717 :       assert( pSrc );
   54665          223717 :       memcpy(pPayload, pSrc, n);
   54666                 :     }else{
   54667               0 :       memset(pPayload, 0, n);
   54668                 :     }
   54669          223717 :     nPayload -= n;
   54670          223717 :     pPayload += n;
   54671          223717 :     pSrc += n;
   54672          223717 :     nSrc -= n;
   54673          223717 :     spaceLeft -= n;
   54674          223717 :     if( nSrc==0 ){
   54675          223717 :       nSrc = nData;
   54676          223717 :       pSrc = pData;
   54677                 :     }
   54678                 :   }
   54679          230859 :   releasePage(pToRelease);
   54680          230859 :   return SQLITE_OK;
   54681                 : }
   54682                 : 
   54683                 : /*
   54684                 : ** Remove the i-th cell from pPage.  This routine effects pPage only.
   54685                 : ** The cell content is not freed or deallocated.  It is assumed that
   54686                 : ** the cell content has been copied someplace else.  This routine just
   54687                 : ** removes the reference to the cell from pPage.
   54688                 : **
   54689                 : ** "sz" must be the number of bytes in the cell.
   54690                 : */
   54691           93232 : static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
   54692                 :   u32 pc;         /* Offset to cell content of cell being deleted */
   54693                 :   u8 *data;       /* pPage->aData */
   54694                 :   u8 *ptr;        /* Used to move bytes around within data[] */
   54695                 :   u8 *endPtr;     /* End of loop */
   54696                 :   int rc;         /* The return code */
   54697                 :   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
   54698                 : 
   54699           93232 :   if( *pRC ) return;
   54700                 : 
   54701           93232 :   assert( idx>=0 && idx<pPage->nCell );
   54702           93232 :   assert( sz==cellSize(pPage, idx) );
   54703           93232 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   54704           93232 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   54705           93232 :   data = pPage->aData;
   54706           93232 :   ptr = &pPage->aCellIdx[2*idx];
   54707           93232 :   pc = get2byte(ptr);
   54708           93232 :   hdr = pPage->hdrOffset;
   54709                 :   testcase( pc==get2byte(&data[hdr+5]) );
   54710                 :   testcase( pc+sz==pPage->pBt->usableSize );
   54711           93232 :   if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
   54712               0 :     *pRC = SQLITE_CORRUPT_BKPT;
   54713               0 :     return;
   54714                 :   }
   54715           93232 :   rc = freeSpace(pPage, pc, sz);
   54716           93232 :   if( rc ){
   54717               0 :     *pRC = rc;
   54718               0 :     return;
   54719                 :   }
   54720           93232 :   endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
   54721           93232 :   assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
   54722         3032302 :   while( ptr<endPtr ){
   54723         2845838 :     *(u16*)ptr = *(u16*)&ptr[2];
   54724         2845838 :     ptr += 2;
   54725                 :   }
   54726           93232 :   pPage->nCell--;
   54727           93232 :   put2byte(&data[hdr+3], pPage->nCell);
   54728           93232 :   pPage->nFree += 2;
   54729                 : }
   54730                 : 
   54731                 : /*
   54732                 : ** Insert a new cell on pPage at cell index "i".  pCell points to the
   54733                 : ** content of the cell.
   54734                 : **
   54735                 : ** If the cell content will fit on the page, then put it there.  If it
   54736                 : ** will not fit, then make a copy of the cell content into pTemp if
   54737                 : ** pTemp is not null.  Regardless of pTemp, allocate a new entry
   54738                 : ** in pPage->aOvfl[] and make it point to the cell content (either
   54739                 : ** in pTemp or the original pCell) and also record its index. 
   54740                 : ** Allocating a new entry in pPage->aCell[] implies that 
   54741                 : ** pPage->nOverflow is incremented.
   54742                 : **
   54743                 : ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
   54744                 : ** cell. The caller will overwrite them after this function returns. If
   54745                 : ** nSkip is non-zero, then pCell may not point to an invalid memory location 
   54746                 : ** (but pCell+nSkip is always valid).
   54747                 : */
   54748          231294 : static void insertCell(
   54749                 :   MemPage *pPage,   /* Page into which we are copying */
   54750                 :   int i,            /* New cell becomes the i-th cell of the page */
   54751                 :   u8 *pCell,        /* Content of the new cell */
   54752                 :   int sz,           /* Bytes of content in pCell */
   54753                 :   u8 *pTemp,        /* Temp storage space for pCell, if needed */
   54754                 :   Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
   54755                 :   int *pRC          /* Read and write return code from here */
   54756                 : ){
   54757          231294 :   int idx = 0;      /* Where to write new cell content in data[] */
   54758                 :   int j;            /* Loop counter */
   54759                 :   int end;          /* First byte past the last cell pointer in data[] */
   54760                 :   int ins;          /* Index in data[] where new cell pointer is inserted */
   54761                 :   int cellOffset;   /* Address of first cell pointer in data[] */
   54762                 :   u8 *data;         /* The content of the whole page */
   54763                 :   u8 *ptr;          /* Used for moving information around in data[] */
   54764                 :   u8 *endPtr;       /* End of the loop */
   54765                 : 
   54766          231294 :   int nSkip = (iChild ? 4 : 0);
   54767                 : 
   54768          231294 :   if( *pRC ) return;
   54769                 : 
   54770          231294 :   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
   54771          231294 :   assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
   54772          231294 :   assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
   54773          231294 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   54774                 :   /* The cell should normally be sized correctly.  However, when moving a
   54775                 :   ** malformed cell from a leaf page to an interior page, if the cell size
   54776                 :   ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
   54777                 :   ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
   54778                 :   ** the term after the || in the following assert(). */
   54779          231294 :   assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
   54780          231294 :   if( pPage->nOverflow || sz+2>pPage->nFree ){
   54781             248 :     if( pTemp ){
   54782               0 :       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
   54783               0 :       pCell = pTemp;
   54784                 :     }
   54785             248 :     if( iChild ){
   54786               0 :       put4byte(pCell, iChild);
   54787                 :     }
   54788             248 :     j = pPage->nOverflow++;
   54789             248 :     assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
   54790             248 :     pPage->aOvfl[j].pCell = pCell;
   54791             248 :     pPage->aOvfl[j].idx = (u16)i;
   54792                 :   }else{
   54793          231046 :     int rc = sqlite3PagerWrite(pPage->pDbPage);
   54794          231046 :     if( rc!=SQLITE_OK ){
   54795               0 :       *pRC = rc;
   54796               0 :       return;
   54797                 :     }
   54798          231046 :     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   54799          231046 :     data = pPage->aData;
   54800          231046 :     cellOffset = pPage->cellOffset;
   54801          231046 :     end = cellOffset + 2*pPage->nCell;
   54802          231046 :     ins = cellOffset + 2*i;
   54803          231046 :     rc = allocateSpace(pPage, sz, &idx);
   54804          231046 :     if( rc ){ *pRC = rc; return; }
   54805                 :     /* The allocateSpace() routine guarantees the following two properties
   54806                 :     ** if it returns success */
   54807          231046 :     assert( idx >= end+2 );
   54808          231046 :     assert( idx+sz <= (int)pPage->pBt->usableSize );
   54809          231046 :     pPage->nCell++;
   54810          231046 :     pPage->nFree -= (u16)(2 + sz);
   54811          231046 :     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
   54812          231046 :     if( iChild ){
   54813             435 :       put4byte(&data[idx], iChild);
   54814                 :     }
   54815          231046 :     ptr = &data[end];
   54816          231046 :     endPtr = &data[ins];
   54817          231046 :     assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 );  /* ptr is always 2-byte aligned */
   54818        14451609 :     while( ptr>endPtr ){
   54819        13989517 :       *(u16*)ptr = *(u16*)&ptr[-2];
   54820        13989517 :       ptr -= 2;
   54821                 :     }
   54822          231046 :     put2byte(&data[ins], idx);
   54823          231046 :     put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
   54824                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   54825          231046 :     if( pPage->pBt->autoVacuum ){
   54826                 :       /* The cell may contain a pointer to an overflow page. If so, write
   54827                 :       ** the entry for the overflow page into the pointer map.
   54828                 :       */
   54829               0 :       ptrmapPutOvflPtr(pPage, pCell, pRC);
   54830                 :     }
   54831                 : #endif
   54832                 :   }
   54833                 : }
   54834                 : 
   54835                 : /*
   54836                 : ** Add a list of cells to a page.  The page should be initially empty.
   54837                 : ** The cells are guaranteed to fit on the page.
   54838                 : */
   54839             640 : static void assemblePage(
   54840                 :   MemPage *pPage,   /* The page to be assemblied */
   54841                 :   int nCell,        /* The number of cells to add to this page */
   54842                 :   u8 **apCell,      /* Pointers to cell bodies */
   54843                 :   u16 *aSize        /* Sizes of the cells */
   54844                 : ){
   54845                 :   int i;            /* Loop counter */
   54846                 :   u8 *pCellptr;     /* Address of next cell pointer */
   54847                 :   int cellbody;     /* Address of next cell body */
   54848             640 :   u8 * const data = pPage->aData;             /* Pointer to data for pPage */
   54849             640 :   const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
   54850             640 :   const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
   54851                 : 
   54852             640 :   assert( pPage->nOverflow==0 );
   54853             640 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   54854             640 :   assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
   54855                 :             && (int)MX_CELL(pPage->pBt)<=10921);
   54856             640 :   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
   54857                 : 
   54858                 :   /* Check that the page has just been zeroed by zeroPage() */
   54859             640 :   assert( pPage->nCell==0 );
   54860             640 :   assert( get2byteNotZero(&data[hdr+5])==nUsable );
   54861                 : 
   54862             640 :   pCellptr = &pPage->aCellIdx[nCell*2];
   54863             640 :   cellbody = nUsable;
   54864          105500 :   for(i=nCell-1; i>=0; i--){
   54865          104860 :     u16 sz = aSize[i];
   54866          104860 :     pCellptr -= 2;
   54867          104860 :     cellbody -= sz;
   54868          104860 :     put2byte(pCellptr, cellbody);
   54869          104860 :     memcpy(&data[cellbody], apCell[i], sz);
   54870                 :   }
   54871             640 :   put2byte(&data[hdr+3], nCell);
   54872             640 :   put2byte(&data[hdr+5], cellbody);
   54873             640 :   pPage->nFree -= (nCell*2 + nUsable - cellbody);
   54874             640 :   pPage->nCell = (u16)nCell;
   54875             640 : }
   54876                 : 
   54877                 : /*
   54878                 : ** The following parameters determine how many adjacent pages get involved
   54879                 : ** in a balancing operation.  NN is the number of neighbors on either side
   54880                 : ** of the page that participate in the balancing operation.  NB is the
   54881                 : ** total number of pages that participate, including the target page and
   54882                 : ** NN neighbors on either side.
   54883                 : **
   54884                 : ** The minimum value of NN is 1 (of course).  Increasing NN above 1
   54885                 : ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
   54886                 : ** in exchange for a larger degradation in INSERT and UPDATE performance.
   54887                 : ** The value of NN appears to give the best results overall.
   54888                 : */
   54889                 : #define NN 1             /* Number of neighbors on either side of pPage */
   54890                 : #define NB (NN*2+1)      /* Total pages involved in the balance */
   54891                 : 
   54892                 : 
   54893                 : #ifndef SQLITE_OMIT_QUICKBALANCE
   54894                 : /*
   54895                 : ** This version of balance() handles the common special case where
   54896                 : ** a new entry is being inserted on the extreme right-end of the
   54897                 : ** tree, in other words, when the new entry will become the largest
   54898                 : ** entry in the tree.
   54899                 : **
   54900                 : ** Instead of trying to balance the 3 right-most leaf pages, just add
   54901                 : ** a new page to the right-hand side and put the one new entry in
   54902                 : ** that page.  This leaves the right side of the tree somewhat
   54903                 : ** unbalanced.  But odds are that we will be inserting new entries
   54904                 : ** at the end soon afterwards so the nearly empty page will quickly
   54905                 : ** fill up.  On average.
   54906                 : **
   54907                 : ** pPage is the leaf page which is the right-most page in the tree.
   54908                 : ** pParent is its parent.  pPage must have a single overflow entry
   54909                 : ** which is also the right-most entry on the page.
   54910                 : **
   54911                 : ** The pSpace buffer is used to store a temporary copy of the divider
   54912                 : ** cell that will be inserted into pParent. Such a cell consists of a 4
   54913                 : ** byte page number followed by a variable length integer. In other
   54914                 : ** words, at most 13 bytes. Hence the pSpace buffer must be at
   54915                 : ** least 13 bytes in size.
   54916                 : */
   54917              54 : static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
   54918              54 :   BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
   54919                 :   MemPage *pNew;                       /* Newly allocated page */
   54920                 :   int rc;                              /* Return Code */
   54921                 :   Pgno pgnoNew;                        /* Page number of pNew */
   54922                 : 
   54923              54 :   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
   54924              54 :   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
   54925              54 :   assert( pPage->nOverflow==1 );
   54926                 : 
   54927                 :   /* This error condition is now caught prior to reaching this function */
   54928              54 :   if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT;
   54929                 : 
   54930                 :   /* Allocate a new page. This page will become the right-sibling of 
   54931                 :   ** pPage. Make the parent page writable, so that the new divider cell
   54932                 :   ** may be inserted. If both these operations are successful, proceed.
   54933                 :   */
   54934              54 :   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
   54935                 : 
   54936              54 :   if( rc==SQLITE_OK ){
   54937                 : 
   54938              54 :     u8 *pOut = &pSpace[4];
   54939              54 :     u8 *pCell = pPage->aOvfl[0].pCell;
   54940              54 :     u16 szCell = cellSizePtr(pPage, pCell);
   54941                 :     u8 *pStop;
   54942                 : 
   54943              54 :     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
   54944              54 :     assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
   54945              54 :     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
   54946              54 :     assemblePage(pNew, 1, &pCell, &szCell);
   54947                 : 
   54948                 :     /* If this is an auto-vacuum database, update the pointer map
   54949                 :     ** with entries for the new page, and any pointer from the 
   54950                 :     ** cell on the page to an overflow page. If either of these
   54951                 :     ** operations fails, the return code is set, but the contents
   54952                 :     ** of the parent page are still manipulated by thh code below.
   54953                 :     ** That is Ok, at this point the parent page is guaranteed to
   54954                 :     ** be marked as dirty. Returning an error code will cause a
   54955                 :     ** rollback, undoing any changes made to the parent page.
   54956                 :     */
   54957              54 :     if( ISAUTOVACUUM ){
   54958               0 :       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
   54959               0 :       if( szCell>pNew->minLocal ){
   54960               0 :         ptrmapPutOvflPtr(pNew, pCell, &rc);
   54961                 :       }
   54962                 :     }
   54963                 :   
   54964                 :     /* Create a divider cell to insert into pParent. The divider cell
   54965                 :     ** consists of a 4-byte page number (the page number of pPage) and
   54966                 :     ** a variable length key value (which must be the same value as the
   54967                 :     ** largest key on pPage).
   54968                 :     **
   54969                 :     ** To find the largest key value on pPage, first find the right-most 
   54970                 :     ** cell on pPage. The first two fields of this cell are the 
   54971                 :     ** record-length (a variable length integer at most 32-bits in size)
   54972                 :     ** and the key value (a variable length integer, may have any value).
   54973                 :     ** The first of the while(...) loops below skips over the record-length
   54974                 :     ** field. The second while(...) loop copies the key value from the
   54975                 :     ** cell on pPage into the pSpace buffer.
   54976                 :     */
   54977              54 :     pCell = findCell(pPage, pPage->nCell-1);
   54978              54 :     pStop = &pCell[9];
   54979              54 :     while( (*(pCell++)&0x80) && pCell<pStop );
   54980              54 :     pStop = &pCell[9];
   54981              54 :     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
   54982                 : 
   54983                 :     /* Insert the new divider cell into pParent. */
   54984              54 :     insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
   54985                 :                0, pPage->pgno, &rc);
   54986                 : 
   54987                 :     /* Set the right-child pointer of pParent to point to the new page. */
   54988              54 :     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
   54989                 :   
   54990                 :     /* Release the reference to the new page. */
   54991              54 :     releasePage(pNew);
   54992                 :   }
   54993                 : 
   54994              54 :   return rc;
   54995                 : }
   54996                 : #endif /* SQLITE_OMIT_QUICKBALANCE */
   54997                 : 
   54998                 : #if 0
   54999                 : /*
   55000                 : ** This function does not contribute anything to the operation of SQLite.
   55001                 : ** it is sometimes activated temporarily while debugging code responsible 
   55002                 : ** for setting pointer-map entries.
   55003                 : */
   55004                 : static int ptrmapCheckPages(MemPage **apPage, int nPage){
   55005                 :   int i, j;
   55006                 :   for(i=0; i<nPage; i++){
   55007                 :     Pgno n;
   55008                 :     u8 e;
   55009                 :     MemPage *pPage = apPage[i];
   55010                 :     BtShared *pBt = pPage->pBt;
   55011                 :     assert( pPage->isInit );
   55012                 : 
   55013                 :     for(j=0; j<pPage->nCell; j++){
   55014                 :       CellInfo info;
   55015                 :       u8 *z;
   55016                 :      
   55017                 :       z = findCell(pPage, j);
   55018                 :       btreeParseCellPtr(pPage, z, &info);
   55019                 :       if( info.iOverflow ){
   55020                 :         Pgno ovfl = get4byte(&z[info.iOverflow]);
   55021                 :         ptrmapGet(pBt, ovfl, &e, &n);
   55022                 :         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
   55023                 :       }
   55024                 :       if( !pPage->leaf ){
   55025                 :         Pgno child = get4byte(z);
   55026                 :         ptrmapGet(pBt, child, &e, &n);
   55027                 :         assert( n==pPage->pgno && e==PTRMAP_BTREE );
   55028                 :       }
   55029                 :     }
   55030                 :     if( !pPage->leaf ){
   55031                 :       Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
   55032                 :       ptrmapGet(pBt, child, &e, &n);
   55033                 :       assert( n==pPage->pgno && e==PTRMAP_BTREE );
   55034                 :     }
   55035                 :   }
   55036                 :   return 1;
   55037                 : }
   55038                 : #endif
   55039                 : 
   55040                 : /*
   55041                 : ** This function is used to copy the contents of the b-tree node stored 
   55042                 : ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
   55043                 : ** the pointer-map entries for each child page are updated so that the
   55044                 : ** parent page stored in the pointer map is page pTo. If pFrom contained
   55045                 : ** any cells with overflow page pointers, then the corresponding pointer
   55046                 : ** map entries are also updated so that the parent page is page pTo.
   55047                 : **
   55048                 : ** If pFrom is currently carrying any overflow cells (entries in the
   55049                 : ** MemPage.aOvfl[] array), they are not copied to pTo. 
   55050                 : **
   55051                 : ** Before returning, page pTo is reinitialized using btreeInitPage().
   55052                 : **
   55053                 : ** The performance of this function is not critical. It is only used by 
   55054                 : ** the balance_shallower() and balance_deeper() procedures, neither of
   55055                 : ** which are called often under normal circumstances.
   55056                 : */
   55057              28 : static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
   55058              28 :   if( (*pRC)==SQLITE_OK ){
   55059              28 :     BtShared * const pBt = pFrom->pBt;
   55060              28 :     u8 * const aFrom = pFrom->aData;
   55061              28 :     u8 * const aTo = pTo->aData;
   55062              28 :     int const iFromHdr = pFrom->hdrOffset;
   55063              28 :     int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
   55064                 :     int rc;
   55065                 :     int iData;
   55066                 :   
   55067                 :   
   55068              28 :     assert( pFrom->isInit );
   55069              28 :     assert( pFrom->nFree>=iToHdr );
   55070              28 :     assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
   55071                 :   
   55072                 :     /* Copy the b-tree node content from page pFrom to page pTo. */
   55073              28 :     iData = get2byte(&aFrom[iFromHdr+5]);
   55074              28 :     memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
   55075              28 :     memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
   55076                 :   
   55077                 :     /* Reinitialize page pTo so that the contents of the MemPage structure
   55078                 :     ** match the new data. The initialization of pTo can actually fail under
   55079                 :     ** fairly obscure circumstances, even though it is a copy of initialized 
   55080                 :     ** page pFrom.
   55081                 :     */
   55082              28 :     pTo->isInit = 0;
   55083              28 :     rc = btreeInitPage(pTo);
   55084              28 :     if( rc!=SQLITE_OK ){
   55085               0 :       *pRC = rc;
   55086               0 :       return;
   55087                 :     }
   55088                 :   
   55089                 :     /* If this is an auto-vacuum database, update the pointer-map entries
   55090                 :     ** for any b-tree or overflow pages that pTo now contains the pointers to.
   55091                 :     */
   55092              28 :     if( ISAUTOVACUUM ){
   55093               0 :       *pRC = setChildPtrmaps(pTo);
   55094                 :     }
   55095                 :   }
   55096                 : }
   55097                 : 
   55098                 : /*
   55099                 : ** This routine redistributes cells on the iParentIdx'th child of pParent
   55100                 : ** (hereafter "the page") and up to 2 siblings so that all pages have about the
   55101                 : ** same amount of free space. Usually a single sibling on either side of the
   55102                 : ** page are used in the balancing, though both siblings might come from one
   55103                 : ** side if the page is the first or last child of its parent. If the page 
   55104                 : ** has fewer than 2 siblings (something which can only happen if the page
   55105                 : ** is a root page or a child of a root page) then all available siblings
   55106                 : ** participate in the balancing.
   55107                 : **
   55108                 : ** The number of siblings of the page might be increased or decreased by 
   55109                 : ** one or two in an effort to keep pages nearly full but not over full. 
   55110                 : **
   55111                 : ** Note that when this routine is called, some of the cells on the page
   55112                 : ** might not actually be stored in MemPage.aData[]. This can happen
   55113                 : ** if the page is overfull. This routine ensures that all cells allocated
   55114                 : ** to the page and its siblings fit into MemPage.aData[] before returning.
   55115                 : **
   55116                 : ** In the course of balancing the page and its siblings, cells may be
   55117                 : ** inserted into or removed from the parent page (pParent). Doing so
   55118                 : ** may cause the parent page to become overfull or underfull. If this
   55119                 : ** happens, it is the responsibility of the caller to invoke the correct
   55120                 : ** balancing routine to fix this problem (see the balance() routine). 
   55121                 : **
   55122                 : ** If this routine fails for any reason, it might leave the database
   55123                 : ** in a corrupted state. So if this routine fails, the database should
   55124                 : ** be rolled back.
   55125                 : **
   55126                 : ** The third argument to this function, aOvflSpace, is a pointer to a
   55127                 : ** buffer big enough to hold one page. If while inserting cells into the parent
   55128                 : ** page (pParent) the parent page becomes overfull, this buffer is
   55129                 : ** used to store the parent's overflow cells. Because this function inserts
   55130                 : ** a maximum of four divider cells into the parent page, and the maximum
   55131                 : ** size of a cell stored within an internal node is always less than 1/4
   55132                 : ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
   55133                 : ** enough for all overflow cells.
   55134                 : **
   55135                 : ** If aOvflSpace is set to a null pointer, this function returns 
   55136                 : ** SQLITE_NOMEM.
   55137                 : */
   55138             205 : static int balance_nonroot(
   55139                 :   MemPage *pParent,               /* Parent page of siblings being balanced */
   55140                 :   int iParentIdx,                 /* Index of "the page" in pParent */
   55141                 :   u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
   55142                 :   int isRoot                      /* True if pParent is a root-page */
   55143                 : ){
   55144                 :   BtShared *pBt;               /* The whole database */
   55145             205 :   int nCell = 0;               /* Number of cells in apCell[] */
   55146             205 :   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
   55147             205 :   int nNew = 0;                /* Number of pages in apNew[] */
   55148                 :   int nOld;                    /* Number of pages in apOld[] */
   55149                 :   int i, j, k;                 /* Loop counters */
   55150                 :   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
   55151             205 :   int rc = SQLITE_OK;          /* The return code */
   55152                 :   u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
   55153                 :   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
   55154                 :   int usableSpace;             /* Bytes in pPage beyond the header */
   55155                 :   int pageFlags;               /* Value of pPage->aData[0] */
   55156                 :   int subtotal;                /* Subtotal of bytes in cells on one page */
   55157             205 :   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
   55158             205 :   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
   55159                 :   int szScratch;               /* Size of scratch memory requested */
   55160                 :   MemPage *apOld[NB];          /* pPage and up to two siblings */
   55161                 :   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
   55162                 :   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
   55163                 :   u8 *pRight;                  /* Location in parent of right-sibling pointer */
   55164                 :   u8 *apDiv[NB-1];             /* Divider cells in pParent */
   55165                 :   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
   55166                 :   int szNew[NB+2];             /* Combined size of cells place on i-th page */
   55167             205 :   u8 **apCell = 0;             /* All cells begin balanced */
   55168                 :   u16 *szCell;                 /* Local size of all cells in apCell[] */
   55169                 :   u8 *aSpace1;                 /* Space for copies of dividers cells */
   55170                 :   Pgno pgno;                   /* Temp var to store a page number in */
   55171                 : 
   55172             205 :   pBt = pParent->pBt;
   55173             205 :   assert( sqlite3_mutex_held(pBt->mutex) );
   55174             205 :   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
   55175                 : 
   55176                 : #if 0
   55177                 :   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
   55178                 : #endif
   55179                 : 
   55180                 :   /* At this point pParent may have at most one overflow cell. And if
   55181                 :   ** this overflow cell is present, it must be the cell with 
   55182                 :   ** index iParentIdx. This scenario comes about when this function
   55183                 :   ** is called (indirectly) from sqlite3BtreeDelete().
   55184                 :   */
   55185             205 :   assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
   55186             205 :   assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx );
   55187                 : 
   55188             205 :   if( !aOvflSpace ){
   55189               0 :     return SQLITE_NOMEM;
   55190                 :   }
   55191                 : 
   55192                 :   /* Find the sibling pages to balance. Also locate the cells in pParent 
   55193                 :   ** that divide the siblings. An attempt is made to find NN siblings on 
   55194                 :   ** either side of pPage. More siblings are taken from one side, however, 
   55195                 :   ** if there are fewer than NN siblings on the other side. If pParent
   55196                 :   ** has NB or fewer children then all children of pParent are taken.  
   55197                 :   **
   55198                 :   ** This loop also drops the divider cells from the parent page. This
   55199                 :   ** way, the remainder of the function does not have to deal with any
   55200                 :   ** overflow cells in the parent page, since if any existed they will
   55201                 :   ** have already been removed.
   55202                 :   */
   55203             205 :   i = pParent->nOverflow + pParent->nCell;
   55204             205 :   if( i<2 ){
   55205              51 :     nxDiv = 0;
   55206              51 :     nOld = i+1;
   55207                 :   }else{
   55208             154 :     nOld = 3;
   55209             154 :     if( iParentIdx==0 ){                 
   55210              10 :       nxDiv = 0;
   55211             144 :     }else if( iParentIdx==i ){
   55212              25 :       nxDiv = i-2;
   55213                 :     }else{
   55214             119 :       nxDiv = iParentIdx-1;
   55215                 :     }
   55216             154 :     i = 2;
   55217                 :   }
   55218             205 :   if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
   55219              94 :     pRight = &pParent->aData[pParent->hdrOffset+8];
   55220                 :   }else{
   55221             111 :     pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
   55222                 :   }
   55223             205 :   pgno = get4byte(pRight);
   55224                 :   while( 1 ){
   55225             549 :     rc = getAndInitPage(pBt, pgno, &apOld[i]);
   55226             549 :     if( rc ){
   55227               0 :       memset(apOld, 0, (i+1)*sizeof(MemPage*));
   55228               0 :       goto balance_cleanup;
   55229                 :     }
   55230             549 :     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
   55231             549 :     if( (i--)==0 ) break;
   55232                 : 
   55233             344 :     if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){
   55234               0 :       apDiv[i] = pParent->aOvfl[0].pCell;
   55235               0 :       pgno = get4byte(apDiv[i]);
   55236               0 :       szNew[i] = cellSizePtr(pParent, apDiv[i]);
   55237               0 :       pParent->nOverflow = 0;
   55238                 :     }else{
   55239             344 :       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
   55240             344 :       pgno = get4byte(apDiv[i]);
   55241             344 :       szNew[i] = cellSizePtr(pParent, apDiv[i]);
   55242                 : 
   55243                 :       /* Drop the cell from the parent page. apDiv[i] still points to
   55244                 :       ** the cell within the parent, even though it has been dropped.
   55245                 :       ** This is safe because dropping a cell only overwrites the first
   55246                 :       ** four bytes of it, and this function does not need the first
   55247                 :       ** four bytes of the divider cell. So the pointer is safe to use
   55248                 :       ** later on.  
   55249                 :       **
   55250                 :       ** But not if we are in secure-delete mode. In secure-delete mode,
   55251                 :       ** the dropCell() routine will overwrite the entire cell with zeroes.
   55252                 :       ** In this case, temporarily copy the cell into the aOvflSpace[]
   55253                 :       ** buffer. It will be copied out again as soon as the aSpace[] buffer
   55254                 :       ** is allocated.  */
   55255             344 :       if( pBt->btsFlags & BTS_SECURE_DELETE ){
   55256                 :         int iOff;
   55257                 : 
   55258             344 :         iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
   55259             344 :         if( (iOff+szNew[i])>(int)pBt->usableSize ){
   55260               0 :           rc = SQLITE_CORRUPT_BKPT;
   55261               0 :           memset(apOld, 0, (i+1)*sizeof(MemPage*));
   55262               0 :           goto balance_cleanup;
   55263                 :         }else{
   55264             344 :           memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
   55265             344 :           apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
   55266                 :         }
   55267                 :       }
   55268             344 :       dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
   55269                 :     }
   55270             344 :   }
   55271                 : 
   55272                 :   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
   55273                 :   ** alignment */
   55274             205 :   nMaxCells = (nMaxCells + 3)&~3;
   55275                 : 
   55276                 :   /*
   55277                 :   ** Allocate space for memory structures
   55278                 :   */
   55279             205 :   k = pBt->pageSize + ROUND8(sizeof(MemPage));
   55280             205 :   szScratch =
   55281                 :        nMaxCells*sizeof(u8*)                       /* apCell */
   55282             205 :      + nMaxCells*sizeof(u16)                       /* szCell */
   55283             205 :      + pBt->pageSize                               /* aSpace1 */
   55284             410 :      + k*nOld;                                     /* Page copies (apCopy) */
   55285             205 :   apCell = sqlite3ScratchMalloc( szScratch ); 
   55286             205 :   if( apCell==0 ){
   55287               0 :     rc = SQLITE_NOMEM;
   55288               0 :     goto balance_cleanup;
   55289                 :   }
   55290             205 :   szCell = (u16*)&apCell[nMaxCells];
   55291             205 :   aSpace1 = (u8*)&szCell[nMaxCells];
   55292             205 :   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
   55293                 : 
   55294                 :   /*
   55295                 :   ** Load pointers to all cells on sibling pages and the divider cells
   55296                 :   ** into the local apCell[] array.  Make copies of the divider cells
   55297                 :   ** into space obtained from aSpace1[] and remove the the divider Cells
   55298                 :   ** from pParent.
   55299                 :   **
   55300                 :   ** If the siblings are on leaf pages, then the child pointers of the
   55301                 :   ** divider cells are stripped from the cells before they are copied
   55302                 :   ** into aSpace1[].  In this way, all cells in apCell[] are without
   55303                 :   ** child pointers.  If siblings are not leaves, then all cell in
   55304                 :   ** apCell[] include child pointers.  Either way, all cells in apCell[]
   55305                 :   ** are alike.
   55306                 :   **
   55307                 :   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
   55308                 :   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
   55309                 :   */
   55310             205 :   leafCorrection = apOld[0]->leaf*4;
   55311             205 :   leafData = apOld[0]->hasData;
   55312             754 :   for(i=0; i<nOld; i++){
   55313                 :     int limit;
   55314                 :     
   55315                 :     /* Before doing anything else, take a copy of the i'th original sibling
   55316                 :     ** The rest of this function will use data from the copies rather
   55317                 :     ** that the original pages since the original pages will be in the
   55318                 :     ** process of being overwritten.  */
   55319             549 :     MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
   55320             549 :     memcpy(pOld, apOld[i], sizeof(MemPage));
   55321             549 :     pOld->aData = (void*)&pOld[1];
   55322             549 :     memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
   55323                 : 
   55324             549 :     limit = pOld->nCell+pOld->nOverflow;
   55325             549 :     if( pOld->nOverflow>0 ){
   55326           59976 :       for(j=0; j<limit; j++){
   55327           59782 :         assert( nCell<nMaxCells );
   55328           59782 :         apCell[nCell] = findOverflowCell(pOld, j);
   55329           59782 :         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
   55330           59782 :         nCell++;
   55331                 :       }
   55332                 :     }else{
   55333             355 :       u8 *aData = pOld->aData;
   55334             355 :       u16 maskPage = pOld->maskPage;
   55335             355 :       u16 cellOffset = pOld->cellOffset;
   55336           45404 :       for(j=0; j<limit; j++){
   55337           45049 :         assert( nCell<nMaxCells );
   55338           45049 :         apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
   55339           45049 :         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
   55340           45049 :         nCell++;
   55341                 :       }
   55342                 :     }       
   55343             549 :     if( i<nOld-1 && !leafData){
   55344             240 :       u16 sz = (u16)szNew[i];
   55345                 :       u8 *pTemp;
   55346             240 :       assert( nCell<nMaxCells );
   55347             240 :       szCell[nCell] = sz;
   55348             240 :       pTemp = &aSpace1[iSpace1];
   55349             240 :       iSpace1 += sz;
   55350             240 :       assert( sz<=pBt->maxLocal+23 );
   55351             240 :       assert( iSpace1 <= (int)pBt->pageSize );
   55352             240 :       memcpy(pTemp, apDiv[i], sz);
   55353             240 :       apCell[nCell] = pTemp+leafCorrection;
   55354             240 :       assert( leafCorrection==0 || leafCorrection==4 );
   55355             240 :       szCell[nCell] = szCell[nCell] - leafCorrection;
   55356             240 :       if( !pOld->leaf ){
   55357               0 :         assert( leafCorrection==0 );
   55358               0 :         assert( pOld->hdrOffset==0 );
   55359                 :         /* The right pointer of the child page pOld becomes the left
   55360                 :         ** pointer of the divider cell */
   55361               0 :         memcpy(apCell[nCell], &pOld->aData[8], 4);
   55362                 :       }else{
   55363             240 :         assert( leafCorrection==4 );
   55364             240 :         if( szCell[nCell]<4 ){
   55365                 :           /* Do not allow any cells smaller than 4 bytes. */
   55366               0 :           szCell[nCell] = 4;
   55367                 :         }
   55368                 :       }
   55369             240 :       nCell++;
   55370                 :     }
   55371                 :   }
   55372                 : 
   55373                 :   /*
   55374                 :   ** Figure out the number of pages needed to hold all nCell cells.
   55375                 :   ** Store this number in "k".  Also compute szNew[] which is the total
   55376                 :   ** size of all cells on the i-th page and cntNew[] which is the index
   55377                 :   ** in apCell[] of the cell that divides page i from page i+1.  
   55378                 :   ** cntNew[k] should equal nCell.
   55379                 :   **
   55380                 :   ** Values computed by this block:
   55381                 :   **
   55382                 :   **           k: The total number of sibling pages
   55383                 :   **    szNew[i]: Spaced used on the i-th sibling page.
   55384                 :   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
   55385                 :   **              the right of the i-th sibling page.
   55386                 :   ** usableSpace: Number of bytes of space available on each sibling.
   55387                 :   ** 
   55388                 :   */
   55389             205 :   usableSpace = pBt->usableSize - 12 + leafCorrection;
   55390          105392 :   for(subtotal=k=i=0; i<nCell; i++){
   55391          105187 :     assert( i<nMaxCells );
   55392          105187 :     subtotal += szCell[i] + 2;
   55393          105187 :     if( subtotal > usableSpace ){
   55394             381 :       szNew[k] = subtotal - szCell[i];
   55395             381 :       cntNew[k] = i;
   55396             381 :       if( leafData ){ i--; }
   55397             381 :       subtotal = 0;
   55398             381 :       k++;
   55399             381 :       if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
   55400                 :     }
   55401                 :   }
   55402             205 :   szNew[k] = subtotal;
   55403             205 :   cntNew[k] = nCell;
   55404             205 :   k++;
   55405                 : 
   55406                 :   /*
   55407                 :   ** The packing computed by the previous block is biased toward the siblings
   55408                 :   ** on the left side.  The left siblings are always nearly full, while the
   55409                 :   ** right-most sibling might be nearly empty.  This block of code attempts
   55410                 :   ** to adjust the packing of siblings to get a better balance.
   55411                 :   **
   55412                 :   ** This adjustment is more than an optimization.  The packing above might
   55413                 :   ** be so out of balance as to be illegal.  For example, the right-most
   55414                 :   ** sibling might be completely empty.  This adjustment is not optional.
   55415                 :   */
   55416             586 :   for(i=k-1; i>0; i--){
   55417             381 :     int szRight = szNew[i];  /* Size of sibling on the right */
   55418             381 :     int szLeft = szNew[i-1]; /* Size of sibling on the left */
   55419                 :     int r;              /* Index of right-most cell in left sibling */
   55420                 :     int d;              /* Index of first cell to the left of right sibling */
   55421                 : 
   55422             381 :     r = cntNew[i-1] - 1;
   55423             381 :     d = r + 1 - leafData;
   55424             381 :     assert( d<nMaxCells );
   55425             381 :     assert( r<nMaxCells );
   55426           18037 :     while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
   55427           17275 :       szRight += szCell[d] + 2;
   55428           17275 :       szLeft -= szCell[r] + 2;
   55429           17275 :       cntNew[i-1]--;
   55430           17275 :       r = cntNew[i-1] - 1;
   55431           17275 :       d = r + 1 - leafData;
   55432                 :     }
   55433             381 :     szNew[i] = szRight;
   55434             381 :     szNew[i-1] = szLeft;
   55435                 :   }
   55436                 : 
   55437                 :   /* Either we found one or more cells (cntnew[0])>0) or pPage is
   55438                 :   ** a virtual root page.  A virtual root page is when the real root
   55439                 :   ** page is page 1 and we are the only child of that page.
   55440                 :   **
   55441                 :   ** UPDATE:  The assert() below is not necessarily true if the database
   55442                 :   ** file is corrupt.  The corruption will be detected and reported later
   55443                 :   ** in this procedure so there is no need to act upon it now.
   55444                 :   */
   55445                 : #if 0
   55446                 :   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
   55447                 : #endif
   55448                 : 
   55449                 :   TRACE(("BALANCE: old: %d %d %d  ",
   55450                 :     apOld[0]->pgno, 
   55451                 :     nOld>=2 ? apOld[1]->pgno : 0,
   55452                 :     nOld>=3 ? apOld[2]->pgno : 0
   55453                 :   ));
   55454                 : 
   55455                 :   /*
   55456                 :   ** Allocate k new pages.  Reuse old pages where possible.
   55457                 :   */
   55458             205 :   if( apOld[0]->pgno<=1 ){
   55459               0 :     rc = SQLITE_CORRUPT_BKPT;
   55460               0 :     goto balance_cleanup;
   55461                 :   }
   55462             205 :   pageFlags = apOld[0]->aData[0];
   55463             791 :   for(i=0; i<k; i++){
   55464                 :     MemPage *pNew;
   55465             586 :     if( i<nOld ){
   55466             544 :       pNew = apNew[i] = apOld[i];
   55467             544 :       apOld[i] = 0;
   55468             544 :       rc = sqlite3PagerWrite(pNew->pDbPage);
   55469             544 :       nNew++;
   55470             544 :       if( rc ) goto balance_cleanup;
   55471                 :     }else{
   55472              42 :       assert( i>0 );
   55473              42 :       rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0);
   55474              42 :       if( rc ) goto balance_cleanup;
   55475              42 :       apNew[i] = pNew;
   55476              42 :       nNew++;
   55477                 : 
   55478                 :       /* Set the pointer-map entry for the new sibling page. */
   55479              42 :       if( ISAUTOVACUUM ){
   55480               0 :         ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
   55481               0 :         if( rc!=SQLITE_OK ){
   55482               0 :           goto balance_cleanup;
   55483                 :         }
   55484                 :       }
   55485                 :     }
   55486                 :   }
   55487                 : 
   55488                 :   /* Free any old pages that were not reused as new pages.
   55489                 :   */
   55490             415 :   while( i<nOld ){
   55491               5 :     freePage(apOld[i], &rc);
   55492               5 :     if( rc ) goto balance_cleanup;
   55493               5 :     releasePage(apOld[i]);
   55494               5 :     apOld[i] = 0;
   55495               5 :     i++;
   55496                 :   }
   55497                 : 
   55498                 :   /*
   55499                 :   ** Put the new pages in accending order.  This helps to
   55500                 :   ** keep entries in the disk file in order so that a scan
   55501                 :   ** of the table is a linear scan through the file.  That
   55502                 :   ** in turn helps the operating system to deliver pages
   55503                 :   ** from the disk more rapidly.
   55504                 :   **
   55505                 :   ** An O(n^2) insertion sort algorithm is used, but since
   55506                 :   ** n is never more than NB (a small constant), that should
   55507                 :   ** not be a problem.
   55508                 :   **
   55509                 :   ** When NB==3, this one optimization makes the database
   55510                 :   ** about 25% faster for large insertions and deletions.
   55511                 :   */
   55512             586 :   for(i=0; i<k-1; i++){
   55513             381 :     int minV = apNew[i]->pgno;
   55514             381 :     int minI = i;
   55515             966 :     for(j=i+1; j<k; j++){
   55516             585 :       if( apNew[j]->pgno<(unsigned)minV ){
   55517              66 :         minI = j;
   55518              66 :         minV = apNew[j]->pgno;
   55519                 :       }
   55520                 :     }
   55521             381 :     if( minI>i ){
   55522                 :       MemPage *pT;
   55523              66 :       pT = apNew[i];
   55524              66 :       apNew[i] = apNew[minI];
   55525              66 :       apNew[minI] = pT;
   55526                 :     }
   55527                 :   }
   55528                 :   TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
   55529                 :     apNew[0]->pgno, szNew[0],
   55530                 :     nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
   55531                 :     nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
   55532                 :     nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
   55533                 :     nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
   55534                 : 
   55535             205 :   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
   55536             205 :   put4byte(pRight, apNew[nNew-1]->pgno);
   55537                 : 
   55538                 :   /*
   55539                 :   ** Evenly distribute the data in apCell[] across the new pages.
   55540                 :   ** Insert divider cells into pParent as necessary.
   55541                 :   */
   55542             205 :   j = 0;
   55543             791 :   for(i=0; i<nNew; i++){
   55544                 :     /* Assemble the new sibling page. */
   55545             586 :     MemPage *pNew = apNew[i];
   55546             586 :     assert( j<nMaxCells );
   55547             586 :     zeroPage(pNew, pageFlags);
   55548             586 :     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
   55549             586 :     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
   55550             586 :     assert( pNew->nOverflow==0 );
   55551                 : 
   55552             586 :     j = cntNew[i];
   55553                 : 
   55554                 :     /* If the sibling page assembled above was not the right-most sibling,
   55555                 :     ** insert a divider cell into the parent page.
   55556                 :     */
   55557             586 :     assert( i<nNew-1 || j==nCell );
   55558             586 :     if( j<nCell ){
   55559                 :       u8 *pCell;
   55560                 :       u8 *pTemp;
   55561                 :       int sz;
   55562                 : 
   55563             381 :       assert( j<nMaxCells );
   55564             381 :       pCell = apCell[j];
   55565             381 :       sz = szCell[j] + leafCorrection;
   55566             381 :       pTemp = &aOvflSpace[iOvflSpace];
   55567             381 :       if( !pNew->leaf ){
   55568               0 :         memcpy(&pNew->aData[8], pCell, 4);
   55569             381 :       }else if( leafData ){
   55570                 :         /* If the tree is a leaf-data tree, and the siblings are leaves, 
   55571                 :         ** then there is no divider cell in apCell[]. Instead, the divider 
   55572                 :         ** cell consists of the integer key for the right-most cell of 
   55573                 :         ** the sibling-page assembled above only.
   55574                 :         */
   55575                 :         CellInfo info;
   55576             116 :         j--;
   55577             116 :         btreeParseCellPtr(pNew, apCell[j], &info);
   55578             116 :         pCell = pTemp;
   55579             116 :         sz = 4 + putVarint(&pCell[4], info.nKey);
   55580             116 :         pTemp = 0;
   55581                 :       }else{
   55582             265 :         pCell -= 4;
   55583                 :         /* Obscure case for non-leaf-data trees: If the cell at pCell was
   55584                 :         ** previously stored on a leaf node, and its reported size was 4
   55585                 :         ** bytes, then it may actually be smaller than this 
   55586                 :         ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
   55587                 :         ** any cell). But it is important to pass the correct size to 
   55588                 :         ** insertCell(), so reparse the cell now.
   55589                 :         **
   55590                 :         ** Note that this can never happen in an SQLite data file, as all
   55591                 :         ** cells are at least 4 bytes. It only happens in b-trees used
   55592                 :         ** to evaluate "IN (SELECT ...)" and similar clauses.
   55593                 :         */
   55594             265 :         if( szCell[j]==4 ){
   55595               0 :           assert(leafCorrection==4);
   55596               0 :           sz = cellSizePtr(pParent, pCell);
   55597                 :         }
   55598                 :       }
   55599             381 :       iOvflSpace += sz;
   55600             381 :       assert( sz<=pBt->maxLocal+23 );
   55601             381 :       assert( iOvflSpace <= (int)pBt->pageSize );
   55602             381 :       insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
   55603             381 :       if( rc!=SQLITE_OK ) goto balance_cleanup;
   55604             381 :       assert( sqlite3PagerIswriteable(pParent->pDbPage) );
   55605                 : 
   55606             381 :       j++;
   55607             381 :       nxDiv++;
   55608                 :     }
   55609                 :   }
   55610             205 :   assert( j==nCell );
   55611             205 :   assert( nOld>0 );
   55612             205 :   assert( nNew>0 );
   55613             205 :   if( (pageFlags & PTF_LEAF)==0 ){
   55614               0 :     u8 *zChild = &apCopy[nOld-1]->aData[8];
   55615               0 :     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
   55616                 :   }
   55617                 : 
   55618             205 :   if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
   55619                 :     /* The root page of the b-tree now contains no cells. The only sibling
   55620                 :     ** page is the right-child of the parent. Copy the contents of the
   55621                 :     ** child page into the parent, decreasing the overall height of the
   55622                 :     ** b-tree structure by one. This is described as the "balance-shallower"
   55623                 :     ** sub-algorithm in some documentation.
   55624                 :     **
   55625                 :     ** If this is an auto-vacuum database, the call to copyNodeContent() 
   55626                 :     ** sets all pointer-map entries corresponding to database image pages 
   55627                 :     ** for which the pointer is stored within the content being copied.
   55628                 :     **
   55629                 :     ** The second assert below verifies that the child page is defragmented
   55630                 :     ** (it must be, as it was just reconstructed using assemblePage()). This
   55631                 :     ** is important if the parent page happens to be page 1 of the database
   55632                 :     ** image.  */
   55633               3 :     assert( nNew==1 );
   55634               3 :     assert( apNew[0]->nFree == 
   55635                 :         (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) 
   55636                 :     );
   55637               3 :     copyNodeContent(apNew[0], pParent, &rc);
   55638               3 :     freePage(apNew[0], &rc);
   55639             202 :   }else if( ISAUTOVACUUM ){
   55640                 :     /* Fix the pointer-map entries for all the cells that were shifted around. 
   55641                 :     ** There are several different types of pointer-map entries that need to
   55642                 :     ** be dealt with by this routine. Some of these have been set already, but
   55643                 :     ** many have not. The following is a summary:
   55644                 :     **
   55645                 :     **   1) The entries associated with new sibling pages that were not
   55646                 :     **      siblings when this function was called. These have already
   55647                 :     **      been set. We don't need to worry about old siblings that were
   55648                 :     **      moved to the free-list - the freePage() code has taken care
   55649                 :     **      of those.
   55650                 :     **
   55651                 :     **   2) The pointer-map entries associated with the first overflow
   55652                 :     **      page in any overflow chains used by new divider cells. These 
   55653                 :     **      have also already been taken care of by the insertCell() code.
   55654                 :     **
   55655                 :     **   3) If the sibling pages are not leaves, then the child pages of
   55656                 :     **      cells stored on the sibling pages may need to be updated.
   55657                 :     **
   55658                 :     **   4) If the sibling pages are not internal intkey nodes, then any
   55659                 :     **      overflow pages used by these cells may need to be updated
   55660                 :     **      (internal intkey nodes never contain pointers to overflow pages).
   55661                 :     **
   55662                 :     **   5) If the sibling pages are not leaves, then the pointer-map
   55663                 :     **      entries for the right-child pages of each sibling may need
   55664                 :     **      to be updated.
   55665                 :     **
   55666                 :     ** Cases 1 and 2 are dealt with above by other code. The next
   55667                 :     ** block deals with cases 3 and 4 and the one after that, case 5. Since
   55668                 :     ** setting a pointer map entry is a relatively expensive operation, this
   55669                 :     ** code only sets pointer map entries for child or overflow pages that have
   55670                 :     ** actually moved between pages.  */
   55671               0 :     MemPage *pNew = apNew[0];
   55672               0 :     MemPage *pOld = apCopy[0];
   55673               0 :     int nOverflow = pOld->nOverflow;
   55674               0 :     int iNextOld = pOld->nCell + nOverflow;
   55675               0 :     int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1);
   55676               0 :     j = 0;                             /* Current 'old' sibling page */
   55677               0 :     k = 0;                             /* Current 'new' sibling page */
   55678               0 :     for(i=0; i<nCell; i++){
   55679               0 :       int isDivider = 0;
   55680               0 :       while( i==iNextOld ){
   55681                 :         /* Cell i is the cell immediately following the last cell on old
   55682                 :         ** sibling page j. If the siblings are not leaf pages of an
   55683                 :         ** intkey b-tree, then cell i was a divider cell. */
   55684               0 :         assert( j+1 < ArraySize(apCopy) );
   55685               0 :         pOld = apCopy[++j];
   55686               0 :         iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
   55687               0 :         if( pOld->nOverflow ){
   55688               0 :           nOverflow = pOld->nOverflow;
   55689               0 :           iOverflow = i + !leafData + pOld->aOvfl[0].idx;
   55690                 :         }
   55691               0 :         isDivider = !leafData;  
   55692                 :       }
   55693                 : 
   55694               0 :       assert(nOverflow>0 || iOverflow<i );
   55695               0 :       assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1);
   55696               0 :       assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1);
   55697               0 :       if( i==iOverflow ){
   55698               0 :         isDivider = 1;
   55699               0 :         if( (--nOverflow)>0 ){
   55700               0 :           iOverflow++;
   55701                 :         }
   55702                 :       }
   55703                 : 
   55704               0 :       if( i==cntNew[k] ){
   55705                 :         /* Cell i is the cell immediately following the last cell on new
   55706                 :         ** sibling page k. If the siblings are not leaf pages of an
   55707                 :         ** intkey b-tree, then cell i is a divider cell.  */
   55708               0 :         pNew = apNew[++k];
   55709               0 :         if( !leafData ) continue;
   55710                 :       }
   55711               0 :       assert( j<nOld );
   55712               0 :       assert( k<nNew );
   55713                 : 
   55714                 :       /* If the cell was originally divider cell (and is not now) or
   55715                 :       ** an overflow cell, or if the cell was located on a different sibling
   55716                 :       ** page before the balancing, then the pointer map entries associated
   55717                 :       ** with any child or overflow pages need to be updated.  */
   55718               0 :       if( isDivider || pOld->pgno!=pNew->pgno ){
   55719               0 :         if( !leafCorrection ){
   55720               0 :           ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
   55721                 :         }
   55722               0 :         if( szCell[i]>pNew->minLocal ){
   55723               0 :           ptrmapPutOvflPtr(pNew, apCell[i], &rc);
   55724                 :         }
   55725                 :       }
   55726                 :     }
   55727                 : 
   55728               0 :     if( !leafCorrection ){
   55729               0 :       for(i=0; i<nNew; i++){
   55730               0 :         u32 key = get4byte(&apNew[i]->aData[8]);
   55731               0 :         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
   55732                 :       }
   55733                 :     }
   55734                 : 
   55735                 : #if 0
   55736                 :     /* The ptrmapCheckPages() contains assert() statements that verify that
   55737                 :     ** all pointer map pages are set correctly. This is helpful while 
   55738                 :     ** debugging. This is usually disabled because a corrupt database may
   55739                 :     ** cause an assert() statement to fail.  */
   55740                 :     ptrmapCheckPages(apNew, nNew);
   55741                 :     ptrmapCheckPages(&pParent, 1);
   55742                 : #endif
   55743                 :   }
   55744                 : 
   55745             205 :   assert( pParent->isInit );
   55746                 :   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
   55747                 :           nOld, nNew, nCell));
   55748                 : 
   55749                 :   /*
   55750                 :   ** Cleanup before returning.
   55751                 :   */
   55752                 : balance_cleanup:
   55753             205 :   sqlite3ScratchFree(apCell);
   55754             754 :   for(i=0; i<nOld; i++){
   55755             549 :     releasePage(apOld[i]);
   55756                 :   }
   55757             791 :   for(i=0; i<nNew; i++){
   55758             586 :     releasePage(apNew[i]);
   55759                 :   }
   55760                 : 
   55761             205 :   return rc;
   55762                 : }
   55763                 : 
   55764                 : 
   55765                 : /*
   55766                 : ** This function is called when the root page of a b-tree structure is
   55767                 : ** overfull (has one or more overflow pages).
   55768                 : **
   55769                 : ** A new child page is allocated and the contents of the current root
   55770                 : ** page, including overflow cells, are copied into the child. The root
   55771                 : ** page is then overwritten to make it an empty page with the right-child 
   55772                 : ** pointer pointing to the new page.
   55773                 : **
   55774                 : ** Before returning, all pointer-map entries corresponding to pages 
   55775                 : ** that the new child-page now contains pointers to are updated. The
   55776                 : ** entry corresponding to the new right-child pointer of the root
   55777                 : ** page is also updated.
   55778                 : **
   55779                 : ** If successful, *ppChild is set to contain a reference to the child 
   55780                 : ** page and SQLITE_OK is returned. In this case the caller is required
   55781                 : ** to call releasePage() on *ppChild exactly once. If an error occurs,
   55782                 : ** an error code is returned and *ppChild is set to 0.
   55783                 : */
   55784              25 : static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
   55785                 :   int rc;                        /* Return value from subprocedures */
   55786              25 :   MemPage *pChild = 0;           /* Pointer to a new child page */
   55787              25 :   Pgno pgnoChild = 0;            /* Page number of the new child page */
   55788              25 :   BtShared *pBt = pRoot->pBt;    /* The BTree */
   55789                 : 
   55790              25 :   assert( pRoot->nOverflow>0 );
   55791              25 :   assert( sqlite3_mutex_held(pBt->mutex) );
   55792                 : 
   55793                 :   /* Make pRoot, the root page of the b-tree, writable. Allocate a new 
   55794                 :   ** page that will become the new right-child of pPage. Copy the contents
   55795                 :   ** of the node stored on pRoot into the new child page.
   55796                 :   */
   55797              25 :   rc = sqlite3PagerWrite(pRoot->pDbPage);
   55798              25 :   if( rc==SQLITE_OK ){
   55799              25 :     rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
   55800              25 :     copyNodeContent(pRoot, pChild, &rc);
   55801              25 :     if( ISAUTOVACUUM ){
   55802               0 :       ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
   55803                 :     }
   55804                 :   }
   55805              25 :   if( rc ){
   55806               0 :     *ppChild = 0;
   55807               0 :     releasePage(pChild);
   55808               0 :     return rc;
   55809                 :   }
   55810              25 :   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
   55811              25 :   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
   55812              25 :   assert( pChild->nCell==pRoot->nCell );
   55813                 : 
   55814                 :   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
   55815                 : 
   55816                 :   /* Copy the overflow cells from pRoot to pChild */
   55817              25 :   memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0]));
   55818              25 :   pChild->nOverflow = pRoot->nOverflow;
   55819                 : 
   55820                 :   /* Zero the contents of pRoot. Then install pChild as the right-child. */
   55821              25 :   zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
   55822              25 :   put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
   55823                 : 
   55824              25 :   *ppChild = pChild;
   55825              25 :   return SQLITE_OK;
   55826                 : }
   55827                 : 
   55828                 : /*
   55829                 : ** The page that pCur currently points to has just been modified in
   55830                 : ** some way. This function figures out if this modification means the
   55831                 : ** tree needs to be balanced, and if so calls the appropriate balancing 
   55832                 : ** routine. Balancing routines are:
   55833                 : **
   55834                 : **   balance_quick()
   55835                 : **   balance_deeper()
   55836                 : **   balance_nonroot()
   55837                 : */
   55838           52673 : static int balance(BtCursor *pCur){
   55839           52673 :   int rc = SQLITE_OK;
   55840           52673 :   const int nMin = pCur->pBt->usableSize * 2 / 3;
   55841                 :   u8 aBalanceQuickSpace[13];
   55842           52673 :   u8 *pFree = 0;
   55843                 : 
   55844           52673 :   TESTONLY( int balance_quick_called = 0 );
   55845           52673 :   TESTONLY( int balance_deeper_called = 0 );
   55846                 : 
   55847                 :   do {
   55848           52957 :     int iPage = pCur->iPage;
   55849           52957 :     MemPage *pPage = pCur->apPage[iPage];
   55850                 : 
   55851           52957 :     if( iPage==0 ){
   55852           50640 :       if( pPage->nOverflow ){
   55853                 :         /* The root page of the b-tree is overfull. In this case call the
   55854                 :         ** balance_deeper() function to create a new child for the root-page
   55855                 :         ** and copy the current contents of the root-page to it. The
   55856                 :         ** next iteration of the do-loop will balance the child page.
   55857                 :         */ 
   55858              25 :         assert( (balance_deeper_called++)==0 );
   55859              25 :         rc = balance_deeper(pPage, &pCur->apPage[1]);
   55860              25 :         if( rc==SQLITE_OK ){
   55861              25 :           pCur->iPage = 1;
   55862              25 :           pCur->aiIdx[0] = 0;
   55863              25 :           pCur->aiIdx[1] = 0;
   55864              25 :           assert( pCur->apPage[1]->nOverflow );
   55865                 :         }
   55866                 :       }else{
   55867           50615 :         break;
   55868                 :       }
   55869            2317 :     }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
   55870                 :       break;
   55871                 :     }else{
   55872             259 :       MemPage * const pParent = pCur->apPage[iPage-1];
   55873             259 :       int const iIdx = pCur->aiIdx[iPage-1];
   55874                 : 
   55875             259 :       rc = sqlite3PagerWrite(pParent->pDbPage);
   55876             259 :       if( rc==SQLITE_OK ){
   55877                 : #ifndef SQLITE_OMIT_QUICKBALANCE
   55878             259 :         if( pPage->hasData
   55879             113 :          && pPage->nOverflow==1
   55880             107 :          && pPage->aOvfl[0].idx==pPage->nCell
   55881              56 :          && pParent->pgno!=1
   55882              55 :          && pParent->nCell==iIdx
   55883                 :         ){
   55884                 :           /* Call balance_quick() to create a new sibling of pPage on which
   55885                 :           ** to store the overflow cell. balance_quick() inserts a new cell
   55886                 :           ** into pParent, which may cause pParent overflow. If this
   55887                 :           ** happens, the next interation of the do-loop will balance pParent 
   55888                 :           ** use either balance_nonroot() or balance_deeper(). Until this
   55889                 :           ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
   55890                 :           ** buffer. 
   55891                 :           **
   55892                 :           ** The purpose of the following assert() is to check that only a
   55893                 :           ** single call to balance_quick() is made for each call to this
   55894                 :           ** function. If this were not verified, a subtle bug involving reuse
   55895                 :           ** of the aBalanceQuickSpace[] might sneak in.
   55896                 :           */
   55897              54 :           assert( (balance_quick_called++)==0 );
   55898              54 :           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
   55899                 :         }else
   55900                 : #endif
   55901                 :         {
   55902                 :           /* In this case, call balance_nonroot() to redistribute cells
   55903                 :           ** between pPage and up to 2 of its sibling pages. This involves
   55904                 :           ** modifying the contents of pParent, which may cause pParent to
   55905                 :           ** become overfull or underfull. The next iteration of the do-loop
   55906                 :           ** will balance the parent page to correct this.
   55907                 :           ** 
   55908                 :           ** If the parent page becomes overfull, the overflow cell or cells
   55909                 :           ** are stored in the pSpace buffer allocated immediately below. 
   55910                 :           ** A subsequent iteration of the do-loop will deal with this by
   55911                 :           ** calling balance_nonroot() (balance_deeper() may be called first,
   55912                 :           ** but it doesn't deal with overflow cells - just moves them to a
   55913                 :           ** different page). Once this subsequent call to balance_nonroot() 
   55914                 :           ** has completed, it is safe to release the pSpace buffer used by
   55915                 :           ** the previous call, as the overflow cell data will have been 
   55916                 :           ** copied either into the body of a database page or into the new
   55917                 :           ** pSpace buffer passed to the latter call to balance_nonroot().
   55918                 :           */
   55919             205 :           u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
   55920             205 :           rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1);
   55921             205 :           if( pFree ){
   55922                 :             /* If pFree is not NULL, it points to the pSpace buffer used 
   55923                 :             ** by a previous call to balance_nonroot(). Its contents are
   55924                 :             ** now stored either on real database pages or within the 
   55925                 :             ** new pSpace buffer, so it may be safely freed here. */
   55926               0 :             sqlite3PageFree(pFree);
   55927                 :           }
   55928                 : 
   55929                 :           /* The pSpace buffer will be freed after the next call to
   55930                 :           ** balance_nonroot(), or just before this function returns, whichever
   55931                 :           ** comes first. */
   55932             205 :           pFree = pSpace;
   55933                 :         }
   55934                 :       }
   55935                 : 
   55936             259 :       pPage->nOverflow = 0;
   55937                 : 
   55938                 :       /* The next iteration of the do-loop balances the parent page. */
   55939             259 :       releasePage(pPage);
   55940             259 :       pCur->iPage--;
   55941                 :     }
   55942             284 :   }while( rc==SQLITE_OK );
   55943                 : 
   55944           52673 :   if( pFree ){
   55945             205 :     sqlite3PageFree(pFree);
   55946                 :   }
   55947           52673 :   return rc;
   55948                 : }
   55949                 : 
   55950                 : 
   55951                 : /*
   55952                 : ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
   55953                 : ** and the data is given by (pData,nData).  The cursor is used only to
   55954                 : ** define what table the record should be inserted into.  The cursor
   55955                 : ** is left pointing at a random location.
   55956                 : **
   55957                 : ** For an INTKEY table, only the nKey value of the key is used.  pKey is
   55958                 : ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
   55959                 : **
   55960                 : ** If the seekResult parameter is non-zero, then a successful call to
   55961                 : ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
   55962                 : ** been performed. seekResult is the search result returned (a negative
   55963                 : ** number if pCur points at an entry that is smaller than (pKey, nKey), or
   55964                 : ** a positive value if pCur points at an etry that is larger than 
   55965                 : ** (pKey, nKey)). 
   55966                 : **
   55967                 : ** If the seekResult parameter is non-zero, then the caller guarantees that
   55968                 : ** cursor pCur is pointing at the existing copy of a row that is to be
   55969                 : ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
   55970                 : ** point to any entry or to no entry at all and so this function has to seek
   55971                 : ** the cursor before the new key can be inserted.
   55972                 : */
   55973          230859 : SQLITE_PRIVATE int sqlite3BtreeInsert(
   55974                 :   BtCursor *pCur,                /* Insert data into the table of this cursor */
   55975                 :   const void *pKey, i64 nKey,    /* The key of the new record */
   55976                 :   const void *pData, int nData,  /* The data of the new record */
   55977                 :   int nZero,                     /* Number of extra 0 bytes to append to data */
   55978                 :   int appendBias,                /* True if this is likely an append */
   55979                 :   int seekResult                 /* Result of prior MovetoUnpacked() call */
   55980                 : ){
   55981                 :   int rc;
   55982          230859 :   int loc = seekResult;          /* -1: before desired location  +1: after */
   55983          230859 :   int szNew = 0;
   55984                 :   int idx;
   55985                 :   MemPage *pPage;
   55986          230859 :   Btree *p = pCur->pBtree;
   55987          230859 :   BtShared *pBt = p->pBt;
   55988                 :   unsigned char *oldCell;
   55989          230859 :   unsigned char *newCell = 0;
   55990                 : 
   55991          230859 :   if( pCur->eState==CURSOR_FAULT ){
   55992               0 :     assert( pCur->skipNext!=SQLITE_OK );
   55993               0 :     return pCur->skipNext;
   55994                 :   }
   55995                 : 
   55996          230859 :   assert( cursorHoldsMutex(pCur) );
   55997          230859 :   assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
   55998                 :               && (pBt->btsFlags & BTS_READ_ONLY)==0 );
   55999          230859 :   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
   56000                 : 
   56001                 :   /* Assert that the caller has been consistent. If this cursor was opened
   56002                 :   ** expecting an index b-tree, then the caller should be inserting blob
   56003                 :   ** keys with no associated data. If the cursor was opened expecting an
   56004                 :   ** intkey table, the caller should be inserting integer keys with a
   56005                 :   ** blob of associated data.  */
   56006          230859 :   assert( (pKey==0)==(pCur->pKeyInfo==0) );
   56007                 : 
   56008                 :   /* If this is an insert into a table b-tree, invalidate any incrblob 
   56009                 :   ** cursors open on the row being replaced (assuming this is a replace
   56010                 :   ** operation - if it is not, the following is a no-op).  */
   56011          230859 :   if( pCur->pKeyInfo==0 ){
   56012           99148 :     invalidateIncrblobCursors(p, nKey, 0);
   56013                 :   }
   56014                 : 
   56015                 :   /* Save the positions of any other cursors open on this table.
   56016                 :   **
   56017                 :   ** In some cases, the call to btreeMoveto() below is a no-op. For
   56018                 :   ** example, when inserting data into a table with auto-generated integer
   56019                 :   ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 
   56020                 :   ** integer key to use. It then calls this function to actually insert the 
   56021                 :   ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
   56022                 :   ** that the cursor is already where it needs to be and returns without
   56023                 :   ** doing any work. To avoid thwarting these optimizations, it is important
   56024                 :   ** not to clear the cursor here.
   56025                 :   */
   56026          230859 :   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
   56027          230859 :   if( rc ) return rc;
   56028          230859 :   if( !loc ){
   56029          194389 :     rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
   56030          194389 :     if( rc ) return rc;
   56031                 :   }
   56032          230859 :   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
   56033                 : 
   56034          230859 :   pPage = pCur->apPage[pCur->iPage];
   56035          230859 :   assert( pPage->intKey || nKey>=0 );
   56036          230859 :   assert( pPage->leaf || !pPage->intKey );
   56037                 : 
   56038                 :   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
   56039                 :           pCur->pgnoRoot, nKey, nData, pPage->pgno,
   56040                 :           loc==0 ? "overwrite" : "new entry"));
   56041          230859 :   assert( pPage->isInit );
   56042          230859 :   allocateTempSpace(pBt);
   56043          230859 :   newCell = pBt->pTmpSpace;
   56044          230859 :   if( newCell==0 ) return SQLITE_NOMEM;
   56045          230859 :   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
   56046          230859 :   if( rc ) goto end_insert;
   56047          230859 :   assert( szNew==cellSizePtr(pPage, newCell) );
   56048          230859 :   assert( szNew <= MX_CELL_SIZE(pBt) );
   56049          230859 :   idx = pCur->aiIdx[pCur->iPage];
   56050          230859 :   if( loc==0 ){
   56051                 :     u16 szOld;
   56052           40463 :     assert( idx<pPage->nCell );
   56053           40463 :     rc = sqlite3PagerWrite(pPage->pDbPage);
   56054           40463 :     if( rc ){
   56055               0 :       goto end_insert;
   56056                 :     }
   56057           40463 :     oldCell = findCell(pPage, idx);
   56058           40463 :     if( !pPage->leaf ){
   56059               0 :       memcpy(newCell, oldCell, 4);
   56060                 :     }
   56061           40463 :     szOld = cellSizePtr(pPage, oldCell);
   56062           40463 :     rc = clearCell(pPage, oldCell);
   56063           40463 :     dropCell(pPage, idx, szOld, &rc);
   56064           40463 :     if( rc ) goto end_insert;
   56065          190396 :   }else if( loc<0 && pPage->nCell>0 ){
   56066          123706 :     assert( pPage->leaf );
   56067          123706 :     idx = ++pCur->aiIdx[pCur->iPage];
   56068                 :   }else{
   56069           66690 :     assert( pPage->leaf );
   56070                 :   }
   56071          230859 :   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
   56072          230859 :   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
   56073                 : 
   56074                 :   /* If no error has occured and pPage has an overflow cell, call balance() 
   56075                 :   ** to redistribute the cells within the tree. Since balance() may move
   56076                 :   ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
   56077                 :   ** variables.
   56078                 :   **
   56079                 :   ** Previous versions of SQLite called moveToRoot() to move the cursor
   56080                 :   ** back to the root page as balance() used to invalidate the contents
   56081                 :   ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
   56082                 :   ** set the cursor state to "invalid". This makes common insert operations
   56083                 :   ** slightly faster.
   56084                 :   **
   56085                 :   ** There is a subtle but important optimization here too. When inserting
   56086                 :   ** multiple records into an intkey b-tree using a single cursor (as can
   56087                 :   ** happen while processing an "INSERT INTO ... SELECT" statement), it
   56088                 :   ** is advantageous to leave the cursor pointing to the last entry in
   56089                 :   ** the b-tree if possible. If the cursor is left pointing to the last
   56090                 :   ** entry in the table, and the next row inserted has an integer key
   56091                 :   ** larger than the largest existing key, it is possible to insert the
   56092                 :   ** row without seeking the cursor. This can be a big performance boost.
   56093                 :   */
   56094          230859 :   pCur->info.nSize = 0;
   56095          230859 :   pCur->validNKey = 0;
   56096          230859 :   if( rc==SQLITE_OK && pPage->nOverflow ){
   56097             248 :     rc = balance(pCur);
   56098                 : 
   56099                 :     /* Must make sure nOverflow is reset to zero even if the balance()
   56100                 :     ** fails. Internal data structure corruption will result otherwise. 
   56101                 :     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
   56102                 :     ** from trying to save the current position of the cursor.  */
   56103             248 :     pCur->apPage[pCur->iPage]->nOverflow = 0;
   56104             248 :     pCur->eState = CURSOR_INVALID;
   56105                 :   }
   56106          230859 :   assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
   56107                 : 
   56108                 : end_insert:
   56109          230859 :   return rc;
   56110                 : }
   56111                 : 
   56112                 : /*
   56113                 : ** Delete the entry that the cursor is pointing to.  The cursor
   56114                 : ** is left pointing at a arbitrary location.
   56115                 : */
   56116           52425 : SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
   56117           52425 :   Btree *p = pCur->pBtree;
   56118           52425 :   BtShared *pBt = p->pBt;              
   56119                 :   int rc;                              /* Return code */
   56120                 :   MemPage *pPage;                      /* Page to delete cell from */
   56121                 :   unsigned char *pCell;                /* Pointer to cell to delete */
   56122                 :   int iCellIdx;                        /* Index of cell to delete */
   56123                 :   int iCellDepth;                      /* Depth of node containing pCell */ 
   56124                 : 
   56125           52425 :   assert( cursorHoldsMutex(pCur) );
   56126           52425 :   assert( pBt->inTransaction==TRANS_WRITE );
   56127           52425 :   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
   56128           52425 :   assert( pCur->wrFlag );
   56129           52425 :   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
   56130           52425 :   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
   56131                 : 
   56132          104850 :   if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) 
   56133           52425 :    || NEVER(pCur->eState!=CURSOR_VALID)
   56134                 :   ){
   56135                 :     return SQLITE_ERROR;  /* Something has gone awry. */
   56136                 :   }
   56137                 : 
   56138                 :   /* If this is a delete operation to remove a row from a table b-tree,
   56139                 :   ** invalidate any incrblob cursors open on the row being deleted.  */
   56140           52425 :   if( pCur->pKeyInfo==0 ){
   56141            9497 :     invalidateIncrblobCursors(p, pCur->info.nKey, 0);
   56142                 :   }
   56143                 : 
   56144           52425 :   iCellDepth = pCur->iPage;
   56145           52425 :   iCellIdx = pCur->aiIdx[iCellDepth];
   56146           52425 :   pPage = pCur->apPage[iCellDepth];
   56147           52425 :   pCell = findCell(pPage, iCellIdx);
   56148                 : 
   56149                 :   /* If the page containing the entry to delete is not a leaf page, move
   56150                 :   ** the cursor to the largest entry in the tree that is smaller than
   56151                 :   ** the entry being deleted. This cell will replace the cell being deleted
   56152                 :   ** from the internal node. The 'previous' entry is used for this instead
   56153                 :   ** of the 'next' entry, as the previous entry is always a part of the
   56154                 :   ** sub-tree headed by the child page of the cell being deleted. This makes
   56155                 :   ** balancing the tree following the delete operation easier.  */
   56156           52425 :   if( !pPage->leaf ){
   56157                 :     int notUsed;
   56158               0 :     rc = sqlite3BtreePrevious(pCur, &notUsed);
   56159               0 :     if( rc ) return rc;
   56160                 :   }
   56161                 : 
   56162                 :   /* Save the positions of any other cursors open on this table before
   56163                 :   ** making any modifications. Make the page containing the entry to be 
   56164                 :   ** deleted writable. Then free any overflow pages associated with the 
   56165                 :   ** entry and finally remove the cell itself from within the page.  
   56166                 :   */
   56167           52425 :   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
   56168           52425 :   if( rc ) return rc;
   56169           52425 :   rc = sqlite3PagerWrite(pPage->pDbPage);
   56170           52425 :   if( rc ) return rc;
   56171           52425 :   rc = clearCell(pPage, pCell);
   56172           52425 :   dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
   56173           52425 :   if( rc ) return rc;
   56174                 : 
   56175                 :   /* If the cell deleted was not located on a leaf page, then the cursor
   56176                 :   ** is currently pointing to the largest entry in the sub-tree headed
   56177                 :   ** by the child-page of the cell that was just deleted from an internal
   56178                 :   ** node. The cell from the leaf node needs to be moved to the internal
   56179                 :   ** node to replace the deleted cell.  */
   56180           52425 :   if( !pPage->leaf ){
   56181               0 :     MemPage *pLeaf = pCur->apPage[pCur->iPage];
   56182                 :     int nCell;
   56183               0 :     Pgno n = pCur->apPage[iCellDepth+1]->pgno;
   56184                 :     unsigned char *pTmp;
   56185                 : 
   56186               0 :     pCell = findCell(pLeaf, pLeaf->nCell-1);
   56187               0 :     nCell = cellSizePtr(pLeaf, pCell);
   56188               0 :     assert( MX_CELL_SIZE(pBt) >= nCell );
   56189                 : 
   56190               0 :     allocateTempSpace(pBt);
   56191               0 :     pTmp = pBt->pTmpSpace;
   56192                 : 
   56193               0 :     rc = sqlite3PagerWrite(pLeaf->pDbPage);
   56194               0 :     insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
   56195               0 :     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
   56196               0 :     if( rc ) return rc;
   56197                 :   }
   56198                 : 
   56199                 :   /* Balance the tree. If the entry deleted was located on a leaf page,
   56200                 :   ** then the cursor still points to that page. In this case the first
   56201                 :   ** call to balance() repairs the tree, and the if(...) condition is
   56202                 :   ** never true.
   56203                 :   **
   56204                 :   ** Otherwise, if the entry deleted was on an internal node page, then
   56205                 :   ** pCur is pointing to the leaf page from which a cell was removed to
   56206                 :   ** replace the cell deleted from the internal node. This is slightly
   56207                 :   ** tricky as the leaf node may be underfull, and the internal node may
   56208                 :   ** be either under or overfull. In this case run the balancing algorithm
   56209                 :   ** on the leaf node first. If the balance proceeds far enough up the
   56210                 :   ** tree that we can be sure that any problem in the internal node has
   56211                 :   ** been corrected, so be it. Otherwise, after balancing the leaf node,
   56212                 :   ** walk the cursor up the tree to the internal node and balance it as 
   56213                 :   ** well.  */
   56214           52425 :   rc = balance(pCur);
   56215           52425 :   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
   56216               0 :     while( pCur->iPage>iCellDepth ){
   56217               0 :       releasePage(pCur->apPage[pCur->iPage--]);
   56218                 :     }
   56219               0 :     rc = balance(pCur);
   56220                 :   }
   56221                 : 
   56222           52425 :   if( rc==SQLITE_OK ){
   56223           52425 :     moveToRoot(pCur);
   56224                 :   }
   56225           52425 :   return rc;
   56226                 : }
   56227                 : 
   56228                 : /*
   56229                 : ** Create a new BTree table.  Write into *piTable the page
   56230                 : ** number for the root page of the new table.
   56231                 : **
   56232                 : ** The type of type is determined by the flags parameter.  Only the
   56233                 : ** following values of flags are currently in use.  Other values for
   56234                 : ** flags might not work:
   56235                 : **
   56236                 : **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
   56237                 : **     BTREE_ZERODATA                  Used for SQL indices
   56238                 : */
   56239           30571 : static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
   56240           30571 :   BtShared *pBt = p->pBt;
   56241                 :   MemPage *pRoot;
   56242                 :   Pgno pgnoRoot;
   56243                 :   int rc;
   56244                 :   int ptfFlags;          /* Page-type flage for the root page of new table */
   56245                 : 
   56246           30571 :   assert( sqlite3BtreeHoldsMutex(p) );
   56247           30571 :   assert( pBt->inTransaction==TRANS_WRITE );
   56248           30571 :   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
   56249                 : 
   56250                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   56251                 :   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
   56252                 :   if( rc ){
   56253                 :     return rc;
   56254                 :   }
   56255                 : #else
   56256           30571 :   if( pBt->autoVacuum ){
   56257                 :     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
   56258                 :     MemPage *pPageMove; /* The page to move to. */
   56259                 : 
   56260                 :     /* Creating a new table may probably require moving an existing database
   56261                 :     ** to make room for the new tables root page. In case this page turns
   56262                 :     ** out to be an overflow page, delete all overflow page-map caches
   56263                 :     ** held by open cursors.
   56264                 :     */
   56265               0 :     invalidateAllOverflowCache(pBt);
   56266                 : 
   56267                 :     /* Read the value of meta[3] from the database to determine where the
   56268                 :     ** root page of the new table should go. meta[3] is the largest root-page
   56269                 :     ** created so far, so the new root-page is (meta[3]+1).
   56270                 :     */
   56271               0 :     sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
   56272               0 :     pgnoRoot++;
   56273                 : 
   56274                 :     /* The new root-page may not be allocated on a pointer-map page, or the
   56275                 :     ** PENDING_BYTE page.
   56276                 :     */
   56277               0 :     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
   56278               0 :         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
   56279               0 :       pgnoRoot++;
   56280                 :     }
   56281               0 :     assert( pgnoRoot>=3 );
   56282                 : 
   56283                 :     /* Allocate a page. The page that currently resides at pgnoRoot will
   56284                 :     ** be moved to the allocated page (unless the allocated page happens
   56285                 :     ** to reside at pgnoRoot).
   56286                 :     */
   56287               0 :     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
   56288               0 :     if( rc!=SQLITE_OK ){
   56289               0 :       return rc;
   56290                 :     }
   56291                 : 
   56292               0 :     if( pgnoMove!=pgnoRoot ){
   56293                 :       /* pgnoRoot is the page that will be used for the root-page of
   56294                 :       ** the new table (assuming an error did not occur). But we were
   56295                 :       ** allocated pgnoMove. If required (i.e. if it was not allocated
   56296                 :       ** by extending the file), the current page at position pgnoMove
   56297                 :       ** is already journaled.
   56298                 :       */
   56299               0 :       u8 eType = 0;
   56300               0 :       Pgno iPtrPage = 0;
   56301                 : 
   56302               0 :       releasePage(pPageMove);
   56303                 : 
   56304                 :       /* Move the page currently at pgnoRoot to pgnoMove. */
   56305               0 :       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
   56306               0 :       if( rc!=SQLITE_OK ){
   56307               0 :         return rc;
   56308                 :       }
   56309               0 :       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
   56310               0 :       if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
   56311               0 :         rc = SQLITE_CORRUPT_BKPT;
   56312                 :       }
   56313               0 :       if( rc!=SQLITE_OK ){
   56314               0 :         releasePage(pRoot);
   56315               0 :         return rc;
   56316                 :       }
   56317               0 :       assert( eType!=PTRMAP_ROOTPAGE );
   56318               0 :       assert( eType!=PTRMAP_FREEPAGE );
   56319               0 :       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
   56320               0 :       releasePage(pRoot);
   56321                 : 
   56322                 :       /* Obtain the page at pgnoRoot */
   56323               0 :       if( rc!=SQLITE_OK ){
   56324               0 :         return rc;
   56325                 :       }
   56326               0 :       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
   56327               0 :       if( rc!=SQLITE_OK ){
   56328               0 :         return rc;
   56329                 :       }
   56330               0 :       rc = sqlite3PagerWrite(pRoot->pDbPage);
   56331               0 :       if( rc!=SQLITE_OK ){
   56332               0 :         releasePage(pRoot);
   56333               0 :         return rc;
   56334                 :       }
   56335                 :     }else{
   56336               0 :       pRoot = pPageMove;
   56337                 :     } 
   56338                 : 
   56339                 :     /* Update the pointer-map and meta-data with the new root-page number. */
   56340               0 :     ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
   56341               0 :     if( rc ){
   56342               0 :       releasePage(pRoot);
   56343               0 :       return rc;
   56344                 :     }
   56345                 : 
   56346                 :     /* When the new root page was allocated, page 1 was made writable in
   56347                 :     ** order either to increase the database filesize, or to decrement the
   56348                 :     ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
   56349                 :     */
   56350               0 :     assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
   56351               0 :     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
   56352               0 :     if( NEVER(rc) ){
   56353               0 :       releasePage(pRoot);
   56354               0 :       return rc;
   56355                 :     }
   56356                 : 
   56357                 :   }else{
   56358           30571 :     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
   56359           30571 :     if( rc ) return rc;
   56360                 :   }
   56361                 : #endif
   56362           30571 :   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
   56363           30571 :   if( createTabFlags & BTREE_INTKEY ){
   56364            6782 :     ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
   56365                 :   }else{
   56366           23789 :     ptfFlags = PTF_ZERODATA | PTF_LEAF;
   56367                 :   }
   56368           30571 :   zeroPage(pRoot, ptfFlags);
   56369           30571 :   sqlite3PagerUnref(pRoot->pDbPage);
   56370           30571 :   assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
   56371           30571 :   *piTable = (int)pgnoRoot;
   56372           30571 :   return SQLITE_OK;
   56373                 : }
   56374           30571 : SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
   56375                 :   int rc;
   56376           30571 :   sqlite3BtreeEnter(p);
   56377           30571 :   rc = btreeCreateTable(p, piTable, flags);
   56378           30571 :   sqlite3BtreeLeave(p);
   56379           30571 :   return rc;
   56380                 : }
   56381                 : 
   56382                 : /*
   56383                 : ** Erase the given database page and all its children.  Return
   56384                 : ** the page to the freelist.
   56385                 : */
   56386             703 : static int clearDatabasePage(
   56387                 :   BtShared *pBt,           /* The BTree that contains the table */
   56388                 :   Pgno pgno,               /* Page number to clear */
   56389                 :   int freePageFlag,        /* Deallocate page if true */
   56390                 :   int *pnChange            /* Add number of Cells freed to this counter */
   56391                 : ){
   56392                 :   MemPage *pPage;
   56393                 :   int rc;
   56394                 :   unsigned char *pCell;
   56395                 :   int i;
   56396                 : 
   56397             703 :   assert( sqlite3_mutex_held(pBt->mutex) );
   56398             703 :   if( pgno>btreePagecount(pBt) ){
   56399               0 :     return SQLITE_CORRUPT_BKPT;
   56400                 :   }
   56401                 : 
   56402             703 :   rc = getAndInitPage(pBt, pgno, &pPage);
   56403             703 :   if( rc ) return rc;
   56404            3108 :   for(i=0; i<pPage->nCell; i++){
   56405            2405 :     pCell = findCell(pPage, i);
   56406            2405 :     if( !pPage->leaf ){
   56407              19 :       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
   56408              19 :       if( rc ) goto cleardatabasepage_out;
   56409                 :     }
   56410            2405 :     rc = clearCell(pPage, pCell);
   56411            2405 :     if( rc ) goto cleardatabasepage_out;
   56412                 :   }
   56413             703 :   if( !pPage->leaf ){
   56414               2 :     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
   56415               2 :     if( rc ) goto cleardatabasepage_out;
   56416             701 :   }else if( pnChange ){
   56417             269 :     assert( pPage->intKey );
   56418             269 :     *pnChange += pPage->nCell;
   56419                 :   }
   56420             703 :   if( freePageFlag ){
   56421              21 :     freePage(pPage, &rc);
   56422             682 :   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
   56423             682 :     zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
   56424                 :   }
   56425                 : 
   56426                 : cleardatabasepage_out:
   56427             703 :   releasePage(pPage);
   56428             703 :   return rc;
   56429                 : }
   56430                 : 
   56431                 : /*
   56432                 : ** Delete all information from a single table in the database.  iTable is
   56433                 : ** the page number of the root of the table.  After this routine returns,
   56434                 : ** the root page is empty, but still exists.
   56435                 : **
   56436                 : ** This routine will fail with SQLITE_LOCKED if there are any open
   56437                 : ** read cursors on the table.  Open write cursors are moved to the
   56438                 : ** root of the table.
   56439                 : **
   56440                 : ** If pnChange is not NULL, then table iTable must be an intkey table. The
   56441                 : ** integer value pointed to by pnChange is incremented by the number of
   56442                 : ** entries in the table.
   56443                 : */
   56444             682 : SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
   56445                 :   int rc;
   56446             682 :   BtShared *pBt = p->pBt;
   56447             682 :   sqlite3BtreeEnter(p);
   56448             682 :   assert( p->inTrans==TRANS_WRITE );
   56449                 : 
   56450                 :   /* Invalidate all incrblob cursors open on table iTable (assuming iTable
   56451                 :   ** is the root of a table b-tree - if it is not, the following call is
   56452                 :   ** a no-op).  */
   56453             682 :   invalidateIncrblobCursors(p, 0, 1);
   56454                 : 
   56455             682 :   rc = saveAllCursors(pBt, (Pgno)iTable, 0);
   56456             682 :   if( SQLITE_OK==rc ){
   56457             682 :     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
   56458                 :   }
   56459             682 :   sqlite3BtreeLeave(p);
   56460             682 :   return rc;
   56461                 : }
   56462                 : 
   56463                 : /*
   56464                 : ** Erase all information in a table and add the root of the table to
   56465                 : ** the freelist.  Except, the root of the principle table (the one on
   56466                 : ** page 1) is never added to the freelist.
   56467                 : **
   56468                 : ** This routine will fail with SQLITE_LOCKED if there are any open
   56469                 : ** cursors on the table.
   56470                 : **
   56471                 : ** If AUTOVACUUM is enabled and the page at iTable is not the last
   56472                 : ** root page in the database file, then the last root page 
   56473                 : ** in the database file is moved into the slot formerly occupied by
   56474                 : ** iTable and that last slot formerly occupied by the last root page
   56475                 : ** is added to the freelist instead of iTable.  In this say, all
   56476                 : ** root pages are kept at the beginning of the database file, which
   56477                 : ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
   56478                 : ** page number that used to be the last root page in the file before
   56479                 : ** the move.  If no page gets moved, *piMoved is set to 0.
   56480                 : ** The last root page is recorded in meta[3] and the value of
   56481                 : ** meta[3] is updated by this procedure.
   56482                 : */
   56483              64 : static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
   56484                 :   int rc;
   56485              64 :   MemPage *pPage = 0;
   56486              64 :   BtShared *pBt = p->pBt;
   56487                 : 
   56488              64 :   assert( sqlite3BtreeHoldsMutex(p) );
   56489              64 :   assert( p->inTrans==TRANS_WRITE );
   56490                 : 
   56491                 :   /* It is illegal to drop a table if any cursors are open on the
   56492                 :   ** database. This is because in auto-vacuum mode the backend may
   56493                 :   ** need to move another root-page to fill a gap left by the deleted
   56494                 :   ** root page. If an open cursor was using this page a problem would 
   56495                 :   ** occur.
   56496                 :   **
   56497                 :   ** This error is caught long before control reaches this point.
   56498                 :   */
   56499              64 :   if( NEVER(pBt->pCursor) ){
   56500               0 :     sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
   56501               0 :     return SQLITE_LOCKED_SHAREDCACHE;
   56502                 :   }
   56503                 : 
   56504              64 :   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
   56505              64 :   if( rc ) return rc;
   56506              64 :   rc = sqlite3BtreeClearTable(p, iTable, 0);
   56507              64 :   if( rc ){
   56508               0 :     releasePage(pPage);
   56509               0 :     return rc;
   56510                 :   }
   56511                 : 
   56512              64 :   *piMoved = 0;
   56513                 : 
   56514              64 :   if( iTable>1 ){
   56515                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   56516                 :     freePage(pPage, &rc);
   56517                 :     releasePage(pPage);
   56518                 : #else
   56519              64 :     if( pBt->autoVacuum ){
   56520                 :       Pgno maxRootPgno;
   56521               0 :       sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
   56522                 : 
   56523               0 :       if( iTable==maxRootPgno ){
   56524                 :         /* If the table being dropped is the table with the largest root-page
   56525                 :         ** number in the database, put the root page on the free list. 
   56526                 :         */
   56527               0 :         freePage(pPage, &rc);
   56528               0 :         releasePage(pPage);
   56529               0 :         if( rc!=SQLITE_OK ){
   56530               0 :           return rc;
   56531                 :         }
   56532                 :       }else{
   56533                 :         /* The table being dropped does not have the largest root-page
   56534                 :         ** number in the database. So move the page that does into the 
   56535                 :         ** gap left by the deleted root-page.
   56536                 :         */
   56537                 :         MemPage *pMove;
   56538               0 :         releasePage(pPage);
   56539               0 :         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
   56540               0 :         if( rc!=SQLITE_OK ){
   56541               0 :           return rc;
   56542                 :         }
   56543               0 :         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
   56544               0 :         releasePage(pMove);
   56545               0 :         if( rc!=SQLITE_OK ){
   56546               0 :           return rc;
   56547                 :         }
   56548               0 :         pMove = 0;
   56549               0 :         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
   56550               0 :         freePage(pMove, &rc);
   56551               0 :         releasePage(pMove);
   56552               0 :         if( rc!=SQLITE_OK ){
   56553               0 :           return rc;
   56554                 :         }
   56555               0 :         *piMoved = maxRootPgno;
   56556                 :       }
   56557                 : 
   56558                 :       /* Set the new 'max-root-page' value in the database header. This
   56559                 :       ** is the old value less one, less one more if that happens to
   56560                 :       ** be a root-page number, less one again if that is the
   56561                 :       ** PENDING_BYTE_PAGE.
   56562                 :       */
   56563               0 :       maxRootPgno--;
   56564               0 :       while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
   56565               0 :              || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
   56566               0 :         maxRootPgno--;
   56567                 :       }
   56568               0 :       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
   56569                 : 
   56570               0 :       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
   56571                 :     }else{
   56572              64 :       freePage(pPage, &rc);
   56573              64 :       releasePage(pPage);
   56574                 :     }
   56575                 : #endif
   56576                 :   }else{
   56577                 :     /* If sqlite3BtreeDropTable was called on page 1.
   56578                 :     ** This really never should happen except in a corrupt
   56579                 :     ** database. 
   56580                 :     */
   56581               0 :     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
   56582               0 :     releasePage(pPage);
   56583                 :   }
   56584              64 :   return rc;  
   56585                 : }
   56586              64 : SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
   56587                 :   int rc;
   56588              64 :   sqlite3BtreeEnter(p);
   56589              64 :   rc = btreeDropTable(p, iTable, piMoved);
   56590              64 :   sqlite3BtreeLeave(p);
   56591              64 :   return rc;
   56592                 : }
   56593                 : 
   56594                 : 
   56595                 : /*
   56596                 : ** This function may only be called if the b-tree connection already
   56597                 : ** has a read or write transaction open on the database.
   56598                 : **
   56599                 : ** Read the meta-information out of a database file.  Meta[0]
   56600                 : ** is the number of free pages currently in the database.  Meta[1]
   56601                 : ** through meta[15] are available for use by higher layers.  Meta[0]
   56602                 : ** is read-only, the others are read/write.
   56603                 : ** 
   56604                 : ** The schema layer numbers meta values differently.  At the schema
   56605                 : ** layer (and the SetCookie and ReadCookie opcodes) the number of
   56606                 : ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
   56607                 : */
   56608          182926 : SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
   56609          182926 :   BtShared *pBt = p->pBt;
   56610                 : 
   56611          182926 :   sqlite3BtreeEnter(p);
   56612          182926 :   assert( p->inTrans>TRANS_NONE );
   56613          182926 :   assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
   56614          182926 :   assert( pBt->pPage1 );
   56615          182926 :   assert( idx>=0 && idx<=15 );
   56616                 : 
   56617          182926 :   *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
   56618                 : 
   56619                 :   /* If auto-vacuum is disabled in this build and this is an auto-vacuum
   56620                 :   ** database, mark the database as read-only.  */
   56621                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   56622                 :   if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
   56623                 :     pBt->btsFlags |= BTS_READ_ONLY;
   56624                 :   }
   56625                 : #endif
   56626                 : 
   56627          182926 :   sqlite3BtreeLeave(p);
   56628          182926 : }
   56629                 : 
   56630                 : /*
   56631                 : ** Write meta-information back into the database.  Meta[0] is
   56632                 : ** read-only and may not be written.
   56633                 : */
   56634           20009 : SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
   56635           20009 :   BtShared *pBt = p->pBt;
   56636                 :   unsigned char *pP1;
   56637                 :   int rc;
   56638           20009 :   assert( idx>=1 && idx<=15 );
   56639           20009 :   sqlite3BtreeEnter(p);
   56640           20009 :   assert( p->inTrans==TRANS_WRITE );
   56641           20009 :   assert( pBt->pPage1!=0 );
   56642           20009 :   pP1 = pBt->pPage1->aData;
   56643           20009 :   rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
   56644           20009 :   if( rc==SQLITE_OK ){
   56645           20009 :     put4byte(&pP1[36 + idx*4], iMeta);
   56646                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   56647           20009 :     if( idx==BTREE_INCR_VACUUM ){
   56648               0 :       assert( pBt->autoVacuum || iMeta==0 );
   56649               0 :       assert( iMeta==0 || iMeta==1 );
   56650               0 :       pBt->incrVacuum = (u8)iMeta;
   56651                 :     }
   56652                 : #endif
   56653                 :   }
   56654           20009 :   sqlite3BtreeLeave(p);
   56655           20009 :   return rc;
   56656                 : }
   56657                 : 
   56658                 : #ifndef SQLITE_OMIT_BTREECOUNT
   56659                 : /*
   56660                 : ** The first argument, pCur, is a cursor opened on some b-tree. Count the
   56661                 : ** number of entries in the b-tree and write the result to *pnEntry.
   56662                 : **
   56663                 : ** SQLITE_OK is returned if the operation is successfully executed. 
   56664                 : ** Otherwise, if an error is encountered (i.e. an IO error or database
   56665                 : ** corruption) an SQLite error code is returned.
   56666                 : */
   56667             631 : SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
   56668             631 :   i64 nEntry = 0;                      /* Value to return in *pnEntry */
   56669                 :   int rc;                              /* Return code */
   56670                 : 
   56671             631 :   if( pCur->pgnoRoot==0 ){
   56672               0 :     *pnEntry = 0;
   56673               0 :     return SQLITE_OK;
   56674                 :   }
   56675             631 :   rc = moveToRoot(pCur);
   56676                 : 
   56677                 :   /* Unless an error occurs, the following loop runs one iteration for each
   56678                 :   ** page in the B-Tree structure (not including overflow pages). 
   56679                 :   */
   56680            1276 :   while( rc==SQLITE_OK ){
   56681                 :     int iIdx;                          /* Index of child node in parent */
   56682                 :     MemPage *pPage;                    /* Current page of the b-tree */
   56683                 : 
   56684                 :     /* If this is a leaf page or the tree is not an int-key tree, then 
   56685                 :     ** this page contains countable entries. Increment the entry counter
   56686                 :     ** accordingly.
   56687                 :     */
   56688             645 :     pPage = pCur->apPage[pCur->iPage];
   56689             645 :     if( pPage->leaf || !pPage->intKey ){
   56690             645 :       nEntry += pPage->nCell;
   56691                 :     }
   56692                 : 
   56693                 :     /* pPage is a leaf node. This loop navigates the cursor so that it 
   56694                 :     ** points to the first interior cell that it points to the parent of
   56695                 :     ** the next page in the tree that has not yet been visited. The
   56696                 :     ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
   56697                 :     ** of the page, or to the number of cells in the page if the next page
   56698                 :     ** to visit is the right-child of its parent.
   56699                 :     **
   56700                 :     ** If all pages in the tree have been visited, return SQLITE_OK to the
   56701                 :     ** caller.
   56702                 :     */
   56703             645 :     if( pPage->leaf ){
   56704                 :       do {
   56705             645 :         if( pCur->iPage==0 ){
   56706                 :           /* All pages of the b-tree have been visited. Return successfully. */
   56707             631 :           *pnEntry = nEntry;
   56708             631 :           return SQLITE_OK;
   56709                 :         }
   56710              14 :         moveToParent(pCur);
   56711              14 :       }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
   56712                 : 
   56713              13 :       pCur->aiIdx[pCur->iPage]++;
   56714              13 :       pPage = pCur->apPage[pCur->iPage];
   56715                 :     }
   56716                 : 
   56717                 :     /* Descend to the child node of the cell that the cursor currently 
   56718                 :     ** points at. This is the right-child if (iIdx==pPage->nCell).
   56719                 :     */
   56720              14 :     iIdx = pCur->aiIdx[pCur->iPage];
   56721              14 :     if( iIdx==pPage->nCell ){
   56722               1 :       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
   56723                 :     }else{
   56724              13 :       rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
   56725                 :     }
   56726                 :   }
   56727                 : 
   56728                 :   /* An error has occurred. Return an error code. */
   56729               0 :   return rc;
   56730                 : }
   56731                 : #endif
   56732                 : 
   56733                 : /*
   56734                 : ** Return the pager associated with a BTree.  This routine is used for
   56735                 : ** testing and debugging only.
   56736                 : */
   56737          313973 : SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
   56738          313973 :   return p->pBt->pPager;
   56739                 : }
   56740                 : 
   56741                 : #ifndef SQLITE_OMIT_INTEGRITY_CHECK
   56742                 : /*
   56743                 : ** Append a message to the error message string.
   56744                 : */
   56745               0 : static void checkAppendMsg(
   56746                 :   IntegrityCk *pCheck,
   56747                 :   char *zMsg1,
   56748                 :   const char *zFormat,
   56749                 :   ...
   56750                 : ){
   56751                 :   va_list ap;
   56752               0 :   if( !pCheck->mxErr ) return;
   56753               0 :   pCheck->mxErr--;
   56754               0 :   pCheck->nErr++;
   56755               0 :   va_start(ap, zFormat);
   56756               0 :   if( pCheck->errMsg.nChar ){
   56757               0 :     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
   56758                 :   }
   56759               0 :   if( zMsg1 ){
   56760               0 :     sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
   56761                 :   }
   56762               0 :   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
   56763               0 :   va_end(ap);
   56764               0 :   if( pCheck->errMsg.mallocFailed ){
   56765               0 :     pCheck->mallocFailed = 1;
   56766                 :   }
   56767                 : }
   56768                 : #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
   56769                 : 
   56770                 : #ifndef SQLITE_OMIT_INTEGRITY_CHECK
   56771                 : /*
   56772                 : ** Add 1 to the reference count for page iPage.  If this is the second
   56773                 : ** reference to the page, add an error message to pCheck->zErrMsg.
   56774                 : ** Return 1 if there are 2 ore more references to the page and 0 if
   56775                 : ** if this is the first reference to the page.
   56776                 : **
   56777                 : ** Also check that the page number is in bounds.
   56778                 : */
   56779            1033 : static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
   56780            1033 :   if( iPage==0 ) return 1;
   56781            1033 :   if( iPage>pCheck->nPage ){
   56782               0 :     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
   56783               0 :     return 1;
   56784                 :   }
   56785            1033 :   if( pCheck->anRef[iPage]==1 ){
   56786               0 :     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
   56787               0 :     return 1;
   56788                 :   }
   56789            1033 :   return  (pCheck->anRef[iPage]++)>1;
   56790                 : }
   56791                 : 
   56792                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   56793                 : /*
   56794                 : ** Check that the entry in the pointer-map for page iChild maps to 
   56795                 : ** page iParent, pointer type ptrType. If not, append an error message
   56796                 : ** to pCheck.
   56797                 : */
   56798               0 : static void checkPtrmap(
   56799                 :   IntegrityCk *pCheck,   /* Integrity check context */
   56800                 :   Pgno iChild,           /* Child page number */
   56801                 :   u8 eType,              /* Expected pointer map type */
   56802                 :   Pgno iParent,          /* Expected pointer map parent page number */
   56803                 :   char *zContext         /* Context description (used for error msg) */
   56804                 : ){
   56805                 :   int rc;
   56806                 :   u8 ePtrmapType;
   56807                 :   Pgno iPtrmapParent;
   56808                 : 
   56809               0 :   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
   56810               0 :   if( rc!=SQLITE_OK ){
   56811               0 :     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
   56812               0 :     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
   56813               0 :     return;
   56814                 :   }
   56815                 : 
   56816               0 :   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
   56817               0 :     checkAppendMsg(pCheck, zContext, 
   56818                 :       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
   56819                 :       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
   56820                 :   }
   56821                 : }
   56822                 : #endif
   56823                 : 
   56824                 : /*
   56825                 : ** Check the integrity of the freelist or of an overflow page list.
   56826                 : ** Verify that the number of pages on the list is N.
   56827                 : */
   56828              52 : static void checkList(
   56829                 :   IntegrityCk *pCheck,  /* Integrity checking context */
   56830                 :   int isFreeList,       /* True for a freelist.  False for overflow page list */
   56831                 :   int iPage,            /* Page number for first page in the list */
   56832                 :   int N,                /* Expected number of pages in the list */
   56833                 :   char *zContext        /* Context for error messages */
   56834                 : ){
   56835                 :   int i;
   56836              52 :   int expected = N;
   56837              52 :   int iFirst = iPage;
   56838             127 :   while( N-- > 0 && pCheck->mxErr ){
   56839                 :     DbPage *pOvflPage;
   56840                 :     unsigned char *pOvflData;
   56841              23 :     if( iPage<1 ){
   56842               0 :       checkAppendMsg(pCheck, zContext,
   56843                 :          "%d of %d pages missing from overflow list starting at %d",
   56844                 :           N+1, expected, iFirst);
   56845               0 :       break;
   56846                 :     }
   56847              23 :     if( checkRef(pCheck, iPage, zContext) ) break;
   56848              23 :     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
   56849               0 :       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
   56850               0 :       break;
   56851                 :     }
   56852              23 :     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
   56853              23 :     if( isFreeList ){
   56854              23 :       int n = get4byte(&pOvflData[4]);
   56855                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   56856              23 :       if( pCheck->pBt->autoVacuum ){
   56857               0 :         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
   56858                 :       }
   56859                 : #endif
   56860              23 :       if( n>(int)pCheck->pBt->usableSize/4-2 ){
   56861               0 :         checkAppendMsg(pCheck, zContext,
   56862                 :            "freelist leaf count too big on page %d", iPage);
   56863               0 :         N--;
   56864                 :       }else{
   56865              46 :         for(i=0; i<n; i++){
   56866              23 :           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
   56867                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   56868              23 :           if( pCheck->pBt->autoVacuum ){
   56869               0 :             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
   56870                 :           }
   56871                 : #endif
   56872              23 :           checkRef(pCheck, iFreePage, zContext);
   56873                 :         }
   56874              23 :         N -= n;
   56875                 :       }
   56876                 :     }
   56877                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   56878                 :     else{
   56879                 :       /* If this database supports auto-vacuum and iPage is not the last
   56880                 :       ** page in this overflow list, check that the pointer-map entry for
   56881                 :       ** the following page matches iPage.
   56882                 :       */
   56883               0 :       if( pCheck->pBt->autoVacuum && N>0 ){
   56884               0 :         i = get4byte(pOvflData);
   56885               0 :         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
   56886                 :       }
   56887                 :     }
   56888                 : #endif
   56889              23 :     iPage = get4byte(pOvflData);
   56890              23 :     sqlite3PagerUnref(pOvflPage);
   56891                 :   }
   56892              52 : }
   56893                 : #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
   56894                 : 
   56895                 : #ifndef SQLITE_OMIT_INTEGRITY_CHECK
   56896                 : /*
   56897                 : ** Do various sanity checks on a single page of a tree.  Return
   56898                 : ** the tree depth.  Root pages return 0.  Parents of root pages
   56899                 : ** return 1, and so forth.
   56900                 : ** 
   56901                 : ** These checks are done:
   56902                 : **
   56903                 : **      1.  Make sure that cells and freeblocks do not overlap
   56904                 : **          but combine to completely cover the page.
   56905                 : **  NO  2.  Make sure cell keys are in order.
   56906                 : **  NO  3.  Make sure no key is less than or equal to zLowerBound.
   56907                 : **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
   56908                 : **      5.  Check the integrity of overflow pages.
   56909                 : **      6.  Recursively call checkTreePage on all children.
   56910                 : **      7.  Verify that the depth of all children is the same.
   56911                 : **      8.  Make sure this page is at least 33% full or else it is
   56912                 : **          the root of the tree.
   56913                 : */
   56914             987 : static int checkTreePage(
   56915                 :   IntegrityCk *pCheck,  /* Context for the sanity check */
   56916                 :   int iPage,            /* Page number of the page to check */
   56917                 :   char *zParentContext, /* Parent context */
   56918                 :   i64 *pnParentMinKey, 
   56919                 :   i64 *pnParentMaxKey
   56920                 : ){
   56921                 :   MemPage *pPage;
   56922                 :   int i, rc, depth, d2, pgno, cnt;
   56923                 :   int hdr, cellStart;
   56924                 :   int nCell;
   56925                 :   u8 *data;
   56926                 :   BtShared *pBt;
   56927                 :   int usableSize;
   56928                 :   char zContext[100];
   56929             987 :   char *hit = 0;
   56930             987 :   i64 nMinKey = 0;
   56931             987 :   i64 nMaxKey = 0;
   56932                 : 
   56933             987 :   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
   56934                 : 
   56935                 :   /* Check that the page exists
   56936                 :   */
   56937             987 :   pBt = pCheck->pBt;
   56938             987 :   usableSize = pBt->usableSize;
   56939             987 :   if( iPage==0 ) return 0;
   56940             987 :   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
   56941             987 :   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
   56942               0 :     checkAppendMsg(pCheck, zContext,
   56943                 :        "unable to get the page. error code=%d", rc);
   56944               0 :     return 0;
   56945                 :   }
   56946                 : 
   56947                 :   /* Clear MemPage.isInit to make sure the corruption detection code in
   56948                 :   ** btreeInitPage() is executed.  */
   56949             987 :   pPage->isInit = 0;
   56950             987 :   if( (rc = btreeInitPage(pPage))!=0 ){
   56951               0 :     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
   56952               0 :     checkAppendMsg(pCheck, zContext, 
   56953                 :                    "btreeInitPage() returns error code %d", rc);
   56954               0 :     releasePage(pPage);
   56955               0 :     return 0;
   56956                 :   }
   56957                 : 
   56958                 :   /* Check out all the cells.
   56959                 :   */
   56960             987 :   depth = 0;
   56961            3904 :   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
   56962                 :     u8 *pCell;
   56963                 :     u32 sz;
   56964                 :     CellInfo info;
   56965                 : 
   56966                 :     /* Check payload overflow pages
   56967                 :     */
   56968            2917 :     sqlite3_snprintf(sizeof(zContext), zContext,
   56969                 :              "On tree page %d cell %d: ", iPage, i);
   56970            2917 :     pCell = findCell(pPage,i);
   56971            2917 :     btreeParseCellPtr(pPage, pCell, &info);
   56972            2917 :     sz = info.nData;
   56973            2917 :     if( !pPage->intKey ) sz += (int)info.nKey;
   56974                 :     /* For intKey pages, check that the keys are in order.
   56975                 :     */
   56976            1643 :     else if( i==0 ) nMinKey = nMaxKey = info.nKey;
   56977                 :     else{
   56978            1441 :       if( info.nKey <= nMaxKey ){
   56979               0 :         checkAppendMsg(pCheck, zContext, 
   56980                 :             "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
   56981                 :       }
   56982            1441 :       nMaxKey = info.nKey;
   56983                 :     }
   56984            2917 :     assert( sz==info.nPayload );
   56985            2917 :     if( (sz>info.nLocal) 
   56986               0 :      && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
   56987                 :     ){
   56988               0 :       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
   56989               0 :       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
   56990                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   56991               0 :       if( pBt->autoVacuum ){
   56992               0 :         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
   56993                 :       }
   56994                 : #endif
   56995               0 :       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
   56996                 :     }
   56997                 : 
   56998                 :     /* Check sanity of left child page.
   56999                 :     */
   57000            2917 :     if( !pPage->leaf ){
   57001               0 :       pgno = get4byte(pCell);
   57002                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   57003               0 :       if( pBt->autoVacuum ){
   57004               0 :         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
   57005                 :       }
   57006                 : #endif
   57007               0 :       d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
   57008               0 :       if( i>0 && d2!=depth ){
   57009               0 :         checkAppendMsg(pCheck, zContext, "Child page depth differs");
   57010                 :       }
   57011               0 :       depth = d2;
   57012                 :     }
   57013                 :   }
   57014                 : 
   57015             987 :   if( !pPage->leaf ){
   57016               0 :     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
   57017               0 :     sqlite3_snprintf(sizeof(zContext), zContext, 
   57018                 :                      "On page %d at right child: ", iPage);
   57019                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   57020               0 :     if( pBt->autoVacuum ){
   57021               0 :       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
   57022                 :     }
   57023                 : #endif
   57024               0 :     checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
   57025                 :   }
   57026                 :  
   57027                 :   /* For intKey leaf pages, check that the min/max keys are in order
   57028                 :   ** with any left/parent/right pages.
   57029                 :   */
   57030             987 :   if( pPage->leaf && pPage->intKey ){
   57031                 :     /* if we are a left child page */
   57032             415 :     if( pnParentMinKey ){
   57033                 :       /* if we are the left most child page */
   57034               0 :       if( !pnParentMaxKey ){
   57035               0 :         if( nMaxKey > *pnParentMinKey ){
   57036               0 :           checkAppendMsg(pCheck, zContext, 
   57037                 :               "Rowid %lld out of order (max larger than parent min of %lld)",
   57038                 :               nMaxKey, *pnParentMinKey);
   57039                 :         }
   57040                 :       }else{
   57041               0 :         if( nMinKey <= *pnParentMinKey ){
   57042               0 :           checkAppendMsg(pCheck, zContext, 
   57043                 :               "Rowid %lld out of order (min less than parent min of %lld)",
   57044                 :               nMinKey, *pnParentMinKey);
   57045                 :         }
   57046               0 :         if( nMaxKey > *pnParentMaxKey ){
   57047               0 :           checkAppendMsg(pCheck, zContext, 
   57048                 :               "Rowid %lld out of order (max larger than parent max of %lld)",
   57049                 :               nMaxKey, *pnParentMaxKey);
   57050                 :         }
   57051               0 :         *pnParentMinKey = nMaxKey;
   57052                 :       }
   57053                 :     /* else if we're a right child page */
   57054             415 :     } else if( pnParentMaxKey ){
   57055               0 :       if( nMinKey <= *pnParentMaxKey ){
   57056               0 :         checkAppendMsg(pCheck, zContext, 
   57057                 :             "Rowid %lld out of order (min less than parent max of %lld)",
   57058                 :             nMinKey, *pnParentMaxKey);
   57059                 :       }
   57060                 :     }
   57061                 :   }
   57062                 : 
   57063                 :   /* Check for complete coverage of the page
   57064                 :   */
   57065             987 :   data = pPage->aData;
   57066             987 :   hdr = pPage->hdrOffset;
   57067             987 :   hit = sqlite3PageMalloc( pBt->pageSize );
   57068             987 :   if( hit==0 ){
   57069               0 :     pCheck->mallocFailed = 1;
   57070                 :   }else{
   57071             987 :     int contentOffset = get2byteNotZero(&data[hdr+5]);
   57072             987 :     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
   57073             987 :     memset(hit+contentOffset, 0, usableSize-contentOffset);
   57074             987 :     memset(hit, 1, contentOffset);
   57075             987 :     nCell = get2byte(&data[hdr+3]);
   57076             987 :     cellStart = hdr + 12 - 4*pPage->leaf;
   57077            3904 :     for(i=0; i<nCell; i++){
   57078            2917 :       int pc = get2byte(&data[cellStart+i*2]);
   57079            2917 :       u32 size = 65536;
   57080                 :       int j;
   57081            2917 :       if( pc<=usableSize-4 ){
   57082            2917 :         size = cellSizePtr(pPage, &data[pc]);
   57083                 :       }
   57084            2917 :       if( (int)(pc+size-1)>=usableSize ){
   57085               0 :         checkAppendMsg(pCheck, 0, 
   57086                 :             "Corruption detected in cell %d on page %d",i,iPage);
   57087                 :       }else{
   57088            2917 :         for(j=pc+size-1; j>=pc; j--) hit[j]++;
   57089                 :       }
   57090                 :     }
   57091             987 :     i = get2byte(&data[hdr+1]);
   57092            2044 :     while( i>0 ){
   57093                 :       int size, j;
   57094              70 :       assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
   57095              70 :       size = get2byte(&data[i+2]);
   57096              70 :       assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
   57097              70 :       for(j=i+size-1; j>=i; j--) hit[j]++;
   57098              70 :       j = get2byte(&data[i]);
   57099              70 :       assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
   57100              70 :       assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
   57101              70 :       i = j;
   57102                 :     }
   57103        32343003 :     for(i=cnt=0; i<usableSize; i++){
   57104        32342016 :       if( hit[i]==0 ){
   57105               2 :         cnt++;
   57106        32342014 :       }else if( hit[i]>1 ){
   57107               0 :         checkAppendMsg(pCheck, 0,
   57108                 :           "Multiple uses for byte %d of page %d", i, iPage);
   57109               0 :         break;
   57110                 :       }
   57111                 :     }
   57112             987 :     if( cnt!=data[hdr+7] ){
   57113               0 :       checkAppendMsg(pCheck, 0, 
   57114                 :           "Fragmentation of %d bytes reported as %d on page %d",
   57115               0 :           cnt, data[hdr+7], iPage);
   57116                 :     }
   57117                 :   }
   57118             987 :   sqlite3PageFree(hit);
   57119             987 :   releasePage(pPage);
   57120             987 :   return depth+1;
   57121                 : }
   57122                 : #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
   57123                 : 
   57124                 : #ifndef SQLITE_OMIT_INTEGRITY_CHECK
   57125                 : /*
   57126                 : ** This routine does a complete check of the given BTree file.  aRoot[] is
   57127                 : ** an array of pages numbers were each page number is the root page of
   57128                 : ** a table.  nRoot is the number of entries in aRoot.
   57129                 : **
   57130                 : ** A read-only or read-write transaction must be opened before calling
   57131                 : ** this function.
   57132                 : **
   57133                 : ** Write the number of error seen in *pnErr.  Except for some memory
   57134                 : ** allocation errors,  an error message held in memory obtained from
   57135                 : ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
   57136                 : ** returned.  If a memory allocation error occurs, NULL is returned.
   57137                 : */
   57138              52 : SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
   57139                 :   Btree *p,     /* The btree to be checked */
   57140                 :   int *aRoot,   /* An array of root pages numbers for individual trees */
   57141                 :   int nRoot,    /* Number of entries in aRoot[] */
   57142                 :   int mxErr,    /* Stop reporting errors after this many */
   57143                 :   int *pnErr    /* Write number of errors seen to this variable */
   57144                 : ){
   57145                 :   Pgno i;
   57146                 :   int nRef;
   57147                 :   IntegrityCk sCheck;
   57148              52 :   BtShared *pBt = p->pBt;
   57149                 :   char zErr[100];
   57150                 : 
   57151              52 :   sqlite3BtreeEnter(p);
   57152              52 :   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
   57153              52 :   nRef = sqlite3PagerRefcount(pBt->pPager);
   57154              52 :   sCheck.pBt = pBt;
   57155              52 :   sCheck.pPager = pBt->pPager;
   57156              52 :   sCheck.nPage = btreePagecount(sCheck.pBt);
   57157              52 :   sCheck.mxErr = mxErr;
   57158              52 :   sCheck.nErr = 0;
   57159              52 :   sCheck.mallocFailed = 0;
   57160              52 :   *pnErr = 0;
   57161              52 :   if( sCheck.nPage==0 ){
   57162               0 :     sqlite3BtreeLeave(p);
   57163               0 :     return 0;
   57164                 :   }
   57165              52 :   sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
   57166              52 :   if( !sCheck.anRef ){
   57167               0 :     *pnErr = 1;
   57168               0 :     sqlite3BtreeLeave(p);
   57169               0 :     return 0;
   57170                 :   }
   57171              52 :   for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
   57172              52 :   i = PENDING_BYTE_PAGE(pBt);
   57173              52 :   if( i<=sCheck.nPage ){
   57174               0 :     sCheck.anRef[i] = 1;
   57175                 :   }
   57176              52 :   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
   57177              52 :   sCheck.errMsg.useMalloc = 2;
   57178                 : 
   57179                 :   /* Check the integrity of the freelist
   57180                 :   */
   57181              52 :   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
   57182              52 :             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
   57183                 : 
   57184                 :   /* Check all the tables.
   57185                 :   */
   57186            1039 :   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
   57187             987 :     if( aRoot[i]==0 ) continue;
   57188                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   57189             987 :     if( pBt->autoVacuum && aRoot[i]>1 ){
   57190               0 :       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
   57191                 :     }
   57192                 : #endif
   57193             987 :     checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
   57194                 :   }
   57195                 : 
   57196                 :   /* Make sure every page in the file is referenced
   57197                 :   */
   57198            1085 :   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
   57199                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   57200                 :     if( sCheck.anRef[i]==0 ){
   57201                 :       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
   57202                 :     }
   57203                 : #else
   57204                 :     /* If the database supports auto-vacuum, make sure no tables contain
   57205                 :     ** references to pointer-map pages.
   57206                 :     */
   57207            1033 :     if( sCheck.anRef[i]==0 && 
   57208               0 :        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
   57209               0 :       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
   57210                 :     }
   57211            2066 :     if( sCheck.anRef[i]!=0 && 
   57212            1084 :        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
   57213               0 :       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
   57214                 :     }
   57215                 : #endif
   57216                 :   }
   57217                 : 
   57218                 :   /* Make sure this analysis did not leave any unref() pages.
   57219                 :   ** This is an internal consistency check; an integrity check
   57220                 :   ** of the integrity check.
   57221                 :   */
   57222              52 :   if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
   57223               0 :     checkAppendMsg(&sCheck, 0, 
   57224                 :       "Outstanding page count goes from %d to %d during this analysis",
   57225                 :       nRef, sqlite3PagerRefcount(pBt->pPager)
   57226                 :     );
   57227                 :   }
   57228                 : 
   57229                 :   /* Clean  up and report errors.
   57230                 :   */
   57231              52 :   sqlite3BtreeLeave(p);
   57232              52 :   sqlite3_free(sCheck.anRef);
   57233              52 :   if( sCheck.mallocFailed ){
   57234               0 :     sqlite3StrAccumReset(&sCheck.errMsg);
   57235               0 :     *pnErr = sCheck.nErr+1;
   57236               0 :     return 0;
   57237                 :   }
   57238              52 :   *pnErr = sCheck.nErr;
   57239              52 :   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
   57240              52 :   return sqlite3StrAccumFinish(&sCheck.errMsg);
   57241                 : }
   57242                 : #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
   57243                 : 
   57244                 : /*
   57245                 : ** Return the full pathname of the underlying database file.
   57246                 : **
   57247                 : ** The pager filename is invariant as long as the pager is
   57248                 : ** open so it is safe to access without the BtShared mutex.
   57249                 : */
   57250          100131 : SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
   57251          100131 :   assert( p->pBt->pPager!=0 );
   57252          100131 :   return sqlite3PagerFilename(p->pBt->pPager);
   57253                 : }
   57254                 : 
   57255                 : /*
   57256                 : ** Return the pathname of the journal file for this database. The return
   57257                 : ** value of this routine is the same regardless of whether the journal file
   57258                 : ** has been created or not.
   57259                 : **
   57260                 : ** The pager journal filename is invariant as long as the pager is
   57261                 : ** open so it is safe to access without the BtShared mutex.
   57262                 : */
   57263               0 : SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
   57264               0 :   assert( p->pBt->pPager!=0 );
   57265               0 :   return sqlite3PagerJournalname(p->pBt->pPager);
   57266                 : }
   57267                 : 
   57268                 : /*
   57269                 : ** Return non-zero if a transaction is active.
   57270                 : */
   57271          219941 : SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
   57272          219941 :   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
   57273          219941 :   return (p && (p->inTrans==TRANS_WRITE));
   57274                 : }
   57275                 : 
   57276                 : #ifndef SQLITE_OMIT_WAL
   57277                 : /*
   57278                 : ** Run a checkpoint on the Btree passed as the first argument.
   57279                 : **
   57280                 : ** Return SQLITE_LOCKED if this or any other connection has an open 
   57281                 : ** transaction on the shared-cache the argument Btree is connected to.
   57282                 : **
   57283                 : ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
   57284                 : */
   57285            5801 : SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
   57286            5801 :   int rc = SQLITE_OK;
   57287            5801 :   if( p ){
   57288            5800 :     BtShared *pBt = p->pBt;
   57289            5800 :     sqlite3BtreeEnter(p);
   57290            5800 :     if( pBt->inTransaction!=TRANS_NONE ){
   57291              20 :       rc = SQLITE_LOCKED;
   57292                 :     }else{
   57293            5780 :       rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
   57294                 :     }
   57295            5800 :     sqlite3BtreeLeave(p);
   57296                 :   }
   57297            5801 :   return rc;
   57298                 : }
   57299                 : #endif
   57300                 : 
   57301                 : /*
   57302                 : ** Return non-zero if a read (or write) transaction is active.
   57303                 : */
   57304            3890 : SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
   57305            3890 :   assert( p );
   57306            3890 :   assert( sqlite3_mutex_held(p->db->mutex) );
   57307            3890 :   return p->inTrans!=TRANS_NONE;
   57308                 : }
   57309                 : 
   57310            3660 : SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
   57311            3660 :   assert( p );
   57312            3660 :   assert( sqlite3_mutex_held(p->db->mutex) );
   57313            3660 :   return p->nBackup!=0;
   57314                 : }
   57315                 : 
   57316                 : /*
   57317                 : ** This function returns a pointer to a blob of memory associated with
   57318                 : ** a single shared-btree. The memory is used by client code for its own
   57319                 : ** purposes (for example, to store a high-level schema associated with 
   57320                 : ** the shared-btree). The btree layer manages reference counting issues.
   57321                 : **
   57322                 : ** The first time this is called on a shared-btree, nBytes bytes of memory
   57323                 : ** are allocated, zeroed, and returned to the caller. For each subsequent 
   57324                 : ** call the nBytes parameter is ignored and a pointer to the same blob
   57325                 : ** of memory returned. 
   57326                 : **
   57327                 : ** If the nBytes parameter is 0 and the blob of memory has not yet been
   57328                 : ** allocated, a null pointer is returned. If the blob has already been
   57329                 : ** allocated, it is returned as normal.
   57330                 : **
   57331                 : ** Just before the shared-btree is closed, the function passed as the 
   57332                 : ** xFree argument when the memory allocation was made is invoked on the 
   57333                 : ** blob of allocated memory. The xFree function should not call sqlite3_free()
   57334                 : ** on the memory, the btree layer does that.
   57335                 : */
   57336           22976 : SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
   57337           22976 :   BtShared *pBt = p->pBt;
   57338           22976 :   sqlite3BtreeEnter(p);
   57339           22976 :   if( !pBt->pSchema && nBytes ){
   57340            3107 :     pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
   57341            3107 :     pBt->xFreeSchema = xFree;
   57342                 :   }
   57343           22976 :   sqlite3BtreeLeave(p);
   57344           22976 :   return pBt->pSchema;
   57345                 : }
   57346                 : 
   57347                 : /*
   57348                 : ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 
   57349                 : ** btree as the argument handle holds an exclusive lock on the 
   57350                 : ** sqlite_master table. Otherwise SQLITE_OK.
   57351                 : */
   57352          219776 : SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
   57353                 :   int rc;
   57354          219776 :   assert( sqlite3_mutex_held(p->db->mutex) );
   57355          219776 :   sqlite3BtreeEnter(p);
   57356          219776 :   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
   57357          219776 :   assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
   57358          219776 :   sqlite3BtreeLeave(p);
   57359          219776 :   return rc;
   57360                 : }
   57361                 : 
   57362                 : 
   57363                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   57364                 : /*
   57365                 : ** Obtain a lock on the table whose root page is iTab.  The
   57366                 : ** lock is a write lock if isWritelock is true or a read lock
   57367                 : ** if it is false.
   57368                 : */
   57369          242705 : SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
   57370          242705 :   int rc = SQLITE_OK;
   57371          242705 :   assert( p->inTrans!=TRANS_NONE );
   57372          242705 :   if( p->sharable ){
   57373          235684 :     u8 lockType = READ_LOCK + isWriteLock;
   57374                 :     assert( READ_LOCK+1==WRITE_LOCK );
   57375          235684 :     assert( isWriteLock==0 || isWriteLock==1 );
   57376                 : 
   57377          235684 :     sqlite3BtreeEnter(p);
   57378          235684 :     rc = querySharedCacheTableLock(p, iTab, lockType);
   57379          235684 :     if( rc==SQLITE_OK ){
   57380          235386 :       rc = setSharedCacheTableLock(p, iTab, lockType);
   57381                 :     }
   57382          235684 :     sqlite3BtreeLeave(p);
   57383                 :   }
   57384          242705 :   return rc;
   57385                 : }
   57386                 : #endif
   57387                 : 
   57388                 : #ifndef SQLITE_OMIT_INCRBLOB
   57389                 : /*
   57390                 : ** Argument pCsr must be a cursor opened for writing on an 
   57391                 : ** INTKEY table currently pointing at a valid table entry. 
   57392                 : ** This function modifies the data stored as part of that entry.
   57393                 : **
   57394                 : ** Only the data content may only be modified, it is not possible to 
   57395                 : ** change the length of the data stored. If this function is called with
   57396                 : ** parameters that attempt to write past the end of the existing data,
   57397                 : ** no modifications are made and SQLITE_CORRUPT is returned.
   57398                 : */
   57399               0 : SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
   57400                 :   int rc;
   57401               0 :   assert( cursorHoldsMutex(pCsr) );
   57402               0 :   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
   57403               0 :   assert( pCsr->isIncrblobHandle );
   57404                 : 
   57405               0 :   rc = restoreCursorPosition(pCsr);
   57406               0 :   if( rc!=SQLITE_OK ){
   57407               0 :     return rc;
   57408                 :   }
   57409               0 :   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
   57410               0 :   if( pCsr->eState!=CURSOR_VALID ){
   57411               0 :     return SQLITE_ABORT;
   57412                 :   }
   57413                 : 
   57414                 :   /* Check some assumptions: 
   57415                 :   **   (a) the cursor is open for writing,
   57416                 :   **   (b) there is a read/write transaction open,
   57417                 :   **   (c) the connection holds a write-lock on the table (if required),
   57418                 :   **   (d) there are no conflicting read-locks, and
   57419                 :   **   (e) the cursor points at a valid row of an intKey table.
   57420                 :   */
   57421               0 :   if( !pCsr->wrFlag ){
   57422               0 :     return SQLITE_READONLY;
   57423                 :   }
   57424               0 :   assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
   57425                 :               && pCsr->pBt->inTransaction==TRANS_WRITE );
   57426               0 :   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
   57427               0 :   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
   57428               0 :   assert( pCsr->apPage[pCsr->iPage]->intKey );
   57429                 : 
   57430               0 :   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
   57431                 : }
   57432                 : 
   57433                 : /* 
   57434                 : ** Set a flag on this cursor to cache the locations of pages from the 
   57435                 : ** overflow list for the current row. This is used by cursors opened
   57436                 : ** for incremental blob IO only.
   57437                 : **
   57438                 : ** This function sets a flag only. The actual page location cache
   57439                 : ** (stored in BtCursor.aOverflow[]) is allocated and used by function
   57440                 : ** accessPayload() (the worker function for sqlite3BtreeData() and
   57441                 : ** sqlite3BtreePutData()).
   57442                 : */
   57443               0 : SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
   57444               0 :   assert( cursorHoldsMutex(pCur) );
   57445               0 :   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
   57446               0 :   invalidateOverflowCache(pCur);
   57447               0 :   pCur->isIncrblobHandle = 1;
   57448               0 : }
   57449                 : #endif
   57450                 : 
   57451                 : /*
   57452                 : ** Set both the "read version" (single byte at byte offset 18) and 
   57453                 : ** "write version" (single byte at byte offset 19) fields in the database
   57454                 : ** header to iVersion.
   57455                 : */
   57456             477 : SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
   57457             477 :   BtShared *pBt = pBtree->pBt;
   57458                 :   int rc;                         /* Return code */
   57459                 :  
   57460             477 :   assert( iVersion==1 || iVersion==2 );
   57461                 : 
   57462                 :   /* If setting the version fields to 1, do not automatically open the
   57463                 :   ** WAL connection, even if the version fields are currently set to 2.
   57464                 :   */
   57465             477 :   pBt->btsFlags &= ~BTS_NO_WAL;
   57466             477 :   if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
   57467                 : 
   57468             477 :   rc = sqlite3BtreeBeginTrans(pBtree, 0);
   57469             477 :   if( rc==SQLITE_OK ){
   57470             477 :     u8 *aData = pBt->pPage1->aData;
   57471             477 :     if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
   57472             477 :       rc = sqlite3BtreeBeginTrans(pBtree, 2);
   57473             477 :       if( rc==SQLITE_OK ){
   57474             477 :         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
   57475             477 :         if( rc==SQLITE_OK ){
   57476             477 :           aData[18] = (u8)iVersion;
   57477             477 :           aData[19] = (u8)iVersion;
   57478                 :         }
   57479                 :       }
   57480                 :     }
   57481                 :   }
   57482                 : 
   57483             477 :   pBt->btsFlags &= ~BTS_NO_WAL;
   57484             477 :   return rc;
   57485                 : }
   57486                 : 
   57487                 : /************** End of btree.c ***********************************************/
   57488                 : /************** Begin file backup.c ******************************************/
   57489                 : /*
   57490                 : ** 2009 January 28
   57491                 : **
   57492                 : ** The author disclaims copyright to this source code.  In place of
   57493                 : ** a legal notice, here is a blessing:
   57494                 : **
   57495                 : **    May you do good and not evil.
   57496                 : **    May you find forgiveness for yourself and forgive others.
   57497                 : **    May you share freely, never taking more than you give.
   57498                 : **
   57499                 : *************************************************************************
   57500                 : ** This file contains the implementation of the sqlite3_backup_XXX() 
   57501                 : ** API functions and the related features.
   57502                 : */
   57503                 : 
   57504                 : /* Macro to find the minimum of two numeric values.
   57505                 : */
   57506                 : #ifndef MIN
   57507                 : # define MIN(x,y) ((x)<(y)?(x):(y))
   57508                 : #endif
   57509                 : 
   57510                 : /*
   57511                 : ** Structure allocated for each backup operation.
   57512                 : */
   57513                 : struct sqlite3_backup {
   57514                 :   sqlite3* pDestDb;        /* Destination database handle */
   57515                 :   Btree *pDest;            /* Destination b-tree file */
   57516                 :   u32 iDestSchema;         /* Original schema cookie in destination */
   57517                 :   int bDestLocked;         /* True once a write-transaction is open on pDest */
   57518                 : 
   57519                 :   Pgno iNext;              /* Page number of the next source page to copy */
   57520                 :   sqlite3* pSrcDb;         /* Source database handle */
   57521                 :   Btree *pSrc;             /* Source b-tree file */
   57522                 : 
   57523                 :   int rc;                  /* Backup process error code */
   57524                 : 
   57525                 :   /* These two variables are set by every call to backup_step(). They are
   57526                 :   ** read by calls to backup_remaining() and backup_pagecount().
   57527                 :   */
   57528                 :   Pgno nRemaining;         /* Number of pages left to copy */
   57529                 :   Pgno nPagecount;         /* Total number of pages to copy */
   57530                 : 
   57531                 :   int isAttached;          /* True once backup has been registered with pager */
   57532                 :   sqlite3_backup *pNext;   /* Next backup associated with source pager */
   57533                 : };
   57534                 : 
   57535                 : /*
   57536                 : ** THREAD SAFETY NOTES:
   57537                 : **
   57538                 : **   Once it has been created using backup_init(), a single sqlite3_backup
   57539                 : **   structure may be accessed via two groups of thread-safe entry points:
   57540                 : **
   57541                 : **     * Via the sqlite3_backup_XXX() API function backup_step() and 
   57542                 : **       backup_finish(). Both these functions obtain the source database
   57543                 : **       handle mutex and the mutex associated with the source BtShared 
   57544                 : **       structure, in that order.
   57545                 : **
   57546                 : **     * Via the BackupUpdate() and BackupRestart() functions, which are
   57547                 : **       invoked by the pager layer to report various state changes in
   57548                 : **       the page cache associated with the source database. The mutex
   57549                 : **       associated with the source database BtShared structure will always 
   57550                 : **       be held when either of these functions are invoked.
   57551                 : **
   57552                 : **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
   57553                 : **   backup_pagecount() are not thread-safe functions. If they are called
   57554                 : **   while some other thread is calling backup_step() or backup_finish(),
   57555                 : **   the values returned may be invalid. There is no way for a call to
   57556                 : **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
   57557                 : **   or backup_pagecount().
   57558                 : **
   57559                 : **   Depending on the SQLite configuration, the database handles and/or
   57560                 : **   the Btree objects may have their own mutexes that require locking.
   57561                 : **   Non-sharable Btrees (in-memory databases for example), do not have
   57562                 : **   associated mutexes.
   57563                 : */
   57564                 : 
   57565                 : /*
   57566                 : ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
   57567                 : ** in connection handle pDb. If such a database cannot be found, return
   57568                 : ** a NULL pointer and write an error message to pErrorDb.
   57569                 : **
   57570                 : ** If the "temp" database is requested, it may need to be opened by this 
   57571                 : ** function. If an error occurs while doing so, return 0 and write an 
   57572                 : ** error message to pErrorDb.
   57573                 : */
   57574               0 : static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
   57575               0 :   int i = sqlite3FindDbName(pDb, zDb);
   57576                 : 
   57577               0 :   if( i==1 ){
   57578                 :     Parse *pParse;
   57579               0 :     int rc = 0;
   57580               0 :     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
   57581               0 :     if( pParse==0 ){
   57582               0 :       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
   57583               0 :       rc = SQLITE_NOMEM;
   57584                 :     }else{
   57585               0 :       pParse->db = pDb;
   57586               0 :       if( sqlite3OpenTempDatabase(pParse) ){
   57587               0 :         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
   57588               0 :         rc = SQLITE_ERROR;
   57589                 :       }
   57590               0 :       sqlite3DbFree(pErrorDb, pParse->zErrMsg);
   57591               0 :       sqlite3StackFree(pErrorDb, pParse);
   57592                 :     }
   57593               0 :     if( rc ){
   57594               0 :       return 0;
   57595                 :     }
   57596                 :   }
   57597                 : 
   57598               0 :   if( i<0 ){
   57599               0 :     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
   57600               0 :     return 0;
   57601                 :   }
   57602                 : 
   57603               0 :   return pDb->aDb[i].pBt;
   57604                 : }
   57605                 : 
   57606                 : /*
   57607                 : ** Attempt to set the page size of the destination to match the page size
   57608                 : ** of the source.
   57609                 : */
   57610               0 : static int setDestPgsz(sqlite3_backup *p){
   57611                 :   int rc;
   57612               0 :   rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
   57613               0 :   return rc;
   57614                 : }
   57615                 : 
   57616                 : /*
   57617                 : ** Create an sqlite3_backup process to copy the contents of zSrcDb from
   57618                 : ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
   57619                 : ** a pointer to the new sqlite3_backup object.
   57620                 : **
   57621                 : ** If an error occurs, NULL is returned and an error code and error message
   57622                 : ** stored in database handle pDestDb.
   57623                 : */
   57624               0 : SQLITE_API sqlite3_backup *sqlite3_backup_init(
   57625                 :   sqlite3* pDestDb,                     /* Database to write to */
   57626                 :   const char *zDestDb,                  /* Name of database within pDestDb */
   57627                 :   sqlite3* pSrcDb,                      /* Database connection to read from */
   57628                 :   const char *zSrcDb                    /* Name of database within pSrcDb */
   57629                 : ){
   57630                 :   sqlite3_backup *p;                    /* Value to return */
   57631                 : 
   57632                 :   /* Lock the source database handle. The destination database
   57633                 :   ** handle is not locked in this routine, but it is locked in
   57634                 :   ** sqlite3_backup_step(). The user is required to ensure that no
   57635                 :   ** other thread accesses the destination handle for the duration
   57636                 :   ** of the backup operation.  Any attempt to use the destination
   57637                 :   ** database connection while a backup is in progress may cause
   57638                 :   ** a malfunction or a deadlock.
   57639                 :   */
   57640               0 :   sqlite3_mutex_enter(pSrcDb->mutex);
   57641               0 :   sqlite3_mutex_enter(pDestDb->mutex);
   57642                 : 
   57643               0 :   if( pSrcDb==pDestDb ){
   57644               0 :     sqlite3Error(
   57645                 :         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
   57646                 :     );
   57647               0 :     p = 0;
   57648                 :   }else {
   57649                 :     /* Allocate space for a new sqlite3_backup object...
   57650                 :     ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
   57651                 :     ** call to sqlite3_backup_init() and is destroyed by a call to
   57652                 :     ** sqlite3_backup_finish(). */
   57653               0 :     p = (sqlite3_backup *)sqlite3_malloc(sizeof(sqlite3_backup));
   57654               0 :     if( !p ){
   57655               0 :       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
   57656                 :     }
   57657                 :   }
   57658                 : 
   57659                 :   /* If the allocation succeeded, populate the new object. */
   57660               0 :   if( p ){
   57661               0 :     memset(p, 0, sizeof(sqlite3_backup));
   57662               0 :     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
   57663               0 :     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
   57664               0 :     p->pDestDb = pDestDb;
   57665               0 :     p->pSrcDb = pSrcDb;
   57666               0 :     p->iNext = 1;
   57667               0 :     p->isAttached = 0;
   57668                 : 
   57669               0 :     if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
   57670                 :       /* One (or both) of the named databases did not exist or an OOM
   57671                 :       ** error was hit.  The error has already been written into the
   57672                 :       ** pDestDb handle.  All that is left to do here is free the
   57673                 :       ** sqlite3_backup structure.
   57674                 :       */
   57675               0 :       sqlite3_free(p);
   57676               0 :       p = 0;
   57677                 :     }
   57678                 :   }
   57679               0 :   if( p ){
   57680               0 :     p->pSrc->nBackup++;
   57681                 :   }
   57682                 : 
   57683               0 :   sqlite3_mutex_leave(pDestDb->mutex);
   57684               0 :   sqlite3_mutex_leave(pSrcDb->mutex);
   57685               0 :   return p;
   57686                 : }
   57687                 : 
   57688                 : /*
   57689                 : ** Argument rc is an SQLite error code. Return true if this error is 
   57690                 : ** considered fatal if encountered during a backup operation. All errors
   57691                 : ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
   57692                 : */
   57693              62 : static int isFatalError(int rc){
   57694              62 :   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
   57695                 : }
   57696                 : 
   57697                 : /*
   57698                 : ** Parameter zSrcData points to a buffer containing the data for 
   57699                 : ** page iSrcPg from the source database. Copy this data into the 
   57700                 : ** destination database.
   57701                 : */
   57702              56 : static int backupOnePage(sqlite3_backup *p, Pgno iSrcPg, const u8 *zSrcData){
   57703              56 :   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
   57704              56 :   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
   57705              56 :   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
   57706              56 :   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
   57707              56 :   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
   57708                 : #ifdef SQLITE_HAS_CODEC
   57709                 :   int nSrcReserve = sqlite3BtreeGetReserve(p->pSrc);
   57710                 :   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
   57711                 : #endif
   57712                 : 
   57713              56 :   int rc = SQLITE_OK;
   57714                 :   i64 iOff;
   57715                 : 
   57716              56 :   assert( p->bDestLocked );
   57717              56 :   assert( !isFatalError(p->rc) );
   57718              56 :   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
   57719              56 :   assert( zSrcData );
   57720                 : 
   57721                 :   /* Catch the case where the destination is an in-memory database and the
   57722                 :   ** page sizes of the source and destination differ. 
   57723                 :   */
   57724              56 :   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
   57725               0 :     rc = SQLITE_READONLY;
   57726                 :   }
   57727                 : 
   57728                 : #ifdef SQLITE_HAS_CODEC
   57729                 :   /* Backup is not possible if the page size of the destination is changing
   57730                 :   ** and a codec is in use.
   57731                 :   */
   57732                 :   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
   57733                 :     rc = SQLITE_READONLY;
   57734                 :   }
   57735                 : 
   57736                 :   /* Backup is not possible if the number of bytes of reserve space differ
   57737                 :   ** between source and destination.  If there is a difference, try to
   57738                 :   ** fix the destination to agree with the source.  If that is not possible,
   57739                 :   ** then the backup cannot proceed.
   57740                 :   */
   57741                 :   if( nSrcReserve!=nDestReserve ){
   57742                 :     u32 newPgsz = nSrcPgsz;
   57743                 :     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
   57744                 :     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
   57745                 :   }
   57746                 : #endif
   57747                 : 
   57748                 :   /* This loop runs once for each destination page spanned by the source 
   57749                 :   ** page. For each iteration, variable iOff is set to the byte offset
   57750                 :   ** of the destination page.
   57751                 :   */
   57752             360 :   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
   57753             304 :     DbPage *pDestPg = 0;
   57754             304 :     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
   57755             304 :     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
   57756             304 :     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
   57757             304 :      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
   57758                 :     ){
   57759             304 :       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
   57760             304 :       u8 *zDestData = sqlite3PagerGetData(pDestPg);
   57761             304 :       u8 *zOut = &zDestData[iOff%nDestPgsz];
   57762                 : 
   57763                 :       /* Copy the data from the source page into the destination page.
   57764                 :       ** Then clear the Btree layer MemPage.isInit flag. Both this module
   57765                 :       ** and the pager code use this trick (clearing the first byte
   57766                 :       ** of the page 'extra' space to invalidate the Btree layers
   57767                 :       ** cached parse of the page). MemPage.isInit is marked 
   57768                 :       ** "MUST BE FIRST" for this purpose.
   57769                 :       */
   57770             304 :       memcpy(zOut, zIn, nCopy);
   57771             304 :       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
   57772                 :     }
   57773             304 :     sqlite3PagerUnref(pDestPg);
   57774                 :   }
   57775                 : 
   57776              56 :   return rc;
   57777                 : }
   57778                 : 
   57779                 : /*
   57780                 : ** If pFile is currently larger than iSize bytes, then truncate it to
   57781                 : ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
   57782                 : ** this function is a no-op.
   57783                 : **
   57784                 : ** Return SQLITE_OK if everything is successful, or an SQLite error 
   57785                 : ** code if an error occurs.
   57786                 : */
   57787               0 : static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
   57788                 :   i64 iCurrent;
   57789               0 :   int rc = sqlite3OsFileSize(pFile, &iCurrent);
   57790               0 :   if( rc==SQLITE_OK && iCurrent>iSize ){
   57791               0 :     rc = sqlite3OsTruncate(pFile, iSize);
   57792                 :   }
   57793               0 :   return rc;
   57794                 : }
   57795                 : 
   57796                 : /*
   57797                 : ** Register this backup object with the associated source pager for
   57798                 : ** callbacks when pages are changed or the cache invalidated.
   57799                 : */
   57800               0 : static void attachBackupObject(sqlite3_backup *p){
   57801                 :   sqlite3_backup **pp;
   57802               0 :   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
   57803               0 :   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
   57804               0 :   p->pNext = *pp;
   57805               0 :   *pp = p;
   57806               0 :   p->isAttached = 1;
   57807               0 : }
   57808                 : 
   57809                 : /*
   57810                 : ** Copy nPage pages from the source b-tree to the destination.
   57811                 : */
   57812               6 : SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
   57813                 :   int rc;
   57814                 :   int destMode;       /* Destination journal mode */
   57815               6 :   int pgszSrc = 0;    /* Source page size */
   57816               6 :   int pgszDest = 0;   /* Destination page size */
   57817                 : 
   57818               6 :   sqlite3_mutex_enter(p->pSrcDb->mutex);
   57819               6 :   sqlite3BtreeEnter(p->pSrc);
   57820               6 :   if( p->pDestDb ){
   57821               0 :     sqlite3_mutex_enter(p->pDestDb->mutex);
   57822                 :   }
   57823                 : 
   57824               6 :   rc = p->rc;
   57825               6 :   if( !isFatalError(rc) ){
   57826               6 :     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
   57827               6 :     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
   57828                 :     int ii;                            /* Iterator variable */
   57829               6 :     int nSrcPage = -1;                 /* Size of source db in pages */
   57830               6 :     int bCloseTrans = 0;               /* True if src db requires unlocking */
   57831                 : 
   57832                 :     /* If the source pager is currently in a write-transaction, return
   57833                 :     ** SQLITE_BUSY immediately.
   57834                 :     */
   57835               6 :     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
   57836               0 :       rc = SQLITE_BUSY;
   57837                 :     }else{
   57838               6 :       rc = SQLITE_OK;
   57839                 :     }
   57840                 : 
   57841                 :     /* Lock the destination database, if it is not locked already. */
   57842               6 :     if( SQLITE_OK==rc && p->bDestLocked==0
   57843               6 :      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
   57844                 :     ){
   57845               6 :       p->bDestLocked = 1;
   57846               6 :       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
   57847                 :     }
   57848                 : 
   57849                 :     /* If there is no open read-transaction on the source database, open
   57850                 :     ** one now. If a transaction is opened here, then it will be closed
   57851                 :     ** before this function exits.
   57852                 :     */
   57853               6 :     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
   57854               0 :       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
   57855               0 :       bCloseTrans = 1;
   57856                 :     }
   57857                 : 
   57858                 :     /* Do not allow backup if the destination database is in WAL mode
   57859                 :     ** and the page sizes are different between source and destination */
   57860               6 :     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
   57861               6 :     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
   57862               6 :     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
   57863               6 :     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
   57864               0 :       rc = SQLITE_READONLY;
   57865                 :     }
   57866                 :   
   57867                 :     /* Now that there is a read-lock on the source database, query the
   57868                 :     ** source pager for the number of pages in the database.
   57869                 :     */
   57870               6 :     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
   57871               6 :     assert( nSrcPage>=0 );
   57872              62 :     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
   57873              56 :       const Pgno iSrcPg = p->iNext;                 /* Source page number */
   57874              56 :       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
   57875                 :         DbPage *pSrcPg;                             /* Source page object */
   57876              56 :         rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
   57877              56 :         if( rc==SQLITE_OK ){
   57878              56 :           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg));
   57879              56 :           sqlite3PagerUnref(pSrcPg);
   57880                 :         }
   57881                 :       }
   57882              56 :       p->iNext++;
   57883                 :     }
   57884               6 :     if( rc==SQLITE_OK ){
   57885               6 :       p->nPagecount = nSrcPage;
   57886               6 :       p->nRemaining = nSrcPage+1-p->iNext;
   57887               6 :       if( p->iNext>(Pgno)nSrcPage ){
   57888               6 :         rc = SQLITE_DONE;
   57889               0 :       }else if( !p->isAttached ){
   57890               0 :         attachBackupObject(p);
   57891                 :       }
   57892                 :     }
   57893                 :   
   57894                 :     /* Update the schema version field in the destination database. This
   57895                 :     ** is to make sure that the schema-version really does change in
   57896                 :     ** the case where the source and destination databases have the
   57897                 :     ** same schema version.
   57898                 :     */
   57899               6 :     if( rc==SQLITE_DONE ){
   57900               6 :       rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
   57901               6 :       if( rc==SQLITE_OK ){
   57902               6 :         if( p->pDestDb ){
   57903               0 :           sqlite3ResetInternalSchema(p->pDestDb, -1);
   57904                 :         }
   57905               6 :         if( destMode==PAGER_JOURNALMODE_WAL ){
   57906               1 :           rc = sqlite3BtreeSetVersion(p->pDest, 2);
   57907                 :         }
   57908                 :       }
   57909               6 :       if( rc==SQLITE_OK ){
   57910                 :         int nDestTruncate;
   57911                 :         /* Set nDestTruncate to the final number of pages in the destination
   57912                 :         ** database. The complication here is that the destination page
   57913                 :         ** size may be different to the source page size. 
   57914                 :         **
   57915                 :         ** If the source page size is smaller than the destination page size, 
   57916                 :         ** round up. In this case the call to sqlite3OsTruncate() below will
   57917                 :         ** fix the size of the file. However it is important to call
   57918                 :         ** sqlite3PagerTruncateImage() here so that any pages in the 
   57919                 :         ** destination file that lie beyond the nDestTruncate page mark are
   57920                 :         ** journalled by PagerCommitPhaseOne() before they are destroyed
   57921                 :         ** by the file truncation.
   57922                 :         */
   57923               6 :         assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
   57924               6 :         assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
   57925               6 :         if( pgszSrc<pgszDest ){
   57926               0 :           int ratio = pgszDest/pgszSrc;
   57927               0 :           nDestTruncate = (nSrcPage+ratio-1)/ratio;
   57928               0 :           if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
   57929               0 :             nDestTruncate--;
   57930                 :           }
   57931                 :         }else{
   57932               6 :           nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
   57933                 :         }
   57934               6 :         sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
   57935                 : 
   57936               6 :         if( pgszSrc<pgszDest ){
   57937                 :           /* If the source page-size is smaller than the destination page-size,
   57938                 :           ** two extra things may need to happen:
   57939                 :           **
   57940                 :           **   * The destination may need to be truncated, and
   57941                 :           **
   57942                 :           **   * Data stored on the pages immediately following the 
   57943                 :           **     pending-byte page in the source database may need to be
   57944                 :           **     copied into the destination database.
   57945                 :           */
   57946               0 :           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
   57947               0 :           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
   57948                 :           i64 iOff;
   57949                 :           i64 iEnd;
   57950                 : 
   57951               0 :           assert( pFile );
   57952               0 :           assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
   57953                 :                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
   57954                 :              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
   57955                 :           ));
   57956                 : 
   57957                 :           /* This call ensures that all data required to recreate the original
   57958                 :           ** database has been stored in the journal for pDestPager and the
   57959                 :           ** journal synced to disk. So at this point we may safely modify
   57960                 :           ** the database file in any way, knowing that if a power failure
   57961                 :           ** occurs, the original database will be reconstructed from the 
   57962                 :           ** journal file.  */
   57963               0 :           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
   57964                 : 
   57965                 :           /* Write the extra pages and truncate the database file as required */
   57966               0 :           iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
   57967               0 :           for(
   57968               0 :             iOff=PENDING_BYTE+pgszSrc; 
   57969               0 :             rc==SQLITE_OK && iOff<iEnd; 
   57970               0 :             iOff+=pgszSrc
   57971                 :           ){
   57972               0 :             PgHdr *pSrcPg = 0;
   57973               0 :             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
   57974               0 :             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
   57975               0 :             if( rc==SQLITE_OK ){
   57976               0 :               u8 *zData = sqlite3PagerGetData(pSrcPg);
   57977               0 :               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
   57978                 :             }
   57979               0 :             sqlite3PagerUnref(pSrcPg);
   57980                 :           }
   57981               0 :           if( rc==SQLITE_OK ){
   57982               0 :             rc = backupTruncateFile(pFile, iSize);
   57983                 :           }
   57984                 : 
   57985                 :           /* Sync the database file to disk. */
   57986               0 :           if( rc==SQLITE_OK ){
   57987               0 :             rc = sqlite3PagerSync(pDestPager);
   57988                 :           }
   57989                 :         }else{
   57990               6 :           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
   57991                 :         }
   57992                 :     
   57993                 :         /* Finish committing the transaction to the destination database. */
   57994               6 :         if( SQLITE_OK==rc
   57995               6 :          && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
   57996                 :         ){
   57997               6 :           rc = SQLITE_DONE;
   57998                 :         }
   57999                 :       }
   58000                 :     }
   58001                 :   
   58002                 :     /* If bCloseTrans is true, then this function opened a read transaction
   58003                 :     ** on the source database. Close the read transaction here. There is
   58004                 :     ** no need to check the return values of the btree methods here, as
   58005                 :     ** "committing" a read-only transaction cannot fail.
   58006                 :     */
   58007               6 :     if( bCloseTrans ){
   58008                 :       TESTONLY( int rc2 );
   58009               0 :       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
   58010               0 :       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
   58011               0 :       assert( rc2==SQLITE_OK );
   58012                 :     }
   58013                 :   
   58014               6 :     if( rc==SQLITE_IOERR_NOMEM ){
   58015               0 :       rc = SQLITE_NOMEM;
   58016                 :     }
   58017               6 :     p->rc = rc;
   58018                 :   }
   58019               6 :   if( p->pDestDb ){
   58020               0 :     sqlite3_mutex_leave(p->pDestDb->mutex);
   58021                 :   }
   58022               6 :   sqlite3BtreeLeave(p->pSrc);
   58023               6 :   sqlite3_mutex_leave(p->pSrcDb->mutex);
   58024               6 :   return rc;
   58025                 : }
   58026                 : 
   58027                 : /*
   58028                 : ** Release all resources associated with an sqlite3_backup* handle.
   58029                 : */
   58030               6 : SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
   58031                 :   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
   58032                 :   MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
   58033                 :   int rc;                              /* Value to return */
   58034                 : 
   58035                 :   /* Enter the mutexes */
   58036               6 :   if( p==0 ) return SQLITE_OK;
   58037               6 :   sqlite3_mutex_enter(p->pSrcDb->mutex);
   58038               6 :   sqlite3BtreeEnter(p->pSrc);
   58039               6 :   MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
   58040               6 :   if( p->pDestDb ){
   58041               0 :     sqlite3_mutex_enter(p->pDestDb->mutex);
   58042                 :   }
   58043                 : 
   58044                 :   /* Detach this backup from the source pager. */
   58045               6 :   if( p->pDestDb ){
   58046               0 :     p->pSrc->nBackup--;
   58047                 :   }
   58048               6 :   if( p->isAttached ){
   58049               0 :     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
   58050               0 :     while( *pp!=p ){
   58051               0 :       pp = &(*pp)->pNext;
   58052                 :     }
   58053               0 :     *pp = p->pNext;
   58054                 :   }
   58055                 : 
   58056                 :   /* If a transaction is still open on the Btree, roll it back. */
   58057               6 :   sqlite3BtreeRollback(p->pDest);
   58058                 : 
   58059                 :   /* Set the error code of the destination database handle. */
   58060               6 :   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
   58061               6 :   sqlite3Error(p->pDestDb, rc, 0);
   58062                 : 
   58063                 :   /* Exit the mutexes and free the backup context structure. */
   58064               6 :   if( p->pDestDb ){
   58065               0 :     sqlite3_mutex_leave(p->pDestDb->mutex);
   58066                 :   }
   58067               6 :   sqlite3BtreeLeave(p->pSrc);
   58068               6 :   if( p->pDestDb ){
   58069                 :     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
   58070                 :     ** call to sqlite3_backup_init() and is destroyed by a call to
   58071                 :     ** sqlite3_backup_finish(). */
   58072               0 :     sqlite3_free(p);
   58073                 :   }
   58074               6 :   sqlite3_mutex_leave(mutex);
   58075               6 :   return rc;
   58076                 : }
   58077                 : 
   58078                 : /*
   58079                 : ** Return the number of pages still to be backed up as of the most recent
   58080                 : ** call to sqlite3_backup_step().
   58081                 : */
   58082               0 : SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
   58083               0 :   return p->nRemaining;
   58084                 : }
   58085                 : 
   58086                 : /*
   58087                 : ** Return the total number of pages in the source database as of the most 
   58088                 : ** recent call to sqlite3_backup_step().
   58089                 : */
   58090               0 : SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
   58091               0 :   return p->nPagecount;
   58092                 : }
   58093                 : 
   58094                 : /*
   58095                 : ** This function is called after the contents of page iPage of the
   58096                 : ** source database have been modified. If page iPage has already been 
   58097                 : ** copied into the destination database, then the data written to the
   58098                 : ** destination is now invalidated. The destination copy of iPage needs
   58099                 : ** to be updated with the new data before the backup operation is
   58100                 : ** complete.
   58101                 : **
   58102                 : ** It is assumed that the mutex associated with the BtShared object
   58103                 : ** corresponding to the source database is held when this function is
   58104                 : ** called.
   58105                 : */
   58106           26854 : SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
   58107                 :   sqlite3_backup *p;                   /* Iterator variable */
   58108           26854 :   for(p=pBackup; p; p=p->pNext){
   58109               0 :     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
   58110               0 :     if( !isFatalError(p->rc) && iPage<p->iNext ){
   58111                 :       /* The backup process p has already copied page iPage. But now it
   58112                 :       ** has been modified by a transaction on the source pager. Copy
   58113                 :       ** the new data into the backup.
   58114                 :       */
   58115                 :       int rc;
   58116               0 :       assert( p->pDestDb );
   58117               0 :       sqlite3_mutex_enter(p->pDestDb->mutex);
   58118               0 :       rc = backupOnePage(p, iPage, aData);
   58119               0 :       sqlite3_mutex_leave(p->pDestDb->mutex);
   58120               0 :       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
   58121               0 :       if( rc!=SQLITE_OK ){
   58122               0 :         p->rc = rc;
   58123                 :       }
   58124                 :     }
   58125                 :   }
   58126           26854 : }
   58127                 : 
   58128                 : /*
   58129                 : ** Restart the backup process. This is called when the pager layer
   58130                 : ** detects that the database has been modified by an external database
   58131                 : ** connection. In this case there is no way of knowing which of the
   58132                 : ** pages that have been copied into the destination database are still 
   58133                 : ** valid and which are not, so the entire process needs to be restarted.
   58134                 : **
   58135                 : ** It is assumed that the mutex associated with the BtShared object
   58136                 : ** corresponding to the source database is held when this function is
   58137                 : ** called.
   58138                 : */
   58139           42887 : SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
   58140                 :   sqlite3_backup *p;                   /* Iterator variable */
   58141           42887 :   for(p=pBackup; p; p=p->pNext){
   58142               0 :     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
   58143               0 :     p->iNext = 1;
   58144                 :   }
   58145           42887 : }
   58146                 : 
   58147                 : #ifndef SQLITE_OMIT_VACUUM
   58148                 : /*
   58149                 : ** Copy the complete content of pBtFrom into pBtTo.  A transaction
   58150                 : ** must be active for both files.
   58151                 : **
   58152                 : ** The size of file pTo may be reduced by this operation. If anything 
   58153                 : ** goes wrong, the transaction on pTo is rolled back. If successful, the 
   58154                 : ** transaction is committed before returning.
   58155                 : */
   58156               6 : SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
   58157                 :   int rc;
   58158                 :   sqlite3_file *pFd;              /* File descriptor for database pTo */
   58159                 :   sqlite3_backup b;
   58160               6 :   sqlite3BtreeEnter(pTo);
   58161               6 :   sqlite3BtreeEnter(pFrom);
   58162                 : 
   58163               6 :   assert( sqlite3BtreeIsInTrans(pTo) );
   58164               6 :   pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
   58165               6 :   if( pFd->pMethods ){
   58166               6 :     i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
   58167               6 :     rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
   58168               6 :     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
   58169               6 :     if( rc ) goto copy_finished;
   58170                 :   }
   58171                 : 
   58172                 :   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
   58173                 :   ** to 0. This is used by the implementations of sqlite3_backup_step()
   58174                 :   ** and sqlite3_backup_finish() to detect that they are being called
   58175                 :   ** from this function, not directly by the user.
   58176                 :   */
   58177               6 :   memset(&b, 0, sizeof(b));
   58178               6 :   b.pSrcDb = pFrom->db;
   58179               6 :   b.pSrc = pFrom;
   58180               6 :   b.pDest = pTo;
   58181               6 :   b.iNext = 1;
   58182                 : 
   58183                 :   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
   58184                 :   ** file. By passing this as the number of pages to copy to
   58185                 :   ** sqlite3_backup_step(), we can guarantee that the copy finishes 
   58186                 :   ** within a single call (unless an error occurs). The assert() statement
   58187                 :   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE 
   58188                 :   ** or an error code.
   58189                 :   */
   58190               6 :   sqlite3_backup_step(&b, 0x7FFFFFFF);
   58191               6 :   assert( b.rc!=SQLITE_OK );
   58192               6 :   rc = sqlite3_backup_finish(&b);
   58193               6 :   if( rc==SQLITE_OK ){
   58194               6 :     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
   58195                 :   }else{
   58196               0 :     sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
   58197                 :   }
   58198                 : 
   58199               6 :   assert( sqlite3BtreeIsInTrans(pTo)==0 );
   58200                 : copy_finished:
   58201               6 :   sqlite3BtreeLeave(pFrom);
   58202               6 :   sqlite3BtreeLeave(pTo);
   58203               6 :   return rc;
   58204                 : }
   58205                 : #endif /* SQLITE_OMIT_VACUUM */
   58206                 : 
   58207                 : /************** End of backup.c **********************************************/
   58208                 : /************** Begin file vdbemem.c *****************************************/
   58209                 : /*
   58210                 : ** 2004 May 26
   58211                 : **
   58212                 : ** The author disclaims copyright to this source code.  In place of
   58213                 : ** a legal notice, here is a blessing:
   58214                 : **
   58215                 : **    May you do good and not evil.
   58216                 : **    May you find forgiveness for yourself and forgive others.
   58217                 : **    May you share freely, never taking more than you give.
   58218                 : **
   58219                 : *************************************************************************
   58220                 : **
   58221                 : ** This file contains code use to manipulate "Mem" structure.  A "Mem"
   58222                 : ** stores a single value in the VDBE.  Mem is an opaque structure visible
   58223                 : ** only within the VDBE.  Interface routines refer to a Mem using the
   58224                 : ** name sqlite_value
   58225                 : */
   58226                 : 
   58227                 : /*
   58228                 : ** If pMem is an object with a valid string representation, this routine
   58229                 : ** ensures the internal encoding for the string representation is
   58230                 : ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
   58231                 : **
   58232                 : ** If pMem is not a string object, or the encoding of the string
   58233                 : ** representation is already stored using the requested encoding, then this
   58234                 : ** routine is a no-op.
   58235                 : **
   58236                 : ** SQLITE_OK is returned if the conversion is successful (or not required).
   58237                 : ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
   58238                 : ** between formats.
   58239                 : */
   58240         1407721 : SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
   58241                 :   int rc;
   58242         1407721 :   assert( (pMem->flags&MEM_RowSet)==0 );
   58243         1407721 :   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
   58244                 :            || desiredEnc==SQLITE_UTF16BE );
   58245         1407721 :   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
   58246         1168005 :     return SQLITE_OK;
   58247                 :   }
   58248          239716 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58249                 : #ifdef SQLITE_OMIT_UTF16
   58250                 :   return SQLITE_ERROR;
   58251                 : #else
   58252                 : 
   58253                 :   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
   58254                 :   ** then the encoding of the value may not have changed.
   58255                 :   */
   58256          239716 :   rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
   58257          239716 :   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
   58258          239716 :   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
   58259          239716 :   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
   58260          239716 :   return rc;
   58261                 : #endif
   58262                 : }
   58263                 : 
   58264                 : /*
   58265                 : ** Make sure pMem->z points to a writable allocation of at least 
   58266                 : ** n bytes.
   58267                 : **
   58268                 : ** If the memory cell currently contains string or blob data
   58269                 : ** and the third argument passed to this function is true, the 
   58270                 : ** current content of the cell is preserved. Otherwise, it may
   58271                 : ** be discarded.  
   58272                 : **
   58273                 : ** This function sets the MEM_Dyn flag and clears any xDel callback.
   58274                 : ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
   58275                 : ** not set, Mem.n is zeroed.
   58276                 : */
   58277         2423299 : SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
   58278         2423299 :   assert( 1 >=
   58279                 :     ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
   58280                 :     (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
   58281                 :     ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
   58282                 :     ((pMem->flags&MEM_Static) ? 1 : 0)
   58283                 :   );
   58284         2423299 :   assert( (pMem->flags&MEM_RowSet)==0 );
   58285                 : 
   58286         2423299 :   if( n<32 ) n = 32;
   58287         2423299 :   if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
   58288         1710475 :     if( preserve && pMem->z==pMem->zMalloc ){
   58289               0 :       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
   58290               0 :       preserve = 0;
   58291                 :     }else{
   58292         1710475 :       sqlite3DbFree(pMem->db, pMem->zMalloc);
   58293         1710475 :       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
   58294                 :     }
   58295                 :   }
   58296                 : 
   58297         2423291 :   if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
   58298          808196 :     memcpy(pMem->zMalloc, pMem->z, pMem->n);
   58299                 :   }
   58300         2423291 :   if( pMem->flags&MEM_Dyn && pMem->xDel ){
   58301               0 :     pMem->xDel((void *)(pMem->z));
   58302                 :   }
   58303                 : 
   58304         2423284 :   pMem->z = pMem->zMalloc;
   58305         2423284 :   if( pMem->z==0 ){
   58306               0 :     pMem->flags = MEM_Null;
   58307                 :   }else{
   58308         2423285 :     pMem->flags &= ~(MEM_Ephem|MEM_Static);
   58309                 :   }
   58310         2423284 :   pMem->xDel = 0;
   58311         2423284 :   return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
   58312                 : }
   58313                 : 
   58314                 : /*
   58315                 : ** Make the given Mem object MEM_Dyn.  In other words, make it so
   58316                 : ** that any TEXT or BLOB content is stored in memory obtained from
   58317                 : ** malloc().  In this way, we know that the memory is safe to be
   58318                 : ** overwritten or altered.
   58319                 : **
   58320                 : ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
   58321                 : */
   58322         1899059 : SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
   58323                 :   int f;
   58324         1899059 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58325         1899053 :   assert( (pMem->flags&MEM_RowSet)==0 );
   58326         1899053 :   ExpandBlob(pMem);
   58327         1899058 :   f = pMem->flags;
   58328         1899058 :   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
   58329          807316 :     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
   58330               0 :       return SQLITE_NOMEM;
   58331                 :     }
   58332          807306 :     pMem->z[pMem->n] = 0;
   58333          807306 :     pMem->z[pMem->n+1] = 0;
   58334          807306 :     pMem->flags |= MEM_Term;
   58335                 : #ifdef SQLITE_DEBUG
   58336          807306 :     pMem->pScopyFrom = 0;
   58337                 : #endif
   58338                 :   }
   58339                 : 
   58340         1899048 :   return SQLITE_OK;
   58341                 : }
   58342                 : 
   58343                 : /*
   58344                 : ** If the given Mem* has a zero-filled tail, turn it into an ordinary
   58345                 : ** blob stored in dynamically allocated space.
   58346                 : */
   58347                 : #ifndef SQLITE_OMIT_INCRBLOB
   58348            5341 : SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
   58349            5341 :   if( pMem->flags & MEM_Zero ){
   58350                 :     int nByte;
   58351               0 :     assert( pMem->flags&MEM_Blob );
   58352               0 :     assert( (pMem->flags&MEM_RowSet)==0 );
   58353               0 :     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58354                 : 
   58355                 :     /* Set nByte to the number of bytes required to store the expanded blob. */
   58356               0 :     nByte = pMem->n + pMem->u.nZero;
   58357               0 :     if( nByte<=0 ){
   58358               0 :       nByte = 1;
   58359                 :     }
   58360               0 :     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
   58361               0 :       return SQLITE_NOMEM;
   58362                 :     }
   58363                 : 
   58364               0 :     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
   58365               0 :     pMem->n += pMem->u.nZero;
   58366               0 :     pMem->flags &= ~(MEM_Zero|MEM_Term);
   58367                 :   }
   58368            5341 :   return SQLITE_OK;
   58369                 : }
   58370                 : #endif
   58371                 : 
   58372                 : 
   58373                 : /*
   58374                 : ** Make sure the given Mem is \u0000 terminated.
   58375                 : */
   58376         2060090 : SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
   58377         2060090 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58378         2060050 :   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
   58379         2058600 :     return SQLITE_OK;   /* Nothing to do */
   58380                 :   }
   58381            1450 :   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
   58382               0 :     return SQLITE_NOMEM;
   58383                 :   }
   58384            1450 :   pMem->z[pMem->n] = 0;
   58385            1450 :   pMem->z[pMem->n+1] = 0;
   58386            1450 :   pMem->flags |= MEM_Term;
   58387            1450 :   return SQLITE_OK;
   58388                 : }
   58389                 : 
   58390                 : /*
   58391                 : ** Add MEM_Str to the set of representations for the given Mem.  Numbers
   58392                 : ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
   58393                 : ** is a no-op.
   58394                 : **
   58395                 : ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
   58396                 : **
   58397                 : ** A MEM_Null value will never be passed to this function. This function is
   58398                 : ** used for converting values to text for returning to the user (i.e. via
   58399                 : ** sqlite3_value_text()), or for ensuring that values to be used as btree
   58400                 : ** keys are strings. In the former case a NULL pointer is returned the
   58401                 : ** user and the later is an internal programming error.
   58402                 : */
   58403           60710 : SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
   58404           60710 :   int rc = SQLITE_OK;
   58405           60710 :   int fg = pMem->flags;
   58406           60710 :   const int nByte = 32;
   58407                 : 
   58408           60710 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58409           60711 :   assert( !(fg&MEM_Zero) );
   58410           60711 :   assert( !(fg&(MEM_Str|MEM_Blob)) );
   58411           60711 :   assert( fg&(MEM_Int|MEM_Real) );
   58412           60711 :   assert( (pMem->flags&MEM_RowSet)==0 );
   58413           60711 :   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   58414                 : 
   58415                 : 
   58416           60711 :   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
   58417               0 :     return SQLITE_NOMEM;
   58418                 :   }
   58419                 : 
   58420                 :   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
   58421                 :   ** string representation of the value. Then, if the required encoding
   58422                 :   ** is UTF-16le or UTF-16be do a translation.
   58423                 :   ** 
   58424                 :   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
   58425                 :   */
   58426           60711 :   if( fg & MEM_Int ){
   58427           60701 :     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
   58428                 :   }else{
   58429              10 :     assert( fg & MEM_Real );
   58430              10 :     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
   58431                 :   }
   58432           60711 :   pMem->n = sqlite3Strlen30(pMem->z);
   58433           60711 :   pMem->enc = SQLITE_UTF8;
   58434           60711 :   pMem->flags |= MEM_Str|MEM_Term;
   58435           60711 :   sqlite3VdbeChangeEncoding(pMem, enc);
   58436           60711 :   return rc;
   58437                 : }
   58438                 : 
   58439                 : /*
   58440                 : ** Memory cell pMem contains the context of an aggregate function.
   58441                 : ** This routine calls the finalize method for that function.  The
   58442                 : ** result of the aggregate is stored back into pMem.
   58443                 : **
   58444                 : ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
   58445                 : ** otherwise.
   58446                 : */
   58447           17261 : SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
   58448           17261 :   int rc = SQLITE_OK;
   58449           17261 :   if( ALWAYS(pFunc && pFunc->xFinalize) ){
   58450                 :     sqlite3_context ctx;
   58451           17261 :     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
   58452           17261 :     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58453           17261 :     memset(&ctx, 0, sizeof(ctx));
   58454           17261 :     ctx.s.flags = MEM_Null;
   58455           17261 :     ctx.s.db = pMem->db;
   58456           17261 :     ctx.pMem = pMem;
   58457           17261 :     ctx.pFunc = pFunc;
   58458           17261 :     pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
   58459           17261 :     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
   58460           17261 :     sqlite3DbFree(pMem->db, pMem->zMalloc);
   58461           17261 :     memcpy(pMem, &ctx.s, sizeof(ctx.s));
   58462           17261 :     rc = ctx.isError;
   58463                 :   }
   58464           17261 :   return rc;
   58465                 : }
   58466                 : 
   58467                 : /*
   58468                 : ** If the memory cell contains a string value that must be freed by
   58469                 : ** invoking an external callback, free it now. Calling this function
   58470                 : ** does not free any Mem.zMalloc buffer.
   58471                 : */
   58472          458635 : SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
   58473          458635 :   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
   58474          458635 :   if( p->flags&MEM_Agg ){
   58475               1 :     sqlite3VdbeMemFinalize(p, p->u.pDef);
   58476               1 :     assert( (p->flags & MEM_Agg)==0 );
   58477               1 :     sqlite3VdbeMemRelease(p);
   58478          458634 :   }else if( p->flags&MEM_Dyn && p->xDel ){
   58479            9294 :     assert( (p->flags&MEM_RowSet)==0 );
   58480            9294 :     p->xDel((void *)p->z);
   58481            9294 :     p->xDel = 0;
   58482          449340 :   }else if( p->flags&MEM_RowSet ){
   58483               7 :     sqlite3RowSetClear(p->u.pRowSet);
   58484          449333 :   }else if( p->flags&MEM_Frame ){
   58485           14229 :     sqlite3VdbeMemSetNull(p);
   58486                 :   }
   58487          458635 : }
   58488                 : 
   58489                 : /*
   58490                 : ** Release any memory held by the Mem. This may leave the Mem in an
   58491                 : ** inconsistent state, for example with (Mem.z==0) and
   58492                 : ** (Mem.type==SQLITE_TEXT).
   58493                 : */
   58494         4024892 : SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
   58495         4024892 :   VdbeMemRelease(p);
   58496         4024892 :   sqlite3DbFree(p->db, p->zMalloc);
   58497         4024637 :   p->z = 0;
   58498         4024637 :   p->zMalloc = 0;
   58499         4024637 :   p->xDel = 0;
   58500         4024637 : }
   58501                 : 
   58502                 : /*
   58503                 : ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   58504                 : ** If the double is too large, return 0x8000000000000000.
   58505                 : **
   58506                 : ** Most systems appear to do this simply by assigning
   58507                 : ** variables and without the extra range tests.  But
   58508                 : ** there are reports that windows throws an expection
   58509                 : ** if the floating point value is out of range. (See ticket #2880.)
   58510                 : ** Because we do not completely understand the problem, we will
   58511                 : ** take the conservative approach and always do range tests
   58512                 : ** before attempting the conversion.
   58513                 : */
   58514           22213 : static i64 doubleToInt64(double r){
   58515                 : #ifdef SQLITE_OMIT_FLOATING_POINT
   58516                 :   /* When floating-point is omitted, double and int64 are the same thing */
   58517                 :   return r;
   58518                 : #else
   58519                 :   /*
   58520                 :   ** Many compilers we encounter do not define constants for the
   58521                 :   ** minimum and maximum 64-bit integers, or they define them
   58522                 :   ** inconsistently.  And many do not understand the "LL" notation.
   58523                 :   ** So we define our own static constants here using nothing
   58524                 :   ** larger than a 32-bit integer constant.
   58525                 :   */
   58526                 :   static const i64 maxInt = LARGEST_INT64;
   58527                 :   static const i64 minInt = SMALLEST_INT64;
   58528                 : 
   58529           22213 :   if( r<(double)minInt ){
   58530               0 :     return minInt;
   58531           22213 :   }else if( r>(double)maxInt ){
   58532                 :     /* minInt is correct here - not maxInt.  It turns out that assigning
   58533                 :     ** a very large positive number to an integer results in a very large
   58534                 :     ** negative integer.  This makes no sense, but it is what x86 hardware
   58535                 :     ** does so for compatibility we will do the same in software. */
   58536               0 :     return minInt;
   58537                 :   }else{
   58538           22213 :     return (i64)r;
   58539                 :   }
   58540                 : #endif
   58541                 : }
   58542                 : 
   58543                 : /*
   58544                 : ** Return some kind of integer value which is the best we can do
   58545                 : ** at representing the value that *pMem describes as an integer.
   58546                 : ** If pMem is an integer, then the value is exact.  If pMem is
   58547                 : ** a floating-point then the value returned is the integer part.
   58548                 : ** If pMem is a string or blob, then we make an attempt to convert
   58549                 : ** it into a integer and return that.  If pMem represents an
   58550                 : ** an SQL-NULL value, return 0.
   58551                 : **
   58552                 : ** If pMem represents a string value, its encoding might be changed.
   58553                 : */
   58554          554386 : SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
   58555                 :   int flags;
   58556          554386 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58557          554382 :   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   58558          554382 :   flags = pMem->flags;
   58559          554382 :   if( flags & MEM_Int ){
   58560          536448 :     return pMem->u.i;
   58561           17934 :   }else if( flags & MEM_Real ){
   58562            6428 :     return doubleToInt64(pMem->r);
   58563           11506 :   }else if( flags & (MEM_Str|MEM_Blob) ){
   58564              20 :     i64 value = 0;
   58565              20 :     assert( pMem->z || pMem->n==0 );
   58566                 :     testcase( pMem->z==0 );
   58567              20 :     sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
   58568              20 :     return value;
   58569                 :   }else{
   58570           11486 :     return 0;
   58571                 :   }
   58572                 : }
   58573                 : 
   58574                 : /*
   58575                 : ** Return the best representation of pMem that we can get into a
   58576                 : ** double.  If pMem is already a double or an integer, return its
   58577                 : ** value.  If it is a string or blob, try to convert it to a double.
   58578                 : ** If it is a NULL, return 0.0.
   58579                 : */
   58580          101792 : SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
   58581          101792 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58582          101792 :   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   58583          101792 :   if( pMem->flags & MEM_Real ){
   58584           14125 :     return pMem->r;
   58585           87667 :   }else if( pMem->flags & MEM_Int ){
   58586           87486 :     return (double)pMem->u.i;
   58587             181 :   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
   58588                 :     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   58589             179 :     double val = (double)0;
   58590             179 :     sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
   58591             179 :     return val;
   58592                 :   }else{
   58593                 :     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   58594               2 :     return (double)0;
   58595                 :   }
   58596                 : }
   58597                 : 
   58598                 : /*
   58599                 : ** The MEM structure is already a MEM_Real.  Try to also make it a
   58600                 : ** MEM_Int if we can.
   58601                 : */
   58602           15785 : SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
   58603           15785 :   assert( pMem->flags & MEM_Real );
   58604           15785 :   assert( (pMem->flags & MEM_RowSet)==0 );
   58605           15785 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58606           15785 :   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   58607                 : 
   58608           15785 :   pMem->u.i = doubleToInt64(pMem->r);
   58609                 : 
   58610                 :   /* Only mark the value as an integer if
   58611                 :   **
   58612                 :   **    (1) the round-trip conversion real->int->real is a no-op, and
   58613                 :   **    (2) The integer is neither the largest nor the smallest
   58614                 :   **        possible integer (ticket #3922)
   58615                 :   **
   58616                 :   ** The second and third terms in the following conditional enforces
   58617                 :   ** the second condition under the assumption that addition overflow causes
   58618                 :   ** values to wrap around.  On x86 hardware, the third term is always
   58619                 :   ** true and could be omitted.  But we leave it in because other
   58620                 :   ** architectures might behave differently.
   58621                 :   */
   58622           15785 :   if( pMem->r==(double)pMem->u.i && pMem->u.i>SMALLEST_INT64
   58623           15480 :       && ALWAYS(pMem->u.i<LARGEST_INT64) ){
   58624           15480 :     pMem->flags |= MEM_Int;
   58625                 :   }
   58626           15785 : }
   58627                 : 
   58628                 : /*
   58629                 : ** Convert pMem to type integer.  Invalidate any prior representations.
   58630                 : */
   58631          102102 : SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
   58632          102102 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58633          102102 :   assert( (pMem->flags & MEM_RowSet)==0 );
   58634          102102 :   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   58635                 : 
   58636          102102 :   pMem->u.i = sqlite3VdbeIntValue(pMem);
   58637          102102 :   MemSetTypeFlag(pMem, MEM_Int);
   58638          102102 :   return SQLITE_OK;
   58639                 : }
   58640                 : 
   58641                 : /*
   58642                 : ** Convert pMem so that it is of type MEM_Real.
   58643                 : ** Invalidate any prior representations.
   58644                 : */
   58645            4915 : SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
   58646            4915 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58647            4915 :   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   58648                 : 
   58649            4915 :   pMem->r = sqlite3VdbeRealValue(pMem);
   58650            4915 :   MemSetTypeFlag(pMem, MEM_Real);
   58651            4915 :   return SQLITE_OK;
   58652                 : }
   58653                 : 
   58654                 : /*
   58655                 : ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
   58656                 : ** Invalidate any prior representations.
   58657                 : **
   58658                 : ** Every effort is made to force the conversion, even if the input
   58659                 : ** is a string that does not look completely like a number.  Convert
   58660                 : ** as much of the string as we can and ignore the rest.
   58661                 : */
   58662               0 : SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
   58663               0 :   if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
   58664               0 :     assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
   58665               0 :     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58666               0 :     if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
   58667               0 :       MemSetTypeFlag(pMem, MEM_Int);
   58668                 :     }else{
   58669               0 :       pMem->r = sqlite3VdbeRealValue(pMem);
   58670               0 :       MemSetTypeFlag(pMem, MEM_Real);
   58671               0 :       sqlite3VdbeIntegerAffinity(pMem);
   58672                 :     }
   58673                 :   }
   58674               0 :   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
   58675               0 :   pMem->flags &= ~(MEM_Str|MEM_Blob);
   58676               0 :   return SQLITE_OK;
   58677                 : }
   58678                 : 
   58679                 : /*
   58680                 : ** Delete any previous value and set the value stored in *pMem to NULL.
   58681                 : */
   58682         1158953 : SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
   58683         1158953 :   if( pMem->flags & MEM_Frame ){
   58684           14229 :     VdbeFrame *pFrame = pMem->u.pFrame;
   58685           14229 :     pFrame->pParent = pFrame->v->pDelFrame;
   58686           14229 :     pFrame->v->pDelFrame = pFrame;
   58687                 :   }
   58688         1158953 :   if( pMem->flags & MEM_RowSet ){
   58689            6085 :     sqlite3RowSetClear(pMem->u.pRowSet);
   58690                 :   }
   58691         1158953 :   MemSetTypeFlag(pMem, MEM_Null);
   58692         1158953 :   pMem->type = SQLITE_NULL;
   58693         1158953 : }
   58694                 : 
   58695                 : /*
   58696                 : ** Delete any previous value and set the value to be a BLOB of length
   58697                 : ** n containing all zeros.
   58698                 : */
   58699               0 : SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
   58700               0 :   sqlite3VdbeMemRelease(pMem);
   58701               0 :   pMem->flags = MEM_Blob|MEM_Zero;
   58702               0 :   pMem->type = SQLITE_BLOB;
   58703               0 :   pMem->n = 0;
   58704               0 :   if( n<0 ) n = 0;
   58705               0 :   pMem->u.nZero = n;
   58706               0 :   pMem->enc = SQLITE_UTF8;
   58707                 : 
   58708                 : #ifdef SQLITE_OMIT_INCRBLOB
   58709                 :   sqlite3VdbeMemGrow(pMem, n, 0);
   58710                 :   if( pMem->z ){
   58711                 :     pMem->n = n;
   58712                 :     memset(pMem->z, 0, n);
   58713                 :   }
   58714                 : #endif
   58715               0 : }
   58716                 : 
   58717                 : /*
   58718                 : ** Delete any previous value and set the value stored in *pMem to val,
   58719                 : ** manifest type INTEGER.
   58720                 : */
   58721          285945 : SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
   58722          285945 :   sqlite3VdbeMemRelease(pMem);
   58723          285945 :   pMem->u.i = val;
   58724          285945 :   pMem->flags = MEM_Int;
   58725          285945 :   pMem->type = SQLITE_INTEGER;
   58726          285945 : }
   58727                 : 
   58728                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   58729                 : /*
   58730                 : ** Delete any previous value and set the value stored in *pMem to val,
   58731                 : ** manifest type REAL.
   58732                 : */
   58733           25039 : SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
   58734           25039 :   if( sqlite3IsNaN(val) ){
   58735               0 :     sqlite3VdbeMemSetNull(pMem);
   58736                 :   }else{
   58737           25039 :     sqlite3VdbeMemRelease(pMem);
   58738           25039 :     pMem->r = val;
   58739           25039 :     pMem->flags = MEM_Real;
   58740           25039 :     pMem->type = SQLITE_FLOAT;
   58741                 :   }
   58742           25039 : }
   58743                 : #endif
   58744                 : 
   58745                 : /*
   58746                 : ** Delete any previous value and set the value of pMem to be an
   58747                 : ** empty boolean index.
   58748                 : */
   58749            6092 : SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
   58750            6092 :   sqlite3 *db = pMem->db;
   58751            6092 :   assert( db!=0 );
   58752            6092 :   assert( (pMem->flags & MEM_RowSet)==0 );
   58753            6092 :   sqlite3VdbeMemRelease(pMem);
   58754            6092 :   pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
   58755            6092 :   if( db->mallocFailed ){
   58756               0 :     pMem->flags = MEM_Null;
   58757                 :   }else{
   58758            6092 :     assert( pMem->zMalloc );
   58759            6092 :     pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, 
   58760            6092 :                                        sqlite3DbMallocSize(db, pMem->zMalloc));
   58761            6092 :     assert( pMem->u.pRowSet!=0 );
   58762            6092 :     pMem->flags = MEM_RowSet;
   58763                 :   }
   58764            6092 : }
   58765                 : 
   58766                 : /*
   58767                 : ** Return true if the Mem object contains a TEXT or BLOB that is
   58768                 : ** too large - whose size exceeds SQLITE_MAX_LENGTH.
   58769                 : */
   58770          557909 : SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
   58771          557909 :   assert( p->db!=0 );
   58772          557909 :   if( p->flags & (MEM_Str|MEM_Blob) ){
   58773          212107 :     int n = p->n;
   58774          212107 :     if( p->flags & MEM_Zero ){
   58775               0 :       n += p->u.nZero;
   58776                 :     }
   58777          212107 :     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
   58778                 :   }
   58779          345802 :   return 0; 
   58780                 : }
   58781                 : 
   58782                 : #ifdef SQLITE_DEBUG
   58783                 : /*
   58784                 : ** This routine prepares a memory cell for modication by breaking
   58785                 : ** its link to a shallow copy and by marking any current shallow
   58786                 : ** copies of this cell as invalid.
   58787                 : **
   58788                 : ** This is used for testing and debugging only - to make sure shallow
   58789                 : ** copies are not misused.
   58790                 : */
   58791         6246398 : SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
   58792                 :   int i;
   58793                 :   Mem *pX;
   58794       149846499 :   for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
   58795       143600101 :     if( pX->pScopyFrom==pMem ){
   58796          171158 :       pX->flags |= MEM_Invalid;
   58797          171158 :       pX->pScopyFrom = 0;
   58798                 :     }
   58799                 :   }
   58800         6246398 :   pMem->pScopyFrom = 0;
   58801         6246398 : }
   58802                 : #endif /* SQLITE_DEBUG */
   58803                 : 
   58804                 : /*
   58805                 : ** Size of struct Mem not including the Mem.zMalloc member.
   58806                 : */
   58807                 : #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
   58808                 : 
   58809                 : /*
   58810                 : ** Make an shallow copy of pFrom into pTo.  Prior contents of
   58811                 : ** pTo are freed.  The pFrom->z field is not duplicated.  If
   58812                 : ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   58813                 : ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   58814                 : */
   58815          925779 : SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   58816          925779 :   assert( (pFrom->flags & MEM_RowSet)==0 );
   58817          925779 :   VdbeMemRelease(pTo);
   58818          925779 :   memcpy(pTo, pFrom, MEMCELLSIZE);
   58819          925779 :   pTo->xDel = 0;
   58820          925779 :   if( (pFrom->flags&MEM_Static)==0 ){
   58821          761878 :     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   58822          761878 :     assert( srcType==MEM_Ephem || srcType==MEM_Static );
   58823          761878 :     pTo->flags |= srcType;
   58824                 :   }
   58825          925779 : }
   58826                 : 
   58827                 : /*
   58828                 : ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   58829                 : ** freed before the copy is made.
   58830                 : */
   58831           19620 : SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   58832           19620 :   int rc = SQLITE_OK;
   58833                 : 
   58834           19620 :   assert( (pFrom->flags & MEM_RowSet)==0 );
   58835           19620 :   VdbeMemRelease(pTo);
   58836           19620 :   memcpy(pTo, pFrom, MEMCELLSIZE);
   58837           19620 :   pTo->flags &= ~MEM_Dyn;
   58838                 : 
   58839           19620 :   if( pTo->flags&(MEM_Str|MEM_Blob) ){
   58840             156 :     if( 0==(pFrom->flags&MEM_Static) ){
   58841             156 :       pTo->flags |= MEM_Ephem;
   58842             156 :       rc = sqlite3VdbeMemMakeWriteable(pTo);
   58843                 :     }
   58844                 :   }
   58845                 : 
   58846           19620 :   return rc;
   58847                 : }
   58848                 : 
   58849                 : /*
   58850                 : ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
   58851                 : ** freed. If pFrom contains ephemeral data, a copy is made.
   58852                 : **
   58853                 : ** pFrom contains an SQL NULL when this routine returns.
   58854                 : */
   58855          208195 : SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
   58856          208195 :   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
   58857          208195 :   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
   58858          208195 :   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
   58859                 : 
   58860          208195 :   sqlite3VdbeMemRelease(pTo);
   58861          208195 :   memcpy(pTo, pFrom, sizeof(Mem));
   58862          208195 :   pFrom->flags = MEM_Null;
   58863          208195 :   pFrom->xDel = 0;
   58864          208195 :   pFrom->zMalloc = 0;
   58865          208195 : }
   58866                 : 
   58867                 : /*
   58868                 : ** Change the value of a Mem to be a string or a BLOB.
   58869                 : **
   58870                 : ** The memory management strategy depends on the value of the xDel
   58871                 : ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
   58872                 : ** string is copied into a (possibly existing) buffer managed by the 
   58873                 : ** Mem structure. Otherwise, any existing buffer is freed and the
   58874                 : ** pointer copied.
   58875                 : **
   58876                 : ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
   58877                 : ** size limit) then no memory allocation occurs.  If the string can be
   58878                 : ** stored without allocating memory, then it is.  If a memory allocation
   58879                 : ** is required to store the string, then value of pMem is unchanged.  In
   58880                 : ** either case, SQLITE_TOOBIG is returned.
   58881                 : */
   58882         1884111 : SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
   58883                 :   Mem *pMem,          /* Memory cell to set to string value */
   58884                 :   const char *z,      /* String pointer */
   58885                 :   int n,              /* Bytes in string, or negative */
   58886                 :   u8 enc,             /* Encoding of z.  0 for BLOBs */
   58887                 :   void (*xDel)(void*) /* Destructor function */
   58888                 : ){
   58889         1884111 :   int nByte = n;      /* New value for pMem->n */
   58890                 :   int iLimit;         /* Maximum allowed string or blob size */
   58891         1884111 :   u16 flags = 0;      /* New value for pMem->flags */
   58892                 : 
   58893         1884111 :   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   58894         1884110 :   assert( (pMem->flags & MEM_RowSet)==0 );
   58895                 : 
   58896                 :   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
   58897         1884110 :   if( !z ){
   58898         1121448 :     sqlite3VdbeMemSetNull(pMem);
   58899         1121448 :     return SQLITE_OK;
   58900                 :   }
   58901                 : 
   58902          762662 :   if( pMem->db ){
   58903          762662 :     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
   58904                 :   }else{
   58905               0 :     iLimit = SQLITE_MAX_LENGTH;
   58906                 :   }
   58907          762662 :   flags = (enc==0?MEM_Blob:MEM_Str);
   58908          762662 :   if( nByte<0 ){
   58909          570118 :     assert( enc!=0 );
   58910          570118 :     if( enc==SQLITE_UTF8 ){
   58911          569895 :       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
   58912                 :     }else{
   58913             223 :       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
   58914                 :     }
   58915          570118 :     flags |= MEM_Term;
   58916                 :   }
   58917                 : 
   58918                 :   /* The following block sets the new values of Mem.z and Mem.xDel. It
   58919                 :   ** also sets a flag in local variable "flags" to indicate the memory
   58920                 :   ** management (one of MEM_Dyn or MEM_Static).
   58921                 :   */
   58922          762662 :   if( xDel==SQLITE_TRANSIENT ){
   58923          738480 :     int nAlloc = nByte;
   58924          738480 :     if( flags&MEM_Term ){
   58925          554594 :       nAlloc += (enc==SQLITE_UTF8?1:2);
   58926                 :     }
   58927          738480 :     if( nByte>iLimit ){
   58928               0 :       return SQLITE_TOOBIG;
   58929                 :     }
   58930          738480 :     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
   58931               0 :       return SQLITE_NOMEM;
   58932                 :     }
   58933          738478 :     memcpy(pMem->z, z, nAlloc);
   58934           24182 :   }else if( xDel==SQLITE_DYNAMIC ){
   58935            6988 :     sqlite3VdbeMemRelease(pMem);
   58936            6988 :     pMem->zMalloc = pMem->z = (char *)z;
   58937            6988 :     pMem->xDel = 0;
   58938                 :   }else{
   58939           17194 :     sqlite3VdbeMemRelease(pMem);
   58940           17194 :     pMem->z = (char *)z;
   58941           17194 :     pMem->xDel = xDel;
   58942           17194 :     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
   58943                 :   }
   58944                 : 
   58945          762660 :   pMem->n = nByte;
   58946          762660 :   pMem->flags = flags;
   58947          762660 :   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
   58948          762660 :   pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
   58949                 : 
   58950                 : #ifndef SQLITE_OMIT_UTF16
   58951          762660 :   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
   58952               0 :     return SQLITE_NOMEM;
   58953                 :   }
   58954                 : #endif
   58955                 : 
   58956          762660 :   if( nByte>iLimit ){
   58957               0 :     return SQLITE_TOOBIG;
   58958                 :   }
   58959                 : 
   58960          762660 :   return SQLITE_OK;
   58961                 : }
   58962                 : 
   58963                 : /*
   58964                 : ** Compare the values contained by the two memory cells, returning
   58965                 : ** negative, zero or positive if pMem1 is less than, equal to, or greater
   58966                 : ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
   58967                 : ** and reals) sorted numerically, followed by text ordered by the collating
   58968                 : ** sequence pColl and finally blob's ordered by memcmp().
   58969                 : **
   58970                 : ** Two NULL values are considered equal by this function.
   58971                 : */
   58972         1984041 : SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
   58973                 :   int rc;
   58974                 :   int f1, f2;
   58975                 :   int combined_flags;
   58976                 : 
   58977         1984041 :   f1 = pMem1->flags;
   58978         1984041 :   f2 = pMem2->flags;
   58979         1984041 :   combined_flags = f1|f2;
   58980         1984041 :   assert( (combined_flags & MEM_RowSet)==0 );
   58981                 :  
   58982                 :   /* If one value is NULL, it is less than the other. If both values
   58983                 :   ** are NULL, return 0.
   58984                 :   */
   58985         1984041 :   if( combined_flags&MEM_Null ){
   58986          125929 :     return (f2&MEM_Null) - (f1&MEM_Null);
   58987                 :   }
   58988                 : 
   58989                 :   /* If one value is a number and the other is not, the number is less.
   58990                 :   ** If both are numbers, compare as reals if one is a real, or as integers
   58991                 :   ** if both values are integers.
   58992                 :   */
   58993         1858112 :   if( combined_flags&(MEM_Int|MEM_Real) ){
   58994          839080 :     if( !(f1&(MEM_Int|MEM_Real)) ){
   58995               0 :       return 1;
   58996                 :     }
   58997          839080 :     if( !(f2&(MEM_Int|MEM_Real)) ){
   58998               0 :       return -1;
   58999                 :     }
   59000          839080 :     if( (f1 & f2 & MEM_Int)==0 ){
   59001                 :       double r1, r2;
   59002            6871 :       if( (f1&MEM_Real)==0 ){
   59003            1980 :         r1 = (double)pMem1->u.i;
   59004                 :       }else{
   59005            4891 :         r1 = pMem1->r;
   59006                 :       }
   59007            6871 :       if( (f2&MEM_Real)==0 ){
   59008             645 :         r2 = (double)pMem2->u.i;
   59009                 :       }else{
   59010            6226 :         r2 = pMem2->r;
   59011                 :       }
   59012            6871 :       if( r1<r2 ) return -1;
   59013            5164 :       if( r1>r2 ) return 1;
   59014              80 :       return 0;
   59015                 :     }else{
   59016          832209 :       assert( f1&MEM_Int );
   59017          832209 :       assert( f2&MEM_Int );
   59018          832209 :       if( pMem1->u.i < pMem2->u.i ) return -1;
   59019          530984 :       if( pMem1->u.i > pMem2->u.i ) return 1;
   59020          404740 :       return 0;
   59021                 :     }
   59022                 :   }
   59023                 : 
   59024                 :   /* If one value is a string and the other is a blob, the string is less.
   59025                 :   ** If both are strings, compare using the collating functions.
   59026                 :   */
   59027         1019032 :   if( combined_flags&MEM_Str ){
   59028          979812 :     if( (f1 & MEM_Str)==0 ){
   59029               0 :       return 1;
   59030                 :     }
   59031          979812 :     if( (f2 & MEM_Str)==0 ){
   59032               0 :       return -1;
   59033                 :     }
   59034                 : 
   59035          979812 :     assert( pMem1->enc==pMem2->enc );
   59036          979812 :     assert( pMem1->enc==SQLITE_UTF8 || 
   59037                 :             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
   59038                 : 
   59039                 :     /* The collation sequence must be defined at this point, even if
   59040                 :     ** the user deletes the collation sequence after the vdbe program is
   59041                 :     ** compiled (this was not always the case).
   59042                 :     */
   59043          979812 :     assert( !pColl || pColl->xCmp );
   59044                 : 
   59045          979812 :     if( pColl ){
   59046          974683 :       if( pMem1->enc==pColl->enc ){
   59047                 :         /* The strings are already in the correct encoding.  Call the
   59048                 :         ** comparison function directly */
   59049          974683 :         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
   59050                 :       }else{
   59051                 :         const void *v1, *v2;
   59052                 :         int n1, n2;
   59053                 :         Mem c1;
   59054                 :         Mem c2;
   59055               0 :         memset(&c1, 0, sizeof(c1));
   59056               0 :         memset(&c2, 0, sizeof(c2));
   59057               0 :         sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
   59058               0 :         sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
   59059               0 :         v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
   59060               0 :         n1 = v1==0 ? 0 : c1.n;
   59061               0 :         v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
   59062               0 :         n2 = v2==0 ? 0 : c2.n;
   59063               0 :         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
   59064               0 :         sqlite3VdbeMemRelease(&c1);
   59065               0 :         sqlite3VdbeMemRelease(&c2);
   59066               0 :         return rc;
   59067                 :       }
   59068                 :     }
   59069                 :     /* If a NULL pointer was passed as the collate function, fall through
   59070                 :     ** to the blob case and use memcmp().  */
   59071                 :   }
   59072                 :  
   59073                 :   /* Both values must be blobs.  Compare using memcmp().  */
   59074           44349 :   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
   59075           44349 :   if( rc==0 ){
   59076            9456 :     rc = pMem1->n - pMem2->n;
   59077                 :   }
   59078           44349 :   return rc;
   59079                 : }
   59080                 : 
   59081                 : /*
   59082                 : ** Move data out of a btree key or data field and into a Mem structure.
   59083                 : ** The data or key is taken from the entry that pCur is currently pointing
   59084                 : ** to.  offset and amt determine what portion of the data or key to retrieve.
   59085                 : ** key is true to get the key or false to get data.  The result is written
   59086                 : ** into the pMem element.
   59087                 : **
   59088                 : ** The pMem structure is assumed to be uninitialized.  Any prior content
   59089                 : ** is overwritten without being freed.
   59090                 : **
   59091                 : ** If this routine fails for any reason (malloc returns NULL or unable
   59092                 : ** to read from the disk) then the pMem is left in an inconsistent state.
   59093                 : */
   59094          323244 : SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
   59095                 :   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
   59096                 :   int offset,       /* Offset from the start of data to return bytes from. */
   59097                 :   int amt,          /* Number of bytes to return. */
   59098                 :   int key,          /* If true, retrieve from the btree key, not data. */
   59099                 :   Mem *pMem         /* OUT: Return data in this Mem structure. */
   59100                 : ){
   59101                 :   char *zData;        /* Data from the btree layer */
   59102          323244 :   int available = 0;  /* Number of bytes available on the local btree page */
   59103          323244 :   int rc = SQLITE_OK; /* Return code */
   59104                 : 
   59105          323244 :   assert( sqlite3BtreeCursorIsValid(pCur) );
   59106                 : 
   59107                 :   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
   59108                 :   ** that both the BtShared and database handle mutexes are held. */
   59109          323244 :   assert( (pMem->flags & MEM_RowSet)==0 );
   59110          323244 :   if( key ){
   59111          323226 :     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
   59112                 :   }else{
   59113              18 :     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
   59114                 :   }
   59115          323244 :   assert( zData!=0 );
   59116                 : 
   59117          323244 :   if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
   59118          323227 :     sqlite3VdbeMemRelease(pMem);
   59119          323227 :     pMem->z = &zData[offset];
   59120          323227 :     pMem->flags = MEM_Blob|MEM_Ephem;
   59121              17 :   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
   59122              17 :     pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
   59123              17 :     pMem->enc = 0;
   59124              17 :     pMem->type = SQLITE_BLOB;
   59125              17 :     if( key ){
   59126               0 :       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
   59127                 :     }else{
   59128              17 :       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
   59129                 :     }
   59130              17 :     pMem->z[amt] = 0;
   59131              17 :     pMem->z[amt+1] = 0;
   59132              17 :     if( rc!=SQLITE_OK ){
   59133               0 :       sqlite3VdbeMemRelease(pMem);
   59134                 :     }
   59135                 :   }
   59136          323244 :   pMem->n = amt;
   59137                 : 
   59138          323244 :   return rc;
   59139                 : }
   59140                 : 
   59141                 : /* This function is only available internally, it is not part of the
   59142                 : ** external API. It works in a similar way to sqlite3_value_text(),
   59143                 : ** except the data returned is in the encoding specified by the second
   59144                 : ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
   59145                 : ** SQLITE_UTF8.
   59146                 : **
   59147                 : ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
   59148                 : ** If that is the case, then the result must be aligned on an even byte
   59149                 : ** boundary.
   59150                 : */
   59151         1169406 : SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
   59152         1169406 :   if( !pVal ) return 0;
   59153                 : 
   59154         1169406 :   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
   59155         1169373 :   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
   59156         1169373 :   assert( (pVal->flags & MEM_RowSet)==0 );
   59157                 : 
   59158         1169373 :   if( pVal->flags&MEM_Null ){
   59159           13491 :     return 0;
   59160                 :   }
   59161                 :   assert( (MEM_Blob>>3) == MEM_Str );
   59162         1155882 :   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
   59163         1155882 :   ExpandBlob(pVal);
   59164         1155882 :   if( pVal->flags&MEM_Str ){
   59165         1100144 :     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
   59166         1100129 :     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
   59167               0 :       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
   59168               0 :       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
   59169               0 :         return 0;
   59170                 :       }
   59171                 :     }
   59172         1100129 :     sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
   59173                 :   }else{
   59174           55738 :     assert( (pVal->flags&MEM_Blob)==0 );
   59175           55738 :     sqlite3VdbeMemStringify(pVal, enc);
   59176           55739 :     assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
   59177                 :   }
   59178         1155843 :   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
   59179                 :               || pVal->db->mallocFailed );
   59180         1155843 :   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
   59181         1155843 :     return pVal->z;
   59182                 :   }else{
   59183               0 :     return 0;
   59184                 :   }
   59185                 : }
   59186                 : 
   59187                 : /*
   59188                 : ** Create a new sqlite3_value object.
   59189                 : */
   59190           23359 : SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
   59191           23359 :   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
   59192           23359 :   if( p ){
   59193           23359 :     p->flags = MEM_Null;
   59194           23359 :     p->type = SQLITE_NULL;
   59195           23359 :     p->db = db;
   59196                 :   }
   59197           23359 :   return p;
   59198                 : }
   59199                 : 
   59200                 : /*
   59201                 : ** Create a new sqlite3_value object, containing the value of pExpr.
   59202                 : **
   59203                 : ** This only works for very simple expressions that consist of one constant
   59204                 : ** token (i.e. "5", "5.1", "'a string'"). If the expression can
   59205                 : ** be converted directly into a value, then the value is allocated and
   59206                 : ** a pointer written to *ppVal. The caller is responsible for deallocating
   59207                 : ** the value by passing it to sqlite3ValueFree() later on. If the expression
   59208                 : ** cannot be converted to a value, then *ppVal is set to NULL.
   59209                 : */
   59210          430110 : SQLITE_PRIVATE int sqlite3ValueFromExpr(
   59211                 :   sqlite3 *db,              /* The database connection */
   59212                 :   Expr *pExpr,              /* The expression to evaluate */
   59213                 :   u8 enc,                   /* Encoding to use */
   59214                 :   u8 affinity,              /* Affinity to use */
   59215                 :   sqlite3_value **ppVal     /* Write the new value here */
   59216                 : ){
   59217                 :   int op;
   59218          430110 :   char *zVal = 0;
   59219          430110 :   sqlite3_value *pVal = 0;
   59220          430110 :   int negInt = 1;
   59221          430110 :   const char *zNeg = "";
   59222                 : 
   59223          430110 :   if( !pExpr ){
   59224          410184 :     *ppVal = 0;
   59225          410184 :     return SQLITE_OK;
   59226                 :   }
   59227           19926 :   op = pExpr->op;
   59228                 : 
   59229                 :   /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
   59230                 :   ** The ifdef here is to enable us to achieve 100% branch test coverage even
   59231                 :   ** when SQLITE_ENABLE_STAT3 is omitted.
   59232                 :   */
   59233                 : #ifdef SQLITE_ENABLE_STAT3
   59234                 :   if( op==TK_REGISTER ) op = pExpr->op2;
   59235                 : #else
   59236           19926 :   if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
   59237                 : #endif
   59238                 : 
   59239                 :   /* Handle negative integers in a single step.  This is needed in the
   59240                 :   ** case when the value is -9223372036854775808.
   59241                 :   */
   59242           19926 :   if( op==TK_UMINUS
   59243            3683 :    && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
   59244            3683 :     pExpr = pExpr->pLeft;
   59245            3683 :     op = pExpr->op;
   59246            3683 :     negInt = -1;
   59247            3683 :     zNeg = "-";
   59248                 :   }
   59249                 : 
   59250           19926 :   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
   59251           15997 :     pVal = sqlite3ValueNew(db);
   59252           15997 :     if( pVal==0 ) goto no_mem;
   59253           15997 :     if( ExprHasProperty(pExpr, EP_IntValue) ){
   59254           15997 :       sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
   59255                 :     }else{
   59256               0 :       zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
   59257               0 :       if( zVal==0 ) goto no_mem;
   59258               0 :       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
   59259               0 :       if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
   59260                 :     }
   59261           15997 :     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
   59262              37 :       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
   59263                 :     }else{
   59264           15960 :       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
   59265                 :     }
   59266           15997 :     if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
   59267           31994 :     if( enc!=SQLITE_UTF8 ){
   59268               0 :       sqlite3VdbeChangeEncoding(pVal, enc);
   59269                 :     }
   59270            3929 :   }else if( op==TK_UMINUS ) {
   59271                 :     /* This branch happens for multiple negative signs.  Ex: -(-5) */
   59272               0 :     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
   59273               0 :       sqlite3VdbeMemNumerify(pVal);
   59274               0 :       if( pVal->u.i==SMALLEST_INT64 ){
   59275               0 :         pVal->flags &= MEM_Int;
   59276               0 :         pVal->flags |= MEM_Real;
   59277               0 :         pVal->r = (double)LARGEST_INT64;
   59278                 :       }else{
   59279               0 :         pVal->u.i = -pVal->u.i;
   59280                 :       }
   59281               0 :       pVal->r = -pVal->r;
   59282               0 :       sqlite3ValueApplyAffinity(pVal, affinity, enc);
   59283                 :     }
   59284            3929 :   }else if( op==TK_NULL ){
   59285            3929 :     pVal = sqlite3ValueNew(db);
   59286            3929 :     if( pVal==0 ) goto no_mem;
   59287                 :   }
   59288                 : #ifndef SQLITE_OMIT_BLOB_LITERAL
   59289               0 :   else if( op==TK_BLOB ){
   59290                 :     int nVal;
   59291               0 :     assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
   59292               0 :     assert( pExpr->u.zToken[1]=='\'' );
   59293               0 :     pVal = sqlite3ValueNew(db);
   59294               0 :     if( !pVal ) goto no_mem;
   59295               0 :     zVal = &pExpr->u.zToken[2];
   59296               0 :     nVal = sqlite3Strlen30(zVal)-1;
   59297               0 :     assert( zVal[nVal]=='\'' );
   59298               0 :     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
   59299                 :                          0, SQLITE_DYNAMIC);
   59300                 :   }
   59301                 : #endif
   59302                 : 
   59303           19926 :   if( pVal ){
   59304           19926 :     sqlite3VdbeMemStoreType(pVal);
   59305                 :   }
   59306           19926 :   *ppVal = pVal;
   59307           19926 :   return SQLITE_OK;
   59308                 : 
   59309                 : no_mem:
   59310               0 :   db->mallocFailed = 1;
   59311               0 :   sqlite3DbFree(db, zVal);
   59312               0 :   sqlite3ValueFree(pVal);
   59313               0 :   *ppVal = 0;
   59314               0 :   return SQLITE_NOMEM;
   59315                 : }
   59316                 : 
   59317                 : /*
   59318                 : ** Change the string value of an sqlite3_value object
   59319                 : */
   59320         1106853 : SQLITE_PRIVATE void sqlite3ValueSetStr(
   59321                 :   sqlite3_value *v,     /* Value to be set */
   59322                 :   int n,                /* Length of string z */
   59323                 :   const void *z,        /* Text of the new string */
   59324                 :   u8 enc,               /* Encoding to use */
   59325                 :   void (*xDel)(void*)   /* Destructor for the string */
   59326                 : ){
   59327         1106853 :   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
   59328         1106853 : }
   59329                 : 
   59330                 : /*
   59331                 : ** Free an sqlite3_value object
   59332                 : */
   59333           23383 : SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
   59334           23383 :   if( !v ) return;
   59335           23355 :   sqlite3VdbeMemRelease((Mem *)v);
   59336           23355 :   sqlite3DbFree(((Mem*)v)->db, v);
   59337                 : }
   59338                 : 
   59339                 : /*
   59340                 : ** Return the number of bytes in the sqlite3_value object assuming
   59341                 : ** that it uses the encoding "enc"
   59342                 : */
   59343          163560 : SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
   59344          163560 :   Mem *p = (Mem*)pVal;
   59345          163560 :   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
   59346          163201 :     if( p->flags & MEM_Zero ){
   59347               0 :       return p->n + p->u.nZero;
   59348                 :     }else{
   59349          163201 :       return p->n;
   59350                 :     }
   59351                 :   }
   59352             359 :   return 0;
   59353                 : }
   59354                 : 
   59355                 : /************** End of vdbemem.c *********************************************/
   59356                 : /************** Begin file vdbeaux.c *****************************************/
   59357                 : /*
   59358                 : ** 2003 September 6
   59359                 : **
   59360                 : ** The author disclaims copyright to this source code.  In place of
   59361                 : ** a legal notice, here is a blessing:
   59362                 : **
   59363                 : **    May you do good and not evil.
   59364                 : **    May you find forgiveness for yourself and forgive others.
   59365                 : **    May you share freely, never taking more than you give.
   59366                 : **
   59367                 : *************************************************************************
   59368                 : ** This file contains code used for creating, destroying, and populating
   59369                 : ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
   59370                 : ** to version 2.8.7, all this code was combined into the vdbe.c source file.
   59371                 : ** But that file was getting too big so this subroutines were split out.
   59372                 : */
   59373                 : 
   59374                 : 
   59375                 : 
   59376                 : /*
   59377                 : ** When debugging the code generator in a symbolic debugger, one can
   59378                 : ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed
   59379                 : ** as they are added to the instruction stream.
   59380                 : */
   59381                 : #ifdef SQLITE_DEBUG
   59382                 : SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0;
   59383                 : #endif
   59384                 : 
   59385                 : 
   59386                 : /*
   59387                 : ** Create a new virtual database engine.
   59388                 : */
   59389          176050 : SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3 *db){
   59390                 :   Vdbe *p;
   59391          176050 :   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
   59392          176050 :   if( p==0 ) return 0;
   59393          176050 :   p->db = db;
   59394          176050 :   if( db->pVdbe ){
   59395          134086 :     db->pVdbe->pPrev = p;
   59396                 :   }
   59397          176050 :   p->pNext = db->pVdbe;
   59398          176050 :   p->pPrev = 0;
   59399          176050 :   db->pVdbe = p;
   59400          176050 :   p->magic = VDBE_MAGIC_INIT;
   59401          176050 :   return p;
   59402                 : }
   59403                 : 
   59404                 : /*
   59405                 : ** Remember the SQL string for a prepared statement.
   59406                 : */
   59407          103717 : SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
   59408          103717 :   assert( isPrepareV2==1 || isPrepareV2==0 );
   59409          103717 :   if( p==0 ) return;
   59410                 : #ifdef SQLITE_OMIT_TRACE
   59411                 :   if( !isPrepareV2 ) return;
   59412                 : #endif
   59413          103626 :   assert( p->zSql==0 );
   59414          103626 :   p->zSql = sqlite3DbStrNDup(p->db, z, n);
   59415          103626 :   p->isPrepareV2 = (u8)isPrepareV2;
   59416                 : }
   59417                 : 
   59418                 : /*
   59419                 : ** Return the SQL associated with a prepared statement
   59420                 : */
   59421            7535 : SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
   59422            7535 :   Vdbe *p = (Vdbe *)pStmt;
   59423            7535 :   return (p && p->isPrepareV2) ? p->zSql : 0;
   59424                 : }
   59425                 : 
   59426                 : /*
   59427                 : ** Swap all content between two VDBE structures.
   59428                 : */
   59429            1521 : SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
   59430                 :   Vdbe tmp, *pTmp;
   59431                 :   char *zTmp;
   59432            1521 :   tmp = *pA;
   59433            1521 :   *pA = *pB;
   59434            1521 :   *pB = tmp;
   59435            1521 :   pTmp = pA->pNext;
   59436            1521 :   pA->pNext = pB->pNext;
   59437            1521 :   pB->pNext = pTmp;
   59438            1521 :   pTmp = pA->pPrev;
   59439            1521 :   pA->pPrev = pB->pPrev;
   59440            1521 :   pB->pPrev = pTmp;
   59441            1521 :   zTmp = pA->zSql;
   59442            1521 :   pA->zSql = pB->zSql;
   59443            1521 :   pB->zSql = zTmp;
   59444            1521 :   pB->isPrepareV2 = pA->isPrepareV2;
   59445            1521 : }
   59446                 : 
   59447                 : #ifdef SQLITE_DEBUG
   59448                 : /*
   59449                 : ** Turn tracing on or off
   59450                 : */
   59451          172920 : SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
   59452          172920 :   p->trace = trace;
   59453          172920 : }
   59454                 : #endif
   59455                 : 
   59456                 : /*
   59457                 : ** Resize the Vdbe.aOp array so that it is at least one op larger than 
   59458                 : ** it was.
   59459                 : **
   59460                 : ** If an out-of-memory error occurs while resizing the array, return
   59461                 : ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 
   59462                 : ** unchanged (this is so that any opcodes already allocated can be 
   59463                 : ** correctly deallocated along with the rest of the Vdbe).
   59464                 : */
   59465          205835 : static int growOpArray(Vdbe *p){
   59466                 :   VdbeOp *pNew;
   59467          205835 :   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
   59468          205835 :   pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
   59469          205835 :   if( pNew ){
   59470          205835 :     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
   59471          205835 :     p->aOp = pNew;
   59472                 :   }
   59473          205835 :   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
   59474                 : }
   59475                 : 
   59476                 : /*
   59477                 : ** Add a new instruction to the list of instructions current in the
   59478                 : ** VDBE.  Return the address of the new instruction.
   59479                 : **
   59480                 : ** Parameters:
   59481                 : **
   59482                 : **    p               Pointer to the VDBE
   59483                 : **
   59484                 : **    op              The opcode for this instruction
   59485                 : **
   59486                 : **    p1, p2, p3      Operands
   59487                 : **
   59488                 : ** Use the sqlite3VdbeResolveLabel() function to fix an address and
   59489                 : ** the sqlite3VdbeChangeP4() function to change the value of the P4
   59490                 : ** operand.
   59491                 : */
   59492         3267094 : SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
   59493                 :   int i;
   59494                 :   VdbeOp *pOp;
   59495                 : 
   59496         3267094 :   i = p->nOp;
   59497         3267094 :   assert( p->magic==VDBE_MAGIC_INIT );
   59498         3267094 :   assert( op>0 && op<0xff );
   59499         3267094 :   if( p->nOpAlloc<=i ){
   59500          202638 :     if( growOpArray(p) ){
   59501               0 :       return 1;
   59502                 :     }
   59503                 :   }
   59504         3267094 :   p->nOp++;
   59505         3267094 :   pOp = &p->aOp[i];
   59506         3267094 :   pOp->opcode = (u8)op;
   59507         3267094 :   pOp->p5 = 0;
   59508         3267094 :   pOp->p1 = p1;
   59509         3267094 :   pOp->p2 = p2;
   59510         3267094 :   pOp->p3 = p3;
   59511         3267094 :   pOp->p4.p = 0;
   59512         3267094 :   pOp->p4type = P4_NOTUSED;
   59513                 : #ifdef SQLITE_DEBUG
   59514         3267094 :   pOp->zComment = 0;
   59515         3267094 :   if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
   59516                 : #endif
   59517                 : #ifdef VDBE_PROFILE
   59518                 :   pOp->cycles = 0;
   59519                 :   pOp->cnt = 0;
   59520                 : #endif
   59521         3267093 :   return i;
   59522                 : }
   59523          365041 : SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
   59524          365041 :   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
   59525                 : }
   59526          232741 : SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
   59527          232741 :   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
   59528                 : }
   59529         1164810 : SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
   59530         1164810 :   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
   59531                 : }
   59532                 : 
   59533                 : 
   59534                 : /*
   59535                 : ** Add an opcode that includes the p4 value as a pointer.
   59536                 : */
   59537          432317 : SQLITE_PRIVATE int sqlite3VdbeAddOp4(
   59538                 :   Vdbe *p,            /* Add the opcode to this VM */
   59539                 :   int op,             /* The new opcode */
   59540                 :   int p1,             /* The P1 operand */
   59541                 :   int p2,             /* The P2 operand */
   59542                 :   int p3,             /* The P3 operand */
   59543                 :   const char *zP4,    /* The P4 operand */
   59544                 :   int p4type          /* P4 operand type */
   59545                 : ){
   59546          432317 :   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
   59547          432317 :   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
   59548          432317 :   return addr;
   59549                 : }
   59550                 : 
   59551                 : /*
   59552                 : ** Add an OP_ParseSchema opcode.  This routine is broken out from
   59553                 : ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
   59554                 : ** as having been used.
   59555                 : **
   59556                 : ** The zWhere string must have been obtained from sqlite3_malloc().
   59557                 : ** This routine will take ownership of the allocated memory.
   59558                 : */
   59559           15463 : SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
   59560                 :   int j;
   59561           15463 :   int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
   59562           15463 :   sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
   59563           15463 :   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
   59564           15463 : }
   59565                 : 
   59566                 : /*
   59567                 : ** Add an opcode that includes the p4 value as an integer.
   59568                 : */
   59569           52161 : SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
   59570                 :   Vdbe *p,            /* Add the opcode to this VM */
   59571                 :   int op,             /* The new opcode */
   59572                 :   int p1,             /* The P1 operand */
   59573                 :   int p2,             /* The P2 operand */
   59574                 :   int p3,             /* The P3 operand */
   59575                 :   int p4              /* The P4 operand as an integer */
   59576                 : ){
   59577           52161 :   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
   59578           52161 :   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
   59579           52161 :   return addr;
   59580                 : }
   59581                 : 
   59582                 : /*
   59583                 : ** Create a new symbolic label for an instruction that has yet to be
   59584                 : ** coded.  The symbolic label is really just a negative number.  The
   59585                 : ** label can be used as the P2 value of an operation.  Later, when
   59586                 : ** the label is resolved to a specific address, the VDBE will scan
   59587                 : ** through its operation list and change all values of P2 which match
   59588                 : ** the label into the resolved address.
   59589                 : **
   59590                 : ** The VDBE knows that a P2 value is a label because labels are
   59591                 : ** always negative and P2 values are suppose to be non-negative.
   59592                 : ** Hence, a negative P2 value is a label that has yet to be resolved.
   59593                 : **
   59594                 : ** Zero is returned if a malloc() fails.
   59595                 : */
   59596          370745 : SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *p){
   59597                 :   int i;
   59598          370745 :   i = p->nLabel++;
   59599          370745 :   assert( p->magic==VDBE_MAGIC_INIT );
   59600          370745 :   if( i>=p->nLabelAlloc ){
   59601           84498 :     int n = p->nLabelAlloc*2 + 5;
   59602           84498 :     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
   59603           84498 :                                        n*sizeof(p->aLabel[0]));
   59604           84498 :     p->nLabelAlloc = sqlite3DbMallocSize(p->db, p->aLabel)/sizeof(p->aLabel[0]);
   59605                 :   }
   59606          370745 :   if( p->aLabel ){
   59607          370745 :     p->aLabel[i] = -1;
   59608                 :   }
   59609          370745 :   return -1-i;
   59610                 : }
   59611                 : 
   59612                 : /*
   59613                 : ** Resolve label "x" to be the address of the next instruction to
   59614                 : ** be inserted.  The parameter "x" must have been obtained from
   59615                 : ** a prior call to sqlite3VdbeMakeLabel().
   59616                 : */
   59617          370745 : SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){
   59618          370745 :   int j = -1-x;
   59619          370745 :   assert( p->magic==VDBE_MAGIC_INIT );
   59620          370745 :   assert( j>=0 && j<p->nLabel );
   59621          370745 :   if( p->aLabel ){
   59622          370745 :     p->aLabel[j] = p->nOp;
   59623                 :   }
   59624          370745 : }
   59625                 : 
   59626                 : /*
   59627                 : ** Mark the VDBE as one that can only be run one time.
   59628                 : */
   59629           22669 : SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
   59630           22669 :   p->runOnlyOnce = 1;
   59631           22669 : }
   59632                 : 
   59633                 : #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
   59634                 : 
   59635                 : /*
   59636                 : ** The following type and function are used to iterate through all opcodes
   59637                 : ** in a Vdbe main program and each of the sub-programs (triggers) it may 
   59638                 : ** invoke directly or indirectly. It should be used as follows:
   59639                 : **
   59640                 : **   Op *pOp;
   59641                 : **   VdbeOpIter sIter;
   59642                 : **
   59643                 : **   memset(&sIter, 0, sizeof(sIter));
   59644                 : **   sIter.v = v;                            // v is of type Vdbe* 
   59645                 : **   while( (pOp = opIterNext(&sIter)) ){
   59646                 : **     // Do something with pOp
   59647                 : **   }
   59648                 : **   sqlite3DbFree(v->db, sIter.apSub);
   59649                 : ** 
   59650                 : */
   59651                 : typedef struct VdbeOpIter VdbeOpIter;
   59652                 : struct VdbeOpIter {
   59653                 :   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
   59654                 :   SubProgram **apSub;        /* Array of subprograms */
   59655                 :   int nSub;                  /* Number of entries in apSub */
   59656                 :   int iAddr;                 /* Address of next instruction to return */
   59657                 :   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
   59658                 : };
   59659         1276874 : static Op *opIterNext(VdbeOpIter *p){
   59660         1276874 :   Vdbe *v = p->v;
   59661         1276874 :   Op *pRet = 0;
   59662                 :   Op *aOp;
   59663                 :   int nOp;
   59664                 : 
   59665         1276874 :   if( p->iSub<=p->nSub ){
   59666                 : 
   59667         1257047 :     if( p->iSub==0 ){
   59668         1124845 :       aOp = v->aOp;
   59669         1124845 :       nOp = v->nOp;
   59670                 :     }else{
   59671          132202 :       aOp = p->apSub[p->iSub-1]->aOp;
   59672          132202 :       nOp = p->apSub[p->iSub-1]->nOp;
   59673                 :     }
   59674         1257047 :     assert( p->iAddr<nOp );
   59675                 : 
   59676         1257047 :     pRet = &aOp[p->iAddr];
   59677         1257047 :     p->iAddr++;
   59678         1257047 :     if( p->iAddr==nOp ){
   59679           21930 :       p->iSub++;
   59680           21930 :       p->iAddr = 0;
   59681                 :     }
   59682                 :   
   59683         1257047 :     if( pRet->p4type==P4_SUBPROGRAM ){
   59684            2446 :       int nByte = (p->nSub+1)*sizeof(SubProgram*);
   59685                 :       int j;
   59686            4161 :       for(j=0; j<p->nSub; j++){
   59687            2058 :         if( p->apSub[j]==pRet->p4.pProgram ) break;
   59688                 :       }
   59689            2446 :       if( j==p->nSub ){
   59690            2103 :         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
   59691            2103 :         if( !p->apSub ){
   59692               0 :           pRet = 0;
   59693                 :         }else{
   59694            2103 :           p->apSub[p->nSub++] = pRet->p4.pProgram;
   59695                 :         }
   59696                 :       }
   59697                 :     }
   59698                 :   }
   59699                 : 
   59700         1276874 :   return pRet;
   59701                 : }
   59702                 : 
   59703                 : /*
   59704                 : ** Check if the program stored in the VM associated with pParse may
   59705                 : ** throw an ABORT exception (causing the statement, but not entire transaction
   59706                 : ** to be rolled back). This condition is true if the main program or any
   59707                 : ** sub-programs contains any of the following:
   59708                 : **
   59709                 : **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
   59710                 : **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
   59711                 : **   *  OP_Destroy
   59712                 : **   *  OP_VUpdate
   59713                 : **   *  OP_VRename
   59714                 : **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
   59715                 : **
   59716                 : ** Then check that the value of Parse.mayAbort is true if an
   59717                 : ** ABORT may be thrown, or false otherwise. Return true if it does
   59718                 : ** match, or false otherwise. This function is intended to be used as
   59719                 : ** part of an assert statement in the compiler. Similar to:
   59720                 : **
   59721                 : **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
   59722                 : */
   59723           24231 : SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
   59724           24231 :   int hasAbort = 0;
   59725                 :   Op *pOp;
   59726                 :   VdbeOpIter sIter;
   59727           24231 :   memset(&sIter, 0, sizeof(sIter));
   59728           24231 :   sIter.v = v;
   59729                 : 
   59730         1301105 :   while( (pOp = opIterNext(&sIter))!=0 ){
   59731         1257047 :     int opcode = pOp->opcode;
   59732         1257047 :     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 
   59733                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   59734         1256983 :      || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) 
   59735                 : #endif
   59736         1256941 :      || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 
   59737            5910 :       && (pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
   59738                 :     ){
   59739            4404 :       hasAbort = 1;
   59740            4404 :       break;
   59741                 :     }
   59742                 :   }
   59743           24231 :   sqlite3DbFree(v->db, sIter.apSub);
   59744                 : 
   59745                 :   /* Return true if hasAbort==mayAbort. Or if a malloc failure occured.
   59746                 :   ** If malloc failed, then the while() loop above may not have iterated
   59747                 :   ** through all opcodes and hasAbort may be set incorrectly. Return
   59748                 :   ** true for this case to prevent the assert() in the callers frame
   59749                 :   ** from failing.  */
   59750           24231 :   return ( v->db->mallocFailed || hasAbort==mayAbort );
   59751                 : }
   59752                 : #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
   59753                 : 
   59754                 : /*
   59755                 : ** Loop through the program looking for P2 values that are negative
   59756                 : ** on jump instructions.  Each such value is a label.  Resolve the
   59757                 : ** label by setting the P2 value to its correct non-zero value.
   59758                 : **
   59759                 : ** This routine is called once after all opcodes have been inserted.
   59760                 : **
   59761                 : ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
   59762                 : ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
   59763                 : ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
   59764                 : **
   59765                 : ** The Op.opflags field is set on all opcodes.
   59766                 : */
   59767          176017 : static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
   59768                 :   int i;
   59769          176017 :   int nMaxArgs = *pMaxFuncArgs;
   59770                 :   Op *pOp;
   59771          176017 :   int *aLabel = p->aLabel;
   59772          176017 :   p->readOnly = 1;
   59773         3464950 :   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
   59774         3288933 :     u8 opcode = pOp->opcode;
   59775                 : 
   59776         3288933 :     pOp->opflags = sqlite3OpcodeProperty[opcode];
   59777         3288933 :     if( opcode==OP_Function || opcode==OP_AggStep ){
   59778           15853 :       if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
   59779         3273080 :     }else if( (opcode==OP_Transaction && pOp->p2!=0) || opcode==OP_Vacuum ){
   59780           42669 :       p->readOnly = 0;
   59781                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   59782         3230411 :     }else if( opcode==OP_VUpdate ){
   59783               4 :       if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
   59784         3230407 :     }else if( opcode==OP_VFilter ){
   59785                 :       int n;
   59786              54 :       assert( p->nOp - i >= 3 );
   59787              54 :       assert( pOp[-1].opcode==OP_Integer );
   59788              54 :       n = pOp[-1].p1;
   59789              54 :       if( n>nMaxArgs ) nMaxArgs = n;
   59790                 : #endif
   59791         3230353 :     }else if( opcode==OP_Next || opcode==OP_SorterNext ){
   59792           78991 :       pOp->p4.xAdvance = sqlite3BtreeNext;
   59793           78991 :       pOp->p4type = P4_ADVANCE;
   59794         3151362 :     }else if( opcode==OP_Prev ){
   59795            1959 :       pOp->p4.xAdvance = sqlite3BtreePrevious;
   59796            1959 :       pOp->p4type = P4_ADVANCE;
   59797                 :     }
   59798                 : 
   59799         3288933 :     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
   59800          266864 :       assert( -1-pOp->p2<p->nLabel );
   59801          266864 :       pOp->p2 = aLabel[-1-pOp->p2];
   59802                 :     }
   59803                 :   }
   59804          176017 :   sqlite3DbFree(p->db, p->aLabel);
   59805          176017 :   p->aLabel = 0;
   59806                 : 
   59807          176017 :   *pMaxFuncArgs = nMaxArgs;
   59808          176017 : }
   59809                 : 
   59810                 : /*
   59811                 : ** Return the address of the next instruction to be inserted.
   59812                 : */
   59813          229962 : SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
   59814          229962 :   assert( p->magic==VDBE_MAGIC_INIT );
   59815          229962 :   return p->nOp;
   59816                 : }
   59817                 : 
   59818                 : /*
   59819                 : ** This function returns a pointer to the array of opcodes associated with
   59820                 : ** the Vdbe passed as the first argument. It is the callers responsibility
   59821                 : ** to arrange for the returned array to be eventually freed using the 
   59822                 : ** vdbeFreeOpArray() function.
   59823                 : **
   59824                 : ** Before returning, *pnOp is set to the number of entries in the returned
   59825                 : ** array. Also, *pnMaxArg is set to the larger of its current value and 
   59826                 : ** the number of entries in the Vdbe.apArg[] array required to execute the 
   59827                 : ** returned program.
   59828                 : */
   59829            3097 : SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
   59830            3097 :   VdbeOp *aOp = p->aOp;
   59831            3097 :   assert( aOp && !p->db->mallocFailed );
   59832                 : 
   59833                 :   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
   59834            3097 :   assert( p->btreeMask==0 );
   59835                 : 
   59836            3097 :   resolveP2Values(p, pnMaxArg);
   59837            3097 :   *pnOp = p->nOp;
   59838            3097 :   p->aOp = 0;
   59839            3097 :   return aOp;
   59840                 : }
   59841                 : 
   59842                 : /*
   59843                 : ** Add a whole list of operations to the operation stack.  Return the
   59844                 : ** address of the first operation added.
   59845                 : */
   59846            4340 : SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
   59847                 :   int addr;
   59848            4340 :   assert( p->magic==VDBE_MAGIC_INIT );
   59849            4340 :   if( p->nOp + nOp > p->nOpAlloc && growOpArray(p) ){
   59850               0 :     return 0;
   59851                 :   }
   59852            4340 :   addr = p->nOp;
   59853            4340 :   if( ALWAYS(nOp>0) ){
   59854                 :     int i;
   59855            4340 :     VdbeOpList const *pIn = aOp;
   59856           26272 :     for(i=0; i<nOp; i++, pIn++){
   59857           21932 :       int p2 = pIn->p2;
   59858           21932 :       VdbeOp *pOut = &p->aOp[i+addr];
   59859           21932 :       pOut->opcode = pIn->opcode;
   59860           21932 :       pOut->p1 = pIn->p1;
   59861           21932 :       if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
   59862             204 :         pOut->p2 = addr + ADDR(p2);
   59863                 :       }else{
   59864           21728 :         pOut->p2 = p2;
   59865                 :       }
   59866           21932 :       pOut->p3 = pIn->p3;
   59867           21932 :       pOut->p4type = P4_NOTUSED;
   59868           21932 :       pOut->p4.p = 0;
   59869           21932 :       pOut->p5 = 0;
   59870                 : #ifdef SQLITE_DEBUG
   59871           21932 :       pOut->zComment = 0;
   59872           21932 :       if( sqlite3VdbeAddopTrace ){
   59873               0 :         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
   59874                 :       }
   59875                 : #endif
   59876                 :     }
   59877            4340 :     p->nOp += nOp;
   59878                 :   }
   59879            4340 :   return addr;
   59880                 : }
   59881                 : 
   59882                 : /*
   59883                 : ** Change the value of the P1 operand for a specific instruction.
   59884                 : ** This routine is useful when a large program is loaded from a
   59885                 : ** static array using sqlite3VdbeAddOpList but we want to make a
   59886                 : ** few minor changes to the program.
   59887                 : */
   59888            8789 : SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
   59889            8789 :   assert( p!=0 );
   59890            8789 :   if( ((u32)p->nOp)>addr ){
   59891            8789 :     p->aOp[addr].p1 = val;
   59892                 :   }
   59893            8789 : }
   59894                 : 
   59895                 : /*
   59896                 : ** Change the value of the P2 operand for a specific instruction.
   59897                 : ** This routine is useful for setting a jump destination.
   59898                 : */
   59899          202887 : SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
   59900          202887 :   assert( p!=0 );
   59901          202887 :   if( ((u32)p->nOp)>addr ){
   59902          202887 :     p->aOp[addr].p2 = val;
   59903                 :   }
   59904          202887 : }
   59905                 : 
   59906                 : /*
   59907                 : ** Change the value of the P3 operand for a specific instruction.
   59908                 : */
   59909            1953 : SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
   59910            1953 :   assert( p!=0 );
   59911            1953 :   if( ((u32)p->nOp)>addr ){
   59912            1953 :     p->aOp[addr].p3 = val;
   59913                 :   }
   59914            1953 : }
   59915                 : 
   59916                 : /*
   59917                 : ** Change the value of the P5 operand for the most recently
   59918                 : ** added operation.
   59919                 : */
   59920          232474 : SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
   59921          232474 :   assert( p!=0 );
   59922          232474 :   if( p->aOp ){
   59923          232474 :     assert( p->nOp>0 );
   59924          232474 :     p->aOp[p->nOp-1].p5 = val;
   59925                 :   }
   59926          232474 : }
   59927                 : 
   59928                 : /*
   59929                 : ** Change the P2 operand of instruction addr so that it points to
   59930                 : ** the address of the next instruction to be coded.
   59931                 : */
   59932          200395 : SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
   59933          200395 :   assert( addr>=0 || p->db->mallocFailed );
   59934          200395 :   if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
   59935          200395 : }
   59936                 : 
   59937                 : 
   59938                 : /*
   59939                 : ** If the input FuncDef structure is ephemeral, then free it.  If
   59940                 : ** the FuncDef is not ephermal, then do nothing.
   59941                 : */
   59942           19692 : static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
   59943           19692 :   if( ALWAYS(pDef) && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
   59944               0 :     sqlite3DbFree(db, pDef);
   59945                 :   }
   59946           19692 : }
   59947                 : 
   59948                 : static void vdbeFreeOpArray(sqlite3 *, Op *, int);
   59949                 : 
   59950                 : /*
   59951                 : ** Delete a P4 value if necessary.
   59952                 : */
   59953         4224528 : static void freeP4(sqlite3 *db, int p4type, void *p4){
   59954         4224528 :   if( p4 ){
   59955          984999 :     assert( db );
   59956          984999 :     switch( p4type ){
   59957                 :       case P4_REAL:
   59958                 :       case P4_INT64:
   59959                 :       case P4_DYNAMIC:
   59960                 :       case P4_KEYINFO:
   59961                 :       case P4_INTARRAY:
   59962                 :       case P4_KEYINFO_HANDOFF: {
   59963          381254 :         sqlite3DbFree(db, p4);
   59964          381254 :         break;
   59965                 :       }
   59966                 :       case P4_MPRINTF: {
   59967               0 :         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
   59968               0 :         break;
   59969                 :       }
   59970                 :       case P4_VDBEFUNC: {
   59971               0 :         VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
   59972               0 :         freeEphemeralFunction(db, pVdbeFunc->pFunc);
   59973               0 :         if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
   59974               0 :         sqlite3DbFree(db, pVdbeFunc);
   59975               0 :         break;
   59976                 :       }
   59977                 :       case P4_FUNCDEF: {
   59978           19692 :         freeEphemeralFunction(db, (FuncDef*)p4);
   59979           19692 :         break;
   59980                 :       }
   59981                 :       case P4_MEM: {
   59982           19889 :         if( db->pnBytesFreed==0 ){
   59983           19889 :           sqlite3ValueFree((sqlite3_value*)p4);
   59984                 :         }else{
   59985               0 :           Mem *p = (Mem*)p4;
   59986               0 :           sqlite3DbFree(db, p->zMalloc);
   59987               0 :           sqlite3DbFree(db, p);
   59988                 :         }
   59989           19889 :         break;
   59990                 :       }
   59991                 :       case P4_VTAB : {
   59992              62 :         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
   59993              62 :         break;
   59994                 :       }
   59995                 :     }
   59996                 :   }
   59997         4224528 : }
   59998                 : 
   59999                 : /*
   60000                 : ** Free the space allocated for aOp and any p4 values allocated for the
   60001                 : ** opcodes contained within. If aOp is not NULL it is assumed to contain 
   60002                 : ** nOp entries. 
   60003                 : */
   60004          179204 : static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
   60005          179204 :   if( aOp ){
   60006                 :     Op *pOp;
   60007         3467243 :     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
   60008         3291147 :       freeP4(db, pOp->p4type, pOp->p4.p);
   60009                 : #ifdef SQLITE_DEBUG
   60010         3291147 :       sqlite3DbFree(db, pOp->zComment);
   60011                 : #endif     
   60012                 :     }
   60013                 :   }
   60014          179203 :   sqlite3DbFree(db, aOp);
   60015          179204 : }
   60016                 : 
   60017                 : /*
   60018                 : ** Link the SubProgram object passed as the second argument into the linked
   60019                 : ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
   60020                 : ** objects when the VM is no longer required.
   60021                 : */
   60022            3097 : SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
   60023            3097 :   p->pNext = pVdbe->pProgram;
   60024            3097 :   pVdbe->pProgram = p;
   60025            3097 : }
   60026                 : 
   60027                 : /*
   60028                 : ** Change the opcode at addr into OP_Noop
   60029                 : */
   60030           22067 : SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
   60031           22067 :   if( p->aOp ){
   60032           22067 :     VdbeOp *pOp = &p->aOp[addr];
   60033           22067 :     sqlite3 *db = p->db;
   60034           22067 :     freeP4(db, pOp->p4type, pOp->p4.p);
   60035           22067 :     memset(pOp, 0, sizeof(pOp[0]));
   60036           22067 :     pOp->opcode = OP_Noop;
   60037                 :   }
   60038           22067 : }
   60039                 : 
   60040                 : /*
   60041                 : ** Change the value of the P4 operand for a specific instruction.
   60042                 : ** This routine is useful when a large program is loaded from a
   60043                 : ** static array using sqlite3VdbeAddOpList but we want to make a
   60044                 : ** few minor changes to the program.
   60045                 : **
   60046                 : ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
   60047                 : ** the string is made into memory obtained from sqlite3_malloc().
   60048                 : ** A value of n==0 means copy bytes of zP4 up to and including the
   60049                 : ** first null byte.  If n>0 then copy n+1 bytes of zP4.
   60050                 : **
   60051                 : ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure.
   60052                 : ** A copy is made of the KeyInfo structure into memory obtained from
   60053                 : ** sqlite3_malloc, to be freed when the Vdbe is finalized.
   60054                 : ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure
   60055                 : ** stored in memory that the caller has obtained from sqlite3_malloc. The 
   60056                 : ** caller should not free the allocation, it will be freed when the Vdbe is
   60057                 : ** finalized.
   60058                 : ** 
   60059                 : ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
   60060                 : ** to a string or structure that is guaranteed to exist for the lifetime of
   60061                 : ** the Vdbe. In these cases we can just copy the pointer.
   60062                 : **
   60063                 : ** If addr<0 then change P4 on the most recently inserted instruction.
   60064                 : */
   60065          911314 : SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
   60066                 :   Op *pOp;
   60067                 :   sqlite3 *db;
   60068          911314 :   assert( p!=0 );
   60069          911314 :   db = p->db;
   60070          911314 :   assert( p->magic==VDBE_MAGIC_INIT );
   60071          911314 :   if( p->aOp==0 || db->mallocFailed ){
   60072               0 :     if ( n!=P4_KEYINFO && n!=P4_VTAB ) {
   60073               0 :       freeP4(db, n, (void*)*(char**)&zP4);
   60074                 :     }
   60075               0 :     return;
   60076                 :   }
   60077          911314 :   assert( p->nOp>0 );
   60078          911314 :   assert( addr<p->nOp );
   60079          911314 :   if( addr<0 ){
   60080          343011 :     addr = p->nOp - 1;
   60081                 :   }
   60082          911314 :   pOp = &p->aOp[addr];
   60083          911314 :   freeP4(db, pOp->p4type, pOp->p4.p);
   60084          911314 :   pOp->p4.p = 0;
   60085          911314 :   if( n==P4_INT32 ){
   60086                 :     /* Note: this cast is safe, because the origin data point was an int
   60087                 :     ** that was cast to a (const char *). */
   60088          241108 :     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
   60089          241108 :     pOp->p4type = P4_INT32;
   60090          670206 :   }else if( zP4==0 ){
   60091            1225 :     pOp->p4.p = 0;
   60092            1225 :     pOp->p4type = P4_NOTUSED;
   60093          668981 :   }else if( n==P4_KEYINFO ){
   60094                 :     KeyInfo *pKeyInfo;
   60095                 :     int nField, nByte;
   60096                 : 
   60097            9793 :     nField = ((KeyInfo*)zP4)->nField;
   60098            9793 :     nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
   60099            9793 :     pKeyInfo = sqlite3DbMallocRaw(0, nByte);
   60100            9793 :     pOp->p4.pKeyInfo = pKeyInfo;
   60101            9793 :     if( pKeyInfo ){
   60102                 :       u8 *aSortOrder;
   60103            9793 :       memcpy((char*)pKeyInfo, zP4, nByte - nField);
   60104            9793 :       aSortOrder = pKeyInfo->aSortOrder;
   60105            9793 :       if( aSortOrder ){
   60106            5716 :         pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
   60107            5716 :         memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
   60108                 :       }
   60109            9793 :       pOp->p4type = P4_KEYINFO;
   60110                 :     }else{
   60111               0 :       p->db->mallocFailed = 1;
   60112               0 :       pOp->p4type = P4_NOTUSED;
   60113                 :     }
   60114          659188 :   }else if( n==P4_KEYINFO_HANDOFF ){
   60115           86906 :     pOp->p4.p = (void*)zP4;
   60116           86906 :     pOp->p4type = P4_KEYINFO;
   60117          572282 :   }else if( n==P4_VTAB ){
   60118              62 :     pOp->p4.p = (void*)zP4;
   60119              62 :     pOp->p4type = P4_VTAB;
   60120              62 :     sqlite3VtabLock((VTable *)zP4);
   60121              62 :     assert( ((VTable *)zP4)->db==p->db );
   60122          572220 :   }else if( n<0 ){
   60123          324004 :     pOp->p4.p = (void*)zP4;
   60124          324004 :     pOp->p4type = (signed char)n;
   60125                 :   }else{
   60126          248216 :     if( n==0 ) n = sqlite3Strlen30(zP4);
   60127          248216 :     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
   60128          248216 :     pOp->p4type = P4_DYNAMIC;
   60129                 :   }
   60130                 : }
   60131                 : 
   60132                 : #ifndef NDEBUG
   60133                 : /*
   60134                 : ** Change the comment on the the most recently coded instruction.  Or
   60135                 : ** insert a No-op and add the comment to that new instruction.  This
   60136                 : ** makes the code easier to read during debugging.  None of this happens
   60137                 : ** in a production build.
   60138                 : */
   60139          687652 : static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
   60140          687652 :   assert( p->nOp>0 || p->aOp==0 );
   60141          687652 :   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
   60142          687652 :   if( p->nOp ){
   60143          687652 :     assert( p->aOp );
   60144          687652 :     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
   60145          687652 :     p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
   60146                 :   }
   60147          687652 : }
   60148          680469 : SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
   60149                 :   va_list ap;
   60150          680469 :   if( p ){
   60151          680469 :     va_start(ap, zFormat);
   60152          680469 :     vdbeVComment(p, zFormat, ap);
   60153          680469 :     va_end(ap);
   60154                 :   }
   60155          680469 : }
   60156            7183 : SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
   60157                 :   va_list ap;
   60158            7183 :   if( p ){
   60159            7183 :     sqlite3VdbeAddOp0(p, OP_Noop);
   60160            7183 :     va_start(ap, zFormat);
   60161            7183 :     vdbeVComment(p, zFormat, ap);
   60162            7183 :     va_end(ap);
   60163                 :   }
   60164            7183 : }
   60165                 : #endif  /* NDEBUG */
   60166                 : 
   60167                 : /*
   60168                 : ** Return the opcode for a given address.  If the address is -1, then
   60169                 : ** return the most recently inserted opcode.
   60170                 : **
   60171                 : ** If a memory allocation error has occurred prior to the calling of this
   60172                 : ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
   60173                 : ** is readable but not writable, though it is cast to a writable value.
   60174                 : ** The return of a dummy opcode allows the call to continue functioning
   60175                 : ** after a OOM fault without having to check to see if the return from 
   60176                 : ** this routine is a valid pointer.  But because the dummy.opcode is 0,
   60177                 : ** dummy will never be written to.  This is verified by code inspection and
   60178                 : ** by running with Valgrind.
   60179                 : **
   60180                 : ** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
   60181                 : ** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
   60182                 : ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
   60183                 : ** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
   60184                 : ** having to double-check to make sure that the result is non-negative. But
   60185                 : ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
   60186                 : ** check the value of p->nOp-1 before continuing.
   60187                 : */
   60188           51050 : SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
   60189                 :   /* C89 specifies that the constant "dummy" will be initialized to all
   60190                 :   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
   60191                 :   static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
   60192           51050 :   assert( p->magic==VDBE_MAGIC_INIT );
   60193           51050 :   if( addr<0 ){
   60194                 : #ifdef SQLITE_OMIT_TRACE
   60195                 :     if( p->nOp==0 ) return (VdbeOp*)&dummy;
   60196                 : #endif
   60197            1982 :     addr = p->nOp - 1;
   60198                 :   }
   60199           51050 :   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
   60200           51050 :   if( p->db->mallocFailed ){
   60201               0 :     return (VdbeOp*)&dummy;
   60202                 :   }else{
   60203           51050 :     return &p->aOp[addr];
   60204                 :   }
   60205                 : }
   60206                 : 
   60207                 : #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
   60208                 :      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
   60209                 : /*
   60210                 : ** Compute a string that describes the P4 parameter for an opcode.
   60211                 : ** Use zTemp for any required temporary buffer space.
   60212                 : */
   60213               0 : static char *displayP4(Op *pOp, char *zTemp, int nTemp){
   60214               0 :   char *zP4 = zTemp;
   60215               0 :   assert( nTemp>=20 );
   60216               0 :   switch( pOp->p4type ){
   60217                 :     case P4_KEYINFO_STATIC:
   60218                 :     case P4_KEYINFO: {
   60219                 :       int i, j;
   60220               0 :       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
   60221               0 :       sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
   60222               0 :       i = sqlite3Strlen30(zTemp);
   60223               0 :       for(j=0; j<pKeyInfo->nField; j++){
   60224               0 :         CollSeq *pColl = pKeyInfo->aColl[j];
   60225               0 :         if( pColl ){
   60226               0 :           int n = sqlite3Strlen30(pColl->zName);
   60227               0 :           if( i+n>nTemp-6 ){
   60228               0 :             memcpy(&zTemp[i],",...",4);
   60229               0 :             break;
   60230                 :           }
   60231               0 :           zTemp[i++] = ',';
   60232               0 :           if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
   60233               0 :             zTemp[i++] = '-';
   60234                 :           }
   60235               0 :           memcpy(&zTemp[i], pColl->zName,n+1);
   60236               0 :           i += n;
   60237               0 :         }else if( i+4<nTemp-6 ){
   60238               0 :           memcpy(&zTemp[i],",nil",4);
   60239               0 :           i += 4;
   60240                 :         }
   60241                 :       }
   60242               0 :       zTemp[i++] = ')';
   60243               0 :       zTemp[i] = 0;
   60244               0 :       assert( i<nTemp );
   60245               0 :       break;
   60246                 :     }
   60247                 :     case P4_COLLSEQ: {
   60248               0 :       CollSeq *pColl = pOp->p4.pColl;
   60249               0 :       sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
   60250               0 :       break;
   60251                 :     }
   60252                 :     case P4_FUNCDEF: {
   60253               0 :       FuncDef *pDef = pOp->p4.pFunc;
   60254               0 :       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
   60255               0 :       break;
   60256                 :     }
   60257                 :     case P4_INT64: {
   60258               0 :       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
   60259               0 :       break;
   60260                 :     }
   60261                 :     case P4_INT32: {
   60262               0 :       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
   60263               0 :       break;
   60264                 :     }
   60265                 :     case P4_REAL: {
   60266               0 :       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
   60267               0 :       break;
   60268                 :     }
   60269                 :     case P4_MEM: {
   60270               0 :       Mem *pMem = pOp->p4.pMem;
   60271               0 :       if( pMem->flags & MEM_Str ){
   60272               0 :         zP4 = pMem->z;
   60273               0 :       }else if( pMem->flags & MEM_Int ){
   60274               0 :         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
   60275               0 :       }else if( pMem->flags & MEM_Real ){
   60276               0 :         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
   60277               0 :       }else if( pMem->flags & MEM_Null ){
   60278               0 :         sqlite3_snprintf(nTemp, zTemp, "NULL");
   60279                 :       }else{
   60280               0 :         assert( pMem->flags & MEM_Blob );
   60281               0 :         zP4 = "(blob)";
   60282                 :       }
   60283               0 :       break;
   60284                 :     }
   60285                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   60286                 :     case P4_VTAB: {
   60287               0 :       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
   60288               0 :       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
   60289               0 :       break;
   60290                 :     }
   60291                 : #endif
   60292                 :     case P4_INTARRAY: {
   60293               0 :       sqlite3_snprintf(nTemp, zTemp, "intarray");
   60294               0 :       break;
   60295                 :     }
   60296                 :     case P4_SUBPROGRAM: {
   60297               0 :       sqlite3_snprintf(nTemp, zTemp, "program");
   60298               0 :       break;
   60299                 :     }
   60300                 :     case P4_ADVANCE: {
   60301               0 :       zTemp[0] = 0;
   60302               0 :       break;
   60303                 :     }
   60304                 :     default: {
   60305               0 :       zP4 = pOp->p4.z;
   60306               0 :       if( zP4==0 ){
   60307               0 :         zP4 = zTemp;
   60308               0 :         zTemp[0] = 0;
   60309                 :       }
   60310                 :     }
   60311                 :   }
   60312               0 :   assert( zP4!=0 );
   60313               0 :   return zP4;
   60314                 : }
   60315                 : #endif
   60316                 : 
   60317                 : /*
   60318                 : ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
   60319                 : **
   60320                 : ** The prepared statements need to know in advance the complete set of
   60321                 : ** attached databases that will be use.  A mask of these databases
   60322                 : ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
   60323                 : ** p->btreeMask of databases that will require a lock.
   60324                 : */
   60325          129640 : SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
   60326          129640 :   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
   60327          129640 :   assert( i<(int)sizeof(p->btreeMask)*8 );
   60328          129640 :   p->btreeMask |= ((yDbMask)1)<<i;
   60329          129640 :   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
   60330          104442 :     p->lockMask |= ((yDbMask)1)<<i;
   60331                 :   }
   60332          129640 : }
   60333                 : 
   60334                 : #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   60335                 : /*
   60336                 : ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
   60337                 : ** this routine obtains the mutex associated with each BtShared structure
   60338                 : ** that may be accessed by the VM passed as an argument. In doing so it also
   60339                 : ** sets the BtShared.db member of each of the BtShared structures, ensuring
   60340                 : ** that the correct busy-handler callback is invoked if required.
   60341                 : **
   60342                 : ** If SQLite is not threadsafe but does support shared-cache mode, then
   60343                 : ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
   60344                 : ** of all of BtShared structures accessible via the database handle 
   60345                 : ** associated with the VM.
   60346                 : **
   60347                 : ** If SQLite is not threadsafe and does not support shared-cache mode, this
   60348                 : ** function is a no-op.
   60349                 : **
   60350                 : ** The p->btreeMask field is a bitmask of all btrees that the prepared 
   60351                 : ** statement p will ever use.  Let N be the number of bits in p->btreeMask
   60352                 : ** corresponding to btrees that use shared cache.  Then the runtime of
   60353                 : ** this routine is N*N.  But as N is rarely more than 1, this should not
   60354                 : ** be a problem.
   60355                 : */
   60356          585405 : SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
   60357                 :   int i;
   60358                 :   yDbMask mask;
   60359                 :   sqlite3 *db;
   60360                 :   Db *aDb;
   60361                 :   int nDb;
   60362          585405 :   if( p->lockMask==0 ) return;  /* The common case */
   60363          476642 :   db = p->db;
   60364          476642 :   aDb = db->aDb;
   60365          476642 :   nDb = db->nDb;
   60366         1430174 :   for(i=0, mask=1; i<nDb; i++, mask += mask){
   60367          953531 :     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
   60368          476642 :       sqlite3BtreeEnter(aDb[i].pBt);
   60369                 :     }
   60370                 :   }
   60371                 : }
   60372                 : #endif
   60373                 : 
   60374                 : #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   60375                 : /*
   60376                 : ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
   60377                 : */
   60378          585403 : SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
   60379                 :   int i;
   60380                 :   yDbMask mask;
   60381                 :   sqlite3 *db;
   60382                 :   Db *aDb;
   60383                 :   int nDb;
   60384          585403 :   if( p->lockMask==0 ) return;  /* The common case */
   60385          476640 :   db = p->db;
   60386          476640 :   aDb = db->aDb;
   60387          476640 :   nDb = db->nDb;
   60388         1430172 :   for(i=0, mask=1; i<nDb; i++, mask += mask){
   60389          953528 :     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
   60390          476640 :       sqlite3BtreeLeave(aDb[i].pBt);
   60391                 :     }
   60392                 :   }
   60393                 : }
   60394                 : #endif
   60395                 : 
   60396                 : #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
   60397                 : /*
   60398                 : ** Print a single opcode.  This routine is used for debugging only.
   60399                 : */
   60400               0 : SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
   60401                 :   char *zP4;
   60402                 :   char zPtr[50];
   60403                 :   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n";
   60404               0 :   if( pOut==0 ) pOut = stdout;
   60405               0 :   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
   60406               0 :   fprintf(pOut, zFormat1, pc, 
   60407               0 :       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
   60408                 : #ifdef SQLITE_DEBUG
   60409               0 :       pOp->zComment ? pOp->zComment : ""
   60410                 : #else
   60411                 :       ""
   60412                 : #endif
   60413                 :   );
   60414               0 :   fflush(pOut);
   60415               0 : }
   60416                 : #endif
   60417                 : 
   60418                 : /*
   60419                 : ** Release an array of N Mem elements
   60420                 : */
   60421         1021495 : static void releaseMemArray(Mem *p, int N){
   60422         1021495 :   if( p && N ){
   60423                 :     Mem *pEnd;
   60424          573975 :     sqlite3 *db = p->db;
   60425          573975 :     u8 malloc_failed = db->mallocFailed;
   60426          573975 :     if( db->pnBytesFreed ){
   60427             867 :       for(pEnd=&p[N]; p<pEnd; p++){
   60428             813 :         sqlite3DbFree(db, p->zMalloc);
   60429                 :       }
   60430              54 :       return;
   60431                 :     }
   60432        10279187 :     for(pEnd=&p[N]; p<pEnd; p++){
   60433         9705264 :       assert( (&p[1])==pEnd || p[0].db==p[1].db );
   60434                 : 
   60435                 :       /* This block is really an inlined version of sqlite3VdbeMemRelease()
   60436                 :       ** that takes advantage of the fact that the memory cell value is 
   60437                 :       ** being set to NULL after releasing any dynamic resources.
   60438                 :       **
   60439                 :       ** The justification for duplicating code is that according to 
   60440                 :       ** callgrind, this causes a certain test case to hit the CPU 4.7 
   60441                 :       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 
   60442                 :       ** sqlite3MemRelease() were called from here. With -O2, this jumps
   60443                 :       ** to 6.6 percent. The test case is inserting 1000 rows into a table 
   60444                 :       ** with no indexes using a single prepared INSERT statement, bind() 
   60445                 :       ** and reset(). Inserts are grouped into a transaction.
   60446                 :       */
   60447         9705265 :       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
   60448          272908 :         sqlite3VdbeMemRelease(p);
   60449         9432357 :       }else if( p->zMalloc ){
   60450         1296242 :         sqlite3DbFree(db, p->zMalloc);
   60451         1296241 :         p->zMalloc = 0;
   60452                 :       }
   60453                 : 
   60454         9705266 :       p->flags = MEM_Invalid;
   60455                 :     }
   60456          573923 :     db->mallocFailed = malloc_failed;
   60457                 :   }
   60458                 : }
   60459                 : 
   60460                 : /*
   60461                 : ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
   60462                 : ** allocated by the OP_Program opcode in sqlite3VdbeExec().
   60463                 : */
   60464           14229 : SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
   60465                 :   int i;
   60466           14229 :   Mem *aMem = VdbeFrameMem(p);
   60467           14229 :   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
   60468          103055 :   for(i=0; i<p->nChildCsr; i++){
   60469           88826 :     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
   60470                 :   }
   60471           14229 :   releaseMemArray(aMem, p->nChildMem);
   60472           14229 :   sqlite3DbFree(p->v->db, p);
   60473           14229 : }
   60474                 : 
   60475                 : #ifndef SQLITE_OMIT_EXPLAIN
   60476                 : /*
   60477                 : ** Give a listing of the program in the virtual machine.
   60478                 : **
   60479                 : ** The interface is the same as sqlite3VdbeExec().  But instead of
   60480                 : ** running the code, it invokes the callback once for each instruction.
   60481                 : ** This feature is used to implement "EXPLAIN".
   60482                 : **
   60483                 : ** When p->explain==1, each instruction is listed.  When
   60484                 : ** p->explain==2, only OP_Explain instructions are listed and these
   60485                 : ** are shown in a different format.  p->explain==2 is used to implement
   60486                 : ** EXPLAIN QUERY PLAN.
   60487                 : **
   60488                 : ** When p->explain==1, first the main program is listed, then each of
   60489                 : ** the trigger subprograms are listed one by one.
   60490                 : */
   60491               0 : SQLITE_PRIVATE int sqlite3VdbeList(
   60492                 :   Vdbe *p                   /* The VDBE */
   60493                 : ){
   60494                 :   int nRow;                            /* Stop when row count reaches this */
   60495               0 :   int nSub = 0;                        /* Number of sub-vdbes seen so far */
   60496               0 :   SubProgram **apSub = 0;              /* Array of sub-vdbes */
   60497               0 :   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
   60498               0 :   sqlite3 *db = p->db;                 /* The database connection */
   60499                 :   int i;                               /* Loop counter */
   60500               0 :   int rc = SQLITE_OK;                  /* Return code */
   60501               0 :   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
   60502                 : 
   60503               0 :   assert( p->explain );
   60504               0 :   assert( p->magic==VDBE_MAGIC_RUN );
   60505               0 :   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
   60506                 : 
   60507                 :   /* Even though this opcode does not use dynamic strings for
   60508                 :   ** the result, result columns may become dynamic if the user calls
   60509                 :   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
   60510                 :   */
   60511               0 :   releaseMemArray(pMem, 8);
   60512               0 :   p->pResultSet = 0;
   60513                 : 
   60514               0 :   if( p->rc==SQLITE_NOMEM ){
   60515                 :     /* This happens if a malloc() inside a call to sqlite3_column_text() or
   60516                 :     ** sqlite3_column_text16() failed.  */
   60517               0 :     db->mallocFailed = 1;
   60518               0 :     return SQLITE_ERROR;
   60519                 :   }
   60520                 : 
   60521                 :   /* When the number of output rows reaches nRow, that means the
   60522                 :   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
   60523                 :   ** nRow is the sum of the number of rows in the main program, plus
   60524                 :   ** the sum of the number of rows in all trigger subprograms encountered
   60525                 :   ** so far.  The nRow value will increase as new trigger subprograms are
   60526                 :   ** encountered, but p->pc will eventually catch up to nRow.
   60527                 :   */
   60528               0 :   nRow = p->nOp;
   60529               0 :   if( p->explain==1 ){
   60530                 :     /* The first 8 memory cells are used for the result set.  So we will
   60531                 :     ** commandeer the 9th cell to use as storage for an array of pointers
   60532                 :     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
   60533                 :     ** cells.  */
   60534               0 :     assert( p->nMem>9 );
   60535               0 :     pSub = &p->aMem[9];
   60536               0 :     if( pSub->flags&MEM_Blob ){
   60537                 :       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
   60538                 :       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
   60539               0 :       nSub = pSub->n/sizeof(Vdbe*);
   60540               0 :       apSub = (SubProgram **)pSub->z;
   60541                 :     }
   60542               0 :     for(i=0; i<nSub; i++){
   60543               0 :       nRow += apSub[i]->nOp;
   60544                 :     }
   60545                 :   }
   60546                 : 
   60547                 :   do{
   60548               0 :     i = p->pc++;
   60549               0 :   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
   60550               0 :   if( i>=nRow ){
   60551               0 :     p->rc = SQLITE_OK;
   60552               0 :     rc = SQLITE_DONE;
   60553               0 :   }else if( db->u1.isInterrupted ){
   60554               0 :     p->rc = SQLITE_INTERRUPT;
   60555               0 :     rc = SQLITE_ERROR;
   60556               0 :     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
   60557                 :   }else{
   60558                 :     char *z;
   60559                 :     Op *pOp;
   60560               0 :     if( i<p->nOp ){
   60561                 :       /* The output line number is small enough that we are still in the
   60562                 :       ** main program. */
   60563               0 :       pOp = &p->aOp[i];
   60564                 :     }else{
   60565                 :       /* We are currently listing subprograms.  Figure out which one and
   60566                 :       ** pick up the appropriate opcode. */
   60567                 :       int j;
   60568               0 :       i -= p->nOp;
   60569               0 :       for(j=0; i>=apSub[j]->nOp; j++){
   60570               0 :         i -= apSub[j]->nOp;
   60571                 :       }
   60572               0 :       pOp = &apSub[j]->aOp[i];
   60573                 :     }
   60574               0 :     if( p->explain==1 ){
   60575               0 :       pMem->flags = MEM_Int;
   60576               0 :       pMem->type = SQLITE_INTEGER;
   60577               0 :       pMem->u.i = i;                                /* Program counter */
   60578               0 :       pMem++;
   60579                 :   
   60580               0 :       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
   60581               0 :       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
   60582               0 :       assert( pMem->z!=0 );
   60583               0 :       pMem->n = sqlite3Strlen30(pMem->z);
   60584               0 :       pMem->type = SQLITE_TEXT;
   60585               0 :       pMem->enc = SQLITE_UTF8;
   60586               0 :       pMem++;
   60587                 : 
   60588                 :       /* When an OP_Program opcode is encounter (the only opcode that has
   60589                 :       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
   60590                 :       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
   60591                 :       ** has not already been seen.
   60592                 :       */
   60593               0 :       if( pOp->p4type==P4_SUBPROGRAM ){
   60594               0 :         int nByte = (nSub+1)*sizeof(SubProgram*);
   60595                 :         int j;
   60596               0 :         for(j=0; j<nSub; j++){
   60597               0 :           if( apSub[j]==pOp->p4.pProgram ) break;
   60598                 :         }
   60599               0 :         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, 1) ){
   60600               0 :           apSub = (SubProgram **)pSub->z;
   60601               0 :           apSub[nSub++] = pOp->p4.pProgram;
   60602               0 :           pSub->flags |= MEM_Blob;
   60603               0 :           pSub->n = nSub*sizeof(SubProgram*);
   60604                 :         }
   60605                 :       }
   60606                 :     }
   60607                 : 
   60608               0 :     pMem->flags = MEM_Int;
   60609               0 :     pMem->u.i = pOp->p1;                          /* P1 */
   60610               0 :     pMem->type = SQLITE_INTEGER;
   60611               0 :     pMem++;
   60612                 : 
   60613               0 :     pMem->flags = MEM_Int;
   60614               0 :     pMem->u.i = pOp->p2;                          /* P2 */
   60615               0 :     pMem->type = SQLITE_INTEGER;
   60616               0 :     pMem++;
   60617                 : 
   60618               0 :     pMem->flags = MEM_Int;
   60619               0 :     pMem->u.i = pOp->p3;                          /* P3 */
   60620               0 :     pMem->type = SQLITE_INTEGER;
   60621               0 :     pMem++;
   60622                 : 
   60623               0 :     if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
   60624               0 :       assert( p->db->mallocFailed );
   60625               0 :       return SQLITE_ERROR;
   60626                 :     }
   60627               0 :     pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
   60628               0 :     z = displayP4(pOp, pMem->z, 32);
   60629               0 :     if( z!=pMem->z ){
   60630               0 :       sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0);
   60631                 :     }else{
   60632               0 :       assert( pMem->z!=0 );
   60633               0 :       pMem->n = sqlite3Strlen30(pMem->z);
   60634               0 :       pMem->enc = SQLITE_UTF8;
   60635                 :     }
   60636               0 :     pMem->type = SQLITE_TEXT;
   60637               0 :     pMem++;
   60638                 : 
   60639               0 :     if( p->explain==1 ){
   60640               0 :       if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
   60641               0 :         assert( p->db->mallocFailed );
   60642               0 :         return SQLITE_ERROR;
   60643                 :       }
   60644               0 :       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
   60645               0 :       pMem->n = 2;
   60646               0 :       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
   60647               0 :       pMem->type = SQLITE_TEXT;
   60648               0 :       pMem->enc = SQLITE_UTF8;
   60649               0 :       pMem++;
   60650                 :   
   60651                 : #ifdef SQLITE_DEBUG
   60652               0 :       if( pOp->zComment ){
   60653               0 :         pMem->flags = MEM_Str|MEM_Term;
   60654               0 :         pMem->z = pOp->zComment;
   60655               0 :         pMem->n = sqlite3Strlen30(pMem->z);
   60656               0 :         pMem->enc = SQLITE_UTF8;
   60657               0 :         pMem->type = SQLITE_TEXT;
   60658                 :       }else
   60659                 : #endif
   60660                 :       {
   60661               0 :         pMem->flags = MEM_Null;                       /* Comment */
   60662               0 :         pMem->type = SQLITE_NULL;
   60663                 :       }
   60664                 :     }
   60665                 : 
   60666               0 :     p->nResColumn = 8 - 4*(p->explain-1);
   60667               0 :     p->pResultSet = &p->aMem[1];
   60668               0 :     p->rc = SQLITE_OK;
   60669               0 :     rc = SQLITE_ROW;
   60670                 :   }
   60671               0 :   return rc;
   60672                 : }
   60673                 : #endif /* SQLITE_OMIT_EXPLAIN */
   60674                 : 
   60675                 : #ifdef SQLITE_DEBUG
   60676                 : /*
   60677                 : ** Print the SQL that was used to generate a VDBE program.
   60678                 : */
   60679               0 : SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
   60680               0 :   int nOp = p->nOp;
   60681                 :   VdbeOp *pOp;
   60682               0 :   if( nOp<1 ) return;
   60683               0 :   pOp = &p->aOp[0];
   60684               0 :   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
   60685               0 :     const char *z = pOp->p4.z;
   60686               0 :     while( sqlite3Isspace(*z) ) z++;
   60687               0 :     printf("SQL: [%s]\n", z);
   60688                 :   }
   60689                 : }
   60690                 : #endif
   60691                 : 
   60692                 : #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
   60693                 : /*
   60694                 : ** Print an IOTRACE message showing SQL content.
   60695                 : */
   60696                 : SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
   60697                 :   int nOp = p->nOp;
   60698                 :   VdbeOp *pOp;
   60699                 :   if( sqlite3IoTrace==0 ) return;
   60700                 :   if( nOp<1 ) return;
   60701                 :   pOp = &p->aOp[0];
   60702                 :   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
   60703                 :     int i, j;
   60704                 :     char z[1000];
   60705                 :     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
   60706                 :     for(i=0; sqlite3Isspace(z[i]); i++){}
   60707                 :     for(j=0; z[i]; i++){
   60708                 :       if( sqlite3Isspace(z[i]) ){
   60709                 :         if( z[i-1]!=' ' ){
   60710                 :           z[j++] = ' ';
   60711                 :         }
   60712                 :       }else{
   60713                 :         z[j++] = z[i];
   60714                 :       }
   60715                 :     }
   60716                 :     z[j] = 0;
   60717                 :     sqlite3IoTrace("SQL %s\n", z);
   60718                 :   }
   60719                 : }
   60720                 : #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
   60721                 : 
   60722                 : /*
   60723                 : ** Allocate space from a fixed size buffer and return a pointer to
   60724                 : ** that space.  If insufficient space is available, return NULL.
   60725                 : **
   60726                 : ** The pBuf parameter is the initial value of a pointer which will
   60727                 : ** receive the new memory.  pBuf is normally NULL.  If pBuf is not
   60728                 : ** NULL, it means that memory space has already been allocated and that
   60729                 : ** this routine should not allocate any new memory.  When pBuf is not
   60730                 : ** NULL simply return pBuf.  Only allocate new memory space when pBuf
   60731                 : ** is NULL.
   60732                 : **
   60733                 : ** nByte is the number of bytes of space needed.
   60734                 : **
   60735                 : ** *ppFrom points to available space and pEnd points to the end of the
   60736                 : ** available space.  When space is allocated, *ppFrom is advanced past
   60737                 : ** the end of the allocated space.
   60738                 : **
   60739                 : ** *pnByte is a counter of the number of bytes of space that have failed
   60740                 : ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
   60741                 : ** request, then increment *pnByte by the amount of the request.
   60742                 : */
   60743         1272588 : static void *allocSpace(
   60744                 :   void *pBuf,          /* Where return pointer will be stored */
   60745                 :   int nByte,           /* Number of bytes to allocate */
   60746                 :   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
   60747                 :   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
   60748                 :   int *pnByte          /* If allocation cannot be made, increment *pnByte */
   60749                 : ){
   60750         1272588 :   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
   60751         1272588 :   if( pBuf ) return pBuf;
   60752         1095101 :   nByte = ROUND8(nByte);
   60753         1095101 :   if( &(*ppFrom)[nByte] <= pEnd ){
   60754         1037520 :     pBuf = (void*)*ppFrom;
   60755         1037520 :     *ppFrom += nByte;
   60756                 :   }else{
   60757           57581 :     *pnByte += nByte;
   60758                 :   }
   60759         1095101 :   return pBuf;
   60760                 : }
   60761                 : 
   60762                 : /*
   60763                 : ** Rewind the VDBE back to the beginning in preparation for
   60764                 : ** running it.
   60765                 : */
   60766          402492 : SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
   60767                 : #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
   60768                 :   int i;
   60769                 : #endif
   60770          402492 :   assert( p!=0 );
   60771          402492 :   assert( p->magic==VDBE_MAGIC_INIT );
   60772                 : 
   60773                 :   /* There should be at least one opcode.
   60774                 :   */
   60775          402492 :   assert( p->nOp>0 );
   60776                 : 
   60777                 :   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
   60778          402492 :   p->magic = VDBE_MAGIC_RUN;
   60779                 : 
   60780                 : #ifdef SQLITE_DEBUG
   60781         5792986 :   for(i=1; i<p->nMem; i++){
   60782         5390494 :     assert( p->aMem[i].db==p->db );
   60783                 :   }
   60784                 : #endif
   60785          402492 :   p->pc = -1;
   60786          402492 :   p->rc = SQLITE_OK;
   60787          402492 :   p->errorAction = OE_Abort;
   60788          402492 :   p->magic = VDBE_MAGIC_RUN;
   60789          402492 :   p->nChange = 0;
   60790          402492 :   p->cacheCtr = 1;
   60791          402492 :   p->minWriteFileFormat = 255;
   60792          402492 :   p->iStatement = 0;
   60793          402492 :   p->nFkConstraint = 0;
   60794                 : #ifdef VDBE_PROFILE
   60795                 :   for(i=0; i<p->nOp; i++){
   60796                 :     p->aOp[i].cnt = 0;
   60797                 :     p->aOp[i].cycles = 0;
   60798                 :   }
   60799                 : #endif
   60800          402492 : }
   60801                 : 
   60802                 : /*
   60803                 : ** Prepare a virtual machine for execution for the first time after
   60804                 : ** creating the virtual machine.  This involves things such
   60805                 : ** as allocating stack space and initializing the program counter.
   60806                 : ** After the VDBE has be prepped, it can be executed by one or more
   60807                 : ** calls to sqlite3VdbeExec().  
   60808                 : **
   60809                 : ** This function may be called exact once on a each virtual machine.
   60810                 : ** After this routine is called the VM has been "packaged" and is ready
   60811                 : ** to run.  After this routine is called, futher calls to 
   60812                 : ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
   60813                 : ** the Vdbe from the Parse object that helped generate it so that the
   60814                 : ** the Vdbe becomes an independent entity and the Parse object can be
   60815                 : ** destroyed.
   60816                 : **
   60817                 : ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
   60818                 : ** to its initial state after it has been run.
   60819                 : */
   60820          172920 : SQLITE_PRIVATE void sqlite3VdbeMakeReady(
   60821                 :   Vdbe *p,                       /* The VDBE */
   60822                 :   Parse *pParse                  /* Parsing context */
   60823                 : ){
   60824                 :   sqlite3 *db;                   /* The database connection */
   60825                 :   int nVar;                      /* Number of parameters */
   60826                 :   int nMem;                      /* Number of VM memory registers */
   60827                 :   int nCursor;                   /* Number of cursors required */
   60828                 :   int nArg;                      /* Number of arguments in subprograms */
   60829                 :   int nOnce;                     /* Number of OP_Once instructions */
   60830                 :   int n;                         /* Loop counter */
   60831                 :   u8 *zCsr;                      /* Memory available for allocation */
   60832                 :   u8 *zEnd;                      /* First byte past allocated memory */
   60833                 :   int nByte;                     /* How much extra memory is needed */
   60834                 : 
   60835          172920 :   assert( p!=0 );
   60836          172920 :   assert( p->nOp>0 );
   60837          172920 :   assert( pParse!=0 );
   60838          172920 :   assert( p->magic==VDBE_MAGIC_INIT );
   60839          172920 :   db = p->db;
   60840          172920 :   assert( db->mallocFailed==0 );
   60841          172920 :   nVar = pParse->nVar;
   60842          172920 :   nMem = pParse->nMem;
   60843          172920 :   nCursor = pParse->nTab;
   60844          172920 :   nArg = pParse->nMaxArg;
   60845          172920 :   nOnce = pParse->nOnce;
   60846          172920 :   if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
   60847                 :   
   60848                 :   /* For each cursor required, also allocate a memory cell. Memory
   60849                 :   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
   60850                 :   ** the vdbe program. Instead they are used to allocate space for
   60851                 :   ** VdbeCursor/BtCursor structures. The blob of memory associated with 
   60852                 :   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
   60853                 :   ** stores the blob of memory associated with cursor 1, etc.
   60854                 :   **
   60855                 :   ** See also: allocateCursor().
   60856                 :   */
   60857          172920 :   nMem += nCursor;
   60858                 : 
   60859                 :   /* Allocate space for memory registers, SQL variables, VDBE cursors and 
   60860                 :   ** an array to marshal SQL function arguments in.
   60861                 :   */
   60862          172920 :   zCsr = (u8*)&p->aOp[p->nOp];       /* Memory avaliable for allocation */
   60863          172920 :   zEnd = (u8*)&p->aOp[p->nOpAlloc];  /* First byte past end of zCsr[] */
   60864                 : 
   60865          172920 :   resolveP2Values(p, &nArg);
   60866          172920 :   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
   60867          172920 :   if( pParse->explain && nMem<10 ){
   60868               0 :     nMem = 10;
   60869                 :   }
   60870          172920 :   memset(zCsr, 0, zEnd-zCsr);
   60871          172920 :   zCsr += (zCsr - (u8*)0)&7;
   60872          172920 :   assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
   60873          172920 :   p->expired = 0;
   60874                 : 
   60875                 :   /* Memory for registers, parameters, cursor, etc, is allocated in two
   60876                 :   ** passes.  On the first pass, we try to reuse unused space at the 
   60877                 :   ** end of the opcode array.  If we are unable to satisfy all memory
   60878                 :   ** requirements by reusing the opcode array tail, then the second
   60879                 :   ** pass will fill in the rest using a fresh allocation.  
   60880                 :   **
   60881                 :   ** This two-pass approach that reuses as much memory as possible from
   60882                 :   ** the leftover space at the end of the opcode array can significantly
   60883                 :   ** reduce the amount of memory held by a prepared statement.
   60884                 :   */
   60885                 :   do {
   60886          212098 :     nByte = 0;
   60887          212098 :     p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
   60888          212098 :     p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
   60889          212098 :     p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
   60890          212098 :     p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
   60891          212098 :     p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
   60892                 :                           &zCsr, zEnd, &nByte);
   60893          212098 :     p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
   60894          212098 :     if( nByte ){
   60895           39178 :       p->pFree = sqlite3DbMallocZero(db, nByte);
   60896                 :     }
   60897          212098 :     zCsr = p->pFree;
   60898          212098 :     zEnd = &zCsr[nByte];
   60899          212098 :   }while( nByte && !db->mallocFailed );
   60900                 : 
   60901          172920 :   p->nCursor = (u16)nCursor;
   60902          172920 :   p->nOnceFlag = nOnce;
   60903          172920 :   if( p->aVar ){
   60904          172920 :     p->nVar = (ynVar)nVar;
   60905          258326 :     for(n=0; n<nVar; n++){
   60906           85406 :       p->aVar[n].flags = MEM_Null;
   60907           85406 :       p->aVar[n].db = db;
   60908                 :     }
   60909                 :   }
   60910          172920 :   if( p->azVar ){
   60911          172920 :     p->nzVar = pParse->nzVar;
   60912          172920 :     memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
   60913          172920 :     memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
   60914                 :   }
   60915          172920 :   if( p->aMem ){
   60916          172920 :     p->aMem--;                      /* aMem[] goes from 1..nMem */
   60917          172920 :     p->nMem = nMem;                 /*       not from 0..nMem-1 */
   60918         1423991 :     for(n=1; n<=nMem; n++){
   60919         1251071 :       p->aMem[n].flags = MEM_Invalid;
   60920         1251071 :       p->aMem[n].db = db;
   60921                 :     }
   60922                 :   }
   60923          172920 :   p->explain = pParse->explain;
   60924          172920 :   sqlite3VdbeRewind(p);
   60925          172920 : }
   60926                 : 
   60927                 : /*
   60928                 : ** Close a VDBE cursor and release all the resources that cursor 
   60929                 : ** happens to hold.
   60930                 : */
   60931          632800 : SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
   60932          632800 :   if( pCx==0 ){
   60933           90684 :     return;
   60934                 :   }
   60935          542116 :   sqlite3VdbeSorterClose(p->db, pCx);
   60936          542116 :   if( pCx->pBt ){
   60937           16027 :     sqlite3BtreeClose(pCx->pBt);
   60938                 :     /* The pCx->pCursor will be close automatically, if it exists, by
   60939                 :     ** the call above. */
   60940          526089 :   }else if( pCx->pCursor ){
   60941          523688 :     sqlite3BtreeCloseCursor(pCx->pCursor);
   60942                 :   }
   60943                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   60944          542116 :   if( pCx->pVtabCursor ){
   60945              54 :     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
   60946              54 :     const sqlite3_module *pModule = pCx->pModule;
   60947              54 :     p->inVtabMethod = 1;
   60948              54 :     pModule->xClose(pVtabCursor);
   60949              54 :     p->inVtabMethod = 0;
   60950                 :   }
   60951                 : #endif
   60952                 : }
   60953                 : 
   60954                 : /*
   60955                 : ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
   60956                 : ** is used, for example, when a trigger sub-program is halted to restore
   60957                 : ** control to the main program.
   60958                 : */
   60959           19051 : SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
   60960           19051 :   Vdbe *v = pFrame->v;
   60961           19051 :   v->aOnceFlag = pFrame->aOnceFlag;
   60962           19051 :   v->nOnceFlag = pFrame->nOnceFlag;
   60963           19051 :   v->aOp = pFrame->aOp;
   60964           19051 :   v->nOp = pFrame->nOp;
   60965           19051 :   v->aMem = pFrame->aMem;
   60966           19051 :   v->nMem = pFrame->nMem;
   60967           19051 :   v->apCsr = pFrame->apCsr;
   60968           19051 :   v->nCursor = pFrame->nCursor;
   60969           19051 :   v->db->lastRowid = pFrame->lastRowid;
   60970           19051 :   v->nChange = pFrame->nChange;
   60971           19051 :   return pFrame->pc;
   60972                 : }
   60973                 : 
   60974                 : /*
   60975                 : ** Close all cursors.
   60976                 : **
   60977                 : ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 
   60978                 : ** cell array. This is necessary as the memory cell array may contain
   60979                 : ** pointers to VdbeFrame objects, which may in turn contain pointers to
   60980                 : ** open cursors.
   60981                 : */
   60982          600847 : static void closeAllCursors(Vdbe *p){
   60983          600847 :   if( p->pFrame ){
   60984                 :     VdbeFrame *pFrame;
   60985               0 :     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
   60986               0 :     sqlite3VdbeFrameRestore(pFrame);
   60987                 :   }
   60988          600847 :   p->pFrame = 0;
   60989          600847 :   p->nFrame = 0;
   60990                 : 
   60991          600847 :   if( p->apCsr ){
   60992                 :     int i;
   60993         2224890 :     for(i=0; i<p->nCursor; i++){
   60994         1624043 :       VdbeCursor *pC = p->apCsr[i];
   60995         1624043 :       if( pC ){
   60996           50734 :         sqlite3VdbeFreeCursor(p, pC);
   60997           50734 :         p->apCsr[i] = 0;
   60998                 :       }
   60999                 :     }
   61000                 :   }
   61001          600847 :   if( p->aMem ){
   61002          600847 :     releaseMemArray(&p->aMem[1], p->nMem);
   61003                 :   }
   61004         1215923 :   while( p->pDelFrame ){
   61005           14229 :     VdbeFrame *pDel = p->pDelFrame;
   61006           14229 :     p->pDelFrame = pDel->pParent;
   61007           14229 :     sqlite3VdbeFrameDelete(pDel);
   61008                 :   }
   61009          600847 : }
   61010                 : 
   61011                 : /*
   61012                 : ** Clean up the VM after execution.
   61013                 : **
   61014                 : ** This routine will automatically close any cursors, lists, and/or
   61015                 : ** sorters that were left open.  It also deletes the values of
   61016                 : ** variables in the aVar[] array.
   61017                 : */
   61018          402492 : static void Cleanup(Vdbe *p){
   61019          402492 :   sqlite3 *db = p->db;
   61020                 : 
   61021                 : #ifdef SQLITE_DEBUG
   61022                 :   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
   61023                 :   ** Vdbe.aMem[] arrays have already been cleaned up.  */
   61024                 :   int i;
   61025          402492 :   if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
   61026          402492 :   if( p->aMem ){
   61027          402492 :     for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
   61028                 :   }
   61029                 : #endif
   61030                 : 
   61031          402488 :   sqlite3DbFree(db, p->zErrMsg);
   61032          402492 :   p->zErrMsg = 0;
   61033          402492 :   p->pResultSet = 0;
   61034          402492 : }
   61035                 : 
   61036                 : /*
   61037                 : ** Set the number of result columns that will be returned by this SQL
   61038                 : ** statement. This is now set at compile time, rather than during
   61039                 : ** execution of the vdbe program so that sqlite3_column_count() can
   61040                 : ** be called on an SQL statement before sqlite3_step().
   61041                 : */
   61042           54206 : SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
   61043                 :   Mem *pColName;
   61044                 :   int n;
   61045           54206 :   sqlite3 *db = p->db;
   61046                 : 
   61047           54206 :   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
   61048           54206 :   sqlite3DbFree(db, p->aColName);
   61049           54206 :   n = nResColumn*COLNAME_N;
   61050           54206 :   p->nResColumn = (u16)nResColumn;
   61051           54206 :   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
   61052           54206 :   if( p->aColName==0 ) return;
   61053          690114 :   while( n-- > 0 ){
   61054          581702 :     pColName->flags = MEM_Null;
   61055          581702 :     pColName->db = p->db;
   61056          581702 :     pColName++;
   61057                 :   }
   61058                 : }
   61059                 : 
   61060                 : /*
   61061                 : ** Set the name of the idx'th column to be returned by the SQL statement.
   61062                 : ** zName must be a pointer to a nul terminated string.
   61063                 : **
   61064                 : ** This call must be made after a call to sqlite3VdbeSetNumCols().
   61065                 : **
   61066                 : ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
   61067                 : ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
   61068                 : ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
   61069                 : */
   61070          572584 : SQLITE_PRIVATE int sqlite3VdbeSetColName(
   61071                 :   Vdbe *p,                         /* Vdbe being configured */
   61072                 :   int idx,                         /* Index of column zName applies to */
   61073                 :   int var,                         /* One of the COLNAME_* constants */
   61074                 :   const char *zName,               /* Pointer to buffer containing name */
   61075                 :   void (*xDel)(void*)              /* Memory management strategy for zName */
   61076                 : ){
   61077                 :   int rc;
   61078                 :   Mem *pColName;
   61079          572584 :   assert( idx<p->nResColumn );
   61080          572584 :   assert( var<COLNAME_N );
   61081          572584 :   if( p->db->mallocFailed ){
   61082               0 :     assert( !zName || xDel!=SQLITE_DYNAMIC );
   61083               0 :     return SQLITE_NOMEM;
   61084                 :   }
   61085          572584 :   assert( p->aColName!=0 );
   61086          572584 :   pColName = &(p->aColName[idx+var*p->nResColumn]);
   61087          572584 :   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
   61088          572584 :   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
   61089          572584 :   return rc;
   61090                 : }
   61091                 : 
   61092                 : /*
   61093                 : ** A read or write transaction may or may not be active on database handle
   61094                 : ** db. If a transaction is active, commit it. If there is a
   61095                 : ** write-transaction spanning more than one database file, this routine
   61096                 : ** takes care of the master journal trickery.
   61097                 : */
   61098          100131 : static int vdbeCommit(sqlite3 *db, Vdbe *p){
   61099                 :   int i;
   61100          100131 :   int nTrans = 0;  /* Number of databases with an active write-transaction */
   61101          100131 :   int rc = SQLITE_OK;
   61102          100131 :   int needXcommit = 0;
   61103                 : 
   61104                 : #ifdef SQLITE_OMIT_VIRTUALTABLE
   61105                 :   /* With this option, sqlite3VtabSync() is defined to be simply 
   61106                 :   ** SQLITE_OK so p is not used. 
   61107                 :   */
   61108                 :   UNUSED_PARAMETER(p);
   61109                 : #endif
   61110                 : 
   61111                 :   /* Before doing anything else, call the xSync() callback for any
   61112                 :   ** virtual module tables written in this transaction. This has to
   61113                 :   ** be done before determining whether a master journal file is 
   61114                 :   ** required, as an xSync() callback may add an attached database
   61115                 :   ** to the transaction.
   61116                 :   */
   61117          100131 :   rc = sqlite3VtabSync(db, &p->zErrMsg);
   61118                 : 
   61119                 :   /* This loop determines (a) if the commit hook should be invoked and
   61120                 :   ** (b) how many database files have open write transactions, not 
   61121                 :   ** including the temp database. (b) is important because if more than 
   61122                 :   ** one database file has an open write transaction, a master journal
   61123                 :   ** file is required for an atomic commit.
   61124                 :   */ 
   61125          300393 :   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
   61126          200262 :     Btree *pBt = db->aDb[i].pBt;
   61127          200262 :     if( sqlite3BtreeIsInTrans(pBt) ){
   61128           40527 :       needXcommit = 1;
   61129           40527 :       if( i!=1 ) nTrans++;
   61130           40527 :       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
   61131                 :     }
   61132                 :   }
   61133          100131 :   if( rc!=SQLITE_OK ){
   61134               0 :     return rc;
   61135                 :   }
   61136                 : 
   61137                 :   /* If there are any write-transactions at all, invoke the commit hook */
   61138          100131 :   if( needXcommit && db->xCommitCallback ){
   61139               8 :     rc = db->xCommitCallback(db->pCommitArg);
   61140               8 :     if( rc ){
   61141               0 :       return SQLITE_CONSTRAINT;
   61142                 :     }
   61143                 :   }
   61144                 : 
   61145                 :   /* The simple case - no more than one database file (not counting the
   61146                 :   ** TEMP database) has a transaction active.   There is no need for the
   61147                 :   ** master-journal.
   61148                 :   **
   61149                 :   ** If the return value of sqlite3BtreeGetFilename() is a zero length
   61150                 :   ** string, it means the main database is :memory: or a temp file.  In 
   61151                 :   ** that case we do not support atomic multi-file commits, so use the 
   61152                 :   ** simple case then too.
   61153                 :   */
   61154          100131 :   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
   61155           99540 :    || nTrans<=1
   61156                 :   ){
   61157          300393 :     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
   61158          200262 :       Btree *pBt = db->aDb[i].pBt;
   61159          200262 :       if( pBt ){
   61160          126015 :         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
   61161                 :       }
   61162                 :     }
   61163                 : 
   61164                 :     /* Do the commit only if all databases successfully complete phase 1. 
   61165                 :     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
   61166                 :     ** IO error while deleting or truncating a journal file. It is unlikely,
   61167                 :     ** but could happen. In this case abandon processing and return the error.
   61168                 :     */
   61169          300393 :     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
   61170          200262 :       Btree *pBt = db->aDb[i].pBt;
   61171          200262 :       if( pBt ){
   61172          126015 :         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
   61173                 :       }
   61174                 :     }
   61175          200262 :     if( rc==SQLITE_OK ){
   61176          100131 :       sqlite3VtabCommit(db);
   61177                 :     }
   61178                 :   }
   61179                 : 
   61180                 :   /* The complex case - There is a multi-file write-transaction active.
   61181                 :   ** This requires a master journal file to ensure the transaction is
   61182                 :   ** committed atomicly.
   61183                 :   */
   61184                 : #ifndef SQLITE_OMIT_DISKIO
   61185                 :   else{
   61186               0 :     sqlite3_vfs *pVfs = db->pVfs;
   61187               0 :     int needSync = 0;
   61188               0 :     char *zMaster = 0;   /* File-name for the master journal */
   61189               0 :     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
   61190               0 :     sqlite3_file *pMaster = 0;
   61191               0 :     i64 offset = 0;
   61192                 :     int res;
   61193               0 :     int retryCount = 0;
   61194                 :     int nMainFile;
   61195                 : 
   61196                 :     /* Select a master journal file name */
   61197               0 :     nMainFile = sqlite3Strlen30(zMainFile);
   61198               0 :     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
   61199               0 :     if( zMaster==0 ) return SQLITE_NOMEM;
   61200                 :     do {
   61201                 :       u32 iRandom;
   61202               0 :       if( retryCount ){
   61203               0 :         if( retryCount>100 ){
   61204               0 :           sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
   61205               0 :           sqlite3OsDelete(pVfs, zMaster, 0);
   61206               0 :           break;
   61207               0 :         }else if( retryCount==1 ){
   61208               0 :           sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
   61209                 :         }
   61210                 :       }
   61211               0 :       retryCount++;
   61212               0 :       sqlite3_randomness(sizeof(iRandom), &iRandom);
   61213               0 :       sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
   61214                 :                                (iRandom>>8)&0xffffff, iRandom&0xff);
   61215                 :       /* The antipenultimate character of the master journal name must
   61216                 :       ** be "9" to avoid name collisions when using 8+3 filenames. */
   61217               0 :       assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
   61218                 :       sqlite3FileSuffix3(zMainFile, zMaster);
   61219               0 :       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
   61220               0 :     }while( rc==SQLITE_OK && res );
   61221               0 :     if( rc==SQLITE_OK ){
   61222                 :       /* Open the master journal. */
   61223               0 :       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 
   61224                 :           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
   61225                 :           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
   61226                 :       );
   61227                 :     }
   61228               0 :     if( rc!=SQLITE_OK ){
   61229               0 :       sqlite3DbFree(db, zMaster);
   61230               0 :       return rc;
   61231                 :     }
   61232                 :  
   61233                 :     /* Write the name of each database file in the transaction into the new
   61234                 :     ** master journal file. If an error occurs at this point close
   61235                 :     ** and delete the master journal file. All the individual journal files
   61236                 :     ** still have 'null' as the master journal pointer, so they will roll
   61237                 :     ** back independently if a failure occurs.
   61238                 :     */
   61239               0 :     for(i=0; i<db->nDb; i++){
   61240               0 :       Btree *pBt = db->aDb[i].pBt;
   61241               0 :       if( sqlite3BtreeIsInTrans(pBt) ){
   61242               0 :         char const *zFile = sqlite3BtreeGetJournalname(pBt);
   61243               0 :         if( zFile==0 ){
   61244               0 :           continue;  /* Ignore TEMP and :memory: databases */
   61245                 :         }
   61246               0 :         assert( zFile[0]!=0 );
   61247               0 :         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
   61248               0 :           needSync = 1;
   61249                 :         }
   61250               0 :         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
   61251               0 :         offset += sqlite3Strlen30(zFile)+1;
   61252               0 :         if( rc!=SQLITE_OK ){
   61253               0 :           sqlite3OsCloseFree(pMaster);
   61254               0 :           sqlite3OsDelete(pVfs, zMaster, 0);
   61255               0 :           sqlite3DbFree(db, zMaster);
   61256               0 :           return rc;
   61257                 :         }
   61258                 :       }
   61259                 :     }
   61260                 : 
   61261                 :     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
   61262                 :     ** flag is set this is not required.
   61263                 :     */
   61264               0 :     if( needSync 
   61265               0 :      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
   61266               0 :      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
   61267                 :     ){
   61268               0 :       sqlite3OsCloseFree(pMaster);
   61269               0 :       sqlite3OsDelete(pVfs, zMaster, 0);
   61270               0 :       sqlite3DbFree(db, zMaster);
   61271               0 :       return rc;
   61272                 :     }
   61273                 : 
   61274                 :     /* Sync all the db files involved in the transaction. The same call
   61275                 :     ** sets the master journal pointer in each individual journal. If
   61276                 :     ** an error occurs here, do not delete the master journal file.
   61277                 :     **
   61278                 :     ** If the error occurs during the first call to
   61279                 :     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
   61280                 :     ** master journal file will be orphaned. But we cannot delete it,
   61281                 :     ** in case the master journal file name was written into the journal
   61282                 :     ** file before the failure occurred.
   61283                 :     */
   61284               0 :     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 
   61285               0 :       Btree *pBt = db->aDb[i].pBt;
   61286               0 :       if( pBt ){
   61287               0 :         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
   61288                 :       }
   61289                 :     }
   61290               0 :     sqlite3OsCloseFree(pMaster);
   61291               0 :     assert( rc!=SQLITE_BUSY );
   61292               0 :     if( rc!=SQLITE_OK ){
   61293               0 :       sqlite3DbFree(db, zMaster);
   61294               0 :       return rc;
   61295                 :     }
   61296                 : 
   61297                 :     /* Delete the master journal file. This commits the transaction. After
   61298                 :     ** doing this the directory is synced again before any individual
   61299                 :     ** transaction files are deleted.
   61300                 :     */
   61301               0 :     rc = sqlite3OsDelete(pVfs, zMaster, 1);
   61302               0 :     sqlite3DbFree(db, zMaster);
   61303               0 :     zMaster = 0;
   61304               0 :     if( rc ){
   61305               0 :       return rc;
   61306                 :     }
   61307                 : 
   61308                 :     /* All files and directories have already been synced, so the following
   61309                 :     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
   61310                 :     ** deleting or truncating journals. If something goes wrong while
   61311                 :     ** this is happening we don't really care. The integrity of the
   61312                 :     ** transaction is already guaranteed, but some stray 'cold' journals
   61313                 :     ** may be lying around. Returning an error code won't help matters.
   61314                 :     */
   61315                 :     disable_simulated_io_errors();
   61316               0 :     sqlite3BeginBenignMalloc();
   61317               0 :     for(i=0; i<db->nDb; i++){ 
   61318               0 :       Btree *pBt = db->aDb[i].pBt;
   61319               0 :       if( pBt ){
   61320               0 :         sqlite3BtreeCommitPhaseTwo(pBt, 1);
   61321                 :       }
   61322                 :     }
   61323               0 :     sqlite3EndBenignMalloc();
   61324                 :     enable_simulated_io_errors();
   61325                 : 
   61326               0 :     sqlite3VtabCommit(db);
   61327                 :   }
   61328                 : #endif
   61329                 : 
   61330          100131 :   return rc;
   61331                 : }
   61332                 : 
   61333                 : /* 
   61334                 : ** This routine checks that the sqlite3.activeVdbeCnt count variable
   61335                 : ** matches the number of vdbe's in the list sqlite3.pVdbe that are
   61336                 : ** currently active. An assertion fails if the two counts do not match.
   61337                 : ** This is an internal self-check only - it is not an essential processing
   61338                 : ** step.
   61339                 : **
   61340                 : ** This is a no-op if NDEBUG is defined.
   61341                 : */
   61342                 : #ifndef NDEBUG
   61343          804984 : static void checkActiveVdbeCnt(sqlite3 *db){
   61344                 :   Vdbe *p;
   61345          804984 :   int cnt = 0;
   61346          804984 :   int nWrite = 0;
   61347          804984 :   p = db->pVdbe;
   61348        11945853 :   while( p ){
   61349        10335885 :     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
   61350          422438 :       cnt++;
   61351          422438 :       if( p->readOnly==0 ) nWrite++;
   61352                 :     }
   61353        10335885 :     p = p->pNext;
   61354                 :   }
   61355          804984 :   assert( cnt==db->activeVdbeCnt );
   61356          804984 :   assert( nWrite==db->writeVdbeCnt );
   61357          804984 : }
   61358                 : #else
   61359                 : #define checkActiveVdbeCnt(x)
   61360                 : #endif
   61361                 : 
   61362                 : /*
   61363                 : ** For every Btree that in database connection db which 
   61364                 : ** has been modified, "trip" or invalidate each cursor in
   61365                 : ** that Btree might have been modified so that the cursor
   61366                 : ** can never be used again.  This happens when a rollback
   61367                 : *** occurs.  We have to trip all the other cursors, even
   61368                 : ** cursor from other VMs in different database connections,
   61369                 : ** so that none of them try to use the data at which they
   61370                 : ** were pointing and which now may have been changed due
   61371                 : ** to the rollback.
   61372                 : **
   61373                 : ** Remember that a rollback can delete tables complete and
   61374                 : ** reorder rootpages.  So it is not sufficient just to save
   61375                 : ** the state of the cursor.  We have to invalidate the cursor
   61376                 : ** so that it is never used again.
   61377                 : */
   61378               0 : static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
   61379                 :   int i;
   61380               0 :   for(i=0; i<db->nDb; i++){
   61381               0 :     Btree *p = db->aDb[i].pBt;
   61382               0 :     if( p && sqlite3BtreeIsInTrans(p) ){
   61383               0 :       sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
   61384                 :     }
   61385                 :   }
   61386               0 : }
   61387                 : 
   61388                 : /*
   61389                 : ** If the Vdbe passed as the first argument opened a statement-transaction,
   61390                 : ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
   61391                 : ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
   61392                 : ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 
   61393                 : ** statement transaction is commtted.
   61394                 : **
   61395                 : ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 
   61396                 : ** Otherwise SQLITE_OK.
   61397                 : */
   61398          287680 : SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
   61399          287680 :   sqlite3 *const db = p->db;
   61400          287680 :   int rc = SQLITE_OK;
   61401                 : 
   61402                 :   /* If p->iStatement is greater than zero, then this Vdbe opened a 
   61403                 :   ** statement transaction that should be closed here. The only exception
   61404                 :   ** is that an IO error may have occured, causing an emergency rollback.
   61405                 :   ** In this case (db->nStatement==0), and there is nothing to do.
   61406                 :   */
   61407          287680 :   if( db->nStatement && p->iStatement ){
   61408                 :     int i;
   61409           18790 :     const int iSavepoint = p->iStatement-1;
   61410                 : 
   61411           18790 :     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
   61412           18790 :     assert( db->nStatement>0 );
   61413           18790 :     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
   61414                 : 
   61415           56391 :     for(i=0; i<db->nDb; i++){ 
   61416           37601 :       int rc2 = SQLITE_OK;
   61417           37601 :       Btree *pBt = db->aDb[i].pBt;
   61418           37601 :       if( pBt ){
   61419           33223 :         if( eOp==SAVEPOINT_ROLLBACK ){
   61420              17 :           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
   61421                 :         }
   61422           33223 :         if( rc2==SQLITE_OK ){
   61423           33223 :           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
   61424                 :         }
   61425           33223 :         if( rc==SQLITE_OK ){
   61426           33223 :           rc = rc2;
   61427                 :         }
   61428                 :       }
   61429                 :     }
   61430           18790 :     db->nStatement--;
   61431           18790 :     p->iStatement = 0;
   61432                 : 
   61433           18790 :     if( rc==SQLITE_OK ){
   61434           18790 :       if( eOp==SAVEPOINT_ROLLBACK ){
   61435              16 :         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
   61436                 :       }
   61437           18790 :       if( rc==SQLITE_OK ){
   61438           18790 :         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
   61439                 :       }
   61440                 :     }
   61441                 : 
   61442                 :     /* If the statement transaction is being rolled back, also restore the 
   61443                 :     ** database handles deferred constraint counter to the value it had when 
   61444                 :     ** the statement transaction was opened.  */
   61445           18790 :     if( eOp==SAVEPOINT_ROLLBACK ){
   61446              16 :       db->nDeferredCons = p->nStmtDefCons;
   61447                 :     }
   61448                 :   }
   61449          287680 :   return rc;
   61450                 : }
   61451                 : 
   61452                 : /*
   61453                 : ** This function is called when a transaction opened by the database 
   61454                 : ** handle associated with the VM passed as an argument is about to be 
   61455                 : ** committed. If there are outstanding deferred foreign key constraint
   61456                 : ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
   61457                 : **
   61458                 : ** If there are outstanding FK violations and this function returns 
   61459                 : ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT and write
   61460                 : ** an error message to it. Then return SQLITE_ERROR.
   61461                 : */
   61462                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   61463          505515 : SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
   61464          505515 :   sqlite3 *db = p->db;
   61465          505515 :   if( (deferred && db->nDeferredCons>0) || (!deferred && p->nFkConstraint>0) ){
   61466               1 :     p->rc = SQLITE_CONSTRAINT;
   61467               1 :     p->errorAction = OE_Abort;
   61468               1 :     sqlite3SetString(&p->zErrMsg, db, "foreign key constraint failed");
   61469               0 :     return SQLITE_ERROR;
   61470                 :   }
   61471          505514 :   return SQLITE_OK;
   61472                 : }
   61473                 : #endif
   61474                 : 
   61475                 : /*
   61476                 : ** This routine is called the when a VDBE tries to halt.  If the VDBE
   61477                 : ** has made changes and is in autocommit mode, then commit those
   61478                 : ** changes.  If a rollback is needed, then do the rollback.
   61479                 : **
   61480                 : ** This routine is the only way to move the state of a VM from
   61481                 : ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
   61482                 : ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
   61483                 : **
   61484                 : ** Return an error code.  If the commit could not complete because of
   61485                 : ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
   61486                 : ** means the close did not happen and needs to be repeated.
   61487                 : */
   61488          600847 : SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
   61489                 :   int rc;                         /* Used to store transient return codes */
   61490          600847 :   sqlite3 *db = p->db;
   61491                 : 
   61492                 :   /* This function contains the logic that determines if a statement or
   61493                 :   ** transaction will be committed or rolled back as a result of the
   61494                 :   ** execution of this virtual machine. 
   61495                 :   **
   61496                 :   ** If any of the following errors occur:
   61497                 :   **
   61498                 :   **     SQLITE_NOMEM
   61499                 :   **     SQLITE_IOERR
   61500                 :   **     SQLITE_FULL
   61501                 :   **     SQLITE_INTERRUPT
   61502                 :   **
   61503                 :   ** Then the internal cache might have been left in an inconsistent
   61504                 :   ** state.  We need to rollback the statement transaction, if there is
   61505                 :   ** one, or the complete transaction if there is no statement transaction.
   61506                 :   */
   61507                 : 
   61508          600847 :   if( p->db->mallocFailed ){
   61509               0 :     p->rc = SQLITE_NOMEM;
   61510                 :   }
   61511          600847 :   if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
   61512          600847 :   closeAllCursors(p);
   61513          600847 :   if( p->magic!=VDBE_MAGIC_RUN ){
   61514          198355 :     return SQLITE_OK;
   61515                 :   }
   61516          402492 :   checkActiveVdbeCnt(db);
   61517                 : 
   61518                 :   /* No commit or rollback needed if the program never started */
   61519          402492 :   if( p->pc>=0 ){
   61520                 :     int mrc;   /* Primary error code from p->rc */
   61521          227536 :     int eStatementOp = 0;
   61522                 :     int isSpecialError;            /* Set to true if a 'special' error */
   61523                 : 
   61524                 :     /* Lock all btrees used by the statement */
   61525          227536 :     sqlite3VdbeEnter(p);
   61526                 : 
   61527                 :     /* Check for one of the special errors */
   61528          227536 :     mrc = p->rc & 0xff;
   61529          227536 :     assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
   61530          227537 :     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
   61531          227536 :                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
   61532          227536 :     if( isSpecialError ){
   61533                 :       /* If the query was read-only and the error code is SQLITE_INTERRUPT, 
   61534                 :       ** no rollback is necessary. Otherwise, at least a savepoint 
   61535                 :       ** transaction must be rolled back to restore the database to a 
   61536                 :       ** consistent state.
   61537                 :       **
   61538                 :       ** Even if the statement is read-only, it is important to perform
   61539                 :       ** a statement or transaction rollback operation. If the error 
   61540                 :       ** occured while writing to the journal, sub-journal or database
   61541                 :       ** file as part of an effort to free up cache space (see function
   61542                 :       ** pagerStress() in pager.c), the rollback is required to restore 
   61543                 :       ** the pager to a consistent state.
   61544                 :       */
   61545               1 :       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
   61546               0 :         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
   61547               0 :           eStatementOp = SAVEPOINT_ROLLBACK;
   61548                 :         }else{
   61549                 :           /* We are forced to roll back the active transaction. Before doing
   61550                 :           ** so, abort any other statements this handle currently has active.
   61551                 :           */
   61552               0 :           invalidateCursorsOnModifiedBtrees(db);
   61553               0 :           sqlite3RollbackAll(db);
   61554               0 :           sqlite3CloseSavepoints(db);
   61555               0 :           db->autoCommit = 1;
   61556                 :         }
   61557                 :       }
   61558                 :     }
   61559                 : 
   61560                 :     /* Check for immediate foreign key violations. */
   61561          227536 :     if( p->rc==SQLITE_OK ){
   61562          226885 :       sqlite3VdbeCheckFk(p, 0);
   61563                 :     }
   61564                 :   
   61565                 :     /* If the auto-commit flag is set and this is the only active writer 
   61566                 :     ** VM, then we do either a commit or rollback of the current transaction. 
   61567                 :     **
   61568                 :     ** Note: This block also runs if one of the special errors handled 
   61569                 :     ** above has occurred. 
   61570                 :     */
   61571          227536 :     if( !sqlite3VtabInSync(db) 
   61572          227524 :      && db->autoCommit 
   61573          104457 :      && db->writeVdbeCnt==(p->readOnly==0) 
   61574                 :     ){
   61575          100452 :       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
   61576          100131 :         rc = sqlite3VdbeCheckFk(p, 1);
   61577          100131 :         if( rc!=SQLITE_OK ){
   61578               0 :           if( NEVER(p->readOnly) ){
   61579               0 :             sqlite3VdbeLeave(p);
   61580               0 :             return SQLITE_ERROR;
   61581                 :           }
   61582               0 :           rc = SQLITE_CONSTRAINT;
   61583                 :         }else{ 
   61584                 :           /* The auto-commit flag is true, the vdbe program was successful 
   61585                 :           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
   61586                 :           ** key constraints to hold up the transaction. This means a commit 
   61587                 :           ** is required. */
   61588          100131 :           rc = vdbeCommit(db, p);
   61589                 :         }
   61590          200262 :         if( rc==SQLITE_BUSY && p->readOnly ){
   61591               0 :           sqlite3VdbeLeave(p);
   61592               0 :           return SQLITE_BUSY;
   61593          100131 :         }else if( rc!=SQLITE_OK ){
   61594               0 :           p->rc = rc;
   61595               0 :           sqlite3RollbackAll(db);
   61596                 :         }else{
   61597          100131 :           db->nDeferredCons = 0;
   61598          100131 :           sqlite3CommitInternalChanges(db);
   61599                 :         }
   61600                 :       }else{
   61601             321 :         sqlite3RollbackAll(db);
   61602                 :       }
   61603          100452 :       db->nStatement = 0;
   61604          127084 :     }else if( eStatementOp==0 ){
   61605          127084 :       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
   61606          126754 :         eStatementOp = SAVEPOINT_RELEASE;
   61607             330 :       }else if( p->errorAction==OE_Abort ){
   61608             330 :         eStatementOp = SAVEPOINT_ROLLBACK;
   61609                 :       }else{
   61610               0 :         invalidateCursorsOnModifiedBtrees(db);
   61611               0 :         sqlite3RollbackAll(db);
   61612               0 :         sqlite3CloseSavepoints(db);
   61613               0 :         db->autoCommit = 1;
   61614                 :       }
   61615                 :     }
   61616                 :   
   61617                 :     /* If eStatementOp is non-zero, then a statement transaction needs to
   61618                 :     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
   61619                 :     ** do so. If this operation returns an error, and the current statement
   61620                 :     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
   61621                 :     ** current statement error code.
   61622                 :     */
   61623          227536 :     if( eStatementOp ){
   61624          127084 :       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
   61625          127084 :       if( rc ){
   61626               0 :         if( p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT ){
   61627               0 :           p->rc = rc;
   61628               0 :           sqlite3DbFree(db, p->zErrMsg);
   61629               0 :           p->zErrMsg = 0;
   61630                 :         }
   61631               0 :         invalidateCursorsOnModifiedBtrees(db);
   61632               0 :         sqlite3RollbackAll(db);
   61633               0 :         sqlite3CloseSavepoints(db);
   61634               0 :         db->autoCommit = 1;
   61635                 :       }
   61636                 :     }
   61637                 :   
   61638                 :     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
   61639                 :     ** has been rolled back, update the database connection change-counter. 
   61640                 :     */
   61641          227536 :     if( p->changeCntOn ){
   61642           68533 :       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
   61643           68485 :         sqlite3VdbeSetChanges(db, p->nChange);
   61644                 :       }else{
   61645              48 :         sqlite3VdbeSetChanges(db, 0);
   61646                 :       }
   61647           68533 :       p->nChange = 0;
   61648                 :     }
   61649                 :   
   61650                 :     /* Rollback or commit any schema changes that occurred. */
   61651          227536 :     if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
   61652             279 :       sqlite3ResetInternalSchema(db, -1);
   61653             279 :       db->flags = (db->flags | SQLITE_InternChanges);
   61654                 :     }
   61655                 : 
   61656                 :     /* Release the locks */
   61657          227536 :     sqlite3VdbeLeave(p);
   61658                 :   }
   61659                 : 
   61660                 :   /* We have successfully halted and closed the VM.  Record this fact. */
   61661          402492 :   if( p->pc>=0 ){
   61662          227536 :     db->activeVdbeCnt--;
   61663          227536 :     if( !p->readOnly ){
   61664           90355 :       db->writeVdbeCnt--;
   61665                 :     }
   61666          227536 :     assert( db->activeVdbeCnt>=db->writeVdbeCnt );
   61667                 :   }
   61668          402492 :   p->magic = VDBE_MAGIC_HALT;
   61669          402492 :   checkActiveVdbeCnt(db);
   61670          402492 :   if( p->db->mallocFailed ){
   61671               0 :     p->rc = SQLITE_NOMEM;
   61672                 :   }
   61673                 : 
   61674                 :   /* If the auto-commit flag is set to true, then any locks that were held
   61675                 :   ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 
   61676                 :   ** to invoke any required unlock-notify callbacks.
   61677                 :   */
   61678          402492 :   if( db->autoCommit ){
   61679          209541 :     sqlite3ConnectionUnlocked(db);
   61680                 :   }
   61681                 : 
   61682          402492 :   assert( db->activeVdbeCnt>0 || db->autoCommit==0 || db->nStatement==0 );
   61683          402492 :   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
   61684                 : }
   61685                 : 
   61686                 : 
   61687                 : /*
   61688                 : ** Each VDBE holds the result of the most recent sqlite3_step() call
   61689                 : ** in p->rc.  This routine sets that result back to SQLITE_OK.
   61690                 : */
   61691            1521 : SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
   61692            1521 :   p->rc = SQLITE_OK;
   61693            1521 : }
   61694                 : 
   61695                 : /*
   61696                 : ** Copy the error code and error message belonging to the VDBE passed
   61697                 : ** as the first argument to its database handle (so that they will be 
   61698                 : ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
   61699                 : **
   61700                 : ** This function does not clear the VDBE error code or message, just
   61701                 : ** copies them to the database handle.
   61702                 : */
   61703          229419 : SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
   61704          229419 :   sqlite3 *db = p->db;
   61705          229419 :   int rc = p->rc;
   61706          229419 :   if( p->zErrMsg ){
   61707            1286 :     u8 mallocFailed = db->mallocFailed;
   61708            1286 :     sqlite3BeginBenignMalloc();
   61709            1286 :     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
   61710            1286 :     sqlite3EndBenignMalloc();
   61711            1286 :     db->mallocFailed = mallocFailed;
   61712            1286 :     db->errCode = rc;
   61713                 :   }else{
   61714          228133 :     sqlite3Error(db, rc, 0);
   61715                 :   }
   61716          229419 :   return rc;
   61717                 : }
   61718                 : 
   61719                 : /*
   61720                 : ** Clean up a VDBE after execution but do not delete the VDBE just yet.
   61721                 : ** Write any error messages into *pzErrMsg.  Return the result code.
   61722                 : **
   61723                 : ** After this routine is run, the VDBE should be ready to be executed
   61724                 : ** again.
   61725                 : **
   61726                 : ** To look at it another way, this routine resets the state of the
   61727                 : ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
   61728                 : ** VDBE_MAGIC_INIT.
   61729                 : */
   61730          402492 : SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
   61731                 :   sqlite3 *db;
   61732          402492 :   db = p->db;
   61733                 : 
   61734                 :   /* If the VM did not run to completion or if it encountered an
   61735                 :   ** error, then it might not have been halted properly.  So halt
   61736                 :   ** it now.
   61737                 :   */
   61738          402492 :   sqlite3VdbeHalt(p);
   61739                 : 
   61740                 :   /* If the VDBE has be run even partially, then transfer the error code
   61741                 :   ** and error message from the VDBE into the main database structure.  But
   61742                 :   ** if the VDBE has just been set to run but has not actually executed any
   61743                 :   ** instructions yet, leave the main database error information unchanged.
   61744                 :   */
   61745          402492 :   if( p->pc>=0 ){
   61746          227536 :     sqlite3VdbeTransferError(p);
   61747          227536 :     sqlite3DbFree(db, p->zErrMsg);
   61748          227536 :     p->zErrMsg = 0;
   61749          227536 :     if( p->runOnlyOnce ) p->expired = 1;
   61750          174956 :   }else if( p->rc && p->expired ){
   61751                 :     /* The expired flag was set on the VDBE before the first call
   61752                 :     ** to sqlite3_step(). For consistency (since sqlite3_step() was
   61753                 :     ** called), set the database error in this case as well.
   61754                 :     */
   61755               0 :     sqlite3Error(db, p->rc, 0);
   61756               0 :     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
   61757               0 :     sqlite3DbFree(db, p->zErrMsg);
   61758               0 :     p->zErrMsg = 0;
   61759                 :   }
   61760                 : 
   61761                 :   /* Reclaim all memory used by the VDBE
   61762                 :   */
   61763          402492 :   Cleanup(p);
   61764                 : 
   61765                 :   /* Save profiling information from this VDBE run.
   61766                 :   */
   61767                 : #ifdef VDBE_PROFILE
   61768                 :   {
   61769                 :     FILE *out = fopen("vdbe_profile.out", "a");
   61770                 :     if( out ){
   61771                 :       int i;
   61772                 :       fprintf(out, "---- ");
   61773                 :       for(i=0; i<p->nOp; i++){
   61774                 :         fprintf(out, "%02x", p->aOp[i].opcode);
   61775                 :       }
   61776                 :       fprintf(out, "\n");
   61777                 :       for(i=0; i<p->nOp; i++){
   61778                 :         fprintf(out, "%6d %10lld %8lld ",
   61779                 :            p->aOp[i].cnt,
   61780                 :            p->aOp[i].cycles,
   61781                 :            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
   61782                 :         );
   61783                 :         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
   61784                 :       }
   61785                 :       fclose(out);
   61786                 :     }
   61787                 :   }
   61788                 : #endif
   61789          402492 :   p->magic = VDBE_MAGIC_INIT;
   61790          402492 :   return p->rc & db->errMask;
   61791                 : }
   61792                 :  
   61793                 : /*
   61794                 : ** Clean up and delete a VDBE after execution.  Return an integer which is
   61795                 : ** the result code.  Write any error message text into *pzErrMsg.
   61796                 : */
   61797          172920 : SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
   61798          172920 :   int rc = SQLITE_OK;
   61799          172920 :   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
   61800          172920 :     rc = sqlite3VdbeReset(p);
   61801          172920 :     assert( (rc & p->db->errMask)==rc );
   61802                 :   }
   61803          172920 :   sqlite3VdbeDelete(p);
   61804          172920 :   return rc;
   61805                 : }
   61806                 : 
   61807                 : /*
   61808                 : ** Call the destructor for each auxdata entry in pVdbeFunc for which
   61809                 : ** the corresponding bit in mask is clear.  Auxdata entries beyond 31
   61810                 : ** are always destroyed.  To destroy all auxdata entries, call this
   61811                 : ** routine with mask==0.
   61812                 : */
   61813               0 : SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
   61814                 :   int i;
   61815               0 :   for(i=0; i<pVdbeFunc->nAux; i++){
   61816               0 :     struct AuxData *pAux = &pVdbeFunc->apAux[i];
   61817               0 :     if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
   61818               0 :       if( pAux->xDelete ){
   61819               0 :         pAux->xDelete(pAux->pAux);
   61820                 :       }
   61821               0 :       pAux->pAux = 0;
   61822                 :     }
   61823                 :   }
   61824               0 : }
   61825                 : 
   61826                 : /*
   61827                 : ** Free all memory associated with the Vdbe passed as the second argument.
   61828                 : ** The difference between this function and sqlite3VdbeDelete() is that
   61829                 : ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
   61830                 : ** the database connection.
   61831                 : */
   61832          176107 : SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3 *db, Vdbe *p){
   61833                 :   SubProgram *pSub, *pNext;
   61834                 :   int i;
   61835          176107 :   assert( p->db==0 || p->db==db );
   61836          176107 :   releaseMemArray(p->aVar, p->nVar);
   61837          176107 :   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
   61838          179204 :   for(pSub=p->pProgram; pSub; pSub=pNext){
   61839            3097 :     pNext = pSub->pNext;
   61840            3097 :     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
   61841            3097 :     sqlite3DbFree(db, pSub);
   61842                 :   }
   61843          176107 :   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
   61844          176107 :   vdbeFreeOpArray(db, p->aOp, p->nOp);
   61845          176107 :   sqlite3DbFree(db, p->aLabel);
   61846          176107 :   sqlite3DbFree(db, p->aColName);
   61847          176107 :   sqlite3DbFree(db, p->zSql);
   61848          176107 :   sqlite3DbFree(db, p->pFree);
   61849                 : #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
   61850                 :   sqlite3DbFree(db, p->zExplain);
   61851                 :   sqlite3DbFree(db, p->pExplain);
   61852                 : #endif
   61853          176107 :   sqlite3DbFree(db, p);
   61854          176107 : }
   61855                 : 
   61856                 : /*
   61857                 : ** Delete an entire VDBE.
   61858                 : */
   61859          176050 : SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
   61860                 :   sqlite3 *db;
   61861                 : 
   61862          176050 :   if( NEVER(p==0) ) return;
   61863          176050 :   db = p->db;
   61864          176050 :   if( p->pPrev ){
   61865           31276 :     p->pPrev->pNext = p->pNext;
   61866                 :   }else{
   61867          144774 :     assert( db->pVdbe==p );
   61868          144774 :     db->pVdbe = p->pNext;
   61869                 :   }
   61870          176050 :   if( p->pNext ){
   61871          114409 :     p->pNext->pPrev = p->pPrev;
   61872                 :   }
   61873          176050 :   p->magic = VDBE_MAGIC_DEAD;
   61874          176050 :   p->db = 0;
   61875          176050 :   sqlite3VdbeDeleteObject(db, p);
   61876                 : }
   61877                 : 
   61878                 : /*
   61879                 : ** Make sure the cursor p is ready to read or write the row to which it
   61880                 : ** was last positioned.  Return an error code if an OOM fault or I/O error
   61881                 : ** prevents us from positioning the cursor to its correct position.
   61882                 : **
   61883                 : ** If a MoveTo operation is pending on the given cursor, then do that
   61884                 : ** MoveTo now.  If no move is pending, check to see if the row has been
   61885                 : ** deleted out from under the cursor and if it has, mark the row as
   61886                 : ** a NULL row.
   61887                 : **
   61888                 : ** If the cursor is already pointing to the correct row and that row has
   61889                 : ** not been deleted out from under the cursor, then this routine is a no-op.
   61890                 : */
   61891         2084922 : SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
   61892         2084922 :   if( p->deferredMoveto ){
   61893                 :     int res, rc;
   61894                 : #ifdef SQLITE_TEST
   61895                 :     extern int sqlite3_search_count;
   61896                 : #endif
   61897           89941 :     assert( p->isTable );
   61898           89941 :     rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
   61899           89940 :     if( rc ) return rc;
   61900           89939 :     p->lastRowid = p->movetoTarget;
   61901           89939 :     if( res!=0 ) return SQLITE_CORRUPT_BKPT;
   61902           89939 :     p->rowidIsValid = 1;
   61903                 : #ifdef SQLITE_TEST
   61904                 :     sqlite3_search_count++;
   61905                 : #endif
   61906           89939 :     p->deferredMoveto = 0;
   61907           89939 :     p->cacheStatus = CACHE_STALE;
   61908         1994981 :   }else if( ALWAYS(p->pCursor) ){
   61909                 :     int hasMoved;
   61910         1994983 :     int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
   61911         1994967 :     if( rc ) return rc;
   61912         1994967 :     if( hasMoved ){
   61913           24188 :       p->cacheStatus = CACHE_STALE;
   61914           24188 :       p->nullRow = 1;
   61915                 :     }
   61916                 :   }
   61917         2084904 :   return SQLITE_OK;
   61918                 : }
   61919                 : 
   61920                 : /*
   61921                 : ** The following functions:
   61922                 : **
   61923                 : ** sqlite3VdbeSerialType()
   61924                 : ** sqlite3VdbeSerialTypeLen()
   61925                 : ** sqlite3VdbeSerialLen()
   61926                 : ** sqlite3VdbeSerialPut()
   61927                 : ** sqlite3VdbeSerialGet()
   61928                 : **
   61929                 : ** encapsulate the code that serializes values for storage in SQLite
   61930                 : ** data and index records. Each serialized value consists of a
   61931                 : ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
   61932                 : ** integer, stored as a varint.
   61933                 : **
   61934                 : ** In an SQLite index record, the serial type is stored directly before
   61935                 : ** the blob of data that it corresponds to. In a table record, all serial
   61936                 : ** types are stored at the start of the record, and the blobs of data at
   61937                 : ** the end. Hence these functions allow the caller to handle the
   61938                 : ** serial-type and data blob seperately.
   61939                 : **
   61940                 : ** The following table describes the various storage classes for data:
   61941                 : **
   61942                 : **   serial type        bytes of data      type
   61943                 : **   --------------     ---------------    ---------------
   61944                 : **      0                     0            NULL
   61945                 : **      1                     1            signed integer
   61946                 : **      2                     2            signed integer
   61947                 : **      3                     3            signed integer
   61948                 : **      4                     4            signed integer
   61949                 : **      5                     6            signed integer
   61950                 : **      6                     8            signed integer
   61951                 : **      7                     8            IEEE float
   61952                 : **      8                     0            Integer constant 0
   61953                 : **      9                     0            Integer constant 1
   61954                 : **     10,11                               reserved for expansion
   61955                 : **    N>=12 and even       (N-12)/2        BLOB
   61956                 : **    N>=13 and odd        (N-13)/2        text
   61957                 : **
   61958                 : ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
   61959                 : ** of SQLite will not understand those serial types.
   61960                 : */
   61961                 : 
   61962                 : /*
   61963                 : ** Return the serial-type for the value stored in pMem.
   61964                 : */
   61965         3436366 : SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
   61966         3436366 :   int flags = pMem->flags;
   61967                 :   int n;
   61968                 : 
   61969         3436366 :   if( flags&MEM_Null ){
   61970          600780 :     return 0;
   61971                 :   }
   61972         2835586 :   if( flags&MEM_Int ){
   61973                 :     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
   61974                 : #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
   61975         1624370 :     i64 i = pMem->u.i;
   61976                 :     u64 u;
   61977         1624370 :     if( file_format>=4 && (i&1)==i ){
   61978          629202 :       return 8+(u32)i;
   61979                 :     }
   61980          995168 :     if( i<0 ){
   61981           13830 :       if( i<(-MAX_6BYTE) ) return 6;
   61982                 :       /* Previous test prevents:  u = -(-9223372036854775808) */
   61983           13254 :       u = -i;
   61984                 :     }else{
   61985          981338 :       u = i;
   61986                 :     }
   61987          994592 :     if( u<=127 ) return 1;
   61988          511728 :     if( u<=32767 ) return 2;
   61989          343533 :     if( u<=8388607 ) return 3;
   61990          340275 :     if( u<=2147483647 ) return 4;
   61991          291669 :     if( u<=MAX_6BYTE ) return 5;
   61992          254967 :     return 6;
   61993                 :   }
   61994         1211216 :   if( flags&MEM_Real ){
   61995            3063 :     return 7;
   61996                 :   }
   61997         1208153 :   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
   61998         1208153 :   n = pMem->n;
   61999         1208153 :   if( flags & MEM_Zero ){
   62000               0 :     n += pMem->u.nZero;
   62001                 :   }
   62002         1208153 :   assert( n>=0 );
   62003         1208153 :   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
   62004                 : }
   62005                 : 
   62006                 : /*
   62007                 : ** Return the length of the data corresponding to the supplied serial-type.
   62008                 : */
   62009         5846620 : SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
   62010         5846620 :   if( serial_type>=12 ){
   62011         2370349 :     return (serial_type-12)/2;
   62012                 :   }else{
   62013                 :     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
   62014         3476271 :     return aSize[serial_type];
   62015                 :   }
   62016                 : }
   62017                 : 
   62018                 : /*
   62019                 : ** If we are on an architecture with mixed-endian floating 
   62020                 : ** points (ex: ARM7) then swap the lower 4 bytes with the 
   62021                 : ** upper 4 bytes.  Return the result.
   62022                 : **
   62023                 : ** For most architectures, this is a no-op.
   62024                 : **
   62025                 : ** (later):  It is reported to me that the mixed-endian problem
   62026                 : ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
   62027                 : ** that early versions of GCC stored the two words of a 64-bit
   62028                 : ** float in the wrong order.  And that error has been propagated
   62029                 : ** ever since.  The blame is not necessarily with GCC, though.
   62030                 : ** GCC might have just copying the problem from a prior compiler.
   62031                 : ** I am also told that newer versions of GCC that follow a different
   62032                 : ** ABI get the byte order right.
   62033                 : **
   62034                 : ** Developers using SQLite on an ARM7 should compile and run their
   62035                 : ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
   62036                 : ** enabled, some asserts below will ensure that the byte order of
   62037                 : ** floating point values is correct.
   62038                 : **
   62039                 : ** (2007-08-30)  Frank van Vugt has studied this problem closely
   62040                 : ** and has send his findings to the SQLite developers.  Frank
   62041                 : ** writes that some Linux kernels offer floating point hardware
   62042                 : ** emulation that uses only 32-bit mantissas instead of a full 
   62043                 : ** 48-bits as required by the IEEE standard.  (This is the
   62044                 : ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
   62045                 : ** byte swapping becomes very complicated.  To avoid problems,
   62046                 : ** the necessary byte swapping is carried out using a 64-bit integer
   62047                 : ** rather than a 64-bit float.  Frank assures us that the code here
   62048                 : ** works for him.  We, the developers, have no way to independently
   62049                 : ** verify this, but Frank seems to know what he is talking about
   62050                 : ** so we trust him.
   62051                 : */
   62052                 : #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
   62053                 : static u64 floatSwap(u64 in){
   62054                 :   union {
   62055                 :     u64 r;
   62056                 :     u32 i[2];
   62057                 :   } u;
   62058                 :   u32 t;
   62059                 : 
   62060                 :   u.r = in;
   62061                 :   t = u.i[0];
   62062                 :   u.i[0] = u.i[1];
   62063                 :   u.i[1] = t;
   62064                 :   return u.r;
   62065                 : }
   62066                 : # define swapMixedEndianFloat(X)  X = floatSwap(X)
   62067                 : #else
   62068                 : # define swapMixedEndianFloat(X)
   62069                 : #endif
   62070                 : 
   62071                 : /*
   62072                 : ** Write the serialized data blob for the value stored in pMem into 
   62073                 : ** buf. It is assumed that the caller has allocated sufficient space.
   62074                 : ** Return the number of bytes written.
   62075                 : **
   62076                 : ** nBuf is the amount of space left in buf[].  nBuf must always be
   62077                 : ** large enough to hold the entire field.  Except, if the field is
   62078                 : ** a blob with a zero-filled tail, then buf[] might be just the right
   62079                 : ** size to hold everything except for the zero-filled tail.  If buf[]
   62080                 : ** is only big enough to hold the non-zero prefix, then only write that
   62081                 : ** prefix into buf[].  But if buf[] is large enough to hold both the
   62082                 : ** prefix and the tail then write the prefix and set the tail to all
   62083                 : ** zeros.
   62084                 : **
   62085                 : ** Return the number of bytes actually written into buf[].  The number
   62086                 : ** of bytes in the zero-filled tail is included in the return value only
   62087                 : ** if those bytes were zeroed in buf[].
   62088                 : */ 
   62089         1145456 : SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
   62090         1145456 :   u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
   62091                 :   u32 len;
   62092                 : 
   62093                 :   /* Integer and Real */
   62094         1145456 :   if( serial_type<=7 && serial_type>0 ){
   62095                 :     u64 v;
   62096                 :     u32 i;
   62097          332744 :     if( serial_type==7 ){
   62098                 :       assert( sizeof(v)==sizeof(pMem->r) );
   62099            1021 :       memcpy(&v, &pMem->r, sizeof(v));
   62100                 :       swapMixedEndianFloat(v);
   62101                 :     }else{
   62102          331723 :       v = pMem->u.i;
   62103                 :     }
   62104          332744 :     len = i = sqlite3VdbeSerialTypeLen(serial_type);
   62105          332744 :     assert( len<=(u32)nBuf );
   62106         1769659 :     while( i-- ){
   62107         1104171 :       buf[i] = (u8)(v&0xFF);
   62108         1104171 :       v >>= 8;
   62109                 :     }
   62110          332744 :     return len;
   62111                 :   }
   62112                 : 
   62113                 :   /* String or blob */
   62114          812712 :   if( serial_type>=12 ){
   62115          402718 :     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
   62116                 :              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
   62117          402718 :     assert( pMem->n<=nBuf );
   62118          402718 :     len = pMem->n;
   62119          402718 :     memcpy(buf, pMem->z, len);
   62120          402718 :     if( pMem->flags & MEM_Zero ){
   62121               0 :       len += pMem->u.nZero;
   62122               0 :       assert( nBuf>=0 );
   62123               0 :       if( len > (u32)nBuf ){
   62124               0 :         len = (u32)nBuf;
   62125                 :       }
   62126               0 :       memset(&buf[pMem->n], 0, len-pMem->n);
   62127                 :     }
   62128          402718 :     return len;
   62129                 :   }
   62130                 : 
   62131                 :   /* NULL or constants 0 or 1 */
   62132          409994 :   return 0;
   62133                 : }
   62134                 : 
   62135                 : /*
   62136                 : ** Deserialize the data blob pointed to by buf as serial type serial_type
   62137                 : ** and store the result in pMem.  Return the number of bytes read.
   62138                 : */ 
   62139         3804292 : SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
   62140                 :   const unsigned char *buf,     /* Buffer to deserialize from */
   62141                 :   u32 serial_type,              /* Serial type to deserialize */
   62142                 :   Mem *pMem                     /* Memory cell to write value into */
   62143                 : ){
   62144         3804292 :   switch( serial_type ){
   62145                 :     case 10:   /* Reserved for future use */
   62146                 :     case 11:   /* Reserved for future use */
   62147                 :     case 0: {  /* NULL */
   62148          336079 :       pMem->flags = MEM_Null;
   62149                 :       break;
   62150                 :     }
   62151                 :     case 1: { /* 1-byte signed integer */
   62152          796105 :       pMem->u.i = (signed char)buf[0];
   62153          796105 :       pMem->flags = MEM_Int;
   62154          796105 :       return 1;
   62155                 :     }
   62156                 :     case 2: { /* 2-byte signed integer */
   62157          172491 :       pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
   62158          172491 :       pMem->flags = MEM_Int;
   62159          172491 :       return 2;
   62160                 :     }
   62161                 :     case 3: { /* 3-byte signed integer */
   62162            1138 :       pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
   62163            1138 :       pMem->flags = MEM_Int;
   62164            1138 :       return 3;
   62165                 :     }
   62166                 :     case 4: { /* 4-byte signed integer */
   62167           28684 :       pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
   62168           28684 :       pMem->flags = MEM_Int;
   62169           28684 :       return 4;
   62170                 :     }
   62171                 :     case 5: { /* 6-byte signed integer */
   62172           24010 :       u64 x = (((signed char)buf[0])<<8) | buf[1];
   62173           24010 :       u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
   62174           24010 :       x = (x<<32) | y;
   62175           24010 :       pMem->u.i = *(i64*)&x;
   62176           24010 :       pMem->flags = MEM_Int;
   62177           24010 :       return 6;
   62178                 :     }
   62179                 :     case 6:   /* 8-byte signed integer */
   62180                 :     case 7: { /* IEEE floating point */
   62181                 :       u64 x;
   62182                 :       u32 y;
   62183                 : #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
   62184                 :       /* Verify that integers and floating point values use the same
   62185                 :       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
   62186                 :       ** defined that 64-bit floating point values really are mixed
   62187                 :       ** endian.
   62188                 :       */
   62189                 :       static const u64 t1 = ((u64)0x3ff00000)<<32;
   62190                 :       static const double r1 = 1.0;
   62191          230663 :       u64 t2 = t1;
   62192                 :       swapMixedEndianFloat(t2);
   62193          230663 :       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
   62194                 : #endif
   62195                 : 
   62196          230663 :       x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
   62197          230663 :       y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
   62198          230663 :       x = (x<<32) | y;
   62199          230663 :       if( serial_type==6 ){
   62200          229142 :         pMem->u.i = *(i64*)&x;
   62201          229142 :         pMem->flags = MEM_Int;
   62202                 :       }else{
   62203                 :         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
   62204                 :         swapMixedEndianFloat(x);
   62205            1521 :         memcpy(&pMem->r, &x, sizeof(x));
   62206            1521 :         pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
   62207                 :       }
   62208          230663 :       return 8;
   62209                 :     }
   62210                 :     case 8:    /* Integer 0 */
   62211                 :     case 9: {  /* Integer 1 */
   62212          594234 :       pMem->u.i = serial_type-8;
   62213          594234 :       pMem->flags = MEM_Int;
   62214          594234 :       return 0;
   62215                 :     }
   62216                 :     default: {
   62217         1620888 :       u32 len = (serial_type-12)/2;
   62218         1620888 :       pMem->z = (char *)buf;
   62219         1620888 :       pMem->n = len;
   62220         1620888 :       pMem->xDel = 0;
   62221         1620888 :       if( serial_type&0x01 ){
   62222         1568427 :         pMem->flags = MEM_Str | MEM_Ephem;
   62223                 :       }else{
   62224           52461 :         pMem->flags = MEM_Blob | MEM_Ephem;
   62225                 :       }
   62226         1620888 :       return len;
   62227                 :     }
   62228                 :   }
   62229          336079 :   return 0;
   62230                 : }
   62231                 : 
   62232                 : /*
   62233                 : ** This routine is used to allocate sufficient space for an UnpackedRecord
   62234                 : ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
   62235                 : ** the first argument is a pointer to KeyInfo structure pKeyInfo.
   62236                 : **
   62237                 : ** The space is either allocated using sqlite3DbMallocRaw() or from within
   62238                 : ** the unaligned buffer passed via the second and third arguments (presumably
   62239                 : ** stack space). If the former, then *ppFree is set to a pointer that should
   62240                 : ** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
   62241                 : ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
   62242                 : ** before returning.
   62243                 : **
   62244                 : ** If an OOM error occurs, NULL is returned.
   62245                 : */
   62246          107357 : SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
   62247                 :   KeyInfo *pKeyInfo,              /* Description of the record */
   62248                 :   char *pSpace,                   /* Unaligned space available */
   62249                 :   int szSpace,                    /* Size of pSpace[] in bytes */
   62250                 :   char **ppFree                   /* OUT: Caller should free this pointer */
   62251                 : ){
   62252                 :   UnpackedRecord *p;              /* Unpacked record to return */
   62253                 :   int nOff;                       /* Increment pSpace by nOff to align it */
   62254                 :   int nByte;                      /* Number of bytes required for *p */
   62255                 : 
   62256                 :   /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
   62257                 :   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
   62258                 :   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
   62259                 :   */
   62260          107357 :   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
   62261          107357 :   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
   62262          107357 :   if( nByte>szSpace+nOff ){
   62263           27292 :     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
   62264           27292 :     *ppFree = (char *)p;
   62265           27292 :     if( !p ) return 0;
   62266                 :   }else{
   62267           80065 :     p = (UnpackedRecord*)&pSpace[nOff];
   62268           80065 :     *ppFree = 0;
   62269                 :   }
   62270                 : 
   62271          107357 :   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
   62272          107357 :   p->pKeyInfo = pKeyInfo;
   62273          107357 :   p->nField = pKeyInfo->nField + 1;
   62274          107357 :   return p;
   62275                 : }
   62276                 : 
   62277                 : /*
   62278                 : ** Given the nKey-byte encoding of a record in pKey[], populate the 
   62279                 : ** UnpackedRecord structure indicated by the fourth argument with the
   62280                 : ** contents of the decoded record.
   62281                 : */ 
   62282          108128 : SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
   62283                 :   KeyInfo *pKeyInfo,     /* Information about the record format */
   62284                 :   int nKey,              /* Size of the binary record */
   62285                 :   const void *pKey,      /* The binary record */
   62286                 :   UnpackedRecord *p      /* Populate this structure before returning. */
   62287                 : ){
   62288          108128 :   const unsigned char *aKey = (const unsigned char *)pKey;
   62289                 :   int d; 
   62290                 :   u32 idx;                        /* Offset in aKey[] to read from */
   62291                 :   u16 u;                          /* Unsigned loop counter */
   62292                 :   u32 szHdr;
   62293          108128 :   Mem *pMem = p->aMem;
   62294                 : 
   62295          108128 :   p->flags = 0;
   62296          108128 :   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   62297          108128 :   idx = getVarint32(aKey, szHdr);
   62298          108128 :   d = szHdr;
   62299          108128 :   u = 0;
   62300          425879 :   while( idx<szHdr && u<p->nField && d<=nKey ){
   62301                 :     u32 serial_type;
   62302                 : 
   62303          209623 :     idx += getVarint32(&aKey[idx], serial_type);
   62304          209623 :     pMem->enc = pKeyInfo->enc;
   62305          209623 :     pMem->db = pKeyInfo->db;
   62306                 :     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
   62307          209623 :     pMem->zMalloc = 0;
   62308          209623 :     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
   62309          209623 :     pMem++;
   62310          209623 :     u++;
   62311                 :   }
   62312          108128 :   assert( u<=pKeyInfo->nField + 1 );
   62313          108128 :   p->nField = u;
   62314          108128 : }
   62315                 : 
   62316                 : /*
   62317                 : ** This function compares the two table rows or index records
   62318                 : ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
   62319                 : ** or positive integer if key1 is less than, equal to or 
   62320                 : ** greater than key2.  The {nKey1, pKey1} key must be a blob
   62321                 : ** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
   62322                 : ** key must be a parsed key such as obtained from
   62323                 : ** sqlite3VdbeParseRecord.
   62324                 : **
   62325                 : ** Key1 and Key2 do not have to contain the same number of fields.
   62326                 : ** The key with fewer fields is usually compares less than the 
   62327                 : ** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
   62328                 : ** and the common prefixes are equal, then key1 is less than key2.
   62329                 : ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
   62330                 : ** equal, then the keys are considered to be equal and
   62331                 : ** the parts beyond the common prefix are ignored.
   62332                 : */
   62333         1218852 : SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
   62334                 :   int nKey1, const void *pKey1, /* Left key */
   62335                 :   UnpackedRecord *pPKey2        /* Right key */
   62336                 : ){
   62337                 :   int d1;            /* Offset into aKey[] of next data element */
   62338                 :   u32 idx1;          /* Offset into aKey[] of next header element */
   62339                 :   u32 szHdr1;        /* Number of bytes in header */
   62340         1218852 :   int i = 0;
   62341                 :   int nField;
   62342         1218852 :   int rc = 0;
   62343         1218852 :   const unsigned char *aKey1 = (const unsigned char *)pKey1;
   62344                 :   KeyInfo *pKeyInfo;
   62345                 :   Mem mem1;
   62346                 : 
   62347         1218852 :   pKeyInfo = pPKey2->pKeyInfo;
   62348         1218852 :   mem1.enc = pKeyInfo->enc;
   62349         1218852 :   mem1.db = pKeyInfo->db;
   62350                 :   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
   62351         1218852 :   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
   62352                 : 
   62353                 :   /* Compilers may complain that mem1.u.i is potentially uninitialized.
   62354                 :   ** We could initialize it, as shown here, to silence those complaints.
   62355                 :   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 
   62356                 :   ** the unnecessary initialization has a measurable negative performance
   62357                 :   ** impact, since this routine is a very high runner.  And so, we choose
   62358                 :   ** to ignore the compiler warnings and leave this variable uninitialized.
   62359                 :   */
   62360                 :   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
   62361                 :   
   62362         1218852 :   idx1 = getVarint32(aKey1, szHdr1);
   62363         1218852 :   d1 = szHdr1;
   62364         1218852 :   nField = pKeyInfo->nField;
   62365         3118236 :   while( idx1<szHdr1 && i<pPKey2->nField ){
   62366                 :     u32 serial_type1;
   62367                 : 
   62368                 :     /* Read the serial types for the next element in each key. */
   62369         1633910 :     idx1 += getVarint32( aKey1+idx1, serial_type1 );
   62370         1633910 :     if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
   62371                 : 
   62372                 :     /* Extract the values to be compared.
   62373                 :     */
   62374         1633910 :     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
   62375                 : 
   62376                 :     /* Do the comparison
   62377                 :     */
   62378         1633910 :     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
   62379                 :                            i<nField ? pKeyInfo->aColl[i] : 0);
   62380         1633910 :     if( rc!=0 ){
   62381          953378 :       assert( mem1.zMalloc==0 );  /* See comment below */
   62382                 : 
   62383                 :       /* Invert the result if we are using DESC sort order. */
   62384          953378 :       if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
   62385            1478 :         rc = -rc;
   62386                 :       }
   62387                 :     
   62388                 :       /* If the PREFIX_SEARCH flag is set and all fields except the final
   62389                 :       ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
   62390                 :       ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
   62391                 :       ** This is used by the OP_IsUnique opcode.
   62392                 :       */
   62393          953378 :       if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
   62394             976 :         assert( idx1==szHdr1 && rc );
   62395             976 :         assert( mem1.flags & MEM_Int );
   62396             976 :         pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
   62397             976 :         pPKey2->rowid = mem1.u.i;
   62398                 :       }
   62399                 :     
   62400          953378 :       return rc;
   62401                 :     }
   62402          680532 :     i++;
   62403                 :   }
   62404                 : 
   62405                 :   /* No memory allocation is ever used on mem1.  Prove this using
   62406                 :   ** the following assert().  If the assert() fails, it indicates a
   62407                 :   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
   62408                 :   */
   62409          265474 :   assert( mem1.zMalloc==0 );
   62410                 : 
   62411                 :   /* rc==0 here means that one of the keys ran out of fields and
   62412                 :   ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
   62413                 :   ** flag is set, then break the tie by treating key2 as larger.
   62414                 :   ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
   62415                 :   ** are considered to be equal.  Otherwise, the longer key is the 
   62416                 :   ** larger.  As it happens, the pPKey2 will always be the longer
   62417                 :   ** if there is a difference.
   62418                 :   */
   62419          265474 :   assert( rc==0 );
   62420          265474 :   if( pPKey2->flags & UNPACKED_INCRKEY ){
   62421          143965 :     rc = -1;
   62422          121509 :   }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
   62423                 :     /* Leave rc==0 */
   62424          104574 :   }else if( idx1<szHdr1 ){
   62425           62495 :     rc = 1;
   62426                 :   }
   62427          265474 :   return rc;
   62428                 : }
   62429                 :  
   62430                 : 
   62431                 : /*
   62432                 : ** pCur points at an index entry created using the OP_MakeRecord opcode.
   62433                 : ** Read the rowid (the last field in the record) and store it in *rowid.
   62434                 : ** Return SQLITE_OK if everything works, or an error code otherwise.
   62435                 : **
   62436                 : ** pCur might be pointing to text obtained from a corrupt database file.
   62437                 : ** So the content cannot be trusted.  Do appropriate checks on the content.
   62438                 : */
   62439          149363 : SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
   62440          149363 :   i64 nCellKey = 0;
   62441                 :   int rc;
   62442                 :   u32 szHdr;        /* Size of the header */
   62443                 :   u32 typeRowid;    /* Serial type of the rowid */
   62444                 :   u32 lenRowid;     /* Size of the rowid */
   62445                 :   Mem m, v;
   62446                 : 
   62447                 :   UNUSED_PARAMETER(db);
   62448                 : 
   62449                 :   /* Get the size of the index entry.  Only indices entries of less
   62450                 :   ** than 2GiB are support - anything large must be database corruption.
   62451                 :   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
   62452                 :   ** this code can safely assume that nCellKey is 32-bits  
   62453                 :   */
   62454          149363 :   assert( sqlite3BtreeCursorIsValid(pCur) );
   62455          149363 :   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
   62456          149363 :   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
   62457          149363 :   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
   62458                 : 
   62459                 :   /* Read in the complete content of the index entry */
   62460          149363 :   memset(&m, 0, sizeof(m));
   62461          149363 :   rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
   62462          149363 :   if( rc ){
   62463               0 :     return rc;
   62464                 :   }
   62465                 : 
   62466                 :   /* The index entry must begin with a header size */
   62467          149363 :   (void)getVarint32((u8*)m.z, szHdr);
   62468                 :   testcase( szHdr==3 );
   62469                 :   testcase( szHdr==m.n );
   62470          149363 :   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
   62471                 :     goto idx_rowid_corruption;
   62472                 :   }
   62473                 : 
   62474                 :   /* The last field of the index should be an integer - the ROWID.
   62475                 :   ** Verify that the last entry really is an integer. */
   62476          149363 :   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
   62477                 :   testcase( typeRowid==1 );
   62478                 :   testcase( typeRowid==2 );
   62479                 :   testcase( typeRowid==3 );
   62480                 :   testcase( typeRowid==4 );
   62481                 :   testcase( typeRowid==5 );
   62482                 :   testcase( typeRowid==6 );
   62483                 :   testcase( typeRowid==8 );
   62484                 :   testcase( typeRowid==9 );
   62485          149363 :   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
   62486                 :     goto idx_rowid_corruption;
   62487                 :   }
   62488          149363 :   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
   62489                 :   testcase( (u32)m.n==szHdr+lenRowid );
   62490          149363 :   if( unlikely((u32)m.n<szHdr+lenRowid) ){
   62491               0 :     goto idx_rowid_corruption;
   62492                 :   }
   62493                 : 
   62494                 :   /* Fetch the integer off the end of the index record */
   62495          149363 :   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
   62496          149363 :   *rowid = v.u.i;
   62497          149363 :   sqlite3VdbeMemRelease(&m);
   62498          149363 :   return SQLITE_OK;
   62499                 : 
   62500                 :   /* Jump here if database corruption is detected after m has been
   62501                 :   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
   62502                 : idx_rowid_corruption:
   62503                 :   testcase( m.zMalloc!=0 );
   62504               0 :   sqlite3VdbeMemRelease(&m);
   62505               0 :   return SQLITE_CORRUPT_BKPT;
   62506                 : }
   62507                 : 
   62508                 : /*
   62509                 : ** Compare the key of the index entry that cursor pC is pointing to against
   62510                 : ** the key string in pUnpacked.  Write into *pRes a number
   62511                 : ** that is negative, zero, or positive if pC is less than, equal to,
   62512                 : ** or greater than pUnpacked.  Return SQLITE_OK on success.
   62513                 : **
   62514                 : ** pUnpacked is either created without a rowid or is truncated so that it
   62515                 : ** omits the rowid at the end.  The rowid at the end of the index entry
   62516                 : ** is ignored as well.  Hence, this routine only compares the prefixes 
   62517                 : ** of the keys prior to the final rowid, not the entire key.
   62518                 : */
   62519          173863 : SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
   62520                 :   VdbeCursor *pC,             /* The cursor to compare against */
   62521                 :   UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
   62522                 :   int *res                    /* Write the comparison result here */
   62523                 : ){
   62524          173863 :   i64 nCellKey = 0;
   62525                 :   int rc;
   62526          173863 :   BtCursor *pCur = pC->pCursor;
   62527                 :   Mem m;
   62528                 : 
   62529          173863 :   assert( sqlite3BtreeCursorIsValid(pCur) );
   62530          173863 :   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
   62531          173863 :   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
   62532                 :   /* nCellKey will always be between 0 and 0xffffffff because of the say
   62533                 :   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
   62534          173863 :   if( nCellKey<=0 || nCellKey>0x7fffffff ){
   62535               0 :     *res = 0;
   62536               0 :     return SQLITE_CORRUPT_BKPT;
   62537                 :   }
   62538          173863 :   memset(&m, 0, sizeof(m));
   62539          173863 :   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
   62540          173863 :   if( rc ){
   62541               0 :     return rc;
   62542                 :   }
   62543          173863 :   assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
   62544          173863 :   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
   62545          173863 :   sqlite3VdbeMemRelease(&m);
   62546          173863 :   return SQLITE_OK;
   62547                 : }
   62548                 : 
   62549                 : /*
   62550                 : ** This routine sets the value to be returned by subsequent calls to
   62551                 : ** sqlite3_changes() on the database handle 'db'. 
   62552                 : */
   62553          103893 : SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
   62554          103893 :   assert( sqlite3_mutex_held(db->mutex) );
   62555          103893 :   db->nChange = nChange;
   62556          103893 :   db->nTotalChange += nChange;
   62557          103893 : }
   62558                 : 
   62559                 : /*
   62560                 : ** Set a flag in the vdbe to update the change counter when it is finalised
   62561                 : ** or reset.
   62562                 : */
   62563           21720 : SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
   62564           21720 :   v->changeCntOn = 1;
   62565           21720 : }
   62566                 : 
   62567                 : /*
   62568                 : ** Mark every prepared statement associated with a database connection
   62569                 : ** as expired.
   62570                 : **
   62571                 : ** An expired statement means that recompilation of the statement is
   62572                 : ** recommend.  Statements expire when things happen that make their
   62573                 : ** programs obsolete.  Removing user-defined functions or collating
   62574                 : ** sequences, or changing an authorization function are the types of
   62575                 : ** things that make prepared statements obsolete.
   62576                 : */
   62577            9183 : SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
   62578                 :   Vdbe *p;
   62579           33524 :   for(p = db->pVdbe; p; p=p->pNext){
   62580           24341 :     p->expired = 1;
   62581                 :   }
   62582            9183 : }
   62583                 : 
   62584                 : /*
   62585                 : ** Return the database associated with the Vdbe.
   62586                 : */
   62587          891558 : SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
   62588          891558 :   return v->db;
   62589                 : }
   62590                 : 
   62591                 : /*
   62592                 : ** Return a pointer to an sqlite3_value structure containing the value bound
   62593                 : ** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
   62594                 : ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
   62595                 : ** constants) to the value before returning it.
   62596                 : **
   62597                 : ** The returned value must be freed by the caller using sqlite3ValueFree().
   62598                 : */
   62599             168 : SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
   62600             168 :   assert( iVar>0 );
   62601             168 :   if( v ){
   62602             152 :     Mem *pMem = &v->aVar[iVar-1];
   62603             152 :     if( 0==(pMem->flags & MEM_Null) ){
   62604             152 :       sqlite3_value *pRet = sqlite3ValueNew(v->db);
   62605             152 :       if( pRet ){
   62606             152 :         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
   62607             152 :         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
   62608             152 :         sqlite3VdbeMemStoreType((Mem *)pRet);
   62609                 :       }
   62610             152 :       return pRet;
   62611                 :     }
   62612                 :   }
   62613              16 :   return 0;
   62614                 : }
   62615                 : 
   62616                 : /*
   62617                 : ** Configure SQL variable iVar so that binding a new value to it signals
   62618                 : ** to sqlite3_reoptimize() that re-preparing the statement may result
   62619                 : ** in a better query plan.
   62620                 : */
   62621             320 : SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
   62622             320 :   assert( iVar>0 );
   62623             320 :   if( iVar>32 ){
   62624               0 :     v->expmask = 0xffffffff;
   62625                 :   }else{
   62626             320 :     v->expmask |= ((u32)1 << (iVar-1));
   62627                 :   }
   62628             320 : }
   62629                 : 
   62630                 : /************** End of vdbeaux.c *********************************************/
   62631                 : /************** Begin file vdbeapi.c *****************************************/
   62632                 : /*
   62633                 : ** 2004 May 26
   62634                 : **
   62635                 : ** The author disclaims copyright to this source code.  In place of
   62636                 : ** a legal notice, here is a blessing:
   62637                 : **
   62638                 : **    May you do good and not evil.
   62639                 : **    May you find forgiveness for yourself and forgive others.
   62640                 : **    May you share freely, never taking more than you give.
   62641                 : **
   62642                 : *************************************************************************
   62643                 : **
   62644                 : ** This file contains code use to implement APIs that are part of the
   62645                 : ** VDBE.
   62646                 : */
   62647                 : 
   62648                 : #ifndef SQLITE_OMIT_DEPRECATED
   62649                 : /*
   62650                 : ** Return TRUE (non-zero) of the statement supplied as an argument needs
   62651                 : ** to be recompiled.  A statement needs to be recompiled whenever the
   62652                 : ** execution environment changes in a way that would alter the program
   62653                 : ** that sqlite3_prepare() generates.  For example, if new functions or
   62654                 : ** collating sequences are registered or if an authorizer function is
   62655                 : ** added or changed.
   62656                 : */
   62657               0 : SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
   62658               0 :   Vdbe *p = (Vdbe*)pStmt;
   62659               0 :   return p==0 || p->expired;
   62660                 : }
   62661                 : #endif
   62662                 : 
   62663                 : /*
   62664                 : ** Check on a Vdbe to make sure it has not been finalized.  Log
   62665                 : ** an error and return true if it has been finalized (or is otherwise
   62666                 : ** invalid).  Return false if it is ok.
   62667                 : */
   62668          881402 : static int vdbeSafety(Vdbe *p){
   62669          881402 :   if( p->db==0 ){
   62670               0 :     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
   62671               0 :     return 1;
   62672                 :   }else{
   62673          881402 :     return 0;
   62674                 :   }
   62675                 : }
   62676          780523 : static int vdbeSafetyNotNull(Vdbe *p){
   62677          780523 :   if( p==0 ){
   62678               1 :     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
   62679               1 :     return 1;
   62680                 :   }else{
   62681          780522 :     return vdbeSafety(p);
   62682                 :   }
   62683                 : }
   62684                 : 
   62685                 : /*
   62686                 : ** The following routine destroys a virtual machine that is created by
   62687                 : ** the sqlite3_compile() routine. The integer returned is an SQLITE_
   62688                 : ** success/failure code that describes the result of executing the virtual
   62689                 : ** machine.
   62690                 : **
   62691                 : ** This routine sets the error code and string returned by
   62692                 : ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
   62693                 : */
   62694          100901 : SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
   62695                 :   int rc;
   62696          100901 :   if( pStmt==0 ){
   62697                 :     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
   62698                 :     ** pointer is a harmless no-op. */
   62699              23 :     rc = SQLITE_OK;
   62700                 :   }else{
   62701          100878 :     Vdbe *v = (Vdbe*)pStmt;
   62702          100878 :     sqlite3 *db = v->db;
   62703                 : #if SQLITE_THREADSAFE
   62704                 :     sqlite3_mutex *mutex;
   62705                 : #endif
   62706          100878 :     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
   62707                 : #if SQLITE_THREADSAFE
   62708          100878 :     mutex = v->db->mutex;
   62709                 : #endif
   62710          100878 :     sqlite3_mutex_enter(mutex);
   62711          100878 :     rc = sqlite3VdbeFinalize(v);
   62712          100878 :     rc = sqlite3ApiExit(db, rc);
   62713          100878 :     sqlite3_mutex_leave(mutex);
   62714                 :   }
   62715          100901 :   return rc;
   62716                 : }
   62717                 : 
   62718                 : /*
   62719                 : ** Terminate the current execution of an SQL statement and reset it
   62720                 : ** back to its starting state so that it can be reused. A success code from
   62721                 : ** the prior execution is returned.
   62722                 : **
   62723                 : ** This routine sets the error code and string returned by
   62724                 : ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
   62725                 : */
   62726          229577 : SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
   62727                 :   int rc;
   62728          229577 :   if( pStmt==0 ){
   62729               6 :     rc = SQLITE_OK;
   62730                 :   }else{
   62731          229571 :     Vdbe *v = (Vdbe*)pStmt;
   62732          229571 :     sqlite3_mutex_enter(v->db->mutex);
   62733          229572 :     rc = sqlite3VdbeReset(v);
   62734          229572 :     sqlite3VdbeRewind(v);
   62735          229572 :     assert( (rc & (v->db->errMask))==rc );
   62736          229572 :     rc = sqlite3ApiExit(v->db, rc);
   62737          229572 :     sqlite3_mutex_leave(v->db->mutex);
   62738                 :   }
   62739          229578 :   return rc;
   62740                 : }
   62741                 : 
   62742                 : /*
   62743                 : ** Set all the parameters in the compiled SQL statement to NULL.
   62744                 : */
   62745          178463 : SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
   62746                 :   int i;
   62747          178463 :   int rc = SQLITE_OK;
   62748          178463 :   Vdbe *p = (Vdbe*)pStmt;
   62749                 : #if SQLITE_THREADSAFE
   62750          178463 :   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
   62751                 : #endif
   62752          178463 :   sqlite3_mutex_enter(mutex);
   62753          685280 :   for(i=0; i<p->nVar; i++){
   62754          506817 :     sqlite3VdbeMemRelease(&p->aVar[i]);
   62755          506817 :     p->aVar[i].flags = MEM_Null;
   62756                 :   }
   62757          178463 :   if( p->isPrepareV2 && p->expmask ){
   62758             100 :     p->expired = 1;
   62759                 :   }
   62760          178463 :   sqlite3_mutex_leave(mutex);
   62761          178463 :   return rc;
   62762                 : }
   62763                 : 
   62764                 : 
   62765                 : /**************************** sqlite3_value_  *******************************
   62766                 : ** The following routines extract information from a Mem or sqlite3_value
   62767                 : ** structure.
   62768                 : */
   62769            5695 : SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
   62770            5695 :   Mem *p = (Mem*)pVal;
   62771            5695 :   if( p->flags & (MEM_Blob|MEM_Str) ){
   62772            5341 :     sqlite3VdbeMemExpandBlob(p);
   62773            5341 :     p->flags &= ~MEM_Str;
   62774            5341 :     p->flags |= MEM_Blob;
   62775            5341 :     return p->n ? p->z : 0;
   62776                 :   }else{
   62777             354 :     return sqlite3_value_text(pVal);
   62778                 :   }
   62779                 : }
   62780          103979 : SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
   62781          103979 :   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
   62782                 : }
   62783           59581 : SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
   62784           59581 :   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
   62785                 : }
   62786           42113 : SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
   62787           42113 :   return sqlite3VdbeRealValue((Mem*)pVal);
   62788                 : }
   62789           99807 : SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
   62790           99807 :   return (int)sqlite3VdbeIntValue((Mem*)pVal);
   62791                 : }
   62792          242222 : SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
   62793          242222 :   return sqlite3VdbeIntValue((Mem*)pVal);
   62794                 : }
   62795          844025 : SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
   62796          844025 :   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
   62797                 : }
   62798                 : #ifndef SQLITE_OMIT_UTF16
   62799          167401 : SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
   62800          167401 :   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
   62801                 : }
   62802               0 : SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
   62803               0 :   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
   62804                 : }
   62805               0 : SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
   62806               0 :   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
   62807                 : }
   62808                 : #endif /* SQLITE_OMIT_UTF16 */
   62809         1294735 : SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
   62810         1294735 :   return pVal->type;
   62811                 : }
   62812                 : 
   62813                 : /**************************** sqlite3_result_  *******************************
   62814                 : ** The following routines are used by user-defined functions to specify
   62815                 : ** the function result.
   62816                 : **
   62817                 : ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
   62818                 : ** result as a string or blob but if the string or blob is too large, it
   62819                 : ** then sets the error code to SQLITE_TOOBIG
   62820                 : */
   62821           50882 : static void setResultStrOrError(
   62822                 :   sqlite3_context *pCtx,  /* Function context */
   62823                 :   const char *z,          /* String pointer */
   62824                 :   int n,                  /* Bytes in string, or negative */
   62825                 :   u8 enc,                 /* Encoding of z.  0 for BLOBs */
   62826                 :   void (*xDel)(void*)     /* Destructor function */
   62827                 : ){
   62828           50882 :   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
   62829               0 :     sqlite3_result_error_toobig(pCtx);
   62830                 :   }
   62831           50882 : }
   62832               0 : SQLITE_API void sqlite3_result_blob(
   62833                 :   sqlite3_context *pCtx, 
   62834                 :   const void *z, 
   62835                 :   int n, 
   62836                 :   void (*xDel)(void *)
   62837                 : ){
   62838               0 :   assert( n>=0 );
   62839               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62840               0 :   setResultStrOrError(pCtx, z, n, 0, xDel);
   62841               0 : }
   62842           12271 : SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
   62843           12271 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62844           12271 :   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
   62845           12271 : }
   62846               0 : SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
   62847               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62848               0 :   pCtx->isError = SQLITE_ERROR;
   62849               0 :   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
   62850               0 : }
   62851                 : #ifndef SQLITE_OMIT_UTF16
   62852               0 : SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
   62853               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62854               0 :   pCtx->isError = SQLITE_ERROR;
   62855               0 :   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
   62856               0 : }
   62857                 : #endif
   62858            2500 : SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
   62859            2500 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62860            2500 :   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
   62861            2500 : }
   62862           17660 : SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
   62863           17660 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62864           17660 :   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
   62865           17660 : }
   62866             135 : SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
   62867             135 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62868             135 :   sqlite3VdbeMemSetNull(&pCtx->s);
   62869             135 : }
   62870           17458 : SQLITE_API void sqlite3_result_text(
   62871                 :   sqlite3_context *pCtx, 
   62872                 :   const char *z, 
   62873                 :   int n,
   62874                 :   void (*xDel)(void *)
   62875                 : ){
   62876           17458 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62877           17458 :   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
   62878           17458 : }
   62879                 : #ifndef SQLITE_OMIT_UTF16
   62880           33424 : SQLITE_API void sqlite3_result_text16(
   62881                 :   sqlite3_context *pCtx, 
   62882                 :   const void *z, 
   62883                 :   int n, 
   62884                 :   void (*xDel)(void *)
   62885                 : ){
   62886           33424 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62887           33424 :   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
   62888           33424 : }
   62889               0 : SQLITE_API void sqlite3_result_text16be(
   62890                 :   sqlite3_context *pCtx, 
   62891                 :   const void *z, 
   62892                 :   int n, 
   62893                 :   void (*xDel)(void *)
   62894                 : ){
   62895               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62896               0 :   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
   62897               0 : }
   62898               0 : SQLITE_API void sqlite3_result_text16le(
   62899                 :   sqlite3_context *pCtx, 
   62900                 :   const void *z, 
   62901                 :   int n, 
   62902                 :   void (*xDel)(void *)
   62903                 : ){
   62904               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62905               0 :   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
   62906               0 : }
   62907                 : #endif /* SQLITE_OMIT_UTF16 */
   62908           13897 : SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
   62909           13897 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62910           13897 :   sqlite3VdbeMemCopy(&pCtx->s, pValue);
   62911           13897 : }
   62912               0 : SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
   62913               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62914               0 :   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
   62915               0 : }
   62916               0 : SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
   62917               0 :   pCtx->isError = errCode;
   62918               0 :   if( pCtx->s.flags & MEM_Null ){
   62919               0 :     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
   62920                 :                          SQLITE_UTF8, SQLITE_STATIC);
   62921                 :   }
   62922               0 : }
   62923                 : 
   62924                 : /* Force an SQLITE_TOOBIG error. */
   62925               0 : SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
   62926               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62927               0 :   pCtx->isError = SQLITE_TOOBIG;
   62928               0 :   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
   62929                 :                        SQLITE_UTF8, SQLITE_STATIC);
   62930               0 : }
   62931                 : 
   62932                 : /* An SQLITE_NOMEM error. */
   62933               0 : SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
   62934               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   62935               0 :   sqlite3VdbeMemSetNull(&pCtx->s);
   62936               0 :   pCtx->isError = SQLITE_NOMEM;
   62937               0 :   pCtx->s.db->mallocFailed = 1;
   62938               0 : }
   62939                 : 
   62940                 : /*
   62941                 : ** This function is called after a transaction has been committed. It 
   62942                 : ** invokes callbacks registered with sqlite3_wal_hook() as required.
   62943                 : */
   62944          196621 : static int doWalCallbacks(sqlite3 *db){
   62945          196621 :   int rc = SQLITE_OK;
   62946                 : #ifndef SQLITE_OMIT_WAL
   62947                 :   int i;
   62948          590030 :   for(i=0; i<db->nDb; i++){
   62949          393409 :     Btree *pBt = db->aDb[i].pBt;
   62950          393409 :     if( pBt ){
   62951          270810 :       int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
   62952          270810 :       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
   62953           25040 :         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
   62954                 :       }
   62955                 :     }
   62956                 :   }
   62957                 : #endif
   62958          196621 :   return rc;
   62959                 : }
   62960                 : 
   62961                 : /*
   62962                 : ** Execute the statement pStmt, either until a row of data is ready, the
   62963                 : ** statement is completely executed or an error occurs.
   62964                 : **
   62965                 : ** This routine implements the bulk of the logic behind the sqlite_step()
   62966                 : ** API.  The only thing omitted is the automatic recompile if a 
   62967                 : ** schema change has occurred.  That detail is handled by the
   62968                 : ** outer sqlite3_step() wrapper procedure.
   62969                 : */
   62970          359102 : static int sqlite3Step(Vdbe *p){
   62971                 :   sqlite3 *db;
   62972                 :   int rc;
   62973                 : 
   62974          359102 :   assert(p);
   62975          359102 :   if( p->magic!=VDBE_MAGIC_RUN ){
   62976                 :     /* We used to require that sqlite3_reset() be called before retrying
   62977                 :     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
   62978                 :     ** with version 3.7.0, we changed this so that sqlite3_reset() would
   62979                 :     ** be called automatically instead of throwing the SQLITE_MISUSE error.
   62980                 :     ** This "automatic-reset" change is not technically an incompatibility, 
   62981                 :     ** since any application that receives an SQLITE_MISUSE is broken by
   62982                 :     ** definition.
   62983                 :     **
   62984                 :     ** Nevertheless, some published applications that were originally written
   62985                 :     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 
   62986                 :     ** returns, and those were broken by the automatic-reset change.  As a
   62987                 :     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
   62988                 :     ** legacy behavior of returning SQLITE_MISUSE for cases where the 
   62989                 :     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
   62990                 :     ** or SQLITE_BUSY error.
   62991                 :     */
   62992                 : #ifdef SQLITE_OMIT_AUTORESET
   62993                 :     if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
   62994                 :       sqlite3_reset((sqlite3_stmt*)p);
   62995                 :     }else{
   62996                 :       return SQLITE_MISUSE_BKPT;
   62997                 :     }
   62998                 : #else
   62999               0 :     sqlite3_reset((sqlite3_stmt*)p);
   63000                 : #endif
   63001                 :   }
   63002                 : 
   63003                 :   /* Check that malloc() has not failed. If it has, return early. */
   63004          359102 :   db = p->db;
   63005          359102 :   if( db->mallocFailed ){
   63006               0 :     p->rc = SQLITE_NOMEM;
   63007               0 :     return SQLITE_NOMEM;
   63008                 :   }
   63009                 : 
   63010          359102 :   if( p->pc<=0 && p->expired ){
   63011            1234 :     p->rc = SQLITE_SCHEMA;
   63012            1234 :     rc = SQLITE_ERROR;
   63013            1234 :     goto end_of_step;
   63014                 :   }
   63015          357868 :   if( p->pc<0 ){
   63016                 :     /* If there are no other statements currently running, then
   63017                 :     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
   63018                 :     ** from interrupting a statement that has not yet started.
   63019                 :     */
   63020          227536 :     if( db->activeVdbeCnt==0 ){
   63021          198022 :       db->u1.isInterrupted = 0;
   63022                 :     }
   63023                 : 
   63024          227536 :     assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 );
   63025                 : 
   63026                 : #ifndef SQLITE_OMIT_TRACE
   63027          227536 :     if( db->xProfile && !db->init.busy ){
   63028               0 :       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
   63029                 :     }
   63030                 : #endif
   63031                 : 
   63032          227536 :     db->activeVdbeCnt++;
   63033          227536 :     if( p->readOnly==0 ) db->writeVdbeCnt++;
   63034          227536 :     p->pc = 0;
   63035                 :   }
   63036                 : #ifndef SQLITE_OMIT_EXPLAIN
   63037          357868 :   if( p->explain ){
   63038               0 :     rc = sqlite3VdbeList(p);
   63039                 :   }else
   63040                 : #endif /* SQLITE_OMIT_EXPLAIN */
   63041                 :   {
   63042          357868 :     db->vdbeExecCnt++;
   63043          357868 :     rc = sqlite3VdbeExec(p);
   63044          357869 :     db->vdbeExecCnt--;
   63045                 :   }
   63046                 : 
   63047                 : #ifndef SQLITE_OMIT_TRACE
   63048                 :   /* Invoke the profile callback if there is one
   63049                 :   */
   63050          357869 :   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
   63051                 :     sqlite3_int64 iNow;
   63052               0 :     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
   63053               0 :     db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
   63054                 :   }
   63055                 : #endif
   63056                 : 
   63057          357869 :   if( rc==SQLITE_DONE ){
   63058          196621 :     assert( p->rc==SQLITE_OK );
   63059          196621 :     p->rc = doWalCallbacks(db);
   63060          196621 :     if( p->rc!=SQLITE_OK ){
   63061               0 :       rc = SQLITE_ERROR;
   63062                 :     }
   63063                 :   }
   63064                 : 
   63065          357869 :   db->errCode = rc;
   63066          357869 :   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
   63067               0 :     p->rc = SQLITE_NOMEM;
   63068                 :   }
   63069                 : end_of_step:
   63070                 :   /* At this point local variable rc holds the value that should be 
   63071                 :   ** returned if this statement was compiled using the legacy 
   63072                 :   ** sqlite3_prepare() interface. According to the docs, this can only
   63073                 :   ** be one of the values in the first assert() below. Variable p->rc 
   63074                 :   ** contains the value that would be returned if sqlite3_finalize() 
   63075                 :   ** were called on statement p.
   63076                 :   */
   63077          359103 :   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR 
   63078                 :        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
   63079                 :   );
   63080          359103 :   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
   63081          359103 :   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
   63082                 :     /* If this statement was prepared using sqlite3_prepare_v2(), and an
   63083                 :     ** error has occured, then return the error code in p->rc to the
   63084                 :     ** caller. Set the error code in the database handle to the same value.
   63085                 :     */ 
   63086            1883 :     rc = sqlite3VdbeTransferError(p);
   63087                 :   }
   63088          359103 :   return (rc&db->errMask);
   63089                 : }
   63090                 : 
   63091                 : /*
   63092                 : ** The maximum number of times that a statement will try to reparse
   63093                 : ** itself before giving up and returning SQLITE_SCHEMA.
   63094                 : */
   63095                 : #ifndef SQLITE_MAX_SCHEMA_RETRY
   63096                 : # define SQLITE_MAX_SCHEMA_RETRY 5
   63097                 : #endif
   63098                 : 
   63099                 : /*
   63100                 : ** This is the top-level implementation of sqlite3_step().  Call
   63101                 : ** sqlite3Step() to do most of the work.  If a schema error occurs,
   63102                 : ** call sqlite3Reprepare() and try again.
   63103                 : */
   63104          357583 : SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
   63105          357583 :   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
   63106          357583 :   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
   63107          357583 :   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
   63108          357583 :   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
   63109                 :   sqlite3 *db;             /* The database connection */
   63110                 : 
   63111          357583 :   if( vdbeSafetyNotNull(v) ){
   63112               1 :     return SQLITE_MISUSE_BKPT;
   63113                 :   }
   63114          357582 :   db = v->db;
   63115          357582 :   sqlite3_mutex_enter(db->mutex);
   63116          357581 :   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   63117            1521 :          && cnt++ < SQLITE_MAX_SCHEMA_RETRY
   63118            1521 :          && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
   63119            1521 :     sqlite3_reset(pStmt);
   63120            1521 :     assert( v->expired==0 );
   63121                 :   }
   63122          357582 :   if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
   63123                 :     /* This case occurs after failing to recompile an sql statement. 
   63124                 :     ** The error message from the SQL compiler has already been loaded 
   63125                 :     ** into the database handle. This block copies the error message 
   63126                 :     ** from the database handle into the statement and sets the statement
   63127                 :     ** program counter to 0 to ensure that when the statement is 
   63128                 :     ** finalized or reset the parser error message is available via
   63129                 :     ** sqlite3_errmsg() and sqlite3_errcode().
   63130                 :     */
   63131               0 :     const char *zErr = (const char *)sqlite3_value_text(db->pErr); 
   63132               0 :     sqlite3DbFree(db, v->zErrMsg);
   63133               0 :     if( !db->mallocFailed ){
   63134               0 :       v->zErrMsg = sqlite3DbStrDup(db, zErr);
   63135               0 :       v->rc = rc2;
   63136                 :     } else {
   63137               0 :       v->zErrMsg = 0;
   63138               0 :       v->rc = rc = SQLITE_NOMEM;
   63139                 :     }
   63140                 :   }
   63141          357582 :   rc = sqlite3ApiExit(db, rc);
   63142          357582 :   sqlite3_mutex_leave(db->mutex);
   63143          357581 :   return rc;
   63144                 : }
   63145                 : 
   63146                 : /*
   63147                 : ** Extract the user data from a sqlite3_context structure and return a
   63148                 : ** pointer to it.
   63149                 : */
   63150           94031 : SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
   63151           94031 :   assert( p && p->pFunc );
   63152           94031 :   return p->pFunc->pUserData;
   63153                 : }
   63154                 : 
   63155                 : /*
   63156                 : ** Extract the user data from a sqlite3_context structure and return a
   63157                 : ** pointer to it.
   63158                 : **
   63159                 : ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
   63160                 : ** returns a copy of the pointer to the database connection (the 1st
   63161                 : ** parameter) of the sqlite3_create_function() and
   63162                 : ** sqlite3_create_function16() routines that originally registered the
   63163                 : ** application defined function.
   63164                 : */
   63165           23068 : SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
   63166           23068 :   assert( p && p->pFunc );
   63167           23068 :   return p->s.db;
   63168                 : }
   63169                 : 
   63170                 : /*
   63171                 : ** The following is the implementation of an SQL function that always
   63172                 : ** fails with an error message stating that the function is used in the
   63173                 : ** wrong context.  The sqlite3_overload_function() API might construct
   63174                 : ** SQL function that use this routine so that the functions will exist
   63175                 : ** for name resolution but are actually overloaded by the xFindFunction
   63176                 : ** method of virtual tables.
   63177                 : */
   63178               0 : SQLITE_PRIVATE void sqlite3InvalidFunction(
   63179                 :   sqlite3_context *context,  /* The function calling context */
   63180                 :   int NotUsed,               /* Number of arguments to the function */
   63181                 :   sqlite3_value **NotUsed2   /* Value of each argument */
   63182                 : ){
   63183               0 :   const char *zName = context->pFunc->zName;
   63184                 :   char *zErr;
   63185                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   63186               0 :   zErr = sqlite3_mprintf(
   63187                 :       "unable to use function %s in the requested context", zName);
   63188               0 :   sqlite3_result_error(context, zErr, -1);
   63189               0 :   sqlite3_free(zErr);
   63190               0 : }
   63191                 : 
   63192                 : /*
   63193                 : ** Allocate or return the aggregate context for a user function.  A new
   63194                 : ** context is allocated on the first call.  Subsequent calls return the
   63195                 : ** same context that was returned on prior calls.
   63196                 : */
   63197          791611 : SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
   63198                 :   Mem *pMem;
   63199          791611 :   assert( p && p->pFunc && p->pFunc->xStep );
   63200          791611 :   assert( sqlite3_mutex_held(p->s.db->mutex) );
   63201          791611 :   pMem = p->pMem;
   63202                 :   testcase( nByte<0 );
   63203          791611 :   if( (pMem->flags & MEM_Agg)==0 ){
   63204           17255 :     if( nByte<=0 ){
   63205            6342 :       sqlite3VdbeMemReleaseExternal(pMem);
   63206            6342 :       pMem->flags = MEM_Null;
   63207            6342 :       pMem->z = 0;
   63208                 :     }else{
   63209           10913 :       sqlite3VdbeMemGrow(pMem, nByte, 0);
   63210           10913 :       pMem->flags = MEM_Agg;
   63211           10913 :       pMem->u.pDef = p->pFunc;
   63212           10913 :       if( pMem->z ){
   63213           10913 :         memset(pMem->z, 0, nByte);
   63214                 :       }
   63215                 :     }
   63216                 :   }
   63217          791611 :   return (void*)pMem->z;
   63218                 : }
   63219                 : 
   63220                 : /*
   63221                 : ** Return the auxilary data pointer, if any, for the iArg'th argument to
   63222                 : ** the user-function defined by pCtx.
   63223                 : */
   63224               0 : SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   63225                 :   VdbeFunc *pVdbeFunc;
   63226                 : 
   63227               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   63228               0 :   pVdbeFunc = pCtx->pVdbeFunc;
   63229               0 :   if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
   63230               0 :     return 0;
   63231                 :   }
   63232               0 :   return pVdbeFunc->apAux[iArg].pAux;
   63233                 : }
   63234                 : 
   63235                 : /*
   63236                 : ** Set the auxilary data pointer and delete function, for the iArg'th
   63237                 : ** argument to the user-function defined by pCtx. Any previous value is
   63238                 : ** deleted by calling the delete function specified when it was set.
   63239                 : */
   63240               0 : SQLITE_API void sqlite3_set_auxdata(
   63241                 :   sqlite3_context *pCtx, 
   63242                 :   int iArg, 
   63243                 :   void *pAux, 
   63244                 :   void (*xDelete)(void*)
   63245                 : ){
   63246                 :   struct AuxData *pAuxData;
   63247                 :   VdbeFunc *pVdbeFunc;
   63248               0 :   if( iArg<0 ) goto failed;
   63249                 : 
   63250               0 :   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   63251               0 :   pVdbeFunc = pCtx->pVdbeFunc;
   63252               0 :   if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
   63253               0 :     int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
   63254               0 :     int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
   63255               0 :     pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
   63256               0 :     if( !pVdbeFunc ){
   63257               0 :       goto failed;
   63258                 :     }
   63259               0 :     pCtx->pVdbeFunc = pVdbeFunc;
   63260               0 :     memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
   63261               0 :     pVdbeFunc->nAux = iArg+1;
   63262               0 :     pVdbeFunc->pFunc = pCtx->pFunc;
   63263                 :   }
   63264                 : 
   63265               0 :   pAuxData = &pVdbeFunc->apAux[iArg];
   63266               0 :   if( pAuxData->pAux && pAuxData->xDelete ){
   63267               0 :     pAuxData->xDelete(pAuxData->pAux);
   63268                 :   }
   63269               0 :   pAuxData->pAux = pAux;
   63270               0 :   pAuxData->xDelete = xDelete;
   63271               0 :   return;
   63272                 : 
   63273                 : failed:
   63274               0 :   if( xDelete ){
   63275               0 :     xDelete(pAux);
   63276                 :   }
   63277                 : }
   63278                 : 
   63279                 : #ifndef SQLITE_OMIT_DEPRECATED
   63280                 : /*
   63281                 : ** Return the number of times the Step function of a aggregate has been 
   63282                 : ** called.
   63283                 : **
   63284                 : ** This function is deprecated.  Do not use it for new code.  It is
   63285                 : ** provide only to avoid breaking legacy code.  New aggregate function
   63286                 : ** implementations should keep their own counts within their aggregate
   63287                 : ** context.
   63288                 : */
   63289           33812 : SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
   63290           33812 :   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
   63291           33812 :   return p->pMem->n;
   63292                 : }
   63293                 : #endif
   63294                 : 
   63295                 : /*
   63296                 : ** Return the number of columns in the result set for the statement pStmt.
   63297                 : */
   63298          636077 : SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
   63299          636077 :   Vdbe *pVm = (Vdbe *)pStmt;
   63300          636077 :   return pVm ? pVm->nResColumn : 0;
   63301                 : }
   63302                 : 
   63303                 : /*
   63304                 : ** Return the number of values available from the current row of the
   63305                 : ** currently executing statement pStmt.
   63306                 : */
   63307               7 : SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
   63308               7 :   Vdbe *pVm = (Vdbe *)pStmt;
   63309               7 :   if( pVm==0 || pVm->pResultSet==0 ) return 0;
   63310               4 :   return pVm->nResColumn;
   63311                 : }
   63312                 : 
   63313                 : 
   63314                 : /*
   63315                 : ** Check to see if column iCol of the given statement is valid.  If
   63316                 : ** it is, return a pointer to the Mem for the value of that column.
   63317                 : ** If iCol is not valid, return a pointer to a Mem which has a value
   63318                 : ** of NULL.
   63319                 : */
   63320         1351323 : static Mem *columnMem(sqlite3_stmt *pStmt, int i){
   63321                 :   Vdbe *pVm;
   63322                 :   Mem *pOut;
   63323                 : 
   63324         1351323 :   pVm = (Vdbe *)pStmt;
   63325         1351323 :   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
   63326         1351301 :     sqlite3_mutex_enter(pVm->db->mutex);
   63327         1351307 :     pOut = &pVm->pResultSet[i];
   63328                 :   }else{
   63329                 :     /* If the value passed as the second argument is out of range, return
   63330                 :     ** a pointer to the following static Mem object which contains the
   63331                 :     ** value SQL NULL. Even though the Mem structure contains an element
   63332                 :     ** of type i64, on certain architectures (x86) with certain compiler
   63333                 :     ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
   63334                 :     ** instead of an 8-byte one. This all works fine, except that when
   63335                 :     ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
   63336                 :     ** that a Mem structure is located on an 8-byte boundary. To prevent
   63337                 :     ** these assert()s from failing, when building with SQLITE_DEBUG defined
   63338                 :     ** using gcc, we force nullMem to be 8-byte aligned using the magical
   63339                 :     ** __attribute__((aligned(8))) macro.  */
   63340                 :     static const Mem nullMem 
   63341                 : #if defined(SQLITE_DEBUG) && defined(__GNUC__)
   63342                 :       __attribute__((aligned(8))) 
   63343                 : #endif
   63344                 :       = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
   63345                 : #ifdef SQLITE_DEBUG
   63346                 :          0, 0,  /* pScopyFrom, pFiller */
   63347                 : #endif
   63348                 :          0, 0 };
   63349                 : 
   63350              22 :     if( pVm && ALWAYS(pVm->db) ){
   63351               0 :       sqlite3_mutex_enter(pVm->db->mutex);
   63352               0 :       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
   63353                 :     }
   63354               0 :     pOut = (Mem*)&nullMem;
   63355                 :   }
   63356         1351307 :   return pOut;
   63357                 : }
   63358                 : 
   63359                 : /*
   63360                 : ** This function is called after invoking an sqlite3_value_XXX function on a 
   63361                 : ** column value (i.e. a value returned by evaluating an SQL expression in the
   63362                 : ** select list of a SELECT statement) that may cause a malloc() failure. If 
   63363                 : ** malloc() has failed, the threads mallocFailed flag is cleared and the result
   63364                 : ** code of statement pStmt set to SQLITE_NOMEM.
   63365                 : **
   63366                 : ** Specifically, this is called from within:
   63367                 : **
   63368                 : **     sqlite3_column_int()
   63369                 : **     sqlite3_column_int64()
   63370                 : **     sqlite3_column_text()
   63371                 : **     sqlite3_column_text16()
   63372                 : **     sqlite3_column_real()
   63373                 : **     sqlite3_column_bytes()
   63374                 : **     sqlite3_column_bytes16()
   63375                 : **     sqiite3_column_blob()
   63376                 : */
   63377         1351249 : static void columnMallocFailure(sqlite3_stmt *pStmt)
   63378                 : {
   63379                 :   /* If malloc() failed during an encoding conversion within an
   63380                 :   ** sqlite3_column_XXX API, then set the return code of the statement to
   63381                 :   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
   63382                 :   ** and _finalize() will return NOMEM.
   63383                 :   */
   63384         1351249 :   Vdbe *p = (Vdbe *)pStmt;
   63385         1351249 :   if( p ){
   63386         1351249 :     p->rc = sqlite3ApiExit(p->db, p->rc);
   63387         1351240 :     sqlite3_mutex_leave(p->db->mutex);
   63388                 :   }
   63389         1351247 : }
   63390                 : 
   63391                 : /**************************** sqlite3_column_  *******************************
   63392                 : ** The following routines are used to access elements of the current row
   63393                 : ** in the result set.
   63394                 : */
   63395            5695 : SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
   63396                 :   const void *val;
   63397            5695 :   val = sqlite3_value_blob( columnMem(pStmt,i) );
   63398                 :   /* Even though there is no encoding conversion, value_blob() might
   63399                 :   ** need to call malloc() to expand the result of a zeroblob() 
   63400                 :   ** expression. 
   63401                 :   */
   63402            5695 :   columnMallocFailure(pStmt);
   63403            5695 :   return val;
   63404                 : }
   63405           95233 : SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
   63406           95233 :   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
   63407           95233 :   columnMallocFailure(pStmt);
   63408           95233 :   return val;
   63409                 : }
   63410           26389 : SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
   63411           26389 :   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
   63412           26389 :   columnMallocFailure(pStmt);
   63413           26389 :   return val;
   63414                 : }
   63415           29810 : SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
   63416           29810 :   double val = sqlite3_value_double( columnMem(pStmt,i) );
   63417           29810 :   columnMallocFailure(pStmt);
   63418           29810 :   return val;
   63419                 : }
   63420           86451 : SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
   63421           86451 :   int val = sqlite3_value_int( columnMem(pStmt,i) );
   63422           86451 :   columnMallocFailure(pStmt);
   63423           86451 :   return val;
   63424                 : }
   63425          223842 : SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
   63426          223842 :   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
   63427          223840 :   columnMallocFailure(pStmt);
   63428          223843 :   return val;
   63429                 : }
   63430          281797 : SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
   63431          281797 :   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
   63432          281797 :   columnMallocFailure(pStmt);
   63433          281797 :   return val;
   63434                 : }
   63435               4 : SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
   63436               4 :   Mem *pOut = columnMem(pStmt, i);
   63437               4 :   if( pOut->flags&MEM_Static ){
   63438               0 :     pOut->flags &= ~MEM_Static;
   63439               0 :     pOut->flags |= MEM_Ephem;
   63440                 :   }
   63441               4 :   columnMallocFailure(pStmt);
   63442               4 :   return (sqlite3_value *)pOut;
   63443                 : }
   63444                 : #ifndef SQLITE_OMIT_UTF16
   63445          133033 : SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
   63446          133033 :   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
   63447          133033 :   columnMallocFailure(pStmt);
   63448          133033 :   return val;
   63449                 : }
   63450                 : #endif /* SQLITE_OMIT_UTF16 */
   63451          469142 : SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
   63452          469142 :   int iType = sqlite3_value_type( columnMem(pStmt,i) );
   63453          469135 :   columnMallocFailure(pStmt);
   63454          469138 :   return iType;
   63455                 : }
   63456                 : 
   63457                 : /* The following function is experimental and subject to change or
   63458                 : ** removal */
   63459                 : /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){
   63460                 : **  return sqlite3_value_numeric_type( columnMem(pStmt,i) );
   63461                 : **}
   63462                 : */
   63463                 : 
   63464                 : /*
   63465                 : ** Convert the N-th element of pStmt->pColName[] into a string using
   63466                 : ** xFunc() then return that string.  If N is out of range, return 0.
   63467                 : **
   63468                 : ** There are up to 5 names for each column.  useType determines which
   63469                 : ** name is returned.  Here are the names:
   63470                 : **
   63471                 : **    0      The column name as it should be displayed for output
   63472                 : **    1      The datatype name for the column
   63473                 : **    2      The name of the database that the column derives from
   63474                 : **    3      The name of the table that the column derives from
   63475                 : **    4      The name of the table column that the result column derives from
   63476                 : **
   63477                 : ** If the result is not a simple column reference (if it is an expression
   63478                 : ** or a constant) then useTypes 2, 3, and 4 return NULL.
   63479                 : */
   63480          504534 : static const void *columnName(
   63481                 :   sqlite3_stmt *pStmt,
   63482                 :   int N,
   63483                 :   const void *(*xFunc)(Mem*),
   63484                 :   int useType
   63485                 : ){
   63486          504534 :   const void *ret = 0;
   63487          504534 :   Vdbe *p = (Vdbe *)pStmt;
   63488                 :   int n;
   63489          504534 :   sqlite3 *db = p->db;
   63490                 :   
   63491          504534 :   assert( db!=0 );
   63492          504534 :   n = sqlite3_column_count(pStmt);
   63493          504534 :   if( N<n && N>=0 ){
   63494          504534 :     N += useType*n;
   63495          504534 :     sqlite3_mutex_enter(db->mutex);
   63496          504534 :     assert( db->mallocFailed==0 );
   63497          504534 :     ret = xFunc(&p->aColName[N]);
   63498                 :      /* A malloc may have failed inside of the xFunc() call. If this
   63499                 :     ** is the case, clear the mallocFailed flag and return NULL.
   63500                 :     */
   63501          504534 :     if( db->mallocFailed ){
   63502               0 :       db->mallocFailed = 0;
   63503               0 :       ret = 0;
   63504                 :     }
   63505          504534 :     sqlite3_mutex_leave(db->mutex);
   63506                 :   }
   63507          504534 :   return ret;
   63508                 : }
   63509                 : 
   63510                 : /*
   63511                 : ** Return the name of the Nth column of the result set returned by SQL
   63512                 : ** statement pStmt.
   63513                 : */
   63514          504486 : SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
   63515          504486 :   return columnName(
   63516                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
   63517                 : }
   63518                 : #ifndef SQLITE_OMIT_UTF16
   63519              46 : SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
   63520              46 :   return columnName(
   63521                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
   63522                 : }
   63523                 : #endif
   63524                 : 
   63525                 : /*
   63526                 : ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
   63527                 : ** not define OMIT_DECLTYPE.
   63528                 : */
   63529                 : #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
   63530                 : # error "Must not define both SQLITE_OMIT_DECLTYPE \
   63531                 :          and SQLITE_ENABLE_COLUMN_METADATA"
   63532                 : #endif
   63533                 : 
   63534                 : #ifndef SQLITE_OMIT_DECLTYPE
   63535                 : /*
   63536                 : ** Return the column declaration type (if applicable) of the 'i'th column
   63537                 : ** of the result set of SQL statement pStmt.
   63538                 : */
   63539               2 : SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
   63540               2 :   return columnName(
   63541                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
   63542                 : }
   63543                 : #ifndef SQLITE_OMIT_UTF16
   63544               0 : SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
   63545               0 :   return columnName(
   63546                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
   63547                 : }
   63548                 : #endif /* SQLITE_OMIT_UTF16 */
   63549                 : #endif /* SQLITE_OMIT_DECLTYPE */
   63550                 : 
   63551                 : #ifdef SQLITE_ENABLE_COLUMN_METADATA
   63552                 : /*
   63553                 : ** Return the name of the database from which a result column derives.
   63554                 : ** NULL is returned if the result column is an expression or constant or
   63555                 : ** anything else which is not an unabiguous reference to a database column.
   63556                 : */
   63557                 : SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
   63558                 :   return columnName(
   63559                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
   63560                 : }
   63561                 : #ifndef SQLITE_OMIT_UTF16
   63562                 : SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
   63563                 :   return columnName(
   63564                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
   63565                 : }
   63566                 : #endif /* SQLITE_OMIT_UTF16 */
   63567                 : 
   63568                 : /*
   63569                 : ** Return the name of the table from which a result column derives.
   63570                 : ** NULL is returned if the result column is an expression or constant or
   63571                 : ** anything else which is not an unabiguous reference to a database column.
   63572                 : */
   63573                 : SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
   63574                 :   return columnName(
   63575                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
   63576                 : }
   63577                 : #ifndef SQLITE_OMIT_UTF16
   63578                 : SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
   63579                 :   return columnName(
   63580                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
   63581                 : }
   63582                 : #endif /* SQLITE_OMIT_UTF16 */
   63583                 : 
   63584                 : /*
   63585                 : ** Return the name of the table column from which a result column derives.
   63586                 : ** NULL is returned if the result column is an expression or constant or
   63587                 : ** anything else which is not an unabiguous reference to a database column.
   63588                 : */
   63589                 : SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
   63590                 :   return columnName(
   63591                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
   63592                 : }
   63593                 : #ifndef SQLITE_OMIT_UTF16
   63594                 : SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
   63595                 :   return columnName(
   63596                 :       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
   63597                 : }
   63598                 : #endif /* SQLITE_OMIT_UTF16 */
   63599                 : #endif /* SQLITE_ENABLE_COLUMN_METADATA */
   63600                 : 
   63601                 : 
   63602                 : /******************************* sqlite3_bind_  ***************************
   63603                 : ** 
   63604                 : ** Routines used to attach values to wildcards in a compiled SQL statement.
   63605                 : */
   63606                 : /*
   63607                 : ** Unbind the value bound to variable i in virtual machine p. This is the 
   63608                 : ** the same as binding a NULL value to the column. If the "i" parameter is
   63609                 : ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
   63610                 : **
   63611                 : ** A successful evaluation of this routine acquires the mutex on p.
   63612                 : ** the mutex is released if any kind of error occurs.
   63613                 : **
   63614                 : ** The error code stored in database p->db is overwritten with the return
   63615                 : ** value in any case.
   63616                 : */
   63617          422939 : static int vdbeUnbind(Vdbe *p, int i){
   63618                 :   Mem *pVar;
   63619          422939 :   if( vdbeSafetyNotNull(p) ){
   63620               0 :     return SQLITE_MISUSE_BKPT;
   63621                 :   }
   63622          422940 :   sqlite3_mutex_enter(p->db->mutex);
   63623          422942 :   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
   63624               0 :     sqlite3Error(p->db, SQLITE_MISUSE, 0);
   63625               0 :     sqlite3_mutex_leave(p->db->mutex);
   63626               0 :     sqlite3_log(SQLITE_MISUSE, 
   63627                 :         "bind on a busy prepared statement: [%s]", p->zSql);
   63628               0 :     return SQLITE_MISUSE_BKPT;
   63629                 :   }
   63630          422942 :   if( i<1 || i>p->nVar ){
   63631               1 :     sqlite3Error(p->db, SQLITE_RANGE, 0);
   63632               1 :     sqlite3_mutex_leave(p->db->mutex);
   63633               1 :     return SQLITE_RANGE;
   63634                 :   }
   63635          422941 :   i--;
   63636          422941 :   pVar = &p->aVar[i];
   63637          422941 :   sqlite3VdbeMemRelease(pVar);
   63638          422941 :   pVar->flags = MEM_Null;
   63639          422941 :   sqlite3Error(p->db, SQLITE_OK, 0);
   63640                 : 
   63641                 :   /* If the bit corresponding to this variable in Vdbe.expmask is set, then 
   63642                 :   ** binding a new value to this variable invalidates the current query plan.
   63643                 :   **
   63644                 :   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
   63645                 :   ** parameter in the WHERE clause might influence the choice of query plan
   63646                 :   ** for a statement, then the statement will be automatically recompiled,
   63647                 :   ** as if there had been a schema change, on the first sqlite3_step() call
   63648                 :   ** following any change to the bindings of that parameter.
   63649                 :   */
   63650          422941 :   if( p->isPrepareV2 &&
   63651          422941 :      ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
   63652                 :   ){
   63653              52 :     p->expired = 1;
   63654                 :   }
   63655          422941 :   return SQLITE_OK;
   63656                 : }
   63657                 : 
   63658                 : /*
   63659                 : ** Bind a text or BLOB value.
   63660                 : */
   63661          153241 : static int bindText(
   63662                 :   sqlite3_stmt *pStmt,   /* The statement to bind against */
   63663                 :   int i,                 /* Index of the parameter to bind */
   63664                 :   const void *zData,     /* Pointer to the data to be bound */
   63665                 :   int nData,             /* Number of bytes of data to be bound */
   63666                 :   void (*xDel)(void*),   /* Destructor for the data */
   63667                 :   u8 encoding            /* Encoding for the data */
   63668                 : ){
   63669          153241 :   Vdbe *p = (Vdbe *)pStmt;
   63670                 :   Mem *pVar;
   63671                 :   int rc;
   63672                 : 
   63673          153241 :   rc = vdbeUnbind(p, i);
   63674          153241 :   if( rc==SQLITE_OK ){
   63675          153241 :     if( zData!=0 ){
   63676          153212 :       pVar = &p->aVar[i-1];
   63677          153212 :       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
   63678          153212 :       if( rc==SQLITE_OK && encoding!=0 ){
   63679          145193 :         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
   63680                 :       }
   63681          153212 :       sqlite3Error(p->db, rc, 0);
   63682          153212 :       rc = sqlite3ApiExit(p->db, rc);
   63683                 :     }
   63684          153241 :     sqlite3_mutex_leave(p->db->mutex);
   63685               0 :   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
   63686               0 :     xDel((void*)zData);
   63687                 :   }
   63688          153241 :   return rc;
   63689                 : }
   63690                 : 
   63691                 : 
   63692                 : /*
   63693                 : ** Bind a blob value to an SQL statement variable.
   63694                 : */
   63695            8048 : SQLITE_API int sqlite3_bind_blob(
   63696                 :   sqlite3_stmt *pStmt, 
   63697                 :   int i, 
   63698                 :   const void *zData, 
   63699                 :   int nData, 
   63700                 :   void (*xDel)(void*)
   63701                 : ){
   63702            8048 :   return bindText(pStmt, i, zData, nData, xDel, 0);
   63703                 : }
   63704           12768 : SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
   63705                 :   int rc;
   63706           12768 :   Vdbe *p = (Vdbe *)pStmt;
   63707           12768 :   rc = vdbeUnbind(p, i);
   63708           12768 :   if( rc==SQLITE_OK ){
   63709           12768 :     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
   63710           12768 :     sqlite3_mutex_leave(p->db->mutex);
   63711                 :   }
   63712           12768 :   return rc;
   63713                 : }
   63714            1141 : SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
   63715            1141 :   return sqlite3_bind_int64(p, i, (i64)iValue);
   63716                 : }
   63717          230586 : SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
   63718                 :   int rc;
   63719          230586 :   Vdbe *p = (Vdbe *)pStmt;
   63720          230586 :   rc = vdbeUnbind(p, i);
   63721          230587 :   if( rc==SQLITE_OK ){
   63722          230586 :     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
   63723          230586 :     sqlite3_mutex_leave(p->db->mutex);
   63724                 :   }
   63725          230587 :   return rc;
   63726                 : }
   63727           26346 : SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
   63728                 :   int rc;
   63729           26346 :   Vdbe *p = (Vdbe*)pStmt;
   63730           26346 :   rc = vdbeUnbind(p, i);
   63731           26346 :   if( rc==SQLITE_OK ){
   63732           26346 :     sqlite3_mutex_leave(p->db->mutex);
   63733                 :   }
   63734           26346 :   return rc;
   63735                 : }
   63736          103404 : SQLITE_API int sqlite3_bind_text( 
   63737                 :   sqlite3_stmt *pStmt, 
   63738                 :   int i, 
   63739                 :   const char *zData, 
   63740                 :   int nData, 
   63741                 :   void (*xDel)(void*)
   63742                 : ){
   63743          103404 :   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
   63744                 : }
   63745                 : #ifndef SQLITE_OMIT_UTF16
   63746           41781 : SQLITE_API int sqlite3_bind_text16(
   63747                 :   sqlite3_stmt *pStmt, 
   63748                 :   int i, 
   63749                 :   const void *zData, 
   63750                 :   int nData, 
   63751                 :   void (*xDel)(void*)
   63752                 : ){
   63753           41781 :   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
   63754                 : }
   63755                 : #endif /* SQLITE_OMIT_UTF16 */
   63756              12 : SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
   63757                 :   int rc;
   63758              12 :   switch( pValue->type ){
   63759                 :     case SQLITE_INTEGER: {
   63760               0 :       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
   63761               0 :       break;
   63762                 :     }
   63763                 :     case SQLITE_FLOAT: {
   63764               0 :       rc = sqlite3_bind_double(pStmt, i, pValue->r);
   63765               0 :       break;
   63766                 :     }
   63767                 :     case SQLITE_BLOB: {
   63768               0 :       if( pValue->flags & MEM_Zero ){
   63769               0 :         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
   63770                 :       }else{
   63771               0 :         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
   63772                 :       }
   63773               0 :       break;
   63774                 :     }
   63775                 :     case SQLITE_TEXT: {
   63776               8 :       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
   63777               8 :                               pValue->enc);
   63778               8 :       break;
   63779                 :     }
   63780                 :     default: {
   63781               4 :       rc = sqlite3_bind_null(pStmt, i);
   63782               4 :       break;
   63783                 :     }
   63784                 :   }
   63785              12 :   return rc;
   63786                 : }
   63787               0 : SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
   63788                 :   int rc;
   63789               0 :   Vdbe *p = (Vdbe *)pStmt;
   63790               0 :   rc = vdbeUnbind(p, i);
   63791               0 :   if( rc==SQLITE_OK ){
   63792               0 :     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
   63793               0 :     sqlite3_mutex_leave(p->db->mutex);
   63794                 :   }
   63795               0 :   return rc;
   63796                 : }
   63797                 : 
   63798                 : /*
   63799                 : ** Return the number of wildcards that can be potentially bound to.
   63800                 : ** This routine is added to support DBD::SQLite.  
   63801                 : */
   63802           36766 : SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
   63803           36766 :   Vdbe *p = (Vdbe*)pStmt;
   63804           36766 :   return p ? p->nVar : 0;
   63805                 : }
   63806                 : 
   63807                 : /*
   63808                 : ** Return the name of a wildcard parameter.  Return NULL if the index
   63809                 : ** is out of range or if the wildcard is unnamed.
   63810                 : **
   63811                 : ** The result is always UTF-8.
   63812                 : */
   63813              16 : SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
   63814              16 :   Vdbe *p = (Vdbe*)pStmt;
   63815              16 :   if( p==0 || i<1 || i>p->nzVar ){
   63816               0 :     return 0;
   63817                 :   }
   63818              16 :   return p->azVar[i-1];
   63819                 : }
   63820                 : 
   63821                 : /*
   63822                 : ** Given a wildcard parameter name, return the index of the variable
   63823                 : ** with that name.  If there is no variable with the given name,
   63824                 : ** return 0.
   63825                 : */
   63826          869438 : SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
   63827                 :   int i;
   63828          869438 :   if( p==0 ){
   63829               0 :     return 0;
   63830                 :   }
   63831          869438 :   if( zName ){
   63832         4629028 :     for(i=0; i<p->nzVar; i++){
   63833         4628995 :       const char *z = p->azVar[i];
   63834         4628995 :       if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){
   63835          869408 :         return i+1;
   63836                 :       }
   63837                 :     }
   63838                 :   }
   63839              30 :   return 0;
   63840                 : }
   63841          449258 : SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
   63842          449258 :   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
   63843                 : }
   63844                 : 
   63845                 : /*
   63846                 : ** Transfer all bindings from the first statement over to the second.
   63847                 : */
   63848            1521 : SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
   63849            1521 :   Vdbe *pFrom = (Vdbe*)pFromStmt;
   63850            1521 :   Vdbe *pTo = (Vdbe*)pToStmt;
   63851                 :   int i;
   63852            1521 :   assert( pTo->db==pFrom->db );
   63853            1521 :   assert( pTo->nVar==pFrom->nVar );
   63854            1521 :   sqlite3_mutex_enter(pTo->db->mutex);
   63855            3876 :   for(i=0; i<pFrom->nVar; i++){
   63856            2355 :     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
   63857                 :   }
   63858            1521 :   sqlite3_mutex_leave(pTo->db->mutex);
   63859            1521 :   return SQLITE_OK;
   63860                 : }
   63861                 : 
   63862                 : #ifndef SQLITE_OMIT_DEPRECATED
   63863                 : /*
   63864                 : ** Deprecated external interface.  Internal/core SQLite code
   63865                 : ** should call sqlite3TransferBindings.
   63866                 : **
   63867                 : ** Is is misuse to call this routine with statements from different
   63868                 : ** database connections.  But as this is a deprecated interface, we
   63869                 : ** will not bother to check for that condition.
   63870                 : **
   63871                 : ** If the two statements contain a different number of bindings, then
   63872                 : ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
   63873                 : ** SQLITE_OK is returned.
   63874                 : */
   63875               0 : SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
   63876               0 :   Vdbe *pFrom = (Vdbe*)pFromStmt;
   63877               0 :   Vdbe *pTo = (Vdbe*)pToStmt;
   63878               0 :   if( pFrom->nVar!=pTo->nVar ){
   63879               0 :     return SQLITE_ERROR;
   63880                 :   }
   63881               0 :   if( pTo->isPrepareV2 && pTo->expmask ){
   63882               0 :     pTo->expired = 1;
   63883                 :   }
   63884               0 :   if( pFrom->isPrepareV2 && pFrom->expmask ){
   63885               0 :     pFrom->expired = 1;
   63886                 :   }
   63887               0 :   return sqlite3TransferBindings(pFromStmt, pToStmt);
   63888                 : }
   63889                 : #endif
   63890                 : 
   63891                 : /*
   63892                 : ** Return the sqlite3* database handle to which the prepared statement given
   63893                 : ** in the argument belongs.  This is the same database handle that was
   63894                 : ** the first argument to the sqlite3_prepare() that was used to create
   63895                 : ** the statement in the first place.
   63896                 : */
   63897               1 : SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
   63898               1 :   return pStmt ? ((Vdbe*)pStmt)->db : 0;
   63899                 : }
   63900                 : 
   63901                 : /*
   63902                 : ** Return true if the prepared statement is guaranteed to not modify the
   63903                 : ** database.
   63904                 : */
   63905           48628 : SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
   63906           48628 :   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
   63907                 : }
   63908                 : 
   63909                 : /*
   63910                 : ** Return true if the prepared statement is in need of being reset.
   63911                 : */
   63912               0 : SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
   63913               0 :   Vdbe *v = (Vdbe*)pStmt;
   63914               0 :   return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
   63915                 : }
   63916                 : 
   63917                 : /*
   63918                 : ** Return a pointer to the next prepared statement after pStmt associated
   63919                 : ** with database connection pDb.  If pStmt is NULL, return the first
   63920                 : ** prepared statement for the database connection.  Return NULL if there
   63921                 : ** are no more.
   63922                 : */
   63923            3273 : SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
   63924                 :   sqlite3_stmt *pNext;
   63925            3273 :   sqlite3_mutex_enter(pDb->mutex);
   63926            3273 :   if( pStmt==0 ){
   63927            3267 :     pNext = (sqlite3_stmt*)pDb->pVdbe;
   63928                 :   }else{
   63929               6 :     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
   63930                 :   }
   63931            3273 :   sqlite3_mutex_leave(pDb->mutex);
   63932            3273 :   return pNext;
   63933                 : }
   63934                 : 
   63935                 : /*
   63936                 : ** Return the value of a status counter for a prepared statement
   63937                 : */
   63938          178590 : SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
   63939          178590 :   Vdbe *pVdbe = (Vdbe*)pStmt;
   63940          178590 :   int v = pVdbe->aCounter[op-1];
   63941          178590 :   if( resetFlag ) pVdbe->aCounter[op-1] = 0;
   63942          178590 :   return v;
   63943                 : }
   63944                 : 
   63945                 : /************** End of vdbeapi.c *********************************************/
   63946                 : /************** Begin file vdbetrace.c ***************************************/
   63947                 : /*
   63948                 : ** 2009 November 25
   63949                 : **
   63950                 : ** The author disclaims copyright to this source code.  In place of
   63951                 : ** a legal notice, here is a blessing:
   63952                 : **
   63953                 : **    May you do good and not evil.
   63954                 : **    May you find forgiveness for yourself and forgive others.
   63955                 : **    May you share freely, never taking more than you give.
   63956                 : **
   63957                 : *************************************************************************
   63958                 : **
   63959                 : ** This file contains code used to insert the values of host parameters
   63960                 : ** (aka "wildcards") into the SQL text output by sqlite3_trace().
   63961                 : **
   63962                 : ** The Vdbe parse-tree explainer is also found here.
   63963                 : */
   63964                 : 
   63965                 : #ifndef SQLITE_OMIT_TRACE
   63966                 : 
   63967                 : /*
   63968                 : ** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
   63969                 : ** bytes in this text up to but excluding the first character in
   63970                 : ** a host parameter.  If the text contains no host parameters, return
   63971                 : ** the total number of bytes in the text.
   63972                 : */
   63973          576499 : static int findNextHostParameter(const char *zSql, int *pnToken){
   63974                 :   int tokenType;
   63975          576499 :   int nTotal = 0;
   63976                 :   int n;
   63977                 : 
   63978          576499 :   *pnToken = 0;
   63979         7939554 :   while( zSql[0] ){
   63980         7214446 :     n = sqlite3GetToken((u8*)zSql, &tokenType);
   63981         7214444 :     assert( n>0 && tokenType!=TK_ILLEGAL );
   63982         7214445 :     if( tokenType==TK_VARIABLE ){
   63983          427889 :       *pnToken = n;
   63984          427889 :       break;
   63985                 :     }
   63986         6786556 :     nTotal += n;
   63987         6786556 :     zSql += n;
   63988                 :   }
   63989          576498 :   return nTotal;
   63990                 : }
   63991                 : 
   63992                 : /*
   63993                 : ** This function returns a pointer to a nul-terminated string in memory
   63994                 : ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
   63995                 : ** string contains a copy of zRawSql but with host parameters expanded to 
   63996                 : ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1, 
   63997                 : ** then the returned string holds a copy of zRawSql with "-- " prepended
   63998                 : ** to each line of text.
   63999                 : **
   64000                 : ** The calling function is responsible for making sure the memory returned
   64001                 : ** is eventually freed.
   64002                 : **
   64003                 : ** ALGORITHM:  Scan the input string looking for host parameters in any of
   64004                 : ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
   64005                 : ** string literals, quoted identifier names, and comments.  For text forms,
   64006                 : ** the host parameter index is found by scanning the perpared
   64007                 : ** statement for the corresponding OP_Variable opcode.  Once the host
   64008                 : ** parameter index is known, locate the value in p->aVar[].  Then render
   64009                 : ** the value as a literal in place of the host parameter name.
   64010                 : */
   64011          204609 : SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
   64012                 :   Vdbe *p,                 /* The prepared statement being evaluated */
   64013                 :   const char *zRawSql      /* Raw text of the SQL statement */
   64014                 : ){
   64015                 :   sqlite3 *db;             /* The database connection */
   64016          204609 :   int idx = 0;             /* Index of a host parameter */
   64017          204609 :   int nextIndex = 1;       /* Index of next ? host parameter */
   64018                 :   int n;                   /* Length of a token prefix */
   64019                 :   int nToken;              /* Length of the parameter token */
   64020                 :   int i;                   /* Loop counter */
   64021                 :   Mem *pVar;               /* Value of a host parameter */
   64022                 :   StrAccum out;            /* Accumulate the output here */
   64023                 :   char zBase[100];         /* Initial working space */
   64024                 : 
   64025          204609 :   db = p->db;
   64026          204609 :   sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
   64027                 :                       db->aLimit[SQLITE_LIMIT_LENGTH]);
   64028          204609 :   out.db = db;
   64029          204609 :   if( db->vdbeExecCnt>1 ){
   64030           35697 :     while( *zRawSql ){
   64031           11899 :       const char *zStart = zRawSql;
   64032           11899 :       while( *(zRawSql++)!='\n' && *zRawSql );
   64033           11899 :       sqlite3StrAccumAppend(&out, "-- ", 3);
   64034           11899 :       sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
   64035                 :     }
   64036                 :   }else{
   64037          813308 :     while( zRawSql[0] ){
   64038          576499 :       n = findNextHostParameter(zRawSql, &nToken);
   64039          576500 :       assert( n>0 );
   64040          576500 :       sqlite3StrAccumAppend(&out, zRawSql, n);
   64041          576499 :       zRawSql += n;
   64042          576499 :       assert( zRawSql[0] || nToken==0 );
   64043          576499 :       if( nToken==0 ) break;
   64044          427888 :       if( zRawSql[0]=='?' ){
   64045            7705 :         if( nToken>1 ){
   64046            6493 :           assert( sqlite3Isdigit(zRawSql[1]) );
   64047            6493 :           sqlite3GetInt32(&zRawSql[1], &idx);
   64048                 :         }else{
   64049            1212 :           idx = nextIndex;
   64050                 :         }
   64051                 :       }else{
   64052          420183 :         assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
   64053                 :         testcase( zRawSql[0]==':' );
   64054                 :         testcase( zRawSql[0]=='$' );
   64055                 :         testcase( zRawSql[0]=='@' );
   64056          420183 :         idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
   64057          420184 :         assert( idx>0 );
   64058                 :       }
   64059          427889 :       zRawSql += nToken;
   64060          427889 :       nextIndex = idx + 1;
   64061          427889 :       assert( idx>0 && idx<=p->nVar );
   64062          427889 :       pVar = &p->aVar[idx-1];
   64063          427889 :       if( pVar->flags & MEM_Null ){
   64064           26375 :         sqlite3StrAccumAppend(&out, "NULL", 4);
   64065          401514 :       }else if( pVar->flags & MEM_Int ){
   64066          231196 :         sqlite3XPrintf(&out, "%lld", pVar->u.i);
   64067          170318 :       }else if( pVar->flags & MEM_Real ){
   64068           12816 :         sqlite3XPrintf(&out, "%!.15g", pVar->r);
   64069          157502 :       }else if( pVar->flags & MEM_Str ){
   64070                 : #ifndef SQLITE_OMIT_UTF16
   64071          149038 :         u8 enc = ENC(db);
   64072          149038 :         if( enc!=SQLITE_UTF8 ){
   64073                 :           Mem utf8;
   64074             199 :           memset(&utf8, 0, sizeof(utf8));
   64075             199 :           utf8.db = db;
   64076             199 :           sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
   64077             199 :           sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
   64078             199 :           sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
   64079             199 :           sqlite3VdbeMemRelease(&utf8);
   64080                 :         }else
   64081                 : #endif
   64082                 :         {
   64083          148839 :           sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
   64084                 :         }
   64085            8464 :       }else if( pVar->flags & MEM_Zero ){
   64086               0 :         sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
   64087                 :       }else{
   64088            8464 :         assert( pVar->flags & MEM_Blob );
   64089            8464 :         sqlite3StrAccumAppend(&out, "x'", 2);
   64090          157975 :         for(i=0; i<pVar->n; i++){
   64091          149511 :           sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
   64092                 :         }
   64093            8464 :         sqlite3StrAccumAppend(&out, "'", 1);
   64094                 :       }
   64095                 :     }
   64096                 :   }
   64097          204609 :   return sqlite3StrAccumFinish(&out);
   64098                 : }
   64099                 : 
   64100                 : #endif /* #ifndef SQLITE_OMIT_TRACE */
   64101                 : 
   64102                 : /*****************************************************************************
   64103                 : ** The following code implements the data-structure explaining logic
   64104                 : ** for the Vdbe.
   64105                 : */
   64106                 : 
   64107                 : #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
   64108                 : 
   64109                 : /*
   64110                 : ** Allocate a new Explain object
   64111                 : */
   64112                 : SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
   64113                 :   if( pVdbe ){
   64114                 :     sqlite3BeginBenignMalloc();
   64115                 :     Explain *p = sqlite3_malloc( sizeof(Explain) );
   64116                 :     if( p ){
   64117                 :       memset(p, 0, sizeof(*p));
   64118                 :       p->pVdbe = pVdbe;
   64119                 :       sqlite3_free(pVdbe->pExplain);
   64120                 :       pVdbe->pExplain = p;
   64121                 :       sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
   64122                 :                           SQLITE_MAX_LENGTH);
   64123                 :       p->str.useMalloc = 2;
   64124                 :     }else{
   64125                 :       sqlite3EndBenignMalloc();
   64126                 :     }
   64127                 :   }
   64128                 : }
   64129                 : 
   64130                 : /*
   64131                 : ** Return true if the Explain ends with a new-line.
   64132                 : */
   64133                 : static int endsWithNL(Explain *p){
   64134                 :   return p && p->str.zText && p->str.nChar
   64135                 :            && p->str.zText[p->str.nChar-1]=='\n';
   64136                 : }
   64137                 :     
   64138                 : /*
   64139                 : ** Append text to the indentation
   64140                 : */
   64141                 : SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
   64142                 :   Explain *p;
   64143                 :   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
   64144                 :     va_list ap;
   64145                 :     if( p->nIndent && endsWithNL(p) ){
   64146                 :       int n = p->nIndent;
   64147                 :       if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
   64148                 :       sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
   64149                 :     }   
   64150                 :     va_start(ap, zFormat);
   64151                 :     sqlite3VXPrintf(&p->str, 1, zFormat, ap);
   64152                 :     va_end(ap);
   64153                 :   }
   64154                 : }
   64155                 : 
   64156                 : /*
   64157                 : ** Append a '\n' if there is not already one.
   64158                 : */
   64159                 : SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
   64160                 :   Explain *p;
   64161                 :   if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
   64162                 :     sqlite3StrAccumAppend(&p->str, "\n", 1);
   64163                 :   }
   64164                 : }
   64165                 : 
   64166                 : /*
   64167                 : ** Push a new indentation level.  Subsequent lines will be indented
   64168                 : ** so that they begin at the current cursor position.
   64169                 : */
   64170                 : SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
   64171                 :   Explain *p;
   64172                 :   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
   64173                 :     if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
   64174                 :       const char *z = p->str.zText;
   64175                 :       int i = p->str.nChar-1;
   64176                 :       int x;
   64177                 :       while( i>=0 && z[i]!='\n' ){ i--; }
   64178                 :       x = (p->str.nChar - 1) - i;
   64179                 :       if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
   64180                 :         x = p->aIndent[p->nIndent-1];
   64181                 :       }
   64182                 :       p->aIndent[p->nIndent] = x;
   64183                 :     }
   64184                 :     p->nIndent++;
   64185                 :   }
   64186                 : }
   64187                 : 
   64188                 : /*
   64189                 : ** Pop the indentation stack by one level.
   64190                 : */
   64191                 : SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
   64192                 :   if( p && p->pExplain ) p->pExplain->nIndent--;
   64193                 : }
   64194                 : 
   64195                 : /*
   64196                 : ** Free the indentation structure
   64197                 : */
   64198                 : SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
   64199                 :   if( pVdbe && pVdbe->pExplain ){
   64200                 :     sqlite3_free(pVdbe->zExplain);
   64201                 :     sqlite3ExplainNL(pVdbe);
   64202                 :     pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
   64203                 :     sqlite3_free(pVdbe->pExplain);
   64204                 :     pVdbe->pExplain = 0;
   64205                 :     sqlite3EndBenignMalloc();
   64206                 :   }
   64207                 : }
   64208                 : 
   64209                 : /*
   64210                 : ** Return the explanation of a virtual machine.
   64211                 : */
   64212                 : SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
   64213                 :   return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
   64214                 : }
   64215                 : #endif /* defined(SQLITE_DEBUG) */
   64216                 : 
   64217                 : /************** End of vdbetrace.c *******************************************/
   64218                 : /************** Begin file vdbe.c ********************************************/
   64219                 : /*
   64220                 : ** 2001 September 15
   64221                 : **
   64222                 : ** The author disclaims copyright to this source code.  In place of
   64223                 : ** a legal notice, here is a blessing:
   64224                 : **
   64225                 : **    May you do good and not evil.
   64226                 : **    May you find forgiveness for yourself and forgive others.
   64227                 : **    May you share freely, never taking more than you give.
   64228                 : **
   64229                 : *************************************************************************
   64230                 : ** The code in this file implements execution method of the 
   64231                 : ** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
   64232                 : ** handles housekeeping details such as creating and deleting
   64233                 : ** VDBE instances.  This file is solely interested in executing
   64234                 : ** the VDBE program.
   64235                 : **
   64236                 : ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
   64237                 : ** to a VDBE.
   64238                 : **
   64239                 : ** The SQL parser generates a program which is then executed by
   64240                 : ** the VDBE to do the work of the SQL statement.  VDBE programs are 
   64241                 : ** similar in form to assembly language.  The program consists of
   64242                 : ** a linear sequence of operations.  Each operation has an opcode 
   64243                 : ** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4 
   64244                 : ** is a null-terminated string.  Operand P5 is an unsigned character.
   64245                 : ** Few opcodes use all 5 operands.
   64246                 : **
   64247                 : ** Computation results are stored on a set of registers numbered beginning
   64248                 : ** with 1 and going up to Vdbe.nMem.  Each register can store
   64249                 : ** either an integer, a null-terminated string, a floating point
   64250                 : ** number, or the SQL "NULL" value.  An implicit conversion from one
   64251                 : ** type to the other occurs as necessary.
   64252                 : ** 
   64253                 : ** Most of the code in this file is taken up by the sqlite3VdbeExec()
   64254                 : ** function which does the work of interpreting a VDBE program.
   64255                 : ** But other routines are also provided to help in building up
   64256                 : ** a program instruction by instruction.
   64257                 : **
   64258                 : ** Various scripts scan this source file in order to generate HTML
   64259                 : ** documentation, headers files, or other derived files.  The formatting
   64260                 : ** of the code in this file is, therefore, important.  See other comments
   64261                 : ** in this file for details.  If in doubt, do not deviate from existing
   64262                 : ** commenting and indentation practices when changing or adding code.
   64263                 : */
   64264                 : 
   64265                 : /*
   64266                 : ** Invoke this macro on memory cells just prior to changing the
   64267                 : ** value of the cell.  This macro verifies that shallow copies are
   64268                 : ** not misused.
   64269                 : */
   64270                 : #ifdef SQLITE_DEBUG
   64271                 : # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
   64272                 : #else
   64273                 : # define memAboutToChange(P,M)
   64274                 : #endif
   64275                 : 
   64276                 : /*
   64277                 : ** The following global variable is incremented every time a cursor
   64278                 : ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
   64279                 : ** procedures use this information to make sure that indices are
   64280                 : ** working correctly.  This variable has no function other than to
   64281                 : ** help verify the correct operation of the library.
   64282                 : */
   64283                 : #ifdef SQLITE_TEST
   64284                 : SQLITE_API int sqlite3_search_count = 0;
   64285                 : #endif
   64286                 : 
   64287                 : /*
   64288                 : ** When this global variable is positive, it gets decremented once before
   64289                 : ** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
   64290                 : ** field of the sqlite3 structure is set in order to simulate an interrupt.
   64291                 : **
   64292                 : ** This facility is used for testing purposes only.  It does not function
   64293                 : ** in an ordinary build.
   64294                 : */
   64295                 : #ifdef SQLITE_TEST
   64296                 : SQLITE_API int sqlite3_interrupt_count = 0;
   64297                 : #endif
   64298                 : 
   64299                 : /*
   64300                 : ** The next global variable is incremented each type the OP_Sort opcode
   64301                 : ** is executed.  The test procedures use this information to make sure that
   64302                 : ** sorting is occurring or not occurring at appropriate times.   This variable
   64303                 : ** has no function other than to help verify the correct operation of the
   64304                 : ** library.
   64305                 : */
   64306                 : #ifdef SQLITE_TEST
   64307                 : SQLITE_API int sqlite3_sort_count = 0;
   64308                 : #endif
   64309                 : 
   64310                 : /*
   64311                 : ** The next global variable records the size of the largest MEM_Blob
   64312                 : ** or MEM_Str that has been used by a VDBE opcode.  The test procedures
   64313                 : ** use this information to make sure that the zero-blob functionality
   64314                 : ** is working correctly.   This variable has no function other than to
   64315                 : ** help verify the correct operation of the library.
   64316                 : */
   64317                 : #ifdef SQLITE_TEST
   64318                 : SQLITE_API int sqlite3_max_blobsize = 0;
   64319                 : static void updateMaxBlobsize(Mem *p){
   64320                 :   if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
   64321                 :     sqlite3_max_blobsize = p->n;
   64322                 :   }
   64323                 : }
   64324                 : #endif
   64325                 : 
   64326                 : /*
   64327                 : ** The next global variable is incremented each type the OP_Found opcode
   64328                 : ** is executed. This is used to test whether or not the foreign key
   64329                 : ** operation implemented using OP_FkIsZero is working. This variable
   64330                 : ** has no function other than to help verify the correct operation of the
   64331                 : ** library.
   64332                 : */
   64333                 : #ifdef SQLITE_TEST
   64334                 : SQLITE_API int sqlite3_found_count = 0;
   64335                 : #endif
   64336                 : 
   64337                 : /*
   64338                 : ** Test a register to see if it exceeds the current maximum blob size.
   64339                 : ** If it does, record the new maximum blob size.
   64340                 : */
   64341                 : #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
   64342                 : # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
   64343                 : #else
   64344                 : # define UPDATE_MAX_BLOBSIZE(P)
   64345                 : #endif
   64346                 : 
   64347                 : /*
   64348                 : ** Convert the given register into a string if it isn't one
   64349                 : ** already. Return non-zero if a malloc() fails.
   64350                 : */
   64351                 : #define Stringify(P, enc) \
   64352                 :    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
   64353                 :      { goto no_mem; }
   64354                 : 
   64355                 : /*
   64356                 : ** An ephemeral string value (signified by the MEM_Ephem flag) contains
   64357                 : ** a pointer to a dynamically allocated string where some other entity
   64358                 : ** is responsible for deallocating that string.  Because the register
   64359                 : ** does not control the string, it might be deleted without the register
   64360                 : ** knowing it.
   64361                 : **
   64362                 : ** This routine converts an ephemeral string into a dynamically allocated
   64363                 : ** string that the register itself controls.  In other words, it
   64364                 : ** converts an MEM_Ephem string into an MEM_Dyn string.
   64365                 : */
   64366                 : #define Deephemeralize(P) \
   64367                 :    if( ((P)->flags&MEM_Ephem)!=0 \
   64368                 :        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
   64369                 : 
   64370                 : /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
   64371                 : #ifdef SQLITE_OMIT_MERGE_SORT
   64372                 : # define isSorter(x) 0
   64373                 : #else
   64374                 : # define isSorter(x) ((x)->pSorter!=0)
   64375                 : #endif
   64376                 : 
   64377                 : /*
   64378                 : ** Argument pMem points at a register that will be passed to a
   64379                 : ** user-defined function or returned to the user as the result of a query.
   64380                 : ** This routine sets the pMem->type variable used by the sqlite3_value_*() 
   64381                 : ** routines.
   64382                 : */
   64383         1873217 : SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
   64384         1873217 :   int flags = pMem->flags;
   64385         1873217 :   if( flags & MEM_Null ){
   64386          132203 :     pMem->type = SQLITE_NULL;
   64387                 :   }
   64388         1741014 :   else if( flags & MEM_Int ){
   64389         1258046 :     pMem->type = SQLITE_INTEGER;
   64390                 :   }
   64391          482968 :   else if( flags & MEM_Real ){
   64392           14490 :     pMem->type = SQLITE_FLOAT;
   64393                 :   }
   64394          468478 :   else if( flags & MEM_Str ){
   64395          462726 :     pMem->type = SQLITE_TEXT;
   64396                 :   }else{
   64397            5752 :     pMem->type = SQLITE_BLOB;
   64398                 :   }
   64399         1873217 : }
   64400                 : 
   64401                 : /*
   64402                 : ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
   64403                 : ** if we run out of memory.
   64404                 : */
   64405          542116 : static VdbeCursor *allocateCursor(
   64406                 :   Vdbe *p,              /* The virtual machine */
   64407                 :   int iCur,             /* Index of the new VdbeCursor */
   64408                 :   int nField,           /* Number of fields in the table or index */
   64409                 :   int iDb,              /* Database the cursor belongs to, or -1 */
   64410                 :   int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
   64411                 : ){
   64412                 :   /* Find the memory cell that will be used to store the blob of memory
   64413                 :   ** required for this VdbeCursor structure. It is convenient to use a 
   64414                 :   ** vdbe memory cell to manage the memory allocation required for a
   64415                 :   ** VdbeCursor structure for the following reasons:
   64416                 :   **
   64417                 :   **   * Sometimes cursor numbers are used for a couple of different
   64418                 :   **     purposes in a vdbe program. The different uses might require
   64419                 :   **     different sized allocations. Memory cells provide growable
   64420                 :   **     allocations.
   64421                 :   **
   64422                 :   **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
   64423                 :   **     be freed lazily via the sqlite3_release_memory() API. This
   64424                 :   **     minimizes the number of malloc calls made by the system.
   64425                 :   **
   64426                 :   ** Memory cells for cursors are allocated at the top of the address
   64427                 :   ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
   64428                 :   ** cursor 1 is managed by memory cell (p->nMem-1), etc.
   64429                 :   */
   64430          542116 :   Mem *pMem = &p->aMem[p->nMem-iCur];
   64431                 : 
   64432                 :   int nByte;
   64433          542116 :   VdbeCursor *pCx = 0;
   64434          542116 :   nByte = 
   64435         1081831 :       ROUND8(sizeof(VdbeCursor)) + 
   64436         1081831 :       (isBtreeCursor?sqlite3BtreeCursorSize():0) + 
   64437          542116 :       2*nField*sizeof(u32);
   64438                 : 
   64439          542116 :   assert( iCur<p->nCursor );
   64440          542116 :   if( p->apCsr[iCur] ){
   64441            2139 :     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   64442            2139 :     p->apCsr[iCur] = 0;
   64443                 :   }
   64444          542116 :   if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
   64445          542116 :     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
   64446          542116 :     memset(pCx, 0, sizeof(VdbeCursor));
   64447          542116 :     pCx->iDb = iDb;
   64448          542116 :     pCx->nField = nField;
   64449          542116 :     if( nField ){
   64450          525427 :       pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
   64451                 :     }
   64452          542116 :     if( isBtreeCursor ){
   64453         1079430 :       pCx->pCursor = (BtCursor*)
   64454         1079430 :           &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
   64455          539715 :       sqlite3BtreeCursorZero(pCx->pCursor);
   64456                 :     }
   64457                 :   }
   64458          542116 :   return pCx;
   64459                 : }
   64460                 : 
   64461                 : /*
   64462                 : ** Try to convert a value into a numeric representation if we can
   64463                 : ** do so without loss of information.  In other words, if the string
   64464                 : ** looks like a number, convert it into a number.  If it does not
   64465                 : ** look like a number, leave it alone.
   64466                 : */
   64467         1015884 : static void applyNumericAffinity(Mem *pRec){
   64468         1015884 :   if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
   64469                 :     double rValue;
   64470                 :     i64 iValue;
   64471          133148 :     u8 enc = pRec->enc;
   64472          133148 :     if( (pRec->flags&MEM_Str)==0 ) return;
   64473           10778 :     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
   64474           10732 :     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
   64475           10732 :       pRec->u.i = iValue;
   64476           10732 :       pRec->flags |= MEM_Int;
   64477                 :     }else{
   64478               0 :       pRec->r = rValue;
   64479               0 :       pRec->flags |= MEM_Real;
   64480                 :     }
   64481                 :   }
   64482                 : }
   64483                 : 
   64484                 : /*
   64485                 : ** Processing is determine by the affinity parameter:
   64486                 : **
   64487                 : ** SQLITE_AFF_INTEGER:
   64488                 : ** SQLITE_AFF_REAL:
   64489                 : ** SQLITE_AFF_NUMERIC:
   64490                 : **    Try to convert pRec to an integer representation or a 
   64491                 : **    floating-point representation if an integer representation
   64492                 : **    is not possible.  Note that the integer representation is
   64493                 : **    always preferred, even if the affinity is REAL, because
   64494                 : **    an integer representation is more space efficient on disk.
   64495                 : **
   64496                 : ** SQLITE_AFF_TEXT:
   64497                 : **    Convert pRec to a text representation.
   64498                 : **
   64499                 : ** SQLITE_AFF_NONE:
   64500                 : **    No-op.  pRec is unchanged.
   64501                 : */
   64502         1886457 : static void applyAffinity(
   64503                 :   Mem *pRec,          /* The value to apply affinity to */
   64504                 :   char affinity,      /* The affinity to be applied */
   64505                 :   u8 enc              /* Use this text encoding */
   64506                 : ){
   64507         1886457 :   if( affinity==SQLITE_AFF_TEXT ){
   64508                 :     /* Only attempt the conversion to TEXT if there is an integer or real
   64509                 :     ** representation (blob and NULL do not get converted) but no string
   64510                 :     ** representation.
   64511                 :     */
   64512          950543 :     if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
   64513             167 :       sqlite3VdbeMemStringify(pRec, enc);
   64514                 :     }
   64515          950543 :     pRec->flags &= ~(MEM_Real|MEM_Int);
   64516          935914 :   }else if( affinity!=SQLITE_AFF_NONE ){
   64517          865784 :     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
   64518                 :              || affinity==SQLITE_AFF_NUMERIC );
   64519          865784 :     applyNumericAffinity(pRec);
   64520          865784 :     if( pRec->flags & MEM_Real ){
   64521           15753 :       sqlite3VdbeIntegerAffinity(pRec);
   64522                 :     }
   64523                 :   }
   64524         1886457 : }
   64525                 : 
   64526                 : /*
   64527                 : ** Try to convert the type of a function argument or a result column
   64528                 : ** into a numeric representation.  Use either INTEGER or REAL whichever
   64529                 : ** is appropriate.  But only do the conversion if it is possible without
   64530                 : ** loss of information and return the revised type of the argument.
   64531                 : */
   64532           11499 : SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
   64533           11499 :   Mem *pMem = (Mem*)pVal;
   64534           11499 :   if( pMem->type==SQLITE_TEXT ){
   64535               0 :     applyNumericAffinity(pMem);
   64536               0 :     sqlite3VdbeMemStoreType(pMem);
   64537                 :   }
   64538           11499 :   return pMem->type;
   64539                 : }
   64540                 : 
   64541                 : /*
   64542                 : ** Exported version of applyAffinity(). This one works on sqlite3_value*, 
   64543                 : ** not the internal Mem* type.
   64544                 : */
   64545           16149 : SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
   64546                 :   sqlite3_value *pVal, 
   64547                 :   u8 affinity, 
   64548                 :   u8 enc
   64549                 : ){
   64550           16149 :   applyAffinity((Mem *)pVal, affinity, enc);
   64551           16149 : }
   64552                 : 
   64553                 : #ifdef SQLITE_DEBUG
   64554                 : /*
   64555                 : ** Write a nice string representation of the contents of cell pMem
   64556                 : ** into buffer zBuf, length nBuf.
   64557                 : */
   64558               0 : SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
   64559               0 :   char *zCsr = zBuf;
   64560               0 :   int f = pMem->flags;
   64561                 : 
   64562                 :   static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
   64563                 : 
   64564               0 :   if( f&MEM_Blob ){
   64565                 :     int i;
   64566                 :     char c;
   64567               0 :     if( f & MEM_Dyn ){
   64568               0 :       c = 'z';
   64569               0 :       assert( (f & (MEM_Static|MEM_Ephem))==0 );
   64570               0 :     }else if( f & MEM_Static ){
   64571               0 :       c = 't';
   64572               0 :       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
   64573               0 :     }else if( f & MEM_Ephem ){
   64574               0 :       c = 'e';
   64575               0 :       assert( (f & (MEM_Static|MEM_Dyn))==0 );
   64576                 :     }else{
   64577               0 :       c = 's';
   64578                 :     }
   64579                 : 
   64580               0 :     sqlite3_snprintf(100, zCsr, "%c", c);
   64581               0 :     zCsr += sqlite3Strlen30(zCsr);
   64582               0 :     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
   64583               0 :     zCsr += sqlite3Strlen30(zCsr);
   64584               0 :     for(i=0; i<16 && i<pMem->n; i++){
   64585               0 :       sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
   64586               0 :       zCsr += sqlite3Strlen30(zCsr);
   64587                 :     }
   64588               0 :     for(i=0; i<16 && i<pMem->n; i++){
   64589               0 :       char z = pMem->z[i];
   64590               0 :       if( z<32 || z>126 ) *zCsr++ = '.';
   64591               0 :       else *zCsr++ = z;
   64592                 :     }
   64593                 : 
   64594               0 :     sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
   64595               0 :     zCsr += sqlite3Strlen30(zCsr);
   64596               0 :     if( f & MEM_Zero ){
   64597               0 :       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
   64598               0 :       zCsr += sqlite3Strlen30(zCsr);
   64599                 :     }
   64600               0 :     *zCsr = '\0';
   64601               0 :   }else if( f & MEM_Str ){
   64602                 :     int j, k;
   64603               0 :     zBuf[0] = ' ';
   64604               0 :     if( f & MEM_Dyn ){
   64605               0 :       zBuf[1] = 'z';
   64606               0 :       assert( (f & (MEM_Static|MEM_Ephem))==0 );
   64607               0 :     }else if( f & MEM_Static ){
   64608               0 :       zBuf[1] = 't';
   64609               0 :       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
   64610               0 :     }else if( f & MEM_Ephem ){
   64611               0 :       zBuf[1] = 'e';
   64612               0 :       assert( (f & (MEM_Static|MEM_Dyn))==0 );
   64613                 :     }else{
   64614               0 :       zBuf[1] = 's';
   64615                 :     }
   64616               0 :     k = 2;
   64617               0 :     sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
   64618               0 :     k += sqlite3Strlen30(&zBuf[k]);
   64619               0 :     zBuf[k++] = '[';
   64620               0 :     for(j=0; j<15 && j<pMem->n; j++){
   64621               0 :       u8 c = pMem->z[j];
   64622               0 :       if( c>=0x20 && c<0x7f ){
   64623               0 :         zBuf[k++] = c;
   64624                 :       }else{
   64625               0 :         zBuf[k++] = '.';
   64626                 :       }
   64627                 :     }
   64628               0 :     zBuf[k++] = ']';
   64629               0 :     sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
   64630               0 :     k += sqlite3Strlen30(&zBuf[k]);
   64631               0 :     zBuf[k++] = 0;
   64632                 :   }
   64633               0 : }
   64634                 : #endif
   64635                 : 
   64636                 : #ifdef SQLITE_DEBUG
   64637                 : /*
   64638                 : ** Print the value of a register for tracing purposes:
   64639                 : */
   64640               0 : static void memTracePrint(FILE *out, Mem *p){
   64641               0 :   if( p->flags & MEM_Null ){
   64642               0 :     fprintf(out, " NULL");
   64643               0 :   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
   64644               0 :     fprintf(out, " si:%lld", p->u.i);
   64645               0 :   }else if( p->flags & MEM_Int ){
   64646               0 :     fprintf(out, " i:%lld", p->u.i);
   64647                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   64648               0 :   }else if( p->flags & MEM_Real ){
   64649               0 :     fprintf(out, " r:%g", p->r);
   64650                 : #endif
   64651               0 :   }else if( p->flags & MEM_RowSet ){
   64652               0 :     fprintf(out, " (rowset)");
   64653                 :   }else{
   64654                 :     char zBuf[200];
   64655               0 :     sqlite3VdbeMemPrettyPrint(p, zBuf);
   64656               0 :     fprintf(out, " ");
   64657               0 :     fprintf(out, "%s", zBuf);
   64658                 :   }
   64659               0 : }
   64660               0 : static void registerTrace(FILE *out, int iReg, Mem *p){
   64661               0 :   fprintf(out, "REG[%d] = ", iReg);
   64662               0 :   memTracePrint(out, p);
   64663               0 :   fprintf(out, "\n");
   64664               0 : }
   64665                 : #endif
   64666                 : 
   64667                 : #ifdef SQLITE_DEBUG
   64668                 : #  define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
   64669                 : #else
   64670                 : #  define REGISTER_TRACE(R,M)
   64671                 : #endif
   64672                 : 
   64673                 : 
   64674                 : #ifdef VDBE_PROFILE
   64675                 : 
   64676                 : /* 
   64677                 : ** hwtime.h contains inline assembler code for implementing 
   64678                 : ** high-performance timing routines.
   64679                 : */
   64680                 : /************** Include hwtime.h in the middle of vdbe.c *********************/
   64681                 : /************** Begin file hwtime.h ******************************************/
   64682                 : /*
   64683                 : ** 2008 May 27
   64684                 : **
   64685                 : ** The author disclaims copyright to this source code.  In place of
   64686                 : ** a legal notice, here is a blessing:
   64687                 : **
   64688                 : **    May you do good and not evil.
   64689                 : **    May you find forgiveness for yourself and forgive others.
   64690                 : **    May you share freely, never taking more than you give.
   64691                 : **
   64692                 : ******************************************************************************
   64693                 : **
   64694                 : ** This file contains inline asm code for retrieving "high-performance"
   64695                 : ** counters for x86 class CPUs.
   64696                 : */
   64697                 : #ifndef _HWTIME_H_
   64698                 : #define _HWTIME_H_
   64699                 : 
   64700                 : /*
   64701                 : ** The following routine only works on pentium-class (or newer) processors.
   64702                 : ** It uses the RDTSC opcode to read the cycle count value out of the
   64703                 : ** processor and returns that value.  This can be used for high-res
   64704                 : ** profiling.
   64705                 : */
   64706                 : #if (defined(__GNUC__) || defined(_MSC_VER)) && \
   64707                 :       (defined(i386) || defined(__i386__) || defined(_M_IX86))
   64708                 : 
   64709                 :   #if defined(__GNUC__)
   64710                 : 
   64711                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   64712                 :      unsigned int lo, hi;
   64713                 :      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
   64714                 :      return (sqlite_uint64)hi << 32 | lo;
   64715                 :   }
   64716                 : 
   64717                 :   #elif defined(_MSC_VER)
   64718                 : 
   64719                 :   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
   64720                 :      __asm {
   64721                 :         rdtsc
   64722                 :         ret       ; return value at EDX:EAX
   64723                 :      }
   64724                 :   }
   64725                 : 
   64726                 :   #endif
   64727                 : 
   64728                 : #elif (defined(__GNUC__) && defined(__x86_64__))
   64729                 : 
   64730                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   64731                 :       unsigned long val;
   64732                 :       __asm__ __volatile__ ("rdtsc" : "=A" (val));
   64733                 :       return val;
   64734                 :   }
   64735                 :  
   64736                 : #elif (defined(__GNUC__) && defined(__ppc__))
   64737                 : 
   64738                 :   __inline__ sqlite_uint64 sqlite3Hwtime(void){
   64739                 :       unsigned long long retval;
   64740                 :       unsigned long junk;
   64741                 :       __asm__ __volatile__ ("\n\
   64742                 :           1:      mftbu   %1\n\
   64743                 :                   mftb    %L0\n\
   64744                 :                   mftbu   %0\n\
   64745                 :                   cmpw    %0,%1\n\
   64746                 :                   bne     1b"
   64747                 :                   : "=r" (retval), "=r" (junk));
   64748                 :       return retval;
   64749                 :   }
   64750                 : 
   64751                 : #else
   64752                 : 
   64753                 :   #error Need implementation of sqlite3Hwtime() for your platform.
   64754                 : 
   64755                 :   /*
   64756                 :   ** To compile without implementing sqlite3Hwtime() for your platform,
   64757                 :   ** you can remove the above #error and use the following
   64758                 :   ** stub function.  You will lose timing support for many
   64759                 :   ** of the debugging and testing utilities, but it should at
   64760                 :   ** least compile and run.
   64761                 :   */
   64762                 : SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
   64763                 : 
   64764                 : #endif
   64765                 : 
   64766                 : #endif /* !defined(_HWTIME_H_) */
   64767                 : 
   64768                 : /************** End of hwtime.h **********************************************/
   64769                 : /************** Continuing where we left off in vdbe.c ***********************/
   64770                 : 
   64771                 : #endif
   64772                 : 
   64773                 : /*
   64774                 : ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
   64775                 : ** sqlite3_interrupt() routine has been called.  If it has been, then
   64776                 : ** processing of the VDBE program is interrupted.
   64777                 : **
   64778                 : ** This macro added to every instruction that does a jump in order to
   64779                 : ** implement a loop.  This test used to be on every single instruction,
   64780                 : ** but that meant we more testing than we needed.  By only testing the
   64781                 : ** flag on jump instructions, we get a (small) speed improvement.
   64782                 : */
   64783                 : #define CHECK_FOR_INTERRUPT \
   64784                 :    if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
   64785                 : 
   64786                 : 
   64787                 : #ifndef NDEBUG
   64788                 : /*
   64789                 : ** This function is only called from within an assert() expression. It
   64790                 : ** checks that the sqlite3.nTransaction variable is correctly set to
   64791                 : ** the number of non-transaction savepoints currently in the 
   64792                 : ** linked list starting at sqlite3.pSavepoint.
   64793                 : ** 
   64794                 : ** Usage:
   64795                 : **
   64796                 : **     assert( checkSavepointCount(db) );
   64797                 : */
   64798           14518 : static int checkSavepointCount(sqlite3 *db){
   64799           14518 :   int n = 0;
   64800                 :   Savepoint *p;
   64801           14518 :   for(p=db->pSavepoint; p; p=p->pNext) n++;
   64802           14518 :   assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
   64803           14518 :   return 1;
   64804                 : }
   64805                 : #endif
   64806                 : 
   64807                 : /*
   64808                 : ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
   64809                 : ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
   64810                 : ** in memory obtained from sqlite3DbMalloc).
   64811                 : */
   64812             128 : static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
   64813             128 :   sqlite3 *db = p->db;
   64814             128 :   sqlite3DbFree(db, p->zErrMsg);
   64815             128 :   p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
   64816             128 :   sqlite3_free(pVtab->zErrMsg);
   64817             128 :   pVtab->zErrMsg = 0;
   64818             128 : }
   64819                 : 
   64820                 : 
   64821                 : /*
   64822                 : ** Execute as much of a VDBE program as we can then return.
   64823                 : **
   64824                 : ** sqlite3VdbeMakeReady() must be called before this routine in order to
   64825                 : ** close the program with a final OP_Halt and to set up the callbacks
   64826                 : ** and the error message pointer.
   64827                 : **
   64828                 : ** Whenever a row or result data is available, this routine will either
   64829                 : ** invoke the result callback (if there is one) or return with
   64830                 : ** SQLITE_ROW.
   64831                 : **
   64832                 : ** If an attempt is made to open a locked database, then this routine
   64833                 : ** will either invoke the busy callback (if there is one) or it will
   64834                 : ** return SQLITE_BUSY.
   64835                 : **
   64836                 : ** If an error occurs, an error message is written to memory obtained
   64837                 : ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
   64838                 : ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
   64839                 : **
   64840                 : ** If the callback ever returns non-zero, then the program exits
   64841                 : ** immediately.  There will be no error message but the p->rc field is
   64842                 : ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
   64843                 : **
   64844                 : ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
   64845                 : ** routine to return SQLITE_ERROR.
   64846                 : **
   64847                 : ** Other fatal errors return SQLITE_ERROR.
   64848                 : **
   64849                 : ** After this routine has finished, sqlite3VdbeFinalize() should be
   64850                 : ** used to clean up the mess that was left behind.
   64851                 : */
   64852          357868 : SQLITE_PRIVATE int sqlite3VdbeExec(
   64853                 :   Vdbe *p                    /* The VDBE */
   64854                 : ){
   64855          357868 :   int pc=0;                  /* The program counter */
   64856          357868 :   Op *aOp = p->aOp;          /* Copy of p->aOp */
   64857                 :   Op *pOp;                   /* Current operation */
   64858          357868 :   int rc = SQLITE_OK;        /* Value to return */
   64859          357868 :   sqlite3 *db = p->db;       /* The database */
   64860          357868 :   u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
   64861          357868 :   u8 encoding = ENC(db);     /* The database encoding */
   64862                 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   64863                 :   int checkProgress;         /* True if progress callbacks are enabled */
   64864          357868 :   int nProgressOps = 0;      /* Opcodes executed since progress callback. */
   64865                 : #endif
   64866          357868 :   Mem *aMem = p->aMem;       /* Copy of p->aMem */
   64867          357868 :   Mem *pIn1 = 0;             /* 1st input operand */
   64868          357868 :   Mem *pIn2 = 0;             /* 2nd input operand */
   64869          357868 :   Mem *pIn3 = 0;             /* 3rd input operand */
   64870          357868 :   Mem *pOut = 0;             /* Output operand */
   64871          357868 :   int iCompare = 0;          /* Result of last OP_Compare operation */
   64872          357868 :   int *aPermute = 0;         /* Permutation of columns for OP_Compare */
   64873          357868 :   i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
   64874                 : #ifdef VDBE_PROFILE
   64875                 :   u64 start;                 /* CPU clock count at start of opcode */
   64876                 :   int origPc;                /* Program counter at start of opcode */
   64877                 : #endif
   64878                 :   /********************************************************************
   64879                 :   ** Automatically generated code
   64880                 :   **
   64881                 :   ** The following union is automatically generated by the
   64882                 :   ** vdbe-compress.tcl script.  The purpose of this union is to
   64883                 :   ** reduce the amount of stack space required by this function.
   64884                 :   ** See comments in the vdbe-compress.tcl script for details.
   64885                 :   */
   64886                 :   union vdbeExecUnion {
   64887                 :     struct OP_Yield_stack_vars {
   64888                 :       int pcDest;
   64889                 :     } aa;
   64890                 :     struct OP_Null_stack_vars {
   64891                 :       int cnt;
   64892                 :     } ab;
   64893                 :     struct OP_Variable_stack_vars {
   64894                 :       Mem *pVar;       /* Value being transferred */
   64895                 :     } ac;
   64896                 :     struct OP_Move_stack_vars {
   64897                 :       char *zMalloc;   /* Holding variable for allocated memory */
   64898                 :       int n;           /* Number of registers left to copy */
   64899                 :       int p1;          /* Register to copy from */
   64900                 :       int p2;          /* Register to copy to */
   64901                 :     } ad;
   64902                 :     struct OP_ResultRow_stack_vars {
   64903                 :       Mem *pMem;
   64904                 :       int i;
   64905                 :     } ae;
   64906                 :     struct OP_Concat_stack_vars {
   64907                 :       i64 nByte;
   64908                 :     } af;
   64909                 :     struct OP_Remainder_stack_vars {
   64910                 :       int flags;      /* Combined MEM_* flags from both inputs */
   64911                 :       i64 iA;         /* Integer value of left operand */
   64912                 :       i64 iB;         /* Integer value of right operand */
   64913                 :       double rA;      /* Real value of left operand */
   64914                 :       double rB;      /* Real value of right operand */
   64915                 :     } ag;
   64916                 :     struct OP_Function_stack_vars {
   64917                 :       int i;
   64918                 :       Mem *pArg;
   64919                 :       sqlite3_context ctx;
   64920                 :       sqlite3_value **apVal;
   64921                 :       int n;
   64922                 :     } ah;
   64923                 :     struct OP_ShiftRight_stack_vars {
   64924                 :       i64 iA;
   64925                 :       u64 uA;
   64926                 :       i64 iB;
   64927                 :       u8 op;
   64928                 :     } ai;
   64929                 :     struct OP_Ge_stack_vars {
   64930                 :       int res;            /* Result of the comparison of pIn1 against pIn3 */
   64931                 :       char affinity;      /* Affinity to use for comparison */
   64932                 :       u16 flags1;         /* Copy of initial value of pIn1->flags */
   64933                 :       u16 flags3;         /* Copy of initial value of pIn3->flags */
   64934                 :     } aj;
   64935                 :     struct OP_Compare_stack_vars {
   64936                 :       int n;
   64937                 :       int i;
   64938                 :       int p1;
   64939                 :       int p2;
   64940                 :       const KeyInfo *pKeyInfo;
   64941                 :       int idx;
   64942                 :       CollSeq *pColl;    /* Collating sequence to use on this term */
   64943                 :       int bRev;          /* True for DESCENDING sort order */
   64944                 :     } ak;
   64945                 :     struct OP_Or_stack_vars {
   64946                 :       int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
   64947                 :       int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
   64948                 :     } al;
   64949                 :     struct OP_IfNot_stack_vars {
   64950                 :       int c;
   64951                 :     } am;
   64952                 :     struct OP_Column_stack_vars {
   64953                 :       u32 payloadSize;   /* Number of bytes in the record */
   64954                 :       i64 payloadSize64; /* Number of bytes in the record */
   64955                 :       int p1;            /* P1 value of the opcode */
   64956                 :       int p2;            /* column number to retrieve */
   64957                 :       VdbeCursor *pC;    /* The VDBE cursor */
   64958                 :       char *zRec;        /* Pointer to complete record-data */
   64959                 :       BtCursor *pCrsr;   /* The BTree cursor */
   64960                 :       u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
   64961                 :       u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
   64962                 :       int nField;        /* number of fields in the record */
   64963                 :       int len;           /* The length of the serialized data for the column */
   64964                 :       int i;             /* Loop counter */
   64965                 :       char *zData;       /* Part of the record being decoded */
   64966                 :       Mem *pDest;        /* Where to write the extracted value */
   64967                 :       Mem sMem;          /* For storing the record being decoded */
   64968                 :       u8 *zIdx;          /* Index into header */
   64969                 :       u8 *zEndHdr;       /* Pointer to first byte after the header */
   64970                 :       u32 offset;        /* Offset into the data */
   64971                 :       u32 szField;       /* Number of bytes in the content of a field */
   64972                 :       int szHdr;         /* Size of the header size field at start of record */
   64973                 :       int avail;         /* Number of bytes of available data */
   64974                 :       u32 t;             /* A type code from the record header */
   64975                 :       Mem *pReg;         /* PseudoTable input register */
   64976                 :     } an;
   64977                 :     struct OP_Affinity_stack_vars {
   64978                 :       const char *zAffinity;   /* The affinity to be applied */
   64979                 :       char cAff;               /* A single character of affinity */
   64980                 :     } ao;
   64981                 :     struct OP_MakeRecord_stack_vars {
   64982                 :       u8 *zNewRecord;        /* A buffer to hold the data for the new record */
   64983                 :       Mem *pRec;             /* The new record */
   64984                 :       u64 nData;             /* Number of bytes of data space */
   64985                 :       int nHdr;              /* Number of bytes of header space */
   64986                 :       i64 nByte;             /* Data space required for this record */
   64987                 :       int nZero;             /* Number of zero bytes at the end of the record */
   64988                 :       int nVarint;           /* Number of bytes in a varint */
   64989                 :       u32 serial_type;       /* Type field */
   64990                 :       Mem *pData0;           /* First field to be combined into the record */
   64991                 :       Mem *pLast;            /* Last field of the record */
   64992                 :       int nField;            /* Number of fields in the record */
   64993                 :       char *zAffinity;       /* The affinity string for the record */
   64994                 :       int file_format;       /* File format to use for encoding */
   64995                 :       int i;                 /* Space used in zNewRecord[] */
   64996                 :       int len;               /* Length of a field */
   64997                 :     } ap;
   64998                 :     struct OP_Count_stack_vars {
   64999                 :       i64 nEntry;
   65000                 :       BtCursor *pCrsr;
   65001                 :     } aq;
   65002                 :     struct OP_Savepoint_stack_vars {
   65003                 :       int p1;                         /* Value of P1 operand */
   65004                 :       char *zName;                    /* Name of savepoint */
   65005                 :       int nName;
   65006                 :       Savepoint *pNew;
   65007                 :       Savepoint *pSavepoint;
   65008                 :       Savepoint *pTmp;
   65009                 :       int iSavepoint;
   65010                 :       int ii;
   65011                 :     } ar;
   65012                 :     struct OP_AutoCommit_stack_vars {
   65013                 :       int desiredAutoCommit;
   65014                 :       int iRollback;
   65015                 :       int turnOnAC;
   65016                 :     } as;
   65017                 :     struct OP_Transaction_stack_vars {
   65018                 :       Btree *pBt;
   65019                 :     } at;
   65020                 :     struct OP_ReadCookie_stack_vars {
   65021                 :       int iMeta;
   65022                 :       int iDb;
   65023                 :       int iCookie;
   65024                 :     } au;
   65025                 :     struct OP_SetCookie_stack_vars {
   65026                 :       Db *pDb;
   65027                 :     } av;
   65028                 :     struct OP_VerifyCookie_stack_vars {
   65029                 :       int iMeta;
   65030                 :       int iGen;
   65031                 :       Btree *pBt;
   65032                 :     } aw;
   65033                 :     struct OP_OpenWrite_stack_vars {
   65034                 :       int nField;
   65035                 :       KeyInfo *pKeyInfo;
   65036                 :       int p2;
   65037                 :       int iDb;
   65038                 :       int wrFlag;
   65039                 :       Btree *pX;
   65040                 :       VdbeCursor *pCur;
   65041                 :       Db *pDb;
   65042                 :     } ax;
   65043                 :     struct OP_OpenEphemeral_stack_vars {
   65044                 :       VdbeCursor *pCx;
   65045                 :     } ay;
   65046                 :     struct OP_SorterOpen_stack_vars {
   65047                 :       VdbeCursor *pCx;
   65048                 :     } az;
   65049                 :     struct OP_OpenPseudo_stack_vars {
   65050                 :       VdbeCursor *pCx;
   65051                 :     } ba;
   65052                 :     struct OP_SeekGt_stack_vars {
   65053                 :       int res;
   65054                 :       int oc;
   65055                 :       VdbeCursor *pC;
   65056                 :       UnpackedRecord r;
   65057                 :       int nField;
   65058                 :       i64 iKey;      /* The rowid we are to seek to */
   65059                 :     } bb;
   65060                 :     struct OP_Seek_stack_vars {
   65061                 :       VdbeCursor *pC;
   65062                 :     } bc;
   65063                 :     struct OP_Found_stack_vars {
   65064                 :       int alreadyExists;
   65065                 :       VdbeCursor *pC;
   65066                 :       int res;
   65067                 :       char *pFree;
   65068                 :       UnpackedRecord *pIdxKey;
   65069                 :       UnpackedRecord r;
   65070                 :       char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
   65071                 :     } bd;
   65072                 :     struct OP_IsUnique_stack_vars {
   65073                 :       u16 ii;
   65074                 :       VdbeCursor *pCx;
   65075                 :       BtCursor *pCrsr;
   65076                 :       u16 nField;
   65077                 :       Mem *aMx;
   65078                 :       UnpackedRecord r;                  /* B-Tree index search key */
   65079                 :       i64 R;                             /* Rowid stored in register P3 */
   65080                 :     } be;
   65081                 :     struct OP_NotExists_stack_vars {
   65082                 :       VdbeCursor *pC;
   65083                 :       BtCursor *pCrsr;
   65084                 :       int res;
   65085                 :       u64 iKey;
   65086                 :     } bf;
   65087                 :     struct OP_NewRowid_stack_vars {
   65088                 :       i64 v;                 /* The new rowid */
   65089                 :       VdbeCursor *pC;        /* Cursor of table to get the new rowid */
   65090                 :       int res;               /* Result of an sqlite3BtreeLast() */
   65091                 :       int cnt;               /* Counter to limit the number of searches */
   65092                 :       Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
   65093                 :       VdbeFrame *pFrame;     /* Root frame of VDBE */
   65094                 :     } bg;
   65095                 :     struct OP_InsertInt_stack_vars {
   65096                 :       Mem *pData;       /* MEM cell holding data for the record to be inserted */
   65097                 :       Mem *pKey;        /* MEM cell holding key  for the record */
   65098                 :       i64 iKey;         /* The integer ROWID or key for the record to be inserted */
   65099                 :       VdbeCursor *pC;   /* Cursor to table into which insert is written */
   65100                 :       int nZero;        /* Number of zero-bytes to append */
   65101                 :       int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
   65102                 :       const char *zDb;  /* database name - used by the update hook */
   65103                 :       const char *zTbl; /* Table name - used by the opdate hook */
   65104                 :       int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
   65105                 :     } bh;
   65106                 :     struct OP_Delete_stack_vars {
   65107                 :       i64 iKey;
   65108                 :       VdbeCursor *pC;
   65109                 :     } bi;
   65110                 :     struct OP_SorterCompare_stack_vars {
   65111                 :       VdbeCursor *pC;
   65112                 :       int res;
   65113                 :     } bj;
   65114                 :     struct OP_SorterData_stack_vars {
   65115                 :       VdbeCursor *pC;
   65116                 :     } bk;
   65117                 :     struct OP_RowData_stack_vars {
   65118                 :       VdbeCursor *pC;
   65119                 :       BtCursor *pCrsr;
   65120                 :       u32 n;
   65121                 :       i64 n64;
   65122                 :     } bl;
   65123                 :     struct OP_Rowid_stack_vars {
   65124                 :       VdbeCursor *pC;
   65125                 :       i64 v;
   65126                 :       sqlite3_vtab *pVtab;
   65127                 :       const sqlite3_module *pModule;
   65128                 :     } bm;
   65129                 :     struct OP_NullRow_stack_vars {
   65130                 :       VdbeCursor *pC;
   65131                 :     } bn;
   65132                 :     struct OP_Last_stack_vars {
   65133                 :       VdbeCursor *pC;
   65134                 :       BtCursor *pCrsr;
   65135                 :       int res;
   65136                 :     } bo;
   65137                 :     struct OP_Rewind_stack_vars {
   65138                 :       VdbeCursor *pC;
   65139                 :       BtCursor *pCrsr;
   65140                 :       int res;
   65141                 :     } bp;
   65142                 :     struct OP_Next_stack_vars {
   65143                 :       VdbeCursor *pC;
   65144                 :       int res;
   65145                 :     } bq;
   65146                 :     struct OP_IdxInsert_stack_vars {
   65147                 :       VdbeCursor *pC;
   65148                 :       BtCursor *pCrsr;
   65149                 :       int nKey;
   65150                 :       const char *zKey;
   65151                 :     } br;
   65152                 :     struct OP_IdxDelete_stack_vars {
   65153                 :       VdbeCursor *pC;
   65154                 :       BtCursor *pCrsr;
   65155                 :       int res;
   65156                 :       UnpackedRecord r;
   65157                 :     } bs;
   65158                 :     struct OP_IdxRowid_stack_vars {
   65159                 :       BtCursor *pCrsr;
   65160                 :       VdbeCursor *pC;
   65161                 :       i64 rowid;
   65162                 :     } bt;
   65163                 :     struct OP_IdxGE_stack_vars {
   65164                 :       VdbeCursor *pC;
   65165                 :       int res;
   65166                 :       UnpackedRecord r;
   65167                 :     } bu;
   65168                 :     struct OP_Destroy_stack_vars {
   65169                 :       int iMoved;
   65170                 :       int iCnt;
   65171                 :       Vdbe *pVdbe;
   65172                 :       int iDb;
   65173                 :     } bv;
   65174                 :     struct OP_Clear_stack_vars {
   65175                 :       int nChange;
   65176                 :     } bw;
   65177                 :     struct OP_CreateTable_stack_vars {
   65178                 :       int pgno;
   65179                 :       int flags;
   65180                 :       Db *pDb;
   65181                 :     } bx;
   65182                 :     struct OP_ParseSchema_stack_vars {
   65183                 :       int iDb;
   65184                 :       const char *zMaster;
   65185                 :       char *zSql;
   65186                 :       InitData initData;
   65187                 :     } by;
   65188                 :     struct OP_IntegrityCk_stack_vars {
   65189                 :       int nRoot;      /* Number of tables to check.  (Number of root pages.) */
   65190                 :       int *aRoot;     /* Array of rootpage numbers for tables to be checked */
   65191                 :       int j;          /* Loop counter */
   65192                 :       int nErr;       /* Number of errors reported */
   65193                 :       char *z;        /* Text of the error report */
   65194                 :       Mem *pnErr;     /* Register keeping track of errors remaining */
   65195                 :     } bz;
   65196                 :     struct OP_RowSetRead_stack_vars {
   65197                 :       i64 val;
   65198                 :     } ca;
   65199                 :     struct OP_RowSetTest_stack_vars {
   65200                 :       int iSet;
   65201                 :       int exists;
   65202                 :     } cb;
   65203                 :     struct OP_Program_stack_vars {
   65204                 :       int nMem;               /* Number of memory registers for sub-program */
   65205                 :       int nByte;              /* Bytes of runtime space required for sub-program */
   65206                 :       Mem *pRt;               /* Register to allocate runtime space */
   65207                 :       Mem *pMem;              /* Used to iterate through memory cells */
   65208                 :       Mem *pEnd;              /* Last memory cell in new array */
   65209                 :       VdbeFrame *pFrame;      /* New vdbe frame to execute in */
   65210                 :       SubProgram *pProgram;   /* Sub-program to execute */
   65211                 :       void *t;                /* Token identifying trigger */
   65212                 :     } cc;
   65213                 :     struct OP_Param_stack_vars {
   65214                 :       VdbeFrame *pFrame;
   65215                 :       Mem *pIn;
   65216                 :     } cd;
   65217                 :     struct OP_MemMax_stack_vars {
   65218                 :       Mem *pIn1;
   65219                 :       VdbeFrame *pFrame;
   65220                 :     } ce;
   65221                 :     struct OP_AggStep_stack_vars {
   65222                 :       int n;
   65223                 :       int i;
   65224                 :       Mem *pMem;
   65225                 :       Mem *pRec;
   65226                 :       sqlite3_context ctx;
   65227                 :       sqlite3_value **apVal;
   65228                 :     } cf;
   65229                 :     struct OP_AggFinal_stack_vars {
   65230                 :       Mem *pMem;
   65231                 :     } cg;
   65232                 :     struct OP_Checkpoint_stack_vars {
   65233                 :       int i;                          /* Loop counter */
   65234                 :       int aRes[3];                    /* Results */
   65235                 :       Mem *pMem;                      /* Write results here */
   65236                 :     } ch;
   65237                 :     struct OP_JournalMode_stack_vars {
   65238                 :       Btree *pBt;                     /* Btree to change journal mode of */
   65239                 :       Pager *pPager;                  /* Pager associated with pBt */
   65240                 :       int eNew;                       /* New journal mode */
   65241                 :       int eOld;                       /* The old journal mode */
   65242                 :       const char *zFilename;          /* Name of database file for pPager */
   65243                 :     } ci;
   65244                 :     struct OP_IncrVacuum_stack_vars {
   65245                 :       Btree *pBt;
   65246                 :     } cj;
   65247                 :     struct OP_VBegin_stack_vars {
   65248                 :       VTable *pVTab;
   65249                 :     } ck;
   65250                 :     struct OP_VOpen_stack_vars {
   65251                 :       VdbeCursor *pCur;
   65252                 :       sqlite3_vtab_cursor *pVtabCursor;
   65253                 :       sqlite3_vtab *pVtab;
   65254                 :       sqlite3_module *pModule;
   65255                 :     } cl;
   65256                 :     struct OP_VFilter_stack_vars {
   65257                 :       int nArg;
   65258                 :       int iQuery;
   65259                 :       const sqlite3_module *pModule;
   65260                 :       Mem *pQuery;
   65261                 :       Mem *pArgc;
   65262                 :       sqlite3_vtab_cursor *pVtabCursor;
   65263                 :       sqlite3_vtab *pVtab;
   65264                 :       VdbeCursor *pCur;
   65265                 :       int res;
   65266                 :       int i;
   65267                 :       Mem **apArg;
   65268                 :     } cm;
   65269                 :     struct OP_VColumn_stack_vars {
   65270                 :       sqlite3_vtab *pVtab;
   65271                 :       const sqlite3_module *pModule;
   65272                 :       Mem *pDest;
   65273                 :       sqlite3_context sContext;
   65274                 :     } cn;
   65275                 :     struct OP_VNext_stack_vars {
   65276                 :       sqlite3_vtab *pVtab;
   65277                 :       const sqlite3_module *pModule;
   65278                 :       int res;
   65279                 :       VdbeCursor *pCur;
   65280                 :     } co;
   65281                 :     struct OP_VRename_stack_vars {
   65282                 :       sqlite3_vtab *pVtab;
   65283                 :       Mem *pName;
   65284                 :     } cp;
   65285                 :     struct OP_VUpdate_stack_vars {
   65286                 :       sqlite3_vtab *pVtab;
   65287                 :       sqlite3_module *pModule;
   65288                 :       int nArg;
   65289                 :       int i;
   65290                 :       sqlite_int64 rowid;
   65291                 :       Mem **apArg;
   65292                 :       Mem *pX;
   65293                 :     } cq;
   65294                 :     struct OP_Trace_stack_vars {
   65295                 :       char *zTrace;
   65296                 :       char *z;
   65297                 :     } cr;
   65298                 :   } u;
   65299                 :   /* End automatically generated code
   65300                 :   ********************************************************************/
   65301                 : 
   65302          357868 :   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
   65303          357868 :   sqlite3VdbeEnter(p);
   65304          357869 :   if( p->rc==SQLITE_NOMEM ){
   65305                 :     /* This happens if a malloc() inside a call to sqlite3_column_text() or
   65306                 :     ** sqlite3_column_text16() failed.  */
   65307               0 :     goto no_mem;
   65308                 :   }
   65309          357869 :   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
   65310          357869 :   p->rc = SQLITE_OK;
   65311          357869 :   assert( p->explain==0 );
   65312          357869 :   p->pResultSet = 0;
   65313          357869 :   db->busyHandler.nBusy = 0;
   65314          357869 :   CHECK_FOR_INTERRUPT;
   65315                 :   sqlite3VdbeIOTraceSql(p);
   65316                 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   65317          357869 :   checkProgress = db->xProgress!=0;
   65318                 : #endif
   65319                 : #ifdef SQLITE_DEBUG
   65320          357869 :   sqlite3BeginBenignMalloc();
   65321          357868 :   if( p->pc==0  && (p->db->flags & SQLITE_VdbeListing)!=0 ){
   65322                 :     int i;
   65323               0 :     printf("VDBE Program Listing:\n");
   65324               0 :     sqlite3VdbePrintSql(p);
   65325               0 :     for(i=0; i<p->nOp; i++){
   65326               0 :       sqlite3VdbePrintOp(stdout, i, &aOp[i]);
   65327                 :     }
   65328                 :   }
   65329          357868 :   sqlite3EndBenignMalloc();
   65330                 : #endif
   65331        11946087 :   for(pc=p->pc; rc==SQLITE_OK; pc++){
   65332        11945494 :     assert( pc>=0 && pc<p->nOp );
   65333        11945499 :     if( db->mallocFailed ) goto no_mem;
   65334                 : #ifdef VDBE_PROFILE
   65335                 :     origPc = pc;
   65336                 :     start = sqlite3Hwtime();
   65337                 : #endif
   65338        11945499 :     pOp = &aOp[pc];
   65339                 : 
   65340                 :     /* Only allow tracing if SQLITE_DEBUG is defined.
   65341                 :     */
   65342                 : #ifdef SQLITE_DEBUG
   65343        11945499 :     if( p->trace ){
   65344               0 :       if( pc==0 ){
   65345               0 :         printf("VDBE Execution Trace:\n");
   65346               0 :         sqlite3VdbePrintSql(p);
   65347                 :       }
   65348               0 :       sqlite3VdbePrintOp(p->trace, pc, pOp);
   65349                 :     }
   65350                 : #endif
   65351                 :       
   65352                 : 
   65353                 :     /* Check to see if we need to simulate an interrupt.  This only happens
   65354                 :     ** if we have a special test build.
   65355                 :     */
   65356                 : #ifdef SQLITE_TEST
   65357                 :     if( sqlite3_interrupt_count>0 ){
   65358                 :       sqlite3_interrupt_count--;
   65359                 :       if( sqlite3_interrupt_count==0 ){
   65360                 :         sqlite3_interrupt(db);
   65361                 :       }
   65362                 :     }
   65363                 : #endif
   65364                 : 
   65365                 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   65366                 :     /* Call the progress callback if it is configured and the required number
   65367                 :     ** of VDBE ops have been executed (either since this invocation of
   65368                 :     ** sqlite3VdbeExec() or since last time the progress callback was called).
   65369                 :     ** If the progress callback returns non-zero, exit the virtual machine with
   65370                 :     ** a return code SQLITE_ABORT.
   65371                 :     */
   65372        11945492 :     if( checkProgress ){
   65373          376072 :       if( db->nProgressOps==nProgressOps ){
   65374                 :         int prc;
   65375            1084 :         prc = db->xProgress(db->pProgressArg);
   65376            1084 :         if( prc!=0 ){
   65377               1 :           rc = SQLITE_INTERRUPT;
   65378               1 :           goto vdbe_error_halt;
   65379                 :         }
   65380            1083 :         nProgressOps = 0;
   65381                 :       }
   65382          376071 :       nProgressOps++;
   65383                 :     }
   65384                 : #endif
   65385                 : 
   65386                 :     /* On any opcode with the "out2-prerelase" tag, free any
   65387                 :     ** external allocations out of mem[p2] and set mem[p2] to be
   65388                 :     ** an undefined integer.  Opcodes will either fill in the integer
   65389                 :     ** value or convert mem[p2] to a different type.
   65390                 :     */
   65391        11945491 :     assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   65392        11945491 :     if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   65393         2299819 :       assert( pOp->p2>0 );
   65394         2299819 :       assert( pOp->p2<=p->nMem );
   65395         2299819 :       pOut = &aMem[pOp->p2];
   65396         2299819 :       memAboutToChange(p, pOut);
   65397         2299819 :       VdbeMemRelease(pOut);
   65398         2299819 :       pOut->flags = MEM_Int;
   65399                 :     }
   65400                 : 
   65401                 :     /* Sanity checking on other operands */
   65402                 : #ifdef SQLITE_DEBUG
   65403        11945491 :     if( (pOp->opflags & OPFLG_IN1)!=0 ){
   65404         1395923 :       assert( pOp->p1>0 );
   65405         1395923 :       assert( pOp->p1<=p->nMem );
   65406         1395923 :       assert( memIsValid(&aMem[pOp->p1]) );
   65407         1395923 :       REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
   65408                 :     }
   65409        11945561 :     if( (pOp->opflags & OPFLG_IN2)!=0 ){
   65410          357005 :       assert( pOp->p2>0 );
   65411          357005 :       assert( pOp->p2<=p->nMem );
   65412          357005 :       assert( memIsValid(&aMem[pOp->p2]) );
   65413          357005 :       REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
   65414                 :     }
   65415        11945561 :     if( (pOp->opflags & OPFLG_IN3)!=0 ){
   65416          721445 :       assert( pOp->p3>0 );
   65417          721445 :       assert( pOp->p3<=p->nMem );
   65418          721445 :       assert( memIsValid(&aMem[pOp->p3]) );
   65419          721445 :       REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
   65420                 :     }
   65421        11945561 :     if( (pOp->opflags & OPFLG_OUT2)!=0 ){
   65422          411546 :       assert( pOp->p2>0 );
   65423          411546 :       assert( pOp->p2<=p->nMem );
   65424          411546 :       memAboutToChange(p, &aMem[pOp->p2]);
   65425                 :     }
   65426        11945561 :     if( (pOp->opflags & OPFLG_OUT3)!=0 ){
   65427          143124 :       assert( pOp->p3>0 );
   65428          143124 :       assert( pOp->p3<=p->nMem );
   65429          143124 :       memAboutToChange(p, &aMem[pOp->p3]);
   65430                 :     }
   65431                 : #endif
   65432                 :   
   65433        11945561 :     switch( pOp->opcode ){
   65434                 : 
   65435                 : /*****************************************************************************
   65436                 : ** What follows is a massive switch statement where each case implements a
   65437                 : ** separate instruction in the virtual machine.  If we follow the usual
   65438                 : ** indentation conventions, each case should be indented by 6 spaces.  But
   65439                 : ** that is a lot of wasted space on the left margin.  So the code within
   65440                 : ** the switch statement will break with convention and be flush-left. Another
   65441                 : ** big comment (similar to this one) will mark the point in the code where
   65442                 : ** we transition back to normal indentation.
   65443                 : **
   65444                 : ** The formatting of each case is important.  The makefile for SQLite
   65445                 : ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
   65446                 : ** file looking for lines that begin with "case OP_".  The opcodes.h files
   65447                 : ** will be filled with #defines that give unique integer values to each
   65448                 : ** opcode and the opcodes.c file is filled with an array of strings where
   65449                 : ** each string is the symbolic name for the corresponding opcode.  If the
   65450                 : ** case statement is followed by a comment of the form "/# same as ... #/"
   65451                 : ** that comment is used to determine the particular value of the opcode.
   65452                 : **
   65453                 : ** Other keywords in the comment that follows each case are used to
   65454                 : ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
   65455                 : ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
   65456                 : ** the mkopcodeh.awk script for additional information.
   65457                 : **
   65458                 : ** Documentation about VDBE opcodes is generated by scanning this file
   65459                 : ** for lines of that contain "Opcode:".  That line and all subsequent
   65460                 : ** comment lines are used in the generation of the opcode.html documentation
   65461                 : ** file.
   65462                 : **
   65463                 : ** SUMMARY:
   65464                 : **
   65465                 : **     Formatting is important to scripts that scan this file.
   65466                 : **     Do not deviate from the formatting style currently in use.
   65467                 : **
   65468                 : *****************************************************************************/
   65469                 : 
   65470                 : /* Opcode:  Goto * P2 * * *
   65471                 : **
   65472                 : ** An unconditional jump to address P2.
   65473                 : ** The next instruction executed will be 
   65474                 : ** the one at index P2 from the beginning of
   65475                 : ** the program.
   65476                 : */
   65477                 : case OP_Goto: {             /* jump */
   65478          467016 :   CHECK_FOR_INTERRUPT;
   65479          467016 :   pc = pOp->p2 - 1;
   65480          467016 :   break;
   65481                 : }
   65482                 : 
   65483                 : /* Opcode:  Gosub P1 P2 * * *
   65484                 : **
   65485                 : ** Write the current address onto register P1
   65486                 : ** and then jump to address P2.
   65487                 : */
   65488                 : case OP_Gosub: {            /* jump */
   65489            2365 :   assert( pOp->p1>0 && pOp->p1<=p->nMem );
   65490            2365 :   pIn1 = &aMem[pOp->p1];
   65491            2365 :   assert( (pIn1->flags & MEM_Dyn)==0 );
   65492            2365 :   memAboutToChange(p, pIn1);
   65493            2365 :   pIn1->flags = MEM_Int;
   65494            2365 :   pIn1->u.i = pc;
   65495            2365 :   REGISTER_TRACE(pOp->p1, pIn1);
   65496            2365 :   pc = pOp->p2 - 1;
   65497            2365 :   break;
   65498                 : }
   65499                 : 
   65500                 : /* Opcode:  Return P1 * * * *
   65501                 : **
   65502                 : ** Jump to the next instruction after the address in register P1.
   65503                 : */
   65504                 : case OP_Return: {           /* in1 */
   65505            2859 :   pIn1 = &aMem[pOp->p1];
   65506            2859 :   assert( pIn1->flags & MEM_Int );
   65507            2859 :   pc = (int)pIn1->u.i;
   65508            2859 :   break;
   65509                 : }
   65510                 : 
   65511                 : /* Opcode:  Yield P1 * * * *
   65512                 : **
   65513                 : ** Swap the program counter with the value in register P1.
   65514                 : */
   65515                 : case OP_Yield: {            /* in1 */
   65516                 : #if 0  /* local variables moved into u.aa */
   65517                 :   int pcDest;
   65518                 : #endif /* local variables moved into u.aa */
   65519            2402 :   pIn1 = &aMem[pOp->p1];
   65520            2402 :   assert( (pIn1->flags & MEM_Dyn)==0 );
   65521            2402 :   pIn1->flags = MEM_Int;
   65522            2402 :   u.aa.pcDest = (int)pIn1->u.i;
   65523            2402 :   pIn1->u.i = pc;
   65524            2402 :   REGISTER_TRACE(pOp->p1, pIn1);
   65525            2402 :   pc = u.aa.pcDest;
   65526            2402 :   break;
   65527                 : }
   65528                 : 
   65529                 : /* Opcode:  HaltIfNull  P1 P2 P3 P4 *
   65530                 : **
   65531                 : ** Check the value in register P3.  If it is NULL then Halt using
   65532                 : ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
   65533                 : ** value in register P3 is not NULL, then this routine is a no-op.
   65534                 : */
   65535                 : case OP_HaltIfNull: {      /* in3 */
   65536           66450 :   pIn3 = &aMem[pOp->p3];
   65537           66450 :   if( (pIn3->flags & MEM_Null)==0 ) break;
   65538                 :   /* Fall through into OP_Halt */
   65539                 : }
   65540                 : 
   65541                 : /* Opcode:  Halt P1 P2 * P4 *
   65542                 : **
   65543                 : ** Exit immediately.  All open cursors, etc are closed
   65544                 : ** automatically.
   65545                 : **
   65546                 : ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
   65547                 : ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
   65548                 : ** For errors, it can be some other value.  If P1!=0 then P2 will determine
   65549                 : ** whether or not to rollback the current transaction.  Do not rollback
   65550                 : ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
   65551                 : ** then back out all changes that have occurred during this execution of the
   65552                 : ** VDBE, but do not rollback the transaction. 
   65553                 : **
   65554                 : ** If P4 is not null then it is an error message string.
   65555                 : **
   65556                 : ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
   65557                 : ** every program.  So a jump past the last instruction of the program
   65558                 : ** is the same as executing Halt.
   65559                 : */
   65560                 : case OP_Halt: {
   65561          198905 :   if( pOp->p1==SQLITE_OK && p->pFrame ){
   65562                 :     /* Halt the sub-program. Return control to the parent frame. */
   65563           19051 :     VdbeFrame *pFrame = p->pFrame;
   65564           19051 :     p->pFrame = pFrame->pParent;
   65565           19051 :     p->nFrame--;
   65566           19051 :     sqlite3VdbeSetChanges(db, p->nChange);
   65567           19051 :     pc = sqlite3VdbeFrameRestore(pFrame);
   65568           19051 :     lastRowid = db->lastRowid;
   65569           19051 :     if( pOp->p2==OE_Ignore ){
   65570                 :       /* Instruction pc is the OP_Program that invoked the sub-program 
   65571                 :       ** currently being halted. If the p2 instruction of this OP_Halt
   65572                 :       ** instruction is set to OE_Ignore, then the sub-program is throwing
   65573                 :       ** an IGNORE exception. In this case jump to the address specified
   65574                 :       ** as the p2 of the calling OP_Program.  */
   65575               0 :       pc = p->aOp[pc].p2-1;
   65576                 :     }
   65577           19051 :     aOp = p->aOp;
   65578           19051 :     aMem = p->aMem;
   65579           19051 :     break;
   65580                 :   }
   65581                 : 
   65582          179854 :   p->rc = pOp->p1;
   65583          179854 :   p->errorAction = (u8)pOp->p2;
   65584          179854 :   p->pc = pc;
   65585          179854 :   if( pOp->p4.z ){
   65586              53 :     assert( p->rc!=SQLITE_OK );
   65587              53 :     sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
   65588                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   65589              53 :     sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
   65590          179801 :   }else if( p->rc ){
   65591                 :     testcase( sqlite3GlobalConfig.xLog!=0 );
   65592               0 :     sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
   65593                 :   }
   65594          179854 :   rc = sqlite3VdbeHalt(p);
   65595          179854 :   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
   65596          179854 :   if( rc==SQLITE_BUSY ){
   65597               0 :     p->rc = rc = SQLITE_BUSY;
   65598                 :   }else{
   65599          179854 :     assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
   65600          179854 :     assert( rc==SQLITE_OK || db->nDeferredCons>0 );
   65601          179854 :     rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
   65602                 :   }
   65603          179854 :   goto vdbe_return;
   65604                 : }
   65605                 : 
   65606                 : /* Opcode: Integer P1 P2 * * *
   65607                 : **
   65608                 : ** The 32-bit integer value P1 is written into register P2.
   65609                 : */
   65610                 : case OP_Integer: {         /* out2-prerelease */
   65611          955792 :   pOut->u.i = pOp->p1;
   65612          955792 :   break;
   65613                 : }
   65614                 : 
   65615                 : /* Opcode: Int64 * P2 * P4 *
   65616                 : **
   65617                 : ** P4 is a pointer to a 64-bit integer value.
   65618                 : ** Write that value into register P2.
   65619                 : */
   65620                 : case OP_Int64: {           /* out2-prerelease */
   65621            5444 :   assert( pOp->p4.pI64!=0 );
   65622            5444 :   pOut->u.i = *pOp->p4.pI64;
   65623            5444 :   break;
   65624                 : }
   65625                 : 
   65626                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   65627                 : /* Opcode: Real * P2 * P4 *
   65628                 : **
   65629                 : ** P4 is a pointer to a 64-bit floating point value.
   65630                 : ** Write that value into register P2.
   65631                 : */
   65632                 : case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
   65633            6359 :   pOut->flags = MEM_Real;
   65634            6359 :   assert( !sqlite3IsNaN(*pOp->p4.pReal) );
   65635            6359 :   pOut->r = *pOp->p4.pReal;
   65636            6359 :   break;
   65637                 : }
   65638                 : #endif
   65639                 : 
   65640                 : /* Opcode: String8 * P2 * P4 *
   65641                 : **
   65642                 : ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
   65643                 : ** into an OP_String before it is executed for the first time.
   65644                 : */
   65645                 : case OP_String8: {         /* same as TK_STRING, out2-prerelease */
   65646          111989 :   assert( pOp->p4.z!=0 );
   65647          111989 :   pOp->opcode = OP_String;
   65648          111989 :   pOp->p1 = sqlite3Strlen30(pOp->p4.z);
   65649                 : 
   65650                 : #ifndef SQLITE_OMIT_UTF16
   65651          111989 :   if( encoding!=SQLITE_UTF8 ){
   65652               7 :     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
   65653               7 :     if( rc==SQLITE_TOOBIG ) goto too_big;
   65654               7 :     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
   65655               7 :     assert( pOut->zMalloc==pOut->z );
   65656               7 :     assert( pOut->flags & MEM_Dyn );
   65657               7 :     pOut->zMalloc = 0;
   65658               7 :     pOut->flags |= MEM_Static;
   65659               7 :     pOut->flags &= ~MEM_Dyn;
   65660               7 :     if( pOp->p4type==P4_DYNAMIC ){
   65661               5 :       sqlite3DbFree(db, pOp->p4.z);
   65662                 :     }
   65663               7 :     pOp->p4type = P4_DYNAMIC;
   65664               7 :     pOp->p4.z = pOut->z;
   65665               7 :     pOp->p1 = pOut->n;
   65666                 :   }
   65667                 : #endif
   65668          111989 :   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   65669               0 :     goto too_big;
   65670                 :   }
   65671                 :   /* Fall through to the next case, OP_String */
   65672                 : }
   65673                 :   
   65674                 : /* Opcode: String P1 P2 * P4 *
   65675                 : **
   65676                 : ** The string value P4 of length P1 (bytes) is stored in register P2.
   65677                 : */
   65678                 : case OP_String: {          /* out2-prerelease */
   65679          198703 :   assert( pOp->p4.z!=0 );
   65680          198703 :   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
   65681          198703 :   pOut->z = pOp->p4.z;
   65682          198703 :   pOut->n = pOp->p1;
   65683          198703 :   pOut->enc = encoding;
   65684                 :   UPDATE_MAX_BLOBSIZE(pOut);
   65685          198703 :   break;
   65686                 : }
   65687                 : 
   65688                 : /* Opcode: Null * P2 P3 * *
   65689                 : **
   65690                 : ** Write a NULL into registers P2.  If P3 greater than P2, then also write
   65691                 : ** NULL into register P3 and ever register in between P2 and P3.  If P3
   65692                 : ** is less than P2 (typically P3 is zero) then only register P2 is
   65693                 : ** set to NULL
   65694                 : */
   65695                 : case OP_Null: {           /* out2-prerelease */
   65696                 : #if 0  /* local variables moved into u.ab */
   65697                 :   int cnt;
   65698                 : #endif /* local variables moved into u.ab */
   65699          271319 :   u.ab.cnt = pOp->p3-pOp->p2;
   65700          271319 :   assert( pOp->p3<=p->nMem );
   65701          271319 :   pOut->flags = MEM_Null;
   65702          953482 :   while( u.ab.cnt>0 ){
   65703          410844 :     pOut++;
   65704          410844 :     memAboutToChange(p, pOut);
   65705          410844 :     VdbeMemRelease(pOut);
   65706          410844 :     pOut->flags = MEM_Null;
   65707          410844 :     u.ab.cnt--;
   65708                 :   }
   65709          271319 :   break;
   65710                 : }
   65711                 : 
   65712                 : 
   65713                 : /* Opcode: Blob P1 P2 * P4
   65714                 : **
   65715                 : ** P4 points to a blob of data P1 bytes long.  Store this
   65716                 : ** blob in register P2.
   65717                 : */
   65718                 : case OP_Blob: {                /* out2-prerelease */
   65719             374 :   assert( pOp->p1 <= SQLITE_MAX_LENGTH );
   65720             374 :   sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
   65721             374 :   pOut->enc = encoding;
   65722                 :   UPDATE_MAX_BLOBSIZE(pOut);
   65723             374 :   break;
   65724                 : }
   65725                 : 
   65726                 : /* Opcode: Variable P1 P2 * P4 *
   65727                 : **
   65728                 : ** Transfer the values of bound parameter P1 into register P2
   65729                 : **
   65730                 : ** If the parameter is named, then its name appears in P4 and P3==1.
   65731                 : ** The P4 value is used by sqlite3_bind_parameter_name().
   65732                 : */
   65733                 : case OP_Variable: {            /* out2-prerelease */
   65734                 : #if 0  /* local variables moved into u.ac */
   65735                 :   Mem *pVar;       /* Value being transferred */
   65736                 : #endif /* local variables moved into u.ac */
   65737                 : 
   65738          457042 :   assert( pOp->p1>0 && pOp->p1<=p->nVar );
   65739          457042 :   assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
   65740          457042 :   u.ac.pVar = &p->aVar[pOp->p1 - 1];
   65741          457042 :   if( sqlite3VdbeMemTooBig(u.ac.pVar) ){
   65742               0 :     goto too_big;
   65743                 :   }
   65744          457042 :   sqlite3VdbeMemShallowCopy(pOut, u.ac.pVar, MEM_Static);
   65745                 :   UPDATE_MAX_BLOBSIZE(pOut);
   65746          457042 :   break;
   65747                 : }
   65748                 : 
   65749                 : /* Opcode: Move P1 P2 P3 * *
   65750                 : **
   65751                 : ** Move the values in register P1..P1+P3-1 over into
   65752                 : ** registers P2..P2+P3-1.  Registers P1..P1+P1-1 are
   65753                 : ** left holding a NULL.  It is an error for register ranges
   65754                 : ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
   65755                 : */
   65756                 : case OP_Move: {
   65757                 : #if 0  /* local variables moved into u.ad */
   65758                 :   char *zMalloc;   /* Holding variable for allocated memory */
   65759                 :   int n;           /* Number of registers left to copy */
   65760                 :   int p1;          /* Register to copy from */
   65761                 :   int p2;          /* Register to copy to */
   65762                 : #endif /* local variables moved into u.ad */
   65763                 : 
   65764           38608 :   u.ad.n = pOp->p3;
   65765           38608 :   u.ad.p1 = pOp->p1;
   65766           38608 :   u.ad.p2 = pOp->p2;
   65767           38608 :   assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
   65768           38608 :   assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
   65769                 : 
   65770           38608 :   pIn1 = &aMem[u.ad.p1];
   65771           38608 :   pOut = &aMem[u.ad.p2];
   65772          115824 :   while( u.ad.n-- ){
   65773           38608 :     assert( pOut<=&aMem[p->nMem] );
   65774           38608 :     assert( pIn1<=&aMem[p->nMem] );
   65775           38608 :     assert( memIsValid(pIn1) );
   65776           38608 :     memAboutToChange(p, pOut);
   65777           38608 :     u.ad.zMalloc = pOut->zMalloc;
   65778           38608 :     pOut->zMalloc = 0;
   65779           38608 :     sqlite3VdbeMemMove(pOut, pIn1);
   65780                 : #ifdef SQLITE_DEBUG
   65781           38608 :     if( pOut->pScopyFrom>=&aMem[u.ad.p1] && pOut->pScopyFrom<&aMem[u.ad.p1+pOp->p3] ){
   65782               0 :       pOut->pScopyFrom += u.ad.p1 - pOp->p2;
   65783                 :     }
   65784                 : #endif
   65785           38608 :     pIn1->zMalloc = u.ad.zMalloc;
   65786           38608 :     REGISTER_TRACE(u.ad.p2++, pOut);
   65787           38608 :     pIn1++;
   65788           38608 :     pOut++;
   65789                 :   }
   65790           38608 :   break;
   65791                 : }
   65792                 : 
   65793                 : /* Opcode: Copy P1 P2 * * *
   65794                 : **
   65795                 : ** Make a copy of register P1 into register P2.
   65796                 : **
   65797                 : ** This instruction makes a deep copy of the value.  A duplicate
   65798                 : ** is made of any string or blob constant.  See also OP_SCopy.
   65799                 : */
   65800                 : case OP_Copy: {             /* in1, out2 */
   65801           66778 :   pIn1 = &aMem[pOp->p1];
   65802           66778 :   pOut = &aMem[pOp->p2];
   65803           66778 :   assert( pOut!=pIn1 );
   65804           66778 :   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
   65805           66778 :   Deephemeralize(pOut);
   65806           66778 :   REGISTER_TRACE(pOp->p2, pOut);
   65807           66778 :   break;
   65808                 : }
   65809                 : 
   65810                 : /* Opcode: SCopy P1 P2 * * *
   65811                 : **
   65812                 : ** Make a shallow copy of register P1 into register P2.
   65813                 : **
   65814                 : ** This instruction makes a shallow copy of the value.  If the value
   65815                 : ** is a string or blob, then the copy is only a pointer to the
   65816                 : ** original and hence if the original changes so will the copy.
   65817                 : ** Worse, if the original is deallocated, the copy becomes invalid.
   65818                 : ** Thus the program must guarantee that the original will not change
   65819                 : ** during the lifetime of the copy.  Use OP_Copy to make a complete
   65820                 : ** copy.
   65821                 : */
   65822                 : case OP_SCopy: {            /* in1, out2 */
   65823          341220 :   pIn1 = &aMem[pOp->p1];
   65824          341220 :   pOut = &aMem[pOp->p2];
   65825          341220 :   assert( pOut!=pIn1 );
   65826          341220 :   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
   65827                 : #ifdef SQLITE_DEBUG
   65828          341220 :   if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
   65829                 : #endif
   65830          341220 :   REGISTER_TRACE(pOp->p2, pOut);
   65831          341220 :   break;
   65832                 : }
   65833                 : 
   65834                 : /* Opcode: ResultRow P1 P2 * * *
   65835                 : **
   65836                 : ** The registers P1 through P1+P2-1 contain a single row of
   65837                 : ** results. This opcode causes the sqlite3_step() call to terminate
   65838                 : ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
   65839                 : ** structure to provide access to the top P1 values as the result
   65840                 : ** row.
   65841                 : */
   65842                 : case OP_ResultRow: {
   65843                 : #if 0  /* local variables moved into u.ae */
   65844                 :   Mem *pMem;
   65845                 :   int i;
   65846                 : #endif /* local variables moved into u.ae */
   65847          160596 :   assert( p->nResColumn==pOp->p2 );
   65848          160596 :   assert( pOp->p1>0 );
   65849          160596 :   assert( pOp->p1+pOp->p2<=p->nMem+1 );
   65850                 : 
   65851                 :   /* If this statement has violated immediate foreign key constraints, do
   65852                 :   ** not return the number of rows modified. And do not RELEASE the statement
   65853                 :   ** transaction. It needs to be rolled back.  */
   65854          160596 :   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
   65855               0 :     assert( db->flags&SQLITE_CountRows );
   65856               0 :     assert( p->usesStmtJournal );
   65857               0 :     break;
   65858                 :   }
   65859                 : 
   65860                 :   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
   65861                 :   ** DML statements invoke this opcode to return the number of rows
   65862                 :   ** modified to the user. This is the only way that a VM that
   65863                 :   ** opens a statement transaction may invoke this opcode.
   65864                 :   **
   65865                 :   ** In case this is such a statement, close any statement transaction
   65866                 :   ** opened by this VM before returning control to the user. This is to
   65867                 :   ** ensure that statement-transactions are always nested, not overlapping.
   65868                 :   ** If the open statement-transaction is not closed here, then the user
   65869                 :   ** may step another VM that opens its own statement transaction. This
   65870                 :   ** may lead to overlapping statement transactions.
   65871                 :   **
   65872                 :   ** The statement transaction is never a top-level transaction.  Hence
   65873                 :   ** the RELEASE call below can never fail.
   65874                 :   */
   65875          160597 :   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
   65876          160597 :   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
   65877          160614 :   if( NEVER(rc!=SQLITE_OK) ){
   65878               0 :     break;
   65879                 :   }
   65880                 : 
   65881                 :   /* Invalidate all ephemeral cursor row caches */
   65882          160614 :   p->cacheCtr = (p->cacheCtr + 2)|1;
   65883                 : 
   65884                 :   /* Make sure the results of the current row are \000 terminated
   65885                 :   ** and have an assigned type.  The results are de-ephemeralized as
   65886                 :   ** a side effect.
   65887                 :   */
   65888          160614 :   u.ae.pMem = p->pResultSet = &aMem[pOp->p1];
   65889         1120584 :   for(u.ae.i=0; u.ae.i<pOp->p2; u.ae.i++){
   65890          959990 :     assert( memIsValid(&u.ae.pMem[u.ae.i]) );
   65891          959992 :     Deephemeralize(&u.ae.pMem[u.ae.i]);
   65892          959988 :     assert( (u.ae.pMem[u.ae.i].flags & MEM_Ephem)==0
   65893                 :             || (u.ae.pMem[u.ae.i].flags & (MEM_Str|MEM_Blob))==0 );
   65894          959988 :     sqlite3VdbeMemNulTerminate(&u.ae.pMem[u.ae.i]);
   65895          959985 :     sqlite3VdbeMemStoreType(&u.ae.pMem[u.ae.i]);
   65896          959968 :     REGISTER_TRACE(pOp->p1+u.ae.i, &u.ae.pMem[u.ae.i]);
   65897                 :   }
   65898          160594 :   if( db->mallocFailed ) goto no_mem;
   65899                 : 
   65900                 :   /* Return SQLITE_ROW
   65901                 :   */
   65902          160594 :   p->pc = pc + 1;
   65903          160594 :   rc = SQLITE_ROW;
   65904          160594 :   goto vdbe_return;
   65905                 : }
   65906                 : 
   65907                 : /* Opcode: Concat P1 P2 P3 * *
   65908                 : **
   65909                 : ** Add the text in register P1 onto the end of the text in
   65910                 : ** register P2 and store the result in register P3.
   65911                 : ** If either the P1 or P2 text are NULL then store NULL in P3.
   65912                 : **
   65913                 : **   P3 = P2 || P1
   65914                 : **
   65915                 : ** It is illegal for P1 and P3 to be the same register. Sometimes,
   65916                 : ** if P3 is the same register as P2, the implementation is able
   65917                 : ** to avoid a memcpy().
   65918                 : */
   65919                 : case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
   65920                 : #if 0  /* local variables moved into u.af */
   65921                 :   i64 nByte;
   65922                 : #endif /* local variables moved into u.af */
   65923                 : 
   65924           27155 :   pIn1 = &aMem[pOp->p1];
   65925           27155 :   pIn2 = &aMem[pOp->p2];
   65926           27155 :   pOut = &aMem[pOp->p3];
   65927           27155 :   assert( pIn1!=pOut );
   65928           27155 :   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
   65929               0 :     sqlite3VdbeMemSetNull(pOut);
   65930               0 :     break;
   65931                 :   }
   65932           27155 :   if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
   65933           27155 :   Stringify(pIn1, encoding);
   65934           27155 :   Stringify(pIn2, encoding);
   65935           27155 :   u.af.nByte = pIn1->n + pIn2->n;
   65936           27155 :   if( u.af.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   65937               0 :     goto too_big;
   65938                 :   }
   65939           27155 :   MemSetTypeFlag(pOut, MEM_Str);
   65940           27155 :   if( sqlite3VdbeMemGrow(pOut, (int)u.af.nByte+2, pOut==pIn2) ){
   65941               0 :     goto no_mem;
   65942                 :   }
   65943           27155 :   if( pOut!=pIn2 ){
   65944           22385 :     memcpy(pOut->z, pIn2->z, pIn2->n);
   65945                 :   }
   65946           27155 :   memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
   65947           27155 :   pOut->z[u.af.nByte] = 0;
   65948           27155 :   pOut->z[u.af.nByte+1] = 0;
   65949           27155 :   pOut->flags |= MEM_Term;
   65950           27155 :   pOut->n = (int)u.af.nByte;
   65951           27155 :   pOut->enc = encoding;
   65952                 :   UPDATE_MAX_BLOBSIZE(pOut);
   65953           27155 :   break;
   65954                 : }
   65955                 : 
   65956                 : /* Opcode: Add P1 P2 P3 * *
   65957                 : **
   65958                 : ** Add the value in register P1 to the value in register P2
   65959                 : ** and store the result in register P3.
   65960                 : ** If either input is NULL, the result is NULL.
   65961                 : */
   65962                 : /* Opcode: Multiply P1 P2 P3 * *
   65963                 : **
   65964                 : **
   65965                 : ** Multiply the value in register P1 by the value in register P2
   65966                 : ** and store the result in register P3.
   65967                 : ** If either input is NULL, the result is NULL.
   65968                 : */
   65969                 : /* Opcode: Subtract P1 P2 P3 * *
   65970                 : **
   65971                 : ** Subtract the value in register P1 from the value in register P2
   65972                 : ** and store the result in register P3.
   65973                 : ** If either input is NULL, the result is NULL.
   65974                 : */
   65975                 : /* Opcode: Divide P1 P2 P3 * *
   65976                 : **
   65977                 : ** Divide the value in register P1 by the value in register P2
   65978                 : ** and store the result in register P3 (P3=P2/P1). If the value in 
   65979                 : ** register P1 is zero, then the result is NULL. If either input is 
   65980                 : ** NULL, the result is NULL.
   65981                 : */
   65982                 : /* Opcode: Remainder P1 P2 P3 * *
   65983                 : **
   65984                 : ** Compute the remainder after integer division of the value in
   65985                 : ** register P1 by the value in register P2 and store the result in P3. 
   65986                 : ** If the value in register P2 is zero the result is NULL.
   65987                 : ** If either operand is NULL, the result is NULL.
   65988                 : */
   65989                 : case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
   65990                 : case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
   65991                 : case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
   65992                 : case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
   65993                 : case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
   65994                 : #if 0  /* local variables moved into u.ag */
   65995                 :   int flags;      /* Combined MEM_* flags from both inputs */
   65996                 :   i64 iA;         /* Integer value of left operand */
   65997                 :   i64 iB;         /* Integer value of right operand */
   65998                 :   double rA;      /* Real value of left operand */
   65999                 :   double rB;      /* Real value of right operand */
   66000                 : #endif /* local variables moved into u.ag */
   66001                 : 
   66002           75038 :   pIn1 = &aMem[pOp->p1];
   66003           75038 :   applyNumericAffinity(pIn1);
   66004           75038 :   pIn2 = &aMem[pOp->p2];
   66005           75038 :   applyNumericAffinity(pIn2);
   66006           75038 :   pOut = &aMem[pOp->p3];
   66007           75038 :   u.ag.flags = pIn1->flags | pIn2->flags;
   66008           75038 :   if( (u.ag.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
   66009           74978 :   if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
   66010           66209 :     u.ag.iA = pIn1->u.i;
   66011           66209 :     u.ag.iB = pIn2->u.i;
   66012           66209 :     switch( pOp->opcode ){
   66013            6185 :       case OP_Add:       if( sqlite3AddInt64(&u.ag.iB,u.ag.iA) ) goto fp_math;  break;
   66014           27729 :       case OP_Subtract:  if( sqlite3SubInt64(&u.ag.iB,u.ag.iA) ) goto fp_math;  break;
   66015           12484 :       case OP_Multiply:  if( sqlite3MulInt64(&u.ag.iB,u.ag.iA) ) goto fp_math;  break;
   66016                 :       case OP_Divide: {
   66017           19811 :         if( u.ag.iA==0 ) goto arithmetic_result_is_null;
   66018           19811 :         if( u.ag.iA==-1 && u.ag.iB==SMALLEST_INT64 ) goto fp_math;
   66019           19811 :         u.ag.iB /= u.ag.iA;
   66020           19811 :         break;
   66021                 :       }
   66022                 :       default: {
   66023               0 :         if( u.ag.iA==0 ) goto arithmetic_result_is_null;
   66024               0 :         if( u.ag.iA==-1 ) u.ag.iA = 1;
   66025               0 :         u.ag.iB %= u.ag.iA;
   66026               0 :         break;
   66027                 :       }
   66028                 :     }
   66029           66209 :     pOut->u.i = u.ag.iB;
   66030           66209 :     MemSetTypeFlag(pOut, MEM_Int);
   66031                 :   }else{
   66032                 : fp_math:
   66033            8769 :     u.ag.rA = sqlite3VdbeRealValue(pIn1);
   66034            8769 :     u.ag.rB = sqlite3VdbeRealValue(pIn2);
   66035            8769 :     switch( pOp->opcode ){
   66036             210 :       case OP_Add:         u.ag.rB += u.ag.rA;       break;
   66037             780 :       case OP_Subtract:    u.ag.rB -= u.ag.rA;       break;
   66038            1301 :       case OP_Multiply:    u.ag.rB *= u.ag.rA;       break;
   66039                 :       case OP_Divide: {
   66040                 :         /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   66041            6478 :         if( u.ag.rA==(double)0 ) goto arithmetic_result_is_null;
   66042            4495 :         u.ag.rB /= u.ag.rA;
   66043            4495 :         break;
   66044                 :       }
   66045                 :       default: {
   66046               0 :         u.ag.iA = (i64)u.ag.rA;
   66047               0 :         u.ag.iB = (i64)u.ag.rB;
   66048               0 :         if( u.ag.iA==0 ) goto arithmetic_result_is_null;
   66049               0 :         if( u.ag.iA==-1 ) u.ag.iA = 1;
   66050               0 :         u.ag.rB = (double)(u.ag.iB % u.ag.iA);
   66051               0 :         break;
   66052                 :       }
   66053                 :     }
   66054                 : #ifdef SQLITE_OMIT_FLOATING_POINT
   66055                 :     pOut->u.i = u.ag.rB;
   66056                 :     MemSetTypeFlag(pOut, MEM_Int);
   66057                 : #else
   66058            6786 :     if( sqlite3IsNaN(u.ag.rB) ){
   66059               0 :       goto arithmetic_result_is_null;
   66060                 :     }
   66061            6786 :     pOut->r = u.ag.rB;
   66062            6786 :     MemSetTypeFlag(pOut, MEM_Real);
   66063            6786 :     if( (u.ag.flags & MEM_Real)==0 ){
   66064              32 :       sqlite3VdbeIntegerAffinity(pOut);
   66065                 :     }
   66066                 : #endif
   66067                 :   }
   66068           72995 :   break;
   66069                 : 
   66070                 : arithmetic_result_is_null:
   66071            2043 :   sqlite3VdbeMemSetNull(pOut);
   66072            2043 :   break;
   66073                 : }
   66074                 : 
   66075                 : /* Opcode: CollSeq * * P4
   66076                 : **
   66077                 : ** P4 is a pointer to a CollSeq struct. If the next call to a user function
   66078                 : ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
   66079                 : ** be returned. This is used by the built-in min(), max() and nullif()
   66080                 : ** functions.
   66081                 : **
   66082                 : ** The interface used by the implementation of the aforementioned functions
   66083                 : ** to retrieve the collation sequence set by this opcode is not available
   66084                 : ** publicly, only to user functions defined in func.c.
   66085                 : */
   66086                 : case OP_CollSeq: {
   66087           53550 :   assert( pOp->p4type==P4_COLLSEQ );
   66088           53550 :   break;
   66089                 : }
   66090                 : 
   66091                 : /* Opcode: Function P1 P2 P3 P4 P5
   66092                 : **
   66093                 : ** Invoke a user function (P4 is a pointer to a Function structure that
   66094                 : ** defines the function) with P5 arguments taken from register P2 and
   66095                 : ** successors.  The result of the function is stored in register P3.
   66096                 : ** Register P3 must not be one of the function inputs.
   66097                 : **
   66098                 : ** P1 is a 32-bit bitmask indicating whether or not each argument to the 
   66099                 : ** function was determined to be constant at compile time. If the first
   66100                 : ** argument was constant then bit 0 of P1 is set. This is used to determine
   66101                 : ** whether meta data associated with a user function argument using the
   66102                 : ** sqlite3_set_auxdata() API may be safely retained until the next
   66103                 : ** invocation of this opcode.
   66104                 : **
   66105                 : ** See also: AggStep and AggFinal
   66106                 : */
   66107                 : case OP_Function: {
   66108                 : #if 0  /* local variables moved into u.ah */
   66109                 :   int i;
   66110                 :   Mem *pArg;
   66111                 :   sqlite3_context ctx;
   66112                 :   sqlite3_value **apVal;
   66113                 :   int n;
   66114                 : #endif /* local variables moved into u.ah */
   66115                 : 
   66116           83603 :   u.ah.n = pOp->p5;
   66117           83603 :   u.ah.apVal = p->apArg;
   66118           83603 :   assert( u.ah.apVal || u.ah.n==0 );
   66119           83603 :   assert( pOp->p3>0 && pOp->p3<=p->nMem );
   66120           83603 :   pOut = &aMem[pOp->p3];
   66121           83603 :   memAboutToChange(p, pOut);
   66122                 : 
   66123           83603 :   assert( u.ah.n==0 || (pOp->p2>0 && pOp->p2+u.ah.n<=p->nMem+1) );
   66124           83603 :   assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ah.n );
   66125           83603 :   u.ah.pArg = &aMem[pOp->p2];
   66126          234640 :   for(u.ah.i=0; u.ah.i<u.ah.n; u.ah.i++, u.ah.pArg++){
   66127          151037 :     assert( memIsValid(u.ah.pArg) );
   66128          151037 :     u.ah.apVal[u.ah.i] = u.ah.pArg;
   66129          151037 :     Deephemeralize(u.ah.pArg);
   66130          151037 :     sqlite3VdbeMemStoreType(u.ah.pArg);
   66131          151037 :     REGISTER_TRACE(pOp->p2+u.ah.i, u.ah.pArg);
   66132                 :   }
   66133                 : 
   66134           83603 :   assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
   66135           83603 :   if( pOp->p4type==P4_FUNCDEF ){
   66136           83603 :     u.ah.ctx.pFunc = pOp->p4.pFunc;
   66137           83603 :     u.ah.ctx.pVdbeFunc = 0;
   66138                 :   }else{
   66139               0 :     u.ah.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
   66140               0 :     u.ah.ctx.pFunc = u.ah.ctx.pVdbeFunc->pFunc;
   66141                 :   }
   66142                 : 
   66143           83603 :   u.ah.ctx.s.flags = MEM_Null;
   66144           83603 :   u.ah.ctx.s.db = db;
   66145           83603 :   u.ah.ctx.s.xDel = 0;
   66146           83603 :   u.ah.ctx.s.zMalloc = 0;
   66147                 : 
   66148                 :   /* The output cell may already have a buffer allocated. Move
   66149                 :   ** the pointer to u.ah.ctx.s so in case the user-function can use
   66150                 :   ** the already allocated buffer instead of allocating a new one.
   66151                 :   */
   66152           83603 :   sqlite3VdbeMemMove(&u.ah.ctx.s, pOut);
   66153           83603 :   MemSetTypeFlag(&u.ah.ctx.s, MEM_Null);
   66154                 : 
   66155           83603 :   u.ah.ctx.isError = 0;
   66156           83603 :   if( u.ah.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
   66157            9658 :     assert( pOp>aOp );
   66158            9658 :     assert( pOp[-1].p4type==P4_COLLSEQ );
   66159            9658 :     assert( pOp[-1].opcode==OP_CollSeq );
   66160            9658 :     u.ah.ctx.pColl = pOp[-1].p4.pColl;
   66161                 :   }
   66162           83603 :   db->lastRowid = lastRowid;
   66163           83603 :   (*u.ah.ctx.pFunc->xFunc)(&u.ah.ctx, u.ah.n, u.ah.apVal); /* IMP: R-24505-23230 */
   66164           83603 :   lastRowid = db->lastRowid;
   66165                 : 
   66166                 :   /* If any auxiliary data functions have been called by this user function,
   66167                 :   ** immediately call the destructor for any non-static values.
   66168                 :   */
   66169           83603 :   if( u.ah.ctx.pVdbeFunc ){
   66170               0 :     sqlite3VdbeDeleteAuxData(u.ah.ctx.pVdbeFunc, pOp->p1);
   66171               0 :     pOp->p4.pVdbeFunc = u.ah.ctx.pVdbeFunc;
   66172               0 :     pOp->p4type = P4_VDBEFUNC;
   66173                 :   }
   66174                 : 
   66175           83603 :   if( db->mallocFailed ){
   66176                 :     /* Even though a malloc() has failed, the implementation of the
   66177                 :     ** user function may have called an sqlite3_result_XXX() function
   66178                 :     ** to return a value. The following call releases any resources
   66179                 :     ** associated with such a value.
   66180                 :     */
   66181               0 :     sqlite3VdbeMemRelease(&u.ah.ctx.s);
   66182               0 :     goto no_mem;
   66183                 :   }
   66184                 : 
   66185                 :   /* If the function returned an error, throw an exception */
   66186           83603 :   if( u.ah.ctx.isError ){
   66187               0 :     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ah.ctx.s));
   66188               0 :     rc = u.ah.ctx.isError;
   66189                 :   }
   66190                 : 
   66191                 :   /* Copy the result of the function into register P3 */
   66192           83603 :   sqlite3VdbeChangeEncoding(&u.ah.ctx.s, encoding);
   66193           83603 :   sqlite3VdbeMemMove(pOut, &u.ah.ctx.s);
   66194           83603 :   if( sqlite3VdbeMemTooBig(pOut) ){
   66195               0 :     goto too_big;
   66196                 :   }
   66197                 : 
   66198                 : #if 0
   66199                 :   /* The app-defined function has done something that as caused this
   66200                 :   ** statement to expire.  (Perhaps the function called sqlite3_exec()
   66201                 :   ** with a CREATE TABLE statement.)
   66202                 :   */
   66203                 :   if( p->expired ) rc = SQLITE_ABORT;
   66204                 : #endif
   66205                 : 
   66206           83603 :   REGISTER_TRACE(pOp->p3, pOut);
   66207                 :   UPDATE_MAX_BLOBSIZE(pOut);
   66208           83603 :   break;
   66209                 : }
   66210                 : 
   66211                 : /* Opcode: BitAnd P1 P2 P3 * *
   66212                 : **
   66213                 : ** Take the bit-wise AND of the values in register P1 and P2 and
   66214                 : ** store the result in register P3.
   66215                 : ** If either input is NULL, the result is NULL.
   66216                 : */
   66217                 : /* Opcode: BitOr P1 P2 P3 * *
   66218                 : **
   66219                 : ** Take the bit-wise OR of the values in register P1 and P2 and
   66220                 : ** store the result in register P3.
   66221                 : ** If either input is NULL, the result is NULL.
   66222                 : */
   66223                 : /* Opcode: ShiftLeft P1 P2 P3 * *
   66224                 : **
   66225                 : ** Shift the integer value in register P2 to the left by the
   66226                 : ** number of bits specified by the integer in register P1.
   66227                 : ** Store the result in register P3.
   66228                 : ** If either input is NULL, the result is NULL.
   66229                 : */
   66230                 : /* Opcode: ShiftRight P1 P2 P3 * *
   66231                 : **
   66232                 : ** Shift the integer value in register P2 to the right by the
   66233                 : ** number of bits specified by the integer in register P1.
   66234                 : ** Store the result in register P3.
   66235                 : ** If either input is NULL, the result is NULL.
   66236                 : */
   66237                 : case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
   66238                 : case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
   66239                 : case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
   66240                 : case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
   66241                 : #if 0  /* local variables moved into u.ai */
   66242                 :   i64 iA;
   66243                 :   u64 uA;
   66244                 :   i64 iB;
   66245                 :   u8 op;
   66246                 : #endif /* local variables moved into u.ai */
   66247                 : 
   66248              24 :   pIn1 = &aMem[pOp->p1];
   66249              24 :   pIn2 = &aMem[pOp->p2];
   66250              24 :   pOut = &aMem[pOp->p3];
   66251              24 :   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
   66252               0 :     sqlite3VdbeMemSetNull(pOut);
   66253               0 :     break;
   66254                 :   }
   66255              24 :   u.ai.iA = sqlite3VdbeIntValue(pIn2);
   66256              24 :   u.ai.iB = sqlite3VdbeIntValue(pIn1);
   66257              24 :   u.ai.op = pOp->opcode;
   66258              24 :   if( u.ai.op==OP_BitAnd ){
   66259               0 :     u.ai.iA &= u.ai.iB;
   66260              24 :   }else if( u.ai.op==OP_BitOr ){
   66261              24 :     u.ai.iA |= u.ai.iB;
   66262               0 :   }else if( u.ai.iB!=0 ){
   66263               0 :     assert( u.ai.op==OP_ShiftRight || u.ai.op==OP_ShiftLeft );
   66264                 : 
   66265                 :     /* If shifting by a negative amount, shift in the other direction */
   66266               0 :     if( u.ai.iB<0 ){
   66267                 :       assert( OP_ShiftRight==OP_ShiftLeft+1 );
   66268               0 :       u.ai.op = 2*OP_ShiftLeft + 1 - u.ai.op;
   66269               0 :       u.ai.iB = u.ai.iB>(-64) ? -u.ai.iB : 64;
   66270                 :     }
   66271                 : 
   66272               0 :     if( u.ai.iB>=64 ){
   66273               0 :       u.ai.iA = (u.ai.iA>=0 || u.ai.op==OP_ShiftLeft) ? 0 : -1;
   66274                 :     }else{
   66275               0 :       memcpy(&u.ai.uA, &u.ai.iA, sizeof(u.ai.uA));
   66276               0 :       if( u.ai.op==OP_ShiftLeft ){
   66277               0 :         u.ai.uA <<= u.ai.iB;
   66278                 :       }else{
   66279               0 :         u.ai.uA >>= u.ai.iB;
   66280                 :         /* Sign-extend on a right shift of a negative number */
   66281               0 :         if( u.ai.iA<0 ) u.ai.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ai.iB);
   66282                 :       }
   66283               0 :       memcpy(&u.ai.iA, &u.ai.uA, sizeof(u.ai.iA));
   66284                 :     }
   66285                 :   }
   66286              24 :   pOut->u.i = u.ai.iA;
   66287              24 :   MemSetTypeFlag(pOut, MEM_Int);
   66288              24 :   break;
   66289                 : }
   66290                 : 
   66291                 : /* Opcode: AddImm  P1 P2 * * *
   66292                 : ** 
   66293                 : ** Add the constant P2 to the value in register P1.
   66294                 : ** The result is always an integer.
   66295                 : **
   66296                 : ** To force any register to be an integer, just add 0.
   66297                 : */
   66298                 : case OP_AddImm: {            /* in1 */
   66299           30307 :   pIn1 = &aMem[pOp->p1];
   66300           30307 :   memAboutToChange(p, pIn1);
   66301           30307 :   sqlite3VdbeMemIntegerify(pIn1);
   66302           30307 :   pIn1->u.i += pOp->p2;
   66303           30307 :   break;
   66304                 : }
   66305                 : 
   66306                 : /* Opcode: MustBeInt P1 P2 * * *
   66307                 : ** 
   66308                 : ** Force the value in register P1 to be an integer.  If the value
   66309                 : ** in P1 is not an integer and cannot be converted into an integer
   66310                 : ** without data loss, then jump immediately to P2, or if P2==0
   66311                 : ** raise an SQLITE_MISMATCH exception.
   66312                 : */
   66313                 : case OP_MustBeInt: {            /* jump, in1 */
   66314           95622 :   pIn1 = &aMem[pOp->p1];
   66315           95622 :   applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
   66316           95622 :   if( (pIn1->flags & MEM_Int)==0 ){
   66317            9384 :     if( pOp->p2==0 ){
   66318               0 :       rc = SQLITE_MISMATCH;
   66319               0 :       goto abort_due_to_error;
   66320                 :     }else{
   66321            9384 :       pc = pOp->p2 - 1;
   66322                 :     }
   66323                 :   }else{
   66324           86238 :     MemSetTypeFlag(pIn1, MEM_Int);
   66325                 :   }
   66326           95622 :   break;
   66327                 : }
   66328                 : 
   66329                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   66330                 : /* Opcode: RealAffinity P1 * * * *
   66331                 : **
   66332                 : ** If register P1 holds an integer convert it to a real value.
   66333                 : **
   66334                 : ** This opcode is used when extracting information from a column that
   66335                 : ** has REAL affinity.  Such column values may still be stored as
   66336                 : ** integers, for space efficiency, but after extraction we want them
   66337                 : ** to have only a real value.
   66338                 : */
   66339                 : case OP_RealAffinity: {                  /* in1 */
   66340              46 :   pIn1 = &aMem[pOp->p1];
   66341              46 :   if( pIn1->flags & MEM_Int ){
   66342               0 :     sqlite3VdbeMemRealify(pIn1);
   66343                 :   }
   66344              46 :   break;
   66345                 : }
   66346                 : #endif
   66347                 : 
   66348                 : #ifndef SQLITE_OMIT_CAST
   66349                 : /* Opcode: ToText P1 * * * *
   66350                 : **
   66351                 : ** Force the value in register P1 to be text.
   66352                 : ** If the value is numeric, convert it to a string using the
   66353                 : ** equivalent of printf().  Blob values are unchanged and
   66354                 : ** are afterwards simply interpreted as text.
   66355                 : **
   66356                 : ** A NULL value is not changed by this routine.  It remains NULL.
   66357                 : */
   66358                 : case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
   66359               0 :   pIn1 = &aMem[pOp->p1];
   66360               0 :   memAboutToChange(p, pIn1);
   66361               0 :   if( pIn1->flags & MEM_Null ) break;
   66362                 :   assert( MEM_Str==(MEM_Blob>>3) );
   66363               0 :   pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
   66364               0 :   applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
   66365               0 :   rc = ExpandBlob(pIn1);
   66366               0 :   assert( pIn1->flags & MEM_Str || db->mallocFailed );
   66367               0 :   pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
   66368                 :   UPDATE_MAX_BLOBSIZE(pIn1);
   66369               0 :   break;
   66370                 : }
   66371                 : 
   66372                 : /* Opcode: ToBlob P1 * * * *
   66373                 : **
   66374                 : ** Force the value in register P1 to be a BLOB.
   66375                 : ** If the value is numeric, convert it to a string first.
   66376                 : ** Strings are simply reinterpreted as blobs with no change
   66377                 : ** to the underlying data.
   66378                 : **
   66379                 : ** A NULL value is not changed by this routine.  It remains NULL.
   66380                 : */
   66381                 : case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
   66382               0 :   pIn1 = &aMem[pOp->p1];
   66383               0 :   if( pIn1->flags & MEM_Null ) break;
   66384               0 :   if( (pIn1->flags & MEM_Blob)==0 ){
   66385               0 :     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
   66386               0 :     assert( pIn1->flags & MEM_Str || db->mallocFailed );
   66387               0 :     MemSetTypeFlag(pIn1, MEM_Blob);
   66388                 :   }else{
   66389               0 :     pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
   66390                 :   }
   66391                 :   UPDATE_MAX_BLOBSIZE(pIn1);
   66392               0 :   break;
   66393                 : }
   66394                 : 
   66395                 : /* Opcode: ToNumeric P1 * * * *
   66396                 : **
   66397                 : ** Force the value in register P1 to be numeric (either an
   66398                 : ** integer or a floating-point number.)
   66399                 : ** If the value is text or blob, try to convert it to an using the
   66400                 : ** equivalent of atoi() or atof() and store 0 if no such conversion 
   66401                 : ** is possible.
   66402                 : **
   66403                 : ** A NULL value is not changed by this routine.  It remains NULL.
   66404                 : */
   66405                 : case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
   66406               0 :   pIn1 = &aMem[pOp->p1];
   66407               0 :   sqlite3VdbeMemNumerify(pIn1);
   66408               0 :   break;
   66409                 : }
   66410                 : #endif /* SQLITE_OMIT_CAST */
   66411                 : 
   66412                 : /* Opcode: ToInt P1 * * * *
   66413                 : **
   66414                 : ** Force the value in register P1 to be an integer.  If
   66415                 : ** The value is currently a real number, drop its fractional part.
   66416                 : ** If the value is text or blob, try to convert it to an integer using the
   66417                 : ** equivalent of atoi() and store 0 if no such conversion is possible.
   66418                 : **
   66419                 : ** A NULL value is not changed by this routine.  It remains NULL.
   66420                 : */
   66421                 : case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
   66422            2385 :   pIn1 = &aMem[pOp->p1];
   66423            2385 :   if( (pIn1->flags & MEM_Null)==0 ){
   66424            2385 :     sqlite3VdbeMemIntegerify(pIn1);
   66425                 :   }
   66426            2385 :   break;
   66427                 : }
   66428                 : 
   66429                 : #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
   66430                 : /* Opcode: ToReal P1 * * * *
   66431                 : **
   66432                 : ** Force the value in register P1 to be a floating point number.
   66433                 : ** If The value is currently an integer, convert it.
   66434                 : ** If the value is text or blob, try to convert it to an integer using the
   66435                 : ** equivalent of atoi() and store 0.0 if no such conversion is possible.
   66436                 : **
   66437                 : ** A NULL value is not changed by this routine.  It remains NULL.
   66438                 : */
   66439                 : case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
   66440            4915 :   pIn1 = &aMem[pOp->p1];
   66441            4915 :   memAboutToChange(p, pIn1);
   66442            4915 :   if( (pIn1->flags & MEM_Null)==0 ){
   66443            4915 :     sqlite3VdbeMemRealify(pIn1);
   66444                 :   }
   66445            4915 :   break;
   66446                 : }
   66447                 : #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
   66448                 : 
   66449                 : /* Opcode: Lt P1 P2 P3 P4 P5
   66450                 : **
   66451                 : ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
   66452                 : ** jump to address P2.  
   66453                 : **
   66454                 : ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
   66455                 : ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
   66456                 : ** bit is clear then fall through if either operand is NULL.
   66457                 : **
   66458                 : ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
   66459                 : ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made 
   66460                 : ** to coerce both inputs according to this affinity before the
   66461                 : ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
   66462                 : ** affinity is used. Note that the affinity conversions are stored
   66463                 : ** back into the input registers P1 and P3.  So this opcode can cause
   66464                 : ** persistent changes to registers P1 and P3.
   66465                 : **
   66466                 : ** Once any conversions have taken place, and neither value is NULL, 
   66467                 : ** the values are compared. If both values are blobs then memcmp() is
   66468                 : ** used to determine the results of the comparison.  If both values
   66469                 : ** are text, then the appropriate collating function specified in
   66470                 : ** P4 is  used to do the comparison.  If P4 is not specified then
   66471                 : ** memcmp() is used to compare text string.  If both values are
   66472                 : ** numeric, then a numeric comparison is used. If the two values
   66473                 : ** are of different types, then numbers are considered less than
   66474                 : ** strings and strings are considered less than blobs.
   66475                 : **
   66476                 : ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
   66477                 : ** store a boolean result (either 0, or 1, or NULL) in register P2.
   66478                 : */
   66479                 : /* Opcode: Ne P1 P2 P3 P4 P5
   66480                 : **
   66481                 : ** This works just like the Lt opcode except that the jump is taken if
   66482                 : ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
   66483                 : ** additional information.
   66484                 : **
   66485                 : ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
   66486                 : ** true or false and is never NULL.  If both operands are NULL then the result
   66487                 : ** of comparison is false.  If either operand is NULL then the result is true.
   66488                 : ** If neither operand is NULL the result is the same as it would be if
   66489                 : ** the SQLITE_NULLEQ flag were omitted from P5.
   66490                 : */
   66491                 : /* Opcode: Eq P1 P2 P3 P4 P5
   66492                 : **
   66493                 : ** This works just like the Lt opcode except that the jump is taken if
   66494                 : ** the operands in registers P1 and P3 are equal.
   66495                 : ** See the Lt opcode for additional information.
   66496                 : **
   66497                 : ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
   66498                 : ** true or false and is never NULL.  If both operands are NULL then the result
   66499                 : ** of comparison is true.  If either operand is NULL then the result is false.
   66500                 : ** If neither operand is NULL the result is the same as it would be if
   66501                 : ** the SQLITE_NULLEQ flag were omitted from P5.
   66502                 : */
   66503                 : /* Opcode: Le P1 P2 P3 P4 P5
   66504                 : **
   66505                 : ** This works just like the Lt opcode except that the jump is taken if
   66506                 : ** the content of register P3 is less than or equal to the content of
   66507                 : ** register P1.  See the Lt opcode for additional information.
   66508                 : */
   66509                 : /* Opcode: Gt P1 P2 P3 P4 P5
   66510                 : **
   66511                 : ** This works just like the Lt opcode except that the jump is taken if
   66512                 : ** the content of register P3 is greater than the content of
   66513                 : ** register P1.  See the Lt opcode for additional information.
   66514                 : */
   66515                 : /* Opcode: Ge P1 P2 P3 P4 P5
   66516                 : **
   66517                 : ** This works just like the Lt opcode except that the jump is taken if
   66518                 : ** the content of register P3 is greater than or equal to the content of
   66519                 : ** register P1.  See the Lt opcode for additional information.
   66520                 : */
   66521                 : case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
   66522                 : case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
   66523                 : case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
   66524                 : case OP_Le:               /* same as TK_LE, jump, in1, in3 */
   66525                 : case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
   66526                 : case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
   66527                 : #if 0  /* local variables moved into u.aj */
   66528                 :   int res;            /* Result of the comparison of pIn1 against pIn3 */
   66529                 :   char affinity;      /* Affinity to use for comparison */
   66530                 :   u16 flags1;         /* Copy of initial value of pIn1->flags */
   66531                 :   u16 flags3;         /* Copy of initial value of pIn3->flags */
   66532                 : #endif /* local variables moved into u.aj */
   66533                 : 
   66534          300205 :   pIn1 = &aMem[pOp->p1];
   66535          300205 :   pIn3 = &aMem[pOp->p3];
   66536          300205 :   u.aj.flags1 = pIn1->flags;
   66537          300205 :   u.aj.flags3 = pIn3->flags;
   66538          300205 :   if( (u.aj.flags1 | u.aj.flags3)&MEM_Null ){
   66539                 :     /* One or both operands are NULL */
   66540            6795 :     if( pOp->p5 & SQLITE_NULLEQ ){
   66541                 :       /* If SQLITE_NULLEQ is set (which will only happen if the operator is
   66542                 :       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
   66543                 :       ** or not both operands are null.
   66544                 :       */
   66545            3318 :       assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
   66546            3318 :       u.aj.res = (u.aj.flags1 & u.aj.flags3 & MEM_Null)==0;
   66547                 :     }else{
   66548                 :       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
   66549                 :       ** then the result is always NULL.
   66550                 :       ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
   66551                 :       */
   66552            3477 :       if( pOp->p5 & SQLITE_STOREP2 ){
   66553               0 :         pOut = &aMem[pOp->p2];
   66554               0 :         MemSetTypeFlag(pOut, MEM_Null);
   66555               0 :         REGISTER_TRACE(pOp->p2, pOut);
   66556            3477 :       }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
   66557            3474 :         pc = pOp->p2-1;
   66558                 :       }
   66559            3477 :       break;
   66560                 :     }
   66561                 :   }else{
   66562                 :     /* Neither operand is NULL.  Do a comparison. */
   66563          293410 :     u.aj.affinity = pOp->p5 & SQLITE_AFF_MASK;
   66564          293410 :     if( u.aj.affinity ){
   66565          277948 :       applyAffinity(pIn1, u.aj.affinity, encoding);
   66566          277948 :       applyAffinity(pIn3, u.aj.affinity, encoding);
   66567          277948 :       if( db->mallocFailed ) goto no_mem;
   66568                 :     }
   66569                 : 
   66570          293410 :     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
   66571          293410 :     ExpandBlob(pIn1);
   66572          293410 :     ExpandBlob(pIn3);
   66573          293410 :     u.aj.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
   66574                 :   }
   66575          296728 :   switch( pOp->opcode ){
   66576           13930 :     case OP_Eq:    u.aj.res = u.aj.res==0;     break;
   66577          258197 :     case OP_Ne:    u.aj.res = u.aj.res!=0;     break;
   66578           10680 :     case OP_Lt:    u.aj.res = u.aj.res<0;      break;
   66579            7789 :     case OP_Le:    u.aj.res = u.aj.res<=0;     break;
   66580            5956 :     case OP_Gt:    u.aj.res = u.aj.res>0;      break;
   66581             176 :     default:       u.aj.res = u.aj.res>=0;     break;
   66582                 :   }
   66583                 : 
   66584          296728 :   if( pOp->p5 & SQLITE_STOREP2 ){
   66585           10371 :     pOut = &aMem[pOp->p2];
   66586           10371 :     memAboutToChange(p, pOut);
   66587           10371 :     MemSetTypeFlag(pOut, MEM_Int);
   66588           10371 :     pOut->u.i = u.aj.res;
   66589           10371 :     REGISTER_TRACE(pOp->p2, pOut);
   66590          286357 :   }else if( u.aj.res ){
   66591          201448 :     pc = pOp->p2-1;
   66592                 :   }
   66593                 : 
   66594                 :   /* Undo any changes made by applyAffinity() to the input registers. */
   66595          296728 :   pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.aj.flags1&MEM_TypeMask);
   66596          296728 :   pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.aj.flags3&MEM_TypeMask);
   66597          296728 :   break;
   66598                 : }
   66599                 : 
   66600                 : /* Opcode: Permutation * * * P4 *
   66601                 : **
   66602                 : ** Set the permutation used by the OP_Compare operator to be the array
   66603                 : ** of integers in P4.
   66604                 : **
   66605                 : ** The permutation is only valid until the next OP_Permutation, OP_Compare,
   66606                 : ** OP_Halt, or OP_ResultRow.  Typically the OP_Permutation should occur
   66607                 : ** immediately prior to the OP_Compare.
   66608                 : */
   66609                 : case OP_Permutation: {
   66610               0 :   assert( pOp->p4type==P4_INTARRAY );
   66611               0 :   assert( pOp->p4.ai );
   66612               0 :   aPermute = pOp->p4.ai;
   66613               0 :   break;
   66614                 : }
   66615                 : 
   66616                 : /* Opcode: Compare P1 P2 P3 P4 *
   66617                 : **
   66618                 : ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
   66619                 : ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
   66620                 : ** the comparison for use by the next OP_Jump instruct.
   66621                 : **
   66622                 : ** P4 is a KeyInfo structure that defines collating sequences and sort
   66623                 : ** orders for the comparison.  The permutation applies to registers
   66624                 : ** only.  The KeyInfo elements are used sequentially.
   66625                 : **
   66626                 : ** The comparison is a sort comparison, so NULLs compare equal,
   66627                 : ** NULLs are less than numbers, numbers are less than strings,
   66628                 : ** and strings are less than blobs.
   66629                 : */
   66630                 : case OP_Compare: {
   66631                 : #if 0  /* local variables moved into u.ak */
   66632                 :   int n;
   66633                 :   int i;
   66634                 :   int p1;
   66635                 :   int p2;
   66636                 :   const KeyInfo *pKeyInfo;
   66637                 :   int idx;
   66638                 :   CollSeq *pColl;    /* Collating sequence to use on this term */
   66639                 :   int bRev;          /* True for DESCENDING sort order */
   66640                 : #endif /* local variables moved into u.ak */
   66641                 : 
   66642            1369 :   u.ak.n = pOp->p3;
   66643            1369 :   u.ak.pKeyInfo = pOp->p4.pKeyInfo;
   66644            1369 :   assert( u.ak.n>0 );
   66645            1369 :   assert( u.ak.pKeyInfo!=0 );
   66646            1369 :   u.ak.p1 = pOp->p1;
   66647            1369 :   u.ak.p2 = pOp->p2;
   66648                 : #if SQLITE_DEBUG
   66649            1369 :   if( aPermute ){
   66650               0 :     int k, mx = 0;
   66651               0 :     for(k=0; k<u.ak.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
   66652               0 :     assert( u.ak.p1>0 && u.ak.p1+mx<=p->nMem+1 );
   66653               0 :     assert( u.ak.p2>0 && u.ak.p2+mx<=p->nMem+1 );
   66654                 :   }else{
   66655            1369 :     assert( u.ak.p1>0 && u.ak.p1+u.ak.n<=p->nMem+1 );
   66656            1369 :     assert( u.ak.p2>0 && u.ak.p2+u.ak.n<=p->nMem+1 );
   66657                 :   }
   66658                 : #endif /* SQLITE_DEBUG */
   66659            2268 :   for(u.ak.i=0; u.ak.i<u.ak.n; u.ak.i++){
   66660            1369 :     u.ak.idx = aPermute ? aPermute[u.ak.i] : u.ak.i;
   66661            1369 :     assert( memIsValid(&aMem[u.ak.p1+u.ak.idx]) );
   66662            1369 :     assert( memIsValid(&aMem[u.ak.p2+u.ak.idx]) );
   66663            1369 :     REGISTER_TRACE(u.ak.p1+u.ak.idx, &aMem[u.ak.p1+u.ak.idx]);
   66664            1369 :     REGISTER_TRACE(u.ak.p2+u.ak.idx, &aMem[u.ak.p2+u.ak.idx]);
   66665            1369 :     assert( u.ak.i<u.ak.pKeyInfo->nField );
   66666            1369 :     u.ak.pColl = u.ak.pKeyInfo->aColl[u.ak.i];
   66667            1369 :     u.ak.bRev = u.ak.pKeyInfo->aSortOrder[u.ak.i];
   66668            1369 :     iCompare = sqlite3MemCompare(&aMem[u.ak.p1+u.ak.idx], &aMem[u.ak.p2+u.ak.idx], u.ak.pColl);
   66669            1369 :     if( iCompare ){
   66670             470 :       if( u.ak.bRev ) iCompare = -iCompare;
   66671             470 :       break;
   66672                 :     }
   66673                 :   }
   66674            1369 :   aPermute = 0;
   66675            1369 :   break;
   66676                 : }
   66677                 : 
   66678                 : /* Opcode: Jump P1 P2 P3 * *
   66679                 : **
   66680                 : ** Jump to the instruction at address P1, P2, or P3 depending on whether
   66681                 : ** in the most recent OP_Compare instruction the P1 vector was less than
   66682                 : ** equal to, or greater than the P2 vector, respectively.
   66683                 : */
   66684                 : case OP_Jump: {             /* jump */
   66685            1369 :   if( iCompare<0 ){
   66686             470 :     pc = pOp->p1 - 1;
   66687             899 :   }else if( iCompare==0 ){
   66688             899 :     pc = pOp->p2 - 1;
   66689                 :   }else{
   66690               0 :     pc = pOp->p3 - 1;
   66691                 :   }
   66692            1369 :   break;
   66693                 : }
   66694                 : 
   66695                 : /* Opcode: And P1 P2 P3 * *
   66696                 : **
   66697                 : ** Take the logical AND of the values in registers P1 and P2 and
   66698                 : ** write the result into register P3.
   66699                 : **
   66700                 : ** If either P1 or P2 is 0 (false) then the result is 0 even if
   66701                 : ** the other input is NULL.  A NULL and true or two NULLs give
   66702                 : ** a NULL output.
   66703                 : */
   66704                 : /* Opcode: Or P1 P2 P3 * *
   66705                 : **
   66706                 : ** Take the logical OR of the values in register P1 and P2 and
   66707                 : ** store the answer in register P3.
   66708                 : **
   66709                 : ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
   66710                 : ** even if the other input is NULL.  A NULL and false or two NULLs
   66711                 : ** give a NULL output.
   66712                 : */
   66713                 : case OP_And:              /* same as TK_AND, in1, in2, out3 */
   66714                 : case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
   66715                 : #if 0  /* local variables moved into u.al */
   66716                 :   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
   66717                 :   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
   66718                 : #endif /* local variables moved into u.al */
   66719                 : 
   66720            5002 :   pIn1 = &aMem[pOp->p1];
   66721            5002 :   if( pIn1->flags & MEM_Null ){
   66722               0 :     u.al.v1 = 2;
   66723                 :   }else{
   66724            5002 :     u.al.v1 = sqlite3VdbeIntValue(pIn1)!=0;
   66725                 :   }
   66726            5002 :   pIn2 = &aMem[pOp->p2];
   66727            5002 :   if( pIn2->flags & MEM_Null ){
   66728               0 :     u.al.v2 = 2;
   66729                 :   }else{
   66730            5002 :     u.al.v2 = sqlite3VdbeIntValue(pIn2)!=0;
   66731                 :   }
   66732            5002 :   if( pOp->opcode==OP_And ){
   66733                 :     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
   66734            5002 :     u.al.v1 = and_logic[u.al.v1*3+u.al.v2];
   66735                 :   }else{
   66736                 :     static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
   66737               0 :     u.al.v1 = or_logic[u.al.v1*3+u.al.v2];
   66738                 :   }
   66739            5002 :   pOut = &aMem[pOp->p3];
   66740            5002 :   if( u.al.v1==2 ){
   66741               0 :     MemSetTypeFlag(pOut, MEM_Null);
   66742                 :   }else{
   66743            5002 :     pOut->u.i = u.al.v1;
   66744            5002 :     MemSetTypeFlag(pOut, MEM_Int);
   66745                 :   }
   66746            5002 :   break;
   66747                 : }
   66748                 : 
   66749                 : /* Opcode: Not P1 P2 * * *
   66750                 : **
   66751                 : ** Interpret the value in register P1 as a boolean value.  Store the
   66752                 : ** boolean complement in register P2.  If the value in register P1 is 
   66753                 : ** NULL, then a NULL is stored in P2.
   66754                 : */
   66755                 : case OP_Not: {                /* same as TK_NOT, in1, out2 */
   66756            3548 :   pIn1 = &aMem[pOp->p1];
   66757            3548 :   pOut = &aMem[pOp->p2];
   66758            3548 :   if( pIn1->flags & MEM_Null ){
   66759               1 :     sqlite3VdbeMemSetNull(pOut);
   66760                 :   }else{
   66761            3547 :     sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
   66762                 :   }
   66763            3548 :   break;
   66764                 : }
   66765                 : 
   66766                 : /* Opcode: BitNot P1 P2 * * *
   66767                 : **
   66768                 : ** Interpret the content of register P1 as an integer.  Store the
   66769                 : ** ones-complement of the P1 value into register P2.  If P1 holds
   66770                 : ** a NULL then store a NULL in P2.
   66771                 : */
   66772                 : case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
   66773               0 :   pIn1 = &aMem[pOp->p1];
   66774               0 :   pOut = &aMem[pOp->p2];
   66775               0 :   if( pIn1->flags & MEM_Null ){
   66776               0 :     sqlite3VdbeMemSetNull(pOut);
   66777                 :   }else{
   66778               0 :     sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
   66779                 :   }
   66780               0 :   break;
   66781                 : }
   66782                 : 
   66783                 : /* Opcode: Once P1 P2 * * *
   66784                 : **
   66785                 : ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
   66786                 : ** set the flag and fall through to the next instruction.
   66787                 : **
   66788                 : ** See also: JumpOnce
   66789                 : */
   66790                 : case OP_Once: {             /* jump */
   66791           48320 :   assert( pOp->p1<p->nOnceFlag );
   66792           48320 :   if( p->aOnceFlag[pOp->p1] ){
   66793            3328 :     pc = pOp->p2-1;
   66794                 :   }else{
   66795           44992 :     p->aOnceFlag[pOp->p1] = 1;
   66796                 :   }
   66797           48320 :   break;
   66798                 : }
   66799                 : 
   66800                 : /* Opcode: If P1 P2 P3 * *
   66801                 : **
   66802                 : ** Jump to P2 if the value in register P1 is true.  The value
   66803                 : ** is considered true if it is numeric and non-zero.  If the value
   66804                 : ** in P1 is NULL then take the jump if P3 is non-zero.
   66805                 : */
   66806                 : /* Opcode: IfNot P1 P2 P3 * *
   66807                 : **
   66808                 : ** Jump to P2 if the value in register P1 is False.  The value
   66809                 : ** is considered false if it has a numeric value of zero.  If the value
   66810                 : ** in P1 is NULL then take the jump if P3 is zero.
   66811                 : */
   66812                 : case OP_If:                 /* jump, in1 */
   66813                 : case OP_IfNot: {            /* jump, in1 */
   66814                 : #if 0  /* local variables moved into u.am */
   66815                 :   int c;
   66816                 : #endif /* local variables moved into u.am */
   66817           37240 :   pIn1 = &aMem[pOp->p1];
   66818           37240 :   if( pIn1->flags & MEM_Null ){
   66819              14 :     u.am.c = pOp->p3;
   66820                 :   }else{
   66821                 : #ifdef SQLITE_OMIT_FLOATING_POINT
   66822                 :     u.am.c = sqlite3VdbeIntValue(pIn1)!=0;
   66823                 : #else
   66824           37226 :     u.am.c = sqlite3VdbeRealValue(pIn1)!=0.0;
   66825                 : #endif
   66826           37226 :     if( pOp->opcode==OP_IfNot ) u.am.c = !u.am.c;
   66827                 :   }
   66828           37240 :   if( u.am.c ){
   66829           14223 :     pc = pOp->p2-1;
   66830                 :   }
   66831           37240 :   break;
   66832                 : }
   66833                 : 
   66834                 : /* Opcode: IsNull P1 P2 * * *
   66835                 : **
   66836                 : ** Jump to P2 if the value in register P1 is NULL.
   66837                 : */
   66838                 : case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
   66839          179355 :   pIn1 = &aMem[pOp->p1];
   66840          179355 :   if( (pIn1->flags & MEM_Null)!=0 ){
   66841            5575 :     pc = pOp->p2 - 1;
   66842                 :   }
   66843          179355 :   break;
   66844                 : }
   66845                 : 
   66846                 : /* Opcode: NotNull P1 P2 * * *
   66847                 : **
   66848                 : ** Jump to P2 if the value in register P1 is not NULL.  
   66849                 : */
   66850                 : case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
   66851           79642 :   pIn1 = &aMem[pOp->p1];
   66852           79642 :   if( (pIn1->flags & MEM_Null)==0 ){
   66853           52864 :     pc = pOp->p2 - 1;
   66854                 :   }
   66855           79642 :   break;
   66856                 : }
   66857                 : 
   66858                 : /* Opcode: Column P1 P2 P3 P4 P5
   66859                 : **
   66860                 : ** Interpret the data that cursor P1 points to as a structure built using
   66861                 : ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
   66862                 : ** information about the format of the data.)  Extract the P2-th column
   66863                 : ** from this record.  If there are less that (P2+1) 
   66864                 : ** values in the record, extract a NULL.
   66865                 : **
   66866                 : ** The value extracted is stored in register P3.
   66867                 : **
   66868                 : ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
   66869                 : ** if the P4 argument is a P4_MEM use the value of the P4 argument as
   66870                 : ** the result.
   66871                 : **
   66872                 : ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
   66873                 : ** then the cache of the cursor is reset prior to extracting the column.
   66874                 : ** The first OP_Column against a pseudo-table after the value of the content
   66875                 : ** register has changed should have this bit set.
   66876                 : */
   66877                 : case OP_Column: {
   66878                 : #if 0  /* local variables moved into u.an */
   66879                 :   u32 payloadSize;   /* Number of bytes in the record */
   66880                 :   i64 payloadSize64; /* Number of bytes in the record */
   66881                 :   int p1;            /* P1 value of the opcode */
   66882                 :   int p2;            /* column number to retrieve */
   66883                 :   VdbeCursor *pC;    /* The VDBE cursor */
   66884                 :   char *zRec;        /* Pointer to complete record-data */
   66885                 :   BtCursor *pCrsr;   /* The BTree cursor */
   66886                 :   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
   66887                 :   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
   66888                 :   int nField;        /* number of fields in the record */
   66889                 :   int len;           /* The length of the serialized data for the column */
   66890                 :   int i;             /* Loop counter */
   66891                 :   char *zData;       /* Part of the record being decoded */
   66892                 :   Mem *pDest;        /* Where to write the extracted value */
   66893                 :   Mem sMem;          /* For storing the record being decoded */
   66894                 :   u8 *zIdx;          /* Index into header */
   66895                 :   u8 *zEndHdr;       /* Pointer to first byte after the header */
   66896                 :   u32 offset;        /* Offset into the data */
   66897                 :   u32 szField;       /* Number of bytes in the content of a field */
   66898                 :   int szHdr;         /* Size of the header size field at start of record */
   66899                 :   int avail;         /* Number of bytes of available data */
   66900                 :   u32 t;             /* A type code from the record header */
   66901                 :   Mem *pReg;         /* PseudoTable input register */
   66902                 : #endif /* local variables moved into u.an */
   66903                 : 
   66904                 : 
   66905         1835308 :   u.an.p1 = pOp->p1;
   66906         1835308 :   u.an.p2 = pOp->p2;
   66907         1835308 :   u.an.pC = 0;
   66908         1835308 :   memset(&u.an.sMem, 0, sizeof(u.an.sMem));
   66909         1835308 :   assert( u.an.p1<p->nCursor );
   66910         1835308 :   assert( pOp->p3>0 && pOp->p3<=p->nMem );
   66911         1835305 :   u.an.pDest = &aMem[pOp->p3];
   66912         1835305 :   memAboutToChange(p, u.an.pDest);
   66913         1835334 :   u.an.zRec = 0;
   66914                 : 
   66915                 :   /* This block sets the variable u.an.payloadSize to be the total number of
   66916                 :   ** bytes in the record.
   66917                 :   **
   66918                 :   ** u.an.zRec is set to be the complete text of the record if it is available.
   66919                 :   ** The complete record text is always available for pseudo-tables
   66920                 :   ** If the record is stored in a cursor, the complete record text
   66921                 :   ** might be available in the  u.an.pC->aRow cache.  Or it might not be.
   66922                 :   ** If the data is unavailable,  u.an.zRec is set to NULL.
   66923                 :   **
   66924                 :   ** We also compute the number of columns in the record.  For cursors,
   66925                 :   ** the number of columns is stored in the VdbeCursor.nField element.
   66926                 :   */
   66927         1835334 :   u.an.pC = p->apCsr[u.an.p1];
   66928         1835334 :   assert( u.an.pC!=0 );
   66929                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   66930         1835334 :   assert( u.an.pC->pVtabCursor==0 );
   66931                 : #endif
   66932         1835334 :   u.an.pCrsr = u.an.pC->pCursor;
   66933         1835334 :   if( u.an.pCrsr!=0 ){
   66934                 :     /* The record is stored in a B-Tree */
   66935         1816608 :     rc = sqlite3VdbeCursorMoveto(u.an.pC);
   66936         1816612 :     if( rc ) goto abort_due_to_error;
   66937         1816611 :     if( u.an.pC->nullRow ){
   66938           21651 :       u.an.payloadSize = 0;
   66939         1794960 :     }else if( u.an.pC->cacheStatus==p->cacheCtr ){
   66940         1231483 :       u.an.payloadSize = u.an.pC->payloadSize;
   66941         1231483 :       u.an.zRec = (char*)u.an.pC->aRow;
   66942          563477 :     }else if( u.an.pC->isIndex ){
   66943           57582 :       assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
   66944           57582 :       VVA_ONLY(rc =) sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
   66945           57582 :       assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
   66946                 :       /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
   66947                 :       ** payload size, so it is impossible for u.an.payloadSize64 to be
   66948                 :       ** larger than 32 bits. */
   66949           57582 :       assert( (u.an.payloadSize64 & SQLITE_MAX_U32)==(u64)u.an.payloadSize64 );
   66950           57582 :       u.an.payloadSize = (u32)u.an.payloadSize64;
   66951                 :     }else{
   66952          505895 :       assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
   66953          505894 :       VVA_ONLY(rc =) sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
   66954          505895 :       assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
   66955                 :     }
   66956           18726 :   }else if( ALWAYS(u.an.pC->pseudoTableReg>0) ){
   66957           18726 :     u.an.pReg = &aMem[u.an.pC->pseudoTableReg];
   66958           18726 :     assert( u.an.pReg->flags & MEM_Blob );
   66959           18726 :     assert( memIsValid(u.an.pReg) );
   66960           18726 :     u.an.payloadSize = u.an.pReg->n;
   66961           18726 :     u.an.zRec = u.an.pReg->z;
   66962           18726 :     u.an.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
   66963           18726 :     assert( u.an.payloadSize==0 || u.an.zRec!=0 );
   66964                 :   }else{
   66965                 :     /* Consider the row to be NULL */
   66966               0 :     u.an.payloadSize = 0;
   66967                 :   }
   66968                 : 
   66969                 :   /* If u.an.payloadSize is 0, then just store a NULL.  This can happen because of
   66970                 :   ** nullRow or because of a corrupt database. */
   66971         1835337 :   if( u.an.payloadSize==0 ){
   66972           21651 :     MemSetTypeFlag(u.an.pDest, MEM_Null);
   66973           21651 :     goto op_column_out;
   66974                 :   }
   66975         1813686 :   assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
   66976         1813686 :   if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
   66977               0 :     goto too_big;
   66978                 :   }
   66979                 : 
   66980         1813686 :   u.an.nField = u.an.pC->nField;
   66981         1813686 :   assert( u.an.p2<u.an.nField );
   66982                 : 
   66983                 :   /* Read and parse the table header.  Store the results of the parse
   66984                 :   ** into the record header cache fields of the cursor.
   66985                 :   */
   66986         1813686 :   u.an.aType = u.an.pC->aType;
   66987         1813686 :   if( u.an.pC->cacheStatus==p->cacheCtr ){
   66988         1244175 :     u.an.aOffset = u.an.pC->aOffset;
   66989                 :   }else{
   66990          569511 :     assert(u.an.aType);
   66991          569511 :     u.an.avail = 0;
   66992          569511 :     u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
   66993          569511 :     u.an.pC->payloadSize = u.an.payloadSize;
   66994          569511 :     u.an.pC->cacheStatus = p->cacheCtr;
   66995                 : 
   66996                 :     /* Figure out how many bytes are in the header */
   66997          569511 :     if( u.an.zRec ){
   66998            6034 :       u.an.zData = u.an.zRec;
   66999                 :     }else{
   67000          563477 :       if( u.an.pC->isIndex ){
   67001           57582 :         u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
   67002                 :       }else{
   67003          505895 :         u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
   67004                 :       }
   67005                 :       /* If KeyFetch()/DataFetch() managed to get the entire payload,
   67006                 :       ** save the payload in the u.an.pC->aRow cache.  That will save us from
   67007                 :       ** having to make additional calls to fetch the content portion of
   67008                 :       ** the record.
   67009                 :       */
   67010          563477 :       assert( u.an.avail>=0 );
   67011          563477 :       if( u.an.payloadSize <= (u32)u.an.avail ){
   67012          563472 :         u.an.zRec = u.an.zData;
   67013          563472 :         u.an.pC->aRow = (u8*)u.an.zData;
   67014                 :       }else{
   67015               5 :         u.an.pC->aRow = 0;
   67016                 :       }
   67017                 :     }
   67018                 :     /* The following assert is true in all cases accept when
   67019                 :     ** the database file has been corrupted externally.
   67020                 :     **    assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
   67021          569511 :     u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
   67022                 : 
   67023                 :     /* Make sure a corrupt database has not given us an oversize header.
   67024                 :     ** Do this now to avoid an oversize memory allocation.
   67025                 :     **
   67026                 :     ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
   67027                 :     ** types use so much data space that there can only be 4096 and 32 of
   67028                 :     ** them, respectively.  So the maximum header length results from a
   67029                 :     ** 3-byte type for each of the maximum of 32768 columns plus three
   67030                 :     ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
   67031                 :     */
   67032          569511 :     if( u.an.offset > 98307 ){
   67033               0 :       rc = SQLITE_CORRUPT_BKPT;
   67034               0 :       goto op_column_out;
   67035                 :     }
   67036                 : 
   67037                 :     /* Compute in u.an.len the number of bytes of data we need to read in order
   67038                 :     ** to get u.an.nField type values.  u.an.offset is an upper bound on this.  But
   67039                 :     ** u.an.nField might be significantly less than the true number of columns
   67040                 :     ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
   67041                 :     ** We want to minimize u.an.len in order to limit the size of the memory
   67042                 :     ** allocation, especially if a corrupt database file has caused u.an.offset
   67043                 :     ** to be oversized. Offset is limited to 98307 above.  But 98307 might
   67044                 :     ** still exceed Robson memory allocation limits on some configurations.
   67045                 :     ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
   67046                 :     ** will likely be much smaller since u.an.nField will likely be less than
   67047                 :     ** 20 or so.  This insures that Robson memory allocation limits are
   67048                 :     ** not exceeded even for corrupt database files.
   67049                 :     */
   67050          569511 :     u.an.len = u.an.nField*5 + 3;
   67051          569511 :     if( u.an.len > (int)u.an.offset ) u.an.len = (int)u.an.offset;
   67052                 : 
   67053                 :     /* The KeyFetch() or DataFetch() above are fast and will get the entire
   67054                 :     ** record header in most cases.  But they will fail to get the complete
   67055                 :     ** record header if the record header does not fit on a single page
   67056                 :     ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
   67057                 :     ** acquire the complete header text.
   67058                 :     */
   67059          569511 :     if( !u.an.zRec && u.an.avail<u.an.len ){
   67060               0 :       u.an.sMem.flags = 0;
   67061               0 :       u.an.sMem.db = 0;
   67062               0 :       rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
   67063               0 :       if( rc!=SQLITE_OK ){
   67064               0 :         goto op_column_out;
   67065                 :       }
   67066               0 :       u.an.zData = u.an.sMem.z;
   67067                 :     }
   67068          569511 :     u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
   67069          569511 :     u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
   67070                 : 
   67071                 :     /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
   67072                 :     ** arrays.  u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
   67073                 :     ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
   67074                 :     ** of the record to the start of the data for the u.an.i-th column
   67075                 :     */
   67076         4350066 :     for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
   67077         3780557 :       if( u.an.zIdx<u.an.zEndHdr ){
   67078         3775000 :         u.an.aOffset[u.an.i] = u.an.offset;
   67079         3775000 :         if( u.an.zIdx[0]<0x80 ){
   67080         3584431 :           u.an.t = u.an.zIdx[0];
   67081         3584431 :           u.an.zIdx++;
   67082                 :         }else{
   67083          190569 :           u.an.zIdx += sqlite3GetVarint32(u.an.zIdx, &u.an.t);
   67084                 :         }
   67085         3774996 :         u.an.aType[u.an.i] = u.an.t;
   67086         3774996 :         u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
   67087         3774998 :         u.an.offset += u.an.szField;
   67088         3774998 :         if( u.an.offset<u.an.szField ){  /* True if u.an.offset overflows */
   67089               0 :           u.an.zIdx = &u.an.zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
   67090               0 :           break;
   67091                 :         }
   67092                 :       }else{
   67093                 :         /* If u.an.i is less that u.an.nField, then there are less fields in this
   67094                 :         ** record than SetNumColumns indicated there are columns in the
   67095                 :         ** table. Set the u.an.offset for any extra columns not present in
   67096                 :         ** the record to 0. This tells code below to store a NULL
   67097                 :         ** instead of deserializing a value from the record.
   67098                 :         */
   67099            5557 :         u.an.aOffset[u.an.i] = 0;
   67100                 :       }
   67101                 :     }
   67102          569509 :     sqlite3VdbeMemRelease(&u.an.sMem);
   67103          569511 :     u.an.sMem.flags = MEM_Null;
   67104                 : 
   67105                 :     /* If we have read more header data than was contained in the header,
   67106                 :     ** or if the end of the last field appears to be past the end of the
   67107                 :     ** record, or if the end of the last field appears to be before the end
   67108                 :     ** of the record (when all fields present), then we must be dealing
   67109                 :     ** with a corrupt database.
   67110                 :     */
   67111          569511 :     if( (u.an.zIdx > u.an.zEndHdr) || (u.an.offset > u.an.payloadSize)
   67112          569508 :          || (u.an.zIdx==u.an.zEndHdr && u.an.offset!=u.an.payloadSize) ){
   67113              14 :       rc = SQLITE_CORRUPT_BKPT;
   67114               3 :       goto op_column_out;
   67115                 :     }
   67116                 :   }
   67117                 : 
   67118                 :   /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
   67119                 :   ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
   67120                 :   ** then there are not enough fields in the record to satisfy the
   67121                 :   ** request.  In this case, set the value NULL or to P4 if P4 is
   67122                 :   ** a pointer to a Mem object.
   67123                 :   */
   67124         1813672 :   if( u.an.aOffset[u.an.p2] ){
   67125         1811403 :     assert( rc==SQLITE_OK );
   67126         1811403 :     if( u.an.zRec ){
   67127         1811385 :       VdbeMemRelease(u.an.pDest);
   67128         1811385 :       sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
   67129                 :     }else{
   67130              18 :       u.an.len = sqlite3VdbeSerialTypeLen(u.an.aType[u.an.p2]);
   67131              18 :       sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
   67132              18 :       rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex, &u.an.sMem);
   67133              18 :       if( rc!=SQLITE_OK ){
   67134               0 :         goto op_column_out;
   67135                 :       }
   67136              18 :       u.an.zData = u.an.sMem.z;
   67137              18 :       sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.aType[u.an.p2], u.an.pDest);
   67138                 :     }
   67139         1811381 :     u.an.pDest->enc = encoding;
   67140                 :   }else{
   67141            2269 :     if( pOp->p4type==P4_MEM ){
   67142             302 :       sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
   67143                 :     }else{
   67144            1967 :       MemSetTypeFlag(u.an.pDest, MEM_Null);
   67145                 :     }
   67146                 :   }
   67147                 : 
   67148                 :   /* If we dynamically allocated space to hold the data (in the
   67149                 :   ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
   67150                 :   ** dynamically allocated space over to the u.an.pDest structure.
   67151                 :   ** This prevents a memory copy.
   67152                 :   */
   67153         1813649 :   if( u.an.sMem.zMalloc ){
   67154              17 :     assert( u.an.sMem.z==u.an.sMem.zMalloc );
   67155              17 :     assert( !(u.an.pDest->flags & MEM_Dyn) );
   67156              17 :     assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
   67157              17 :     u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
   67158              17 :     u.an.pDest->flags |= MEM_Term;
   67159              17 :     u.an.pDest->z = u.an.sMem.z;
   67160              17 :     u.an.pDest->zMalloc = u.an.sMem.zMalloc;
   67161                 :   }
   67162                 : 
   67163         1813649 :   rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
   67164                 : 
   67165                 : op_column_out:
   67166                 :   UPDATE_MAX_BLOBSIZE(u.an.pDest);
   67167         1835299 :   REGISTER_TRACE(pOp->p3, u.an.pDest);
   67168         1835299 :   break;
   67169                 : }
   67170                 : 
   67171                 : /* Opcode: Affinity P1 P2 * P4 *
   67172                 : **
   67173                 : ** Apply affinities to a range of P2 registers starting with P1.
   67174                 : **
   67175                 : ** P4 is a string that is P2 characters long. The nth character of the
   67176                 : ** string indicates the column affinity that should be used for the nth
   67177                 : ** memory cell in the range.
   67178                 : */
   67179                 : case OP_Affinity: {
   67180                 : #if 0  /* local variables moved into u.ao */
   67181                 :   const char *zAffinity;   /* The affinity to be applied */
   67182                 :   char cAff;               /* A single character of affinity */
   67183                 : #endif /* local variables moved into u.ao */
   67184                 : 
   67185          104455 :   u.ao.zAffinity = pOp->p4.z;
   67186          104455 :   assert( u.ao.zAffinity!=0 );
   67187          104455 :   assert( u.ao.zAffinity[pOp->p2]==0 );
   67188          104455 :   pIn1 = &aMem[pOp->p1];
   67189          320080 :   while( (u.ao.cAff = *(u.ao.zAffinity++))!=0 ){
   67190          111170 :     assert( pIn1 <= &p->aMem[p->nMem] );
   67191          111170 :     assert( memIsValid(pIn1) );
   67192          111170 :     ExpandBlob(pIn1);
   67193          111170 :     applyAffinity(pIn1, u.ao.cAff, encoding);
   67194          111170 :     pIn1++;
   67195                 :   }
   67196          104455 :   break;
   67197                 : }
   67198                 : 
   67199                 : /* Opcode: MakeRecord P1 P2 P3 P4 *
   67200                 : **
   67201                 : ** Convert P2 registers beginning with P1 into the [record format]
   67202                 : ** use as a data record in a database table or as a key
   67203                 : ** in an index.  The OP_Column opcode can decode the record later.
   67204                 : **
   67205                 : ** P4 may be a string that is P2 characters long.  The nth character of the
   67206                 : ** string indicates the column affinity that should be used for the nth
   67207                 : ** field of the index key.
   67208                 : **
   67209                 : ** The mapping from character to affinity is given by the SQLITE_AFF_
   67210                 : ** macros defined in sqliteInt.h.
   67211                 : **
   67212                 : ** If P4 is NULL then all index fields have the affinity NONE.
   67213                 : */
   67214                 : case OP_MakeRecord: {
   67215                 : #if 0  /* local variables moved into u.ap */
   67216                 :   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
   67217                 :   Mem *pRec;             /* The new record */
   67218                 :   u64 nData;             /* Number of bytes of data space */
   67219                 :   int nHdr;              /* Number of bytes of header space */
   67220                 :   i64 nByte;             /* Data space required for this record */
   67221                 :   int nZero;             /* Number of zero bytes at the end of the record */
   67222                 :   int nVarint;           /* Number of bytes in a varint */
   67223                 :   u32 serial_type;       /* Type field */
   67224                 :   Mem *pData0;           /* First field to be combined into the record */
   67225                 :   Mem *pLast;            /* Last field of the record */
   67226                 :   int nField;            /* Number of fields in the record */
   67227                 :   char *zAffinity;       /* The affinity string for the record */
   67228                 :   int file_format;       /* File format to use for encoding */
   67229                 :   int i;                 /* Space used in zNewRecord[] */
   67230                 :   int len;               /* Length of a field */
   67231                 : #endif /* local variables moved into u.ap */
   67232                 : 
   67233                 :   /* Assuming the record contains N fields, the record format looks
   67234                 :   ** like this:
   67235                 :   **
   67236                 :   ** ------------------------------------------------------------------------
   67237                 :   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
   67238                 :   ** ------------------------------------------------------------------------
   67239                 :   **
   67240                 :   ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
   67241                 :   ** and so froth.
   67242                 :   **
   67243                 :   ** Each type field is a varint representing the serial type of the
   67244                 :   ** corresponding data element (see sqlite3VdbeSerialType()). The
   67245                 :   ** hdr-size field is also a varint which is the offset from the beginning
   67246                 :   ** of the record to data0.
   67247                 :   */
   67248          228471 :   u.ap.nData = 0;         /* Number of bytes of data space */
   67249          228471 :   u.ap.nHdr = 0;          /* Number of bytes of header space */
   67250          228471 :   u.ap.nZero = 0;         /* Number of zero bytes at the end of the record */
   67251          228471 :   u.ap.nField = pOp->p1;
   67252          228471 :   u.ap.zAffinity = pOp->p4.z;
   67253          228471 :   assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
   67254          228471 :   u.ap.pData0 = &aMem[u.ap.nField];
   67255          228471 :   u.ap.nField = pOp->p2;
   67256          228471 :   u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
   67257          228471 :   u.ap.file_format = p->minWriteFileFormat;
   67258                 : 
   67259                 :   /* Identify the output register */
   67260          228471 :   assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
   67261          228471 :   pOut = &aMem[pOp->p3];
   67262          228471 :   memAboutToChange(p, pOut);
   67263                 : 
   67264                 :   /* Loop through the elements that will make up the record to figure
   67265                 :   ** out how much space is required for the new record.
   67266                 :   */
   67267         1373927 :   for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
   67268         1145456 :     assert( memIsValid(u.ap.pRec) );
   67269         1145456 :     if( u.ap.zAffinity ){
   67270         1107620 :       applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
   67271                 :     }
   67272         1145456 :     if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
   67273               0 :       sqlite3VdbeMemExpandBlob(u.ap.pRec);
   67274                 :     }
   67275         1145456 :     u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
   67276         1145455 :     u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
   67277         1145455 :     u.ap.nData += u.ap.len;
   67278         1145455 :     u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
   67279         1145455 :     if( u.ap.pRec->flags & MEM_Zero ){
   67280                 :       /* Only pure zero-filled BLOBs can be input to this Opcode.
   67281                 :       ** We do not allow blobs with a prefix and a zero-filled tail. */
   67282               0 :       u.ap.nZero += u.ap.pRec->u.nZero;
   67283         1145455 :     }else if( u.ap.len ){
   67284          732631 :       u.ap.nZero = 0;
   67285                 :     }
   67286                 :   }
   67287                 : 
   67288                 :   /* Add the initial header varint and total the size */
   67289          228471 :   u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
   67290          228471 :   if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
   67291               0 :     u.ap.nHdr++;
   67292                 :   }
   67293          228471 :   u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
   67294          228471 :   if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   67295               0 :     goto too_big;
   67296                 :   }
   67297                 : 
   67298                 :   /* Make sure the output register has a buffer large enough to store
   67299                 :   ** the new record. The output register (pOp->p3) is not allowed to
   67300                 :   ** be one of the input registers (because the following call to
   67301                 :   ** sqlite3VdbeMemGrow() could clobber the value before it is used).
   67302                 :   */
   67303          228471 :   if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
   67304               0 :     goto no_mem;
   67305                 :   }
   67306          228472 :   u.ap.zNewRecord = (u8 *)pOut->z;
   67307                 : 
   67308                 :   /* Write the record */
   67309          228472 :   u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
   67310         1373926 :   for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
   67311         1145455 :     u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
   67312         1145454 :     u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type);      /* serial type */
   67313                 :   }
   67314         1373927 :   for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){  /* serial data */
   67315         1145456 :     u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
   67316                 :   }
   67317          228471 :   assert( u.ap.i==u.ap.nByte );
   67318                 : 
   67319          228471 :   assert( pOp->p3>0 && pOp->p3<=p->nMem );
   67320          228471 :   pOut->n = (int)u.ap.nByte;
   67321          228471 :   pOut->flags = MEM_Blob | MEM_Dyn;
   67322          228471 :   pOut->xDel = 0;
   67323          228471 :   if( u.ap.nZero ){
   67324               0 :     pOut->u.nZero = u.ap.nZero;
   67325               0 :     pOut->flags |= MEM_Zero;
   67326                 :   }
   67327          228471 :   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
   67328          228471 :   REGISTER_TRACE(pOp->p3, pOut);
   67329                 :   UPDATE_MAX_BLOBSIZE(pOut);
   67330          228471 :   break;
   67331                 : }
   67332                 : 
   67333                 : /* Opcode: Count P1 P2 * * *
   67334                 : **
   67335                 : ** Store the number of entries (an integer value) in the table or index 
   67336                 : ** opened by cursor P1 in register P2
   67337                 : */
   67338                 : #ifndef SQLITE_OMIT_BTREECOUNT
   67339                 : case OP_Count: {         /* out2-prerelease */
   67340                 : #if 0  /* local variables moved into u.aq */
   67341                 :   i64 nEntry;
   67342                 :   BtCursor *pCrsr;
   67343                 : #endif /* local variables moved into u.aq */
   67344                 : 
   67345             631 :   u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
   67346             631 :   if( ALWAYS(u.aq.pCrsr) ){
   67347             631 :     rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
   67348                 :   }else{
   67349               0 :     u.aq.nEntry = 0;
   67350                 :   }
   67351             631 :   pOut->u.i = u.aq.nEntry;
   67352             631 :   break;
   67353                 : }
   67354                 : #endif
   67355                 : 
   67356                 : /* Opcode: Savepoint P1 * * P4 *
   67357                 : **
   67358                 : ** Open, release or rollback the savepoint named by parameter P4, depending
   67359                 : ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
   67360                 : ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
   67361                 : */
   67362                 : case OP_Savepoint: {
   67363                 : #if 0  /* local variables moved into u.ar */
   67364                 :   int p1;                         /* Value of P1 operand */
   67365                 :   char *zName;                    /* Name of savepoint */
   67366                 :   int nName;
   67367                 :   Savepoint *pNew;
   67368                 :   Savepoint *pSavepoint;
   67369                 :   Savepoint *pTmp;
   67370                 :   int iSavepoint;
   67371                 :   int ii;
   67372                 : #endif /* local variables moved into u.ar */
   67373                 : 
   67374           14518 :   u.ar.p1 = pOp->p1;
   67375           14518 :   u.ar.zName = pOp->p4.z;
   67376                 : 
   67377                 :   /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
   67378                 :   ** transaction, then there cannot be any savepoints.
   67379                 :   */
   67380           14518 :   assert( db->pSavepoint==0 || db->autoCommit==0 );
   67381           14518 :   assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
   67382           14518 :   assert( db->pSavepoint || db->isTransactionSavepoint==0 );
   67383           14518 :   assert( checkSavepointCount(db) );
   67384                 : 
   67385           14518 :   if( u.ar.p1==SAVEPOINT_BEGIN ){
   67386            7259 :     if( db->writeVdbeCnt>0 ){
   67387                 :       /* A new savepoint cannot be created if there are active write
   67388                 :       ** statements (i.e. open read/write incremental blob handles).
   67389                 :       */
   67390               0 :       sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
   67391                 :         "SQL statements in progress");
   67392               0 :       rc = SQLITE_BUSY;
   67393                 :     }else{
   67394            7259 :       u.ar.nName = sqlite3Strlen30(u.ar.zName);
   67395                 : 
   67396                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   67397                 :       /* This call is Ok even if this savepoint is actually a transaction
   67398                 :       ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
   67399                 :       ** If this is a transaction savepoint being opened, it is guaranteed
   67400                 :       ** that the db->aVTrans[] array is empty.  */
   67401            7259 :       assert( db->autoCommit==0 || db->nVTrans==0 );
   67402            7259 :       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
   67403            7259 :                                 db->nStatement+db->nSavepoint);
   67404            7259 :       if( rc!=SQLITE_OK ) goto abort_due_to_error;
   67405                 : #endif
   67406                 : 
   67407                 :       /* Create a new savepoint structure. */
   67408            7259 :       u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
   67409            7259 :       if( u.ar.pNew ){
   67410            7259 :         u.ar.pNew->zName = (char *)&u.ar.pNew[1];
   67411            7259 :         memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
   67412                 : 
   67413                 :         /* If there is no open transaction, then mark this as a special
   67414                 :         ** "transaction savepoint". */
   67415            7259 :         if( db->autoCommit ){
   67416            1127 :           db->autoCommit = 0;
   67417            1127 :           db->isTransactionSavepoint = 1;
   67418                 :         }else{
   67419            6132 :           db->nSavepoint++;
   67420                 :         }
   67421                 : 
   67422                 :         /* Link the new savepoint into the database handle's list. */
   67423            7259 :         u.ar.pNew->pNext = db->pSavepoint;
   67424            7259 :         db->pSavepoint = u.ar.pNew;
   67425            7259 :         u.ar.pNew->nDeferredCons = db->nDeferredCons;
   67426                 :       }
   67427                 :     }
   67428                 :   }else{
   67429            7259 :     u.ar.iSavepoint = 0;
   67430                 : 
   67431                 :     /* Find the named savepoint. If there is no such savepoint, then an
   67432                 :     ** an error is returned to the user.  */
   67433           14518 :     for(
   67434            7259 :       u.ar.pSavepoint = db->pSavepoint;
   67435           14518 :       u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
   67436               0 :       u.ar.pSavepoint = u.ar.pSavepoint->pNext
   67437                 :     ){
   67438               0 :       u.ar.iSavepoint++;
   67439                 :     }
   67440            7259 :     if( !u.ar.pSavepoint ){
   67441               0 :       sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
   67442               0 :       rc = SQLITE_ERROR;
   67443            7259 :     }else if(
   67444           14518 :         db->writeVdbeCnt>0 || (u.ar.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
   67445                 :     ){
   67446                 :       /* It is not possible to release (commit) a savepoint if there are
   67447                 :       ** active write statements. It is not possible to rollback a savepoint
   67448                 :       ** if there are any active statements at all.
   67449                 :       */
   67450               0 :       sqlite3SetString(&p->zErrMsg, db,
   67451                 :         "cannot %s savepoint - SQL statements in progress",
   67452               0 :         (u.ar.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
   67453                 :       );
   67454               0 :       rc = SQLITE_BUSY;
   67455                 :     }else{
   67456                 : 
   67457                 :       /* Determine whether or not this is a transaction savepoint. If so,
   67458                 :       ** and this is a RELEASE command, then the current transaction
   67459                 :       ** is committed.
   67460                 :       */
   67461            7259 :       int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
   67462            7259 :       if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
   67463            1127 :         if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
   67464               0 :           goto vdbe_return;
   67465                 :         }
   67466            1127 :         db->autoCommit = 1;
   67467            1127 :         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
   67468               0 :           p->pc = pc;
   67469               0 :           db->autoCommit = 0;
   67470               0 :           p->rc = rc = SQLITE_BUSY;
   67471               0 :           goto vdbe_return;
   67472                 :         }
   67473            1127 :         db->isTransactionSavepoint = 0;
   67474            1127 :         rc = p->rc;
   67475                 :       }else{
   67476            6132 :         u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
   67477           18396 :         for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
   67478           12264 :           rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
   67479           12264 :           if( rc!=SQLITE_OK ){
   67480               0 :             goto abort_due_to_error;
   67481                 :           }
   67482                 :         }
   67483            6132 :         if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
   67484               0 :           sqlite3ExpirePreparedStatements(db);
   67485               0 :           sqlite3ResetInternalSchema(db, -1);
   67486               0 :           db->flags = (db->flags | SQLITE_InternChanges);
   67487                 :         }
   67488                 :       }
   67489                 : 
   67490                 :       /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
   67491                 :       ** savepoints nested inside of the savepoint being operated on. */
   67492           14518 :       while( db->pSavepoint!=u.ar.pSavepoint ){
   67493               0 :         u.ar.pTmp = db->pSavepoint;
   67494               0 :         db->pSavepoint = u.ar.pTmp->pNext;
   67495               0 :         sqlite3DbFree(db, u.ar.pTmp);
   67496               0 :         db->nSavepoint--;
   67497                 :       }
   67498                 : 
   67499                 :       /* If it is a RELEASE, then destroy the savepoint being operated on
   67500                 :       ** too. If it is a ROLLBACK TO, then set the number of deferred
   67501                 :       ** constraint violations present in the database to the value stored
   67502                 :       ** when the savepoint was created.  */
   67503            7259 :       if( u.ar.p1==SAVEPOINT_RELEASE ){
   67504            7240 :         assert( u.ar.pSavepoint==db->pSavepoint );
   67505            7240 :         db->pSavepoint = u.ar.pSavepoint->pNext;
   67506            7240 :         sqlite3DbFree(db, u.ar.pSavepoint);
   67507            7240 :         if( !isTransaction ){
   67508            6113 :           db->nSavepoint--;
   67509                 :         }
   67510                 :       }else{
   67511              19 :         db->nDeferredCons = u.ar.pSavepoint->nDeferredCons;
   67512                 :       }
   67513                 : 
   67514            7259 :       if( !isTransaction ){
   67515            6132 :         rc = sqlite3VtabSavepoint(db, u.ar.p1, u.ar.iSavepoint);
   67516            6132 :         if( rc!=SQLITE_OK ) goto abort_due_to_error;
   67517                 :       }
   67518                 :     }
   67519                 :   }
   67520                 : 
   67521           14518 :   break;
   67522                 : }
   67523                 : 
   67524                 : /* Opcode: AutoCommit P1 P2 * * *
   67525                 : **
   67526                 : ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
   67527                 : ** back any currently active btree transactions. If there are any active
   67528                 : ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
   67529                 : ** there are active writing VMs or active VMs that use shared cache.
   67530                 : **
   67531                 : ** This instruction causes the VM to halt.
   67532                 : */
   67533                 : case OP_AutoCommit: {
   67534                 : #if 0  /* local variables moved into u.as */
   67535                 :   int desiredAutoCommit;
   67536                 :   int iRollback;
   67537                 :   int turnOnAC;
   67538                 : #endif /* local variables moved into u.as */
   67539                 : 
   67540           16822 :   u.as.desiredAutoCommit = pOp->p1;
   67541           16822 :   u.as.iRollback = pOp->p2;
   67542           16822 :   u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
   67543           16822 :   assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
   67544           16822 :   assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
   67545           16822 :   assert( db->activeVdbeCnt>0 );  /* At least this one VM is active */
   67546                 : 
   67547           16822 :   if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
   67548                 :     /* If this instruction implements a ROLLBACK and other VMs are
   67549                 :     ** still running, and a transaction is active, return an error indicating
   67550                 :     ** that the other VMs must complete first.
   67551                 :     */
   67552               0 :     sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
   67553                 :         "SQL statements in progress");
   67554               0 :     rc = SQLITE_BUSY;
   67555           16822 :   }else if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
   67556                 :     /* If this instruction implements a COMMIT and other VMs are writing
   67557                 :     ** return an error indicating that the other VMs must complete first.
   67558                 :     */
   67559               0 :     sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
   67560                 :         "SQL statements in progress");
   67561               0 :     rc = SQLITE_BUSY;
   67562           16822 :   }else if( u.as.desiredAutoCommit!=db->autoCommit ){
   67563           16820 :     if( u.as.iRollback ){
   67564              44 :       assert( u.as.desiredAutoCommit==1 );
   67565              44 :       sqlite3RollbackAll(db);
   67566              44 :       db->autoCommit = 1;
   67567           16776 :     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
   67568               0 :       goto vdbe_return;
   67569                 :     }else{
   67570           16776 :       db->autoCommit = (u8)u.as.desiredAutoCommit;
   67571           16776 :       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
   67572               0 :         p->pc = pc;
   67573               0 :         db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
   67574               0 :         p->rc = rc = SQLITE_BUSY;
   67575               0 :         goto vdbe_return;
   67576                 :       }
   67577                 :     }
   67578           16820 :     assert( db->nStatement==0 );
   67579           16820 :     sqlite3CloseSavepoints(db);
   67580           16820 :     if( p->rc==SQLITE_OK ){
   67581           16820 :       rc = SQLITE_DONE;
   67582                 :     }else{
   67583               0 :       rc = SQLITE_ERROR;
   67584                 :     }
   67585           16820 :     goto vdbe_return;
   67586                 :   }else{
   67587               4 :     sqlite3SetString(&p->zErrMsg, db,
   67588               4 :         (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
   67589               2 :         (u.as.iRollback)?"cannot rollback - no transaction is active":
   67590                 :                    "cannot commit - no transaction is active"));
   67591                 : 
   67592               2 :     rc = SQLITE_ERROR;
   67593                 :   }
   67594               2 :   break;
   67595                 : }
   67596                 : 
   67597                 : /* Opcode: Transaction P1 P2 * * *
   67598                 : **
   67599                 : ** Begin a transaction.  The transaction ends when a Commit or Rollback
   67600                 : ** opcode is encountered.  Depending on the ON CONFLICT setting, the
   67601                 : ** transaction might also be rolled back if an error is encountered.
   67602                 : **
   67603                 : ** P1 is the index of the database file on which the transaction is
   67604                 : ** started.  Index 0 is the main database file and index 1 is the
   67605                 : ** file used for temporary tables.  Indices of 2 or more are used for
   67606                 : ** attached databases.
   67607                 : **
   67608                 : ** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
   67609                 : ** obtained on the database file when a write-transaction is started.  No
   67610                 : ** other process can start another write transaction while this transaction is
   67611                 : ** underway.  Starting a write transaction also creates a rollback journal. A
   67612                 : ** write transaction must be started before any changes can be made to the
   67613                 : ** database.  If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
   67614                 : ** on the file.
   67615                 : **
   67616                 : ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
   67617                 : ** true (this flag is set if the Vdbe may modify more than one row and may
   67618                 : ** throw an ABORT exception), a statement transaction may also be opened.
   67619                 : ** More specifically, a statement transaction is opened iff the database
   67620                 : ** connection is currently not in autocommit mode, or if there are other
   67621                 : ** active statements. A statement transaction allows the affects of this
   67622                 : ** VDBE to be rolled back after an error without having to roll back the
   67623                 : ** entire transaction. If no error is encountered, the statement transaction
   67624                 : ** will automatically commit when the VDBE halts.
   67625                 : **
   67626                 : ** If P2 is zero, then a read-lock is obtained on the database file.
   67627                 : */
   67628                 : case OP_Transaction: {
   67629                 : #if 0  /* local variables moved into u.at */
   67630                 :   Btree *pBt;
   67631                 : #endif /* local variables moved into u.at */
   67632                 : 
   67633          184747 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
   67634          184747 :   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
   67635          184747 :   u.at.pBt = db->aDb[pOp->p1].pBt;
   67636                 : 
   67637          184747 :   if( u.at.pBt ){
   67638          184301 :     rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
   67639          184301 :     if( rc==SQLITE_BUSY ){
   67640               0 :       p->pc = pc;
   67641               0 :       p->rc = rc = SQLITE_BUSY;
   67642               0 :       goto vdbe_return;
   67643                 :     }
   67644          184301 :     if( rc!=SQLITE_OK ){
   67645               3 :       goto abort_due_to_error;
   67646                 :     }
   67647                 : 
   67648          184298 :     if( pOp->p2 && p->usesStmtJournal
   67649           21205 :      && (db->autoCommit==0 || db->activeVdbeCnt>1)
   67650                 :     ){
   67651           18795 :       assert( sqlite3BtreeIsInTrans(u.at.pBt) );
   67652           18795 :       if( p->iStatement==0 ){
   67653           18795 :         assert( db->nStatement>=0 && db->nSavepoint>=0 );
   67654           18795 :         db->nStatement++;
   67655           18795 :         p->iStatement = db->nSavepoint + db->nStatement;
   67656                 :       }
   67657                 : 
   67658           18795 :       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
   67659           18795 :       if( rc==SQLITE_OK ){
   67660           18795 :         rc = sqlite3BtreeBeginStmt(u.at.pBt, p->iStatement);
   67661                 :       }
   67662                 : 
   67663                 :       /* Store the current value of the database handles deferred constraint
   67664                 :       ** counter. If the statement transaction needs to be rolled back,
   67665                 :       ** the value of this counter needs to be restored too.  */
   67666           18795 :       p->nStmtDefCons = db->nDeferredCons;
   67667                 :     }
   67668                 :   }
   67669          184744 :   break;
   67670                 : }
   67671                 : 
   67672                 : /* Opcode: ReadCookie P1 P2 P3 * *
   67673                 : **
   67674                 : ** Read cookie number P3 from database P1 and write it into register P2.
   67675                 : ** P3==1 is the schema version.  P3==2 is the database format.
   67676                 : ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
   67677                 : ** the main database file and P1==1 is the database file used to store
   67678                 : ** temporary tables.
   67679                 : **
   67680                 : ** There must be a read-lock on the database (either a transaction
   67681                 : ** must be started or there must be an open cursor) before
   67682                 : ** executing this instruction.
   67683                 : */
   67684                 : case OP_ReadCookie: {               /* out2-prerelease */
   67685                 : #if 0  /* local variables moved into u.au */
   67686                 :   int iMeta;
   67687                 :   int iDb;
   67688                 :   int iCookie;
   67689                 : #endif /* local variables moved into u.au */
   67690                 : 
   67691            8833 :   u.au.iDb = pOp->p1;
   67692            8833 :   u.au.iCookie = pOp->p3;
   67693            8833 :   assert( pOp->p3<SQLITE_N_BTREE_META );
   67694            8833 :   assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
   67695            8833 :   assert( db->aDb[u.au.iDb].pBt!=0 );
   67696            8833 :   assert( (p->btreeMask & (((yDbMask)1)<<u.au.iDb))!=0 );
   67697                 : 
   67698            8833 :   sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
   67699            8833 :   pOut->u.i = u.au.iMeta;
   67700            8833 :   break;
   67701                 : }
   67702                 : 
   67703                 : /* Opcode: SetCookie P1 P2 P3 * *
   67704                 : **
   67705                 : ** Write the content of register P3 (interpreted as an integer)
   67706                 : ** into cookie number P2 of database P1.  P2==1 is the schema version.  
   67707                 : ** P2==2 is the database format. P2==3 is the recommended pager cache 
   67708                 : ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
   67709                 : ** database file used to store temporary tables.
   67710                 : **
   67711                 : ** A transaction must be started before executing this opcode.
   67712                 : */
   67713                 : case OP_SetCookie: {       /* in3 */
   67714                 : #if 0  /* local variables moved into u.av */
   67715                 :   Db *pDb;
   67716                 : #endif /* local variables moved into u.av */
   67717           19979 :   assert( pOp->p2<SQLITE_N_BTREE_META );
   67718           19979 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
   67719           19979 :   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
   67720           19979 :   u.av.pDb = &db->aDb[pOp->p1];
   67721           19979 :   assert( u.av.pDb->pBt!=0 );
   67722           19979 :   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
   67723           19979 :   pIn3 = &aMem[pOp->p3];
   67724           19979 :   sqlite3VdbeMemIntegerify(pIn3);
   67725                 :   /* See note about index shifting on OP_ReadCookie */
   67726           19979 :   rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
   67727           19979 :   if( pOp->p2==BTREE_SCHEMA_VERSION ){
   67728                 :     /* When the schema cookie changes, record the new cookie internally */
   67729           15302 :     u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
   67730           15302 :     db->flags |= SQLITE_InternChanges;
   67731            4677 :   }else if( pOp->p2==BTREE_FILE_FORMAT ){
   67732                 :     /* Record changes in the file format */
   67733            1687 :     u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
   67734                 :   }
   67735           19979 :   if( pOp->p1==1 ){
   67736                 :     /* Invalidate all prepared statements whenever the TEMP database
   67737                 :     ** schema is changed.  Ticket #1644 */
   67738            2901 :     sqlite3ExpirePreparedStatements(db);
   67739            2901 :     p->expired = 0;
   67740                 :   }
   67741           19979 :   break;
   67742                 : }
   67743                 : 
   67744                 : /* Opcode: VerifyCookie P1 P2 P3 * *
   67745                 : **
   67746                 : ** Check the value of global database parameter number 0 (the
   67747                 : ** schema version) and make sure it is equal to P2 and that the
   67748                 : ** generation counter on the local schema parse equals P3.
   67749                 : **
   67750                 : ** P1 is the database number which is 0 for the main database file
   67751                 : ** and 1 for the file holding temporary tables and some higher number
   67752                 : ** for auxiliary databases.
   67753                 : **
   67754                 : ** The cookie changes its value whenever the database schema changes.
   67755                 : ** This operation is used to detect when that the cookie has changed
   67756                 : ** and that the current process needs to reread the schema.
   67757                 : **
   67758                 : ** Either a transaction needs to have been started or an OP_Open needs
   67759                 : ** to be executed (to establish a read lock) before this opcode is
   67760                 : ** invoked.
   67761                 : */
   67762                 : case OP_VerifyCookie: {
   67763                 : #if 0  /* local variables moved into u.aw */
   67764                 :   int iMeta;
   67765                 :   int iGen;
   67766                 :   Btree *pBt;
   67767                 : #endif /* local variables moved into u.aw */
   67768                 : 
   67769          155149 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
   67770          155149 :   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
   67771          155149 :   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
   67772          155149 :   u.aw.pBt = db->aDb[pOp->p1].pBt;
   67773          155149 :   if( u.aw.pBt ){
   67774          155149 :     sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
   67775          155149 :     u.aw.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
   67776                 :   }else{
   67777               0 :     u.aw.iGen = u.aw.iMeta = 0;
   67778                 :   }
   67779          155149 :   if( u.aw.iMeta!=pOp->p2 || u.aw.iGen!=pOp->p3 ){
   67780             287 :     sqlite3DbFree(db, p->zErrMsg);
   67781             287 :     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
   67782                 :     /* If the schema-cookie from the database file matches the cookie
   67783                 :     ** stored with the in-memory representation of the schema, do
   67784                 :     ** not reload the schema from the database file.
   67785                 :     **
   67786                 :     ** If virtual-tables are in use, this is not just an optimization.
   67787                 :     ** Often, v-tables store their data in other SQLite tables, which
   67788                 :     ** are queried from within xNext() and other v-table methods using
   67789                 :     ** prepared queries. If such a query is out-of-date, we do not want to
   67790                 :     ** discard the database schema, as the user code implementing the
   67791                 :     ** v-table would have to be ready for the sqlite3_vtab structure itself
   67792                 :     ** to be invalidated whenever sqlite3_step() is called from within
   67793                 :     ** a v-table method.
   67794                 :     */
   67795             287 :     if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
   67796               2 :       sqlite3ResetInternalSchema(db, pOp->p1);
   67797                 :     }
   67798                 : 
   67799             287 :     p->expired = 1;
   67800             287 :     rc = SQLITE_SCHEMA;
   67801                 :   }
   67802          155149 :   break;
   67803                 : }
   67804                 : 
   67805                 : /* Opcode: OpenRead P1 P2 P3 P4 P5
   67806                 : **
   67807                 : ** Open a read-only cursor for the database table whose root page is
   67808                 : ** P2 in a database file.  The database file is determined by P3. 
   67809                 : ** P3==0 means the main database, P3==1 means the database used for 
   67810                 : ** temporary tables, and P3>1 means used the corresponding attached
   67811                 : ** database.  Give the new cursor an identifier of P1.  The P1
   67812                 : ** values need not be contiguous but all P1 values should be small integers.
   67813                 : ** It is an error for P1 to be negative.
   67814                 : **
   67815                 : ** If P5!=0 then use the content of register P2 as the root page, not
   67816                 : ** the value of P2 itself.
   67817                 : **
   67818                 : ** There will be a read lock on the database whenever there is an
   67819                 : ** open cursor.  If the database was unlocked prior to this instruction
   67820                 : ** then a read lock is acquired as part of this instruction.  A read
   67821                 : ** lock allows other processes to read the database but prohibits
   67822                 : ** any other process from modifying the database.  The read lock is
   67823                 : ** released when all cursors are closed.  If this instruction attempts
   67824                 : ** to get a read lock but fails, the script terminates with an
   67825                 : ** SQLITE_BUSY error code.
   67826                 : **
   67827                 : ** The P4 value may be either an integer (P4_INT32) or a pointer to
   67828                 : ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
   67829                 : ** structure, then said structure defines the content and collating 
   67830                 : ** sequence of the index being opened. Otherwise, if P4 is an integer 
   67831                 : ** value, it is set to the number of columns in the table.
   67832                 : **
   67833                 : ** See also OpenWrite.
   67834                 : */
   67835                 : /* Opcode: OpenWrite P1 P2 P3 P4 P5
   67836                 : **
   67837                 : ** Open a read/write cursor named P1 on the table or index whose root
   67838                 : ** page is P2.  Or if P5!=0 use the content of register P2 to find the
   67839                 : ** root page.
   67840                 : **
   67841                 : ** The P4 value may be either an integer (P4_INT32) or a pointer to
   67842                 : ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo 
   67843                 : ** structure, then said structure defines the content and collating 
   67844                 : ** sequence of the index being opened. Otherwise, if P4 is an integer 
   67845                 : ** value, it is set to the number of columns in the table, or to the
   67846                 : ** largest index of any column of the table that is actually used.
   67847                 : **
   67848                 : ** This instruction works just like OpenRead except that it opens the cursor
   67849                 : ** in read/write mode.  For a given table, there can be one or more read-only
   67850                 : ** cursors or a single read/write cursor but not both.
   67851                 : **
   67852                 : ** See also OpenRead.
   67853                 : */
   67854                 : case OP_OpenRead:
   67855                 : case OP_OpenWrite: {
   67856                 : #if 0  /* local variables moved into u.ax */
   67857                 :   int nField;
   67858                 :   KeyInfo *pKeyInfo;
   67859                 :   int p2;
   67860                 :   int iDb;
   67861                 :   int wrFlag;
   67862                 :   Btree *pX;
   67863                 :   VdbeCursor *pCur;
   67864                 :   Db *pDb;
   67865                 : #endif /* local variables moved into u.ax */
   67866                 : 
   67867          516802 :   if( p->expired ){
   67868               0 :     rc = SQLITE_ABORT;
   67869               0 :     break;
   67870                 :   }
   67871                 : 
   67872          516802 :   u.ax.nField = 0;
   67873          516802 :   u.ax.pKeyInfo = 0;
   67874          516802 :   u.ax.p2 = pOp->p2;
   67875          516802 :   u.ax.iDb = pOp->p3;
   67876          516802 :   assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
   67877          516802 :   assert( (p->btreeMask & (((yDbMask)1)<<u.ax.iDb))!=0 );
   67878          516802 :   u.ax.pDb = &db->aDb[u.ax.iDb];
   67879          516802 :   u.ax.pX = u.ax.pDb->pBt;
   67880          516802 :   assert( u.ax.pX!=0 );
   67881          516802 :   if( pOp->opcode==OP_OpenWrite ){
   67882          246840 :     u.ax.wrFlag = 1;
   67883          246840 :     assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
   67884          246840 :     if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
   67885           84500 :       p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
   67886                 :     }
   67887                 :   }else{
   67888          269962 :     u.ax.wrFlag = 0;
   67889                 :   }
   67890          516802 :   if( pOp->p5 ){
   67891            5832 :     assert( u.ax.p2>0 );
   67892            5832 :     assert( u.ax.p2<=p->nMem );
   67893            5832 :     pIn2 = &aMem[u.ax.p2];
   67894            5832 :     assert( memIsValid(pIn2) );
   67895            5832 :     assert( (pIn2->flags & MEM_Int)!=0 );
   67896            5832 :     sqlite3VdbeMemIntegerify(pIn2);
   67897            5832 :     u.ax.p2 = (int)pIn2->u.i;
   67898                 :     /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
   67899                 :     ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
   67900                 :     ** If there were a failure, the prepared statement would have halted
   67901                 :     ** before reaching this instruction. */
   67902            5832 :     if( NEVER(u.ax.p2<2) ) {
   67903               0 :       rc = SQLITE_CORRUPT_BKPT;
   67904               0 :       goto abort_due_to_error;
   67905                 :     }
   67906                 :   }
   67907          516802 :   if( pOp->p4type==P4_KEYINFO ){
   67908          243915 :     u.ax.pKeyInfo = pOp->p4.pKeyInfo;
   67909          243915 :     u.ax.pKeyInfo->enc = ENC(p->db);
   67910          243915 :     u.ax.nField = u.ax.pKeyInfo->nField+1;
   67911          272887 :   }else if( pOp->p4type==P4_INT32 ){
   67912          272835 :     u.ax.nField = pOp->p4.i;
   67913                 :   }
   67914          516802 :   assert( pOp->p1>=0 );
   67915          516802 :   u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
   67916          516802 :   if( u.ax.pCur==0 ) goto no_mem;
   67917          516802 :   u.ax.pCur->nullRow = 1;
   67918          516802 :   u.ax.pCur->isOrdered = 1;
   67919          516802 :   rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
   67920          516802 :   u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
   67921                 : 
   67922                 :   /* Since it performs no memory allocation or IO, the only value that
   67923                 :   ** sqlite3BtreeCursor() may return is SQLITE_OK. */
   67924          516802 :   assert( rc==SQLITE_OK );
   67925                 : 
   67926                 :   /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
   67927                 :   ** SQLite used to check if the root-page flags were sane at this point
   67928                 :   ** and report database corruption if they were not, but this check has
   67929                 :   ** since moved into the btree layer.  */
   67930          516802 :   u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
   67931          516802 :   u.ax.pCur->isIndex = !u.ax.pCur->isTable;
   67932          516802 :   break;
   67933                 : }
   67934                 : 
   67935                 : /* Opcode: OpenEphemeral P1 P2 * P4 P5
   67936                 : **
   67937                 : ** Open a new cursor P1 to a transient table.
   67938                 : ** The cursor is always opened read/write even if 
   67939                 : ** the main database is read-only.  The ephemeral
   67940                 : ** table is deleted automatically when the cursor is closed.
   67941                 : **
   67942                 : ** P2 is the number of columns in the ephemeral table.
   67943                 : ** The cursor points to a BTree table if P4==0 and to a BTree index
   67944                 : ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
   67945                 : ** that defines the format of keys in the index.
   67946                 : **
   67947                 : ** This opcode was once called OpenTemp.  But that created
   67948                 : ** confusion because the term "temp table", might refer either
   67949                 : ** to a TEMP table at the SQL level, or to a table opened by
   67950                 : ** this opcode.  Then this opcode was call OpenVirtual.  But
   67951                 : ** that created confusion with the whole virtual-table idea.
   67952                 : **
   67953                 : ** The P5 parameter can be a mask of the BTREE_* flags defined
   67954                 : ** in btree.h.  These flags control aspects of the operation of
   67955                 : ** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
   67956                 : ** added automatically.
   67957                 : */
   67958                 : /* Opcode: OpenAutoindex P1 P2 * P4 *
   67959                 : **
   67960                 : ** This opcode works the same as OP_OpenEphemeral.  It has a
   67961                 : ** different name to distinguish its use.  Tables created using
   67962                 : ** by this opcode will be used for automatically created transient
   67963                 : ** indices in joins.
   67964                 : */
   67965                 : case OP_OpenAutoindex: 
   67966                 : case OP_OpenEphemeral: {
   67967                 : #if 0  /* local variables moved into u.ay */
   67968                 :   VdbeCursor *pCx;
   67969                 : #endif /* local variables moved into u.ay */
   67970                 :   static const int vfsFlags =
   67971                 :       SQLITE_OPEN_READWRITE |
   67972                 :       SQLITE_OPEN_CREATE |
   67973                 :       SQLITE_OPEN_EXCLUSIVE |
   67974                 :       SQLITE_OPEN_DELETEONCLOSE |
   67975                 :       SQLITE_OPEN_TRANSIENT_DB;
   67976                 : 
   67977           16027 :   assert( pOp->p1>=0 );
   67978           16027 :   u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
   67979           16027 :   if( u.ay.pCx==0 ) goto no_mem;
   67980           16027 :   u.ay.pCx->nullRow = 1;
   67981           32054 :   rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ay.pCx->pBt,
   67982           16027 :                         BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
   67983           16027 :   if( rc==SQLITE_OK ){
   67984           16027 :     rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
   67985                 :   }
   67986           16027 :   if( rc==SQLITE_OK ){
   67987                 :     /* If a transient index is required, create it by calling
   67988                 :     ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
   67989                 :     ** opening it. If a transient table is required, just use the
   67990                 :     ** automatically created table with root-page 1 (an BLOB_INTKEY table).
   67991                 :     */
   67992           16027 :     if( pOp->p4.pKeyInfo ){
   67993                 :       int pgno;
   67994           14971 :       assert( pOp->p4type==P4_KEYINFO );
   67995           14971 :       rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
   67996           14971 :       if( rc==SQLITE_OK ){
   67997           14971 :         assert( pgno==MASTER_ROOT+1 );
   67998           29942 :         rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
   67999           29942 :                                 (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
   68000           14971 :         u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
   68001           14971 :         u.ay.pCx->pKeyInfo->enc = ENC(p->db);
   68002                 :       }
   68003           14971 :       u.ay.pCx->isTable = 0;
   68004                 :     }else{
   68005            1056 :       rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
   68006            1056 :       u.ay.pCx->isTable = 1;
   68007                 :     }
   68008                 :   }
   68009           16027 :   u.ay.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
   68010           16027 :   u.ay.pCx->isIndex = !u.ay.pCx->isTable;
   68011           16027 :   break;
   68012                 : }
   68013                 : 
   68014                 : /* Opcode: OpenSorter P1 P2 * P4 *
   68015                 : **
   68016                 : ** This opcode works like OP_OpenEphemeral except that it opens
   68017                 : ** a transient index that is specifically designed to sort large
   68018                 : ** tables using an external merge-sort algorithm.
   68019                 : */
   68020                 : case OP_SorterOpen: {
   68021                 : #if 0  /* local variables moved into u.az */
   68022                 :   VdbeCursor *pCx;
   68023                 : #endif /* local variables moved into u.az */
   68024                 : #ifndef SQLITE_OMIT_MERGE_SORT
   68025            6886 :   u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
   68026            6886 :   if( u.az.pCx==0 ) goto no_mem;
   68027            6886 :   u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
   68028            6886 :   u.az.pCx->pKeyInfo->enc = ENC(p->db);
   68029            6886 :   u.az.pCx->isSorter = 1;
   68030            6886 :   rc = sqlite3VdbeSorterInit(db, u.az.pCx);
   68031                 : #else
   68032                 :   pOp->opcode = OP_OpenEphemeral;
   68033                 :   pc--;
   68034                 : #endif
   68035            6886 :   break;
   68036                 : }
   68037                 : 
   68038                 : /* Opcode: OpenPseudo P1 P2 P3 * *
   68039                 : **
   68040                 : ** Open a new cursor that points to a fake table that contains a single
   68041                 : ** row of data.  The content of that one row in the content of memory
   68042                 : ** register P2.  In other words, cursor P1 becomes an alias for the 
   68043                 : ** MEM_Blob content contained in register P2.
   68044                 : **
   68045                 : ** A pseudo-table created by this opcode is used to hold a single
   68046                 : ** row output from the sorter so that the row can be decomposed into
   68047                 : ** individual columns using the OP_Column opcode.  The OP_Column opcode
   68048                 : ** is the only cursor opcode that works with a pseudo-table.
   68049                 : **
   68050                 : ** P3 is the number of fields in the records that will be stored by
   68051                 : ** the pseudo-table.
   68052                 : */
   68053                 : case OP_OpenPseudo: {
   68054                 : #if 0  /* local variables moved into u.ba */
   68055                 :   VdbeCursor *pCx;
   68056                 : #endif /* local variables moved into u.ba */
   68057                 : 
   68058            2347 :   assert( pOp->p1>=0 );
   68059            2347 :   u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
   68060            2347 :   if( u.ba.pCx==0 ) goto no_mem;
   68061            2347 :   u.ba.pCx->nullRow = 1;
   68062            2347 :   u.ba.pCx->pseudoTableReg = pOp->p2;
   68063            2347 :   u.ba.pCx->isTable = 1;
   68064            2347 :   u.ba.pCx->isIndex = 0;
   68065            2347 :   break;
   68066                 : }
   68067                 : 
   68068                 : /* Opcode: Close P1 * * * *
   68069                 : **
   68070                 : ** Close a cursor previously opened as P1.  If P1 is not
   68071                 : ** currently open, this instruction is a no-op.
   68072                 : */
   68073                 : case OP_Close: {
   68074          491101 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68075          491101 :   sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
   68076          491101 :   p->apCsr[pOp->p1] = 0;
   68077          491101 :   break;
   68078                 : }
   68079                 : 
   68080                 : /* Opcode: SeekGe P1 P2 P3 P4 *
   68081                 : **
   68082                 : ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
   68083                 : ** use the value in register P3 as the key.  If cursor P1 refers 
   68084                 : ** to an SQL index, then P3 is the first in an array of P4 registers 
   68085                 : ** that are used as an unpacked index key. 
   68086                 : **
   68087                 : ** Reposition cursor P1 so that  it points to the smallest entry that 
   68088                 : ** is greater than or equal to the key value. If there are no records 
   68089                 : ** greater than or equal to the key and P2 is not zero, then jump to P2.
   68090                 : **
   68091                 : ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
   68092                 : */
   68093                 : /* Opcode: SeekGt P1 P2 P3 P4 *
   68094                 : **
   68095                 : ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
   68096                 : ** use the value in register P3 as a key. If cursor P1 refers 
   68097                 : ** to an SQL index, then P3 is the first in an array of P4 registers 
   68098                 : ** that are used as an unpacked index key. 
   68099                 : **
   68100                 : ** Reposition cursor P1 so that  it points to the smallest entry that 
   68101                 : ** is greater than the key value. If there are no records greater than 
   68102                 : ** the key and P2 is not zero, then jump to P2.
   68103                 : **
   68104                 : ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
   68105                 : */
   68106                 : /* Opcode: SeekLt P1 P2 P3 P4 * 
   68107                 : **
   68108                 : ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
   68109                 : ** use the value in register P3 as a key. If cursor P1 refers 
   68110                 : ** to an SQL index, then P3 is the first in an array of P4 registers 
   68111                 : ** that are used as an unpacked index key. 
   68112                 : **
   68113                 : ** Reposition cursor P1 so that  it points to the largest entry that 
   68114                 : ** is less than the key value. If there are no records less than 
   68115                 : ** the key and P2 is not zero, then jump to P2.
   68116                 : **
   68117                 : ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
   68118                 : */
   68119                 : /* Opcode: SeekLe P1 P2 P3 P4 *
   68120                 : **
   68121                 : ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), 
   68122                 : ** use the value in register P3 as a key. If cursor P1 refers 
   68123                 : ** to an SQL index, then P3 is the first in an array of P4 registers 
   68124                 : ** that are used as an unpacked index key. 
   68125                 : **
   68126                 : ** Reposition cursor P1 so that it points to the largest entry that 
   68127                 : ** is less than or equal to the key value. If there are no records 
   68128                 : ** less than or equal to the key and P2 is not zero, then jump to P2.
   68129                 : **
   68130                 : ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
   68131                 : */
   68132                 : case OP_SeekLt:         /* jump, in3 */
   68133                 : case OP_SeekLe:         /* jump, in3 */
   68134                 : case OP_SeekGe:         /* jump, in3 */
   68135                 : case OP_SeekGt: {       /* jump, in3 */
   68136                 : #if 0  /* local variables moved into u.bb */
   68137                 :   int res;
   68138                 :   int oc;
   68139                 :   VdbeCursor *pC;
   68140                 :   UnpackedRecord r;
   68141                 :   int nField;
   68142                 :   i64 iKey;      /* The rowid we are to seek to */
   68143                 : #endif /* local variables moved into u.bb */
   68144                 : 
   68145          111380 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68146          111380 :   assert( pOp->p2!=0 );
   68147          111380 :   u.bb.pC = p->apCsr[pOp->p1];
   68148          111380 :   assert( u.bb.pC!=0 );
   68149          111380 :   assert( u.bb.pC->pseudoTableReg==0 );
   68150                 :   assert( OP_SeekLe == OP_SeekLt+1 );
   68151                 :   assert( OP_SeekGe == OP_SeekLt+2 );
   68152                 :   assert( OP_SeekGt == OP_SeekLt+3 );
   68153          111380 :   assert( u.bb.pC->isOrdered );
   68154          111380 :   if( ALWAYS(u.bb.pC->pCursor!=0) ){
   68155          111380 :     u.bb.oc = pOp->opcode;
   68156          111380 :     u.bb.pC->nullRow = 0;
   68157          111380 :     if( u.bb.pC->isTable ){
   68158                 :       /* The input value in P3 might be of any type: integer, real, string,
   68159                 :       ** blob, or NULL.  But it needs to be an integer before we can do
   68160                 :       ** the seek, so covert it. */
   68161              24 :       pIn3 = &aMem[pOp->p3];
   68162              24 :       applyNumericAffinity(pIn3);
   68163              24 :       u.bb.iKey = sqlite3VdbeIntValue(pIn3);
   68164              24 :       u.bb.pC->rowidIsValid = 0;
   68165                 : 
   68166                 :       /* If the P3 value could not be converted into an integer without
   68167                 :       ** loss of information, then special processing is required... */
   68168              24 :       if( (pIn3->flags & MEM_Int)==0 ){
   68169               0 :         if( (pIn3->flags & MEM_Real)==0 ){
   68170                 :           /* If the P3 value cannot be converted into any kind of a number,
   68171                 :           ** then the seek is not possible, so jump to P2 */
   68172               0 :           pc = pOp->p2 - 1;
   68173               0 :           break;
   68174                 :         }
   68175                 :         /* If we reach this point, then the P3 value must be a floating
   68176                 :         ** point number. */
   68177               0 :         assert( (pIn3->flags & MEM_Real)!=0 );
   68178                 : 
   68179               0 :         if( u.bb.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bb.iKey || pIn3->r>0) ){
   68180                 :           /* The P3 value is too large in magnitude to be expressed as an
   68181                 :           ** integer. */
   68182               0 :           u.bb.res = 1;
   68183               0 :           if( pIn3->r<0 ){
   68184               0 :             if( u.bb.oc>=OP_SeekGe ){  assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
   68185               0 :               rc = sqlite3BtreeFirst(u.bb.pC->pCursor, &u.bb.res);
   68186               0 :               if( rc!=SQLITE_OK ) goto abort_due_to_error;
   68187                 :             }
   68188                 :           }else{
   68189               0 :             if( u.bb.oc<=OP_SeekLe ){  assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
   68190               0 :               rc = sqlite3BtreeLast(u.bb.pC->pCursor, &u.bb.res);
   68191               0 :               if( rc!=SQLITE_OK ) goto abort_due_to_error;
   68192                 :             }
   68193                 :           }
   68194               0 :           if( u.bb.res ){
   68195               0 :             pc = pOp->p2 - 1;
   68196                 :           }
   68197               0 :           break;
   68198               0 :         }else if( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekGe ){
   68199                 :           /* Use the ceiling() function to convert real->int */
   68200               0 :           if( pIn3->r > (double)u.bb.iKey ) u.bb.iKey++;
   68201                 :         }else{
   68202                 :           /* Use the floor() function to convert real->int */
   68203               0 :           assert( u.bb.oc==OP_SeekLe || u.bb.oc==OP_SeekGt );
   68204               0 :           if( pIn3->r < (double)u.bb.iKey ) u.bb.iKey--;
   68205                 :         }
   68206                 :       }
   68207              24 :       rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, 0, (u64)u.bb.iKey, 0, &u.bb.res);
   68208              24 :       if( rc!=SQLITE_OK ){
   68209               0 :         goto abort_due_to_error;
   68210                 :       }
   68211              24 :       if( u.bb.res==0 ){
   68212              24 :         u.bb.pC->rowidIsValid = 1;
   68213              24 :         u.bb.pC->lastRowid = u.bb.iKey;
   68214                 :       }
   68215                 :     }else{
   68216          111356 :       u.bb.nField = pOp->p4.i;
   68217          111356 :       assert( pOp->p4type==P4_INT32 );
   68218          111356 :       assert( u.bb.nField>0 );
   68219          111356 :       u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
   68220          111356 :       u.bb.r.nField = (u16)u.bb.nField;
   68221                 : 
   68222                 :       /* The next line of code computes as follows, only faster:
   68223                 :       **   if( u.bb.oc==OP_SeekGt || u.bb.oc==OP_SeekLe ){
   68224                 :       **     u.bb.r.flags = UNPACKED_INCRKEY;
   68225                 :       **   }else{
   68226                 :       **     u.bb.r.flags = 0;
   68227                 :       **   }
   68228                 :       */
   68229          111356 :       u.bb.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bb.oc - OP_SeekLt)));
   68230          111356 :       assert( u.bb.oc!=OP_SeekGt || u.bb.r.flags==UNPACKED_INCRKEY );
   68231          111356 :       assert( u.bb.oc!=OP_SeekLe || u.bb.r.flags==UNPACKED_INCRKEY );
   68232          111356 :       assert( u.bb.oc!=OP_SeekGe || u.bb.r.flags==0 );
   68233          111356 :       assert( u.bb.oc!=OP_SeekLt || u.bb.r.flags==0 );
   68234                 : 
   68235          111356 :       u.bb.r.aMem = &aMem[pOp->p3];
   68236                 : #ifdef SQLITE_DEBUG
   68237          111356 :       { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
   68238                 : #endif
   68239          111356 :       ExpandBlob(u.bb.r.aMem);
   68240          111356 :       rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, &u.bb.r, 0, 0, &u.bb.res);
   68241          111356 :       if( rc!=SQLITE_OK ){
   68242               0 :         goto abort_due_to_error;
   68243                 :       }
   68244          111356 :       u.bb.pC->rowidIsValid = 0;
   68245                 :     }
   68246          111380 :     u.bb.pC->deferredMoveto = 0;
   68247          111380 :     u.bb.pC->cacheStatus = CACHE_STALE;
   68248                 : #ifdef SQLITE_TEST
   68249                 :     sqlite3_search_count++;
   68250                 : #endif
   68251          111380 :     if( u.bb.oc>=OP_SeekGe ){  assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
   68252          100519 :       if( u.bb.res<0 || (u.bb.res==0 && u.bb.oc==OP_SeekGt) ){
   68253           49201 :         rc = sqlite3BtreeNext(u.bb.pC->pCursor, &u.bb.res);
   68254           49201 :         if( rc!=SQLITE_OK ) goto abort_due_to_error;
   68255           49201 :         u.bb.pC->rowidIsValid = 0;
   68256                 :       }else{
   68257           51318 :         u.bb.res = 0;
   68258                 :       }
   68259                 :     }else{
   68260           10861 :       assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
   68261           10861 :       if( u.bb.res>0 || (u.bb.res==0 && u.bb.oc==OP_SeekLt) ){
   68262            2313 :         rc = sqlite3BtreePrevious(u.bb.pC->pCursor, &u.bb.res);
   68263            2313 :         if( rc!=SQLITE_OK ) goto abort_due_to_error;
   68264            2313 :         u.bb.pC->rowidIsValid = 0;
   68265                 :       }else{
   68266                 :         /* u.bb.res might be negative because the table is empty.  Check to
   68267                 :         ** see if this is the case.
   68268                 :         */
   68269            8548 :         u.bb.res = sqlite3BtreeEof(u.bb.pC->pCursor);
   68270                 :       }
   68271                 :     }
   68272          111380 :     assert( pOp->p2>0 );
   68273          111380 :     if( u.bb.res ){
   68274           39772 :       pc = pOp->p2 - 1;
   68275                 :     }
   68276                 :   }else{
   68277                 :     /* This happens when attempting to open the sqlite3_master table
   68278                 :     ** for read access returns SQLITE_EMPTY. In this case always
   68279                 :     ** take the jump (since there are no records in the table).
   68280                 :     */
   68281               0 :     pc = pOp->p2 - 1;
   68282                 :   }
   68283          111380 :   break;
   68284                 : }
   68285                 : 
   68286                 : /* Opcode: Seek P1 P2 * * *
   68287                 : **
   68288                 : ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
   68289                 : ** for P1 to move so that it points to the rowid given by P2.
   68290                 : **
   68291                 : ** This is actually a deferred seek.  Nothing actually happens until
   68292                 : ** the cursor is used to read a record.  That way, if no reads
   68293                 : ** occur, no unnecessary I/O happens.
   68294                 : */
   68295                 : case OP_Seek: {    /* in2 */
   68296                 : #if 0  /* local variables moved into u.bc */
   68297                 :   VdbeCursor *pC;
   68298                 : #endif /* local variables moved into u.bc */
   68299                 : 
   68300           95650 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68301           95650 :   u.bc.pC = p->apCsr[pOp->p1];
   68302           95650 :   assert( u.bc.pC!=0 );
   68303           95650 :   if( ALWAYS(u.bc.pC->pCursor!=0) ){
   68304           95650 :     assert( u.bc.pC->isTable );
   68305           95650 :     u.bc.pC->nullRow = 0;
   68306           95650 :     pIn2 = &aMem[pOp->p2];
   68307           95650 :     u.bc.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
   68308           95650 :     u.bc.pC->rowidIsValid = 0;
   68309           95650 :     u.bc.pC->deferredMoveto = 1;
   68310                 :   }
   68311           95650 :   break;
   68312                 : }
   68313                 :   
   68314                 : 
   68315                 : /* Opcode: Found P1 P2 P3 P4 *
   68316                 : **
   68317                 : ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
   68318                 : ** P4>0 then register P3 is the first of P4 registers that form an unpacked
   68319                 : ** record.
   68320                 : **
   68321                 : ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
   68322                 : ** is a prefix of any entry in P1 then a jump is made to P2 and
   68323                 : ** P1 is left pointing at the matching entry.
   68324                 : */
   68325                 : /* Opcode: NotFound P1 P2 P3 P4 *
   68326                 : **
   68327                 : ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
   68328                 : ** P4>0 then register P3 is the first of P4 registers that form an unpacked
   68329                 : ** record.
   68330                 : ** 
   68331                 : ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
   68332                 : ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1 
   68333                 : ** does contain an entry whose prefix matches the P3/P4 record then control
   68334                 : ** falls through to the next instruction and P1 is left pointing at the
   68335                 : ** matching entry.
   68336                 : **
   68337                 : ** See also: Found, NotExists, IsUnique
   68338                 : */
   68339                 : case OP_NotFound:       /* jump, in3 */
   68340                 : case OP_Found: {        /* jump, in3 */
   68341                 : #if 0  /* local variables moved into u.bd */
   68342                 :   int alreadyExists;
   68343                 :   VdbeCursor *pC;
   68344                 :   int res;
   68345                 :   char *pFree;
   68346                 :   UnpackedRecord *pIdxKey;
   68347                 :   UnpackedRecord r;
   68348                 :   char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
   68349                 : #endif /* local variables moved into u.bd */
   68350                 : 
   68351                 : #ifdef SQLITE_TEST
   68352                 :   sqlite3_found_count++;
   68353                 : #endif
   68354                 : 
   68355           13893 :   u.bd.alreadyExists = 0;
   68356           13893 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68357           13893 :   assert( pOp->p4type==P4_INT32 );
   68358           13893 :   u.bd.pC = p->apCsr[pOp->p1];
   68359           13893 :   assert( u.bd.pC!=0 );
   68360           13893 :   pIn3 = &aMem[pOp->p3];
   68361           13893 :   if( ALWAYS(u.bd.pC->pCursor!=0) ){
   68362                 : 
   68363           13893 :     assert( u.bd.pC->isTable==0 );
   68364           13893 :     if( pOp->p4.i>0 ){
   68365           13893 :       u.bd.r.pKeyInfo = u.bd.pC->pKeyInfo;
   68366           13893 :       u.bd.r.nField = (u16)pOp->p4.i;
   68367           13893 :       u.bd.r.aMem = pIn3;
   68368                 : #ifdef SQLITE_DEBUG
   68369           13893 :       { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
   68370                 : #endif
   68371           13893 :       u.bd.r.flags = UNPACKED_PREFIX_MATCH;
   68372           13893 :       u.bd.pIdxKey = &u.bd.r;
   68373                 :     }else{
   68374               0 :       u.bd.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
   68375               0 :           u.bd.pC->pKeyInfo, u.bd.aTempRec, sizeof(u.bd.aTempRec), &u.bd.pFree
   68376                 :       );
   68377               0 :       if( u.bd.pIdxKey==0 ) goto no_mem;
   68378               0 :       assert( pIn3->flags & MEM_Blob );
   68379               0 :       assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
   68380               0 :       sqlite3VdbeRecordUnpack(u.bd.pC->pKeyInfo, pIn3->n, pIn3->z, u.bd.pIdxKey);
   68381               0 :       u.bd.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
   68382                 :     }
   68383           13893 :     rc = sqlite3BtreeMovetoUnpacked(u.bd.pC->pCursor, u.bd.pIdxKey, 0, 0, &u.bd.res);
   68384           13893 :     if( pOp->p4.i==0 ){
   68385               0 :       sqlite3DbFree(db, u.bd.pFree);
   68386                 :     }
   68387           13893 :     if( rc!=SQLITE_OK ){
   68388               0 :       break;
   68389                 :     }
   68390           13893 :     u.bd.alreadyExists = (u.bd.res==0);
   68391           13893 :     u.bd.pC->deferredMoveto = 0;
   68392           13893 :     u.bd.pC->cacheStatus = CACHE_STALE;
   68393                 :   }
   68394           13893 :   if( pOp->opcode==OP_Found ){
   68395           11076 :     if( u.bd.alreadyExists ) pc = pOp->p2 - 1;
   68396                 :   }else{
   68397            2817 :     if( !u.bd.alreadyExists ) pc = pOp->p2 - 1;
   68398                 :   }
   68399           13893 :   break;
   68400                 : }
   68401                 : 
   68402                 : /* Opcode: IsUnique P1 P2 P3 P4 *
   68403                 : **
   68404                 : ** Cursor P1 is open on an index b-tree - that is to say, a btree which
   68405                 : ** no data and where the key are records generated by OP_MakeRecord with
   68406                 : ** the list field being the integer ROWID of the entry that the index
   68407                 : ** entry refers to.
   68408                 : **
   68409                 : ** The P3 register contains an integer record number. Call this record 
   68410                 : ** number R. Register P4 is the first in a set of N contiguous registers
   68411                 : ** that make up an unpacked index key that can be used with cursor P1.
   68412                 : ** The value of N can be inferred from the cursor. N includes the rowid
   68413                 : ** value appended to the end of the index record. This rowid value may
   68414                 : ** or may not be the same as R.
   68415                 : **
   68416                 : ** If any of the N registers beginning with register P4 contains a NULL
   68417                 : ** value, jump immediately to P2.
   68418                 : **
   68419                 : ** Otherwise, this instruction checks if cursor P1 contains an entry
   68420                 : ** where the first (N-1) fields match but the rowid value at the end
   68421                 : ** of the index entry is not R. If there is no such entry, control jumps
   68422                 : ** to instruction P2. Otherwise, the rowid of the conflicting index
   68423                 : ** entry is copied to register P3 and control falls through to the next
   68424                 : ** instruction.
   68425                 : **
   68426                 : ** See also: NotFound, NotExists, Found
   68427                 : */
   68428                 : case OP_IsUnique: {        /* jump, in3 */
   68429                 : #if 0  /* local variables moved into u.be */
   68430                 :   u16 ii;
   68431                 :   VdbeCursor *pCx;
   68432                 :   BtCursor *pCrsr;
   68433                 :   u16 nField;
   68434                 :   Mem *aMx;
   68435                 :   UnpackedRecord r;                  /* B-Tree index search key */
   68436                 :   i64 R;                             /* Rowid stored in register P3 */
   68437                 : #endif /* local variables moved into u.be */
   68438                 : 
   68439           34691 :   pIn3 = &aMem[pOp->p3];
   68440           34691 :   u.be.aMx = &aMem[pOp->p4.i];
   68441                 :   /* Assert that the values of parameters P1 and P4 are in range. */
   68442           34691 :   assert( pOp->p4type==P4_INT32 );
   68443           34691 :   assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
   68444           34691 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68445                 : 
   68446                 :   /* Find the index cursor. */
   68447           34691 :   u.be.pCx = p->apCsr[pOp->p1];
   68448           34691 :   assert( u.be.pCx->deferredMoveto==0 );
   68449           34691 :   u.be.pCx->seekResult = 0;
   68450           34691 :   u.be.pCx->cacheStatus = CACHE_STALE;
   68451           34691 :   u.be.pCrsr = u.be.pCx->pCursor;
   68452                 : 
   68453                 :   /* If any of the values are NULL, take the jump. */
   68454           34691 :   u.be.nField = u.be.pCx->pKeyInfo->nField;
   68455          107803 :   for(u.be.ii=0; u.be.ii<u.be.nField; u.be.ii++){
   68456           73150 :     if( u.be.aMx[u.be.ii].flags & MEM_Null ){
   68457              38 :       pc = pOp->p2 - 1;
   68458              38 :       u.be.pCrsr = 0;
   68459              38 :       break;
   68460                 :     }
   68461                 :   }
   68462           34691 :   assert( (u.be.aMx[u.be.nField].flags & MEM_Null)==0 );
   68463                 : 
   68464           34691 :   if( u.be.pCrsr!=0 ){
   68465                 :     /* Populate the index search key. */
   68466           34653 :     u.be.r.pKeyInfo = u.be.pCx->pKeyInfo;
   68467           34653 :     u.be.r.nField = u.be.nField + 1;
   68468           34653 :     u.be.r.flags = UNPACKED_PREFIX_SEARCH;
   68469           34653 :     u.be.r.aMem = u.be.aMx;
   68470                 : #ifdef SQLITE_DEBUG
   68471           34653 :     { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
   68472                 : #endif
   68473                 : 
   68474                 :     /* Extract the value of u.be.R from register P3. */
   68475           34653 :     sqlite3VdbeMemIntegerify(pIn3);
   68476           34653 :     u.be.R = pIn3->u.i;
   68477                 : 
   68478                 :     /* Search the B-Tree index. If no conflicting record is found, jump
   68479                 :     ** to P2. Otherwise, copy the rowid of the conflicting record to
   68480                 :     ** register P3 and fall through to the next instruction.  */
   68481           34653 :     rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, &u.be.r, 0, 0, &u.be.pCx->seekResult);
   68482           34653 :     if( (u.be.r.flags & UNPACKED_PREFIX_SEARCH) || u.be.r.rowid==u.be.R ){
   68483           33677 :       pc = pOp->p2 - 1;
   68484                 :     }else{
   68485             976 :       pIn3->u.i = u.be.r.rowid;
   68486                 :     }
   68487                 :   }
   68488           34691 :   break;
   68489                 : }
   68490                 : 
   68491                 : /* Opcode: NotExists P1 P2 P3 * *
   68492                 : **
   68493                 : ** Use the content of register P3 as an integer key.  If a record 
   68494                 : ** with that key does not exist in table of P1, then jump to P2. 
   68495                 : ** If the record does exist, then fall through.  The cursor is left 
   68496                 : ** pointing to the record if it exists.
   68497                 : **
   68498                 : ** The difference between this operation and NotFound is that this
   68499                 : ** operation assumes the key is an integer and that P1 is a table whereas
   68500                 : ** NotFound assumes key is a blob constructed from MakeRecord and
   68501                 : ** P1 is an index.
   68502                 : **
   68503                 : ** See also: Found, NotFound, IsUnique
   68504                 : */
   68505                 : case OP_NotExists: {        /* jump, in3 */
   68506                 : #if 0  /* local variables moved into u.bf */
   68507                 :   VdbeCursor *pC;
   68508                 :   BtCursor *pCrsr;
   68509                 :   int res;
   68510                 :   u64 iKey;
   68511                 : #endif /* local variables moved into u.bf */
   68512                 : 
   68513          174812 :   pIn3 = &aMem[pOp->p3];
   68514          174812 :   assert( pIn3->flags & MEM_Int );
   68515          174812 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68516          174812 :   u.bf.pC = p->apCsr[pOp->p1];
   68517          174812 :   assert( u.bf.pC!=0 );
   68518          174812 :   assert( u.bf.pC->isTable );
   68519          174812 :   assert( u.bf.pC->pseudoTableReg==0 );
   68520          174812 :   u.bf.pCrsr = u.bf.pC->pCursor;
   68521          174812 :   if( ALWAYS(u.bf.pCrsr!=0) ){
   68522          174812 :     u.bf.res = 0;
   68523          174812 :     u.bf.iKey = pIn3->u.i;
   68524          174812 :     rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, 0, u.bf.iKey, 0, &u.bf.res);
   68525          174812 :     u.bf.pC->lastRowid = pIn3->u.i;
   68526          174812 :     u.bf.pC->rowidIsValid = u.bf.res==0 ?1:0;
   68527          174812 :     u.bf.pC->nullRow = 0;
   68528          174812 :     u.bf.pC->cacheStatus = CACHE_STALE;
   68529          174812 :     u.bf.pC->deferredMoveto = 0;
   68530          174812 :     if( u.bf.res!=0 ){
   68531           16608 :       pc = pOp->p2 - 1;
   68532           16608 :       assert( u.bf.pC->rowidIsValid==0 );
   68533                 :     }
   68534          174812 :     u.bf.pC->seekResult = u.bf.res;
   68535                 :   }else{
   68536                 :     /* This happens when an attempt to open a read cursor on the
   68537                 :     ** sqlite_master table returns SQLITE_EMPTY.
   68538                 :     */
   68539               0 :     pc = pOp->p2 - 1;
   68540               0 :     assert( u.bf.pC->rowidIsValid==0 );
   68541               0 :     u.bf.pC->seekResult = 0;
   68542                 :   }
   68543          174812 :   break;
   68544                 : }
   68545                 : 
   68546                 : /* Opcode: Sequence P1 P2 * * *
   68547                 : **
   68548                 : ** Find the next available sequence number for cursor P1.
   68549                 : ** Write the sequence number into register P2.
   68550                 : ** The sequence number on the cursor is incremented after this
   68551                 : ** instruction.  
   68552                 : */
   68553                 : case OP_Sequence: {           /* out2-prerelease */
   68554            4508 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68555            4508 :   assert( p->apCsr[pOp->p1]!=0 );
   68556            4508 :   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
   68557            4508 :   break;
   68558                 : }
   68559                 : 
   68560                 : 
   68561                 : /* Opcode: NewRowid P1 P2 P3 * *
   68562                 : **
   68563                 : ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
   68564                 : ** The record number is not previously used as a key in the database
   68565                 : ** table that cursor P1 points to.  The new record number is written
   68566                 : ** written to register P2.
   68567                 : **
   68568                 : ** If P3>0 then P3 is a register in the root frame of this VDBE that holds 
   68569                 : ** the largest previously generated record number. No new record numbers are
   68570                 : ** allowed to be less than this value. When this value reaches its maximum, 
   68571                 : ** an SQLITE_FULL error is generated. The P3 register is updated with the '
   68572                 : ** generated record number. This P3 mechanism is used to help implement the
   68573                 : ** AUTOINCREMENT feature.
   68574                 : */
   68575                 : case OP_NewRowid: {           /* out2-prerelease */
   68576                 : #if 0  /* local variables moved into u.bg */
   68577                 :   i64 v;                 /* The new rowid */
   68578                 :   VdbeCursor *pC;        /* Cursor of table to get the new rowid */
   68579                 :   int res;               /* Result of an sqlite3BtreeLast() */
   68580                 :   int cnt;               /* Counter to limit the number of searches */
   68581                 :   Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
   68582                 :   VdbeFrame *pFrame;     /* Root frame of VDBE */
   68583                 : #endif /* local variables moved into u.bg */
   68584                 : 
   68585           57443 :   u.bg.v = 0;
   68586           57443 :   u.bg.res = 0;
   68587           57443 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68588           57443 :   u.bg.pC = p->apCsr[pOp->p1];
   68589           57443 :   assert( u.bg.pC!=0 );
   68590           57443 :   if( NEVER(u.bg.pC->pCursor==0) ){
   68591                 :     /* The zero initialization above is all that is needed */
   68592                 :   }else{
   68593                 :     /* The next rowid or record number (different terms for the same
   68594                 :     ** thing) is obtained in a two-step algorithm.
   68595                 :     **
   68596                 :     ** First we attempt to find the largest existing rowid and add one
   68597                 :     ** to that.  But if the largest existing rowid is already the maximum
   68598                 :     ** positive integer, we have to fall through to the second
   68599                 :     ** probabilistic algorithm
   68600                 :     **
   68601                 :     ** The second algorithm is to select a rowid at random and see if
   68602                 :     ** it already exists in the table.  If it does not exist, we have
   68603                 :     ** succeeded.  If the random rowid does exist, we select a new one
   68604                 :     ** and try again, up to 100 times.
   68605                 :     */
   68606           57443 :     assert( u.bg.pC->isTable );
   68607                 : 
   68608                 : #ifdef SQLITE_32BIT_ROWID
   68609                 : #   define MAX_ROWID 0x7fffffff
   68610                 : #else
   68611                 :     /* Some compilers complain about constants of the form 0x7fffffffffffffff.
   68612                 :     ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
   68613                 :     ** to provide the constant while making all compilers happy.
   68614                 :     */
   68615                 : #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
   68616                 : #endif
   68617                 : 
   68618           57443 :     if( !u.bg.pC->useRandomRowid ){
   68619           57443 :       u.bg.v = sqlite3BtreeGetCachedRowid(u.bg.pC->pCursor);
   68620           57443 :       if( u.bg.v==0 ){
   68621           57443 :         rc = sqlite3BtreeLast(u.bg.pC->pCursor, &u.bg.res);
   68622           57443 :         if( rc!=SQLITE_OK ){
   68623               0 :           goto abort_due_to_error;
   68624                 :         }
   68625           57443 :         if( u.bg.res ){
   68626            6559 :           u.bg.v = 1;   /* IMP: R-61914-48074 */
   68627                 :         }else{
   68628           50884 :           assert( sqlite3BtreeCursorIsValid(u.bg.pC->pCursor) );
   68629           50884 :           rc = sqlite3BtreeKeySize(u.bg.pC->pCursor, &u.bg.v);
   68630           50884 :           assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
   68631           50884 :           if( u.bg.v==MAX_ROWID ){
   68632               0 :             u.bg.pC->useRandomRowid = 1;
   68633                 :           }else{
   68634           50884 :             u.bg.v++;   /* IMP: R-29538-34987 */
   68635                 :           }
   68636                 :         }
   68637                 :       }
   68638                 : 
   68639                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   68640           57443 :       if( pOp->p3 ){
   68641                 :         /* Assert that P3 is a valid memory cell. */
   68642            2980 :         assert( pOp->p3>0 );
   68643            2980 :         if( p->pFrame ){
   68644               0 :           for(u.bg.pFrame=p->pFrame; u.bg.pFrame->pParent; u.bg.pFrame=u.bg.pFrame->pParent);
   68645                 :           /* Assert that P3 is a valid memory cell. */
   68646               0 :           assert( pOp->p3<=u.bg.pFrame->nMem );
   68647               0 :           u.bg.pMem = &u.bg.pFrame->aMem[pOp->p3];
   68648                 :         }else{
   68649                 :           /* Assert that P3 is a valid memory cell. */
   68650            2980 :           assert( pOp->p3<=p->nMem );
   68651            2980 :           u.bg.pMem = &aMem[pOp->p3];
   68652            2980 :           memAboutToChange(p, u.bg.pMem);
   68653                 :         }
   68654            2980 :         assert( memIsValid(u.bg.pMem) );
   68655                 : 
   68656            2980 :         REGISTER_TRACE(pOp->p3, u.bg.pMem);
   68657            2980 :         sqlite3VdbeMemIntegerify(u.bg.pMem);
   68658            2980 :         assert( (u.bg.pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
   68659            2980 :         if( u.bg.pMem->u.i==MAX_ROWID || u.bg.pC->useRandomRowid ){
   68660               0 :           rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
   68661               0 :           goto abort_due_to_error;
   68662                 :         }
   68663            2980 :         if( u.bg.v<u.bg.pMem->u.i+1 ){
   68664             574 :           u.bg.v = u.bg.pMem->u.i + 1;
   68665                 :         }
   68666            2980 :         u.bg.pMem->u.i = u.bg.v;
   68667                 :       }
   68668                 : #endif
   68669                 : 
   68670           57443 :       sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, u.bg.v<MAX_ROWID ? u.bg.v+1 : 0);
   68671                 :     }
   68672           57443 :     if( u.bg.pC->useRandomRowid ){
   68673                 :       /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
   68674                 :       ** largest possible integer (9223372036854775807) then the database
   68675                 :       ** engine starts picking positive candidate ROWIDs at random until
   68676                 :       ** it finds one that is not previously used. */
   68677               0 :       assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
   68678                 :                              ** an AUTOINCREMENT table. */
   68679                 :       /* on the first attempt, simply do one more than previous */
   68680               0 :       u.bg.v = lastRowid;
   68681               0 :       u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
   68682               0 :       u.bg.v++; /* ensure non-zero */
   68683               0 :       u.bg.cnt = 0;
   68684               0 :       while(   ((rc = sqlite3BtreeMovetoUnpacked(u.bg.pC->pCursor, 0, (u64)u.bg.v,
   68685                 :                                                  0, &u.bg.res))==SQLITE_OK)
   68686               0 :             && (u.bg.res==0)
   68687               0 :             && (++u.bg.cnt<100)){
   68688                 :         /* collision - try another random rowid */
   68689               0 :         sqlite3_randomness(sizeof(u.bg.v), &u.bg.v);
   68690               0 :         if( u.bg.cnt<5 ){
   68691                 :           /* try "small" random rowids for the initial attempts */
   68692               0 :           u.bg.v &= 0xffffff;
   68693                 :         }else{
   68694               0 :           u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
   68695                 :         }
   68696               0 :         u.bg.v++; /* ensure non-zero */
   68697                 :       }
   68698               0 :       if( rc==SQLITE_OK && u.bg.res==0 ){
   68699               0 :         rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
   68700               0 :         goto abort_due_to_error;
   68701                 :       }
   68702               0 :       assert( u.bg.v>0 );  /* EV: R-40812-03570 */
   68703                 :     }
   68704           57443 :     u.bg.pC->rowidIsValid = 0;
   68705           57443 :     u.bg.pC->deferredMoveto = 0;
   68706           57443 :     u.bg.pC->cacheStatus = CACHE_STALE;
   68707                 :   }
   68708           57443 :   pOut->u.i = u.bg.v;
   68709           57443 :   break;
   68710                 : }
   68711                 : 
   68712                 : /* Opcode: Insert P1 P2 P3 P4 P5
   68713                 : **
   68714                 : ** Write an entry into the table of cursor P1.  A new entry is
   68715                 : ** created if it doesn't already exist or the data for an existing
   68716                 : ** entry is overwritten.  The data is the value MEM_Blob stored in register
   68717                 : ** number P2. The key is stored in register P3. The key must
   68718                 : ** be a MEM_Int.
   68719                 : **
   68720                 : ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
   68721                 : ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
   68722                 : ** then rowid is stored for subsequent return by the
   68723                 : ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
   68724                 : **
   68725                 : ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
   68726                 : ** the last seek operation (OP_NotExists) was a success, then this
   68727                 : ** operation will not attempt to find the appropriate row before doing
   68728                 : ** the insert but will instead overwrite the row that the cursor is
   68729                 : ** currently pointing to.  Presumably, the prior OP_NotExists opcode
   68730                 : ** has already positioned the cursor correctly.  This is an optimization
   68731                 : ** that boosts performance by avoiding redundant seeks.
   68732                 : **
   68733                 : ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
   68734                 : ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
   68735                 : ** is part of an INSERT operation.  The difference is only important to
   68736                 : ** the update hook.
   68737                 : **
   68738                 : ** Parameter P4 may point to a string containing the table-name, or
   68739                 : ** may be NULL. If it is not NULL, then the update-hook 
   68740                 : ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
   68741                 : **
   68742                 : ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
   68743                 : ** allocated, then ownership of P2 is transferred to the pseudo-cursor
   68744                 : ** and register P2 becomes ephemeral.  If the cursor is changed, the
   68745                 : ** value of register P2 will then change.  Make sure this does not
   68746                 : ** cause any problems.)
   68747                 : **
   68748                 : ** This instruction only works on tables.  The equivalent instruction
   68749                 : ** for indices is OP_IdxInsert.
   68750                 : */
   68751                 : /* Opcode: InsertInt P1 P2 P3 P4 P5
   68752                 : **
   68753                 : ** This works exactly like OP_Insert except that the key is the
   68754                 : ** integer value P3, not the value of the integer stored in register P3.
   68755                 : */
   68756                 : case OP_Insert: 
   68757                 : case OP_InsertInt: {
   68758                 : #if 0  /* local variables moved into u.bh */
   68759                 :   Mem *pData;       /* MEM cell holding data for the record to be inserted */
   68760                 :   Mem *pKey;        /* MEM cell holding key  for the record */
   68761                 :   i64 iKey;         /* The integer ROWID or key for the record to be inserted */
   68762                 :   VdbeCursor *pC;   /* Cursor to table into which insert is written */
   68763                 :   int nZero;        /* Number of zero-bytes to append */
   68764                 :   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
   68765                 :   const char *zDb;  /* database name - used by the update hook */
   68766                 :   const char *zTbl; /* Table name - used by the opdate hook */
   68767                 :   int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
   68768                 : #endif /* local variables moved into u.bh */
   68769                 : 
   68770           99148 :   u.bh.pData = &aMem[pOp->p2];
   68771           99148 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68772           99148 :   assert( memIsValid(u.bh.pData) );
   68773           99148 :   u.bh.pC = p->apCsr[pOp->p1];
   68774           99148 :   assert( u.bh.pC!=0 );
   68775           99148 :   assert( u.bh.pC->pCursor!=0 );
   68776           99148 :   assert( u.bh.pC->pseudoTableReg==0 );
   68777           99148 :   assert( u.bh.pC->isTable );
   68778           99148 :   REGISTER_TRACE(pOp->p2, u.bh.pData);
   68779                 : 
   68780           99148 :   if( pOp->opcode==OP_Insert ){
   68781           98919 :     u.bh.pKey = &aMem[pOp->p3];
   68782           98919 :     assert( u.bh.pKey->flags & MEM_Int );
   68783           98919 :     assert( memIsValid(u.bh.pKey) );
   68784           98919 :     REGISTER_TRACE(pOp->p3, u.bh.pKey);
   68785           98919 :     u.bh.iKey = u.bh.pKey->u.i;
   68786                 :   }else{
   68787             229 :     assert( pOp->opcode==OP_InsertInt );
   68788             229 :     u.bh.iKey = pOp->p3;
   68789                 :   }
   68790                 : 
   68791           99148 :   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
   68792           99148 :   if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bh.iKey;
   68793           99148 :   if( u.bh.pData->flags & MEM_Null ){
   68794            7142 :     u.bh.pData->z = 0;
   68795            7142 :     u.bh.pData->n = 0;
   68796                 :   }else{
   68797           92006 :     assert( u.bh.pData->flags & (MEM_Blob|MEM_Str) );
   68798                 :   }
   68799           99148 :   u.bh.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bh.pC->seekResult : 0);
   68800           99148 :   if( u.bh.pData->flags & MEM_Zero ){
   68801               0 :     u.bh.nZero = u.bh.pData->u.nZero;
   68802                 :   }else{
   68803           99148 :     u.bh.nZero = 0;
   68804                 :   }
   68805           99148 :   sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
   68806          396592 :   rc = sqlite3BtreeInsert(u.bh.pC->pCursor, 0, u.bh.iKey,
   68807          198296 :                           u.bh.pData->z, u.bh.pData->n, u.bh.nZero,
   68808           99148 :                           pOp->p5 & OPFLAG_APPEND, u.bh.seekResult
   68809                 :   );
   68810           99148 :   u.bh.pC->rowidIsValid = 0;
   68811           99148 :   u.bh.pC->deferredMoveto = 0;
   68812           99148 :   u.bh.pC->cacheStatus = CACHE_STALE;
   68813                 : 
   68814                 :   /* Invoke the update-hook if required. */
   68815           99148 :   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
   68816               0 :     u.bh.zDb = db->aDb[u.bh.pC->iDb].zName;
   68817               0 :     u.bh.zTbl = pOp->p4.z;
   68818               0 :     u.bh.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
   68819               0 :     assert( u.bh.pC->isTable );
   68820               0 :     db->xUpdateCallback(db->pUpdateArg, u.bh.op, u.bh.zDb, u.bh.zTbl, u.bh.iKey);
   68821               0 :     assert( u.bh.pC->iDb>=0 );
   68822                 :   }
   68823           99148 :   break;
   68824                 : }
   68825                 : 
   68826                 : /* Opcode: Delete P1 P2 * P4 *
   68827                 : **
   68828                 : ** Delete the record at which the P1 cursor is currently pointing.
   68829                 : **
   68830                 : ** The cursor will be left pointing at either the next or the previous
   68831                 : ** record in the table. If it is left pointing at the next record, then
   68832                 : ** the next Next instruction will be a no-op.  Hence it is OK to delete
   68833                 : ** a record from within an Next loop.
   68834                 : **
   68835                 : ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
   68836                 : ** incremented (otherwise not).
   68837                 : **
   68838                 : ** P1 must not be pseudo-table.  It has to be a real table with
   68839                 : ** multiple rows.
   68840                 : **
   68841                 : ** If P4 is not NULL, then it is the name of the table that P1 is
   68842                 : ** pointing to.  The update hook will be invoked, if it exists.
   68843                 : ** If P4 is not NULL then the P1 cursor must have been positioned
   68844                 : ** using OP_NotFound prior to invoking this opcode.
   68845                 : */
   68846                 : case OP_Delete: {
   68847                 : #if 0  /* local variables moved into u.bi */
   68848                 :   i64 iKey;
   68849                 :   VdbeCursor *pC;
   68850                 : #endif /* local variables moved into u.bi */
   68851                 : 
   68852           10446 :   u.bi.iKey = 0;
   68853           10446 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68854           10446 :   u.bi.pC = p->apCsr[pOp->p1];
   68855           10446 :   assert( u.bi.pC!=0 );
   68856           10446 :   assert( u.bi.pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
   68857                 : 
   68858                 :   /* If the update-hook will be invoked, set u.bi.iKey to the rowid of the
   68859                 :   ** row being deleted.
   68860                 :   */
   68861           10446 :   if( db->xUpdateCallback && pOp->p4.z ){
   68862               0 :     assert( u.bi.pC->isTable );
   68863               0 :     assert( u.bi.pC->rowidIsValid );  /* lastRowid set by previous OP_NotFound */
   68864               0 :     u.bi.iKey = u.bi.pC->lastRowid;
   68865                 :   }
   68866                 : 
   68867                 :   /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
   68868                 :   ** OP_Column on the same table without any intervening operations that
   68869                 :   ** might move or invalidate the cursor.  Hence cursor u.bi.pC is always pointing
   68870                 :   ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
   68871                 :   ** below is always a no-op and cannot fail.  We will run it anyhow, though,
   68872                 :   ** to guard against future changes to the code generator.
   68873                 :   **/
   68874           10446 :   assert( u.bi.pC->deferredMoveto==0 );
   68875           10446 :   rc = sqlite3VdbeCursorMoveto(u.bi.pC);
   68876           10446 :   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
   68877                 : 
   68878           10446 :   sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
   68879           10446 :   rc = sqlite3BtreeDelete(u.bi.pC->pCursor);
   68880           10446 :   u.bi.pC->cacheStatus = CACHE_STALE;
   68881                 : 
   68882                 :   /* Invoke the update-hook if required. */
   68883           10446 :   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
   68884               0 :     const char *zDb = db->aDb[u.bi.pC->iDb].zName;
   68885               0 :     const char *zTbl = pOp->p4.z;
   68886               0 :     db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bi.iKey);
   68887               0 :     assert( u.bi.pC->iDb>=0 );
   68888                 :   }
   68889           10446 :   if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
   68890           10446 :   break;
   68891                 : }
   68892                 : /* Opcode: ResetCount * * * * *
   68893                 : **
   68894                 : ** The value of the change counter is copied to the database handle
   68895                 : ** change counter (returned by subsequent calls to sqlite3_changes()).
   68896                 : ** Then the VMs internal change counter resets to 0.
   68897                 : ** This is used by trigger programs.
   68898                 : */
   68899                 : case OP_ResetCount: {
   68900           16309 :   sqlite3VdbeSetChanges(db, p->nChange);
   68901           16309 :   p->nChange = 0;
   68902           16309 :   break;
   68903                 : }
   68904                 : 
   68905                 : /* Opcode: SorterCompare P1 P2 P3
   68906                 : **
   68907                 : ** P1 is a sorter cursor. This instruction compares the record blob in 
   68908                 : ** register P3 with the entry that the sorter cursor currently points to.
   68909                 : ** If, excluding the rowid fields at the end, the two records are a match,
   68910                 : ** fall through to the next instruction. Otherwise, jump to instruction P2.
   68911                 : */
   68912                 : case OP_SorterCompare: {
   68913                 : #if 0  /* local variables moved into u.bj */
   68914                 :   VdbeCursor *pC;
   68915                 :   int res;
   68916                 : #endif /* local variables moved into u.bj */
   68917                 : 
   68918             443 :   u.bj.pC = p->apCsr[pOp->p1];
   68919             443 :   assert( isSorter(u.bj.pC) );
   68920             443 :   pIn3 = &aMem[pOp->p3];
   68921             443 :   rc = sqlite3VdbeSorterCompare(u.bj.pC, pIn3, &u.bj.res);
   68922             443 :   if( u.bj.res ){
   68923             443 :     pc = pOp->p2-1;
   68924                 :   }
   68925             443 :   break;
   68926                 : };
   68927                 : 
   68928                 : /* Opcode: SorterData P1 P2 * * *
   68929                 : **
   68930                 : ** Write into register P2 the current sorter data for sorter cursor P1.
   68931                 : */
   68932                 : case OP_SorterData: {
   68933                 : #if 0  /* local variables moved into u.bk */
   68934                 :   VdbeCursor *pC;
   68935                 : #endif /* local variables moved into u.bk */
   68936                 : #ifndef SQLITE_OMIT_MERGE_SORT
   68937            4582 :   pOut = &aMem[pOp->p2];
   68938            4582 :   u.bk.pC = p->apCsr[pOp->p1];
   68939            4582 :   assert( u.bk.pC->isSorter );
   68940            4582 :   rc = sqlite3VdbeSorterRowkey(u.bk.pC, pOut);
   68941                 : #else
   68942                 :   pOp->opcode = OP_RowKey;
   68943                 :   pc--;
   68944                 : #endif
   68945            4582 :   break;
   68946                 : }
   68947                 : 
   68948                 : /* Opcode: RowData P1 P2 * * *
   68949                 : **
   68950                 : ** Write into register P2 the complete row data for cursor P1.
   68951                 : ** There is no interpretation of the data.  
   68952                 : ** It is just copied onto the P2 register exactly as 
   68953                 : ** it is found in the database file.
   68954                 : **
   68955                 : ** If the P1 cursor must be pointing to a valid row (not a NULL row)
   68956                 : ** of a real table, not a pseudo-table.
   68957                 : */
   68958                 : /* Opcode: RowKey P1 P2 * * *
   68959                 : **
   68960                 : ** Write into register P2 the complete row key for cursor P1.
   68961                 : ** There is no interpretation of the data.  
   68962                 : ** The key is copied onto the P3 register exactly as 
   68963                 : ** it is found in the database file.
   68964                 : **
   68965                 : ** If the P1 cursor must be pointing to a valid row (not a NULL row)
   68966                 : ** of a real table, not a pseudo-table.
   68967                 : */
   68968                 : case OP_RowKey:
   68969                 : case OP_RowData: {
   68970                 : #if 0  /* local variables moved into u.bl */
   68971                 :   VdbeCursor *pC;
   68972                 :   BtCursor *pCrsr;
   68973                 :   u32 n;
   68974                 :   i64 n64;
   68975                 : #endif /* local variables moved into u.bl */
   68976                 : 
   68977            2089 :   pOut = &aMem[pOp->p2];
   68978            2089 :   memAboutToChange(p, pOut);
   68979                 : 
   68980                 :   /* Note that RowKey and RowData are really exactly the same instruction */
   68981            2089 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   68982            2089 :   u.bl.pC = p->apCsr[pOp->p1];
   68983            2089 :   assert( u.bl.pC->isSorter==0 );
   68984            2089 :   assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
   68985            2089 :   assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
   68986            2089 :   assert( u.bl.pC!=0 );
   68987            2089 :   assert( u.bl.pC->nullRow==0 );
   68988            2089 :   assert( u.bl.pC->pseudoTableReg==0 );
   68989            2089 :   assert( !u.bl.pC->isSorter );
   68990            2089 :   assert( u.bl.pC->pCursor!=0 );
   68991            2089 :   u.bl.pCrsr = u.bl.pC->pCursor;
   68992            2089 :   assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
   68993                 : 
   68994                 :   /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
   68995                 :   ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
   68996                 :   ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
   68997                 :   ** a no-op and can never fail.  But we leave it in place as a safety.
   68998                 :   */
   68999            2089 :   assert( u.bl.pC->deferredMoveto==0 );
   69000            2089 :   rc = sqlite3VdbeCursorMoveto(u.bl.pC);
   69001            2089 :   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
   69002                 : 
   69003            2089 :   if( u.bl.pC->isIndex ){
   69004            1562 :     assert( !u.bl.pC->isTable );
   69005            1562 :     VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bl.pCrsr, &u.bl.n64);
   69006            1562 :     assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
   69007            1562 :     if( u.bl.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   69008               0 :       goto too_big;
   69009                 :     }
   69010            1562 :     u.bl.n = (u32)u.bl.n64;
   69011                 :   }else{
   69012             527 :     VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bl.pCrsr, &u.bl.n);
   69013             527 :     assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
   69014             527 :     if( u.bl.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
   69015               0 :       goto too_big;
   69016                 :     }
   69017                 :   }
   69018            2089 :   if( sqlite3VdbeMemGrow(pOut, u.bl.n, 0) ){
   69019               0 :     goto no_mem;
   69020                 :   }
   69021            2089 :   pOut->n = u.bl.n;
   69022            2089 :   MemSetTypeFlag(pOut, MEM_Blob);
   69023            2089 :   if( u.bl.pC->isIndex ){
   69024            1562 :     rc = sqlite3BtreeKey(u.bl.pCrsr, 0, u.bl.n, pOut->z);
   69025                 :   }else{
   69026             527 :     rc = sqlite3BtreeData(u.bl.pCrsr, 0, u.bl.n, pOut->z);
   69027                 :   }
   69028            2089 :   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
   69029                 :   UPDATE_MAX_BLOBSIZE(pOut);
   69030            2089 :   break;
   69031                 : }
   69032                 : 
   69033                 : /* Opcode: Rowid P1 P2 * * *
   69034                 : **
   69035                 : ** Store in register P2 an integer which is the key of the table entry that
   69036                 : ** P1 is currently point to.
   69037                 : **
   69038                 : ** P1 can be either an ordinary table or a virtual table.  There used to
   69039                 : ** be a separate OP_VRowid opcode for use with virtual tables, but this
   69040                 : ** one opcode now works for both table types.
   69041                 : */
   69042                 : case OP_Rowid: {                 /* out2-prerelease */
   69043                 : #if 0  /* local variables moved into u.bm */
   69044                 :   VdbeCursor *pC;
   69045                 :   i64 v;
   69046                 :   sqlite3_vtab *pVtab;
   69047                 :   const sqlite3_module *pModule;
   69048                 : #endif /* local variables moved into u.bm */
   69049                 : 
   69050          104850 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69051          104850 :   u.bm.pC = p->apCsr[pOp->p1];
   69052          104850 :   assert( u.bm.pC!=0 );
   69053          104850 :   assert( u.bm.pC->pseudoTableReg==0 );
   69054          104850 :   if( u.bm.pC->nullRow ){
   69055             971 :     pOut->flags = MEM_Null;
   69056             971 :     break;
   69057          103879 :   }else if( u.bm.pC->deferredMoveto ){
   69058               0 :     u.bm.v = u.bm.pC->movetoTarget;
   69059                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   69060          103879 :   }else if( u.bm.pC->pVtabCursor ){
   69061               2 :     u.bm.pVtab = u.bm.pC->pVtabCursor->pVtab;
   69062               2 :     u.bm.pModule = u.bm.pVtab->pModule;
   69063               2 :     assert( u.bm.pModule->xRowid );
   69064               2 :     rc = u.bm.pModule->xRowid(u.bm.pC->pVtabCursor, &u.bm.v);
   69065               2 :     importVtabErrMsg(p, u.bm.pVtab);
   69066                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   69067                 :   }else{
   69068          103877 :     assert( u.bm.pC->pCursor!=0 );
   69069          103877 :     rc = sqlite3VdbeCursorMoveto(u.bm.pC);
   69070          103877 :     if( rc ) goto abort_due_to_error;
   69071          103877 :     if( u.bm.pC->rowidIsValid ){
   69072           82898 :       u.bm.v = u.bm.pC->lastRowid;
   69073                 :     }else{
   69074           20979 :       rc = sqlite3BtreeKeySize(u.bm.pC->pCursor, &u.bm.v);
   69075           20979 :       assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
   69076                 :     }
   69077                 :   }
   69078          103879 :   pOut->u.i = u.bm.v;
   69079          103879 :   break;
   69080                 : }
   69081                 : 
   69082                 : /* Opcode: NullRow P1 * * * *
   69083                 : **
   69084                 : ** Move the cursor P1 to a null row.  Any OP_Column operations
   69085                 : ** that occur while the cursor is on the null row will always
   69086                 : ** write a NULL.
   69087                 : */
   69088                 : case OP_NullRow: {
   69089                 : #if 0  /* local variables moved into u.bn */
   69090                 :   VdbeCursor *pC;
   69091                 : #endif /* local variables moved into u.bn */
   69092                 : 
   69093           22579 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69094           22579 :   u.bn.pC = p->apCsr[pOp->p1];
   69095           22579 :   assert( u.bn.pC!=0 );
   69096           22579 :   u.bn.pC->nullRow = 1;
   69097           22579 :   u.bn.pC->rowidIsValid = 0;
   69098           22579 :   assert( u.bn.pC->pCursor || u.bn.pC->pVtabCursor );
   69099           22579 :   if( u.bn.pC->pCursor ){
   69100           22579 :     sqlite3BtreeClearCursor(u.bn.pC->pCursor);
   69101                 :   }
   69102           22579 :   break;
   69103                 : }
   69104                 : 
   69105                 : /* Opcode: Last P1 P2 * * *
   69106                 : **
   69107                 : ** The next use of the Rowid or Column or Next instruction for P1 
   69108                 : ** will refer to the last entry in the database table or index.
   69109                 : ** If the table or index is empty and P2>0, then jump immediately to P2.
   69110                 : ** If P2 is 0 or if the table or index is not empty, fall through
   69111                 : ** to the following instruction.
   69112                 : */
   69113                 : case OP_Last: {        /* jump */
   69114                 : #if 0  /* local variables moved into u.bo */
   69115                 :   VdbeCursor *pC;
   69116                 :   BtCursor *pCrsr;
   69117                 :   int res;
   69118                 : #endif /* local variables moved into u.bo */
   69119                 : 
   69120            3471 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69121            3471 :   u.bo.pC = p->apCsr[pOp->p1];
   69122            3471 :   assert( u.bo.pC!=0 );
   69123            3471 :   u.bo.pCrsr = u.bo.pC->pCursor;
   69124            3471 :   u.bo.res = 0;
   69125            3471 :   if( ALWAYS(u.bo.pCrsr!=0) ){
   69126            3471 :     rc = sqlite3BtreeLast(u.bo.pCrsr, &u.bo.res);
   69127                 :   }
   69128            3471 :   u.bo.pC->nullRow = (u8)u.bo.res;
   69129            3471 :   u.bo.pC->deferredMoveto = 0;
   69130            3471 :   u.bo.pC->rowidIsValid = 0;
   69131            3471 :   u.bo.pC->cacheStatus = CACHE_STALE;
   69132            3471 :   if( pOp->p2>0 && u.bo.res ){
   69133             216 :     pc = pOp->p2 - 1;
   69134                 :   }
   69135            3471 :   break;
   69136                 : }
   69137                 : 
   69138                 : 
   69139                 : /* Opcode: Sort P1 P2 * * *
   69140                 : **
   69141                 : ** This opcode does exactly the same thing as OP_Rewind except that
   69142                 : ** it increments an undocumented global variable used for testing.
   69143                 : **
   69144                 : ** Sorting is accomplished by writing records into a sorting index,
   69145                 : ** then rewinding that index and playing it back from beginning to
   69146                 : ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
   69147                 : ** rewinding so that the global variable will be incremented and
   69148                 : ** regression tests can determine whether or not the optimizer is
   69149                 : ** correctly optimizing out sorts.
   69150                 : */
   69151                 : case OP_SorterSort:    /* jump */
   69152                 : #ifdef SQLITE_OMIT_MERGE_SORT
   69153                 :   pOp->opcode = OP_Sort;
   69154                 : #endif
   69155                 : case OP_Sort: {        /* jump */
   69156                 : #ifdef SQLITE_TEST
   69157                 :   sqlite3_sort_count++;
   69158                 :   sqlite3_search_count--;
   69159                 : #endif
   69160            7138 :   p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
   69161                 :   /* Fall through into OP_Rewind */
   69162                 : }
   69163                 : /* Opcode: Rewind P1 P2 * * *
   69164                 : **
   69165                 : ** The next use of the Rowid or Column or Next instruction for P1 
   69166                 : ** will refer to the first entry in the database table or index.
   69167                 : ** If the table or index is empty and P2>0, then jump immediately to P2.
   69168                 : ** If P2 is 0 or if the table or index is not empty, fall through
   69169                 : ** to the following instruction.
   69170                 : */
   69171                 : case OP_Rewind: {        /* jump */
   69172                 : #if 0  /* local variables moved into u.bp */
   69173                 :   VdbeCursor *pC;
   69174                 :   BtCursor *pCrsr;
   69175                 :   int res;
   69176                 : #endif /* local variables moved into u.bp */
   69177                 : 
   69178           74935 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69179           74935 :   u.bp.pC = p->apCsr[pOp->p1];
   69180           74935 :   assert( u.bp.pC!=0 );
   69181           74935 :   assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterSort) );
   69182           74935 :   u.bp.res = 1;
   69183           74935 :   if( isSorter(u.bp.pC) ){
   69184            6886 :     rc = sqlite3VdbeSorterRewind(db, u.bp.pC, &u.bp.res);
   69185                 :   }else{
   69186           68049 :     u.bp.pCrsr = u.bp.pC->pCursor;
   69187           68049 :     assert( u.bp.pCrsr );
   69188           68049 :     rc = sqlite3BtreeFirst(u.bp.pCrsr, &u.bp.res);
   69189           68049 :     u.bp.pC->atFirst = u.bp.res==0 ?1:0;
   69190           68049 :     u.bp.pC->deferredMoveto = 0;
   69191           68049 :     u.bp.pC->cacheStatus = CACHE_STALE;
   69192           68049 :     u.bp.pC->rowidIsValid = 0;
   69193                 :   }
   69194           74935 :   u.bp.pC->nullRow = (u8)u.bp.res;
   69195           74935 :   assert( pOp->p2>0 && pOp->p2<p->nOp );
   69196           74935 :   if( u.bp.res ){
   69197           31704 :     pc = pOp->p2 - 1;
   69198                 :   }
   69199           74935 :   break;
   69200                 : }
   69201                 : 
   69202                 : /* Opcode: Next P1 P2 * P4 P5
   69203                 : **
   69204                 : ** Advance cursor P1 so that it points to the next key/data pair in its
   69205                 : ** table or index.  If there are no more key/value pairs then fall through
   69206                 : ** to the following instruction.  But if the cursor advance was successful,
   69207                 : ** jump immediately to P2.
   69208                 : **
   69209                 : ** The P1 cursor must be for a real table, not a pseudo-table.
   69210                 : **
   69211                 : ** P4 is always of type P4_ADVANCE. The function pointer points to
   69212                 : ** sqlite3BtreeNext().
   69213                 : **
   69214                 : ** If P5 is positive and the jump is taken, then event counter
   69215                 : ** number P5-1 in the prepared statement is incremented.
   69216                 : **
   69217                 : ** See also: Prev
   69218                 : */
   69219                 : /* Opcode: Prev P1 P2 * * P5
   69220                 : **
   69221                 : ** Back up cursor P1 so that it points to the previous key/data pair in its
   69222                 : ** table or index.  If there is no previous key/value pairs then fall through
   69223                 : ** to the following instruction.  But if the cursor backup was successful,
   69224                 : ** jump immediately to P2.
   69225                 : **
   69226                 : ** The P1 cursor must be for a real table, not a pseudo-table.
   69227                 : **
   69228                 : ** P4 is always of type P4_ADVANCE. The function pointer points to
   69229                 : ** sqlite3BtreePrevious().
   69230                 : **
   69231                 : ** If P5 is positive and the jump is taken, then event counter
   69232                 : ** number P5-1 in the prepared statement is incremented.
   69233                 : */
   69234                 : case OP_SorterNext:    /* jump */
   69235                 : #ifdef SQLITE_OMIT_MERGE_SORT
   69236                 :   pOp->opcode = OP_Next;
   69237                 : #endif
   69238                 : case OP_Prev:          /* jump */
   69239                 : case OP_Next: {        /* jump */
   69240                 : #if 0  /* local variables moved into u.bq */
   69241                 :   VdbeCursor *pC;
   69242                 :   int res;
   69243                 : #endif /* local variables moved into u.bq */
   69244                 : 
   69245         1154636 :   CHECK_FOR_INTERRUPT;
   69246         1154636 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69247         1154636 :   assert( pOp->p5<=ArraySize(p->aCounter) );
   69248         1154636 :   u.bq.pC = p->apCsr[pOp->p1];
   69249         1154636 :   if( u.bq.pC==0 ){
   69250               0 :     break;  /* See ticket #2273 */
   69251                 :   }
   69252         1154636 :   assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterNext) );
   69253         1154636 :   if( isSorter(u.bq.pC) ){
   69254            4574 :     assert( pOp->opcode==OP_SorterNext );
   69255            4574 :     rc = sqlite3VdbeSorterNext(db, u.bq.pC, &u.bq.res);
   69256                 :   }else{
   69257         1150062 :     u.bq.res = 1;
   69258         1150062 :     assert( u.bq.pC->deferredMoveto==0 );
   69259         1150062 :     assert( u.bq.pC->pCursor );
   69260         1150062 :     assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
   69261         1150062 :     assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
   69262         1150062 :     rc = pOp->p4.xAdvance(u.bq.pC->pCursor, &u.bq.res);
   69263                 :   }
   69264         1154637 :   u.bq.pC->nullRow = (u8)u.bq.res;
   69265         1154637 :   u.bq.pC->cacheStatus = CACHE_STALE;
   69266         1154637 :   if( u.bq.res==0 ){
   69267         1100871 :     pc = pOp->p2 - 1;
   69268         1100871 :     if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
   69269                 : #ifdef SQLITE_TEST
   69270                 :     sqlite3_search_count++;
   69271                 : #endif
   69272                 :   }
   69273         1154637 :   u.bq.pC->rowidIsValid = 0;
   69274         1154637 :   break;
   69275                 : }
   69276                 : 
   69277                 : /* Opcode: IdxInsert P1 P2 P3 * P5
   69278                 : **
   69279                 : ** Register P2 holds an SQL index key made using the
   69280                 : ** MakeRecord instructions.  This opcode writes that key
   69281                 : ** into the index P1.  Data for the entry is nil.
   69282                 : **
   69283                 : ** P3 is a flag that provides a hint to the b-tree layer that this
   69284                 : ** insert is likely to be an append.
   69285                 : **
   69286                 : ** This instruction only works for indices.  The equivalent instruction
   69287                 : ** for tables is OP_Insert.
   69288                 : */
   69289                 : case OP_SorterInsert:       /* in2 */
   69290                 : #ifdef SQLITE_OMIT_MERGE_SORT
   69291                 :   pOp->opcode = OP_IdxInsert;
   69292                 : #endif
   69293                 : case OP_IdxInsert: {        /* in2 */
   69294                 : #if 0  /* local variables moved into u.br */
   69295                 :   VdbeCursor *pC;
   69296                 :   BtCursor *pCrsr;
   69297                 :   int nKey;
   69298                 :   const char *zKey;
   69299                 : #endif /* local variables moved into u.br */
   69300                 : 
   69301          136293 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69302          136293 :   u.br.pC = p->apCsr[pOp->p1];
   69303          136293 :   assert( u.br.pC!=0 );
   69304          136293 :   assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
   69305          136293 :   pIn2 = &aMem[pOp->p2];
   69306          136293 :   assert( pIn2->flags & MEM_Blob );
   69307          136293 :   u.br.pCrsr = u.br.pC->pCursor;
   69308          136293 :   if( ALWAYS(u.br.pCrsr!=0) ){
   69309          136293 :     assert( u.br.pC->isTable==0 );
   69310          136293 :     rc = ExpandBlob(pIn2);
   69311          136293 :     if( rc==SQLITE_OK ){
   69312          136293 :       if( isSorter(u.br.pC) ){
   69313            4582 :         rc = sqlite3VdbeSorterWrite(db, u.br.pC, pIn2);
   69314                 :       }else{
   69315          131711 :         u.br.nKey = pIn2->n;
   69316          131711 :         u.br.zKey = pIn2->z;
   69317          208961 :         rc = sqlite3BtreeInsert(u.br.pCrsr, u.br.zKey, u.br.nKey, "", 0, 0, pOp->p3,
   69318          208961 :             ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.br.pC->seekResult : 0)
   69319                 :             );
   69320          131711 :         assert( u.br.pC->deferredMoveto==0 );
   69321          131711 :         u.br.pC->cacheStatus = CACHE_STALE;
   69322                 :       }
   69323                 :     }
   69324                 :   }
   69325          136293 :   break;
   69326                 : }
   69327                 : 
   69328                 : /* Opcode: IdxDelete P1 P2 P3 * *
   69329                 : **
   69330                 : ** The content of P3 registers starting at register P2 form
   69331                 : ** an unpacked index key. This opcode removes that entry from the 
   69332                 : ** index opened by cursor P1.
   69333                 : */
   69334                 : case OP_IdxDelete: {
   69335                 : #if 0  /* local variables moved into u.bs */
   69336                 :   VdbeCursor *pC;
   69337                 :   BtCursor *pCrsr;
   69338                 :   int res;
   69339                 :   UnpackedRecord r;
   69340                 : #endif /* local variables moved into u.bs */
   69341                 : 
   69342           42007 :   assert( pOp->p3>0 );
   69343           42007 :   assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
   69344           42007 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69345           42007 :   u.bs.pC = p->apCsr[pOp->p1];
   69346           42007 :   assert( u.bs.pC!=0 );
   69347           42007 :   u.bs.pCrsr = u.bs.pC->pCursor;
   69348           42007 :   if( ALWAYS(u.bs.pCrsr!=0) ){
   69349           42007 :     u.bs.r.pKeyInfo = u.bs.pC->pKeyInfo;
   69350           42007 :     u.bs.r.nField = (u16)pOp->p3;
   69351           42007 :     u.bs.r.flags = 0;
   69352           42007 :     u.bs.r.aMem = &aMem[pOp->p2];
   69353                 : #ifdef SQLITE_DEBUG
   69354           42007 :     { int i; for(i=0; i<u.bs.r.nField; i++) assert( memIsValid(&u.bs.r.aMem[i]) ); }
   69355                 : #endif
   69356           42007 :     rc = sqlite3BtreeMovetoUnpacked(u.bs.pCrsr, &u.bs.r, 0, 0, &u.bs.res);
   69357           42007 :     if( rc==SQLITE_OK && u.bs.res==0 ){
   69358           41979 :       rc = sqlite3BtreeDelete(u.bs.pCrsr);
   69359                 :     }
   69360           42007 :     assert( u.bs.pC->deferredMoveto==0 );
   69361           42007 :     u.bs.pC->cacheStatus = CACHE_STALE;
   69362                 :   }
   69363           42007 :   break;
   69364                 : }
   69365                 : 
   69366                 : /* Opcode: IdxRowid P1 P2 * * *
   69367                 : **
   69368                 : ** Write into register P2 an integer which is the last entry in the record at
   69369                 : ** the end of the index key pointed to by cursor P1.  This integer should be
   69370                 : ** the rowid of the table entry to which this index entry points.
   69371                 : **
   69372                 : ** See also: Rowid, MakeRecord.
   69373                 : */
   69374                 : case OP_IdxRowid: {              /* out2-prerelease */
   69375                 : #if 0  /* local variables moved into u.bt */
   69376                 :   BtCursor *pCrsr;
   69377                 :   VdbeCursor *pC;
   69378                 :   i64 rowid;
   69379                 : #endif /* local variables moved into u.bt */
   69380                 : 
   69381          151900 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69382          151900 :   u.bt.pC = p->apCsr[pOp->p1];
   69383          151900 :   assert( u.bt.pC!=0 );
   69384          151900 :   u.bt.pCrsr = u.bt.pC->pCursor;
   69385          151900 :   pOut->flags = MEM_Null;
   69386          151900 :   if( ALWAYS(u.bt.pCrsr!=0) ){
   69387          151900 :     rc = sqlite3VdbeCursorMoveto(u.bt.pC);
   69388          151900 :     if( NEVER(rc) ) goto abort_due_to_error;
   69389          151900 :     assert( u.bt.pC->deferredMoveto==0 );
   69390          151900 :     assert( u.bt.pC->isTable==0 );
   69391          151900 :     if( !u.bt.pC->nullRow ){
   69392          149363 :       rc = sqlite3VdbeIdxRowid(db, u.bt.pCrsr, &u.bt.rowid);
   69393          149363 :       if( rc!=SQLITE_OK ){
   69394               0 :         goto abort_due_to_error;
   69395                 :       }
   69396          149363 :       pOut->u.i = u.bt.rowid;
   69397          149363 :       pOut->flags = MEM_Int;
   69398                 :     }
   69399                 :   }
   69400          151900 :   break;
   69401                 : }
   69402                 : 
   69403                 : /* Opcode: IdxGE P1 P2 P3 P4 P5
   69404                 : **
   69405                 : ** The P4 register values beginning with P3 form an unpacked index 
   69406                 : ** key that omits the ROWID.  Compare this key value against the index 
   69407                 : ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
   69408                 : **
   69409                 : ** If the P1 index entry is greater than or equal to the key value
   69410                 : ** then jump to P2.  Otherwise fall through to the next instruction.
   69411                 : **
   69412                 : ** If P5 is non-zero then the key value is increased by an epsilon 
   69413                 : ** prior to the comparison.  This make the opcode work like IdxGT except
   69414                 : ** that if the key from register P3 is a prefix of the key in the cursor,
   69415                 : ** the result is false whereas it would be true with IdxGT.
   69416                 : */
   69417                 : /* Opcode: IdxLT P1 P2 P3 P4 P5
   69418                 : **
   69419                 : ** The P4 register values beginning with P3 form an unpacked index 
   69420                 : ** key that omits the ROWID.  Compare this key value against the index 
   69421                 : ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
   69422                 : **
   69423                 : ** If the P1 index entry is less than the key value then jump to P2.
   69424                 : ** Otherwise fall through to the next instruction.
   69425                 : **
   69426                 : ** If P5 is non-zero then the key value is increased by an epsilon prior 
   69427                 : ** to the comparison.  This makes the opcode work like IdxLE.
   69428                 : */
   69429                 : case OP_IdxLT:          /* jump */
   69430                 : case OP_IdxGE: {        /* jump */
   69431                 : #if 0  /* local variables moved into u.bu */
   69432                 :   VdbeCursor *pC;
   69433                 :   int res;
   69434                 :   UnpackedRecord r;
   69435                 : #endif /* local variables moved into u.bu */
   69436                 : 
   69437          173863 :   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
   69438          173863 :   u.bu.pC = p->apCsr[pOp->p1];
   69439          173863 :   assert( u.bu.pC!=0 );
   69440          173863 :   assert( u.bu.pC->isOrdered );
   69441          173863 :   if( ALWAYS(u.bu.pC->pCursor!=0) ){
   69442          173863 :     assert( u.bu.pC->deferredMoveto==0 );
   69443          173863 :     assert( pOp->p5==0 || pOp->p5==1 );
   69444          173863 :     assert( pOp->p4type==P4_INT32 );
   69445          173863 :     u.bu.r.pKeyInfo = u.bu.pC->pKeyInfo;
   69446          173863 :     u.bu.r.nField = (u16)pOp->p4.i;
   69447          173863 :     if( pOp->p5 ){
   69448          154079 :       u.bu.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
   69449                 :     }else{
   69450           19784 :       u.bu.r.flags = UNPACKED_PREFIX_MATCH;
   69451                 :     }
   69452          173863 :     u.bu.r.aMem = &aMem[pOp->p3];
   69453                 : #ifdef SQLITE_DEBUG
   69454          173863 :     { int i; for(i=0; i<u.bu.r.nField; i++) assert( memIsValid(&u.bu.r.aMem[i]) ); }
   69455                 : #endif
   69456          173863 :     rc = sqlite3VdbeIdxKeyCompare(u.bu.pC, &u.bu.r, &u.bu.res);
   69457          173863 :     if( pOp->opcode==OP_IdxLT ){
   69458           18760 :       u.bu.res = -u.bu.res;
   69459                 :     }else{
   69460          155103 :       assert( pOp->opcode==OP_IdxGE );
   69461          155103 :       u.bu.res++;
   69462                 :     }
   69463          173863 :     if( u.bu.res>0 ){
   69464           24225 :       pc = pOp->p2 - 1 ;
   69465                 :     }
   69466                 :   }
   69467          173863 :   break;
   69468                 : }
   69469                 : 
   69470                 : /* Opcode: Destroy P1 P2 P3 * *
   69471                 : **
   69472                 : ** Delete an entire database table or index whose root page in the database
   69473                 : ** file is given by P1.
   69474                 : **
   69475                 : ** The table being destroyed is in the main database file if P3==0.  If
   69476                 : ** P3==1 then the table to be clear is in the auxiliary database file
   69477                 : ** that is used to store tables create using CREATE TEMPORARY TABLE.
   69478                 : **
   69479                 : ** If AUTOVACUUM is enabled then it is possible that another root page
   69480                 : ** might be moved into the newly deleted root page in order to keep all
   69481                 : ** root pages contiguous at the beginning of the database.  The former
   69482                 : ** value of the root page that moved - its value before the move occurred -
   69483                 : ** is stored in register P2.  If no page 
   69484                 : ** movement was required (because the table being dropped was already 
   69485                 : ** the last one in the database) then a zero is stored in register P2.
   69486                 : ** If AUTOVACUUM is disabled then a zero is stored in register P2.
   69487                 : **
   69488                 : ** See also: Clear
   69489                 : */
   69490                 : case OP_Destroy: {     /* out2-prerelease */
   69491                 : #if 0  /* local variables moved into u.bv */
   69492                 :   int iMoved;
   69493                 :   int iCnt;
   69494                 :   Vdbe *pVdbe;
   69495                 :   int iDb;
   69496                 : #endif /* local variables moved into u.bv */
   69497                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   69498              66 :   u.bv.iCnt = 0;
   69499            2304 :   for(u.bv.pVdbe=db->pVdbe; u.bv.pVdbe; u.bv.pVdbe = u.bv.pVdbe->pNext){
   69500            2238 :     if( u.bv.pVdbe->magic==VDBE_MAGIC_RUN && u.bv.pVdbe->inVtabMethod<2 && u.bv.pVdbe->pc>=0 ){
   69501              68 :       u.bv.iCnt++;
   69502                 :     }
   69503                 :   }
   69504                 : #else
   69505                 :   u.bv.iCnt = db->activeVdbeCnt;
   69506                 : #endif
   69507              66 :   pOut->flags = MEM_Null;
   69508              66 :   if( u.bv.iCnt>1 ){
   69509               2 :     rc = SQLITE_LOCKED;
   69510               2 :     p->errorAction = OE_Abort;
   69511                 :   }else{
   69512              64 :     u.bv.iDb = pOp->p3;
   69513              64 :     assert( u.bv.iCnt==1 );
   69514              64 :     assert( (p->btreeMask & (((yDbMask)1)<<u.bv.iDb))!=0 );
   69515              64 :     rc = sqlite3BtreeDropTable(db->aDb[u.bv.iDb].pBt, pOp->p1, &u.bv.iMoved);
   69516              64 :     pOut->flags = MEM_Int;
   69517              64 :     pOut->u.i = u.bv.iMoved;
   69518                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   69519              64 :     if( rc==SQLITE_OK && u.bv.iMoved!=0 ){
   69520               0 :       sqlite3RootPageMoved(db, u.bv.iDb, u.bv.iMoved, pOp->p1);
   69521                 :       /* All OP_Destroy operations occur on the same btree */
   69522               0 :       assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bv.iDb+1 );
   69523               0 :       resetSchemaOnFault = u.bv.iDb+1;
   69524                 :     }
   69525                 : #endif
   69526                 :   }
   69527              66 :   break;
   69528                 : }
   69529                 : 
   69530                 : /* Opcode: Clear P1 P2 P3
   69531                 : **
   69532                 : ** Delete all contents of the database table or index whose root page
   69533                 : ** in the database file is given by P1.  But, unlike Destroy, do not
   69534                 : ** remove the table or index from the database file.
   69535                 : **
   69536                 : ** The table being clear is in the main database file if P2==0.  If
   69537                 : ** P2==1 then the table to be clear is in the auxiliary database file
   69538                 : ** that is used to store tables create using CREATE TEMPORARY TABLE.
   69539                 : **
   69540                 : ** If the P3 value is non-zero, then the table referred to must be an
   69541                 : ** intkey table (an SQL table, not an index). In this case the row change 
   69542                 : ** count is incremented by the number of rows in the table being cleared. 
   69543                 : ** If P3 is greater than zero, then the value stored in register P3 is
   69544                 : ** also incremented by the number of rows in the table being cleared.
   69545                 : **
   69546                 : ** See also: Destroy
   69547                 : */
   69548                 : case OP_Clear: {
   69549                 : #if 0  /* local variables moved into u.bw */
   69550                 :   int nChange;
   69551                 : #endif /* local variables moved into u.bw */
   69552                 : 
   69553             618 :   u.bw.nChange = 0;
   69554             618 :   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
   69555            1236 :   rc = sqlite3BtreeClearTable(
   69556            1236 :       db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bw.nChange : 0)
   69557                 :   );
   69558             618 :   if( pOp->p3 ){
   69559             268 :     p->nChange += u.bw.nChange;
   69560             268 :     if( pOp->p3>0 ){
   69561               0 :       assert( memIsValid(&aMem[pOp->p3]) );
   69562               0 :       memAboutToChange(p, &aMem[pOp->p3]);
   69563               0 :       aMem[pOp->p3].u.i += u.bw.nChange;
   69564                 :     }
   69565                 :   }
   69566             618 :   break;
   69567                 : }
   69568                 : 
   69569                 : /* Opcode: CreateTable P1 P2 * * *
   69570                 : **
   69571                 : ** Allocate a new table in the main database file if P1==0 or in the
   69572                 : ** auxiliary database file if P1==1 or in an attached database if
   69573                 : ** P1>1.  Write the root page number of the new table into
   69574                 : ** register P2
   69575                 : **
   69576                 : ** The difference between a table and an index is this:  A table must
   69577                 : ** have a 4-byte integer key and can have arbitrary data.  An index
   69578                 : ** has an arbitrary key but no data.
   69579                 : **
   69580                 : ** See also: CreateIndex
   69581                 : */
   69582                 : /* Opcode: CreateIndex P1 P2 * * *
   69583                 : **
   69584                 : ** Allocate a new index in the main database file if P1==0 or in the
   69585                 : ** auxiliary database file if P1==1 or in an attached database if
   69586                 : ** P1>1.  Write the root page number of the new table into
   69587                 : ** register P2.
   69588                 : **
   69589                 : ** See documentation on OP_CreateTable for additional information.
   69590                 : */
   69591                 : case OP_CreateIndex:            /* out2-prerelease */
   69592                 : case OP_CreateTable: {          /* out2-prerelease */
   69593                 : #if 0  /* local variables moved into u.bx */
   69594                 :   int pgno;
   69595                 :   int flags;
   69596                 :   Db *pDb;
   69597                 : #endif /* local variables moved into u.bx */
   69598                 : 
   69599           15600 :   u.bx.pgno = 0;
   69600           15600 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
   69601           15600 :   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
   69602           15600 :   u.bx.pDb = &db->aDb[pOp->p1];
   69603           15600 :   assert( u.bx.pDb->pBt!=0 );
   69604           15600 :   if( pOp->opcode==OP_CreateTable ){
   69605                 :     /* u.bx.flags = BTREE_INTKEY; */
   69606            6782 :     u.bx.flags = BTREE_INTKEY;
   69607                 :   }else{
   69608            8818 :     u.bx.flags = BTREE_BLOBKEY;
   69609                 :   }
   69610           15600 :   rc = sqlite3BtreeCreateTable(u.bx.pDb->pBt, &u.bx.pgno, u.bx.flags);
   69611           15600 :   pOut->u.i = u.bx.pgno;
   69612           15600 :   break;
   69613                 : }
   69614                 : 
   69615                 : /* Opcode: ParseSchema P1 * * P4 *
   69616                 : **
   69617                 : ** Read and parse all entries from the SQLITE_MASTER table of database P1
   69618                 : ** that match the WHERE clause P4. 
   69619                 : **
   69620                 : ** This opcode invokes the parser to create a new virtual machine,
   69621                 : ** then runs the new virtual machine.  It is thus a re-entrant opcode.
   69622                 : */
   69623                 : case OP_ParseSchema: {
   69624                 : #if 0  /* local variables moved into u.by */
   69625                 :   int iDb;
   69626                 :   const char *zMaster;
   69627                 :   char *zSql;
   69628                 :   InitData initData;
   69629                 : #endif /* local variables moved into u.by */
   69630                 : 
   69631                 :   /* Any prepared statement that invokes this opcode will hold mutexes
   69632                 :   ** on every btree.  This is a prerequisite for invoking
   69633                 :   ** sqlite3InitCallback().
   69634                 :   */
   69635                 : #ifdef SQLITE_DEBUG
   69636           45449 :   for(u.by.iDb=0; u.by.iDb<db->nDb; u.by.iDb++){
   69637           30313 :     assert( u.by.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.by.iDb].pBt) );
   69638                 :   }
   69639                 : #endif
   69640                 : 
   69641           15136 :   u.by.iDb = pOp->p1;
   69642           15136 :   assert( u.by.iDb>=0 && u.by.iDb<db->nDb );
   69643           15136 :   assert( DbHasProperty(db, u.by.iDb, DB_SchemaLoaded) );
   69644                 :   /* Used to be a conditional */ {
   69645           15136 :     u.by.zMaster = SCHEMA_TABLE(u.by.iDb);
   69646           15136 :     u.by.initData.db = db;
   69647           15136 :     u.by.initData.iDb = pOp->p1;
   69648           15136 :     u.by.initData.pzErrMsg = &p->zErrMsg;
   69649           30272 :     u.by.zSql = sqlite3MPrintf(db,
   69650                 :        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
   69651           15136 :        db->aDb[u.by.iDb].zName, u.by.zMaster, pOp->p4.z);
   69652           15136 :     if( u.by.zSql==0 ){
   69653               0 :       rc = SQLITE_NOMEM;
   69654                 :     }else{
   69655           15136 :       assert( db->init.busy==0 );
   69656           15136 :       db->init.busy = 1;
   69657           15136 :       u.by.initData.rc = SQLITE_OK;
   69658           15136 :       assert( !db->mallocFailed );
   69659           15136 :       rc = sqlite3_exec(db, u.by.zSql, sqlite3InitCallback, &u.by.initData, 0);
   69660           15136 :       if( rc==SQLITE_OK ) rc = u.by.initData.rc;
   69661           15136 :       sqlite3DbFree(db, u.by.zSql);
   69662           15136 :       db->init.busy = 0;
   69663                 :     }
   69664                 :   }
   69665           15136 :   if( rc==SQLITE_NOMEM ){
   69666               0 :     goto no_mem;
   69667                 :   }
   69668           15136 :   break;
   69669                 : }
   69670                 : 
   69671                 : #if !defined(SQLITE_OMIT_ANALYZE)
   69672                 : /* Opcode: LoadAnalysis P1 * * * *
   69673                 : **
   69674                 : ** Read the sqlite_stat1 table for database P1 and load the content
   69675                 : ** of that table into the internal index hash table.  This will cause
   69676                 : ** the analysis to be used when preparing all subsequent queries.
   69677                 : */
   69678                 : case OP_LoadAnalysis: {
   69679            2033 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
   69680            2033 :   rc = sqlite3AnalysisLoad(db, pOp->p1);
   69681            2033 :   break;  
   69682                 : }
   69683                 : #endif /* !defined(SQLITE_OMIT_ANALYZE) */
   69684                 : 
   69685                 : /* Opcode: DropTable P1 * * P4 *
   69686                 : **
   69687                 : ** Remove the internal (in-memory) data structures that describe
   69688                 : ** the table named P4 in database P1.  This is called after a table
   69689                 : ** is dropped in order to keep the internal representation of the
   69690                 : ** schema consistent with what is on disk.
   69691                 : */
   69692                 : case OP_DropTable: {
   69693             209 :   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
   69694             209 :   break;
   69695                 : }
   69696                 : 
   69697                 : /* Opcode: DropIndex P1 * * P4 *
   69698                 : **
   69699                 : ** Remove the internal (in-memory) data structures that describe
   69700                 : ** the index named P4 in database P1.  This is called after an index
   69701                 : ** is dropped in order to keep the internal representation of the
   69702                 : ** schema consistent with what is on disk.
   69703                 : */
   69704                 : case OP_DropIndex: {
   69705              26 :   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
   69706              26 :   break;
   69707                 : }
   69708                 : 
   69709                 : /* Opcode: DropTrigger P1 * * P4 *
   69710                 : **
   69711                 : ** Remove the internal (in-memory) data structures that describe
   69712                 : ** the trigger named P4 in database P1.  This is called after a trigger
   69713                 : ** is dropped in order to keep the internal representation of the
   69714                 : ** schema consistent with what is on disk.
   69715                 : */
   69716                 : case OP_DropTrigger: {
   69717              54 :   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
   69718              54 :   break;
   69719                 : }
   69720                 : 
   69721                 : 
   69722                 : #ifndef SQLITE_OMIT_INTEGRITY_CHECK
   69723                 : /* Opcode: IntegrityCk P1 P2 P3 * P5
   69724                 : **
   69725                 : ** Do an analysis of the currently open database.  Store in
   69726                 : ** register P1 the text of an error message describing any problems.
   69727                 : ** If no problems are found, store a NULL in register P1.
   69728                 : **
   69729                 : ** The register P3 contains the maximum number of allowed errors.
   69730                 : ** At most reg(P3) errors will be reported.
   69731                 : ** In other words, the analysis stops as soon as reg(P1) errors are 
   69732                 : ** seen.  Reg(P1) is updated with the number of errors remaining.
   69733                 : **
   69734                 : ** The root page numbers of all tables in the database are integer
   69735                 : ** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
   69736                 : ** total.
   69737                 : **
   69738                 : ** If P5 is not zero, the check is done on the auxiliary database
   69739                 : ** file, not the main database file.
   69740                 : **
   69741                 : ** This opcode is used to implement the integrity_check pragma.
   69742                 : */
   69743                 : case OP_IntegrityCk: {
   69744                 : #if 0  /* local variables moved into u.bz */
   69745                 :   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
   69746                 :   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
   69747                 :   int j;          /* Loop counter */
   69748                 :   int nErr;       /* Number of errors reported */
   69749                 :   char *z;        /* Text of the error report */
   69750                 :   Mem *pnErr;     /* Register keeping track of errors remaining */
   69751                 : #endif /* local variables moved into u.bz */
   69752                 : 
   69753              52 :   u.bz.nRoot = pOp->p2;
   69754              52 :   assert( u.bz.nRoot>0 );
   69755              52 :   u.bz.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bz.nRoot+1) );
   69756              52 :   if( u.bz.aRoot==0 ) goto no_mem;
   69757              52 :   assert( pOp->p3>0 && pOp->p3<=p->nMem );
   69758              52 :   u.bz.pnErr = &aMem[pOp->p3];
   69759              52 :   assert( (u.bz.pnErr->flags & MEM_Int)!=0 );
   69760              52 :   assert( (u.bz.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
   69761              52 :   pIn1 = &aMem[pOp->p1];
   69762            1039 :   for(u.bz.j=0; u.bz.j<u.bz.nRoot; u.bz.j++){
   69763             987 :     u.bz.aRoot[u.bz.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bz.j]);
   69764                 :   }
   69765              52 :   u.bz.aRoot[u.bz.j] = 0;
   69766              52 :   assert( pOp->p5<db->nDb );
   69767              52 :   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
   69768              52 :   u.bz.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bz.aRoot, u.bz.nRoot,
   69769              52 :                                  (int)u.bz.pnErr->u.i, &u.bz.nErr);
   69770              52 :   sqlite3DbFree(db, u.bz.aRoot);
   69771              52 :   u.bz.pnErr->u.i -= u.bz.nErr;
   69772              52 :   sqlite3VdbeMemSetNull(pIn1);
   69773              52 :   if( u.bz.nErr==0 ){
   69774              52 :     assert( u.bz.z==0 );
   69775               0 :   }else if( u.bz.z==0 ){
   69776               0 :     goto no_mem;
   69777                 :   }else{
   69778               0 :     sqlite3VdbeMemSetStr(pIn1, u.bz.z, -1, SQLITE_UTF8, sqlite3_free);
   69779                 :   }
   69780                 :   UPDATE_MAX_BLOBSIZE(pIn1);
   69781              52 :   sqlite3VdbeChangeEncoding(pIn1, encoding);
   69782              52 :   break;
   69783                 : }
   69784                 : #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
   69785                 : 
   69786                 : /* Opcode: RowSetAdd P1 P2 * * *
   69787                 : **
   69788                 : ** Insert the integer value held by register P2 into a boolean index
   69789                 : ** held in register P1.
   69790                 : **
   69791                 : ** An assertion fails if P2 is not an integer.
   69792                 : */
   69793                 : case OP_RowSetAdd: {       /* in1, in2 */
   69794           14860 :   pIn1 = &aMem[pOp->p1];
   69795           14860 :   pIn2 = &aMem[pOp->p2];
   69796           14860 :   assert( (pIn2->flags & MEM_Int)!=0 );
   69797           14860 :   if( (pIn1->flags & MEM_RowSet)==0 ){
   69798            6085 :     sqlite3VdbeMemSetRowSet(pIn1);
   69799            6085 :     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
   69800                 :   }
   69801           14860 :   sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
   69802           14860 :   break;
   69803                 : }
   69804                 : 
   69805                 : /* Opcode: RowSetRead P1 P2 P3 * *
   69806                 : **
   69807                 : ** Extract the smallest value from boolean index P1 and put that value into
   69808                 : ** register P3.  Or, if boolean index P1 is initially empty, leave P3
   69809                 : ** unchanged and jump to instruction P2.
   69810                 : */
   69811                 : case OP_RowSetRead: {       /* jump, in1, out3 */
   69812                 : #if 0  /* local variables moved into u.ca */
   69813                 :   i64 val;
   69814                 : #endif /* local variables moved into u.ca */
   69815           35905 :   CHECK_FOR_INTERRUPT;
   69816           35905 :   pIn1 = &aMem[pOp->p1];
   69817           35905 :   if( (pIn1->flags & MEM_RowSet)==0
   69818           20945 :    || sqlite3RowSetNext(pIn1->u.pRowSet, &u.ca.val)==0
   69819                 :   ){
   69820                 :     /* The boolean index is empty */
   69821           21045 :     sqlite3VdbeMemSetNull(pIn1);
   69822           21045 :     pc = pOp->p2 - 1;
   69823                 :   }else{
   69824                 :     /* A value was pulled from the index */
   69825           14860 :     sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.ca.val);
   69826                 :   }
   69827           35905 :   break;
   69828                 : }
   69829                 : 
   69830                 : /* Opcode: RowSetTest P1 P2 P3 P4
   69831                 : **
   69832                 : ** Register P3 is assumed to hold a 64-bit integer value. If register P1
   69833                 : ** contains a RowSet object and that RowSet object contains
   69834                 : ** the value held in P3, jump to register P2. Otherwise, insert the
   69835                 : ** integer in P3 into the RowSet and continue on to the
   69836                 : ** next opcode.
   69837                 : **
   69838                 : ** The RowSet object is optimized for the case where successive sets
   69839                 : ** of integers, where each set contains no duplicates. Each set
   69840                 : ** of values is identified by a unique P4 value. The first set
   69841                 : ** must have P4==0, the final set P4=-1.  P4 must be either -1 or
   69842                 : ** non-negative.  For non-negative values of P4 only the lower 4
   69843                 : ** bits are significant.
   69844                 : **
   69845                 : ** This allows optimizations: (a) when P4==0 there is no need to test
   69846                 : ** the rowset object for P3, as it is guaranteed not to contain it,
   69847                 : ** (b) when P4==-1 there is no need to insert the value, as it will
   69848                 : ** never be tested for, and (c) when a value that is part of set X is
   69849                 : ** inserted, there is no need to search to see if the same value was
   69850                 : ** previously inserted as part of set X (only if it was previously
   69851                 : ** inserted as part of some other set).
   69852                 : */
   69853                 : case OP_RowSetTest: {                     /* jump, in1, in3 */
   69854                 : #if 0  /* local variables moved into u.cb */
   69855                 :   int iSet;
   69856                 :   int exists;
   69857                 : #endif /* local variables moved into u.cb */
   69858                 : 
   69859              35 :   pIn1 = &aMem[pOp->p1];
   69860              35 :   pIn3 = &aMem[pOp->p3];
   69861              35 :   u.cb.iSet = pOp->p4.i;
   69862              35 :   assert( pIn3->flags&MEM_Int );
   69863                 : 
   69864                 :   /* If there is anything other than a rowset object in memory cell P1,
   69865                 :   ** delete it now and initialize P1 with an empty rowset
   69866                 :   */
   69867              35 :   if( (pIn1->flags & MEM_RowSet)==0 ){
   69868               7 :     sqlite3VdbeMemSetRowSet(pIn1);
   69869               7 :     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
   69870                 :   }
   69871                 : 
   69872              35 :   assert( pOp->p4type==P4_INT32 );
   69873              35 :   assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
   69874              35 :   if( u.cb.iSet ){
   69875               4 :     u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
   69876               2 :                                (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
   69877                 :                                pIn3->u.i);
   69878               2 :     if( u.cb.exists ){
   69879               1 :       pc = pOp->p2 - 1;
   69880               1 :       break;
   69881                 :     }
   69882                 :   }
   69883              34 :   if( u.cb.iSet>=0 ){
   69884              33 :     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
   69885                 :   }
   69886              34 :   break;
   69887                 : }
   69888                 : 
   69889                 : 
   69890                 : #ifndef SQLITE_OMIT_TRIGGER
   69891                 : 
   69892                 : /* Opcode: Program P1 P2 P3 P4 *
   69893                 : **
   69894                 : ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). 
   69895                 : **
   69896                 : ** P1 contains the address of the memory cell that contains the first memory 
   69897                 : ** cell in an array of values used as arguments to the sub-program. P2 
   69898                 : ** contains the address to jump to if the sub-program throws an IGNORE 
   69899                 : ** exception using the RAISE() function. Register P3 contains the address 
   69900                 : ** of a memory cell in this (the parent) VM that is used to allocate the 
   69901                 : ** memory required by the sub-vdbe at runtime.
   69902                 : **
   69903                 : ** P4 is a pointer to the VM containing the trigger program.
   69904                 : */
   69905                 : case OP_Program: {        /* jump */
   69906                 : #if 0  /* local variables moved into u.cc */
   69907                 :   int nMem;               /* Number of memory registers for sub-program */
   69908                 :   int nByte;              /* Bytes of runtime space required for sub-program */
   69909                 :   Mem *pRt;               /* Register to allocate runtime space */
   69910                 :   Mem *pMem;              /* Used to iterate through memory cells */
   69911                 :   Mem *pEnd;              /* Last memory cell in new array */
   69912                 :   VdbeFrame *pFrame;      /* New vdbe frame to execute in */
   69913                 :   SubProgram *pProgram;   /* Sub-program to execute */
   69914                 :   void *t;                /* Token identifying trigger */
   69915                 : #endif /* local variables moved into u.cc */
   69916                 : 
   69917           19051 :   u.cc.pProgram = pOp->p4.pProgram;
   69918           19051 :   u.cc.pRt = &aMem[pOp->p3];
   69919           19051 :   assert( u.cc.pProgram->nOp>0 );
   69920                 : 
   69921                 :   /* If the p5 flag is clear, then recursive invocation of triggers is
   69922                 :   ** disabled for backwards compatibility (p5 is set if this sub-program
   69923                 :   ** is really a trigger, not a foreign key action, and the flag set
   69924                 :   ** and cleared by the "PRAGMA recursive_triggers" command is clear).
   69925                 :   **
   69926                 :   ** It is recursive invocation of triggers, at the SQL level, that is
   69927                 :   ** disabled. In some cases a single trigger may generate more than one
   69928                 :   ** SubProgram (if the trigger may be executed with more than one different
   69929                 :   ** ON CONFLICT algorithm). SubProgram structures associated with a
   69930                 :   ** single trigger all have the same value for the SubProgram.token
   69931                 :   ** variable.  */
   69932           19051 :   if( pOp->p5 ){
   69933           16237 :     u.cc.t = u.cc.pProgram->token;
   69934           16237 :     for(u.cc.pFrame=p->pFrame; u.cc.pFrame && u.cc.pFrame->token!=u.cc.t; u.cc.pFrame=u.cc.pFrame->pParent);
   69935           16237 :     if( u.cc.pFrame ) break;
   69936                 :   }
   69937                 : 
   69938           19051 :   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
   69939               0 :     rc = SQLITE_ERROR;
   69940               0 :     sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
   69941               0 :     break;
   69942                 :   }
   69943                 : 
   69944                 :   /* Register u.cc.pRt is used to store the memory required to save the state
   69945                 :   ** of the current program, and the memory required at runtime to execute
   69946                 :   ** the trigger program. If this trigger has been fired before, then u.cc.pRt
   69947                 :   ** is already allocated. Otherwise, it must be initialized.  */
   69948           19051 :   if( (u.cc.pRt->flags&MEM_Frame)==0 ){
   69949                 :     /* SubProgram.nMem is set to the number of memory cells used by the
   69950                 :     ** program stored in SubProgram.aOp. As well as these, one memory
   69951                 :     ** cell is required for each cursor used by the program. Set local
   69952                 :     ** variable u.cc.nMem (and later, VdbeFrame.nChildMem) to this value.
   69953                 :     */
   69954           14229 :     u.cc.nMem = u.cc.pProgram->nMem + u.cc.pProgram->nCsr;
   69955           14229 :     u.cc.nByte = ROUND8(sizeof(VdbeFrame))
   69956           14229 :               + u.cc.nMem * sizeof(Mem)
   69957           14229 :               + u.cc.pProgram->nCsr * sizeof(VdbeCursor *)
   69958           14229 :               + u.cc.pProgram->nOnce * sizeof(u8);
   69959           14229 :     u.cc.pFrame = sqlite3DbMallocZero(db, u.cc.nByte);
   69960           14229 :     if( !u.cc.pFrame ){
   69961               0 :       goto no_mem;
   69962                 :     }
   69963           14229 :     sqlite3VdbeMemRelease(u.cc.pRt);
   69964           14229 :     u.cc.pRt->flags = MEM_Frame;
   69965           14229 :     u.cc.pRt->u.pFrame = u.cc.pFrame;
   69966                 : 
   69967           14229 :     u.cc.pFrame->v = p;
   69968           14229 :     u.cc.pFrame->nChildMem = u.cc.nMem;
   69969           14229 :     u.cc.pFrame->nChildCsr = u.cc.pProgram->nCsr;
   69970           14229 :     u.cc.pFrame->pc = pc;
   69971           14229 :     u.cc.pFrame->aMem = p->aMem;
   69972           14229 :     u.cc.pFrame->nMem = p->nMem;
   69973           14229 :     u.cc.pFrame->apCsr = p->apCsr;
   69974           14229 :     u.cc.pFrame->nCursor = p->nCursor;
   69975           14229 :     u.cc.pFrame->aOp = p->aOp;
   69976           14229 :     u.cc.pFrame->nOp = p->nOp;
   69977           14229 :     u.cc.pFrame->token = u.cc.pProgram->token;
   69978           14229 :     u.cc.pFrame->aOnceFlag = p->aOnceFlag;
   69979           14229 :     u.cc.pFrame->nOnceFlag = p->nOnceFlag;
   69980                 : 
   69981           14229 :     u.cc.pEnd = &VdbeFrameMem(u.cc.pFrame)[u.cc.pFrame->nChildMem];
   69982          406989 :     for(u.cc.pMem=VdbeFrameMem(u.cc.pFrame); u.cc.pMem!=u.cc.pEnd; u.cc.pMem++){
   69983          392760 :       u.cc.pMem->flags = MEM_Invalid;
   69984          392760 :       u.cc.pMem->db = db;
   69985                 :     }
   69986                 :   }else{
   69987            4822 :     u.cc.pFrame = u.cc.pRt->u.pFrame;
   69988            4822 :     assert( u.cc.pProgram->nMem+u.cc.pProgram->nCsr==u.cc.pFrame->nChildMem );
   69989            4822 :     assert( u.cc.pProgram->nCsr==u.cc.pFrame->nChildCsr );
   69990            4822 :     assert( pc==u.cc.pFrame->pc );
   69991                 :   }
   69992                 : 
   69993           19051 :   p->nFrame++;
   69994           19051 :   u.cc.pFrame->pParent = p->pFrame;
   69995           19051 :   u.cc.pFrame->lastRowid = lastRowid;
   69996           19051 :   u.cc.pFrame->nChange = p->nChange;
   69997           19051 :   p->nChange = 0;
   69998           19051 :   p->pFrame = u.cc.pFrame;
   69999           19051 :   p->aMem = aMem = &VdbeFrameMem(u.cc.pFrame)[-1];
   70000           19051 :   p->nMem = u.cc.pFrame->nChildMem;
   70001           19051 :   p->nCursor = (u16)u.cc.pFrame->nChildCsr;
   70002           19051 :   p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
   70003           19051 :   p->aOp = aOp = u.cc.pProgram->aOp;
   70004           19051 :   p->nOp = u.cc.pProgram->nOp;
   70005           19051 :   p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
   70006           19051 :   p->nOnceFlag = u.cc.pProgram->nOnce;
   70007           19051 :   p->nOp = u.cc.pProgram->nOp;
   70008           19051 :   pc = -1;
   70009           19051 :   memset(p->aOnceFlag, 0, p->nOnceFlag);
   70010                 : 
   70011           19051 :   break;
   70012                 : }
   70013                 : 
   70014                 : /* Opcode: Param P1 P2 * * *
   70015                 : **
   70016                 : ** This opcode is only ever present in sub-programs called via the 
   70017                 : ** OP_Program instruction. Copy a value currently stored in a memory 
   70018                 : ** cell of the calling (parent) frame to cell P2 in the current frames 
   70019                 : ** address space. This is used by trigger programs to access the new.* 
   70020                 : ** and old.* values.
   70021                 : **
   70022                 : ** The address of the cell in the parent frame is determined by adding
   70023                 : ** the value of the P1 argument to the value of the P1 argument to the
   70024                 : ** calling OP_Program instruction.
   70025                 : */
   70026                 : case OP_Param: {           /* out2-prerelease */
   70027                 : #if 0  /* local variables moved into u.cd */
   70028                 :   VdbeFrame *pFrame;
   70029                 :   Mem *pIn;
   70030                 : #endif /* local variables moved into u.cd */
   70031           60437 :   u.cd.pFrame = p->pFrame;
   70032           60437 :   u.cd.pIn = &u.cd.pFrame->aMem[pOp->p1 + u.cd.pFrame->aOp[u.cd.pFrame->pc].p1];
   70033           60437 :   sqlite3VdbeMemShallowCopy(pOut, u.cd.pIn, MEM_Ephem);
   70034           60437 :   break;
   70035                 : }
   70036                 : 
   70037                 : #endif /* #ifndef SQLITE_OMIT_TRIGGER */
   70038                 : 
   70039                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   70040                 : /* Opcode: FkCounter P1 P2 * * *
   70041                 : **
   70042                 : ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
   70043                 : ** If P1 is non-zero, the database constraint counter is incremented 
   70044                 : ** (deferred foreign key constraints). Otherwise, if P1 is zero, the 
   70045                 : ** statement counter is incremented (immediate foreign key constraints).
   70046                 : */
   70047                 : case OP_FkCounter: {
   70048             658 :   if( pOp->p1 ){
   70049               0 :     db->nDeferredCons += pOp->p2;
   70050                 :   }else{
   70051             658 :     p->nFkConstraint += pOp->p2;
   70052                 :   }
   70053             658 :   break;
   70054                 : }
   70055                 : 
   70056                 : /* Opcode: FkIfZero P1 P2 * * *
   70057                 : **
   70058                 : ** This opcode tests if a foreign key constraint-counter is currently zero.
   70059                 : ** If so, jump to instruction P2. Otherwise, fall through to the next 
   70060                 : ** instruction.
   70061                 : **
   70062                 : ** If P1 is non-zero, then the jump is taken if the database constraint-counter
   70063                 : ** is zero (the one that counts deferred constraint violations). If P1 is
   70064                 : ** zero, the jump is taken if the statement constraint-counter is zero
   70065                 : ** (immediate foreign key constraint violations).
   70066                 : */
   70067                 : case OP_FkIfZero: {         /* jump */
   70068            5238 :   if( pOp->p1 ){
   70069               0 :     if( db->nDeferredCons==0 ) pc = pOp->p2-1;
   70070                 :   }else{
   70071            5238 :     if( p->nFkConstraint==0 ) pc = pOp->p2-1;
   70072                 :   }
   70073            5238 :   break;
   70074                 : }
   70075                 : #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
   70076                 : 
   70077                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   70078                 : /* Opcode: MemMax P1 P2 * * *
   70079                 : **
   70080                 : ** P1 is a register in the root frame of this VM (the root frame is
   70081                 : ** different from the current frame if this instruction is being executed
   70082                 : ** within a sub-program). Set the value of register P1 to the maximum of 
   70083                 : ** its current value and the value in register P2.
   70084                 : **
   70085                 : ** This instruction throws an error if the memory cell is not initially
   70086                 : ** an integer.
   70087                 : */
   70088                 : case OP_MemMax: {        /* in2 */
   70089                 : #if 0  /* local variables moved into u.ce */
   70090                 :   Mem *pIn1;
   70091                 :   VdbeFrame *pFrame;
   70092                 : #endif /* local variables moved into u.ce */
   70093            2983 :   if( p->pFrame ){
   70094               0 :     for(u.ce.pFrame=p->pFrame; u.ce.pFrame->pParent; u.ce.pFrame=u.ce.pFrame->pParent);
   70095               0 :     u.ce.pIn1 = &u.ce.pFrame->aMem[pOp->p1];
   70096                 :   }else{
   70097            2983 :     u.ce.pIn1 = &aMem[pOp->p1];
   70098                 :   }
   70099            2983 :   assert( memIsValid(u.ce.pIn1) );
   70100            2983 :   sqlite3VdbeMemIntegerify(u.ce.pIn1);
   70101            2983 :   pIn2 = &aMem[pOp->p2];
   70102            2983 :   sqlite3VdbeMemIntegerify(pIn2);
   70103            2983 :   if( u.ce.pIn1->u.i<pIn2->u.i){
   70104               2 :     u.ce.pIn1->u.i = pIn2->u.i;
   70105                 :   }
   70106            2983 :   break;
   70107                 : }
   70108                 : #endif /* SQLITE_OMIT_AUTOINCREMENT */
   70109                 : 
   70110                 : /* Opcode: IfPos P1 P2 * * *
   70111                 : **
   70112                 : ** If the value of register P1 is 1 or greater, jump to P2.
   70113                 : **
   70114                 : ** It is illegal to use this instruction on a register that does
   70115                 : ** not contain an integer.  An assertion fault will result if you try.
   70116                 : */
   70117                 : case OP_IfPos: {        /* jump, in1 */
   70118           45572 :   pIn1 = &aMem[pOp->p1];
   70119           45572 :   assert( pIn1->flags&MEM_Int );
   70120           45572 :   if( pIn1->u.i>0 ){
   70121           25012 :      pc = pOp->p2 - 1;
   70122                 :   }
   70123           45572 :   break;
   70124                 : }
   70125                 : 
   70126                 : /* Opcode: IfNeg P1 P2 * * *
   70127                 : **
   70128                 : ** If the value of register P1 is less than zero, jump to P2. 
   70129                 : **
   70130                 : ** It is illegal to use this instruction on a register that does
   70131                 : ** not contain an integer.  An assertion fault will result if you try.
   70132                 : */
   70133                 : case OP_IfNeg: {        /* jump, in1 */
   70134              26 :   pIn1 = &aMem[pOp->p1];
   70135              26 :   assert( pIn1->flags&MEM_Int );
   70136              26 :   if( pIn1->u.i<0 ){
   70137               0 :      pc = pOp->p2 - 1;
   70138                 :   }
   70139              26 :   break;
   70140                 : }
   70141                 : 
   70142                 : /* Opcode: IfZero P1 P2 P3 * *
   70143                 : **
   70144                 : ** The register P1 must contain an integer.  Add literal P3 to the
   70145                 : ** value in register P1.  If the result is exactly 0, jump to P2. 
   70146                 : **
   70147                 : ** It is illegal to use this instruction on a register that does
   70148                 : ** not contain an integer.  An assertion fault will result if you try.
   70149                 : */
   70150                 : case OP_IfZero: {        /* jump, in1 */
   70151           45783 :   pIn1 = &aMem[pOp->p1];
   70152           45783 :   assert( pIn1->flags&MEM_Int );
   70153           45783 :   pIn1->u.i += pOp->p3;
   70154           45783 :   if( pIn1->u.i==0 ){
   70155           40755 :      pc = pOp->p2 - 1;
   70156                 :   }
   70157           45783 :   break;
   70158                 : }
   70159                 : 
   70160                 : /* Opcode: AggStep * P2 P3 P4 P5
   70161                 : **
   70162                 : ** Execute the step function for an aggregate.  The
   70163                 : ** function has P5 arguments.   P4 is a pointer to the FuncDef
   70164                 : ** structure that specifies the function.  Use register
   70165                 : ** P3 as the accumulator.
   70166                 : **
   70167                 : ** The P5 arguments are taken from register P2 and its
   70168                 : ** successors.
   70169                 : */
   70170                 : case OP_AggStep: {
   70171                 : #if 0  /* local variables moved into u.cf */
   70172                 :   int n;
   70173                 :   int i;
   70174                 :   Mem *pMem;
   70175                 :   Mem *pRec;
   70176                 :   sqlite3_context ctx;
   70177                 :   sqlite3_value **apVal;
   70178                 : #endif /* local variables moved into u.cf */
   70179                 : 
   70180          774418 :   u.cf.n = pOp->p5;
   70181          774418 :   assert( u.cf.n>=0 );
   70182          774418 :   u.cf.pRec = &aMem[pOp->p2];
   70183          774418 :   u.cf.apVal = p->apArg;
   70184          774418 :   assert( u.cf.apVal || u.cf.n==0 );
   70185         1516406 :   for(u.cf.i=0; u.cf.i<u.cf.n; u.cf.i++, u.cf.pRec++){
   70186          741988 :     assert( memIsValid(u.cf.pRec) );
   70187          741988 :     u.cf.apVal[u.cf.i] = u.cf.pRec;
   70188          741988 :     memAboutToChange(p, u.cf.pRec);
   70189          741988 :     sqlite3VdbeMemStoreType(u.cf.pRec);
   70190                 :   }
   70191          774418 :   u.cf.ctx.pFunc = pOp->p4.pFunc;
   70192          774418 :   assert( pOp->p3>0 && pOp->p3<=p->nMem );
   70193          774418 :   u.cf.ctx.pMem = u.cf.pMem = &aMem[pOp->p3];
   70194          774418 :   u.cf.pMem->n++;
   70195          774418 :   u.cf.ctx.s.flags = MEM_Null;
   70196          774418 :   u.cf.ctx.s.z = 0;
   70197          774418 :   u.cf.ctx.s.zMalloc = 0;
   70198          774418 :   u.cf.ctx.s.xDel = 0;
   70199          774418 :   u.cf.ctx.s.db = db;
   70200          774418 :   u.cf.ctx.isError = 0;
   70201          774418 :   u.cf.ctx.pColl = 0;
   70202          774418 :   if( u.cf.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
   70203           43892 :     assert( pOp>p->aOp );
   70204           43892 :     assert( pOp[-1].p4type==P4_COLLSEQ );
   70205           43892 :     assert( pOp[-1].opcode==OP_CollSeq );
   70206           43892 :     u.cf.ctx.pColl = pOp[-1].p4.pColl;
   70207                 :   }
   70208          774418 :   (u.cf.ctx.pFunc->xStep)(&u.cf.ctx, u.cf.n, u.cf.apVal); /* IMP: R-24505-23230 */
   70209          774418 :   if( u.cf.ctx.isError ){
   70210               0 :     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cf.ctx.s));
   70211               0 :     rc = u.cf.ctx.isError;
   70212                 :   }
   70213                 : 
   70214          774418 :   sqlite3VdbeMemRelease(&u.cf.ctx.s);
   70215                 : 
   70216          774418 :   break;
   70217                 : }
   70218                 : 
   70219                 : /* Opcode: AggFinal P1 P2 * P4 *
   70220                 : **
   70221                 : ** Execute the finalizer function for an aggregate.  P1 is
   70222                 : ** the memory location that is the accumulator for the aggregate.
   70223                 : **
   70224                 : ** P2 is the number of arguments that the step function takes and
   70225                 : ** P4 is a pointer to the FuncDef for this function.  The P2
   70226                 : ** argument is not used by this opcode.  It is only there to disambiguate
   70227                 : ** functions that can take varying numbers of arguments.  The
   70228                 : ** P4 argument is only needed for the degenerate case where
   70229                 : ** the step function was not previously called.
   70230                 : */
   70231                 : case OP_AggFinal: {
   70232                 : #if 0  /* local variables moved into u.cg */
   70233                 :   Mem *pMem;
   70234                 : #endif /* local variables moved into u.cg */
   70235           17260 :   assert( pOp->p1>0 && pOp->p1<=p->nMem );
   70236           17260 :   u.cg.pMem = &aMem[pOp->p1];
   70237           17260 :   assert( (u.cg.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
   70238           17260 :   rc = sqlite3VdbeMemFinalize(u.cg.pMem, pOp->p4.pFunc);
   70239           17260 :   if( rc ){
   70240               0 :     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cg.pMem));
   70241                 :   }
   70242           17260 :   sqlite3VdbeChangeEncoding(u.cg.pMem, encoding);
   70243                 :   UPDATE_MAX_BLOBSIZE(u.cg.pMem);
   70244           17260 :   if( sqlite3VdbeMemTooBig(u.cg.pMem) ){
   70245               0 :     goto too_big;
   70246                 :   }
   70247           17260 :   break;
   70248                 : }
   70249                 : 
   70250                 : #ifndef SQLITE_OMIT_WAL
   70251                 : /* Opcode: Checkpoint P1 P2 P3 * *
   70252                 : **
   70253                 : ** Checkpoint database P1. This is a no-op if P1 is not currently in
   70254                 : ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
   70255                 : ** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
   70256                 : ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
   70257                 : ** WAL after the checkpoint into mem[P3+1] and the number of pages
   70258                 : ** in the WAL that have been checkpointed after the checkpoint
   70259                 : ** completes into mem[P3+2].  However on an error, mem[P3+1] and
   70260                 : ** mem[P3+2] are initialized to -1.
   70261                 : */
   70262                 : case OP_Checkpoint: {
   70263                 : #if 0  /* local variables moved into u.ch */
   70264                 :   int i;                          /* Loop counter */
   70265                 :   int aRes[3];                    /* Results */
   70266                 :   Mem *pMem;                      /* Write results here */
   70267                 : #endif /* local variables moved into u.ch */
   70268                 : 
   70269             265 :   u.ch.aRes[0] = 0;
   70270             265 :   u.ch.aRes[1] = u.ch.aRes[2] = -1;
   70271             265 :   assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
   70272                 :        || pOp->p2==SQLITE_CHECKPOINT_FULL
   70273                 :        || pOp->p2==SQLITE_CHECKPOINT_RESTART
   70274                 :   );
   70275             265 :   rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ch.aRes[1], &u.ch.aRes[2]);
   70276             265 :   if( rc==SQLITE_BUSY ){
   70277               0 :     rc = SQLITE_OK;
   70278               0 :     u.ch.aRes[0] = 1;
   70279                 :   }
   70280            1060 :   for(u.ch.i=0, u.ch.pMem = &aMem[pOp->p3]; u.ch.i<3; u.ch.i++, u.ch.pMem++){
   70281             795 :     sqlite3VdbeMemSetInt64(u.ch.pMem, (i64)u.ch.aRes[u.ch.i]);
   70282                 :   }
   70283             265 :   break;
   70284                 : };  
   70285                 : #endif
   70286                 : 
   70287                 : #ifndef SQLITE_OMIT_PRAGMA
   70288                 : /* Opcode: JournalMode P1 P2 P3 * P5
   70289                 : **
   70290                 : ** Change the journal mode of database P1 to P3. P3 must be one of the
   70291                 : ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
   70292                 : ** modes (delete, truncate, persist, off and memory), this is a simple
   70293                 : ** operation. No IO is required.
   70294                 : **
   70295                 : ** If changing into or out of WAL mode the procedure is more complicated.
   70296                 : **
   70297                 : ** Write a string containing the final journal-mode to register P2.
   70298                 : */
   70299                 : case OP_JournalMode: {    /* out2-prerelease */
   70300                 : #if 0  /* local variables moved into u.ci */
   70301                 :   Btree *pBt;                     /* Btree to change journal mode of */
   70302                 :   Pager *pPager;                  /* Pager associated with pBt */
   70303                 :   int eNew;                       /* New journal mode */
   70304                 :   int eOld;                       /* The old journal mode */
   70305                 :   const char *zFilename;          /* Name of database file for pPager */
   70306                 : #endif /* local variables moved into u.ci */
   70307                 : 
   70308             517 :   u.ci.eNew = pOp->p3;
   70309             517 :   assert( u.ci.eNew==PAGER_JOURNALMODE_DELETE
   70310                 :        || u.ci.eNew==PAGER_JOURNALMODE_TRUNCATE
   70311                 :        || u.ci.eNew==PAGER_JOURNALMODE_PERSIST
   70312                 :        || u.ci.eNew==PAGER_JOURNALMODE_OFF
   70313                 :        || u.ci.eNew==PAGER_JOURNALMODE_MEMORY
   70314                 :        || u.ci.eNew==PAGER_JOURNALMODE_WAL
   70315                 :        || u.ci.eNew==PAGER_JOURNALMODE_QUERY
   70316                 :   );
   70317             517 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
   70318                 : 
   70319             517 :   u.ci.pBt = db->aDb[pOp->p1].pBt;
   70320             517 :   u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
   70321             517 :   u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
   70322             517 :   if( u.ci.eNew==PAGER_JOURNALMODE_QUERY ) u.ci.eNew = u.ci.eOld;
   70323             517 :   if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
   70324                 : 
   70325                 : #ifndef SQLITE_OMIT_WAL
   70326             517 :   u.ci.zFilename = sqlite3PagerFilename(u.ci.pPager);
   70327                 : 
   70328                 :   /* Do not allow a transition to journal_mode=WAL for a database
   70329                 :   ** in temporary storage or if the VFS does not support shared memory
   70330                 :   */
   70331             517 :   if( u.ci.eNew==PAGER_JOURNALMODE_WAL
   70332             514 :    && (sqlite3Strlen30(u.ci.zFilename)==0           /* Temp file */
   70333             514 :        || !sqlite3PagerWalSupported(u.ci.pPager))   /* No shared-memory support */
   70334                 :   ){
   70335               0 :     u.ci.eNew = u.ci.eOld;
   70336                 :   }
   70337                 : 
   70338             517 :   if( (u.ci.eNew!=u.ci.eOld)
   70339             476 :    && (u.ci.eOld==PAGER_JOURNALMODE_WAL || u.ci.eNew==PAGER_JOURNALMODE_WAL)
   70340                 :   ){
   70341             476 :     if( !db->autoCommit || db->activeVdbeCnt>1 ){
   70342               0 :       rc = SQLITE_ERROR;
   70343               0 :       sqlite3SetString(&p->zErrMsg, db,
   70344                 :           "cannot change %s wal mode from within a transaction",
   70345               0 :           (u.ci.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
   70346                 :       );
   70347               0 :       break;
   70348                 :     }else{
   70349                 : 
   70350             476 :       if( u.ci.eOld==PAGER_JOURNALMODE_WAL ){
   70351                 :         /* If leaving WAL mode, close the log file. If successful, the call
   70352                 :         ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
   70353                 :         ** file. An EXCLUSIVE lock may still be held on the database file
   70354                 :         ** after a successful return.
   70355                 :         */
   70356               0 :         rc = sqlite3PagerCloseWal(u.ci.pPager);
   70357               0 :         if( rc==SQLITE_OK ){
   70358               0 :           sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
   70359                 :         }
   70360             476 :       }else if( u.ci.eOld==PAGER_JOURNALMODE_MEMORY ){
   70361                 :         /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
   70362                 :         ** as an intermediate */
   70363               0 :         sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
   70364                 :       }
   70365                 : 
   70366                 :       /* Open a transaction on the database file. Regardless of the journal
   70367                 :       ** mode, this transaction always uses a rollback journal.
   70368                 :       */
   70369             476 :       assert( sqlite3BtreeIsInTrans(u.ci.pBt)==0 );
   70370             476 :       if( rc==SQLITE_OK ){
   70371             476 :         rc = sqlite3BtreeSetVersion(u.ci.pBt, (u.ci.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
   70372                 :       }
   70373                 :     }
   70374                 :   }
   70375                 : #endif /* ifndef SQLITE_OMIT_WAL */
   70376                 : 
   70377             517 :   if( rc ){
   70378               0 :     u.ci.eNew = u.ci.eOld;
   70379                 :   }
   70380             517 :   u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
   70381                 : 
   70382             517 :   pOut = &aMem[pOp->p2];
   70383             517 :   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
   70384             517 :   pOut->z = (char *)sqlite3JournalModename(u.ci.eNew);
   70385             517 :   pOut->n = sqlite3Strlen30(pOut->z);
   70386             517 :   pOut->enc = SQLITE_UTF8;
   70387             517 :   sqlite3VdbeChangeEncoding(pOut, encoding);
   70388             517 :   break;
   70389                 : };
   70390                 : #endif /* SQLITE_OMIT_PRAGMA */
   70391                 : 
   70392                 : #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
   70393                 : /* Opcode: Vacuum * * * * *
   70394                 : **
   70395                 : ** Vacuum the entire database.  This opcode will cause other virtual
   70396                 : ** machines to be created and run.  It may not be called from within
   70397                 : ** a transaction.
   70398                 : */
   70399                 : case OP_Vacuum: {
   70400               6 :   rc = sqlite3RunVacuum(&p->zErrMsg, db);
   70401               6 :   break;
   70402                 : }
   70403                 : #endif
   70404                 : 
   70405                 : #if !defined(SQLITE_OMIT_AUTOVACUUM)
   70406                 : /* Opcode: IncrVacuum P1 P2 * * *
   70407                 : **
   70408                 : ** Perform a single step of the incremental vacuum procedure on
   70409                 : ** the P1 database. If the vacuum has finished, jump to instruction
   70410                 : ** P2. Otherwise, fall through to the next instruction.
   70411                 : */
   70412                 : case OP_IncrVacuum: {        /* jump */
   70413                 : #if 0  /* local variables moved into u.cj */
   70414                 :   Btree *pBt;
   70415                 : #endif /* local variables moved into u.cj */
   70416                 : 
   70417               0 :   assert( pOp->p1>=0 && pOp->p1<db->nDb );
   70418               0 :   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
   70419               0 :   u.cj.pBt = db->aDb[pOp->p1].pBt;
   70420               0 :   rc = sqlite3BtreeIncrVacuum(u.cj.pBt);
   70421               0 :   if( rc==SQLITE_DONE ){
   70422               0 :     pc = pOp->p2 - 1;
   70423               0 :     rc = SQLITE_OK;
   70424                 :   }
   70425               0 :   break;
   70426                 : }
   70427                 : #endif
   70428                 : 
   70429                 : /* Opcode: Expire P1 * * * *
   70430                 : **
   70431                 : ** Cause precompiled statements to become expired. An expired statement
   70432                 : ** fails with an error code of SQLITE_SCHEMA if it is ever executed 
   70433                 : ** (via sqlite3_step()).
   70434                 : ** 
   70435                 : ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
   70436                 : ** then only the currently executing statement is affected. 
   70437                 : */
   70438                 : case OP_Expire: {
   70439            6245 :   if( !pOp->p1 ){
   70440            6239 :     sqlite3ExpirePreparedStatements(db);
   70441                 :   }else{
   70442               6 :     p->expired = 1;
   70443                 :   }
   70444            6245 :   break;
   70445                 : }
   70446                 : 
   70447                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   70448                 : /* Opcode: TableLock P1 P2 P3 P4 *
   70449                 : **
   70450                 : ** Obtain a lock on a particular table. This instruction is only used when
   70451                 : ** the shared-cache feature is enabled. 
   70452                 : **
   70453                 : ** P1 is the index of the database in sqlite3.aDb[] of the database
   70454                 : ** on which the lock is acquired.  A readlock is obtained if P3==0 or
   70455                 : ** a write lock if P3==1.
   70456                 : **
   70457                 : ** P2 contains the root-page of the table to lock.
   70458                 : **
   70459                 : ** P4 contains a pointer to the name of the table being locked. This is only
   70460                 : ** used to generate an error message if the lock cannot be obtained.
   70461                 : */
   70462                 : case OP_TableLock: {
   70463          242705 :   u8 isWriteLock = (u8)pOp->p3;
   70464          242705 :   if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
   70465          242705 :     int p1 = pOp->p1; 
   70466          242705 :     assert( p1>=0 && p1<db->nDb );
   70467          242705 :     assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
   70468          242705 :     assert( isWriteLock==0 || isWriteLock==1 );
   70469          242705 :     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
   70470          242705 :     if( (rc&0xFF)==SQLITE_LOCKED ){
   70471             298 :       const char *z = pOp->p4.z;
   70472             298 :       sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
   70473                 :     }
   70474                 :   }
   70475          242705 :   break;
   70476                 : }
   70477                 : #endif /* SQLITE_OMIT_SHARED_CACHE */
   70478                 : 
   70479                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70480                 : /* Opcode: VBegin * * * P4 *
   70481                 : **
   70482                 : ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the 
   70483                 : ** xBegin method for that table.
   70484                 : **
   70485                 : ** Also, whether or not P4 is set, check that this is not being called from
   70486                 : ** within a callback to a virtual table xSync() method. If it is, the error
   70487                 : ** code will be set to SQLITE_LOCKED.
   70488                 : */
   70489                 : case OP_VBegin: {
   70490                 : #if 0  /* local variables moved into u.ck */
   70491                 :   VTable *pVTab;
   70492                 : #endif /* local variables moved into u.ck */
   70493             109 :   u.ck.pVTab = pOp->p4.pVtab;
   70494             109 :   rc = sqlite3VtabBegin(db, u.ck.pVTab);
   70495             109 :   if( u.ck.pVTab ) importVtabErrMsg(p, u.ck.pVTab->pVtab);
   70496             109 :   break;
   70497                 : }
   70498                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70499                 : 
   70500                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70501                 : /* Opcode: VCreate P1 * * P4 *
   70502                 : **
   70503                 : ** P4 is the name of a virtual table in database P1. Call the xCreate method
   70504                 : ** for that table.
   70505                 : */
   70506                 : case OP_VCreate: {
   70507              53 :   rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
   70508              53 :   break;
   70509                 : }
   70510                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70511                 : 
   70512                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70513                 : /* Opcode: VDestroy P1 * * P4 *
   70514                 : **
   70515                 : ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
   70516                 : ** of that table.
   70517                 : */
   70518                 : case OP_VDestroy: {
   70519              52 :   p->inVtabMethod = 2;
   70520              52 :   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
   70521              52 :   p->inVtabMethod = 0;
   70522              52 :   break;
   70523                 : }
   70524                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70525                 : 
   70526                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70527                 : /* Opcode: VOpen P1 * * P4 *
   70528                 : **
   70529                 : ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
   70530                 : ** P1 is a cursor number.  This opcode opens a cursor to the virtual
   70531                 : ** table and stores that cursor in P1.
   70532                 : */
   70533                 : case OP_VOpen: {
   70534                 : #if 0  /* local variables moved into u.cl */
   70535                 :   VdbeCursor *pCur;
   70536                 :   sqlite3_vtab_cursor *pVtabCursor;
   70537                 :   sqlite3_vtab *pVtab;
   70538                 :   sqlite3_module *pModule;
   70539                 : #endif /* local variables moved into u.cl */
   70540                 : 
   70541              54 :   u.cl.pCur = 0;
   70542              54 :   u.cl.pVtabCursor = 0;
   70543              54 :   u.cl.pVtab = pOp->p4.pVtab->pVtab;
   70544              54 :   u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
   70545              54 :   assert(u.cl.pVtab && u.cl.pModule);
   70546              54 :   rc = u.cl.pModule->xOpen(u.cl.pVtab, &u.cl.pVtabCursor);
   70547              54 :   importVtabErrMsg(p, u.cl.pVtab);
   70548              54 :   if( SQLITE_OK==rc ){
   70549                 :     /* Initialize sqlite3_vtab_cursor base class */
   70550              54 :     u.cl.pVtabCursor->pVtab = u.cl.pVtab;
   70551                 : 
   70552                 :     /* Initialise vdbe cursor object */
   70553              54 :     u.cl.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
   70554              54 :     if( u.cl.pCur ){
   70555              54 :       u.cl.pCur->pVtabCursor = u.cl.pVtabCursor;
   70556              54 :       u.cl.pCur->pModule = u.cl.pVtabCursor->pVtab->pModule;
   70557                 :     }else{
   70558               0 :       db->mallocFailed = 1;
   70559               0 :       u.cl.pModule->xClose(u.cl.pVtabCursor);
   70560                 :     }
   70561                 :   }
   70562              54 :   break;
   70563                 : }
   70564                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70565                 : 
   70566                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70567                 : /* Opcode: VFilter P1 P2 P3 P4 *
   70568                 : **
   70569                 : ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
   70570                 : ** the filtered result set is empty.
   70571                 : **
   70572                 : ** P4 is either NULL or a string that was generated by the xBestIndex
   70573                 : ** method of the module.  The interpretation of the P4 string is left
   70574                 : ** to the module implementation.
   70575                 : **
   70576                 : ** This opcode invokes the xFilter method on the virtual table specified
   70577                 : ** by P1.  The integer query plan parameter to xFilter is stored in register
   70578                 : ** P3. Register P3+1 stores the argc parameter to be passed to the
   70579                 : ** xFilter method. Registers P3+2..P3+1+argc are the argc
   70580                 : ** additional parameters which are passed to
   70581                 : ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
   70582                 : **
   70583                 : ** A jump is made to P2 if the result set after filtering would be empty.
   70584                 : */
   70585                 : case OP_VFilter: {   /* jump */
   70586                 : #if 0  /* local variables moved into u.cm */
   70587                 :   int nArg;
   70588                 :   int iQuery;
   70589                 :   const sqlite3_module *pModule;
   70590                 :   Mem *pQuery;
   70591                 :   Mem *pArgc;
   70592                 :   sqlite3_vtab_cursor *pVtabCursor;
   70593                 :   sqlite3_vtab *pVtab;
   70594                 :   VdbeCursor *pCur;
   70595                 :   int res;
   70596                 :   int i;
   70597                 :   Mem **apArg;
   70598                 : #endif /* local variables moved into u.cm */
   70599                 : 
   70600              54 :   u.cm.pQuery = &aMem[pOp->p3];
   70601              54 :   u.cm.pArgc = &u.cm.pQuery[1];
   70602              54 :   u.cm.pCur = p->apCsr[pOp->p1];
   70603              54 :   assert( memIsValid(u.cm.pQuery) );
   70604              54 :   REGISTER_TRACE(pOp->p3, u.cm.pQuery);
   70605              54 :   assert( u.cm.pCur->pVtabCursor );
   70606              54 :   u.cm.pVtabCursor = u.cm.pCur->pVtabCursor;
   70607              54 :   u.cm.pVtab = u.cm.pVtabCursor->pVtab;
   70608              54 :   u.cm.pModule = u.cm.pVtab->pModule;
   70609                 : 
   70610                 :   /* Grab the index number and argc parameters */
   70611              54 :   assert( (u.cm.pQuery->flags&MEM_Int)!=0 && u.cm.pArgc->flags==MEM_Int );
   70612              54 :   u.cm.nArg = (int)u.cm.pArgc->u.i;
   70613              54 :   u.cm.iQuery = (int)u.cm.pQuery->u.i;
   70614                 : 
   70615                 :   /* Invoke the xFilter method */
   70616                 :   {
   70617              54 :     u.cm.res = 0;
   70618              54 :     u.cm.apArg = p->apArg;
   70619             107 :     for(u.cm.i = 0; u.cm.i<u.cm.nArg; u.cm.i++){
   70620              53 :       u.cm.apArg[u.cm.i] = &u.cm.pArgc[u.cm.i+1];
   70621              53 :       sqlite3VdbeMemStoreType(u.cm.apArg[u.cm.i]);
   70622                 :     }
   70623                 : 
   70624              54 :     p->inVtabMethod = 1;
   70625              54 :     rc = u.cm.pModule->xFilter(u.cm.pVtabCursor, u.cm.iQuery, pOp->p4.z, u.cm.nArg, u.cm.apArg);
   70626              54 :     p->inVtabMethod = 0;
   70627              54 :     importVtabErrMsg(p, u.cm.pVtab);
   70628              54 :     if( rc==SQLITE_OK ){
   70629              54 :       u.cm.res = u.cm.pModule->xEof(u.cm.pVtabCursor);
   70630                 :     }
   70631                 : 
   70632              54 :     if( u.cm.res ){
   70633              52 :       pc = pOp->p2 - 1;
   70634                 :     }
   70635                 :   }
   70636              54 :   u.cm.pCur->nullRow = 0;
   70637                 : 
   70638              54 :   break;
   70639                 : }
   70640                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70641                 : 
   70642                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70643                 : /* Opcode: VColumn P1 P2 P3 * *
   70644                 : **
   70645                 : ** Store the value of the P2-th column of
   70646                 : ** the row of the virtual-table that the 
   70647                 : ** P1 cursor is pointing to into register P3.
   70648                 : */
   70649                 : case OP_VColumn: {
   70650                 : #if 0  /* local variables moved into u.cn */
   70651                 :   sqlite3_vtab *pVtab;
   70652                 :   const sqlite3_module *pModule;
   70653                 :   Mem *pDest;
   70654                 :   sqlite3_context sContext;
   70655                 : #endif /* local variables moved into u.cn */
   70656                 : 
   70657               4 :   VdbeCursor *pCur = p->apCsr[pOp->p1];
   70658               4 :   assert( pCur->pVtabCursor );
   70659               4 :   assert( pOp->p3>0 && pOp->p3<=p->nMem );
   70660               4 :   u.cn.pDest = &aMem[pOp->p3];
   70661               4 :   memAboutToChange(p, u.cn.pDest);
   70662               4 :   if( pCur->nullRow ){
   70663               0 :     sqlite3VdbeMemSetNull(u.cn.pDest);
   70664               0 :     break;
   70665                 :   }
   70666               4 :   u.cn.pVtab = pCur->pVtabCursor->pVtab;
   70667               4 :   u.cn.pModule = u.cn.pVtab->pModule;
   70668               4 :   assert( u.cn.pModule->xColumn );
   70669               4 :   memset(&u.cn.sContext, 0, sizeof(u.cn.sContext));
   70670                 : 
   70671                 :   /* The output cell may already have a buffer allocated. Move
   70672                 :   ** the current contents to u.cn.sContext.s so in case the user-function
   70673                 :   ** can use the already allocated buffer instead of allocating a
   70674                 :   ** new one.
   70675                 :   */
   70676               4 :   sqlite3VdbeMemMove(&u.cn.sContext.s, u.cn.pDest);
   70677               4 :   MemSetTypeFlag(&u.cn.sContext.s, MEM_Null);
   70678                 : 
   70679               4 :   rc = u.cn.pModule->xColumn(pCur->pVtabCursor, &u.cn.sContext, pOp->p2);
   70680               4 :   importVtabErrMsg(p, u.cn.pVtab);
   70681               4 :   if( u.cn.sContext.isError ){
   70682               0 :     rc = u.cn.sContext.isError;
   70683                 :   }
   70684                 : 
   70685                 :   /* Copy the result of the function to the P3 register. We
   70686                 :   ** do this regardless of whether or not an error occurred to ensure any
   70687                 :   ** dynamic allocation in u.cn.sContext.s (a Mem struct) is  released.
   70688                 :   */
   70689               4 :   sqlite3VdbeChangeEncoding(&u.cn.sContext.s, encoding);
   70690               4 :   sqlite3VdbeMemMove(u.cn.pDest, &u.cn.sContext.s);
   70691               4 :   REGISTER_TRACE(pOp->p3, u.cn.pDest);
   70692                 :   UPDATE_MAX_BLOBSIZE(u.cn.pDest);
   70693                 : 
   70694               4 :   if( sqlite3VdbeMemTooBig(u.cn.pDest) ){
   70695               0 :     goto too_big;
   70696                 :   }
   70697               4 :   break;
   70698                 : }
   70699                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70700                 : 
   70701                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70702                 : /* Opcode: VNext P1 P2 * * *
   70703                 : **
   70704                 : ** Advance virtual table P1 to the next row in its result set and
   70705                 : ** jump to instruction P2.  Or, if the virtual table has reached
   70706                 : ** the end of its result set, then fall through to the next instruction.
   70707                 : */
   70708                 : case OP_VNext: {   /* jump */
   70709                 : #if 0  /* local variables moved into u.co */
   70710                 :   sqlite3_vtab *pVtab;
   70711                 :   const sqlite3_module *pModule;
   70712                 :   int res;
   70713                 :   VdbeCursor *pCur;
   70714                 : #endif /* local variables moved into u.co */
   70715                 : 
   70716               6 :   u.co.res = 0;
   70717               6 :   u.co.pCur = p->apCsr[pOp->p1];
   70718               6 :   assert( u.co.pCur->pVtabCursor );
   70719               6 :   if( u.co.pCur->nullRow ){
   70720               0 :     break;
   70721                 :   }
   70722               6 :   u.co.pVtab = u.co.pCur->pVtabCursor->pVtab;
   70723               6 :   u.co.pModule = u.co.pVtab->pModule;
   70724               6 :   assert( u.co.pModule->xNext );
   70725                 : 
   70726                 :   /* Invoke the xNext() method of the module. There is no way for the
   70727                 :   ** underlying implementation to return an error if one occurs during
   70728                 :   ** xNext(). Instead, if an error occurs, true is returned (indicating that
   70729                 :   ** data is available) and the error code returned when xColumn or
   70730                 :   ** some other method is next invoked on the save virtual table cursor.
   70731                 :   */
   70732               6 :   p->inVtabMethod = 1;
   70733               6 :   rc = u.co.pModule->xNext(u.co.pCur->pVtabCursor);
   70734               6 :   p->inVtabMethod = 0;
   70735               6 :   importVtabErrMsg(p, u.co.pVtab);
   70736               6 :   if( rc==SQLITE_OK ){
   70737               6 :     u.co.res = u.co.pModule->xEof(u.co.pCur->pVtabCursor);
   70738                 :   }
   70739                 : 
   70740               6 :   if( !u.co.res ){
   70741                 :     /* If there is data, jump to P2 */
   70742               4 :     pc = pOp->p2 - 1;
   70743                 :   }
   70744               6 :   break;
   70745                 : }
   70746                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70747                 : 
   70748                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70749                 : /* Opcode: VRename P1 * * P4 *
   70750                 : **
   70751                 : ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
   70752                 : ** This opcode invokes the corresponding xRename method. The value
   70753                 : ** in register P1 is passed as the zName argument to the xRename method.
   70754                 : */
   70755                 : case OP_VRename: {
   70756                 : #if 0  /* local variables moved into u.cp */
   70757                 :   sqlite3_vtab *pVtab;
   70758                 :   Mem *pName;
   70759                 : #endif /* local variables moved into u.cp */
   70760                 : 
   70761               0 :   u.cp.pVtab = pOp->p4.pVtab->pVtab;
   70762               0 :   u.cp.pName = &aMem[pOp->p1];
   70763               0 :   assert( u.cp.pVtab->pModule->xRename );
   70764               0 :   assert( memIsValid(u.cp.pName) );
   70765               0 :   REGISTER_TRACE(pOp->p1, u.cp.pName);
   70766               0 :   assert( u.cp.pName->flags & MEM_Str );
   70767                 :   testcase( u.cp.pName->enc==SQLITE_UTF8 );
   70768                 :   testcase( u.cp.pName->enc==SQLITE_UTF16BE );
   70769                 :   testcase( u.cp.pName->enc==SQLITE_UTF16LE );
   70770               0 :   rc = sqlite3VdbeChangeEncoding(u.cp.pName, SQLITE_UTF8);
   70771               0 :   if( rc==SQLITE_OK ){
   70772               0 :     rc = u.cp.pVtab->pModule->xRename(u.cp.pVtab, u.cp.pName->z);
   70773               0 :     importVtabErrMsg(p, u.cp.pVtab);
   70774               0 :     p->expired = 0;
   70775                 :   }
   70776               0 :   break;
   70777                 : }
   70778                 : #endif
   70779                 : 
   70780                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   70781                 : /* Opcode: VUpdate P1 P2 P3 P4 *
   70782                 : **
   70783                 : ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
   70784                 : ** This opcode invokes the corresponding xUpdate method. P2 values
   70785                 : ** are contiguous memory cells starting at P3 to pass to the xUpdate 
   70786                 : ** invocation. The value in register (P3+P2-1) corresponds to the 
   70787                 : ** p2th element of the argv array passed to xUpdate.
   70788                 : **
   70789                 : ** The xUpdate method will do a DELETE or an INSERT or both.
   70790                 : ** The argv[0] element (which corresponds to memory cell P3)
   70791                 : ** is the rowid of a row to delete.  If argv[0] is NULL then no 
   70792                 : ** deletion occurs.  The argv[1] element is the rowid of the new 
   70793                 : ** row.  This can be NULL to have the virtual table select the new 
   70794                 : ** rowid for itself.  The subsequent elements in the array are 
   70795                 : ** the values of columns in the new row.
   70796                 : **
   70797                 : ** If P2==1 then no insert is performed.  argv[0] is the rowid of
   70798                 : ** a row to delete.
   70799                 : **
   70800                 : ** P1 is a boolean flag. If it is set to true and the xUpdate call
   70801                 : ** is successful, then the value returned by sqlite3_last_insert_rowid() 
   70802                 : ** is set to the value of the rowid for the row just inserted.
   70803                 : */
   70804                 : case OP_VUpdate: {
   70805                 : #if 0  /* local variables moved into u.cq */
   70806                 :   sqlite3_vtab *pVtab;
   70807                 :   sqlite3_module *pModule;
   70808                 :   int nArg;
   70809                 :   int i;
   70810                 :   sqlite_int64 rowid;
   70811                 :   Mem **apArg;
   70812                 :   Mem *pX;
   70813                 : #endif /* local variables moved into u.cq */
   70814                 : 
   70815               4 :   assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
   70816                 :        || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
   70817                 :   );
   70818               4 :   u.cq.pVtab = pOp->p4.pVtab->pVtab;
   70819               4 :   u.cq.pModule = (sqlite3_module *)u.cq.pVtab->pModule;
   70820               4 :   u.cq.nArg = pOp->p2;
   70821               4 :   assert( pOp->p4type==P4_VTAB );
   70822               4 :   if( ALWAYS(u.cq.pModule->xUpdate) ){
   70823               4 :     u8 vtabOnConflict = db->vtabOnConflict;
   70824               4 :     u.cq.apArg = p->apArg;
   70825               4 :     u.cq.pX = &aMem[pOp->p3];
   70826              28 :     for(u.cq.i=0; u.cq.i<u.cq.nArg; u.cq.i++){
   70827              24 :       assert( memIsValid(u.cq.pX) );
   70828              24 :       memAboutToChange(p, u.cq.pX);
   70829              24 :       sqlite3VdbeMemStoreType(u.cq.pX);
   70830              24 :       u.cq.apArg[u.cq.i] = u.cq.pX;
   70831              24 :       u.cq.pX++;
   70832                 :     }
   70833               4 :     db->vtabOnConflict = pOp->p5;
   70834               4 :     rc = u.cq.pModule->xUpdate(u.cq.pVtab, u.cq.nArg, u.cq.apArg, &u.cq.rowid);
   70835               4 :     db->vtabOnConflict = vtabOnConflict;
   70836               4 :     importVtabErrMsg(p, u.cq.pVtab);
   70837               4 :     if( rc==SQLITE_OK && pOp->p1 ){
   70838               4 :       assert( u.cq.nArg>1 && u.cq.apArg[0] && (u.cq.apArg[0]->flags&MEM_Null) );
   70839               4 :       db->lastRowid = lastRowid = u.cq.rowid;
   70840                 :     }
   70841               4 :     if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
   70842               0 :       if( pOp->p5==OE_Ignore ){
   70843               0 :         rc = SQLITE_OK;
   70844                 :       }else{
   70845               0 :         p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
   70846                 :       }
   70847                 :     }else{
   70848               4 :       p->nChange++;
   70849                 :     }
   70850                 :   }
   70851               4 :   break;
   70852                 : }
   70853                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
   70854                 : 
   70855                 : #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
   70856                 : /* Opcode: Pagecount P1 P2 * * *
   70857                 : **
   70858                 : ** Write the current number of pages in database P1 to memory cell P2.
   70859                 : */
   70860                 : case OP_Pagecount: {            /* out2-prerelease */
   70861               1 :   pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
   70862               1 :   break;
   70863                 : }
   70864                 : #endif
   70865                 : 
   70866                 : 
   70867                 : #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
   70868                 : /* Opcode: MaxPgcnt P1 P2 P3 * *
   70869                 : **
   70870                 : ** Try to set the maximum page count for database P1 to the value in P3.
   70871                 : ** Do not let the maximum page count fall below the current page count and
   70872                 : ** do not change the maximum page count value if P3==0.
   70873                 : **
   70874                 : ** Store the maximum page count after the change in register P2.
   70875                 : */
   70876                 : case OP_MaxPgcnt: {            /* out2-prerelease */
   70877                 :   unsigned int newMax;
   70878                 :   Btree *pBt;
   70879                 : 
   70880               0 :   pBt = db->aDb[pOp->p1].pBt;
   70881               0 :   newMax = 0;
   70882               0 :   if( pOp->p3 ){
   70883               0 :     newMax = sqlite3BtreeLastPage(pBt);
   70884               0 :     if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
   70885                 :   }
   70886               0 :   pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
   70887               0 :   break;
   70888                 : }
   70889                 : #endif
   70890                 : 
   70891                 : 
   70892                 : #ifndef SQLITE_OMIT_TRACE
   70893                 : /* Opcode: Trace * * * P4 *
   70894                 : **
   70895                 : ** If tracing is enabled (by the sqlite3_trace()) interface, then
   70896                 : ** the UTF-8 string contained in P4 is emitted on the trace callback.
   70897                 : */
   70898                 : case OP_Trace: {
   70899                 : #if 0  /* local variables moved into u.cr */
   70900                 :   char *zTrace;
   70901                 :   char *z;
   70902                 : #endif /* local variables moved into u.cr */
   70903                 : 
   70904          223931 :   if( db->xTrace && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
   70905          204609 :     u.cr.z = sqlite3VdbeExpandSql(p, u.cr.zTrace);
   70906          204609 :     db->xTrace(db->pTraceArg, u.cr.z);
   70907          204609 :     sqlite3DbFree(db, u.cr.z);
   70908                 :   }
   70909                 : #ifdef SQLITE_DEBUG
   70910          223931 :   if( (db->flags & SQLITE_SqlTrace)!=0
   70911               0 :    && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
   70912                 :   ){
   70913               0 :     sqlite3DebugPrintf("SQL-trace: %s\n", u.cr.zTrace);
   70914                 :   }
   70915                 : #endif /* SQLITE_DEBUG */
   70916          223931 :   break;
   70917                 : }
   70918                 : #endif
   70919                 : 
   70920                 : 
   70921                 : /* Opcode: Noop * * * * *
   70922                 : **
   70923                 : ** Do nothing.  This instruction is often useful as a jump
   70924                 : ** destination.
   70925                 : */
   70926                 : /*
   70927                 : ** The magic Explain opcode are only inserted when explain==2 (which
   70928                 : ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
   70929                 : ** This opcode records information from the optimizer.  It is the
   70930                 : ** the same as a no-op.  This opcodesnever appears in a real VM program.
   70931                 : */
   70932                 : default: {          /* This is really OP_Noop and OP_Explain */
   70933           52605 :   assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
   70934           52605 :   break;
   70935                 : }
   70936                 : 
   70937                 : /*****************************************************************************
   70938                 : ** The cases of the switch statement above this line should all be indented
   70939                 : ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
   70940                 : ** readability.  From this point on down, the normal indentation rules are
   70941                 : ** restored.
   70942                 : *****************************************************************************/
   70943                 :     }
   70944                 : 
   70945                 : #ifdef VDBE_PROFILE
   70946                 :     {
   70947                 :       u64 elapsed = sqlite3Hwtime() - start;
   70948                 :       pOp->cycles += elapsed;
   70949                 :       pOp->cnt++;
   70950                 : #if 0
   70951                 :         fprintf(stdout, "%10llu ", elapsed);
   70952                 :         sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
   70953                 : #endif
   70954                 :     }
   70955                 : #endif
   70956                 : 
   70957                 :     /* The following code adds nothing to the actual functionality
   70958                 :     ** of the program.  It is only here for testing and debugging.
   70959                 :     ** On the other hand, it does burn CPU cycles every time through
   70960                 :     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
   70961                 :     */
   70962                 : #ifndef NDEBUG
   70963        11588280 :     assert( pc>=-1 && pc<p->nOp );
   70964                 : 
   70965                 : #ifdef SQLITE_DEBUG
   70966        11588233 :     if( p->trace ){
   70967               0 :       if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
   70968               0 :       if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
   70969               0 :         registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
   70970                 :       }
   70971               0 :       if( pOp->opflags & OPFLG_OUT3 ){
   70972               0 :         registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
   70973                 :       }
   70974                 :     }
   70975                 : #endif  /* SQLITE_DEBUG */
   70976                 : #endif  /* NDEBUG */
   70977                 :   }  /* The end of the for(;;) loop the loops through opcodes */
   70978                 : 
   70979                 :   /* If we reach this point, it means that execution is finished with
   70980                 :   ** an error of some kind.
   70981                 :   */
   70982                 : vdbe_error_halt:
   70983             598 :   assert( rc );
   70984             598 :   p->rc = rc;
   70985                 :   testcase( sqlite3GlobalConfig.xLog!=0 );
   70986             598 :   sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
   70987                 :                    pc, p->zSql, p->zErrMsg);
   70988             598 :   sqlite3VdbeHalt(p);
   70989             598 :   if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
   70990             598 :   rc = SQLITE_ERROR;
   70991             598 :   if( resetSchemaOnFault>0 ){
   70992               0 :     sqlite3ResetInternalSchema(db, resetSchemaOnFault-1);
   70993                 :   }
   70994                 : 
   70995                 :   /* This is the only way out of this procedure.  We have to
   70996                 :   ** release the mutexes on btrees that were acquired at the
   70997                 :   ** top. */
   70998                 : vdbe_return:
   70999          357866 :   db->lastRowid = lastRowid;
   71000          357866 :   sqlite3VdbeLeave(p);
   71001          357869 :   return rc;
   71002                 : 
   71003                 :   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
   71004                 :   ** is encountered.
   71005                 :   */
   71006                 : too_big:
   71007               0 :   sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
   71008               0 :   rc = SQLITE_TOOBIG;
   71009               0 :   goto vdbe_error_halt;
   71010                 : 
   71011                 :   /* Jump to here if a malloc() fails.
   71012                 :   */
   71013                 : no_mem:
   71014               4 :   db->mallocFailed = 1;
   71015               4 :   sqlite3SetString(&p->zErrMsg, db, "out of memory");
   71016               0 :   rc = SQLITE_NOMEM;
   71017               0 :   goto vdbe_error_halt;
   71018                 : 
   71019                 :   /* Jump to here for any other kind of fatal error.  The "rc" variable
   71020                 :   ** should hold the error number.
   71021                 :   */
   71022                 : abort_due_to_error:
   71023               4 :   assert( p->zErrMsg==0 );
   71024               4 :   if( db->mallocFailed ) rc = SQLITE_NOMEM;
   71025               4 :   if( rc!=SQLITE_IOERR_NOMEM ){
   71026               4 :     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
   71027                 :   }
   71028               4 :   goto vdbe_error_halt;
   71029                 : 
   71030                 :   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
   71031                 :   ** flag.
   71032                 :   */
   71033                 : abort_due_to_interrupt:
   71034               0 :   assert( db->u1.isInterrupted );
   71035               0 :   rc = SQLITE_INTERRUPT;
   71036               0 :   p->rc = rc;
   71037               0 :   sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
   71038               0 :   goto vdbe_error_halt;
   71039                 : }
   71040                 : 
   71041                 : /************** End of vdbe.c ************************************************/
   71042                 : /************** Begin file vdbeblob.c ****************************************/
   71043                 : /*
   71044                 : ** 2007 May 1
   71045                 : **
   71046                 : ** The author disclaims copyright to this source code.  In place of
   71047                 : ** a legal notice, here is a blessing:
   71048                 : **
   71049                 : **    May you do good and not evil.
   71050                 : **    May you find forgiveness for yourself and forgive others.
   71051                 : **    May you share freely, never taking more than you give.
   71052                 : **
   71053                 : *************************************************************************
   71054                 : **
   71055                 : ** This file contains code used to implement incremental BLOB I/O.
   71056                 : */
   71057                 : 
   71058                 : 
   71059                 : #ifndef SQLITE_OMIT_INCRBLOB
   71060                 : 
   71061                 : /*
   71062                 : ** Valid sqlite3_blob* handles point to Incrblob structures.
   71063                 : */
   71064                 : typedef struct Incrblob Incrblob;
   71065                 : struct Incrblob {
   71066                 :   int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
   71067                 :   int nByte;              /* Size of open blob, in bytes */
   71068                 :   int iOffset;            /* Byte offset of blob in cursor data */
   71069                 :   int iCol;               /* Table column this handle is open on */
   71070                 :   BtCursor *pCsr;         /* Cursor pointing at blob row */
   71071                 :   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
   71072                 :   sqlite3 *db;            /* The associated database */
   71073                 : };
   71074                 : 
   71075                 : 
   71076                 : /*
   71077                 : ** This function is used by both blob_open() and blob_reopen(). It seeks
   71078                 : ** the b-tree cursor associated with blob handle p to point to row iRow.
   71079                 : ** If successful, SQLITE_OK is returned and subsequent calls to
   71080                 : ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
   71081                 : **
   71082                 : ** If an error occurs, or if the specified row does not exist or does not
   71083                 : ** contain a value of type TEXT or BLOB in the column nominated when the
   71084                 : ** blob handle was opened, then an error code is returned and *pzErr may
   71085                 : ** be set to point to a buffer containing an error message. It is the
   71086                 : ** responsibility of the caller to free the error message buffer using
   71087                 : ** sqlite3DbFree().
   71088                 : **
   71089                 : ** If an error does occur, then the b-tree cursor is closed. All subsequent
   71090                 : ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will 
   71091                 : ** immediately return SQLITE_ABORT.
   71092                 : */
   71093               0 : static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
   71094                 :   int rc;                         /* Error code */
   71095               0 :   char *zErr = 0;                 /* Error message */
   71096               0 :   Vdbe *v = (Vdbe *)p->pStmt;
   71097                 : 
   71098                 :   /* Set the value of the SQL statements only variable to integer iRow. 
   71099                 :   ** This is done directly instead of using sqlite3_bind_int64() to avoid 
   71100                 :   ** triggering asserts related to mutexes.
   71101                 :   */
   71102               0 :   assert( v->aVar[0].flags&MEM_Int );
   71103               0 :   v->aVar[0].u.i = iRow;
   71104                 : 
   71105               0 :   rc = sqlite3_step(p->pStmt);
   71106               0 :   if( rc==SQLITE_ROW ){
   71107               0 :     u32 type = v->apCsr[0]->aType[p->iCol];
   71108               0 :     if( type<12 ){
   71109               0 :       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
   71110               0 :           type==0?"null": type==7?"real": "integer"
   71111                 :       );
   71112               0 :       rc = SQLITE_ERROR;
   71113               0 :       sqlite3_finalize(p->pStmt);
   71114               0 :       p->pStmt = 0;
   71115                 :     }else{
   71116               0 :       p->iOffset = v->apCsr[0]->aOffset[p->iCol];
   71117               0 :       p->nByte = sqlite3VdbeSerialTypeLen(type);
   71118               0 :       p->pCsr =  v->apCsr[0]->pCursor;
   71119               0 :       sqlite3BtreeEnterCursor(p->pCsr);
   71120               0 :       sqlite3BtreeCacheOverflow(p->pCsr);
   71121               0 :       sqlite3BtreeLeaveCursor(p->pCsr);
   71122                 :     }
   71123                 :   }
   71124                 : 
   71125               0 :   if( rc==SQLITE_ROW ){
   71126               0 :     rc = SQLITE_OK;
   71127               0 :   }else if( p->pStmt ){
   71128               0 :     rc = sqlite3_finalize(p->pStmt);
   71129               0 :     p->pStmt = 0;
   71130               0 :     if( rc==SQLITE_OK ){
   71131               0 :       zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
   71132               0 :       rc = SQLITE_ERROR;
   71133                 :     }else{
   71134               0 :       zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
   71135                 :     }
   71136                 :   }
   71137                 : 
   71138               0 :   assert( rc!=SQLITE_OK || zErr==0 );
   71139               0 :   assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
   71140                 : 
   71141               0 :   *pzErr = zErr;
   71142               0 :   return rc;
   71143                 : }
   71144                 : 
   71145                 : /*
   71146                 : ** Open a blob handle.
   71147                 : */
   71148               0 : SQLITE_API int sqlite3_blob_open(
   71149                 :   sqlite3* db,            /* The database connection */
   71150                 :   const char *zDb,        /* The attached database containing the blob */
   71151                 :   const char *zTable,     /* The table containing the blob */
   71152                 :   const char *zColumn,    /* The column containing the blob */
   71153                 :   sqlite_int64 iRow,      /* The row containing the glob */
   71154                 :   int flags,              /* True -> read/write access, false -> read-only */
   71155                 :   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
   71156                 : ){
   71157               0 :   int nAttempt = 0;
   71158                 :   int iCol;               /* Index of zColumn in row-record */
   71159                 : 
   71160                 :   /* This VDBE program seeks a btree cursor to the identified 
   71161                 :   ** db/table/row entry. The reason for using a vdbe program instead
   71162                 :   ** of writing code to use the b-tree layer directly is that the
   71163                 :   ** vdbe program will take advantage of the various transaction,
   71164                 :   ** locking and error handling infrastructure built into the vdbe.
   71165                 :   **
   71166                 :   ** After seeking the cursor, the vdbe executes an OP_ResultRow.
   71167                 :   ** Code external to the Vdbe then "borrows" the b-tree cursor and
   71168                 :   ** uses it to implement the blob_read(), blob_write() and 
   71169                 :   ** blob_bytes() functions.
   71170                 :   **
   71171                 :   ** The sqlite3_blob_close() function finalizes the vdbe program,
   71172                 :   ** which closes the b-tree cursor and (possibly) commits the 
   71173                 :   ** transaction.
   71174                 :   */
   71175                 :   static const VdbeOpList openBlob[] = {
   71176                 :     {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
   71177                 :     {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
   71178                 :     {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
   71179                 : 
   71180                 :     /* One of the following two instructions is replaced by an OP_Noop. */
   71181                 :     {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
   71182                 :     {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
   71183                 : 
   71184                 :     {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
   71185                 :     {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
   71186                 :     {OP_Column, 0, 0, 1},          /* 7  */
   71187                 :     {OP_ResultRow, 1, 0, 0},       /* 8  */
   71188                 :     {OP_Goto, 0, 5, 0},            /* 9  */
   71189                 :     {OP_Close, 0, 0, 0},           /* 10 */
   71190                 :     {OP_Halt, 0, 0, 0},            /* 11 */
   71191                 :   };
   71192                 : 
   71193               0 :   int rc = SQLITE_OK;
   71194               0 :   char *zErr = 0;
   71195                 :   Table *pTab;
   71196               0 :   Parse *pParse = 0;
   71197               0 :   Incrblob *pBlob = 0;
   71198                 : 
   71199               0 :   flags = !!flags;                /* flags = (flags ? 1 : 0); */
   71200               0 :   *ppBlob = 0;
   71201                 : 
   71202               0 :   sqlite3_mutex_enter(db->mutex);
   71203                 : 
   71204               0 :   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
   71205               0 :   if( !pBlob ) goto blob_open_out;
   71206               0 :   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
   71207               0 :   if( !pParse ) goto blob_open_out;
   71208                 : 
   71209                 :   do {
   71210               0 :     memset(pParse, 0, sizeof(Parse));
   71211               0 :     pParse->db = db;
   71212               0 :     sqlite3DbFree(db, zErr);
   71213               0 :     zErr = 0;
   71214                 : 
   71215               0 :     sqlite3BtreeEnterAll(db);
   71216               0 :     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
   71217               0 :     if( pTab && IsVirtual(pTab) ){
   71218               0 :       pTab = 0;
   71219               0 :       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
   71220                 :     }
   71221                 : #ifndef SQLITE_OMIT_VIEW
   71222               0 :     if( pTab && pTab->pSelect ){
   71223               0 :       pTab = 0;
   71224               0 :       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
   71225                 :     }
   71226                 : #endif
   71227               0 :     if( !pTab ){
   71228               0 :       if( pParse->zErrMsg ){
   71229               0 :         sqlite3DbFree(db, zErr);
   71230               0 :         zErr = pParse->zErrMsg;
   71231               0 :         pParse->zErrMsg = 0;
   71232                 :       }
   71233               0 :       rc = SQLITE_ERROR;
   71234               0 :       sqlite3BtreeLeaveAll(db);
   71235               0 :       goto blob_open_out;
   71236                 :     }
   71237                 : 
   71238                 :     /* Now search pTab for the exact column. */
   71239               0 :     for(iCol=0; iCol<pTab->nCol; iCol++) {
   71240               0 :       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
   71241               0 :         break;
   71242                 :       }
   71243                 :     }
   71244               0 :     if( iCol==pTab->nCol ){
   71245               0 :       sqlite3DbFree(db, zErr);
   71246               0 :       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
   71247               0 :       rc = SQLITE_ERROR;
   71248               0 :       sqlite3BtreeLeaveAll(db);
   71249               0 :       goto blob_open_out;
   71250                 :     }
   71251                 : 
   71252                 :     /* If the value is being opened for writing, check that the
   71253                 :     ** column is not indexed, and that it is not part of a foreign key. 
   71254                 :     ** It is against the rules to open a column to which either of these
   71255                 :     ** descriptions applies for writing.  */
   71256               0 :     if( flags ){
   71257               0 :       const char *zFault = 0;
   71258                 :       Index *pIdx;
   71259                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   71260               0 :       if( db->flags&SQLITE_ForeignKeys ){
   71261                 :         /* Check that the column is not part of an FK child key definition. It
   71262                 :         ** is not necessary to check if it is part of a parent key, as parent
   71263                 :         ** key columns must be indexed. The check below will pick up this 
   71264                 :         ** case.  */
   71265                 :         FKey *pFKey;
   71266               0 :         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
   71267                 :           int j;
   71268               0 :           for(j=0; j<pFKey->nCol; j++){
   71269               0 :             if( pFKey->aCol[j].iFrom==iCol ){
   71270               0 :               zFault = "foreign key";
   71271                 :             }
   71272                 :           }
   71273                 :         }
   71274                 :       }
   71275                 : #endif
   71276               0 :       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   71277                 :         int j;
   71278               0 :         for(j=0; j<pIdx->nColumn; j++){
   71279               0 :           if( pIdx->aiColumn[j]==iCol ){
   71280               0 :             zFault = "indexed";
   71281                 :           }
   71282                 :         }
   71283                 :       }
   71284               0 :       if( zFault ){
   71285               0 :         sqlite3DbFree(db, zErr);
   71286               0 :         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
   71287               0 :         rc = SQLITE_ERROR;
   71288               0 :         sqlite3BtreeLeaveAll(db);
   71289               0 :         goto blob_open_out;
   71290                 :       }
   71291                 :     }
   71292                 : 
   71293               0 :     pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(db);
   71294               0 :     assert( pBlob->pStmt || db->mallocFailed );
   71295               0 :     if( pBlob->pStmt ){
   71296               0 :       Vdbe *v = (Vdbe *)pBlob->pStmt;
   71297               0 :       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   71298                 : 
   71299               0 :       sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
   71300                 : 
   71301                 : 
   71302                 :       /* Configure the OP_Transaction */
   71303               0 :       sqlite3VdbeChangeP1(v, 0, iDb);
   71304               0 :       sqlite3VdbeChangeP2(v, 0, flags);
   71305                 : 
   71306                 :       /* Configure the OP_VerifyCookie */
   71307               0 :       sqlite3VdbeChangeP1(v, 1, iDb);
   71308               0 :       sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
   71309               0 :       sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
   71310                 : 
   71311                 :       /* Make sure a mutex is held on the table to be accessed */
   71312               0 :       sqlite3VdbeUsesBtree(v, iDb); 
   71313                 : 
   71314                 :       /* Configure the OP_TableLock instruction */
   71315                 : #ifdef SQLITE_OMIT_SHARED_CACHE
   71316                 :       sqlite3VdbeChangeToNoop(v, 2);
   71317                 : #else
   71318               0 :       sqlite3VdbeChangeP1(v, 2, iDb);
   71319               0 :       sqlite3VdbeChangeP2(v, 2, pTab->tnum);
   71320               0 :       sqlite3VdbeChangeP3(v, 2, flags);
   71321               0 :       sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
   71322                 : #endif
   71323                 : 
   71324                 :       /* Remove either the OP_OpenWrite or OpenRead. Set the P2 
   71325                 :       ** parameter of the other to pTab->tnum.  */
   71326               0 :       sqlite3VdbeChangeToNoop(v, 4 - flags);
   71327               0 :       sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
   71328               0 :       sqlite3VdbeChangeP3(v, 3 + flags, iDb);
   71329                 : 
   71330                 :       /* Configure the number of columns. Configure the cursor to
   71331                 :       ** think that the table has one more column than it really
   71332                 :       ** does. An OP_Column to retrieve this imaginary column will
   71333                 :       ** always return an SQL NULL. This is useful because it means
   71334                 :       ** we can invoke OP_Column to fill in the vdbe cursors type 
   71335                 :       ** and offset cache without causing any IO.
   71336                 :       */
   71337               0 :       sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
   71338               0 :       sqlite3VdbeChangeP2(v, 7, pTab->nCol);
   71339               0 :       if( !db->mallocFailed ){
   71340               0 :         pParse->nVar = 1;
   71341               0 :         pParse->nMem = 1;
   71342               0 :         pParse->nTab = 1;
   71343               0 :         sqlite3VdbeMakeReady(v, pParse);
   71344                 :       }
   71345                 :     }
   71346                 :    
   71347               0 :     pBlob->flags = flags;
   71348               0 :     pBlob->iCol = iCol;
   71349               0 :     pBlob->db = db;
   71350               0 :     sqlite3BtreeLeaveAll(db);
   71351               0 :     if( db->mallocFailed ){
   71352               0 :       goto blob_open_out;
   71353                 :     }
   71354               0 :     sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
   71355               0 :     rc = blobSeekToRow(pBlob, iRow, &zErr);
   71356               0 :   } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
   71357                 : 
   71358                 : blob_open_out:
   71359               0 :   if( rc==SQLITE_OK && db->mallocFailed==0 ){
   71360               0 :     *ppBlob = (sqlite3_blob *)pBlob;
   71361                 :   }else{
   71362               0 :     if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
   71363               0 :     sqlite3DbFree(db, pBlob);
   71364                 :   }
   71365               0 :   sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
   71366               0 :   sqlite3DbFree(db, zErr);
   71367               0 :   sqlite3StackFree(db, pParse);
   71368               0 :   rc = sqlite3ApiExit(db, rc);
   71369               0 :   sqlite3_mutex_leave(db->mutex);
   71370               0 :   return rc;
   71371                 : }
   71372                 : 
   71373                 : /*
   71374                 : ** Close a blob handle that was previously created using
   71375                 : ** sqlite3_blob_open().
   71376                 : */
   71377              10 : SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
   71378              10 :   Incrblob *p = (Incrblob *)pBlob;
   71379                 :   int rc;
   71380                 :   sqlite3 *db;
   71381                 : 
   71382              10 :   if( p ){
   71383               0 :     db = p->db;
   71384               0 :     sqlite3_mutex_enter(db->mutex);
   71385               0 :     rc = sqlite3_finalize(p->pStmt);
   71386               0 :     sqlite3DbFree(db, p);
   71387               0 :     sqlite3_mutex_leave(db->mutex);
   71388                 :   }else{
   71389              10 :     rc = SQLITE_OK;
   71390                 :   }
   71391              10 :   return rc;
   71392                 : }
   71393                 : 
   71394                 : /*
   71395                 : ** Perform a read or write operation on a blob
   71396                 : */
   71397               0 : static int blobReadWrite(
   71398                 :   sqlite3_blob *pBlob, 
   71399                 :   void *z, 
   71400                 :   int n, 
   71401                 :   int iOffset, 
   71402                 :   int (*xCall)(BtCursor*, u32, u32, void*)
   71403                 : ){
   71404                 :   int rc;
   71405               0 :   Incrblob *p = (Incrblob *)pBlob;
   71406                 :   Vdbe *v;
   71407                 :   sqlite3 *db;
   71408                 : 
   71409               0 :   if( p==0 ) return SQLITE_MISUSE_BKPT;
   71410               0 :   db = p->db;
   71411               0 :   sqlite3_mutex_enter(db->mutex);
   71412               0 :   v = (Vdbe*)p->pStmt;
   71413                 : 
   71414               0 :   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
   71415                 :     /* Request is out of range. Return a transient error. */
   71416               0 :     rc = SQLITE_ERROR;
   71417               0 :     sqlite3Error(db, SQLITE_ERROR, 0);
   71418               0 :   }else if( v==0 ){
   71419                 :     /* If there is no statement handle, then the blob-handle has
   71420                 :     ** already been invalidated. Return SQLITE_ABORT in this case.
   71421                 :     */
   71422               0 :     rc = SQLITE_ABORT;
   71423                 :   }else{
   71424                 :     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
   71425                 :     ** returned, clean-up the statement handle.
   71426                 :     */
   71427               0 :     assert( db == v->db );
   71428               0 :     sqlite3BtreeEnterCursor(p->pCsr);
   71429               0 :     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
   71430               0 :     sqlite3BtreeLeaveCursor(p->pCsr);
   71431               0 :     if( rc==SQLITE_ABORT ){
   71432               0 :       sqlite3VdbeFinalize(v);
   71433               0 :       p->pStmt = 0;
   71434                 :     }else{
   71435               0 :       db->errCode = rc;
   71436               0 :       v->rc = rc;
   71437                 :     }
   71438                 :   }
   71439               0 :   rc = sqlite3ApiExit(db, rc);
   71440               0 :   sqlite3_mutex_leave(db->mutex);
   71441               0 :   return rc;
   71442                 : }
   71443                 : 
   71444                 : /*
   71445                 : ** Read data from a blob handle.
   71446                 : */
   71447               0 : SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
   71448               0 :   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
   71449                 : }
   71450                 : 
   71451                 : /*
   71452                 : ** Write data to a blob handle.
   71453                 : */
   71454               0 : SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
   71455               0 :   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
   71456                 : }
   71457                 : 
   71458                 : /*
   71459                 : ** Query a blob handle for the size of the data.
   71460                 : **
   71461                 : ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
   71462                 : ** so no mutex is required for access.
   71463                 : */
   71464               0 : SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
   71465               0 :   Incrblob *p = (Incrblob *)pBlob;
   71466               0 :   return (p && p->pStmt) ? p->nByte : 0;
   71467                 : }
   71468                 : 
   71469                 : /*
   71470                 : ** Move an existing blob handle to point to a different row of the same
   71471                 : ** database table.
   71472                 : **
   71473                 : ** If an error occurs, or if the specified row does not exist or does not
   71474                 : ** contain a blob or text value, then an error code is returned and the
   71475                 : ** database handle error code and message set. If this happens, then all 
   71476                 : ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close()) 
   71477                 : ** immediately return SQLITE_ABORT.
   71478                 : */
   71479               0 : SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
   71480                 :   int rc;
   71481               0 :   Incrblob *p = (Incrblob *)pBlob;
   71482                 :   sqlite3 *db;
   71483                 : 
   71484               0 :   if( p==0 ) return SQLITE_MISUSE_BKPT;
   71485               0 :   db = p->db;
   71486               0 :   sqlite3_mutex_enter(db->mutex);
   71487                 : 
   71488               0 :   if( p->pStmt==0 ){
   71489                 :     /* If there is no statement handle, then the blob-handle has
   71490                 :     ** already been invalidated. Return SQLITE_ABORT in this case.
   71491                 :     */
   71492               0 :     rc = SQLITE_ABORT;
   71493                 :   }else{
   71494                 :     char *zErr;
   71495               0 :     rc = blobSeekToRow(p, iRow, &zErr);
   71496               0 :     if( rc!=SQLITE_OK ){
   71497               0 :       sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
   71498               0 :       sqlite3DbFree(db, zErr);
   71499                 :     }
   71500               0 :     assert( rc!=SQLITE_SCHEMA );
   71501                 :   }
   71502                 : 
   71503               0 :   rc = sqlite3ApiExit(db, rc);
   71504               0 :   assert( rc==SQLITE_OK || p->pStmt==0 );
   71505               0 :   sqlite3_mutex_leave(db->mutex);
   71506               0 :   return rc;
   71507                 : }
   71508                 : 
   71509                 : #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
   71510                 : 
   71511                 : /************** End of vdbeblob.c ********************************************/
   71512                 : /************** Begin file vdbesort.c ****************************************/
   71513                 : /*
   71514                 : ** 2011 July 9
   71515                 : **
   71516                 : ** The author disclaims copyright to this source code.  In place of
   71517                 : ** a legal notice, here is a blessing:
   71518                 : **
   71519                 : **    May you do good and not evil.
   71520                 : **    May you find forgiveness for yourself and forgive others.
   71521                 : **    May you share freely, never taking more than you give.
   71522                 : **
   71523                 : *************************************************************************
   71524                 : ** This file contains code for the VdbeSorter object, used in concert with
   71525                 : ** a VdbeCursor to sort large numbers of keys (as may be required, for
   71526                 : ** example, by CREATE INDEX statements on tables too large to fit in main
   71527                 : ** memory).
   71528                 : */
   71529                 : 
   71530                 : 
   71531                 : #ifndef SQLITE_OMIT_MERGE_SORT
   71532                 : 
   71533                 : typedef struct VdbeSorterIter VdbeSorterIter;
   71534                 : typedef struct SorterRecord SorterRecord;
   71535                 : 
   71536                 : /*
   71537                 : ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
   71538                 : **
   71539                 : ** As keys are added to the sorter, they are written to disk in a series
   71540                 : ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
   71541                 : ** the same as the cache-size allowed for temporary databases. In order
   71542                 : ** to allow the caller to extract keys from the sorter in sorted order,
   71543                 : ** all PMAs currently stored on disk must be merged together. This comment
   71544                 : ** describes the data structure used to do so. The structure supports 
   71545                 : ** merging any number of arrays in a single pass with no redundant comparison 
   71546                 : ** operations.
   71547                 : **
   71548                 : ** The aIter[] array contains an iterator for each of the PMAs being merged.
   71549                 : ** An aIter[] iterator either points to a valid key or else is at EOF. For 
   71550                 : ** the purposes of the paragraphs below, we assume that the array is actually 
   71551                 : ** N elements in size, where N is the smallest power of 2 greater to or equal 
   71552                 : ** to the number of iterators being merged. The extra aIter[] elements are 
   71553                 : ** treated as if they are empty (always at EOF).
   71554                 : **
   71555                 : ** The aTree[] array is also N elements in size. The value of N is stored in
   71556                 : ** the VdbeSorter.nTree variable.
   71557                 : **
   71558                 : ** The final (N/2) elements of aTree[] contain the results of comparing
   71559                 : ** pairs of iterator keys together. Element i contains the result of 
   71560                 : ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
   71561                 : ** aTree element is set to the index of it. 
   71562                 : **
   71563                 : ** For the purposes of this comparison, EOF is considered greater than any
   71564                 : ** other key value. If the keys are equal (only possible with two EOF
   71565                 : ** values), it doesn't matter which index is stored.
   71566                 : **
   71567                 : ** The (N/4) elements of aTree[] that preceed the final (N/2) described 
   71568                 : ** above contains the index of the smallest of each block of 4 iterators.
   71569                 : ** And so on. So that aTree[1] contains the index of the iterator that 
   71570                 : ** currently points to the smallest key value. aTree[0] is unused.
   71571                 : **
   71572                 : ** Example:
   71573                 : **
   71574                 : **     aIter[0] -> Banana
   71575                 : **     aIter[1] -> Feijoa
   71576                 : **     aIter[2] -> Elderberry
   71577                 : **     aIter[3] -> Currant
   71578                 : **     aIter[4] -> Grapefruit
   71579                 : **     aIter[5] -> Apple
   71580                 : **     aIter[6] -> Durian
   71581                 : **     aIter[7] -> EOF
   71582                 : **
   71583                 : **     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
   71584                 : **
   71585                 : ** The current element is "Apple" (the value of the key indicated by 
   71586                 : ** iterator 5). When the Next() operation is invoked, iterator 5 will
   71587                 : ** be advanced to the next key in its segment. Say the next key is
   71588                 : ** "Eggplant":
   71589                 : **
   71590                 : **     aIter[5] -> Eggplant
   71591                 : **
   71592                 : ** The contents of aTree[] are updated first by comparing the new iterator
   71593                 : ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
   71594                 : ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
   71595                 : ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
   71596                 : ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
   71597                 : ** so the value written into element 1 of the array is 0. As follows:
   71598                 : **
   71599                 : **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
   71600                 : **
   71601                 : ** In other words, each time we advance to the next sorter element, log2(N)
   71602                 : ** key comparison operations are required, where N is the number of segments
   71603                 : ** being merged (rounded up to the next power of 2).
   71604                 : */
   71605                 : struct VdbeSorter {
   71606                 :   int nInMemory;                  /* Current size of pRecord list as PMA */
   71607                 :   int nTree;                      /* Used size of aTree/aIter (power of 2) */
   71608                 :   VdbeSorterIter *aIter;          /* Array of iterators to merge */
   71609                 :   int *aTree;                     /* Current state of incremental merge */
   71610                 :   i64 iWriteOff;                  /* Current write offset within file pTemp1 */
   71611                 :   i64 iReadOff;                   /* Current read offset within file pTemp1 */
   71612                 :   sqlite3_file *pTemp1;           /* PMA file 1 */
   71613                 :   int nPMA;                       /* Number of PMAs stored in pTemp1 */
   71614                 :   SorterRecord *pRecord;          /* Head of in-memory record list */
   71615                 :   int mnPmaSize;                  /* Minimum PMA size, in bytes */
   71616                 :   int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
   71617                 :   UnpackedRecord *pUnpacked;      /* Used to unpack keys */
   71618                 : };
   71619                 : 
   71620                 : /*
   71621                 : ** The following type is an iterator for a PMA. It caches the current key in 
   71622                 : ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
   71623                 : */
   71624                 : struct VdbeSorterIter {
   71625                 :   i64 iReadOff;                   /* Current read offset */
   71626                 :   i64 iEof;                       /* 1 byte past EOF for this iterator */
   71627                 :   sqlite3_file *pFile;            /* File iterator is reading from */
   71628                 :   int nAlloc;                     /* Bytes of space at aAlloc */
   71629                 :   u8 *aAlloc;                     /* Allocated space */
   71630                 :   int nKey;                       /* Number of bytes in key */
   71631                 :   u8 *aKey;                       /* Pointer to current key */
   71632                 : };
   71633                 : 
   71634                 : /*
   71635                 : ** A structure to store a single record. All in-memory records are connected
   71636                 : ** together into a linked list headed at VdbeSorter.pRecord using the 
   71637                 : ** SorterRecord.pNext pointer.
   71638                 : */
   71639                 : struct SorterRecord {
   71640                 :   void *pVal;
   71641                 :   int nVal;
   71642                 :   SorterRecord *pNext;
   71643                 : };
   71644                 : 
   71645                 : /* Minimum allowable value for the VdbeSorter.nWorking variable */
   71646                 : #define SORTER_MIN_WORKING 10
   71647                 : 
   71648                 : /* Maximum number of segments to merge in a single pass. */
   71649                 : #define SORTER_MAX_MERGE_COUNT 16
   71650                 : 
   71651                 : /*
   71652                 : ** Free all memory belonging to the VdbeSorterIter object passed as the second
   71653                 : ** argument. All structure fields are set to zero before returning.
   71654                 : */
   71655               0 : static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
   71656               0 :   sqlite3DbFree(db, pIter->aAlloc);
   71657               0 :   memset(pIter, 0, sizeof(VdbeSorterIter));
   71658               0 : }
   71659                 : 
   71660                 : /*
   71661                 : ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
   71662                 : ** no error occurs, or an SQLite error code if one does.
   71663                 : */
   71664               0 : static int vdbeSorterIterNext(
   71665                 :   sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
   71666                 :   VdbeSorterIter *pIter           /* Iterator to advance */
   71667                 : ){
   71668                 :   int rc;                         /* Return Code */
   71669                 :   int nRead;                      /* Number of bytes read */
   71670               0 :   int nRec = 0;                   /* Size of record in bytes */
   71671               0 :   int iOff = 0;                   /* Size of serialized size varint in bytes */
   71672                 : 
   71673               0 :   assert( pIter->iEof>=pIter->iReadOff );
   71674               0 :   if( pIter->iEof-pIter->iReadOff>5 ){
   71675               0 :     nRead = 5;
   71676                 :   }else{
   71677               0 :     nRead = (int)(pIter->iEof - pIter->iReadOff);
   71678                 :   }
   71679               0 :   if( nRead<=0 ){
   71680                 :     /* This is an EOF condition */
   71681               0 :     vdbeSorterIterZero(db, pIter);
   71682               0 :     return SQLITE_OK;
   71683                 :   }
   71684                 : 
   71685               0 :   rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
   71686               0 :   if( rc==SQLITE_OK ){
   71687               0 :     iOff = getVarint32(pIter->aAlloc, nRec);
   71688               0 :     if( (iOff+nRec)>nRead ){
   71689                 :       int nRead2;                   /* Number of extra bytes to read */
   71690               0 :       if( (iOff+nRec)>pIter->nAlloc ){
   71691               0 :         int nNew = pIter->nAlloc*2;
   71692               0 :         while( (iOff+nRec)>nNew ) nNew = nNew*2;
   71693               0 :         pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
   71694               0 :         if( !pIter->aAlloc ) return SQLITE_NOMEM;
   71695               0 :         pIter->nAlloc = nNew;
   71696                 :       }
   71697                 :   
   71698               0 :       nRead2 = iOff + nRec - nRead;
   71699               0 :       rc = sqlite3OsRead(
   71700               0 :           pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
   71701                 :       );
   71702                 :     }
   71703                 :   }
   71704                 : 
   71705               0 :   assert( rc!=SQLITE_OK || nRec>0 );
   71706               0 :   pIter->iReadOff += iOff+nRec;
   71707               0 :   pIter->nKey = nRec;
   71708               0 :   pIter->aKey = &pIter->aAlloc[iOff];
   71709               0 :   return rc;
   71710                 : }
   71711                 : 
   71712                 : /*
   71713                 : ** Write a single varint, value iVal, to file-descriptor pFile. Return
   71714                 : ** SQLITE_OK if successful, or an SQLite error code if some error occurs.
   71715                 : **
   71716                 : ** The value of *piOffset when this function is called is used as the byte
   71717                 : ** offset in file pFile to write to. Before returning, *piOffset is 
   71718                 : ** incremented by the number of bytes written.
   71719                 : */
   71720               0 : static int vdbeSorterWriteVarint(
   71721                 :   sqlite3_file *pFile,            /* File to write to */
   71722                 :   i64 iVal,                       /* Value to write as a varint */
   71723                 :   i64 *piOffset                   /* IN/OUT: Write offset in file pFile */
   71724                 : ){
   71725                 :   u8 aVarint[9];                  /* Buffer large enough for a varint */
   71726                 :   int nVarint;                    /* Number of used bytes in varint */
   71727                 :   int rc;                         /* Result of write() call */
   71728                 : 
   71729               0 :   nVarint = sqlite3PutVarint(aVarint, iVal);
   71730               0 :   rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
   71731               0 :   *piOffset += nVarint;
   71732                 : 
   71733               0 :   return rc;
   71734                 : }
   71735                 : 
   71736                 : /*
   71737                 : ** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
   71738                 : ** successful, or an SQLite error code if some error occurs.
   71739                 : **
   71740                 : ** The value of *piOffset when this function is called is used as the
   71741                 : ** byte offset in file pFile from whence to read the varint. If successful
   71742                 : ** (i.e. if no IO error occurs), then *piOffset is set to the offset of
   71743                 : ** the first byte past the end of the varint before returning. *piVal is
   71744                 : ** set to the integer value read. If an error occurs, the final values of
   71745                 : ** both *piOffset and *piVal are undefined.
   71746                 : */
   71747               0 : static int vdbeSorterReadVarint(
   71748                 :   sqlite3_file *pFile,            /* File to read from */
   71749                 :   i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
   71750                 :   i64 *piVal                      /* OUT: Value read from file */
   71751                 : ){
   71752                 :   u8 aVarint[9];                  /* Buffer large enough for a varint */
   71753               0 :   i64 iOff = *piOffset;           /* Offset in file to read from */
   71754                 :   int rc;                         /* Return code */
   71755                 : 
   71756               0 :   rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
   71757               0 :   if( rc==SQLITE_OK ){
   71758               0 :     *piOffset += getVarint(aVarint, (u64 *)piVal);
   71759                 :   }
   71760                 : 
   71761               0 :   return rc;
   71762                 : }
   71763                 : 
   71764                 : /*
   71765                 : ** Initialize iterator pIter to scan through the PMA stored in file pFile
   71766                 : ** starting at offset iStart and ending at offset iEof-1. This function 
   71767                 : ** leaves the iterator pointing to the first key in the PMA (or EOF if the 
   71768                 : ** PMA is empty).
   71769                 : */
   71770               0 : static int vdbeSorterIterInit(
   71771                 :   sqlite3 *db,                    /* Database handle */
   71772                 :   VdbeSorter *pSorter,            /* Sorter object */
   71773                 :   i64 iStart,                     /* Start offset in pFile */
   71774                 :   VdbeSorterIter *pIter,          /* Iterator to populate */
   71775                 :   i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
   71776                 : ){
   71777                 :   int rc;
   71778                 : 
   71779               0 :   assert( pSorter->iWriteOff>iStart );
   71780               0 :   assert( pIter->aAlloc==0 );
   71781               0 :   pIter->pFile = pSorter->pTemp1;
   71782               0 :   pIter->iReadOff = iStart;
   71783               0 :   pIter->nAlloc = 128;
   71784               0 :   pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
   71785               0 :   if( !pIter->aAlloc ){
   71786               0 :     rc = SQLITE_NOMEM;
   71787                 :   }else{
   71788                 :     i64 nByte;                         /* Total size of PMA in bytes */
   71789               0 :     rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
   71790               0 :     *pnByte += nByte;
   71791               0 :     pIter->iEof = pIter->iReadOff + nByte;
   71792                 :   }
   71793               0 :   if( rc==SQLITE_OK ){
   71794               0 :     rc = vdbeSorterIterNext(db, pIter);
   71795                 :   }
   71796               0 :   return rc;
   71797                 : }
   71798                 : 
   71799                 : 
   71800                 : /*
   71801                 : ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
   71802                 : ** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
   71803                 : ** used by the comparison. If an error occurs, return an SQLite error code.
   71804                 : ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
   71805                 : ** value, depending on whether key1 is smaller, equal to or larger than key2.
   71806                 : **
   71807                 : ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
   71808                 : ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
   71809                 : ** is true and key1 contains even a single NULL value, it is considered to
   71810                 : ** be less than key2. Even if key2 also contains NULL values.
   71811                 : **
   71812                 : ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
   71813                 : ** has been allocated and contains an unpacked record that is used as key2.
   71814                 : */
   71815           14452 : static void vdbeSorterCompare(
   71816                 :   VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
   71817                 :   int bOmitRowid,                 /* Ignore rowid field at end of keys */
   71818                 :   void *pKey1, int nKey1,         /* Left side of comparison */
   71819                 :   void *pKey2, int nKey2,         /* Right side of comparison */
   71820                 :   int *pRes                       /* OUT: Result of comparison */
   71821                 : ){
   71822           14452 :   KeyInfo *pKeyInfo = pCsr->pKeyInfo;
   71823           14452 :   VdbeSorter *pSorter = pCsr->pSorter;
   71824           14452 :   UnpackedRecord *r2 = pSorter->pUnpacked;
   71825                 :   int i;
   71826                 : 
   71827           14452 :   if( pKey2 ){
   71828            7657 :     sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
   71829                 :   }
   71830                 : 
   71831           14452 :   if( bOmitRowid ){
   71832             443 :     r2->nField = pKeyInfo->nField;
   71833             443 :     assert( r2->nField>0 );
   71834             700 :     for(i=0; i<r2->nField; i++){
   71835             603 :       if( r2->aMem[i].flags & MEM_Null ){
   71836             346 :         *pRes = -1;
   71837             346 :         return;
   71838                 :       }
   71839                 :     }
   71840              97 :     r2->flags |= UNPACKED_PREFIX_MATCH;
   71841                 :   }
   71842                 : 
   71843           14106 :   *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
   71844                 : }
   71845                 : 
   71846                 : /*
   71847                 : ** This function is called to compare two iterator keys when merging 
   71848                 : ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
   71849                 : ** value to recalculate.
   71850                 : */
   71851               0 : static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
   71852               0 :   VdbeSorter *pSorter = pCsr->pSorter;
   71853                 :   int i1;
   71854                 :   int i2;
   71855                 :   int iRes;
   71856                 :   VdbeSorterIter *p1;
   71857                 :   VdbeSorterIter *p2;
   71858                 : 
   71859               0 :   assert( iOut<pSorter->nTree && iOut>0 );
   71860                 : 
   71861               0 :   if( iOut>=(pSorter->nTree/2) ){
   71862               0 :     i1 = (iOut - pSorter->nTree/2) * 2;
   71863               0 :     i2 = i1 + 1;
   71864                 :   }else{
   71865               0 :     i1 = pSorter->aTree[iOut*2];
   71866               0 :     i2 = pSorter->aTree[iOut*2+1];
   71867                 :   }
   71868                 : 
   71869               0 :   p1 = &pSorter->aIter[i1];
   71870               0 :   p2 = &pSorter->aIter[i2];
   71871                 : 
   71872               0 :   if( p1->pFile==0 ){
   71873               0 :     iRes = i2;
   71874               0 :   }else if( p2->pFile==0 ){
   71875               0 :     iRes = i1;
   71876                 :   }else{
   71877                 :     int res;
   71878               0 :     assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
   71879               0 :     vdbeSorterCompare(
   71880               0 :         pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
   71881                 :     );
   71882               0 :     if( res<=0 ){
   71883               0 :       iRes = i1;
   71884                 :     }else{
   71885               0 :       iRes = i2;
   71886                 :     }
   71887                 :   }
   71888                 : 
   71889               0 :   pSorter->aTree[iOut] = iRes;
   71890               0 :   return SQLITE_OK;
   71891                 : }
   71892                 : 
   71893                 : /*
   71894                 : ** Initialize the temporary index cursor just opened as a sorter cursor.
   71895                 : */
   71896            6886 : SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
   71897                 :   int pgsz;                       /* Page size of main database */
   71898                 :   int mxCache;                    /* Cache size */
   71899                 :   VdbeSorter *pSorter;            /* The new sorter */
   71900                 :   char *d;                        /* Dummy */
   71901                 : 
   71902            6886 :   assert( pCsr->pKeyInfo && pCsr->pBt==0 );
   71903            6886 :   pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
   71904            6886 :   if( pSorter==0 ){
   71905               0 :     return SQLITE_NOMEM;
   71906                 :   }
   71907                 :   
   71908            6886 :   pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
   71909            6886 :   if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
   71910            6886 :   assert( pSorter->pUnpacked==(UnpackedRecord *)d );
   71911                 : 
   71912            6886 :   if( !sqlite3TempInMemory(db) ){
   71913            1282 :     pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
   71914            1282 :     pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
   71915            1282 :     mxCache = db->aDb[0].pSchema->cache_size;
   71916            1282 :     if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
   71917            1282 :     pSorter->mxPmaSize = mxCache * pgsz;
   71918                 :   }
   71919                 : 
   71920            6886 :   return SQLITE_OK;
   71921                 : }
   71922                 : 
   71923                 : /*
   71924                 : ** Free the list of sorted records starting at pRecord.
   71925                 : */
   71926           11460 : static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
   71927                 :   SorterRecord *p;
   71928                 :   SorterRecord *pNext;
   71929           16042 :   for(p=pRecord; p; p=pNext){
   71930            4582 :     pNext = p->pNext;
   71931            4582 :     sqlite3DbFree(db, p);
   71932                 :   }
   71933           11460 : }
   71934                 : 
   71935                 : /*
   71936                 : ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
   71937                 : */
   71938          542116 : SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
   71939          542116 :   VdbeSorter *pSorter = pCsr->pSorter;
   71940          542116 :   if( pSorter ){
   71941            6886 :     if( pSorter->aIter ){
   71942                 :       int i;
   71943               0 :       for(i=0; i<pSorter->nTree; i++){
   71944               0 :         vdbeSorterIterZero(db, &pSorter->aIter[i]);
   71945                 :       }
   71946               0 :       sqlite3DbFree(db, pSorter->aIter);
   71947                 :     }
   71948            6886 :     if( pSorter->pTemp1 ){
   71949               0 :       sqlite3OsCloseFree(pSorter->pTemp1);
   71950                 :     }
   71951            6886 :     vdbeSorterRecordFree(db, pSorter->pRecord);
   71952            6886 :     sqlite3DbFree(db, pSorter->pUnpacked);
   71953            6886 :     sqlite3DbFree(db, pSorter);
   71954            6886 :     pCsr->pSorter = 0;
   71955                 :   }
   71956          542116 : }
   71957                 : 
   71958                 : /*
   71959                 : ** Allocate space for a file-handle and open a temporary file. If successful,
   71960                 : ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
   71961                 : ** Otherwise, set *ppFile to 0 and return an SQLite error code.
   71962                 : */
   71963               0 : static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
   71964                 :   int dummy;
   71965               0 :   return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
   71966                 :       SQLITE_OPEN_TEMP_JOURNAL |
   71967                 :       SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
   71968                 :       SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
   71969                 :   );
   71970                 : }
   71971                 : 
   71972                 : /*
   71973                 : ** Merge the two sorted lists p1 and p2 into a single list.
   71974                 : ** Set *ppOut to the head of the new list.
   71975                 : */
   71976          444395 : static void vdbeSorterMerge(
   71977                 :   VdbeCursor *pCsr,               /* For pKeyInfo */
   71978                 :   SorterRecord *p1,               /* First list to merge */
   71979                 :   SorterRecord *p2,               /* Second list to merge */
   71980                 :   SorterRecord **ppOut            /* OUT: Head of merged list */
   71981                 : ){
   71982          444395 :   SorterRecord *pFinal = 0;
   71983          444395 :   SorterRecord **pp = &pFinal;
   71984          444395 :   void *pVal2 = p2 ? p2->pVal : 0;
   71985                 : 
   71986          901988 :   while( p1 && p2 ){
   71987                 :     int res;
   71988           14009 :     vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
   71989           14009 :     if( res<=0 ){
   71990            9932 :       *pp = p1;
   71991            9932 :       pp = &p1->pNext;
   71992            9932 :       p1 = p1->pNext;
   71993            9932 :       pVal2 = 0;
   71994                 :     }else{
   71995            4077 :       *pp = p2;
   71996            4077 :        pp = &p2->pNext;
   71997            4077 :       p2 = p2->pNext;
   71998            4077 :       if( p2==0 ) break;
   71999            3266 :       pVal2 = p2->pVal;
   72000                 :     }
   72001                 :   }
   72002          444395 :   *pp = p1 ? p1 : p2;
   72003          444395 :   *ppOut = pFinal;
   72004          444395 : }
   72005                 : 
   72006                 : /*
   72007                 : ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
   72008                 : ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
   72009                 : ** occurs.
   72010                 : */
   72011            6886 : static int vdbeSorterSort(VdbeCursor *pCsr){
   72012                 :   int i;
   72013                 :   SorterRecord **aSlot;
   72014                 :   SorterRecord *p;
   72015            6886 :   VdbeSorter *pSorter = pCsr->pSorter;
   72016                 : 
   72017            6886 :   aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
   72018            6886 :   if( !aSlot ){
   72019               0 :     return SQLITE_NOMEM;
   72020                 :   }
   72021                 : 
   72022            6886 :   p = pSorter->pRecord;
   72023           18354 :   while( p ){
   72024            4582 :     SorterRecord *pNext = p->pNext;
   72025            4582 :     p->pNext = 0;
   72026            8273 :     for(i=0; aSlot[i]; i++){
   72027            3691 :       vdbeSorterMerge(pCsr, p, aSlot[i], &p);
   72028            3691 :       aSlot[i] = 0;
   72029                 :     }
   72030            4582 :     aSlot[i] = p;
   72031            4582 :     p = pNext;
   72032                 :   }
   72033                 : 
   72034            6886 :   p = 0;
   72035          447590 :   for(i=0; i<64; i++){
   72036          440704 :     vdbeSorterMerge(pCsr, p, aSlot[i], &p);
   72037                 :   }
   72038            6886 :   pSorter->pRecord = p;
   72039                 : 
   72040            6886 :   sqlite3_free(aSlot);
   72041            6886 :   return SQLITE_OK;
   72042                 : }
   72043                 : 
   72044                 : 
   72045                 : /*
   72046                 : ** Write the current contents of the in-memory linked-list to a PMA. Return
   72047                 : ** SQLITE_OK if successful, or an SQLite error code otherwise.
   72048                 : **
   72049                 : ** The format of a PMA is:
   72050                 : **
   72051                 : **     * A varint. This varint contains the total number of bytes of content
   72052                 : **       in the PMA (not including the varint itself).
   72053                 : **
   72054                 : **     * One or more records packed end-to-end in order of ascending keys. 
   72055                 : **       Each record consists of a varint followed by a blob of data (the 
   72056                 : **       key). The varint is the number of bytes in the blob of data.
   72057                 : */
   72058               0 : static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
   72059               0 :   int rc = SQLITE_OK;             /* Return code */
   72060               0 :   VdbeSorter *pSorter = pCsr->pSorter;
   72061                 : 
   72062               0 :   if( pSorter->nInMemory==0 ){
   72063               0 :     assert( pSorter->pRecord==0 );
   72064               0 :     return rc;
   72065                 :   }
   72066                 : 
   72067               0 :   rc = vdbeSorterSort(pCsr);
   72068                 : 
   72069                 :   /* If the first temporary PMA file has not been opened, open it now. */
   72070               0 :   if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
   72071               0 :     rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
   72072               0 :     assert( rc!=SQLITE_OK || pSorter->pTemp1 );
   72073               0 :     assert( pSorter->iWriteOff==0 );
   72074               0 :     assert( pSorter->nPMA==0 );
   72075                 :   }
   72076                 : 
   72077               0 :   if( rc==SQLITE_OK ){
   72078               0 :     i64 iOff = pSorter->iWriteOff;
   72079                 :     SorterRecord *p;
   72080               0 :     SorterRecord *pNext = 0;
   72081                 :     static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
   72082                 : 
   72083               0 :     pSorter->nPMA++;
   72084               0 :     rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
   72085               0 :     for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
   72086               0 :       pNext = p->pNext;
   72087               0 :       rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
   72088                 : 
   72089               0 :       if( rc==SQLITE_OK ){
   72090               0 :         rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
   72091               0 :         iOff += p->nVal;
   72092                 :       }
   72093                 : 
   72094               0 :       sqlite3DbFree(db, p);
   72095                 :     }
   72096                 : 
   72097                 :     /* This assert verifies that unless an error has occurred, the size of 
   72098                 :     ** the PMA on disk is the same as the expected size stored in
   72099                 :     ** pSorter->nInMemory. */ 
   72100               0 :     assert( rc!=SQLITE_OK || pSorter->nInMemory==(
   72101                 :           iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
   72102                 :     ));
   72103                 : 
   72104               0 :     pSorter->iWriteOff = iOff;
   72105               0 :     if( rc==SQLITE_OK ){
   72106                 :       /* Terminate each file with 8 extra bytes so that from any offset
   72107                 :       ** in the file we can always read 9 bytes without a SHORT_READ error */
   72108               0 :       rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
   72109                 :     }
   72110               0 :     pSorter->pRecord = p;
   72111                 :   }
   72112                 : 
   72113               0 :   return rc;
   72114                 : }
   72115                 : 
   72116                 : /*
   72117                 : ** Add a record to the sorter.
   72118                 : */
   72119            4582 : SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
   72120                 :   sqlite3 *db,                    /* Database handle */
   72121                 :   VdbeCursor *pCsr,               /* Sorter cursor */
   72122                 :   Mem *pVal                       /* Memory cell containing record */
   72123                 : ){
   72124            4582 :   VdbeSorter *pSorter = pCsr->pSorter;
   72125            4582 :   int rc = SQLITE_OK;             /* Return Code */
   72126                 :   SorterRecord *pNew;             /* New list element */
   72127                 : 
   72128            4582 :   assert( pSorter );
   72129            4582 :   pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
   72130                 : 
   72131            4582 :   pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
   72132            4582 :   if( pNew==0 ){
   72133               0 :     rc = SQLITE_NOMEM;
   72134                 :   }else{
   72135            4582 :     pNew->pVal = (void *)&pNew[1];
   72136            4582 :     memcpy(pNew->pVal, pVal->z, pVal->n);
   72137            4582 :     pNew->nVal = pVal->n;
   72138            4582 :     pNew->pNext = pSorter->pRecord;
   72139            4582 :     pSorter->pRecord = pNew;
   72140                 :   }
   72141                 : 
   72142                 :   /* See if the contents of the sorter should now be written out. They
   72143                 :   ** are written out when either of the following are true:
   72144                 :   **
   72145                 :   **   * The total memory allocated for the in-memory list is greater 
   72146                 :   **     than (page-size * cache-size), or
   72147                 :   **
   72148                 :   **   * The total memory allocated for the in-memory list is greater 
   72149                 :   **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
   72150                 :   */
   72151            7002 :   if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
   72152            2420 :         (pSorter->nInMemory>pSorter->mxPmaSize)
   72153            2420 :      || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
   72154                 :   )){
   72155               0 :     rc = vdbeSorterListToPMA(db, pCsr);
   72156               0 :     pSorter->nInMemory = 0;
   72157                 :   }
   72158                 : 
   72159            4582 :   return rc;
   72160                 : }
   72161                 : 
   72162                 : /*
   72163                 : ** Helper function for sqlite3VdbeSorterRewind(). 
   72164                 : */
   72165               0 : static int vdbeSorterInitMerge(
   72166                 :   sqlite3 *db,                    /* Database handle */
   72167                 :   VdbeCursor *pCsr,               /* Cursor handle for this sorter */
   72168                 :   i64 *pnByte                     /* Sum of bytes in all opened PMAs */
   72169                 : ){
   72170               0 :   VdbeSorter *pSorter = pCsr->pSorter;
   72171               0 :   int rc = SQLITE_OK;             /* Return code */
   72172                 :   int i;                          /* Used to iterator through aIter[] */
   72173               0 :   i64 nByte = 0;                  /* Total bytes in all opened PMAs */
   72174                 : 
   72175                 :   /* Initialize the iterators. */
   72176               0 :   for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
   72177               0 :     VdbeSorterIter *pIter = &pSorter->aIter[i];
   72178               0 :     rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
   72179               0 :     pSorter->iReadOff = pIter->iEof;
   72180               0 :     assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
   72181               0 :     if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
   72182                 :   }
   72183                 : 
   72184                 :   /* Initialize the aTree[] array. */
   72185               0 :   for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
   72186               0 :     rc = vdbeSorterDoCompare(pCsr, i);
   72187                 :   }
   72188                 : 
   72189               0 :   *pnByte = nByte;
   72190               0 :   return rc;
   72191                 : }
   72192                 : 
   72193                 : /*
   72194                 : ** Once the sorter has been populated, this function is called to prepare
   72195                 : ** for iterating through its contents in sorted order.
   72196                 : */
   72197            6886 : SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
   72198            6886 :   VdbeSorter *pSorter = pCsr->pSorter;
   72199                 :   int rc;                         /* Return code */
   72200            6886 :   sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
   72201            6886 :   i64 iWrite2 = 0;                /* Write offset for pTemp2 */
   72202                 :   int nIter;                      /* Number of iterators used */
   72203                 :   int nByte;                      /* Bytes of space required for aIter/aTree */
   72204            6886 :   int N = 2;                      /* Power of 2 >= nIter */
   72205                 : 
   72206            6886 :   assert( pSorter );
   72207                 : 
   72208                 :   /* If no data has been written to disk, then do not do so now. Instead,
   72209                 :   ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
   72210                 :   ** from the in-memory list.  */
   72211            6886 :   if( pSorter->nPMA==0 ){
   72212            6886 :     *pbEof = !pSorter->pRecord;
   72213            6886 :     assert( pSorter->aTree==0 );
   72214            6886 :     return vdbeSorterSort(pCsr);
   72215                 :   }
   72216                 : 
   72217                 :   /* Write the current b-tree to a PMA. Close the b-tree cursor. */
   72218               0 :   rc = vdbeSorterListToPMA(db, pCsr);
   72219               0 :   if( rc!=SQLITE_OK ) return rc;
   72220                 : 
   72221                 :   /* Allocate space for aIter[] and aTree[]. */
   72222               0 :   nIter = pSorter->nPMA;
   72223               0 :   if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
   72224               0 :   assert( nIter>0 );
   72225               0 :   while( N<nIter ) N += N;
   72226               0 :   nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
   72227               0 :   pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
   72228               0 :   if( !pSorter->aIter ) return SQLITE_NOMEM;
   72229               0 :   pSorter->aTree = (int *)&pSorter->aIter[N];
   72230               0 :   pSorter->nTree = N;
   72231                 : 
   72232                 :   do {
   72233                 :     int iNew;                     /* Index of new, merged, PMA */
   72234                 : 
   72235               0 :     for(iNew=0; 
   72236               0 :         rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
   72237               0 :         iNew++
   72238                 :     ){
   72239                 :       i64 nWrite;                 /* Number of bytes in new PMA */
   72240                 : 
   72241                 :       /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
   72242                 :       ** initialize an iterator for each of them and break out of the loop.
   72243                 :       ** These iterators will be incrementally merged as the VDBE layer calls
   72244                 :       ** sqlite3VdbeSorterNext().
   72245                 :       **
   72246                 :       ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
   72247                 :       ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
   72248                 :       ** are merged into a single PMA that is written to file pTemp2.
   72249                 :       */
   72250               0 :       rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
   72251               0 :       assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
   72252               0 :       if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
   72253                 :         break;
   72254                 :       }
   72255                 : 
   72256                 :       /* Open the second temp file, if it is not already open. */
   72257               0 :       if( pTemp2==0 ){
   72258               0 :         assert( iWrite2==0 );
   72259               0 :         rc = vdbeSorterOpenTempFile(db, &pTemp2);
   72260                 :       }
   72261                 : 
   72262               0 :       if( rc==SQLITE_OK ){
   72263               0 :         rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
   72264                 :       }
   72265                 : 
   72266               0 :       if( rc==SQLITE_OK ){
   72267               0 :         int bEof = 0;
   72268               0 :         while( rc==SQLITE_OK && bEof==0 ){
   72269                 :           int nToWrite;
   72270               0 :           VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
   72271               0 :           assert( pIter->pFile );
   72272               0 :           nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
   72273               0 :           rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
   72274               0 :           iWrite2 += nToWrite;
   72275               0 :           if( rc==SQLITE_OK ){
   72276               0 :             rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
   72277                 :           }
   72278                 :         }
   72279                 :       }
   72280                 :     }
   72281                 : 
   72282               0 :     if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
   72283               0 :       break;
   72284                 :     }else{
   72285               0 :       sqlite3_file *pTmp = pSorter->pTemp1;
   72286               0 :       pSorter->nPMA = iNew;
   72287               0 :       pSorter->pTemp1 = pTemp2;
   72288               0 :       pTemp2 = pTmp;
   72289               0 :       pSorter->iWriteOff = iWrite2;
   72290               0 :       pSorter->iReadOff = 0;
   72291               0 :       iWrite2 = 0;
   72292                 :     }
   72293               0 :   }while( rc==SQLITE_OK );
   72294                 : 
   72295               0 :   if( pTemp2 ){
   72296               0 :     sqlite3OsCloseFree(pTemp2);
   72297                 :   }
   72298               0 :   *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
   72299               0 :   return rc;
   72300                 : }
   72301                 : 
   72302                 : /*
   72303                 : ** Advance to the next element in the sorter.
   72304                 : */
   72305            4574 : SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
   72306            4574 :   VdbeSorter *pSorter = pCsr->pSorter;
   72307                 :   int rc;                         /* Return code */
   72308                 : 
   72309            4574 :   if( pSorter->aTree ){
   72310               0 :     int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
   72311                 :     int i;                        /* Index of aTree[] to recalculate */
   72312                 : 
   72313               0 :     rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
   72314               0 :     for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
   72315               0 :       rc = vdbeSorterDoCompare(pCsr, i);
   72316                 :     }
   72317                 : 
   72318               0 :     *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
   72319                 :   }else{
   72320            4574 :     SorterRecord *pFree = pSorter->pRecord;
   72321            4574 :     pSorter->pRecord = pFree->pNext;
   72322            4574 :     pFree->pNext = 0;
   72323            4574 :     vdbeSorterRecordFree(db, pFree);
   72324            4574 :     *pbEof = !pSorter->pRecord;
   72325            4574 :     rc = SQLITE_OK;
   72326                 :   }
   72327            4574 :   return rc;
   72328                 : }
   72329                 : 
   72330                 : /*
   72331                 : ** Return a pointer to a buffer owned by the sorter that contains the 
   72332                 : ** current key.
   72333                 : */
   72334            5025 : static void *vdbeSorterRowkey(
   72335                 :   VdbeSorter *pSorter,            /* Sorter object */
   72336                 :   int *pnKey                      /* OUT: Size of current key in bytes */
   72337                 : ){
   72338                 :   void *pKey;
   72339            5025 :   if( pSorter->aTree ){
   72340                 :     VdbeSorterIter *pIter;
   72341               0 :     pIter = &pSorter->aIter[ pSorter->aTree[1] ];
   72342               0 :     *pnKey = pIter->nKey;
   72343               0 :     pKey = pIter->aKey;
   72344                 :   }else{
   72345            5025 :     *pnKey = pSorter->pRecord->nVal;
   72346            5025 :     pKey = pSorter->pRecord->pVal;
   72347                 :   }
   72348            5025 :   return pKey;
   72349                 : }
   72350                 : 
   72351                 : /*
   72352                 : ** Copy the current sorter key into the memory cell pOut.
   72353                 : */
   72354            4582 : SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
   72355            4582 :   VdbeSorter *pSorter = pCsr->pSorter;
   72356                 :   void *pKey; int nKey;           /* Sorter key to copy into pOut */
   72357                 : 
   72358            4582 :   pKey = vdbeSorterRowkey(pSorter, &nKey);
   72359            4582 :   if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
   72360               0 :     return SQLITE_NOMEM;
   72361                 :   }
   72362            4582 :   pOut->n = nKey;
   72363            4582 :   MemSetTypeFlag(pOut, MEM_Blob);
   72364            4582 :   memcpy(pOut->z, pKey, nKey);
   72365                 : 
   72366            4582 :   return SQLITE_OK;
   72367                 : }
   72368                 : 
   72369                 : /*
   72370                 : ** Compare the key in memory cell pVal with the key that the sorter cursor
   72371                 : ** passed as the first argument currently points to. For the purposes of
   72372                 : ** the comparison, ignore the rowid field at the end of each record.
   72373                 : **
   72374                 : ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
   72375                 : ** Otherwise, set *pRes to a negative, zero or positive value if the
   72376                 : ** key in pVal is smaller than, equal to or larger than the current sorter
   72377                 : ** key.
   72378                 : */
   72379             443 : SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
   72380                 :   VdbeCursor *pCsr,               /* Sorter cursor */
   72381                 :   Mem *pVal,                      /* Value to compare to current sorter key */
   72382                 :   int *pRes                       /* OUT: Result of comparison */
   72383                 : ){
   72384             443 :   VdbeSorter *pSorter = pCsr->pSorter;
   72385                 :   void *pKey; int nKey;           /* Sorter key to compare pVal with */
   72386                 : 
   72387             443 :   pKey = vdbeSorterRowkey(pSorter, &nKey);
   72388             443 :   vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
   72389             443 :   return SQLITE_OK;
   72390                 : }
   72391                 : 
   72392                 : #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
   72393                 : 
   72394                 : /************** End of vdbesort.c ********************************************/
   72395                 : /************** Begin file journal.c *****************************************/
   72396                 : /*
   72397                 : ** 2007 August 22
   72398                 : **
   72399                 : ** The author disclaims copyright to this source code.  In place of
   72400                 : ** a legal notice, here is a blessing:
   72401                 : **
   72402                 : **    May you do good and not evil.
   72403                 : **    May you find forgiveness for yourself and forgive others.
   72404                 : **    May you share freely, never taking more than you give.
   72405                 : **
   72406                 : *************************************************************************
   72407                 : **
   72408                 : ** This file implements a special kind of sqlite3_file object used
   72409                 : ** by SQLite to create journal files if the atomic-write optimization
   72410                 : ** is enabled.
   72411                 : **
   72412                 : ** The distinctive characteristic of this sqlite3_file is that the
   72413                 : ** actual on disk file is created lazily. When the file is created,
   72414                 : ** the caller specifies a buffer size for an in-memory buffer to
   72415                 : ** be used to service read() and write() requests. The actual file
   72416                 : ** on disk is not created or populated until either:
   72417                 : **
   72418                 : **   1) The in-memory representation grows too large for the allocated 
   72419                 : **      buffer, or
   72420                 : **   2) The sqlite3JournalCreate() function is called.
   72421                 : */
   72422                 : #ifdef SQLITE_ENABLE_ATOMIC_WRITE
   72423                 : 
   72424                 : 
   72425                 : /*
   72426                 : ** A JournalFile object is a subclass of sqlite3_file used by
   72427                 : ** as an open file handle for journal files.
   72428                 : */
   72429                 : struct JournalFile {
   72430                 :   sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
   72431                 :   int nBuf;                       /* Size of zBuf[] in bytes */
   72432                 :   char *zBuf;                     /* Space to buffer journal writes */
   72433                 :   int iSize;                      /* Amount of zBuf[] currently used */
   72434                 :   int flags;                      /* xOpen flags */
   72435                 :   sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
   72436                 :   sqlite3_file *pReal;            /* The "real" underlying file descriptor */
   72437                 :   const char *zJournal;           /* Name of the journal file */
   72438                 : };
   72439                 : typedef struct JournalFile JournalFile;
   72440                 : 
   72441                 : /*
   72442                 : ** If it does not already exists, create and populate the on-disk file 
   72443                 : ** for JournalFile p.
   72444                 : */
   72445                 : static int createFile(JournalFile *p){
   72446                 :   int rc = SQLITE_OK;
   72447                 :   if( !p->pReal ){
   72448                 :     sqlite3_file *pReal = (sqlite3_file *)&p[1];
   72449                 :     rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
   72450                 :     if( rc==SQLITE_OK ){
   72451                 :       p->pReal = pReal;
   72452                 :       if( p->iSize>0 ){
   72453                 :         assert(p->iSize<=p->nBuf);
   72454                 :         rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
   72455                 :       }
   72456                 :     }
   72457                 :   }
   72458                 :   return rc;
   72459                 : }
   72460                 : 
   72461                 : /*
   72462                 : ** Close the file.
   72463                 : */
   72464                 : static int jrnlClose(sqlite3_file *pJfd){
   72465                 :   JournalFile *p = (JournalFile *)pJfd;
   72466                 :   if( p->pReal ){
   72467                 :     sqlite3OsClose(p->pReal);
   72468                 :   }
   72469                 :   sqlite3_free(p->zBuf);
   72470                 :   return SQLITE_OK;
   72471                 : }
   72472                 : 
   72473                 : /*
   72474                 : ** Read data from the file.
   72475                 : */
   72476                 : static int jrnlRead(
   72477                 :   sqlite3_file *pJfd,    /* The journal file from which to read */
   72478                 :   void *zBuf,            /* Put the results here */
   72479                 :   int iAmt,              /* Number of bytes to read */
   72480                 :   sqlite_int64 iOfst     /* Begin reading at this offset */
   72481                 : ){
   72482                 :   int rc = SQLITE_OK;
   72483                 :   JournalFile *p = (JournalFile *)pJfd;
   72484                 :   if( p->pReal ){
   72485                 :     rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
   72486                 :   }else if( (iAmt+iOfst)>p->iSize ){
   72487                 :     rc = SQLITE_IOERR_SHORT_READ;
   72488                 :   }else{
   72489                 :     memcpy(zBuf, &p->zBuf[iOfst], iAmt);
   72490                 :   }
   72491                 :   return rc;
   72492                 : }
   72493                 : 
   72494                 : /*
   72495                 : ** Write data to the file.
   72496                 : */
   72497                 : static int jrnlWrite(
   72498                 :   sqlite3_file *pJfd,    /* The journal file into which to write */
   72499                 :   const void *zBuf,      /* Take data to be written from here */
   72500                 :   int iAmt,              /* Number of bytes to write */
   72501                 :   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
   72502                 : ){
   72503                 :   int rc = SQLITE_OK;
   72504                 :   JournalFile *p = (JournalFile *)pJfd;
   72505                 :   if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
   72506                 :     rc = createFile(p);
   72507                 :   }
   72508                 :   if( rc==SQLITE_OK ){
   72509                 :     if( p->pReal ){
   72510                 :       rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
   72511                 :     }else{
   72512                 :       memcpy(&p->zBuf[iOfst], zBuf, iAmt);
   72513                 :       if( p->iSize<(iOfst+iAmt) ){
   72514                 :         p->iSize = (iOfst+iAmt);
   72515                 :       }
   72516                 :     }
   72517                 :   }
   72518                 :   return rc;
   72519                 : }
   72520                 : 
   72521                 : /*
   72522                 : ** Truncate the file.
   72523                 : */
   72524                 : static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
   72525                 :   int rc = SQLITE_OK;
   72526                 :   JournalFile *p = (JournalFile *)pJfd;
   72527                 :   if( p->pReal ){
   72528                 :     rc = sqlite3OsTruncate(p->pReal, size);
   72529                 :   }else if( size<p->iSize ){
   72530                 :     p->iSize = size;
   72531                 :   }
   72532                 :   return rc;
   72533                 : }
   72534                 : 
   72535                 : /*
   72536                 : ** Sync the file.
   72537                 : */
   72538                 : static int jrnlSync(sqlite3_file *pJfd, int flags){
   72539                 :   int rc;
   72540                 :   JournalFile *p = (JournalFile *)pJfd;
   72541                 :   if( p->pReal ){
   72542                 :     rc = sqlite3OsSync(p->pReal, flags);
   72543                 :   }else{
   72544                 :     rc = SQLITE_OK;
   72545                 :   }
   72546                 :   return rc;
   72547                 : }
   72548                 : 
   72549                 : /*
   72550                 : ** Query the size of the file in bytes.
   72551                 : */
   72552                 : static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
   72553                 :   int rc = SQLITE_OK;
   72554                 :   JournalFile *p = (JournalFile *)pJfd;
   72555                 :   if( p->pReal ){
   72556                 :     rc = sqlite3OsFileSize(p->pReal, pSize);
   72557                 :   }else{
   72558                 :     *pSize = (sqlite_int64) p->iSize;
   72559                 :   }
   72560                 :   return rc;
   72561                 : }
   72562                 : 
   72563                 : /*
   72564                 : ** Table of methods for JournalFile sqlite3_file object.
   72565                 : */
   72566                 : static struct sqlite3_io_methods JournalFileMethods = {
   72567                 :   1,             /* iVersion */
   72568                 :   jrnlClose,     /* xClose */
   72569                 :   jrnlRead,      /* xRead */
   72570                 :   jrnlWrite,     /* xWrite */
   72571                 :   jrnlTruncate,  /* xTruncate */
   72572                 :   jrnlSync,      /* xSync */
   72573                 :   jrnlFileSize,  /* xFileSize */
   72574                 :   0,             /* xLock */
   72575                 :   0,             /* xUnlock */
   72576                 :   0,             /* xCheckReservedLock */
   72577                 :   0,             /* xFileControl */
   72578                 :   0,             /* xSectorSize */
   72579                 :   0,             /* xDeviceCharacteristics */
   72580                 :   0,             /* xShmMap */
   72581                 :   0,             /* xShmLock */
   72582                 :   0,             /* xShmBarrier */
   72583                 :   0              /* xShmUnmap */
   72584                 : };
   72585                 : 
   72586                 : /* 
   72587                 : ** Open a journal file.
   72588                 : */
   72589                 : SQLITE_PRIVATE int sqlite3JournalOpen(
   72590                 :   sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
   72591                 :   const char *zName,         /* Name of the journal file */
   72592                 :   sqlite3_file *pJfd,        /* Preallocated, blank file handle */
   72593                 :   int flags,                 /* Opening flags */
   72594                 :   int nBuf                   /* Bytes buffered before opening the file */
   72595                 : ){
   72596                 :   JournalFile *p = (JournalFile *)pJfd;
   72597                 :   memset(p, 0, sqlite3JournalSize(pVfs));
   72598                 :   if( nBuf>0 ){
   72599                 :     p->zBuf = sqlite3MallocZero(nBuf);
   72600                 :     if( !p->zBuf ){
   72601                 :       return SQLITE_NOMEM;
   72602                 :     }
   72603                 :   }else{
   72604                 :     return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
   72605                 :   }
   72606                 :   p->pMethod = &JournalFileMethods;
   72607                 :   p->nBuf = nBuf;
   72608                 :   p->flags = flags;
   72609                 :   p->zJournal = zName;
   72610                 :   p->pVfs = pVfs;
   72611                 :   return SQLITE_OK;
   72612                 : }
   72613                 : 
   72614                 : /*
   72615                 : ** If the argument p points to a JournalFile structure, and the underlying
   72616                 : ** file has not yet been created, create it now.
   72617                 : */
   72618                 : SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
   72619                 :   if( p->pMethods!=&JournalFileMethods ){
   72620                 :     return SQLITE_OK;
   72621                 :   }
   72622                 :   return createFile((JournalFile *)p);
   72623                 : }
   72624                 : 
   72625                 : /* 
   72626                 : ** Return the number of bytes required to store a JournalFile that uses vfs
   72627                 : ** pVfs to create the underlying on-disk files.
   72628                 : */
   72629                 : SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
   72630                 :   return (pVfs->szOsFile+sizeof(JournalFile));
   72631                 : }
   72632                 : #endif
   72633                 : 
   72634                 : /************** End of journal.c *********************************************/
   72635                 : /************** Begin file memjournal.c **************************************/
   72636                 : /*
   72637                 : ** 2008 October 7
   72638                 : **
   72639                 : ** The author disclaims copyright to this source code.  In place of
   72640                 : ** a legal notice, here is a blessing:
   72641                 : **
   72642                 : **    May you do good and not evil.
   72643                 : **    May you find forgiveness for yourself and forgive others.
   72644                 : **    May you share freely, never taking more than you give.
   72645                 : **
   72646                 : *************************************************************************
   72647                 : **
   72648                 : ** This file contains code use to implement an in-memory rollback journal.
   72649                 : ** The in-memory rollback journal is used to journal transactions for
   72650                 : ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
   72651                 : */
   72652                 : 
   72653                 : /* Forward references to internal structures */
   72654                 : typedef struct MemJournal MemJournal;
   72655                 : typedef struct FilePoint FilePoint;
   72656                 : typedef struct FileChunk FileChunk;
   72657                 : 
   72658                 : /* Space to hold the rollback journal is allocated in increments of
   72659                 : ** this many bytes.
   72660                 : **
   72661                 : ** The size chosen is a little less than a power of two.  That way,
   72662                 : ** the FileChunk object will have a size that almost exactly fills
   72663                 : ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
   72664                 : ** memory allocators.
   72665                 : */
   72666                 : #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
   72667                 : 
   72668                 : /* Macro to find the minimum of two numeric values.
   72669                 : */
   72670                 : #ifndef MIN
   72671                 : # define MIN(x,y) ((x)<(y)?(x):(y))
   72672                 : #endif
   72673                 : 
   72674                 : /*
   72675                 : ** The rollback journal is composed of a linked list of these structures.
   72676                 : */
   72677                 : struct FileChunk {
   72678                 :   FileChunk *pNext;               /* Next chunk in the journal */
   72679                 :   u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
   72680                 : };
   72681                 : 
   72682                 : /*
   72683                 : ** An instance of this object serves as a cursor into the rollback journal.
   72684                 : ** The cursor can be either for reading or writing.
   72685                 : */
   72686                 : struct FilePoint {
   72687                 :   sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
   72688                 :   FileChunk *pChunk;              /* Specific chunk into which cursor points */
   72689                 : };
   72690                 : 
   72691                 : /*
   72692                 : ** This subclass is a subclass of sqlite3_file.  Each open memory-journal
   72693                 : ** is an instance of this class.
   72694                 : */
   72695                 : struct MemJournal {
   72696                 :   sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
   72697                 :   FileChunk *pFirst;              /* Head of in-memory chunk-list */
   72698                 :   FilePoint endpoint;             /* Pointer to the end of the file */
   72699                 :   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
   72700                 : };
   72701                 : 
   72702                 : /*
   72703                 : ** Read data from the in-memory journal file.  This is the implementation
   72704                 : ** of the sqlite3_vfs.xRead method.
   72705                 : */
   72706              33 : static int memjrnlRead(
   72707                 :   sqlite3_file *pJfd,    /* The journal file from which to read */
   72708                 :   void *zBuf,            /* Put the results here */
   72709                 :   int iAmt,              /* Number of bytes to read */
   72710                 :   sqlite_int64 iOfst     /* Begin reading at this offset */
   72711                 : ){
   72712              33 :   MemJournal *p = (MemJournal *)pJfd;
   72713              33 :   u8 *zOut = zBuf;
   72714              33 :   int nRead = iAmt;
   72715                 :   int iChunkOffset;
   72716                 :   FileChunk *pChunk;
   72717                 : 
   72718                 :   /* SQLite never tries to read past the end of a rollback journal file */
   72719              33 :   assert( iOfst+iAmt<=p->endpoint.iOffset );
   72720                 : 
   72721              42 :   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
   72722               9 :     sqlite3_int64 iOff = 0;
   72723              18 :     for(pChunk=p->pFirst; 
   72724               0 :         ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
   72725               0 :         pChunk=pChunk->pNext
   72726                 :     ){
   72727               0 :       iOff += JOURNAL_CHUNKSIZE;
   72728                 :     }
   72729                 :   }else{
   72730              24 :     pChunk = p->readpoint.pChunk;
   72731                 :   }
   72732                 : 
   72733              33 :   iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
   72734                 :   do {
   72735              33 :     int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
   72736              33 :     int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
   72737              33 :     memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
   72738              33 :     zOut += nCopy;
   72739              33 :     nRead -= iSpace;
   72740              33 :     iChunkOffset = 0;
   72741              33 :   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
   72742              33 :   p->readpoint.iOffset = iOfst+iAmt;
   72743              33 :   p->readpoint.pChunk = pChunk;
   72744                 : 
   72745              33 :   return SQLITE_OK;
   72746                 : }
   72747                 : 
   72748                 : /*
   72749                 : ** Write data to the file.
   72750                 : */
   72751           88414 : static int memjrnlWrite(
   72752                 :   sqlite3_file *pJfd,    /* The journal file into which to write */
   72753                 :   const void *zBuf,      /* Take data to be written from here */
   72754                 :   int iAmt,              /* Number of bytes to write */
   72755                 :   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
   72756                 : ){
   72757           88414 :   MemJournal *p = (MemJournal *)pJfd;
   72758           88414 :   int nWrite = iAmt;
   72759           88414 :   u8 *zWrite = (u8 *)zBuf;
   72760                 : 
   72761                 :   /* An in-memory journal file should only ever be appended to. Random
   72762                 :   ** access writes are not required by sqlite.
   72763                 :   */
   72764           88414 :   assert( iOfst==p->endpoint.iOffset );
   72765                 :   UNUSED_PARAMETER(iOfst);
   72766                 : 
   72767         1611527 :   while( nWrite>0 ){
   72768         1434699 :     FileChunk *pChunk = p->endpoint.pChunk;
   72769         1434699 :     int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
   72770         1434699 :     int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
   72771                 : 
   72772         1434699 :     if( iChunkOffset==0 ){
   72773                 :       /* New chunk is required to extend the file. */
   72774         1362235 :       FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
   72775         1362235 :       if( !pNew ){
   72776               0 :         return SQLITE_IOERR_NOMEM;
   72777                 :       }
   72778         1362235 :       pNew->pNext = 0;
   72779         1362235 :       if( pChunk ){
   72780         1346285 :         assert( p->pFirst );
   72781         1346285 :         pChunk->pNext = pNew;
   72782                 :       }else{
   72783           15950 :         assert( !p->pFirst );
   72784           15950 :         p->pFirst = pNew;
   72785                 :       }
   72786         1362235 :       p->endpoint.pChunk = pNew;
   72787                 :     }
   72788                 : 
   72789         1434699 :     memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
   72790         1434699 :     zWrite += iSpace;
   72791         1434699 :     nWrite -= iSpace;
   72792         1434699 :     p->endpoint.iOffset += iSpace;
   72793                 :   }
   72794                 : 
   72795           88414 :   return SQLITE_OK;
   72796                 : }
   72797                 : 
   72798                 : /*
   72799                 : ** Truncate the file.
   72800                 : */
   72801           22902 : static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
   72802           22902 :   MemJournal *p = (MemJournal *)pJfd;
   72803                 :   FileChunk *pChunk;
   72804           22902 :   assert(size==0);
   72805                 :   UNUSED_PARAMETER(size);
   72806           22902 :   pChunk = p->pFirst;
   72807         1408039 :   while( pChunk ){
   72808         1362235 :     FileChunk *pTmp = pChunk;
   72809         1362235 :     pChunk = pChunk->pNext;
   72810         1362235 :     sqlite3_free(pTmp);
   72811                 :   }
   72812           22902 :   sqlite3MemJournalOpen(pJfd);
   72813           22902 :   return SQLITE_OK;
   72814                 : }
   72815                 : 
   72816                 : /*
   72817                 : ** Close the file.
   72818                 : */
   72819            7210 : static int memjrnlClose(sqlite3_file *pJfd){
   72820            7210 :   memjrnlTruncate(pJfd, 0);
   72821            7210 :   return SQLITE_OK;
   72822                 : }
   72823                 : 
   72824                 : 
   72825                 : /*
   72826                 : ** Sync the file.
   72827                 : **
   72828                 : ** Syncing an in-memory journal is a no-op.  And, in fact, this routine
   72829                 : ** is never called in a working implementation.  This implementation
   72830                 : ** exists purely as a contingency, in case some malfunction in some other
   72831                 : ** part of SQLite causes Sync to be called by mistake.
   72832                 : */
   72833               0 : static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
   72834                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   72835               0 :   return SQLITE_OK;
   72836                 : }
   72837                 : 
   72838                 : /*
   72839                 : ** Query the size of the file in bytes.
   72840                 : */
   72841               9 : static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
   72842               9 :   MemJournal *p = (MemJournal *)pJfd;
   72843               9 :   *pSize = (sqlite_int64) p->endpoint.iOffset;
   72844               9 :   return SQLITE_OK;
   72845                 : }
   72846                 : 
   72847                 : /*
   72848                 : ** Table of methods for MemJournal sqlite3_file object.
   72849                 : */
   72850                 : static const struct sqlite3_io_methods MemJournalMethods = {
   72851                 :   1,                /* iVersion */
   72852                 :   memjrnlClose,     /* xClose */
   72853                 :   memjrnlRead,      /* xRead */
   72854                 :   memjrnlWrite,     /* xWrite */
   72855                 :   memjrnlTruncate,  /* xTruncate */
   72856                 :   memjrnlSync,      /* xSync */
   72857                 :   memjrnlFileSize,  /* xFileSize */
   72858                 :   0,                /* xLock */
   72859                 :   0,                /* xUnlock */
   72860                 :   0,                /* xCheckReservedLock */
   72861                 :   0,                /* xFileControl */
   72862                 :   0,                /* xSectorSize */
   72863                 :   0,                /* xDeviceCharacteristics */
   72864                 :   0,                /* xShmMap */
   72865                 :   0,                /* xShmLock */
   72866                 :   0,                /* xShmBarrier */
   72867                 :   0                 /* xShmUnlock */
   72868                 : };
   72869                 : 
   72870                 : /* 
   72871                 : ** Open a journal file.
   72872                 : */
   72873           30112 : SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
   72874           30112 :   MemJournal *p = (MemJournal *)pJfd;
   72875           30112 :   assert( EIGHT_BYTE_ALIGNMENT(p) );
   72876           30112 :   memset(p, 0, sqlite3MemJournalSize());
   72877           30112 :   p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
   72878           30112 : }
   72879                 : 
   72880                 : /*
   72881                 : ** Return true if the file-handle passed as an argument is 
   72882                 : ** an in-memory journal 
   72883                 : */
   72884           99332 : SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
   72885           99332 :   return pJfd->pMethods==&MemJournalMethods;
   72886                 : }
   72887                 : 
   72888                 : /* 
   72889                 : ** Return the number of bytes required to store a MemJournal file descriptor.
   72890                 : */
   72891           49633 : SQLITE_PRIVATE int sqlite3MemJournalSize(void){
   72892           49633 :   return sizeof(MemJournal);
   72893                 : }
   72894                 : 
   72895                 : /************** End of memjournal.c ******************************************/
   72896                 : /************** Begin file walker.c ******************************************/
   72897                 : /*
   72898                 : ** 2008 August 16
   72899                 : **
   72900                 : ** The author disclaims copyright to this source code.  In place of
   72901                 : ** a legal notice, here is a blessing:
   72902                 : **
   72903                 : **    May you do good and not evil.
   72904                 : **    May you find forgiveness for yourself and forgive others.
   72905                 : **    May you share freely, never taking more than you give.
   72906                 : **
   72907                 : *************************************************************************
   72908                 : ** This file contains routines used for walking the parser tree for
   72909                 : ** an SQL statement.
   72910                 : */
   72911                 : /* #include <stdlib.h> */
   72912                 : /* #include <string.h> */
   72913                 : 
   72914                 : 
   72915                 : /*
   72916                 : ** Walk an expression tree.  Invoke the callback once for each node
   72917                 : ** of the expression, while decending.  (In other words, the callback
   72918                 : ** is invoked before visiting children.)
   72919                 : **
   72920                 : ** The return value from the callback should be one of the WRC_*
   72921                 : ** constants to specify how to proceed with the walk.
   72922                 : **
   72923                 : **    WRC_Continue      Continue descending down the tree.
   72924                 : **
   72925                 : **    WRC_Prune         Do not descend into child nodes.  But allow
   72926                 : **                      the walk to continue with sibling nodes.
   72927                 : **
   72928                 : **    WRC_Abort         Do no more callbacks.  Unwind the stack and
   72929                 : **                      return the top-level walk call.
   72930                 : **
   72931                 : ** The return value from this routine is WRC_Abort to abandon the tree walk
   72932                 : ** and WRC_Continue to continue.
   72933                 : */
   72934         6217605 : SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
   72935                 :   int rc;
   72936         6217605 :   if( pExpr==0 ) return WRC_Continue;
   72937                 :   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
   72938                 :   testcase( ExprHasProperty(pExpr, EP_Reduced) );
   72939         2884972 :   rc = pWalker->xExprCallback(pWalker, pExpr);
   72940         2884972 :   if( rc==WRC_Continue
   72941         2136639 :               && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
   72942         2136639 :     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
   72943         1933155 :     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
   72944         1929004 :     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   72945           29395 :       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
   72946                 :     }else{
   72947         1899609 :       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
   72948                 :     }
   72949                 :   }
   72950         2674415 :   return rc & WRC_Abort;
   72951                 : }
   72952                 : 
   72953                 : /*
   72954                 : ** Call sqlite3WalkExpr() for every expression in list p or until
   72955                 : ** an abort request is seen.
   72956                 : */
   72957         2282638 : SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
   72958                 :   int i;
   72959                 :   struct ExprList_item *pItem;
   72960         2282638 :   if( p ){
   72961          939884 :     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
   72962          728111 :       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
   72963                 :     }
   72964                 :   }
   72965         2282638 :   return WRC_Continue;
   72966                 : }
   72967                 : 
   72968                 : /*
   72969                 : ** Walk all expressions associated with SELECT statement p.  Do
   72970                 : ** not invoke the SELECT callback on p, but do (of course) invoke
   72971                 : ** any expr callbacks and SELECT callbacks that come from subqueries.
   72972                 : ** Return WRC_Abort or WRC_Continue.
   72973                 : */
   72974          120834 : SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
   72975          120834 :   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
   72976          120834 :   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
   72977          120834 :   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
   72978          120834 :   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
   72979          120834 :   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
   72980          120834 :   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
   72981          120834 :   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
   72982          120834 :   return WRC_Continue;
   72983                 : }
   72984                 : 
   72985                 : /*
   72986                 : ** Walk the parse trees associated with all subqueries in the
   72987                 : ** FROM clause of SELECT statement p.  Do not invoke the select
   72988                 : ** callback on p, but do invoke it on each FROM clause subquery
   72989                 : ** and on any subqueries further down in the tree.  Return 
   72990                 : ** WRC_Abort or WRC_Continue;
   72991                 : */
   72992          120834 : SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
   72993                 :   SrcList *pSrc;
   72994                 :   int i;
   72995                 :   struct SrcList_item *pItem;
   72996                 : 
   72997          120834 :   pSrc = p->pSrc;
   72998          120834 :   if( ALWAYS(pSrc) ){
   72999          249071 :     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
   73000          128237 :       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
   73001               0 :         return WRC_Abort;
   73002                 :       }
   73003                 :     }
   73004                 :   }
   73005          120834 :   return WRC_Continue;
   73006                 : } 
   73007                 : 
   73008                 : /*
   73009                 : ** Call sqlite3WalkExpr() for every expression in Select statement p.
   73010                 : ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
   73011                 : ** on the compound select chain, p->pPrior.
   73012                 : **
   73013                 : ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
   73014                 : ** there is an abort request.
   73015                 : **
   73016                 : ** If the Walker does not have an xSelectCallback() then this routine
   73017                 : ** is a no-op returning WRC_Continue.
   73018                 : */
   73019          333907 : SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
   73020                 :   int rc;
   73021          333907 :   if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
   73022          204595 :   rc = WRC_Continue;
   73023          530024 :   while( p  ){
   73024          205109 :     rc = pWalker->xSelectCallback(pWalker, p);
   73025          205109 :     if( rc ) break;
   73026          120834 :     if( sqlite3WalkSelectExpr(pWalker, p) ) return WRC_Abort;
   73027          120834 :     if( sqlite3WalkSelectFrom(pWalker, p) ) return WRC_Abort;
   73028          120834 :     p = p->pPrior;
   73029                 :   }
   73030          204595 :   return rc & WRC_Abort;
   73031                 : }
   73032                 : 
   73033                 : /************** End of walker.c **********************************************/
   73034                 : /************** Begin file resolve.c *****************************************/
   73035                 : /*
   73036                 : ** 2008 August 18
   73037                 : **
   73038                 : ** The author disclaims copyright to this source code.  In place of
   73039                 : ** a legal notice, here is a blessing:
   73040                 : **
   73041                 : **    May you do good and not evil.
   73042                 : **    May you find forgiveness for yourself and forgive others.
   73043                 : **    May you share freely, never taking more than you give.
   73044                 : **
   73045                 : *************************************************************************
   73046                 : **
   73047                 : ** This file contains routines used for walking the parser tree and
   73048                 : ** resolve all identifiers by associating them with a particular
   73049                 : ** table and column.
   73050                 : */
   73051                 : /* #include <stdlib.h> */
   73052                 : /* #include <string.h> */
   73053                 : 
   73054                 : /*
   73055                 : ** Turn the pExpr expression into an alias for the iCol-th column of the
   73056                 : ** result set in pEList.
   73057                 : **
   73058                 : ** If the result set column is a simple column reference, then this routine
   73059                 : ** makes an exact copy.  But for any other kind of expression, this
   73060                 : ** routine make a copy of the result set column as the argument to the
   73061                 : ** TK_AS operator.  The TK_AS operator causes the expression to be
   73062                 : ** evaluated just once and then reused for each alias.
   73063                 : **
   73064                 : ** The reason for suppressing the TK_AS term when the expression is a simple
   73065                 : ** column reference is so that the column reference will be recognized as
   73066                 : ** usable by indices within the WHERE clause processing logic. 
   73067                 : **
   73068                 : ** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
   73069                 : ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
   73070                 : **
   73071                 : **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
   73072                 : **
   73073                 : ** Is equivalent to:
   73074                 : **
   73075                 : **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
   73076                 : **
   73077                 : ** The result of random()%5 in the GROUP BY clause is probably different
   73078                 : ** from the result in the result-set.  We might fix this someday.  Or
   73079                 : ** then again, we might not...
   73080                 : */
   73081            1029 : static void resolveAlias(
   73082                 :   Parse *pParse,         /* Parsing context */
   73083                 :   ExprList *pEList,      /* A result set */
   73084                 :   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
   73085                 :   Expr *pExpr,           /* Transform this into an alias to the result set */
   73086                 :   const char *zType      /* "GROUP" or "ORDER" or "" */
   73087                 : ){
   73088                 :   Expr *pOrig;           /* The iCol-th column of the result set */
   73089                 :   Expr *pDup;            /* Copy of pOrig */
   73090                 :   sqlite3 *db;           /* The database connection */
   73091                 : 
   73092            1029 :   assert( iCol>=0 && iCol<pEList->nExpr );
   73093            1029 :   pOrig = pEList->a[iCol].pExpr;
   73094            1029 :   assert( pOrig!=0 );
   73095            1029 :   assert( pOrig->flags & EP_Resolved );
   73096            1029 :   db = pParse->db;
   73097            1029 :   if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
   73098             365 :     pDup = sqlite3ExprDup(db, pOrig, 0);
   73099             365 :     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
   73100             365 :     if( pDup==0 ) return;
   73101             365 :     if( pEList->a[iCol].iAlias==0 ){
   73102             337 :       pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
   73103                 :     }
   73104             365 :     pDup->iTable = pEList->a[iCol].iAlias;
   73105             664 :   }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){
   73106              89 :     pDup = sqlite3ExprDup(db, pOrig, 0);
   73107              89 :     if( pDup==0 ) return;
   73108                 :   }else{
   73109             575 :     char *zToken = pOrig->u.zToken;
   73110             575 :     assert( zToken!=0 );
   73111             575 :     pOrig->u.zToken = 0;
   73112             575 :     pDup = sqlite3ExprDup(db, pOrig, 0);
   73113             575 :     pOrig->u.zToken = zToken;
   73114             575 :     if( pDup==0 ) return;
   73115             575 :     assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
   73116             575 :     pDup->flags2 |= EP2_MallocedToken;
   73117             575 :     pDup->u.zToken = sqlite3DbStrDup(db, zToken);
   73118                 :   }
   73119            1029 :   if( pExpr->flags & EP_ExpCollate ){
   73120              28 :     pDup->pColl = pExpr->pColl;
   73121              28 :     pDup->flags |= EP_ExpCollate;
   73122                 :   }
   73123                 : 
   73124                 :   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
   73125                 :   ** prevents ExprDelete() from deleting the Expr structure itself,
   73126                 :   ** allowing it to be repopulated by the memcpy() on the following line.
   73127                 :   */
   73128            1029 :   ExprSetProperty(pExpr, EP_Static);
   73129            1029 :   sqlite3ExprDelete(db, pExpr);
   73130            1029 :   memcpy(pExpr, pDup, sizeof(*pExpr));
   73131            1029 :   sqlite3DbFree(db, pDup);
   73132                 : }
   73133                 : 
   73134                 : 
   73135                 : /*
   73136                 : ** Return TRUE if the name zCol occurs anywhere in the USING clause.
   73137                 : **
   73138                 : ** Return FALSE if the USING clause is NULL or if it does not contain
   73139                 : ** zCol.
   73140                 : */
   73141               0 : static int nameInUsingClause(IdList *pUsing, const char *zCol){
   73142               0 :   if( pUsing ){
   73143                 :     int k;
   73144               0 :     for(k=0; k<pUsing->nId; k++){
   73145               0 :       if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
   73146                 :     }
   73147                 :   }
   73148               0 :   return 0;
   73149                 : }
   73150                 : 
   73151                 : 
   73152                 : /*
   73153                 : ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
   73154                 : ** that name in the set of source tables in pSrcList and make the pExpr 
   73155                 : ** expression node refer back to that source column.  The following changes
   73156                 : ** are made to pExpr:
   73157                 : **
   73158                 : **    pExpr->iDb           Set the index in db->aDb[] of the database X
   73159                 : **                         (even if X is implied).
   73160                 : **    pExpr->iTable        Set to the cursor number for the table obtained
   73161                 : **                         from pSrcList.
   73162                 : **    pExpr->pTab          Points to the Table structure of X.Y (even if
   73163                 : **                         X and/or Y are implied.)
   73164                 : **    pExpr->iColumn       Set to the column number within the table.
   73165                 : **    pExpr->op            Set to TK_COLUMN.
   73166                 : **    pExpr->pLeft         Any expression this points to is deleted
   73167                 : **    pExpr->pRight        Any expression this points to is deleted.
   73168                 : **
   73169                 : ** The zDb variable is the name of the database (the "X").  This value may be
   73170                 : ** NULL meaning that name is of the form Y.Z or Z.  Any available database
   73171                 : ** can be used.  The zTable variable is the name of the table (the "Y").  This
   73172                 : ** value can be NULL if zDb is also NULL.  If zTable is NULL it
   73173                 : ** means that the form of the name is Z and that columns from any table
   73174                 : ** can be used.
   73175                 : **
   73176                 : ** If the name cannot be resolved unambiguously, leave an error message
   73177                 : ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
   73178                 : */
   73179          430100 : static int lookupName(
   73180                 :   Parse *pParse,       /* The parsing context */
   73181                 :   const char *zDb,     /* Name of the database containing table, or NULL */
   73182                 :   const char *zTab,    /* Name of table containing column, or NULL */
   73183                 :   const char *zCol,    /* Name of the column. */
   73184                 :   NameContext *pNC,    /* The name context used to resolve the name */
   73185                 :   Expr *pExpr          /* Make this EXPR node point to the selected column */
   73186                 : ){
   73187                 :   int i, j;            /* Loop counters */
   73188          430100 :   int cnt = 0;                      /* Number of matching column names */
   73189          430100 :   int cntTab = 0;                   /* Number of matching table names */
   73190          430100 :   sqlite3 *db = pParse->db;         /* The database connection */
   73191                 :   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
   73192          430100 :   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   73193          430100 :   NameContext *pTopNC = pNC;        /* First namecontext in the list */
   73194          430100 :   Schema *pSchema = 0;              /* Schema of the expression */
   73195          430100 :   int isTrigger = 0;
   73196                 : 
   73197          430100 :   assert( pNC );     /* the name context cannot be NULL. */
   73198          430100 :   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
   73199                 :   assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
   73200                 : 
   73201                 :   /* Initialize the node to no-match */
   73202          430100 :   pExpr->iTable = -1;
   73203          430100 :   pExpr->pTab = 0;
   73204          430100 :   ExprSetIrreducible(pExpr);
   73205                 : 
   73206                 :   /* Start at the inner-most context and move outward until a match is found */
   73207         1292486 :   while( pNC && cnt==0 ){
   73208                 :     ExprList *pEList;
   73209          432647 :     SrcList *pSrcList = pNC->pSrcList;
   73210                 : 
   73211          432647 :     if( pSrcList ){
   73212          909069 :       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
   73213                 :         Table *pTab;
   73214                 :         int iDb;
   73215                 :         Column *pCol;
   73216                 :   
   73217          480284 :         pTab = pItem->pTab;
   73218          480284 :         assert( pTab!=0 && pTab->zName!=0 );
   73219          480284 :         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   73220          480284 :         assert( pTab->nCol>0 );
   73221          480284 :         if( zTab ){
   73222           95791 :           if( pItem->zAlias ){
   73223           63921 :             char *zTabName = pItem->zAlias;
   73224           63921 :             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
   73225                 :           }else{
   73226           31870 :             char *zTabName = pTab->zName;
   73227           31870 :             if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){
   73228           19330 :               continue;
   73229                 :             }
   73230           12540 :             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
   73231               0 :               continue;
   73232                 :             }
   73233                 :           }
   73234                 :         }
   73235          423557 :         if( 0==(cntTab++) ){
   73236          419744 :           pExpr->iTable = pItem->iCursor;
   73237          419744 :           pExpr->pTab = pTab;
   73238          419744 :           pSchema = pTab->pSchema;
   73239          419744 :           pMatch = pItem;
   73240                 :         }
   73241         3485766 :         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
   73242         3455046 :           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
   73243                 :             /* If there has been exactly one prior match and this match
   73244                 :             ** is for the right-hand table of a NATURAL JOIN or is in a 
   73245                 :             ** USING clause, then skip this match.
   73246                 :             */
   73247          392837 :             if( cnt==1 ){
   73248               0 :               if( pItem->jointype & JT_NATURAL ) continue;
   73249               0 :               if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
   73250                 :             }
   73251          392837 :             cnt++;
   73252          392837 :             pExpr->iTable = pItem->iCursor;
   73253          392837 :             pExpr->pTab = pTab;
   73254          392837 :             pMatch = pItem;
   73255          392837 :             pSchema = pTab->pSchema;
   73256                 :             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
   73257          392837 :             pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
   73258          392837 :             break;
   73259                 :           }
   73260                 :         }
   73261                 :       }
   73262                 :     }
   73263                 : 
   73264                 : #ifndef SQLITE_OMIT_TRIGGER
   73265                 :     /* If we have not already resolved the name, then maybe 
   73266                 :     ** it is a new.* or old.* trigger argument reference
   73267                 :     */
   73268          432647 :     if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
   73269           10356 :       int op = pParse->eTriggerOp;
   73270           10356 :       Table *pTab = 0;
   73271           10356 :       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
   73272           10356 :       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
   73273            5314 :         pExpr->iTable = 1;
   73274            5314 :         pTab = pParse->pTriggerTab;
   73275            5042 :       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
   73276            5042 :         pExpr->iTable = 0;
   73277            5042 :         pTab = pParse->pTriggerTab;
   73278                 :       }
   73279                 : 
   73280           10356 :       if( pTab ){ 
   73281                 :         int iCol;
   73282           10356 :         pSchema = pTab->pSchema;
   73283           10356 :         cntTab++;
   73284           53081 :         for(iCol=0; iCol<pTab->nCol; iCol++){
   73285           52927 :           Column *pCol = &pTab->aCol[iCol];
   73286           52927 :           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
   73287           10202 :             if( iCol==pTab->iPKey ){
   73288            1427 :               iCol = -1;
   73289                 :             }
   73290           10202 :             break;
   73291                 :           }
   73292                 :         }
   73293           10356 :         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
   73294             154 :           iCol = -1;        /* IMP: R-44911-55124 */
   73295                 :         }
   73296           10356 :         if( iCol<pTab->nCol ){
   73297           10356 :           cnt++;
   73298           10356 :           if( iCol<0 ){
   73299            1581 :             pExpr->affinity = SQLITE_AFF_INTEGER;
   73300            8775 :           }else if( pExpr->iTable==0 ){
   73301                 :             testcase( iCol==31 );
   73302                 :             testcase( iCol==32 );
   73303            3461 :             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
   73304                 :           }else{
   73305                 :             testcase( iCol==31 );
   73306                 :             testcase( iCol==32 );
   73307            5314 :             pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
   73308                 :           }
   73309           10356 :           pExpr->iColumn = (i16)iCol;
   73310           10356 :           pExpr->pTab = pTab;
   73311           10356 :           isTrigger = 1;
   73312                 :         }
   73313                 :       }
   73314                 :     }
   73315                 : #endif /* !defined(SQLITE_OMIT_TRIGGER) */
   73316                 : 
   73317                 :     /*
   73318                 :     ** Perhaps the name is a reference to the ROWID
   73319                 :     */
   73320          432647 :     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
   73321           26493 :       cnt = 1;
   73322           26493 :       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
   73323           26493 :       pExpr->affinity = SQLITE_AFF_INTEGER;
   73324                 :     }
   73325                 : 
   73326                 :     /*
   73327                 :     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
   73328                 :     ** might refer to an result-set alias.  This happens, for example, when
   73329                 :     ** we are resolving names in the WHERE clause of the following command:
   73330                 :     **
   73331                 :     **     SELECT a+b AS x FROM table WHERE x<10;
   73332                 :     **
   73333                 :     ** In cases like this, replace pExpr with a copy of the expression that
   73334                 :     ** forms the result set entry ("a+b" in the example) and return immediately.
   73335                 :     ** Note that the expression in the result set should have already been
   73336                 :     ** resolved by the time the WHERE clause is resolved.
   73337                 :     */
   73338          432647 :     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
   73339            3415 :       for(j=0; j<pEList->nExpr; j++){
   73340            2421 :         char *zAs = pEList->a[j].zName;
   73341            2421 :         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
   73342                 :           Expr *pOrig;
   73343             361 :           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
   73344             361 :           assert( pExpr->x.pList==0 );
   73345             361 :           assert( pExpr->x.pSelect==0 );
   73346             361 :           pOrig = pEList->a[j].pExpr;
   73347             361 :           if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
   73348               0 :             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
   73349               0 :             return WRC_Abort;
   73350                 :           }
   73351             361 :           resolveAlias(pParse, pEList, j, pExpr, "");
   73352             361 :           cnt = 1;
   73353             361 :           pMatch = 0;
   73354             361 :           assert( zTab==0 && zDb==0 );
   73355             361 :           goto lookupname_end;
   73356                 :         }
   73357                 :       } 
   73358                 :     }
   73359                 : 
   73360                 :     /* Advance to the next name context.  The loop will exit when either
   73361                 :     ** we have a match (cnt>0) or when we run out of name contexts.
   73362                 :     */
   73363          432286 :     if( cnt==0 ){
   73364            2600 :       pNC = pNC->pNext;
   73365                 :     }
   73366                 :   }
   73367                 : 
   73368                 :   /*
   73369                 :   ** If X and Y are NULL (in other words if only the column name Z is
   73370                 :   ** supplied) and the value of Z is enclosed in double-quotes, then
   73371                 :   ** Z is a string literal if it doesn't match any column names.  In that
   73372                 :   ** case, we need to return right away and not make any changes to
   73373                 :   ** pExpr.
   73374                 :   **
   73375                 :   ** Because no reference was made to outer contexts, the pNC->nRef
   73376                 :   ** fields are not changed in any context.
   73377                 :   */
   73378          429739 :   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
   73379               0 :     pExpr->op = TK_STRING;
   73380               0 :     pExpr->pTab = 0;
   73381               0 :     return WRC_Prune;
   73382                 :   }
   73383                 : 
   73384                 :   /*
   73385                 :   ** cnt==0 means there was not match.  cnt>1 means there were two or
   73386                 :   ** more matches.  Either way, we have an error.
   73387                 :   */
   73388          429739 :   if( cnt!=1 ){
   73389                 :     const char *zErr;
   73390              53 :     zErr = cnt==0 ? "no such column" : "ambiguous column name";
   73391              53 :     if( zDb ){
   73392               0 :       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
   73393              53 :     }else if( zTab ){
   73394               0 :       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
   73395                 :     }else{
   73396              53 :       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
   73397                 :     }
   73398              53 :     pParse->checkSchema = 1;
   73399              53 :     pTopNC->nErr++;
   73400                 :   }
   73401                 : 
   73402                 :   /* If a column from a table in pSrcList is referenced, then record
   73403                 :   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
   73404                 :   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
   73405                 :   ** column number is greater than the number of bits in the bitmask
   73406                 :   ** then set the high-order bit of the bitmask.
   73407                 :   */
   73408          429739 :   if( pExpr->iColumn>=0 && pMatch!=0 ){
   73409          354858 :     int n = pExpr->iColumn;
   73410                 :     testcase( n==BMS-1 );
   73411          354858 :     if( n>=BMS ){
   73412               0 :       n = BMS-1;
   73413                 :     }
   73414          354858 :     assert( pMatch->iCursor==pExpr->iTable );
   73415          354858 :     pMatch->colUsed |= ((Bitmask)1)<<n;
   73416                 :   }
   73417                 : 
   73418                 :   /* Clean up and return
   73419                 :   */
   73420          429739 :   sqlite3ExprDelete(db, pExpr->pLeft);
   73421          429739 :   pExpr->pLeft = 0;
   73422          429739 :   sqlite3ExprDelete(db, pExpr->pRight);
   73423          429739 :   pExpr->pRight = 0;
   73424          429739 :   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
   73425                 : lookupname_end:
   73426          430100 :   if( cnt==1 ){
   73427          430047 :     assert( pNC!=0 );
   73428          430047 :     sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
   73429                 :     /* Increment the nRef value on all name contexts from TopNC up to
   73430                 :     ** the point where the name matched. */
   73431                 :     for(;;){
   73432          432594 :       assert( pTopNC!=0 );
   73433          432594 :       pTopNC->nRef++;
   73434          432594 :       if( pTopNC==pNC ) break;
   73435            2547 :       pTopNC = pTopNC->pNext;
   73436            2547 :     }
   73437          430047 :     return WRC_Prune;
   73438                 :   } else {
   73439              53 :     return WRC_Abort;
   73440                 :   }
   73441                 : }
   73442                 : 
   73443                 : /*
   73444                 : ** Allocate and return a pointer to an expression to load the column iCol
   73445                 : ** from datasource iSrc in SrcList pSrc.
   73446                 : */
   73447               0 : SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
   73448               0 :   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
   73449               0 :   if( p ){
   73450               0 :     struct SrcList_item *pItem = &pSrc->a[iSrc];
   73451               0 :     p->pTab = pItem->pTab;
   73452               0 :     p->iTable = pItem->iCursor;
   73453               0 :     if( p->pTab->iPKey==iCol ){
   73454               0 :       p->iColumn = -1;
   73455                 :     }else{
   73456               0 :       p->iColumn = (ynVar)iCol;
   73457                 :       testcase( iCol==BMS );
   73458                 :       testcase( iCol==BMS-1 );
   73459               0 :       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
   73460                 :     }
   73461               0 :     ExprSetProperty(p, EP_Resolved);
   73462                 :   }
   73463               0 :   return p;
   73464                 : }
   73465                 : 
   73466                 : /*
   73467                 : ** This routine is callback for sqlite3WalkExpr().
   73468                 : **
   73469                 : ** Resolve symbolic names into TK_COLUMN operators for the current
   73470                 : ** node in the expression tree.  Return 0 to continue the search down
   73471                 : ** the tree or 2 to abort the tree walk.
   73472                 : **
   73473                 : ** This routine also does error checking and name resolution for
   73474                 : ** function names.  The operator for aggregate functions is changed
   73475                 : ** to TK_AGG_FUNCTION.
   73476                 : */
   73477          849465 : static int resolveExprStep(Walker *pWalker, Expr *pExpr){
   73478                 :   NameContext *pNC;
   73479                 :   Parse *pParse;
   73480                 : 
   73481          849465 :   pNC = pWalker->u.pNC;
   73482          849465 :   assert( pNC!=0 );
   73483          849465 :   pParse = pNC->pParse;
   73484          849465 :   assert( pParse==pWalker->pParse );
   73485                 : 
   73486          849465 :   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
   73487          849465 :   ExprSetProperty(pExpr, EP_Resolved);
   73488                 : #ifndef NDEBUG
   73489          849465 :   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
   73490          712147 :     SrcList *pSrcList = pNC->pSrcList;
   73491                 :     int i;
   73492         1514561 :     for(i=0; i<pNC->pSrcList->nSrc; i++){
   73493          802414 :       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
   73494                 :     }
   73495                 :   }
   73496                 : #endif
   73497          849465 :   switch( pExpr->op ){
   73498                 : 
   73499                 : #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
   73500                 :     /* The special operator TK_ROW means use the rowid for the first
   73501                 :     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
   73502                 :     ** clause processing on UPDATE and DELETE statements.
   73503                 :     */
   73504                 :     case TK_ROW: {
   73505                 :       SrcList *pSrcList = pNC->pSrcList;
   73506                 :       struct SrcList_item *pItem;
   73507                 :       assert( pSrcList && pSrcList->nSrc==1 );
   73508                 :       pItem = pSrcList->a; 
   73509                 :       pExpr->op = TK_COLUMN;
   73510                 :       pExpr->pTab = pItem->pTab;
   73511                 :       pExpr->iTable = pItem->iCursor;
   73512                 :       pExpr->iColumn = -1;
   73513                 :       pExpr->affinity = SQLITE_AFF_INTEGER;
   73514                 :       break;
   73515                 :     }
   73516                 : #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
   73517                 : 
   73518                 :     /* A lone identifier is the name of a column.
   73519                 :     */
   73520                 :     case TK_ID: {
   73521          380680 :       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
   73522                 :     }
   73523                 :   
   73524                 :     /* A table name and column name:     ID.ID
   73525                 :     ** Or a database, table and column:  ID.ID.ID
   73526                 :     */
   73527                 :     case TK_DOT: {
   73528                 :       const char *zColumn;
   73529                 :       const char *zTable;
   73530                 :       const char *zDb;
   73531                 :       Expr *pRight;
   73532                 : 
   73533                 :       /* if( pSrcList==0 ) break; */
   73534           49420 :       pRight = pExpr->pRight;
   73535           49420 :       if( pRight->op==TK_ID ){
   73536           49420 :         zDb = 0;
   73537           49420 :         zTable = pExpr->pLeft->u.zToken;
   73538           49420 :         zColumn = pRight->u.zToken;
   73539                 :       }else{
   73540               0 :         assert( pRight->op==TK_DOT );
   73541               0 :         zDb = pExpr->pLeft->u.zToken;
   73542               0 :         zTable = pRight->pLeft->u.zToken;
   73543               0 :         zColumn = pRight->pRight->u.zToken;
   73544                 :       }
   73545           49420 :       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
   73546                 :     }
   73547                 : 
   73548                 :     /* Resolve function names
   73549                 :     */
   73550                 :     case TK_CONST_FUNC:
   73551                 :     case TK_FUNCTION: {
   73552           20527 :       ExprList *pList = pExpr->x.pList;    /* The argument list */
   73553           20527 :       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
   73554           20527 :       int no_such_func = 0;       /* True if no such function exists */
   73555           20527 :       int wrong_num_args = 0;     /* True if wrong number of arguments */
   73556           20527 :       int is_agg = 0;             /* True if is an aggregate function */
   73557                 :       int auth;                   /* Authorization to use the function */
   73558                 :       int nId;                    /* Number of characters in function name */
   73559                 :       const char *zId;            /* The function name. */
   73560                 :       FuncDef *pDef;              /* Information about the function */
   73561           20527 :       u8 enc = ENC(pParse->db);   /* The database encoding */
   73562                 : 
   73563                 :       testcase( pExpr->op==TK_CONST_FUNC );
   73564           20527 :       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   73565           20527 :       zId = pExpr->u.zToken;
   73566           20527 :       nId = sqlite3Strlen30(zId);
   73567           20527 :       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
   73568           20527 :       if( pDef==0 ){
   73569               0 :         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
   73570               0 :         if( pDef==0 ){
   73571               0 :           no_such_func = 1;
   73572                 :         }else{
   73573               0 :           wrong_num_args = 1;
   73574                 :         }
   73575                 :       }else{
   73576           20527 :         is_agg = pDef->xFunc==0;
   73577                 :       }
   73578                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   73579           20527 :       if( pDef ){
   73580           20527 :         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
   73581           20527 :         if( auth!=SQLITE_OK ){
   73582               0 :           if( auth==SQLITE_DENY ){
   73583               0 :             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
   73584                 :                                     pDef->zName);
   73585               0 :             pNC->nErr++;
   73586                 :           }
   73587               0 :           pExpr->op = TK_NULL;
   73588               0 :           return WRC_Prune;
   73589                 :         }
   73590                 :       }
   73591                 : #endif
   73592           20527 :       if( is_agg && !pNC->allowAgg ){
   73593               0 :         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
   73594               0 :         pNC->nErr++;
   73595               0 :         is_agg = 0;
   73596           20527 :       }else if( no_such_func ){
   73597               0 :         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
   73598               0 :         pNC->nErr++;
   73599           20527 :       }else if( wrong_num_args ){
   73600               0 :         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
   73601                 :              nId, zId);
   73602               0 :         pNC->nErr++;
   73603                 :       }
   73604           20527 :       if( is_agg ){
   73605            4405 :         pExpr->op = TK_AGG_FUNCTION;
   73606            4405 :         pNC->hasAgg = 1;
   73607                 :       }
   73608           20527 :       if( is_agg ) pNC->allowAgg = 0;
   73609           20527 :       sqlite3WalkExprList(pWalker, pList);
   73610           20527 :       if( is_agg ) pNC->allowAgg = 1;
   73611                 :       /* FIX ME:  Compute pExpr->affinity based on the expected return
   73612                 :       ** type of the function 
   73613                 :       */
   73614           20527 :       return WRC_Prune;
   73615                 :     }
   73616                 : #ifndef SQLITE_OMIT_SUBQUERY
   73617                 :     case TK_SELECT:
   73618                 :     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
   73619                 : #endif
   73620                 :     case TK_IN: {
   73621                 :       testcase( pExpr->op==TK_IN );
   73622           14648 :       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   73623           13307 :         int nRef = pNC->nRef;
   73624                 : #ifndef SQLITE_OMIT_CHECK
   73625           13307 :         if( pNC->isCheck ){
   73626               0 :           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
   73627                 :         }
   73628                 : #endif
   73629           13307 :         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
   73630           13307 :         assert( pNC->nRef>=nRef );
   73631           13307 :         if( nRef!=pNC->nRef ){
   73632            1846 :           ExprSetProperty(pExpr, EP_VarSelect);
   73633                 :         }
   73634                 :       }
   73635           14648 :       break;
   73636                 :     }
   73637                 : #ifndef SQLITE_OMIT_CHECK
   73638                 :     case TK_VARIABLE: {
   73639           87230 :       if( pNC->isCheck ){
   73640               0 :         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
   73641                 :       }
   73642           87230 :       break;
   73643                 :     }
   73644                 : #endif
   73645                 :   }
   73646          398838 :   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
   73647                 : }
   73648                 : 
   73649                 : /*
   73650                 : ** pEList is a list of expressions which are really the result set of the
   73651                 : ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
   73652                 : ** This routine checks to see if pE is a simple identifier which corresponds
   73653                 : ** to the AS-name of one of the terms of the expression list.  If it is,
   73654                 : ** this routine return an integer between 1 and N where N is the number of
   73655                 : ** elements in pEList, corresponding to the matching entry.  If there is
   73656                 : ** no match, or if pE is not a simple identifier, then this routine
   73657                 : ** return 0.
   73658                 : **
   73659                 : ** pEList has been resolved.  pE has not.
   73660                 : */
   73661           22570 : static int resolveAsName(
   73662                 :   Parse *pParse,     /* Parsing context for error messages */
   73663                 :   ExprList *pEList,  /* List of expressions to scan */
   73664                 :   Expr *pE           /* Expression we are trying to match */
   73665                 : ){
   73666                 :   int i;             /* Loop counter */
   73667                 : 
   73668                 :   UNUSED_PARAMETER(pParse);
   73669                 : 
   73670           22570 :   if( pE->op==TK_ID ){
   73671           21144 :     char *zCol = pE->u.zToken;
   73672           83012 :     for(i=0; i<pEList->nExpr; i++){
   73673           62444 :       char *zAs = pEList->a[i].zName;
   73674           62444 :       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
   73675             576 :         return i+1;
   73676                 :       }
   73677                 :     }
   73678                 :   }
   73679           21994 :   return 0;
   73680                 : }
   73681                 : 
   73682                 : /*
   73683                 : ** pE is a pointer to an expression which is a single term in the
   73684                 : ** ORDER BY of a compound SELECT.  The expression has not been
   73685                 : ** name resolved.
   73686                 : **
   73687                 : ** At the point this routine is called, we already know that the
   73688                 : ** ORDER BY term is not an integer index into the result set.  That
   73689                 : ** case is handled by the calling routine.
   73690                 : **
   73691                 : ** Attempt to match pE against result set columns in the left-most
   73692                 : ** SELECT statement.  Return the index i of the matching column,
   73693                 : ** as an indication to the caller that it should sort by the i-th column.
   73694                 : ** The left-most column is 1.  In other words, the value returned is the
   73695                 : ** same integer value that would be used in the SQL statement to indicate
   73696                 : ** the column.
   73697                 : **
   73698                 : ** If there is no match, return 0.  Return -1 if an error occurs.
   73699                 : */
   73700               0 : static int resolveOrderByTermToExprList(
   73701                 :   Parse *pParse,     /* Parsing context for error messages */
   73702                 :   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
   73703                 :   Expr *pE           /* The specific ORDER BY term */
   73704                 : ){
   73705                 :   int i;             /* Loop counter */
   73706                 :   ExprList *pEList;  /* The columns of the result set */
   73707                 :   NameContext nc;    /* Name context for resolving pE */
   73708                 :   sqlite3 *db;       /* Database connection */
   73709                 :   int rc;            /* Return code from subprocedures */
   73710                 :   u8 savedSuppErr;   /* Saved value of db->suppressErr */
   73711                 : 
   73712               0 :   assert( sqlite3ExprIsInteger(pE, &i)==0 );
   73713               0 :   pEList = pSelect->pEList;
   73714                 : 
   73715                 :   /* Resolve all names in the ORDER BY term expression
   73716                 :   */
   73717               0 :   memset(&nc, 0, sizeof(nc));
   73718               0 :   nc.pParse = pParse;
   73719               0 :   nc.pSrcList = pSelect->pSrc;
   73720               0 :   nc.pEList = pEList;
   73721               0 :   nc.allowAgg = 1;
   73722               0 :   nc.nErr = 0;
   73723               0 :   db = pParse->db;
   73724               0 :   savedSuppErr = db->suppressErr;
   73725               0 :   db->suppressErr = 1;
   73726               0 :   rc = sqlite3ResolveExprNames(&nc, pE);
   73727               0 :   db->suppressErr = savedSuppErr;
   73728               0 :   if( rc ) return 0;
   73729                 : 
   73730                 :   /* Try to match the ORDER BY expression against an expression
   73731                 :   ** in the result set.  Return an 1-based index of the matching
   73732                 :   ** result-set entry.
   73733                 :   */
   73734               0 :   for(i=0; i<pEList->nExpr; i++){
   73735               0 :     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
   73736               0 :       return i+1;
   73737                 :     }
   73738                 :   }
   73739                 : 
   73740                 :   /* If no match, return 0. */
   73741               0 :   return 0;
   73742                 : }
   73743                 : 
   73744                 : /*
   73745                 : ** Generate an ORDER BY or GROUP BY term out-of-range error.
   73746                 : */
   73747               0 : static void resolveOutOfRangeError(
   73748                 :   Parse *pParse,         /* The error context into which to write the error */
   73749                 :   const char *zType,     /* "ORDER" or "GROUP" */
   73750                 :   int i,                 /* The index (1-based) of the term out of range */
   73751                 :   int mx                 /* Largest permissible value of i */
   73752                 : ){
   73753               0 :   sqlite3ErrorMsg(pParse, 
   73754                 :     "%r %s BY term out of range - should be "
   73755                 :     "between 1 and %d", i, zType, mx);
   73756               0 : }
   73757                 : 
   73758                 : /*
   73759                 : ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
   73760                 : ** each term of the ORDER BY clause is a constant integer between 1
   73761                 : ** and N where N is the number of columns in the compound SELECT.
   73762                 : **
   73763                 : ** ORDER BY terms that are already an integer between 1 and N are
   73764                 : ** unmodified.  ORDER BY terms that are integers outside the range of
   73765                 : ** 1 through N generate an error.  ORDER BY terms that are expressions
   73766                 : ** are matched against result set expressions of compound SELECT
   73767                 : ** beginning with the left-most SELECT and working toward the right.
   73768                 : ** At the first match, the ORDER BY expression is transformed into
   73769                 : ** the integer column number.
   73770                 : **
   73771                 : ** Return the number of errors seen.
   73772                 : */
   73773             193 : static int resolveCompoundOrderBy(
   73774                 :   Parse *pParse,        /* Parsing context.  Leave error messages here */
   73775                 :   Select *pSelect       /* The SELECT statement containing the ORDER BY */
   73776                 : ){
   73777                 :   int i;
   73778                 :   ExprList *pOrderBy;
   73779                 :   ExprList *pEList;
   73780                 :   sqlite3 *db;
   73781             193 :   int moreToDo = 1;
   73782                 : 
   73783             193 :   pOrderBy = pSelect->pOrderBy;
   73784             193 :   if( pOrderBy==0 ) return 0;
   73785               8 :   db = pParse->db;
   73786                 : #if SQLITE_MAX_COLUMN
   73787               8 :   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
   73788               0 :     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
   73789               0 :     return 1;
   73790                 :   }
   73791                 : #endif
   73792              16 :   for(i=0; i<pOrderBy->nExpr; i++){
   73793               8 :     pOrderBy->a[i].done = 0;
   73794                 :   }
   73795               8 :   pSelect->pNext = 0;
   73796              24 :   while( pSelect->pPrior ){
   73797               8 :     pSelect->pPrior->pNext = pSelect;
   73798               8 :     pSelect = pSelect->pPrior;
   73799                 :   }
   73800              24 :   while( pSelect && moreToDo ){
   73801                 :     struct ExprList_item *pItem;
   73802               8 :     moreToDo = 0;
   73803               8 :     pEList = pSelect->pEList;
   73804               8 :     assert( pEList!=0 );
   73805              16 :     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
   73806               8 :       int iCol = -1;
   73807                 :       Expr *pE, *pDup;
   73808               8 :       if( pItem->done ) continue;
   73809               8 :       pE = pItem->pExpr;
   73810               8 :       if( sqlite3ExprIsInteger(pE, &iCol) ){
   73811               8 :         if( iCol<=0 || iCol>pEList->nExpr ){
   73812               0 :           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
   73813               0 :           return 1;
   73814                 :         }
   73815                 :       }else{
   73816               0 :         iCol = resolveAsName(pParse, pEList, pE);
   73817               0 :         if( iCol==0 ){
   73818               0 :           pDup = sqlite3ExprDup(db, pE, 0);
   73819               0 :           if( !db->mallocFailed ){
   73820               0 :             assert(pDup);
   73821               0 :             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
   73822                 :           }
   73823               0 :           sqlite3ExprDelete(db, pDup);
   73824                 :         }
   73825                 :       }
   73826               8 :       if( iCol>0 ){
   73827               8 :         CollSeq *pColl = pE->pColl;
   73828               8 :         int flags = pE->flags & EP_ExpCollate;
   73829               8 :         sqlite3ExprDelete(db, pE);
   73830               8 :         pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
   73831               8 :         if( pE==0 ) return 1;
   73832               8 :         pE->pColl = pColl;
   73833               8 :         pE->flags |= EP_IntValue | flags;
   73834               8 :         pE->u.iValue = iCol;
   73835               8 :         pItem->iOrderByCol = (u16)iCol;
   73836               8 :         pItem->done = 1;
   73837                 :       }else{
   73838               0 :         moreToDo = 1;
   73839                 :       }
   73840                 :     }
   73841               8 :     pSelect = pSelect->pNext;
   73842                 :   }
   73843              16 :   for(i=0; i<pOrderBy->nExpr; i++){
   73844               8 :     if( pOrderBy->a[i].done==0 ){
   73845               0 :       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
   73846                 :             "column in the result set", i+1);
   73847               0 :       return 1;
   73848                 :     }
   73849                 :   }
   73850               8 :   return 0;
   73851                 : }
   73852                 : 
   73853                 : /*
   73854                 : ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
   73855                 : ** the SELECT statement pSelect.  If any term is reference to a
   73856                 : ** result set expression (as determined by the ExprList.a.iCol field)
   73857                 : ** then convert that term into a copy of the corresponding result set
   73858                 : ** column.
   73859                 : **
   73860                 : ** If any errors are detected, add an error message to pParse and
   73861                 : ** return non-zero.  Return zero if no errors are seen.
   73862                 : */
   73863           21933 : SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
   73864                 :   Parse *pParse,        /* Parsing context.  Leave error messages here */
   73865                 :   Select *pSelect,      /* The SELECT statement containing the clause */
   73866                 :   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
   73867                 :   const char *zType     /* "ORDER" or "GROUP" */
   73868                 : ){
   73869                 :   int i;
   73870           21933 :   sqlite3 *db = pParse->db;
   73871                 :   ExprList *pEList;
   73872                 :   struct ExprList_item *pItem;
   73873                 : 
   73874           21933 :   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
   73875                 : #if SQLITE_MAX_COLUMN
   73876           21933 :   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
   73877               0 :     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
   73878               0 :     return 1;
   73879                 :   }
   73880                 : #endif
   73881           21933 :   pEList = pSelect->pEList;
   73882           21933 :   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
   73883           44519 :   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
   73884           22586 :     if( pItem->iOrderByCol ){
   73885             668 :       if( pItem->iOrderByCol>pEList->nExpr ){
   73886               0 :         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
   73887               0 :         return 1;
   73888                 :       }
   73889             668 :       resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType);
   73890                 :     }
   73891                 :   }
   73892           21933 :   return 0;
   73893                 : }
   73894                 : 
   73895                 : /*
   73896                 : ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
   73897                 : ** The Name context of the SELECT statement is pNC.  zType is either
   73898                 : ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
   73899                 : **
   73900                 : ** This routine resolves each term of the clause into an expression.
   73901                 : ** If the order-by term is an integer I between 1 and N (where N is the
   73902                 : ** number of columns in the result set of the SELECT) then the expression
   73903                 : ** in the resolution is a copy of the I-th result-set expression.  If
   73904                 : ** the order-by term is an identify that corresponds to the AS-name of
   73905                 : ** a result-set expression, then the term resolves to a copy of the
   73906                 : ** result-set expression.  Otherwise, the expression is resolved in
   73907                 : ** the usual way - using sqlite3ResolveExprNames().
   73908                 : **
   73909                 : ** This routine returns the number of errors.  If errors occur, then
   73910                 : ** an appropriate error message might be left in pParse.  (OOM errors
   73911                 : ** excepted.)
   73912                 : */
   73913           59678 : static int resolveOrderGroupBy(
   73914                 :   NameContext *pNC,     /* The name context of the SELECT statement */
   73915                 :   Select *pSelect,      /* The SELECT statement holding pOrderBy */
   73916                 :   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
   73917                 :   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
   73918                 : ){
   73919                 :   int i;                         /* Loop counter */
   73920                 :   int iCol;                      /* Column number */
   73921                 :   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
   73922                 :   Parse *pParse;                 /* Parsing context */
   73923                 :   int nResult;                   /* Number of terms in the result set */
   73924                 : 
   73925           59678 :   if( pOrderBy==0 ) return 0;
   73926           21917 :   nResult = pSelect->pEList->nExpr;
   73927           21917 :   pParse = pNC->pParse;
   73928           44487 :   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
   73929           22570 :     Expr *pE = pItem->pExpr;
   73930           22570 :     iCol = resolveAsName(pParse, pSelect->pEList, pE);
   73931           22570 :     if( iCol>0 ){
   73932                 :       /* If an AS-name match is found, mark this ORDER BY column as being
   73933                 :       ** a copy of the iCol-th result-set column.  The subsequent call to
   73934                 :       ** sqlite3ResolveOrderGroupBy() will convert the expression to a
   73935                 :       ** copy of the iCol-th result-set expression. */
   73936             576 :       pItem->iOrderByCol = (u16)iCol;
   73937             576 :       continue;
   73938                 :     }
   73939           21994 :     if( sqlite3ExprIsInteger(pE, &iCol) ){
   73940                 :       /* The ORDER BY term is an integer constant.  Again, set the column
   73941                 :       ** number so that sqlite3ResolveOrderGroupBy() will convert the
   73942                 :       ** order-by term to a copy of the result-set expression */
   73943              76 :       if( iCol<1 ){
   73944               0 :         resolveOutOfRangeError(pParse, zType, i+1, nResult);
   73945               0 :         return 1;
   73946                 :       }
   73947              76 :       pItem->iOrderByCol = (u16)iCol;
   73948              76 :       continue;
   73949                 :     }
   73950                 : 
   73951                 :     /* Otherwise, treat the ORDER BY term as an ordinary expression */
   73952           21918 :     pItem->iOrderByCol = 0;
   73953           21918 :     if( sqlite3ResolveExprNames(pNC, pE) ){
   73954               0 :       return 1;
   73955                 :     }
   73956                 :   }
   73957           21917 :   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
   73958                 : }
   73959                 : 
   73960                 : /*
   73961                 : ** Resolve names in the SELECT statement p and all of its descendents.
   73962                 : */
   73963           80848 : static int resolveSelectStep(Walker *pWalker, Select *p){
   73964                 :   NameContext *pOuterNC;  /* Context that contains this SELECT */
   73965                 :   NameContext sNC;        /* Name context of this SELECT */
   73966                 :   int isCompound;         /* True if p is a compound select */
   73967                 :   int nCompound;          /* Number of compound terms processed so far */
   73968                 :   Parse *pParse;          /* Parsing context */
   73969                 :   ExprList *pEList;       /* Result set expression list */
   73970                 :   int i;                  /* Loop counter */
   73971                 :   ExprList *pGroupBy;     /* The GROUP BY clause */
   73972                 :   Select *pLeftmost;      /* Left-most of SELECT of a compound */
   73973                 :   sqlite3 *db;            /* Database connection */
   73974                 :   
   73975                 : 
   73976           80848 :   assert( p!=0 );
   73977           80848 :   if( p->selFlags & SF_Resolved ){
   73978           13307 :     return WRC_Prune;
   73979                 :   }
   73980           67541 :   pOuterNC = pWalker->u.pNC;
   73981           67541 :   pParse = pWalker->pParse;
   73982           67541 :   db = pParse->db;
   73983                 : 
   73984                 :   /* Normally sqlite3SelectExpand() will be called first and will have
   73985                 :   ** already expanded this SELECT.  However, if this is a subquery within
   73986                 :   ** an expression, sqlite3ResolveExprNames() will be called without a
   73987                 :   ** prior call to sqlite3SelectExpand().  When that happens, let
   73988                 :   ** sqlite3SelectPrep() do all of the processing for this SELECT.
   73989                 :   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
   73990                 :   ** this routine in the correct order.
   73991                 :   */
   73992           67541 :   if( (p->selFlags & SF_Expanded)==0 ){
   73993            7714 :     sqlite3SelectPrep(pParse, p, pOuterNC);
   73994            7714 :     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
   73995                 :   }
   73996                 : 
   73997           59827 :   isCompound = p->pPrior!=0;
   73998           59827 :   nCompound = 0;
   73999           59827 :   pLeftmost = p;
   74000          179689 :   while( p ){
   74001           60084 :     assert( (p->selFlags & SF_Expanded)!=0 );
   74002           60084 :     assert( (p->selFlags & SF_Resolved)==0 );
   74003           60084 :     p->selFlags |= SF_Resolved;
   74004                 : 
   74005                 :     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
   74006                 :     ** are not allowed to refer to any names, so pass an empty NameContext.
   74007                 :     */
   74008           60084 :     memset(&sNC, 0, sizeof(sNC));
   74009           60084 :     sNC.pParse = pParse;
   74010          120168 :     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
   74011           60084 :         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
   74012               0 :       return WRC_Abort;
   74013                 :     }
   74014                 :   
   74015                 :     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
   74016                 :     ** resolve the result-set expression list.
   74017                 :     */
   74018           60084 :     sNC.allowAgg = 1;
   74019           60084 :     sNC.pSrcList = p->pSrc;
   74020           60084 :     sNC.pNext = pOuterNC;
   74021                 :   
   74022                 :     /* Resolve names in the result set. */
   74023           60084 :     pEList = p->pEList;
   74024           60084 :     assert( pEList!=0 );
   74025          359013 :     for(i=0; i<pEList->nExpr; i++){
   74026          298978 :       Expr *pX = pEList->a[i].pExpr;
   74027          298978 :       if( sqlite3ResolveExprNames(&sNC, pX) ){
   74028              49 :         return WRC_Abort;
   74029                 :       }
   74030                 :     }
   74031                 :   
   74032                 :     /* Recursively resolve names in all subqueries
   74033                 :     */
   74034          123733 :     for(i=0; i<p->pSrc->nSrc; i++){
   74035           63698 :       struct SrcList_item *pItem = &p->pSrc->a[i];
   74036           63698 :       if( pItem->pSelect ){
   74037                 :         NameContext *pNC;         /* Used to iterate name contexts */
   74038             122 :         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
   74039             122 :         const char *zSavedContext = pParse->zAuthContext;
   74040                 : 
   74041                 :         /* Count the total number of references to pOuterNC and all of its
   74042                 :         ** parent contexts. After resolving references to expressions in
   74043                 :         ** pItem->pSelect, check if this value has changed. If so, then
   74044                 :         ** SELECT statement pItem->pSelect must be correlated. Set the
   74045                 :         ** pItem->isCorrelated flag if this is the case. */
   74046             122 :         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
   74047                 : 
   74048             122 :         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
   74049             122 :         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
   74050             122 :         pParse->zAuthContext = zSavedContext;
   74051             122 :         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
   74052                 : 
   74053             122 :         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
   74054             122 :         assert( pItem->isCorrelated==0 && nRef<=0 );
   74055             122 :         pItem->isCorrelated = (nRef!=0);
   74056                 :       }
   74057                 :     }
   74058                 :   
   74059                 :     /* If there are no aggregate functions in the result-set, and no GROUP BY 
   74060                 :     ** expression, do not allow aggregates in any of the other expressions.
   74061                 :     */
   74062           60035 :     assert( (p->selFlags & SF_Aggregate)==0 );
   74063           60035 :     pGroupBy = p->pGroupBy;
   74064           60035 :     if( pGroupBy || sNC.hasAgg ){
   74065            4353 :       p->selFlags |= SF_Aggregate;
   74066                 :     }else{
   74067           55682 :       sNC.allowAgg = 0;
   74068                 :     }
   74069                 :   
   74070                 :     /* If a HAVING clause is present, then there must be a GROUP BY clause.
   74071                 :     */
   74072           60035 :     if( p->pHaving && !pGroupBy ){
   74073               0 :       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
   74074               0 :       return WRC_Abort;
   74075                 :     }
   74076                 :   
   74077                 :     /* Add the expression list to the name-context before parsing the
   74078                 :     ** other expressions in the SELECT statement. This is so that
   74079                 :     ** expressions in the WHERE clause (etc.) can refer to expressions by
   74080                 :     ** aliases in the result set.
   74081                 :     **
   74082                 :     ** Minor point: If this is the case, then the expression will be
   74083                 :     ** re-evaluated for each reference to it.
   74084                 :     */
   74085           60035 :     sNC.pEList = p->pEList;
   74086          120070 :     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
   74087           60035 :        sqlite3ResolveExprNames(&sNC, p->pHaving)
   74088                 :     ){
   74089               0 :       return WRC_Abort;
   74090                 :     }
   74091                 : 
   74092                 :     /* The ORDER BY and GROUP BY clauses may not refer to terms in
   74093                 :     ** outer queries 
   74094                 :     */
   74095           60035 :     sNC.pNext = 0;
   74096           60035 :     sNC.allowAgg = 1;
   74097                 : 
   74098                 :     /* Process the ORDER BY clause for singleton SELECT statements.
   74099                 :     ** The ORDER BY clause for compounds SELECT statements is handled
   74100                 :     ** below, after all of the result-sets for all of the elements of
   74101                 :     ** the compound have been resolved.
   74102                 :     */
   74103           60035 :     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
   74104               0 :       return WRC_Abort;
   74105                 :     }
   74106           60035 :     if( db->mallocFailed ){
   74107               0 :       return WRC_Abort;
   74108                 :     }
   74109                 :   
   74110                 :     /* Resolve the GROUP BY clause.  At the same time, make sure 
   74111                 :     ** the GROUP BY clause does not contain aggregate functions.
   74112                 :     */
   74113           60035 :     if( pGroupBy ){
   74114                 :       struct ExprList_item *pItem;
   74115                 :     
   74116              93 :       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
   74117               0 :         return WRC_Abort;
   74118                 :       }
   74119             186 :       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
   74120              93 :         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
   74121               0 :           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
   74122                 :               "the GROUP BY clause");
   74123               0 :           return WRC_Abort;
   74124                 :         }
   74125                 :       }
   74126                 :     }
   74127                 : 
   74128                 :     /* Advance to the next term of the compound
   74129                 :     */
   74130           60035 :     p = p->pPrior;
   74131           60035 :     nCompound++;
   74132                 :   }
   74133                 : 
   74134                 :   /* Resolve the ORDER BY on a compound SELECT after all terms of
   74135                 :   ** the compound have been resolved.
   74136                 :   */
   74137           59778 :   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
   74138               0 :     return WRC_Abort;
   74139                 :   }
   74140                 : 
   74141           59778 :   return WRC_Prune;
   74142                 : }
   74143                 : 
   74144                 : /*
   74145                 : ** This routine walks an expression tree and resolves references to
   74146                 : ** table columns and result-set columns.  At the same time, do error
   74147                 : ** checking on function usage and set a flag if any aggregate functions
   74148                 : ** are seen.
   74149                 : **
   74150                 : ** To resolve table columns references we look for nodes (or subtrees) of the 
   74151                 : ** form X.Y.Z or Y.Z or just Z where
   74152                 : **
   74153                 : **      X:   The name of a database.  Ex:  "main" or "temp" or
   74154                 : **           the symbolic name assigned to an ATTACH-ed database.
   74155                 : **
   74156                 : **      Y:   The name of a table in a FROM clause.  Or in a trigger
   74157                 : **           one of the special names "old" or "new".
   74158                 : **
   74159                 : **      Z:   The name of a column in table Y.
   74160                 : **
   74161                 : ** The node at the root of the subtree is modified as follows:
   74162                 : **
   74163                 : **    Expr.op        Changed to TK_COLUMN
   74164                 : **    Expr.pTab      Points to the Table object for X.Y
   74165                 : **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
   74166                 : **    Expr.iTable    The VDBE cursor number for X.Y
   74167                 : **
   74168                 : **
   74169                 : ** To resolve result-set references, look for expression nodes of the
   74170                 : ** form Z (with no X and Y prefix) where the Z matches the right-hand
   74171                 : ** size of an AS clause in the result-set of a SELECT.  The Z expression
   74172                 : ** is replaced by a copy of the left-hand side of the result-set expression.
   74173                 : ** Table-name and function resolution occurs on the substituted expression
   74174                 : ** tree.  For example, in:
   74175                 : **
   74176                 : **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
   74177                 : **
   74178                 : ** The "x" term of the order by is replaced by "a+b" to render:
   74179                 : **
   74180                 : **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
   74181                 : **
   74182                 : ** Function calls are checked to make sure that the function is 
   74183                 : ** defined and that the correct number of arguments are specified.
   74184                 : ** If the function is an aggregate function, then the pNC->hasAgg is
   74185                 : ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
   74186                 : ** If an expression contains aggregate functions then the EP_Agg
   74187                 : ** property on the expression is set.
   74188                 : **
   74189                 : ** An error message is left in pParse if anything is amiss.  The number
   74190                 : ** if errors is returned.
   74191                 : */
   74192          742869 : SQLITE_PRIVATE int sqlite3ResolveExprNames( 
   74193                 :   NameContext *pNC,       /* Namespace to resolve expressions in. */
   74194                 :   Expr *pExpr             /* The expression to be analyzed. */
   74195                 : ){
   74196                 :   int savedHasAgg;
   74197                 :   Walker w;
   74198                 : 
   74199          742869 :   if( pExpr==0 ) return 0;
   74200                 : #if SQLITE_MAX_EXPR_DEPTH>0
   74201                 :   {
   74202          551303 :     Parse *pParse = pNC->pParse;
   74203          551303 :     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
   74204               0 :       return 1;
   74205                 :     }
   74206          551303 :     pParse->nHeight += pExpr->nHeight;
   74207                 :   }
   74208                 : #endif
   74209          551303 :   savedHasAgg = pNC->hasAgg;
   74210          551303 :   pNC->hasAgg = 0;
   74211          551303 :   w.xExprCallback = resolveExprStep;
   74212          551303 :   w.xSelectCallback = resolveSelectStep;
   74213          551303 :   w.pParse = pNC->pParse;
   74214          551303 :   w.u.pNC = pNC;
   74215          551303 :   sqlite3WalkExpr(&w, pExpr);
   74216                 : #if SQLITE_MAX_EXPR_DEPTH>0
   74217          551303 :   pNC->pParse->nHeight -= pExpr->nHeight;
   74218                 : #endif
   74219          551303 :   if( pNC->nErr>0 || w.pParse->nErr>0 ){
   74220              53 :     ExprSetProperty(pExpr, EP_Error);
   74221                 :   }
   74222          551303 :   if( pNC->hasAgg ){
   74223            4353 :     ExprSetProperty(pExpr, EP_Agg);
   74224          546950 :   }else if( savedHasAgg ){
   74225            2181 :     pNC->hasAgg = 1;
   74226                 :   }
   74227          551303 :   return ExprHasProperty(pExpr, EP_Error);
   74228                 : }
   74229                 : 
   74230                 : 
   74231                 : /*
   74232                 : ** Resolve all names in all expressions of a SELECT and in all
   74233                 : ** decendents of the SELECT, including compounds off of p->pPrior,
   74234                 : ** subqueries in expressions, and subqueries used as FROM clause
   74235                 : ** terms.
   74236                 : **
   74237                 : ** See sqlite3ResolveExprNames() for a description of the kinds of
   74238                 : ** transformations that occur.
   74239                 : **
   74240                 : ** All SELECT statements should have been expanded using
   74241                 : ** sqlite3SelectExpand() prior to invoking this routine.
   74242                 : */
   74243           54234 : SQLITE_PRIVATE void sqlite3ResolveSelectNames(
   74244                 :   Parse *pParse,         /* The parser context */
   74245                 :   Select *p,             /* The SELECT statement being coded. */
   74246                 :   NameContext *pOuterNC  /* Name context for parent SELECT statement */
   74247                 : ){
   74248                 :   Walker w;
   74249                 : 
   74250           54234 :   assert( p!=0 );
   74251           54234 :   w.xExprCallback = resolveExprStep;
   74252           54234 :   w.xSelectCallback = resolveSelectStep;
   74253           54234 :   w.pParse = pParse;
   74254           54234 :   w.u.pNC = pOuterNC;
   74255           54234 :   sqlite3WalkSelect(&w, p);
   74256           54234 : }
   74257                 : 
   74258                 : /************** End of resolve.c *********************************************/
   74259                 : /************** Begin file expr.c ********************************************/
   74260                 : /*
   74261                 : ** 2001 September 15
   74262                 : **
   74263                 : ** The author disclaims copyright to this source code.  In place of
   74264                 : ** a legal notice, here is a blessing:
   74265                 : **
   74266                 : **    May you do good and not evil.
   74267                 : **    May you find forgiveness for yourself and forgive others.
   74268                 : **    May you share freely, never taking more than you give.
   74269                 : **
   74270                 : *************************************************************************
   74271                 : ** This file contains routines used for analyzing expressions and
   74272                 : ** for generating VDBE code that evaluates expressions in SQLite.
   74273                 : */
   74274                 : 
   74275                 : /*
   74276                 : ** Return the 'affinity' of the expression pExpr if any.
   74277                 : **
   74278                 : ** If pExpr is a column, a reference to a column via an 'AS' alias,
   74279                 : ** or a sub-select with a column as the return value, then the 
   74280                 : ** affinity of that column is returned. Otherwise, 0x00 is returned,
   74281                 : ** indicating no affinity for the expression.
   74282                 : **
   74283                 : ** i.e. the WHERE clause expresssions in the following statements all
   74284                 : ** have an affinity:
   74285                 : **
   74286                 : ** CREATE TABLE t1(a);
   74287                 : ** SELECT * FROM t1 WHERE a;
   74288                 : ** SELECT a AS b FROM t1 WHERE b;
   74289                 : ** SELECT * FROM t1 WHERE (select a from t1);
   74290                 : */
   74291          245223 : SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
   74292          245223 :   int op = pExpr->op;
   74293          245223 :   if( op==TK_SELECT ){
   74294            2461 :     assert( pExpr->flags&EP_xIsSelect );
   74295            2461 :     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
   74296                 :   }
   74297                 : #ifndef SQLITE_OMIT_CAST
   74298          242762 :   if( op==TK_CAST ){
   74299               0 :     assert( !ExprHasProperty(pExpr, EP_IntValue) );
   74300               0 :     return sqlite3AffinityType(pExpr->u.zToken);
   74301                 :   }
   74302                 : #endif
   74303          242762 :   if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
   74304          201634 :    && pExpr->pTab!=0
   74305                 :   ){
   74306                 :     /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
   74307                 :     ** a TK_COLUMN but was previously evaluated and cached in a register */
   74308          128464 :     int j = pExpr->iColumn;
   74309          128464 :     if( j<0 ) return SQLITE_AFF_INTEGER;
   74310          110277 :     assert( pExpr->pTab && j<pExpr->pTab->nCol );
   74311          110277 :     return pExpr->pTab->aCol[j].affinity;
   74312                 :   }
   74313          114298 :   return pExpr->affinity;
   74314                 : }
   74315                 : 
   74316                 : /*
   74317                 : ** Set the explicit collating sequence for an expression to the
   74318                 : ** collating sequence supplied in the second argument.
   74319                 : */
   74320             359 : SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Expr *pExpr, CollSeq *pColl){
   74321             359 :   if( pExpr && pColl ){
   74322             359 :     pExpr->pColl = pColl;
   74323             359 :     pExpr->flags |= EP_ExpCollate;
   74324                 :   }
   74325             359 :   return pExpr;
   74326                 : }
   74327                 : 
   74328                 : /*
   74329                 : ** Set the collating sequence for expression pExpr to be the collating
   74330                 : ** sequence named by pToken.   Return a pointer to the revised expression.
   74331                 : ** The collating sequence is marked as "explicit" using the EP_ExpCollate
   74332                 : ** flag.  An explicit collating sequence will override implicit
   74333                 : ** collating sequences.
   74334                 : */
   74335              31 : SQLITE_PRIVATE Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){
   74336              31 :   char *zColl = 0;            /* Dequoted name of collation sequence */
   74337                 :   CollSeq *pColl;
   74338              31 :   sqlite3 *db = pParse->db;
   74339              31 :   zColl = sqlite3NameFromToken(db, pCollName);
   74340              31 :   pColl = sqlite3LocateCollSeq(pParse, zColl);
   74341              31 :   sqlite3ExprSetColl(pExpr, pColl);
   74342              31 :   sqlite3DbFree(db, zColl);
   74343              31 :   return pExpr;
   74344                 : }
   74345                 : 
   74346                 : /*
   74347                 : ** Return the default collation sequence for the expression pExpr. If
   74348                 : ** there is no default collation type, return 0.
   74349                 : */
   74350          194127 : SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
   74351          194127 :   CollSeq *pColl = 0;
   74352          194127 :   Expr *p = pExpr;
   74353          388256 :   while( p ){
   74354                 :     int op;
   74355          194129 :     pColl = p->pColl;
   74356          194129 :     if( pColl ) break;
   74357          144281 :     op = p->op;
   74358          144281 :     if( p->pTab!=0 && (
   74359          138236 :         op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER || op==TK_TRIGGER
   74360                 :     )){
   74361                 :       /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
   74362                 :       ** a TK_COLUMN but was previously evaluated and cached in a register */
   74363                 :       const char *zColl;
   74364          140103 :       int j = p->iColumn;
   74365          140103 :       if( j>=0 ){
   74366           86472 :         sqlite3 *db = pParse->db;
   74367           86472 :         zColl = p->pTab->aCol[j].zColl;
   74368           86472 :         pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
   74369           86472 :         pExpr->pColl = pColl;
   74370                 :       }
   74371          140103 :       break;
   74372                 :     }
   74373            4178 :     if( op!=TK_CAST && op!=TK_UPLUS ){
   74374            4176 :       break;
   74375                 :     }
   74376               2 :     p = p->pLeft;
   74377                 :   }
   74378          194127 :   if( sqlite3CheckCollSeq(pParse, pColl) ){ 
   74379               0 :     pColl = 0;
   74380                 :   }
   74381          194127 :   return pColl;
   74382                 : }
   74383                 : 
   74384                 : /*
   74385                 : ** pExpr is an operand of a comparison operator.  aff2 is the
   74386                 : ** type affinity of the other operand.  This routine returns the
   74387                 : ** type affinity that should be used for the comparison operator.
   74388                 : */
   74389          130467 : SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
   74390          130467 :   char aff1 = sqlite3ExprAffinity(pExpr);
   74391          130467 :   if( aff1 && aff2 ){
   74392                 :     /* Both sides of the comparison are columns. If one has numeric
   74393                 :     ** affinity, use that. Otherwise use no affinity.
   74394                 :     */
   74395           22730 :     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
   74396           21092 :       return SQLITE_AFF_NUMERIC;
   74397                 :     }else{
   74398            1638 :       return SQLITE_AFF_NONE;
   74399                 :     }
   74400          107737 :   }else if( !aff1 && !aff2 ){
   74401                 :     /* Neither side of the comparison is a column.  Compare the
   74402                 :     ** results directly.
   74403                 :     */
   74404            1573 :     return SQLITE_AFF_NONE;
   74405                 :   }else{
   74406                 :     /* One side is a column, the other is not. Use the columns affinity. */
   74407          106164 :     assert( aff1==0 || aff2==0 );
   74408          106164 :     return (aff1 + aff2);
   74409                 :   }
   74410                 : }
   74411                 : 
   74412                 : /*
   74413                 : ** pExpr is a comparison operator.  Return the type affinity that should
   74414                 : ** be applied to both operands prior to doing the comparison.
   74415                 : */
   74416           62548 : static char comparisonAffinity(Expr *pExpr){
   74417                 :   char aff;
   74418           62548 :   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
   74419                 :           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
   74420                 :           pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
   74421           62548 :   assert( pExpr->pLeft );
   74422           62548 :   aff = sqlite3ExprAffinity(pExpr->pLeft);
   74423           62548 :   if( pExpr->pRight ){
   74424           58989 :     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
   74425            3559 :   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   74426            1103 :     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
   74427            2456 :   }else if( !aff ){
   74428             576 :     aff = SQLITE_AFF_NONE;
   74429                 :   }
   74430           62548 :   return aff;
   74431                 : }
   74432                 : 
   74433                 : /*
   74434                 : ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
   74435                 : ** idx_affinity is the affinity of an indexed column. Return true
   74436                 : ** if the index with affinity idx_affinity may be used to implement
   74437                 : ** the comparison in pExpr.
   74438                 : */
   74439           60625 : SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
   74440           60625 :   char aff = comparisonAffinity(pExpr);
   74441           60625 :   switch( aff ){
   74442                 :     case SQLITE_AFF_NONE:
   74443            2815 :       return 1;
   74444                 :     case SQLITE_AFF_TEXT:
   74445           17570 :       return idx_affinity==SQLITE_AFF_TEXT;
   74446                 :     default:
   74447           40240 :       return sqlite3IsNumericAffinity(idx_affinity);
   74448                 :   }
   74449                 : }
   74450                 : 
   74451                 : /*
   74452                 : ** Return the P5 value that should be used for a binary comparison
   74453                 : ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
   74454                 : */
   74455           42017 : static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
   74456           42017 :   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
   74457           42017 :   aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
   74458           42017 :   return aff;
   74459                 : }
   74460                 : 
   74461                 : /*
   74462                 : ** Return a pointer to the collation sequence that should be used by
   74463                 : ** a binary comparison operator comparing pLeft and pRight.
   74464                 : **
   74465                 : ** If the left hand expression has a collating sequence type, then it is
   74466                 : ** used. Otherwise the collation sequence for the right hand expression
   74467                 : ** is used, or the default (BINARY) if neither expression has a collating
   74468                 : ** type.
   74469                 : **
   74470                 : ** Argument pRight (but not pLeft) may be a null pointer. In this case,
   74471                 : ** it is not considered.
   74472                 : */
   74473          105045 : SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
   74474                 :   Parse *pParse, 
   74475                 :   Expr *pLeft, 
   74476                 :   Expr *pRight
   74477                 : ){
   74478                 :   CollSeq *pColl;
   74479          105045 :   assert( pLeft );
   74480          105045 :   if( pLeft->flags & EP_ExpCollate ){
   74481             520 :     assert( pLeft->pColl );
   74482             520 :     pColl = pLeft->pColl;
   74483          104525 :   }else if( pRight && pRight->flags & EP_ExpCollate ){
   74484               0 :     assert( pRight->pColl );
   74485               0 :     pColl = pRight->pColl;
   74486                 :   }else{
   74487          104525 :     pColl = sqlite3ExprCollSeq(pParse, pLeft);
   74488          104525 :     if( !pColl ){
   74489            3830 :       pColl = sqlite3ExprCollSeq(pParse, pRight);
   74490                 :     }
   74491                 :   }
   74492          105045 :   return pColl;
   74493                 : }
   74494                 : 
   74495                 : /*
   74496                 : ** Generate code for a comparison operator.
   74497                 : */
   74498           42017 : static int codeCompare(
   74499                 :   Parse *pParse,    /* The parsing (and code generating) context */
   74500                 :   Expr *pLeft,      /* The left operand */
   74501                 :   Expr *pRight,     /* The right operand */
   74502                 :   int opcode,       /* The comparison opcode */
   74503                 :   int in1, int in2, /* Register holding operands */
   74504                 :   int dest,         /* Jump here if true.  */
   74505                 :   int jumpIfNull    /* If true, jump if either operand is NULL */
   74506                 : ){
   74507                 :   int p5;
   74508                 :   int addr;
   74509                 :   CollSeq *p4;
   74510                 : 
   74511           42017 :   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
   74512           42017 :   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
   74513           42017 :   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
   74514                 :                            (void*)p4, P4_COLLSEQ);
   74515           42017 :   sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
   74516           42017 :   return addr;
   74517                 : }
   74518                 : 
   74519                 : #if SQLITE_MAX_EXPR_DEPTH>0
   74520                 : /*
   74521                 : ** Check that argument nHeight is less than or equal to the maximum
   74522                 : ** expression depth allowed. If it is not, leave an error message in
   74523                 : ** pParse.
   74524                 : */
   74525         1695790 : SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
   74526         1695790 :   int rc = SQLITE_OK;
   74527         1695790 :   int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
   74528         1695790 :   if( nHeight>mxHeight ){
   74529               0 :     sqlite3ErrorMsg(pParse, 
   74530                 :        "Expression tree is too large (maximum depth %d)", mxHeight
   74531                 :     );
   74532               0 :     rc = SQLITE_ERROR;
   74533                 :   }
   74534         1695790 :   return rc;
   74535                 : }
   74536                 : 
   74537                 : /* The following three functions, heightOfExpr(), heightOfExprList()
   74538                 : ** and heightOfSelect(), are used to determine the maximum height
   74539                 : ** of any expression tree referenced by the structure passed as the
   74540                 : ** first argument.
   74541                 : **
   74542                 : ** If this maximum height is greater than the current value pointed
   74543                 : ** to by pnHeight, the second parameter, then set *pnHeight to that
   74544                 : ** value.
   74545                 : */
   74546         2454367 : static void heightOfExpr(Expr *p, int *pnHeight){
   74547         2454367 :   if( p ){
   74548          580934 :     if( p->nHeight>*pnHeight ){
   74549          337386 :       *pnHeight = p->nHeight;
   74550                 :     }
   74551                 :   }
   74552         2454367 : }
   74553         1187163 : static void heightOfExprList(ExprList *p, int *pnHeight){
   74554         1187163 :   if( p ){
   74555                 :     int i;
   74556          135811 :     for(i=0; i<p->nExpr; i++){
   74557           80651 :       heightOfExpr(p->a[i].pExpr, pnHeight);
   74558                 :     }
   74559                 :   }
   74560         1187163 : }
   74561           37009 : static void heightOfSelect(Select *p, int *pnHeight){
   74562           37009 :   if( p ){
   74563           18535 :     heightOfExpr(p->pWhere, pnHeight);
   74564           18535 :     heightOfExpr(p->pHaving, pnHeight);
   74565           18535 :     heightOfExpr(p->pLimit, pnHeight);
   74566           18535 :     heightOfExpr(p->pOffset, pnHeight);
   74567           18535 :     heightOfExprList(p->pEList, pnHeight);
   74568           18535 :     heightOfExprList(p->pGroupBy, pnHeight);
   74569           18535 :     heightOfExprList(p->pOrderBy, pnHeight);
   74570           18535 :     heightOfSelect(p->pPrior, pnHeight);
   74571                 :   }
   74572           37009 : }
   74573                 : 
   74574                 : /*
   74575                 : ** Set the Expr.nHeight variable in the structure passed as an 
   74576                 : ** argument. An expression with no children, Expr.pList or 
   74577                 : ** Expr.pSelect member has a height of 1. Any other expression
   74578                 : ** has a height equal to the maximum height of any other 
   74579                 : ** referenced Expr plus one.
   74580                 : */
   74581         1149788 : static void exprSetHeight(Expr *p){
   74582         1149788 :   int nHeight = 0;
   74583         1149788 :   heightOfExpr(p->pLeft, &nHeight);
   74584         1149788 :   heightOfExpr(p->pRight, &nHeight);
   74585         1149788 :   if( ExprHasProperty(p, EP_xIsSelect) ){
   74586           18230 :     heightOfSelect(p->x.pSelect, &nHeight);
   74587                 :   }else{
   74588         1131558 :     heightOfExprList(p->x.pList, &nHeight);
   74589                 :   }
   74590         1149788 :   p->nHeight = nHeight + 1;
   74591         1149788 : }
   74592                 : 
   74593                 : /*
   74594                 : ** Set the Expr.nHeight variable using the exprSetHeight() function. If
   74595                 : ** the height is greater than the maximum allowed expression depth,
   74596                 : ** leave an error in pParse.
   74597                 : */
   74598           57192 : SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
   74599           57192 :   exprSetHeight(p);
   74600           57192 :   sqlite3ExprCheckHeight(pParse, p->nHeight);
   74601           57192 : }
   74602                 : 
   74603                 : /*
   74604                 : ** Return the maximum height of any expression tree referenced
   74605                 : ** by the select statement passed as an argument.
   74606                 : */
   74607             244 : SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
   74608             244 :   int nHeight = 0;
   74609             244 :   heightOfSelect(p, &nHeight);
   74610             244 :   return nHeight;
   74611                 : }
   74612                 : #else
   74613                 :   #define exprSetHeight(y)
   74614                 : #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
   74615                 : 
   74616                 : /*
   74617                 : ** This routine is the core allocator for Expr nodes.
   74618                 : **
   74619                 : ** Construct a new expression node and return a pointer to it.  Memory
   74620                 : ** for this node and for the pToken argument is a single allocation
   74621                 : ** obtained from sqlite3DbMalloc().  The calling function
   74622                 : ** is responsible for making sure the node eventually gets freed.
   74623                 : **
   74624                 : ** If dequote is true, then the token (if it exists) is dequoted.
   74625                 : ** If dequote is false, no dequoting is performance.  The deQuote
   74626                 : ** parameter is ignored if pToken is NULL or if the token does not
   74627                 : ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
   74628                 : ** then the EP_DblQuoted flag is set on the expression node.
   74629                 : **
   74630                 : ** Special case:  If op==TK_INTEGER and pToken points to a string that
   74631                 : ** can be translated into a 32-bit integer, then the token is not
   74632                 : ** stored in u.zToken.  Instead, the integer values is written
   74633                 : ** into u.iValue and the EP_IntValue flag is set.  No extra storage
   74634                 : ** is allocated to hold the integer text and the dequote flag is ignored.
   74635                 : */
   74636         1151665 : SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
   74637                 :   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
   74638                 :   int op,                 /* Expression opcode */
   74639                 :   const Token *pToken,    /* Token argument.  Might be NULL */
   74640                 :   int dequote             /* True to dequote */
   74641                 : ){
   74642                 :   Expr *pNew;
   74643         1151665 :   int nExtra = 0;
   74644         1151665 :   int iValue = 0;
   74645                 : 
   74646         1151665 :   if( pToken ){
   74647          889238 :     if( op!=TK_INTEGER || pToken->z==0
   74648           61168 :           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
   74649          828245 :       nExtra = pToken->n+1;
   74650          828245 :       assert( iValue>=0 );
   74651                 :     }
   74652                 :   }
   74653         1151665 :   pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
   74654         1151665 :   if( pNew ){
   74655         1151665 :     pNew->op = (u8)op;
   74656         1151665 :     pNew->iAgg = -1;
   74657         1151665 :     if( pToken ){
   74658          889238 :       if( nExtra==0 ){
   74659           60993 :         pNew->flags |= EP_IntValue;
   74660           60993 :         pNew->u.iValue = iValue;
   74661                 :       }else{
   74662                 :         int c;
   74663          828245 :         pNew->u.zToken = (char*)&pNew[1];
   74664          828245 :         assert( pToken->z!=0 || pToken->n==0 );
   74665          828245 :         if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
   74666          828245 :         pNew->u.zToken[pToken->n] = 0;
   74667          828245 :         if( dequote && nExtra>=3 
   74668          779441 :              && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
   74669          110776 :           sqlite3Dequote(pNew->u.zToken);
   74670          110775 :           if( c=='"' ) pNew->flags |= EP_DblQuoted;
   74671                 :         }
   74672                 :       }
   74673                 :     }
   74674                 : #if SQLITE_MAX_EXPR_DEPTH>0
   74675         1151664 :     pNew->nHeight = 1;
   74676                 : #endif  
   74677                 :   }
   74678         1151664 :   return pNew;
   74679                 : }
   74680                 : 
   74681                 : /*
   74682                 : ** Allocate a new expression node from a zero-terminated token that has
   74683                 : ** already been dequoted.
   74684                 : */
   74685           22554 : SQLITE_PRIVATE Expr *sqlite3Expr(
   74686                 :   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
   74687                 :   int op,                 /* Expression opcode */
   74688                 :   const char *zToken      /* Token argument.  Might be NULL */
   74689                 : ){
   74690                 :   Token x;
   74691           22554 :   x.z = zToken;
   74692           22554 :   x.n = zToken ? sqlite3Strlen30(zToken) : 0;
   74693           22554 :   return sqlite3ExprAlloc(db, op, &x, 0);
   74694                 : }
   74695                 : 
   74696                 : /*
   74697                 : ** Attach subtrees pLeft and pRight to the Expr node pRoot.
   74698                 : **
   74699                 : ** If pRoot==NULL that means that a memory allocation error has occurred.
   74700                 : ** In that case, delete the subtrees pLeft and pRight.
   74701                 : */
   74702         1092595 : SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
   74703                 :   sqlite3 *db,
   74704                 :   Expr *pRoot,
   74705                 :   Expr *pLeft,
   74706                 :   Expr *pRight
   74707                 : ){
   74708         1092595 :   if( pRoot==0 ){
   74709               0 :     assert( db->mallocFailed );
   74710               0 :     sqlite3ExprDelete(db, pLeft);
   74711               0 :     sqlite3ExprDelete(db, pRight);
   74712                 :   }else{
   74713         1092595 :     if( pRight ){
   74714          231641 :       pRoot->pRight = pRight;
   74715          231641 :       if( pRight->flags & EP_ExpCollate ){
   74716               0 :         pRoot->flags |= EP_ExpCollate;
   74717               0 :         pRoot->pColl = pRight->pColl;
   74718                 :       }
   74719                 :     }
   74720         1092595 :     if( pLeft ){
   74721          247318 :       pRoot->pLeft = pLeft;
   74722          247318 :       if( pLeft->flags & EP_ExpCollate ){
   74723             328 :         pRoot->flags |= EP_ExpCollate;
   74724             328 :         pRoot->pColl = pLeft->pColl;
   74725                 :       }
   74726                 :     }
   74727         1092595 :     exprSetHeight(pRoot);
   74728                 :   }
   74729         1092595 : }
   74730                 : 
   74731                 : /*
   74732                 : ** Allocate a Expr node which joins as many as two subtrees.
   74733                 : **
   74734                 : ** One or both of the subtrees can be NULL.  Return a pointer to the new
   74735                 : ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
   74736                 : ** free the subtrees and return NULL.
   74737                 : */
   74738         1087296 : SQLITE_PRIVATE Expr *sqlite3PExpr(
   74739                 :   Parse *pParse,          /* Parsing context */
   74740                 :   int op,                 /* Expression opcode */
   74741                 :   Expr *pLeft,            /* Left operand */
   74742                 :   Expr *pRight,           /* Right operand */
   74743                 :   const Token *pToken     /* Argument token */
   74744                 : ){
   74745         1087296 :   Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
   74746         1087295 :   sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
   74747         1087295 :   if( p ) {
   74748         1087295 :     sqlite3ExprCheckHeight(pParse, p->nHeight);
   74749                 :   }
   74750         1087295 :   return p;
   74751                 : }
   74752                 : 
   74753                 : /*
   74754                 : ** Join two expressions using an AND operator.  If either expression is
   74755                 : ** NULL, then just return the other expression.
   74756                 : */
   74757            6395 : SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
   74758            6395 :   if( pLeft==0 ){
   74759            1041 :     return pRight;
   74760            5354 :   }else if( pRight==0 ){
   74761              54 :     return pLeft;
   74762                 :   }else{
   74763            5300 :     Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
   74764            5300 :     sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
   74765            5300 :     return pNew;
   74766                 :   }
   74767                 : }
   74768                 : 
   74769                 : /*
   74770                 : ** Construct a new expression node for a function with multiple
   74771                 : ** arguments.
   74772                 : */
   74773           36513 : SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
   74774                 :   Expr *pNew;
   74775           36513 :   sqlite3 *db = pParse->db;
   74776           36513 :   assert( pToken );
   74777           36513 :   pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
   74778           36513 :   if( pNew==0 ){
   74779               0 :     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
   74780               0 :     return 0;
   74781                 :   }
   74782           36513 :   pNew->x.pList = pList;
   74783           36513 :   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   74784           36513 :   sqlite3ExprSetHeight(pParse, pNew);
   74785           36513 :   return pNew;
   74786                 : }
   74787                 : 
   74788                 : /*
   74789                 : ** Assign a variable number to an expression that encodes a wildcard
   74790                 : ** in the original SQL statement.  
   74791                 : **
   74792                 : ** Wildcards consisting of a single "?" are assigned the next sequential
   74793                 : ** variable number.
   74794                 : **
   74795                 : ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
   74796                 : ** sure "nnn" is not too be to avoid a denial of service attack when
   74797                 : ** the SQL statement comes from an external source.
   74798                 : **
   74799                 : ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
   74800                 : ** as the previous instance of the same wildcard.  Or if this is the first
   74801                 : ** instance of the wildcard, the next sequenial variable number is
   74802                 : ** assigned.
   74803                 : */
   74804           87175 : SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
   74805           87175 :   sqlite3 *db = pParse->db;
   74806                 :   const char *z;
   74807                 : 
   74808           87175 :   if( pExpr==0 ) return;
   74809           87175 :   assert( !ExprHasAnyProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
   74810           87175 :   z = pExpr->u.zToken;
   74811           87175 :   assert( z!=0 );
   74812           87175 :   assert( z[0]!=0 );
   74813           87175 :   if( z[1]==0 ){
   74814                 :     /* Wildcard of the form "?".  Assign the next variable number */
   74815             981 :     assert( z[0]=='?' );
   74816             981 :     pExpr->iColumn = (ynVar)(++pParse->nVar);
   74817                 :   }else{
   74818           86194 :     ynVar x = 0;
   74819           86194 :     u32 n = sqlite3Strlen30(z);
   74820           86194 :     if( z[0]=='?' ){
   74821                 :       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
   74822                 :       ** use it as the variable number */
   74823                 :       i64 i;
   74824           12430 :       int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
   74825           12430 :       pExpr->iColumn = x = (ynVar)i;
   74826                 :       testcase( i==0 );
   74827                 :       testcase( i==1 );
   74828                 :       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
   74829                 :       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
   74830           12430 :       if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
   74831               0 :         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
   74832                 :             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
   74833               0 :         x = 0;
   74834                 :       }
   74835           12430 :       if( i>pParse->nVar ){
   74836           11830 :         pParse->nVar = (int)i;
   74837                 :       }
   74838                 :     }else{
   74839                 :       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
   74840                 :       ** number as the prior appearance of the same name, or if the name
   74841                 :       ** has never appeared before, reuse the same variable number
   74842                 :       */
   74843                 :       ynVar i;
   74844          405168 :       for(i=0; i<pParse->nzVar; i++){
   74845          333143 :         if( pParse->azVar[i] && memcmp(pParse->azVar[i],z,n+1)==0 ){
   74846            1739 :           pExpr->iColumn = x = (ynVar)i+1;
   74847            1739 :           break;
   74848                 :         }
   74849                 :       }
   74850           73764 :       if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
   74851                 :     }
   74852           86194 :     if( x>0 ){
   74853           86194 :       if( x>pParse->nzVar ){
   74854                 :         char **a;
   74855           83855 :         a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
   74856           83855 :         if( a==0 ) return;  /* Error reported through db->mallocFailed */
   74857           83855 :         pParse->azVar = a;
   74858           83855 :         memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
   74859           83855 :         pParse->nzVar = x;
   74860                 :       }
   74861           86194 :       if( z[0]!='?' || pParse->azVar[x-1]==0 ){
   74862           86164 :         sqlite3DbFree(db, pParse->azVar[x-1]);
   74863           86164 :         pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
   74864                 :       }
   74865                 :     }
   74866                 :   } 
   74867           87175 :   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
   74868               0 :     sqlite3ErrorMsg(pParse, "too many SQL variables");
   74869                 :   }
   74870                 : }
   74871                 : 
   74872                 : /*
   74873                 : ** Recursively delete an expression tree.
   74874                 : */
   74875         5150586 : SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
   74876         5150586 :   if( p==0 ) return;
   74877                 :   /* Sanity check: Assert that the IntValue is non-negative if it exists */
   74878         1514137 :   assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
   74879         1514137 :   if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
   74880         1376510 :     sqlite3ExprDelete(db, p->pLeft);
   74881         1376510 :     sqlite3ExprDelete(db, p->pRight);
   74882         1376510 :     if( !ExprHasProperty(p, EP_Reduced) && (p->flags2 & EP2_MallocedToken)!=0 ){
   74883             575 :       sqlite3DbFree(db, p->u.zToken);
   74884                 :     }
   74885         1376510 :     if( ExprHasProperty(p, EP_xIsSelect) ){
   74886           29294 :       sqlite3SelectDelete(db, p->x.pSelect);
   74887                 :     }else{
   74888         1347216 :       sqlite3ExprListDelete(db, p->x.pList);
   74889                 :     }
   74890                 :   }
   74891         1514136 :   if( !ExprHasProperty(p, EP_Static) ){
   74892         1359544 :     sqlite3DbFree(db, p);
   74893                 :   }
   74894                 : }
   74895                 : 
   74896                 : /*
   74897                 : ** Return the number of bytes allocated for the expression structure 
   74898                 : ** passed as the first argument. This is always one of EXPR_FULLSIZE,
   74899                 : ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
   74900                 : */
   74901          113681 : static int exprStructSize(Expr *p){
   74902          113681 :   if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
   74903           74266 :   if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
   74904           38815 :   return EXPR_FULLSIZE;
   74905                 : }
   74906                 : 
   74907                 : /*
   74908                 : ** The dupedExpr*Size() routines each return the number of bytes required
   74909                 : ** to store a copy of an expression or expression tree.  They differ in
   74910                 : ** how much of the tree is measured.
   74911                 : **
   74912                 : **     dupedExprStructSize()     Size of only the Expr structure 
   74913                 : **     dupedExprNodeSize()       Size of Expr + space for token
   74914                 : **     dupedExprSize()           Expr + token + subtree components
   74915                 : **
   74916                 : ***************************************************************************
   74917                 : **
   74918                 : ** The dupedExprStructSize() function returns two values OR-ed together:  
   74919                 : ** (1) the space required for a copy of the Expr structure only and 
   74920                 : ** (2) the EP_xxx flags that indicate what the structure size should be.
   74921                 : ** The return values is always one of:
   74922                 : **
   74923                 : **      EXPR_FULLSIZE
   74924                 : **      EXPR_REDUCEDSIZE   | EP_Reduced
   74925                 : **      EXPR_TOKENONLYSIZE | EP_TokenOnly
   74926                 : **
   74927                 : ** The size of the structure can be found by masking the return value
   74928                 : ** of this routine with 0xfff.  The flags can be found by masking the
   74929                 : ** return value with EP_Reduced|EP_TokenOnly.
   74930                 : **
   74931                 : ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
   74932                 : ** (unreduced) Expr objects as they or originally constructed by the parser.
   74933                 : ** During expression analysis, extra information is computed and moved into
   74934                 : ** later parts of teh Expr object and that extra information might get chopped
   74935                 : ** off if the expression is reduced.  Note also that it does not work to
   74936                 : ** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
   74937                 : ** to reduce a pristine expression tree from the parser.  The implementation
   74938                 : ** of dupedExprStructSize() contain multiple assert() statements that attempt
   74939                 : ** to enforce this constraint.
   74940                 : */
   74941          973399 : static int dupedExprStructSize(Expr *p, int flags){
   74942                 :   int nSize;
   74943          973399 :   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
   74944          973399 :   if( 0==(flags&EXPRDUP_REDUCE) ){
   74945          227362 :     nSize = EXPR_FULLSIZE;
   74946                 :   }else{
   74947          746037 :     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
   74948          746037 :     assert( !ExprHasProperty(p, EP_FromJoin) ); 
   74949          746037 :     assert( (p->flags2 & EP2_MallocedToken)==0 );
   74950          746037 :     assert( (p->flags2 & EP2_Irreducible)==0 );
   74951          746037 :     if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
   74952          333354 :       nSize = EXPR_REDUCEDSIZE | EP_Reduced;
   74953                 :     }else{
   74954          412683 :       nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
   74955                 :     }
   74956                 :   }
   74957          973399 :   return nSize;
   74958                 : }
   74959                 : 
   74960                 : /*
   74961                 : ** This function returns the space in bytes required to store the copy 
   74962                 : ** of the Expr structure and a copy of the Expr.u.zToken string (if that
   74963                 : ** string is defined.)
   74964                 : */
   74965          611039 : static int dupedExprNodeSize(Expr *p, int flags){
   74966          611039 :   int nByte = dupedExprStructSize(p, flags) & 0xfff;
   74967          611039 :   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
   74968          319937 :     nByte += sqlite3Strlen30(p->u.zToken)+1;
   74969                 :   }
   74970          611039 :   return ROUND8(nByte);
   74971                 : }
   74972                 : 
   74973                 : /*
   74974                 : ** Return the number of bytes required to create a duplicate of the 
   74975                 : ** expression passed as the first argument. The second argument is a
   74976                 : ** mask containing EXPRDUP_XXX flags.
   74977                 : **
   74978                 : ** The value returned includes space to create a copy of the Expr struct
   74979                 : ** itself and the buffer referred to by Expr.u.zToken, if any.
   74980                 : **
   74981                 : ** If the EXPRDUP_REDUCE flag is set, then the return value includes 
   74982                 : ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
   74983                 : ** and Expr.pRight variables (but not for any structures pointed to or 
   74984                 : ** descended from the Expr.x.pList or Expr.x.pSelect variables).
   74985                 : */
   74986          706242 : static int dupedExprSize(Expr *p, int flags){
   74987          706242 :   int nByte = 0;
   74988          706242 :   if( p ){
   74989          362360 :     nByte = dupedExprNodeSize(p, flags);
   74990          362360 :     if( flags&EXPRDUP_REDUCE ){
   74991          248679 :       nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
   74992                 :     }
   74993                 :   }
   74994          706242 :   return nByte;
   74995                 : }
   74996                 : 
   74997                 : /*
   74998                 : ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
   74999                 : ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
   75000                 : ** to store the copy of expression p, the copies of p->u.zToken
   75001                 : ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
   75002                 : ** if any. Before returning, *pzBuffer is set to the first byte passed the
   75003                 : ** portion of the buffer copied into by this function.
   75004                 : */
   75005          563846 : static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
   75006          563846 :   Expr *pNew = 0;                      /* Value to return */
   75007          563846 :   if( p ){
   75008          362360 :     const int isReduced = (flags&EXPRDUP_REDUCE);
   75009                 :     u8 *zAlloc;
   75010          362360 :     u32 staticFlag = 0;
   75011                 : 
   75012          362360 :     assert( pzBuffer==0 || isReduced );
   75013                 : 
   75014                 :     /* Figure out where to write the new Expr structure. */
   75015          362360 :     if( pzBuffer ){
   75016          153476 :       zAlloc = *pzBuffer;
   75017          153476 :       staticFlag = EP_Static;
   75018                 :     }else{
   75019          208884 :       zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
   75020                 :     }
   75021          362360 :     pNew = (Expr *)zAlloc;
   75022                 : 
   75023          362360 :     if( pNew ){
   75024                 :       /* Set nNewSize to the size allocated for the structure pointed to
   75025                 :       ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
   75026                 :       ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
   75027                 :       ** by the copy of the p->u.zToken string (if any).
   75028                 :       */
   75029          362360 :       const unsigned nStructSize = dupedExprStructSize(p, flags);
   75030          362360 :       const int nNewSize = nStructSize & 0xfff;
   75031                 :       int nToken;
   75032          362360 :       if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
   75033          188014 :         nToken = sqlite3Strlen30(p->u.zToken) + 1;
   75034                 :       }else{
   75035          174346 :         nToken = 0;
   75036                 :       }
   75037          362360 :       if( isReduced ){
   75038          248679 :         assert( ExprHasProperty(p, EP_Reduced)==0 );
   75039          248679 :         memcpy(zAlloc, p, nNewSize);
   75040                 :       }else{
   75041          113681 :         int nSize = exprStructSize(p);
   75042          113681 :         memcpy(zAlloc, p, nSize);
   75043          113681 :         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
   75044                 :       }
   75045                 : 
   75046                 :       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
   75047          362360 :       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
   75048          362360 :       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
   75049          362360 :       pNew->flags |= staticFlag;
   75050                 : 
   75051                 :       /* Copy the p->u.zToken string, if any. */
   75052          362360 :       if( nToken ){
   75053          188014 :         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
   75054          188014 :         memcpy(zToken, p->u.zToken, nToken);
   75055                 :       }
   75056                 : 
   75057          362360 :       if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
   75058                 :         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
   75059          185384 :         if( ExprHasProperty(p, EP_xIsSelect) ){
   75060           11061 :           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
   75061                 :         }else{
   75062          174323 :           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
   75063                 :         }
   75064                 :       }
   75065                 : 
   75066                 :       /* Fill in pNew->pLeft and pNew->pRight. */
   75067          362360 :       if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
   75068          248679 :         zAlloc += dupedExprNodeSize(p, flags);
   75069          248679 :         if( ExprHasProperty(pNew, EP_Reduced) ){
   75070          111118 :           pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
   75071          111118 :           pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
   75072                 :         }
   75073          248679 :         if( pzBuffer ){
   75074          153476 :           *pzBuffer = zAlloc;
   75075                 :         }
   75076                 :       }else{
   75077          113681 :         pNew->flags2 = 0;
   75078          113681 :         if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
   75079           74266 :           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
   75080           74266 :           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
   75081                 :         }
   75082                 :       }
   75083                 : 
   75084                 :     }
   75085                 :   }
   75086          563846 :   return pNew;
   75087                 : }
   75088                 : 
   75089                 : /*
   75090                 : ** The following group of routines make deep copies of expressions,
   75091                 : ** expression lists, ID lists, and select statements.  The copies can
   75092                 : ** be deleted (by being passed to their respective ...Delete() routines)
   75093                 : ** without effecting the originals.
   75094                 : **
   75095                 : ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
   75096                 : ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
   75097                 : ** by subsequent calls to sqlite*ListAppend() routines.
   75098                 : **
   75099                 : ** Any tables that the SrcList might point to are not duplicated.
   75100                 : **
   75101                 : ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
   75102                 : ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
   75103                 : ** truncated version of the usual Expr structure that will be stored as
   75104                 : ** part of the in-memory representation of the database schema.
   75105                 : */
   75106          341610 : SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
   75107          341610 :   return exprDup(db, p, flags, 0);
   75108                 : }
   75109          217953 : SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
   75110                 :   ExprList *pNew;
   75111                 :   struct ExprList_item *pItem, *pOldItem;
   75112                 :   int i;
   75113          217953 :   if( p==0 ) return 0;
   75114           53332 :   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
   75115           53332 :   if( pNew==0 ) return 0;
   75116           53332 :   pNew->iECursor = 0;
   75117           53332 :   pNew->nExpr = pNew->nAlloc = p->nExpr;
   75118           53332 :   pNew->a = pItem = sqlite3DbMallocRaw(db,  p->nExpr*sizeof(p->a[0]) );
   75119           53332 :   if( pItem==0 ){
   75120               0 :     sqlite3DbFree(db, pNew);
   75121               0 :     return 0;
   75122                 :   } 
   75123           53332 :   pOldItem = p->a;
   75124          128935 :   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
   75125           75603 :     Expr *pOldExpr = pOldItem->pExpr;
   75126           75603 :     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
   75127           75603 :     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
   75128           75603 :     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
   75129           75603 :     pItem->sortOrder = pOldItem->sortOrder;
   75130           75603 :     pItem->done = 0;
   75131           75603 :     pItem->iOrderByCol = pOldItem->iOrderByCol;
   75132           75603 :     pItem->iAlias = pOldItem->iAlias;
   75133                 :   }
   75134           53332 :   return pNew;
   75135                 : }
   75136                 : 
   75137                 : /*
   75138                 : ** If cursors, triggers, views and subqueries are all omitted from
   75139                 : ** the build, then none of the following routines, except for 
   75140                 : ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
   75141                 : ** called with a NULL argument.
   75142                 : */
   75143                 : #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
   75144                 :  || !defined(SQLITE_OMIT_SUBQUERY)
   75145           12113 : SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
   75146                 :   SrcList *pNew;
   75147                 :   int i;
   75148                 :   int nByte;
   75149           12113 :   if( p==0 ) return 0;
   75150           12059 :   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
   75151           12059 :   pNew = sqlite3DbMallocRaw(db, nByte );
   75152           12059 :   if( pNew==0 ) return 0;
   75153           12059 :   pNew->nSrc = pNew->nAlloc = p->nSrc;
   75154           21617 :   for(i=0; i<p->nSrc; i++){
   75155            9558 :     struct SrcList_item *pNewItem = &pNew->a[i];
   75156            9558 :     struct SrcList_item *pOldItem = &p->a[i];
   75157                 :     Table *pTab;
   75158            9558 :     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
   75159            9558 :     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
   75160            9558 :     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
   75161            9558 :     pNewItem->jointype = pOldItem->jointype;
   75162            9558 :     pNewItem->iCursor = pOldItem->iCursor;
   75163            9558 :     pNewItem->addrFillSub = pOldItem->addrFillSub;
   75164            9558 :     pNewItem->regReturn = pOldItem->regReturn;
   75165            9558 :     pNewItem->isCorrelated = pOldItem->isCorrelated;
   75166            9558 :     pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
   75167            9558 :     pNewItem->notIndexed = pOldItem->notIndexed;
   75168            9558 :     pNewItem->pIndex = pOldItem->pIndex;
   75169            9558 :     pTab = pNewItem->pTab = pOldItem->pTab;
   75170            9558 :     if( pTab ){
   75171             465 :       pTab->nRef++;
   75172                 :     }
   75173            9558 :     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
   75174            9558 :     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
   75175            9558 :     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
   75176            9558 :     pNewItem->colUsed = pOldItem->colUsed;
   75177                 :   }
   75178           12059 :   return pNew;
   75179                 : }
   75180           21194 : SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
   75181                 :   IdList *pNew;
   75182                 :   int i;
   75183           21194 :   if( p==0 ) return 0;
   75184            2598 :   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
   75185            2598 :   if( pNew==0 ) return 0;
   75186            2598 :   pNew->nId = pNew->nAlloc = p->nId;
   75187            2598 :   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
   75188            2598 :   if( pNew->a==0 ){
   75189               0 :     sqlite3DbFree(db, pNew);
   75190               0 :     return 0;
   75191                 :   }
   75192            6120 :   for(i=0; i<p->nId; i++){
   75193            3522 :     struct IdList_item *pNewItem = &pNew->a[i];
   75194            3522 :     struct IdList_item *pOldItem = &p->a[i];
   75195            3522 :     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
   75196            3522 :     pNewItem->idx = pOldItem->idx;
   75197                 :   }
   75198            2598 :   return pNew;
   75199                 : }
   75200           34772 : SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
   75201                 :   Select *pNew, *pPrior;
   75202           34772 :   if( p==0 ) return 0;
   75203           12113 :   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
   75204           12113 :   if( pNew==0 ) return 0;
   75205           12113 :   pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
   75206           12113 :   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
   75207           12113 :   pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
   75208           12113 :   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
   75209           12113 :   pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
   75210           12113 :   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
   75211           12113 :   pNew->op = p->op;
   75212           12113 :   pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
   75213           12113 :   if( pPrior ) pPrior->pNext = pNew;
   75214           12113 :   pNew->pNext = 0;
   75215           12113 :   pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
   75216           12113 :   pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
   75217           12113 :   pNew->iLimit = 0;
   75218           12113 :   pNew->iOffset = 0;
   75219           12113 :   pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
   75220           12113 :   pNew->pRightmost = 0;
   75221           12113 :   pNew->addrOpenEphm[0] = -1;
   75222           12113 :   pNew->addrOpenEphm[1] = -1;
   75223           12113 :   pNew->addrOpenEphm[2] = -1;
   75224           12113 :   return pNew;
   75225                 : }
   75226                 : #else
   75227                 : SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
   75228                 :   assert( p==0 );
   75229                 :   return 0;
   75230                 : }
   75231                 : #endif
   75232                 : 
   75233                 : 
   75234                 : /*
   75235                 : ** Add a new element to the end of an expression list.  If pList is
   75236                 : ** initially NULL, then create a new expression list.
   75237                 : **
   75238                 : ** If a memory allocation error occurs, the entire list is freed and
   75239                 : ** NULL is returned.  If non-NULL is returned, then it is guaranteed
   75240                 : ** that the new entry was successfully appended.
   75241                 : */
   75242          616567 : SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
   75243                 :   Parse *pParse,          /* Parsing context */
   75244                 :   ExprList *pList,        /* List to which to append. Might be NULL */
   75245                 :   Expr *pExpr             /* Expression to be appended. Might be NULL */
   75246                 : ){
   75247          616567 :   sqlite3 *db = pParse->db;
   75248          616567 :   if( pList==0 ){
   75249          206488 :     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
   75250          206488 :     if( pList==0 ){
   75251               0 :       goto no_mem;
   75252                 :     }
   75253          206488 :     assert( pList->nAlloc==0 );
   75254                 :   }
   75255          616567 :   if( pList->nAlloc<=pList->nExpr ){
   75256                 :     struct ExprList_item *a;
   75257          222075 :     int n = pList->nAlloc*2 + 4;
   75258          222075 :     a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
   75259          222075 :     if( a==0 ){
   75260               0 :       goto no_mem;
   75261                 :     }
   75262          222075 :     pList->a = a;
   75263          222075 :     pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
   75264                 :   }
   75265          616567 :   assert( pList->a!=0 );
   75266                 :   if( 1 ){
   75267          616567 :     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
   75268          616567 :     memset(pItem, 0, sizeof(*pItem));
   75269          616567 :     pItem->pExpr = pExpr;
   75270                 :   }
   75271          616567 :   return pList;
   75272                 : 
   75273                 : no_mem:     
   75274                 :   /* Avoid leaking memory if malloc has failed. */
   75275               0 :   sqlite3ExprDelete(db, pExpr);
   75276               0 :   sqlite3ExprListDelete(db, pList);
   75277               0 :   return 0;
   75278                 : }
   75279                 : 
   75280                 : /*
   75281                 : ** Set the ExprList.a[].zName element of the most recently added item
   75282                 : ** on the expression list.
   75283                 : **
   75284                 : ** pList might be NULL following an OOM error.  But pName should never be
   75285                 : ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
   75286                 : ** is set.
   75287                 : */
   75288          130291 : SQLITE_PRIVATE void sqlite3ExprListSetName(
   75289                 :   Parse *pParse,          /* Parsing context */
   75290                 :   ExprList *pList,        /* List to which to add the span. */
   75291                 :   Token *pName,           /* Name to be added */
   75292                 :   int dequote             /* True to cause the name to be dequoted */
   75293                 : ){
   75294          130291 :   assert( pList!=0 || pParse->db->mallocFailed!=0 );
   75295          130291 :   if( pList ){
   75296                 :     struct ExprList_item *pItem;
   75297          130291 :     assert( pList->nExpr>0 );
   75298          130291 :     pItem = &pList->a[pList->nExpr-1];
   75299          130291 :     assert( pItem->zName==0 );
   75300          130291 :     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
   75301          130291 :     if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
   75302                 :   }
   75303          130291 : }
   75304                 : 
   75305                 : /*
   75306                 : ** Set the ExprList.a[].zSpan element of the most recently added item
   75307                 : ** on the expression list.
   75308                 : **
   75309                 : ** pList might be NULL following an OOM error.  But pSpan should never be
   75310                 : ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
   75311                 : ** is set.
   75312                 : */
   75313          287406 : SQLITE_PRIVATE void sqlite3ExprListSetSpan(
   75314                 :   Parse *pParse,          /* Parsing context */
   75315                 :   ExprList *pList,        /* List to which to add the span. */
   75316                 :   ExprSpan *pSpan         /* The span to be added */
   75317                 : ){
   75318          287406 :   sqlite3 *db = pParse->db;
   75319          287406 :   assert( pList!=0 || db->mallocFailed!=0 );
   75320          287406 :   if( pList ){
   75321          287406 :     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
   75322          287406 :     assert( pList->nExpr>0 );
   75323          287406 :     assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
   75324          287406 :     sqlite3DbFree(db, pItem->zSpan);
   75325          574812 :     pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
   75326          574812 :                                     (int)(pSpan->zEnd - pSpan->zStart));
   75327                 :   }
   75328          287406 : }
   75329                 : 
   75330                 : /*
   75331                 : ** If the expression list pEList contains more than iLimit elements,
   75332                 : ** leave an error message in pParse.
   75333                 : */
   75334           68470 : SQLITE_PRIVATE void sqlite3ExprListCheckLength(
   75335                 :   Parse *pParse,
   75336                 :   ExprList *pEList,
   75337                 :   const char *zObject
   75338                 : ){
   75339           68470 :   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
   75340                 :   testcase( pEList && pEList->nExpr==mx );
   75341                 :   testcase( pEList && pEList->nExpr==mx+1 );
   75342           68470 :   if( pEList && pEList->nExpr>mx ){
   75343               0 :     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
   75344                 :   }
   75345           68470 : }
   75346                 : 
   75347                 : /*
   75348                 : ** Delete an entire expression list.
   75349                 : */
   75350         1706448 : SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
   75351                 :   int i;
   75352                 :   struct ExprList_item *pItem;
   75353         1706448 :   if( pList==0 ) return;
   75354          259823 :   assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
   75355          259823 :   assert( pList->nExpr<=pList->nAlloc );
   75356          951996 :   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
   75357          692173 :     sqlite3ExprDelete(db, pItem->pExpr);
   75358          692173 :     sqlite3DbFree(db, pItem->zName);
   75359          692173 :     sqlite3DbFree(db, pItem->zSpan);
   75360                 :   }
   75361          259823 :   sqlite3DbFree(db, pList->a);
   75362          259823 :   sqlite3DbFree(db, pList);
   75363                 : }
   75364                 : 
   75365                 : /*
   75366                 : ** These routines are Walker callbacks.  Walker.u.pi is a pointer
   75367                 : ** to an integer.  These routines are checking an expression to see
   75368                 : ** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
   75369                 : ** not constant.
   75370                 : **
   75371                 : ** These callback routines are used to implement the following:
   75372                 : **
   75373                 : **     sqlite3ExprIsConstant()
   75374                 : **     sqlite3ExprIsConstantNotJoin()
   75375                 : **     sqlite3ExprIsConstantOrFunction()
   75376                 : **
   75377                 : */
   75378          537357 : static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
   75379                 : 
   75380                 :   /* If pWalker->u.i is 3 then any term of the expression that comes from
   75381                 :   ** the ON or USING clauses of a join disqualifies the expression
   75382                 :   ** from being considered constant. */
   75383          537357 :   if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
   75384            4967 :     pWalker->u.i = 0;
   75385            4967 :     return WRC_Abort;
   75386                 :   }
   75387                 : 
   75388          532390 :   switch( pExpr->op ){
   75389                 :     /* Consider functions to be constant if all their arguments are constant
   75390                 :     ** and pWalker->u.i==2 */
   75391                 :     case TK_FUNCTION:
   75392            9698 :       if( pWalker->u.i==2 ) return 0;
   75393                 :       /* Fall through */
   75394                 :     case TK_ID:
   75395                 :     case TK_COLUMN:
   75396                 :     case TK_AGG_FUNCTION:
   75397                 :     case TK_AGG_COLUMN:
   75398                 :       testcase( pExpr->op==TK_ID );
   75399                 :       testcase( pExpr->op==TK_COLUMN );
   75400                 :       testcase( pExpr->op==TK_AGG_FUNCTION );
   75401                 :       testcase( pExpr->op==TK_AGG_COLUMN );
   75402          227930 :       pWalker->u.i = 0;
   75403          227930 :       return WRC_Abort;
   75404                 :     default:
   75405                 :       testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
   75406                 :       testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
   75407          304460 :       return WRC_Continue;
   75408                 :   }
   75409                 : }
   75410            2922 : static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
   75411                 :   UNUSED_PARAMETER(NotUsed);
   75412            2922 :   pWalker->u.i = 0;
   75413            2922 :   return WRC_Abort;
   75414                 : }
   75415          324561 : static int exprIsConst(Expr *p, int initFlag){
   75416                 :   Walker w;
   75417          324561 :   w.u.i = initFlag;
   75418          324561 :   w.xExprCallback = exprNodeIsConstant;
   75419          324561 :   w.xSelectCallback = selectNodeIsConstant;
   75420          324561 :   sqlite3WalkExpr(&w, p);
   75421          324561 :   return w.u.i;
   75422                 : }
   75423                 : 
   75424                 : /*
   75425                 : ** Walk an expression tree.  Return 1 if the expression is constant
   75426                 : ** and 0 if it involves variables or function calls.
   75427                 : **
   75428                 : ** For the purposes of this function, a double-quoted string (ex: "abc")
   75429                 : ** is considered a variable but a single-quoted string (ex: 'abc') is
   75430                 : ** a constant.
   75431                 : */
   75432           23249 : SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
   75433           23249 :   return exprIsConst(p, 1);
   75434                 : }
   75435                 : 
   75436                 : /*
   75437                 : ** Walk an expression tree.  Return 1 if the expression is constant
   75438                 : ** that does no originate from the ON or USING clauses of a join.
   75439                 : ** Return 0 if it involves variables or function calls or terms from
   75440                 : ** an ON or USING clause.
   75441                 : */
   75442          283550 : SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
   75443          283550 :   return exprIsConst(p, 3);
   75444                 : }
   75445                 : 
   75446                 : /*
   75447                 : ** Walk an expression tree.  Return 1 if the expression is constant
   75448                 : ** or a function call with constant arguments.  Return and 0 if there
   75449                 : ** are any variables.
   75450                 : **
   75451                 : ** For the purposes of this function, a double-quoted string (ex: "abc")
   75452                 : ** is considered a variable but a single-quoted string (ex: 'abc') is
   75453                 : ** a constant.
   75454                 : */
   75455           17762 : SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
   75456           17762 :   return exprIsConst(p, 2);
   75457                 : }
   75458                 : 
   75459                 : /*
   75460                 : ** If the expression p codes a constant integer that is small enough
   75461                 : ** to fit in a 32-bit integer, return 1 and put the value of the integer
   75462                 : ** in *pValue.  If the expression is not an integer or if it is too big
   75463                 : ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
   75464                 : */
   75465           35543 : SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
   75466           35543 :   int rc = 0;
   75467                 : 
   75468                 :   /* If an expression is an integer literal that fits in a signed 32-bit
   75469                 :   ** integer, then the EP_IntValue flag will have already been set */
   75470           35543 :   assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
   75471                 :            || sqlite3GetInt32(p->u.zToken, &rc)==0 );
   75472                 : 
   75473           35543 :   if( p->flags & EP_IntValue ){
   75474           12143 :     *pValue = p->u.iValue;
   75475           12143 :     return 1;
   75476                 :   }
   75477           23400 :   switch( p->op ){
   75478                 :     case TK_UPLUS: {
   75479               0 :       rc = sqlite3ExprIsInteger(p->pLeft, pValue);
   75480               0 :       break;
   75481                 :     }
   75482                 :     case TK_UMINUS: {
   75483                 :       int v;
   75484               0 :       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
   75485               0 :         *pValue = -v;
   75486               0 :         rc = 1;
   75487                 :       }
   75488               0 :       break;
   75489                 :     }
   75490           23400 :     default: break;
   75491                 :   }
   75492           23400 :   return rc;
   75493                 : }
   75494                 : 
   75495                 : /*
   75496                 : ** Return FALSE if there is no chance that the expression can be NULL.
   75497                 : **
   75498                 : ** If the expression might be NULL or if the expression is too complex
   75499                 : ** to tell return TRUE.  
   75500                 : **
   75501                 : ** This routine is used as an optimization, to skip OP_IsNull opcodes
   75502                 : ** when we know that a value cannot be NULL.  Hence, a false positive
   75503                 : ** (returning TRUE when in fact the expression can never be NULL) might
   75504                 : ** be a small performance hit but is otherwise harmless.  On the other
   75505                 : ** hand, a false negative (returning FALSE when the result could be NULL)
   75506                 : ** will likely result in an incorrect answer.  So when in doubt, return
   75507                 : ** TRUE.
   75508                 : */
   75509           26009 : SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
   75510                 :   u8 op;
   75511           26009 :   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
   75512           26009 :   op = p->op;
   75513           26009 :   if( op==TK_REGISTER ) op = p->op2;
   75514           26009 :   switch( op ){
   75515                 :     case TK_INTEGER:
   75516                 :     case TK_STRING:
   75517                 :     case TK_FLOAT:
   75518                 :     case TK_BLOB:
   75519            1707 :       return 0;
   75520                 :     default:
   75521           24302 :       return 1;
   75522                 :   }
   75523                 : }
   75524                 : 
   75525                 : /*
   75526                 : ** Generate an OP_IsNull instruction that tests register iReg and jumps
   75527                 : ** to location iDest if the value in iReg is NULL.  The value in iReg 
   75528                 : ** was computed by pExpr.  If we can look at pExpr at compile-time and
   75529                 : ** determine that it can never generate a NULL, then the OP_IsNull operation
   75530                 : ** can be omitted.
   75531                 : */
   75532           26009 : SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
   75533                 :   Vdbe *v,            /* The VDBE under construction */
   75534                 :   const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
   75535                 :   int iReg,           /* Test the value in this register for NULL */
   75536                 :   int iDest           /* Jump here if the value is null */
   75537                 : ){
   75538           26009 :   if( sqlite3ExprCanBeNull(pExpr) ){
   75539           24302 :     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
   75540                 :   }
   75541           26009 : }
   75542                 : 
   75543                 : /*
   75544                 : ** Return TRUE if the given expression is a constant which would be
   75545                 : ** unchanged by OP_Affinity with the affinity given in the second
   75546                 : ** argument.
   75547                 : **
   75548                 : ** This routine is used to determine if the OP_Affinity operation
   75549                 : ** can be omitted.  When in doubt return FALSE.  A false negative
   75550                 : ** is harmless.  A false positive, however, can result in the wrong
   75551                 : ** answer.
   75552                 : */
   75553           26009 : SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
   75554                 :   u8 op;
   75555           26009 :   if( aff==SQLITE_AFF_NONE ) return 1;
   75556           24922 :   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
   75557           24922 :   op = p->op;
   75558           24922 :   if( op==TK_REGISTER ) op = p->op2;
   75559           24922 :   switch( op ){
   75560                 :     case TK_INTEGER: {
   75561            1339 :       return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
   75562                 :     }
   75563                 :     case TK_FLOAT: {
   75564               0 :       return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
   75565                 :     }
   75566                 :     case TK_STRING: {
   75567             368 :       return aff==SQLITE_AFF_TEXT;
   75568                 :     }
   75569                 :     case TK_BLOB: {
   75570               0 :       return 1;
   75571                 :     }
   75572                 :     case TK_COLUMN: {
   75573            1663 :       assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
   75574            3234 :       return p->iColumn<0
   75575            1571 :           && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
   75576                 :     }
   75577                 :     default: {
   75578           21552 :       return 0;
   75579                 :     }
   75580                 :   }
   75581                 : }
   75582                 : 
   75583                 : /*
   75584                 : ** Return TRUE if the given string is a row-id column name.
   75585                 : */
   75586           27696 : SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
   75587           27696 :   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
   75588           27696 :   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
   75589            1203 :   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
   75590            1049 :   return 0;
   75591                 : }
   75592                 : 
   75593                 : /*
   75594                 : ** Return true if we are able to the IN operator optimization on a
   75595                 : ** query of the form
   75596                 : **
   75597                 : **       x IN (SELECT ...)
   75598                 : **
   75599                 : ** Where the SELECT... clause is as specified by the parameter to this
   75600                 : ** routine.
   75601                 : **
   75602                 : ** The Select object passed in has already been preprocessed and no
   75603                 : ** errors have been found.
   75604                 : */
   75605                 : #ifndef SQLITE_OMIT_SUBQUERY
   75606            4224 : static int isCandidateForInOpt(Select *p){
   75607                 :   SrcList *pSrc;
   75608                 :   ExprList *pEList;
   75609                 :   Table *pTab;
   75610            4224 :   if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
   75611            2401 :   if( p->pPrior ) return 0;              /* Not a compound SELECT */
   75612            2401 :   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
   75613                 :     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
   75614                 :     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
   75615              82 :     return 0; /* No DISTINCT keyword and no aggregate functions */
   75616                 :   }
   75617            2319 :   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
   75618            2319 :   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
   75619            2057 :   assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
   75620            2057 :   if( p->pWhere ) return 0;              /* Has no WHERE clause */
   75621             520 :   pSrc = p->pSrc;
   75622             520 :   assert( pSrc!=0 );
   75623             520 :   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
   75624             520 :   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
   75625             520 :   pTab = pSrc->a[0].pTab;
   75626             520 :   if( NEVER(pTab==0) ) return 0;
   75627             520 :   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
   75628             520 :   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
   75629             520 :   pEList = p->pEList;
   75630             520 :   if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
   75631             520 :   if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
   75632             520 :   return 1;
   75633                 : }
   75634                 : #endif /* SQLITE_OMIT_SUBQUERY */
   75635                 : 
   75636                 : /*
   75637                 : ** Code an OP_Once instruction and allocate space for its flag. Return the 
   75638                 : ** address of the new instruction.
   75639                 : */
   75640           13660 : SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
   75641           13660 :   Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
   75642           13660 :   return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
   75643                 : }
   75644                 : 
   75645                 : /*
   75646                 : ** This function is used by the implementation of the IN (...) operator.
   75647                 : ** It's job is to find or create a b-tree structure that may be used
   75648                 : ** either to test for membership of the (...) set or to iterate through
   75649                 : ** its members, skipping duplicates.
   75650                 : **
   75651                 : ** The index of the cursor opened on the b-tree (database table, database index 
   75652                 : ** or ephermal table) is stored in pX->iTable before this function returns.
   75653                 : ** The returned value of this function indicates the b-tree type, as follows:
   75654                 : **
   75655                 : **   IN_INDEX_ROWID - The cursor was opened on a database table.
   75656                 : **   IN_INDEX_INDEX - The cursor was opened on a database index.
   75657                 : **   IN_INDEX_EPH -   The cursor was opened on a specially created and
   75658                 : **                    populated epheremal table.
   75659                 : **
   75660                 : ** An existing b-tree may only be used if the SELECT is of the simple
   75661                 : ** form:
   75662                 : **
   75663                 : **     SELECT <column> FROM <table>
   75664                 : **
   75665                 : ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
   75666                 : ** through the set members, skipping any duplicates. In this case an
   75667                 : ** epheremal table must be used unless the selected <column> is guaranteed
   75668                 : ** to be unique - either because it is an INTEGER PRIMARY KEY or it
   75669                 : ** has a UNIQUE constraint or UNIQUE index.
   75670                 : **
   75671                 : ** If the prNotFound parameter is not 0, then the b-tree will be used 
   75672                 : ** for fast set membership tests. In this case an epheremal table must 
   75673                 : ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
   75674                 : ** be found with <column> as its left-most column.
   75675                 : **
   75676                 : ** When the b-tree is being used for membership tests, the calling function
   75677                 : ** needs to know whether or not the structure contains an SQL NULL 
   75678                 : ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
   75679                 : ** If there is any chance that the (...) might contain a NULL value at
   75680                 : ** runtime, then a register is allocated and the register number written
   75681                 : ** to *prNotFound. If there is no chance that the (...) contains a
   75682                 : ** NULL value, then *prNotFound is left unchanged.
   75683                 : **
   75684                 : ** If a register is allocated and its location stored in *prNotFound, then
   75685                 : ** its initial value is NULL.  If the (...) does not remain constant
   75686                 : ** for the duration of the query (i.e. the SELECT within the (...)
   75687                 : ** is a correlated subquery) then the value of the allocated register is
   75688                 : ** reset to NULL each time the subquery is rerun. This allows the
   75689                 : ** caller to use vdbe code equivalent to the following:
   75690                 : **
   75691                 : **   if( register==NULL ){
   75692                 : **     has_null = <test if data structure contains null>
   75693                 : **     register = 1
   75694                 : **   }
   75695                 : **
   75696                 : ** in order to avoid running the <test if data structure contains null>
   75697                 : ** test more often than is necessary.
   75698                 : */
   75699                 : #ifndef SQLITE_OMIT_SUBQUERY
   75700            4224 : SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
   75701                 :   Select *p;                            /* SELECT to the right of IN operator */
   75702            4224 :   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
   75703            4224 :   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
   75704            4224 :   int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
   75705            4224 :   Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
   75706                 : 
   75707            4224 :   assert( pX->op==TK_IN );
   75708                 : 
   75709                 :   /* Check to see if an existing table or index can be used to
   75710                 :   ** satisfy the query.  This is preferable to generating a new 
   75711                 :   ** ephemeral table.
   75712                 :   */
   75713            4224 :   p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
   75714            4224 :   if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
   75715             520 :     sqlite3 *db = pParse->db;              /* Database connection */
   75716                 :     Table *pTab;                           /* Table <table>. */
   75717                 :     Expr *pExpr;                           /* Expression <column> */
   75718                 :     int iCol;                              /* Index of column <column> */
   75719                 :     int iDb;                               /* Database idx for pTab */
   75720                 : 
   75721             520 :     assert( p );                        /* Because of isCandidateForInOpt(p) */
   75722             520 :     assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
   75723             520 :     assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
   75724             520 :     assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
   75725             520 :     pTab = p->pSrc->a[0].pTab;
   75726             520 :     pExpr = p->pEList->a[0].pExpr;
   75727             520 :     iCol = pExpr->iColumn;
   75728                 :    
   75729                 :     /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
   75730             520 :     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   75731             520 :     sqlite3CodeVerifySchema(pParse, iDb);
   75732             520 :     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
   75733                 : 
   75734                 :     /* This function is only called from two places. In both cases the vdbe
   75735                 :     ** has already been allocated. So assume sqlite3GetVdbe() is always
   75736                 :     ** successful here.
   75737                 :     */
   75738             520 :     assert(v);
   75739             520 :     if( iCol<0 ){
   75740                 :       int iAddr;
   75741                 : 
   75742              52 :       iAddr = sqlite3CodeOnce(pParse);
   75743                 : 
   75744              52 :       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
   75745              52 :       eType = IN_INDEX_ROWID;
   75746                 : 
   75747              52 :       sqlite3VdbeJumpHere(v, iAddr);
   75748                 :     }else{
   75749                 :       Index *pIdx;                         /* Iterator variable */
   75750                 : 
   75751                 :       /* The collation sequence used by the comparison. If an index is to
   75752                 :       ** be used in place of a temp-table, it must be ordered according
   75753                 :       ** to this collation sequence.  */
   75754             468 :       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
   75755                 : 
   75756                 :       /* Check that the affinity that will be used to perform the 
   75757                 :       ** comparison is the same as the affinity of the column. If
   75758                 :       ** it is not, it is not possible to use any index.
   75759                 :       */
   75760             468 :       char aff = comparisonAffinity(pX);
   75761             468 :       int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
   75762                 : 
   75763             468 :       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
   75764               0 :         if( (pIdx->aiColumn[0]==iCol)
   75765               0 :          && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
   75766               0 :          && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
   75767                 :         ){
   75768                 :           int iAddr;
   75769                 :           char *pKey;
   75770                 :   
   75771               0 :           pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
   75772               0 :           iAddr = sqlite3CodeOnce(pParse);
   75773                 :   
   75774               0 :           sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
   75775                 :                                pKey,P4_KEYINFO_HANDOFF);
   75776               0 :           VdbeComment((v, "%s", pIdx->zName));
   75777               0 :           eType = IN_INDEX_INDEX;
   75778                 : 
   75779               0 :           sqlite3VdbeJumpHere(v, iAddr);
   75780               0 :           if( prNotFound && !pTab->aCol[iCol].notNull ){
   75781               0 :             *prNotFound = ++pParse->nMem;
   75782               0 :             sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
   75783                 :           }
   75784                 :         }
   75785                 :       }
   75786                 :     }
   75787                 :   }
   75788                 : 
   75789            4224 :   if( eType==0 ){
   75790                 :     /* Could not found an existing table or index to use as the RHS b-tree.
   75791                 :     ** We will have to generate an ephemeral table to do the job.
   75792                 :     */
   75793            4172 :     double savedNQueryLoop = pParse->nQueryLoop;
   75794            4172 :     int rMayHaveNull = 0;
   75795            4172 :     eType = IN_INDEX_EPH;
   75796            4172 :     if( prNotFound ){
   75797            1403 :       *prNotFound = rMayHaveNull = ++pParse->nMem;
   75798            1403 :       sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
   75799                 :     }else{
   75800                 :       testcase( pParse->nQueryLoop>(double)1 );
   75801            2769 :       pParse->nQueryLoop = (double)1;
   75802            2769 :       if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
   75803              99 :         eType = IN_INDEX_ROWID;
   75804                 :       }
   75805                 :     }
   75806            4172 :     sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
   75807            4172 :     pParse->nQueryLoop = savedNQueryLoop;
   75808                 :   }else{
   75809              52 :     pX->iTable = iTab;
   75810                 :   }
   75811            4224 :   return eType;
   75812                 : }
   75813                 : #endif
   75814                 : 
   75815                 : /*
   75816                 : ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
   75817                 : ** or IN operators.  Examples:
   75818                 : **
   75819                 : **     (SELECT a FROM b)          -- subquery
   75820                 : **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
   75821                 : **     x IN (4,5,11)              -- IN operator with list on right-hand side
   75822                 : **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
   75823                 : **
   75824                 : ** The pExpr parameter describes the expression that contains the IN
   75825                 : ** operator or subquery.
   75826                 : **
   75827                 : ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
   75828                 : ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
   75829                 : ** to some integer key column of a table B-Tree. In this case, use an
   75830                 : ** intkey B-Tree to store the set of IN(...) values instead of the usual
   75831                 : ** (slower) variable length keys B-Tree.
   75832                 : **
   75833                 : ** If rMayHaveNull is non-zero, that means that the operation is an IN
   75834                 : ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
   75835                 : ** Furthermore, the IN is in a WHERE clause and that we really want
   75836                 : ** to iterate over the RHS of the IN operator in order to quickly locate
   75837                 : ** all corresponding LHS elements.  All this routine does is initialize
   75838                 : ** the register given by rMayHaveNull to NULL.  Calling routines will take
   75839                 : ** care of changing this register value to non-NULL if the RHS is NULL-free.
   75840                 : **
   75841                 : ** If rMayHaveNull is zero, that means that the subquery is being used
   75842                 : ** for membership testing only.  There is no need to initialize any
   75843                 : ** registers to indicate the presense or absence of NULLs on the RHS.
   75844                 : **
   75845                 : ** For a SELECT or EXISTS operator, return the register that holds the
   75846                 : ** result.  For IN operators or if an error occurs, the return value is 0.
   75847                 : */
   75848                 : #ifndef SQLITE_OMIT_SUBQUERY
   75849           15336 : SQLITE_PRIVATE int sqlite3CodeSubselect(
   75850                 :   Parse *pParse,          /* Parsing context */
   75851                 :   Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
   75852                 :   int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
   75853                 :   int isRowid             /* If true, LHS of IN operator is a rowid */
   75854                 : ){
   75855           15336 :   int testAddr = -1;                      /* One-time test address */
   75856           15336 :   int rReg = 0;                           /* Register storing resulting */
   75857           15336 :   Vdbe *v = sqlite3GetVdbe(pParse);
   75858           15336 :   if( NEVER(v==0) ) return 0;
   75859           15336 :   sqlite3ExprCachePush(pParse);
   75860                 : 
   75861                 :   /* This code must be run in its entirety every time it is encountered
   75862                 :   ** if any of the following is true:
   75863                 :   **
   75864                 :   **    *  The right-hand side is a correlated subquery
   75865                 :   **    *  The right-hand side is an expression list containing variables
   75866                 :   **    *  We are inside a trigger
   75867                 :   **
   75868                 :   ** If all of the above are false, then we can run this code just once
   75869                 :   ** save the results, and reuse the same result on subsequent invocations.
   75870                 :   */
   75871           15336 :   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) ){
   75872           13342 :     testAddr = sqlite3CodeOnce(pParse);
   75873                 :   }
   75874                 : 
   75875                 : #ifndef SQLITE_OMIT_EXPLAIN
   75876           15336 :   if( pParse->explain==2 ){
   75877               0 :     char *zMsg = sqlite3MPrintf(
   75878                 :         pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
   75879               0 :         pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
   75880                 :     );
   75881               0 :     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
   75882                 :   }
   75883                 : #endif
   75884                 : 
   75885           15336 :   switch( pExpr->op ){
   75886                 :     case TK_IN: {
   75887                 :       char affinity;              /* Affinity of the LHS of the IN */
   75888                 :       KeyInfo keyInfo;            /* Keyinfo for the generated table */
   75889                 :       int addr;                   /* Address of OP_OpenEphemeral instruction */
   75890            4172 :       Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
   75891                 : 
   75892            4172 :       if( rMayHaveNull ){
   75893            1403 :         sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
   75894                 :       }
   75895                 : 
   75896            4172 :       affinity = sqlite3ExprAffinity(pLeft);
   75897                 : 
   75898                 :       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
   75899                 :       ** expression it is handled the same way.  An ephemeral table is 
   75900                 :       ** filled with single-field index keys representing the results
   75901                 :       ** from the SELECT or the <exprlist>.
   75902                 :       **
   75903                 :       ** If the 'x' expression is a column value, or the SELECT...
   75904                 :       ** statement returns a column value, then the affinity of that
   75905                 :       ** column is used to build the index keys. If both 'x' and the
   75906                 :       ** SELECT... statement are columns, then numeric affinity is used
   75907                 :       ** if either column has NUMERIC or INTEGER affinity. If neither
   75908                 :       ** 'x' nor the SELECT... statement are columns, then numeric affinity
   75909                 :       ** is used.
   75910                 :       */
   75911            4172 :       pExpr->iTable = pParse->nTab++;
   75912            4172 :       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
   75913            4172 :       if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
   75914            4172 :       memset(&keyInfo, 0, sizeof(keyInfo));
   75915            4172 :       keyInfo.nField = 1;
   75916                 : 
   75917            4172 :       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   75918                 :         /* Case 1:     expr IN (SELECT ...)
   75919                 :         **
   75920                 :         ** Generate code to write the results of the select into the temporary
   75921                 :         ** table allocated and opened above.
   75922                 :         */
   75923                 :         SelectDest dest;
   75924                 :         ExprList *pEList;
   75925                 : 
   75926            2349 :         assert( !isRowid );
   75927            2349 :         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
   75928            2349 :         dest.affinity = (u8)affinity;
   75929            2349 :         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
   75930            2349 :         pExpr->x.pSelect->iLimit = 0;
   75931            2349 :         if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
   75932               0 :           return 0;
   75933                 :         }
   75934            2349 :         pEList = pExpr->x.pSelect->pEList;
   75935            2349 :         if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ 
   75936            2349 :           keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
   75937            2349 :               pEList->a[0].pExpr);
   75938                 :         }
   75939            1823 :       }else if( ALWAYS(pExpr->x.pList!=0) ){
   75940                 :         /* Case 2:     expr IN (exprlist)
   75941                 :         **
   75942                 :         ** For each expression, build an index key from the evaluation and
   75943                 :         ** store it in the temporary table. If <expr> is a column, then use
   75944                 :         ** that columns affinity when building index keys. If <expr> is not
   75945                 :         ** a column, use numeric affinity.
   75946                 :         */
   75947                 :         int i;
   75948            1823 :         ExprList *pList = pExpr->x.pList;
   75949                 :         struct ExprList_item *pItem;
   75950                 :         int r1, r2, r3;
   75951                 : 
   75952            1823 :         if( !affinity ){
   75953             576 :           affinity = SQLITE_AFF_NONE;
   75954                 :         }
   75955            1823 :         keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
   75956                 : 
   75957                 :         /* Loop through each expression in <exprlist>. */
   75958            1823 :         r1 = sqlite3GetTempReg(pParse);
   75959            1823 :         r2 = sqlite3GetTempReg(pParse);
   75960            1823 :         sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
   75961            7122 :         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
   75962            5299 :           Expr *pE2 = pItem->pExpr;
   75963                 :           int iValToIns;
   75964                 : 
   75965                 :           /* If the expression is not constant then we will need to
   75966                 :           ** disable the test that was generated above that makes sure
   75967                 :           ** this code only executes once.  Because for a non-constant
   75968                 :           ** expression we need to rerun this code each time.
   75969                 :           */
   75970            5299 :           if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
   75971             468 :             sqlite3VdbeChangeToNoop(v, testAddr);
   75972             468 :             testAddr = -1;
   75973                 :           }
   75974                 : 
   75975                 :           /* Evaluate the expression and insert it into the temp table */
   75976            5299 :           if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
   75977             207 :             sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
   75978                 :           }else{
   75979            5092 :             r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
   75980            5092 :             if( isRowid ){
   75981               4 :               sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
   75982               4 :                                 sqlite3VdbeCurrentAddr(v)+2);
   75983               4 :               sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
   75984                 :             }else{
   75985            5088 :               sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
   75986            5088 :               sqlite3ExprCacheAffinityChange(pParse, r3, 1);
   75987            5088 :               sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
   75988                 :             }
   75989                 :           }
   75990                 :         }
   75991            1823 :         sqlite3ReleaseTempReg(pParse, r1);
   75992            1823 :         sqlite3ReleaseTempReg(pParse, r2);
   75993                 :       }
   75994            4172 :       if( !isRowid ){
   75995            4073 :         sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
   75996                 :       }
   75997            4172 :       break;
   75998                 :     }
   75999                 : 
   76000                 :     case TK_EXISTS:
   76001                 :     case TK_SELECT:
   76002                 :     default: {
   76003                 :       /* If this has to be a scalar SELECT.  Generate code to put the
   76004                 :       ** value of this select in a memory cell and record the number
   76005                 :       ** of the memory cell in iColumn.  If this is an EXISTS, write
   76006                 :       ** an integer 0 (not exists) or 1 (exists) into a memory cell
   76007                 :       ** and record that memory cell in iColumn.
   76008                 :       */
   76009                 :       Select *pSel;                         /* SELECT statement to encode */
   76010                 :       SelectDest dest;                      /* How to deal with SELECt result */
   76011                 : 
   76012                 :       testcase( pExpr->op==TK_EXISTS );
   76013                 :       testcase( pExpr->op==TK_SELECT );
   76014           11164 :       assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
   76015                 : 
   76016           11164 :       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
   76017           11164 :       pSel = pExpr->x.pSelect;
   76018           11164 :       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
   76019           11164 :       if( pExpr->op==TK_SELECT ){
   76020            9405 :         dest.eDest = SRT_Mem;
   76021            9405 :         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
   76022            9405 :         VdbeComment((v, "Init subquery result"));
   76023                 :       }else{
   76024            1759 :         dest.eDest = SRT_Exists;
   76025            1759 :         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
   76026            1759 :         VdbeComment((v, "Init EXISTS result"));
   76027                 :       }
   76028           11164 :       sqlite3ExprDelete(pParse->db, pSel->pLimit);
   76029           11164 :       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
   76030                 :                                   &sqlite3IntTokens[1]);
   76031           11164 :       pSel->iLimit = 0;
   76032           11164 :       if( sqlite3Select(pParse, pSel, &dest) ){
   76033               0 :         return 0;
   76034                 :       }
   76035           11164 :       rReg = dest.iParm;
   76036           11164 :       ExprSetIrreducible(pExpr);
   76037           11164 :       break;
   76038                 :     }
   76039                 :   }
   76040                 : 
   76041           15336 :   if( testAddr>=0 ){
   76042           12874 :     sqlite3VdbeJumpHere(v, testAddr);
   76043                 :   }
   76044           15336 :   sqlite3ExprCachePop(pParse, 1);
   76045                 : 
   76046           15336 :   return rReg;
   76047                 : }
   76048                 : #endif /* SQLITE_OMIT_SUBQUERY */
   76049                 : 
   76050                 : #ifndef SQLITE_OMIT_SUBQUERY
   76051                 : /*
   76052                 : ** Generate code for an IN expression.
   76053                 : **
   76054                 : **      x IN (SELECT ...)
   76055                 : **      x IN (value, value, ...)
   76056                 : **
   76057                 : ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
   76058                 : ** is an array of zero or more values.  The expression is true if the LHS is
   76059                 : ** contained within the RHS.  The value of the expression is unknown (NULL)
   76060                 : ** if the LHS is NULL or if the LHS is not contained within the RHS and the
   76061                 : ** RHS contains one or more NULL values.
   76062                 : **
   76063                 : ** This routine generates code will jump to destIfFalse if the LHS is not 
   76064                 : ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
   76065                 : ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
   76066                 : ** within the RHS then fall through.
   76067                 : */
   76068            1455 : static void sqlite3ExprCodeIN(
   76069                 :   Parse *pParse,        /* Parsing and code generating context */
   76070                 :   Expr *pExpr,          /* The IN expression */
   76071                 :   int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
   76072                 :   int destIfNull        /* Jump here if the results are unknown due to NULLs */
   76073                 : ){
   76074            1455 :   int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
   76075                 :   char affinity;        /* Comparison affinity to use */
   76076                 :   int eType;            /* Type of the RHS */
   76077                 :   int r1;               /* Temporary use register */
   76078                 :   Vdbe *v;              /* Statement under construction */
   76079                 : 
   76080                 :   /* Compute the RHS.   After this step, the table with cursor
   76081                 :   ** pExpr->iTable will contains the values that make up the RHS.
   76082                 :   */
   76083            1455 :   v = pParse->pVdbe;
   76084            1455 :   assert( v!=0 );       /* OOM detected prior to this routine */
   76085            1455 :   VdbeNoopComment((v, "begin IN expr"));
   76086            1455 :   eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
   76087                 : 
   76088                 :   /* Figure out the affinity to use to create a key from the results
   76089                 :   ** of the expression. affinityStr stores a static string suitable for
   76090                 :   ** P4 of OP_MakeRecord.
   76091                 :   */
   76092            1455 :   affinity = comparisonAffinity(pExpr);
   76093                 : 
   76094                 :   /* Code the LHS, the <expr> from "<expr> IN (...)".
   76095                 :   */
   76096            1455 :   sqlite3ExprCachePush(pParse);
   76097            1455 :   r1 = sqlite3GetTempReg(pParse);
   76098            1455 :   sqlite3ExprCode(pParse, pExpr->pLeft, r1);
   76099                 : 
   76100                 :   /* If the LHS is NULL, then the result is either false or NULL depending
   76101                 :   ** on whether the RHS is empty or not, respectively.
   76102                 :   */
   76103            1455 :   if( destIfNull==destIfFalse ){
   76104                 :     /* Shortcut for the common case where the false and NULL outcomes are
   76105                 :     ** the same. */
   76106             326 :     sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
   76107                 :   }else{
   76108            1129 :     int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
   76109            1129 :     sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
   76110            1129 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
   76111            1129 :     sqlite3VdbeJumpHere(v, addr1);
   76112                 :   }
   76113                 : 
   76114            1455 :   if( eType==IN_INDEX_ROWID ){
   76115                 :     /* In this case, the RHS is the ROWID of table b-tree
   76116                 :     */
   76117              52 :     sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
   76118              52 :     sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
   76119                 :   }else{
   76120                 :     /* In this case, the RHS is an index b-tree.
   76121                 :     */
   76122            1403 :     sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
   76123                 : 
   76124                 :     /* If the set membership test fails, then the result of the 
   76125                 :     ** "x IN (...)" expression must be either 0 or NULL. If the set
   76126                 :     ** contains no NULL values, then the result is 0. If the set 
   76127                 :     ** contains one or more NULL values, then the result of the
   76128                 :     ** expression is also NULL.
   76129                 :     */
   76130            1403 :     if( rRhsHasNull==0 || destIfFalse==destIfNull ){
   76131                 :       /* This branch runs if it is known at compile time that the RHS
   76132                 :       ** cannot contain NULL values. This happens as the result
   76133                 :       ** of a "NOT NULL" constraint in the database schema.
   76134                 :       **
   76135                 :       ** Also run this branch if NULL is equivalent to FALSE
   76136                 :       ** for this particular IN operator.
   76137                 :       */
   76138             326 :       sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
   76139                 : 
   76140                 :     }else{
   76141                 :       /* In this branch, the RHS of the IN might contain a NULL and
   76142                 :       ** the presence of a NULL on the RHS makes a difference in the
   76143                 :       ** outcome.
   76144                 :       */
   76145                 :       int j1, j2, j3;
   76146                 : 
   76147                 :       /* First check to see if the LHS is contained in the RHS.  If so,
   76148                 :       ** then the presence of NULLs in the RHS does not matter, so jump
   76149                 :       ** over all of the code that follows.
   76150                 :       */
   76151            1077 :       j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
   76152                 : 
   76153                 :       /* Here we begin generating code that runs if the LHS is not
   76154                 :       ** contained within the RHS.  Generate additional code that
   76155                 :       ** tests the RHS for NULLs.  If the RHS contains a NULL then
   76156                 :       ** jump to destIfNull.  If there are no NULLs in the RHS then
   76157                 :       ** jump to destIfFalse.
   76158                 :       */
   76159            1077 :       j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
   76160            1077 :       j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
   76161            1077 :       sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
   76162            1077 :       sqlite3VdbeJumpHere(v, j3);
   76163            1077 :       sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
   76164            1077 :       sqlite3VdbeJumpHere(v, j2);
   76165                 : 
   76166                 :       /* Jump to the appropriate target depending on whether or not
   76167                 :       ** the RHS contains a NULL
   76168                 :       */
   76169            1077 :       sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
   76170            1077 :       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
   76171                 : 
   76172                 :       /* The OP_Found at the top of this branch jumps here when true, 
   76173                 :       ** causing the overall IN expression evaluation to fall through.
   76174                 :       */
   76175            1077 :       sqlite3VdbeJumpHere(v, j1);
   76176                 :     }
   76177                 :   }
   76178            1455 :   sqlite3ReleaseTempReg(pParse, r1);
   76179            1455 :   sqlite3ExprCachePop(pParse, 1);
   76180            1455 :   VdbeComment((v, "end IN expr"));
   76181            1455 : }
   76182                 : #endif /* SQLITE_OMIT_SUBQUERY */
   76183                 : 
   76184                 : /*
   76185                 : ** Duplicate an 8-byte value
   76186                 : */
   76187             571 : static char *dup8bytes(Vdbe *v, const char *in){
   76188             571 :   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
   76189             571 :   if( out ){
   76190             571 :     memcpy(out, in, 8);
   76191                 :   }
   76192             571 :   return out;
   76193                 : }
   76194                 : 
   76195                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   76196                 : /*
   76197                 : ** Generate an instruction that will put the floating point
   76198                 : ** value described by z[0..n-1] into register iMem.
   76199                 : **
   76200                 : ** The z[] string will probably not be zero-terminated.  But the 
   76201                 : ** z[n] character is guaranteed to be something that does not look
   76202                 : ** like the continuation of the number.
   76203                 : */
   76204             396 : static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
   76205             396 :   if( ALWAYS(z!=0) ){
   76206                 :     double value;
   76207                 :     char *zV;
   76208             396 :     sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
   76209             396 :     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
   76210             396 :     if( negateFlag ) value = -value;
   76211             396 :     zV = dup8bytes(v, (char*)&value);
   76212             396 :     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
   76213                 :   }
   76214             396 : }
   76215                 : #endif
   76216                 : 
   76217                 : 
   76218                 : /*
   76219                 : ** Generate an instruction that will put the integer describe by
   76220                 : ** text z[0..n-1] into register iMem.
   76221                 : **
   76222                 : ** Expr.u.zToken is always UTF8 and zero-terminated.
   76223                 : */
   76224           23818 : static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
   76225           23818 :   Vdbe *v = pParse->pVdbe;
   76226           23818 :   if( pExpr->flags & EP_IntValue ){
   76227           23643 :     int i = pExpr->u.iValue;
   76228           23643 :     assert( i>=0 );
   76229           23643 :     if( negFlag ) i = -i;
   76230           23643 :     sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
   76231                 :   }else{
   76232                 :     int c;
   76233                 :     i64 value;
   76234             175 :     const char *z = pExpr->u.zToken;
   76235             175 :     assert( z!=0 );
   76236             175 :     c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
   76237             350 :     if( c==0 || (c==2 && negFlag) ){
   76238                 :       char *zV;
   76239             175 :       if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
   76240             175 :       zV = dup8bytes(v, (char*)&value);
   76241             175 :       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
   76242                 :     }else{
   76243                 : #ifdef SQLITE_OMIT_FLOATING_POINT
   76244                 :       sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
   76245                 : #else
   76246               0 :       codeReal(v, z, negFlag, iMem);
   76247                 : #endif
   76248                 :     }
   76249                 :   }
   76250           23818 : }
   76251                 : 
   76252                 : /*
   76253                 : ** Clear a cache entry.
   76254                 : */
   76255          281828 : static void cacheEntryClear(Parse *pParse, struct yColCache *p){
   76256          281828 :   if( p->tempReg ){
   76257           63164 :     if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
   76258           63160 :       pParse->aTempReg[pParse->nTempReg++] = p->iReg;
   76259                 :     }
   76260           63164 :     p->tempReg = 0;
   76261                 :   }
   76262          281828 : }
   76263                 : 
   76264                 : 
   76265                 : /*
   76266                 : ** Record in the column cache that a particular column from a
   76267                 : ** particular table is stored in a particular register.
   76268                 : */
   76269          382674 : SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
   76270                 :   int i;
   76271                 :   int minLru;
   76272                 :   int idxLru;
   76273                 :   struct yColCache *p;
   76274                 : 
   76275          382674 :   assert( iReg>0 );  /* Register numbers are always positive */
   76276          382674 :   assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
   76277                 : 
   76278                 :   /* The SQLITE_ColumnCache flag disables the column cache.  This is used
   76279                 :   ** for testing only - to verify that SQLite always gets the same answer
   76280                 :   ** with and without the column cache.
   76281                 :   */
   76282          382674 :   if( pParse->db->flags & SQLITE_ColumnCache ) return;
   76283                 : 
   76284                 :   /* First replace any existing entry.
   76285                 :   **
   76286                 :   ** Actually, the way the column cache is currently used, we are guaranteed
   76287                 :   ** that the object will never already be in cache.  Verify this guarantee.
   76288                 :   */
   76289                 : #ifndef NDEBUG
   76290         4209414 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76291                 : #if 0 /* This code wold remove the entry from the cache if it existed */
   76292                 :     if( p->iReg && p->iTable==iTab && p->iColumn==iCol ){
   76293                 :       cacheEntryClear(pParse, p);
   76294                 :       p->iLevel = pParse->iCacheLevel;
   76295                 :       p->iReg = iReg;
   76296                 :       p->lru = pParse->iCacheCnt++;
   76297                 :       return;
   76298                 :     }
   76299                 : #endif
   76300         3826740 :     assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
   76301                 :   }
   76302                 : #endif
   76303                 : 
   76304                 :   /* Find an empty slot and replace it */
   76305         1832764 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76306         1732038 :     if( p->iReg==0 ){
   76307          281948 :       p->iLevel = pParse->iCacheLevel;
   76308          281948 :       p->iTable = iTab;
   76309          281948 :       p->iColumn = iCol;
   76310          281948 :       p->iReg = iReg;
   76311          281948 :       p->tempReg = 0;
   76312          281948 :       p->lru = pParse->iCacheCnt++;
   76313          281948 :       return;
   76314                 :     }
   76315                 :   }
   76316                 : 
   76317                 :   /* Replace the last recently used */
   76318          100726 :   minLru = 0x7fffffff;
   76319          100726 :   idxLru = -1;
   76320         1107986 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76321         1007260 :     if( p->lru<minLru ){
   76322          187993 :       idxLru = i;
   76323          187993 :       minLru = p->lru;
   76324                 :     }
   76325                 :   }
   76326          100726 :   if( ALWAYS(idxLru>=0) ){
   76327          100726 :     p = &pParse->aColCache[idxLru];
   76328          100726 :     p->iLevel = pParse->iCacheLevel;
   76329          100726 :     p->iTable = iTab;
   76330          100726 :     p->iColumn = iCol;
   76331          100726 :     p->iReg = iReg;
   76332          100726 :     p->tempReg = 0;
   76333          100726 :     p->lru = pParse->iCacheCnt++;
   76334          100726 :     return;
   76335                 :   }
   76336                 : }
   76337                 : 
   76338                 : /*
   76339                 : ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
   76340                 : ** Purge the range of registers from the column cache.
   76341                 : */
   76342          186854 : SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
   76343                 :   int i;
   76344          186854 :   int iLast = iReg + nReg - 1;
   76345                 :   struct yColCache *p;
   76346         2055394 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76347         1868540 :     int r = p->iReg;
   76348         1868540 :     if( r>=iReg && r<=iLast ){
   76349          176579 :       cacheEntryClear(pParse, p);
   76350          176579 :       p->iReg = 0;
   76351                 :     }
   76352                 :   }
   76353          186854 : }
   76354                 : 
   76355                 : /*
   76356                 : ** Remember the current column cache context.  Any new entries added
   76357                 : ** added to the column cache after this call are removed when the
   76358                 : ** corresponding pop occurs.
   76359                 : */
   76360           31674 : SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
   76361           31674 :   pParse->iCacheLevel++;
   76362           31674 : }
   76363                 : 
   76364                 : /*
   76365                 : ** Remove from the column cache any entries that were added since the
   76366                 : ** the previous N Push operations.  In other words, restore the cache
   76367                 : ** to the state it was in N Pushes ago.
   76368                 : */
   76369           31674 : SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
   76370                 :   int i;
   76371                 :   struct yColCache *p;
   76372           31674 :   assert( N>0 );
   76373           31674 :   assert( pParse->iCacheLevel>=N );
   76374           31674 :   pParse->iCacheLevel -= N;
   76375          348414 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76376          316740 :     if( p->iReg && p->iLevel>pParse->iCacheLevel ){
   76377            7840 :       cacheEntryClear(pParse, p);
   76378            7840 :       p->iReg = 0;
   76379                 :     }
   76380                 :   }
   76381           31674 : }
   76382                 : 
   76383                 : /*
   76384                 : ** When a cached column is reused, make sure that its register is
   76385                 : ** no longer available as a temp register.  ticket #3879:  that same
   76386                 : ** register might be in the cache in multiple places, so be sure to
   76387                 : ** get them all.
   76388                 : */
   76389            4399 : static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
   76390                 :   int i;
   76391                 :   struct yColCache *p;
   76392           48389 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76393           43990 :     if( p->iReg==iReg ){
   76394            4400 :       p->tempReg = 0;
   76395                 :     }
   76396                 :   }
   76397            4399 : }
   76398                 : 
   76399                 : /*
   76400                 : ** Generate code to extract the value of the iCol-th column of a table.
   76401                 : */
   76402          355001 : SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
   76403                 :   Vdbe *v,        /* The VDBE under construction */
   76404                 :   Table *pTab,    /* The table containing the value */
   76405                 :   int iTabCur,    /* The cursor for this table */
   76406                 :   int iCol,       /* Index of the column to extract */
   76407                 :   int regOut      /* Extract the valud into this register */
   76408                 : ){
   76409          355001 :   if( iCol<0 || iCol==pTab->iPKey ){
   76410           26376 :     sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
   76411                 :   }else{
   76412          328625 :     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
   76413          328625 :     sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut);
   76414                 :   }
   76415          355001 :   if( iCol>=0 ){
   76416          329032 :     sqlite3ColumnDefault(v, pTab, iCol, regOut);
   76417                 :   }
   76418          355001 : }
   76419                 : 
   76420                 : /*
   76421                 : ** Generate code that will extract the iColumn-th column from
   76422                 : ** table pTab and store the column value in a register.  An effort
   76423                 : ** is made to store the column value in register iReg, but this is
   76424                 : ** not guaranteed.  The location of the column value is returned.
   76425                 : **
   76426                 : ** There must be an open cursor to pTab in iTable when this routine
   76427                 : ** is called.  If iColumn<0 then code is generated that extracts the rowid.
   76428                 : */
   76429          352754 : SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
   76430                 :   Parse *pParse,   /* Parsing and code generating context */
   76431                 :   Table *pTab,     /* Description of the table we are reading from */
   76432                 :   int iColumn,     /* Index of the table column */
   76433                 :   int iTable,      /* The cursor pointing to the table */
   76434                 :   int iReg         /* Store results here */
   76435                 : ){
   76436          352754 :   Vdbe *v = pParse->pVdbe;
   76437                 :   int i;
   76438                 :   struct yColCache *p;
   76439                 : 
   76440         3836406 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76441         3488050 :     if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
   76442            4398 :       p->lru = pParse->iCacheCnt++;
   76443            4398 :       sqlite3ExprCachePinRegister(pParse, p->iReg);
   76444            4399 :       return p->iReg;
   76445                 :     }
   76446                 :   }  
   76447          348356 :   assert( v!=0 );
   76448          348356 :   sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
   76449          348356 :   sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
   76450          348356 :   return iReg;
   76451                 : }
   76452                 : 
   76453                 : /*
   76454                 : ** Clear all column cache entries.
   76455                 : */
   76456          214336 : SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
   76457                 :   int i;
   76458                 :   struct yColCache *p;
   76459                 : 
   76460         2357696 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76461         2143360 :     if( p->iReg ){
   76462           97409 :       cacheEntryClear(pParse, p);
   76463           97409 :       p->iReg = 0;
   76464                 :     }
   76465                 :   }
   76466          214336 : }
   76467                 : 
   76468                 : /*
   76469                 : ** Record the fact that an affinity change has occurred on iCount
   76470                 : ** registers starting with iStart.
   76471                 : */
   76472          130459 : SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
   76473          130459 :   sqlite3ExprCacheRemove(pParse, iStart, iCount);
   76474          130459 : }
   76475                 : 
   76476                 : /*
   76477                 : ** Generate code to move content from registers iFrom...iFrom+nReg-1
   76478                 : ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
   76479                 : */
   76480            9895 : SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
   76481                 :   int i;
   76482                 :   struct yColCache *p;
   76483            9895 :   if( NEVER(iFrom==iTo) ) return;
   76484            9895 :   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
   76485          108845 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76486           98950 :     int x = p->iReg;
   76487           98950 :     if( x>=iFrom && x<iFrom+nReg ){
   76488            5582 :       p->iReg += iTo-iFrom;
   76489                 :     }
   76490                 :   }
   76491                 : }
   76492                 : 
   76493                 : /*
   76494                 : ** Generate code to copy content from registers iFrom...iFrom+nReg-1
   76495                 : ** over to iTo..iTo+nReg-1.
   76496                 : */
   76497               0 : SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
   76498                 :   int i;
   76499               0 :   if( NEVER(iFrom==iTo) ) return;
   76500               0 :   for(i=0; i<nReg; i++){
   76501               0 :     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
   76502                 :   }
   76503                 : }
   76504                 : 
   76505                 : #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
   76506                 : /*
   76507                 : ** Return true if any register in the range iFrom..iTo (inclusive)
   76508                 : ** is used as part of the column cache.
   76509                 : **
   76510                 : ** This routine is used within assert() and testcase() macros only
   76511                 : ** and does not appear in a normal build.
   76512                 : */
   76513           18470 : static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
   76514                 :   int i;
   76515                 :   struct yColCache *p;
   76516          203170 :   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   76517          184700 :     int r = p->iReg;
   76518          184700 :     if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
   76519                 :   }
   76520           18470 :   return 0;
   76521                 : }
   76522                 : #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
   76523                 : 
   76524                 : /*
   76525                 : ** Generate code into the current Vdbe to evaluate the given
   76526                 : ** expression.  Attempt to store the results in register "target".
   76527                 : ** Return the register where results are stored.
   76528                 : **
   76529                 : ** With this routine, there is no guarantee that results will
   76530                 : ** be stored in target.  The result might be stored in some other
   76531                 : ** register if it is convenient to do so.  The calling function
   76532                 : ** must check the return code and move the results to the desired
   76533                 : ** register.
   76534                 : */
   76535          695057 : SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
   76536          695057 :   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
   76537                 :   int op;                   /* The opcode being coded */
   76538          695057 :   int inReg = target;       /* Results stored in register inReg */
   76539          695057 :   int regFree1 = 0;         /* If non-zero free this temporary register */
   76540          695057 :   int regFree2 = 0;         /* If non-zero free this temporary register */
   76541                 :   int r1, r2, r3, r4;       /* Various register numbers */
   76542          695057 :   sqlite3 *db = pParse->db; /* The database connection */
   76543                 : 
   76544          695057 :   assert( target>0 && target<=pParse->nMem );
   76545          695057 :   if( v==0 ){
   76546               0 :     assert( pParse->db->mallocFailed );
   76547               0 :     return 0;
   76548                 :   }
   76549                 : 
   76550          695057 :   if( pExpr==0 ){
   76551            4300 :     op = TK_NULL;
   76552                 :   }else{
   76553          690757 :     op = pExpr->op;
   76554                 :   }
   76555          695057 :   switch( op ){
   76556                 :     case TK_AGG_COLUMN: {
   76557            2692 :       AggInfo *pAggInfo = pExpr->pAggInfo;
   76558            2692 :       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
   76559            2692 :       if( !pAggInfo->directMode ){
   76560             180 :         assert( pCol->iMem>0 );
   76561             180 :         inReg = pCol->iMem;
   76562             180 :         break;
   76563            2512 :       }else if( pAggInfo->useSortingIdx ){
   76564              93 :         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
   76565                 :                               pCol->iSorterColumn, target);
   76566              93 :         break;
   76567                 :       }
   76568                 :       /* Otherwise, fall thru into the TK_COLUMN case */
   76569                 :     }
   76570                 :     case TK_COLUMN: {
   76571          344736 :       if( pExpr->iTable<0 ){
   76572                 :         /* This only happens when coding check constraints */
   76573               0 :         assert( pParse->ckBase>0 );
   76574               0 :         inReg = pExpr->iColumn + pParse->ckBase;
   76575                 :       }else{
   76576          689472 :         inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
   76577          344736 :                                  pExpr->iColumn, pExpr->iTable, target);
   76578                 :       }
   76579          344737 :       break;
   76580                 :     }
   76581                 :     case TK_INTEGER: {
   76582           23641 :       codeInteger(pParse, pExpr, 0, target);
   76583           23641 :       break;
   76584                 :     }
   76585                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   76586                 :     case TK_FLOAT: {
   76587             396 :       assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76588             396 :       codeReal(v, pExpr->u.zToken, 0, target);
   76589             396 :       break;
   76590                 :     }
   76591                 : #endif
   76592                 :     case TK_STRING: {
   76593          106648 :       assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76594          106648 :       sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
   76595          106648 :       break;
   76596                 :     }
   76597                 :     case TK_NULL: {
   76598           12299 :       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
   76599           12299 :       break;
   76600                 :     }
   76601                 : #ifndef SQLITE_OMIT_BLOB_LITERAL
   76602                 :     case TK_BLOB: {
   76603                 :       int n;
   76604                 :       const char *z;
   76605                 :       char *zBlob;
   76606              29 :       assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76607              29 :       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
   76608              29 :       assert( pExpr->u.zToken[1]=='\'' );
   76609              29 :       z = &pExpr->u.zToken[2];
   76610              29 :       n = sqlite3Strlen30(z) - 1;
   76611              29 :       assert( z[n]=='\'' );
   76612              29 :       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
   76613              29 :       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
   76614              29 :       break;
   76615                 :     }
   76616                 : #endif
   76617                 :     case TK_VARIABLE: {
   76618           87675 :       assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76619           87675 :       assert( pExpr->u.zToken!=0 );
   76620           87675 :       assert( pExpr->u.zToken[0]!=0 );
   76621           87675 :       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
   76622           87675 :       if( pExpr->u.zToken[1]!=0 ){
   76623           86694 :         assert( pExpr->u.zToken[0]=='?' 
   76624                 :              || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
   76625           86694 :         sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
   76626                 :       }
   76627           87675 :       break;
   76628                 :     }
   76629                 :     case TK_REGISTER: {
   76630           65489 :       inReg = pExpr->iTable;
   76631           65489 :       break;
   76632                 :     }
   76633                 :     case TK_AS: {
   76634             357 :       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
   76635             357 :       break;
   76636                 :     }
   76637                 : #ifndef SQLITE_OMIT_CAST
   76638                 :     case TK_CAST: {
   76639                 :       /* Expressions of the form:   CAST(pLeft AS token) */
   76640                 :       int aff, to_op;
   76641             369 :       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
   76642             369 :       assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76643             369 :       aff = sqlite3AffinityType(pExpr->u.zToken);
   76644             369 :       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
   76645             369 :       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
   76646             369 :       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
   76647             369 :       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
   76648             369 :       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
   76649             369 :       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
   76650                 :       testcase( to_op==OP_ToText );
   76651                 :       testcase( to_op==OP_ToBlob );
   76652                 :       testcase( to_op==OP_ToNumeric );
   76653                 :       testcase( to_op==OP_ToInt );
   76654                 :       testcase( to_op==OP_ToReal );
   76655             369 :       if( inReg!=target ){
   76656               0 :         sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
   76657               0 :         inReg = target;
   76658                 :       }
   76659             369 :       sqlite3VdbeAddOp1(v, to_op, inReg);
   76660                 :       testcase( usedAsColumnCache(pParse, inReg, inReg) );
   76661             369 :       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
   76662             369 :       break;
   76663                 :     }
   76664                 : #endif /* SQLITE_OMIT_CAST */
   76665                 :     case TK_LT:
   76666                 :     case TK_LE:
   76667                 :     case TK_GT:
   76668                 :     case TK_GE:
   76669                 :     case TK_NE:
   76670                 :     case TK_EQ: {
   76671                 :       assert( TK_LT==OP_Lt );
   76672                 :       assert( TK_LE==OP_Le );
   76673                 :       assert( TK_GT==OP_Gt );
   76674                 :       assert( TK_GE==OP_Ge );
   76675                 :       assert( TK_EQ==OP_Eq );
   76676                 :       assert( TK_NE==OP_Ne );
   76677                 :       testcase( op==TK_LT );
   76678                 :       testcase( op==TK_LE );
   76679                 :       testcase( op==TK_GT );
   76680                 :       testcase( op==TK_GE );
   76681                 :       testcase( op==TK_EQ );
   76682                 :       testcase( op==TK_NE );
   76683             645 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   76684             645 :       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
   76685             645 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
   76686                 :                   r1, r2, inReg, SQLITE_STOREP2);
   76687                 :       testcase( regFree1==0 );
   76688                 :       testcase( regFree2==0 );
   76689             645 :       break;
   76690                 :     }
   76691                 :     case TK_IS:
   76692                 :     case TK_ISNOT: {
   76693                 :       testcase( op==TK_IS );
   76694                 :       testcase( op==TK_ISNOT );
   76695               0 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   76696               0 :       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
   76697               0 :       op = (op==TK_IS) ? TK_EQ : TK_NE;
   76698               0 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
   76699                 :                   r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
   76700                 :       testcase( regFree1==0 );
   76701                 :       testcase( regFree2==0 );
   76702               0 :       break;
   76703                 :     }
   76704                 :     case TK_AND:
   76705                 :     case TK_OR:
   76706                 :     case TK_PLUS:
   76707                 :     case TK_STAR:
   76708                 :     case TK_MINUS:
   76709                 :     case TK_REM:
   76710                 :     case TK_BITAND:
   76711                 :     case TK_BITOR:
   76712                 :     case TK_SLASH:
   76713                 :     case TK_LSHIFT:
   76714                 :     case TK_RSHIFT: 
   76715                 :     case TK_CONCAT: {
   76716                 :       assert( TK_AND==OP_And );
   76717                 :       assert( TK_OR==OP_Or );
   76718                 :       assert( TK_PLUS==OP_Add );
   76719                 :       assert( TK_MINUS==OP_Subtract );
   76720                 :       assert( TK_REM==OP_Remainder );
   76721                 :       assert( TK_BITAND==OP_BitAnd );
   76722                 :       assert( TK_BITOR==OP_BitOr );
   76723                 :       assert( TK_SLASH==OP_Divide );
   76724                 :       assert( TK_LSHIFT==OP_ShiftLeft );
   76725                 :       assert( TK_RSHIFT==OP_ShiftRight );
   76726                 :       assert( TK_CONCAT==OP_Concat );
   76727                 :       testcase( op==TK_AND );
   76728                 :       testcase( op==TK_OR );
   76729                 :       testcase( op==TK_PLUS );
   76730                 :       testcase( op==TK_MINUS );
   76731                 :       testcase( op==TK_REM );
   76732                 :       testcase( op==TK_BITAND );
   76733                 :       testcase( op==TK_BITOR );
   76734                 :       testcase( op==TK_SLASH );
   76735                 :       testcase( op==TK_LSHIFT );
   76736                 :       testcase( op==TK_RSHIFT );
   76737                 :       testcase( op==TK_CONCAT );
   76738            8646 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   76739            8646 :       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
   76740            8646 :       sqlite3VdbeAddOp3(v, op, r2, r1, target);
   76741                 :       testcase( regFree1==0 );
   76742                 :       testcase( regFree2==0 );
   76743            8646 :       break;
   76744                 :     }
   76745                 :     case TK_UMINUS: {
   76746             177 :       Expr *pLeft = pExpr->pLeft;
   76747             177 :       assert( pLeft );
   76748             177 :       if( pLeft->op==TK_INTEGER ){
   76749             177 :         codeInteger(pParse, pLeft, 1, target);
   76750                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   76751               0 :       }else if( pLeft->op==TK_FLOAT ){
   76752               0 :         assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76753               0 :         codeReal(v, pLeft->u.zToken, 1, target);
   76754                 : #endif
   76755                 :       }else{
   76756               0 :         regFree1 = r1 = sqlite3GetTempReg(pParse);
   76757               0 :         sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
   76758               0 :         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
   76759               0 :         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
   76760                 :         testcase( regFree2==0 );
   76761                 :       }
   76762             177 :       inReg = target;
   76763             177 :       break;
   76764                 :     }
   76765                 :     case TK_BITNOT:
   76766                 :     case TK_NOT: {
   76767                 :       assert( TK_BITNOT==OP_BitNot );
   76768                 :       assert( TK_NOT==OP_Not );
   76769                 :       testcase( op==TK_BITNOT );
   76770                 :       testcase( op==TK_NOT );
   76771             639 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   76772                 :       testcase( regFree1==0 );
   76773             639 :       inReg = target;
   76774             639 :       sqlite3VdbeAddOp2(v, op, r1, inReg);
   76775             639 :       break;
   76776                 :     }
   76777                 :     case TK_ISNULL:
   76778                 :     case TK_NOTNULL: {
   76779                 :       int addr;
   76780                 :       assert( TK_ISNULL==OP_IsNull );
   76781                 :       assert( TK_NOTNULL==OP_NotNull );
   76782                 :       testcase( op==TK_ISNULL );
   76783                 :       testcase( op==TK_NOTNULL );
   76784               0 :       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
   76785               0 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   76786                 :       testcase( regFree1==0 );
   76787               0 :       addr = sqlite3VdbeAddOp1(v, op, r1);
   76788               0 :       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
   76789               0 :       sqlite3VdbeJumpHere(v, addr);
   76790               0 :       break;
   76791                 :     }
   76792                 :     case TK_AGG_FUNCTION: {
   76793            4494 :       AggInfo *pInfo = pExpr->pAggInfo;
   76794            4494 :       if( pInfo==0 ){
   76795               0 :         assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76796               0 :         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
   76797                 :       }else{
   76798            4494 :         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
   76799                 :       }
   76800            4494 :       break;
   76801                 :     }
   76802                 :     case TK_CONST_FUNC:
   76803                 :     case TK_FUNCTION: {
   76804                 :       ExprList *pFarg;       /* List of function arguments */
   76805                 :       int nFarg;             /* Number of function arguments */
   76806                 :       FuncDef *pDef;         /* The function definition object */
   76807                 :       int nId;               /* Length of the function name in bytes */
   76808                 :       const char *zId;       /* The function name */
   76809           16144 :       int constMask = 0;     /* Mask of function arguments that are constant */
   76810                 :       int i;                 /* Loop counter */
   76811           16144 :       u8 enc = ENC(db);      /* The text encoding used by this database */
   76812           16144 :       CollSeq *pColl = 0;    /* A collating sequence */
   76813                 : 
   76814           16144 :       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   76815                 :       testcase( op==TK_CONST_FUNC );
   76816                 :       testcase( op==TK_FUNCTION );
   76817           16144 :       if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
   76818               0 :         pFarg = 0;
   76819                 :       }else{
   76820           16144 :         pFarg = pExpr->x.pList;
   76821                 :       }
   76822           16144 :       nFarg = pFarg ? pFarg->nExpr : 0;
   76823           16144 :       assert( !ExprHasProperty(pExpr, EP_IntValue) );
   76824           16144 :       zId = pExpr->u.zToken;
   76825           16144 :       nId = sqlite3Strlen30(zId);
   76826           16144 :       pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
   76827           16144 :       if( pDef==0 ){
   76828               0 :         sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
   76829               0 :         break;
   76830                 :       }
   76831                 : 
   76832                 :       /* Attempt a direct implementation of the built-in COALESCE() and
   76833                 :       ** IFNULL() functions.  This avoids unnecessary evalation of
   76834                 :       ** arguments past the first non-NULL argument.
   76835                 :       */
   76836           16144 :       if( pDef->flags & SQLITE_FUNC_COALESCE ){
   76837            4133 :         int endCoalesce = sqlite3VdbeMakeLabel(v);
   76838            4133 :         assert( nFarg>=2 );
   76839            4133 :         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
   76840            8291 :         for(i=1; i<nFarg; i++){
   76841            4158 :           sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
   76842            4158 :           sqlite3ExprCacheRemove(pParse, target, 1);
   76843            4158 :           sqlite3ExprCachePush(pParse);
   76844            4158 :           sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
   76845            4158 :           sqlite3ExprCachePop(pParse, 1);
   76846                 :         }
   76847            4133 :         sqlite3VdbeResolveLabel(v, endCoalesce);
   76848            4133 :         break;
   76849                 :       }
   76850                 : 
   76851                 : 
   76852           12011 :       if( pFarg ){
   76853            9907 :         r1 = sqlite3GetTempRange(pParse, nFarg);
   76854            9907 :         sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
   76855            9907 :         sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
   76856            9907 :         sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
   76857                 :       }else{
   76858            2104 :         r1 = 0;
   76859                 :       }
   76860                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   76861                 :       /* Possibly overload the function if the first argument is
   76862                 :       ** a virtual table column.
   76863                 :       **
   76864                 :       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
   76865                 :       ** second argument, not the first, as the argument to test to
   76866                 :       ** see if it is a column in a virtual table.  This is done because
   76867                 :       ** the left operand of infix functions (the operand we want to
   76868                 :       ** control overloading) ends up as the second argument to the
   76869                 :       ** function.  The expression "A glob B" is equivalent to 
   76870                 :       ** "glob(B,A).  We want to use the A in "A glob B" to test
   76871                 :       ** for function overloading.  But we use the B term in "glob(B,A)".
   76872                 :       */
   76873           12011 :       if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
   76874             216 :         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
   76875           11795 :       }else if( nFarg>0 ){
   76876            9691 :         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
   76877                 :       }
   76878                 : #endif
   76879           30423 :       for(i=0; i<nFarg; i++){
   76880           18412 :         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
   76881            8991 :           constMask |= (1<<i);
   76882                 :         }
   76883           18412 :         if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
   76884            2532 :           pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
   76885                 :         }
   76886                 :       }
   76887           12011 :       if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
   76888            1717 :         if( !pColl ) pColl = db->pDfltColl; 
   76889            1717 :         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
   76890                 :       }
   76891           12011 :       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
   76892                 :                         (char*)pDef, P4_FUNCDEF);
   76893           12011 :       sqlite3VdbeChangeP5(v, (u8)nFarg);
   76894           12011 :       if( nFarg ){
   76895            9907 :         sqlite3ReleaseTempRange(pParse, r1, nFarg);
   76896                 :       }
   76897           12011 :       break;
   76898                 :     }
   76899                 : #ifndef SQLITE_OMIT_SUBQUERY
   76900                 :     case TK_EXISTS:
   76901                 :     case TK_SELECT: {
   76902                 :       testcase( op==TK_EXISTS );
   76903                 :       testcase( op==TK_SELECT );
   76904           11164 :       inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
   76905           11164 :       break;
   76906                 :     }
   76907                 :     case TK_IN: {
   76908             628 :       int destIfFalse = sqlite3VdbeMakeLabel(v);
   76909             628 :       int destIfNull = sqlite3VdbeMakeLabel(v);
   76910             628 :       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
   76911             628 :       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
   76912             628 :       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
   76913             628 :       sqlite3VdbeResolveLabel(v, destIfFalse);
   76914             628 :       sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
   76915             628 :       sqlite3VdbeResolveLabel(v, destIfNull);
   76916             628 :       break;
   76917                 :     }
   76918                 : #endif /* SQLITE_OMIT_SUBQUERY */
   76919                 : 
   76920                 : 
   76921                 :     /*
   76922                 :     **    x BETWEEN y AND z
   76923                 :     **
   76924                 :     ** This is equivalent to
   76925                 :     **
   76926                 :     **    x>=y AND x<=z
   76927                 :     **
   76928                 :     ** X is stored in pExpr->pLeft.
   76929                 :     ** Y is stored in pExpr->pList->a[0].pExpr.
   76930                 :     ** Z is stored in pExpr->pList->a[1].pExpr.
   76931                 :     */
   76932                 :     case TK_BETWEEN: {
   76933               0 :       Expr *pLeft = pExpr->pLeft;
   76934               0 :       struct ExprList_item *pLItem = pExpr->x.pList->a;
   76935               0 :       Expr *pRight = pLItem->pExpr;
   76936                 : 
   76937               0 :       r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
   76938               0 :       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
   76939                 :       testcase( regFree1==0 );
   76940                 :       testcase( regFree2==0 );
   76941               0 :       r3 = sqlite3GetTempReg(pParse);
   76942               0 :       r4 = sqlite3GetTempReg(pParse);
   76943               0 :       codeCompare(pParse, pLeft, pRight, OP_Ge,
   76944                 :                   r1, r2, r3, SQLITE_STOREP2);
   76945               0 :       pLItem++;
   76946               0 :       pRight = pLItem->pExpr;
   76947               0 :       sqlite3ReleaseTempReg(pParse, regFree2);
   76948               0 :       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
   76949                 :       testcase( regFree2==0 );
   76950               0 :       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
   76951               0 :       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
   76952               0 :       sqlite3ReleaseTempReg(pParse, r3);
   76953               0 :       sqlite3ReleaseTempReg(pParse, r4);
   76954               0 :       break;
   76955                 :     }
   76956                 :     case TK_UPLUS: {
   76957             182 :       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
   76958             182 :       break;
   76959                 :     }
   76960                 : 
   76961                 :     case TK_TRIGGER: {
   76962                 :       /* If the opcode is TK_TRIGGER, then the expression is a reference
   76963                 :       ** to a column in the new.* or old.* pseudo-tables available to
   76964                 :       ** trigger programs. In this case Expr.iTable is set to 1 for the
   76965                 :       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
   76966                 :       ** is set to the column of the pseudo-table to read, or to -1 to
   76967                 :       ** read the rowid field.
   76968                 :       **
   76969                 :       ** The expression is implemented using an OP_Param opcode. The p1
   76970                 :       ** parameter is set to 0 for an old.rowid reference, or to (i+1)
   76971                 :       ** to reference another column of the old.* pseudo-table, where 
   76972                 :       ** i is the index of the column. For a new.rowid reference, p1 is
   76973                 :       ** set to (n+1), where n is the number of columns in each pseudo-table.
   76974                 :       ** For a reference to any other column in the new.* pseudo-table, p1
   76975                 :       ** is set to (n+2+i), where n and i are as defined previously. For
   76976                 :       ** example, if the table on which triggers are being fired is
   76977                 :       ** declared as:
   76978                 :       **
   76979                 :       **   CREATE TABLE t1(a, b);
   76980                 :       **
   76981                 :       ** Then p1 is interpreted as follows:
   76982                 :       **
   76983                 :       **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
   76984                 :       **   p1==1   ->    old.a         p1==4   ->    new.a
   76985                 :       **   p1==2   ->    old.b         p1==5   ->    new.b       
   76986                 :       */
   76987           10356 :       Table *pTab = pExpr->pTab;
   76988           10356 :       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
   76989                 : 
   76990           10356 :       assert( pExpr->iTable==0 || pExpr->iTable==1 );
   76991           10356 :       assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
   76992           10356 :       assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
   76993           10356 :       assert( p1>=0 && p1<(pTab->nCol*2+2) );
   76994                 : 
   76995           10356 :       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
   76996           10356 :       VdbeComment((v, "%s.%s -> $%d",
   76997                 :         (pExpr->iTable ? "new" : "old"),
   76998                 :         (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
   76999                 :         target
   77000                 :       ));
   77001                 : 
   77002                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   77003                 :       /* If the column has REAL affinity, it may currently be stored as an
   77004                 :       ** integer. Use OP_RealAffinity to make sure it is really real.  */
   77005           10356 :       if( pExpr->iColumn>=0 
   77006            8775 :        && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
   77007                 :       ){
   77008               0 :         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
   77009                 :       }
   77010                 : #endif
   77011           10356 :       break;
   77012                 :     }
   77013                 : 
   77014                 : 
   77015                 :     /*
   77016                 :     ** Form A:
   77017                 :     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
   77018                 :     **
   77019                 :     ** Form B:
   77020                 :     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
   77021                 :     **
   77022                 :     ** Form A is can be transformed into the equivalent form B as follows:
   77023                 :     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
   77024                 :     **        WHEN x=eN THEN rN ELSE y END
   77025                 :     **
   77026                 :     ** X (if it exists) is in pExpr->pLeft.
   77027                 :     ** Y is in pExpr->pRight.  The Y is also optional.  If there is no
   77028                 :     ** ELSE clause and no other term matches, then the result of the
   77029                 :     ** exprssion is NULL.
   77030                 :     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
   77031                 :     **
   77032                 :     ** The result of the expression is the Ri for the first matching Ei,
   77033                 :     ** or if there is no matching Ei, the ELSE term Y, or if there is
   77034                 :     ** no ELSE term, NULL.
   77035                 :     */
   77036              70 :     default: assert( op==TK_CASE ); {
   77037                 :       int endLabel;                     /* GOTO label for end of CASE stmt */
   77038                 :       int nextCase;                     /* GOTO label for next WHEN clause */
   77039                 :       int nExpr;                        /* 2x number of WHEN terms */
   77040                 :       int i;                            /* Loop counter */
   77041                 :       ExprList *pEList;                 /* List of WHEN terms */
   77042                 :       struct ExprList_item *aListelem;  /* Array of WHEN terms */
   77043                 :       Expr opCompare;                   /* The X==Ei expression */
   77044                 :       Expr cacheX;                      /* Cached expression X */
   77045                 :       Expr *pX;                         /* The X expression */
   77046              70 :       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
   77047              70 :       VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
   77048                 : 
   77049              70 :       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
   77050              70 :       assert((pExpr->x.pList->nExpr % 2) == 0);
   77051              70 :       assert(pExpr->x.pList->nExpr > 0);
   77052              70 :       pEList = pExpr->x.pList;
   77053              70 :       aListelem = pEList->a;
   77054              70 :       nExpr = pEList->nExpr;
   77055              70 :       endLabel = sqlite3VdbeMakeLabel(v);
   77056              70 :       if( (pX = pExpr->pLeft)!=0 ){
   77057               0 :         cacheX = *pX;
   77058                 :         testcase( pX->op==TK_COLUMN );
   77059                 :         testcase( pX->op==TK_REGISTER );
   77060               0 :         cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
   77061                 :         testcase( regFree1==0 );
   77062               0 :         cacheX.op = TK_REGISTER;
   77063               0 :         opCompare.op = TK_EQ;
   77064               0 :         opCompare.pLeft = &cacheX;
   77065               0 :         pTest = &opCompare;
   77066                 :         /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
   77067                 :         ** The value in regFree1 might get SCopy-ed into the file result.
   77068                 :         ** So make sure that the regFree1 register is not reused for other
   77069                 :         ** purposes and possibly overwritten.  */
   77070               0 :         regFree1 = 0;
   77071                 :       }
   77072             140 :       for(i=0; i<nExpr; i=i+2){
   77073              70 :         sqlite3ExprCachePush(pParse);
   77074              70 :         if( pX ){
   77075               0 :           assert( pTest!=0 );
   77076               0 :           opCompare.pRight = aListelem[i].pExpr;
   77077                 :         }else{
   77078              70 :           pTest = aListelem[i].pExpr;
   77079                 :         }
   77080              70 :         nextCase = sqlite3VdbeMakeLabel(v);
   77081                 :         testcase( pTest->op==TK_COLUMN );
   77082              70 :         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
   77083                 :         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
   77084                 :         testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
   77085              70 :         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
   77086              70 :         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
   77087              70 :         sqlite3ExprCachePop(pParse, 1);
   77088              70 :         sqlite3VdbeResolveLabel(v, nextCase);
   77089                 :       }
   77090              70 :       if( pExpr->pRight ){
   77091              70 :         sqlite3ExprCachePush(pParse);
   77092              70 :         sqlite3ExprCode(pParse, pExpr->pRight, target);
   77093              70 :         sqlite3ExprCachePop(pParse, 1);
   77094                 :       }else{
   77095               0 :         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
   77096                 :       }
   77097              70 :       assert( db->mallocFailed || pParse->nErr>0 
   77098                 :            || pParse->iCacheLevel==iCacheLevel );
   77099              70 :       sqlite3VdbeResolveLabel(v, endLabel);
   77100              70 :       break;
   77101                 :     }
   77102                 : #ifndef SQLITE_OMIT_TRIGGER
   77103                 :     case TK_RAISE: {
   77104               0 :       assert( pExpr->affinity==OE_Rollback 
   77105                 :            || pExpr->affinity==OE_Abort
   77106                 :            || pExpr->affinity==OE_Fail
   77107                 :            || pExpr->affinity==OE_Ignore
   77108                 :       );
   77109               0 :       if( !pParse->pTriggerTab ){
   77110               0 :         sqlite3ErrorMsg(pParse,
   77111                 :                        "RAISE() may only be used within a trigger-program");
   77112               0 :         return 0;
   77113                 :       }
   77114               0 :       if( pExpr->affinity==OE_Abort ){
   77115               0 :         sqlite3MayAbort(pParse);
   77116                 :       }
   77117               0 :       assert( !ExprHasProperty(pExpr, EP_IntValue) );
   77118               0 :       if( pExpr->affinity==OE_Ignore ){
   77119               0 :         sqlite3VdbeAddOp4(
   77120               0 :             v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
   77121                 :       }else{
   77122               0 :         sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0);
   77123                 :       }
   77124                 : 
   77125               0 :       break;
   77126                 :     }
   77127                 : #endif
   77128                 :   }
   77129          695058 :   sqlite3ReleaseTempReg(pParse, regFree1);
   77130          695058 :   sqlite3ReleaseTempReg(pParse, regFree2);
   77131          695058 :   return inReg;
   77132                 : }
   77133                 : 
   77134                 : /*
   77135                 : ** Generate code to evaluate an expression and store the results
   77136                 : ** into a register.  Return the register number where the results
   77137                 : ** are stored.
   77138                 : **
   77139                 : ** If the register is a temporary register that can be deallocated,
   77140                 : ** then write its number into *pReg.  If the result register is not
   77141                 : ** a temporary, then set *pReg to zero.
   77142                 : */
   77143          108444 : SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
   77144          108444 :   int r1 = sqlite3GetTempReg(pParse);
   77145          108443 :   int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
   77146          108444 :   if( r2==r1 ){
   77147           66107 :     *pReg = r1;
   77148                 :   }else{
   77149           42337 :     sqlite3ReleaseTempReg(pParse, r1);
   77150           42337 :     *pReg = 0;
   77151                 :   }
   77152          108444 :   return r2;
   77153                 : }
   77154                 : 
   77155                 : /*
   77156                 : ** Generate code that will evaluate expression pExpr and store the
   77157                 : ** results in register target.  The results are guaranteed to appear
   77158                 : ** in register target.
   77159                 : */
   77160          177139 : SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
   77161                 :   int inReg;
   77162                 : 
   77163          177139 :   assert( target>0 && target<=pParse->nMem );
   77164          177139 :   if( pExpr && pExpr->op==TK_REGISTER ){
   77165           16389 :     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
   77166                 :   }else{
   77167          160750 :     inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
   77168          160750 :     assert( pParse->pVdbe || pParse->db->mallocFailed );
   77169          160750 :     if( inReg!=target && pParse->pVdbe ){
   77170            5511 :       sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
   77171                 :     }
   77172                 :   }
   77173          177139 :   return target;
   77174                 : }
   77175                 : 
   77176                 : /*
   77177                 : ** Generate code that evalutes the given expression and puts the result
   77178                 : ** in register target.
   77179                 : **
   77180                 : ** Also make a copy of the expression results into another "cache" register
   77181                 : ** and modify the expression so that the next time it is evaluated,
   77182                 : ** the result is a copy of the cache register.
   77183                 : **
   77184                 : ** This routine is used for expressions that are used multiple 
   77185                 : ** times.  They are evaluated once and the results of the expression
   77186                 : ** are reused.
   77187                 : */
   77188               0 : SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
   77189               0 :   Vdbe *v = pParse->pVdbe;
   77190                 :   int inReg;
   77191               0 :   inReg = sqlite3ExprCode(pParse, pExpr, target);
   77192               0 :   assert( target>0 );
   77193                 :   /* This routine is called for terms to INSERT or UPDATE.  And the only
   77194                 :   ** other place where expressions can be converted into TK_REGISTER is
   77195                 :   ** in WHERE clause processing.  So as currently implemented, there is
   77196                 :   ** no way for a TK_REGISTER to exist here.  But it seems prudent to
   77197                 :   ** keep the ALWAYS() in case the conditions above change with future
   77198                 :   ** modifications or enhancements. */
   77199               0 :   if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
   77200                 :     int iMem;
   77201               0 :     iMem = ++pParse->nMem;
   77202               0 :     sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
   77203               0 :     pExpr->iTable = iMem;
   77204               0 :     pExpr->op2 = pExpr->op;
   77205               0 :     pExpr->op = TK_REGISTER;
   77206                 :   }
   77207               0 :   return inReg;
   77208                 : }
   77209                 : 
   77210                 : #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
   77211                 : /*
   77212                 : ** Generate a human-readable explanation of an expression tree.
   77213                 : */
   77214                 : SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
   77215                 :   int op;                   /* The opcode being coded */
   77216                 :   const char *zBinOp = 0;   /* Binary operator */
   77217                 :   const char *zUniOp = 0;   /* Unary operator */
   77218                 :   if( pExpr==0 ){
   77219                 :     op = TK_NULL;
   77220                 :   }else{
   77221                 :     op = pExpr->op;
   77222                 :   }
   77223                 :   switch( op ){
   77224                 :     case TK_AGG_COLUMN: {
   77225                 :       sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
   77226                 :             pExpr->iTable, pExpr->iColumn);
   77227                 :       break;
   77228                 :     }
   77229                 :     case TK_COLUMN: {
   77230                 :       if( pExpr->iTable<0 ){
   77231                 :         /* This only happens when coding check constraints */
   77232                 :         sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
   77233                 :       }else{
   77234                 :         sqlite3ExplainPrintf(pOut, "{%d:%d}",
   77235                 :                              pExpr->iTable, pExpr->iColumn);
   77236                 :       }
   77237                 :       break;
   77238                 :     }
   77239                 :     case TK_INTEGER: {
   77240                 :       if( pExpr->flags & EP_IntValue ){
   77241                 :         sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
   77242                 :       }else{
   77243                 :         sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
   77244                 :       }
   77245                 :       break;
   77246                 :     }
   77247                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   77248                 :     case TK_FLOAT: {
   77249                 :       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
   77250                 :       break;
   77251                 :     }
   77252                 : #endif
   77253                 :     case TK_STRING: {
   77254                 :       sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
   77255                 :       break;
   77256                 :     }
   77257                 :     case TK_NULL: {
   77258                 :       sqlite3ExplainPrintf(pOut,"NULL");
   77259                 :       break;
   77260                 :     }
   77261                 : #ifndef SQLITE_OMIT_BLOB_LITERAL
   77262                 :     case TK_BLOB: {
   77263                 :       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
   77264                 :       break;
   77265                 :     }
   77266                 : #endif
   77267                 :     case TK_VARIABLE: {
   77268                 :       sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
   77269                 :                            pExpr->u.zToken, pExpr->iColumn);
   77270                 :       break;
   77271                 :     }
   77272                 :     case TK_REGISTER: {
   77273                 :       sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
   77274                 :       break;
   77275                 :     }
   77276                 :     case TK_AS: {
   77277                 :       sqlite3ExplainExpr(pOut, pExpr->pLeft);
   77278                 :       break;
   77279                 :     }
   77280                 : #ifndef SQLITE_OMIT_CAST
   77281                 :     case TK_CAST: {
   77282                 :       /* Expressions of the form:   CAST(pLeft AS token) */
   77283                 :       const char *zAff = "unk";
   77284                 :       switch( sqlite3AffinityType(pExpr->u.zToken) ){
   77285                 :         case SQLITE_AFF_TEXT:    zAff = "TEXT";     break;
   77286                 :         case SQLITE_AFF_NONE:    zAff = "NONE";     break;
   77287                 :         case SQLITE_AFF_NUMERIC: zAff = "NUMERIC";  break;
   77288                 :         case SQLITE_AFF_INTEGER: zAff = "INTEGER";  break;
   77289                 :         case SQLITE_AFF_REAL:    zAff = "REAL";     break;
   77290                 :       }
   77291                 :       sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
   77292                 :       sqlite3ExplainExpr(pOut, pExpr->pLeft);
   77293                 :       sqlite3ExplainPrintf(pOut, ")");
   77294                 :       break;
   77295                 :     }
   77296                 : #endif /* SQLITE_OMIT_CAST */
   77297                 :     case TK_LT:      zBinOp = "LT";     break;
   77298                 :     case TK_LE:      zBinOp = "LE";     break;
   77299                 :     case TK_GT:      zBinOp = "GT";     break;
   77300                 :     case TK_GE:      zBinOp = "GE";     break;
   77301                 :     case TK_NE:      zBinOp = "NE";     break;
   77302                 :     case TK_EQ:      zBinOp = "EQ";     break;
   77303                 :     case TK_IS:      zBinOp = "IS";     break;
   77304                 :     case TK_ISNOT:   zBinOp = "ISNOT";  break;
   77305                 :     case TK_AND:     zBinOp = "AND";    break;
   77306                 :     case TK_OR:      zBinOp = "OR";     break;
   77307                 :     case TK_PLUS:    zBinOp = "ADD";    break;
   77308                 :     case TK_STAR:    zBinOp = "MUL";    break;
   77309                 :     case TK_MINUS:   zBinOp = "SUB";    break;
   77310                 :     case TK_REM:     zBinOp = "REM";    break;
   77311                 :     case TK_BITAND:  zBinOp = "BITAND"; break;
   77312                 :     case TK_BITOR:   zBinOp = "BITOR";  break;
   77313                 :     case TK_SLASH:   zBinOp = "DIV";    break;
   77314                 :     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
   77315                 :     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
   77316                 :     case TK_CONCAT:  zBinOp = "CONCAT"; break;
   77317                 : 
   77318                 :     case TK_UMINUS:  zUniOp = "UMINUS"; break;
   77319                 :     case TK_UPLUS:   zUniOp = "UPLUS";  break;
   77320                 :     case TK_BITNOT:  zUniOp = "BITNOT"; break;
   77321                 :     case TK_NOT:     zUniOp = "NOT";    break;
   77322                 :     case TK_ISNULL:  zUniOp = "ISNULL"; break;
   77323                 :     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
   77324                 : 
   77325                 :     case TK_AGG_FUNCTION:
   77326                 :     case TK_CONST_FUNC:
   77327                 :     case TK_FUNCTION: {
   77328                 :       ExprList *pFarg;       /* List of function arguments */
   77329                 :       if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
   77330                 :         pFarg = 0;
   77331                 :       }else{
   77332                 :         pFarg = pExpr->x.pList;
   77333                 :       }
   77334                 :       sqlite3ExplainPrintf(pOut, "%sFUNCTION:%s(",
   77335                 :                            op==TK_AGG_FUNCTION ? "AGG_" : "",
   77336                 :                            pExpr->u.zToken);
   77337                 :       if( pFarg ){
   77338                 :         sqlite3ExplainExprList(pOut, pFarg);
   77339                 :       }
   77340                 :       sqlite3ExplainPrintf(pOut, ")");
   77341                 :       break;
   77342                 :     }
   77343                 : #ifndef SQLITE_OMIT_SUBQUERY
   77344                 :     case TK_EXISTS: {
   77345                 :       sqlite3ExplainPrintf(pOut, "EXISTS(");
   77346                 :       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
   77347                 :       sqlite3ExplainPrintf(pOut,")");
   77348                 :       break;
   77349                 :     }
   77350                 :     case TK_SELECT: {
   77351                 :       sqlite3ExplainPrintf(pOut, "(");
   77352                 :       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
   77353                 :       sqlite3ExplainPrintf(pOut, ")");
   77354                 :       break;
   77355                 :     }
   77356                 :     case TK_IN: {
   77357                 :       sqlite3ExplainPrintf(pOut, "IN(");
   77358                 :       sqlite3ExplainExpr(pOut, pExpr->pLeft);
   77359                 :       sqlite3ExplainPrintf(pOut, ",");
   77360                 :       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   77361                 :         sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
   77362                 :       }else{
   77363                 :         sqlite3ExplainExprList(pOut, pExpr->x.pList);
   77364                 :       }
   77365                 :       sqlite3ExplainPrintf(pOut, ")");
   77366                 :       break;
   77367                 :     }
   77368                 : #endif /* SQLITE_OMIT_SUBQUERY */
   77369                 : 
   77370                 :     /*
   77371                 :     **    x BETWEEN y AND z
   77372                 :     **
   77373                 :     ** This is equivalent to
   77374                 :     **
   77375                 :     **    x>=y AND x<=z
   77376                 :     **
   77377                 :     ** X is stored in pExpr->pLeft.
   77378                 :     ** Y is stored in pExpr->pList->a[0].pExpr.
   77379                 :     ** Z is stored in pExpr->pList->a[1].pExpr.
   77380                 :     */
   77381                 :     case TK_BETWEEN: {
   77382                 :       Expr *pX = pExpr->pLeft;
   77383                 :       Expr *pY = pExpr->x.pList->a[0].pExpr;
   77384                 :       Expr *pZ = pExpr->x.pList->a[1].pExpr;
   77385                 :       sqlite3ExplainPrintf(pOut, "BETWEEN(");
   77386                 :       sqlite3ExplainExpr(pOut, pX);
   77387                 :       sqlite3ExplainPrintf(pOut, ",");
   77388                 :       sqlite3ExplainExpr(pOut, pY);
   77389                 :       sqlite3ExplainPrintf(pOut, ",");
   77390                 :       sqlite3ExplainExpr(pOut, pZ);
   77391                 :       sqlite3ExplainPrintf(pOut, ")");
   77392                 :       break;
   77393                 :     }
   77394                 :     case TK_TRIGGER: {
   77395                 :       /* If the opcode is TK_TRIGGER, then the expression is a reference
   77396                 :       ** to a column in the new.* or old.* pseudo-tables available to
   77397                 :       ** trigger programs. In this case Expr.iTable is set to 1 for the
   77398                 :       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
   77399                 :       ** is set to the column of the pseudo-table to read, or to -1 to
   77400                 :       ** read the rowid field.
   77401                 :       */
   77402                 :       sqlite3ExplainPrintf(pOut, "%s(%d)", 
   77403                 :           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
   77404                 :       break;
   77405                 :     }
   77406                 :     case TK_CASE: {
   77407                 :       sqlite3ExplainPrintf(pOut, "CASE(");
   77408                 :       sqlite3ExplainExpr(pOut, pExpr->pLeft);
   77409                 :       sqlite3ExplainPrintf(pOut, ",");
   77410                 :       sqlite3ExplainExprList(pOut, pExpr->x.pList);
   77411                 :       break;
   77412                 :     }
   77413                 : #ifndef SQLITE_OMIT_TRIGGER
   77414                 :     case TK_RAISE: {
   77415                 :       const char *zType = "unk";
   77416                 :       switch( pExpr->affinity ){
   77417                 :         case OE_Rollback:   zType = "rollback";  break;
   77418                 :         case OE_Abort:      zType = "abort";     break;
   77419                 :         case OE_Fail:       zType = "fail";      break;
   77420                 :         case OE_Ignore:     zType = "ignore";    break;
   77421                 :       }
   77422                 :       sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
   77423                 :       break;
   77424                 :     }
   77425                 : #endif
   77426                 :   }
   77427                 :   if( zBinOp ){
   77428                 :     sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
   77429                 :     sqlite3ExplainExpr(pOut, pExpr->pLeft);
   77430                 :     sqlite3ExplainPrintf(pOut,",");
   77431                 :     sqlite3ExplainExpr(pOut, pExpr->pRight);
   77432                 :     sqlite3ExplainPrintf(pOut,")");
   77433                 :   }else if( zUniOp ){
   77434                 :     sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
   77435                 :     sqlite3ExplainExpr(pOut, pExpr->pLeft);
   77436                 :     sqlite3ExplainPrintf(pOut,")");
   77437                 :   }
   77438                 : }
   77439                 : #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
   77440                 : 
   77441                 : #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
   77442                 : /*
   77443                 : ** Generate a human-readable explanation of an expression list.
   77444                 : */
   77445                 : SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
   77446                 :   int i;
   77447                 :   if( pList==0 || pList->nExpr==0 ){
   77448                 :     sqlite3ExplainPrintf(pOut, "(empty-list)");
   77449                 :     return;
   77450                 :   }else if( pList->nExpr==1 ){
   77451                 :     sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
   77452                 :   }else{
   77453                 :     sqlite3ExplainPush(pOut);
   77454                 :     for(i=0; i<pList->nExpr; i++){
   77455                 :       sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
   77456                 :       sqlite3ExplainPush(pOut);
   77457                 :       sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
   77458                 :       sqlite3ExplainPop(pOut);
   77459                 :       if( i<pList->nExpr-1 ){
   77460                 :         sqlite3ExplainNL(pOut);
   77461                 :       }
   77462                 :     }
   77463                 :     sqlite3ExplainPop(pOut);
   77464                 :   }
   77465                 : }
   77466                 : #endif /* SQLITE_DEBUG */
   77467                 : 
   77468                 : /*
   77469                 : ** Return TRUE if pExpr is an constant expression that is appropriate
   77470                 : ** for factoring out of a loop.  Appropriate expressions are:
   77471                 : **
   77472                 : **    *  Any expression that evaluates to two or more opcodes.
   77473                 : **
   77474                 : **    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, 
   77475                 : **       or OP_Variable that does not need to be placed in a 
   77476                 : **       specific register.
   77477                 : **
   77478                 : ** There is no point in factoring out single-instruction constant
   77479                 : ** expressions that need to be placed in a particular register.  
   77480                 : ** We could factor them out, but then we would end up adding an
   77481                 : ** OP_SCopy instruction to move the value into the correct register
   77482                 : ** later.  We might as well just use the original instruction and
   77483                 : ** avoid the OP_SCopy.
   77484                 : */
   77485          216102 : static int isAppropriateForFactoring(Expr *p){
   77486          216102 :   if( !sqlite3ExprIsConstantNotJoin(p) ){
   77487          158495 :     return 0;  /* Only constant expressions are appropriate for factoring */
   77488                 :   }
   77489           57607 :   if( (p->flags & EP_FixedDest)==0 ){
   77490           55336 :     return 1;  /* Any constant without a fixed destination is appropriate */
   77491                 :   }
   77492            2271 :   while( p->op==TK_UPLUS ) p = p->pLeft;
   77493            2271 :   switch( p->op ){
   77494                 : #ifndef SQLITE_OMIT_BLOB_LITERAL
   77495                 :     case TK_BLOB:
   77496                 : #endif
   77497                 :     case TK_VARIABLE:
   77498                 :     case TK_INTEGER:
   77499                 :     case TK_FLOAT:
   77500                 :     case TK_NULL:
   77501                 :     case TK_STRING: {
   77502                 :       testcase( p->op==TK_BLOB );
   77503                 :       testcase( p->op==TK_VARIABLE );
   77504                 :       testcase( p->op==TK_INTEGER );
   77505                 :       testcase( p->op==TK_FLOAT );
   77506                 :       testcase( p->op==TK_NULL );
   77507                 :       testcase( p->op==TK_STRING );
   77508                 :       /* Single-instruction constants with a fixed destination are
   77509                 :       ** better done in-line.  If we factor them, they will just end
   77510                 :       ** up generating an OP_SCopy to move the value to the destination
   77511                 :       ** register. */
   77512             814 :       return 0;
   77513                 :     }
   77514                 :     case TK_UMINUS: {
   77515               0 :       if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
   77516               0 :         return 0;
   77517                 :       }
   77518               0 :       break;
   77519                 :     }
   77520                 :     default: {
   77521            1457 :       break;
   77522                 :     }
   77523                 :   }
   77524            1457 :   return 1;
   77525                 : }
   77526                 : 
   77527                 : /*
   77528                 : ** If pExpr is a constant expression that is appropriate for
   77529                 : ** factoring out of a loop, then evaluate the expression
   77530                 : ** into a register and convert the expression into a TK_REGISTER
   77531                 : ** expression.
   77532                 : */
   77533          216324 : static int evalConstExpr(Walker *pWalker, Expr *pExpr){
   77534          216324 :   Parse *pParse = pWalker->pParse;
   77535          216324 :   switch( pExpr->op ){
   77536                 :     case TK_IN:
   77537                 :     case TK_REGISTER: {
   77538             222 :       return WRC_Prune;
   77539                 :     }
   77540                 :     case TK_FUNCTION:
   77541                 :     case TK_AGG_FUNCTION:
   77542                 :     case TK_CONST_FUNC: {
   77543                 :       /* The arguments to a function have a fixed destination.
   77544                 :       ** Mark them this way to avoid generated unneeded OP_SCopy
   77545                 :       ** instructions. 
   77546                 :       */
   77547            4312 :       ExprList *pList = pExpr->x.pList;
   77548            4312 :       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   77549            4312 :       if( pList ){
   77550            4312 :         int i = pList->nExpr;
   77551            4312 :         struct ExprList_item *pItem = pList->a;
   77552            9721 :         for(; i>0; i--, pItem++){
   77553            5409 :           if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
   77554                 :         }
   77555                 :       }
   77556            4312 :       break;
   77557                 :     }
   77558                 :   }
   77559          216102 :   if( isAppropriateForFactoring(pExpr) ){
   77560           56793 :     int r1 = ++pParse->nMem;
   77561                 :     int r2;
   77562           56793 :     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
   77563           56793 :     if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1);
   77564           56793 :     pExpr->op2 = pExpr->op;
   77565           56793 :     pExpr->op = TK_REGISTER;
   77566           56793 :     pExpr->iTable = r2;
   77567           56793 :     return WRC_Prune;
   77568                 :   }
   77569          159309 :   return WRC_Continue;
   77570                 : }
   77571                 : 
   77572                 : /*
   77573                 : ** Preevaluate constant subexpressions within pExpr and store the
   77574                 : ** results in registers.  Modify pExpr so that the constant subexpresions
   77575                 : ** are TK_REGISTER opcodes that refer to the precomputed values.
   77576                 : **
   77577                 : ** This routine is a no-op if the jump to the cookie-check code has
   77578                 : ** already occur.  Since the cookie-check jump is generated prior to
   77579                 : ** any other serious processing, this check ensures that there is no
   77580                 : ** way to accidently bypass the constant initializations.
   77581                 : **
   77582                 : ** This routine is also a no-op if the SQLITE_FactorOutConst optimization
   77583                 : ** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
   77584                 : ** interface.  This allows test logic to verify that the same answer is
   77585                 : ** obtained for queries regardless of whether or not constants are
   77586                 : ** precomputed into registers or if they are inserted in-line.
   77587                 : */
   77588           81398 : SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
   77589                 :   Walker w;
   77590           81398 :   if( pParse->cookieGoto ) return;
   77591           52306 :   if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
   77592           52306 :   w.xExprCallback = evalConstExpr;
   77593           52306 :   w.xSelectCallback = 0;
   77594           52306 :   w.pParse = pParse;
   77595           52306 :   sqlite3WalkExpr(&w, pExpr);
   77596                 : }
   77597                 : 
   77598                 : 
   77599                 : /*
   77600                 : ** Generate code that pushes the value of every element of the given
   77601                 : ** expression list into a sequence of registers beginning at target.
   77602                 : **
   77603                 : ** Return the number of elements evaluated.
   77604                 : */
   77605           71602 : SQLITE_PRIVATE int sqlite3ExprCodeExprList(
   77606                 :   Parse *pParse,     /* Parsing context */
   77607                 :   ExprList *pList,   /* The expression list to be coded */
   77608                 :   int target,        /* Where to write results */
   77609                 :   int doHardCopy     /* Make a hard copy of every element */
   77610                 : ){
   77611                 :   struct ExprList_item *pItem;
   77612                 :   int i, n;
   77613           71602 :   assert( pList!=0 );
   77614           71602 :   assert( target>0 );
   77615           71602 :   assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
   77616           71602 :   n = pList->nExpr;
   77617          389124 :   for(pItem=pList->a, i=0; i<n; i++, pItem++){
   77618          317522 :     Expr *pExpr = pItem->pExpr;
   77619          317522 :     int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
   77620          317522 :     if( inReg!=target+i ){
   77621            8660 :       sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
   77622                 :                         inReg, target+i);
   77623                 :     }
   77624                 :   }
   77625           71602 :   return n;
   77626                 : }
   77627                 : 
   77628                 : /*
   77629                 : ** Generate code for a BETWEEN operator.
   77630                 : **
   77631                 : **    x BETWEEN y AND z
   77632                 : **
   77633                 : ** The above is equivalent to 
   77634                 : **
   77635                 : **    x>=y AND x<=z
   77636                 : **
   77637                 : ** Code it as such, taking care to do the common subexpression
   77638                 : ** elementation of x.
   77639                 : */
   77640             409 : static void exprCodeBetween(
   77641                 :   Parse *pParse,    /* Parsing and code generating context */
   77642                 :   Expr *pExpr,      /* The BETWEEN expression */
   77643                 :   int dest,         /* Jump here if the jump is taken */
   77644                 :   int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
   77645                 :   int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
   77646                 : ){
   77647                 :   Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
   77648                 :   Expr compLeft;    /* The  x>=y  term */
   77649                 :   Expr compRight;   /* The  x<=z  term */
   77650                 :   Expr exprX;       /* The  x  subexpression */
   77651             409 :   int regFree1 = 0; /* Temporary use register */
   77652                 : 
   77653             409 :   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   77654             409 :   exprX = *pExpr->pLeft;
   77655             409 :   exprAnd.op = TK_AND;
   77656             409 :   exprAnd.pLeft = &compLeft;
   77657             409 :   exprAnd.pRight = &compRight;
   77658             409 :   compLeft.op = TK_GE;
   77659             409 :   compLeft.pLeft = &exprX;
   77660             409 :   compLeft.pRight = pExpr->x.pList->a[0].pExpr;
   77661             409 :   compRight.op = TK_LE;
   77662             409 :   compRight.pLeft = &exprX;
   77663             409 :   compRight.pRight = pExpr->x.pList->a[1].pExpr;
   77664             409 :   exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
   77665             409 :   exprX.op = TK_REGISTER;
   77666             409 :   if( jumpIfTrue ){
   77667              22 :     sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
   77668                 :   }else{
   77669             387 :     sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
   77670                 :   }
   77671             409 :   sqlite3ReleaseTempReg(pParse, regFree1);
   77672                 : 
   77673                 :   /* Ensure adequate test coverage */
   77674                 :   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
   77675                 :   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
   77676                 :   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
   77677                 :   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
   77678                 :   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
   77679                 :   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
   77680                 :   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
   77681                 :   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
   77682             409 : }
   77683                 : 
   77684                 : /*
   77685                 : ** Generate code for a boolean expression such that a jump is made
   77686                 : ** to the label "dest" if the expression is true but execution
   77687                 : ** continues straight thru if the expression is false.
   77688                 : **
   77689                 : ** If the expression evaluates to NULL (neither true nor false), then
   77690                 : ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
   77691                 : **
   77692                 : ** This code depends on the fact that certain token values (ex: TK_EQ)
   77693                 : ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
   77694                 : ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
   77695                 : ** the make process cause these values to align.  Assert()s in the code
   77696                 : ** below verify that the numbers are aligned correctly.
   77697                 : */
   77698            2713 : SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
   77699            2713 :   Vdbe *v = pParse->pVdbe;
   77700            2713 :   int op = 0;
   77701            2713 :   int regFree1 = 0;
   77702            2713 :   int regFree2 = 0;
   77703                 :   int r1, r2;
   77704                 : 
   77705            2713 :   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
   77706            2713 :   if( NEVER(v==0) )     return;  /* Existance of VDBE checked by caller */
   77707            2713 :   if( NEVER(pExpr==0) ) return;  /* No way this can happen */
   77708            2713 :   op = pExpr->op;
   77709            2713 :   switch( op ){
   77710                 :     case TK_AND: {
   77711             250 :       int d2 = sqlite3VdbeMakeLabel(v);
   77712                 :       testcase( jumpIfNull==0 );
   77713             250 :       sqlite3ExprCachePush(pParse);
   77714             250 :       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
   77715             250 :       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
   77716             250 :       sqlite3VdbeResolveLabel(v, d2);
   77717             250 :       sqlite3ExprCachePop(pParse, 1);
   77718             250 :       break;
   77719                 :     }
   77720                 :     case TK_OR: {
   77721                 :       testcase( jumpIfNull==0 );
   77722             220 :       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
   77723             220 :       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
   77724             220 :       break;
   77725                 :     }
   77726                 :     case TK_NOT: {
   77727                 :       testcase( jumpIfNull==0 );
   77728               2 :       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
   77729               2 :       break;
   77730                 :     }
   77731                 :     case TK_LT:
   77732                 :     case TK_LE:
   77733                 :     case TK_GT:
   77734                 :     case TK_GE:
   77735                 :     case TK_NE:
   77736                 :     case TK_EQ: {
   77737                 :       assert( TK_LT==OP_Lt );
   77738                 :       assert( TK_LE==OP_Le );
   77739                 :       assert( TK_GT==OP_Gt );
   77740                 :       assert( TK_GE==OP_Ge );
   77741                 :       assert( TK_EQ==OP_Eq );
   77742                 :       assert( TK_NE==OP_Ne );
   77743                 :       testcase( op==TK_LT );
   77744                 :       testcase( op==TK_LE );
   77745                 :       testcase( op==TK_GT );
   77746                 :       testcase( op==TK_GE );
   77747                 :       testcase( op==TK_EQ );
   77748                 :       testcase( op==TK_NE );
   77749                 :       testcase( jumpIfNull==0 );
   77750             581 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   77751             581 :       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
   77752             581 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
   77753                 :                   r1, r2, dest, jumpIfNull);
   77754                 :       testcase( regFree1==0 );
   77755                 :       testcase( regFree2==0 );
   77756             581 :       break;
   77757                 :     }
   77758                 :     case TK_IS:
   77759                 :     case TK_ISNOT: {
   77760                 :       testcase( op==TK_IS );
   77761                 :       testcase( op==TK_ISNOT );
   77762               0 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   77763               0 :       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
   77764               0 :       op = (op==TK_IS) ? TK_EQ : TK_NE;
   77765               0 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
   77766                 :                   r1, r2, dest, SQLITE_NULLEQ);
   77767                 :       testcase( regFree1==0 );
   77768                 :       testcase( regFree2==0 );
   77769               0 :       break;
   77770                 :     }
   77771                 :     case TK_ISNULL:
   77772                 :     case TK_NOTNULL: {
   77773                 :       assert( TK_ISNULL==OP_IsNull );
   77774                 :       assert( TK_NOTNULL==OP_NotNull );
   77775                 :       testcase( op==TK_ISNULL );
   77776                 :       testcase( op==TK_NOTNULL );
   77777              22 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   77778              22 :       sqlite3VdbeAddOp2(v, op, r1, dest);
   77779                 :       testcase( regFree1==0 );
   77780              22 :       break;
   77781                 :     }
   77782                 :     case TK_BETWEEN: {
   77783                 :       testcase( jumpIfNull==0 );
   77784              22 :       exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
   77785              22 :       break;
   77786                 :     }
   77787                 : #ifndef SQLITE_OMIT_SUBQUERY
   77788                 :     case TK_IN: {
   77789             511 :       int destIfFalse = sqlite3VdbeMakeLabel(v);
   77790             511 :       int destIfNull = jumpIfNull ? dest : destIfFalse;
   77791             511 :       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
   77792             511 :       sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
   77793             511 :       sqlite3VdbeResolveLabel(v, destIfFalse);
   77794             511 :       break;
   77795                 :     }
   77796                 : #endif
   77797                 :     default: {
   77798            1105 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
   77799            1105 :       sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
   77800                 :       testcase( regFree1==0 );
   77801                 :       testcase( jumpIfNull==0 );
   77802            1105 :       break;
   77803                 :     }
   77804                 :   }
   77805            2713 :   sqlite3ReleaseTempReg(pParse, regFree1);
   77806            2713 :   sqlite3ReleaseTempReg(pParse, regFree2);  
   77807                 : }
   77808                 : 
   77809                 : /*
   77810                 : ** Generate code for a boolean expression such that a jump is made
   77811                 : ** to the label "dest" if the expression is false but execution
   77812                 : ** continues straight thru if the expression is true.
   77813                 : **
   77814                 : ** If the expression evaluates to NULL (neither true nor false) then
   77815                 : ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
   77816                 : ** is 0.
   77817                 : */
   77818           53093 : SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
   77819           53093 :   Vdbe *v = pParse->pVdbe;
   77820           53093 :   int op = 0;
   77821           53093 :   int regFree1 = 0;
   77822           53093 :   int regFree2 = 0;
   77823                 :   int r1, r2;
   77824                 : 
   77825           53093 :   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
   77826           53093 :   if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */
   77827           53093 :   if( pExpr==0 )    return;
   77828                 : 
   77829                 :   /* The value of pExpr->op and op are related as follows:
   77830                 :   **
   77831                 :   **       pExpr->op            op
   77832                 :   **       ---------          ----------
   77833                 :   **       TK_ISNULL          OP_NotNull
   77834                 :   **       TK_NOTNULL         OP_IsNull
   77835                 :   **       TK_NE              OP_Eq
   77836                 :   **       TK_EQ              OP_Ne
   77837                 :   **       TK_GT              OP_Le
   77838                 :   **       TK_LE              OP_Gt
   77839                 :   **       TK_GE              OP_Lt
   77840                 :   **       TK_LT              OP_Ge
   77841                 :   **
   77842                 :   ** For other values of pExpr->op, op is undefined and unused.
   77843                 :   ** The value of TK_ and OP_ constants are arranged such that we
   77844                 :   ** can compute the mapping above using the following expression.
   77845                 :   ** Assert()s verify that the computation is correct.
   77846                 :   */
   77847           48704 :   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
   77848                 : 
   77849                 :   /* Verify correct alignment of TK_ and OP_ constants
   77850                 :   */
   77851           48704 :   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
   77852           48704 :   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
   77853           48704 :   assert( pExpr->op!=TK_NE || op==OP_Eq );
   77854           48704 :   assert( pExpr->op!=TK_EQ || op==OP_Ne );
   77855           48704 :   assert( pExpr->op!=TK_LT || op==OP_Ge );
   77856           48704 :   assert( pExpr->op!=TK_LE || op==OP_Gt );
   77857           48704 :   assert( pExpr->op!=TK_GT || op==OP_Le );
   77858           48704 :   assert( pExpr->op!=TK_GE || op==OP_Lt );
   77859                 : 
   77860           48704 :   switch( pExpr->op ){
   77861                 :     case TK_AND: {
   77862                 :       testcase( jumpIfNull==0 );
   77863             854 :       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
   77864             854 :       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
   77865             854 :       break;
   77866                 :     }
   77867                 :     case TK_OR: {
   77868             374 :       int d2 = sqlite3VdbeMakeLabel(v);
   77869                 :       testcase( jumpIfNull==0 );
   77870             374 :       sqlite3ExprCachePush(pParse);
   77871             374 :       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
   77872             374 :       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
   77873             374 :       sqlite3VdbeResolveLabel(v, d2);
   77874             374 :       sqlite3ExprCachePop(pParse, 1);
   77875             374 :       break;
   77876                 :     }
   77877                 :     case TK_NOT: {
   77878                 :       testcase( jumpIfNull==0 );
   77879            1627 :       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
   77880            1627 :       break;
   77881                 :     }
   77882                 :     case TK_LT:
   77883                 :     case TK_LE:
   77884                 :     case TK_GT:
   77885                 :     case TK_GE:
   77886                 :     case TK_NE:
   77887                 :     case TK_EQ: {
   77888                 :       testcase( op==TK_LT );
   77889                 :       testcase( op==TK_LE );
   77890                 :       testcase( op==TK_GT );
   77891                 :       testcase( op==TK_GE );
   77892                 :       testcase( op==TK_EQ );
   77893                 :       testcase( op==TK_NE );
   77894                 :       testcase( jumpIfNull==0 );
   77895           40791 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   77896           40791 :       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
   77897           40791 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
   77898                 :                   r1, r2, dest, jumpIfNull);
   77899                 :       testcase( regFree1==0 );
   77900                 :       testcase( regFree2==0 );
   77901           40791 :       break;
   77902                 :     }
   77903                 :     case TK_IS:
   77904                 :     case TK_ISNOT: {
   77905                 :       testcase( pExpr->op==TK_IS );
   77906                 :       testcase( pExpr->op==TK_ISNOT );
   77907               0 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   77908               0 :       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
   77909               0 :       op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
   77910               0 :       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
   77911                 :                   r1, r2, dest, SQLITE_NULLEQ);
   77912                 :       testcase( regFree1==0 );
   77913                 :       testcase( regFree2==0 );
   77914               0 :       break;
   77915                 :     }
   77916                 :     case TK_ISNULL:
   77917                 :     case TK_NOTNULL: {
   77918                 :       testcase( op==TK_ISNULL );
   77919                 :       testcase( op==TK_NOTNULL );
   77920            3557 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
   77921            3557 :       sqlite3VdbeAddOp2(v, op, r1, dest);
   77922                 :       testcase( regFree1==0 );
   77923            3557 :       break;
   77924                 :     }
   77925                 :     case TK_BETWEEN: {
   77926                 :       testcase( jumpIfNull==0 );
   77927             387 :       exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
   77928             387 :       break;
   77929                 :     }
   77930                 : #ifndef SQLITE_OMIT_SUBQUERY
   77931                 :     case TK_IN: {
   77932             316 :       if( jumpIfNull ){
   77933             315 :         sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
   77934                 :       }else{
   77935               1 :         int destIfNull = sqlite3VdbeMakeLabel(v);
   77936               1 :         sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
   77937               1 :         sqlite3VdbeResolveLabel(v, destIfNull);
   77938                 :       }
   77939             316 :       break;
   77940                 :     }
   77941                 : #endif
   77942                 :     default: {
   77943             798 :       r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
   77944             798 :       sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
   77945                 :       testcase( regFree1==0 );
   77946                 :       testcase( jumpIfNull==0 );
   77947             798 :       break;
   77948                 :     }
   77949                 :   }
   77950           48704 :   sqlite3ReleaseTempReg(pParse, regFree1);
   77951           48704 :   sqlite3ReleaseTempReg(pParse, regFree2);
   77952                 : }
   77953                 : 
   77954                 : /*
   77955                 : ** Do a deep comparison of two expression trees.  Return 0 if the two
   77956                 : ** expressions are completely identical.  Return 1 if they differ only
   77957                 : ** by a COLLATE operator at the top level.  Return 2 if there are differences
   77958                 : ** other than the top-level COLLATE operator.
   77959                 : **
   77960                 : ** Sometimes this routine will return 2 even if the two expressions
   77961                 : ** really are equivalent.  If we cannot prove that the expressions are
   77962                 : ** identical, we return 2 just to be safe.  So if this routine
   77963                 : ** returns 2, then you do not really know for certain if the two
   77964                 : ** expressions are the same.  But if you get a 0 or 1 return, then you
   77965                 : ** can be sure the expressions are the same.  In the places where
   77966                 : ** this routine is used, it does not hurt to get an extra 2 - that
   77967                 : ** just might result in some slightly slower code.  But returning
   77968                 : ** an incorrect 0 or 1 could lead to a malfunction.
   77969                 : */
   77970             195 : SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){
   77971             195 :   if( pA==0||pB==0 ){
   77972              80 :     return pB==pA ? 0 : 2;
   77973                 :   }
   77974             115 :   assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
   77975             115 :   assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
   77976             115 :   if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
   77977               0 :     return 2;
   77978                 :   }
   77979             115 :   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
   77980              63 :   if( pA->op!=pB->op ) return 2;
   77981              40 :   if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
   77982              40 :   if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
   77983              40 :   if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
   77984              26 :   if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
   77985              26 :   if( ExprHasProperty(pA, EP_IntValue) ){
   77986               0 :     if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
   77987               0 :       return 2;
   77988                 :     }
   77989              26 :   }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
   77990              26 :     if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
   77991              26 :     if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
   77992               0 :       return 2;
   77993                 :     }
   77994                 :   }
   77995              26 :   if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
   77996              26 :   if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
   77997              26 :   return 0;
   77998                 : }
   77999                 : 
   78000                 : /*
   78001                 : ** Compare two ExprList objects.  Return 0 if they are identical and 
   78002                 : ** non-zero if they differ in any way.
   78003                 : **
   78004                 : ** This routine might return non-zero for equivalent ExprLists.  The
   78005                 : ** only consequence will be disabled optimizations.  But this routine
   78006                 : ** must never return 0 if the two ExprList objects are different, or
   78007                 : ** a malfunction will result.
   78008                 : **
   78009                 : ** Two NULL pointers are considered to be the same.  But a NULL pointer
   78010                 : ** always differs from a non-NULL pointer.
   78011                 : */
   78012           60777 : SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
   78013                 :   int i;
   78014           60777 :   if( pA==0 && pB==0 ) return 0;
   78015           22740 :   if( pA==0 || pB==0 ) return 1;
   78016              23 :   if( pA->nExpr!=pB->nExpr ) return 1;
   78017              23 :   for(i=0; i<pA->nExpr; i++){
   78018              23 :     Expr *pExprA = pA->a[i].pExpr;
   78019              23 :     Expr *pExprB = pB->a[i].pExpr;
   78020              23 :     if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
   78021              23 :     if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
   78022                 :   }
   78023               0 :   return 0;
   78024                 : }
   78025                 : 
   78026                 : /*
   78027                 : ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
   78028                 : ** the new element.  Return a negative number if malloc fails.
   78029                 : */
   78030            2512 : static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
   78031                 :   int i;
   78032            5024 :   pInfo->aCol = sqlite3ArrayAllocate(
   78033                 :        db,
   78034            2512 :        pInfo->aCol,
   78035                 :        sizeof(pInfo->aCol[0]),
   78036                 :        3,
   78037                 :        &pInfo->nColumn,
   78038                 :        &pInfo->nColumnAlloc,
   78039                 :        &i
   78040                 :   );
   78041            2512 :   return i;
   78042                 : }    
   78043                 : 
   78044                 : /*
   78045                 : ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
   78046                 : ** the new element.  Return a negative number if malloc fails.
   78047                 : */
   78048            4468 : static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
   78049                 :   int i;
   78050            8936 :   pInfo->aFunc = sqlite3ArrayAllocate(
   78051                 :        db, 
   78052            4468 :        pInfo->aFunc,
   78053                 :        sizeof(pInfo->aFunc[0]),
   78054                 :        3,
   78055                 :        &pInfo->nFunc,
   78056                 :        &pInfo->nFuncAlloc,
   78057                 :        &i
   78058                 :   );
   78059            4468 :   return i;
   78060                 : }    
   78061                 : 
   78062                 : /*
   78063                 : ** This is the xExprCallback for a tree walker.  It is used to
   78064                 : ** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
   78065                 : ** for additional information.
   78066                 : */
   78067           10472 : static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
   78068                 :   int i;
   78069           10472 :   NameContext *pNC = pWalker->u.pNC;
   78070           10472 :   Parse *pParse = pNC->pParse;
   78071           10472 :   SrcList *pSrcList = pNC->pSrcList;
   78072           10472 :   AggInfo *pAggInfo = pNC->pAggInfo;
   78073                 : 
   78074           10472 :   switch( pExpr->op ){
   78075                 :     case TK_AGG_COLUMN:
   78076                 :     case TK_COLUMN: {
   78077                 :       testcase( pExpr->op==TK_AGG_COLUMN );
   78078                 :       testcase( pExpr->op==TK_COLUMN );
   78079                 :       /* Check to see if the column is in one of the tables in the FROM
   78080                 :       ** clause of the aggregate query */
   78081            3300 :       if( ALWAYS(pSrcList!=0) ){
   78082            3300 :         struct SrcList_item *pItem = pSrcList->a;
   78083            4245 :         for(i=0; i<pSrcList->nSrc; i++, pItem++){
   78084                 :           struct AggInfo_col *pCol;
   78085            3478 :           assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
   78086            3478 :           if( pExpr->iTable==pItem->iCursor ){
   78087                 :             /* If we reach this point, it means that pExpr refers to a table
   78088                 :             ** that is in the FROM clause of the aggregate query.  
   78089                 :             **
   78090                 :             ** Make an entry for the column in pAggInfo->aCol[] if there
   78091                 :             ** is not an entry there already.
   78092                 :             */
   78093                 :             int k;
   78094            2533 :             pCol = pAggInfo->aCol;
   78095            2853 :             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
   78096             678 :               if( pCol->iTable==pExpr->iTable &&
   78097             337 :                   pCol->iColumn==pExpr->iColumn ){
   78098              21 :                 break;
   78099                 :               }
   78100                 :             }
   78101            2533 :             if( (k>=pAggInfo->nColumn)
   78102            2512 :              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
   78103                 :             ){
   78104            2512 :               pCol = &pAggInfo->aCol[k];
   78105            2512 :               pCol->pTab = pExpr->pTab;
   78106            2512 :               pCol->iTable = pExpr->iTable;
   78107            2512 :               pCol->iColumn = pExpr->iColumn;
   78108            2512 :               pCol->iMem = ++pParse->nMem;
   78109            2512 :               pCol->iSorterColumn = -1;
   78110            2512 :               pCol->pExpr = pExpr;
   78111            2512 :               if( pAggInfo->pGroupBy ){
   78112                 :                 int j, n;
   78113             252 :                 ExprList *pGB = pAggInfo->pGroupBy;
   78114             252 :                 struct ExprList_item *pTerm = pGB->a;
   78115             252 :                 n = pGB->nExpr;
   78116             399 :                 for(j=0; j<n; j++, pTerm++){
   78117             252 :                   Expr *pE = pTerm->pExpr;
   78118             503 :                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
   78119             251 :                       pE->iColumn==pExpr->iColumn ){
   78120             105 :                     pCol->iSorterColumn = j;
   78121             105 :                     break;
   78122                 :                   }
   78123                 :                 }
   78124                 :               }
   78125            2512 :               if( pCol->iSorterColumn<0 ){
   78126            2407 :                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
   78127                 :               }
   78128                 :             }
   78129                 :             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
   78130                 :             ** because it was there before or because we just created it).
   78131                 :             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
   78132                 :             ** pAggInfo->aCol[] entry.
   78133                 :             */
   78134            2533 :             ExprSetIrreducible(pExpr);
   78135            2533 :             pExpr->pAggInfo = pAggInfo;
   78136            2533 :             pExpr->op = TK_AGG_COLUMN;
   78137            2533 :             pExpr->iAgg = (i16)k;
   78138            2533 :             break;
   78139                 :           } /* endif pExpr->iTable==pItem->iCursor */
   78140                 :         } /* end loop over pSrcList */
   78141                 :       }
   78142            3300 :       return WRC_Prune;
   78143                 :     }
   78144                 :     case TK_AGG_FUNCTION: {
   78145                 :       /* The pNC->nDepth==0 test causes aggregate functions in subqueries
   78146                 :       ** to be ignored */
   78147            4499 :       if( pNC->nDepth==0 ){
   78148                 :         /* Check to see if pExpr is a duplicate of another aggregate 
   78149                 :         ** function that is already in the pAggInfo structure
   78150                 :         */
   78151            4494 :         struct AggInfo_func *pItem = pAggInfo->aFunc;
   78152            4560 :         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
   78153              92 :           if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
   78154              26 :             break;
   78155                 :           }
   78156                 :         }
   78157            4494 :         if( i>=pAggInfo->nFunc ){
   78158                 :           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
   78159                 :           */
   78160            4468 :           u8 enc = ENC(pParse->db);
   78161            4468 :           i = addAggInfoFunc(pParse->db, pAggInfo);
   78162            4468 :           if( i>=0 ){
   78163            4468 :             assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   78164            4468 :             pItem = &pAggInfo->aFunc[i];
   78165            4468 :             pItem->pExpr = pExpr;
   78166            4468 :             pItem->iMem = ++pParse->nMem;
   78167            4468 :             assert( !ExprHasProperty(pExpr, EP_IntValue) );
   78168           20911 :             pItem->pFunc = sqlite3FindFunction(pParse->db,
   78169            8936 :                    pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
   78170            7507 :                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
   78171            4468 :             if( pExpr->flags & EP_Distinct ){
   78172              27 :               pItem->iDistinct = pParse->nTab++;
   78173                 :             }else{
   78174            4441 :               pItem->iDistinct = -1;
   78175                 :             }
   78176                 :           }
   78177                 :         }
   78178                 :         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
   78179                 :         */
   78180            4494 :         assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
   78181            4494 :         ExprSetIrreducible(pExpr);
   78182            4494 :         pExpr->iAgg = (i16)i;
   78183            4494 :         pExpr->pAggInfo = pAggInfo;
   78184            4494 :         return WRC_Prune;
   78185                 :       }
   78186                 :     }
   78187                 :   }
   78188            2678 :   return WRC_Continue;
   78189                 : }
   78190             762 : static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
   78191             762 :   NameContext *pNC = pWalker->u.pNC;
   78192             762 :   if( pNC->nDepth==0 ){
   78193             381 :     pNC->nDepth++;
   78194             381 :     sqlite3WalkSelect(pWalker, pSelect);
   78195             381 :     pNC->nDepth--;
   78196             381 :     return WRC_Prune;
   78197                 :   }else{
   78198             381 :     return WRC_Continue;
   78199                 :   }
   78200                 : }
   78201                 : 
   78202                 : /*
   78203                 : ** Analyze the given expression looking for aggregate functions and
   78204                 : ** for variables that need to be added to the pParse->aAgg[] array.
   78205                 : ** Make additional entries to the pParse->aAgg[] array as necessary.
   78206                 : **
   78207                 : ** This routine should only be called after the expression has been
   78208                 : ** analyzed by sqlite3ResolveExprNames().
   78209                 : */
   78210            8194 : SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
   78211                 :   Walker w;
   78212            8194 :   w.xExprCallback = analyzeAggregate;
   78213            8194 :   w.xSelectCallback = analyzeAggregatesInSelect;
   78214            8194 :   w.u.pNC = pNC;
   78215            8194 :   assert( pNC->pSrcList!=0 );
   78216            8194 :   sqlite3WalkExpr(&w, pExpr);
   78217            8194 : }
   78218                 : 
   78219                 : /*
   78220                 : ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
   78221                 : ** expression list.  Return the number of errors.
   78222                 : **
   78223                 : ** If an error is found, the analysis is cut short.
   78224                 : */
   78225           13352 : SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
   78226                 :   struct ExprList_item *pItem;
   78227                 :   int i;
   78228           13352 :   if( pList ){
   78229           15631 :     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
   78230            8141 :       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
   78231                 :     }
   78232                 :   }
   78233           13352 : }
   78234                 : 
   78235                 : /*
   78236                 : ** Allocate a single new register for use to hold some intermediate result.
   78237                 : */
   78238          241731 : SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
   78239          241731 :   if( pParse->nTempReg==0 ){
   78240          143664 :     return ++pParse->nMem;
   78241                 :   }
   78242           98067 :   return pParse->aTempReg[--pParse->nTempReg];
   78243                 : }
   78244                 : 
   78245                 : /*
   78246                 : ** Deallocate a register, making available for reuse for some other
   78247                 : ** purpose.
   78248                 : **
   78249                 : ** If a register is currently being used by the column cache, then
   78250                 : ** the dallocation is deferred until the column cache line that uses
   78251                 : ** the register becomes stale.
   78252                 : */
   78253         1699169 : SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
   78254         1699169 :   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
   78255                 :     int i;
   78256                 :     struct yColCache *p;
   78257         1784443 :     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
   78258         1630829 :       if( p->iReg==iReg ){
   78259           66671 :         p->tempReg = 1;
   78260           66671 :         return;
   78261                 :       }
   78262                 :     }
   78263          153614 :     pParse->aTempReg[pParse->nTempReg++] = iReg;
   78264                 :   }
   78265                 : }
   78266                 : 
   78267                 : /*
   78268                 : ** Allocate or deallocate a block of nReg consecutive registers
   78269                 : */
   78270           50944 : SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
   78271                 :   int i, n;
   78272           50944 :   i = pParse->iRangeReg;
   78273           50944 :   n = pParse->nRangeReg;
   78274           50944 :   if( nReg<=n ){
   78275           18470 :     assert( !usedAsColumnCache(pParse, i, i+n-1) );
   78276           18470 :     pParse->iRangeReg += nReg;
   78277           18470 :     pParse->nRangeReg -= nReg;
   78278                 :   }else{
   78279           32474 :     i = pParse->nMem+1;
   78280           32474 :     pParse->nMem += nReg;
   78281                 :   }
   78282           50944 :   return i;
   78283                 : }
   78284           51735 : SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
   78285           51735 :   sqlite3ExprCacheRemove(pParse, iReg, nReg);
   78286           51735 :   if( nReg>pParse->nRangeReg ){
   78287           48662 :     pParse->nRangeReg = nReg;
   78288           48662 :     pParse->iRangeReg = iReg;
   78289                 :   }
   78290           51735 : }
   78291                 : 
   78292                 : /*
   78293                 : ** Mark all temporary registers as being unavailable for reuse.
   78294                 : */
   78295              59 : SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
   78296              59 :   pParse->nTempReg = 0;
   78297              59 :   pParse->nRangeReg = 0;
   78298              59 : }
   78299                 : 
   78300                 : /************** End of expr.c ************************************************/
   78301                 : /************** Begin file alter.c *******************************************/
   78302                 : /*
   78303                 : ** 2005 February 15
   78304                 : **
   78305                 : ** The author disclaims copyright to this source code.  In place of
   78306                 : ** a legal notice, here is a blessing:
   78307                 : **
   78308                 : **    May you do good and not evil.
   78309                 : **    May you find forgiveness for yourself and forgive others.
   78310                 : **    May you share freely, never taking more than you give.
   78311                 : **
   78312                 : *************************************************************************
   78313                 : ** This file contains C code routines that used to generate VDBE code
   78314                 : ** that implements the ALTER TABLE command.
   78315                 : */
   78316                 : 
   78317                 : /*
   78318                 : ** The code in this file only exists if we are not omitting the
   78319                 : ** ALTER TABLE logic from the build.
   78320                 : */
   78321                 : #ifndef SQLITE_OMIT_ALTERTABLE
   78322                 : 
   78323                 : 
   78324                 : /*
   78325                 : ** This function is used by SQL generated to implement the 
   78326                 : ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
   78327                 : ** CREATE INDEX command. The second is a table name. The table name in 
   78328                 : ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
   78329                 : ** argument and the result returned. Examples:
   78330                 : **
   78331                 : ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
   78332                 : **     -> 'CREATE TABLE def(a, b, c)'
   78333                 : **
   78334                 : ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
   78335                 : **     -> 'CREATE INDEX i ON def(a, b, c)'
   78336                 : */
   78337               0 : static void renameTableFunc(
   78338                 :   sqlite3_context *context,
   78339                 :   int NotUsed,
   78340                 :   sqlite3_value **argv
   78341                 : ){
   78342               0 :   unsigned char const *zSql = sqlite3_value_text(argv[0]);
   78343               0 :   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
   78344                 : 
   78345                 :   int token;
   78346                 :   Token tname;
   78347               0 :   unsigned char const *zCsr = zSql;
   78348               0 :   int len = 0;
   78349                 :   char *zRet;
   78350                 : 
   78351               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   78352                 : 
   78353                 :   UNUSED_PARAMETER(NotUsed);
   78354                 : 
   78355                 :   /* The principle used to locate the table name in the CREATE TABLE 
   78356                 :   ** statement is that the table name is the first non-space token that
   78357                 :   ** is immediately followed by a TK_LP or TK_USING token.
   78358                 :   */
   78359               0 :   if( zSql ){
   78360                 :     do {
   78361               0 :       if( !*zCsr ){
   78362                 :         /* Ran out of input before finding an opening bracket. Return NULL. */
   78363               0 :         return;
   78364                 :       }
   78365                 : 
   78366                 :       /* Store the token that zCsr points to in tname. */
   78367               0 :       tname.z = (char*)zCsr;
   78368               0 :       tname.n = len;
   78369                 : 
   78370                 :       /* Advance zCsr to the next token. Store that token type in 'token',
   78371                 :       ** and its length in 'len' (to be used next iteration of this loop).
   78372                 :       */
   78373                 :       do {
   78374               0 :         zCsr += len;
   78375               0 :         len = sqlite3GetToken(zCsr, &token);
   78376               0 :       } while( token==TK_SPACE );
   78377               0 :       assert( len>0 );
   78378               0 :     } while( token!=TK_LP && token!=TK_USING );
   78379                 : 
   78380               0 :     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
   78381               0 :        zTableName, tname.z+tname.n);
   78382               0 :     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
   78383                 :   }
   78384                 : }
   78385                 : 
   78386                 : /*
   78387                 : ** This C function implements an SQL user function that is used by SQL code
   78388                 : ** generated by the ALTER TABLE ... RENAME command to modify the definition
   78389                 : ** of any foreign key constraints that use the table being renamed as the 
   78390                 : ** parent table. It is passed three arguments:
   78391                 : **
   78392                 : **   1) The complete text of the CREATE TABLE statement being modified,
   78393                 : **   2) The old name of the table being renamed, and
   78394                 : **   3) The new name of the table being renamed.
   78395                 : **
   78396                 : ** It returns the new CREATE TABLE statement. For example:
   78397                 : **
   78398                 : **   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
   78399                 : **       -> 'CREATE TABLE t1(a REFERENCES t3)'
   78400                 : */
   78401                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   78402               0 : static void renameParentFunc(
   78403                 :   sqlite3_context *context,
   78404                 :   int NotUsed,
   78405                 :   sqlite3_value **argv
   78406                 : ){
   78407               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   78408               0 :   char *zOutput = 0;
   78409                 :   char *zResult;
   78410               0 :   unsigned char const *zInput = sqlite3_value_text(argv[0]);
   78411               0 :   unsigned char const *zOld = sqlite3_value_text(argv[1]);
   78412               0 :   unsigned char const *zNew = sqlite3_value_text(argv[2]);
   78413                 : 
   78414                 :   unsigned const char *z;         /* Pointer to token */
   78415                 :   int n;                          /* Length of token z */
   78416                 :   int token;                      /* Type of token */
   78417                 : 
   78418                 :   UNUSED_PARAMETER(NotUsed);
   78419               0 :   for(z=zInput; *z; z=z+n){
   78420               0 :     n = sqlite3GetToken(z, &token);
   78421               0 :     if( token==TK_REFERENCES ){
   78422                 :       char *zParent;
   78423                 :       do {
   78424               0 :         z += n;
   78425               0 :         n = sqlite3GetToken(z, &token);
   78426               0 :       }while( token==TK_SPACE );
   78427                 : 
   78428               0 :       zParent = sqlite3DbStrNDup(db, (const char *)z, n);
   78429               0 :       if( zParent==0 ) break;
   78430               0 :       sqlite3Dequote(zParent);
   78431               0 :       if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
   78432               0 :         char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", 
   78433                 :             (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
   78434                 :         );
   78435               0 :         sqlite3DbFree(db, zOutput);
   78436               0 :         zOutput = zOut;
   78437               0 :         zInput = &z[n];
   78438                 :       }
   78439               0 :       sqlite3DbFree(db, zParent);
   78440                 :     }
   78441                 :   }
   78442                 : 
   78443               0 :   zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), 
   78444               0 :   sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
   78445               0 :   sqlite3DbFree(db, zOutput);
   78446               0 : }
   78447                 : #endif
   78448                 : 
   78449                 : #ifndef SQLITE_OMIT_TRIGGER
   78450                 : /* This function is used by SQL generated to implement the
   78451                 : ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
   78452                 : ** statement. The second is a table name. The table name in the CREATE 
   78453                 : ** TRIGGER statement is replaced with the third argument and the result 
   78454                 : ** returned. This is analagous to renameTableFunc() above, except for CREATE
   78455                 : ** TRIGGER, not CREATE INDEX and CREATE TABLE.
   78456                 : */
   78457               0 : static void renameTriggerFunc(
   78458                 :   sqlite3_context *context,
   78459                 :   int NotUsed,
   78460                 :   sqlite3_value **argv
   78461                 : ){
   78462               0 :   unsigned char const *zSql = sqlite3_value_text(argv[0]);
   78463               0 :   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
   78464                 : 
   78465                 :   int token;
   78466                 :   Token tname;
   78467               0 :   int dist = 3;
   78468               0 :   unsigned char const *zCsr = zSql;
   78469               0 :   int len = 0;
   78470                 :   char *zRet;
   78471               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   78472                 : 
   78473                 :   UNUSED_PARAMETER(NotUsed);
   78474                 : 
   78475                 :   /* The principle used to locate the table name in the CREATE TRIGGER 
   78476                 :   ** statement is that the table name is the first token that is immediatedly
   78477                 :   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
   78478                 :   ** of TK_WHEN, TK_BEGIN or TK_FOR.
   78479                 :   */
   78480               0 :   if( zSql ){
   78481                 :     do {
   78482                 : 
   78483               0 :       if( !*zCsr ){
   78484                 :         /* Ran out of input before finding the table name. Return NULL. */
   78485               0 :         return;
   78486                 :       }
   78487                 : 
   78488                 :       /* Store the token that zCsr points to in tname. */
   78489               0 :       tname.z = (char*)zCsr;
   78490               0 :       tname.n = len;
   78491                 : 
   78492                 :       /* Advance zCsr to the next token. Store that token type in 'token',
   78493                 :       ** and its length in 'len' (to be used next iteration of this loop).
   78494                 :       */
   78495                 :       do {
   78496               0 :         zCsr += len;
   78497               0 :         len = sqlite3GetToken(zCsr, &token);
   78498               0 :       }while( token==TK_SPACE );
   78499               0 :       assert( len>0 );
   78500                 : 
   78501                 :       /* Variable 'dist' stores the number of tokens read since the most
   78502                 :       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
   78503                 :       ** token is read and 'dist' equals 2, the condition stated above
   78504                 :       ** to be met.
   78505                 :       **
   78506                 :       ** Note that ON cannot be a database, table or column name, so
   78507                 :       ** there is no need to worry about syntax like 
   78508                 :       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
   78509                 :       */
   78510               0 :       dist++;
   78511               0 :       if( token==TK_DOT || token==TK_ON ){
   78512               0 :         dist = 0;
   78513                 :       }
   78514               0 :     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
   78515                 : 
   78516                 :     /* Variable tname now contains the token that is the old table-name
   78517                 :     ** in the CREATE TRIGGER statement.
   78518                 :     */
   78519               0 :     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
   78520               0 :        zTableName, tname.z+tname.n);
   78521               0 :     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
   78522                 :   }
   78523                 : }
   78524                 : #endif   /* !SQLITE_OMIT_TRIGGER */
   78525                 : 
   78526                 : /*
   78527                 : ** Register built-in functions used to help implement ALTER TABLE
   78528                 : */
   78529             806 : SQLITE_PRIVATE void sqlite3AlterFunctions(void){
   78530                 :   static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
   78531                 :     FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
   78532                 : #ifndef SQLITE_OMIT_TRIGGER
   78533                 :     FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
   78534                 : #endif
   78535                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   78536                 :     FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
   78537                 : #endif
   78538                 :   };
   78539                 :   int i;
   78540             806 :   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
   78541             806 :   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
   78542                 : 
   78543            3224 :   for(i=0; i<ArraySize(aAlterTableFuncs); i++){
   78544            2418 :     sqlite3FuncDefInsert(pHash, &aFunc[i]);
   78545                 :   }
   78546             806 : }
   78547                 : 
   78548                 : /*
   78549                 : ** This function is used to create the text of expressions of the form:
   78550                 : **
   78551                 : **   name=<constant1> OR name=<constant2> OR ...
   78552                 : **
   78553                 : ** If argument zWhere is NULL, then a pointer string containing the text 
   78554                 : ** "name=<constant>" is returned, where <constant> is the quoted version
   78555                 : ** of the string passed as argument zConstant. The returned buffer is
   78556                 : ** allocated using sqlite3DbMalloc(). It is the responsibility of the
   78557                 : ** caller to ensure that it is eventually freed.
   78558                 : **
   78559                 : ** If argument zWhere is not NULL, then the string returned is 
   78560                 : ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
   78561                 : ** In this case zWhere is passed to sqlite3DbFree() before returning.
   78562                 : ** 
   78563                 : */
   78564               0 : static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
   78565                 :   char *zNew;
   78566               0 :   if( !zWhere ){
   78567               0 :     zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
   78568                 :   }else{
   78569               0 :     zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
   78570               0 :     sqlite3DbFree(db, zWhere);
   78571                 :   }
   78572               0 :   return zNew;
   78573                 : }
   78574                 : 
   78575                 : #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   78576                 : /*
   78577                 : ** Generate the text of a WHERE expression which can be used to select all
   78578                 : ** tables that have foreign key constraints that refer to table pTab (i.e.
   78579                 : ** constraints for which pTab is the parent table) from the sqlite_master
   78580                 : ** table.
   78581                 : */
   78582               0 : static char *whereForeignKeys(Parse *pParse, Table *pTab){
   78583                 :   FKey *p;
   78584               0 :   char *zWhere = 0;
   78585               0 :   for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   78586               0 :     zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
   78587                 :   }
   78588               0 :   return zWhere;
   78589                 : }
   78590                 : #endif
   78591                 : 
   78592                 : /*
   78593                 : ** Generate the text of a WHERE expression which can be used to select all
   78594                 : ** temporary triggers on table pTab from the sqlite_temp_master table. If
   78595                 : ** table pTab has no temporary triggers, or is itself stored in the 
   78596                 : ** temporary database, NULL is returned.
   78597                 : */
   78598             121 : static char *whereTempTriggers(Parse *pParse, Table *pTab){
   78599                 :   Trigger *pTrig;
   78600             121 :   char *zWhere = 0;
   78601             121 :   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
   78602                 : 
   78603                 :   /* If the table is not located in the temp-db (in which case NULL is 
   78604                 :   ** returned, loop through the tables list of triggers. For each trigger
   78605                 :   ** that is not part of the temp-db schema, add a clause to the WHERE 
   78606                 :   ** expression being built up in zWhere.
   78607                 :   */
   78608             121 :   if( pTab->pSchema!=pTempSchema ){
   78609             121 :     sqlite3 *db = pParse->db;
   78610             124 :     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
   78611               3 :       if( pTrig->pSchema==pTempSchema ){
   78612               0 :         zWhere = whereOrName(db, zWhere, pTrig->zName);
   78613                 :       }
   78614                 :     }
   78615                 :   }
   78616             121 :   if( zWhere ){
   78617               0 :     char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
   78618               0 :     sqlite3DbFree(pParse->db, zWhere);
   78619               0 :     zWhere = zNew;
   78620                 :   }
   78621             121 :   return zWhere;
   78622                 : }
   78623                 : 
   78624                 : /*
   78625                 : ** Generate code to drop and reload the internal representation of table
   78626                 : ** pTab from the database, including triggers and temporary triggers.
   78627                 : ** Argument zName is the name of the table in the database schema at
   78628                 : ** the time the generated code is executed. This can be different from
   78629                 : ** pTab->zName if this function is being called to code part of an 
   78630                 : ** "ALTER TABLE RENAME TO" statement.
   78631                 : */
   78632             121 : static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
   78633                 :   Vdbe *v;
   78634                 :   char *zWhere;
   78635                 :   int iDb;                   /* Index of database containing pTab */
   78636                 : #ifndef SQLITE_OMIT_TRIGGER
   78637                 :   Trigger *pTrig;
   78638                 : #endif
   78639                 : 
   78640             121 :   v = sqlite3GetVdbe(pParse);
   78641             121 :   if( NEVER(v==0) ) return;
   78642             121 :   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   78643             121 :   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   78644             121 :   assert( iDb>=0 );
   78645                 : 
   78646                 : #ifndef SQLITE_OMIT_TRIGGER
   78647                 :   /* Drop any table triggers from the internal schema. */
   78648             124 :   for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
   78649               3 :     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
   78650               3 :     assert( iTrigDb==iDb || iTrigDb==1 );
   78651               3 :     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
   78652                 :   }
   78653                 : #endif
   78654                 : 
   78655                 :   /* Drop the table and index from the internal schema.  */
   78656             121 :   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
   78657                 : 
   78658                 :   /* Reload the table, index and permanent trigger schemas. */
   78659             121 :   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
   78660             121 :   if( !zWhere ) return;
   78661             121 :   sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
   78662                 : 
   78663                 : #ifndef SQLITE_OMIT_TRIGGER
   78664                 :   /* Now, if the table is not stored in the temp database, reload any temp 
   78665                 :   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
   78666                 :   */
   78667             121 :   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
   78668               0 :     sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
   78669                 :   }
   78670                 : #endif
   78671                 : }
   78672                 : 
   78673                 : /*
   78674                 : ** Parameter zName is the name of a table that is about to be altered
   78675                 : ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
   78676                 : ** If the table is a system table, this function leaves an error message
   78677                 : ** in pParse->zErr (system tables may not be altered) and returns non-zero.
   78678                 : **
   78679                 : ** Or, if zName is not a system table, zero is returned.
   78680                 : */
   78681             139 : static int isSystemTable(Parse *pParse, const char *zName){
   78682             139 :   if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
   78683               0 :     sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
   78684               0 :     return 1;
   78685                 :   }
   78686             139 :   return 0;
   78687                 : }
   78688                 : 
   78689                 : /*
   78690                 : ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
   78691                 : ** command. 
   78692                 : */
   78693               0 : SQLITE_PRIVATE void sqlite3AlterRenameTable(
   78694                 :   Parse *pParse,            /* Parser context. */
   78695                 :   SrcList *pSrc,            /* The table to rename. */
   78696                 :   Token *pName              /* The new table name. */
   78697                 : ){
   78698                 :   int iDb;                  /* Database that contains the table */
   78699                 :   char *zDb;                /* Name of database iDb */
   78700                 :   Table *pTab;              /* Table being renamed */
   78701               0 :   char *zName = 0;          /* NULL-terminated version of pName */ 
   78702               0 :   sqlite3 *db = pParse->db; /* Database connection */
   78703                 :   int nTabName;             /* Number of UTF-8 characters in zTabName */
   78704                 :   const char *zTabName;     /* Original name of the table */
   78705                 :   Vdbe *v;
   78706                 : #ifndef SQLITE_OMIT_TRIGGER
   78707               0 :   char *zWhere = 0;         /* Where clause to locate temp triggers */
   78708                 : #endif
   78709               0 :   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
   78710                 :   int savedDbFlags;         /* Saved value of db->flags */
   78711                 : 
   78712               0 :   savedDbFlags = db->flags;  
   78713               0 :   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
   78714               0 :   assert( pSrc->nSrc==1 );
   78715               0 :   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   78716                 : 
   78717               0 :   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   78718               0 :   if( !pTab ) goto exit_rename_table;
   78719               0 :   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   78720               0 :   zDb = db->aDb[iDb].zName;
   78721               0 :   db->flags |= SQLITE_PreferBuiltin;
   78722                 : 
   78723                 :   /* Get a NULL terminated version of the new table name. */
   78724               0 :   zName = sqlite3NameFromToken(db, pName);
   78725               0 :   if( !zName ) goto exit_rename_table;
   78726                 : 
   78727                 :   /* Check that a table or index named 'zName' does not already exist
   78728                 :   ** in database iDb. If so, this is an error.
   78729                 :   */
   78730               0 :   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
   78731               0 :     sqlite3ErrorMsg(pParse, 
   78732                 :         "there is already another table or index with this name: %s", zName);
   78733               0 :     goto exit_rename_table;
   78734                 :   }
   78735                 : 
   78736                 :   /* Make sure it is not a system table being altered, or a reserved name
   78737                 :   ** that the table is being renamed to.
   78738                 :   */
   78739               0 :   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
   78740               0 :     goto exit_rename_table;
   78741                 :   }
   78742               0 :   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
   78743                 :     exit_rename_table;
   78744                 :   }
   78745                 : 
   78746                 : #ifndef SQLITE_OMIT_VIEW
   78747               0 :   if( pTab->pSelect ){
   78748               0 :     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
   78749               0 :     goto exit_rename_table;
   78750                 :   }
   78751                 : #endif
   78752                 : 
   78753                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   78754                 :   /* Invoke the authorization callback. */
   78755               0 :   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
   78756               0 :     goto exit_rename_table;
   78757                 :   }
   78758                 : #endif
   78759                 : 
   78760                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   78761               0 :   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
   78762               0 :     goto exit_rename_table;
   78763                 :   }
   78764               0 :   if( IsVirtual(pTab) ){
   78765               0 :     pVTab = sqlite3GetVTable(db, pTab);
   78766               0 :     if( pVTab->pVtab->pModule->xRename==0 ){
   78767               0 :       pVTab = 0;
   78768                 :     }
   78769                 :   }
   78770                 : #endif
   78771                 : 
   78772                 :   /* Begin a transaction and code the VerifyCookie for database iDb. 
   78773                 :   ** Then modify the schema cookie (since the ALTER TABLE modifies the
   78774                 :   ** schema). Open a statement transaction if the table is a virtual
   78775                 :   ** table.
   78776                 :   */
   78777               0 :   v = sqlite3GetVdbe(pParse);
   78778               0 :   if( v==0 ){
   78779               0 :     goto exit_rename_table;
   78780                 :   }
   78781               0 :   sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
   78782               0 :   sqlite3ChangeCookie(pParse, iDb);
   78783                 : 
   78784                 :   /* If this is a virtual table, invoke the xRename() function if
   78785                 :   ** one is defined. The xRename() callback will modify the names
   78786                 :   ** of any resources used by the v-table implementation (including other
   78787                 :   ** SQLite tables) that are identified by the name of the virtual table.
   78788                 :   */
   78789                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   78790               0 :   if( pVTab ){
   78791               0 :     int i = ++pParse->nMem;
   78792               0 :     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
   78793               0 :     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
   78794               0 :     sqlite3MayAbort(pParse);
   78795                 :   }
   78796                 : #endif
   78797                 : 
   78798                 :   /* figure out how many UTF-8 characters are in zName */
   78799               0 :   zTabName = pTab->zName;
   78800               0 :   nTabName = sqlite3Utf8CharLen(zTabName, -1);
   78801                 : 
   78802                 : #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   78803               0 :   if( db->flags&SQLITE_ForeignKeys ){
   78804                 :     /* If foreign-key support is enabled, rewrite the CREATE TABLE 
   78805                 :     ** statements corresponding to all child tables of foreign key constraints
   78806                 :     ** for which the renamed table is the parent table.  */
   78807               0 :     if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
   78808               0 :       sqlite3NestedParse(pParse, 
   78809                 :           "UPDATE \"%w\".%s SET "
   78810                 :               "sql = sqlite_rename_parent(sql, %Q, %Q) "
   78811                 :               "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
   78812               0 :       sqlite3DbFree(db, zWhere);
   78813                 :     }
   78814                 :   }
   78815                 : #endif
   78816                 : 
   78817                 :   /* Modify the sqlite_master table to use the new table name. */
   78818               0 :   sqlite3NestedParse(pParse,
   78819                 :       "UPDATE %Q.%s SET "
   78820                 : #ifdef SQLITE_OMIT_TRIGGER
   78821                 :           "sql = sqlite_rename_table(sql, %Q), "
   78822                 : #else
   78823                 :           "sql = CASE "
   78824                 :             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
   78825                 :             "ELSE sqlite_rename_table(sql, %Q) END, "
   78826                 : #endif
   78827                 :           "tbl_name = %Q, "
   78828                 :           "name = CASE "
   78829                 :             "WHEN type='table' THEN %Q "
   78830                 :             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
   78831                 :              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
   78832                 :             "ELSE name END "
   78833                 :       "WHERE tbl_name=%Q AND "
   78834                 :           "(type='table' OR type='index' OR type='trigger');", 
   78835                 :       zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
   78836                 : #ifndef SQLITE_OMIT_TRIGGER
   78837                 :       zName,
   78838                 : #endif
   78839                 :       zName, nTabName, zTabName
   78840                 :   );
   78841                 : 
   78842                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   78843                 :   /* If the sqlite_sequence table exists in this database, then update 
   78844                 :   ** it with the new table name.
   78845                 :   */
   78846               0 :   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
   78847               0 :     sqlite3NestedParse(pParse,
   78848                 :         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
   78849                 :         zDb, zName, pTab->zName);
   78850                 :   }
   78851                 : #endif
   78852                 : 
   78853                 : #ifndef SQLITE_OMIT_TRIGGER
   78854                 :   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
   78855                 :   ** table. Don't do this if the table being ALTERed is itself located in
   78856                 :   ** the temp database.
   78857                 :   */
   78858               0 :   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
   78859               0 :     sqlite3NestedParse(pParse, 
   78860                 :         "UPDATE sqlite_temp_master SET "
   78861                 :             "sql = sqlite_rename_trigger(sql, %Q), "
   78862                 :             "tbl_name = %Q "
   78863                 :             "WHERE %s;", zName, zName, zWhere);
   78864               0 :     sqlite3DbFree(db, zWhere);
   78865                 :   }
   78866                 : #endif
   78867                 : 
   78868                 : #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   78869               0 :   if( db->flags&SQLITE_ForeignKeys ){
   78870                 :     FKey *p;
   78871               0 :     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   78872               0 :       Table *pFrom = p->pFrom;
   78873               0 :       if( pFrom!=pTab ){
   78874               0 :         reloadTableSchema(pParse, p->pFrom, pFrom->zName);
   78875                 :       }
   78876                 :     }
   78877                 :   }
   78878                 : #endif
   78879                 : 
   78880                 :   /* Drop and reload the internal table schema. */
   78881               0 :   reloadTableSchema(pParse, pTab, zName);
   78882                 : 
   78883                 : exit_rename_table:
   78884               0 :   sqlite3SrcListDelete(db, pSrc);
   78885               0 :   sqlite3DbFree(db, zName);
   78886               0 :   db->flags = savedDbFlags;
   78887               0 : }
   78888                 : 
   78889                 : 
   78890                 : /*
   78891                 : ** Generate code to make sure the file format number is at least minFormat.
   78892                 : ** The generated code will increase the file format number if necessary.
   78893                 : */
   78894             121 : SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
   78895                 :   Vdbe *v;
   78896             121 :   v = sqlite3GetVdbe(pParse);
   78897                 :   /* The VDBE should have been allocated before this routine is called.
   78898                 :   ** If that allocation failed, we would have quit before reaching this
   78899                 :   ** point */
   78900             121 :   if( ALWAYS(v) ){
   78901             121 :     int r1 = sqlite3GetTempReg(pParse);
   78902             121 :     int r2 = sqlite3GetTempReg(pParse);
   78903                 :     int j1;
   78904             121 :     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
   78905             121 :     sqlite3VdbeUsesBtree(v, iDb);
   78906             121 :     sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
   78907             121 :     j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
   78908             121 :     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
   78909             121 :     sqlite3VdbeJumpHere(v, j1);
   78910             121 :     sqlite3ReleaseTempReg(pParse, r1);
   78911             121 :     sqlite3ReleaseTempReg(pParse, r2);
   78912                 :   }
   78913             121 : }
   78914                 : 
   78915                 : /*
   78916                 : ** This function is called after an "ALTER TABLE ... ADD" statement
   78917                 : ** has been parsed. Argument pColDef contains the text of the new
   78918                 : ** column definition.
   78919                 : **
   78920                 : ** The Table structure pParse->pNewTable was extended to include
   78921                 : ** the new column during parsing.
   78922                 : */
   78923             121 : SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
   78924                 :   Table *pNew;              /* Copy of pParse->pNewTable */
   78925                 :   Table *pTab;              /* Table being altered */
   78926                 :   int iDb;                  /* Database number */
   78927                 :   const char *zDb;          /* Database name */
   78928                 :   const char *zTab;         /* Table name */
   78929                 :   char *zCol;               /* Null-terminated column definition */
   78930                 :   Column *pCol;             /* The new column */
   78931                 :   Expr *pDflt;              /* Default value for the new column */
   78932                 :   sqlite3 *db;              /* The database connection; */
   78933                 : 
   78934             121 :   db = pParse->db;
   78935             121 :   if( pParse->nErr || db->mallocFailed ) return;
   78936             121 :   pNew = pParse->pNewTable;
   78937             121 :   assert( pNew );
   78938                 : 
   78939             121 :   assert( sqlite3BtreeHoldsAllMutexes(db) );
   78940             121 :   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
   78941             121 :   zDb = db->aDb[iDb].zName;
   78942             121 :   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
   78943             121 :   pCol = &pNew->aCol[pNew->nCol-1];
   78944             121 :   pDflt = pCol->pDflt;
   78945             121 :   pTab = sqlite3FindTable(db, zTab, zDb);
   78946             121 :   assert( pTab );
   78947                 : 
   78948                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   78949                 :   /* Invoke the authorization callback. */
   78950             121 :   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
   78951               0 :     return;
   78952                 :   }
   78953                 : #endif
   78954                 : 
   78955                 :   /* If the default value for the new column was specified with a 
   78956                 :   ** literal NULL, then set pDflt to 0. This simplifies checking
   78957                 :   ** for an SQL NULL default below.
   78958                 :   */
   78959             121 :   if( pDflt && pDflt->op==TK_NULL ){
   78960               0 :     pDflt = 0;
   78961                 :   }
   78962                 : 
   78963                 :   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
   78964                 :   ** If there is a NOT NULL constraint, then the default value for the
   78965                 :   ** column must not be NULL.
   78966                 :   */
   78967             121 :   if( pCol->isPrimKey ){
   78968               0 :     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
   78969               0 :     return;
   78970                 :   }
   78971             121 :   if( pNew->pIndex ){
   78972               0 :     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
   78973               0 :     return;
   78974                 :   }
   78975             121 :   if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
   78976               0 :     sqlite3ErrorMsg(pParse, 
   78977                 :         "Cannot add a REFERENCES column with non-NULL default value");
   78978               0 :     return;
   78979                 :   }
   78980             121 :   if( pCol->notNull && !pDflt ){
   78981               0 :     sqlite3ErrorMsg(pParse, 
   78982                 :         "Cannot add a NOT NULL column with default value NULL");
   78983               0 :     return;
   78984                 :   }
   78985                 : 
   78986                 :   /* Ensure the default expression is something that sqlite3ValueFromExpr()
   78987                 :   ** can handle (i.e. not CURRENT_TIME etc.)
   78988                 :   */
   78989             121 :   if( pDflt ){
   78990                 :     sqlite3_value *pVal;
   78991              37 :     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
   78992               0 :       db->mallocFailed = 1;
   78993               0 :       return;
   78994                 :     }
   78995              37 :     if( !pVal ){
   78996               0 :       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
   78997               0 :       return;
   78998                 :     }
   78999              37 :     sqlite3ValueFree(pVal);
   79000                 :   }
   79001                 : 
   79002                 :   /* Modify the CREATE TABLE statement. */
   79003             121 :   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
   79004             121 :   if( zCol ){
   79005             121 :     char *zEnd = &zCol[pColDef->n-1];
   79006             121 :     int savedDbFlags = db->flags;
   79007             242 :     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
   79008               0 :       *zEnd-- = '\0';
   79009                 :     }
   79010             121 :     db->flags |= SQLITE_PreferBuiltin;
   79011             121 :     sqlite3NestedParse(pParse, 
   79012                 :         "UPDATE \"%w\".%s SET "
   79013                 :           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
   79014                 :         "WHERE type = 'table' AND name = %Q", 
   79015             121 :       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
   79016                 :       zTab
   79017                 :     );
   79018             121 :     sqlite3DbFree(db, zCol);
   79019             121 :     db->flags = savedDbFlags;
   79020                 :   }
   79021                 : 
   79022                 :   /* If the default value of the new column is NULL, then set the file
   79023                 :   ** format to 2. If the default value of the new column is not NULL,
   79024                 :   ** the file format becomes 3.
   79025                 :   */
   79026             121 :   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
   79027                 : 
   79028                 :   /* Reload the schema of the modified table. */
   79029             121 :   reloadTableSchema(pParse, pTab, pTab->zName);
   79030                 : }
   79031                 : 
   79032                 : /*
   79033                 : ** This function is called by the parser after the table-name in
   79034                 : ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
   79035                 : ** pSrc is the full-name of the table being altered.
   79036                 : **
   79037                 : ** This routine makes a (partial) copy of the Table structure
   79038                 : ** for the table being altered and sets Parse.pNewTable to point
   79039                 : ** to it. Routines called by the parser as the column definition
   79040                 : ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
   79041                 : ** the copy. The copy of the Table structure is deleted by tokenize.c 
   79042                 : ** after parsing is finished.
   79043                 : **
   79044                 : ** Routine sqlite3AlterFinishAddColumn() will be called to complete
   79045                 : ** coding the "ALTER TABLE ... ADD" statement.
   79046                 : */
   79047             139 : SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
   79048                 :   Table *pNew;
   79049                 :   Table *pTab;
   79050                 :   Vdbe *v;
   79051                 :   int iDb;
   79052                 :   int i;
   79053                 :   int nAlloc;
   79054             139 :   sqlite3 *db = pParse->db;
   79055                 : 
   79056                 :   /* Look up the table being altered. */
   79057             139 :   assert( pParse->pNewTable==0 );
   79058             139 :   assert( sqlite3BtreeHoldsAllMutexes(db) );
   79059             139 :   if( db->mallocFailed ) goto exit_begin_add_column;
   79060             139 :   pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
   79061             139 :   if( !pTab ) goto exit_begin_add_column;
   79062                 : 
   79063                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   79064             139 :   if( IsVirtual(pTab) ){
   79065               0 :     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
   79066               0 :     goto exit_begin_add_column;
   79067                 :   }
   79068                 : #endif
   79069                 : 
   79070                 :   /* Make sure this is not an attempt to ALTER a view. */
   79071             139 :   if( pTab->pSelect ){
   79072               0 :     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
   79073               0 :     goto exit_begin_add_column;
   79074                 :   }
   79075             139 :   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
   79076               0 :     goto exit_begin_add_column;
   79077                 :   }
   79078                 : 
   79079             139 :   assert( pTab->addColOffset>0 );
   79080             139 :   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   79081                 : 
   79082                 :   /* Put a copy of the Table struct in Parse.pNewTable for the
   79083                 :   ** sqlite3AddColumn() function and friends to modify.  But modify
   79084                 :   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
   79085                 :   ** prefix, we insure that the name will not collide with an existing
   79086                 :   ** table because user table are not allowed to have the "sqlite_"
   79087                 :   ** prefix on their name.
   79088                 :   */
   79089             139 :   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
   79090             139 :   if( !pNew ) goto exit_begin_add_column;
   79091             139 :   pParse->pNewTable = pNew;
   79092             139 :   pNew->nRef = 1;
   79093             139 :   pNew->nCol = pTab->nCol;
   79094             139 :   assert( pNew->nCol>0 );
   79095             139 :   nAlloc = (((pNew->nCol-1)/8)*8)+8;
   79096             139 :   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
   79097             139 :   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
   79098             139 :   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
   79099             139 :   if( !pNew->aCol || !pNew->zName ){
   79100               0 :     db->mallocFailed = 1;
   79101               0 :     goto exit_begin_add_column;
   79102                 :   }
   79103             139 :   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
   79104            1419 :   for(i=0; i<pNew->nCol; i++){
   79105            1280 :     Column *pCol = &pNew->aCol[i];
   79106            1280 :     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
   79107            1280 :     pCol->zColl = 0;
   79108            1280 :     pCol->zType = 0;
   79109            1280 :     pCol->pDflt = 0;
   79110            1280 :     pCol->zDflt = 0;
   79111                 :   }
   79112             139 :   pNew->pSchema = db->aDb[iDb].pSchema;
   79113             139 :   pNew->addColOffset = pTab->addColOffset;
   79114             139 :   pNew->nRef = 1;
   79115                 : 
   79116                 :   /* Begin a transaction and increment the schema cookie.  */
   79117             139 :   sqlite3BeginWriteOperation(pParse, 0, iDb);
   79118             139 :   v = sqlite3GetVdbe(pParse);
   79119             139 :   if( !v ) goto exit_begin_add_column;
   79120             139 :   sqlite3ChangeCookie(pParse, iDb);
   79121                 : 
   79122                 : exit_begin_add_column:
   79123             139 :   sqlite3SrcListDelete(db, pSrc);
   79124                 :   return;
   79125                 : }
   79126                 : #endif  /* SQLITE_ALTER_TABLE */
   79127                 : 
   79128                 : /************** End of alter.c ***********************************************/
   79129                 : /************** Begin file analyze.c *****************************************/
   79130                 : /*
   79131                 : ** 2005 July 8
   79132                 : **
   79133                 : ** The author disclaims copyright to this source code.  In place of
   79134                 : ** a legal notice, here is a blessing:
   79135                 : **
   79136                 : **    May you do good and not evil.
   79137                 : **    May you find forgiveness for yourself and forgive others.
   79138                 : **    May you share freely, never taking more than you give.
   79139                 : **
   79140                 : *************************************************************************
   79141                 : ** This file contains code associated with the ANALYZE command.
   79142                 : **
   79143                 : ** The ANALYZE command gather statistics about the content of tables
   79144                 : ** and indices.  These statistics are made available to the query planner
   79145                 : ** to help it make better decisions about how to perform queries.
   79146                 : **
   79147                 : ** The following system tables are or have been supported:
   79148                 : **
   79149                 : **    CREATE TABLE sqlite_stat1(tbl, idx, stat);
   79150                 : **    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
   79151                 : **    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
   79152                 : **
   79153                 : ** Additional tables might be added in future releases of SQLite.
   79154                 : ** The sqlite_stat2 table is not created or used unless the SQLite version
   79155                 : ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
   79156                 : ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
   79157                 : ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
   79158                 : ** created and used by SQLite versions 3.7.9 and later and with
   79159                 : ** SQLITE_ENABLE_STAT3 defined.  The fucntionality of sqlite_stat3
   79160                 : ** is a superset of sqlite_stat2.  
   79161                 : **
   79162                 : ** Format of sqlite_stat1:
   79163                 : **
   79164                 : ** There is normally one row per index, with the index identified by the
   79165                 : ** name in the idx column.  The tbl column is the name of the table to
   79166                 : ** which the index belongs.  In each such row, the stat column will be
   79167                 : ** a string consisting of a list of integers.  The first integer in this
   79168                 : ** list is the number of rows in the index and in the table.  The second
   79169                 : ** integer is the average number of rows in the index that have the same
   79170                 : ** value in the first column of the index.  The third integer is the average
   79171                 : ** number of rows in the index that have the same value for the first two
   79172                 : ** columns.  The N-th integer (for N>1) is the average number of rows in 
   79173                 : ** the index which have the same value for the first N-1 columns.  For
   79174                 : ** a K-column index, there will be K+1 integers in the stat column.  If
   79175                 : ** the index is unique, then the last integer will be 1.
   79176                 : **
   79177                 : ** The list of integers in the stat column can optionally be followed
   79178                 : ** by the keyword "unordered".  The "unordered" keyword, if it is present,
   79179                 : ** must be separated from the last integer by a single space.  If the
   79180                 : ** "unordered" keyword is present, then the query planner assumes that
   79181                 : ** the index is unordered and will not use the index for a range query.
   79182                 : ** 
   79183                 : ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
   79184                 : ** column contains a single integer which is the (estimated) number of
   79185                 : ** rows in the table identified by sqlite_stat1.tbl.
   79186                 : **
   79187                 : ** Format of sqlite_stat2:
   79188                 : **
   79189                 : ** The sqlite_stat2 is only created and is only used if SQLite is compiled
   79190                 : ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
   79191                 : ** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
   79192                 : ** about the distribution of keys within an index.  The index is identified by
   79193                 : ** the "idx" column and the "tbl" column is the name of the table to which
   79194                 : ** the index belongs.  There are usually 10 rows in the sqlite_stat2
   79195                 : ** table for each index.
   79196                 : **
   79197                 : ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
   79198                 : ** inclusive are samples of the left-most key value in the index taken at
   79199                 : ** evenly spaced points along the index.  Let the number of samples be S
   79200                 : ** (10 in the standard build) and let C be the number of rows in the index.
   79201                 : ** Then the sampled rows are given by:
   79202                 : **
   79203                 : **     rownumber = (i*C*2 + C)/(S*2)
   79204                 : **
   79205                 : ** For i between 0 and S-1.  Conceptually, the index space is divided into
   79206                 : ** S uniform buckets and the samples are the middle row from each bucket.
   79207                 : **
   79208                 : ** The format for sqlite_stat2 is recorded here for legacy reference.  This
   79209                 : ** version of SQLite does not support sqlite_stat2.  It neither reads nor
   79210                 : ** writes the sqlite_stat2 table.  This version of SQLite only supports
   79211                 : ** sqlite_stat3.
   79212                 : **
   79213                 : ** Format for sqlite_stat3:
   79214                 : **
   79215                 : ** The sqlite_stat3 is an enhancement to sqlite_stat2.  A new name is
   79216                 : ** used to avoid compatibility problems.  
   79217                 : **
   79218                 : ** The format of the sqlite_stat3 table is similar to the format of
   79219                 : ** the sqlite_stat2 table.  There are multiple entries for each index.
   79220                 : ** The idx column names the index and the tbl column is the table of the
   79221                 : ** index.  If the idx and tbl columns are the same, then the sample is
   79222                 : ** of the INTEGER PRIMARY KEY.  The sample column is a value taken from
   79223                 : ** the left-most column of the index.  The nEq column is the approximate
   79224                 : ** number of entires in the index whose left-most column exactly matches
   79225                 : ** the sample.  nLt is the approximate number of entires whose left-most
   79226                 : ** column is less than the sample.  The nDLt column is the approximate
   79227                 : ** number of distinct left-most entries in the index that are less than
   79228                 : ** the sample.
   79229                 : **
   79230                 : ** Future versions of SQLite might change to store a string containing
   79231                 : ** multiple integers values in the nDLt column of sqlite_stat3.  The first
   79232                 : ** integer will be the number of prior index entires that are distinct in
   79233                 : ** the left-most column.  The second integer will be the number of prior index
   79234                 : ** entries that are distinct in the first two columns.  The third integer
   79235                 : ** will be the number of prior index entries that are distinct in the first
   79236                 : ** three columns.  And so forth.  With that extension, the nDLt field is
   79237                 : ** similar in function to the sqlite_stat1.stat field.
   79238                 : **
   79239                 : ** There can be an arbitrary number of sqlite_stat3 entries per index.
   79240                 : ** The ANALYZE command will typically generate sqlite_stat3 tables
   79241                 : ** that contain between 10 and 40 samples which are distributed across
   79242                 : ** the key space, though not uniformly, and which include samples with
   79243                 : ** largest possible nEq values.
   79244                 : */
   79245                 : #ifndef SQLITE_OMIT_ANALYZE
   79246                 : 
   79247                 : /*
   79248                 : ** This routine generates code that opens the sqlite_stat1 table for
   79249                 : ** writing with cursor iStatCur. If the library was built with the
   79250                 : ** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
   79251                 : ** opened for writing using cursor (iStatCur+1)
   79252                 : **
   79253                 : ** If the sqlite_stat1 tables does not previously exist, it is created.
   79254                 : ** Similarly, if the sqlite_stat3 table does not exist and the library
   79255                 : ** is compiled with SQLITE_ENABLE_STAT3 defined, it is created. 
   79256                 : **
   79257                 : ** Argument zWhere may be a pointer to a buffer containing a table name,
   79258                 : ** or it may be a NULL pointer. If it is not NULL, then all entries in
   79259                 : ** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
   79260                 : ** with the named table are deleted. If zWhere==0, then code is generated
   79261                 : ** to delete all stat table entries.
   79262                 : */
   79263            1475 : static void openStatTable(
   79264                 :   Parse *pParse,          /* Parsing context */
   79265                 :   int iDb,                /* The database we are looking in */
   79266                 :   int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
   79267                 :   const char *zWhere,     /* Delete entries for this table or index */
   79268                 :   const char *zWhereType  /* Either "tbl" or "idx" */
   79269                 : ){
   79270                 :   static const struct {
   79271                 :     const char *zName;
   79272                 :     const char *zCols;
   79273                 :   } aTable[] = {
   79274                 :     { "sqlite_stat1", "tbl,idx,stat" },
   79275                 : #ifdef SQLITE_ENABLE_STAT3
   79276                 :     { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
   79277                 : #endif
   79278                 :   };
   79279                 : 
   79280            1475 :   int aRoot[] = {0, 0};
   79281            1475 :   u8 aCreateTbl[] = {0, 0};
   79282                 : 
   79283                 :   int i;
   79284            1475 :   sqlite3 *db = pParse->db;
   79285                 :   Db *pDb;
   79286            1475 :   Vdbe *v = sqlite3GetVdbe(pParse);
   79287            1475 :   if( v==0 ) return;
   79288            1475 :   assert( sqlite3BtreeHoldsAllMutexes(db) );
   79289            1475 :   assert( sqlite3VdbeDb(v)==db );
   79290            1475 :   pDb = &db->aDb[iDb];
   79291                 : 
   79292                 :   /* Create new statistic tables if they do not exist, or clear them
   79293                 :   ** if they do already exist.
   79294                 :   */
   79295            2950 :   for(i=0; i<ArraySize(aTable); i++){
   79296            1475 :     const char *zTab = aTable[i].zName;
   79297                 :     Table *pStat;
   79298            1475 :     if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
   79299                 :       /* The sqlite_stat[12] table does not exist. Create it. Note that a 
   79300                 :       ** side-effect of the CREATE TABLE statement is to leave the rootpage 
   79301                 :       ** of the new table in register pParse->regRoot. This is important 
   79302                 :       ** because the OpenWrite opcode below will be needing it. */
   79303             530 :       sqlite3NestedParse(pParse,
   79304                 :           "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
   79305                 :       );
   79306             530 :       aRoot[i] = pParse->regRoot;
   79307             530 :       aCreateTbl[i] = 1;
   79308                 :     }else{
   79309                 :       /* The table already exists. If zWhere is not NULL, delete all entries 
   79310                 :       ** associated with the table zWhere. If zWhere is NULL, delete the
   79311                 :       ** entire contents of the table. */
   79312             945 :       aRoot[i] = pStat->tnum;
   79313             945 :       sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
   79314             945 :       if( zWhere ){
   79315             945 :         sqlite3NestedParse(pParse,
   79316                 :            "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
   79317                 :         );
   79318                 :       }else{
   79319                 :         /* The sqlite_stat[12] table already exists.  Delete all rows. */
   79320               0 :         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
   79321                 :       }
   79322                 :     }
   79323                 :   }
   79324                 : 
   79325                 :   /* Open the sqlite_stat[13] tables for writing. */
   79326            2950 :   for(i=0; i<ArraySize(aTable); i++){
   79327            1475 :     sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
   79328            1475 :     sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
   79329            1475 :     sqlite3VdbeChangeP5(v, aCreateTbl[i]);
   79330                 :   }
   79331                 : }
   79332                 : 
   79333                 : /*
   79334                 : ** Recommended number of samples for sqlite_stat3
   79335                 : */
   79336                 : #ifndef SQLITE_STAT3_SAMPLES
   79337                 : # define SQLITE_STAT3_SAMPLES 24
   79338                 : #endif
   79339                 : 
   79340                 : /*
   79341                 : ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
   79342                 : ** share an instance of the following structure to hold their state
   79343                 : ** information.
   79344                 : */
   79345                 : typedef struct Stat3Accum Stat3Accum;
   79346                 : struct Stat3Accum {
   79347                 :   tRowcnt nRow;             /* Number of rows in the entire table */
   79348                 :   tRowcnt nPSample;         /* How often to do a periodic sample */
   79349                 :   int iMin;                 /* Index of entry with minimum nEq and hash */
   79350                 :   int mxSample;             /* Maximum number of samples to accumulate */
   79351                 :   int nSample;              /* Current number of samples */
   79352                 :   u32 iPrn;                 /* Pseudo-random number used for sampling */
   79353                 :   struct Stat3Sample {
   79354                 :     i64 iRowid;                /* Rowid in main table of the key */
   79355                 :     tRowcnt nEq;               /* sqlite_stat3.nEq */
   79356                 :     tRowcnt nLt;               /* sqlite_stat3.nLt */
   79357                 :     tRowcnt nDLt;              /* sqlite_stat3.nDLt */
   79358                 :     u8 isPSample;              /* True if a periodic sample */
   79359                 :     u32 iHash;                 /* Tiebreaker hash */
   79360                 :   } *a;                     /* An array of samples */
   79361                 : };
   79362                 : 
   79363                 : #ifdef SQLITE_ENABLE_STAT3
   79364                 : /*
   79365                 : ** Implementation of the stat3_init(C,S) SQL function.  The two parameters
   79366                 : ** are the number of rows in the table or index (C) and the number of samples
   79367                 : ** to accumulate (S).
   79368                 : **
   79369                 : ** This routine allocates the Stat3Accum object.
   79370                 : **
   79371                 : ** The return value is the Stat3Accum object (P).
   79372                 : */
   79373                 : static void stat3Init(
   79374                 :   sqlite3_context *context,
   79375                 :   int argc,
   79376                 :   sqlite3_value **argv
   79377                 : ){
   79378                 :   Stat3Accum *p;
   79379                 :   tRowcnt nRow;
   79380                 :   int mxSample;
   79381                 :   int n;
   79382                 : 
   79383                 :   UNUSED_PARAMETER(argc);
   79384                 :   nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
   79385                 :   mxSample = sqlite3_value_int(argv[1]);
   79386                 :   n = sizeof(*p) + sizeof(p->a[0])*mxSample;
   79387                 :   p = sqlite3_malloc( n );
   79388                 :   if( p==0 ){
   79389                 :     sqlite3_result_error_nomem(context);
   79390                 :     return;
   79391                 :   }
   79392                 :   memset(p, 0, n);
   79393                 :   p->a = (struct Stat3Sample*)&p[1];
   79394                 :   p->nRow = nRow;
   79395                 :   p->mxSample = mxSample;
   79396                 :   p->nPSample = p->nRow/(mxSample/3+1) + 1;
   79397                 :   sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
   79398                 :   sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
   79399                 : }
   79400                 : static const FuncDef stat3InitFuncdef = {
   79401                 :   2,                /* nArg */
   79402                 :   SQLITE_UTF8,      /* iPrefEnc */
   79403                 :   0,                /* flags */
   79404                 :   0,                /* pUserData */
   79405                 :   0,                /* pNext */
   79406                 :   stat3Init,        /* xFunc */
   79407                 :   0,                /* xStep */
   79408                 :   0,                /* xFinalize */
   79409                 :   "stat3_init",     /* zName */
   79410                 :   0,                /* pHash */
   79411                 :   0                 /* pDestructor */
   79412                 : };
   79413                 : 
   79414                 : 
   79415                 : /*
   79416                 : ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function.  The
   79417                 : ** arguments describe a single key instance.  This routine makes the 
   79418                 : ** decision about whether or not to retain this key for the sqlite_stat3
   79419                 : ** table.
   79420                 : **
   79421                 : ** The return value is NULL.
   79422                 : */
   79423                 : static void stat3Push(
   79424                 :   sqlite3_context *context,
   79425                 :   int argc,
   79426                 :   sqlite3_value **argv
   79427                 : ){
   79428                 :   Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
   79429                 :   tRowcnt nEq = sqlite3_value_int64(argv[0]);
   79430                 :   tRowcnt nLt = sqlite3_value_int64(argv[1]);
   79431                 :   tRowcnt nDLt = sqlite3_value_int64(argv[2]);
   79432                 :   i64 rowid = sqlite3_value_int64(argv[3]);
   79433                 :   u8 isPSample = 0;
   79434                 :   u8 doInsert = 0;
   79435                 :   int iMin = p->iMin;
   79436                 :   struct Stat3Sample *pSample;
   79437                 :   int i;
   79438                 :   u32 h;
   79439                 : 
   79440                 :   UNUSED_PARAMETER(context);
   79441                 :   UNUSED_PARAMETER(argc);
   79442                 :   if( nEq==0 ) return;
   79443                 :   h = p->iPrn = p->iPrn*1103515245 + 12345;
   79444                 :   if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
   79445                 :     doInsert = isPSample = 1;
   79446                 :   }else if( p->nSample<p->mxSample ){
   79447                 :     doInsert = 1;
   79448                 :   }else{
   79449                 :     if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
   79450                 :       doInsert = 1;
   79451                 :     }
   79452                 :   }
   79453                 :   if( !doInsert ) return;
   79454                 :   if( p->nSample==p->mxSample ){
   79455                 :     assert( p->nSample - iMin - 1 >= 0 );
   79456                 :     memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
   79457                 :     pSample = &p->a[p->nSample-1];
   79458                 :   }else{
   79459                 :     pSample = &p->a[p->nSample++];
   79460                 :   }
   79461                 :   pSample->iRowid = rowid;
   79462                 :   pSample->nEq = nEq;
   79463                 :   pSample->nLt = nLt;
   79464                 :   pSample->nDLt = nDLt;
   79465                 :   pSample->iHash = h;
   79466                 :   pSample->isPSample = isPSample;
   79467                 : 
   79468                 :   /* Find the new minimum */
   79469                 :   if( p->nSample==p->mxSample ){
   79470                 :     pSample = p->a;
   79471                 :     i = 0;
   79472                 :     while( pSample->isPSample ){
   79473                 :       i++;
   79474                 :       pSample++;
   79475                 :       assert( i<p->nSample );
   79476                 :     }
   79477                 :     nEq = pSample->nEq;
   79478                 :     h = pSample->iHash;
   79479                 :     iMin = i;
   79480                 :     for(i++, pSample++; i<p->nSample; i++, pSample++){
   79481                 :       if( pSample->isPSample ) continue;
   79482                 :       if( pSample->nEq<nEq
   79483                 :        || (pSample->nEq==nEq && pSample->iHash<h)
   79484                 :       ){
   79485                 :         iMin = i;
   79486                 :         nEq = pSample->nEq;
   79487                 :         h = pSample->iHash;
   79488                 :       }
   79489                 :     }
   79490                 :     p->iMin = iMin;
   79491                 :   }
   79492                 : }
   79493                 : static const FuncDef stat3PushFuncdef = {
   79494                 :   5,                /* nArg */
   79495                 :   SQLITE_UTF8,      /* iPrefEnc */
   79496                 :   0,                /* flags */
   79497                 :   0,                /* pUserData */
   79498                 :   0,                /* pNext */
   79499                 :   stat3Push,        /* xFunc */
   79500                 :   0,                /* xStep */
   79501                 :   0,                /* xFinalize */
   79502                 :   "stat3_push",     /* zName */
   79503                 :   0,                /* pHash */
   79504                 :   0                 /* pDestructor */
   79505                 : };
   79506                 : 
   79507                 : /*
   79508                 : ** Implementation of the stat3_get(P,N,...) SQL function.  This routine is
   79509                 : ** used to query the results.  Content is returned for the Nth sqlite_stat3
   79510                 : ** row where N is between 0 and S-1 and S is the number of samples.  The
   79511                 : ** value returned depends on the number of arguments.
   79512                 : **
   79513                 : **   argc==2    result:  rowid
   79514                 : **   argc==3    result:  nEq
   79515                 : **   argc==4    result:  nLt
   79516                 : **   argc==5    result:  nDLt
   79517                 : */
   79518                 : static void stat3Get(
   79519                 :   sqlite3_context *context,
   79520                 :   int argc,
   79521                 :   sqlite3_value **argv
   79522                 : ){
   79523                 :   int n = sqlite3_value_int(argv[1]);
   79524                 :   Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
   79525                 : 
   79526                 :   assert( p!=0 );
   79527                 :   if( p->nSample<=n ) return;
   79528                 :   switch( argc ){
   79529                 :     case 2:  sqlite3_result_int64(context, p->a[n].iRowid); break;
   79530                 :     case 3:  sqlite3_result_int64(context, p->a[n].nEq);    break;
   79531                 :     case 4:  sqlite3_result_int64(context, p->a[n].nLt);    break;
   79532                 :     default: sqlite3_result_int64(context, p->a[n].nDLt);   break;
   79533                 :   }
   79534                 : }
   79535                 : static const FuncDef stat3GetFuncdef = {
   79536                 :   -1,               /* nArg */
   79537                 :   SQLITE_UTF8,      /* iPrefEnc */
   79538                 :   0,                /* flags */
   79539                 :   0,                /* pUserData */
   79540                 :   0,                /* pNext */
   79541                 :   stat3Get,         /* xFunc */
   79542                 :   0,                /* xStep */
   79543                 :   0,                /* xFinalize */
   79544                 :   "stat3_get",     /* zName */
   79545                 :   0,                /* pHash */
   79546                 :   0                 /* pDestructor */
   79547                 : };
   79548                 : #endif /* SQLITE_ENABLE_STAT3 */
   79549                 : 
   79550                 : 
   79551                 : 
   79552                 : 
   79553                 : /*
   79554                 : ** Generate code to do an analysis of all indices associated with
   79555                 : ** a single table.
   79556                 : */
   79557            1475 : static void analyzeOneTable(
   79558                 :   Parse *pParse,   /* Parser context */
   79559                 :   Table *pTab,     /* Table whose indices are to be analyzed */
   79560                 :   Index *pOnlyIdx, /* If not NULL, only analyze this one index */
   79561                 :   int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
   79562                 :   int iMem         /* Available memory locations begin here */
   79563                 : ){
   79564            1475 :   sqlite3 *db = pParse->db;    /* Database handle */
   79565                 :   Index *pIdx;                 /* An index to being analyzed */
   79566                 :   int iIdxCur;                 /* Cursor open on index being analyzed */
   79567                 :   Vdbe *v;                     /* The virtual machine being built up */
   79568                 :   int i;                       /* Loop counter */
   79569                 :   int topOfLoop;               /* The top of the loop */
   79570                 :   int endOfLoop;               /* The end of the loop */
   79571            1475 :   int jZeroRows = -1;          /* Jump from here if number of rows is zero */
   79572                 :   int iDb;                     /* Index of database containing pTab */
   79573            1475 :   int regTabname = iMem++;     /* Register containing table name */
   79574            1475 :   int regIdxname = iMem++;     /* Register containing index name */
   79575            1475 :   int regStat1 = iMem++;       /* The stat column of sqlite_stat1 */
   79576                 : #ifdef SQLITE_ENABLE_STAT3
   79577                 :   int regNumEq = regStat1;     /* Number of instances.  Same as regStat1 */
   79578                 :   int regNumLt = iMem++;       /* Number of keys less than regSample */
   79579                 :   int regNumDLt = iMem++;      /* Number of distinct keys less than regSample */
   79580                 :   int regSample = iMem++;      /* The next sample value */
   79581                 :   int regRowid = regSample;    /* Rowid of a sample */
   79582                 :   int regAccum = iMem++;       /* Register to hold Stat3Accum object */
   79583                 :   int regLoop = iMem++;        /* Loop counter */
   79584                 :   int regCount = iMem++;       /* Number of rows in the table or index */
   79585                 :   int regTemp1 = iMem++;       /* Intermediate register */
   79586                 :   int regTemp2 = iMem++;       /* Intermediate register */
   79587                 :   int once = 1;                /* One-time initialization */
   79588                 :   int shortJump = 0;           /* Instruction address */
   79589                 :   int iTabCur = pParse->nTab++; /* Table cursor */
   79590                 : #endif
   79591            1475 :   int regCol = iMem++;         /* Content of a column in analyzed table */
   79592            1475 :   int regRec = iMem++;         /* Register holding completed record */
   79593            1475 :   int regTemp = iMem++;        /* Temporary use register */
   79594            1475 :   int regNewRowid = iMem++;    /* Rowid for the inserted record */
   79595                 : 
   79596                 : 
   79597            1475 :   v = sqlite3GetVdbe(pParse);
   79598            1475 :   if( v==0 || NEVER(pTab==0) ){
   79599               0 :     return;
   79600                 :   }
   79601            1475 :   if( pTab->tnum==0 ){
   79602                 :     /* Do not gather statistics on views or virtual tables */
   79603               0 :     return;
   79604                 :   }
   79605            1475 :   if( memcmp(pTab->zName, "sqlite_", 7)==0 ){
   79606                 :     /* Do not gather statistics on system tables */
   79607               0 :     return;
   79608                 :   }
   79609            1475 :   assert( sqlite3BtreeHoldsAllMutexes(db) );
   79610            1475 :   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   79611            1475 :   assert( iDb>=0 );
   79612            1475 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   79613                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   79614            1475 :   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
   79615            1475 :       db->aDb[iDb].zName ) ){
   79616               0 :     return;
   79617                 :   }
   79618                 : #endif
   79619                 : 
   79620                 :   /* Establish a read-lock on the table at the shared-cache level. */
   79621            1475 :   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
   79622                 : 
   79623            1475 :   iIdxCur = pParse->nTab++;
   79624            1475 :   sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
   79625            7064 :   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   79626                 :     int nCol;
   79627                 :     KeyInfo *pKey;
   79628            5589 :     int addrIfNot = 0;           /* address of OP_IfNot */
   79629                 :     int *aChngAddr;              /* Array of jump instruction addresses */
   79630                 : 
   79631            5589 :     if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
   79632            5589 :     VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
   79633            5589 :     nCol = pIdx->nColumn;
   79634            5589 :     aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
   79635            5589 :     if( aChngAddr==0 ) continue;
   79636            5589 :     pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   79637            5589 :     if( iMem+1+(nCol*2)>pParse->nMem ){
   79638            2330 :       pParse->nMem = iMem+1+(nCol*2);
   79639                 :     }
   79640                 : 
   79641                 :     /* Open a cursor to the index to be analyzed. */
   79642            5589 :     assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
   79643            5589 :     sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
   79644                 :         (char *)pKey, P4_KEYINFO_HANDOFF);
   79645            5589 :     VdbeComment((v, "%s", pIdx->zName));
   79646                 : 
   79647                 :     /* Populate the register containing the index name. */
   79648            5589 :     sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
   79649                 : 
   79650                 : #ifdef SQLITE_ENABLE_STAT3
   79651                 :     if( once ){
   79652                 :       once = 0;
   79653                 :       sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
   79654                 :     }
   79655                 :     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
   79656                 :     sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
   79657                 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
   79658                 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
   79659                 :     sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
   79660                 :     sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
   79661                 :     sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
   79662                 :                       (char*)&stat3InitFuncdef, P4_FUNCDEF);
   79663                 :     sqlite3VdbeChangeP5(v, 2);
   79664                 : #endif /* SQLITE_ENABLE_STAT3 */
   79665                 : 
   79666                 :     /* The block of memory cells initialized here is used as follows.
   79667                 :     **
   79668                 :     **    iMem:                
   79669                 :     **        The total number of rows in the table.
   79670                 :     **
   79671                 :     **    iMem+1 .. iMem+nCol: 
   79672                 :     **        Number of distinct entries in index considering the 
   79673                 :     **        left-most N columns only, where N is between 1 and nCol, 
   79674                 :     **        inclusive.
   79675                 :     **
   79676                 :     **    iMem+nCol+1 .. Mem+2*nCol:  
   79677                 :     **        Previous value of indexed columns, from left to right.
   79678                 :     **
   79679                 :     ** Cells iMem through iMem+nCol are initialized to 0. The others are 
   79680                 :     ** initialized to contain an SQL NULL.
   79681                 :     */
   79682           19020 :     for(i=0; i<=nCol; i++){
   79683           13431 :       sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
   79684                 :     }
   79685           13431 :     for(i=0; i<nCol; i++){
   79686            7842 :       sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
   79687                 :     }
   79688                 : 
   79689                 :     /* Start the analysis loop. This loop runs through all the entries in
   79690                 :     ** the index b-tree.  */
   79691            5589 :     endOfLoop = sqlite3VdbeMakeLabel(v);
   79692            5589 :     sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
   79693            5589 :     topOfLoop = sqlite3VdbeCurrentAddr(v);
   79694            5589 :     sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);  /* Increment row counter */
   79695                 : 
   79696           13431 :     for(i=0; i<nCol; i++){
   79697                 :       CollSeq *pColl;
   79698            7842 :       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
   79699            7842 :       if( i==0 ){
   79700                 :         /* Always record the very first row */
   79701            5589 :         addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
   79702                 :       }
   79703            7842 :       assert( pIdx->azColl!=0 );
   79704            7842 :       assert( pIdx->azColl[i]!=0 );
   79705            7842 :       pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
   79706            7842 :       aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
   79707                 :                                       (char*)pColl, P4_COLLSEQ);
   79708            7842 :       sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
   79709            7842 :       VdbeComment((v, "jump if column %d changed", i));
   79710                 : #ifdef SQLITE_ENABLE_STAT3
   79711                 :       if( i==0 ){
   79712                 :         sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
   79713                 :         VdbeComment((v, "incr repeat count"));
   79714                 :       }
   79715                 : #endif
   79716                 :     }
   79717            5589 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
   79718           13431 :     for(i=0; i<nCol; i++){
   79719            7842 :       sqlite3VdbeJumpHere(v, aChngAddr[i]);  /* Set jump dest for the OP_Ne */
   79720            7842 :       if( i==0 ){
   79721            5589 :         sqlite3VdbeJumpHere(v, addrIfNot);   /* Jump dest for OP_IfNot */
   79722                 : #ifdef SQLITE_ENABLE_STAT3
   79723                 :         sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
   79724                 :                           (char*)&stat3PushFuncdef, P4_FUNCDEF);
   79725                 :         sqlite3VdbeChangeP5(v, 5);
   79726                 :         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
   79727                 :         sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
   79728                 :         sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
   79729                 :         sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
   79730                 : #endif        
   79731                 :       }
   79732            7842 :       sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
   79733            7842 :       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
   79734                 :     }
   79735            5589 :     sqlite3DbFree(db, aChngAddr);
   79736                 : 
   79737                 :     /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
   79738            5589 :     sqlite3VdbeResolveLabel(v, endOfLoop);
   79739                 : 
   79740            5589 :     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
   79741            5589 :     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
   79742                 : #ifdef SQLITE_ENABLE_STAT3
   79743                 :     sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
   79744                 :                       (char*)&stat3PushFuncdef, P4_FUNCDEF);
   79745                 :     sqlite3VdbeChangeP5(v, 5);
   79746                 :     sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
   79747                 :     shortJump = 
   79748                 :     sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
   79749                 :     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
   79750                 :                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
   79751                 :     sqlite3VdbeChangeP5(v, 2);
   79752                 :     sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
   79753                 :     sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
   79754                 :     sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
   79755                 :     sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
   79756                 :     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
   79757                 :                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
   79758                 :     sqlite3VdbeChangeP5(v, 3);
   79759                 :     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
   79760                 :                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
   79761                 :     sqlite3VdbeChangeP5(v, 4);
   79762                 :     sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
   79763                 :                       (char*)&stat3GetFuncdef, P4_FUNCDEF);
   79764                 :     sqlite3VdbeChangeP5(v, 5);
   79765                 :     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
   79766                 :     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
   79767                 :     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
   79768                 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
   79769                 :     sqlite3VdbeJumpHere(v, shortJump+2);
   79770                 : #endif        
   79771                 : 
   79772                 :     /* Store the results in sqlite_stat1.
   79773                 :     **
   79774                 :     ** The result is a single row of the sqlite_stat1 table.  The first
   79775                 :     ** two columns are the names of the table and index.  The third column
   79776                 :     ** is a string composed of a list of integer statistics about the
   79777                 :     ** index.  The first integer in the list is the total number of entries
   79778                 :     ** in the index.  There is one additional integer in the list for each
   79779                 :     ** column of the table.  This additional integer is a guess of how many
   79780                 :     ** rows of the table the index will select.  If D is the count of distinct
   79781                 :     ** values and K is the total number of rows, then the integer is computed
   79782                 :     ** as:
   79783                 :     **
   79784                 :     **        I = (K+D-1)/D
   79785                 :     **
   79786                 :     ** If K==0 then no entry is made into the sqlite_stat1 table.  
   79787                 :     ** If K>0 then it is always the case the D>0 so division by zero
   79788                 :     ** is never possible.
   79789                 :     */
   79790            5589 :     sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
   79791            5589 :     if( jZeroRows<0 ){
   79792            1475 :       jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
   79793                 :     }
   79794           13431 :     for(i=0; i<nCol; i++){
   79795            7842 :       sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
   79796            7842 :       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
   79797            7842 :       sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
   79798            7842 :       sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
   79799            7842 :       sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
   79800            7842 :       sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
   79801            7842 :       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
   79802                 :     }
   79803            5589 :     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
   79804            5589 :     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
   79805            5589 :     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
   79806            5589 :     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   79807                 :   }
   79808                 : 
   79809                 :   /* If the table has no indices, create a single sqlite_stat1 entry
   79810                 :   ** containing NULL as the index name and the row count as the content.
   79811                 :   */
   79812            1475 :   if( pTab->pIndex==0 ){
   79813               0 :     sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
   79814               0 :     VdbeComment((v, "%s", pTab->zName));
   79815               0 :     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
   79816               0 :     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
   79817               0 :     jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
   79818                 :   }else{
   79819            1475 :     sqlite3VdbeJumpHere(v, jZeroRows);
   79820            1475 :     jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
   79821                 :   }
   79822            1475 :   sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
   79823            1475 :   sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
   79824            1475 :   sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
   79825            1475 :   sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
   79826            1475 :   sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   79827            1475 :   if( pParse->nMem<regRec ) pParse->nMem = regRec;
   79828            1475 :   sqlite3VdbeJumpHere(v, jZeroRows);
   79829                 : }
   79830                 : 
   79831                 : 
   79832                 : /*
   79833                 : ** Generate code that will cause the most recent index analysis to
   79834                 : ** be loaded into internal hash tables where is can be used.
   79835                 : */
   79836            1475 : static void loadAnalysis(Parse *pParse, int iDb){
   79837            1475 :   Vdbe *v = sqlite3GetVdbe(pParse);
   79838            1475 :   if( v ){
   79839            1475 :     sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
   79840                 :   }
   79841            1475 : }
   79842                 : 
   79843                 : /*
   79844                 : ** Generate code that will do an analysis of an entire database
   79845                 : */
   79846               0 : static void analyzeDatabase(Parse *pParse, int iDb){
   79847               0 :   sqlite3 *db = pParse->db;
   79848               0 :   Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
   79849                 :   HashElem *k;
   79850                 :   int iStatCur;
   79851                 :   int iMem;
   79852                 : 
   79853               0 :   sqlite3BeginWriteOperation(pParse, 0, iDb);
   79854               0 :   iStatCur = pParse->nTab;
   79855               0 :   pParse->nTab += 3;
   79856               0 :   openStatTable(pParse, iDb, iStatCur, 0, 0);
   79857               0 :   iMem = pParse->nMem+1;
   79858               0 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   79859               0 :   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
   79860               0 :     Table *pTab = (Table*)sqliteHashData(k);
   79861               0 :     analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
   79862                 :   }
   79863               0 :   loadAnalysis(pParse, iDb);
   79864               0 : }
   79865                 : 
   79866                 : /*
   79867                 : ** Generate code that will do an analysis of a single table in
   79868                 : ** a database.  If pOnlyIdx is not NULL then it is a single index
   79869                 : ** in pTab that should be analyzed.
   79870                 : */
   79871            1475 : static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
   79872                 :   int iDb;
   79873                 :   int iStatCur;
   79874                 : 
   79875            1475 :   assert( pTab!=0 );
   79876            1475 :   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   79877            1475 :   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   79878            1475 :   sqlite3BeginWriteOperation(pParse, 0, iDb);
   79879            1475 :   iStatCur = pParse->nTab;
   79880            1475 :   pParse->nTab += 3;
   79881            1475 :   if( pOnlyIdx ){
   79882               0 :     openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
   79883                 :   }else{
   79884            1475 :     openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
   79885                 :   }
   79886            1475 :   analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
   79887            1475 :   loadAnalysis(pParse, iDb);
   79888            1475 : }
   79889                 : 
   79890                 : /*
   79891                 : ** Generate code for the ANALYZE command.  The parser calls this routine
   79892                 : ** when it recognizes an ANALYZE command.
   79893                 : **
   79894                 : **        ANALYZE                            -- 1
   79895                 : **        ANALYZE  <database>                -- 2
   79896                 : **        ANALYZE  ?<database>.?<tablename>  -- 3
   79897                 : **
   79898                 : ** Form 1 causes all indices in all attached databases to be analyzed.
   79899                 : ** Form 2 analyzes all indices the single database named.
   79900                 : ** Form 3 analyzes all indices associated with the named table.
   79901                 : */
   79902            1475 : SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
   79903            1475 :   sqlite3 *db = pParse->db;
   79904                 :   int iDb;
   79905                 :   int i;
   79906                 :   char *z, *zDb;
   79907                 :   Table *pTab;
   79908                 :   Index *pIdx;
   79909                 :   Token *pTableName;
   79910                 : 
   79911                 :   /* Read the database schema. If an error occurs, leave an error message
   79912                 :   ** and code in pParse and return NULL. */
   79913            1475 :   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
   79914            1475 :   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   79915               0 :     return;
   79916                 :   }
   79917                 : 
   79918            1475 :   assert( pName2!=0 || pName1==0 );
   79919            1475 :   if( pName1==0 ){
   79920                 :     /* Form 1:  Analyze everything */
   79921               0 :     for(i=0; i<db->nDb; i++){
   79922               0 :       if( i==1 ) continue;  /* Do not analyze the TEMP database */
   79923               0 :       analyzeDatabase(pParse, i);
   79924                 :     }
   79925            1475 :   }else if( pName2->n==0 ){
   79926                 :     /* Form 2:  Analyze the database or table named */
   79927            1475 :     iDb = sqlite3FindDb(db, pName1);
   79928            1475 :     if( iDb>=0 ){
   79929               0 :       analyzeDatabase(pParse, iDb);
   79930                 :     }else{
   79931            1475 :       z = sqlite3NameFromToken(db, pName1);
   79932            1475 :       if( z ){
   79933            1475 :         if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
   79934               0 :           analyzeTable(pParse, pIdx->pTable, pIdx);
   79935            1475 :         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
   79936            1475 :           analyzeTable(pParse, pTab, 0);
   79937                 :         }
   79938            1475 :         sqlite3DbFree(db, z);
   79939                 :       }
   79940                 :     }
   79941                 :   }else{
   79942                 :     /* Form 3: Analyze the fully qualified table name */
   79943               0 :     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
   79944               0 :     if( iDb>=0 ){
   79945               0 :       zDb = db->aDb[iDb].zName;
   79946               0 :       z = sqlite3NameFromToken(db, pTableName);
   79947               0 :       if( z ){
   79948               0 :         if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
   79949               0 :           analyzeTable(pParse, pIdx->pTable, pIdx);
   79950               0 :         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
   79951               0 :           analyzeTable(pParse, pTab, 0);
   79952                 :         }
   79953               0 :         sqlite3DbFree(db, z);
   79954                 :       }
   79955                 :     }   
   79956                 :   }
   79957                 : }
   79958                 : 
   79959                 : /*
   79960                 : ** Used to pass information from the analyzer reader through to the
   79961                 : ** callback routine.
   79962                 : */
   79963                 : typedef struct analysisInfo analysisInfo;
   79964                 : struct analysisInfo {
   79965                 :   sqlite3 *db;
   79966                 :   const char *zDatabase;
   79967                 : };
   79968                 : 
   79969                 : /*
   79970                 : ** This callback is invoked once for each index when reading the
   79971                 : ** sqlite_stat1 table.  
   79972                 : **
   79973                 : **     argv[0] = name of the table
   79974                 : **     argv[1] = name of the index (might be NULL)
   79975                 : **     argv[2] = results of analysis - on integer for each column
   79976                 : **
   79977                 : ** Entries for which argv[1]==NULL simply record the number of rows in
   79978                 : ** the table.
   79979                 : */
   79980            8332 : static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
   79981            8332 :   analysisInfo *pInfo = (analysisInfo*)pData;
   79982                 :   Index *pIndex;
   79983                 :   Table *pTable;
   79984                 :   int i, c, n;
   79985                 :   tRowcnt v;
   79986                 :   const char *z;
   79987                 : 
   79988            8332 :   assert( argc==3 );
   79989                 :   UNUSED_PARAMETER2(NotUsed, argc);
   79990                 : 
   79991            8332 :   if( argv==0 || argv[0]==0 || argv[2]==0 ){
   79992               0 :     return 0;
   79993                 :   }
   79994            8332 :   pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
   79995            8332 :   if( pTable==0 ){
   79996               0 :     return 0;
   79997                 :   }
   79998            8332 :   if( argv[1] ){
   79999            8332 :     pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
   80000                 :   }else{
   80001               0 :     pIndex = 0;
   80002                 :   }
   80003            8332 :   n = pIndex ? pIndex->nColumn : 0;
   80004            8332 :   z = argv[2];
   80005           30464 :   for(i=0; *z && i<=n; i++){
   80006           22132 :     v = 0;
   80007           67281 :     while( (c=z[0])>='0' && c<='9' ){
   80008           23017 :       v = v*10 + c - '0';
   80009           23017 :       z++;
   80010                 :     }
   80011           22132 :     if( i==0 ) pTable->nRowEst = v;
   80012           22132 :     if( pIndex==0 ) break;
   80013           22132 :     pIndex->aiRowEst[i] = v;
   80014           22132 :     if( *z==' ' ) z++;
   80015           22132 :     if( memcmp(z, "unordered", 10)==0 ){
   80016               0 :       pIndex->bUnordered = 1;
   80017               0 :       break;
   80018                 :     }
   80019                 :   }
   80020            8332 :   return 0;
   80021                 : }
   80022                 : 
   80023                 : /*
   80024                 : ** If the Index.aSample variable is not NULL, delete the aSample[] array
   80025                 : ** and its contents.
   80026                 : */
   80027           28810 : SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
   80028                 : #ifdef SQLITE_ENABLE_STAT3
   80029                 :   if( pIdx->aSample ){
   80030                 :     int j;
   80031                 :     for(j=0; j<pIdx->nSample; j++){
   80032                 :       IndexSample *p = &pIdx->aSample[j];
   80033                 :       if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
   80034                 :         sqlite3DbFree(db, p->u.z);
   80035                 :       }
   80036                 :     }
   80037                 :     sqlite3DbFree(db, pIdx->aSample);
   80038                 :   }
   80039                 :   if( db && db->pnBytesFreed==0 ){
   80040                 :     pIdx->nSample = 0;
   80041                 :     pIdx->aSample = 0;
   80042                 :   }
   80043                 : #else
   80044                 :   UNUSED_PARAMETER(db);
   80045                 :   UNUSED_PARAMETER(pIdx);
   80046                 : #endif
   80047           28810 : }
   80048                 : 
   80049                 : #ifdef SQLITE_ENABLE_STAT3
   80050                 : /*
   80051                 : ** Load content from the sqlite_stat3 table into the Index.aSample[]
   80052                 : ** arrays of all indices.
   80053                 : */
   80054                 : static int loadStat3(sqlite3 *db, const char *zDb){
   80055                 :   int rc;                       /* Result codes from subroutines */
   80056                 :   sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
   80057                 :   char *zSql;                   /* Text of the SQL statement */
   80058                 :   Index *pPrevIdx = 0;          /* Previous index in the loop */
   80059                 :   int idx = 0;                  /* slot in pIdx->aSample[] for next sample */
   80060                 :   int eType;                    /* Datatype of a sample */
   80061                 :   IndexSample *pSample;         /* A slot in pIdx->aSample[] */
   80062                 : 
   80063                 :   if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
   80064                 :     return SQLITE_OK;
   80065                 :   }
   80066                 : 
   80067                 :   zSql = sqlite3MPrintf(db, 
   80068                 :       "SELECT idx,count(*) FROM %Q.sqlite_stat3"
   80069                 :       " GROUP BY idx", zDb);
   80070                 :   if( !zSql ){
   80071                 :     return SQLITE_NOMEM;
   80072                 :   }
   80073                 :   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
   80074                 :   sqlite3DbFree(db, zSql);
   80075                 :   if( rc ) return rc;
   80076                 : 
   80077                 :   while( sqlite3_step(pStmt)==SQLITE_ROW ){
   80078                 :     char *zIndex;   /* Index name */
   80079                 :     Index *pIdx;    /* Pointer to the index object */
   80080                 :     int nSample;    /* Number of samples */
   80081                 : 
   80082                 :     zIndex = (char *)sqlite3_column_text(pStmt, 0);
   80083                 :     if( zIndex==0 ) continue;
   80084                 :     nSample = sqlite3_column_int(pStmt, 1);
   80085                 :     pIdx = sqlite3FindIndex(db, zIndex, zDb);
   80086                 :     if( pIdx==0 ) continue;
   80087                 :     assert( pIdx->nSample==0 );
   80088                 :     pIdx->nSample = nSample;
   80089                 :     pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
   80090                 :     pIdx->avgEq = pIdx->aiRowEst[1];
   80091                 :     if( pIdx->aSample==0 ){
   80092                 :       db->mallocFailed = 1;
   80093                 :       sqlite3_finalize(pStmt);
   80094                 :       return SQLITE_NOMEM;
   80095                 :     }
   80096                 :   }
   80097                 :   rc = sqlite3_finalize(pStmt);
   80098                 :   if( rc ) return rc;
   80099                 : 
   80100                 :   zSql = sqlite3MPrintf(db, 
   80101                 :       "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
   80102                 :   if( !zSql ){
   80103                 :     return SQLITE_NOMEM;
   80104                 :   }
   80105                 :   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
   80106                 :   sqlite3DbFree(db, zSql);
   80107                 :   if( rc ) return rc;
   80108                 : 
   80109                 :   while( sqlite3_step(pStmt)==SQLITE_ROW ){
   80110                 :     char *zIndex;   /* Index name */
   80111                 :     Index *pIdx;    /* Pointer to the index object */
   80112                 :     int i;          /* Loop counter */
   80113                 :     tRowcnt sumEq;  /* Sum of the nEq values */
   80114                 : 
   80115                 :     zIndex = (char *)sqlite3_column_text(pStmt, 0);
   80116                 :     if( zIndex==0 ) continue;
   80117                 :     pIdx = sqlite3FindIndex(db, zIndex, zDb);
   80118                 :     if( pIdx==0 ) continue;
   80119                 :     if( pIdx==pPrevIdx ){
   80120                 :       idx++;
   80121                 :     }else{
   80122                 :       pPrevIdx = pIdx;
   80123                 :       idx = 0;
   80124                 :     }
   80125                 :     assert( idx<pIdx->nSample );
   80126                 :     pSample = &pIdx->aSample[idx];
   80127                 :     pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
   80128                 :     pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
   80129                 :     pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
   80130                 :     if( idx==pIdx->nSample-1 ){
   80131                 :       if( pSample->nDLt>0 ){
   80132                 :         for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
   80133                 :         pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
   80134                 :       }
   80135                 :       if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
   80136                 :     }
   80137                 :     eType = sqlite3_column_type(pStmt, 4);
   80138                 :     pSample->eType = (u8)eType;
   80139                 :     switch( eType ){
   80140                 :       case SQLITE_INTEGER: {
   80141                 :         pSample->u.i = sqlite3_column_int64(pStmt, 4);
   80142                 :         break;
   80143                 :       }
   80144                 :       case SQLITE_FLOAT: {
   80145                 :         pSample->u.r = sqlite3_column_double(pStmt, 4);
   80146                 :         break;
   80147                 :       }
   80148                 :       case SQLITE_NULL: {
   80149                 :         break;
   80150                 :       }
   80151                 :       default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
   80152                 :         const char *z = (const char *)(
   80153                 :               (eType==SQLITE_BLOB) ?
   80154                 :               sqlite3_column_blob(pStmt, 4):
   80155                 :               sqlite3_column_text(pStmt, 4)
   80156                 :            );
   80157                 :         int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
   80158                 :         pSample->nByte = n;
   80159                 :         if( n < 1){
   80160                 :           pSample->u.z = 0;
   80161                 :         }else{
   80162                 :           pSample->u.z = sqlite3Malloc(n);
   80163                 :           if( pSample->u.z==0 ){
   80164                 :             db->mallocFailed = 1;
   80165                 :             sqlite3_finalize(pStmt);
   80166                 :             return SQLITE_NOMEM;
   80167                 :           }
   80168                 :           memcpy(pSample->u.z, z, n);
   80169                 :         }
   80170                 :       }
   80171                 :     }
   80172                 :   }
   80173                 :   return sqlite3_finalize(pStmt);
   80174                 : }
   80175                 : #endif /* SQLITE_ENABLE_STAT3 */
   80176                 : 
   80177                 : /*
   80178                 : ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
   80179                 : ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
   80180                 : ** arrays. The contents of sqlite_stat3 are used to populate the
   80181                 : ** Index.aSample[] arrays.
   80182                 : **
   80183                 : ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
   80184                 : ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined 
   80185                 : ** during compilation and the sqlite_stat3 table is present, no data is 
   80186                 : ** read from it.
   80187                 : **
   80188                 : ** If SQLITE_ENABLE_STAT3 was defined during compilation and the 
   80189                 : ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
   80190                 : ** returned. However, in this case, data is read from the sqlite_stat1
   80191                 : ** table (if it is present) before returning.
   80192                 : **
   80193                 : ** If an OOM error occurs, this function always sets db->mallocFailed.
   80194                 : ** This means if the caller does not care about other errors, the return
   80195                 : ** code may be ignored.
   80196                 : */
   80197            5793 : SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
   80198                 :   analysisInfo sInfo;
   80199                 :   HashElem *i;
   80200                 :   char *zSql;
   80201                 :   int rc;
   80202                 : 
   80203            5793 :   assert( iDb>=0 && iDb<db->nDb );
   80204            5793 :   assert( db->aDb[iDb].pBt!=0 );
   80205                 : 
   80206                 :   /* Clear any prior statistics */
   80207            5793 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   80208           67052 :   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
   80209           61259 :     Index *pIdx = sqliteHashData(i);
   80210           61259 :     sqlite3DefaultRowEst(pIdx);
   80211                 : #ifdef SQLITE_ENABLE_STAT3
   80212                 :     sqlite3DeleteIndexSamples(db, pIdx);
   80213                 :     pIdx->aSample = 0;
   80214                 : #endif
   80215                 :   }
   80216                 : 
   80217                 :   /* Check to make sure the sqlite_stat1 table exists */
   80218            5793 :   sInfo.db = db;
   80219            5793 :   sInfo.zDatabase = db->aDb[iDb].zName;
   80220            5793 :   if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
   80221            3448 :     return SQLITE_ERROR;
   80222                 :   }
   80223                 : 
   80224                 :   /* Load new statistics out of the sqlite_stat1 table */
   80225            2345 :   zSql = sqlite3MPrintf(db, 
   80226                 :       "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
   80227            2345 :   if( zSql==0 ){
   80228               0 :     rc = SQLITE_NOMEM;
   80229                 :   }else{
   80230            2345 :     rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
   80231            2345 :     sqlite3DbFree(db, zSql);
   80232                 :   }
   80233                 : 
   80234                 : 
   80235                 :   /* Load the statistics from the sqlite_stat3 table. */
   80236                 : #ifdef SQLITE_ENABLE_STAT3
   80237                 :   if( rc==SQLITE_OK ){
   80238                 :     rc = loadStat3(db, sInfo.zDatabase);
   80239                 :   }
   80240                 : #endif
   80241                 : 
   80242            2345 :   if( rc==SQLITE_NOMEM ){
   80243               0 :     db->mallocFailed = 1;
   80244                 :   }
   80245            2345 :   return rc;
   80246                 : }
   80247                 : 
   80248                 : 
   80249                 : #endif /* SQLITE_OMIT_ANALYZE */
   80250                 : 
   80251                 : /************** End of analyze.c *********************************************/
   80252                 : /************** Begin file attach.c ******************************************/
   80253                 : /*
   80254                 : ** 2003 April 6
   80255                 : **
   80256                 : ** The author disclaims copyright to this source code.  In place of
   80257                 : ** a legal notice, here is a blessing:
   80258                 : **
   80259                 : **    May you do good and not evil.
   80260                 : **    May you find forgiveness for yourself and forgive others.
   80261                 : **    May you share freely, never taking more than you give.
   80262                 : **
   80263                 : *************************************************************************
   80264                 : ** This file contains code used to implement the ATTACH and DETACH commands.
   80265                 : */
   80266                 : 
   80267                 : #ifndef SQLITE_OMIT_ATTACH
   80268                 : /*
   80269                 : ** Resolve an expression that was part of an ATTACH or DETACH statement. This
   80270                 : ** is slightly different from resolving a normal SQL expression, because simple
   80271                 : ** identifiers are treated as strings, not possible column names or aliases.
   80272                 : **
   80273                 : ** i.e. if the parser sees:
   80274                 : **
   80275                 : **     ATTACH DATABASE abc AS def
   80276                 : **
   80277                 : ** it treats the two expressions as literal strings 'abc' and 'def' instead of
   80278                 : ** looking for columns of the same name.
   80279                 : **
   80280                 : ** This only applies to the root node of pExpr, so the statement:
   80281                 : **
   80282                 : **     ATTACH DATABASE abc||def AS 'db2'
   80283                 : **
   80284                 : ** will fail because neither abc or def can be resolved.
   80285                 : */
   80286              18 : static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
   80287                 : {
   80288              18 :   int rc = SQLITE_OK;
   80289              18 :   if( pExpr ){
   80290              12 :     if( pExpr->op!=TK_ID ){
   80291               6 :       rc = sqlite3ResolveExprNames(pName, pExpr);
   80292               6 :       if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
   80293               0 :         sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken);
   80294               0 :         return SQLITE_ERROR;
   80295                 :       }
   80296                 :     }else{
   80297               6 :       pExpr->op = TK_STRING;
   80298                 :     }
   80299                 :   }
   80300              18 :   return rc;
   80301                 : }
   80302                 : 
   80303                 : /*
   80304                 : ** An SQL user-function registered to do the work of an ATTACH statement. The
   80305                 : ** three arguments to the function come directly from an attach statement:
   80306                 : **
   80307                 : **     ATTACH DATABASE x AS y KEY z
   80308                 : **
   80309                 : **     SELECT sqlite_attach(x, y, z)
   80310                 : **
   80311                 : ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
   80312                 : ** third argument.
   80313                 : */
   80314               6 : static void attachFunc(
   80315                 :   sqlite3_context *context,
   80316                 :   int NotUsed,
   80317                 :   sqlite3_value **argv
   80318                 : ){
   80319                 :   int i;
   80320               6 :   int rc = 0;
   80321               6 :   sqlite3 *db = sqlite3_context_db_handle(context);
   80322                 :   const char *zName;
   80323                 :   const char *zFile;
   80324               6 :   char *zPath = 0;
   80325               6 :   char *zErr = 0;
   80326                 :   unsigned int flags;
   80327                 :   Db *aNew;
   80328               6 :   char *zErrDyn = 0;
   80329                 :   sqlite3_vfs *pVfs;
   80330                 : 
   80331                 :   UNUSED_PARAMETER(NotUsed);
   80332                 : 
   80333               6 :   zFile = (const char *)sqlite3_value_text(argv[0]);
   80334               6 :   zName = (const char *)sqlite3_value_text(argv[1]);
   80335               6 :   if( zFile==0 ) zFile = "";
   80336               6 :   if( zName==0 ) zName = "";
   80337                 : 
   80338                 :   /* Check for the following errors:
   80339                 :   **
   80340                 :   **     * Too many attached databases,
   80341                 :   **     * Transaction currently open
   80342                 :   **     * Specified database name already being used.
   80343                 :   */
   80344               6 :   if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
   80345               0 :     zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", 
   80346                 :       db->aLimit[SQLITE_LIMIT_ATTACHED]
   80347                 :     );
   80348               0 :     goto attach_error;
   80349                 :   }
   80350               6 :   if( !db->autoCommit ){
   80351               0 :     zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
   80352               0 :     goto attach_error;
   80353                 :   }
   80354              18 :   for(i=0; i<db->nDb; i++){
   80355              12 :     char *z = db->aDb[i].zName;
   80356              12 :     assert( z && zName );
   80357              12 :     if( sqlite3StrICmp(z, zName)==0 ){
   80358               0 :       zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
   80359               0 :       goto attach_error;
   80360                 :     }
   80361                 :   }
   80362                 : 
   80363                 :   /* Allocate the new entry in the db->aDb[] array and initialise the schema
   80364                 :   ** hash tables.
   80365                 :   */
   80366               6 :   if( db->aDb==db->aDbStatic ){
   80367               6 :     aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
   80368               6 :     if( aNew==0 ) return;
   80369               6 :     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
   80370                 :   }else{
   80371               0 :     aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
   80372               0 :     if( aNew==0 ) return;
   80373                 :   }
   80374               6 :   db->aDb = aNew;
   80375               6 :   aNew = &db->aDb[db->nDb];
   80376               6 :   memset(aNew, 0, sizeof(*aNew));
   80377                 : 
   80378                 :   /* Open the database file. If the btree is successfully opened, use
   80379                 :   ** it to obtain the database schema. At this point the schema may
   80380                 :   ** or may not be initialised.
   80381                 :   */
   80382               6 :   flags = db->openFlags;
   80383               6 :   rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
   80384               6 :   if( rc!=SQLITE_OK ){
   80385               0 :     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
   80386               0 :     sqlite3_result_error(context, zErr, -1);
   80387               0 :     sqlite3_free(zErr);
   80388               0 :     return;
   80389                 :   }
   80390               6 :   assert( pVfs );
   80391               6 :   flags |= SQLITE_OPEN_MAIN_DB;
   80392               6 :   rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
   80393               6 :   sqlite3_free( zPath );
   80394               6 :   db->nDb++;
   80395               6 :   if( rc==SQLITE_CONSTRAINT ){
   80396               0 :     rc = SQLITE_ERROR;
   80397               0 :     zErrDyn = sqlite3MPrintf(db, "database is already attached");
   80398               6 :   }else if( rc==SQLITE_OK ){
   80399                 :     Pager *pPager;
   80400               6 :     aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
   80401               6 :     if( !aNew->pSchema ){
   80402               0 :       rc = SQLITE_NOMEM;
   80403               6 :     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
   80404               0 :       zErrDyn = sqlite3MPrintf(db, 
   80405                 :         "attached databases must use the same text encoding as main database");
   80406               0 :       rc = SQLITE_ERROR;
   80407                 :     }
   80408               6 :     pPager = sqlite3BtreePager(aNew->pBt);
   80409               6 :     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
   80410               6 :     sqlite3BtreeSecureDelete(aNew->pBt,
   80411               6 :                              sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
   80412                 :   }
   80413               6 :   aNew->safety_level = 3;
   80414               6 :   aNew->zName = sqlite3DbStrDup(db, zName);
   80415               6 :   if( rc==SQLITE_OK && aNew->zName==0 ){
   80416               0 :     rc = SQLITE_NOMEM;
   80417                 :   }
   80418                 : 
   80419                 : 
   80420                 : #ifdef SQLITE_HAS_CODEC
   80421                 :   if( rc==SQLITE_OK ){
   80422                 :     extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
   80423                 :     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
   80424                 :     int nKey;
   80425                 :     char *zKey;
   80426                 :     int t = sqlite3_value_type(argv[2]);
   80427                 :     switch( t ){
   80428                 :       case SQLITE_INTEGER:
   80429                 :       case SQLITE_FLOAT:
   80430                 :         zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
   80431                 :         rc = SQLITE_ERROR;
   80432                 :         break;
   80433                 :         
   80434                 :       case SQLITE_TEXT:
   80435                 :       case SQLITE_BLOB:
   80436                 :         nKey = sqlite3_value_bytes(argv[2]);
   80437                 :         zKey = (char *)sqlite3_value_blob(argv[2]);
   80438                 :         rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
   80439                 :         break;
   80440                 : 
   80441                 :       case SQLITE_NULL:
   80442                 :         /* No key specified.  Use the key from the main database */
   80443                 :         sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
   80444                 :         if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
   80445                 :           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
   80446                 :         }
   80447                 :         break;
   80448                 :     }
   80449                 :   }
   80450                 : #endif
   80451                 : 
   80452                 :   /* If the file was opened successfully, read the schema for the new database.
   80453                 :   ** If this fails, or if opening the file failed, then close the file and 
   80454                 :   ** remove the entry from the db->aDb[] array. i.e. put everything back the way
   80455                 :   ** we found it.
   80456                 :   */
   80457               6 :   if( rc==SQLITE_OK ){
   80458               6 :     sqlite3BtreeEnterAll(db);
   80459               6 :     rc = sqlite3Init(db, &zErrDyn);
   80460               6 :     sqlite3BtreeLeaveAll(db);
   80461                 :   }
   80462               6 :   if( rc ){
   80463               0 :     int iDb = db->nDb - 1;
   80464               0 :     assert( iDb>=2 );
   80465               0 :     if( db->aDb[iDb].pBt ){
   80466               0 :       sqlite3BtreeClose(db->aDb[iDb].pBt);
   80467               0 :       db->aDb[iDb].pBt = 0;
   80468               0 :       db->aDb[iDb].pSchema = 0;
   80469                 :     }
   80470               0 :     sqlite3ResetInternalSchema(db, -1);
   80471               0 :     db->nDb = iDb;
   80472               0 :     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
   80473               0 :       db->mallocFailed = 1;
   80474               0 :       sqlite3DbFree(db, zErrDyn);
   80475               0 :       zErrDyn = sqlite3MPrintf(db, "out of memory");
   80476               0 :     }else if( zErrDyn==0 ){
   80477               0 :       zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
   80478                 :     }
   80479               0 :     goto attach_error;
   80480                 :   }
   80481                 :   
   80482               6 :   return;
   80483                 : 
   80484                 : attach_error:
   80485                 :   /* Return an error if we get here */
   80486               0 :   if( zErrDyn ){
   80487               0 :     sqlite3_result_error(context, zErrDyn, -1);
   80488               0 :     sqlite3DbFree(db, zErrDyn);
   80489                 :   }
   80490               0 :   if( rc ) sqlite3_result_error_code(context, rc);
   80491                 : }
   80492                 : 
   80493                 : /*
   80494                 : ** An SQL user-function registered to do the work of an DETACH statement. The
   80495                 : ** three arguments to the function come directly from a detach statement:
   80496                 : **
   80497                 : **     DETACH DATABASE x
   80498                 : **
   80499                 : **     SELECT sqlite_detach(x)
   80500                 : */
   80501               0 : static void detachFunc(
   80502                 :   sqlite3_context *context,
   80503                 :   int NotUsed,
   80504                 :   sqlite3_value **argv
   80505                 : ){
   80506               0 :   const char *zName = (const char *)sqlite3_value_text(argv[0]);
   80507               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   80508                 :   int i;
   80509               0 :   Db *pDb = 0;
   80510                 :   char zErr[128];
   80511                 : 
   80512                 :   UNUSED_PARAMETER(NotUsed);
   80513                 : 
   80514               0 :   if( zName==0 ) zName = "";
   80515               0 :   for(i=0; i<db->nDb; i++){
   80516               0 :     pDb = &db->aDb[i];
   80517               0 :     if( pDb->pBt==0 ) continue;
   80518               0 :     if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
   80519                 :   }
   80520                 : 
   80521               0 :   if( i>=db->nDb ){
   80522               0 :     sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
   80523               0 :     goto detach_error;
   80524                 :   }
   80525               0 :   if( i<2 ){
   80526               0 :     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
   80527               0 :     goto detach_error;
   80528                 :   }
   80529               0 :   if( !db->autoCommit ){
   80530               0 :     sqlite3_snprintf(sizeof(zErr), zErr,
   80531                 :                      "cannot DETACH database within transaction");
   80532               0 :     goto detach_error;
   80533                 :   }
   80534               0 :   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
   80535               0 :     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
   80536               0 :     goto detach_error;
   80537                 :   }
   80538                 : 
   80539               0 :   sqlite3BtreeClose(pDb->pBt);
   80540               0 :   pDb->pBt = 0;
   80541               0 :   pDb->pSchema = 0;
   80542               0 :   sqlite3ResetInternalSchema(db, -1);
   80543               0 :   return;
   80544                 : 
   80545                 : detach_error:
   80546               0 :   sqlite3_result_error(context, zErr, -1);
   80547                 : }
   80548                 : 
   80549                 : /*
   80550                 : ** This procedure generates VDBE code for a single invocation of either the
   80551                 : ** sqlite_detach() or sqlite_attach() SQL user functions.
   80552                 : */
   80553               6 : static void codeAttach(
   80554                 :   Parse *pParse,       /* The parser context */
   80555                 :   int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
   80556                 :   FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
   80557                 :   Expr *pAuthArg,      /* Expression to pass to authorization callback */
   80558                 :   Expr *pFilename,     /* Name of database file */
   80559                 :   Expr *pDbname,       /* Name of the database to use internally */
   80560                 :   Expr *pKey           /* Database key for encryption extension */
   80561                 : ){
   80562                 :   int rc;
   80563                 :   NameContext sName;
   80564                 :   Vdbe *v;
   80565               6 :   sqlite3* db = pParse->db;
   80566                 :   int regArgs;
   80567                 : 
   80568               6 :   memset(&sName, 0, sizeof(NameContext));
   80569               6 :   sName.pParse = pParse;
   80570                 : 
   80571               6 :   if( 
   80572               6 :       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
   80573               6 :       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
   80574                 :       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
   80575                 :   ){
   80576               0 :     pParse->nErr++;
   80577               0 :     goto attach_end;
   80578                 :   }
   80579                 : 
   80580                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   80581               6 :   if( pAuthArg ){
   80582                 :     char *zAuthArg;
   80583               6 :     if( pAuthArg->op==TK_STRING ){
   80584               6 :       zAuthArg = pAuthArg->u.zToken;
   80585                 :     }else{
   80586               0 :       zAuthArg = 0;
   80587                 :     }
   80588               6 :     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
   80589               6 :     if(rc!=SQLITE_OK ){
   80590               0 :       goto attach_end;
   80591                 :     }
   80592                 :   }
   80593                 : #endif /* SQLITE_OMIT_AUTHORIZATION */
   80594                 : 
   80595                 : 
   80596               6 :   v = sqlite3GetVdbe(pParse);
   80597               6 :   regArgs = sqlite3GetTempRange(pParse, 4);
   80598               6 :   sqlite3ExprCode(pParse, pFilename, regArgs);
   80599               6 :   sqlite3ExprCode(pParse, pDbname, regArgs+1);
   80600               6 :   sqlite3ExprCode(pParse, pKey, regArgs+2);
   80601                 : 
   80602               6 :   assert( v || db->mallocFailed );
   80603               6 :   if( v ){
   80604               6 :     sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
   80605               6 :     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
   80606               6 :     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
   80607               6 :     sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
   80608                 : 
   80609                 :     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
   80610                 :     ** statement only). For DETACH, set it to false (expire all existing
   80611                 :     ** statements).
   80612                 :     */
   80613               6 :     sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
   80614                 :   }
   80615                 :   
   80616                 : attach_end:
   80617               6 :   sqlite3ExprDelete(db, pFilename);
   80618               6 :   sqlite3ExprDelete(db, pDbname);
   80619               6 :   sqlite3ExprDelete(db, pKey);
   80620               6 : }
   80621                 : 
   80622                 : /*
   80623                 : ** Called by the parser to compile a DETACH statement.
   80624                 : **
   80625                 : **     DETACH pDbname
   80626                 : */
   80627               0 : SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
   80628                 :   static const FuncDef detach_func = {
   80629                 :     1,                /* nArg */
   80630                 :     SQLITE_UTF8,      /* iPrefEnc */
   80631                 :     0,                /* flags */
   80632                 :     0,                /* pUserData */
   80633                 :     0,                /* pNext */
   80634                 :     detachFunc,       /* xFunc */
   80635                 :     0,                /* xStep */
   80636                 :     0,                /* xFinalize */
   80637                 :     "sqlite_detach",  /* zName */
   80638                 :     0,                /* pHash */
   80639                 :     0                 /* pDestructor */
   80640                 :   };
   80641               0 :   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
   80642               0 : }
   80643                 : 
   80644                 : /*
   80645                 : ** Called by the parser to compile an ATTACH statement.
   80646                 : **
   80647                 : **     ATTACH p AS pDbname KEY pKey
   80648                 : */
   80649               6 : SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
   80650                 :   static const FuncDef attach_func = {
   80651                 :     3,                /* nArg */
   80652                 :     SQLITE_UTF8,      /* iPrefEnc */
   80653                 :     0,                /* flags */
   80654                 :     0,                /* pUserData */
   80655                 :     0,                /* pNext */
   80656                 :     attachFunc,       /* xFunc */
   80657                 :     0,                /* xStep */
   80658                 :     0,                /* xFinalize */
   80659                 :     "sqlite_attach",  /* zName */
   80660                 :     0,                /* pHash */
   80661                 :     0                 /* pDestructor */
   80662                 :   };
   80663               6 :   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
   80664               6 : }
   80665                 : #endif /* SQLITE_OMIT_ATTACH */
   80666                 : 
   80667                 : /*
   80668                 : ** Initialize a DbFixer structure.  This routine must be called prior
   80669                 : ** to passing the structure to one of the sqliteFixAAAA() routines below.
   80670                 : **
   80671                 : ** The return value indicates whether or not fixation is required.  TRUE
   80672                 : ** means we do need to fix the database references, FALSE means we do not.
   80673                 : */
   80674           41511 : SQLITE_PRIVATE int sqlite3FixInit(
   80675                 :   DbFixer *pFix,      /* The fixer to be initialized */
   80676                 :   Parse *pParse,      /* Error messages will be written here */
   80677                 :   int iDb,            /* This is the database that must be used */
   80678                 :   const char *zType,  /* "view", "trigger", or "index" */
   80679                 :   const Token *pName  /* Name of the view, trigger, or index */
   80680                 : ){
   80681                 :   sqlite3 *db;
   80682                 : 
   80683           41511 :   if( NEVER(iDb<0) || iDb==1 ) return 0;
   80684           30911 :   db = pParse->db;
   80685           30911 :   assert( db->nDb>iDb );
   80686           30911 :   pFix->pParse = pParse;
   80687           30911 :   pFix->zDb = db->aDb[iDb].zName;
   80688           30911 :   pFix->zType = zType;
   80689           30911 :   pFix->pName = pName;
   80690           30911 :   return 1;
   80691                 : }
   80692                 : 
   80693                 : /*
   80694                 : ** The following set of routines walk through the parse tree and assign
   80695                 : ** a specific database to all table references where the database name
   80696                 : ** was left unspecified in the original SQL statement.  The pFix structure
   80697                 : ** must have been initialized by a prior call to sqlite3FixInit().
   80698                 : **
   80699                 : ** These routines are used to make sure that an index, trigger, or
   80700                 : ** view in one database does not refer to objects in a different database.
   80701                 : ** (Exception: indices, triggers, and views in the TEMP database are
   80702                 : ** allowed to refer to anything.)  If a reference is explicitly made
   80703                 : ** to an object in a different database, an error message is added to
   80704                 : ** pParse->zErrMsg and these routines return non-zero.  If everything
   80705                 : ** checks out, these routines return 0.
   80706                 : */
   80707           26611 : SQLITE_PRIVATE int sqlite3FixSrcList(
   80708                 :   DbFixer *pFix,       /* Context of the fixation */
   80709                 :   SrcList *pList       /* The Source list to check and modify */
   80710                 : ){
   80711                 :   int i;
   80712                 :   const char *zDb;
   80713                 :   struct SrcList_item *pItem;
   80714                 : 
   80715           26611 :   if( NEVER(pList==0) ) return 0;
   80716           26611 :   zDb = pFix->zDb;
   80717           51326 :   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
   80718           24715 :     if( pItem->zDatabase==0 ){
   80719           24715 :       pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
   80720               0 :     }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
   80721               0 :       sqlite3ErrorMsg(pFix->pParse,
   80722                 :          "%s %T cannot reference objects in database %s",
   80723                 :          pFix->zType, pFix->pName, pItem->zDatabase);
   80724               0 :       return 1;
   80725                 :     }
   80726                 : #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
   80727           24715 :     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
   80728           24715 :     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
   80729                 : #endif
   80730                 :   }
   80731           26611 :   return 0;
   80732                 : }
   80733                 : #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
   80734           34703 : SQLITE_PRIVATE int sqlite3FixSelect(
   80735                 :   DbFixer *pFix,       /* Context of the fixation */
   80736                 :   Select *pSelect      /* The SELECT statement to be fixed to one database */
   80737                 : ){
   80738           71316 :   while( pSelect ){
   80739            1910 :     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
   80740               0 :       return 1;
   80741                 :     }
   80742            1910 :     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
   80743               0 :       return 1;
   80744                 :     }
   80745            1910 :     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
   80746               0 :       return 1;
   80747                 :     }
   80748            1910 :     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
   80749               0 :       return 1;
   80750                 :     }
   80751            1910 :     pSelect = pSelect->pPrior;
   80752                 :   }
   80753           34703 :   return 0;
   80754                 : }
   80755           71239 : SQLITE_PRIVATE int sqlite3FixExpr(
   80756                 :   DbFixer *pFix,     /* Context of the fixation */
   80757                 :   Expr *pExpr        /* The expression to be fixed to one database */
   80758                 : ){
   80759          169502 :   while( pExpr ){
   80760           51372 :     if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
   80761           27024 :     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   80762              14 :       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
   80763                 :     }else{
   80764           27010 :       if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
   80765                 :     }
   80766           27024 :     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
   80767               0 :       return 1;
   80768                 :     }
   80769           27024 :     pExpr = pExpr->pLeft;
   80770                 :   }
   80771           71239 :   return 0;
   80772                 : }
   80773           38894 : SQLITE_PRIVATE int sqlite3FixExprList(
   80774                 :   DbFixer *pFix,     /* Context of the fixation */
   80775                 :   ExprList *pList    /* The expression to be fixed to one database */
   80776                 : ){
   80777                 :   int i;
   80778                 :   struct ExprList_item *pItem;
   80779           38894 :   if( pList==0 ) return 0;
   80780            9516 :   for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
   80781            5706 :     if( sqlite3FixExpr(pFix, pItem->pExpr) ){
   80782               0 :       return 1;
   80783                 :     }
   80784                 :   }
   80785            3810 :   return 0;
   80786                 : }
   80787                 : #endif
   80788                 : 
   80789                 : #ifndef SQLITE_OMIT_TRIGGER
   80790            6210 : SQLITE_PRIVATE int sqlite3FixTriggerStep(
   80791                 :   DbFixer *pFix,     /* Context of the fixation */
   80792                 :   TriggerStep *pStep /* The trigger step be fixed to one database */
   80793                 : ){
   80794           22394 :   while( pStep ){
   80795            9974 :     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
   80796               0 :       return 1;
   80797                 :     }
   80798            9974 :     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
   80799               0 :       return 1;
   80800                 :     }
   80801            9974 :     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
   80802               0 :       return 1;
   80803                 :     }
   80804            9974 :     pStep = pStep->pNext;
   80805                 :   }
   80806            6210 :   return 0;
   80807                 : }
   80808                 : #endif
   80809                 : 
   80810                 : /************** End of attach.c **********************************************/
   80811                 : /************** Begin file auth.c ********************************************/
   80812                 : /*
   80813                 : ** 2003 January 11
   80814                 : **
   80815                 : ** The author disclaims copyright to this source code.  In place of
   80816                 : ** a legal notice, here is a blessing:
   80817                 : **
   80818                 : **    May you do good and not evil.
   80819                 : **    May you find forgiveness for yourself and forgive others.
   80820                 : **    May you share freely, never taking more than you give.
   80821                 : **
   80822                 : *************************************************************************
   80823                 : ** This file contains code used to implement the sqlite3_set_authorizer()
   80824                 : ** API.  This facility is an optional feature of the library.  Embedded
   80825                 : ** systems that do not need this facility may omit it by recompiling
   80826                 : ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
   80827                 : */
   80828                 : 
   80829                 : /*
   80830                 : ** All of the code in this file may be omitted by defining a single
   80831                 : ** macro.
   80832                 : */
   80833                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   80834                 : 
   80835                 : /*
   80836                 : ** Set or clear the access authorization function.
   80837                 : **
   80838                 : ** The access authorization function is be called during the compilation
   80839                 : ** phase to verify that the user has read and/or write access permission on
   80840                 : ** various fields of the database.  The first argument to the auth function
   80841                 : ** is a copy of the 3rd argument to this routine.  The second argument
   80842                 : ** to the auth function is one of these constants:
   80843                 : **
   80844                 : **       SQLITE_CREATE_INDEX
   80845                 : **       SQLITE_CREATE_TABLE
   80846                 : **       SQLITE_CREATE_TEMP_INDEX
   80847                 : **       SQLITE_CREATE_TEMP_TABLE
   80848                 : **       SQLITE_CREATE_TEMP_TRIGGER
   80849                 : **       SQLITE_CREATE_TEMP_VIEW
   80850                 : **       SQLITE_CREATE_TRIGGER
   80851                 : **       SQLITE_CREATE_VIEW
   80852                 : **       SQLITE_DELETE
   80853                 : **       SQLITE_DROP_INDEX
   80854                 : **       SQLITE_DROP_TABLE
   80855                 : **       SQLITE_DROP_TEMP_INDEX
   80856                 : **       SQLITE_DROP_TEMP_TABLE
   80857                 : **       SQLITE_DROP_TEMP_TRIGGER
   80858                 : **       SQLITE_DROP_TEMP_VIEW
   80859                 : **       SQLITE_DROP_TRIGGER
   80860                 : **       SQLITE_DROP_VIEW
   80861                 : **       SQLITE_INSERT
   80862                 : **       SQLITE_PRAGMA
   80863                 : **       SQLITE_READ
   80864                 : **       SQLITE_SELECT
   80865                 : **       SQLITE_TRANSACTION
   80866                 : **       SQLITE_UPDATE
   80867                 : **
   80868                 : ** The third and fourth arguments to the auth function are the name of
   80869                 : ** the table and the column that are being accessed.  The auth function
   80870                 : ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
   80871                 : ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
   80872                 : ** means that the SQL statement will never-run - the sqlite3_exec() call
   80873                 : ** will return with an error.  SQLITE_IGNORE means that the SQL statement
   80874                 : ** should run but attempts to read the specified column will return NULL
   80875                 : ** and attempts to write the column will be ignored.
   80876                 : **
   80877                 : ** Setting the auth function to NULL disables this hook.  The default
   80878                 : ** setting of the auth function is NULL.
   80879                 : */
   80880               0 : SQLITE_API int sqlite3_set_authorizer(
   80881                 :   sqlite3 *db,
   80882                 :   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
   80883                 :   void *pArg
   80884                 : ){
   80885               0 :   sqlite3_mutex_enter(db->mutex);
   80886               0 :   db->xAuth = xAuth;
   80887               0 :   db->pAuthArg = pArg;
   80888               0 :   sqlite3ExpirePreparedStatements(db);
   80889               0 :   sqlite3_mutex_leave(db->mutex);
   80890               0 :   return SQLITE_OK;
   80891                 : }
   80892                 : 
   80893                 : /*
   80894                 : ** Write an error message into pParse->zErrMsg that explains that the
   80895                 : ** user-supplied authorization function returned an illegal value.
   80896                 : */
   80897               0 : static void sqliteAuthBadReturnCode(Parse *pParse){
   80898               0 :   sqlite3ErrorMsg(pParse, "authorizer malfunction");
   80899               0 :   pParse->rc = SQLITE_ERROR;
   80900               0 : }
   80901                 : 
   80902                 : /*
   80903                 : ** Invoke the authorization callback for permission to read column zCol from
   80904                 : ** table zTab in database zDb. This function assumes that an authorization
   80905                 : ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
   80906                 : **
   80907                 : ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
   80908                 : ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
   80909                 : ** is treated as SQLITE_DENY. In this case an error is left in pParse.
   80910                 : */
   80911               0 : SQLITE_PRIVATE int sqlite3AuthReadCol(
   80912                 :   Parse *pParse,                  /* The parser context */
   80913                 :   const char *zTab,               /* Table name */
   80914                 :   const char *zCol,               /* Column name */
   80915                 :   int iDb                         /* Index of containing database. */
   80916                 : ){
   80917               0 :   sqlite3 *db = pParse->db;       /* Database handle */
   80918               0 :   char *zDb = db->aDb[iDb].zName; /* Name of attached database */
   80919                 :   int rc;                         /* Auth callback return code */
   80920                 : 
   80921               0 :   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
   80922               0 :   if( rc==SQLITE_DENY ){
   80923               0 :     if( db->nDb>2 || iDb!=0 ){
   80924               0 :       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
   80925                 :     }else{
   80926               0 :       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
   80927                 :     }
   80928               0 :     pParse->rc = SQLITE_AUTH;
   80929               0 :   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
   80930               0 :     sqliteAuthBadReturnCode(pParse);
   80931                 :   }
   80932               0 :   return rc;
   80933                 : }
   80934                 : 
   80935                 : /*
   80936                 : ** The pExpr should be a TK_COLUMN expression.  The table referred to
   80937                 : ** is in pTabList or else it is the NEW or OLD table of a trigger.  
   80938                 : ** Check to see if it is OK to read this particular column.
   80939                 : **
   80940                 : ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
   80941                 : ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
   80942                 : ** then generate an error.
   80943                 : */
   80944          430047 : SQLITE_PRIVATE void sqlite3AuthRead(
   80945                 :   Parse *pParse,        /* The parser context */
   80946                 :   Expr *pExpr,          /* The expression to check authorization on */
   80947                 :   Schema *pSchema,      /* The schema of the expression */
   80948                 :   SrcList *pTabList     /* All table that pExpr might refer to */
   80949                 : ){
   80950          430047 :   sqlite3 *db = pParse->db;
   80951          430047 :   Table *pTab = 0;      /* The table being read */
   80952                 :   const char *zCol;     /* Name of the column of the table */
   80953                 :   int iSrc;             /* Index in pTabList->a[] of table being read */
   80954                 :   int iDb;              /* The index of the database the expression refers to */
   80955                 :   int iCol;             /* Index of column in table */
   80956                 : 
   80957          430047 :   if( db->xAuth==0 ) return;
   80958               0 :   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
   80959               0 :   if( iDb<0 ){
   80960                 :     /* An attempt to read a column out of a subquery or other
   80961                 :     ** temporary table. */
   80962               0 :     return;
   80963                 :   }
   80964                 : 
   80965               0 :   assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
   80966               0 :   if( pExpr->op==TK_TRIGGER ){
   80967               0 :     pTab = pParse->pTriggerTab;
   80968                 :   }else{
   80969               0 :     assert( pTabList );
   80970               0 :     for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
   80971               0 :       if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
   80972               0 :         pTab = pTabList->a[iSrc].pTab;
   80973               0 :         break;
   80974                 :       }
   80975                 :     }
   80976                 :   }
   80977               0 :   iCol = pExpr->iColumn;
   80978               0 :   if( NEVER(pTab==0) ) return;
   80979                 : 
   80980               0 :   if( iCol>=0 ){
   80981               0 :     assert( iCol<pTab->nCol );
   80982               0 :     zCol = pTab->aCol[iCol].zName;
   80983               0 :   }else if( pTab->iPKey>=0 ){
   80984               0 :     assert( pTab->iPKey<pTab->nCol );
   80985               0 :     zCol = pTab->aCol[pTab->iPKey].zName;
   80986                 :   }else{
   80987               0 :     zCol = "ROWID";
   80988                 :   }
   80989               0 :   assert( iDb>=0 && iDb<db->nDb );
   80990               0 :   if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
   80991               0 :     pExpr->op = TK_NULL;
   80992                 :   }
   80993                 : }
   80994                 : 
   80995                 : /*
   80996                 : ** Do an authorization check using the code and arguments given.  Return
   80997                 : ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
   80998                 : ** is returned, then the error count and error message in pParse are
   80999                 : ** modified appropriately.
   81000                 : */
   81001          369743 : SQLITE_PRIVATE int sqlite3AuthCheck(
   81002                 :   Parse *pParse,
   81003                 :   int code,
   81004                 :   const char *zArg1,
   81005                 :   const char *zArg2,
   81006                 :   const char *zArg3
   81007                 : ){
   81008          369743 :   sqlite3 *db = pParse->db;
   81009                 :   int rc;
   81010                 : 
   81011                 :   /* Don't do any authorization checks if the database is initialising
   81012                 :   ** or if the parser is being invoked from within sqlite3_declare_vtab.
   81013                 :   */
   81014          369743 :   if( db->init.busy || IN_DECLARE_VTAB ){
   81015          144880 :     return SQLITE_OK;
   81016                 :   }
   81017                 : 
   81018          224863 :   if( db->xAuth==0 ){
   81019          224863 :     return SQLITE_OK;
   81020                 :   }
   81021               0 :   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
   81022               0 :   if( rc==SQLITE_DENY ){
   81023               0 :     sqlite3ErrorMsg(pParse, "not authorized");
   81024               0 :     pParse->rc = SQLITE_AUTH;
   81025               0 :   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
   81026               0 :     rc = SQLITE_DENY;
   81027               0 :     sqliteAuthBadReturnCode(pParse);
   81028                 :   }
   81029               0 :   return rc;
   81030                 : }
   81031                 : 
   81032                 : /*
   81033                 : ** Push an authorization context.  After this routine is called, the
   81034                 : ** zArg3 argument to authorization callbacks will be zContext until
   81035                 : ** popped.  Or if pParse==0, this routine is a no-op.
   81036                 : */
   81037              59 : SQLITE_PRIVATE void sqlite3AuthContextPush(
   81038                 :   Parse *pParse,
   81039                 :   AuthContext *pContext, 
   81040                 :   const char *zContext
   81041                 : ){
   81042              59 :   assert( pParse );
   81043              59 :   pContext->pParse = pParse;
   81044              59 :   pContext->zAuthContext = pParse->zAuthContext;
   81045              59 :   pParse->zAuthContext = zContext;
   81046              59 : }
   81047                 : 
   81048                 : /*
   81049                 : ** Pop an authorization context that was previously pushed
   81050                 : ** by sqlite3AuthContextPush
   81051                 : */
   81052           21382 : SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
   81053           21382 :   if( pContext->pParse ){
   81054              59 :     pContext->pParse->zAuthContext = pContext->zAuthContext;
   81055              59 :     pContext->pParse = 0;
   81056                 :   }
   81057           21382 : }
   81058                 : 
   81059                 : #endif /* SQLITE_OMIT_AUTHORIZATION */
   81060                 : 
   81061                 : /************** End of auth.c ************************************************/
   81062                 : /************** Begin file build.c *******************************************/
   81063                 : /*
   81064                 : ** 2001 September 15
   81065                 : **
   81066                 : ** The author disclaims copyright to this source code.  In place of
   81067                 : ** a legal notice, here is a blessing:
   81068                 : **
   81069                 : **    May you do good and not evil.
   81070                 : **    May you find forgiveness for yourself and forgive others.
   81071                 : **    May you share freely, never taking more than you give.
   81072                 : **
   81073                 : *************************************************************************
   81074                 : ** This file contains C code routines that are called by the SQLite parser
   81075                 : ** when syntax rules are reduced.  The routines in this file handle the
   81076                 : ** following kinds of SQL syntax:
   81077                 : **
   81078                 : **     CREATE TABLE
   81079                 : **     DROP TABLE
   81080                 : **     CREATE INDEX
   81081                 : **     DROP INDEX
   81082                 : **     creating ID lists
   81083                 : **     BEGIN TRANSACTION
   81084                 : **     COMMIT
   81085                 : **     ROLLBACK
   81086                 : */
   81087                 : 
   81088                 : /*
   81089                 : ** This routine is called when a new SQL statement is beginning to
   81090                 : ** be parsed.  Initialize the pParse structure as needed.
   81091                 : */
   81092          193857 : SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
   81093          193857 :   pParse->explain = (u8)explainFlag;
   81094          193857 :   pParse->nVar = 0;
   81095          193857 : }
   81096                 : 
   81097                 : #ifndef SQLITE_OMIT_SHARED_CACHE
   81098                 : /*
   81099                 : ** The TableLock structure is only used by the sqlite3TableLock() and
   81100                 : ** codeTableLocks() functions.
   81101                 : */
   81102                 : struct TableLock {
   81103                 :   int iDb;             /* The database containing the table to be locked */
   81104                 :   int iTab;            /* The root page of the table to be locked */
   81105                 :   u8 isWriteLock;      /* True for write lock.  False for a read lock */
   81106                 :   const char *zName;   /* Name of the table */
   81107                 : };
   81108                 : 
   81109                 : /*
   81110                 : ** Record the fact that we want to lock a table at run-time.  
   81111                 : **
   81112                 : ** The table to be locked has root page iTab and is found in database iDb.
   81113                 : ** A read or a write lock can be taken depending on isWritelock.
   81114                 : **
   81115                 : ** This routine just records the fact that the lock is desired.  The
   81116                 : ** code to make the lock occur is generated by a later call to
   81117                 : ** codeTableLocks() which occurs during sqlite3FinishCoding().
   81118                 : */
   81119          141344 : SQLITE_PRIVATE void sqlite3TableLock(
   81120                 :   Parse *pParse,     /* Parsing context */
   81121                 :   int iDb,           /* Index of the database containing the table to lock */
   81122                 :   int iTab,          /* Root page number of the table to be locked */
   81123                 :   u8 isWriteLock,    /* True for a write lock */
   81124                 :   const char *zName  /* Name of the table to be locked */
   81125                 : ){
   81126          141344 :   Parse *pToplevel = sqlite3ParseToplevel(pParse);
   81127                 :   int i;
   81128                 :   int nBytes;
   81129                 :   TableLock *p;
   81130          141344 :   assert( iDb>=0 );
   81131                 : 
   81132          193806 :   for(i=0; i<pToplevel->nTableLock; i++){
   81133           92969 :     p = &pToplevel->aTableLock[i];
   81134           92969 :     if( p->iDb==iDb && p->iTab==iTab ){
   81135           40507 :       p->isWriteLock = (p->isWriteLock || isWriteLock);
   81136           40507 :       return;
   81137                 :     }
   81138                 :   }
   81139                 : 
   81140          100837 :   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
   81141          100837 :   pToplevel->aTableLock =
   81142          100837 :       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
   81143          100837 :   if( pToplevel->aTableLock ){
   81144          100837 :     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
   81145          100837 :     p->iDb = iDb;
   81146          100837 :     p->iTab = iTab;
   81147          100837 :     p->isWriteLock = isWriteLock;
   81148          100837 :     p->zName = zName;
   81149                 :   }else{
   81150               0 :     pToplevel->nTableLock = 0;
   81151               0 :     pToplevel->db->mallocFailed = 1;
   81152                 :   }
   81153                 : }
   81154                 : 
   81155                 : /*
   81156                 : ** Code an OP_TableLock instruction for each table locked by the
   81157                 : ** statement (configured by calls to sqlite3TableLock()).
   81158                 : */
   81159           80393 : static void codeTableLocks(Parse *pParse){
   81160                 :   int i;
   81161                 :   Vdbe *pVdbe; 
   81162                 : 
   81163           80393 :   pVdbe = sqlite3GetVdbe(pParse);
   81164           80393 :   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
   81165                 : 
   81166          181229 :   for(i=0; i<pParse->nTableLock; i++){
   81167          100836 :     TableLock *p = &pParse->aTableLock[i];
   81168          100836 :     int p1 = p->iDb;
   81169          100836 :     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
   81170                 :                       p->zName, P4_STATIC);
   81171                 :   }
   81172           80393 : }
   81173                 : #else
   81174                 :   #define codeTableLocks(x)
   81175                 : #endif
   81176                 : 
   81177                 : /*
   81178                 : ** This routine is called after a single SQL statement has been
   81179                 : ** parsed and a VDBE program to execute that statement has been
   81180                 : ** prepared.  This routine puts the finishing touches on the
   81181                 : ** VDBE program and resets the pParse structure for the next
   81182                 : ** parse.
   81183                 : **
   81184                 : ** Note that if an error occurred, it might be the case that
   81185                 : ** no VDBE code was generated.
   81186                 : */
   81187          193834 : SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
   81188                 :   sqlite3 *db;
   81189                 :   Vdbe *v;
   81190                 : 
   81191          193834 :   db = pParse->db;
   81192          193834 :   if( db->mallocFailed ) return;
   81193          193834 :   if( pParse->nested ) return;
   81194          172986 :   if( pParse->nErr ) return;
   81195                 : 
   81196                 :   /* Begin by generating some termination code at the end of the
   81197                 :   ** vdbe program
   81198                 :   */
   81199          172920 :   v = sqlite3GetVdbe(pParse);
   81200          172920 :   assert( !pParse->isMultiWrite 
   81201                 :        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
   81202          172920 :   if( v ){
   81203          172920 :     sqlite3VdbeAddOp0(v, OP_Halt);
   81204                 : 
   81205                 :     /* The cookie mask contains one bit for each database file open.
   81206                 :     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
   81207                 :     ** set for each database that is used.  Generate code to start a
   81208                 :     ** transaction on each used database and to verify the schema cookie
   81209                 :     ** on each used database.
   81210                 :     */
   81211          172920 :     if( pParse->cookieGoto>0 ){
   81212                 :       yDbMask mask;
   81213                 :       int iDb;
   81214           80393 :       sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
   81215          241328 :       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
   81216          160935 :         if( (mask & pParse->cookieMask)==0 ) continue;
   81217           80403 :         sqlite3VdbeUsesBtree(v, iDb);
   81218           80403 :         sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
   81219           80403 :         if( db->init.busy==0 ){
   81220           61195 :           assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   81221           61195 :           sqlite3VdbeAddOp3(v, OP_VerifyCookie,
   81222                 :                             iDb, pParse->cookieValue[iDb],
   81223           61195 :                             db->aDb[iDb].pSchema->iGeneration);
   81224                 :         }
   81225                 :       }
   81226                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   81227                 :       {
   81228                 :         int i;
   81229           80397 :         for(i=0; i<pParse->nVtabLock; i++){
   81230               4 :           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
   81231               4 :           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
   81232                 :         }
   81233           80393 :         pParse->nVtabLock = 0;
   81234                 :       }
   81235                 : #endif
   81236                 : 
   81237                 :       /* Once all the cookies have been verified and transactions opened, 
   81238                 :       ** obtain the required table-locks. This is a no-op unless the 
   81239                 :       ** shared-cache feature is enabled.
   81240                 :       */
   81241           80393 :       codeTableLocks(pParse);
   81242                 : 
   81243                 :       /* Initialize any AUTOINCREMENT data structures required.
   81244                 :       */
   81245           80393 :       sqlite3AutoincrementBegin(pParse);
   81246                 : 
   81247                 :       /* Finally, jump back to the beginning of the executable code. */
   81248           80393 :       sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
   81249                 :     }
   81250                 :   }
   81251                 : 
   81252                 : 
   81253                 :   /* Get the VDBE program ready for execution
   81254                 :   */
   81255          345840 :   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
   81256                 : #ifdef SQLITE_DEBUG
   81257          172920 :     FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
   81258          172920 :     sqlite3VdbeTrace(v, trace);
   81259                 : #endif
   81260          172920 :     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
   81261                 :     /* A minimum of one cursor is required if autoincrement is used
   81262                 :     *  See ticket [a696379c1f08866] */
   81263          172920 :     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
   81264          172920 :     sqlite3VdbeMakeReady(v, pParse);
   81265          172920 :     pParse->rc = SQLITE_DONE;
   81266          172920 :     pParse->colNamesSet = 0;
   81267                 :   }else{
   81268               0 :     pParse->rc = SQLITE_ERROR;
   81269                 :   }
   81270          172920 :   pParse->nTab = 0;
   81271          172920 :   pParse->nMem = 0;
   81272          172920 :   pParse->nSet = 0;
   81273          172920 :   pParse->nVar = 0;
   81274          172920 :   pParse->cookieMask = 0;
   81275          172920 :   pParse->cookieGoto = 0;
   81276                 : }
   81277                 : 
   81278                 : /*
   81279                 : ** Run the parser and code generator recursively in order to generate
   81280                 : ** code for the SQL statement given onto the end of the pParse context
   81281                 : ** currently under construction.  When the parser is run recursively
   81282                 : ** this way, the final OP_Halt is not appended and other initialization
   81283                 : ** and finalization steps are omitted because those are handling by the
   81284                 : ** outermost parser.
   81285                 : **
   81286                 : ** Not everything is nestable.  This facility is designed to permit
   81287                 : ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
   81288                 : ** care if you decide to try to use this routine for some other purposes.
   81289                 : */
   81290           20848 : SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
   81291                 :   va_list ap;
   81292                 :   char *zSql;
   81293           20848 :   char *zErrMsg = 0;
   81294           20848 :   sqlite3 *db = pParse->db;
   81295                 : # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
   81296                 :   char saveBuf[SAVE_SZ];
   81297                 : 
   81298           20848 :   if( pParse->nErr ) return;
   81299           20848 :   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
   81300           20848 :   va_start(ap, zFormat);
   81301           20848 :   zSql = sqlite3VMPrintf(db, zFormat, ap);
   81302           20848 :   va_end(ap);
   81303           20848 :   if( zSql==0 ){
   81304               0 :     return;   /* A malloc must have failed */
   81305                 :   }
   81306           20848 :   pParse->nested++;
   81307           20848 :   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
   81308           20848 :   memset(&pParse->nVar, 0, SAVE_SZ);
   81309           20848 :   sqlite3RunParser(pParse, zSql, &zErrMsg);
   81310           20848 :   sqlite3DbFree(db, zErrMsg);
   81311           20848 :   sqlite3DbFree(db, zSql);
   81312           20848 :   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
   81313           20848 :   pParse->nested--;
   81314                 : }
   81315                 : 
   81316                 : /*
   81317                 : ** Locate the in-memory structure that describes a particular database
   81318                 : ** table given the name of that table and (optionally) the name of the
   81319                 : ** database containing the table.  Return NULL if not found.
   81320                 : **
   81321                 : ** If zDatabase is 0, all databases are searched for the table and the
   81322                 : ** first matching table is returned.  (No checking for duplicate table
   81323                 : ** names is done.)  The search order is TEMP first, then MAIN, then any
   81324                 : ** auxiliary databases added using the ATTACH command.
   81325                 : **
   81326                 : ** See also sqlite3LocateTable().
   81327                 : */
   81328          220139 : SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
   81329          220139 :   Table *p = 0;
   81330                 :   int i;
   81331                 :   int nName;
   81332          220139 :   assert( zName!=0 );
   81333          220139 :   nName = sqlite3Strlen30(zName);
   81334                 :   /* All mutexes are required for schema access.  Make sure we hold them. */
   81335          220139 :   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   81336          476408 :   for(i=OMIT_TEMPDB; i<db->nDb; i++){
   81337          430522 :     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
   81338          430522 :     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
   81339          310543 :     assert( sqlite3SchemaMutexHeld(db, j, 0) );
   81340          310543 :     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
   81341          310543 :     if( p ) break;
   81342                 :   }
   81343          220139 :   return p;
   81344                 : }
   81345                 : 
   81346                 : /*
   81347                 : ** Locate the in-memory structure that describes a particular database
   81348                 : ** table given the name of that table and (optionally) the name of the
   81349                 : ** database containing the table.  Return NULL if not found.  Also leave an
   81350                 : ** error message in pParse->zErrMsg.
   81351                 : **
   81352                 : ** The difference between this routine and sqlite3FindTable() is that this
   81353                 : ** routine leaves an error message in pParse->zErrMsg where
   81354                 : ** sqlite3FindTable() does not.
   81355                 : */
   81356          155088 : SQLITE_PRIVATE Table *sqlite3LocateTable(
   81357                 :   Parse *pParse,         /* context in which to report errors */
   81358                 :   int isView,            /* True if looking for a VIEW rather than a TABLE */
   81359                 :   const char *zName,     /* Name of the table we are looking for */
   81360                 :   const char *zDbase     /* Name of the database.  Might be NULL */
   81361                 : ){
   81362                 :   Table *p;
   81363                 : 
   81364                 :   /* Read the database schema. If an error occurs, leave an error message
   81365                 :   ** and code in pParse and return NULL. */
   81366          155088 :   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   81367               0 :     return 0;
   81368                 :   }
   81369                 : 
   81370          155088 :   p = sqlite3FindTable(pParse->db, zName, zDbase);
   81371          155088 :   if( p==0 ){
   81372               9 :     const char *zMsg = isView ? "no such view" : "no such table";
   81373               9 :     if( zDbase ){
   81374               0 :       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
   81375                 :     }else{
   81376               9 :       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
   81377                 :     }
   81378               9 :     pParse->checkSchema = 1;
   81379                 :   }
   81380          155088 :   return p;
   81381                 : }
   81382                 : 
   81383                 : /*
   81384                 : ** Locate the in-memory structure that describes 
   81385                 : ** a particular index given the name of that index
   81386                 : ** and the name of the database that contains the index.
   81387                 : ** Return NULL if not found.
   81388                 : **
   81389                 : ** If zDatabase is 0, all databases are searched for the
   81390                 : ** table and the first matching index is returned.  (No checking
   81391                 : ** for duplicate index names is done.)  The search order is
   81392                 : ** TEMP first, then MAIN, then any auxiliary databases added
   81393                 : ** using the ATTACH command.
   81394                 : */
   81395           76954 : SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
   81396           76954 :   Index *p = 0;
   81397                 :   int i;
   81398           76954 :   int nName = sqlite3Strlen30(zName);
   81399                 :   /* All mutexes are required for schema access.  Make sure we hold them. */
   81400           76954 :   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   81401          209454 :   for(i=OMIT_TEMPDB; i<db->nDb; i++){
   81402          153953 :     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
   81403          153953 :     Schema *pSchema = db->aDb[j].pSchema;
   81404          153953 :     assert( pSchema );
   81405          153953 :     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
   81406           78461 :     assert( sqlite3SchemaMutexHeld(db, j, 0) );
   81407           78461 :     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
   81408           78461 :     if( p ) break;
   81409                 :   }
   81410           76954 :   return p;
   81411                 : }
   81412                 : 
   81413                 : /*
   81414                 : ** Reclaim the memory used by an index
   81415                 : */
   81416           28810 : static void freeIndex(sqlite3 *db, Index *p){
   81417                 : #ifndef SQLITE_OMIT_ANALYZE
   81418           28810 :   sqlite3DeleteIndexSamples(db, p);
   81419                 : #endif
   81420           28810 :   sqlite3DbFree(db, p->zColAff);
   81421           28810 :   sqlite3DbFree(db, p);
   81422           28810 : }
   81423                 : 
   81424                 : /*
   81425                 : ** For the index called zIdxName which is found in the database iDb,
   81426                 : ** unlike that index from its Table then remove the index from
   81427                 : ** the index hash table and free all memory structures associated
   81428                 : ** with the index.
   81429                 : */
   81430              26 : SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
   81431                 :   Index *pIndex;
   81432                 :   int len;
   81433                 :   Hash *pHash;
   81434                 : 
   81435              26 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   81436              26 :   pHash = &db->aDb[iDb].pSchema->idxHash;
   81437              26 :   len = sqlite3Strlen30(zIdxName);
   81438              26 :   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
   81439              26 :   if( ALWAYS(pIndex) ){
   81440              26 :     if( pIndex->pTable->pIndex==pIndex ){
   81441              26 :       pIndex->pTable->pIndex = pIndex->pNext;
   81442                 :     }else{
   81443                 :       Index *p;
   81444                 :       /* Justification of ALWAYS();  The index must be on the list of
   81445                 :       ** indices. */
   81446               0 :       p = pIndex->pTable->pIndex;
   81447               0 :       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
   81448               0 :       if( ALWAYS(p && p->pNext==pIndex) ){
   81449               0 :         p->pNext = pIndex->pNext;
   81450                 :       }
   81451                 :     }
   81452              26 :     freeIndex(db, pIndex);
   81453                 :   }
   81454              26 :   db->flags |= SQLITE_InternChanges;
   81455              26 : }
   81456                 : 
   81457                 : /*
   81458                 : ** Erase all schema information from the in-memory hash tables of
   81459                 : ** a single database.  This routine is called to reclaim memory
   81460                 : ** before the database closes.  It is also called during a rollback
   81461                 : ** if there were schema changes during the transaction or if a
   81462                 : ** schema-cookie mismatch occurs.
   81463                 : **
   81464                 : ** If iDb<0 then reset the internal schema tables for all database
   81465                 : ** files.  If iDb>=0 then reset the internal schema for only the
   81466                 : ** single file indicated.
   81467                 : */
   81468            6855 : SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
   81469                 :   int i, j;
   81470            6855 :   assert( iDb<db->nDb );
   81471                 : 
   81472            6855 :   if( iDb>=0 ){
   81473                 :     /* Case 1:  Reset the single schema identified by iDb */
   81474              12 :     Db *pDb = &db->aDb[iDb];
   81475              12 :     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   81476              12 :     assert( pDb->pSchema!=0 );
   81477              12 :     sqlite3SchemaClear(pDb->pSchema);
   81478                 : 
   81479                 :     /* If any database other than TEMP is reset, then also reset TEMP
   81480                 :     ** since TEMP might be holding triggers that reference tables in the
   81481                 :     ** other database.
   81482                 :     */
   81483              12 :     if( iDb!=1 ){
   81484              12 :       pDb = &db->aDb[1];
   81485              12 :       assert( pDb->pSchema!=0 );
   81486              12 :       sqlite3SchemaClear(pDb->pSchema);
   81487                 :     }
   81488              12 :     return;
   81489                 :   }
   81490                 :   /* Case 2 (from here to the end): Reset all schemas for all attached
   81491                 :   ** databases. */
   81492            6843 :   assert( iDb<0 );
   81493            6843 :   sqlite3BtreeEnterAll(db);
   81494           20535 :   for(i=0; i<db->nDb; i++){
   81495           13692 :     Db *pDb = &db->aDb[i];
   81496           13692 :     if( pDb->pSchema ){
   81497           10409 :       sqlite3SchemaClear(pDb->pSchema);
   81498                 :     }
   81499                 :   }
   81500            6843 :   db->flags &= ~SQLITE_InternChanges;
   81501            6843 :   sqlite3VtabUnlockList(db);
   81502            6843 :   sqlite3BtreeLeaveAll(db);
   81503                 : 
   81504                 :   /* If one or more of the auxiliary database files has been closed,
   81505                 :   ** then remove them from the auxiliary database list.  We take the
   81506                 :   ** opportunity to do this here since we have just deleted all of the
   81507                 :   ** schema hash tables and therefore do not have to make any changes
   81508                 :   ** to any of those tables.
   81509                 :   */
   81510            6849 :   for(i=j=2; i<db->nDb; i++){
   81511               6 :     struct Db *pDb = &db->aDb[i];
   81512               6 :     if( pDb->pBt==0 ){
   81513               6 :       sqlite3DbFree(db, pDb->zName);
   81514               6 :       pDb->zName = 0;
   81515               6 :       continue;
   81516                 :     }
   81517               0 :     if( j<i ){
   81518               0 :       db->aDb[j] = db->aDb[i];
   81519                 :     }
   81520               0 :     j++;
   81521                 :   }
   81522            6843 :   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
   81523            6843 :   db->nDb = j;
   81524            6843 :   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
   81525               6 :     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
   81526               6 :     sqlite3DbFree(db, db->aDb);
   81527               6 :     db->aDb = db->aDbStatic;
   81528                 :   }
   81529                 : }
   81530                 : 
   81531                 : /*
   81532                 : ** This routine is called when a commit occurs.
   81533                 : */
   81534          183463 : SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
   81535          183463 :   db->flags &= ~SQLITE_InternChanges;
   81536          183463 : }
   81537                 : 
   81538                 : /*
   81539                 : ** Delete memory allocated for the column names of a table or view (the
   81540                 : ** Table.aCol[] array).
   81541                 : */
   81542           36087 : static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
   81543                 :   int i;
   81544                 :   Column *pCol;
   81545           36087 :   assert( pTable!=0 );
   81546           36087 :   if( (pCol = pTable->aCol)!=0 ){
   81547          245406 :     for(i=0; i<pTable->nCol; i++, pCol++){
   81548          209582 :       sqlite3DbFree(db, pCol->zName);
   81549          209582 :       sqlite3ExprDelete(db, pCol->pDflt);
   81550          209582 :       sqlite3DbFree(db, pCol->zDflt);
   81551          209582 :       sqlite3DbFree(db, pCol->zType);
   81552          209582 :       sqlite3DbFree(db, pCol->zColl);
   81553                 :     }
   81554           35824 :     sqlite3DbFree(db, pTable->aCol);
   81555                 :   }
   81556           36087 : }
   81557                 : 
   81558                 : /*
   81559                 : ** Remove the memory data structures associated with the given
   81560                 : ** Table.  No changes are made to disk by this routine.
   81561                 : **
   81562                 : ** This routine just deletes the data structure.  It does not unlink
   81563                 : ** the table data structure from the hash table.  But it does destroy
   81564                 : ** memory structures of the indices and foreign keys associated with 
   81565                 : ** the table.
   81566                 : */
   81567          442696 : SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
   81568                 :   Index *pIndex, *pNext;
   81569                 : 
   81570          442696 :   assert( !pTable || pTable->nRef>0 );
   81571                 : 
   81572                 :   /* Do not delete the table until the reference count reaches zero. */
   81573          442696 :   if( !pTable ) return;
   81574          171134 :   if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
   81575                 : 
   81576                 :   /* Delete all indices associated with this table. */
   81577           64871 :   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
   81578           28784 :     pNext = pIndex->pNext;
   81579           28784 :     assert( pIndex->pSchema==pTable->pSchema );
   81580           28784 :     if( !db || db->pnBytesFreed==0 ){
   81581           28763 :       char *zName = pIndex->zName; 
   81582           57526 :       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
   81583           28763 :           &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
   81584                 :       );
   81585           28763 :       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
   81586           28763 :       assert( pOld==pIndex || pOld==0 );
   81587                 :     }
   81588           28784 :     freeIndex(db, pIndex);
   81589                 :   }
   81590                 : 
   81591                 :   /* Delete any foreign keys attached to this table. */
   81592           36087 :   sqlite3FkDelete(db, pTable);
   81593                 : 
   81594                 :   /* Delete the Table structure itself.
   81595                 :   */
   81596           36087 :   sqliteDeleteColumnNames(db, pTable);
   81597           36087 :   sqlite3DbFree(db, pTable->zName);
   81598           36087 :   sqlite3DbFree(db, pTable->zColAff);
   81599           36087 :   sqlite3SelectDelete(db, pTable->pSelect);
   81600                 : #ifndef SQLITE_OMIT_CHECK
   81601           36087 :   sqlite3ExprDelete(db, pTable->pCheck);
   81602                 : #endif
   81603                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   81604           36087 :   sqlite3VtabClear(db, pTable);
   81605                 : #endif
   81606           36087 :   sqlite3DbFree(db, pTable);
   81607                 : }
   81608                 : 
   81609                 : /*
   81610                 : ** Unlink the given table from the hash tables and the delete the
   81611                 : ** table structure with all its indices and foreign keys.
   81612                 : */
   81613             209 : SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
   81614                 :   Table *p;
   81615                 :   Db *pDb;
   81616                 : 
   81617             209 :   assert( db!=0 );
   81618             209 :   assert( iDb>=0 && iDb<db->nDb );
   81619             209 :   assert( zTabName );
   81620             209 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   81621                 :   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
   81622             209 :   pDb = &db->aDb[iDb];
   81623             209 :   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
   81624                 :                         sqlite3Strlen30(zTabName),0);
   81625             209 :   sqlite3DeleteTable(db, p);
   81626             209 :   db->flags |= SQLITE_InternChanges;
   81627             209 : }
   81628                 : 
   81629                 : /*
   81630                 : ** Given a token, return a string that consists of the text of that
   81631                 : ** token.  Space to hold the returned string
   81632                 : ** is obtained from sqliteMalloc() and must be freed by the calling
   81633                 : ** function.
   81634                 : **
   81635                 : ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
   81636                 : ** surround the body of the token are removed.
   81637                 : **
   81638                 : ** Tokens are often just pointers into the original SQL text and so
   81639                 : ** are not \000 terminated and are not persistent.  The returned string
   81640                 : ** is \000 terminated and is persistent.
   81641                 : */
   81642          930297 : SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
   81643                 :   char *zName;
   81644          930297 :   if( pName ){
   81645          823889 :     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
   81646          823889 :     sqlite3Dequote(zName);
   81647                 :   }else{
   81648          106408 :     zName = 0;
   81649                 :   }
   81650          930297 :   return zName;
   81651                 : }
   81652                 : 
   81653                 : /*
   81654                 : ** Open the sqlite_master table stored in database number iDb for
   81655                 : ** writing. The table is opened using cursor 0.
   81656                 : */
   81657            7292 : SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
   81658            7292 :   Vdbe *v = sqlite3GetVdbe(p);
   81659            7292 :   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
   81660            7292 :   sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
   81661            7292 :   sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
   81662            7292 :   if( p->nTab==0 ){
   81663            6286 :     p->nTab = 1;
   81664                 :   }
   81665            7292 : }
   81666                 : 
   81667                 : /*
   81668                 : ** Parameter zName points to a nul-terminated buffer containing the name
   81669                 : ** of a database ("main", "temp" or the name of an attached db). This
   81670                 : ** function returns the index of the named database in db->aDb[], or
   81671                 : ** -1 if the named db cannot be found.
   81672                 : */
   81673            7802 : SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
   81674            7802 :   int i = -1;         /* Database number */
   81675            7802 :   if( zName ){
   81676                 :     Db *pDb;
   81677            7802 :     int n = sqlite3Strlen30(zName);
   81678           17032 :     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
   81679           28164 :       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && 
   81680           12607 :           0==sqlite3StrICmp(pDb->zName, zName) ){
   81681            6327 :         break;
   81682                 :       }
   81683                 :     }
   81684                 :   }
   81685            7802 :   return i;
   81686                 : }
   81687                 : 
   81688                 : /*
   81689                 : ** The token *pName contains the name of a database (either "main" or
   81690                 : ** "temp" or the name of an attached db). This routine returns the
   81691                 : ** index of the named database in db->aDb[], or -1 if the named db 
   81692                 : ** does not exist.
   81693                 : */
   81694            2531 : SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
   81695                 :   int i;                               /* Database number */
   81696                 :   char *zName;                         /* Name we are searching for */
   81697            2531 :   zName = sqlite3NameFromToken(db, pName);
   81698            2531 :   i = sqlite3FindDbName(db, zName);
   81699            2531 :   sqlite3DbFree(db, zName);
   81700            2531 :   return i;
   81701                 : }
   81702                 : 
   81703                 : /* The table or view or trigger name is passed to this routine via tokens
   81704                 : ** pName1 and pName2. If the table name was fully qualified, for example:
   81705                 : **
   81706                 : ** CREATE TABLE xxx.yyy (...);
   81707                 : ** 
   81708                 : ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
   81709                 : ** the table name is not fully qualified, i.e.:
   81710                 : **
   81711                 : ** CREATE TABLE yyy(...);
   81712                 : **
   81713                 : ** Then pName1 is set to "yyy" and pName2 is "".
   81714                 : **
   81715                 : ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
   81716                 : ** pName2) that stores the unqualified table name.  The index of the
   81717                 : ** database "xxx" is returned.
   81718                 : */
   81719           87069 : SQLITE_PRIVATE int sqlite3TwoPartName(
   81720                 :   Parse *pParse,      /* Parsing and code generating context */
   81721                 :   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
   81722                 :   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
   81723                 :   Token **pUnqual     /* Write the unqualified object name here */
   81724                 : ){
   81725                 :   int iDb;                    /* Database holding the object */
   81726           87069 :   sqlite3 *db = pParse->db;
   81727                 : 
   81728           87069 :   if( ALWAYS(pName2!=0) && pName2->n>0 ){
   81729            1056 :     if( db->init.busy ) {
   81730               0 :       sqlite3ErrorMsg(pParse, "corrupt database");
   81731               0 :       pParse->nErr++;
   81732               0 :       return -1;
   81733                 :     }
   81734            1056 :     *pUnqual = pName2;
   81735            1056 :     iDb = sqlite3FindDb(db, pName1);
   81736            2112 :     if( iDb<0 ){
   81737               0 :       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
   81738               0 :       pParse->nErr++;
   81739               0 :       return -1;
   81740                 :     }
   81741                 :   }else{
   81742           86013 :     assert( db->init.iDb==0 || db->init.busy );
   81743           86013 :     iDb = db->init.iDb;
   81744           86013 :     *pUnqual = pName1;
   81745                 :   }
   81746           87069 :   return iDb;
   81747                 : }
   81748                 : 
   81749                 : /*
   81750                 : ** This routine is used to check if the UTF-8 string zName is a legal
   81751                 : ** unqualified name for a new schema object (table, index, view or
   81752                 : ** trigger). All names are legal except those that begin with the string
   81753                 : ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
   81754                 : ** is reserved for internal use.
   81755                 : */
   81756           65978 : SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
   81757           65978 :   if( !pParse->db->init.busy && pParse->nested==0 
   81758           14939 :           && (pParse->db->flags & SQLITE_WriteSchema)==0
   81759           14899 :           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
   81760               0 :     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
   81761               0 :     return SQLITE_ERROR;
   81762                 :   }
   81763           65978 :   return SQLITE_OK;
   81764                 : }
   81765                 : 
   81766                 : /*
   81767                 : ** Begin constructing a new table representation in memory.  This is
   81768                 : ** the first of several action routines that get called in response
   81769                 : ** to a CREATE TABLE statement.  In particular, this routine is called
   81770                 : ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
   81771                 : ** flag is true if the table should be stored in the auxiliary database
   81772                 : ** file instead of in the main database file.  This is normally the case
   81773                 : ** when the "TEMP" or "TEMPORARY" keyword occurs in between
   81774                 : ** CREATE and TABLE.
   81775                 : **
   81776                 : ** The new table record is initialized and put in pParse->pNewTable.
   81777                 : ** As more of the CREATE TABLE statement is parsed, additional action
   81778                 : ** routines will be called to add more information to this record.
   81779                 : ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
   81780                 : ** is called to complete the construction of the new table record.
   81781                 : */
   81782           35951 : SQLITE_PRIVATE void sqlite3StartTable(
   81783                 :   Parse *pParse,   /* Parser context */
   81784                 :   Token *pName1,   /* First part of the name of the table or view */
   81785                 :   Token *pName2,   /* Second part of the name of the table or view */
   81786                 :   int isTemp,      /* True if this is a TEMP table */
   81787                 :   int isView,      /* True if this is a VIEW */
   81788                 :   int isVirtual,   /* True if this is a VIRTUAL table */
   81789                 :   int noErr        /* Do nothing if table already exists */
   81790                 : ){
   81791                 :   Table *pTable;
   81792           35951 :   char *zName = 0; /* The name of the new table */
   81793           35951 :   sqlite3 *db = pParse->db;
   81794                 :   Vdbe *v;
   81795                 :   int iDb;         /* Database number to create the table in */
   81796                 :   Token *pName;    /* Unqualified name of the table to create */
   81797                 : 
   81798                 :   /* The table or view name to create is passed to this routine via tokens
   81799                 :   ** pName1 and pName2. If the table name was fully qualified, for example:
   81800                 :   **
   81801                 :   ** CREATE TABLE xxx.yyy (...);
   81802                 :   ** 
   81803                 :   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
   81804                 :   ** the table name is not fully qualified, i.e.:
   81805                 :   **
   81806                 :   ** CREATE TABLE yyy(...);
   81807                 :   **
   81808                 :   ** Then pName1 is set to "yyy" and pName2 is "".
   81809                 :   **
   81810                 :   ** The call below sets the pName pointer to point at the token (pName1 or
   81811                 :   ** pName2) that stores the unqualified table name. The variable iDb is
   81812                 :   ** set to the index of the database that the table or view is to be
   81813                 :   ** created in.
   81814                 :   */
   81815           35951 :   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
   81816           35951 :   if( iDb<0 ) return;
   81817           35951 :   if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
   81818                 :     /* If creating a temp table, the name may not be qualified. Unless 
   81819                 :     ** the database name is "temp" anyway.  */
   81820               0 :     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
   81821               0 :     return;
   81822                 :   }
   81823           35951 :   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
   81824                 : 
   81825           35951 :   pParse->sNameToken = *pName;
   81826           35951 :   zName = sqlite3NameFromToken(db, pName);
   81827           35951 :   if( zName==0 ) return;
   81828           35951 :   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   81829               0 :     goto begin_table_error;
   81830                 :   }
   81831           35951 :   if( db->init.iDb==1 ) isTemp = 1;
   81832                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   81833           35951 :   assert( (isTemp & 1)==isTemp );
   81834                 :   {
   81835                 :     int code;
   81836           35951 :     char *zDb = db->aDb[iDb].zName;
   81837           35951 :     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
   81838               0 :       goto begin_table_error;
   81839                 :     }
   81840           35951 :     if( isView ){
   81841             156 :       if( !OMIT_TEMPDB && isTemp ){
   81842             156 :         code = SQLITE_CREATE_TEMP_VIEW;
   81843                 :       }else{
   81844               0 :         code = SQLITE_CREATE_VIEW;
   81845                 :       }
   81846                 :     }else{
   81847           35795 :       if( !OMIT_TEMPDB && isTemp ){
   81848            4182 :         code = SQLITE_CREATE_TEMP_TABLE;
   81849                 :       }else{
   81850           31613 :         code = SQLITE_CREATE_TABLE;
   81851                 :       }
   81852                 :     }
   81853           35951 :     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
   81854               0 :       goto begin_table_error;
   81855                 :     }
   81856                 :   }
   81857                 : #endif
   81858                 : 
   81859                 :   /* Make sure the new table name does not collide with an existing
   81860                 :   ** index or table name in the same database.  Issue an error message if
   81861                 :   ** it does. The exception is if the statement being parsed was passed
   81862                 :   ** to an sqlite3_declare_vtab() call. In that case only the column names
   81863                 :   ** and types will be used, so there is no need to test for namespace
   81864                 :   ** collisions.
   81865                 :   */
   81866           35951 :   if( !IN_DECLARE_VTAB ){
   81867           35898 :     char *zDb = db->aDb[iDb].zName;
   81868           35898 :     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   81869               0 :       goto begin_table_error;
   81870                 :     }
   81871           35898 :     pTable = sqlite3FindTable(db, zName, zDb);
   81872           35898 :     if( pTable ){
   81873             258 :       if( !noErr ){
   81874               1 :         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
   81875                 :       }else{
   81876             257 :         assert( !db->init.busy );
   81877             257 :         sqlite3CodeVerifySchema(pParse, iDb);
   81878                 :       }
   81879             258 :       goto begin_table_error;
   81880                 :     }
   81881           35640 :     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
   81882               0 :       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
   81883               0 :       goto begin_table_error;
   81884                 :     }
   81885                 :   }
   81886                 : 
   81887           35693 :   pTable = sqlite3DbMallocZero(db, sizeof(Table));
   81888           35693 :   if( pTable==0 ){
   81889               0 :     db->mallocFailed = 1;
   81890               0 :     pParse->rc = SQLITE_NOMEM;
   81891               0 :     pParse->nErr++;
   81892               0 :     goto begin_table_error;
   81893                 :   }
   81894           35693 :   pTable->zName = zName;
   81895           35693 :   pTable->iPKey = -1;
   81896           35693 :   pTable->pSchema = db->aDb[iDb].pSchema;
   81897           35693 :   pTable->nRef = 1;
   81898           35693 :   pTable->nRowEst = 1000000;
   81899           35693 :   assert( pParse->pNewTable==0 );
   81900           35693 :   pParse->pNewTable = pTable;
   81901                 : 
   81902                 :   /* If this is the magic sqlite_sequence table used by autoincrement,
   81903                 :   ** then record a pointer to this table in the main database structure
   81904                 :   ** so that INSERT can find the table easily.
   81905                 :   */
   81906                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   81907           35693 :   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
   81908            1673 :     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   81909            1673 :     pTable->pSchema->pSeqTab = pTable;
   81910                 :   }
   81911                 : #endif
   81912                 : 
   81913                 :   /* Begin generating the code that will insert the table record into
   81914                 :   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
   81915                 :   ** and allocate the record number for the table entry now.  Before any
   81916                 :   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
   81917                 :   ** indices to be created and the table record must come before the 
   81918                 :   ** indices.  Hence, the record number for the table must be allocated
   81919                 :   ** now.
   81920                 :   */
   81921           35693 :   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
   81922                 :     int j1;
   81923                 :     int fileFormat;
   81924                 :     int reg1, reg2, reg3;
   81925            7241 :     sqlite3BeginWriteOperation(pParse, 0, iDb);
   81926                 : 
   81927                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   81928            7241 :     if( isVirtual ){
   81929              53 :       sqlite3VdbeAddOp0(v, OP_VBegin);
   81930                 :     }
   81931                 : #endif
   81932                 : 
   81933                 :     /* If the file format and encoding in the database have not been set, 
   81934                 :     ** set them now.
   81935                 :     */
   81936            7241 :     reg1 = pParse->regRowid = ++pParse->nMem;
   81937            7241 :     reg2 = pParse->regRoot = ++pParse->nMem;
   81938            7241 :     reg3 = ++pParse->nMem;
   81939            7241 :     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
   81940            7241 :     sqlite3VdbeUsesBtree(v, iDb);
   81941            7241 :     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
   81942           14482 :     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
   81943            7241 :                   1 : SQLITE_MAX_FILE_FORMAT;
   81944            7241 :     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
   81945            7241 :     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
   81946            7241 :     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
   81947            7241 :     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
   81948            7241 :     sqlite3VdbeJumpHere(v, j1);
   81949                 : 
   81950                 :     /* This just creates a place-holder record in the sqlite_master table.
   81951                 :     ** The record created does not contain anything yet.  It will be replaced
   81952                 :     ** by the real entry in code generated at sqlite3EndTable().
   81953                 :     **
   81954                 :     ** The rowid for the new entry is left in register pParse->regRowid.
   81955                 :     ** The root page number of the new table is left in reg pParse->regRoot.
   81956                 :     ** The rowid and root page number values are needed by the code that
   81957                 :     ** sqlite3EndTable will generate.
   81958                 :     */
   81959                 : #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
   81960            7241 :     if( isView || isVirtual ){
   81961             131 :       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
   81962                 :     }else
   81963                 : #endif
   81964                 :     {
   81965            7110 :       sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
   81966                 :     }
   81967            7241 :     sqlite3OpenMasterTable(pParse, iDb);
   81968            7241 :     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
   81969            7241 :     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
   81970            7241 :     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
   81971            7241 :     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   81972            7241 :     sqlite3VdbeAddOp0(v, OP_Close);
   81973                 :   }
   81974                 : 
   81975                 :   /* Normal (non-error) return. */
   81976           35693 :   return;
   81977                 : 
   81978                 :   /* If an error occurs, we jump here */
   81979                 : begin_table_error:
   81980             258 :   sqlite3DbFree(db, zName);
   81981             258 :   return;
   81982                 : }
   81983                 : 
   81984                 : /*
   81985                 : ** This macro is used to compare two strings in a case-insensitive manner.
   81986                 : ** It is slightly faster than calling sqlite3StrICmp() directly, but
   81987                 : ** produces larger code.
   81988                 : **
   81989                 : ** WARNING: This macro is not compatible with the strcmp() family. It
   81990                 : ** returns true if the two strings are equal, otherwise false.
   81991                 : */
   81992                 : #define STRICMP(x, y) (\
   81993                 : sqlite3UpperToLower[*(unsigned char *)(x)]==   \
   81994                 : sqlite3UpperToLower[*(unsigned char *)(y)]     \
   81995                 : && sqlite3StrICmp((x)+1,(y)+1)==0 )
   81996                 : 
   81997                 : /*
   81998                 : ** Add a new column to the table currently being constructed.
   81999                 : **
   82000                 : ** The parser calls this routine once for each column declaration
   82001                 : ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
   82002                 : ** first to get things going.  Then this routine is called for each
   82003                 : ** column.
   82004                 : */
   82005          208635 : SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
   82006                 :   Table *p;
   82007                 :   int i;
   82008                 :   char *z;
   82009                 :   Column *pCol;
   82010          208635 :   sqlite3 *db = pParse->db;
   82011          208635 :   if( (p = pParse->pNewTable)==0 ) return;
   82012                 : #if SQLITE_MAX_COLUMN
   82013          207180 :   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
   82014               0 :     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
   82015               0 :     return;
   82016                 :   }
   82017                 : #endif
   82018          207180 :   z = sqlite3NameFromToken(db, pName);
   82019          207180 :   if( z==0 ) return;
   82020         1308541 :   for(i=0; i<p->nCol; i++){
   82021         1101379 :     if( STRICMP(z, p->aCol[i].zName) ){
   82022              18 :       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
   82023              18 :       sqlite3DbFree(db, z);
   82024              18 :       return;
   82025                 :     }
   82026                 :   }
   82027          207162 :   if( (p->nCol & 0x7)==0 ){
   82028                 :     Column *aNew;
   82029           44881 :     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
   82030           44881 :     if( aNew==0 ){
   82031               0 :       sqlite3DbFree(db, z);
   82032               0 :       return;
   82033                 :     }
   82034           44881 :     p->aCol = aNew;
   82035                 :   }
   82036          207162 :   pCol = &p->aCol[p->nCol];
   82037          207162 :   memset(pCol, 0, sizeof(p->aCol[0]));
   82038          207162 :   pCol->zName = z;
   82039                 :  
   82040                 :   /* If there is no type specified, columns have the default affinity
   82041                 :   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
   82042                 :   ** be called next to set pCol->affinity correctly.
   82043                 :   */
   82044          207162 :   pCol->affinity = SQLITE_AFF_NONE;
   82045          207162 :   p->nCol++;
   82046                 : }
   82047                 : 
   82048                 : /*
   82049                 : ** This routine is called by the parser while in the middle of
   82050                 : ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
   82051                 : ** been seen on a column.  This routine sets the notNull flag on
   82052                 : ** the column currently under construction.
   82053                 : */
   82054           23903 : SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
   82055                 :   Table *p;
   82056           23903 :   p = pParse->pNewTable;
   82057           23903 :   if( p==0 || NEVER(p->nCol<1) ) return;
   82058           23903 :   p->aCol[p->nCol-1].notNull = (u8)onError;
   82059                 : }
   82060                 : 
   82061                 : /*
   82062                 : ** Scan the column type name zType (length nType) and return the
   82063                 : ** associated affinity type.
   82064                 : **
   82065                 : ** This routine does a case-independent search of zType for the 
   82066                 : ** substrings in the following table. If one of the substrings is
   82067                 : ** found, the corresponding affinity is returned. If zType contains
   82068                 : ** more than one of the substrings, entries toward the top of 
   82069                 : ** the table take priority. For example, if zType is 'BLOBINT', 
   82070                 : ** SQLITE_AFF_INTEGER is returned.
   82071                 : **
   82072                 : ** Substring     | Affinity
   82073                 : ** --------------------------------
   82074                 : ** 'INT'         | SQLITE_AFF_INTEGER
   82075                 : ** 'CHAR'        | SQLITE_AFF_TEXT
   82076                 : ** 'CLOB'        | SQLITE_AFF_TEXT
   82077                 : ** 'TEXT'        | SQLITE_AFF_TEXT
   82078                 : ** 'BLOB'        | SQLITE_AFF_NONE
   82079                 : ** 'REAL'        | SQLITE_AFF_REAL
   82080                 : ** 'FLOA'        | SQLITE_AFF_REAL
   82081                 : ** 'DOUB'        | SQLITE_AFF_REAL
   82082                 : **
   82083                 : ** If none of the substrings in the above table are found,
   82084                 : ** SQLITE_AFF_NUMERIC is returned.
   82085                 : */
   82086          198702 : SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
   82087          198702 :   u32 h = 0;
   82088          198702 :   char aff = SQLITE_AFF_NUMERIC;
   82089                 : 
   82090         1073305 :   if( zIn ) while( zIn[0] ){
   82091          774118 :     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
   82092          774118 :     zIn++;
   82093          774118 :     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
   82094           11073 :       aff = SQLITE_AFF_TEXT; 
   82095          763045 :     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
   82096               0 :       aff = SQLITE_AFF_TEXT;
   82097          763045 :     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
   82098           82241 :       aff = SQLITE_AFF_TEXT;
   82099          680804 :     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
   82100            5913 :         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
   82101            5913 :       aff = SQLITE_AFF_NONE;
   82102                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   82103          674891 :     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
   82104             379 :         && aff==SQLITE_AFF_NUMERIC ){
   82105             379 :       aff = SQLITE_AFF_REAL;
   82106          674512 :     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
   82107               0 :         && aff==SQLITE_AFF_NUMERIC ){
   82108               0 :       aff = SQLITE_AFF_REAL;
   82109          674512 :     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
   82110               0 :         && aff==SQLITE_AFF_NUMERIC ){
   82111               0 :       aff = SQLITE_AFF_REAL;
   82112                 : #endif
   82113          674512 :     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
   82114           98217 :       aff = SQLITE_AFF_INTEGER;
   82115           98217 :       break;
   82116                 :     }
   82117                 :   }
   82118                 : 
   82119          198702 :   return aff;
   82120                 : }
   82121                 : 
   82122                 : /*
   82123                 : ** This routine is called by the parser while in the middle of
   82124                 : ** parsing a CREATE TABLE statement.  The pFirst token is the first
   82125                 : ** token in the sequence of tokens that describe the type of the
   82126                 : ** column currently under construction.   pLast is the last token
   82127                 : ** in the sequence.  Use this information to construct a string
   82128                 : ** that contains the typename of the column and store that string
   82129                 : ** in zType.
   82130                 : */ 
   82131          199788 : SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
   82132                 :   Table *p;
   82133                 :   Column *pCol;
   82134                 : 
   82135          199788 :   p = pParse->pNewTable;
   82136          199788 :   if( p==0 || NEVER(p->nCol<1) ) return;
   82137          198333 :   pCol = &p->aCol[p->nCol-1];
   82138          198333 :   assert( pCol->zType==0 );
   82139          198333 :   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
   82140          198333 :   pCol->affinity = sqlite3AffinityType(pCol->zType);
   82141                 : }
   82142                 : 
   82143                 : /*
   82144                 : ** The expression is the default value for the most recently added column
   82145                 : ** of the table currently under construction.
   82146                 : **
   82147                 : ** Default value expressions must be constant.  Raise an exception if this
   82148                 : ** is not the case.
   82149                 : **
   82150                 : ** This routine is called by the parser while in the middle of
   82151                 : ** parsing a CREATE TABLE statement.
   82152                 : */
   82153           17762 : SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
   82154                 :   Table *p;
   82155                 :   Column *pCol;
   82156           17762 :   sqlite3 *db = pParse->db;
   82157           17762 :   p = pParse->pNewTable;
   82158           17762 :   if( p!=0 ){
   82159           17762 :     pCol = &(p->aCol[p->nCol-1]);
   82160           17762 :     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
   82161               0 :       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
   82162                 :           pCol->zName);
   82163                 :     }else{
   82164                 :       /* A copy of pExpr is used instead of the original, as pExpr contains
   82165                 :       ** tokens that point to volatile memory. The 'span' of the expression
   82166                 :       ** is required by pragma table_info.
   82167                 :       */
   82168           17762 :       sqlite3ExprDelete(db, pCol->pDflt);
   82169           17762 :       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
   82170           17762 :       sqlite3DbFree(db, pCol->zDflt);
   82171           35524 :       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
   82172           35524 :                                      (int)(pSpan->zEnd - pSpan->zStart));
   82173                 :     }
   82174                 :   }
   82175           17762 :   sqlite3ExprDelete(db, pSpan->pExpr);
   82176           17762 : }
   82177                 : 
   82178                 : /*
   82179                 : ** Designate the PRIMARY KEY for the table.  pList is a list of names 
   82180                 : ** of columns that form the primary key.  If pList is NULL, then the
   82181                 : ** most recently added column of the table is the primary key.
   82182                 : **
   82183                 : ** A table can have at most one primary key.  If the table already has
   82184                 : ** a primary key (and this is the second primary key) then create an
   82185                 : ** error.
   82186                 : **
   82187                 : ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
   82188                 : ** then we will try to use that column as the rowid.  Set the Table.iPKey
   82189                 : ** field of the table under construction to be the index of the
   82190                 : ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
   82191                 : ** no INTEGER PRIMARY KEY.
   82192                 : **
   82193                 : ** If the key is not an INTEGER PRIMARY KEY, then create a unique
   82194                 : ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
   82195                 : */
   82196           18619 : SQLITE_PRIVATE void sqlite3AddPrimaryKey(
   82197                 :   Parse *pParse,    /* Parsing context */
   82198                 :   ExprList *pList,  /* List of field names to be indexed */
   82199                 :   int onError,      /* What to do with a uniqueness conflict */
   82200                 :   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
   82201                 :   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
   82202                 : ){
   82203           18619 :   Table *pTab = pParse->pNewTable;
   82204           18619 :   char *zType = 0;
   82205           18619 :   int iCol = -1, i;
   82206           18619 :   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
   82207           18364 :   if( pTab->tabFlags & TF_HasPrimaryKey ){
   82208               0 :     sqlite3ErrorMsg(pParse, 
   82209                 :       "table \"%s\" has more than one primary key", pTab->zName);
   82210               0 :     goto primary_key_exit;
   82211                 :   }
   82212           18364 :   pTab->tabFlags |= TF_HasPrimaryKey;
   82213           18364 :   if( pList==0 ){
   82214           15941 :     iCol = pTab->nCol - 1;
   82215           15941 :     pTab->aCol[iCol].isPrimKey = 1;
   82216                 :   }else{
   82217            8533 :     for(i=0; i<pList->nExpr; i++){
   82218           11061 :       for(iCol=0; iCol<pTab->nCol; iCol++){
   82219           11061 :         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
   82220            6110 :           break;
   82221                 :         }
   82222                 :       }
   82223            6110 :       if( iCol<pTab->nCol ){
   82224            6110 :         pTab->aCol[iCol].isPrimKey = 1;
   82225                 :       }
   82226                 :     }
   82227            2423 :     if( pList->nExpr>1 ) iCol = -1;
   82228                 :   }
   82229           18364 :   if( iCol>=0 && iCol<pTab->nCol ){
   82230           15941 :     zType = pTab->aCol[iCol].zType;
   82231                 :   }
   82232           18364 :   if( zType && sqlite3StrICmp(zType, "INTEGER")==0
   82233           15848 :         && sortOrder==SQLITE_SO_ASC ){
   82234           15848 :     pTab->iPKey = iCol;
   82235           15848 :     pTab->keyConf = (u8)onError;
   82236           15848 :     assert( autoInc==0 || autoInc==1 );
   82237           15848 :     pTab->tabFlags |= autoInc*TF_Autoincrement;
   82238            2516 :   }else if( autoInc ){
   82239                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   82240               0 :     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
   82241                 :        "INTEGER PRIMARY KEY");
   82242                 : #endif
   82243                 :   }else{
   82244                 :     Index *p;
   82245            2516 :     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
   82246            2516 :     if( p ){
   82247            2516 :       p->autoIndex = 2;
   82248                 :     }
   82249            2516 :     pList = 0;
   82250                 :   }
   82251                 : 
   82252                 : primary_key_exit:
   82253           18619 :   sqlite3ExprListDelete(pParse->db, pList);
   82254                 :   return;
   82255                 : }
   82256                 : 
   82257                 : /*
   82258                 : ** Add a new CHECK constraint to the table currently under construction.
   82259                 : */
   82260               0 : SQLITE_PRIVATE void sqlite3AddCheckConstraint(
   82261                 :   Parse *pParse,    /* Parsing context */
   82262                 :   Expr *pCheckExpr  /* The check expression */
   82263                 : ){
   82264               0 :   sqlite3 *db = pParse->db;
   82265                 : #ifndef SQLITE_OMIT_CHECK
   82266               0 :   Table *pTab = pParse->pNewTable;
   82267               0 :   if( pTab && !IN_DECLARE_VTAB ){
   82268               0 :     pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr);
   82269                 :   }else
   82270                 : #endif
   82271                 :   {
   82272               0 :     sqlite3ExprDelete(db, pCheckExpr);
   82273                 :   }
   82274               0 : }
   82275                 : 
   82276                 : /*
   82277                 : ** Set the collation function of the most recently parsed table column
   82278                 : ** to the CollSeq given.
   82279                 : */
   82280               0 : SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
   82281                 :   Table *p;
   82282                 :   int i;
   82283                 :   char *zColl;              /* Dequoted name of collation sequence */
   82284                 :   sqlite3 *db;
   82285                 : 
   82286               0 :   if( (p = pParse->pNewTable)==0 ) return;
   82287               0 :   i = p->nCol-1;
   82288               0 :   db = pParse->db;
   82289               0 :   zColl = sqlite3NameFromToken(db, pToken);
   82290               0 :   if( !zColl ) return;
   82291                 : 
   82292               0 :   if( sqlite3LocateCollSeq(pParse, zColl) ){
   82293                 :     Index *pIdx;
   82294               0 :     p->aCol[i].zColl = zColl;
   82295                 :   
   82296                 :     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
   82297                 :     ** then an index may have been created on this column before the
   82298                 :     ** collation type was added. Correct this if it is the case.
   82299                 :     */
   82300               0 :     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
   82301               0 :       assert( pIdx->nColumn==1 );
   82302               0 :       if( pIdx->aiColumn[0]==i ){
   82303               0 :         pIdx->azColl[0] = p->aCol[i].zColl;
   82304                 :       }
   82305                 :     }
   82306                 :   }else{
   82307               0 :     sqlite3DbFree(db, zColl);
   82308                 :   }
   82309                 : }
   82310                 : 
   82311                 : /*
   82312                 : ** This function returns the collation sequence for database native text
   82313                 : ** encoding identified by the string zName, length nName.
   82314                 : **
   82315                 : ** If the requested collation sequence is not available, or not available
   82316                 : ** in the database native encoding, the collation factory is invoked to
   82317                 : ** request it. If the collation factory does not supply such a sequence,
   82318                 : ** and the sequence is available in another text encoding, then that is
   82319                 : ** returned instead.
   82320                 : **
   82321                 : ** If no versions of the requested collations sequence are available, or
   82322                 : ** another error occurs, NULL is returned and an error message written into
   82323                 : ** pParse.
   82324                 : **
   82325                 : ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
   82326                 : ** invokes the collation factory if the named collation cannot be found
   82327                 : ** and generates an error message.
   82328                 : **
   82329                 : ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
   82330                 : */
   82331          121930 : SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
   82332          121930 :   sqlite3 *db = pParse->db;
   82333          121930 :   u8 enc = ENC(db);
   82334          121930 :   u8 initbusy = db->init.busy;
   82335                 :   CollSeq *pColl;
   82336                 : 
   82337          121930 :   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
   82338          121930 :   if( !initbusy && (!pColl || !pColl->xCmp) ){
   82339               0 :     pColl = sqlite3GetCollSeq(db, enc, pColl, zName);
   82340               0 :     if( !pColl ){
   82341               0 :       sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
   82342                 :     }
   82343                 :   }
   82344                 : 
   82345          121930 :   return pColl;
   82346                 : }
   82347                 : 
   82348                 : 
   82349                 : /*
   82350                 : ** Generate code that will increment the schema cookie.
   82351                 : **
   82352                 : ** The schema cookie is used to determine when the schema for the
   82353                 : ** database changes.  After each schema change, the cookie value
   82354                 : ** changes.  When a process first reads the schema it records the
   82355                 : ** cookie.  Thereafter, whenever it goes to access the database,
   82356                 : ** it checks the cookie to make sure the schema has not changed
   82357                 : ** since it was last read.
   82358                 : **
   82359                 : ** This plan is not completely bullet-proof.  It is possible for
   82360                 : ** the schema to change multiple times and for the cookie to be
   82361                 : ** set back to prior value.  But schema changes are infrequent
   82362                 : ** and the probability of hitting the same cookie value is only
   82363                 : ** 1 chance in 2^32.  So we're safe enough.
   82364                 : */
   82365           15648 : SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
   82366           15648 :   int r1 = sqlite3GetTempReg(pParse);
   82367           15648 :   sqlite3 *db = pParse->db;
   82368           15648 :   Vdbe *v = pParse->pVdbe;
   82369           15648 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   82370           15648 :   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
   82371           15648 :   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
   82372           15648 :   sqlite3ReleaseTempReg(pParse, r1);
   82373           15648 : }
   82374                 : 
   82375                 : /*
   82376                 : ** Measure the number of characters needed to output the given
   82377                 : ** identifier.  The number returned includes any quotes used
   82378                 : ** but does not include the null terminator.
   82379                 : **
   82380                 : ** The estimate is conservative.  It might be larger that what is
   82381                 : ** really needed.
   82382                 : */
   82383               0 : static int identLength(const char *z){
   82384                 :   int n;
   82385               0 :   for(n=0; *z; n++, z++){
   82386               0 :     if( *z=='"' ){ n++; }
   82387                 :   }
   82388               0 :   return n + 2;
   82389                 : }
   82390                 : 
   82391                 : /*
   82392                 : ** The first parameter is a pointer to an output buffer. The second 
   82393                 : ** parameter is a pointer to an integer that contains the offset at
   82394                 : ** which to write into the output buffer. This function copies the
   82395                 : ** nul-terminated string pointed to by the third parameter, zSignedIdent,
   82396                 : ** to the specified offset in the buffer and updates *pIdx to refer
   82397                 : ** to the first byte after the last byte written before returning.
   82398                 : ** 
   82399                 : ** If the string zSignedIdent consists entirely of alpha-numeric
   82400                 : ** characters, does not begin with a digit and is not an SQL keyword,
   82401                 : ** then it is copied to the output buffer exactly as it is. Otherwise,
   82402                 : ** it is quoted using double-quotes.
   82403                 : */
   82404               0 : static void identPut(char *z, int *pIdx, char *zSignedIdent){
   82405               0 :   unsigned char *zIdent = (unsigned char*)zSignedIdent;
   82406                 :   int i, j, needQuote;
   82407               0 :   i = *pIdx;
   82408                 : 
   82409               0 :   for(j=0; zIdent[j]; j++){
   82410               0 :     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
   82411                 :   }
   82412               0 :   needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
   82413               0 :   if( !needQuote ){
   82414               0 :     needQuote = zIdent[j];
   82415                 :   }
   82416                 : 
   82417               0 :   if( needQuote ) z[i++] = '"';
   82418               0 :   for(j=0; zIdent[j]; j++){
   82419               0 :     z[i++] = zIdent[j];
   82420               0 :     if( zIdent[j]=='"' ) z[i++] = '"';
   82421                 :   }
   82422               0 :   if( needQuote ) z[i++] = '"';
   82423               0 :   z[i] = 0;
   82424               0 :   *pIdx = i;
   82425               0 : }
   82426                 : 
   82427                 : /*
   82428                 : ** Generate a CREATE TABLE statement appropriate for the given
   82429                 : ** table.  Memory to hold the text of the statement is obtained
   82430                 : ** from sqliteMalloc() and must be freed by the calling function.
   82431                 : */
   82432               0 : static char *createTableStmt(sqlite3 *db, Table *p){
   82433                 :   int i, k, n;
   82434                 :   char *zStmt;
   82435                 :   char *zSep, *zSep2, *zEnd;
   82436                 :   Column *pCol;
   82437               0 :   n = 0;
   82438               0 :   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
   82439               0 :     n += identLength(pCol->zName) + 5;
   82440                 :   }
   82441               0 :   n += identLength(p->zName);
   82442               0 :   if( n<50 ){ 
   82443               0 :     zSep = "";
   82444               0 :     zSep2 = ",";
   82445               0 :     zEnd = ")";
   82446                 :   }else{
   82447               0 :     zSep = "\n  ";
   82448               0 :     zSep2 = ",\n  ";
   82449               0 :     zEnd = "\n)";
   82450                 :   }
   82451               0 :   n += 35 + 6*p->nCol;
   82452               0 :   zStmt = sqlite3DbMallocRaw(0, n);
   82453               0 :   if( zStmt==0 ){
   82454               0 :     db->mallocFailed = 1;
   82455               0 :     return 0;
   82456                 :   }
   82457               0 :   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
   82458               0 :   k = sqlite3Strlen30(zStmt);
   82459               0 :   identPut(zStmt, &k, p->zName);
   82460               0 :   zStmt[k++] = '(';
   82461               0 :   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
   82462                 :     static const char * const azType[] = {
   82463                 :         /* SQLITE_AFF_TEXT    */ " TEXT",
   82464                 :         /* SQLITE_AFF_NONE    */ "",
   82465                 :         /* SQLITE_AFF_NUMERIC */ " NUM",
   82466                 :         /* SQLITE_AFF_INTEGER */ " INT",
   82467                 :         /* SQLITE_AFF_REAL    */ " REAL"
   82468                 :     };
   82469                 :     int len;
   82470                 :     const char *zType;
   82471                 : 
   82472               0 :     sqlite3_snprintf(n-k, &zStmt[k], zSep);
   82473               0 :     k += sqlite3Strlen30(&zStmt[k]);
   82474               0 :     zSep = zSep2;
   82475               0 :     identPut(zStmt, &k, pCol->zName);
   82476               0 :     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
   82477               0 :     assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
   82478                 :     testcase( pCol->affinity==SQLITE_AFF_TEXT );
   82479                 :     testcase( pCol->affinity==SQLITE_AFF_NONE );
   82480                 :     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
   82481                 :     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
   82482                 :     testcase( pCol->affinity==SQLITE_AFF_REAL );
   82483                 :     
   82484               0 :     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
   82485               0 :     len = sqlite3Strlen30(zType);
   82486               0 :     assert( pCol->affinity==SQLITE_AFF_NONE 
   82487                 :             || pCol->affinity==sqlite3AffinityType(zType) );
   82488               0 :     memcpy(&zStmt[k], zType, len);
   82489               0 :     k += len;
   82490               0 :     assert( k<=n );
   82491                 :   }
   82492               0 :   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
   82493               0 :   return zStmt;
   82494                 : }
   82495                 : 
   82496                 : /*
   82497                 : ** This routine is called to report the final ")" that terminates
   82498                 : ** a CREATE TABLE statement.
   82499                 : **
   82500                 : ** The table structure that other action routines have been building
   82501                 : ** is added to the internal hash tables, assuming no errors have
   82502                 : ** occurred.
   82503                 : **
   82504                 : ** An entry for the table is made in the master table on disk, unless
   82505                 : ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
   82506                 : ** it means we are reading the sqlite_master table because we just
   82507                 : ** connected to the database or because the sqlite_master table has
   82508                 : ** recently changed, so the entry for this table already exists in
   82509                 : ** the sqlite_master table.  We do not want to create it again.
   82510                 : **
   82511                 : ** If the pSelect argument is not NULL, it means that this routine
   82512                 : ** was called to create a table generated from a 
   82513                 : ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
   82514                 : ** the new table will match the result set of the SELECT.
   82515                 : */
   82516           35843 : SQLITE_PRIVATE void sqlite3EndTable(
   82517                 :   Parse *pParse,          /* Parse context */
   82518                 :   Token *pCons,           /* The ',' token after the last column defn. */
   82519                 :   Token *pEnd,            /* The final ')' token in the CREATE TABLE */
   82520                 :   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
   82521                 : ){
   82522                 :   Table *p;
   82523           35843 :   sqlite3 *db = pParse->db;
   82524                 :   int iDb;
   82525                 : 
   82526           35843 :   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
   82527               0 :     return;
   82528                 :   }
   82529           35843 :   p = pParse->pNewTable;
   82530           35843 :   if( p==0 ) return;
   82531                 : 
   82532           35586 :   assert( !db->init.busy || !pSelect );
   82533                 : 
   82534           35586 :   iDb = sqlite3SchemaToIndex(db, p->pSchema);
   82535                 : 
   82536                 : #ifndef SQLITE_OMIT_CHECK
   82537                 :   /* Resolve names in all CHECK constraint expressions.
   82538                 :   */
   82539           35586 :   if( p->pCheck ){
   82540                 :     SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
   82541                 :     NameContext sNC;                /* Name context for pParse->pNewTable */
   82542                 : 
   82543               0 :     memset(&sNC, 0, sizeof(sNC));
   82544               0 :     memset(&sSrc, 0, sizeof(sSrc));
   82545               0 :     sSrc.nSrc = 1;
   82546               0 :     sSrc.a[0].zName = p->zName;
   82547               0 :     sSrc.a[0].pTab = p;
   82548               0 :     sSrc.a[0].iCursor = -1;
   82549               0 :     sNC.pParse = pParse;
   82550               0 :     sNC.pSrcList = &sSrc;
   82551               0 :     sNC.isCheck = 1;
   82552               0 :     if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){
   82553               0 :       return;
   82554                 :     }
   82555                 :   }
   82556                 : #endif /* !defined(SQLITE_OMIT_CHECK) */
   82557                 : 
   82558                 :   /* If the db->init.busy is 1 it means we are reading the SQL off the
   82559                 :   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
   82560                 :   ** So do not write to the disk again.  Extract the root page number
   82561                 :   ** for the table from the db->init.newTnum field.  (The page number
   82562                 :   ** should have been put there by the sqliteOpenCb routine.)
   82563                 :   */
   82564           35586 :   if( db->init.busy ){
   82565           28399 :     p->tnum = db->init.newTnum;
   82566                 :   }
   82567                 : 
   82568                 :   /* If not initializing, then create a record for the new table
   82569                 :   ** in the SQLITE_MASTER table of the database.
   82570                 :   **
   82571                 :   ** If this is a TEMPORARY table, write the entry into the auxiliary
   82572                 :   ** file instead of into the main database file.
   82573                 :   */
   82574           35586 :   if( !db->init.busy ){
   82575                 :     int n;
   82576                 :     Vdbe *v;
   82577                 :     char *zType;    /* "view" or "table" */
   82578                 :     char *zType2;   /* "VIEW" or "TABLE" */
   82579                 :     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
   82580                 : 
   82581            7187 :     v = sqlite3GetVdbe(pParse);
   82582            7187 :     if( NEVER(v==0) ) return;
   82583                 : 
   82584            7187 :     sqlite3VdbeAddOp1(v, OP_Close, 0);
   82585                 : 
   82586                 :     /* 
   82587                 :     ** Initialize zType for the new view or table.
   82588                 :     */
   82589            7187 :     if( p->pSelect==0 ){
   82590                 :       /* A regular table */
   82591            7109 :       zType = "table";
   82592            7109 :       zType2 = "TABLE";
   82593                 : #ifndef SQLITE_OMIT_VIEW
   82594                 :     }else{
   82595                 :       /* A view */
   82596              78 :       zType = "view";
   82597              78 :       zType2 = "VIEW";
   82598                 : #endif
   82599                 :     }
   82600                 : 
   82601                 :     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
   82602                 :     ** statement to populate the new table. The root-page number for the
   82603                 :     ** new table is in register pParse->regRoot.
   82604                 :     **
   82605                 :     ** Once the SELECT has been coded by sqlite3Select(), it is in a
   82606                 :     ** suitable state to query for the column names and types to be used
   82607                 :     ** by the new table.
   82608                 :     **
   82609                 :     ** A shared-cache write-lock is not required to write to the new table,
   82610                 :     ** as a schema-lock must have already been obtained to create it. Since
   82611                 :     ** a schema-lock excludes all other database users, the write-lock would
   82612                 :     ** be redundant.
   82613                 :     */
   82614            7187 :     if( pSelect ){
   82615                 :       SelectDest dest;
   82616                 :       Table *pSelTab;
   82617                 : 
   82618               0 :       assert(pParse->nTab==1);
   82619               0 :       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
   82620               0 :       sqlite3VdbeChangeP5(v, 1);
   82621               0 :       pParse->nTab = 2;
   82622               0 :       sqlite3SelectDestInit(&dest, SRT_Table, 1);
   82623               0 :       sqlite3Select(pParse, pSelect, &dest);
   82624               0 :       sqlite3VdbeAddOp1(v, OP_Close, 1);
   82625               0 :       if( pParse->nErr==0 ){
   82626               0 :         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
   82627               0 :         if( pSelTab==0 ) return;
   82628               0 :         assert( p->aCol==0 );
   82629               0 :         p->nCol = pSelTab->nCol;
   82630               0 :         p->aCol = pSelTab->aCol;
   82631               0 :         pSelTab->nCol = 0;
   82632               0 :         pSelTab->aCol = 0;
   82633               0 :         sqlite3DeleteTable(db, pSelTab);
   82634                 :       }
   82635                 :     }
   82636                 : 
   82637                 :     /* Compute the complete text of the CREATE statement */
   82638            7187 :     if( pSelect ){
   82639               0 :       zStmt = createTableStmt(db, p);
   82640                 :     }else{
   82641            7187 :       n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
   82642            7187 :       zStmt = sqlite3MPrintf(db, 
   82643                 :           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
   82644                 :       );
   82645                 :     }
   82646                 : 
   82647                 :     /* A slot for the record has already been allocated in the 
   82648                 :     ** SQLITE_MASTER table.  We just need to update that slot with all
   82649                 :     ** the information we've collected.
   82650                 :     */
   82651           14374 :     sqlite3NestedParse(pParse,
   82652                 :       "UPDATE %Q.%s "
   82653                 :          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
   82654                 :        "WHERE rowid=#%d",
   82655            7187 :       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
   82656                 :       zType,
   82657                 :       p->zName,
   82658                 :       p->zName,
   82659                 :       pParse->regRoot,
   82660                 :       zStmt,
   82661                 :       pParse->regRowid
   82662                 :     );
   82663            7187 :     sqlite3DbFree(db, zStmt);
   82664            7187 :     sqlite3ChangeCookie(pParse, iDb);
   82665                 : 
   82666                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   82667                 :     /* Check to see if we need to create an sqlite_sequence table for
   82668                 :     ** keeping track of autoincrement keys.
   82669                 :     */
   82670            7187 :     if( p->tabFlags & TF_Autoincrement ){
   82671             667 :       Db *pDb = &db->aDb[iDb];
   82672             667 :       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   82673             667 :       if( pDb->pSchema->pSeqTab==0 ){
   82674             476 :         sqlite3NestedParse(pParse,
   82675                 :           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
   82676                 :           pDb->zName
   82677                 :         );
   82678                 :       }
   82679                 :     }
   82680                 : #endif
   82681                 : 
   82682                 :     /* Reparse everything to update our internal data structures */
   82683            7187 :     sqlite3VdbeAddParseSchemaOp(v, iDb,
   82684                 :                sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
   82685                 :   }
   82686                 : 
   82687                 : 
   82688                 :   /* Add the table to the in-memory representation of the database.
   82689                 :   */
   82690           35586 :   if( db->init.busy ){
   82691                 :     Table *pOld;
   82692           28399 :     Schema *pSchema = p->pSchema;
   82693           28399 :     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   82694           28399 :     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
   82695           28399 :                              sqlite3Strlen30(p->zName),p);
   82696           28399 :     if( pOld ){
   82697               0 :       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
   82698               0 :       db->mallocFailed = 1;
   82699               0 :       return;
   82700                 :     }
   82701           28399 :     pParse->pNewTable = 0;
   82702           28399 :     db->nTable++;
   82703           28399 :     db->flags |= SQLITE_InternChanges;
   82704                 : 
   82705                 : #ifndef SQLITE_OMIT_ALTERTABLE
   82706           28399 :     if( !p->pSelect ){
   82707           28321 :       const char *zName = (const char *)pParse->sNameToken.z;
   82708                 :       int nName;
   82709           28321 :       assert( !pSelect && pCons && pEnd );
   82710           28321 :       if( pCons->z==0 ){
   82711           20311 :         pCons = pEnd;
   82712                 :       }
   82713           28321 :       nName = (int)((const char *)pCons->z - zName);
   82714           28321 :       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
   82715                 :     }
   82716                 : #endif
   82717                 :   }
   82718                 : }
   82719                 : 
   82720                 : #ifndef SQLITE_OMIT_VIEW
   82721                 : /*
   82722                 : ** The parser calls this routine in order to create a new VIEW
   82723                 : */
   82724             156 : SQLITE_PRIVATE void sqlite3CreateView(
   82725                 :   Parse *pParse,     /* The parsing context */
   82726                 :   Token *pBegin,     /* The CREATE token that begins the statement */
   82727                 :   Token *pName1,     /* The token that holds the name of the view */
   82728                 :   Token *pName2,     /* The token that holds the name of the view */
   82729                 :   Select *pSelect,   /* A SELECT statement that will become the new view */
   82730                 :   int isTemp,        /* TRUE for a TEMPORARY view */
   82731                 :   int noErr          /* Suppress error messages if VIEW already exists */
   82732                 : ){
   82733                 :   Table *p;
   82734                 :   int n;
   82735                 :   const char *z;
   82736                 :   Token sEnd;
   82737                 :   DbFixer sFix;
   82738             156 :   Token *pName = 0;
   82739                 :   int iDb;
   82740             156 :   sqlite3 *db = pParse->db;
   82741                 : 
   82742             156 :   if( pParse->nVar>0 ){
   82743               0 :     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
   82744               0 :     sqlite3SelectDelete(db, pSelect);
   82745               0 :     return;
   82746                 :   }
   82747             156 :   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
   82748             156 :   p = pParse->pNewTable;
   82749             156 :   if( p==0 || pParse->nErr ){
   82750               0 :     sqlite3SelectDelete(db, pSelect);
   82751               0 :     return;
   82752                 :   }
   82753             156 :   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
   82754             156 :   iDb = sqlite3SchemaToIndex(db, p->pSchema);
   82755             156 :   if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
   82756               0 :     && sqlite3FixSelect(&sFix, pSelect)
   82757                 :   ){
   82758               0 :     sqlite3SelectDelete(db, pSelect);
   82759               0 :     return;
   82760                 :   }
   82761                 : 
   82762                 :   /* Make a copy of the entire SELECT statement that defines the view.
   82763                 :   ** This will force all the Expr.token.z values to be dynamically
   82764                 :   ** allocated rather than point to the input string - which means that
   82765                 :   ** they will persist after the current sqlite3_exec() call returns.
   82766                 :   */
   82767             156 :   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
   82768             156 :   sqlite3SelectDelete(db, pSelect);
   82769             156 :   if( db->mallocFailed ){
   82770               0 :     return;
   82771                 :   }
   82772             156 :   if( !db->init.busy ){
   82773              78 :     sqlite3ViewGetColumnNames(pParse, p);
   82774                 :   }
   82775                 : 
   82776                 :   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
   82777                 :   ** the end.
   82778                 :   */
   82779             156 :   sEnd = pParse->sLastToken;
   82780             156 :   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
   82781             156 :     sEnd.z += sEnd.n;
   82782                 :   }
   82783             156 :   sEnd.n = 0;
   82784             156 :   n = (int)(sEnd.z - pBegin->z);
   82785             156 :   z = pBegin->z;
   82786             156 :   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
   82787             156 :   sEnd.z = &z[n-1];
   82788             156 :   sEnd.n = 1;
   82789                 : 
   82790                 :   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
   82791             156 :   sqlite3EndTable(pParse, 0, &sEnd, 0);
   82792             156 :   return;
   82793                 : }
   82794                 : #endif /* SQLITE_OMIT_VIEW */
   82795                 : 
   82796                 : #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
   82797                 : /*
   82798                 : ** The Table structure pTable is really a VIEW.  Fill in the names of
   82799                 : ** the columns of the view in the pTable structure.  Return the number
   82800                 : ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
   82801                 : */
   82802           41766 : SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
   82803                 :   Table *pSelTab;   /* A fake table from which we get the result set */
   82804                 :   Select *pSel;     /* Copy of the SELECT that implements the view */
   82805           41766 :   int nErr = 0;     /* Number of errors encountered */
   82806                 :   int n;            /* Temporarily holds the number of cursors assigned */
   82807           41766 :   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
   82808                 :   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
   82809                 : 
   82810           41766 :   assert( pTable );
   82811                 : 
   82812                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   82813           41766 :   if( sqlite3VtabCallConnect(pParse, pTable) ){
   82814               0 :     return SQLITE_ERROR;
   82815                 :   }
   82816           41766 :   if( IsVirtual(pTable) ) return 0;
   82817                 : #endif
   82818                 : 
   82819                 : #ifndef SQLITE_OMIT_VIEW
   82820                 :   /* A positive nCol means the columns names for this view are
   82821                 :   ** already known.
   82822                 :   */
   82823           41656 :   if( pTable->nCol>0 ) return 0;
   82824                 : 
   82825                 :   /* A negative nCol is a special marker meaning that we are currently
   82826                 :   ** trying to compute the column names.  If we enter this routine with
   82827                 :   ** a negative nCol, it means two or more views form a loop, like this:
   82828                 :   **
   82829                 :   **     CREATE VIEW one AS SELECT * FROM two;
   82830                 :   **     CREATE VIEW two AS SELECT * FROM one;
   82831                 :   **
   82832                 :   ** Actually, the error above is now caught prior to reaching this point.
   82833                 :   ** But the following test is still important as it does come up
   82834                 :   ** in the following:
   82835                 :   ** 
   82836                 :   **     CREATE TABLE main.ex1(a);
   82837                 :   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
   82838                 :   **     SELECT * FROM temp.ex1;
   82839                 :   */
   82840              88 :   if( pTable->nCol<0 ){
   82841               0 :     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
   82842               0 :     return 1;
   82843                 :   }
   82844              88 :   assert( pTable->nCol>=0 );
   82845                 : 
   82846                 :   /* If we get this far, it means we need to compute the table names.
   82847                 :   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
   82848                 :   ** "*" elements in the results set of the view and will assign cursors
   82849                 :   ** to the elements of the FROM clause.  But we do not want these changes
   82850                 :   ** to be permanent.  So the computation is done on a copy of the SELECT
   82851                 :   ** statement that defines the view.
   82852                 :   */
   82853              88 :   assert( pTable->pSelect );
   82854              88 :   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
   82855              88 :   if( pSel ){
   82856              88 :     u8 enableLookaside = db->lookaside.bEnabled;
   82857              88 :     n = pParse->nTab;
   82858              88 :     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
   82859              88 :     pTable->nCol = -1;
   82860              88 :     db->lookaside.bEnabled = 0;
   82861                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   82862              88 :     xAuth = db->xAuth;
   82863              88 :     db->xAuth = 0;
   82864              88 :     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
   82865              88 :     db->xAuth = xAuth;
   82866                 : #else
   82867                 :     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
   82868                 : #endif
   82869              88 :     db->lookaside.bEnabled = enableLookaside;
   82870              88 :     pParse->nTab = n;
   82871              88 :     if( pSelTab ){
   82872              88 :       assert( pTable->aCol==0 );
   82873              88 :       pTable->nCol = pSelTab->nCol;
   82874              88 :       pTable->aCol = pSelTab->aCol;
   82875              88 :       pSelTab->nCol = 0;
   82876              88 :       pSelTab->aCol = 0;
   82877              88 :       sqlite3DeleteTable(db, pSelTab);
   82878              88 :       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
   82879              88 :       pTable->pSchema->flags |= DB_UnresetViews;
   82880                 :     }else{
   82881               0 :       pTable->nCol = 0;
   82882               0 :       nErr++;
   82883                 :     }
   82884              88 :     sqlite3SelectDelete(db, pSel);
   82885                 :   } else {
   82886               0 :     nErr++;
   82887                 :   }
   82888                 : #endif /* SQLITE_OMIT_VIEW */
   82889              88 :   return nErr;  
   82890                 : }
   82891                 : #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
   82892                 : 
   82893                 : #ifndef SQLITE_OMIT_VIEW
   82894                 : /*
   82895                 : ** Clear the column names from every VIEW in database idx.
   82896                 : */
   82897              89 : static void sqliteViewResetAll(sqlite3 *db, int idx){
   82898                 :   HashElem *i;
   82899              89 :   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
   82900              89 :   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
   82901               0 :   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
   82902               0 :     Table *pTab = sqliteHashData(i);
   82903               0 :     if( pTab->pSelect ){
   82904               0 :       sqliteDeleteColumnNames(db, pTab);
   82905               0 :       pTab->aCol = 0;
   82906               0 :       pTab->nCol = 0;
   82907                 :     }
   82908                 :   }
   82909               0 :   DbClearProperty(db, idx, DB_UnresetViews);
   82910                 : }
   82911                 : #else
   82912                 : # define sqliteViewResetAll(A,B)
   82913                 : #endif /* SQLITE_OMIT_VIEW */
   82914                 : 
   82915                 : /*
   82916                 : ** This function is called by the VDBE to adjust the internal schema
   82917                 : ** used by SQLite when the btree layer moves a table root page. The
   82918                 : ** root-page of a table or index in database iDb has changed from iFrom
   82919                 : ** to iTo.
   82920                 : **
   82921                 : ** Ticket #1728:  The symbol table might still contain information
   82922                 : ** on tables and/or indices that are the process of being deleted.
   82923                 : ** If you are unlucky, one of those deleted indices or tables might
   82924                 : ** have the same rootpage number as the real table or index that is
   82925                 : ** being moved.  So we cannot stop searching after the first match 
   82926                 : ** because the first match might be for one of the deleted indices
   82927                 : ** or tables and not the table/index that is actually being moved.
   82928                 : ** We must continue looping until all tables and indices with
   82929                 : ** rootpage==iFrom have been converted to have a rootpage of iTo
   82930                 : ** in order to be certain that we got the right one.
   82931                 : */
   82932                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   82933               0 : SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
   82934                 :   HashElem *pElem;
   82935                 :   Hash *pHash;
   82936                 :   Db *pDb;
   82937                 : 
   82938               0 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   82939               0 :   pDb = &db->aDb[iDb];
   82940               0 :   pHash = &pDb->pSchema->tblHash;
   82941               0 :   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
   82942               0 :     Table *pTab = sqliteHashData(pElem);
   82943               0 :     if( pTab->tnum==iFrom ){
   82944               0 :       pTab->tnum = iTo;
   82945                 :     }
   82946                 :   }
   82947               0 :   pHash = &pDb->pSchema->idxHash;
   82948               0 :   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
   82949               0 :     Index *pIdx = sqliteHashData(pElem);
   82950               0 :     if( pIdx->tnum==iFrom ){
   82951               0 :       pIdx->tnum = iTo;
   82952                 :     }
   82953                 :   }
   82954               0 : }
   82955                 : #endif
   82956                 : 
   82957                 : /*
   82958                 : ** Write code to erase the table with root-page iTable from database iDb.
   82959                 : ** Also write code to modify the sqlite_master table and internal schema
   82960                 : ** if a root-page of another table is moved by the btree-layer whilst
   82961                 : ** erasing iTable (this can happen with an auto-vacuum database).
   82962                 : */ 
   82963              67 : static void destroyRootPage(Parse *pParse, int iTable, int iDb){
   82964              67 :   Vdbe *v = sqlite3GetVdbe(pParse);
   82965              67 :   int r1 = sqlite3GetTempReg(pParse);
   82966              67 :   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
   82967              67 :   sqlite3MayAbort(pParse);
   82968                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   82969                 :   /* OP_Destroy stores an in integer r1. If this integer
   82970                 :   ** is non-zero, then it is the root page number of a table moved to
   82971                 :   ** location iTable. The following code modifies the sqlite_master table to
   82972                 :   ** reflect this.
   82973                 :   **
   82974                 :   ** The "#NNN" in the SQL is a special constant that means whatever value
   82975                 :   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
   82976                 :   ** token for additional information.
   82977                 :   */
   82978             134 :   sqlite3NestedParse(pParse, 
   82979                 :      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
   82980              67 :      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
   82981                 : #endif
   82982              67 :   sqlite3ReleaseTempReg(pParse, r1);
   82983              67 : }
   82984                 : 
   82985                 : /*
   82986                 : ** Write VDBE code to erase table pTab and all associated indices on disk.
   82987                 : ** Code to update the sqlite_master tables and internal schema definitions
   82988                 : ** in case a root-page belonging to another table is moved by the btree layer
   82989                 : ** is also added (this can happen with an auto-vacuum database).
   82990                 : */
   82991              37 : static void destroyTable(Parse *pParse, Table *pTab){
   82992                 : #ifdef SQLITE_OMIT_AUTOVACUUM
   82993                 :   Index *pIdx;
   82994                 :   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   82995                 :   destroyRootPage(pParse, pTab->tnum, iDb);
   82996                 :   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   82997                 :     destroyRootPage(pParse, pIdx->tnum, iDb);
   82998                 :   }
   82999                 : #else
   83000                 :   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
   83001                 :   ** is not defined), then it is important to call OP_Destroy on the
   83002                 :   ** table and index root-pages in order, starting with the numerically 
   83003                 :   ** largest root-page number. This guarantees that none of the root-pages
   83004                 :   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
   83005                 :   ** following were coded:
   83006                 :   **
   83007                 :   ** OP_Destroy 4 0
   83008                 :   ** ...
   83009                 :   ** OP_Destroy 5 0
   83010                 :   **
   83011                 :   ** and root page 5 happened to be the largest root-page number in the
   83012                 :   ** database, then root page 5 would be moved to page 4 by the 
   83013                 :   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
   83014                 :   ** a free-list page.
   83015                 :   */
   83016              37 :   int iTab = pTab->tnum;
   83017              37 :   int iDestroyed = 0;
   83018                 : 
   83019                 :   while( 1 ){
   83020                 :     Index *pIdx;
   83021              77 :     int iLargest = 0;
   83022                 : 
   83023              77 :     if( iDestroyed==0 || iTab<iDestroyed ){
   83024              40 :       iLargest = iTab;
   83025                 :     }
   83026              88 :     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   83027              11 :       int iIdx = pIdx->tnum;
   83028              11 :       assert( pIdx->pSchema==pTab->pSchema );
   83029              11 :       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
   83030               3 :         iLargest = iIdx;
   83031                 :       }
   83032                 :     }
   83033              77 :     if( iLargest==0 ){
   83034                 :       return;
   83035                 :     }else{
   83036              40 :       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   83037              40 :       destroyRootPage(pParse, iLargest, iDb);
   83038              40 :       iDestroyed = iLargest;
   83039                 :     }
   83040              40 :   }
   83041                 : #endif
   83042                 : }
   83043                 : 
   83044                 : /*
   83045                 : ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
   83046                 : ** after a DROP INDEX or DROP TABLE command.
   83047                 : */
   83048             116 : static void sqlite3ClearStatTables(
   83049                 :   Parse *pParse,         /* The parsing context */
   83050                 :   int iDb,               /* The database number */
   83051                 :   const char *zType,     /* "idx" or "tbl" */
   83052                 :   const char *zName      /* Name of index or table */
   83053                 : ){
   83054                 :   int i;
   83055             116 :   const char *zDbName = pParse->db->aDb[iDb].zName;
   83056             464 :   for(i=1; i<=3; i++){
   83057                 :     char zTab[24];
   83058             348 :     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
   83059             348 :     if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
   83060               0 :       sqlite3NestedParse(pParse,
   83061                 :         "DELETE FROM %Q.%s WHERE %s=%Q",
   83062                 :         zDbName, zTab, zType, zName
   83063                 :       );
   83064                 :     }
   83065                 :   }
   83066             116 : }
   83067                 : 
   83068                 : /*
   83069                 : ** Generate code to drop a table.
   83070                 : */
   83071              89 : SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
   83072                 :   Vdbe *v;
   83073              89 :   sqlite3 *db = pParse->db;
   83074                 :   Trigger *pTrigger;
   83075              89 :   Db *pDb = &db->aDb[iDb];
   83076                 : 
   83077              89 :   v = sqlite3GetVdbe(pParse);
   83078              89 :   assert( v!=0 );
   83079              89 :   sqlite3BeginWriteOperation(pParse, 1, iDb);
   83080                 : 
   83081                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   83082              89 :   if( IsVirtual(pTab) ){
   83083              52 :     sqlite3VdbeAddOp0(v, OP_VBegin);
   83084                 :   }
   83085                 : #endif
   83086                 : 
   83087                 :   /* Drop all triggers associated with the table being dropped. Code
   83088                 :   ** is generated to remove entries from sqlite_master and/or
   83089                 :   ** sqlite_temp_master if required.
   83090                 :   */
   83091              89 :   pTrigger = sqlite3TriggerList(pParse, pTab);
   83092             178 :   while( pTrigger ){
   83093               0 :     assert( pTrigger->pSchema==pTab->pSchema || 
   83094                 :         pTrigger->pSchema==db->aDb[1].pSchema );
   83095               0 :     sqlite3DropTriggerPtr(pParse, pTrigger);
   83096               0 :     pTrigger = pTrigger->pNext;
   83097                 :   }
   83098                 : 
   83099                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   83100                 :   /* Remove any entries of the sqlite_sequence table associated with
   83101                 :   ** the table being dropped. This is done before the table is dropped
   83102                 :   ** at the btree level, in case the sqlite_sequence table needs to
   83103                 :   ** move as a result of the drop (can happen in auto-vacuum mode).
   83104                 :   */
   83105              89 :   if( pTab->tabFlags & TF_Autoincrement ){
   83106               0 :     sqlite3NestedParse(pParse,
   83107                 :       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
   83108                 :       pDb->zName, pTab->zName
   83109                 :     );
   83110                 :   }
   83111                 : #endif
   83112                 : 
   83113                 :   /* Drop all SQLITE_MASTER table and index entries that refer to the
   83114                 :   ** table. The program name loops through the master table and deletes
   83115                 :   ** every row that refers to a table of the same name as the one being
   83116                 :   ** dropped. Triggers are handled seperately because a trigger can be
   83117                 :   ** created in the temp database that refers to a table in another
   83118                 :   ** database.
   83119                 :   */
   83120              89 :   sqlite3NestedParse(pParse, 
   83121                 :       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
   83122                 :       pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
   83123              89 :   if( !isView && !IsVirtual(pTab) ){
   83124              37 :     destroyTable(pParse, pTab);
   83125                 :   }
   83126                 : 
   83127                 :   /* Remove the table entry from SQLite's internal schema and modify
   83128                 :   ** the schema cookie.
   83129                 :   */
   83130              89 :   if( IsVirtual(pTab) ){
   83131              52 :     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
   83132                 :   }
   83133              89 :   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
   83134              89 :   sqlite3ChangeCookie(pParse, iDb);
   83135              89 :   sqliteViewResetAll(db, iDb);
   83136              89 : }
   83137                 : 
   83138                 : /*
   83139                 : ** This routine is called to do the work of a DROP TABLE statement.
   83140                 : ** pName is the name of the table to be dropped.
   83141                 : */
   83142              96 : SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
   83143                 :   Table *pTab;
   83144                 :   Vdbe *v;
   83145              96 :   sqlite3 *db = pParse->db;
   83146                 :   int iDb;
   83147                 : 
   83148              96 :   if( db->mallocFailed ){
   83149               0 :     goto exit_drop_table;
   83150                 :   }
   83151              96 :   assert( pParse->nErr==0 );
   83152              96 :   assert( pName->nSrc==1 );
   83153              96 :   if( noErr ) db->suppressErr++;
   83154              96 :   pTab = sqlite3LocateTable(pParse, isView, 
   83155              96 :                             pName->a[0].zName, pName->a[0].zDatabase);
   83156              96 :   if( noErr ) db->suppressErr--;
   83157                 : 
   83158              96 :   if( pTab==0 ){
   83159               7 :     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
   83160               7 :     goto exit_drop_table;
   83161                 :   }
   83162              89 :   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   83163              89 :   assert( iDb>=0 && iDb<db->nDb );
   83164                 : 
   83165                 :   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
   83166                 :   ** it is initialized.
   83167                 :   */
   83168              89 :   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
   83169               0 :     goto exit_drop_table;
   83170                 :   }
   83171                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   83172                 :   {
   83173                 :     int code;
   83174              89 :     const char *zTab = SCHEMA_TABLE(iDb);
   83175              89 :     const char *zDb = db->aDb[iDb].zName;
   83176              89 :     const char *zArg2 = 0;
   83177              89 :     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
   83178               0 :       goto exit_drop_table;
   83179                 :     }
   83180              89 :     if( isView ){
   83181               0 :       if( !OMIT_TEMPDB && iDb==1 ){
   83182               0 :         code = SQLITE_DROP_TEMP_VIEW;
   83183                 :       }else{
   83184               0 :         code = SQLITE_DROP_VIEW;
   83185                 :       }
   83186                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   83187              89 :     }else if( IsVirtual(pTab) ){
   83188              52 :       code = SQLITE_DROP_VTABLE;
   83189              52 :       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
   83190                 : #endif
   83191                 :     }else{
   83192              37 :       if( !OMIT_TEMPDB && iDb==1 ){
   83193              27 :         code = SQLITE_DROP_TEMP_TABLE;
   83194                 :       }else{
   83195              10 :         code = SQLITE_DROP_TABLE;
   83196                 :       }
   83197                 :     }
   83198              89 :     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
   83199               0 :       goto exit_drop_table;
   83200                 :     }
   83201              89 :     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
   83202               0 :       goto exit_drop_table;
   83203                 :     }
   83204                 :   }
   83205                 : #endif
   83206              89 :   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
   83207               0 :     && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
   83208               0 :     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
   83209               0 :     goto exit_drop_table;
   83210                 :   }
   83211                 : 
   83212                 : #ifndef SQLITE_OMIT_VIEW
   83213                 :   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
   83214                 :   ** on a table.
   83215                 :   */
   83216              89 :   if( isView && pTab->pSelect==0 ){
   83217               0 :     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
   83218               0 :     goto exit_drop_table;
   83219                 :   }
   83220              89 :   if( !isView && pTab->pSelect ){
   83221               0 :     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
   83222               0 :     goto exit_drop_table;
   83223                 :   }
   83224                 : #endif
   83225                 : 
   83226                 :   /* Generate code to remove the table from the master table
   83227                 :   ** on disk.
   83228                 :   */
   83229              89 :   v = sqlite3GetVdbe(pParse);
   83230              89 :   if( v ){
   83231              89 :     sqlite3BeginWriteOperation(pParse, 1, iDb);
   83232              89 :     sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
   83233              89 :     sqlite3FkDropTable(pParse, pName, pTab);
   83234              89 :     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
   83235                 :   }
   83236                 : 
   83237                 : exit_drop_table:
   83238              96 :   sqlite3SrcListDelete(db, pName);
   83239              96 : }
   83240                 : 
   83241                 : /*
   83242                 : ** This routine is called to create a new foreign key on the table
   83243                 : ** currently under construction.  pFromCol determines which columns
   83244                 : ** in the current table point to the foreign key.  If pFromCol==0 then
   83245                 : ** connect the key to the last column inserted.  pTo is the name of
   83246                 : ** the table referred to.  pToCol is a list of tables in the other
   83247                 : ** pTo table that the foreign key points to.  flags contains all
   83248                 : ** information about the conflict resolution algorithms specified
   83249                 : ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
   83250                 : **
   83251                 : ** An FKey structure is created and added to the table currently
   83252                 : ** under construction in the pParse->pNewTable field.
   83253                 : **
   83254                 : ** The foreign key is set for IMMEDIATE processing.  A subsequent call
   83255                 : ** to sqlite3DeferForeignKey() might change this to DEFERRED.
   83256                 : */
   83257            3880 : SQLITE_PRIVATE void sqlite3CreateForeignKey(
   83258                 :   Parse *pParse,       /* Parsing context */
   83259                 :   ExprList *pFromCol,  /* Columns in this table that point to other table */
   83260                 :   Token *pTo,          /* Name of the other table */
   83261                 :   ExprList *pToCol,    /* Columns in the other table */
   83262                 :   int flags            /* Conflict resolution algorithms. */
   83263                 : ){
   83264            3880 :   sqlite3 *db = pParse->db;
   83265                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   83266            3880 :   FKey *pFKey = 0;
   83267                 :   FKey *pNextTo;
   83268            3880 :   Table *p = pParse->pNewTable;
   83269                 :   int nByte;
   83270                 :   int i;
   83271                 :   int nCol;
   83272                 :   char *z;
   83273                 : 
   83274            3880 :   assert( pTo!=0 );
   83275            3880 :   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
   83276            3880 :   if( pFromCol==0 ){
   83277              88 :     int iCol = p->nCol-1;
   83278              88 :     if( NEVER(iCol<0) ) goto fk_end;
   83279              88 :     if( pToCol && pToCol->nExpr!=1 ){
   83280               0 :       sqlite3ErrorMsg(pParse, "foreign key on %s"
   83281                 :          " should reference only one column of table %T",
   83282               0 :          p->aCol[iCol].zName, pTo);
   83283               0 :       goto fk_end;
   83284                 :     }
   83285              88 :     nCol = 1;
   83286            3792 :   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
   83287               0 :     sqlite3ErrorMsg(pParse,
   83288                 :         "number of columns in foreign key does not match the number of "
   83289                 :         "columns in the referenced table");
   83290               0 :     goto fk_end;
   83291                 :   }else{
   83292            3792 :     nCol = pFromCol->nExpr;
   83293                 :   }
   83294            3880 :   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
   83295            3880 :   if( pToCol ){
   83296            7760 :     for(i=0; i<pToCol->nExpr; i++){
   83297            3880 :       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
   83298                 :     }
   83299                 :   }
   83300            3880 :   pFKey = sqlite3DbMallocZero(db, nByte );
   83301            3880 :   if( pFKey==0 ){
   83302               0 :     goto fk_end;
   83303                 :   }
   83304            3880 :   pFKey->pFrom = p;
   83305            3880 :   pFKey->pNextFrom = p->pFKey;
   83306            3880 :   z = (char*)&pFKey->aCol[nCol];
   83307            3880 :   pFKey->zTo = z;
   83308            3880 :   memcpy(z, pTo->z, pTo->n);
   83309            3880 :   z[pTo->n] = 0;
   83310            3880 :   sqlite3Dequote(z);
   83311            3880 :   z += pTo->n+1;
   83312            3880 :   pFKey->nCol = nCol;
   83313            3880 :   if( pFromCol==0 ){
   83314              88 :     pFKey->aCol[0].iFrom = p->nCol-1;
   83315                 :   }else{
   83316            7584 :     for(i=0; i<nCol; i++){
   83317                 :       int j;
   83318            8848 :       for(j=0; j<p->nCol; j++){
   83319            8848 :         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
   83320            3792 :           pFKey->aCol[i].iFrom = j;
   83321            3792 :           break;
   83322                 :         }
   83323                 :       }
   83324            3792 :       if( j>=p->nCol ){
   83325               0 :         sqlite3ErrorMsg(pParse, 
   83326                 :           "unknown column \"%s\" in foreign key definition", 
   83327               0 :           pFromCol->a[i].zName);
   83328               0 :         goto fk_end;
   83329                 :       }
   83330                 :     }
   83331                 :   }
   83332            3880 :   if( pToCol ){
   83333            7760 :     for(i=0; i<nCol; i++){
   83334            3880 :       int n = sqlite3Strlen30(pToCol->a[i].zName);
   83335            3880 :       pFKey->aCol[i].zCol = z;
   83336            3880 :       memcpy(z, pToCol->a[i].zName, n);
   83337            3880 :       z[n] = 0;
   83338            3880 :       z += n+1;
   83339                 :     }
   83340                 :   }
   83341            3880 :   pFKey->isDeferred = 0;
   83342            3880 :   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
   83343            3880 :   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
   83344                 : 
   83345            3880 :   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
   83346            7760 :   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, 
   83347            7760 :       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
   83348                 :   );
   83349            3880 :   if( pNextTo==pFKey ){
   83350               0 :     db->mallocFailed = 1;
   83351               0 :     goto fk_end;
   83352                 :   }
   83353            3880 :   if( pNextTo ){
   83354            1896 :     assert( pNextTo->pPrevTo==0 );
   83355            1896 :     pFKey->pNextTo = pNextTo;
   83356            1896 :     pNextTo->pPrevTo = pFKey;
   83357                 :   }
   83358                 : 
   83359                 :   /* Link the foreign key to the table as the last step.
   83360                 :   */
   83361            3880 :   p->pFKey = pFKey;
   83362            3880 :   pFKey = 0;
   83363                 : 
   83364                 : fk_end:
   83365            3880 :   sqlite3DbFree(db, pFKey);
   83366                 : #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
   83367            3880 :   sqlite3ExprListDelete(db, pFromCol);
   83368            3880 :   sqlite3ExprListDelete(db, pToCol);
   83369            3880 : }
   83370                 : 
   83371                 : /*
   83372                 : ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
   83373                 : ** clause is seen as part of a foreign key definition.  The isDeferred
   83374                 : ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
   83375                 : ** The behavior of the most recently created foreign key is adjusted
   83376                 : ** accordingly.
   83377                 : */
   83378            3792 : SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
   83379                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   83380                 :   Table *pTab;
   83381                 :   FKey *pFKey;
   83382            3792 :   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
   83383            3792 :   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
   83384            3792 :   pFKey->isDeferred = (u8)isDeferred;
   83385                 : #endif
   83386                 : }
   83387                 : 
   83388                 : /*
   83389                 : ** Generate code that will erase and refill index *pIdx.  This is
   83390                 : ** used to initialize a newly created index or to recompute the
   83391                 : ** content of an index in response to a REINDEX command.
   83392                 : **
   83393                 : ** if memRootPage is not negative, it means that the index is newly
   83394                 : ** created.  The register specified by memRootPage contains the
   83395                 : ** root page number of the index.  If memRootPage is negative, then
   83396                 : ** the index already exists and must be cleared before being refilled and
   83397                 : ** the root page number of the index is taken from pIndex->tnum.
   83398                 : */
   83399            5611 : static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
   83400            5611 :   Table *pTab = pIndex->pTable;  /* The table that is indexed */
   83401            5611 :   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
   83402            5611 :   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
   83403                 :   int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
   83404                 :   int addr1;                     /* Address of top of loop */
   83405                 :   int addr2;                     /* Address to jump to for next iteration */
   83406                 :   int tnum;                      /* Root page of index */
   83407                 :   Vdbe *v;                       /* Generate code into this virtual machine */
   83408                 :   KeyInfo *pKey;                 /* KeyInfo for index */
   83409                 : #ifdef SQLITE_OMIT_MERGE_SORT
   83410                 :   int regIdxKey;                 /* Registers containing the index key */
   83411                 : #endif
   83412                 :   int regRecord;                 /* Register holding assemblied index record */
   83413            5611 :   sqlite3 *db = pParse->db;      /* The database connection */
   83414            5611 :   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
   83415                 : 
   83416                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   83417            5611 :   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
   83418            5611 :       db->aDb[iDb].zName ) ){
   83419               0 :     return;
   83420                 :   }
   83421                 : #endif
   83422                 : 
   83423                 :   /* Require a write-lock on the table to perform this operation */
   83424            5611 :   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
   83425                 : 
   83426            5611 :   v = sqlite3GetVdbe(pParse);
   83427            5611 :   if( v==0 ) return;
   83428            5611 :   if( memRootPage>=0 ){
   83429            5567 :     tnum = memRootPage;
   83430                 :   }else{
   83431              44 :     tnum = pIndex->tnum;
   83432              44 :     sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
   83433                 :   }
   83434            5611 :   pKey = sqlite3IndexKeyinfo(pParse, pIndex);
   83435            5611 :   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
   83436                 :                     (char *)pKey, P4_KEYINFO_HANDOFF);
   83437            5611 :   if( memRootPage>=0 ){
   83438            5567 :     sqlite3VdbeChangeP5(v, 1);
   83439                 :   }
   83440                 : 
   83441                 : #ifndef SQLITE_OMIT_MERGE_SORT
   83442                 :   /* Open the sorter cursor if we are to use one. */
   83443            5611 :   iSorter = pParse->nTab++;
   83444            5611 :   sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
   83445                 : #else
   83446                 :   iSorter = iTab;
   83447                 : #endif
   83448                 : 
   83449                 :   /* Open the table. Loop through all rows of the table, inserting index
   83450                 :   ** records into the sorter. */
   83451            5611 :   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
   83452            5611 :   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
   83453            5611 :   regRecord = sqlite3GetTempReg(pParse);
   83454                 : 
   83455                 : #ifndef SQLITE_OMIT_MERGE_SORT
   83456            5611 :   sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
   83457            5611 :   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
   83458            5611 :   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
   83459            5611 :   sqlite3VdbeJumpHere(v, addr1);
   83460            5611 :   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
   83461            5611 :   if( pIndex->onError!=OE_None ){
   83462            1526 :     int j2 = sqlite3VdbeCurrentAddr(v) + 3;
   83463            1526 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
   83464            1526 :     addr2 = sqlite3VdbeCurrentAddr(v);
   83465            1526 :     sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
   83466            1526 :     sqlite3HaltConstraint(
   83467                 :         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
   83468                 :     );
   83469                 :   }else{
   83470            4085 :     addr2 = sqlite3VdbeCurrentAddr(v);
   83471                 :   }
   83472            5611 :   sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
   83473            5611 :   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
   83474            5611 :   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
   83475                 : #else
   83476                 :   regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
   83477                 :   addr2 = addr1 + 1;
   83478                 :   if( pIndex->onError!=OE_None ){
   83479                 :     const int regRowid = regIdxKey + pIndex->nColumn;
   83480                 :     const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
   83481                 :     void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
   83482                 : 
   83483                 :     /* The registers accessed by the OP_IsUnique opcode were allocated
   83484                 :     ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
   83485                 :     ** call above. Just before that function was freed they were released
   83486                 :     ** (made available to the compiler for reuse) using 
   83487                 :     ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
   83488                 :     ** opcode use the values stored within seems dangerous. However, since
   83489                 :     ** we can be sure that no other temp registers have been allocated
   83490                 :     ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
   83491                 :     */
   83492                 :     sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
   83493                 :     sqlite3HaltConstraint(
   83494                 :         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
   83495                 :   }
   83496                 :   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
   83497                 :   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
   83498                 : #endif
   83499            5611 :   sqlite3ReleaseTempReg(pParse, regRecord);
   83500            5611 :   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
   83501            5611 :   sqlite3VdbeJumpHere(v, addr1);
   83502                 : 
   83503            5611 :   sqlite3VdbeAddOp1(v, OP_Close, iTab);
   83504            5611 :   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
   83505            5611 :   sqlite3VdbeAddOp1(v, OP_Close, iSorter);
   83506                 : }
   83507                 : 
   83508                 : /*
   83509                 : ** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
   83510                 : ** and pTblList is the name of the table that is to be indexed.  Both will 
   83511                 : ** be NULL for a primary key or an index that is created to satisfy a
   83512                 : ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
   83513                 : ** as the table to be indexed.  pParse->pNewTable is a table that is
   83514                 : ** currently being constructed by a CREATE TABLE statement.
   83515                 : **
   83516                 : ** pList is a list of columns to be indexed.  pList will be NULL if this
   83517                 : ** is a primary key or unique-constraint on the most recent column added
   83518                 : ** to the table currently under construction.  
   83519                 : **
   83520                 : ** If the index is created successfully, return a pointer to the new Index
   83521                 : ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
   83522                 : ** as the tables primary key (Index.autoIndex==2).
   83523                 : */
   83524           34700 : SQLITE_PRIVATE Index *sqlite3CreateIndex(
   83525                 :   Parse *pParse,     /* All information about this parse */
   83526                 :   Token *pName1,     /* First part of index name. May be NULL */
   83527                 :   Token *pName2,     /* Second part of index name. May be NULL */
   83528                 :   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
   83529                 :   ExprList *pList,   /* A list of columns to be indexed */
   83530                 :   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
   83531                 :   Token *pStart,     /* The CREATE token that begins this statement */
   83532                 :   Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
   83533                 :   int sortOrder,     /* Sort order of primary key when pList==NULL */
   83534                 :   int ifNotExist     /* Omit error if index already exists */
   83535                 : ){
   83536           34700 :   Index *pRet = 0;     /* Pointer to return */
   83537           34700 :   Table *pTab = 0;     /* Table to be indexed */
   83538           34700 :   Index *pIndex = 0;   /* The index to be created */
   83539           34700 :   char *zName = 0;     /* Name of the index */
   83540                 :   int nName;           /* Number of characters in zName */
   83541                 :   int i, j;
   83542                 :   Token nullId;        /* Fake token for an empty ID list */
   83543                 :   DbFixer sFix;        /* For assigning database names to pTable */
   83544                 :   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
   83545           34700 :   sqlite3 *db = pParse->db;
   83546                 :   Db *pDb;             /* The specific table containing the indexed database */
   83547                 :   int iDb;             /* Index of the database that is being written */
   83548           34700 :   Token *pName = 0;    /* Unqualified name of the index to create */
   83549                 :   struct ExprList_item *pListItem; /* For looping over pList */
   83550                 :   int nCol;
   83551           34700 :   int nExtra = 0;
   83552                 :   char *zExtra;
   83553                 : 
   83554           34700 :   assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
   83555           34700 :   assert( pParse->nErr==0 );      /* Never called with prior errors */
   83556           34700 :   if( db->mallocFailed || IN_DECLARE_VTAB ){
   83557                 :     goto exit_create_index;
   83558                 :   }
   83559           34700 :   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   83560               0 :     goto exit_create_index;
   83561                 :   }
   83562                 : 
   83563                 :   /*
   83564                 :   ** Find the table that is to be indexed.  Return early if not found.
   83565                 :   */
   83566           34700 :   if( pTblName!=0 ){
   83567                 : 
   83568                 :     /* Use the two-part index name to determine the database 
   83569                 :     ** to search for the table. 'Fix' the table name to this db
   83570                 :     ** before looking up the table.
   83571                 :     */
   83572           18699 :     assert( pName1 && pName2 );
   83573           18699 :     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
   83574           18699 :     if( iDb<0 ) goto exit_create_index;
   83575           18699 :     assert( pName && pName->z );
   83576                 : 
   83577                 : #ifndef SQLITE_OMIT_TEMPDB
   83578                 :     /* If the index name was unqualified, check if the the table
   83579                 :     ** is a temp table. If so, set the database to 1. Do not do this
   83580                 :     ** if initialising a database schema.
   83581                 :     */
   83582           18699 :     if( !db->init.busy ){
   83583            5911 :       pTab = sqlite3SrcListLookup(pParse, pTblName);
   83584            5911 :       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
   83585             104 :         iDb = 1;
   83586                 :       }
   83587                 :     }
   83588                 : #endif
   83589                 : 
   83590           37190 :     if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
   83591           18491 :         sqlite3FixSrcList(&sFix, pTblName)
   83592                 :     ){
   83593                 :       /* Because the parser constructs pTblName from a single identifier,
   83594                 :       ** sqlite3FixSrcList can never fail. */
   83595               0 :       assert(0);
   83596                 :     }
   83597           18699 :     pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, 
   83598           18699 :         pTblName->a[0].zDatabase);
   83599           18699 :     if( !pTab || db->mallocFailed ) goto exit_create_index;
   83600           18699 :     assert( db->aDb[iDb].pSchema==pTab->pSchema );
   83601                 :   }else{
   83602           16001 :     assert( pName==0 );
   83603           16001 :     assert( pStart==0 );
   83604           16001 :     pTab = pParse->pNewTable;
   83605           16001 :     if( !pTab ) goto exit_create_index;
   83606           16001 :     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   83607                 :   }
   83608           34700 :   pDb = &db->aDb[iDb];
   83609                 : 
   83610           34700 :   assert( pTab!=0 );
   83611           34700 :   assert( pParse->nErr==0 );
   83612           34700 :   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 
   83613               0 :        && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
   83614               0 :     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
   83615               0 :     goto exit_create_index;
   83616                 :   }
   83617                 : #ifndef SQLITE_OMIT_VIEW
   83618           34700 :   if( pTab->pSelect ){
   83619               0 :     sqlite3ErrorMsg(pParse, "views may not be indexed");
   83620               0 :     goto exit_create_index;
   83621                 :   }
   83622                 : #endif
   83623                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   83624           34700 :   if( IsVirtual(pTab) ){
   83625               0 :     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
   83626               0 :     goto exit_create_index;
   83627                 :   }
   83628                 : #endif
   83629                 : 
   83630                 :   /*
   83631                 :   ** Find the name of the index.  Make sure there is not already another
   83632                 :   ** index or table with the same name.  
   83633                 :   **
   83634                 :   ** Exception:  If we are reading the names of permanent indices from the
   83635                 :   ** sqlite_master table (because some other process changed the schema) and
   83636                 :   ** one of the index names collides with the name of a temporary table or
   83637                 :   ** index, then we will continue to process this index.
   83638                 :   **
   83639                 :   ** If pName==0 it means that we are
   83640                 :   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
   83641                 :   ** own name.
   83642                 :   */
   83643           34700 :   if( pName ){
   83644           18699 :     zName = sqlite3NameFromToken(db, pName);
   83645           18699 :     if( zName==0 ) goto exit_create_index;
   83646           18699 :     assert( pName->z!=0 );
   83647           18699 :     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   83648               0 :       goto exit_create_index;
   83649                 :     }
   83650           18699 :     if( !db->init.busy ){
   83651            5911 :       if( sqlite3FindTable(db, zName, 0)!=0 ){
   83652               0 :         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
   83653               0 :         goto exit_create_index;
   83654                 :       }
   83655                 :     }
   83656           18699 :     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
   83657             344 :       if( !ifNotExist ){
   83658               1 :         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
   83659                 :       }else{
   83660             343 :         assert( !db->init.busy );
   83661             343 :         sqlite3CodeVerifySchema(pParse, iDb);
   83662                 :       }
   83663             344 :       goto exit_create_index;
   83664                 :     }
   83665                 :   }else{
   83666                 :     int n;
   83667                 :     Index *pLoop;
   83668           16001 :     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
   83669           16001 :     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
   83670           16001 :     if( zName==0 ){
   83671               0 :       goto exit_create_index;
   83672                 :     }
   83673                 :   }
   83674                 : 
   83675                 :   /* Check for authorization to create an index.
   83676                 :   */
   83677                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   83678                 :   {
   83679           34356 :     const char *zDb = pDb->zName;
   83680           34356 :     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
   83681               0 :       goto exit_create_index;
   83682                 :     }
   83683           34356 :     i = SQLITE_CREATE_INDEX;
   83684           34356 :     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
   83685           34356 :     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
   83686               0 :       goto exit_create_index;
   83687                 :     }
   83688                 :   }
   83689                 : #endif
   83690                 : 
   83691                 :   /* If pList==0, it means this routine was called to make a primary
   83692                 :   ** key out of the last column added to the table under construction.
   83693                 :   ** So create a fake list to simulate this.
   83694                 :   */
   83695           34356 :   if( pList==0 ){
   83696            4619 :     nullId.z = pTab->aCol[pTab->nCol-1].zName;
   83697            4619 :     nullId.n = sqlite3Strlen30((char*)nullId.z);
   83698            4619 :     pList = sqlite3ExprListAppend(pParse, 0, 0);
   83699            4619 :     if( pList==0 ) goto exit_create_index;
   83700            4619 :     sqlite3ExprListSetName(pParse, pList, &nullId, 0);
   83701            4619 :     pList->a[0].sortOrder = (u8)sortOrder;
   83702                 :   }
   83703                 : 
   83704                 :   /* Figure out how many bytes of space are required to store explicitly
   83705                 :   ** specified collation sequence names.
   83706                 :   */
   83707           87182 :   for(i=0; i<pList->nExpr; i++){
   83708           52826 :     Expr *pExpr = pList->a[i].pExpr;
   83709           52826 :     if( pExpr ){
   83710               0 :       CollSeq *pColl = pExpr->pColl;
   83711                 :       /* Either pColl!=0 or there was an OOM failure.  But if an OOM
   83712                 :       ** failure we have quit before reaching this point. */
   83713               0 :       if( ALWAYS(pColl) ){
   83714               0 :         nExtra += (1 + sqlite3Strlen30(pColl->zName));
   83715                 :       }
   83716                 :     }
   83717                 :   }
   83718                 : 
   83719                 :   /* 
   83720                 :   ** Allocate the index structure. 
   83721                 :   */
   83722           34356 :   nName = sqlite3Strlen30(zName);
   83723           34356 :   nCol = pList->nExpr;
   83724           34356 :   pIndex = sqlite3DbMallocZero(db, 
   83725                 :       ROUND8(sizeof(Index)) +              /* Index structure  */
   83726           68712 :       ROUND8(sizeof(tRowcnt)*(nCol+1)) +   /* Index.aiRowEst   */
   83727           68712 :       sizeof(char *)*nCol +                /* Index.azColl     */
   83728           68712 :       sizeof(int)*nCol +                   /* Index.aiColumn   */
   83729           34356 :       sizeof(u8)*nCol +                    /* Index.aSortOrder */
   83730           34356 :       nName + 1 +                          /* Index.zName      */
   83731                 :       nExtra                               /* Collation sequence names */
   83732                 :   );
   83733           34356 :   if( db->mallocFailed ){
   83734               0 :     goto exit_create_index;
   83735                 :   }
   83736           34356 :   zExtra = (char*)pIndex;
   83737           34356 :   pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
   83738           68712 :   pIndex->azColl = (char**)
   83739           68712 :      ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
   83740           34356 :   assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
   83741           34356 :   assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
   83742           34356 :   pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
   83743           34356 :   pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
   83744           34356 :   pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
   83745           34356 :   zExtra = (char *)(&pIndex->zName[nName+1]);
   83746           34356 :   memcpy(pIndex->zName, zName, nName+1);
   83747           34356 :   pIndex->pTable = pTab;
   83748           34356 :   pIndex->nColumn = pList->nExpr;
   83749           34356 :   pIndex->onError = (u8)onError;
   83750           34356 :   pIndex->autoIndex = (u8)(pName==0);
   83751           34356 :   pIndex->pSchema = db->aDb[iDb].pSchema;
   83752           34356 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   83753                 : 
   83754                 :   /* Check to see if we should honor DESC requests on index columns
   83755                 :   */
   83756           34356 :   if( pDb->pSchema->file_format>=4 ){
   83757           32947 :     sortOrderMask = -1;   /* Honor DESC */
   83758                 :   }else{
   83759            1409 :     sortOrderMask = 0;    /* Ignore DESC */
   83760                 :   }
   83761                 : 
   83762                 :   /* Scan the names of the columns of the table to be indexed and
   83763                 :   ** load the column indices into the Index structure.  Report an error
   83764                 :   ** if any column is not found.
   83765                 :   **
   83766                 :   ** TODO:  Add a test to make sure that the same column is not named
   83767                 :   ** more than once within the same index.  Only the first instance of
   83768                 :   ** the column will ever be used by the optimizer.  Note that using the
   83769                 :   ** same column more than once cannot be an error because that would 
   83770                 :   ** break backwards compatibility - it needs to be a warning.
   83771                 :   */
   83772           87182 :   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
   83773           52826 :     const char *zColName = pListItem->zName;
   83774                 :     Column *pTabCol;
   83775                 :     int requestedSortOrder;
   83776                 :     char *zColl;                   /* Collation sequence name */
   83777                 : 
   83778          174597 :     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
   83779          174597 :       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
   83780                 :     }
   83781           52826 :     if( j>=pTab->nCol ){
   83782               0 :       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
   83783                 :         pTab->zName, zColName);
   83784               0 :       pParse->checkSchema = 1;
   83785               0 :       goto exit_create_index;
   83786                 :     }
   83787           52826 :     pIndex->aiColumn[i] = j;
   83788                 :     /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
   83789                 :     ** the way the "idxlist" non-terminal is constructed by the parser,
   83790                 :     ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
   83791                 :     ** must exist or else there must have been an OOM error.  But if there
   83792                 :     ** was an OOM error, we would never reach this point. */
   83793           52826 :     if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
   83794                 :       int nColl;
   83795               0 :       zColl = pListItem->pExpr->pColl->zName;
   83796               0 :       nColl = sqlite3Strlen30(zColl) + 1;
   83797               0 :       assert( nExtra>=nColl );
   83798               0 :       memcpy(zExtra, zColl, nColl);
   83799               0 :       zColl = zExtra;
   83800               0 :       zExtra += nColl;
   83801               0 :       nExtra -= nColl;
   83802                 :     }else{
   83803           52826 :       zColl = pTab->aCol[j].zColl;
   83804           52826 :       if( !zColl ){
   83805           52826 :         zColl = db->pDfltColl->zName;
   83806                 :       }
   83807                 :     }
   83808           52826 :     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
   83809               0 :       goto exit_create_index;
   83810                 :     }
   83811           52826 :     pIndex->azColl[i] = zColl;
   83812           52826 :     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
   83813           52826 :     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
   83814                 :   }
   83815           34356 :   sqlite3DefaultRowEst(pIndex);
   83816                 : 
   83817           34356 :   if( pTab==pParse->pNewTable ){
   83818                 :     /* This routine has been called to create an automatic index as a
   83819                 :     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
   83820                 :     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
   83821                 :     ** i.e. one of:
   83822                 :     **
   83823                 :     ** CREATE TABLE t(x PRIMARY KEY, y);
   83824                 :     ** CREATE TABLE t(x, y, UNIQUE(x, y));
   83825                 :     **
   83826                 :     ** Either way, check to see if the table already has such an index. If
   83827                 :     ** so, don't bother creating this one. This only applies to
   83828                 :     ** automatically created indices. Users can do as they wish with
   83829                 :     ** explicit indices.
   83830                 :     **
   83831                 :     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
   83832                 :     ** (and thus suppressing the second one) even if they have different
   83833                 :     ** sort orders.
   83834                 :     **
   83835                 :     ** If there are different collating sequences or if the columns of
   83836                 :     ** the constraint occur in different orders, then the constraints are
   83837                 :     ** considered distinct and both result in separate indices.
   83838                 :     */
   83839                 :     Index *pIdx;
   83840           17821 :     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   83841                 :       int k;
   83842            1820 :       assert( pIdx->onError!=OE_None );
   83843            1820 :       assert( pIdx->autoIndex );
   83844            1820 :       assert( pIndex->onError!=OE_None );
   83845                 : 
   83846            1820 :       if( pIdx->nColumn!=pIndex->nColumn ) continue;
   83847               0 :       for(k=0; k<pIdx->nColumn; k++){
   83848                 :         const char *z1;
   83849                 :         const char *z2;
   83850               0 :         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
   83851               0 :         z1 = pIdx->azColl[k];
   83852               0 :         z2 = pIndex->azColl[k];
   83853               0 :         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
   83854                 :       }
   83855               0 :       if( k==pIdx->nColumn ){
   83856               0 :         if( pIdx->onError!=pIndex->onError ){
   83857                 :           /* This constraint creates the same index as a previous
   83858                 :           ** constraint specified somewhere in the CREATE TABLE statement.
   83859                 :           ** However the ON CONFLICT clauses are different. If both this 
   83860                 :           ** constraint and the previous equivalent constraint have explicit
   83861                 :           ** ON CONFLICT clauses this is an error. Otherwise, use the
   83862                 :           ** explicitly specified behaviour for the index.
   83863                 :           */
   83864               0 :           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
   83865               0 :             sqlite3ErrorMsg(pParse, 
   83866                 :                 "conflicting ON CONFLICT clauses specified", 0);
   83867                 :           }
   83868               0 :           if( pIdx->onError==OE_Default ){
   83869               0 :             pIdx->onError = pIndex->onError;
   83870                 :           }
   83871                 :         }
   83872               0 :         goto exit_create_index;
   83873                 :       }
   83874                 :     }
   83875                 :   }
   83876                 : 
   83877                 :   /* Link the new Index structure to its table and to the other
   83878                 :   ** in-memory database structures. 
   83879                 :   */
   83880           34356 :   if( db->init.busy ){
   83881                 :     Index *p;
   83882           25538 :     assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
   83883           51076 :     p = sqlite3HashInsert(&pIndex->pSchema->idxHash, 
   83884           51076 :                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
   83885                 :                           pIndex);
   83886           25538 :     if( p ){
   83887               0 :       assert( p==pIndex );  /* Malloc must have failed */
   83888               0 :       db->mallocFailed = 1;
   83889               0 :       goto exit_create_index;
   83890                 :     }
   83891           25538 :     db->flags |= SQLITE_InternChanges;
   83892           25538 :     if( pTblName!=0 ){
   83893           12788 :       pIndex->tnum = db->init.newTnum;
   83894                 :     }
   83895                 :   }
   83896                 : 
   83897                 :   /* If the db->init.busy is 0 then create the index on disk.  This
   83898                 :   ** involves writing the index into the master table and filling in the
   83899                 :   ** index with the current table contents.
   83900                 :   **
   83901                 :   ** The db->init.busy is 0 when the user first enters a CREATE INDEX 
   83902                 :   ** command.  db->init.busy is 1 when a database is opened and 
   83903                 :   ** CREATE INDEX statements are read out of the master table.  In
   83904                 :   ** the latter case the index already exists on disk, which is why
   83905                 :   ** we don't want to recreate it.
   83906                 :   **
   83907                 :   ** If pTblName==0 it means this index is generated as a primary key
   83908                 :   ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
   83909                 :   ** has just been created, it contains no data and the index initialization
   83910                 :   ** step can be skipped.
   83911                 :   */
   83912                 :   else{ /* if( db->init.busy==0 ) */
   83913                 :     Vdbe *v;
   83914                 :     char *zStmt;
   83915            8818 :     int iMem = ++pParse->nMem;
   83916                 : 
   83917            8818 :     v = sqlite3GetVdbe(pParse);
   83918            8818 :     if( v==0 ) goto exit_create_index;
   83919                 : 
   83920                 : 
   83921                 :     /* Create the rootpage for the index
   83922                 :     */
   83923            8818 :     sqlite3BeginWriteOperation(pParse, 1, iDb);
   83924            8818 :     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
   83925                 : 
   83926                 :     /* Gather the complete text of the CREATE INDEX statement into
   83927                 :     ** the zStmt variable
   83928                 :     */
   83929            8818 :     if( pStart ){
   83930            5567 :       assert( pEnd!=0 );
   83931                 :       /* A named index with an explicit CREATE INDEX statement */
   83932           11134 :       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
   83933                 :         onError==OE_None ? "" : " UNIQUE",
   83934            5567 :         (int)(pEnd->z - pName->z) + 1,
   83935            5567 :         pName->z);
   83936                 :     }else{
   83937                 :       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
   83938                 :       /* zStmt = sqlite3MPrintf(""); */
   83939            3251 :       zStmt = 0;
   83940                 :     }
   83941                 : 
   83942                 :     /* Add an entry in sqlite_master for this index
   83943                 :     */
   83944           17636 :     sqlite3NestedParse(pParse, 
   83945                 :         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
   83946            8818 :         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
   83947                 :         pIndex->zName,
   83948                 :         pTab->zName,
   83949                 :         iMem,
   83950                 :         zStmt
   83951                 :     );
   83952            8818 :     sqlite3DbFree(db, zStmt);
   83953                 : 
   83954                 :     /* Fill the index with data and reparse the schema. Code an OP_Expire
   83955                 :     ** to invalidate all pre-compiled statements.
   83956                 :     */
   83957            8818 :     if( pTblName ){
   83958            5567 :       sqlite3RefillIndex(pParse, pIndex, iMem);
   83959            5567 :       sqlite3ChangeCookie(pParse, iDb);
   83960            5567 :       sqlite3VdbeAddParseSchemaOp(v, iDb,
   83961                 :          sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
   83962            5567 :       sqlite3VdbeAddOp1(v, OP_Expire, 0);
   83963                 :     }
   83964                 :   }
   83965                 : 
   83966                 :   /* When adding an index to the list of indices for a table, make
   83967                 :   ** sure all indices labeled OE_Replace come after all those labeled
   83968                 :   ** OE_Ignore.  This is necessary for the correct constraint check
   83969                 :   ** processing (in sqlite3GenerateConstraintChecks()) as part of
   83970                 :   ** UPDATE and INSERT statements.  
   83971                 :   */
   83972           34356 :   if( db->init.busy || pTblName==0 ){
   83973           28789 :     if( onError!=OE_Replace || pTab->pIndex==0
   83974               0 :          || pTab->pIndex->onError==OE_Replace){
   83975           28789 :       pIndex->pNext = pTab->pIndex;
   83976           28789 :       pTab->pIndex = pIndex;
   83977                 :     }else{
   83978               0 :       Index *pOther = pTab->pIndex;
   83979               0 :       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
   83980               0 :         pOther = pOther->pNext;
   83981                 :       }
   83982               0 :       pIndex->pNext = pOther->pNext;
   83983               0 :       pOther->pNext = pIndex;
   83984                 :     }
   83985           28789 :     pRet = pIndex;
   83986           28789 :     pIndex = 0;
   83987                 :   }
   83988                 : 
   83989                 :   /* Clean up before exiting */
   83990                 : exit_create_index:
   83991           34700 :   if( pIndex ){
   83992            5567 :     sqlite3DbFree(db, pIndex->zColAff);
   83993            5567 :     sqlite3DbFree(db, pIndex);
   83994                 :   }
   83995           34700 :   sqlite3ExprListDelete(db, pList);
   83996           34700 :   sqlite3SrcListDelete(db, pTblName);
   83997           34700 :   sqlite3DbFree(db, zName);
   83998           34700 :   return pRet;
   83999                 : }
   84000                 : 
   84001                 : /*
   84002                 : ** Fill the Index.aiRowEst[] array with default information - information
   84003                 : ** to be used when we have not run the ANALYZE command.
   84004                 : **
   84005                 : ** aiRowEst[0] is suppose to contain the number of elements in the index.
   84006                 : ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
   84007                 : ** number of rows in the table that match any particular value of the
   84008                 : ** first column of the index.  aiRowEst[2] is an estimate of the number
   84009                 : ** of rows that match any particular combiniation of the first 2 columns
   84010                 : ** of the index.  And so forth.  It must always be the case that
   84011                 : *
   84012                 : **           aiRowEst[N]<=aiRowEst[N-1]
   84013                 : **           aiRowEst[N]>=1
   84014                 : **
   84015                 : ** Apart from that, we have little to go on besides intuition as to
   84016                 : ** how aiRowEst[] should be initialized.  The numbers generated here
   84017                 : ** are based on typical values found in actual indices.
   84018                 : */
   84019           95615 : SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
   84020           95615 :   tRowcnt *a = pIdx->aiRowEst;
   84021                 :   int i;
   84022                 :   tRowcnt n;
   84023           95615 :   assert( a!=0 );
   84024           95615 :   a[0] = pIdx->pTable->nRowEst;
   84025           95615 :   if( a[0]<10 ) a[0] = 10;
   84026           95615 :   n = 10;
   84027          234330 :   for(i=1; i<=pIdx->nColumn; i++){
   84028          138715 :     a[i] = n;
   84029          138715 :     if( n>5 ) n--;
   84030                 :   }
   84031           95615 :   if( pIdx->onError!=OE_None ){
   84032           54153 :     a[pIdx->nColumn] = 1;
   84033                 :   }
   84034           95615 : }
   84035                 : 
   84036                 : /*
   84037                 : ** This routine will drop an existing named index.  This routine
   84038                 : ** implements the DROP INDEX statement.
   84039                 : */
   84040              58 : SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
   84041                 :   Index *pIndex;
   84042                 :   Vdbe *v;
   84043              58 :   sqlite3 *db = pParse->db;
   84044                 :   int iDb;
   84045                 : 
   84046              58 :   assert( pParse->nErr==0 );   /* Never called with prior errors */
   84047              58 :   if( db->mallocFailed ){
   84048               0 :     goto exit_drop_index;
   84049                 :   }
   84050              58 :   assert( pName->nSrc==1 );
   84051              58 :   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   84052               0 :     goto exit_drop_index;
   84053                 :   }
   84054              58 :   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
   84055              58 :   if( pIndex==0 ){
   84056              31 :     if( !ifExists ){
   84057               0 :       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
   84058                 :     }else{
   84059              31 :       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
   84060                 :     }
   84061              31 :     pParse->checkSchema = 1;
   84062              31 :     goto exit_drop_index;
   84063                 :   }
   84064              27 :   if( pIndex->autoIndex ){
   84065               0 :     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
   84066                 :       "or PRIMARY KEY constraint cannot be dropped", 0);
   84067               0 :     goto exit_drop_index;
   84068                 :   }
   84069              27 :   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
   84070                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   84071                 :   {
   84072              27 :     int code = SQLITE_DROP_INDEX;
   84073              27 :     Table *pTab = pIndex->pTable;
   84074              27 :     const char *zDb = db->aDb[iDb].zName;
   84075              27 :     const char *zTab = SCHEMA_TABLE(iDb);
   84076              27 :     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
   84077               0 :       goto exit_drop_index;
   84078                 :     }
   84079              27 :     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
   84080              27 :     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
   84081               0 :       goto exit_drop_index;
   84082                 :     }
   84083                 :   }
   84084                 : #endif
   84085                 : 
   84086                 :   /* Generate code to remove the index and from the master table */
   84087              27 :   v = sqlite3GetVdbe(pParse);
   84088              27 :   if( v ){
   84089              27 :     sqlite3BeginWriteOperation(pParse, 1, iDb);
   84090              54 :     sqlite3NestedParse(pParse,
   84091                 :        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
   84092              27 :        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
   84093                 :     );
   84094              27 :     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
   84095              27 :     sqlite3ChangeCookie(pParse, iDb);
   84096              27 :     destroyRootPage(pParse, pIndex->tnum, iDb);
   84097              27 :     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
   84098                 :   }
   84099                 : 
   84100                 : exit_drop_index:
   84101              58 :   sqlite3SrcListDelete(db, pName);
   84102              58 : }
   84103                 : 
   84104                 : /*
   84105                 : ** pArray is a pointer to an array of objects.  Each object in the
   84106                 : ** array is szEntry bytes in size.  This routine allocates a new
   84107                 : ** object on the end of the array.
   84108                 : **
   84109                 : ** *pnEntry is the number of entries already in use.  *pnAlloc is
   84110                 : ** the previously allocated size of the array.  initSize is the
   84111                 : ** suggested initial array size allocation.
   84112                 : **
   84113                 : ** The index of the new entry is returned in *pIdx.
   84114                 : **
   84115                 : ** This routine returns a pointer to the array of objects.  This
   84116                 : ** might be the same as the pArray parameter or it might be a different
   84117                 : ** pointer if the array was resized.
   84118                 : */
   84119           39343 : SQLITE_PRIVATE void *sqlite3ArrayAllocate(
   84120                 :   sqlite3 *db,      /* Connection to notify of malloc failures */
   84121                 :   void *pArray,     /* Array of objects.  Might be reallocated */
   84122                 :   int szEntry,      /* Size of each object in the array */
   84123                 :   int initSize,     /* Suggested initial allocation, in elements */
   84124                 :   int *pnEntry,     /* Number of objects currently in use */
   84125                 :   int *pnAlloc,     /* Current size of the allocation, in elements */
   84126                 :   int *pIdx         /* Write the index of a new slot here */
   84127                 : ){
   84128                 :   char *z;
   84129           39343 :   if( *pnEntry >= *pnAlloc ){
   84130                 :     void *pNew;
   84131                 :     int newSize;
   84132           15438 :     newSize = (*pnAlloc)*2 + initSize;
   84133           15438 :     pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry);
   84134           15438 :     if( pNew==0 ){
   84135               0 :       *pIdx = -1;
   84136               0 :       return pArray;
   84137                 :     }
   84138           15438 :     *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry;
   84139           15438 :     pArray = pNew;
   84140                 :   }
   84141           39343 :   z = (char*)pArray;
   84142           39343 :   memset(&z[*pnEntry * szEntry], 0, szEntry);
   84143           39343 :   *pIdx = *pnEntry;
   84144           39343 :   ++*pnEntry;
   84145           39343 :   return pArray;
   84146                 : }
   84147                 : 
   84148                 : /*
   84149                 : ** Append a new element to the given IdList.  Create a new IdList if
   84150                 : ** need be.
   84151                 : **
   84152                 : ** A new IdList is returned, or NULL if malloc() fails.
   84153                 : */
   84154           32363 : SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
   84155                 :   int i;
   84156           32363 :   if( pList==0 ){
   84157            8620 :     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
   84158            8620 :     if( pList==0 ) return 0;
   84159            8620 :     pList->nAlloc = 0;
   84160                 :   }
   84161           64726 :   pList->a = sqlite3ArrayAllocate(
   84162                 :       db,
   84163           32363 :       pList->a,
   84164                 :       sizeof(pList->a[0]),
   84165                 :       5,
   84166                 :       &pList->nId,
   84167                 :       &pList->nAlloc,
   84168                 :       &i
   84169                 :   );
   84170           32363 :   if( i<0 ){
   84171               0 :     sqlite3IdListDelete(db, pList);
   84172               0 :     return 0;
   84173                 :   }
   84174           32363 :   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
   84175           32363 :   return pList;
   84176                 : }
   84177                 : 
   84178                 : /*
   84179                 : ** Delete an IdList.
   84180                 : */
   84181          208288 : SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
   84182                 :   int i;
   84183          208288 :   if( pList==0 ) return;
   84184           47103 :   for(i=0; i<pList->nId; i++){
   84185           35885 :     sqlite3DbFree(db, pList->a[i].zName);
   84186                 :   }
   84187           11218 :   sqlite3DbFree(db, pList->a);
   84188           11218 :   sqlite3DbFree(db, pList);
   84189                 : }
   84190                 : 
   84191                 : /*
   84192                 : ** Return the index in pList of the identifier named zId.  Return -1
   84193                 : ** if not found.
   84194                 : */
   84195            5976 : SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
   84196                 :   int i;
   84197            5976 :   if( pList==0 ) return -1;
   84198           10728 :   for(i=0; i<pList->nId; i++){
   84199            5976 :     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
   84200                 :   }
   84201            4752 :   return -1;
   84202                 : }
   84203                 : 
   84204                 : /*
   84205                 : ** Expand the space allocated for the given SrcList object by
   84206                 : ** creating nExtra new slots beginning at iStart.  iStart is zero based.
   84207                 : ** New slots are zeroed.
   84208                 : **
   84209                 : ** For example, suppose a SrcList initially contains two entries: A,B.
   84210                 : ** To append 3 new entries onto the end, do this:
   84211                 : **
   84212                 : **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
   84213                 : **
   84214                 : ** After the call above it would contain:  A, B, nil, nil, nil.
   84215                 : ** If the iStart argument had been 1 instead of 2, then the result
   84216                 : ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
   84217                 : ** the iStart value would be 0.  The result then would
   84218                 : ** be: nil, nil, nil, A, B.
   84219                 : **
   84220                 : ** If a memory allocation fails the SrcList is unchanged.  The
   84221                 : ** db->mallocFailed flag will be set to true.
   84222                 : */
   84223          140499 : SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
   84224                 :   sqlite3 *db,       /* Database connection to notify of OOM errors */
   84225                 :   SrcList *pSrc,     /* The SrcList to be enlarged */
   84226                 :   int nExtra,        /* Number of new slots to add to pSrc->a[] */
   84227                 :   int iStart         /* Index in pSrc->a[] of first new slot */
   84228                 : ){
   84229                 :   int i;
   84230                 : 
   84231                 :   /* Sanity checking on calling parameters */
   84232          140499 :   assert( iStart>=0 );
   84233          140499 :   assert( nExtra>=1 );
   84234          140499 :   assert( pSrc!=0 );
   84235          140499 :   assert( iStart<=pSrc->nSrc );
   84236                 : 
   84237                 :   /* Allocate additional space if needed */
   84238          140499 :   if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
   84239                 :     SrcList *pNew;
   84240            5338 :     int nAlloc = pSrc->nSrc+nExtra;
   84241                 :     int nGot;
   84242            5338 :     pNew = sqlite3DbRealloc(db, pSrc,
   84243            5338 :                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
   84244            5338 :     if( pNew==0 ){
   84245               0 :       assert( db->mallocFailed );
   84246               0 :       return pSrc;
   84247                 :     }
   84248            5338 :     pSrc = pNew;
   84249            5338 :     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
   84250            5338 :     pSrc->nAlloc = (u16)nGot;
   84251                 :   }
   84252                 : 
   84253                 :   /* Move existing slots that come after the newly inserted slots
   84254                 :   ** out of the way */
   84255          140499 :   for(i=pSrc->nSrc-1; i>=iStart; i--){
   84256               0 :     pSrc->a[i+nExtra] = pSrc->a[i];
   84257                 :   }
   84258          140499 :   pSrc->nSrc += (i16)nExtra;
   84259                 : 
   84260                 :   /* Zero the newly allocated slots */
   84261          140499 :   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
   84262          280998 :   for(i=iStart; i<iStart+nExtra; i++){
   84263          140499 :     pSrc->a[i].iCursor = -1;
   84264                 :   }
   84265                 : 
   84266                 :   /* Return a pointer to the enlarged SrcList */
   84267          140499 :   return pSrc;
   84268                 : }
   84269                 : 
   84270                 : 
   84271                 : /*
   84272                 : ** Append a new table name to the given SrcList.  Create a new SrcList if
   84273                 : ** need be.  A new entry is created in the SrcList even if pTable is NULL.
   84274                 : **
   84275                 : ** A SrcList is returned, or NULL if there is an OOM error.  The returned
   84276                 : ** SrcList might be the same as the SrcList that was input or it might be
   84277                 : ** a new one.  If an OOM error does occurs, then the prior value of pList
   84278                 : ** that is input to this routine is automatically freed.
   84279                 : **
   84280                 : ** If pDatabase is not null, it means that the table has an optional
   84281                 : ** database name prefix.  Like this:  "database.table".  The pDatabase
   84282                 : ** points to the table name and the pTable points to the database name.
   84283                 : ** The SrcList.a[].zName field is filled with the table name which might
   84284                 : ** come from pTable (if pDatabase is NULL) or from pDatabase.  
   84285                 : ** SrcList.a[].zDatabase is filled with the database name from pTable,
   84286                 : ** or with NULL if no database is specified.
   84287                 : **
   84288                 : ** In other words, if call like this:
   84289                 : **
   84290                 : **         sqlite3SrcListAppend(D,A,B,0);
   84291                 : **
   84292                 : ** Then B is a table name and the database name is unspecified.  If called
   84293                 : ** like this:
   84294                 : **
   84295                 : **         sqlite3SrcListAppend(D,A,B,C);
   84296                 : **
   84297                 : ** Then C is the table name and B is the database name.  If C is defined
   84298                 : ** then so is B.  In other words, we never have a case where:
   84299                 : **
   84300                 : **         sqlite3SrcListAppend(D,A,0,C);
   84301                 : **
   84302                 : ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
   84303                 : ** before being added to the SrcList.
   84304                 : */
   84305          140499 : SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
   84306                 :   sqlite3 *db,        /* Connection to notify of malloc failures */
   84307                 :   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
   84308                 :   Token *pTable,      /* Table to append */
   84309                 :   Token *pDatabase    /* Database of the table */
   84310                 : ){
   84311                 :   struct SrcList_item *pItem;
   84312          140499 :   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
   84313          140499 :   if( pList==0 ){
   84314          135161 :     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
   84315          135161 :     if( pList==0 ) return 0;
   84316          135161 :     pList->nAlloc = 1;
   84317                 :   }
   84318          140499 :   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
   84319          140499 :   if( db->mallocFailed ){
   84320               0 :     sqlite3SrcListDelete(db, pList);
   84321               0 :     return 0;
   84322                 :   }
   84323          140499 :   pItem = &pList->a[pList->nSrc-1];
   84324          140499 :   if( pDatabase && pDatabase->z==0 ){
   84325           75807 :     pDatabase = 0;
   84326                 :   }
   84327          140499 :   if( pDatabase ){
   84328           41160 :     Token *pTemp = pDatabase;
   84329           41160 :     pDatabase = pTable;
   84330           41160 :     pTable = pTemp;
   84331                 :   }
   84332          140499 :   pItem->zName = sqlite3NameFromToken(db, pTable);
   84333          140499 :   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
   84334          140499 :   return pList;
   84335                 : }
   84336                 : 
   84337                 : /*
   84338                 : ** Assign VdbeCursor index numbers to all tables in a SrcList
   84339                 : */
   84340           60296 : SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
   84341                 :   int i;
   84342                 :   struct SrcList_item *pItem;
   84343           60296 :   assert(pList || pParse->db->mallocFailed );
   84344           60296 :   if( pList ){
   84345          124045 :     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
   84346           63943 :       if( pItem->iCursor>=0 ) break;
   84347           63749 :       pItem->iCursor = pParse->nTab++;
   84348           63749 :       if( pItem->pSelect ){
   84349             122 :         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
   84350                 :       }
   84351                 :     }
   84352                 :   }
   84353           60296 : }
   84354                 : 
   84355                 : /*
   84356                 : ** Delete an entire SrcList including all its substructure.
   84357                 : */
   84358          167469 : SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
   84359                 :   int i;
   84360                 :   struct SrcList_item *pItem;
   84361          167469 :   if( pList==0 ) return;
   84362          301528 :   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
   84363          150060 :     sqlite3DbFree(db, pItem->zDatabase);
   84364          150060 :     sqlite3DbFree(db, pItem->zName);
   84365          150060 :     sqlite3DbFree(db, pItem->zAlias);
   84366          150060 :     sqlite3DbFree(db, pItem->zIndex);
   84367          150060 :     sqlite3DeleteTable(db, pItem->pTab);
   84368          150060 :     sqlite3SelectDelete(db, pItem->pSelect);
   84369          150060 :     sqlite3ExprDelete(db, pItem->pOn);
   84370          150060 :     sqlite3IdListDelete(db, pItem->pUsing);
   84371                 :   }
   84372          151468 :   sqlite3DbFree(db, pList);
   84373                 : }
   84374                 : 
   84375                 : /*
   84376                 : ** This routine is called by the parser to add a new term to the
   84377                 : ** end of a growing FROM clause.  The "p" parameter is the part of
   84378                 : ** the FROM clause that has already been constructed.  "p" is NULL
   84379                 : ** if this is the first term of the FROM clause.  pTable and pDatabase
   84380                 : ** are the name of the table and database named in the FROM clause term.
   84381                 : ** pDatabase is NULL if the database name qualifier is missing - the
   84382                 : ** usual case.  If the term has a alias, then pAlias points to the
   84383                 : ** alias token.  If the term is a subquery, then pSubquery is the
   84384                 : ** SELECT statement that the subquery encodes.  The pTable and
   84385                 : ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
   84386                 : ** parameters are the content of the ON and USING clauses.
   84387                 : **
   84388                 : ** Return a new SrcList which encodes is the FROM with the new
   84389                 : ** term added.
   84390                 : */
   84391           67610 : SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
   84392                 :   Parse *pParse,          /* Parsing context */
   84393                 :   SrcList *p,             /* The left part of the FROM clause already seen */
   84394                 :   Token *pTable,          /* Name of the table to add to the FROM clause */
   84395                 :   Token *pDatabase,       /* Name of the database containing pTable */
   84396                 :   Token *pAlias,          /* The right-hand side of the AS subexpression */
   84397                 :   Select *pSubquery,      /* A subquery used in place of a table name */
   84398                 :   Expr *pOn,              /* The ON clause of a join */
   84399                 :   IdList *pUsing          /* The USING clause of a join */
   84400                 : ){
   84401                 :   struct SrcList_item *pItem;
   84402           67610 :   sqlite3 *db = pParse->db;
   84403           67610 :   if( !p && (pOn || pUsing) ){
   84404               0 :     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
   84405                 :       (pOn ? "ON" : "USING")
   84406                 :     );
   84407               0 :     goto append_from_error;
   84408                 :   }
   84409           67610 :   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
   84410           67610 :   if( p==0 || NEVER(p->nSrc==0) ){
   84411                 :     goto append_from_error;
   84412                 :   }
   84413           67610 :   pItem = &p->a[p->nSrc-1];
   84414           67610 :   assert( pAlias!=0 );
   84415           67610 :   if( pAlias->n ){
   84416            7570 :     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
   84417                 :   }
   84418           67610 :   pItem->pSelect = pSubquery;
   84419           67610 :   pItem->pOn = pOn;
   84420           67610 :   pItem->pUsing = pUsing;
   84421           67610 :   return p;
   84422                 : 
   84423                 :  append_from_error:
   84424               0 :   assert( p==0 );
   84425               0 :   sqlite3ExprDelete(db, pOn);
   84426               0 :   sqlite3IdListDelete(db, pUsing);
   84427               0 :   sqlite3SelectDelete(db, pSubquery);
   84428               0 :   return 0;
   84429                 : }
   84430                 : 
   84431                 : /*
   84432                 : ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
   84433                 : ** element of the source-list passed as the second argument.
   84434                 : */
   84435           85403 : SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
   84436           85403 :   assert( pIndexedBy!=0 );
   84437           85403 :   if( p && ALWAYS(p->nSrc>0) ){
   84438           85403 :     struct SrcList_item *pItem = &p->a[p->nSrc-1];
   84439           85403 :     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
   84440           85403 :     if( pIndexedBy->n==1 && !pIndexedBy->z ){
   84441                 :       /* A "NOT INDEXED" clause was supplied. See parse.y 
   84442                 :       ** construct "indexed_opt" for details. */
   84443               0 :       pItem->notIndexed = 1;
   84444                 :     }else{
   84445           85403 :       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
   84446                 :     }
   84447                 :   }
   84448           85403 : }
   84449                 : 
   84450                 : /*
   84451                 : ** When building up a FROM clause in the parser, the join operator
   84452                 : ** is initially attached to the left operand.  But the code generator
   84453                 : ** expects the join operator to be on the right operand.  This routine
   84454                 : ** Shifts all join operators from left to right for an entire FROM
   84455                 : ** clause.
   84456                 : **
   84457                 : ** Example: Suppose the join is like this:
   84458                 : **
   84459                 : **           A natural cross join B
   84460                 : **
   84461                 : ** The operator is "natural cross join".  The A and B operands are stored
   84462                 : ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
   84463                 : ** operator with A.  This routine shifts that operator over to B.
   84464                 : */
   84465           62272 : SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
   84466           62272 :   if( p ){
   84467                 :     int i;
   84468           62272 :     assert( p->a || p->nSrc==0 );
   84469           67610 :     for(i=p->nSrc-1; i>0; i--){
   84470            5338 :       p->a[i].jointype = p->a[i-1].jointype;
   84471                 :     }
   84472           62272 :     p->a[0].jointype = 0;
   84473                 :   }
   84474           62272 : }
   84475                 : 
   84476                 : /*
   84477                 : ** Begin a transaction
   84478                 : */
   84479            8415 : SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
   84480                 :   sqlite3 *db;
   84481                 :   Vdbe *v;
   84482                 :   int i;
   84483                 : 
   84484            8415 :   assert( pParse!=0 );
   84485            8415 :   db = pParse->db;
   84486            8415 :   assert( db!=0 );
   84487                 : /*  if( db->aDb[0].pBt==0 ) return; */
   84488            8415 :   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
   84489               0 :     return;
   84490                 :   }
   84491            8415 :   v = sqlite3GetVdbe(pParse);
   84492            8415 :   if( !v ) return;
   84493            8415 :   if( type!=TK_DEFERRED ){
   84494           10905 :     for(i=0; i<db->nDb; i++){
   84495            7272 :       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
   84496            7272 :       sqlite3VdbeUsesBtree(v, i);
   84497                 :     }
   84498                 :   }
   84499            8415 :   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
   84500                 : }
   84501                 : 
   84502                 : /*
   84503                 : ** Commit a transaction
   84504                 : */
   84505            8365 : SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
   84506                 :   Vdbe *v;
   84507                 : 
   84508            8365 :   assert( pParse!=0 );
   84509            8365 :   assert( pParse->db!=0 );
   84510            8365 :   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
   84511               0 :     return;
   84512                 :   }
   84513            8365 :   v = sqlite3GetVdbe(pParse);
   84514            8365 :   if( v ){
   84515            8365 :     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
   84516                 :   }
   84517                 : }
   84518                 : 
   84519                 : /*
   84520                 : ** Rollback a transaction
   84521                 : */
   84522              44 : SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
   84523                 :   Vdbe *v;
   84524                 : 
   84525              44 :   assert( pParse!=0 );
   84526              44 :   assert( pParse->db!=0 );
   84527              44 :   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
   84528               0 :     return;
   84529                 :   }
   84530              44 :   v = sqlite3GetVdbe(pParse);
   84531              44 :   if( v ){
   84532              44 :     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
   84533                 :   }
   84534                 : }
   84535                 : 
   84536                 : /*
   84537                 : ** This function is called by the parser when it parses a command to create,
   84538                 : ** release or rollback an SQL savepoint. 
   84539                 : */
   84540            3026 : SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
   84541            3026 :   char *zName = sqlite3NameFromToken(pParse->db, pName);
   84542            3026 :   if( zName ){
   84543            3026 :     Vdbe *v = sqlite3GetVdbe(pParse);
   84544                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   84545                 :     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
   84546                 :     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
   84547                 : #endif
   84548            3026 :     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
   84549               0 :       sqlite3DbFree(pParse->db, zName);
   84550               0 :       return;
   84551                 :     }
   84552            3026 :     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
   84553                 :   }
   84554                 : }
   84555                 : 
   84556                 : /*
   84557                 : ** Make sure the TEMP database is open and available for use.  Return
   84558                 : ** the number of errors.  Leave any error messages in the pParse structure.
   84559                 : */
   84560            5207 : SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
   84561            5207 :   sqlite3 *db = pParse->db;
   84562            5207 :   if( db->aDb[1].pBt==0 && !pParse->explain ){
   84563                 :     int rc;
   84564                 :     Btree *pBt;
   84565                 :     static const int flags = 
   84566                 :           SQLITE_OPEN_READWRITE |
   84567                 :           SQLITE_OPEN_CREATE |
   84568                 :           SQLITE_OPEN_EXCLUSIVE |
   84569                 :           SQLITE_OPEN_DELETEONCLOSE |
   84570                 :           SQLITE_OPEN_TEMP_DB;
   84571                 : 
   84572             383 :     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
   84573             383 :     if( rc!=SQLITE_OK ){
   84574               0 :       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
   84575                 :         "file for storing temporary tables");
   84576               0 :       pParse->rc = rc;
   84577               0 :       return 1;
   84578                 :     }
   84579             383 :     db->aDb[1].pBt = pBt;
   84580             383 :     assert( db->aDb[1].pSchema );
   84581             383 :     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
   84582               0 :       db->mallocFailed = 1;
   84583               0 :       return 1;
   84584                 :     }
   84585                 :   }
   84586            5207 :   return 0;
   84587                 : }
   84588                 : 
   84589                 : /*
   84590                 : ** Generate VDBE code that will verify the schema cookie and start
   84591                 : ** a read-transaction for all named database files.
   84592                 : **
   84593                 : ** It is important that all schema cookies be verified and all
   84594                 : ** read transactions be started before anything else happens in
   84595                 : ** the VDBE program.  But this routine can be called after much other
   84596                 : ** code has been generated.  So here is what we do:
   84597                 : **
   84598                 : ** The first time this routine is called, we code an OP_Goto that
   84599                 : ** will jump to a subroutine at the end of the program.  Then we
   84600                 : ** record every database that needs its schema verified in the
   84601                 : ** pParse->cookieMask field.  Later, after all other code has been
   84602                 : ** generated, the subroutine that does the cookie verifications and
   84603                 : ** starts the transactions will be coded and the OP_Goto P2 value
   84604                 : ** will be made to point to that subroutine.  The generation of the
   84605                 : ** cookie verification subroutine code happens in sqlite3FinishCoding().
   84606                 : **
   84607                 : ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
   84608                 : ** schema on any databases.  This can be used to position the OP_Goto
   84609                 : ** early in the code, before we know if any database tables will be used.
   84610                 : */
   84611          230500 : SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
   84612          230500 :   Parse *pToplevel = sqlite3ParseToplevel(pParse);
   84613                 : 
   84614          230500 :   if( pToplevel->cookieGoto==0 ){
   84615           80416 :     Vdbe *v = sqlite3GetVdbe(pToplevel);
   84616           80416 :     if( v==0 ) return;  /* This only happens if there was a prior error */
   84617           80416 :     pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
   84618                 :   }
   84619          230500 :   if( iDb>=0 ){
   84620          149043 :     sqlite3 *db = pToplevel->db;
   84621                 :     yDbMask mask;
   84622                 : 
   84623          149043 :     assert( iDb<db->nDb );
   84624          149043 :     assert( db->aDb[iDb].pBt!=0 || iDb==1 );
   84625          149043 :     assert( iDb<SQLITE_MAX_ATTACHED+2 );
   84626          149043 :     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   84627          149043 :     mask = ((yDbMask)1)<<iDb;
   84628          149043 :     if( (pToplevel->cookieMask & mask)==0 ){
   84629           80426 :       pToplevel->cookieMask |= mask;
   84630           80426 :       pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
   84631           80426 :       if( !OMIT_TEMPDB && iDb==1 ){
   84632            5207 :         sqlite3OpenTempDatabase(pToplevel);
   84633                 :       }
   84634                 :     }
   84635                 :   }
   84636                 : }
   84637                 : 
   84638                 : /*
   84639                 : ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each 
   84640                 : ** attached database. Otherwise, invoke it for the database named zDb only.
   84641                 : */
   84642              58 : SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
   84643              58 :   sqlite3 *db = pParse->db;
   84644                 :   int i;
   84645             174 :   for(i=0; i<db->nDb; i++){
   84646             116 :     Db *pDb = &db->aDb[i];
   84647             116 :     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
   84648              58 :       sqlite3CodeVerifySchema(pParse, i);
   84649                 :     }
   84650                 :   }
   84651              58 : }
   84652                 : 
   84653                 : /*
   84654                 : ** Generate VDBE code that prepares for doing an operation that
   84655                 : ** might change the database.
   84656                 : **
   84657                 : ** This routine starts a new transaction if we are not already within
   84658                 : ** a transaction.  If we are already within a transaction, then a checkpoint
   84659                 : ** is set if the setStatement parameter is true.  A checkpoint should
   84660                 : ** be set for operations that might fail (due to a constraint) part of
   84661                 : ** the way through and which will need to undo some writes without having to
   84662                 : ** rollback the whole transaction.  For operations where all constraints
   84663                 : ** can be checked before any changes are made to the database, it is never
   84664                 : ** necessary to undo a write and the checkpoint should not be set.
   84665                 : */
   84666           62070 : SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
   84667           62070 :   Parse *pToplevel = sqlite3ParseToplevel(pParse);
   84668           62070 :   sqlite3CodeVerifySchema(pParse, iDb);
   84669           62070 :   pToplevel->writeMask |= ((yDbMask)1)<<iDb;
   84670           62070 :   pToplevel->isMultiWrite |= setStatement;
   84671           62070 : }
   84672                 : 
   84673                 : /*
   84674                 : ** Indicate that the statement currently under construction might write
   84675                 : ** more than one entry (example: deleting one row then inserting another,
   84676                 : ** inserting multiple rows in a table, or inserting a row and index entries.)
   84677                 : ** If an abort occurs after some of these writes have completed, then it will
   84678                 : ** be necessary to undo the completed writes.
   84679                 : */
   84680             670 : SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
   84681             670 :   Parse *pToplevel = sqlite3ParseToplevel(pParse);
   84682             670 :   pToplevel->isMultiWrite = 1;
   84683             670 : }
   84684                 : 
   84685                 : /* 
   84686                 : ** The code generator calls this routine if is discovers that it is
   84687                 : ** possible to abort a statement prior to completion.  In order to 
   84688                 : ** perform this abort without corrupting the database, we need to make
   84689                 : ** sure that the statement is protected by a statement transaction.
   84690                 : **
   84691                 : ** Technically, we only need to set the mayAbort flag if the
   84692                 : ** isMultiWrite flag was previously set.  There is a time dependency
   84693                 : ** such that the abort must occur after the multiwrite.  This makes
   84694                 : ** some statements involving the REPLACE conflict resolution algorithm
   84695                 : ** go a little faster.  But taking advantage of this time dependency
   84696                 : ** makes it more difficult to prove that the code is correct (in 
   84697                 : ** particular, it prevents us from writing an effective
   84698                 : ** implementation of sqlite3AssertMayAbort()) and so we have chosen
   84699                 : ** to take the safe route and skip the optimization.
   84700                 : */
   84701           18410 : SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
   84702           18410 :   Parse *pToplevel = sqlite3ParseToplevel(pParse);
   84703           18410 :   pToplevel->mayAbort = 1;
   84704           18410 : }
   84705                 : 
   84706                 : /*
   84707                 : ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
   84708                 : ** error. The onError parameter determines which (if any) of the statement
   84709                 : ** and/or current transaction is rolled back.
   84710                 : */
   84711            9164 : SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
   84712            9164 :   Vdbe *v = sqlite3GetVdbe(pParse);
   84713            9164 :   if( onError==OE_Abort ){
   84714            9164 :     sqlite3MayAbort(pParse);
   84715                 :   }
   84716            9164 :   sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
   84717            9164 : }
   84718                 : 
   84719                 : /*
   84720                 : ** Check to see if pIndex uses the collating sequence pColl.  Return
   84721                 : ** true if it does and false if it does not.
   84722                 : */
   84723                 : #ifndef SQLITE_OMIT_REINDEX
   84724               0 : static int collationMatch(const char *zColl, Index *pIndex){
   84725                 :   int i;
   84726               0 :   assert( zColl!=0 );
   84727               0 :   for(i=0; i<pIndex->nColumn; i++){
   84728               0 :     const char *z = pIndex->azColl[i];
   84729               0 :     assert( z!=0 );
   84730               0 :     if( 0==sqlite3StrICmp(z, zColl) ){
   84731               0 :       return 1;
   84732                 :     }
   84733                 :   }
   84734               0 :   return 0;
   84735                 : }
   84736                 : #endif
   84737                 : 
   84738                 : /*
   84739                 : ** Recompute all indices of pTab that use the collating sequence pColl.
   84740                 : ** If pColl==0 then recompute all indices of pTab.
   84741                 : */
   84742                 : #ifndef SQLITE_OMIT_REINDEX
   84743              30 : static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
   84744                 :   Index *pIndex;              /* An index associated with pTab */
   84745                 : 
   84746              74 :   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
   84747              44 :     if( zColl==0 || collationMatch(zColl, pIndex) ){
   84748              44 :       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   84749              44 :       sqlite3BeginWriteOperation(pParse, 0, iDb);
   84750              44 :       sqlite3RefillIndex(pParse, pIndex, -1);
   84751                 :     }
   84752                 :   }
   84753              30 : }
   84754                 : #endif
   84755                 : 
   84756                 : /*
   84757                 : ** Recompute all indices of all tables in all databases where the
   84758                 : ** indices use the collating sequence pColl.  If pColl==0 then recompute
   84759                 : ** all indices everywhere.
   84760                 : */
   84761                 : #ifndef SQLITE_OMIT_REINDEX
   84762               2 : static void reindexDatabases(Parse *pParse, char const *zColl){
   84763                 :   Db *pDb;                    /* A single database */
   84764                 :   int iDb;                    /* The database index number */
   84765               2 :   sqlite3 *db = pParse->db;   /* The database connection */
   84766                 :   HashElem *k;                /* For looping over tables in pDb */
   84767                 :   Table *pTab;                /* A table in the database */
   84768                 : 
   84769               2 :   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
   84770               6 :   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
   84771               4 :     assert( pDb!=0 );
   84772              34 :     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
   84773              30 :       pTab = (Table*)sqliteHashData(k);
   84774              30 :       reindexTable(pParse, pTab, zColl);
   84775                 :     }
   84776                 :   }
   84777               2 : }
   84778                 : #endif
   84779                 : 
   84780                 : /*
   84781                 : ** Generate code for the REINDEX command.
   84782                 : **
   84783                 : **        REINDEX                            -- 1
   84784                 : **        REINDEX  <collation>               -- 2
   84785                 : **        REINDEX  ?<database>.?<tablename>  -- 3
   84786                 : **        REINDEX  ?<database>.?<indexname>  -- 4
   84787                 : **
   84788                 : ** Form 1 causes all indices in all attached databases to be rebuilt.
   84789                 : ** Form 2 rebuilds all indices in all databases that use the named
   84790                 : ** collating function.  Forms 3 and 4 rebuild the named index or all
   84791                 : ** indices associated with the named table.
   84792                 : */
   84793                 : #ifndef SQLITE_OMIT_REINDEX
   84794               2 : SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
   84795                 :   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
   84796                 :   char *z;                    /* Name of a table or index */
   84797                 :   const char *zDb;            /* Name of the database */
   84798                 :   Table *pTab;                /* A table in the database */
   84799                 :   Index *pIndex;              /* An index associated with pTab */
   84800                 :   int iDb;                    /* The database index number */
   84801               2 :   sqlite3 *db = pParse->db;   /* The database connection */
   84802                 :   Token *pObjName;            /* Name of the table or index to be reindexed */
   84803                 : 
   84804                 :   /* Read the database schema. If an error occurs, leave an error message
   84805                 :   ** and code in pParse and return NULL. */
   84806               2 :   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   84807               0 :     return;
   84808                 :   }
   84809                 : 
   84810               2 :   if( pName1==0 ){
   84811               2 :     reindexDatabases(pParse, 0);
   84812               2 :     return;
   84813               0 :   }else if( NEVER(pName2==0) || pName2->z==0 ){
   84814                 :     char *zColl;
   84815               0 :     assert( pName1->z );
   84816               0 :     zColl = sqlite3NameFromToken(pParse->db, pName1);
   84817               0 :     if( !zColl ) return;
   84818               0 :     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
   84819               0 :     if( pColl ){
   84820               0 :       reindexDatabases(pParse, zColl);
   84821               0 :       sqlite3DbFree(db, zColl);
   84822               0 :       return;
   84823                 :     }
   84824               0 :     sqlite3DbFree(db, zColl);
   84825                 :   }
   84826               0 :   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
   84827               0 :   if( iDb<0 ) return;
   84828               0 :   z = sqlite3NameFromToken(db, pObjName);
   84829               0 :   if( z==0 ) return;
   84830               0 :   zDb = db->aDb[iDb].zName;
   84831               0 :   pTab = sqlite3FindTable(db, z, zDb);
   84832               0 :   if( pTab ){
   84833               0 :     reindexTable(pParse, pTab, 0);
   84834               0 :     sqlite3DbFree(db, z);
   84835               0 :     return;
   84836                 :   }
   84837               0 :   pIndex = sqlite3FindIndex(db, z, zDb);
   84838               0 :   sqlite3DbFree(db, z);
   84839               0 :   if( pIndex ){
   84840               0 :     sqlite3BeginWriteOperation(pParse, 0, iDb);
   84841               0 :     sqlite3RefillIndex(pParse, pIndex, -1);
   84842               0 :     return;
   84843                 :   }
   84844               0 :   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
   84845                 : }
   84846                 : #endif
   84847                 : 
   84848                 : /*
   84849                 : ** Return a dynamicly allocated KeyInfo structure that can be used
   84850                 : ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
   84851                 : **
   84852                 : ** If successful, a pointer to the new structure is returned. In this case
   84853                 : ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned 
   84854                 : ** pointer. If an error occurs (out of memory or missing collation 
   84855                 : ** sequence), NULL is returned and the state of pParse updated to reflect
   84856                 : ** the error.
   84857                 : */
   84858           64136 : SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
   84859                 :   int i;
   84860           64136 :   int nCol = pIdx->nColumn;
   84861           64136 :   int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
   84862           64136 :   sqlite3 *db = pParse->db;
   84863           64136 :   KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
   84864                 : 
   84865           64136 :   if( pKey ){
   84866           64136 :     pKey->db = pParse->db;
   84867           64136 :     pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
   84868           64136 :     assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
   84869          165373 :     for(i=0; i<nCol; i++){
   84870          101237 :       char *zColl = pIdx->azColl[i];
   84871          101237 :       assert( zColl );
   84872          101237 :       pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
   84873          101237 :       pKey->aSortOrder[i] = pIdx->aSortOrder[i];
   84874                 :     }
   84875           64136 :     pKey->nField = (u16)nCol;
   84876                 :   }
   84877                 : 
   84878           64136 :   if( pParse->nErr ){
   84879               0 :     sqlite3DbFree(db, pKey);
   84880               0 :     pKey = 0;
   84881                 :   }
   84882           64136 :   return pKey;
   84883                 : }
   84884                 : 
   84885                 : /************** End of build.c ***********************************************/
   84886                 : /************** Begin file callback.c ****************************************/
   84887                 : /*
   84888                 : ** 2005 May 23 
   84889                 : **
   84890                 : ** The author disclaims copyright to this source code.  In place of
   84891                 : ** a legal notice, here is a blessing:
   84892                 : **
   84893                 : **    May you do good and not evil.
   84894                 : **    May you find forgiveness for yourself and forgive others.
   84895                 : **    May you share freely, never taking more than you give.
   84896                 : **
   84897                 : *************************************************************************
   84898                 : **
   84899                 : ** This file contains functions used to access the internal hash tables
   84900                 : ** of user defined functions and collation sequences.
   84901                 : */
   84902                 : 
   84903                 : 
   84904                 : /*
   84905                 : ** Invoke the 'collation needed' callback to request a collation sequence
   84906                 : ** in the encoding enc of name zName, length nName.
   84907                 : */
   84908               0 : static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
   84909               0 :   assert( !db->xCollNeeded || !db->xCollNeeded16 );
   84910               0 :   if( db->xCollNeeded ){
   84911               0 :     char *zExternal = sqlite3DbStrDup(db, zName);
   84912               0 :     if( !zExternal ) return;
   84913               0 :     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
   84914               0 :     sqlite3DbFree(db, zExternal);
   84915                 :   }
   84916                 : #ifndef SQLITE_OMIT_UTF16
   84917               0 :   if( db->xCollNeeded16 ){
   84918                 :     char const *zExternal;
   84919               0 :     sqlite3_value *pTmp = sqlite3ValueNew(db);
   84920               0 :     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
   84921               0 :     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
   84922               0 :     if( zExternal ){
   84923               0 :       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
   84924                 :     }
   84925               0 :     sqlite3ValueFree(pTmp);
   84926                 :   }
   84927                 : #endif
   84928                 : }
   84929                 : 
   84930                 : /*
   84931                 : ** This routine is called if the collation factory fails to deliver a
   84932                 : ** collation function in the best encoding but there may be other versions
   84933                 : ** of this collation function (for other text encodings) available. Use one
   84934                 : ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
   84935                 : ** possible.
   84936                 : */
   84937               0 : static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
   84938                 :   CollSeq *pColl2;
   84939               0 :   char *z = pColl->zName;
   84940                 :   int i;
   84941                 :   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
   84942               0 :   for(i=0; i<3; i++){
   84943               0 :     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
   84944               0 :     if( pColl2->xCmp!=0 ){
   84945               0 :       memcpy(pColl, pColl2, sizeof(CollSeq));
   84946               0 :       pColl->xDel = 0;         /* Do not copy the destructor */
   84947               0 :       return SQLITE_OK;
   84948                 :     }
   84949                 :   }
   84950               0 :   return SQLITE_ERROR;
   84951                 : }
   84952                 : 
   84953                 : /*
   84954                 : ** This function is responsible for invoking the collation factory callback
   84955                 : ** or substituting a collation sequence of a different encoding when the
   84956                 : ** requested collation sequence is not available in the desired encoding.
   84957                 : ** 
   84958                 : ** If it is not NULL, then pColl must point to the database native encoding 
   84959                 : ** collation sequence with name zName, length nName.
   84960                 : **
   84961                 : ** The return value is either the collation sequence to be used in database
   84962                 : ** db for collation type name zName, length nName, or NULL, if no collation
   84963                 : ** sequence can be found.
   84964                 : **
   84965                 : ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
   84966                 : */
   84967          136320 : SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
   84968                 :   sqlite3* db,          /* The database connection */
   84969                 :   u8 enc,               /* The desired encoding for the collating sequence */
   84970                 :   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
   84971                 :   const char *zName     /* Collating sequence name */
   84972                 : ){
   84973                 :   CollSeq *p;
   84974                 : 
   84975          136320 :   p = pColl;
   84976          136320 :   if( !p ){
   84977               0 :     p = sqlite3FindCollSeq(db, enc, zName, 0);
   84978                 :   }
   84979          136320 :   if( !p || !p->xCmp ){
   84980                 :     /* No collation sequence of this type for this encoding is registered.
   84981                 :     ** Call the collation factory to see if it can supply us with one.
   84982                 :     */
   84983               0 :     callCollNeeded(db, enc, zName);
   84984               0 :     p = sqlite3FindCollSeq(db, enc, zName, 0);
   84985                 :   }
   84986          136320 :   if( p && !p->xCmp && synthCollSeq(db, p) ){
   84987               0 :     p = 0;
   84988                 :   }
   84989          136320 :   assert( !p || p->xCmp );
   84990          136320 :   return p;
   84991                 : }
   84992                 : 
   84993                 : /*
   84994                 : ** This routine is called on a collation sequence before it is used to
   84995                 : ** check that it is defined. An undefined collation sequence exists when
   84996                 : ** a database is loaded that contains references to collation sequences
   84997                 : ** that have not been defined by sqlite3_create_collation() etc.
   84998                 : **
   84999                 : ** If required, this routine calls the 'collation needed' callback to
   85000                 : ** request a definition of the collating sequence. If this doesn't work, 
   85001                 : ** an equivalent collating sequence that uses a text encoding different
   85002                 : ** from the main database is substituted, if one is available.
   85003                 : */
   85004          194127 : SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
   85005          194127 :   if( pColl ){
   85006          136320 :     const char *zName = pColl->zName;
   85007          136320 :     sqlite3 *db = pParse->db;
   85008          136320 :     CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName);
   85009          136320 :     if( !p ){
   85010               0 :       sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
   85011               0 :       pParse->nErr++;
   85012               0 :       return SQLITE_ERROR;
   85013                 :     }
   85014          136320 :     assert( p==pColl );
   85015                 :   }
   85016          194127 :   return SQLITE_OK;
   85017                 : }
   85018                 : 
   85019                 : 
   85020                 : 
   85021                 : /*
   85022                 : ** Locate and return an entry from the db.aCollSeq hash table. If the entry
   85023                 : ** specified by zName and nName is not found and parameter 'create' is
   85024                 : ** true, then create a new entry. Otherwise return NULL.
   85025                 : **
   85026                 : ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
   85027                 : ** array of three CollSeq structures. The first is the collation sequence
   85028                 : ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
   85029                 : **
   85030                 : ** Stored immediately after the three collation sequences is a copy of
   85031                 : ** the collation sequence name. A pointer to this string is stored in
   85032                 : ** each collation sequence structure.
   85033                 : */
   85034          212512 : static CollSeq *findCollSeqEntry(
   85035                 :   sqlite3 *db,          /* Database connection */
   85036                 :   const char *zName,    /* Name of the collating sequence */
   85037                 :   int create            /* Create a new entry if true */
   85038                 : ){
   85039                 :   CollSeq *pColl;
   85040          212512 :   int nName = sqlite3Strlen30(zName);
   85041          212512 :   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
   85042                 : 
   85043          212512 :   if( 0==pColl && create ){
   85044           22911 :     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
   85045           22911 :     if( pColl ){
   85046           22911 :       CollSeq *pDel = 0;
   85047           22911 :       pColl[0].zName = (char*)&pColl[3];
   85048           22911 :       pColl[0].enc = SQLITE_UTF8;
   85049           22911 :       pColl[1].zName = (char*)&pColl[3];
   85050           22911 :       pColl[1].enc = SQLITE_UTF16LE;
   85051           22911 :       pColl[2].zName = (char*)&pColl[3];
   85052           22911 :       pColl[2].enc = SQLITE_UTF16BE;
   85053           22911 :       memcpy(pColl[0].zName, zName, nName);
   85054           22911 :       pColl[0].zName[nName] = 0;
   85055           22911 :       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
   85056                 : 
   85057                 :       /* If a malloc() failure occurred in sqlite3HashInsert(), it will 
   85058                 :       ** return the pColl pointer to be deleted (because it wasn't added
   85059                 :       ** to the hash table).
   85060                 :       */
   85061           22911 :       assert( pDel==0 || pDel==pColl );
   85062           22911 :       if( pDel!=0 ){
   85063               0 :         db->mallocFailed = 1;
   85064               0 :         sqlite3DbFree(db, pDel);
   85065               0 :         pColl = 0;
   85066                 :       }
   85067                 :     }
   85068                 :   }
   85069          212512 :   return pColl;
   85070                 : }
   85071                 : 
   85072                 : /*
   85073                 : ** Parameter zName points to a UTF-8 encoded string nName bytes long.
   85074                 : ** Return the CollSeq* pointer for the collation sequence named zName
   85075                 : ** for the encoding 'enc' from the database 'db'.
   85076                 : **
   85077                 : ** If the entry specified is not found and 'create' is true, then create a
   85078                 : ** new entry.  Otherwise return NULL.
   85079                 : **
   85080                 : ** A separate function sqlite3LocateCollSeq() is a wrapper around
   85081                 : ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
   85082                 : ** if necessary and generates an error message if the collating sequence
   85083                 : ** cannot be found.
   85084                 : **
   85085                 : ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
   85086                 : */
   85087          298957 : SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
   85088                 :   sqlite3 *db,
   85089                 :   u8 enc,
   85090                 :   const char *zName,
   85091                 :   int create
   85092                 : ){
   85093                 :   CollSeq *pColl;
   85094          298957 :   if( zName ){
   85095          212512 :     pColl = findCollSeqEntry(db, zName, create);
   85096                 :   }else{
   85097           86445 :     pColl = db->pDfltColl;
   85098                 :   }
   85099                 :   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
   85100          298957 :   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
   85101          298957 :   if( pColl ) pColl += enc-1;
   85102          298957 :   return pColl;
   85103                 : }
   85104                 : 
   85105                 : /* During the search for the best function definition, this procedure
   85106                 : ** is called to test how well the function passed as the first argument
   85107                 : ** matches the request for a function with nArg arguments in a system
   85108                 : ** that uses encoding enc. The value returned indicates how well the
   85109                 : ** request is matched. A higher value indicates a better match.
   85110                 : **
   85111                 : ** The returned value is always between 0 and 6, as follows:
   85112                 : **
   85113                 : ** 0: Not a match, or if nArg<0 and the function is has no implementation.
   85114                 : ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
   85115                 : **    encoding is requested, or vice versa.
   85116                 : ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
   85117                 : **    requested, or vice versa.
   85118                 : ** 3: A variable arguments function using the same text encoding.
   85119                 : ** 4: A function with the exact number of arguments requested that
   85120                 : **    prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
   85121                 : ** 5: A function with the exact number of arguments requested that
   85122                 : **    prefers UTF-16LE when UTF-16BE is requested, or vice versa.
   85123                 : ** 6: An exact match.
   85124                 : **
   85125                 : */
   85126          299636 : static int matchQuality(FuncDef *p, int nArg, u8 enc){
   85127          299636 :   int match = 0;
   85128          299636 :   if( p->nArg==-1 || p->nArg==nArg 
   85129          125572 :    || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0))
   85130                 :   ){
   85131          174064 :     match = 1;
   85132          174064 :     if( p->nArg==nArg || nArg==-1 ){
   85133          165374 :       match = 4;
   85134                 :     }
   85135          174064 :     if( enc==p->iPrefEnc ){
   85136           45099 :       match += 2;
   85137                 :     }
   85138          128965 :     else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
   85139           35932 :              (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
   85140           17970 :       match += 1;
   85141                 :     }
   85142                 :   }
   85143          299636 :   return match;
   85144                 : }
   85145                 : 
   85146                 : /*
   85147                 : ** Search a FuncDefHash for a function with the given name.  Return
   85148                 : ** a pointer to the matching FuncDef if found, or 0 if there is no match.
   85149                 : */
   85150          440765 : static FuncDef *functionSearch(
   85151                 :   FuncDefHash *pHash,  /* Hash table to search */
   85152                 :   int h,               /* Hash of the name */
   85153                 :   const char *zFunc,   /* Name of function */
   85154                 :   int nFunc            /* Number of bytes in zFunc */
   85155                 : ){
   85156                 :   FuncDef *p;
   85157          699744 :   for(p=pHash->a[h]; p; p=p->pHash){
   85158          462300 :     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
   85159          203321 :       return p;
   85160                 :     }
   85161                 :   }
   85162          237444 :   return 0;
   85163                 : }
   85164                 : 
   85165                 : /*
   85166                 : ** Insert a new FuncDef into a FuncDefHash hash table.
   85167                 : */
   85168          130021 : SQLITE_PRIVATE void sqlite3FuncDefInsert(
   85169                 :   FuncDefHash *pHash,  /* The hash table into which to insert */
   85170                 :   FuncDef *pDef        /* The function definition to insert */
   85171                 : ){
   85172                 :   FuncDef *pOther;
   85173          130021 :   int nName = sqlite3Strlen30(pDef->zName);
   85174          130021 :   u8 c1 = (u8)pDef->zName[0];
   85175          130021 :   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
   85176          130021 :   pOther = functionSearch(pHash, h, pDef->zName, nName);
   85177          130021 :   if( pOther ){
   85178           56561 :     assert( pOther!=pDef && pOther->pNext!=pDef );
   85179           56561 :     pDef->pNext = pOther->pNext;
   85180           56561 :     pOther->pNext = pDef;
   85181                 :   }else{
   85182           73460 :     pDef->pNext = 0;
   85183           73460 :     pDef->pHash = pHash->a[h];
   85184           73460 :     pHash->a[h] = pDef;
   85185                 :   }
   85186          130021 : }
   85187                 :   
   85188                 :   
   85189                 : 
   85190                 : /*
   85191                 : ** Locate a user function given a name, a number of arguments and a flag
   85192                 : ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
   85193                 : ** pointer to the FuncDef structure that defines that function, or return
   85194                 : ** NULL if the function does not exist.
   85195                 : **
   85196                 : ** If the createFlag argument is true, then a new (blank) FuncDef
   85197                 : ** structure is created and liked into the "db" structure if a
   85198                 : ** no matching function previously existed.  When createFlag is true
   85199                 : ** and the nArg parameter is -1, then only a function that accepts
   85200                 : ** any number of arguments will be returned.
   85201                 : **
   85202                 : ** If createFlag is false and nArg is -1, then the first valid
   85203                 : ** function found is returned.  A function is valid if either xFunc
   85204                 : ** or xStep is non-zero.
   85205                 : **
   85206                 : ** If createFlag is false, then a function with the required name and
   85207                 : ** number of arguments may be returned even if the eTextRep flag does not
   85208                 : ** match that requested.
   85209                 : */
   85210          219585 : SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
   85211                 :   sqlite3 *db,       /* An open database */
   85212                 :   const char *zName, /* Name of the function.  Not null-terminated */
   85213                 :   int nName,         /* Number of characters in the name */
   85214                 :   int nArg,          /* Number of arguments.  -1 means any number */
   85215                 :   u8 enc,            /* Preferred text encoding */
   85216                 :   int createFlag     /* Create new entry if true and does not otherwise exist */
   85217                 : ){
   85218                 :   FuncDef *p;         /* Iterator variable */
   85219          219585 :   FuncDef *pBest = 0; /* Best match found so far */
   85220          219585 :   int bestScore = 0;  /* Score of best match */
   85221                 :   int h;              /* Hash value */
   85222                 : 
   85223                 : 
   85224          219585 :   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
   85225          219585 :   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
   85226                 : 
   85227                 :   /* First search for a match amongst the application-defined functions.
   85228                 :   */
   85229          219585 :   p = functionSearch(&db->aFunc, h, zName, nName);
   85230          672194 :   while( p ){
   85231          233024 :     int score = matchQuality(p, nArg, enc);
   85232          233024 :     if( score>bestScore ){
   85233           94405 :       pBest = p;
   85234           94405 :       bestScore = score;
   85235                 :     }
   85236          233024 :     p = p->pNext;
   85237                 :   }
   85238                 : 
   85239                 :   /* If no match is found, search the built-in functions.
   85240                 :   **
   85241                 :   ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
   85242                 :   ** functions even if a prior app-defined function was found.  And give
   85243                 :   ** priority to built-in functions.
   85244                 :   **
   85245                 :   ** Except, if createFlag is true, that means that we are trying to
   85246                 :   ** install a new function.  Whatever FuncDef structure is returned it will
   85247                 :   ** have fields overwritten with new information appropriate for the
   85248                 :   ** new function.  But the FuncDefs for built-in functions are read-only.
   85249                 :   ** So we must not search for built-ins when creating a new function.
   85250                 :   */ 
   85251          219585 :   if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
   85252           91159 :     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
   85253           91159 :     bestScore = 0;
   85254           91159 :     p = functionSearch(pHash, h, zName, nName);
   85255          248930 :     while( p ){
   85256           66612 :       int score = matchQuality(p, nArg, enc);
   85257           66612 :       if( score>bestScore ){
   85258           43229 :         pBest = p;
   85259           43229 :         bestScore = score;
   85260                 :       }
   85261           66612 :       p = p->pNext;
   85262                 :     }
   85263                 :   }
   85264                 : 
   85265                 :   /* If the createFlag parameter is true and the search did not reveal an
   85266                 :   ** exact match for the name, number of arguments and encoding, then add a
   85267                 :   ** new entry to the hash table and return it.
   85268                 :   */
   85269          298828 :   if( createFlag && (bestScore<6 || pBest->nArg!=nArg) && 
   85270           79243 :       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
   85271           79243 :     pBest->zName = (char *)&pBest[1];
   85272           79243 :     pBest->nArg = (u16)nArg;
   85273           79243 :     pBest->iPrefEnc = enc;
   85274           79243 :     memcpy(pBest->zName, zName, nName);
   85275           79243 :     pBest->zName[nName] = 0;
   85276           79243 :     sqlite3FuncDefInsert(&db->aFunc, pBest);
   85277                 :   }
   85278                 : 
   85279          219585 :   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
   85280          167291 :     return pBest;
   85281                 :   }
   85282           52294 :   return 0;
   85283                 : }
   85284                 : 
   85285                 : /*
   85286                 : ** Free all resources held by the schema structure. The void* argument points
   85287                 : ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the 
   85288                 : ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
   85289                 : ** of the schema hash tables).
   85290                 : **
   85291                 : ** The Schema.cache_size variable is not cleared.
   85292                 : */
   85293           13540 : SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
   85294                 :   Hash temp1;
   85295                 :   Hash temp2;
   85296                 :   HashElem *pElem;
   85297           13540 :   Schema *pSchema = (Schema *)p;
   85298                 : 
   85299           13540 :   temp1 = pSchema->tblHash;
   85300           13540 :   temp2 = pSchema->trigHash;
   85301           13540 :   sqlite3HashInit(&pSchema->trigHash);
   85302           13540 :   sqlite3HashClear(&pSchema->idxHash);
   85303           22279 :   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
   85304            8739 :     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
   85305                 :   }
   85306           13540 :   sqlite3HashClear(&temp2);
   85307           13540 :   sqlite3HashInit(&pSchema->tblHash);
   85308           41783 :   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
   85309           28243 :     Table *pTab = sqliteHashData(pElem);
   85310           28243 :     sqlite3DeleteTable(0, pTab);
   85311                 :   }
   85312           13540 :   sqlite3HashClear(&temp1);
   85313           13540 :   sqlite3HashClear(&pSchema->fkeyHash);
   85314           13540 :   pSchema->pSeqTab = 0;
   85315           13540 :   if( pSchema->flags & DB_SchemaLoaded ){
   85316            7038 :     pSchema->iGeneration++;
   85317            7038 :     pSchema->flags &= ~DB_SchemaLoaded;
   85318                 :   }
   85319           13540 : }
   85320                 : 
   85321                 : /*
   85322                 : ** Find and return the schema associated with a BTree.  Create
   85323                 : ** a new one if necessary.
   85324                 : */
   85325            6560 : SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
   85326                 :   Schema * p;
   85327            6560 :   if( pBt ){
   85328            3283 :     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
   85329                 :   }else{
   85330            3277 :     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
   85331                 :   }
   85332            6560 :   if( !p ){
   85333               0 :     db->mallocFailed = 1;
   85334            6560 :   }else if ( 0==p->file_format ){
   85335            6384 :     sqlite3HashInit(&p->tblHash);
   85336            6384 :     sqlite3HashInit(&p->idxHash);
   85337            6384 :     sqlite3HashInit(&p->trigHash);
   85338            6384 :     sqlite3HashInit(&p->fkeyHash);
   85339            6384 :     p->enc = SQLITE_UTF8;
   85340                 :   }
   85341            6560 :   return p;
   85342                 : }
   85343                 : 
   85344                 : /************** End of callback.c ********************************************/
   85345                 : /************** Begin file delete.c ******************************************/
   85346                 : /*
   85347                 : ** 2001 September 15
   85348                 : **
   85349                 : ** The author disclaims copyright to this source code.  In place of
   85350                 : ** a legal notice, here is a blessing:
   85351                 : **
   85352                 : **    May you do good and not evil.
   85353                 : **    May you find forgiveness for yourself and forgive others.
   85354                 : **    May you share freely, never taking more than you give.
   85355                 : **
   85356                 : *************************************************************************
   85357                 : ** This file contains C code routines that are called by the parser
   85358                 : ** in order to generate code for DELETE FROM statements.
   85359                 : */
   85360                 : 
   85361                 : /*
   85362                 : ** While a SrcList can in general represent multiple tables and subqueries
   85363                 : ** (as in the FROM clause of a SELECT statement) in this case it contains
   85364                 : ** the name of a single table, as one might find in an INSERT, DELETE,
   85365                 : ** or UPDATE statement.  Look up that table in the symbol table and
   85366                 : ** return a pointer.  Set an error message and return NULL if the table 
   85367                 : ** name is not found or if any other error occurs.
   85368                 : **
   85369                 : ** The following fields are initialized appropriate in pSrc:
   85370                 : **
   85371                 : **    pSrc->a[0].pTab       Pointer to the Table object
   85372                 : **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
   85373                 : **
   85374                 : */
   85375           70129 : SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
   85376           70129 :   struct SrcList_item *pItem = pSrc->a;
   85377                 :   Table *pTab;
   85378           70129 :   assert( pItem && pSrc->nSrc==1 );
   85379           70129 :   pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
   85380           70129 :   sqlite3DeleteTable(pParse->db, pItem->pTab);
   85381           70129 :   pItem->pTab = pTab;
   85382           70129 :   if( pTab ){
   85383           70129 :     pTab->nRef++;
   85384                 :   }
   85385           70129 :   if( sqlite3IndexedByLookup(pParse, pItem) ){
   85386               0 :     pTab = 0;
   85387                 :   }
   85388           70129 :   return pTab;
   85389                 : }
   85390                 : 
   85391                 : /*
   85392                 : ** Check to make sure the given table is writable.  If it is not
   85393                 : ** writable, generate an error message and return 1.  If it is
   85394                 : ** writable return 0;
   85395                 : */
   85396           41562 : SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
   85397                 :   /* A table is not writable under the following circumstances:
   85398                 :   **
   85399                 :   **   1) It is a virtual table and no implementation of the xUpdate method
   85400                 :   **      has been provided, or
   85401                 :   **   2) It is a system table (i.e. sqlite_master), this call is not
   85402                 :   **      part of a nested parse and writable_schema pragma has not 
   85403                 :   **      been specified.
   85404                 :   **
   85405                 :   ** In either case leave an error message in pParse and return non-zero.
   85406                 :   */
   85407           41562 :   if( ( IsVirtual(pTab) 
   85408               4 :      && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
   85409           41562 :    || ( (pTab->tabFlags & TF_Readonly)!=0
   85410           18903 :      && (pParse->db->flags & SQLITE_WriteSchema)==0
   85411           18849 :      && pParse->nested==0 )
   85412                 :   ){
   85413               0 :     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
   85414               0 :     return 1;
   85415                 :   }
   85416                 : 
   85417                 : #ifndef SQLITE_OMIT_VIEW
   85418           41562 :   if( !viewOk && pTab->pSelect ){
   85419               0 :     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
   85420               0 :     return 1;
   85421                 :   }
   85422                 : #endif
   85423           41562 :   return 0;
   85424                 : }
   85425                 : 
   85426                 : 
   85427                 : #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   85428                 : /*
   85429                 : ** Evaluate a view and store its result in an ephemeral table.  The
   85430                 : ** pWhere argument is an optional WHERE clause that restricts the
   85431                 : ** set of rows in the view that are to be added to the ephemeral table.
   85432                 : */
   85433              59 : SQLITE_PRIVATE void sqlite3MaterializeView(
   85434                 :   Parse *pParse,       /* Parsing context */
   85435                 :   Table *pView,        /* View definition */
   85436                 :   Expr *pWhere,        /* Optional WHERE clause to be added */
   85437                 :   int iCur             /* Cursor number for ephemerial table */
   85438                 : ){
   85439                 :   SelectDest dest;
   85440                 :   Select *pDup;
   85441              59 :   sqlite3 *db = pParse->db;
   85442                 : 
   85443              59 :   pDup = sqlite3SelectDup(db, pView->pSelect, 0);
   85444              59 :   if( pWhere ){
   85445                 :     SrcList *pFrom;
   85446                 :     
   85447              54 :     pWhere = sqlite3ExprDup(db, pWhere, 0);
   85448              54 :     pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
   85449              54 :     if( pFrom ){
   85450              54 :       assert( pFrom->nSrc==1 );
   85451              54 :       pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
   85452              54 :       pFrom->a[0].pSelect = pDup;
   85453              54 :       assert( pFrom->a[0].pOn==0 );
   85454              54 :       assert( pFrom->a[0].pUsing==0 );
   85455                 :     }else{
   85456               0 :       sqlite3SelectDelete(db, pDup);
   85457                 :     }
   85458              54 :     pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
   85459                 :   }
   85460              59 :   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
   85461              59 :   sqlite3Select(pParse, pDup, &dest);
   85462              59 :   sqlite3SelectDelete(db, pDup);
   85463              59 : }
   85464                 : #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
   85465                 : 
   85466                 : #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
   85467                 : /*
   85468                 : ** Generate an expression tree to implement the WHERE, ORDER BY,
   85469                 : ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
   85470                 : **
   85471                 : **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
   85472                 : **                            \__________________________/
   85473                 : **                               pLimitWhere (pInClause)
   85474                 : */
   85475                 : SQLITE_PRIVATE Expr *sqlite3LimitWhere(
   85476                 :   Parse *pParse,               /* The parser context */
   85477                 :   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
   85478                 :   Expr *pWhere,                /* The WHERE clause.  May be null */
   85479                 :   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
   85480                 :   Expr *pLimit,                /* The LIMIT clause.  May be null */
   85481                 :   Expr *pOffset,               /* The OFFSET clause.  May be null */
   85482                 :   char *zStmtType              /* Either DELETE or UPDATE.  For error messages. */
   85483                 : ){
   85484                 :   Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
   85485                 :   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
   85486                 :   Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
   85487                 :   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
   85488                 :   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
   85489                 :   Select *pSelect = NULL;      /* Complete SELECT tree */
   85490                 : 
   85491                 :   /* Check that there isn't an ORDER BY without a LIMIT clause.
   85492                 :   */
   85493                 :   if( pOrderBy && (pLimit == 0) ) {
   85494                 :     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
   85495                 :     goto limit_where_cleanup_2;
   85496                 :   }
   85497                 : 
   85498                 :   /* We only need to generate a select expression if there
   85499                 :   ** is a limit/offset term to enforce.
   85500                 :   */
   85501                 :   if( pLimit == 0 ) {
   85502                 :     /* if pLimit is null, pOffset will always be null as well. */
   85503                 :     assert( pOffset == 0 );
   85504                 :     return pWhere;
   85505                 :   }
   85506                 : 
   85507                 :   /* Generate a select expression tree to enforce the limit/offset 
   85508                 :   ** term for the DELETE or UPDATE statement.  For example:
   85509                 :   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
   85510                 :   ** becomes:
   85511                 :   **   DELETE FROM table_a WHERE rowid IN ( 
   85512                 :   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
   85513                 :   **   );
   85514                 :   */
   85515                 : 
   85516                 :   pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
   85517                 :   if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
   85518                 :   pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
   85519                 :   if( pEList == 0 ) goto limit_where_cleanup_2;
   85520                 : 
   85521                 :   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
   85522                 :   ** and the SELECT subtree. */
   85523                 :   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
   85524                 :   if( pSelectSrc == 0 ) {
   85525                 :     sqlite3ExprListDelete(pParse->db, pEList);
   85526                 :     goto limit_where_cleanup_2;
   85527                 :   }
   85528                 : 
   85529                 :   /* generate the SELECT expression tree. */
   85530                 :   pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
   85531                 :                              pOrderBy,0,pLimit,pOffset);
   85532                 :   if( pSelect == 0 ) return 0;
   85533                 : 
   85534                 :   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
   85535                 :   pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
   85536                 :   if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
   85537                 :   pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
   85538                 :   if( pInClause == 0 ) goto limit_where_cleanup_1;
   85539                 : 
   85540                 :   pInClause->x.pSelect = pSelect;
   85541                 :   pInClause->flags |= EP_xIsSelect;
   85542                 :   sqlite3ExprSetHeight(pParse, pInClause);
   85543                 :   return pInClause;
   85544                 : 
   85545                 :   /* something went wrong. clean up anything allocated. */
   85546                 : limit_where_cleanup_1:
   85547                 :   sqlite3SelectDelete(pParse->db, pSelect);
   85548                 :   return 0;
   85549                 : 
   85550                 : limit_where_cleanup_2:
   85551                 :   sqlite3ExprDelete(pParse->db, pWhere);
   85552                 :   sqlite3ExprListDelete(pParse->db, pOrderBy);
   85553                 :   sqlite3ExprDelete(pParse->db, pLimit);
   85554                 :   sqlite3ExprDelete(pParse->db, pOffset);
   85555                 :   return 0;
   85556                 : }
   85557                 : #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
   85558                 : 
   85559                 : /*
   85560                 : ** Generate code for a DELETE FROM statement.
   85561                 : **
   85562                 : **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
   85563                 : **                 \________/       \________________/
   85564                 : **                  pTabList              pWhere
   85565                 : */
   85566            8130 : SQLITE_PRIVATE void sqlite3DeleteFrom(
   85567                 :   Parse *pParse,         /* The parser context */
   85568                 :   SrcList *pTabList,     /* The table from which we should delete things */
   85569                 :   Expr *pWhere           /* The WHERE clause.  May be null */
   85570                 : ){
   85571                 :   Vdbe *v;               /* The virtual database engine */
   85572                 :   Table *pTab;           /* The table from which records will be deleted */
   85573                 :   const char *zDb;       /* Name of database holding pTab */
   85574            8130 :   int end, addr = 0;     /* A couple addresses of generated code */
   85575                 :   int i;                 /* Loop counter */
   85576                 :   WhereInfo *pWInfo;     /* Information about the WHERE clause */
   85577                 :   Index *pIdx;           /* For looping over indices of the table */
   85578                 :   int iCur;              /* VDBE Cursor number for pTab */
   85579                 :   sqlite3 *db;           /* Main database structure */
   85580                 :   AuthContext sContext;  /* Authorization context */
   85581                 :   NameContext sNC;       /* Name context to resolve expressions in */
   85582                 :   int iDb;               /* Database number */
   85583            8130 :   int memCnt = -1;       /* Memory cell used for change counting */
   85584                 :   int rcauth;            /* Value returned by authorization callback */
   85585                 : 
   85586                 : #ifndef SQLITE_OMIT_TRIGGER
   85587                 :   int isView;                  /* True if attempting to delete from a view */
   85588                 :   Trigger *pTrigger;           /* List of table triggers, if required */
   85589                 : #endif
   85590                 : 
   85591            8130 :   memset(&sContext, 0, sizeof(sContext));
   85592            8130 :   db = pParse->db;
   85593            8130 :   if( pParse->nErr || db->mallocFailed ){
   85594                 :     goto delete_from_cleanup;
   85595                 :   }
   85596            8130 :   assert( pTabList->nSrc==1 );
   85597                 : 
   85598                 :   /* Locate the table which we want to delete.  This table has to be
   85599                 :   ** put in an SrcList structure because some of the subroutines we
   85600                 :   ** will be calling are designed to work with multiple tables and expect
   85601                 :   ** an SrcList* parameter instead of just a Table* parameter.
   85602                 :   */
   85603            8130 :   pTab = sqlite3SrcListLookup(pParse, pTabList);
   85604            8130 :   if( pTab==0 )  goto delete_from_cleanup;
   85605                 : 
   85606                 :   /* Figure out if we have any triggers and if the table being
   85607                 :   ** deleted from is a view
   85608                 :   */
   85609                 : #ifndef SQLITE_OMIT_TRIGGER
   85610            8130 :   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
   85611            8130 :   isView = pTab->pSelect!=0;
   85612                 : #else
   85613                 : # define pTrigger 0
   85614                 : # define isView 0
   85615                 : #endif
   85616                 : #ifdef SQLITE_OMIT_VIEW
   85617                 : # undef isView
   85618                 : # define isView 0
   85619                 : #endif
   85620                 : 
   85621                 :   /* If pTab is really a view, make sure it has been initialized.
   85622                 :   */
   85623            8130 :   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
   85624               0 :     goto delete_from_cleanup;
   85625                 :   }
   85626                 : 
   85627            8130 :   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
   85628               0 :     goto delete_from_cleanup;
   85629                 :   }
   85630            8130 :   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   85631            8130 :   assert( iDb<db->nDb );
   85632            8130 :   zDb = db->aDb[iDb].zName;
   85633            8130 :   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
   85634            8130 :   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
   85635            8130 :   if( rcauth==SQLITE_DENY ){
   85636               0 :     goto delete_from_cleanup;
   85637                 :   }
   85638            8130 :   assert(!isView || pTrigger);
   85639                 : 
   85640                 :   /* Assign  cursor number to the table and all its indices.
   85641                 :   */
   85642            8130 :   assert( pTabList->nSrc==1 );
   85643            8130 :   iCur = pTabList->a[0].iCursor = pParse->nTab++;
   85644           17636 :   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   85645            9506 :     pParse->nTab++;
   85646                 :   }
   85647                 : 
   85648                 :   /* Start the view context
   85649                 :   */
   85650            8130 :   if( isView ){
   85651              59 :     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
   85652                 :   }
   85653                 : 
   85654                 :   /* Begin generating code.
   85655                 :   */
   85656            8130 :   v = sqlite3GetVdbe(pParse);
   85657            8130 :   if( v==0 ){
   85658               0 :     goto delete_from_cleanup;
   85659                 :   }
   85660            8130 :   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
   85661            8130 :   sqlite3BeginWriteOperation(pParse, 1, iDb);
   85662                 : 
   85663                 :   /* If we are trying to delete from a view, realize that view into
   85664                 :   ** a ephemeral table.
   85665                 :   */
   85666                 : #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
   85667            8130 :   if( isView ){
   85668              59 :     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
   85669                 :   }
   85670                 : #endif
   85671                 : 
   85672                 :   /* Resolve the column names in the WHERE clause.
   85673                 :   */
   85674            8130 :   memset(&sNC, 0, sizeof(sNC));
   85675            8130 :   sNC.pParse = pParse;
   85676            8130 :   sNC.pSrcList = pTabList;
   85677            8130 :   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
   85678               4 :     goto delete_from_cleanup;
   85679                 :   }
   85680                 : 
   85681                 :   /* Initialize the counter of the number of rows deleted, if
   85682                 :   ** we are counting rows.
   85683                 :   */
   85684            8126 :   if( db->flags & SQLITE_CountRows ){
   85685               0 :     memCnt = ++pParse->nMem;
   85686               0 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
   85687                 :   }
   85688                 : 
   85689                 : #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
   85690                 :   /* Special case: A DELETE without a WHERE clause deletes everything.
   85691                 :   ** It is easier just to erase the whole table. Prior to version 3.6.5,
   85692                 :   ** this optimization caused the row change count (the value returned by 
   85693                 :   ** API function sqlite3_count_changes) to be set incorrectly.  */
   85694            8364 :   if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) 
   85695             238 :    && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
   85696                 :   ){
   85697             238 :     assert( !isView );
   85698             238 :     sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
   85699             238 :                       pTab->zName, P4_STATIC);
   85700             491 :     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   85701             253 :       assert( pIdx->pSchema==pTab->pSchema );
   85702             253 :       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
   85703                 :     }
   85704                 :   }else
   85705                 : #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
   85706                 :   /* The usual case: There is a WHERE clause so we have to scan through
   85707                 :   ** the table and pick which records to delete.
   85708                 :   */
   85709                 :   {
   85710            7888 :     int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
   85711            7888 :     int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
   85712                 :     int regRowid;                   /* Actual register containing rowids */
   85713                 : 
   85714                 :     /* Collect rowids of every row to be deleted.
   85715                 :     */
   85716            7888 :     sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
   85717            7888 :     pWInfo = sqlite3WhereBegin(
   85718                 :         pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK
   85719                 :     );
   85720            7888 :     if( pWInfo==0 ) goto delete_from_cleanup;
   85721            7888 :     regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid);
   85722            7888 :     sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
   85723            7888 :     if( db->flags & SQLITE_CountRows ){
   85724               0 :       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
   85725                 :     }
   85726            7888 :     sqlite3WhereEnd(pWInfo);
   85727                 : 
   85728                 :     /* Delete every item whose key was written to the list during the
   85729                 :     ** database scan.  We have to delete items after the scan is complete
   85730                 :     ** because deleting an item can change the scan order.  */
   85731            7888 :     end = sqlite3VdbeMakeLabel(v);
   85732                 : 
   85733                 :     /* Unless this is a view, open cursors for the table we are 
   85734                 :     ** deleting from and all its indices. If this is a view, then the
   85735                 :     ** only effect this statement has is to fire the INSTEAD OF 
   85736                 :     ** triggers.  */
   85737            7888 :     if( !isView ){
   85738            7829 :       sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite);
   85739                 :     }
   85740                 : 
   85741            7888 :     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
   85742                 : 
   85743                 :     /* Delete the row */
   85744                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   85745            7888 :     if( IsVirtual(pTab) ){
   85746               0 :       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
   85747               0 :       sqlite3VtabMakeWritable(pParse, pTab);
   85748               0 :       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB);
   85749               0 :       sqlite3VdbeChangeP5(v, OE_Abort);
   85750               0 :       sqlite3MayAbort(pParse);
   85751                 :     }else
   85752                 : #endif
   85753                 :     {
   85754            7888 :       int count = (pParse->nested==0);    /* True to count changes */
   85755            7888 :       sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default);
   85756                 :     }
   85757                 : 
   85758                 :     /* End of the delete loop */
   85759            7888 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
   85760            7888 :     sqlite3VdbeResolveLabel(v, end);
   85761                 : 
   85762                 :     /* Close the cursors open on the table and its indexes. */
   85763            7888 :     if( !isView && !IsVirtual(pTab) ){
   85764           17080 :       for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   85765            9251 :         sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum);
   85766                 :       }
   85767            7829 :       sqlite3VdbeAddOp1(v, OP_Close, iCur);
   85768                 :     }
   85769                 :   }
   85770                 : 
   85771                 :   /* Update the sqlite_sequence table by storing the content of the
   85772                 :   ** maximum rowid counter values recorded while inserting into
   85773                 :   ** autoincrement tables.
   85774                 :   */
   85775            8126 :   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
   85776            4553 :     sqlite3AutoincrementEnd(pParse);
   85777                 :   }
   85778                 : 
   85779                 :   /* Return the number of rows that were deleted. If this routine is 
   85780                 :   ** generating code because of a call to sqlite3NestedParse(), do not
   85781                 :   ** invoke the callback function.
   85782                 :   */
   85783            8126 :   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
   85784               0 :     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
   85785               0 :     sqlite3VdbeSetNumCols(v, 1);
   85786               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
   85787                 :   }
   85788                 : 
   85789                 : delete_from_cleanup:
   85790            8130 :   sqlite3AuthContextPop(&sContext);
   85791            8130 :   sqlite3SrcListDelete(db, pTabList);
   85792            8130 :   sqlite3ExprDelete(db, pWhere);
   85793                 :   return;
   85794                 : }
   85795                 : /* Make sure "isView" and other macros defined above are undefined. Otherwise
   85796                 : ** thely may interfere with compilation of other functions in this file
   85797                 : ** (or in another file, if this file becomes part of the amalgamation).  */
   85798                 : #ifdef isView
   85799                 :  #undef isView
   85800                 : #endif
   85801                 : #ifdef pTrigger
   85802                 :  #undef pTrigger
   85803                 : #endif
   85804                 : 
   85805                 : /*
   85806                 : ** This routine generates VDBE code that causes a single row of a
   85807                 : ** single table to be deleted.
   85808                 : **
   85809                 : ** The VDBE must be in a particular state when this routine is called.
   85810                 : ** These are the requirements:
   85811                 : **
   85812                 : **   1.  A read/write cursor pointing to pTab, the table containing the row
   85813                 : **       to be deleted, must be opened as cursor number $iCur.
   85814                 : **
   85815                 : **   2.  Read/write cursors for all indices of pTab must be open as
   85816                 : **       cursor number base+i for the i-th index.
   85817                 : **
   85818                 : **   3.  The record number of the row to be deleted must be stored in
   85819                 : **       memory cell iRowid.
   85820                 : **
   85821                 : ** This routine generates code to remove both the table record and all 
   85822                 : ** index entries that point to that record.
   85823                 : */
   85824            8117 : SQLITE_PRIVATE void sqlite3GenerateRowDelete(
   85825                 :   Parse *pParse,     /* Parsing context */
   85826                 :   Table *pTab,       /* Table containing the row to be deleted */
   85827                 :   int iCur,          /* Cursor number for the table */
   85828                 :   int iRowid,        /* Memory cell that contains the rowid to delete */
   85829                 :   int count,         /* If non-zero, increment the row change counter */
   85830                 :   Trigger *pTrigger, /* List of triggers to (potentially) fire */
   85831                 :   int onconf         /* Default ON CONFLICT policy for triggers */
   85832                 : ){
   85833            8117 :   Vdbe *v = pParse->pVdbe;        /* Vdbe */
   85834            8117 :   int iOld = 0;                   /* First register in OLD.* array */
   85835                 :   int iLabel;                     /* Label resolved to end of generated code */
   85836                 : 
   85837                 :   /* Vdbe is guaranteed to have been allocated by this stage. */
   85838            8117 :   assert( v );
   85839                 : 
   85840                 :   /* Seek cursor iCur to the row to delete. If this row no longer exists 
   85841                 :   ** (this can happen if a trigger program has already deleted it), do
   85842                 :   ** not attempt to delete it or fire any DELETE triggers.  */
   85843            8117 :   iLabel = sqlite3VdbeMakeLabel(v);
   85844            8117 :   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
   85845                 :  
   85846                 :   /* If there are any triggers to fire, allocate a range of registers to
   85847                 :   ** use for the old.* references in the triggers.  */
   85848            8117 :   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
   85849                 :     u32 mask;                     /* Mask of OLD.* columns in use */
   85850                 :     int iCol;                     /* Iterator used while populating OLD.* */
   85851                 : 
   85852                 :     /* TODO: Could use temporary registers here. Also could attempt to
   85853                 :     ** avoid copying the contents of the rowid register.  */
   85854            2258 :     mask = sqlite3TriggerColmask(
   85855                 :         pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
   85856                 :     );
   85857            2258 :     mask |= sqlite3FkOldmask(pParse, pTab);
   85858            2258 :     iOld = pParse->nMem+1;
   85859            2258 :     pParse->nMem += (1 + pTab->nCol);
   85860                 : 
   85861                 :     /* Populate the OLD.* pseudo-table register array. These values will be 
   85862                 :     ** used by any BEFORE and AFTER triggers that exist.  */
   85863            2258 :     sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld);
   85864           23868 :     for(iCol=0; iCol<pTab->nCol; iCol++){
   85865           21610 :       if( mask==0xffffffff || mask&(1<<iCol) ){
   85866            2253 :         sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, iOld+iCol+1);
   85867                 :       }
   85868                 :     }
   85869                 : 
   85870                 :     /* Invoke BEFORE DELETE trigger programs. */
   85871            2258 :     sqlite3CodeRowTrigger(pParse, pTrigger, 
   85872                 :         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
   85873                 :     );
   85874                 : 
   85875                 :     /* Seek the cursor to the row to be deleted again. It may be that
   85876                 :     ** the BEFORE triggers coded above have already removed the row
   85877                 :     ** being deleted. Do not attempt to delete the row a second time, and 
   85878                 :     ** do not fire AFTER triggers.  */
   85879            2258 :     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid);
   85880                 : 
   85881                 :     /* Do FK processing. This call checks that any FK constraints that
   85882                 :     ** refer to this table (i.e. constraints attached to other tables) 
   85883                 :     ** are not violated by deleting this row.  */
   85884            2258 :     sqlite3FkCheck(pParse, pTab, iOld, 0);
   85885                 :   }
   85886                 : 
   85887                 :   /* Delete the index and table entries. Skip this step if pTab is really
   85888                 :   ** a view (in which case the only effect of the DELETE statement is to
   85889                 :   ** fire the INSTEAD OF triggers).  */ 
   85890            8117 :   if( pTab->pSelect==0 ){
   85891            8058 :     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0);
   85892            8058 :     sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0));
   85893            8058 :     if( count ){
   85894            6768 :       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
   85895                 :     }
   85896                 :   }
   85897                 : 
   85898                 :   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
   85899                 :   ** handle rows (possibly in other tables) that refer via a foreign key
   85900                 :   ** to the row just deleted. */ 
   85901            8117 :   sqlite3FkActions(pParse, pTab, 0, iOld);
   85902                 : 
   85903                 :   /* Invoke AFTER DELETE trigger programs. */
   85904            8117 :   sqlite3CodeRowTrigger(pParse, pTrigger, 
   85905                 :       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
   85906                 :   );
   85907                 : 
   85908                 :   /* Jump here if the row had already been deleted before any BEFORE
   85909                 :   ** trigger programs were invoked. Or if a trigger program throws a 
   85910                 :   ** RAISE(IGNORE) exception.  */
   85911            8117 :   sqlite3VdbeResolveLabel(v, iLabel);
   85912            8117 : }
   85913                 : 
   85914                 : /*
   85915                 : ** This routine generates VDBE code that causes the deletion of all
   85916                 : ** index entries associated with a single row of a single table.
   85917                 : **
   85918                 : ** The VDBE must be in a particular state when this routine is called.
   85919                 : ** These are the requirements:
   85920                 : **
   85921                 : **   1.  A read/write cursor pointing to pTab, the table containing the row
   85922                 : **       to be deleted, must be opened as cursor number "iCur".
   85923                 : **
   85924                 : **   2.  Read/write cursors for all indices of pTab must be open as
   85925                 : **       cursor number iCur+i for the i-th index.
   85926                 : **
   85927                 : **   3.  The "iCur" cursor must be pointing to the row that is to be
   85928                 : **       deleted.
   85929                 : */
   85930           21751 : SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
   85931                 :   Parse *pParse,     /* Parsing and code generating context */
   85932                 :   Table *pTab,       /* Table containing the row to be deleted */
   85933                 :   int iCur,          /* Cursor number for the table */
   85934                 :   int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
   85935                 : ){
   85936                 :   int i;
   85937                 :   Index *pIdx;
   85938                 :   int r1;
   85939                 : 
   85940           49982 :   for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   85941           28231 :     if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
   85942           13068 :     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
   85943           13068 :     sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
   85944                 :   }
   85945           21751 : }
   85946                 : 
   85947                 : /*
   85948                 : ** Generate code that will assemble an index key and put it in register
   85949                 : ** regOut.  The key with be for index pIdx which is an index on pTab.
   85950                 : ** iCur is the index of a cursor open on the pTab table and pointing to
   85951                 : ** the entry that needs indexing.
   85952                 : **
   85953                 : ** Return a register number which is the first in a block of
   85954                 : ** registers that holds the elements of the index key.  The
   85955                 : ** block of registers has already been deallocated by the time
   85956                 : ** this routine returns.
   85957                 : */
   85958           19458 : SQLITE_PRIVATE int sqlite3GenerateIndexKey(
   85959                 :   Parse *pParse,     /* Parsing context */
   85960                 :   Index *pIdx,       /* The index for which to generate a key */
   85961                 :   int iCur,          /* Cursor number for the pIdx->pTable table */
   85962                 :   int regOut,        /* Write the new index key to this register */
   85963                 :   int doMakeRec      /* Run the OP_MakeRecord instruction if true */
   85964                 : ){
   85965           19458 :   Vdbe *v = pParse->pVdbe;
   85966                 :   int j;
   85967           19458 :   Table *pTab = pIdx->pTable;
   85968                 :   int regBase;
   85969                 :   int nCol;
   85970                 : 
   85971           19458 :   nCol = pIdx->nColumn;
   85972           19458 :   regBase = sqlite3GetTempRange(pParse, nCol+1);
   85973           19458 :   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
   85974           47192 :   for(j=0; j<nCol; j++){
   85975           27734 :     int idx = pIdx->aiColumn[j];
   85976           27734 :     if( idx==pTab->iPKey ){
   85977               0 :       sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);
   85978                 :     }else{
   85979           27734 :       sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
   85980           27734 :       sqlite3ColumnDefault(v, pTab, idx, -1);
   85981                 :     }
   85982                 :   }
   85983           19458 :   if( doMakeRec ){
   85984                 :     const char *zAff;
   85985            5818 :     if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
   85986               0 :       zAff = 0;
   85987                 :     }else{
   85988            5818 :       zAff = sqlite3IndexAffinityStr(v, pIdx);
   85989                 :     }
   85990            5818 :     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
   85991            5818 :     sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
   85992                 :   }
   85993           19458 :   sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
   85994           19458 :   return regBase;
   85995                 : }
   85996                 : 
   85997                 : /************** End of delete.c **********************************************/
   85998                 : /************** Begin file func.c ********************************************/
   85999                 : /*
   86000                 : ** 2002 February 23
   86001                 : **
   86002                 : ** The author disclaims copyright to this source code.  In place of
   86003                 : ** a legal notice, here is a blessing:
   86004                 : **
   86005                 : **    May you do good and not evil.
   86006                 : **    May you find forgiveness for yourself and forgive others.
   86007                 : **    May you share freely, never taking more than you give.
   86008                 : **
   86009                 : *************************************************************************
   86010                 : ** This file contains the C functions that implement various SQL
   86011                 : ** functions of SQLite.  
   86012                 : **
   86013                 : ** There is only one exported symbol in this file - the function
   86014                 : ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
   86015                 : ** All other code has file scope.
   86016                 : */
   86017                 : /* #include <stdlib.h> */
   86018                 : /* #include <assert.h> */
   86019                 : 
   86020                 : /*
   86021                 : ** Return the collating function associated with a function.
   86022                 : */
   86023           48373 : static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
   86024           48373 :   return context->pColl;
   86025                 : }
   86026                 : 
   86027                 : /*
   86028                 : ** Implementation of the non-aggregate min() and max() functions
   86029                 : */
   86030            9658 : static void minmaxFunc(
   86031                 :   sqlite3_context *context,
   86032                 :   int argc,
   86033                 :   sqlite3_value **argv
   86034                 : ){
   86035                 :   int i;
   86036                 :   int mask;    /* 0 for min() or 0xffffffff for max() */
   86037                 :   int iBest;
   86038                 :   CollSeq *pColl;
   86039                 : 
   86040            9658 :   assert( argc>1 );
   86041            9658 :   mask = sqlite3_user_data(context)==0 ? 0 : -1;
   86042            9658 :   pColl = sqlite3GetFuncCollSeq(context);
   86043            9658 :   assert( pColl );
   86044            9658 :   assert( mask==-1 || mask==0 );
   86045            9658 :   iBest = 0;
   86046            9658 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
   86047           25400 :   for(i=1; i<argc; i++){
   86048           16638 :     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
   86049           16637 :     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
   86050                 :       testcase( mask==0 );
   86051           11190 :       iBest = i;
   86052                 :     }
   86053                 :   }
   86054            8762 :   sqlite3_result_value(context, argv[iBest]);
   86055                 : }
   86056                 : 
   86057                 : /*
   86058                 : ** Return the type of the argument.
   86059                 : */
   86060               0 : static void typeofFunc(
   86061                 :   sqlite3_context *context,
   86062                 :   int NotUsed,
   86063                 :   sqlite3_value **argv
   86064                 : ){
   86065               0 :   const char *z = 0;
   86066                 :   UNUSED_PARAMETER(NotUsed);
   86067               0 :   switch( sqlite3_value_type(argv[0]) ){
   86068               0 :     case SQLITE_INTEGER: z = "integer"; break;
   86069               0 :     case SQLITE_TEXT:    z = "text";    break;
   86070               0 :     case SQLITE_FLOAT:   z = "real";    break;
   86071               0 :     case SQLITE_BLOB:    z = "blob";    break;
   86072               0 :     default:             z = "null";    break;
   86073                 :   }
   86074               0 :   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
   86075               0 : }
   86076                 : 
   86077                 : 
   86078                 : /*
   86079                 : ** Implementation of the length() function
   86080                 : */
   86081            2295 : static void lengthFunc(
   86082                 :   sqlite3_context *context,
   86083                 :   int argc,
   86084                 :   sqlite3_value **argv
   86085                 : ){
   86086                 :   int len;
   86087                 : 
   86088            2295 :   assert( argc==1 );
   86089                 :   UNUSED_PARAMETER(argc);
   86090            2295 :   switch( sqlite3_value_type(argv[0]) ){
   86091                 :     case SQLITE_BLOB:
   86092                 :     case SQLITE_INTEGER:
   86093                 :     case SQLITE_FLOAT: {
   86094             208 :       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
   86095             208 :       break;
   86096                 :     }
   86097                 :     case SQLITE_TEXT: {
   86098            1963 :       const unsigned char *z = sqlite3_value_text(argv[0]);
   86099            1963 :       if( z==0 ) return;
   86100            1963 :       len = 0;
   86101           31250 :       while( *z ){
   86102           27324 :         len++;
   86103           27324 :         SQLITE_SKIP_UTF8(z);
   86104                 :       }
   86105            1963 :       sqlite3_result_int(context, len);
   86106            1963 :       break;
   86107                 :     }
   86108                 :     default: {
   86109             124 :       sqlite3_result_null(context);
   86110             124 :       break;
   86111                 :     }
   86112                 :   }
   86113                 : }
   86114                 : 
   86115                 : /*
   86116                 : ** Implementation of the abs() function.
   86117                 : **
   86118                 : ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
   86119                 : ** the numeric argument X. 
   86120                 : */
   86121            4915 : static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   86122            4915 :   assert( argc==1 );
   86123                 :   UNUSED_PARAMETER(argc);
   86124            4915 :   switch( sqlite3_value_type(argv[0]) ){
   86125                 :     case SQLITE_INTEGER: {
   86126            1980 :       i64 iVal = sqlite3_value_int64(argv[0]);
   86127            1980 :       if( iVal<0 ){
   86128             324 :         if( (iVal<<1)==0 ){
   86129                 :           /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then
   86130                 :           ** abs(X) throws an integer overflow error since there is no
   86131                 :           ** equivalent positive 64-bit two complement value. */
   86132               0 :           sqlite3_result_error(context, "integer overflow", -1);
   86133               0 :           return;
   86134                 :         }
   86135             324 :         iVal = -iVal;
   86136                 :       } 
   86137            1980 :       sqlite3_result_int64(context, iVal);
   86138            1980 :       break;
   86139                 :     }
   86140                 :     case SQLITE_NULL: {
   86141                 :       /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
   86142               0 :       sqlite3_result_null(context);
   86143               0 :       break;
   86144                 :     }
   86145                 :     default: {
   86146                 :       /* Because sqlite3_value_double() returns 0.0 if the argument is not
   86147                 :       ** something that can be converted into a number, we have:
   86148                 :       ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
   86149                 :       ** cannot be converted to a numeric value. 
   86150                 :       */
   86151            2935 :       double rVal = sqlite3_value_double(argv[0]);
   86152            2935 :       if( rVal<0 ) rVal = -rVal;
   86153            2935 :       sqlite3_result_double(context, rVal);
   86154            2935 :       break;
   86155                 :     }
   86156                 :   }
   86157                 : }
   86158                 : 
   86159                 : /*
   86160                 : ** Implementation of the substr() function.
   86161                 : **
   86162                 : ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
   86163                 : ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
   86164                 : ** of x.  If x is text, then we actually count UTF-8 characters.
   86165                 : ** If x is a blob, then we count bytes.
   86166                 : **
   86167                 : ** If p1 is negative, then we begin abs(p1) from the end of x[].
   86168                 : **
   86169                 : ** If p2 is negative, return the p2 characters preceeding p1.
   86170                 : */
   86171             368 : static void substrFunc(
   86172                 :   sqlite3_context *context,
   86173                 :   int argc,
   86174                 :   sqlite3_value **argv
   86175                 : ){
   86176                 :   const unsigned char *z;
   86177                 :   const unsigned char *z2;
   86178                 :   int len;
   86179                 :   int p0type;
   86180                 :   i64 p1, p2;
   86181             368 :   int negP2 = 0;
   86182                 : 
   86183             368 :   assert( argc==3 || argc==2 );
   86184             368 :   if( sqlite3_value_type(argv[1])==SQLITE_NULL
   86185             368 :    || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
   86186                 :   ){
   86187               0 :     return;
   86188                 :   }
   86189             368 :   p0type = sqlite3_value_type(argv[0]);
   86190             368 :   p1 = sqlite3_value_int(argv[1]);
   86191             368 :   if( p0type==SQLITE_BLOB ){
   86192               0 :     len = sqlite3_value_bytes(argv[0]);
   86193               0 :     z = sqlite3_value_blob(argv[0]);
   86194               0 :     if( z==0 ) return;
   86195               0 :     assert( len==sqlite3_value_bytes(argv[0]) );
   86196                 :   }else{
   86197             368 :     z = sqlite3_value_text(argv[0]);
   86198             368 :     if( z==0 ) return;
   86199             368 :     len = 0;
   86200             368 :     if( p1<0 ){
   86201               0 :       for(z2=z; *z2; len++){
   86202               0 :         SQLITE_SKIP_UTF8(z2);
   86203                 :       }
   86204                 :     }
   86205                 :   }
   86206             368 :   if( argc==3 ){
   86207             207 :     p2 = sqlite3_value_int(argv[2]);
   86208             207 :     if( p2<0 ){
   86209               0 :       p2 = -p2;
   86210               0 :       negP2 = 1;
   86211                 :     }
   86212                 :   }else{
   86213             161 :     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
   86214                 :   }
   86215             368 :   if( p1<0 ){
   86216               0 :     p1 += len;
   86217               0 :     if( p1<0 ){
   86218               0 :       p2 += p1;
   86219               0 :       if( p2<0 ) p2 = 0;
   86220               0 :       p1 = 0;
   86221                 :     }
   86222             368 :   }else if( p1>0 ){
   86223             368 :     p1--;
   86224               0 :   }else if( p2>0 ){
   86225               0 :     p2--;
   86226                 :   }
   86227             368 :   if( negP2 ){
   86228               0 :     p1 -= p2;
   86229               0 :     if( p1<0 ){
   86230               0 :       p2 += p1;
   86231               0 :       p1 = 0;
   86232                 :     }
   86233                 :   }
   86234             368 :   assert( p1>=0 && p2>=0 );
   86235             368 :   if( p0type!=SQLITE_BLOB ){
   86236           30053 :     while( *z && p1 ){
   86237           29317 :       SQLITE_SKIP_UTF8(z);
   86238           29317 :       p1--;
   86239                 :     }
   86240           33682 :     for(z2=z; *z2 && p2; p2--){
   86241           33314 :       SQLITE_SKIP_UTF8(z2);
   86242                 :     }
   86243             368 :     sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
   86244                 :   }else{
   86245               0 :     if( p1+p2>len ){
   86246               0 :       p2 = len-p1;
   86247               0 :       if( p2<0 ) p2 = 0;
   86248                 :     }
   86249               0 :     sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
   86250                 :   }
   86251                 : }
   86252                 : 
   86253                 : /*
   86254                 : ** Implementation of the round() function
   86255                 : */
   86256                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   86257            9336 : static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   86258            9336 :   int n = 0;
   86259                 :   double r;
   86260                 :   char *zBuf;
   86261            9336 :   assert( argc==1 || argc==2 );
   86262            9336 :   if( argc==2 ){
   86263             448 :     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
   86264             448 :     n = sqlite3_value_int(argv[1]);
   86265             448 :     if( n>30 ) n = 30;
   86266             448 :     if( n<0 ) n = 0;
   86267                 :   }
   86268            9336 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
   86269            9336 :   r = sqlite3_value_double(argv[0]);
   86270                 :   /* If Y==0 and X will fit in a 64-bit int,
   86271                 :   ** handle the rounding directly,
   86272                 :   ** otherwise use printf.
   86273                 :   */
   86274            9336 :   if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
   86275            8888 :     r = (double)((sqlite_int64)(r+0.5));
   86276             448 :   }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
   86277               0 :     r = -(double)((sqlite_int64)((-r)+0.5));
   86278                 :   }else{
   86279             448 :     zBuf = sqlite3_mprintf("%.*f",n,r);
   86280             448 :     if( zBuf==0 ){
   86281               0 :       sqlite3_result_error_nomem(context);
   86282               0 :       return;
   86283                 :     }
   86284             448 :     sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
   86285             448 :     sqlite3_free(zBuf);
   86286                 :   }
   86287            9336 :   sqlite3_result_double(context, r);
   86288                 : }
   86289                 : #endif
   86290                 : 
   86291                 : /*
   86292                 : ** Allocate nByte bytes of space using sqlite3_malloc(). If the
   86293                 : ** allocation fails, call sqlite3_result_error_nomem() to notify
   86294                 : ** the database handle that malloc() has failed and return NULL.
   86295                 : ** If nByte is larger than the maximum string or blob length, then
   86296                 : ** raise an SQLITE_TOOBIG exception and return NULL.
   86297                 : */
   86298              66 : static void *contextMalloc(sqlite3_context *context, i64 nByte){
   86299                 :   char *z;
   86300              66 :   sqlite3 *db = sqlite3_context_db_handle(context);
   86301              66 :   assert( nByte>0 );
   86302                 :   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
   86303                 :   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
   86304              66 :   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   86305               0 :     sqlite3_result_error_toobig(context);
   86306               0 :     z = 0;
   86307                 :   }else{
   86308              66 :     z = sqlite3Malloc((int)nByte);
   86309              66 :     if( !z ){
   86310               0 :       sqlite3_result_error_nomem(context);
   86311                 :     }
   86312                 :   }
   86313              66 :   return z;
   86314                 : }
   86315                 : 
   86316                 : /*
   86317                 : ** Implementation of the upper() and lower() SQL functions.
   86318                 : */
   86319               0 : static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   86320                 :   char *z1;
   86321                 :   const char *z2;
   86322                 :   int i, n;
   86323                 :   UNUSED_PARAMETER(argc);
   86324               0 :   z2 = (char*)sqlite3_value_text(argv[0]);
   86325               0 :   n = sqlite3_value_bytes(argv[0]);
   86326                 :   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
   86327               0 :   assert( z2==(char*)sqlite3_value_text(argv[0]) );
   86328               0 :   if( z2 ){
   86329               0 :     z1 = contextMalloc(context, ((i64)n)+1);
   86330               0 :     if( z1 ){
   86331               0 :       for(i=0; i<n; i++){
   86332               0 :         z1[i] = (char)sqlite3Toupper(z2[i]);
   86333                 :       }
   86334               0 :       sqlite3_result_text(context, z1, n, sqlite3_free);
   86335                 :     }
   86336                 :   }
   86337               0 : }
   86338               0 : static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   86339                 :   char *z1;
   86340                 :   const char *z2;
   86341                 :   int i, n;
   86342                 :   UNUSED_PARAMETER(argc);
   86343               0 :   z2 = (char*)sqlite3_value_text(argv[0]);
   86344               0 :   n = sqlite3_value_bytes(argv[0]);
   86345                 :   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
   86346               0 :   assert( z2==(char*)sqlite3_value_text(argv[0]) );
   86347               0 :   if( z2 ){
   86348               0 :     z1 = contextMalloc(context, ((i64)n)+1);
   86349               0 :     if( z1 ){
   86350               0 :       for(i=0; i<n; i++){
   86351               0 :         z1[i] = sqlite3Tolower(z2[i]);
   86352                 :       }
   86353               0 :       sqlite3_result_text(context, z1, n, sqlite3_free);
   86354                 :     }
   86355                 :   }
   86356               0 : }
   86357                 : 
   86358                 : 
   86359                 : #if 0  /* This function is never used. */
   86360                 : /*
   86361                 : ** The COALESCE() and IFNULL() functions used to be implemented as shown
   86362                 : ** here.  But now they are implemented as VDBE code so that unused arguments
   86363                 : ** do not have to be computed.  This legacy implementation is retained as
   86364                 : ** comment.
   86365                 : */
   86366                 : /*
   86367                 : ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
   86368                 : ** All three do the same thing.  They return the first non-NULL
   86369                 : ** argument.
   86370                 : */
   86371                 : static void ifnullFunc(
   86372                 :   sqlite3_context *context,
   86373                 :   int argc,
   86374                 :   sqlite3_value **argv
   86375                 : ){
   86376                 :   int i;
   86377                 :   for(i=0; i<argc; i++){
   86378                 :     if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
   86379                 :       sqlite3_result_value(context, argv[i]);
   86380                 :       break;
   86381                 :     }
   86382                 :   }
   86383                 : }
   86384                 : #endif /* NOT USED */
   86385                 : #define ifnullFunc versionFunc   /* Substitute function - never called */
   86386                 : 
   86387                 : /*
   86388                 : ** Implementation of random().  Return a random integer.  
   86389                 : */
   86390             400 : static void randomFunc(
   86391                 :   sqlite3_context *context,
   86392                 :   int NotUsed,
   86393                 :   sqlite3_value **NotUsed2
   86394                 : ){
   86395                 :   sqlite_int64 r;
   86396                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   86397             400 :   sqlite3_randomness(sizeof(r), &r);
   86398             400 :   if( r<0 ){
   86399                 :     /* We need to prevent a random number of 0x8000000000000000 
   86400                 :     ** (or -9223372036854775808) since when you do abs() of that
   86401                 :     ** number of you get the same value back again.  To do this
   86402                 :     ** in a way that is testable, mask the sign bit off of negative
   86403                 :     ** values, resulting in a positive value.  Then take the 
   86404                 :     ** 2s complement of that positive value.  The end result can
   86405                 :     ** therefore be no less than -9223372036854775807.
   86406                 :     */
   86407             187 :     r = -(r ^ (((sqlite3_int64)1)<<63));
   86408                 :   }
   86409             400 :   sqlite3_result_int64(context, r);
   86410             400 : }
   86411                 : 
   86412                 : /*
   86413                 : ** Implementation of randomblob(N).  Return a random blob
   86414                 : ** that is N bytes long.
   86415                 : */
   86416               0 : static void randomBlob(
   86417                 :   sqlite3_context *context,
   86418                 :   int argc,
   86419                 :   sqlite3_value **argv
   86420                 : ){
   86421                 :   int n;
   86422                 :   unsigned char *p;
   86423               0 :   assert( argc==1 );
   86424                 :   UNUSED_PARAMETER(argc);
   86425               0 :   n = sqlite3_value_int(argv[0]);
   86426               0 :   if( n<1 ){
   86427               0 :     n = 1;
   86428                 :   }
   86429               0 :   p = contextMalloc(context, n);
   86430               0 :   if( p ){
   86431               0 :     sqlite3_randomness(n, p);
   86432               0 :     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
   86433                 :   }
   86434               0 : }
   86435                 : 
   86436                 : /*
   86437                 : ** Implementation of the last_insert_rowid() SQL function.  The return
   86438                 : ** value is the same as the sqlite3_last_insert_rowid() API function.
   86439                 : */
   86440               0 : static void last_insert_rowid(
   86441                 :   sqlite3_context *context, 
   86442                 :   int NotUsed, 
   86443                 :   sqlite3_value **NotUsed2
   86444                 : ){
   86445               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   86446                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   86447                 :   /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
   86448                 :   ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
   86449                 :   ** function. */
   86450               0 :   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
   86451               0 : }
   86452                 : 
   86453                 : /*
   86454                 : ** Implementation of the changes() SQL function.
   86455                 : **
   86456                 : ** IMP: R-62073-11209 The changes() SQL function is a wrapper
   86457                 : ** around the sqlite3_changes() C/C++ function and hence follows the same
   86458                 : ** rules for counting changes.
   86459                 : */
   86460               0 : static void changes(
   86461                 :   sqlite3_context *context,
   86462                 :   int NotUsed,
   86463                 :   sqlite3_value **NotUsed2
   86464                 : ){
   86465               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   86466                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   86467               0 :   sqlite3_result_int(context, sqlite3_changes(db));
   86468               0 : }
   86469                 : 
   86470                 : /*
   86471                 : ** Implementation of the total_changes() SQL function.  The return value is
   86472                 : ** the same as the sqlite3_total_changes() API function.
   86473                 : */
   86474               0 : static void total_changes(
   86475                 :   sqlite3_context *context,
   86476                 :   int NotUsed,
   86477                 :   sqlite3_value **NotUsed2
   86478                 : ){
   86479               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   86480                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   86481                 :   /* IMP: R-52756-41993 This function is a wrapper around the
   86482                 :   ** sqlite3_total_changes() C/C++ interface. */
   86483               0 :   sqlite3_result_int(context, sqlite3_total_changes(db));
   86484               0 : }
   86485                 : 
   86486                 : /*
   86487                 : ** A structure defining how to do GLOB-style comparisons.
   86488                 : */
   86489                 : struct compareInfo {
   86490                 :   u8 matchAll;
   86491                 :   u8 matchOne;
   86492                 :   u8 matchSet;
   86493                 :   u8 noCase;
   86494                 : };
   86495                 : 
   86496                 : /*
   86497                 : ** For LIKE and GLOB matching on EBCDIC machines, assume that every
   86498                 : ** character is exactly one byte in size.  Also, all characters are
   86499                 : ** able to participate in upper-case-to-lower-case mappings in EBCDIC
   86500                 : ** whereas only characters less than 0x80 do in ASCII.
   86501                 : */
   86502                 : #if defined(SQLITE_EBCDIC)
   86503                 : # define sqlite3Utf8Read(A,C)  (*(A++))
   86504                 : # define GlogUpperToLower(A)   A = sqlite3UpperToLower[A]
   86505                 : #else
   86506                 : # define GlogUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
   86507                 : #endif
   86508                 : 
   86509                 : static const struct compareInfo globInfo = { '*', '?', '[', 0 };
   86510                 : /* The correct SQL-92 behavior is for the LIKE operator to ignore
   86511                 : ** case.  Thus  'a' LIKE 'A' would be true. */
   86512                 : static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
   86513                 : /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
   86514                 : ** is case sensitive causing 'a' LIKE 'A' to be false */
   86515                 : static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
   86516                 : 
   86517                 : /*
   86518                 : ** Compare two UTF-8 strings for equality where the first string can
   86519                 : ** potentially be a "glob" expression.  Return true (1) if they
   86520                 : ** are the same and false (0) if they are different.
   86521                 : **
   86522                 : ** Globbing rules:
   86523                 : **
   86524                 : **      '*'       Matches any sequence of zero or more characters.
   86525                 : **
   86526                 : **      '?'       Matches exactly one character.
   86527                 : **
   86528                 : **     [...]      Matches one character from the enclosed list of
   86529                 : **                characters.
   86530                 : **
   86531                 : **     [^...]     Matches one character not in the enclosed list.
   86532                 : **
   86533                 : ** With the [...] and [^...] matching, a ']' character can be included
   86534                 : ** in the list by making it the first character after '[' or '^'.  A
   86535                 : ** range of characters can be specified using '-'.  Example:
   86536                 : ** "[a-z]" matches any single lower-case letter.  To match a '-', make
   86537                 : ** it the last character in the list.
   86538                 : **
   86539                 : ** This routine is usually quick, but can be N**2 in the worst case.
   86540                 : **
   86541                 : ** Hints: to match '*' or '?', put them in "[]".  Like this:
   86542                 : **
   86543                 : **         abc[*]xyz        Matches "abc*xyz" only
   86544                 : */
   86545             102 : static int patternCompare(
   86546                 :   const u8 *zPattern,              /* The glob pattern */
   86547                 :   const u8 *zString,               /* The string to compare against the glob */
   86548                 :   const struct compareInfo *pInfo, /* Information about how to do the compare */
   86549                 :   u32 esc                          /* The escape character */
   86550                 : ){
   86551                 :   u32 c, c2;
   86552                 :   int invert;
   86553                 :   int seen;
   86554             102 :   u8 matchOne = pInfo->matchOne;
   86555             102 :   u8 matchAll = pInfo->matchAll;
   86556             102 :   u8 matchSet = pInfo->matchSet;
   86557             102 :   u8 noCase = pInfo->noCase; 
   86558             102 :   int prevEscape = 0;     /* True if the previous character was 'escape' */
   86559                 : 
   86560            1461 :   while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
   86561            1359 :     if( !prevEscape && c==matchAll ){
   86562              84 :       while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
   86563              42 :                || c == matchOne ){
   86564               0 :         if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
   86565               0 :           return 0;
   86566                 :         }
   86567                 :       }
   86568              42 :       if( c==0 ){
   86569              42 :         return 1;
   86570               0 :       }else if( c==esc ){
   86571               0 :         c = sqlite3Utf8Read(zPattern, &zPattern);
   86572               0 :         if( c==0 ){
   86573               0 :           return 0;
   86574                 :         }
   86575               0 :       }else if( c==matchSet ){
   86576               0 :         assert( esc==0 );         /* This is GLOB, not LIKE */
   86577               0 :         assert( matchSet<0x80 );  /* '[' is a single-byte character */
   86578               0 :         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
   86579               0 :           SQLITE_SKIP_UTF8(zString);
   86580                 :         }
   86581               0 :         return *zString!=0;
   86582                 :       }
   86583               0 :       while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
   86584               0 :         if( noCase ){
   86585               0 :           GlogUpperToLower(c2);
   86586               0 :           GlogUpperToLower(c);
   86587               0 :           while( c2 != 0 && c2 != c ){
   86588               0 :             c2 = sqlite3Utf8Read(zString, &zString);
   86589               0 :             GlogUpperToLower(c2);
   86590                 :           }
   86591                 :         }else{
   86592               0 :           while( c2 != 0 && c2 != c ){
   86593               0 :             c2 = sqlite3Utf8Read(zString, &zString);
   86594                 :           }
   86595                 :         }
   86596               0 :         if( c2==0 ) return 0;
   86597               0 :         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
   86598                 :       }
   86599               0 :       return 0;
   86600            1317 :     }else if( !prevEscape && c==matchOne ){
   86601               0 :       if( sqlite3Utf8Read(zString, &zString)==0 ){
   86602               0 :         return 0;
   86603                 :       }
   86604            1317 :     }else if( c==matchSet ){
   86605               0 :       u32 prior_c = 0;
   86606               0 :       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
   86607               0 :       seen = 0;
   86608               0 :       invert = 0;
   86609               0 :       c = sqlite3Utf8Read(zString, &zString);
   86610               0 :       if( c==0 ) return 0;
   86611               0 :       c2 = sqlite3Utf8Read(zPattern, &zPattern);
   86612               0 :       if( c2=='^' ){
   86613               0 :         invert = 1;
   86614               0 :         c2 = sqlite3Utf8Read(zPattern, &zPattern);
   86615                 :       }
   86616               0 :       if( c2==']' ){
   86617               0 :         if( c==']' ) seen = 1;
   86618               0 :         c2 = sqlite3Utf8Read(zPattern, &zPattern);
   86619                 :       }
   86620               0 :       while( c2 && c2!=']' ){
   86621               0 :         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
   86622               0 :           c2 = sqlite3Utf8Read(zPattern, &zPattern);
   86623               0 :           if( c>=prior_c && c<=c2 ) seen = 1;
   86624               0 :           prior_c = 0;
   86625                 :         }else{
   86626               0 :           if( c==c2 ){
   86627               0 :             seen = 1;
   86628                 :           }
   86629               0 :           prior_c = c2;
   86630                 :         }
   86631               0 :         c2 = sqlite3Utf8Read(zPattern, &zPattern);
   86632                 :       }
   86633               0 :       if( c2==0 || (seen ^ invert)==0 ){
   86634               0 :         return 0;
   86635                 :       }
   86636            1317 :     }else if( esc==c && !prevEscape ){
   86637               0 :       prevEscape = 1;
   86638                 :     }else{
   86639            1317 :       c2 = sqlite3Utf8Read(zString, &zString);
   86640            1317 :       if( noCase ){
   86641             801 :         GlogUpperToLower(c);
   86642             801 :         GlogUpperToLower(c2);
   86643                 :       }
   86644            1317 :       if( c!=c2 ){
   86645              60 :         return 0;
   86646                 :       }
   86647            1257 :       prevEscape = 0;
   86648                 :     }
   86649                 :   }
   86650               0 :   return *zString==0;
   86651                 : }
   86652                 : 
   86653                 : /*
   86654                 : ** Count the number of times that the LIKE operator (or GLOB which is
   86655                 : ** just a variation of LIKE) gets called.  This is used for testing
   86656                 : ** only.
   86657                 : */
   86658                 : #ifdef SQLITE_TEST
   86659                 : SQLITE_API int sqlite3_like_count = 0;
   86660                 : #endif
   86661                 : 
   86662                 : 
   86663                 : /*
   86664                 : ** Implementation of the like() SQL function.  This function implements
   86665                 : ** the build-in LIKE operator.  The first argument to the function is the
   86666                 : ** pattern and the second argument is the string.  So, the SQL statements:
   86667                 : **
   86668                 : **       A LIKE B
   86669                 : **
   86670                 : ** is implemented as like(B,A).
   86671                 : **
   86672                 : ** This same function (with a different compareInfo structure) computes
   86673                 : ** the GLOB operator.
   86674                 : */
   86675             116 : static void likeFunc(
   86676                 :   sqlite3_context *context, 
   86677                 :   int argc, 
   86678                 :   sqlite3_value **argv
   86679                 : ){
   86680                 :   const unsigned char *zA, *zB;
   86681             116 :   u32 escape = 0;
   86682                 :   int nPat;
   86683             116 :   sqlite3 *db = sqlite3_context_db_handle(context);
   86684                 : 
   86685             116 :   zB = sqlite3_value_text(argv[0]);
   86686             116 :   zA = sqlite3_value_text(argv[1]);
   86687                 : 
   86688                 :   /* Limit the length of the LIKE or GLOB pattern to avoid problems
   86689                 :   ** of deep recursion and N*N behavior in patternCompare().
   86690                 :   */
   86691             116 :   nPat = sqlite3_value_bytes(argv[0]);
   86692                 :   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
   86693                 :   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
   86694             116 :   if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
   86695               0 :     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
   86696               0 :     return;
   86697                 :   }
   86698             116 :   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
   86699                 : 
   86700             116 :   if( argc==3 ){
   86701                 :     /* The escape character string must consist of a single UTF-8 character.
   86702                 :     ** Otherwise, return an error.
   86703                 :     */
   86704               0 :     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
   86705               0 :     if( zEsc==0 ) return;
   86706               0 :     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
   86707               0 :       sqlite3_result_error(context, 
   86708                 :           "ESCAPE expression must be a single character", -1);
   86709               0 :       return;
   86710                 :     }
   86711               0 :     escape = sqlite3Utf8Read(zEsc, &zEsc);
   86712                 :   }
   86713             116 :   if( zA && zB ){
   86714             102 :     struct compareInfo *pInfo = sqlite3_user_data(context);
   86715                 : #ifdef SQLITE_TEST
   86716                 :     sqlite3_like_count++;
   86717                 : #endif
   86718                 :     
   86719             102 :     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
   86720                 :   }
   86721                 : }
   86722                 : 
   86723                 : /*
   86724                 : ** Implementation of the NULLIF(x,y) function.  The result is the first
   86725                 : ** argument if the arguments are different.  The result is NULL if the
   86726                 : ** arguments are equal to each other.
   86727                 : */
   86728               0 : static void nullifFunc(
   86729                 :   sqlite3_context *context,
   86730                 :   int NotUsed,
   86731                 :   sqlite3_value **argv
   86732                 : ){
   86733               0 :   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
   86734                 :   UNUSED_PARAMETER(NotUsed);
   86735               0 :   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
   86736               0 :     sqlite3_result_value(context, argv[0]);
   86737                 :   }
   86738               0 : }
   86739                 : 
   86740                 : /*
   86741                 : ** Implementation of the sqlite_version() function.  The result is the version
   86742                 : ** of the SQLite library that is running.
   86743                 : */
   86744               0 : static void versionFunc(
   86745                 :   sqlite3_context *context,
   86746                 :   int NotUsed,
   86747                 :   sqlite3_value **NotUsed2
   86748                 : ){
   86749                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   86750                 :   /* IMP: R-48699-48617 This function is an SQL wrapper around the
   86751                 :   ** sqlite3_libversion() C-interface. */
   86752               0 :   sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
   86753               0 : }
   86754                 : 
   86755                 : /*
   86756                 : ** Implementation of the sqlite_source_id() function. The result is a string
   86757                 : ** that identifies the particular version of the source code used to build
   86758                 : ** SQLite.
   86759                 : */
   86760               0 : static void sourceidFunc(
   86761                 :   sqlite3_context *context,
   86762                 :   int NotUsed,
   86763                 :   sqlite3_value **NotUsed2
   86764                 : ){
   86765                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   86766                 :   /* IMP: R-24470-31136 This function is an SQL wrapper around the
   86767                 :   ** sqlite3_sourceid() C interface. */
   86768               0 :   sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
   86769               0 : }
   86770                 : 
   86771                 : /*
   86772                 : ** Implementation of the sqlite_log() function.  This is a wrapper around
   86773                 : ** sqlite3_log().  The return value is NULL.  The function exists purely for
   86774                 : ** its side-effects.
   86775                 : */
   86776               0 : static void errlogFunc(
   86777                 :   sqlite3_context *context,
   86778                 :   int argc,
   86779                 :   sqlite3_value **argv
   86780                 : ){
   86781                 :   UNUSED_PARAMETER(argc);
   86782                 :   UNUSED_PARAMETER(context);
   86783               0 :   sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
   86784               0 : }
   86785                 : 
   86786                 : /*
   86787                 : ** Implementation of the sqlite_compileoption_used() function.
   86788                 : ** The result is an integer that identifies if the compiler option
   86789                 : ** was used to build SQLite.
   86790                 : */
   86791                 : #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   86792               0 : static void compileoptionusedFunc(
   86793                 :   sqlite3_context *context,
   86794                 :   int argc,
   86795                 :   sqlite3_value **argv
   86796                 : ){
   86797                 :   const char *zOptName;
   86798               0 :   assert( argc==1 );
   86799                 :   UNUSED_PARAMETER(argc);
   86800                 :   /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
   86801                 :   ** function is a wrapper around the sqlite3_compileoption_used() C/C++
   86802                 :   ** function.
   86803                 :   */
   86804               0 :   if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
   86805               0 :     sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
   86806                 :   }
   86807               0 : }
   86808                 : #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
   86809                 : 
   86810                 : /*
   86811                 : ** Implementation of the sqlite_compileoption_get() function. 
   86812                 : ** The result is a string that identifies the compiler options 
   86813                 : ** used to build SQLite.
   86814                 : */
   86815                 : #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   86816               0 : static void compileoptiongetFunc(
   86817                 :   sqlite3_context *context,
   86818                 :   int argc,
   86819                 :   sqlite3_value **argv
   86820                 : ){
   86821                 :   int n;
   86822               0 :   assert( argc==1 );
   86823                 :   UNUSED_PARAMETER(argc);
   86824                 :   /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
   86825                 :   ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
   86826                 :   */
   86827               0 :   n = sqlite3_value_int(argv[0]);
   86828               0 :   sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
   86829               0 : }
   86830                 : #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
   86831                 : 
   86832                 : /* Array for converting from half-bytes (nybbles) into ASCII hex
   86833                 : ** digits. */
   86834                 : static const char hexdigits[] = {
   86835                 :   '0', '1', '2', '3', '4', '5', '6', '7',
   86836                 :   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 
   86837                 : };
   86838                 : 
   86839                 : /*
   86840                 : ** EXPERIMENTAL - This is not an official function.  The interface may
   86841                 : ** change.  This function may disappear.  Do not write code that depends
   86842                 : ** on this function.
   86843                 : **
   86844                 : ** Implementation of the QUOTE() function.  This function takes a single
   86845                 : ** argument.  If the argument is numeric, the return value is the same as
   86846                 : ** the argument.  If the argument is NULL, the return value is the string
   86847                 : ** "NULL".  Otherwise, the argument is enclosed in single quotes with
   86848                 : ** single-quote escapes.
   86849                 : */
   86850              39 : static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   86851              39 :   assert( argc==1 );
   86852                 :   UNUSED_PARAMETER(argc);
   86853              39 :   switch( sqlite3_value_type(argv[0]) ){
   86854                 :     case SQLITE_INTEGER:
   86855                 :     case SQLITE_FLOAT: {
   86856               0 :       sqlite3_result_value(context, argv[0]);
   86857               0 :       break;
   86858                 :     }
   86859                 :     case SQLITE_BLOB: {
   86860               0 :       char *zText = 0;
   86861               0 :       char const *zBlob = sqlite3_value_blob(argv[0]);
   86862               0 :       int nBlob = sqlite3_value_bytes(argv[0]);
   86863               0 :       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
   86864               0 :       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); 
   86865               0 :       if( zText ){
   86866                 :         int i;
   86867               0 :         for(i=0; i<nBlob; i++){
   86868               0 :           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
   86869               0 :           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
   86870                 :         }
   86871               0 :         zText[(nBlob*2)+2] = '\'';
   86872               0 :         zText[(nBlob*2)+3] = '\0';
   86873               0 :         zText[0] = 'X';
   86874               0 :         zText[1] = '\'';
   86875               0 :         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
   86876               0 :         sqlite3_free(zText);
   86877                 :       }
   86878               0 :       break;
   86879                 :     }
   86880                 :     case SQLITE_TEXT: {
   86881                 :       int i,j;
   86882                 :       u64 n;
   86883              39 :       const unsigned char *zArg = sqlite3_value_text(argv[0]);
   86884                 :       char *z;
   86885                 : 
   86886              39 :       if( zArg==0 ) return;
   86887              39 :       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
   86888              39 :       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
   86889              39 :       if( z ){
   86890              39 :         z[0] = '\'';
   86891             576 :         for(i=0, j=1; zArg[i]; i++){
   86892             537 :           z[j++] = zArg[i];
   86893             537 :           if( zArg[i]=='\'' ){
   86894               0 :             z[j++] = '\'';
   86895                 :           }
   86896                 :         }
   86897              39 :         z[j++] = '\'';
   86898              39 :         z[j] = 0;
   86899              39 :         sqlite3_result_text(context, z, j, sqlite3_free);
   86900                 :       }
   86901              39 :       break;
   86902                 :     }
   86903                 :     default: {
   86904               0 :       assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
   86905               0 :       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
   86906               0 :       break;
   86907                 :     }
   86908                 :   }
   86909                 : }
   86910                 : 
   86911                 : /*
   86912                 : ** The hex() function.  Interpret the argument as a blob.  Return
   86913                 : ** a hexadecimal rendering as text.
   86914                 : */
   86915               0 : static void hexFunc(
   86916                 :   sqlite3_context *context,
   86917                 :   int argc,
   86918                 :   sqlite3_value **argv
   86919                 : ){
   86920                 :   int i, n;
   86921                 :   const unsigned char *pBlob;
   86922                 :   char *zHex, *z;
   86923               0 :   assert( argc==1 );
   86924                 :   UNUSED_PARAMETER(argc);
   86925               0 :   pBlob = sqlite3_value_blob(argv[0]);
   86926               0 :   n = sqlite3_value_bytes(argv[0]);
   86927               0 :   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
   86928               0 :   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
   86929               0 :   if( zHex ){
   86930               0 :     for(i=0; i<n; i++, pBlob++){
   86931               0 :       unsigned char c = *pBlob;
   86932               0 :       *(z++) = hexdigits[(c>>4)&0xf];
   86933               0 :       *(z++) = hexdigits[c&0xf];
   86934                 :     }
   86935               0 :     *z = 0;
   86936               0 :     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
   86937                 :   }
   86938               0 : }
   86939                 : 
   86940                 : /*
   86941                 : ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
   86942                 : */
   86943               0 : static void zeroblobFunc(
   86944                 :   sqlite3_context *context,
   86945                 :   int argc,
   86946                 :   sqlite3_value **argv
   86947                 : ){
   86948                 :   i64 n;
   86949               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   86950               0 :   assert( argc==1 );
   86951                 :   UNUSED_PARAMETER(argc);
   86952               0 :   n = sqlite3_value_int64(argv[0]);
   86953                 :   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
   86954                 :   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
   86955               0 :   if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   86956               0 :     sqlite3_result_error_toobig(context);
   86957                 :   }else{
   86958               0 :     sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
   86959                 :   }
   86960               0 : }
   86961                 : 
   86962                 : /*
   86963                 : ** The replace() function.  Three arguments are all strings: call
   86964                 : ** them A, B, and C. The result is also a string which is derived
   86965                 : ** from A by replacing every occurance of B with C.  The match
   86966                 : ** must be exact.  Collating sequences are not used.
   86967                 : */
   86968              27 : static void replaceFunc(
   86969                 :   sqlite3_context *context,
   86970                 :   int argc,
   86971                 :   sqlite3_value **argv
   86972                 : ){
   86973                 :   const unsigned char *zStr;        /* The input string A */
   86974                 :   const unsigned char *zPattern;    /* The pattern string B */
   86975                 :   const unsigned char *zRep;        /* The replacement string C */
   86976                 :   unsigned char *zOut;              /* The output */
   86977                 :   int nStr;                /* Size of zStr */
   86978                 :   int nPattern;            /* Size of zPattern */
   86979                 :   int nRep;                /* Size of zRep */
   86980                 :   i64 nOut;                /* Maximum size of zOut */
   86981                 :   int loopLimit;           /* Last zStr[] that might match zPattern[] */
   86982                 :   int i, j;                /* Loop counters */
   86983                 : 
   86984              27 :   assert( argc==3 );
   86985                 :   UNUSED_PARAMETER(argc);
   86986              27 :   zStr = sqlite3_value_text(argv[0]);
   86987              27 :   if( zStr==0 ) return;
   86988              27 :   nStr = sqlite3_value_bytes(argv[0]);
   86989              27 :   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
   86990              27 :   zPattern = sqlite3_value_text(argv[1]);
   86991              27 :   if( zPattern==0 ){
   86992               0 :     assert( sqlite3_value_type(argv[1])==SQLITE_NULL
   86993                 :             || sqlite3_context_db_handle(context)->mallocFailed );
   86994               0 :     return;
   86995                 :   }
   86996              27 :   if( zPattern[0]==0 ){
   86997               0 :     assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
   86998               0 :     sqlite3_result_value(context, argv[0]);
   86999               0 :     return;
   87000                 :   }
   87001              27 :   nPattern = sqlite3_value_bytes(argv[1]);
   87002              27 :   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
   87003              27 :   zRep = sqlite3_value_text(argv[2]);
   87004              27 :   if( zRep==0 ) return;
   87005              27 :   nRep = sqlite3_value_bytes(argv[2]);
   87006              27 :   assert( zRep==sqlite3_value_text(argv[2]) );
   87007              27 :   nOut = nStr + 1;
   87008              27 :   assert( nOut<SQLITE_MAX_LENGTH );
   87009              27 :   zOut = contextMalloc(context, (i64)nOut);
   87010              27 :   if( zOut==0 ){
   87011               0 :     return;
   87012                 :   }
   87013              27 :   loopLimit = nStr - nPattern;  
   87014             558 :   for(i=j=0; i<=loopLimit; i++){
   87015             531 :     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
   87016             504 :       zOut[j++] = zStr[i];
   87017                 :     }else{
   87018                 :       u8 *zOld;
   87019              27 :       sqlite3 *db = sqlite3_context_db_handle(context);
   87020              27 :       nOut += nRep - nPattern;
   87021                 :       testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
   87022                 :       testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
   87023              27 :       if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
   87024               0 :         sqlite3_result_error_toobig(context);
   87025               0 :         sqlite3_free(zOut);
   87026               0 :         return;
   87027                 :       }
   87028              27 :       zOld = zOut;
   87029              27 :       zOut = sqlite3_realloc(zOut, (int)nOut);
   87030              27 :       if( zOut==0 ){
   87031               0 :         sqlite3_result_error_nomem(context);
   87032               0 :         sqlite3_free(zOld);
   87033               0 :         return;
   87034                 :       }
   87035              27 :       memcpy(&zOut[j], zRep, nRep);
   87036              27 :       j += nRep;
   87037              27 :       i += nPattern-1;
   87038                 :     }
   87039                 :   }
   87040              27 :   assert( j+nStr-i+1==nOut );
   87041              27 :   memcpy(&zOut[j], &zStr[i], nStr-i);
   87042              27 :   j += nStr - i;
   87043              27 :   assert( j<=nOut );
   87044              27 :   zOut[j] = 0;
   87045              27 :   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
   87046                 : }
   87047                 : 
   87048                 : /*
   87049                 : ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
   87050                 : ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
   87051                 : */
   87052               0 : static void trimFunc(
   87053                 :   sqlite3_context *context,
   87054                 :   int argc,
   87055                 :   sqlite3_value **argv
   87056                 : ){
   87057                 :   const unsigned char *zIn;         /* Input string */
   87058                 :   const unsigned char *zCharSet;    /* Set of characters to trim */
   87059                 :   int nIn;                          /* Number of bytes in input */
   87060                 :   int flags;                        /* 1: trimleft  2: trimright  3: trim */
   87061                 :   int i;                            /* Loop counter */
   87062               0 :   unsigned char *aLen = 0;          /* Length of each character in zCharSet */
   87063               0 :   unsigned char **azChar = 0;       /* Individual characters in zCharSet */
   87064                 :   int nChar;                        /* Number of characters in zCharSet */
   87065                 : 
   87066               0 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
   87067               0 :     return;
   87068                 :   }
   87069               0 :   zIn = sqlite3_value_text(argv[0]);
   87070               0 :   if( zIn==0 ) return;
   87071               0 :   nIn = sqlite3_value_bytes(argv[0]);
   87072               0 :   assert( zIn==sqlite3_value_text(argv[0]) );
   87073               0 :   if( argc==1 ){
   87074                 :     static const unsigned char lenOne[] = { 1 };
   87075                 :     static unsigned char * const azOne[] = { (u8*)" " };
   87076               0 :     nChar = 1;
   87077               0 :     aLen = (u8*)lenOne;
   87078               0 :     azChar = (unsigned char **)azOne;
   87079               0 :     zCharSet = 0;
   87080               0 :   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
   87081               0 :     return;
   87082                 :   }else{
   87083                 :     const unsigned char *z;
   87084               0 :     for(z=zCharSet, nChar=0; *z; nChar++){
   87085               0 :       SQLITE_SKIP_UTF8(z);
   87086                 :     }
   87087               0 :     if( nChar>0 ){
   87088               0 :       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
   87089               0 :       if( azChar==0 ){
   87090               0 :         return;
   87091                 :       }
   87092               0 :       aLen = (unsigned char*)&azChar[nChar];
   87093               0 :       for(z=zCharSet, nChar=0; *z; nChar++){
   87094               0 :         azChar[nChar] = (unsigned char *)z;
   87095               0 :         SQLITE_SKIP_UTF8(z);
   87096               0 :         aLen[nChar] = (u8)(z - azChar[nChar]);
   87097                 :       }
   87098                 :     }
   87099                 :   }
   87100               0 :   if( nChar>0 ){
   87101               0 :     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
   87102               0 :     if( flags & 1 ){
   87103               0 :       while( nIn>0 ){
   87104               0 :         int len = 0;
   87105               0 :         for(i=0; i<nChar; i++){
   87106               0 :           len = aLen[i];
   87107               0 :           if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
   87108                 :         }
   87109               0 :         if( i>=nChar ) break;
   87110               0 :         zIn += len;
   87111               0 :         nIn -= len;
   87112                 :       }
   87113                 :     }
   87114               0 :     if( flags & 2 ){
   87115               0 :       while( nIn>0 ){
   87116               0 :         int len = 0;
   87117               0 :         for(i=0; i<nChar; i++){
   87118               0 :           len = aLen[i];
   87119               0 :           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
   87120                 :         }
   87121               0 :         if( i>=nChar ) break;
   87122               0 :         nIn -= len;
   87123                 :       }
   87124                 :     }
   87125               0 :     if( zCharSet ){
   87126               0 :       sqlite3_free(azChar);
   87127                 :     }
   87128                 :   }
   87129               0 :   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
   87130                 : }
   87131                 : 
   87132                 : 
   87133                 : /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
   87134                 : ** is only available if the SQLITE_SOUNDEX compile-time option is used
   87135                 : ** when SQLite is built.
   87136                 : */
   87137                 : #ifdef SQLITE_SOUNDEX
   87138                 : /*
   87139                 : ** Compute the soundex encoding of a word.
   87140                 : **
   87141                 : ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
   87142                 : ** soundex encoding of the string X. 
   87143                 : */
   87144                 : static void soundexFunc(
   87145                 :   sqlite3_context *context,
   87146                 :   int argc,
   87147                 :   sqlite3_value **argv
   87148                 : ){
   87149                 :   char zResult[8];
   87150                 :   const u8 *zIn;
   87151                 :   int i, j;
   87152                 :   static const unsigned char iCode[] = {
   87153                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   87154                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   87155                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   87156                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   87157                 :     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
   87158                 :     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
   87159                 :     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
   87160                 :     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
   87161                 :   };
   87162                 :   assert( argc==1 );
   87163                 :   zIn = (u8*)sqlite3_value_text(argv[0]);
   87164                 :   if( zIn==0 ) zIn = (u8*)"";
   87165                 :   for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
   87166                 :   if( zIn[i] ){
   87167                 :     u8 prevcode = iCode[zIn[i]&0x7f];
   87168                 :     zResult[0] = sqlite3Toupper(zIn[i]);
   87169                 :     for(j=1; j<4 && zIn[i]; i++){
   87170                 :       int code = iCode[zIn[i]&0x7f];
   87171                 :       if( code>0 ){
   87172                 :         if( code!=prevcode ){
   87173                 :           prevcode = code;
   87174                 :           zResult[j++] = code + '0';
   87175                 :         }
   87176                 :       }else{
   87177                 :         prevcode = 0;
   87178                 :       }
   87179                 :     }
   87180                 :     while( j<4 ){
   87181                 :       zResult[j++] = '0';
   87182                 :     }
   87183                 :     zResult[j] = 0;
   87184                 :     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
   87185                 :   }else{
   87186                 :     /* IMP: R-64894-50321 The string "?000" is returned if the argument
   87187                 :     ** is NULL or contains no ASCII alphabetic characters. */
   87188                 :     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
   87189                 :   }
   87190                 : }
   87191                 : #endif /* SQLITE_SOUNDEX */
   87192                 : 
   87193                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
   87194                 : /*
   87195                 : ** A function that loads a shared-library extension then returns NULL.
   87196                 : */
   87197               0 : static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
   87198               0 :   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
   87199                 :   const char *zProc;
   87200               0 :   sqlite3 *db = sqlite3_context_db_handle(context);
   87201               0 :   char *zErrMsg = 0;
   87202                 : 
   87203               0 :   if( argc==2 ){
   87204               0 :     zProc = (const char *)sqlite3_value_text(argv[1]);
   87205                 :   }else{
   87206               0 :     zProc = 0;
   87207                 :   }
   87208               0 :   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
   87209               0 :     sqlite3_result_error(context, zErrMsg, -1);
   87210               0 :     sqlite3_free(zErrMsg);
   87211                 :   }
   87212               0 : }
   87213                 : #endif
   87214                 : 
   87215                 : 
   87216                 : /*
   87217                 : ** An instance of the following structure holds the context of a
   87218                 : ** sum() or avg() aggregate computation.
   87219                 : */
   87220                 : typedef struct SumCtx SumCtx;
   87221                 : struct SumCtx {
   87222                 :   double rSum;      /* Floating point sum */
   87223                 :   i64 iSum;         /* Integer sum */   
   87224                 :   i64 cnt;          /* Number of elements summed */
   87225                 :   u8 overflow;      /* True if integer overflow seen */
   87226                 :   u8 approx;        /* True if non-integer value was input to the sum */
   87227                 : };
   87228                 : 
   87229                 : /*
   87230                 : ** Routines used to compute the sum, average, and total.
   87231                 : **
   87232                 : ** The SUM() function follows the (broken) SQL standard which means
   87233                 : ** that it returns NULL if it sums over no inputs.  TOTAL returns
   87234                 : ** 0.0 in that case.  In addition, TOTAL always returns a float where
   87235                 : ** SUM might return an integer if it never encounters a floating point
   87236                 : ** value.  TOTAL never fails, but SUM might through an exception if
   87237                 : ** it overflows an integer.
   87238                 : */
   87239           11499 : static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
   87240                 :   SumCtx *p;
   87241                 :   int type;
   87242           11499 :   assert( argc==1 );
   87243                 :   UNUSED_PARAMETER(argc);
   87244           11499 :   p = sqlite3_aggregate_context(context, sizeof(*p));
   87245           11499 :   type = sqlite3_value_numeric_type(argv[0]);
   87246           11499 :   if( p && type!=SQLITE_NULL ){
   87247           11480 :     p->cnt++;
   87248           11480 :     if( type==SQLITE_INTEGER ){
   87249           11480 :       i64 v = sqlite3_value_int64(argv[0]);
   87250           11480 :       p->rSum += v;
   87251           11480 :       if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
   87252               0 :         p->overflow = 1;
   87253                 :       }
   87254                 :     }else{
   87255               0 :       p->rSum += sqlite3_value_double(argv[0]);
   87256               0 :       p->approx = 1;
   87257                 :     }
   87258                 :   }
   87259           11499 : }
   87260             350 : static void sumFinalize(sqlite3_context *context){
   87261                 :   SumCtx *p;
   87262             350 :   p = sqlite3_aggregate_context(context, 0);
   87263             350 :   if( p && p->cnt>0 ){
   87264             327 :     if( p->overflow ){
   87265               0 :       sqlite3_result_error(context,"integer overflow",-1);
   87266             327 :     }else if( p->approx ){
   87267               0 :       sqlite3_result_double(context, p->rSum);
   87268                 :     }else{
   87269             327 :       sqlite3_result_int64(context, p->iSum);
   87270                 :     }
   87271                 :   }
   87272             350 : }
   87273               0 : static void avgFinalize(sqlite3_context *context){
   87274                 :   SumCtx *p;
   87275               0 :   p = sqlite3_aggregate_context(context, 0);
   87276               0 :   if( p && p->cnt>0 ){
   87277               0 :     sqlite3_result_double(context, p->rSum/(double)p->cnt);
   87278                 :   }
   87279               0 : }
   87280               0 : static void totalFinalize(sqlite3_context *context){
   87281                 :   SumCtx *p;
   87282               0 :   p = sqlite3_aggregate_context(context, 0);
   87283                 :   /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   87284               0 :   sqlite3_result_double(context, p ? p->rSum : (double)0);
   87285               0 : }
   87286                 : 
   87287                 : /*
   87288                 : ** The following structure keeps track of state information for the
   87289                 : ** count() aggregate function.
   87290                 : */
   87291                 : typedef struct CountCtx CountCtx;
   87292                 : struct CountCtx {
   87293                 :   i64 n;
   87294                 : };
   87295                 : 
   87296                 : /*
   87297                 : ** Routines to implement the count() aggregate function.
   87298                 : */
   87299          717629 : static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
   87300                 :   CountCtx *p;
   87301          717629 :   p = sqlite3_aggregate_context(context, sizeof(*p));
   87302          717629 :   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
   87303          717629 :     p->n++;
   87304                 :   }
   87305                 : 
   87306                 : #ifndef SQLITE_OMIT_DEPRECATED
   87307                 :   /* The sqlite3_aggregate_count() function is deprecated.  But just to make
   87308                 :   ** sure it still operates correctly, verify that its count agrees with our 
   87309                 :   ** internal count when using count(*) and when the total count can be
   87310                 :   ** expressed as a 32-bit integer. */
   87311          717629 :   assert( argc==1 || p==0 || p->n>0x7fffffff
   87312                 :           || p->n==sqlite3_aggregate_count(context) );
   87313                 : #endif
   87314          717629 : }   
   87315            7977 : static void countFinalize(sqlite3_context *context){
   87316                 :   CountCtx *p;
   87317            7977 :   p = sqlite3_aggregate_context(context, 0);
   87318            7977 :   sqlite3_result_int64(context, p ? p->n : 0);
   87319            7977 : }
   87320                 : 
   87321                 : /*
   87322                 : ** Routines to implement min() and max() aggregate functions.
   87323                 : */
   87324           43892 : static void minmaxStep(
   87325                 :   sqlite3_context *context, 
   87326                 :   int NotUsed, 
   87327                 :   sqlite3_value **argv
   87328                 : ){
   87329           43892 :   Mem *pArg  = (Mem *)argv[0];
   87330                 :   Mem *pBest;
   87331                 :   UNUSED_PARAMETER(NotUsed);
   87332                 : 
   87333           43892 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
   87334           43846 :   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
   87335           43846 :   if( !pBest ) return;
   87336                 : 
   87337           43846 :   if( pBest->flags ){
   87338                 :     int max;
   87339                 :     int cmp;
   87340           38715 :     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
   87341                 :     /* This step function is used for both the min() and max() aggregates,
   87342                 :     ** the only difference between the two being that the sense of the
   87343                 :     ** comparison is inverted. For the max() aggregate, the
   87344                 :     ** sqlite3_user_data() function returns (void *)-1. For min() it
   87345                 :     ** returns (void *)db, where db is the sqlite3* database pointer.
   87346                 :     ** Therefore the next statement sets variable 'max' to 1 for the max()
   87347                 :     ** aggregate, or 0 for min().
   87348                 :     */
   87349           38715 :     max = sqlite3_user_data(context)!=0;
   87350           38715 :     cmp = sqlite3MemCompare(pBest, pArg, pColl);
   87351           38715 :     if( (max && cmp<0) || (!max && cmp>0) ){
   87352             440 :       sqlite3VdbeMemCopy(pBest, pArg);
   87353                 :     }
   87354                 :   }else{
   87355            5131 :     sqlite3VdbeMemCopy(pBest, pArg);
   87356                 :   }
   87357                 : }
   87358            5486 : static void minMaxFinalize(sqlite3_context *context){
   87359                 :   sqlite3_value *pRes;
   87360            5486 :   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
   87361            5486 :   if( pRes ){
   87362            5131 :     if( ALWAYS(pRes->flags) ){
   87363            5131 :       sqlite3_result_value(context, pRes);
   87364                 :     }
   87365            5131 :     sqlite3VdbeMemRelease(pRes);
   87366                 :   }
   87367            5486 : }
   87368                 : 
   87369                 : /*
   87370                 : ** group_concat(EXPR, ?SEPARATOR?)
   87371                 : */
   87372            1382 : static void groupConcatStep(
   87373                 :   sqlite3_context *context,
   87374                 :   int argc,
   87375                 :   sqlite3_value **argv
   87376                 : ){
   87377                 :   const char *zVal;
   87378                 :   StrAccum *pAccum;
   87379                 :   const char *zSep;
   87380                 :   int nVal, nSep;
   87381            1382 :   assert( argc==1 || argc==2 );
   87382            1382 :   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
   87383            1382 :   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
   87384                 : 
   87385            1382 :   if( pAccum ){
   87386            1382 :     sqlite3 *db = sqlite3_context_db_handle(context);
   87387            1382 :     int firstTerm = pAccum->useMalloc==0;
   87388            1382 :     pAccum->useMalloc = 2;
   87389            1382 :     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
   87390            1382 :     if( !firstTerm ){
   87391             169 :       if( argc==2 ){
   87392             169 :         zSep = (char*)sqlite3_value_text(argv[1]);
   87393             169 :         nSep = sqlite3_value_bytes(argv[1]);
   87394                 :       }else{
   87395               0 :         zSep = ",";
   87396               0 :         nSep = 1;
   87397                 :       }
   87398             169 :       sqlite3StrAccumAppend(pAccum, zSep, nSep);
   87399                 :     }
   87400            1382 :     zVal = (char*)sqlite3_value_text(argv[0]);
   87401            1382 :     nVal = sqlite3_value_bytes(argv[0]);
   87402            1382 :     sqlite3StrAccumAppend(pAccum, zVal, nVal);
   87403                 :   }
   87404                 : }
   87405            3442 : static void groupConcatFinalize(sqlite3_context *context){
   87406                 :   StrAccum *pAccum;
   87407            3442 :   pAccum = sqlite3_aggregate_context(context, 0);
   87408            3442 :   if( pAccum ){
   87409            1213 :     if( pAccum->tooBig ){
   87410               0 :       sqlite3_result_error_toobig(context);
   87411            1213 :     }else if( pAccum->mallocFailed ){
   87412               0 :       sqlite3_result_error_nomem(context);
   87413                 :     }else{    
   87414            1213 :       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, 
   87415                 :                           sqlite3_free);
   87416                 :     }
   87417                 :   }
   87418            3442 : }
   87419                 : 
   87420                 : /*
   87421                 : ** This routine does per-connection function registration.  Most
   87422                 : ** of the built-in functions above are part of the global function set.
   87423                 : ** This routine only deals with those that are not global.
   87424                 : */
   87425            3277 : SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
   87426            3277 :   int rc = sqlite3_overload_function(db, "MATCH", 2);
   87427            3277 :   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
   87428            3277 :   if( rc==SQLITE_NOMEM ){
   87429               0 :     db->mallocFailed = 1;
   87430                 :   }
   87431            3277 : }
   87432                 : 
   87433                 : /*
   87434                 : ** Set the LIKEOPT flag on the 2-argument function with the given name.
   87435                 : */
   87436               0 : static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
   87437                 :   FuncDef *pDef;
   87438               0 :   pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
   87439                 :                              2, SQLITE_UTF8, 0);
   87440               0 :   if( ALWAYS(pDef) ){
   87441               0 :     pDef->flags = flagVal;
   87442                 :   }
   87443               0 : }
   87444                 : 
   87445                 : /*
   87446                 : ** Register the built-in LIKE and GLOB functions.  The caseSensitive
   87447                 : ** parameter determines whether or not the LIKE operator is case
   87448                 : ** sensitive.  GLOB is always case sensitive.
   87449                 : */
   87450               0 : SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
   87451                 :   struct compareInfo *pInfo;
   87452               0 :   if( caseSensitive ){
   87453               0 :     pInfo = (struct compareInfo*)&likeInfoAlt;
   87454                 :   }else{
   87455               0 :     pInfo = (struct compareInfo*)&likeInfoNorm;
   87456                 :   }
   87457               0 :   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
   87458               0 :   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
   87459               0 :   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
   87460                 :       (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
   87461               0 :   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
   87462               0 :   setLikeOptFlag(db, "like", 
   87463                 :       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
   87464               0 : }
   87465                 : 
   87466                 : /*
   87467                 : ** pExpr points to an expression which implements a function.  If
   87468                 : ** it is appropriate to apply the LIKE optimization to that function
   87469                 : ** then set aWc[0] through aWc[2] to the wildcard characters and
   87470                 : ** return TRUE.  If the function is not a LIKE-style function then
   87471                 : ** return FALSE.
   87472                 : */
   87473           97401 : SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
   87474                 :   FuncDef *pDef;
   87475           97401 :   if( pExpr->op!=TK_FUNCTION 
   87476             424 :    || !pExpr->x.pList 
   87477             424 :    || pExpr->x.pList->nExpr!=2
   87478                 :   ){
   87479           97179 :     return 0;
   87480                 :   }
   87481             222 :   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   87482             222 :   pDef = sqlite3FindFunction(db, pExpr->u.zToken, 
   87483             222 :                              sqlite3Strlen30(pExpr->u.zToken),
   87484                 :                              2, SQLITE_UTF8, 0);
   87485             222 :   if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
   87486              12 :     return 0;
   87487                 :   }
   87488                 : 
   87489                 :   /* The memcpy() statement assumes that the wildcard characters are
   87490                 :   ** the first three statements in the compareInfo structure.  The
   87491                 :   ** asserts() that follow verify that assumption
   87492                 :   */
   87493             210 :   memcpy(aWc, pDef->pUserData, 3);
   87494                 :   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
   87495             210 :   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
   87496             210 :   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
   87497             210 :   *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0;
   87498             210 :   return 1;
   87499                 : }
   87500                 : 
   87501                 : /*
   87502                 : ** All all of the FuncDef structures in the aBuiltinFunc[] array above
   87503                 : ** to the global function hash table.  This occurs at start-time (as
   87504                 : ** a consequence of calling sqlite3_initialize()).
   87505                 : **
   87506                 : ** After this routine runs
   87507                 : */
   87508             806 : SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
   87509                 :   /*
   87510                 :   ** The following array holds FuncDef structures for all of the functions
   87511                 :   ** defined in this file.
   87512                 :   **
   87513                 :   ** The array cannot be constant since changes are made to the
   87514                 :   ** FuncDef.pHash elements at start-time.  The elements of this array
   87515                 :   ** are read-only after initialization is complete.
   87516                 :   */
   87517                 :   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
   87518                 :     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
   87519                 :     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
   87520                 :     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
   87521                 :     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
   87522                 :     FUNCTION(trim,               1, 3, 0, trimFunc         ),
   87523                 :     FUNCTION(trim,               2, 3, 0, trimFunc         ),
   87524                 :     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
   87525                 :     FUNCTION(min,                0, 0, 1, 0                ),
   87526                 :     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
   87527                 :     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
   87528                 :     FUNCTION(max,                0, 1, 1, 0                ),
   87529                 :     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
   87530                 :     FUNCTION(typeof,             1, 0, 0, typeofFunc       ),
   87531                 :     FUNCTION(length,             1, 0, 0, lengthFunc       ),
   87532                 :     FUNCTION(substr,             2, 0, 0, substrFunc       ),
   87533                 :     FUNCTION(substr,             3, 0, 0, substrFunc       ),
   87534                 :     FUNCTION(abs,                1, 0, 0, absFunc          ),
   87535                 : #ifndef SQLITE_OMIT_FLOATING_POINT
   87536                 :     FUNCTION(round,              1, 0, 0, roundFunc        ),
   87537                 :     FUNCTION(round,              2, 0, 0, roundFunc        ),
   87538                 : #endif
   87539                 :     FUNCTION(upper,              1, 0, 0, upperFunc        ),
   87540                 :     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
   87541                 :     FUNCTION(coalesce,           1, 0, 0, 0                ),
   87542                 :     FUNCTION(coalesce,           0, 0, 0, 0                ),
   87543                 : /*  FUNCTION(coalesce,          -1, 0, 0, ifnullFunc       ), */
   87544                 :     {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0},
   87545                 :     FUNCTION(hex,                1, 0, 0, hexFunc          ),
   87546                 : /*  FUNCTION(ifnull,             2, 0, 0, ifnullFunc       ), */
   87547                 :     {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0},
   87548                 :     FUNCTION(random,             0, 0, 0, randomFunc       ),
   87549                 :     FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
   87550                 :     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
   87551                 :     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
   87552                 :     FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
   87553                 :     FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
   87554                 : #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   87555                 :     FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
   87556                 :     FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
   87557                 : #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
   87558                 :     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
   87559                 :     FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
   87560                 :     FUNCTION(changes,            0, 0, 0, changes          ),
   87561                 :     FUNCTION(total_changes,      0, 0, 0, total_changes    ),
   87562                 :     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
   87563                 :     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
   87564                 :   #ifdef SQLITE_SOUNDEX
   87565                 :     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
   87566                 :   #endif
   87567                 :   #ifndef SQLITE_OMIT_LOAD_EXTENSION
   87568                 :     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
   87569                 :     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
   87570                 :   #endif
   87571                 :     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
   87572                 :     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
   87573                 :     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
   87574                 :  /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
   87575                 :     {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
   87576                 :     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
   87577                 :     AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
   87578                 :     AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
   87579                 :   
   87580                 :     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
   87581                 :   #ifdef SQLITE_CASE_SENSITIVE_LIKE
   87582                 :     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
   87583                 :     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
   87584                 :   #else
   87585                 :     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
   87586                 :     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
   87587                 :   #endif
   87588                 :   };
   87589                 : 
   87590                 :   int i;
   87591             806 :   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
   87592             806 :   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
   87593                 : 
   87594           42718 :   for(i=0; i<ArraySize(aBuiltinFunc); i++){
   87595           41912 :     sqlite3FuncDefInsert(pHash, &aFunc[i]);
   87596                 :   }
   87597             806 :   sqlite3RegisterDateTimeFunctions();
   87598                 : #ifndef SQLITE_OMIT_ALTERTABLE
   87599             806 :   sqlite3AlterFunctions();
   87600                 : #endif
   87601             806 : }
   87602                 : 
   87603                 : /************** End of func.c ************************************************/
   87604                 : /************** Begin file fkey.c ********************************************/
   87605                 : /*
   87606                 : **
   87607                 : ** The author disclaims copyright to this source code.  In place of
   87608                 : ** a legal notice, here is a blessing:
   87609                 : **
   87610                 : **    May you do good and not evil.
   87611                 : **    May you find forgiveness for yourself and forgive others.
   87612                 : **    May you share freely, never taking more than you give.
   87613                 : **
   87614                 : *************************************************************************
   87615                 : ** This file contains code used by the compiler to add foreign key
   87616                 : ** support to compiled SQL statements.
   87617                 : */
   87618                 : 
   87619                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   87620                 : #ifndef SQLITE_OMIT_TRIGGER
   87621                 : 
   87622                 : /*
   87623                 : ** Deferred and Immediate FKs
   87624                 : ** --------------------------
   87625                 : **
   87626                 : ** Foreign keys in SQLite come in two flavours: deferred and immediate.
   87627                 : ** If an immediate foreign key constraint is violated, SQLITE_CONSTRAINT
   87628                 : ** is returned and the current statement transaction rolled back. If a 
   87629                 : ** deferred foreign key constraint is violated, no action is taken 
   87630                 : ** immediately. However if the application attempts to commit the 
   87631                 : ** transaction before fixing the constraint violation, the attempt fails.
   87632                 : **
   87633                 : ** Deferred constraints are implemented using a simple counter associated
   87634                 : ** with the database handle. The counter is set to zero each time a 
   87635                 : ** database transaction is opened. Each time a statement is executed 
   87636                 : ** that causes a foreign key violation, the counter is incremented. Each
   87637                 : ** time a statement is executed that removes an existing violation from
   87638                 : ** the database, the counter is decremented. When the transaction is
   87639                 : ** committed, the commit fails if the current value of the counter is
   87640                 : ** greater than zero. This scheme has two big drawbacks:
   87641                 : **
   87642                 : **   * When a commit fails due to a deferred foreign key constraint, 
   87643                 : **     there is no way to tell which foreign constraint is not satisfied,
   87644                 : **     or which row it is not satisfied for.
   87645                 : **
   87646                 : **   * If the database contains foreign key violations when the 
   87647                 : **     transaction is opened, this may cause the mechanism to malfunction.
   87648                 : **
   87649                 : ** Despite these problems, this approach is adopted as it seems simpler
   87650                 : ** than the alternatives.
   87651                 : **
   87652                 : ** INSERT operations:
   87653                 : **
   87654                 : **   I.1) For each FK for which the table is the child table, search
   87655                 : **        the parent table for a match. If none is found increment the
   87656                 : **        constraint counter.
   87657                 : **
   87658                 : **   I.2) For each FK for which the table is the parent table, 
   87659                 : **        search the child table for rows that correspond to the new
   87660                 : **        row in the parent table. Decrement the counter for each row
   87661                 : **        found (as the constraint is now satisfied).
   87662                 : **
   87663                 : ** DELETE operations:
   87664                 : **
   87665                 : **   D.1) For each FK for which the table is the child table, 
   87666                 : **        search the parent table for a row that corresponds to the 
   87667                 : **        deleted row in the child table. If such a row is not found, 
   87668                 : **        decrement the counter.
   87669                 : **
   87670                 : **   D.2) For each FK for which the table is the parent table, search 
   87671                 : **        the child table for rows that correspond to the deleted row 
   87672                 : **        in the parent table. For each found increment the counter.
   87673                 : **
   87674                 : ** UPDATE operations:
   87675                 : **
   87676                 : **   An UPDATE command requires that all 4 steps above are taken, but only
   87677                 : **   for FK constraints for which the affected columns are actually 
   87678                 : **   modified (values must be compared at runtime).
   87679                 : **
   87680                 : ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
   87681                 : ** This simplifies the implementation a bit.
   87682                 : **
   87683                 : ** For the purposes of immediate FK constraints, the OR REPLACE conflict
   87684                 : ** resolution is considered to delete rows before the new row is inserted.
   87685                 : ** If a delete caused by OR REPLACE violates an FK constraint, an exception
   87686                 : ** is thrown, even if the FK constraint would be satisfied after the new 
   87687                 : ** row is inserted.
   87688                 : **
   87689                 : ** Immediate constraints are usually handled similarly. The only difference 
   87690                 : ** is that the counter used is stored as part of each individual statement
   87691                 : ** object (struct Vdbe). If, after the statement has run, its immediate
   87692                 : ** constraint counter is greater than zero, it returns SQLITE_CONSTRAINT
   87693                 : ** and the statement transaction is rolled back. An exception is an INSERT
   87694                 : ** statement that inserts a single row only (no triggers). In this case,
   87695                 : ** instead of using a counter, an exception is thrown immediately if the
   87696                 : ** INSERT violates a foreign key constraint. This is necessary as such
   87697                 : ** an INSERT does not open a statement transaction.
   87698                 : **
   87699                 : ** TODO: How should dropping a table be handled? How should renaming a 
   87700                 : ** table be handled?
   87701                 : **
   87702                 : **
   87703                 : ** Query API Notes
   87704                 : ** ---------------
   87705                 : **
   87706                 : ** Before coding an UPDATE or DELETE row operation, the code-generator
   87707                 : ** for those two operations needs to know whether or not the operation
   87708                 : ** requires any FK processing and, if so, which columns of the original
   87709                 : ** row are required by the FK processing VDBE code (i.e. if FKs were
   87710                 : ** implemented using triggers, which of the old.* columns would be 
   87711                 : ** accessed). No information is required by the code-generator before
   87712                 : ** coding an INSERT operation. The functions used by the UPDATE/DELETE
   87713                 : ** generation code to query for this information are:
   87714                 : **
   87715                 : **   sqlite3FkRequired() - Test to see if FK processing is required.
   87716                 : **   sqlite3FkOldmask()  - Query for the set of required old.* columns.
   87717                 : **
   87718                 : **
   87719                 : ** Externally accessible module functions
   87720                 : ** --------------------------------------
   87721                 : **
   87722                 : **   sqlite3FkCheck()    - Check for foreign key violations.
   87723                 : **   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
   87724                 : **   sqlite3FkDelete()   - Delete an FKey structure.
   87725                 : */
   87726                 : 
   87727                 : /*
   87728                 : ** VDBE Calling Convention
   87729                 : ** -----------------------
   87730                 : **
   87731                 : ** Example:
   87732                 : **
   87733                 : **   For the following INSERT statement:
   87734                 : **
   87735                 : **     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
   87736                 : **     INSERT INTO t1 VALUES(1, 2, 3.1);
   87737                 : **
   87738                 : **   Register (x):        2    (type integer)
   87739                 : **   Register (x+1):      1    (type integer)
   87740                 : **   Register (x+2):      NULL (type NULL)
   87741                 : **   Register (x+3):      3.1  (type real)
   87742                 : */
   87743                 : 
   87744                 : /*
   87745                 : ** A foreign key constraint requires that the key columns in the parent
   87746                 : ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
   87747                 : ** Given that pParent is the parent table for foreign key constraint pFKey, 
   87748                 : ** search the schema a unique index on the parent key columns. 
   87749                 : **
   87750                 : ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY 
   87751                 : ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx 
   87752                 : ** is set to point to the unique index. 
   87753                 : ** 
   87754                 : ** If the parent key consists of a single column (the foreign key constraint
   87755                 : ** is not a composite foreign key), output variable *paiCol is set to NULL.
   87756                 : ** Otherwise, it is set to point to an allocated array of size N, where
   87757                 : ** N is the number of columns in the parent key. The first element of the
   87758                 : ** array is the index of the child table column that is mapped by the FK
   87759                 : ** constraint to the parent table column stored in the left-most column
   87760                 : ** of index *ppIdx. The second element of the array is the index of the
   87761                 : ** child table column that corresponds to the second left-most column of
   87762                 : ** *ppIdx, and so on.
   87763                 : **
   87764                 : ** If the required index cannot be found, either because:
   87765                 : **
   87766                 : **   1) The named parent key columns do not exist, or
   87767                 : **
   87768                 : **   2) The named parent key columns do exist, but are not subject to a
   87769                 : **      UNIQUE or PRIMARY KEY constraint, or
   87770                 : **
   87771                 : **   3) No parent key columns were provided explicitly as part of the
   87772                 : **      foreign key definition, and the parent table does not have a
   87773                 : **      PRIMARY KEY, or
   87774                 : **
   87775                 : **   4) No parent key columns were provided explicitly as part of the
   87776                 : **      foreign key definition, and the PRIMARY KEY of the parent table 
   87777                 : **      consists of a a different number of columns to the child key in 
   87778                 : **      the child table.
   87779                 : **
   87780                 : ** then non-zero is returned, and a "foreign key mismatch" error loaded
   87781                 : ** into pParse. If an OOM error occurs, non-zero is returned and the
   87782                 : ** pParse->db->mallocFailed flag is set.
   87783                 : */
   87784            2010 : static int locateFkeyIndex(
   87785                 :   Parse *pParse,                  /* Parse context to store any error in */
   87786                 :   Table *pParent,                 /* Parent table of FK constraint pFKey */
   87787                 :   FKey *pFKey,                    /* Foreign key to find index for */
   87788                 :   Index **ppIdx,                  /* OUT: Unique index on parent table */
   87789                 :   int **paiCol                    /* OUT: Map of index columns in pFKey */
   87790                 : ){
   87791            2010 :   Index *pIdx = 0;                    /* Value to return via *ppIdx */
   87792            2010 :   int *aiCol = 0;                     /* Value to return via *paiCol */
   87793            2010 :   int nCol = pFKey->nCol;             /* Number of columns in parent key */
   87794            2010 :   char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
   87795                 : 
   87796                 :   /* The caller is responsible for zeroing output parameters. */
   87797            2010 :   assert( ppIdx && *ppIdx==0 );
   87798            2010 :   assert( !paiCol || *paiCol==0 );
   87799            2010 :   assert( pParse );
   87800                 : 
   87801                 :   /* If this is a non-composite (single column) foreign key, check if it 
   87802                 :   ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx 
   87803                 :   ** and *paiCol set to zero and return early. 
   87804                 :   **
   87805                 :   ** Otherwise, for a composite foreign key (more than one column), allocate
   87806                 :   ** space for the aiCol array (returned via output parameter *paiCol).
   87807                 :   ** Non-composite foreign keys do not require the aiCol array.
   87808                 :   */
   87809            2010 :   if( nCol==1 ){
   87810                 :     /* The FK maps to the IPK if any of the following are true:
   87811                 :     **
   87812                 :     **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly 
   87813                 :     **      mapped to the primary key of table pParent, or
   87814                 :     **   2) The FK is explicitly mapped to a column declared as INTEGER
   87815                 :     **      PRIMARY KEY.
   87816                 :     */
   87817            2010 :     if( pParent->iPKey>=0 ){
   87818            2010 :       if( !zKey ) return 0;
   87819            2010 :       if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
   87820                 :     }
   87821               0 :   }else if( paiCol ){
   87822               0 :     assert( nCol>1 );
   87823               0 :     aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
   87824               0 :     if( !aiCol ) return 1;
   87825               0 :     *paiCol = aiCol;
   87826                 :   }
   87827                 : 
   87828               0 :   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
   87829               0 :     if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ 
   87830                 :       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
   87831                 :       ** of columns. If each indexed column corresponds to a foreign key
   87832                 :       ** column of pFKey, then this index is a winner.  */
   87833                 : 
   87834               0 :       if( zKey==0 ){
   87835                 :         /* If zKey is NULL, then this foreign key is implicitly mapped to 
   87836                 :         ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 
   87837                 :         ** identified by the test (Index.autoIndex==2).  */
   87838               0 :         if( pIdx->autoIndex==2 ){
   87839               0 :           if( aiCol ){
   87840                 :             int i;
   87841               0 :             for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
   87842                 :           }
   87843               0 :           break;
   87844                 :         }
   87845                 :       }else{
   87846                 :         /* If zKey is non-NULL, then this foreign key was declared to
   87847                 :         ** map to an explicit list of columns in table pParent. Check if this
   87848                 :         ** index matches those columns. Also, check that the index uses
   87849                 :         ** the default collation sequences for each column. */
   87850                 :         int i, j;
   87851               0 :         for(i=0; i<nCol; i++){
   87852               0 :           int iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
   87853                 :           char *zDfltColl;                  /* Def. collation for column */
   87854                 :           char *zIdxCol;                    /* Name of indexed column */
   87855                 : 
   87856                 :           /* If the index uses a collation sequence that is different from
   87857                 :           ** the default collation sequence for the column, this index is
   87858                 :           ** unusable. Bail out early in this case.  */
   87859               0 :           zDfltColl = pParent->aCol[iCol].zColl;
   87860               0 :           if( !zDfltColl ){
   87861               0 :             zDfltColl = "BINARY";
   87862                 :           }
   87863               0 :           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
   87864                 : 
   87865               0 :           zIdxCol = pParent->aCol[iCol].zName;
   87866               0 :           for(j=0; j<nCol; j++){
   87867               0 :             if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
   87868               0 :               if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
   87869               0 :               break;
   87870                 :             }
   87871                 :           }
   87872               0 :           if( j==nCol ) break;
   87873                 :         }
   87874               0 :         if( i==nCol ) break;      /* pIdx is usable */
   87875                 :       }
   87876                 :     }
   87877                 :   }
   87878                 : 
   87879               0 :   if( !pIdx ){
   87880               0 :     if( !pParse->disableTriggers ){
   87881               0 :       sqlite3ErrorMsg(pParse, "foreign key mismatch");
   87882                 :     }
   87883               0 :     sqlite3DbFree(pParse->db, aiCol);
   87884               0 :     return 1;
   87885                 :   }
   87886                 : 
   87887               0 :   *ppIdx = pIdx;
   87888               0 :   return 0;
   87889                 : }
   87890                 : 
   87891                 : /*
   87892                 : ** This function is called when a row is inserted into or deleted from the 
   87893                 : ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed 
   87894                 : ** on the child table of pFKey, this function is invoked twice for each row
   87895                 : ** affected - once to "delete" the old row, and then again to "insert" the
   87896                 : ** new row.
   87897                 : **
   87898                 : ** Each time it is called, this function generates VDBE code to locate the
   87899                 : ** row in the parent table that corresponds to the row being inserted into 
   87900                 : ** or deleted from the child table. If the parent row can be found, no 
   87901                 : ** special action is taken. Otherwise, if the parent row can *not* be
   87902                 : ** found in the parent table:
   87903                 : **
   87904                 : **   Operation | FK type   | Action taken
   87905                 : **   --------------------------------------------------------------------------
   87906                 : **   INSERT      immediate   Increment the "immediate constraint counter".
   87907                 : **
   87908                 : **   DELETE      immediate   Decrement the "immediate constraint counter".
   87909                 : **
   87910                 : **   INSERT      deferred    Increment the "deferred constraint counter".
   87911                 : **
   87912                 : **   DELETE      deferred    Decrement the "deferred constraint counter".
   87913                 : **
   87914                 : ** These operations are identified in the comment at the top of this file 
   87915                 : ** (fkey.c) as "I.1" and "D.1".
   87916                 : */
   87917             904 : static void fkLookupParent(
   87918                 :   Parse *pParse,        /* Parse context */
   87919                 :   int iDb,              /* Index of database housing pTab */
   87920                 :   Table *pTab,          /* Parent table of FK pFKey */
   87921                 :   Index *pIdx,          /* Unique index on parent key columns in pTab */
   87922                 :   FKey *pFKey,          /* Foreign key constraint */
   87923                 :   int *aiCol,           /* Map from parent key columns to child table columns */
   87924                 :   int regData,          /* Address of array containing child table row */
   87925                 :   int nIncr,            /* Increment constraint counter by this */
   87926                 :   int isIgnore          /* If true, pretend pTab contains all NULL values */
   87927                 : ){
   87928                 :   int i;                                    /* Iterator variable */
   87929             904 :   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
   87930             904 :   int iCur = pParse->nTab - 1;              /* Cursor number to use */
   87931             904 :   int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
   87932                 : 
   87933                 :   /* If nIncr is less than zero, then check at runtime if there are any
   87934                 :   ** outstanding constraints to resolve. If there are not, there is no need
   87935                 :   ** to check if deleting this row resolves any outstanding violations.
   87936                 :   **
   87937                 :   ** Check if any of the key columns in the child table row are NULL. If 
   87938                 :   ** any are, then the constraint is considered satisfied. No need to 
   87939                 :   ** search for a matching row in the parent table.  */
   87940             904 :   if( nIncr<0 ){
   87941             421 :     sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
   87942                 :   }
   87943            1808 :   for(i=0; i<pFKey->nCol; i++){
   87944             904 :     int iReg = aiCol[i] + regData + 1;
   87945             904 :     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
   87946                 :   }
   87947                 : 
   87948             904 :   if( isIgnore==0 ){
   87949             904 :     if( pIdx==0 ){
   87950                 :       /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
   87951                 :       ** column of the parent table (table pTab).  */
   87952                 :       int iMustBeInt;               /* Address of MustBeInt instruction */
   87953             904 :       int regTemp = sqlite3GetTempReg(pParse);
   87954                 :   
   87955                 :       /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
   87956                 :       ** apply the affinity of the parent key). If this fails, then there
   87957                 :       ** is no matching parent key. Before using MustBeInt, make a copy of
   87958                 :       ** the value. Otherwise, the value inserted into the child key column
   87959                 :       ** will have INTEGER affinity applied to it, which may not be correct.  */
   87960             904 :       sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
   87961             904 :       iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
   87962                 :   
   87963                 :       /* If the parent table is the same as the child table, and we are about
   87964                 :       ** to increment the constraint-counter (i.e. this is an INSERT operation),
   87965                 :       ** then check if the row being inserted matches itself. If so, do not
   87966                 :       ** increment the constraint-counter.  */
   87967             904 :       if( pTab==pFKey->pFrom && nIncr==1 ){
   87968               0 :         sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
   87969                 :       }
   87970                 :   
   87971             904 :       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
   87972             904 :       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
   87973             904 :       sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
   87974             904 :       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
   87975             904 :       sqlite3VdbeJumpHere(v, iMustBeInt);
   87976             904 :       sqlite3ReleaseTempReg(pParse, regTemp);
   87977                 :     }else{
   87978               0 :       int nCol = pFKey->nCol;
   87979               0 :       int regTemp = sqlite3GetTempRange(pParse, nCol);
   87980               0 :       int regRec = sqlite3GetTempReg(pParse);
   87981               0 :       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   87982                 :   
   87983               0 :       sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
   87984               0 :       sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
   87985               0 :       for(i=0; i<nCol; i++){
   87986               0 :         sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
   87987                 :       }
   87988                 :   
   87989                 :       /* If the parent table is the same as the child table, and we are about
   87990                 :       ** to increment the constraint-counter (i.e. this is an INSERT operation),
   87991                 :       ** then check if the row being inserted matches itself. If so, do not
   87992                 :       ** increment the constraint-counter. 
   87993                 :       **
   87994                 :       ** If any of the parent-key values are NULL, then the row cannot match 
   87995                 :       ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
   87996                 :       ** of the parent-key values are NULL (at this point it is known that
   87997                 :       ** none of the child key values are).
   87998                 :       */
   87999               0 :       if( pTab==pFKey->pFrom && nIncr==1 ){
   88000               0 :         int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
   88001               0 :         for(i=0; i<nCol; i++){
   88002               0 :           int iChild = aiCol[i]+1+regData;
   88003               0 :           int iParent = pIdx->aiColumn[i]+1+regData;
   88004               0 :           assert( aiCol[i]!=pTab->iPKey );
   88005               0 :           if( pIdx->aiColumn[i]==pTab->iPKey ){
   88006                 :             /* The parent key is a composite key that includes the IPK column */
   88007               0 :             iParent = regData;
   88008                 :           }
   88009               0 :           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
   88010               0 :           sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
   88011                 :         }
   88012               0 :         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
   88013                 :       }
   88014                 :   
   88015               0 :       sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
   88016               0 :       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
   88017               0 :       sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
   88018                 :   
   88019               0 :       sqlite3ReleaseTempReg(pParse, regRec);
   88020               0 :       sqlite3ReleaseTempRange(pParse, regTemp, nCol);
   88021                 :     }
   88022                 :   }
   88023                 : 
   88024             904 :   if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
   88025                 :     /* Special case: If this is an INSERT statement that will insert exactly
   88026                 :     ** one row into the table, raise a constraint immediately instead of
   88027                 :     ** incrementing a counter. This is necessary as the VM code is being
   88028                 :     ** generated for will not open a statement transaction.  */
   88029             146 :     assert( nIncr==1 );
   88030             146 :     sqlite3HaltConstraint(
   88031                 :         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
   88032                 :     );
   88033                 :   }else{
   88034             758 :     if( nIncr>0 && pFKey->isDeferred==0 ){
   88035             337 :       sqlite3ParseToplevel(pParse)->mayAbort = 1;
   88036                 :     }
   88037             758 :     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
   88038                 :   }
   88039                 : 
   88040             904 :   sqlite3VdbeResolveLabel(v, iOk);
   88041             904 :   sqlite3VdbeAddOp1(v, OP_Close, iCur);
   88042             904 : }
   88043                 : 
   88044                 : /*
   88045                 : ** This function is called to generate code executed when a row is deleted
   88046                 : ** from the parent table of foreign key constraint pFKey and, if pFKey is 
   88047                 : ** deferred, when a row is inserted into the same table. When generating
   88048                 : ** code for an SQL UPDATE operation, this function may be called twice -
   88049                 : ** once to "delete" the old row and once to "insert" the new row.
   88050                 : **
   88051                 : ** The code generated by this function scans through the rows in the child
   88052                 : ** table that correspond to the parent table row being deleted or inserted.
   88053                 : ** For each child row found, one of the following actions is taken:
   88054                 : **
   88055                 : **   Operation | FK type   | Action taken
   88056                 : **   --------------------------------------------------------------------------
   88057                 : **   DELETE      immediate   Increment the "immediate constraint counter".
   88058                 : **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
   88059                 : **                           throw a "foreign key constraint failed" exception.
   88060                 : **
   88061                 : **   INSERT      immediate   Decrement the "immediate constraint counter".
   88062                 : **
   88063                 : **   DELETE      deferred    Increment the "deferred constraint counter".
   88064                 : **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
   88065                 : **                           throw a "foreign key constraint failed" exception.
   88066                 : **
   88067                 : **   INSERT      deferred    Decrement the "deferred constraint counter".
   88068                 : **
   88069                 : ** These operations are identified in the comment at the top of this file 
   88070                 : ** (fkey.c) as "I.2" and "D.2".
   88071                 : */
   88072             828 : static void fkScanChildren(
   88073                 :   Parse *pParse,                  /* Parse context */
   88074                 :   SrcList *pSrc,                  /* SrcList containing the table to scan */
   88075                 :   Table *pTab,
   88076                 :   Index *pIdx,                    /* Foreign key index */
   88077                 :   FKey *pFKey,                    /* Foreign key relationship */
   88078                 :   int *aiCol,                     /* Map from pIdx cols to child table cols */
   88079                 :   int regData,                    /* Referenced table data starts here */
   88080                 :   int nIncr                       /* Amount to increment deferred counter by */
   88081                 : ){
   88082             828 :   sqlite3 *db = pParse->db;       /* Database handle */
   88083                 :   int i;                          /* Iterator variable */
   88084             828 :   Expr *pWhere = 0;               /* WHERE clause to scan with */
   88085                 :   NameContext sNameContext;       /* Context used to resolve WHERE clause */
   88086                 :   WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
   88087             828 :   int iFkIfZero = 0;              /* Address of OP_FkIfZero */
   88088             828 :   Vdbe *v = sqlite3GetVdbe(pParse);
   88089                 : 
   88090             828 :   assert( !pIdx || pIdx->pTable==pTab );
   88091                 : 
   88092             828 :   if( nIncr<0 ){
   88093             674 :     iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
   88094                 :   }
   88095                 : 
   88096                 :   /* Create an Expr object representing an SQL expression like:
   88097                 :   **
   88098                 :   **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
   88099                 :   **
   88100                 :   ** The collation sequence used for the comparison should be that of
   88101                 :   ** the parent key columns. The affinity of the parent key column should
   88102                 :   ** be applied to each child key value before the comparison takes place.
   88103                 :   */
   88104            1656 :   for(i=0; i<pFKey->nCol; i++){
   88105                 :     Expr *pLeft;                  /* Value from parent table row */
   88106                 :     Expr *pRight;                 /* Column ref to child table */
   88107                 :     Expr *pEq;                    /* Expression (pLeft = pRight) */
   88108                 :     int iCol;                     /* Index of column in child table */ 
   88109                 :     const char *zCol;             /* Name of column in child table */
   88110                 : 
   88111             828 :     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
   88112             828 :     if( pLeft ){
   88113                 :       /* Set the collation sequence and affinity of the LHS of each TK_EQ
   88114                 :       ** expression to the parent key column defaults.  */
   88115             828 :       if( pIdx ){
   88116                 :         Column *pCol;
   88117               0 :         iCol = pIdx->aiColumn[i];
   88118               0 :         pCol = &pTab->aCol[iCol];
   88119               0 :         if( pTab->iPKey==iCol ) iCol = -1;
   88120               0 :         pLeft->iTable = regData+iCol+1;
   88121               0 :         pLeft->affinity = pCol->affinity;
   88122               0 :         pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl);
   88123                 :       }else{
   88124             828 :         pLeft->iTable = regData;
   88125             828 :         pLeft->affinity = SQLITE_AFF_INTEGER;
   88126                 :       }
   88127                 :     }
   88128             828 :     iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
   88129             828 :     assert( iCol>=0 );
   88130             828 :     zCol = pFKey->pFrom->aCol[iCol].zName;
   88131             828 :     pRight = sqlite3Expr(db, TK_ID, zCol);
   88132             828 :     pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
   88133             828 :     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
   88134                 :   }
   88135                 : 
   88136                 :   /* If the child table is the same as the parent table, and this scan
   88137                 :   ** is taking place as part of a DELETE operation (operation D.2), omit the
   88138                 :   ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE 
   88139                 :   ** clause, where $rowid is the rowid of the row being deleted.  */
   88140             828 :   if( pTab==pFKey->pFrom && nIncr>0 ){
   88141                 :     Expr *pEq;                    /* Expression (pLeft = pRight) */
   88142                 :     Expr *pLeft;                  /* Value from parent table row */
   88143                 :     Expr *pRight;                 /* Column ref to child table */
   88144               0 :     pLeft = sqlite3Expr(db, TK_REGISTER, 0);
   88145               0 :     pRight = sqlite3Expr(db, TK_COLUMN, 0);
   88146               0 :     if( pLeft && pRight ){
   88147               0 :       pLeft->iTable = regData;
   88148               0 :       pLeft->affinity = SQLITE_AFF_INTEGER;
   88149               0 :       pRight->iTable = pSrc->a[0].iCursor;
   88150               0 :       pRight->iColumn = -1;
   88151                 :     }
   88152               0 :     pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
   88153               0 :     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
   88154                 :   }
   88155                 : 
   88156                 :   /* Resolve the references in the WHERE clause. */
   88157             828 :   memset(&sNameContext, 0, sizeof(NameContext));
   88158             828 :   sNameContext.pSrcList = pSrc;
   88159             828 :   sNameContext.pParse = pParse;
   88160             828 :   sqlite3ResolveExprNames(&sNameContext, pWhere);
   88161                 : 
   88162                 :   /* Create VDBE to loop through the entries in pSrc that match the WHERE
   88163                 :   ** clause. If the constraint is not deferred, throw an exception for
   88164                 :   ** each row found. Otherwise, for deferred constraints, increment the
   88165                 :   ** deferred constraint counter by nIncr for each row selected.  */
   88166             828 :   pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0);
   88167             828 :   if( nIncr>0 && pFKey->isDeferred==0 ){
   88168             154 :     sqlite3ParseToplevel(pParse)->mayAbort = 1;
   88169                 :   }
   88170             828 :   sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
   88171             828 :   if( pWInfo ){
   88172             828 :     sqlite3WhereEnd(pWInfo);
   88173                 :   }
   88174                 : 
   88175                 :   /* Clean up the WHERE clause constructed above. */
   88176             828 :   sqlite3ExprDelete(db, pWhere);
   88177             828 :   if( iFkIfZero ){
   88178             674 :     sqlite3VdbeJumpHere(v, iFkIfZero);
   88179                 :   }
   88180             828 : }
   88181                 : 
   88182                 : /*
   88183                 : ** This function returns a pointer to the head of a linked list of FK
   88184                 : ** constraints for which table pTab is the parent table. For example,
   88185                 : ** given the following schema:
   88186                 : **
   88187                 : **   CREATE TABLE t1(a PRIMARY KEY);
   88188                 : **   CREATE TABLE t2(b REFERENCES t1(a);
   88189                 : **
   88190                 : ** Calling this function with table "t1" as an argument returns a pointer
   88191                 : ** to the FKey structure representing the foreign key constraint on table
   88192                 : ** "t2". Calling this function with "t2" as the argument would return a
   88193                 : ** NULL pointer (as there are no FK constraints for which t2 is the parent
   88194                 : ** table).
   88195                 : */
   88196            2035 : SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
   88197            2035 :   int nName = sqlite3Strlen30(pTab->zName);
   88198            2035 :   return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
   88199                 : }
   88200                 : 
   88201                 : /*
   88202                 : ** The second argument is a Trigger structure allocated by the 
   88203                 : ** fkActionTrigger() routine. This function deletes the Trigger structure
   88204                 : ** and all of its sub-components.
   88205                 : **
   88206                 : ** The Trigger structure or any of its sub-components may be allocated from
   88207                 : ** the lookaside buffer belonging to database handle dbMem.
   88208                 : */
   88209            7760 : static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
   88210            7760 :   if( p ){
   88211             124 :     TriggerStep *pStep = p->step_list;
   88212             124 :     sqlite3ExprDelete(dbMem, pStep->pWhere);
   88213             124 :     sqlite3ExprListDelete(dbMem, pStep->pExprList);
   88214             124 :     sqlite3SelectDelete(dbMem, pStep->pSelect);
   88215             124 :     sqlite3ExprDelete(dbMem, p->pWhen);
   88216             124 :     sqlite3DbFree(dbMem, p);
   88217                 :   }
   88218            7760 : }
   88219                 : 
   88220                 : /*
   88221                 : ** This function is called to generate code that runs when table pTab is
   88222                 : ** being dropped from the database. The SrcList passed as the second argument
   88223                 : ** to this function contains a single entry guaranteed to resolve to
   88224                 : ** table pTab.
   88225                 : **
   88226                 : ** Normally, no code is required. However, if either
   88227                 : **
   88228                 : **   (a) The table is the parent table of a FK constraint, or
   88229                 : **   (b) The table is the child table of a deferred FK constraint and it is
   88230                 : **       determined at runtime that there are outstanding deferred FK 
   88231                 : **       constraint violations in the database,
   88232                 : **
   88233                 : ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
   88234                 : ** the table from the database. Triggers are disabled while running this
   88235                 : ** DELETE, but foreign key actions are not.
   88236                 : */
   88237              89 : SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
   88238              89 :   sqlite3 *db = pParse->db;
   88239              89 :   if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
   88240               0 :     int iSkip = 0;
   88241               0 :     Vdbe *v = sqlite3GetVdbe(pParse);
   88242                 : 
   88243               0 :     assert( v );                  /* VDBE has already been allocated */
   88244               0 :     if( sqlite3FkReferences(pTab)==0 ){
   88245                 :       /* Search for a deferred foreign key constraint for which this table
   88246                 :       ** is the child table. If one cannot be found, return without 
   88247                 :       ** generating any VDBE code. If one can be found, then jump over
   88248                 :       ** the entire DELETE if there are no outstanding deferred constraints
   88249                 :       ** when this statement is run.  */
   88250                 :       FKey *p;
   88251               0 :       for(p=pTab->pFKey; p; p=p->pNextFrom){
   88252               0 :         if( p->isDeferred ) break;
   88253                 :       }
   88254               0 :       if( !p ) return;
   88255               0 :       iSkip = sqlite3VdbeMakeLabel(v);
   88256               0 :       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
   88257                 :     }
   88258                 : 
   88259               0 :     pParse->disableTriggers = 1;
   88260               0 :     sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
   88261               0 :     pParse->disableTriggers = 0;
   88262                 : 
   88263                 :     /* If the DELETE has generated immediate foreign key constraint 
   88264                 :     ** violations, halt the VDBE and return an error at this point, before
   88265                 :     ** any modifications to the schema are made. This is because statement
   88266                 :     ** transactions are not able to rollback schema changes.  */
   88267               0 :     sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
   88268               0 :     sqlite3HaltConstraint(
   88269                 :         pParse, OE_Abort, "foreign key constraint failed", P4_STATIC
   88270                 :     );
   88271                 : 
   88272               0 :     if( iSkip ){
   88273               0 :       sqlite3VdbeResolveLabel(v, iSkip);
   88274                 :     }
   88275                 :   }
   88276                 : }
   88277                 : 
   88278                 : /*
   88279                 : ** This function is called when inserting, deleting or updating a row of
   88280                 : ** table pTab to generate VDBE code to perform foreign key constraint 
   88281                 : ** processing for the operation.
   88282                 : **
   88283                 : ** For a DELETE operation, parameter regOld is passed the index of the
   88284                 : ** first register in an array of (pTab->nCol+1) registers containing the
   88285                 : ** rowid of the row being deleted, followed by each of the column values
   88286                 : ** of the row being deleted, from left to right. Parameter regNew is passed
   88287                 : ** zero in this case.
   88288                 : **
   88289                 : ** For an INSERT operation, regOld is passed zero and regNew is passed the
   88290                 : ** first register of an array of (pTab->nCol+1) registers containing the new
   88291                 : ** row data.
   88292                 : **
   88293                 : ** For an UPDATE operation, this function is called twice. Once before
   88294                 : ** the original record is deleted from the table using the calling convention
   88295                 : ** described for DELETE. Then again after the original record is deleted
   88296                 : ** but before the new record is inserted using the INSERT convention. 
   88297                 : */
   88298           22426 : SQLITE_PRIVATE void sqlite3FkCheck(
   88299                 :   Parse *pParse,                  /* Parse context */
   88300                 :   Table *pTab,                    /* Row is being deleted from this table */ 
   88301                 :   int regOld,                     /* Previous row data is stored here */
   88302                 :   int regNew                      /* New row data is stored here */
   88303                 : ){
   88304           22426 :   sqlite3 *db = pParse->db;       /* Database handle */
   88305                 :   FKey *pFKey;                    /* Used to iterate through FKs */
   88306                 :   int iDb;                        /* Index of database containing pTab */
   88307                 :   const char *zDb;                /* Name of database containing pTab */
   88308           22426 :   int isIgnoreErrors = pParse->disableTriggers;
   88309                 : 
   88310                 :   /* Exactly one of regOld and regNew should be non-zero. */
   88311           22426 :   assert( (regOld==0)!=(regNew==0) );
   88312                 : 
   88313                 :   /* If foreign-keys are disabled, this function is a no-op. */
   88314           22426 :   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
   88315                 : 
   88316             730 :   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   88317             730 :   zDb = db->aDb[iDb].zName;
   88318                 : 
   88319                 :   /* Loop through all the foreign key constraints for which pTab is the
   88320                 :   ** child table (the table that the foreign key definition is part of).  */
   88321            1634 :   for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
   88322                 :     Table *pTo;                   /* Parent table of foreign key pFKey */
   88323             904 :     Index *pIdx = 0;              /* Index on key columns in pTo */
   88324             904 :     int *aiFree = 0;
   88325                 :     int *aiCol;
   88326                 :     int iCol;
   88327                 :     int i;
   88328             904 :     int isIgnore = 0;
   88329                 : 
   88330                 :     /* Find the parent table of this foreign key. Also find a unique index 
   88331                 :     ** on the parent key columns in the parent table. If either of these 
   88332                 :     ** schema items cannot be located, set an error in pParse and return 
   88333                 :     ** early.  */
   88334             904 :     if( pParse->disableTriggers ){
   88335               0 :       pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
   88336                 :     }else{
   88337             904 :       pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
   88338                 :     }
   88339             904 :     if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
   88340               0 :       assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
   88341               0 :       if( !isIgnoreErrors || db->mallocFailed ) return;
   88342               0 :       if( pTo==0 ){
   88343                 :         /* If isIgnoreErrors is true, then a table is being dropped. In this
   88344                 :         ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
   88345                 :         ** before actually dropping it in order to check FK constraints.
   88346                 :         ** If the parent table of an FK constraint on the current table is
   88347                 :         ** missing, behave as if it is empty. i.e. decrement the relevant
   88348                 :         ** FK counter for each row of the current table with non-NULL keys.
   88349                 :         */
   88350               0 :         Vdbe *v = sqlite3GetVdbe(pParse);
   88351               0 :         int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
   88352               0 :         for(i=0; i<pFKey->nCol; i++){
   88353               0 :           int iReg = pFKey->aCol[i].iFrom + regOld + 1;
   88354               0 :           sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
   88355                 :         }
   88356               0 :         sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
   88357                 :       }
   88358               0 :       continue;
   88359                 :     }
   88360             904 :     assert( pFKey->nCol==1 || (aiFree && pIdx) );
   88361                 : 
   88362             904 :     if( aiFree ){
   88363               0 :       aiCol = aiFree;
   88364                 :     }else{
   88365             904 :       iCol = pFKey->aCol[0].iFrom;
   88366             904 :       aiCol = &iCol;
   88367                 :     }
   88368            1808 :     for(i=0; i<pFKey->nCol; i++){
   88369             904 :       if( aiCol[i]==pTab->iPKey ){
   88370               0 :         aiCol[i] = -1;
   88371                 :       }
   88372                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   88373                 :       /* Request permission to read the parent key columns. If the 
   88374                 :       ** authorization callback returns SQLITE_IGNORE, behave as if any
   88375                 :       ** values read from the parent table are NULL. */
   88376             904 :       if( db->xAuth ){
   88377                 :         int rcauth;
   88378               0 :         char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
   88379               0 :         rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
   88380               0 :         isIgnore = (rcauth==SQLITE_IGNORE);
   88381                 :       }
   88382                 : #endif
   88383                 :     }
   88384                 : 
   88385                 :     /* Take a shared-cache advisory read-lock on the parent table. Allocate 
   88386                 :     ** a cursor to use to search the unique index on the parent key columns 
   88387                 :     ** in the parent table.  */
   88388             904 :     sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
   88389             904 :     pParse->nTab++;
   88390                 : 
   88391             904 :     if( regOld!=0 ){
   88392                 :       /* A row is being removed from the child table. Search for the parent.
   88393                 :       ** If the parent does not exist, removing the child row resolves an 
   88394                 :       ** outstanding foreign key constraint violation. */
   88395             421 :       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
   88396                 :     }
   88397             904 :     if( regNew!=0 ){
   88398                 :       /* A row is being added to the child table. If a parent row cannot
   88399                 :       ** be found, adding the child row has violated the FK constraint. */ 
   88400             483 :       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
   88401                 :     }
   88402                 : 
   88403             904 :     sqlite3DbFree(db, aiFree);
   88404                 :   }
   88405                 : 
   88406                 :   /* Loop through all the foreign key constraints that refer to this table */
   88407            1722 :   for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
   88408             992 :     Index *pIdx = 0;              /* Foreign key index for pFKey */
   88409                 :     SrcList *pSrc;
   88410             992 :     int *aiCol = 0;
   88411                 : 
   88412             992 :     if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){
   88413             164 :       assert( regOld==0 && regNew!=0 );
   88414                 :       /* Inserting a single row into a parent table cannot cause an immediate
   88415                 :       ** foreign key violation. So do nothing in this case.  */
   88416             164 :       continue;
   88417                 :     }
   88418                 : 
   88419             828 :     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
   88420               0 :       if( !isIgnoreErrors || db->mallocFailed ) return;
   88421               0 :       continue;
   88422                 :     }
   88423             828 :     assert( aiCol || pFKey->nCol==1 );
   88424                 : 
   88425                 :     /* Create a SrcList structure containing a single table (the table 
   88426                 :     ** the foreign key that refers to this table is attached to). This
   88427                 :     ** is required for the sqlite3WhereXXX() interface.  */
   88428             828 :     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
   88429             828 :     if( pSrc ){
   88430             828 :       struct SrcList_item *pItem = pSrc->a;
   88431             828 :       pItem->pTab = pFKey->pFrom;
   88432             828 :       pItem->zName = pFKey->pFrom->zName;
   88433             828 :       pItem->pTab->nRef++;
   88434             828 :       pItem->iCursor = pParse->nTab++;
   88435                 :   
   88436             828 :       if( regNew!=0 ){
   88437             674 :         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
   88438                 :       }
   88439             828 :       if( regOld!=0 ){
   88440                 :         /* If there is a RESTRICT action configured for the current operation
   88441                 :         ** on the parent table of this FK, then throw an exception 
   88442                 :         ** immediately if the FK constraint is violated, even if this is a
   88443                 :         ** deferred trigger. That's what RESTRICT means. To defer checking
   88444                 :         ** the constraint, the FK should specify NO ACTION (represented
   88445                 :         ** using OE_None). NO ACTION is the default.  */
   88446             154 :         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
   88447                 :       }
   88448             828 :       pItem->zName = 0;
   88449             828 :       sqlite3SrcListDelete(db, pSrc);
   88450                 :     }
   88451             828 :     sqlite3DbFree(db, aiCol);
   88452                 :   }
   88453                 : }
   88454                 : 
   88455                 : #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
   88456                 : 
   88457                 : /*
   88458                 : ** This function is called before generating code to update or delete a 
   88459                 : ** row contained in table pTab.
   88460                 : */
   88461            2258 : SQLITE_PRIVATE u32 sqlite3FkOldmask(
   88462                 :   Parse *pParse,                  /* Parse context */
   88463                 :   Table *pTab                     /* Table being modified */
   88464                 : ){
   88465            2258 :   u32 mask = 0;
   88466            2258 :   if( pParse->db->flags&SQLITE_ForeignKeys ){
   88467                 :     FKey *p;
   88468                 :     int i;
   88469             672 :     for(p=pTab->pFKey; p; p=p->pNextFrom){
   88470             421 :       for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
   88471                 :     }
   88472             405 :     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   88473             154 :       Index *pIdx = 0;
   88474             154 :       locateFkeyIndex(pParse, pTab, p, &pIdx, 0);
   88475             154 :       if( pIdx ){
   88476               0 :         for(i=0; i<pIdx->nColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
   88477                 :       }
   88478                 :     }
   88479                 :   }
   88480            2258 :   return mask;
   88481                 : }
   88482                 : 
   88483                 : /*
   88484                 : ** This function is called before generating code to update or delete a 
   88485                 : ** row contained in table pTab. If the operation is a DELETE, then
   88486                 : ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
   88487                 : ** to an array of size N, where N is the number of columns in table pTab.
   88488                 : ** If the i'th column is not modified by the UPDATE, then the corresponding 
   88489                 : ** entry in the aChange[] array is set to -1. If the column is modified,
   88490                 : ** the value is 0 or greater. Parameter chngRowid is set to true if the
   88491                 : ** UPDATE statement modifies the rowid fields of the table.
   88492                 : **
   88493                 : ** If any foreign key processing will be required, this function returns
   88494                 : ** true. If there is no foreign key related processing, this function 
   88495                 : ** returns false.
   88496                 : */
   88497           22054 : SQLITE_PRIVATE int sqlite3FkRequired(
   88498                 :   Parse *pParse,                  /* Parse context */
   88499                 :   Table *pTab,                    /* Table being modified */
   88500                 :   int *aChange,                   /* Non-NULL for UPDATE operations */
   88501                 :   int chngRowid                   /* True for UPDATE that affects rowid */
   88502                 : ){
   88503           22054 :   if( pParse->db->flags&SQLITE_ForeignKeys ){
   88504             751 :     if( !aChange ){
   88505                 :       /* A DELETE operation. Foreign key processing is required if the 
   88506                 :       ** table in question is either the child or parent table for any 
   88507                 :       ** foreign key constraint.  */
   88508             303 :       return (sqlite3FkReferences(pTab) || pTab->pFKey);
   88509                 :     }else{
   88510                 :       /* This is an UPDATE. Foreign key processing is only required if the
   88511                 :       ** operation modifies one or more child or parent key columns. */
   88512                 :       int i;
   88513                 :       FKey *p;
   88514                 : 
   88515                 :       /* Check if any child key columns are being modified. */
   88516             448 :       for(p=pTab->pFKey; p; p=p->pNextFrom){
   88517               0 :         for(i=0; i<p->nCol; i++){
   88518               0 :           int iChildKey = p->aCol[i].iFrom;
   88519               0 :           if( aChange[iChildKey]>=0 ) return 1;
   88520               0 :           if( iChildKey==pTab->iPKey && chngRowid ) return 1;
   88521                 :         }
   88522                 :       }
   88523                 : 
   88524                 :       /* Check if any parent key columns are being modified. */
   88525             994 :       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   88526            1092 :         for(i=0; i<p->nCol; i++){
   88527             546 :           char *zKey = p->aCol[i].zCol;
   88528                 :           int iKey;
   88529            2730 :           for(iKey=0; iKey<pTab->nCol; iKey++){
   88530            2184 :             Column *pCol = &pTab->aCol[iKey];
   88531            2184 :             if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
   88532             546 :               if( aChange[iKey]>=0 ) return 1;
   88533             546 :               if( iKey==pTab->iPKey && chngRowid ) return 1;
   88534                 :             }
   88535                 :           }
   88536                 :         }
   88537                 :       }
   88538                 :     }
   88539                 :   }
   88540           21751 :   return 0;
   88541                 : }
   88542                 : 
   88543                 : /*
   88544                 : ** This function is called when an UPDATE or DELETE operation is being 
   88545                 : ** compiled on table pTab, which is the parent table of foreign-key pFKey.
   88546                 : ** If the current operation is an UPDATE, then the pChanges parameter is
   88547                 : ** passed a pointer to the list of columns being modified. If it is a
   88548                 : ** DELETE, pChanges is passed a NULL pointer.
   88549                 : **
   88550                 : ** It returns a pointer to a Trigger structure containing a trigger
   88551                 : ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
   88552                 : ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
   88553                 : ** returned (these actions require no special handling by the triggers
   88554                 : ** sub-system, code for them is created by fkScanChildren()).
   88555                 : **
   88556                 : ** For example, if pFKey is the foreign key and pTab is table "p" in 
   88557                 : ** the following schema:
   88558                 : **
   88559                 : **   CREATE TABLE p(pk PRIMARY KEY);
   88560                 : **   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
   88561                 : **
   88562                 : ** then the returned trigger structure is equivalent to:
   88563                 : **
   88564                 : **   CREATE TRIGGER ... DELETE ON p BEGIN
   88565                 : **     DELETE FROM c WHERE ck = old.pk;
   88566                 : **   END;
   88567                 : **
   88568                 : ** The returned pointer is cached as part of the foreign key object. It
   88569                 : ** is eventually freed along with the rest of the foreign key object by 
   88570                 : ** sqlite3FkDelete().
   88571                 : */
   88572             154 : static Trigger *fkActionTrigger(
   88573                 :   Parse *pParse,                  /* Parse context */
   88574                 :   Table *pTab,                    /* Table being updated or deleted from */
   88575                 :   FKey *pFKey,                    /* Foreign key to get action for */
   88576                 :   ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
   88577                 : ){
   88578             154 :   sqlite3 *db = pParse->db;       /* Database handle */
   88579                 :   int action;                     /* One of OE_None, OE_Cascade etc. */
   88580                 :   Trigger *pTrigger;              /* Trigger definition to return */
   88581             154 :   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
   88582                 : 
   88583             154 :   action = pFKey->aAction[iAction];
   88584             154 :   pTrigger = pFKey->apTrigger[iAction];
   88585                 : 
   88586             154 :   if( action!=OE_None && !pTrigger ){
   88587                 :     u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
   88588                 :     char const *zFrom;            /* Name of child table */
   88589                 :     int nFrom;                    /* Length in bytes of zFrom */
   88590             124 :     Index *pIdx = 0;              /* Parent key index for this FK */
   88591             124 :     int *aiCol = 0;               /* child table cols -> parent key cols */
   88592             124 :     TriggerStep *pStep = 0;        /* First (only) step of trigger program */
   88593             124 :     Expr *pWhere = 0;             /* WHERE clause of trigger step */
   88594             124 :     ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
   88595             124 :     Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
   88596                 :     int i;                        /* Iterator variable */
   88597             124 :     Expr *pWhen = 0;              /* WHEN clause for the trigger */
   88598                 : 
   88599             124 :     if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
   88600             124 :     assert( aiCol || pFKey->nCol==1 );
   88601                 : 
   88602             248 :     for(i=0; i<pFKey->nCol; i++){
   88603             124 :       Token tOld = { "old", 3 };  /* Literal "old" token */
   88604             124 :       Token tNew = { "new", 3 };  /* Literal "new" token */
   88605                 :       Token tFromCol;             /* Name of column in child table */
   88606                 :       Token tToCol;               /* Name of column in parent table */
   88607                 :       int iFromCol;               /* Idx of column in child table */
   88608                 :       Expr *pEq;                  /* tFromCol = OLD.tToCol */
   88609                 : 
   88610             124 :       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
   88611             124 :       assert( iFromCol>=0 );
   88612             124 :       tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
   88613             124 :       tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
   88614                 : 
   88615             124 :       tToCol.n = sqlite3Strlen30(tToCol.z);
   88616             124 :       tFromCol.n = sqlite3Strlen30(tFromCol.z);
   88617                 : 
   88618                 :       /* Create the expression "OLD.zToCol = zFromCol". It is important
   88619                 :       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
   88620                 :       ** that the affinity and collation sequence associated with the
   88621                 :       ** parent table are used for the comparison. */
   88622             124 :       pEq = sqlite3PExpr(pParse, TK_EQ,
   88623                 :           sqlite3PExpr(pParse, TK_DOT, 
   88624                 :             sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
   88625                 :             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
   88626                 :           , 0),
   88627                 :           sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
   88628                 :       , 0);
   88629             124 :       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
   88630                 : 
   88631                 :       /* For ON UPDATE, construct the next term of the WHEN clause.
   88632                 :       ** The final WHEN clause will be like this:
   88633                 :       **
   88634                 :       **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
   88635                 :       */
   88636             124 :       if( pChanges ){
   88637               0 :         pEq = sqlite3PExpr(pParse, TK_IS,
   88638                 :             sqlite3PExpr(pParse, TK_DOT, 
   88639                 :               sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
   88640                 :               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
   88641                 :               0),
   88642                 :             sqlite3PExpr(pParse, TK_DOT, 
   88643                 :               sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
   88644                 :               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
   88645                 :               0),
   88646                 :             0);
   88647               0 :         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
   88648                 :       }
   88649                 :   
   88650             124 :       if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
   88651                 :         Expr *pNew;
   88652               0 :         if( action==OE_Cascade ){
   88653               0 :           pNew = sqlite3PExpr(pParse, TK_DOT, 
   88654                 :             sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
   88655                 :             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
   88656                 :           , 0);
   88657               0 :         }else if( action==OE_SetDflt ){
   88658               0 :           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
   88659               0 :           if( pDflt ){
   88660               0 :             pNew = sqlite3ExprDup(db, pDflt, 0);
   88661                 :           }else{
   88662               0 :             pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
   88663                 :           }
   88664                 :         }else{
   88665               0 :           pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
   88666                 :         }
   88667               0 :         pList = sqlite3ExprListAppend(pParse, pList, pNew);
   88668               0 :         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
   88669                 :       }
   88670                 :     }
   88671             124 :     sqlite3DbFree(db, aiCol);
   88672                 : 
   88673             124 :     zFrom = pFKey->pFrom->zName;
   88674             124 :     nFrom = sqlite3Strlen30(zFrom);
   88675                 : 
   88676             124 :     if( action==OE_Restrict ){
   88677                 :       Token tFrom;
   88678                 :       Expr *pRaise; 
   88679                 : 
   88680               0 :       tFrom.z = zFrom;
   88681               0 :       tFrom.n = nFrom;
   88682               0 :       pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed");
   88683               0 :       if( pRaise ){
   88684               0 :         pRaise->affinity = OE_Abort;
   88685                 :       }
   88686               0 :       pSelect = sqlite3SelectNew(pParse, 
   88687                 :           sqlite3ExprListAppend(pParse, 0, pRaise),
   88688                 :           sqlite3SrcListAppend(db, 0, &tFrom, 0),
   88689                 :           pWhere,
   88690                 :           0, 0, 0, 0, 0, 0
   88691                 :       );
   88692               0 :       pWhere = 0;
   88693                 :     }
   88694                 : 
   88695                 :     /* Disable lookaside memory allocation */
   88696             124 :     enableLookaside = db->lookaside.bEnabled;
   88697             124 :     db->lookaside.bEnabled = 0;
   88698                 : 
   88699             124 :     pTrigger = (Trigger *)sqlite3DbMallocZero(db, 
   88700                 :         sizeof(Trigger) +         /* struct Trigger */
   88701                 :         sizeof(TriggerStep) +     /* Single step in trigger program */
   88702             124 :         nFrom + 1                 /* Space for pStep->target.z */
   88703                 :     );
   88704             124 :     if( pTrigger ){
   88705             124 :       pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
   88706             124 :       pStep->target.z = (char *)&pStep[1];
   88707             124 :       pStep->target.n = nFrom;
   88708             124 :       memcpy((char *)pStep->target.z, zFrom, nFrom);
   88709                 :   
   88710             124 :       pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
   88711             124 :       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
   88712             124 :       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
   88713             124 :       if( pWhen ){
   88714               0 :         pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
   88715               0 :         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
   88716                 :       }
   88717                 :     }
   88718                 : 
   88719                 :     /* Re-enable the lookaside buffer, if it was disabled earlier. */
   88720             124 :     db->lookaside.bEnabled = enableLookaside;
   88721                 : 
   88722             124 :     sqlite3ExprDelete(db, pWhere);
   88723             124 :     sqlite3ExprDelete(db, pWhen);
   88724             124 :     sqlite3ExprListDelete(db, pList);
   88725             124 :     sqlite3SelectDelete(db, pSelect);
   88726             124 :     if( db->mallocFailed==1 ){
   88727               0 :       fkTriggerDelete(db, pTrigger);
   88728               0 :       return 0;
   88729                 :     }
   88730             124 :     assert( pStep!=0 );
   88731                 : 
   88732             124 :     switch( action ){
   88733                 :       case OE_Restrict:
   88734               0 :         pStep->op = TK_SELECT; 
   88735               0 :         break;
   88736                 :       case OE_Cascade: 
   88737             124 :         if( !pChanges ){ 
   88738             124 :           pStep->op = TK_DELETE; 
   88739             124 :           break; 
   88740                 :         }
   88741                 :       default:
   88742               0 :         pStep->op = TK_UPDATE;
   88743                 :     }
   88744             124 :     pStep->pTrig = pTrigger;
   88745             124 :     pTrigger->pSchema = pTab->pSchema;
   88746             124 :     pTrigger->pTabSchema = pTab->pSchema;
   88747             124 :     pFKey->apTrigger[iAction] = pTrigger;
   88748             124 :     pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
   88749                 :   }
   88750                 : 
   88751             154 :   return pTrigger;
   88752                 : }
   88753                 : 
   88754                 : /*
   88755                 : ** This function is called when deleting or updating a row to implement
   88756                 : ** any required CASCADE, SET NULL or SET DEFAULT actions.
   88757                 : */
   88758            8117 : SQLITE_PRIVATE void sqlite3FkActions(
   88759                 :   Parse *pParse,                  /* Parse context */
   88760                 :   Table *pTab,                    /* Table being updated or deleted from */
   88761                 :   ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
   88762                 :   int regOld                      /* Address of array containing old row */
   88763                 : ){
   88764                 :   /* If foreign-key support is enabled, iterate through all FKs that 
   88765                 :   ** refer to table pTab. If there is an action associated with the FK 
   88766                 :   ** for this operation (either update or delete), invoke the associated 
   88767                 :   ** trigger sub-program.  */
   88768            8117 :   if( pParse->db->flags&SQLITE_ForeignKeys ){
   88769                 :     FKey *pFKey;                  /* Iterator variable */
   88770             457 :     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
   88771             154 :       Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges);
   88772             154 :       if( pAction ){
   88773             154 :         sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0);
   88774                 :       }
   88775                 :     }
   88776                 :   }
   88777            8117 : }
   88778                 : 
   88779                 : #endif /* ifndef SQLITE_OMIT_TRIGGER */
   88780                 : 
   88781                 : /*
   88782                 : ** Free all memory associated with foreign key definitions attached to
   88783                 : ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
   88784                 : ** hash table.
   88785                 : */
   88786           36087 : SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
   88787                 :   FKey *pFKey;                    /* Iterator variable */
   88788                 :   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
   88789                 : 
   88790           36087 :   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
   88791           39967 :   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
   88792                 : 
   88793                 :     /* Remove the FK from the fkeyHash hash table. */
   88794            3880 :     if( !db || db->pnBytesFreed==0 ){
   88795            3880 :       if( pFKey->pPrevTo ){
   88796               0 :         pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
   88797                 :       }else{
   88798            3880 :         void *p = (void *)pFKey->pNextTo;
   88799            3880 :         const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
   88800            3880 :         sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
   88801                 :       }
   88802            3880 :       if( pFKey->pNextTo ){
   88803            1896 :         pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
   88804                 :       }
   88805                 :     }
   88806                 : 
   88807                 :     /* EV: R-30323-21917 Each foreign key constraint in SQLite is
   88808                 :     ** classified as either immediate or deferred.
   88809                 :     */
   88810            3880 :     assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
   88811                 : 
   88812                 :     /* Delete any triggers created to implement actions for this FK. */
   88813                 : #ifndef SQLITE_OMIT_TRIGGER
   88814            3880 :     fkTriggerDelete(db, pFKey->apTrigger[0]);
   88815            3880 :     fkTriggerDelete(db, pFKey->apTrigger[1]);
   88816                 : #endif
   88817                 : 
   88818            3880 :     pNext = pFKey->pNextFrom;
   88819            3880 :     sqlite3DbFree(db, pFKey);
   88820                 :   }
   88821           36087 : }
   88822                 : #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
   88823                 : 
   88824                 : /************** End of fkey.c ************************************************/
   88825                 : /************** Begin file insert.c ******************************************/
   88826                 : /*
   88827                 : ** 2001 September 15
   88828                 : **
   88829                 : ** The author disclaims copyright to this source code.  In place of
   88830                 : ** a legal notice, here is a blessing:
   88831                 : **
   88832                 : **    May you do good and not evil.
   88833                 : **    May you find forgiveness for yourself and forgive others.
   88834                 : **    May you share freely, never taking more than you give.
   88835                 : **
   88836                 : *************************************************************************
   88837                 : ** This file contains C code routines that are called by the parser
   88838                 : ** to handle INSERT statements in SQLite.
   88839                 : */
   88840                 : 
   88841                 : /*
   88842                 : ** Generate code that will open a table for reading.
   88843                 : */
   88844          111585 : SQLITE_PRIVATE void sqlite3OpenTable(
   88845                 :   Parse *p,       /* Generate code into this VDBE */
   88846                 :   int iCur,       /* The cursor number of the table */
   88847                 :   int iDb,        /* The database index in sqlite3.aDb[] */
   88848                 :   Table *pTab,    /* The table to be opened */
   88849                 :   int opcode      /* OP_OpenRead or OP_OpenWrite */
   88850                 : ){
   88851                 :   Vdbe *v;
   88852          111585 :   if( IsVirtual(pTab) ) return;
   88853          111585 :   v = sqlite3GetVdbe(p);
   88854          111585 :   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
   88855          111585 :   sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
   88856          111585 :   sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
   88857          111585 :   sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
   88858          111585 :   VdbeComment((v, "%s", pTab->zName));
   88859                 : }
   88860                 : 
   88861                 : /*
   88862                 : ** Return a pointer to the column affinity string associated with index
   88863                 : ** pIdx. A column affinity string has one character for each column in 
   88864                 : ** the table, according to the affinity of the column:
   88865                 : **
   88866                 : **  Character      Column affinity
   88867                 : **  ------------------------------
   88868                 : **  'a'            TEXT
   88869                 : **  'b'            NONE
   88870                 : **  'c'            NUMERIC
   88871                 : **  'd'            INTEGER
   88872                 : **  'e'            REAL
   88873                 : **
   88874                 : ** An extra 'd' is appended to the end of the string to cover the
   88875                 : ** rowid that appears as the last column in every index.
   88876                 : **
   88877                 : ** Memory for the buffer containing the column index affinity string
   88878                 : ** is managed along with the rest of the Index structure. It will be
   88879                 : ** released when sqlite3DeleteIndex() is called.
   88880                 : */
   88881           48293 : SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
   88882           48293 :   if( !pIdx->zColAff ){
   88883                 :     /* The first time a column affinity string for a particular index is
   88884                 :     ** required, it is allocated and populated here. It is then stored as
   88885                 :     ** a member of the Index structure for subsequent use.
   88886                 :     **
   88887                 :     ** The column affinity string will eventually be deleted by
   88888                 :     ** sqliteDeleteIndex() when the Index structure itself is cleaned
   88889                 :     ** up.
   88890                 :     */
   88891                 :     int n;
   88892           17638 :     Table *pTab = pIdx->pTable;
   88893           17638 :     sqlite3 *db = sqlite3VdbeDb(v);
   88894           17638 :     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
   88895           17638 :     if( !pIdx->zColAff ){
   88896               0 :       db->mallocFailed = 1;
   88897               0 :       return 0;
   88898                 :     }
   88899           44730 :     for(n=0; n<pIdx->nColumn; n++){
   88900           27092 :       pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
   88901                 :     }
   88902           17638 :     pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
   88903           17638 :     pIdx->zColAff[n] = 0;
   88904                 :   }
   88905                 :  
   88906           48293 :   return pIdx->zColAff;
   88907                 : }
   88908                 : 
   88909                 : /*
   88910                 : ** Set P4 of the most recently inserted opcode to a column affinity
   88911                 : ** string for table pTab. A column affinity string has one character
   88912                 : ** for each column indexed by the index, according to the affinity of the
   88913                 : ** column:
   88914                 : **
   88915                 : **  Character      Column affinity
   88916                 : **  ------------------------------
   88917                 : **  'a'            TEXT
   88918                 : **  'b'            NONE
   88919                 : **  'c'            NUMERIC
   88920                 : **  'd'            INTEGER
   88921                 : **  'e'            REAL
   88922                 : */
   88923           33420 : SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
   88924                 :   /* The first time a column affinity string for a particular table
   88925                 :   ** is required, it is allocated and populated here. It is then 
   88926                 :   ** stored as a member of the Table structure for subsequent use.
   88927                 :   **
   88928                 :   ** The column affinity string will eventually be deleted by
   88929                 :   ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
   88930                 :   */
   88931           33420 :   if( !pTab->zColAff ){
   88932                 :     char *zColAff;
   88933                 :     int i;
   88934            8657 :     sqlite3 *db = sqlite3VdbeDb(v);
   88935                 : 
   88936            8657 :     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
   88937            8657 :     if( !zColAff ){
   88938               0 :       db->mallocFailed = 1;
   88939               0 :       return;
   88940                 :     }
   88941                 : 
   88942           71709 :     for(i=0; i<pTab->nCol; i++){
   88943           63052 :       zColAff[i] = pTab->aCol[i].affinity;
   88944                 :     }
   88945            8657 :     zColAff[pTab->nCol] = '\0';
   88946                 : 
   88947            8657 :     pTab->zColAff = zColAff;
   88948                 :   }
   88949                 : 
   88950           33420 :   sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
   88951                 : }
   88952                 : 
   88953                 : /*
   88954                 : ** Return non-zero if the table pTab in database iDb or any of its indices
   88955                 : ** have been opened at any point in the VDBE program beginning at location
   88956                 : ** iStartAddr throught the end of the program.  This is used to see if 
   88957                 : ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
   88958                 : ** run without using temporary table for the results of the SELECT. 
   88959                 : */
   88960              90 : static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
   88961              90 :   Vdbe *v = sqlite3GetVdbe(p);
   88962                 :   int i;
   88963              90 :   int iEnd = sqlite3VdbeCurrentAddr(v);
   88964                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   88965              90 :   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
   88966                 : #endif
   88967                 : 
   88968            4689 :   for(i=iStartAddr; i<iEnd; i++){
   88969            4607 :     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
   88970            4607 :     assert( pOp!=0 );
   88971            4607 :     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
   88972                 :       Index *pIndex;
   88973              26 :       int tnum = pOp->p2;
   88974              26 :       if( tnum==pTab->tnum ){
   88975               5 :         return 1;
   88976                 :       }
   88977              39 :       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
   88978              21 :         if( tnum==pIndex->tnum ){
   88979               3 :           return 1;
   88980                 :         }
   88981                 :       }
   88982                 :     }
   88983                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   88984            4599 :     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
   88985               0 :       assert( pOp->p4.pVtab!=0 );
   88986               0 :       assert( pOp->p4type==P4_VTAB );
   88987               0 :       return 1;
   88988                 :     }
   88989                 : #endif
   88990                 :   }
   88991              82 :   return 0;
   88992                 : }
   88993                 : 
   88994                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
   88995                 : /*
   88996                 : ** Locate or create an AutoincInfo structure associated with table pTab
   88997                 : ** which is in database iDb.  Return the register number for the register
   88998                 : ** that holds the maximum rowid.
   88999                 : **
   89000                 : ** There is at most one AutoincInfo structure per table even if the
   89001                 : ** same table is autoincremented multiple times due to inserts within
   89002                 : ** triggers.  A new AutoincInfo structure is created if this is the
   89003                 : ** first use of table pTab.  On 2nd and subsequent uses, the original
   89004                 : ** AutoincInfo structure is used.
   89005                 : **
   89006                 : ** Three memory locations are allocated:
   89007                 : **
   89008                 : **   (1)  Register to hold the name of the pTab table.
   89009                 : **   (2)  Register to hold the maximum ROWID of pTab.
   89010                 : **   (3)  Register to hold the rowid in sqlite_sequence of pTab
   89011                 : **
   89012                 : ** The 2nd register is the one that is returned.  That is all the
   89013                 : ** insert routine needs to know about.
   89014                 : */
   89015           20191 : static int autoIncBegin(
   89016                 :   Parse *pParse,      /* Parsing context */
   89017                 :   int iDb,            /* Index of the database holding pTab */
   89018                 :   Table *pTab         /* The table we are writing to */
   89019                 : ){
   89020           20191 :   int memId = 0;      /* Register holding maximum rowid */
   89021           20191 :   if( pTab->tabFlags & TF_Autoincrement ){
   89022            1075 :     Parse *pToplevel = sqlite3ParseToplevel(pParse);
   89023                 :     AutoincInfo *pInfo;
   89024                 : 
   89025            1075 :     pInfo = pToplevel->pAinc;
   89026            1075 :     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
   89027            1075 :     if( pInfo==0 ){
   89028            1074 :       pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
   89029            1074 :       if( pInfo==0 ) return 0;
   89030            1074 :       pInfo->pNext = pToplevel->pAinc;
   89031            1074 :       pToplevel->pAinc = pInfo;
   89032            1074 :       pInfo->pTab = pTab;
   89033            1074 :       pInfo->iDb = iDb;
   89034            1074 :       pToplevel->nMem++;                  /* Register to hold name of table */
   89035            1074 :       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
   89036            1074 :       pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
   89037                 :     }
   89038            1075 :     memId = pInfo->regCtr;
   89039                 :   }
   89040           20191 :   return memId;
   89041                 : }
   89042                 : 
   89043                 : /*
   89044                 : ** This routine generates code that will initialize all of the
   89045                 : ** register used by the autoincrement tracker.  
   89046                 : */
   89047           80393 : SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
   89048                 :   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
   89049           80393 :   sqlite3 *db = pParse->db;  /* The database connection */
   89050                 :   Db *pDb;                   /* Database only autoinc table */
   89051                 :   int memId;                 /* Register holding max rowid */
   89052                 :   int addr;                  /* A VDBE address */
   89053           80393 :   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
   89054                 : 
   89055                 :   /* This routine is never called during trigger-generation.  It is
   89056                 :   ** only called from the top-level */
   89057           80393 :   assert( pParse->pTriggerTab==0 );
   89058           80393 :   assert( pParse==sqlite3ParseToplevel(pParse) );
   89059                 : 
   89060           80393 :   assert( v );   /* We failed long ago if this is not so */
   89061           81467 :   for(p = pParse->pAinc; p; p = p->pNext){
   89062            1074 :     pDb = &db->aDb[p->iDb];
   89063            1074 :     memId = p->regCtr;
   89064            1074 :     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
   89065            1074 :     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
   89066            1074 :     sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
   89067            1074 :     addr = sqlite3VdbeCurrentAddr(v);
   89068            1074 :     sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
   89069            1074 :     sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
   89070            1074 :     sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
   89071            1074 :     sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
   89072            1074 :     sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
   89073            1074 :     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
   89074            1074 :     sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
   89075            1074 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
   89076            1074 :     sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
   89077            1074 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
   89078            1074 :     sqlite3VdbeAddOp0(v, OP_Close);
   89079                 :   }
   89080           80393 : }
   89081                 : 
   89082                 : /*
   89083                 : ** Update the maximum rowid for an autoincrement calculation.
   89084                 : **
   89085                 : ** This routine should be called when the top of the stack holds a
   89086                 : ** new rowid that is about to be inserted.  If that new rowid is
   89087                 : ** larger than the maximum rowid in the memId memory cell, then the
   89088                 : ** memory cell is updated.  The stack is unchanged.
   89089                 : */
   89090           20185 : static void autoIncStep(Parse *pParse, int memId, int regRowid){
   89091           20185 :   if( memId>0 ){
   89092            1075 :     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
   89093                 :   }
   89094           20185 : }
   89095                 : 
   89096                 : /*
   89097                 : ** This routine generates the code needed to write autoincrement
   89098                 : ** maximum rowid values back into the sqlite_sequence register.
   89099                 : ** Every statement that might do an INSERT into an autoincrement
   89100                 : ** table (either directly or through triggers) needs to call this
   89101                 : ** routine just before the "exit" code.
   89102                 : */
   89103           17887 : SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
   89104                 :   AutoincInfo *p;
   89105           17887 :   Vdbe *v = pParse->pVdbe;
   89106           17887 :   sqlite3 *db = pParse->db;
   89107                 : 
   89108           17887 :   assert( v );
   89109           18961 :   for(p = pParse->pAinc; p; p = p->pNext){
   89110            1074 :     Db *pDb = &db->aDb[p->iDb];
   89111                 :     int j1, j2, j3, j4, j5;
   89112                 :     int iRec;
   89113            1074 :     int memId = p->regCtr;
   89114                 : 
   89115            1074 :     iRec = sqlite3GetTempReg(pParse);
   89116            1074 :     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
   89117            1074 :     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
   89118            1074 :     j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
   89119            1074 :     j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
   89120            1074 :     j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
   89121            1074 :     j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
   89122            1074 :     sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
   89123            1074 :     sqlite3VdbeJumpHere(v, j2);
   89124            1074 :     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
   89125            1074 :     j5 = sqlite3VdbeAddOp0(v, OP_Goto);
   89126            1074 :     sqlite3VdbeJumpHere(v, j4);
   89127            1074 :     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
   89128            1074 :     sqlite3VdbeJumpHere(v, j1);
   89129            1074 :     sqlite3VdbeJumpHere(v, j5);
   89130            1074 :     sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
   89131            1074 :     sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
   89132            1074 :     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   89133            1074 :     sqlite3VdbeAddOp0(v, OP_Close);
   89134            1074 :     sqlite3ReleaseTempReg(pParse, iRec);
   89135                 :   }
   89136           17887 : }
   89137                 : #else
   89138                 : /*
   89139                 : ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
   89140                 : ** above are all no-ops
   89141                 : */
   89142                 : # define autoIncBegin(A,B,C) (0)
   89143                 : # define autoIncStep(A,B,C)
   89144                 : #endif /* SQLITE_OMIT_AUTOINCREMENT */
   89145                 : 
   89146                 : 
   89147                 : /* Forward declaration */
   89148                 : static int xferOptimization(
   89149                 :   Parse *pParse,        /* Parser context */
   89150                 :   Table *pDest,         /* The table we are inserting into */
   89151                 :   Select *pSelect,      /* A SELECT statement to use as the data source */
   89152                 :   int onError,          /* How to handle constraint errors */
   89153                 :   int iDbDest           /* The database of pDest */
   89154                 : );
   89155                 : 
   89156                 : /*
   89157                 : ** This routine is call to handle SQL of the following forms:
   89158                 : **
   89159                 : **    insert into TABLE (IDLIST) values(EXPRLIST)
   89160                 : **    insert into TABLE (IDLIST) select
   89161                 : **
   89162                 : ** The IDLIST following the table name is always optional.  If omitted,
   89163                 : ** then a list of all columns for the table is substituted.  The IDLIST
   89164                 : ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
   89165                 : **
   89166                 : ** The pList parameter holds EXPRLIST in the first form of the INSERT
   89167                 : ** statement above, and pSelect is NULL.  For the second form, pList is
   89168                 : ** NULL and pSelect is a pointer to the select statement used to generate
   89169                 : ** data for the insert.
   89170                 : **
   89171                 : ** The code generated follows one of four templates.  For a simple
   89172                 : ** select with data coming from a VALUES clause, the code executes
   89173                 : ** once straight down through.  Pseudo-code follows (we call this
   89174                 : ** the "1st template"):
   89175                 : **
   89176                 : **         open write cursor to <table> and its indices
   89177                 : **         puts VALUES clause expressions onto the stack
   89178                 : **         write the resulting record into <table>
   89179                 : **         cleanup
   89180                 : **
   89181                 : ** The three remaining templates assume the statement is of the form
   89182                 : **
   89183                 : **   INSERT INTO <table> SELECT ...
   89184                 : **
   89185                 : ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
   89186                 : ** in other words if the SELECT pulls all columns from a single table
   89187                 : ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
   89188                 : ** if <table2> and <table1> are distinct tables but have identical
   89189                 : ** schemas, including all the same indices, then a special optimization
   89190                 : ** is invoked that copies raw records from <table2> over to <table1>.
   89191                 : ** See the xferOptimization() function for the implementation of this
   89192                 : ** template.  This is the 2nd template.
   89193                 : **
   89194                 : **         open a write cursor to <table>
   89195                 : **         open read cursor on <table2>
   89196                 : **         transfer all records in <table2> over to <table>
   89197                 : **         close cursors
   89198                 : **         foreach index on <table>
   89199                 : **           open a write cursor on the <table> index
   89200                 : **           open a read cursor on the corresponding <table2> index
   89201                 : **           transfer all records from the read to the write cursors
   89202                 : **           close cursors
   89203                 : **         end foreach
   89204                 : **
   89205                 : ** The 3rd template is for when the second template does not apply
   89206                 : ** and the SELECT clause does not read from <table> at any time.
   89207                 : ** The generated code follows this template:
   89208                 : **
   89209                 : **         EOF <- 0
   89210                 : **         X <- A
   89211                 : **         goto B
   89212                 : **      A: setup for the SELECT
   89213                 : **         loop over the rows in the SELECT
   89214                 : **           load values into registers R..R+n
   89215                 : **           yield X
   89216                 : **         end loop
   89217                 : **         cleanup after the SELECT
   89218                 : **         EOF <- 1
   89219                 : **         yield X
   89220                 : **         goto A
   89221                 : **      B: open write cursor to <table> and its indices
   89222                 : **      C: yield X
   89223                 : **         if EOF goto D
   89224                 : **         insert the select result into <table> from R..R+n
   89225                 : **         goto C
   89226                 : **      D: cleanup
   89227                 : **
   89228                 : ** The 4th template is used if the insert statement takes its
   89229                 : ** values from a SELECT but the data is being inserted into a table
   89230                 : ** that is also read as part of the SELECT.  In the third form,
   89231                 : ** we have to use a intermediate table to store the results of
   89232                 : ** the select.  The template is like this:
   89233                 : **
   89234                 : **         EOF <- 0
   89235                 : **         X <- A
   89236                 : **         goto B
   89237                 : **      A: setup for the SELECT
   89238                 : **         loop over the tables in the SELECT
   89239                 : **           load value into register R..R+n
   89240                 : **           yield X
   89241                 : **         end loop
   89242                 : **         cleanup after the SELECT
   89243                 : **         EOF <- 1
   89244                 : **         yield X
   89245                 : **         halt-error
   89246                 : **      B: open temp table
   89247                 : **      L: yield X
   89248                 : **         if EOF goto M
   89249                 : **         insert row from R..R+n into temp table
   89250                 : **         goto L
   89251                 : **      M: open write cursor to <table> and its indices
   89252                 : **         rewind temp table
   89253                 : **      C: loop over rows of intermediate table
   89254                 : **           transfer values form intermediate table into <table>
   89255                 : **         end loop
   89256                 : **      D: cleanup
   89257                 : */
   89258           20180 : SQLITE_PRIVATE void sqlite3Insert(
   89259                 :   Parse *pParse,        /* Parser context */
   89260                 :   SrcList *pTabList,    /* Name of table into which we are inserting */
   89261                 :   ExprList *pList,      /* List of values to be inserted */
   89262                 :   Select *pSelect,      /* A SELECT statement to use as the data source */
   89263                 :   IdList *pColumn,      /* Column names corresponding to IDLIST. */
   89264                 :   int onError           /* How to handle constraint errors */
   89265                 : ){
   89266                 :   sqlite3 *db;          /* The main database structure */
   89267                 :   Table *pTab;          /* The table to insert into.  aka TABLE */
   89268                 :   char *zTab;           /* Name of the table into which we are inserting */
   89269                 :   const char *zDb;      /* Name of the database holding this table */
   89270                 :   int i, j, idx;        /* Loop counters */
   89271                 :   Vdbe *v;              /* Generate code into this virtual machine */
   89272                 :   Index *pIdx;          /* For looping over indices of the table */
   89273                 :   int nColumn;          /* Number of columns in the data */
   89274           20180 :   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
   89275           20180 :   int baseCur = 0;      /* VDBE Cursor number for pTab */
   89276           20180 :   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
   89277                 :   int endOfLoop;        /* Label for the end of the insertion loop */
   89278           20180 :   int useTempTable = 0; /* Store SELECT results in intermediate table */
   89279           20180 :   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
   89280           20180 :   int addrInsTop = 0;   /* Jump to label "D" */
   89281           20180 :   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
   89282           20180 :   int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
   89283                 :   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
   89284                 :   int iDb;              /* Index of database holding TABLE */
   89285                 :   Db *pDb;              /* The database containing table being inserted into */
   89286           20180 :   int appendFlag = 0;   /* True if the insert is likely to be an append */
   89287                 : 
   89288                 :   /* Register allocations */
   89289           20180 :   int regFromSelect = 0;/* Base register for data coming from SELECT */
   89290           20180 :   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
   89291           20180 :   int regRowCount = 0;  /* Memory cell used for the row counter */
   89292                 :   int regIns;           /* Block of regs holding rowid+data being inserted */
   89293                 :   int regRowid;         /* registers holding insert rowid */
   89294                 :   int regData;          /* register holding first column to insert */
   89295           20180 :   int regEof = 0;       /* Register recording end of SELECT data */
   89296           20180 :   int *aRegIdx = 0;     /* One register allocated to each index */
   89297                 : 
   89298                 : #ifndef SQLITE_OMIT_TRIGGER
   89299                 :   int isView;                 /* True if attempting to insert into a view */
   89300                 :   Trigger *pTrigger;          /* List of triggers on pTab, if required */
   89301                 :   int tmask;                  /* Mask of trigger times */
   89302                 : #endif
   89303                 : 
   89304           20180 :   db = pParse->db;
   89305           20180 :   memset(&dest, 0, sizeof(dest));
   89306           20180 :   if( pParse->nErr || db->mallocFailed ){
   89307                 :     goto insert_cleanup;
   89308                 :   }
   89309                 : 
   89310                 :   /* Locate the table into which we will be inserting new information.
   89311                 :   */
   89312           20180 :   assert( pTabList->nSrc==1 );
   89313           20180 :   zTab = pTabList->a[0].zName;
   89314           20180 :   if( NEVER(zTab==0) ) goto insert_cleanup;
   89315           20180 :   pTab = sqlite3SrcListLookup(pParse, pTabList);
   89316           20180 :   if( pTab==0 ){
   89317               0 :     goto insert_cleanup;
   89318                 :   }
   89319           20180 :   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   89320           20180 :   assert( iDb<db->nDb );
   89321           20180 :   pDb = &db->aDb[iDb];
   89322           20180 :   zDb = pDb->zName;
   89323           20180 :   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
   89324               0 :     goto insert_cleanup;
   89325                 :   }
   89326                 : 
   89327                 :   /* Figure out if we have any triggers and if the table being
   89328                 :   ** inserted into is a view
   89329                 :   */
   89330                 : #ifndef SQLITE_OMIT_TRIGGER
   89331           20180 :   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
   89332           20180 :   isView = pTab->pSelect!=0;
   89333                 : #else
   89334                 : # define pTrigger 0
   89335                 : # define tmask 0
   89336                 : # define isView 0
   89337                 : #endif
   89338                 : #ifdef SQLITE_OMIT_VIEW
   89339                 : # undef isView
   89340                 : # define isView 0
   89341                 : #endif
   89342           20180 :   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
   89343                 : 
   89344                 :   /* If pTab is really a view, make sure it has been initialized.
   89345                 :   ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 
   89346                 :   ** module table).
   89347                 :   */
   89348           20180 :   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
   89349               0 :     goto insert_cleanup;
   89350                 :   }
   89351                 : 
   89352                 :   /* Ensure that:
   89353                 :   *  (a) the table is not read-only, 
   89354                 :   *  (b) that if it is a view then ON INSERT triggers exist
   89355                 :   */
   89356           20180 :   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
   89357               0 :     goto insert_cleanup;
   89358                 :   }
   89359                 : 
   89360                 :   /* Allocate a VDBE
   89361                 :   */
   89362           20180 :   v = sqlite3GetVdbe(pParse);
   89363           20180 :   if( v==0 ) goto insert_cleanup;
   89364           20180 :   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
   89365           20180 :   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
   89366                 : 
   89367                 : #ifndef SQLITE_OMIT_XFER_OPT
   89368                 :   /* If the statement is of the form
   89369                 :   **
   89370                 :   **       INSERT INTO <table1> SELECT * FROM <table2>;
   89371                 :   **
   89372                 :   ** Then special optimizations can be applied that make the transfer
   89373                 :   ** very fast and which reduce fragmentation of indices.
   89374                 :   **
   89375                 :   ** This is the 2nd template.
   89376                 :   */
   89377           20180 :   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
   89378               8 :     assert( !pTrigger );
   89379               8 :     assert( pList==0 );
   89380               8 :     goto insert_end;
   89381                 :   }
   89382                 : #endif /* SQLITE_OMIT_XFER_OPT */
   89383                 : 
   89384                 :   /* If this is an AUTOINCREMENT table, look up the sequence number in the
   89385                 :   ** sqlite_sequence table and store it in memory cell regAutoinc.
   89386                 :   */
   89387           20172 :   regAutoinc = autoIncBegin(pParse, iDb, pTab);
   89388                 : 
   89389                 :   /* Figure out how many columns of data are supplied.  If the data
   89390                 :   ** is coming from a SELECT statement, then generate a co-routine that
   89391                 :   ** produces a single row of the SELECT on each invocation.  The
   89392                 :   ** co-routine is the common header to the 3rd and 4th templates.
   89393                 :   */
   89394           20172 :   if( pSelect ){
   89395                 :     /* Data is coming from a SELECT.  Generate code to implement that SELECT
   89396                 :     ** as a co-routine.  The code is common to both the 3rd and 4th
   89397                 :     ** templates:
   89398                 :     **
   89399                 :     **         EOF <- 0
   89400                 :     **         X <- A
   89401                 :     **         goto B
   89402                 :     **      A: setup for the SELECT
   89403                 :     **         loop over the tables in the SELECT
   89404                 :     **           load value into register R..R+n
   89405                 :     **           yield X
   89406                 :     **         end loop
   89407                 :     **         cleanup after the SELECT
   89408                 :     **         EOF <- 1
   89409                 :     **         yield X
   89410                 :     **         halt-error
   89411                 :     **
   89412                 :     ** On each invocation of the co-routine, it puts a single row of the
   89413                 :     ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
   89414                 :     ** (These output registers are allocated by sqlite3Select().)  When
   89415                 :     ** the SELECT completes, it sets the EOF flag stored in regEof.
   89416                 :     */
   89417                 :     int rc, j1;
   89418                 : 
   89419              90 :     regEof = ++pParse->nMem;
   89420              90 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
   89421              90 :     VdbeComment((v, "SELECT eof flag"));
   89422              90 :     sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
   89423              90 :     addrSelect = sqlite3VdbeCurrentAddr(v)+2;
   89424              90 :     sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm);
   89425              90 :     j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
   89426              90 :     VdbeComment((v, "Jump over SELECT coroutine"));
   89427                 : 
   89428                 :     /* Resolve the expressions in the SELECT statement and execute it. */
   89429              90 :     rc = sqlite3Select(pParse, pSelect, &dest);
   89430              90 :     assert( pParse->nErr==0 || rc );
   89431              90 :     if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
   89432                 :       goto insert_cleanup;
   89433                 :     }
   89434              90 :     sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
   89435              90 :     sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);   /* yield X */
   89436              90 :     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
   89437              90 :     VdbeComment((v, "End of SELECT coroutine"));
   89438              90 :     sqlite3VdbeJumpHere(v, j1);                          /* label B: */
   89439                 : 
   89440              90 :     regFromSelect = dest.iMem;
   89441              90 :     assert( pSelect->pEList );
   89442              90 :     nColumn = pSelect->pEList->nExpr;
   89443              90 :     assert( dest.nMem==nColumn );
   89444                 : 
   89445                 :     /* Set useTempTable to TRUE if the result of the SELECT statement
   89446                 :     ** should be written into a temporary table (template 4).  Set to
   89447                 :     ** FALSE if each* row of the SELECT can be written directly into
   89448                 :     ** the destination table (template 3).
   89449                 :     **
   89450                 :     ** A temp table must be used if the table being updated is also one
   89451                 :     ** of the tables being read by the SELECT statement.  Also use a 
   89452                 :     ** temp table in the case of row triggers.
   89453                 :     */
   89454              90 :     if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
   89455               8 :       useTempTable = 1;
   89456                 :     }
   89457                 : 
   89458              90 :     if( useTempTable ){
   89459                 :       /* Invoke the coroutine to extract information from the SELECT
   89460                 :       ** and add it to a transient table srcTab.  The code generated
   89461                 :       ** here is from the 4th template:
   89462                 :       **
   89463                 :       **      B: open temp table
   89464                 :       **      L: yield X
   89465                 :       **         if EOF goto M
   89466                 :       **         insert row from R..R+n into temp table
   89467                 :       **         goto L
   89468                 :       **      M: ...
   89469                 :       */
   89470                 :       int regRec;          /* Register to hold packed record */
   89471                 :       int regTempRowid;    /* Register to hold temp table ROWID */
   89472                 :       int addrTop;         /* Label "L" */
   89473                 :       int addrIf;          /* Address of jump to M */
   89474                 : 
   89475               8 :       srcTab = pParse->nTab++;
   89476               8 :       regRec = sqlite3GetTempReg(pParse);
   89477               8 :       regTempRowid = sqlite3GetTempReg(pParse);
   89478               8 :       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
   89479               8 :       addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
   89480               8 :       addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
   89481               8 :       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
   89482               8 :       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
   89483               8 :       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
   89484               8 :       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
   89485               8 :       sqlite3VdbeJumpHere(v, addrIf);
   89486               8 :       sqlite3ReleaseTempReg(pParse, regRec);
   89487               8 :       sqlite3ReleaseTempReg(pParse, regTempRowid);
   89488                 :     }
   89489                 :   }else{
   89490                 :     /* This is the case if the data for the INSERT is coming from a VALUES
   89491                 :     ** clause
   89492                 :     */
   89493                 :     NameContext sNC;
   89494           20082 :     memset(&sNC, 0, sizeof(sNC));
   89495           20082 :     sNC.pParse = pParse;
   89496           20082 :     srcTab = -1;
   89497           20082 :     assert( useTempTable==0 );
   89498           20082 :     nColumn = pList ? pList->nExpr : 0;
   89499          130969 :     for(i=0; i<nColumn; i++){
   89500          110887 :       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
   89501               0 :         goto insert_cleanup;
   89502                 :       }
   89503                 :     }
   89504                 :   }
   89505                 : 
   89506                 :   /* Make sure the number of columns in the source data matches the number
   89507                 :   ** of columns to be inserted into the table.
   89508                 :   */
   89509           20172 :   if( IsVirtual(pTab) ){
   89510              20 :     for(i=0; i<pTab->nCol; i++){
   89511              16 :       nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
   89512                 :     }
   89513                 :   }
   89514           20172 :   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
   89515               0 :     sqlite3ErrorMsg(pParse, 
   89516                 :        "table %S has %d columns but %d values were supplied",
   89517               0 :        pTabList, 0, pTab->nCol-nHidden, nColumn);
   89518               0 :     goto insert_cleanup;
   89519                 :   }
   89520           20172 :   if( pColumn!=0 && nColumn!=pColumn->nId ){
   89521               0 :     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
   89522               0 :     goto insert_cleanup;
   89523                 :   }
   89524                 : 
   89525                 :   /* If the INSERT statement included an IDLIST term, then make sure
   89526                 :   ** all elements of the IDLIST really are columns of the table and 
   89527                 :   ** remember the column indices.
   89528                 :   **
   89529                 :   ** If the table has an INTEGER PRIMARY KEY column and that column
   89530                 :   ** is named in the IDLIST, then record in the keyColumn variable
   89531                 :   ** the index into IDLIST of the primary key column.  keyColumn is
   89532                 :   ** the index of the primary key as it appears in IDLIST, not as
   89533                 :   ** is appears in the original table.  (The index of the primary
   89534                 :   ** key in the original table is pTab->iPKey.)
   89535                 :   */
   89536           20172 :   if( pColumn ){
   89537           33918 :     for(i=0; i<pColumn->nId; i++){
   89538           28085 :       pColumn->a[i].idx = -1;
   89539                 :     }
   89540           33918 :     for(i=0; i<pColumn->nId; i++){
   89541          136751 :       for(j=0; j<pTab->nCol; j++){
   89542          136751 :         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
   89543           28085 :           pColumn->a[i].idx = j;
   89544           28085 :           if( j==pTab->iPKey ){
   89545            1161 :             keyColumn = i;
   89546                 :           }
   89547           28085 :           break;
   89548                 :         }
   89549                 :       }
   89550           28085 :       if( j>=pTab->nCol ){
   89551               0 :         if( sqlite3IsRowid(pColumn->a[i].zName) ){
   89552               0 :           keyColumn = i;
   89553                 :         }else{
   89554               0 :           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
   89555               0 :               pTabList, 0, pColumn->a[i].zName);
   89556               0 :           pParse->checkSchema = 1;
   89557               0 :           goto insert_cleanup;
   89558                 :         }
   89559                 :       }
   89560                 :     }
   89561                 :   }
   89562                 : 
   89563                 :   /* If there is no IDLIST term but the table has an integer primary
   89564                 :   ** key, the set the keyColumn variable to the primary key column index
   89565                 :   ** in the original table definition.
   89566                 :   */
   89567           20172 :   if( pColumn==0 && nColumn>0 ){
   89568           14339 :     keyColumn = pTab->iPKey;
   89569                 :   }
   89570                 :     
   89571                 :   /* Initialize the count of rows to be inserted
   89572                 :   */
   89573           20172 :   if( db->flags & SQLITE_CountRows ){
   89574               0 :     regRowCount = ++pParse->nMem;
   89575               0 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
   89576                 :   }
   89577                 : 
   89578                 :   /* If this is not a view, open the table and and all indices */
   89579           20172 :   if( !isView ){
   89580                 :     int nIdx;
   89581                 : 
   89582           20172 :     baseCur = pParse->nTab;
   89583           20172 :     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
   89584           20172 :     aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
   89585           20172 :     if( aRegIdx==0 ){
   89586               0 :       goto insert_cleanup;
   89587                 :     }
   89588           35396 :     for(i=0; i<nIdx; i++){
   89589           15224 :       aRegIdx[i] = ++pParse->nMem;
   89590                 :     }
   89591                 :   }
   89592                 : 
   89593                 :   /* This is the top of the main insertion loop */
   89594           20172 :   if( useTempTable ){
   89595                 :     /* This block codes the top of loop only.  The complete loop is the
   89596                 :     ** following pseudocode (template 4):
   89597                 :     **
   89598                 :     **         rewind temp table
   89599                 :     **      C: loop over rows of intermediate table
   89600                 :     **           transfer values form intermediate table into <table>
   89601                 :     **         end loop
   89602                 :     **      D: ...
   89603                 :     */
   89604               8 :     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
   89605               8 :     addrCont = sqlite3VdbeCurrentAddr(v);
   89606           20164 :   }else if( pSelect ){
   89607                 :     /* This block codes the top of loop only.  The complete loop is the
   89608                 :     ** following pseudocode (template 3):
   89609                 :     **
   89610                 :     **      C: yield X
   89611                 :     **         if EOF goto D
   89612                 :     **         insert the select result into <table> from R..R+n
   89613                 :     **         goto C
   89614                 :     **      D: ...
   89615                 :     */
   89616              82 :     addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
   89617              82 :     addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
   89618                 :   }
   89619                 : 
   89620                 :   /* Allocate registers for holding the rowid of the new row,
   89621                 :   ** the content of the new row, and the assemblied row record.
   89622                 :   */
   89623           20172 :   regRowid = regIns = pParse->nMem+1;
   89624           20172 :   pParse->nMem += pTab->nCol + 1;
   89625           20172 :   if( IsVirtual(pTab) ){
   89626               4 :     regRowid++;
   89627               4 :     pParse->nMem++;
   89628                 :   }
   89629           20172 :   regData = regRowid+1;
   89630                 : 
   89631                 :   /* Run the BEFORE and INSTEAD OF triggers, if there are any
   89632                 :   */
   89633           20172 :   endOfLoop = sqlite3VdbeMakeLabel(v);
   89634           20172 :   if( tmask & TRIGGER_BEFORE ){
   89635               0 :     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
   89636                 : 
   89637                 :     /* build the NEW.* reference row.  Note that if there is an INTEGER
   89638                 :     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
   89639                 :     ** translated into a unique ID for the row.  But on a BEFORE trigger,
   89640                 :     ** we do not know what the unique ID will be (because the insert has
   89641                 :     ** not happened yet) so we substitute a rowid of -1
   89642                 :     */
   89643               0 :     if( keyColumn<0 ){
   89644               0 :       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
   89645                 :     }else{
   89646                 :       int j1;
   89647               0 :       if( useTempTable ){
   89648               0 :         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
   89649                 :       }else{
   89650               0 :         assert( pSelect==0 );  /* Otherwise useTempTable is true */
   89651               0 :         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
   89652                 :       }
   89653               0 :       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
   89654               0 :       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
   89655               0 :       sqlite3VdbeJumpHere(v, j1);
   89656               0 :       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
   89657                 :     }
   89658                 : 
   89659                 :     /* Cannot have triggers on a virtual table. If it were possible,
   89660                 :     ** this block would have to account for hidden column.
   89661                 :     */
   89662               0 :     assert( !IsVirtual(pTab) );
   89663                 : 
   89664                 :     /* Create the new column data
   89665                 :     */
   89666               0 :     for(i=0; i<pTab->nCol; i++){
   89667               0 :       if( pColumn==0 ){
   89668               0 :         j = i;
   89669                 :       }else{
   89670               0 :         for(j=0; j<pColumn->nId; j++){
   89671               0 :           if( pColumn->a[j].idx==i ) break;
   89672                 :         }
   89673                 :       }
   89674               0 :       if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
   89675               0 :         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
   89676               0 :       }else if( useTempTable ){
   89677               0 :         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 
   89678                 :       }else{
   89679               0 :         assert( pSelect==0 ); /* Otherwise useTempTable is true */
   89680               0 :         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
   89681                 :       }
   89682                 :     }
   89683                 : 
   89684                 :     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
   89685                 :     ** do not attempt any conversions before assembling the record.
   89686                 :     ** If this is a real table, attempt conversions as required by the
   89687                 :     ** table column affinities.
   89688                 :     */
   89689               0 :     if( !isView ){
   89690               0 :       sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
   89691               0 :       sqlite3TableAffinityStr(v, pTab);
   89692                 :     }
   89693                 : 
   89694                 :     /* Fire BEFORE or INSTEAD OF triggers */
   89695               0 :     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
   89696               0 :         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
   89697                 : 
   89698               0 :     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
   89699                 :   }
   89700                 : 
   89701                 :   /* Push the record number for the new entry onto the stack.  The
   89702                 :   ** record number is a randomly generate integer created by NewRowid
   89703                 :   ** except when the table has an INTEGER PRIMARY KEY column, in which
   89704                 :   ** case the record number is the same as that column. 
   89705                 :   */
   89706           20172 :   if( !isView ){
   89707           20172 :     if( IsVirtual(pTab) ){
   89708                 :       /* The row that the VUpdate opcode will delete: none */
   89709               4 :       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
   89710                 :     }
   89711           20172 :     if( keyColumn>=0 ){
   89712            1992 :       if( useTempTable ){
   89713               0 :         sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
   89714            1992 :       }else if( pSelect ){
   89715              10 :         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
   89716                 :       }else{
   89717                 :         VdbeOp *pOp;
   89718            1982 :         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
   89719            1982 :         pOp = sqlite3VdbeGetOp(v, -1);
   89720            1982 :         if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
   89721             679 :           appendFlag = 1;
   89722             679 :           pOp->opcode = OP_NewRowid;
   89723             679 :           pOp->p1 = baseCur;
   89724             679 :           pOp->p2 = regRowid;
   89725             679 :           pOp->p3 = regAutoinc;
   89726                 :         }
   89727                 :       }
   89728                 :       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
   89729                 :       ** to generate a unique primary key value.
   89730                 :       */
   89731            1992 :       if( !appendFlag ){
   89732                 :         int j1;
   89733            1313 :         if( !IsVirtual(pTab) ){
   89734            1313 :           j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
   89735            1313 :           sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
   89736            1313 :           sqlite3VdbeJumpHere(v, j1);
   89737                 :         }else{
   89738               0 :           j1 = sqlite3VdbeCurrentAddr(v);
   89739               0 :           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
   89740                 :         }
   89741            1313 :         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
   89742                 :       }
   89743           18180 :     }else if( IsVirtual(pTab) ){
   89744               4 :       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
   89745                 :     }else{
   89746           18176 :       sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
   89747           18176 :       appendFlag = 1;
   89748                 :     }
   89749           20172 :     autoIncStep(pParse, regAutoinc, regRowid);
   89750                 : 
   89751                 :     /* Push onto the stack, data for all columns of the new entry, beginning
   89752                 :     ** with the first column.
   89753                 :     */
   89754           20172 :     nHidden = 0;
   89755          140830 :     for(i=0; i<pTab->nCol; i++){
   89756          120658 :       int iRegStore = regRowid+1+i;
   89757          120658 :       if( i==pTab->iPKey ){
   89758                 :         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
   89759                 :         ** Whenever this column is read, the record number will be substituted
   89760                 :         ** in its place.  So will fill this column with a NULL to avoid
   89761                 :         ** taking up data space with information that will never be used. */
   89762            5113 :         sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
   89763            5113 :         continue;
   89764                 :       }
   89765          115545 :       if( pColumn==0 ){
   89766           82361 :         if( IsHiddenColumn(&pTab->aCol[i]) ){
   89767               0 :           assert( IsVirtual(pTab) );
   89768               0 :           j = -1;
   89769               0 :           nHidden++;
   89770                 :         }else{
   89771           82361 :           j = i - nHidden;
   89772                 :         }
   89773                 :       }else{
   89774          145418 :         for(j=0; j<pColumn->nId; j++){
   89775          139158 :           if( pColumn->a[j].idx==i ) break;
   89776                 :         }
   89777                 :       }
   89778          115545 :       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
   89779            6260 :         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
   89780          109285 :       }else if( useTempTable ){
   89781              42 :         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
   89782          109243 :       }else if( pSelect ){
   89783             338 :         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
   89784                 :       }else{
   89785          108905 :         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
   89786                 :       }
   89787                 :     }
   89788                 : 
   89789                 :     /* Generate code to check constraints and generate index keys and
   89790                 :     ** do the insertion.
   89791                 :     */
   89792                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   89793           20172 :     if( IsVirtual(pTab) ){
   89794               4 :       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
   89795               4 :       sqlite3VtabMakeWritable(pParse, pTab);
   89796               4 :       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
   89797               4 :       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
   89798               4 :       sqlite3MayAbort(pParse);
   89799                 :     }else
   89800                 : #endif
   89801                 :     {
   89802                 :       int isReplace;    /* Set to true if constraints may cause a replace */
   89803           20168 :       sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
   89804                 :           keyColumn>=0, 0, onError, endOfLoop, &isReplace
   89805                 :       );
   89806           20168 :       sqlite3FkCheck(pParse, pTab, 0, regIns);
   89807           20168 :       sqlite3CompleteInsertion(
   89808                 :           pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
   89809                 :       );
   89810                 :     }
   89811                 :   }
   89812                 : 
   89813                 :   /* Update the count of rows that are inserted
   89814                 :   */
   89815           20172 :   if( (db->flags & SQLITE_CountRows)!=0 ){
   89816               0 :     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
   89817                 :   }
   89818                 : 
   89819           20172 :   if( pTrigger ){
   89820                 :     /* Code AFTER triggers */
   89821             832 :     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 
   89822             832 :         pTab, regData-2-pTab->nCol, onError, endOfLoop);
   89823                 :   }
   89824                 : 
   89825                 :   /* The bottom of the main insertion loop, if the data source
   89826                 :   ** is a SELECT statement.
   89827                 :   */
   89828           20172 :   sqlite3VdbeResolveLabel(v, endOfLoop);
   89829           20172 :   if( useTempTable ){
   89830               8 :     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
   89831               8 :     sqlite3VdbeJumpHere(v, addrInsTop);
   89832               8 :     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
   89833           20164 :   }else if( pSelect ){
   89834              82 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
   89835              82 :     sqlite3VdbeJumpHere(v, addrInsTop);
   89836                 :   }
   89837                 : 
   89838           20172 :   if( !IsVirtual(pTab) && !isView ){
   89839                 :     /* Close all tables opened */
   89840           20168 :     sqlite3VdbeAddOp1(v, OP_Close, baseCur);
   89841           35392 :     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
   89842           15224 :       sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
   89843                 :     }
   89844                 :   }
   89845                 : 
   89846                 : insert_end:
   89847                 :   /* Update the sqlite_sequence table by storing the content of the
   89848                 :   ** maximum rowid counter values recorded while inserting into
   89849                 :   ** autoincrement tables.
   89850                 :   */
   89851           20180 :   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
   89852            8519 :     sqlite3AutoincrementEnd(pParse);
   89853                 :   }
   89854                 : 
   89855                 :   /*
   89856                 :   ** Return the number of rows inserted. If this routine is 
   89857                 :   ** generating code because of a call to sqlite3NestedParse(), do not
   89858                 :   ** invoke the callback function.
   89859                 :   */
   89860           20180 :   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
   89861               0 :     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
   89862               0 :     sqlite3VdbeSetNumCols(v, 1);
   89863               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
   89864                 :   }
   89865                 : 
   89866                 : insert_cleanup:
   89867           20180 :   sqlite3SrcListDelete(db, pTabList);
   89868           20180 :   sqlite3ExprListDelete(db, pList);
   89869           20180 :   sqlite3SelectDelete(db, pSelect);
   89870           20180 :   sqlite3IdListDelete(db, pColumn);
   89871           20180 :   sqlite3DbFree(db, aRegIdx);
   89872           20180 : }
   89873                 : 
   89874                 : /* Make sure "isView" and other macros defined above are undefined. Otherwise
   89875                 : ** thely may interfere with compilation of other functions in this file
   89876                 : ** (or in another file, if this file becomes part of the amalgamation).  */
   89877                 : #ifdef isView
   89878                 :  #undef isView
   89879                 : #endif
   89880                 : #ifdef pTrigger
   89881                 :  #undef pTrigger
   89882                 : #endif
   89883                 : #ifdef tmask
   89884                 :  #undef tmask
   89885                 : #endif
   89886                 : 
   89887                 : 
   89888                 : /*
   89889                 : ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
   89890                 : **
   89891                 : ** The input is a range of consecutive registers as follows:
   89892                 : **
   89893                 : **    1.  The rowid of the row after the update.
   89894                 : **
   89895                 : **    2.  The data in the first column of the entry after the update.
   89896                 : **
   89897                 : **    i.  Data from middle columns...
   89898                 : **
   89899                 : **    N.  The data in the last column of the entry after the update.
   89900                 : **
   89901                 : ** The regRowid parameter is the index of the register containing (1).
   89902                 : **
   89903                 : ** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
   89904                 : ** the address of a register containing the rowid before the update takes
   89905                 : ** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
   89906                 : ** is false, indicating an INSERT statement, then a non-zero rowidChng 
   89907                 : ** indicates that the rowid was explicitly specified as part of the
   89908                 : ** INSERT statement. If rowidChng is false, it means that  the rowid is
   89909                 : ** computed automatically in an insert or that the rowid value is not 
   89910                 : ** modified by an update.
   89911                 : **
   89912                 : ** The code generated by this routine store new index entries into
   89913                 : ** registers identified by aRegIdx[].  No index entry is created for
   89914                 : ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
   89915                 : ** the same as the order of indices on the linked list of indices
   89916                 : ** attached to the table.
   89917                 : **
   89918                 : ** This routine also generates code to check constraints.  NOT NULL,
   89919                 : ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
   89920                 : ** then the appropriate action is performed.  There are five possible
   89921                 : ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
   89922                 : **
   89923                 : **  Constraint type  Action       What Happens
   89924                 : **  ---------------  ----------   ----------------------------------------
   89925                 : **  any              ROLLBACK     The current transaction is rolled back and
   89926                 : **                                sqlite3_exec() returns immediately with a
   89927                 : **                                return code of SQLITE_CONSTRAINT.
   89928                 : **
   89929                 : **  any              ABORT        Back out changes from the current command
   89930                 : **                                only (do not do a complete rollback) then
   89931                 : **                                cause sqlite3_exec() to return immediately
   89932                 : **                                with SQLITE_CONSTRAINT.
   89933                 : **
   89934                 : **  any              FAIL         Sqlite3_exec() returns immediately with a
   89935                 : **                                return code of SQLITE_CONSTRAINT.  The
   89936                 : **                                transaction is not rolled back and any
   89937                 : **                                prior changes are retained.
   89938                 : **
   89939                 : **  any              IGNORE       The record number and data is popped from
   89940                 : **                                the stack and there is an immediate jump
   89941                 : **                                to label ignoreDest.
   89942                 : **
   89943                 : **  NOT NULL         REPLACE      The NULL value is replace by the default
   89944                 : **                                value for that column.  If the default value
   89945                 : **                                is NULL, the action is the same as ABORT.
   89946                 : **
   89947                 : **  UNIQUE           REPLACE      The other row that conflicts with the row
   89948                 : **                                being inserted is removed.
   89949                 : **
   89950                 : **  CHECK            REPLACE      Illegal.  The results in an exception.
   89951                 : **
   89952                 : ** Which action to take is determined by the overrideError parameter.
   89953                 : ** Or if overrideError==OE_Default, then the pParse->onError parameter
   89954                 : ** is used.  Or if pParse->onError==OE_Default then the onError value
   89955                 : ** for the constraint is used.
   89956                 : **
   89957                 : ** The calling routine must open a read/write cursor for pTab with
   89958                 : ** cursor number "baseCur".  All indices of pTab must also have open
   89959                 : ** read/write cursors with cursor number baseCur+i for the i-th cursor.
   89960                 : ** Except, if there is no possibility of a REPLACE action then
   89961                 : ** cursors do not need to be open for indices where aRegIdx[i]==0.
   89962                 : */
   89963           33420 : SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
   89964                 :   Parse *pParse,      /* The parser context */
   89965                 :   Table *pTab,        /* the table into which we are inserting */
   89966                 :   int baseCur,        /* Index of a read/write cursor pointing at pTab */
   89967                 :   int regRowid,       /* Index of the range of input registers */
   89968                 :   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
   89969                 :   int rowidChng,      /* True if the rowid might collide with existing entry */
   89970                 :   int isUpdate,       /* True for UPDATE, False for INSERT */
   89971                 :   int overrideError,  /* Override onError to this if not OE_Default */
   89972                 :   int ignoreDest,     /* Jump to this label on an OE_Ignore resolution */
   89973                 :   int *pbMayReplace   /* OUT: Set to true if constraint may cause a replace */
   89974                 : ){
   89975                 :   int i;              /* loop counter */
   89976                 :   Vdbe *v;            /* VDBE under constrution */
   89977                 :   int nCol;           /* Number of columns */
   89978                 :   int onError;        /* Conflict resolution strategy */
   89979                 :   int j1;             /* Addresss of jump instruction */
   89980           33420 :   int j2 = 0, j3;     /* Addresses of jump instructions */
   89981                 :   int regData;        /* Register containing first data column */
   89982                 :   int iCur;           /* Table cursor number */
   89983                 :   Index *pIdx;         /* Pointer to one of the indices */
   89984           33420 :   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
   89985           33420 :   int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
   89986                 : 
   89987           33420 :   v = sqlite3GetVdbe(pParse);
   89988           33420 :   assert( v!=0 );
   89989           33420 :   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
   89990           33420 :   nCol = pTab->nCol;
   89991           33420 :   regData = regRowid + 1;
   89992                 : 
   89993                 :   /* Test all NOT NULL constraints.
   89994                 :   */
   89995          280324 :   for(i=0; i<nCol; i++){
   89996          246904 :     if( i==pTab->iPKey ){
   89997           10699 :       continue;
   89998                 :     }
   89999          236205 :     onError = pTab->aCol[i].notNull;
   90000          236205 :     if( onError==OE_None ) continue;
   90001           10699 :     if( overrideError!=OE_Default ){
   90002            1732 :       onError = overrideError;
   90003            8967 :     }else if( onError==OE_Default ){
   90004            8967 :       onError = OE_Abort;
   90005                 :     }
   90006           10699 :     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
   90007             208 :       onError = OE_Abort;
   90008                 :     }
   90009           10699 :     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
   90010                 :         || onError==OE_Ignore || onError==OE_Replace );
   90011           10699 :     switch( onError ){
   90012                 :       case OE_Abort:
   90013            9175 :         sqlite3MayAbort(pParse);
   90014                 :       case OE_Rollback:
   90015                 :       case OE_Fail: {
   90016                 :         char *zMsg;
   90017            9175 :         sqlite3VdbeAddOp3(v, OP_HaltIfNull,
   90018                 :                                   SQLITE_CONSTRAINT, onError, regData+i);
   90019            9175 :         zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
   90020            9175 :                               pTab->zName, pTab->aCol[i].zName);
   90021            9175 :         sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
   90022            9175 :         break;
   90023                 :       }
   90024                 :       case OE_Ignore: {
   90025            1467 :         sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
   90026            1467 :         break;
   90027                 :       }
   90028                 :       default: {
   90029              57 :         assert( onError==OE_Replace );
   90030              57 :         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
   90031              57 :         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
   90032              57 :         sqlite3VdbeJumpHere(v, j1);
   90033              57 :         break;
   90034                 :       }
   90035                 :     }
   90036                 :   }
   90037                 : 
   90038                 :   /* Test all CHECK constraints
   90039                 :   */
   90040                 : #ifndef SQLITE_OMIT_CHECK
   90041           33420 :   if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
   90042               0 :     int allOk = sqlite3VdbeMakeLabel(v);
   90043               0 :     pParse->ckBase = regData;
   90044               0 :     sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
   90045               0 :     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
   90046               0 :     if( onError==OE_Ignore ){
   90047               0 :       sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
   90048                 :     }else{
   90049               0 :       if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
   90050               0 :       sqlite3HaltConstraint(pParse, onError, 0, 0);
   90051                 :     }
   90052               0 :     sqlite3VdbeResolveLabel(v, allOk);
   90053                 :   }
   90054                 : #endif /* !defined(SQLITE_OMIT_CHECK) */
   90055                 : 
   90056                 :   /* If we have an INTEGER PRIMARY KEY, make sure the primary key
   90057                 :   ** of the new record does not previously exist.  Except, if this
   90058                 :   ** is an UPDATE and the primary key is not changing, that is OK.
   90059                 :   */
   90060           33420 :   if( rowidChng ){
   90061            1992 :     onError = pTab->keyConf;
   90062            1992 :     if( overrideError!=OE_Default ){
   90063             698 :       onError = overrideError;
   90064            1294 :     }else if( onError==OE_Default ){
   90065            1294 :       onError = OE_Abort;
   90066                 :     }
   90067                 :     
   90068            1992 :     if( isUpdate ){
   90069               0 :       j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
   90070                 :     }
   90071            1992 :     j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
   90072            1992 :     switch( onError ){
   90073                 :       default: {
   90074               0 :         onError = OE_Abort;
   90075                 :         /* Fall thru into the next case */
   90076                 :       }
   90077                 :       case OE_Rollback:
   90078                 :       case OE_Abort:
   90079                 :       case OE_Fail: {
   90080            1294 :         sqlite3HaltConstraint(
   90081                 :           pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
   90082            1294 :         break;
   90083                 :       }
   90084                 :       case OE_Replace: {
   90085                 :         /* If there are DELETE triggers on this table and the
   90086                 :         ** recursive-triggers flag is set, call GenerateRowDelete() to
   90087                 :         ** remove the conflicting row from the the table. This will fire
   90088                 :         ** the triggers and remove both the table and index b-tree entries.
   90089                 :         **
   90090                 :         ** Otherwise, if there are no triggers or the recursive-triggers
   90091                 :         ** flag is not set, but the table has one or more indexes, call 
   90092                 :         ** GenerateRowIndexDelete(). This removes the index b-tree entries 
   90093                 :         ** only. The table b-tree entry will be replaced by the new entry 
   90094                 :         ** when it is inserted.  
   90095                 :         **
   90096                 :         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
   90097                 :         ** also invoke MultiWrite() to indicate that this VDBE may require
   90098                 :         ** statement rollback (if the statement is aborted after the delete
   90099                 :         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
   90100                 :         ** but being more selective here allows statements like:
   90101                 :         **
   90102                 :         **   REPLACE INTO t(rowid) VALUES($newrowid)
   90103                 :         **
   90104                 :         ** to run without a statement journal if there are no indexes on the
   90105                 :         ** table.
   90106                 :         */
   90107             447 :         Trigger *pTrigger = 0;
   90108             447 :         if( pParse->db->flags&SQLITE_RecTriggers ){
   90109               0 :           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
   90110                 :         }
   90111             447 :         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
   90112               0 :           sqlite3MultiWrite(pParse);
   90113               0 :           sqlite3GenerateRowDelete(
   90114                 :               pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
   90115                 :           );
   90116             447 :         }else if( pTab->pIndex ){
   90117             441 :           sqlite3MultiWrite(pParse);
   90118             441 :           sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
   90119                 :         }
   90120             447 :         seenReplace = 1;
   90121             447 :         break;
   90122                 :       }
   90123                 :       case OE_Ignore: {
   90124             251 :         assert( seenReplace==0 );
   90125             251 :         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
   90126             251 :         break;
   90127                 :       }
   90128                 :     }
   90129            1992 :     sqlite3VdbeJumpHere(v, j3);
   90130            1992 :     if( isUpdate ){
   90131               0 :       sqlite3VdbeJumpHere(v, j2);
   90132                 :     }
   90133                 :   }
   90134                 : 
   90135                 :   /* Test all UNIQUE constraints by creating entries for each UNIQUE
   90136                 :   ** index and making sure that duplicate entries do not already exist.
   90137                 :   ** Add the new records to the indices as we go.
   90138                 :   */
   90139           66662 :   for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
   90140                 :     int regIdx;
   90141                 :     int regR;
   90142                 : 
   90143           33242 :     if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
   90144                 : 
   90145                 :     /* Create a key for accessing the index entry */
   90146           18079 :     regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
   90147           44902 :     for(i=0; i<pIdx->nColumn; i++){
   90148           26823 :       int idx = pIdx->aiColumn[i];
   90149           26823 :       if( idx==pTab->iPKey ){
   90150               0 :         sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
   90151                 :       }else{
   90152           26823 :         sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
   90153                 :       }
   90154                 :     }
   90155           18079 :     sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
   90156           18079 :     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
   90157           18079 :     sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
   90158           18079 :     sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
   90159                 : 
   90160                 :     /* Find out what action to take in case there is an indexing conflict */
   90161           18079 :     onError = pIdx->onError;
   90162           18079 :     if( onError==OE_None ){ 
   90163           10781 :       sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
   90164           10781 :       continue;  /* pIdx is not a UNIQUE index */
   90165                 :     }
   90166            7298 :     if( overrideError!=OE_Default ){
   90167            1097 :       onError = overrideError;
   90168            6201 :     }else if( onError==OE_Default ){
   90169            4450 :       onError = OE_Abort;
   90170                 :     }
   90171            7298 :     if( seenReplace ){
   90172             159 :       if( onError==OE_Ignore ) onError = OE_Replace;
   90173             159 :       else if( onError==OE_Fail ) onError = OE_Abort;
   90174                 :     }
   90175                 :     
   90176                 :     /* Check to see if the new index entry will be unique */
   90177            7298 :     regR = sqlite3GetTempReg(pParse);
   90178            7298 :     sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
   90179            7298 :     j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
   90180                 :                            regR, SQLITE_INT_TO_PTR(regIdx),
   90181                 :                            P4_INT32);
   90182            7298 :     sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
   90183                 : 
   90184                 :     /* Generate code that executes if the new index entry is not unique */
   90185            7298 :     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
   90186                 :         || onError==OE_Ignore || onError==OE_Replace );
   90187            7298 :     switch( onError ){
   90188                 :       case OE_Rollback:
   90189                 :       case OE_Abort:
   90190                 :       case OE_Fail: {
   90191                 :         int j;
   90192                 :         StrAccum errMsg;
   90193                 :         const char *zSep;
   90194                 :         char *zErr;
   90195                 : 
   90196            6185 :         sqlite3StrAccumInit(&errMsg, 0, 0, 200);
   90197            6185 :         errMsg.db = pParse->db;
   90198            6185 :         zSep = pIdx->nColumn>1 ? "columns " : "column ";
   90199           15508 :         for(j=0; j<pIdx->nColumn; j++){
   90200            9323 :           char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
   90201            9323 :           sqlite3StrAccumAppend(&errMsg, zSep, -1);
   90202            9323 :           zSep = ", ";
   90203            9323 :           sqlite3StrAccumAppend(&errMsg, zCol, -1);
   90204                 :         }
   90205            6185 :         sqlite3StrAccumAppend(&errMsg,
   90206            6185 :             pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
   90207            6185 :         zErr = sqlite3StrAccumFinish(&errMsg);
   90208            6185 :         sqlite3HaltConstraint(pParse, onError, zErr, 0);
   90209            6185 :         sqlite3DbFree(errMsg.db, zErr);
   90210            6185 :         break;
   90211                 :       }
   90212                 :       case OE_Ignore: {
   90213             884 :         assert( seenReplace==0 );
   90214             884 :         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
   90215             884 :         break;
   90216                 :       }
   90217                 :       default: {
   90218             229 :         Trigger *pTrigger = 0;
   90219             229 :         assert( onError==OE_Replace );
   90220             229 :         sqlite3MultiWrite(pParse);
   90221             229 :         if( pParse->db->flags&SQLITE_RecTriggers ){
   90222               0 :           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
   90223                 :         }
   90224             229 :         sqlite3GenerateRowDelete(
   90225                 :             pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
   90226                 :         );
   90227             229 :         seenReplace = 1;
   90228             229 :         break;
   90229                 :       }
   90230                 :     }
   90231            7298 :     sqlite3VdbeJumpHere(v, j3);
   90232            7298 :     sqlite3ReleaseTempReg(pParse, regR);
   90233                 :   }
   90234                 :   
   90235           33420 :   if( pbMayReplace ){
   90236           20168 :     *pbMayReplace = seenReplace;
   90237                 :   }
   90238           33420 : }
   90239                 : 
   90240                 : /*
   90241                 : ** This routine generates code to finish the INSERT or UPDATE operation
   90242                 : ** that was started by a prior call to sqlite3GenerateConstraintChecks.
   90243                 : ** A consecutive range of registers starting at regRowid contains the
   90244                 : ** rowid and the content to be inserted.
   90245                 : **
   90246                 : ** The arguments to this routine should be the same as the first six
   90247                 : ** arguments to sqlite3GenerateConstraintChecks.
   90248                 : */
   90249           33420 : SQLITE_PRIVATE void sqlite3CompleteInsertion(
   90250                 :   Parse *pParse,      /* The parser context */
   90251                 :   Table *pTab,        /* the table into which we are inserting */
   90252                 :   int baseCur,        /* Index of a read/write cursor pointing at pTab */
   90253                 :   int regRowid,       /* Range of content */
   90254                 :   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
   90255                 :   int isUpdate,       /* True for UPDATE, False for INSERT */
   90256                 :   int appendBias,     /* True if this is likely to be an append */
   90257                 :   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
   90258                 : ){
   90259                 :   int i;
   90260                 :   Vdbe *v;
   90261                 :   int nIdx;
   90262                 :   Index *pIdx;
   90263                 :   u8 pik_flags;
   90264                 :   int regData;
   90265                 :   int regRec;
   90266                 : 
   90267           33420 :   v = sqlite3GetVdbe(pParse);
   90268           33420 :   assert( v!=0 );
   90269           33420 :   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
   90270           33420 :   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
   90271           66662 :   for(i=nIdx-1; i>=0; i--){
   90272           33242 :     if( aRegIdx[i]==0 ) continue;
   90273           18079 :     sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
   90274           18079 :     if( useSeekResult ){
   90275           14426 :       sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
   90276                 :     }
   90277                 :   }
   90278           33420 :   regData = regRowid + 1;
   90279           33420 :   regRec = sqlite3GetTempReg(pParse);
   90280           33420 :   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
   90281           33420 :   sqlite3TableAffinityStr(v, pTab);
   90282           33420 :   sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
   90283           33420 :   if( pParse->nested ){
   90284           18781 :     pik_flags = 0;
   90285                 :   }else{
   90286           14639 :     pik_flags = OPFLAG_NCHANGE;
   90287           14639 :     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
   90288                 :   }
   90289           33420 :   if( appendBias ){
   90290           18855 :     pik_flags |= OPFLAG_APPEND;
   90291                 :   }
   90292           33420 :   if( useSeekResult ){
   90293           19651 :     pik_flags |= OPFLAG_USESEEKRESULT;
   90294                 :   }
   90295           33420 :   sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
   90296           33420 :   if( !pParse->nested ){
   90297           14639 :     sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
   90298                 :   }
   90299           33420 :   sqlite3VdbeChangeP5(v, pik_flags);
   90300           33420 : }
   90301                 : 
   90302                 : /*
   90303                 : ** Generate code that will open cursors for a table and for all
   90304                 : ** indices of that table.  The "baseCur" parameter is the cursor number used
   90305                 : ** for the table.  Indices are opened on subsequent cursors.
   90306                 : **
   90307                 : ** Return the number of indices on the table.
   90308                 : */
   90309           28287 : SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
   90310                 :   Parse *pParse,   /* Parsing context */
   90311                 :   Table *pTab,     /* Table to be opened */
   90312                 :   int baseCur,     /* Cursor number assigned to the table */
   90313                 :   int op           /* OP_OpenRead or OP_OpenWrite */
   90314                 : ){
   90315                 :   int i;
   90316                 :   int iDb;
   90317                 :   Index *pIdx;
   90318                 :   Vdbe *v;
   90319                 : 
   90320           28287 :   if( IsVirtual(pTab) ) return 0;
   90321           28283 :   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   90322           28283 :   v = sqlite3GetVdbe(pParse);
   90323           28283 :   assert( v!=0 );
   90324           28283 :   sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
   90325           53330 :   for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
   90326           25047 :     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   90327           25047 :     assert( pIdx->pSchema==pTab->pSchema );
   90328           25047 :     sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
   90329                 :                       (char*)pKey, P4_KEYINFO_HANDOFF);
   90330           25047 :     VdbeComment((v, "%s", pIdx->zName));
   90331                 :   }
   90332           28283 :   if( pParse->nTab<baseCur+i ){
   90333           20220 :     pParse->nTab = baseCur+i;
   90334                 :   }
   90335           28283 :   return i-1;
   90336                 : }
   90337                 : 
   90338                 : 
   90339                 : #ifdef SQLITE_TEST
   90340                 : /*
   90341                 : ** The following global variable is incremented whenever the
   90342                 : ** transfer optimization is used.  This is used for testing
   90343                 : ** purposes only - to make sure the transfer optimization really
   90344                 : ** is happening when it is suppose to.
   90345                 : */
   90346                 : SQLITE_API int sqlite3_xferopt_count;
   90347                 : #endif /* SQLITE_TEST */
   90348                 : 
   90349                 : 
   90350                 : #ifndef SQLITE_OMIT_XFER_OPT
   90351                 : /*
   90352                 : ** Check to collation names to see if they are compatible.
   90353                 : */
   90354             168 : static int xferCompatibleCollation(const char *z1, const char *z2){
   90355             168 :   if( z1==0 ){
   90356              94 :     return z2==0;
   90357                 :   }
   90358              74 :   if( z2==0 ){
   90359               0 :     return 0;
   90360                 :   }
   90361              74 :   return sqlite3StrICmp(z1, z2)==0;
   90362                 : }
   90363                 : 
   90364                 : 
   90365                 : /*
   90366                 : ** Check to see if index pSrc is compatible as a source of data
   90367                 : ** for index pDest in an insert transfer optimization.  The rules
   90368                 : ** for a compatible index:
   90369                 : **
   90370                 : **    *   The index is over the same set of columns
   90371                 : **    *   The same DESC and ASC markings occurs on all columns
   90372                 : **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
   90373                 : **    *   The same collating sequence on each column
   90374                 : */
   90375             130 : static int xferCompatibleIndex(Index *pDest, Index *pSrc){
   90376                 :   int i;
   90377             130 :   assert( pDest && pSrc );
   90378             130 :   assert( pDest->pTable!=pSrc->pTable );
   90379             130 :   if( pDest->nColumn!=pSrc->nColumn ){
   90380              10 :     return 0;   /* Different number of columns */
   90381                 :   }
   90382             120 :   if( pDest->onError!=pSrc->onError ){
   90383              20 :     return 0;   /* Different conflict resolution strategies */
   90384                 :   }
   90385             174 :   for(i=0; i<pSrc->nColumn; i++){
   90386             116 :     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
   90387              42 :       return 0;   /* Different columns indexed */
   90388                 :     }
   90389              74 :     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
   90390               0 :       return 0;   /* Different sort orders */
   90391                 :     }
   90392              74 :     if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
   90393               0 :       return 0;   /* Different collating sequences */
   90394                 :     }
   90395                 :   }
   90396                 : 
   90397                 :   /* If no test above fails then the indices must be compatible */
   90398              58 :   return 1;
   90399                 : }
   90400                 : 
   90401                 : /*
   90402                 : ** Attempt the transfer optimization on INSERTs of the form
   90403                 : **
   90404                 : **     INSERT INTO tab1 SELECT * FROM tab2;
   90405                 : **
   90406                 : ** The xfer optimization transfers raw records from tab2 over to tab1.  
   90407                 : ** Columns are not decoded and reassemblied, which greatly improves
   90408                 : ** performance.  Raw index records are transferred in the same way.
   90409                 : **
   90410                 : ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
   90411                 : ** There are lots of rules for determining compatibility - see comments
   90412                 : ** embedded in the code for details.
   90413                 : **
   90414                 : ** This routine returns TRUE if the optimization is guaranteed to be used.
   90415                 : ** Sometimes the xfer optimization will only work if the destination table
   90416                 : ** is empty - a factor that can only be determined at run-time.  In that
   90417                 : ** case, this routine generates code for the xfer optimization but also
   90418                 : ** does a test to see if the destination table is empty and jumps over the
   90419                 : ** xfer optimization code if the test fails.  In that case, this routine
   90420                 : ** returns FALSE so that the caller will know to go ahead and generate
   90421                 : ** an unoptimized transfer.  This routine also returns FALSE if there
   90422                 : ** is no chance that the xfer optimization can be applied.
   90423                 : **
   90424                 : ** This optimization is particularly useful at making VACUUM run faster.
   90425                 : */
   90426           14347 : static int xferOptimization(
   90427                 :   Parse *pParse,        /* Parser context */
   90428                 :   Table *pDest,         /* The table we are inserting into */
   90429                 :   Select *pSelect,      /* A SELECT statement to use as the data source */
   90430                 :   int onError,          /* How to handle constraint errors */
   90431                 :   int iDbDest           /* The database of pDest */
   90432                 : ){
   90433                 :   ExprList *pEList;                /* The result set of the SELECT */
   90434                 :   Table *pSrc;                     /* The table in the FROM clause of SELECT */
   90435                 :   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
   90436                 :   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
   90437                 :   int i;                           /* Loop counter */
   90438                 :   int iDbSrc;                      /* The database of pSrc */
   90439                 :   int iSrc, iDest;                 /* Cursors from source and destination */
   90440                 :   int addr1, addr2;                /* Loop addresses */
   90441                 :   int emptyDestTest;               /* Address of test for empty pDest */
   90442                 :   int emptySrcTest;                /* Address of test for empty pSrc */
   90443                 :   Vdbe *v;                         /* The VDBE we are building */
   90444                 :   KeyInfo *pKey;                   /* Key information for an index */
   90445                 :   int regAutoinc;                  /* Memory register used by AUTOINC */
   90446           14347 :   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
   90447                 :   int regData, regRowid;           /* Registers holding data and rowid */
   90448                 : 
   90449           14347 :   if( pSelect==0 ){
   90450           14288 :     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
   90451                 :   }
   90452              59 :   if( sqlite3TriggerList(pParse, pDest) ){
   90453               0 :     return 0;   /* tab1 must not have triggers */
   90454                 :   }
   90455                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   90456              59 :   if( pDest->tabFlags & TF_Virtual ){
   90457               0 :     return 0;   /* tab1 must not be a virtual table */
   90458                 :   }
   90459                 : #endif
   90460              59 :   if( onError==OE_Default ){
   90461              53 :     if( pDest->iPKey>=0 ) onError = pDest->keyConf;
   90462              53 :     if( onError==OE_Default ) onError = OE_Abort;
   90463                 :   }
   90464              59 :   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
   90465              59 :   if( pSelect->pSrc->nSrc!=1 ){
   90466               3 :     return 0;   /* FROM clause must have exactly one term */
   90467                 :   }
   90468              56 :   if( pSelect->pSrc->a[0].pSelect ){
   90469               0 :     return 0;   /* FROM clause cannot contain a subquery */
   90470                 :   }
   90471              56 :   if( pSelect->pWhere ){
   90472              35 :     return 0;   /* SELECT may not have a WHERE clause */
   90473                 :   }
   90474              21 :   if( pSelect->pOrderBy ){
   90475               0 :     return 0;   /* SELECT may not have an ORDER BY clause */
   90476                 :   }
   90477                 :   /* Do not need to test for a HAVING clause.  If HAVING is present but
   90478                 :   ** there is no ORDER BY, we will get an error. */
   90479              21 :   if( pSelect->pGroupBy ){
   90480               0 :     return 0;   /* SELECT may not have a GROUP BY clause */
   90481                 :   }
   90482              21 :   if( pSelect->pLimit ){
   90483               0 :     return 0;   /* SELECT may not have a LIMIT clause */
   90484                 :   }
   90485              21 :   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
   90486              21 :   if( pSelect->pPrior ){
   90487               0 :     return 0;   /* SELECT may not be a compound query */
   90488                 :   }
   90489              21 :   if( pSelect->selFlags & SF_Distinct ){
   90490               0 :     return 0;   /* SELECT may not be DISTINCT */
   90491                 :   }
   90492              21 :   pEList = pSelect->pEList;
   90493              21 :   assert( pEList!=0 );
   90494              21 :   if( pEList->nExpr!=1 ){
   90495               2 :     return 0;   /* The result set must have exactly one column */
   90496                 :   }
   90497              19 :   assert( pEList->a[0].pExpr );
   90498              19 :   if( pEList->a[0].pExpr->op!=TK_ALL ){
   90499               0 :     return 0;   /* The result set must be the special operator "*" */
   90500                 :   }
   90501                 : 
   90502                 :   /* At this point we have established that the statement is of the
   90503                 :   ** correct syntactic form to participate in this optimization.  Now
   90504                 :   ** we have to check the semantics.
   90505                 :   */
   90506              19 :   pItem = pSelect->pSrc->a;
   90507              19 :   pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase);
   90508              19 :   if( pSrc==0 ){
   90509               0 :     return 0;   /* FROM clause does not contain a real table */
   90510                 :   }
   90511              19 :   if( pSrc==pDest ){
   90512               0 :     return 0;   /* tab1 and tab2 may not be the same table */
   90513                 :   }
   90514                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
   90515              19 :   if( pSrc->tabFlags & TF_Virtual ){
   90516               0 :     return 0;   /* tab2 must not be a virtual table */
   90517                 :   }
   90518                 : #endif
   90519              19 :   if( pSrc->pSelect ){
   90520               0 :     return 0;   /* tab2 may not be a view */
   90521                 :   }
   90522              19 :   if( pDest->nCol!=pSrc->nCol ){
   90523               0 :     return 0;   /* Number of columns must be the same in tab1 and tab2 */
   90524                 :   }
   90525              19 :   if( pDest->iPKey!=pSrc->iPKey ){
   90526               0 :     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
   90527                 :   }
   90528             113 :   for(i=0; i<pDest->nCol; i++){
   90529              94 :     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
   90530               0 :       return 0;    /* Affinity must be the same on all columns */
   90531                 :     }
   90532              94 :     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
   90533               0 :       return 0;    /* Collating sequence must be the same on all columns */
   90534                 :     }
   90535              94 :     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
   90536               0 :       return 0;    /* tab2 must be NOT NULL if tab1 is */
   90537                 :     }
   90538                 :   }
   90539              48 :   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
   90540              29 :     if( pDestIdx->onError!=OE_None ){
   90541              12 :       destHasUniqueIdx = 1;
   90542                 :     }
   90543              65 :     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
   90544              65 :       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
   90545                 :     }
   90546              29 :     if( pSrcIdx==0 ){
   90547               0 :       return 0;    /* pDestIdx has no corresponding index in pSrc */
   90548                 :     }
   90549                 :   }
   90550                 : #ifndef SQLITE_OMIT_CHECK
   90551              19 :   if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
   90552               0 :     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
   90553                 :   }
   90554                 : #endif
   90555                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   90556                 :   /* Disallow the transfer optimization if the destination table constains
   90557                 :   ** any foreign key constraints.  This is more restrictive than necessary.
   90558                 :   ** But the main beneficiary of the transfer optimization is the VACUUM 
   90559                 :   ** command, and the VACUUM command disables foreign key constraints.  So
   90560                 :   ** the extra complication to make this rule less restrictive is probably
   90561                 :   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
   90562                 :   */
   90563              19 :   if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
   90564               0 :     return 0;
   90565                 :   }
   90566                 : #endif
   90567              19 :   if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
   90568               0 :     return 0;  /* xfer opt does not play well with PRAGMA count_changes */
   90569                 :   }
   90570                 : 
   90571                 :   /* If we get this far, it means that the xfer optimization is at
   90572                 :   ** least a possibility, though it might only work if the destination
   90573                 :   ** table (tab1) is initially empty.
   90574                 :   */
   90575                 : #ifdef SQLITE_TEST
   90576                 :   sqlite3_xferopt_count++;
   90577                 : #endif
   90578              19 :   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
   90579              19 :   v = sqlite3GetVdbe(pParse);
   90580              19 :   sqlite3CodeVerifySchema(pParse, iDbSrc);
   90581              19 :   iSrc = pParse->nTab++;
   90582              19 :   iDest = pParse->nTab++;
   90583              19 :   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
   90584              19 :   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
   90585              19 :   if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
   90586              16 :    || destHasUniqueIdx                              /* (2) */
   90587               8 :    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
   90588                 :   ){
   90589                 :     /* In some circumstances, we are able to run the xfer optimization
   90590                 :     ** only if the destination table is initially empty.  This code makes
   90591                 :     ** that determination.  Conditions under which the destination must
   90592                 :     ** be empty:
   90593                 :     **
   90594                 :     ** (1) There is no INTEGER PRIMARY KEY but there are indices.
   90595                 :     **     (If the destination is not initially empty, the rowid fields
   90596                 :     **     of index entries might need to change.)
   90597                 :     **
   90598                 :     ** (2) The destination has a unique index.  (The xfer optimization 
   90599                 :     **     is unable to test uniqueness.)
   90600                 :     **
   90601                 :     ** (3) onError is something other than OE_Abort and OE_Rollback.
   90602                 :     */
   90603              11 :     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
   90604              11 :     emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
   90605              11 :     sqlite3VdbeJumpHere(v, addr1);
   90606                 :   }else{
   90607               8 :     emptyDestTest = 0;
   90608                 :   }
   90609              19 :   sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
   90610              19 :   emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
   90611              19 :   regData = sqlite3GetTempReg(pParse);
   90612              19 :   regRowid = sqlite3GetTempReg(pParse);
   90613              19 :   if( pDest->iPKey>=0 ){
   90614              13 :     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
   90615              13 :     addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
   90616              13 :     sqlite3HaltConstraint(
   90617                 :         pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
   90618              13 :     sqlite3VdbeJumpHere(v, addr2);
   90619              13 :     autoIncStep(pParse, regAutoinc, regRowid);
   90620               6 :   }else if( pDest->pIndex==0 ){
   90621               3 :     addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
   90622                 :   }else{
   90623               3 :     addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
   90624               3 :     assert( (pDest->tabFlags & TF_Autoincrement)==0 );
   90625                 :   }
   90626              19 :   sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
   90627              19 :   sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
   90628              19 :   sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
   90629              19 :   sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
   90630              19 :   sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
   90631              48 :   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
   90632              65 :     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
   90633              65 :       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
   90634                 :     }
   90635              29 :     assert( pSrcIdx );
   90636              29 :     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
   90637              29 :     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
   90638              29 :     pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
   90639              29 :     sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
   90640                 :                       (char*)pKey, P4_KEYINFO_HANDOFF);
   90641              29 :     VdbeComment((v, "%s", pSrcIdx->zName));
   90642              29 :     pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
   90643              29 :     sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
   90644                 :                       (char*)pKey, P4_KEYINFO_HANDOFF);
   90645              29 :     VdbeComment((v, "%s", pDestIdx->zName));
   90646              29 :     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
   90647              29 :     sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
   90648              29 :     sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
   90649              29 :     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
   90650              29 :     sqlite3VdbeJumpHere(v, addr1);
   90651                 :   }
   90652              19 :   sqlite3VdbeJumpHere(v, emptySrcTest);
   90653              19 :   sqlite3ReleaseTempReg(pParse, regRowid);
   90654              19 :   sqlite3ReleaseTempReg(pParse, regData);
   90655              19 :   sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
   90656              19 :   sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
   90657              19 :   if( emptyDestTest ){
   90658              11 :     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
   90659              11 :     sqlite3VdbeJumpHere(v, emptyDestTest);
   90660              11 :     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
   90661              11 :     return 0;
   90662                 :   }else{
   90663               8 :     return 1;
   90664                 :   }
   90665                 : }
   90666                 : #endif /* SQLITE_OMIT_XFER_OPT */
   90667                 : 
   90668                 : /************** End of insert.c **********************************************/
   90669                 : /************** Begin file legacy.c ******************************************/
   90670                 : /*
   90671                 : ** 2001 September 15
   90672                 : **
   90673                 : ** The author disclaims copyright to this source code.  In place of
   90674                 : ** a legal notice, here is a blessing:
   90675                 : **
   90676                 : **    May you do good and not evil.
   90677                 : **    May you find forgiveness for yourself and forgive others.
   90678                 : **    May you share freely, never taking more than you give.
   90679                 : **
   90680                 : *************************************************************************
   90681                 : ** Main file for the SQLite library.  The routines in this file
   90682                 : ** implement the programmer interface to the library.  Routines in
   90683                 : ** other files are for internal use by SQLite and should not be
   90684                 : ** accessed by users of the library.
   90685                 : */
   90686                 : 
   90687                 : 
   90688                 : /*
   90689                 : ** Execute SQL code.  Return one of the SQLITE_ success/failure
   90690                 : ** codes.  Also write an error message into memory obtained from
   90691                 : ** malloc() and make *pzErrMsg point to that message.
   90692                 : **
   90693                 : ** If the SQL is a query, then for each row in the query result
   90694                 : ** the xCallback() function is called.  pArg becomes the first
   90695                 : ** argument to xCallback().  If xCallback=NULL then no callback
   90696                 : ** is invoked, even for queries.
   90697                 : */
   90698           70380 : SQLITE_API int sqlite3_exec(
   90699                 :   sqlite3 *db,                /* The database on which the SQL executes */
   90700                 :   const char *zSql,           /* The SQL to be executed */
   90701                 :   sqlite3_callback xCallback, /* Invoke this callback routine */
   90702                 :   void *pArg,                 /* First argument to xCallback() */
   90703                 :   char **pzErrMsg             /* Write error messages here */
   90704                 : ){
   90705           70380 :   int rc = SQLITE_OK;         /* Return code */
   90706                 :   const char *zLeftover;      /* Tail of unprocessed SQL */
   90707           70380 :   sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
   90708           70380 :   char **azCols = 0;          /* Names of result columns */
   90709           70380 :   int nRetry = 0;             /* Number of retry attempts */
   90710                 :   int callbackIsInit;         /* True if callback data is initialized */
   90711                 : 
   90712           70380 :   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
   90713           70380 :   if( zSql==0 ) zSql = "";
   90714                 : 
   90715           70380 :   sqlite3_mutex_enter(db->mutex);
   90716           70380 :   sqlite3Error(db, SQLITE_OK, 0);
   90717           70380 :   while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
   90718                 :     int nCol;
   90719           70380 :     char **azVals = 0;
   90720                 : 
   90721           70380 :     pStmt = 0;
   90722           70380 :     rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
   90723           70380 :     assert( rc==SQLITE_OK || pStmt==0 );
   90724           70380 :     if( rc!=SQLITE_OK ){
   90725              32 :       continue;
   90726                 :     }
   90727           70348 :     if( !pStmt ){
   90728                 :       /* this happens for a comment or white-space */
   90729               0 :       zSql = zLeftover;
   90730               0 :       continue;
   90731                 :     }
   90732                 : 
   90733           70348 :     callbackIsInit = 0;
   90734           70348 :     nCol = sqlite3_column_count(pStmt);
   90735                 : 
   90736                 :     while( 1 ){
   90737                 :       int i;
   90738          136523 :       rc = sqlite3_step(pStmt);
   90739                 : 
   90740                 :       /* Invoke the callback function if required */
   90741          136523 :       if( xCallback && (SQLITE_ROW==rc || 
   90742           21241 :           (SQLITE_DONE==rc && !callbackIsInit
   90743            1983 :                            && db->flags&SQLITE_NullCallback)) ){
   90744           64067 :         if( !callbackIsInit ){
   90745           19258 :           azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
   90746           19258 :           if( azCols==0 ){
   90747               0 :             goto exec_out;
   90748                 :           }
   90749           77032 :           for(i=0; i<nCol; i++){
   90750           57774 :             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
   90751                 :             /* sqlite3VdbeSetColName() installs column names as UTF8
   90752                 :             ** strings so there is no way for sqlite3_column_name() to fail. */
   90753           57774 :             assert( azCols[i]!=0 );
   90754                 :           }
   90755           19258 :           callbackIsInit = 1;
   90756                 :         }
   90757           64067 :         if( rc==SQLITE_ROW ){
   90758           64067 :           azVals = &azCols[nCol];
   90759          256267 :           for(i=0; i<nCol; i++){
   90760          192200 :             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
   90761          192200 :             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
   90762               0 :               db->mallocFailed = 1;
   90763               0 :               goto exec_out;
   90764                 :             }
   90765                 :           }
   90766                 :         }
   90767           64067 :         if( xCallback(pArg, nCol, azVals, azCols) ){
   90768               0 :           rc = SQLITE_ABORT;
   90769               0 :           sqlite3VdbeFinalize((Vdbe *)pStmt);
   90770               0 :           pStmt = 0;
   90771               0 :           sqlite3Error(db, SQLITE_ABORT, 0);
   90772               0 :           goto exec_out;
   90773                 :         }
   90774                 :       }
   90775                 : 
   90776          136523 :       if( rc!=SQLITE_ROW ){
   90777           70348 :         rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
   90778           70348 :         pStmt = 0;
   90779           70348 :         if( rc!=SQLITE_SCHEMA ){
   90780           70348 :           nRetry = 0;
   90781           70348 :           zSql = zLeftover;
   90782           70348 :           while( sqlite3Isspace(zSql[0]) ) zSql++;
   90783                 :         }
   90784                 :         break;
   90785                 :       }
   90786           66175 :     }
   90787                 : 
   90788           70348 :     sqlite3DbFree(db, azCols);
   90789           70348 :     azCols = 0;
   90790                 :   }
   90791                 : 
   90792                 : exec_out:
   90793           70380 :   if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
   90794           70380 :   sqlite3DbFree(db, azCols);
   90795                 : 
   90796           70380 :   rc = sqlite3ApiExit(db, rc);
   90797           70380 :   if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
   90798               0 :     int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
   90799               0 :     *pzErrMsg = sqlite3Malloc(nErrMsg);
   90800               0 :     if( *pzErrMsg ){
   90801               0 :       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
   90802                 :     }else{
   90803               0 :       rc = SQLITE_NOMEM;
   90804               0 :       sqlite3Error(db, SQLITE_NOMEM, 0);
   90805                 :     }
   90806           70380 :   }else if( pzErrMsg ){
   90807               0 :     *pzErrMsg = 0;
   90808                 :   }
   90809                 : 
   90810           70380 :   assert( (rc&db->errMask)==rc );
   90811           70380 :   sqlite3_mutex_leave(db->mutex);
   90812           70380 :   return rc;
   90813                 : }
   90814                 : 
   90815                 : /************** End of legacy.c **********************************************/
   90816                 : /************** Begin file loadext.c *****************************************/
   90817                 : /*
   90818                 : ** 2006 June 7
   90819                 : **
   90820                 : ** The author disclaims copyright to this source code.  In place of
   90821                 : ** a legal notice, here is a blessing:
   90822                 : **
   90823                 : **    May you do good and not evil.
   90824                 : **    May you find forgiveness for yourself and forgive others.
   90825                 : **    May you share freely, never taking more than you give.
   90826                 : **
   90827                 : *************************************************************************
   90828                 : ** This file contains code used to dynamically load extensions into
   90829                 : ** the SQLite library.
   90830                 : */
   90831                 : 
   90832                 : #ifndef SQLITE_CORE
   90833                 :   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
   90834                 : #endif
   90835                 : /************** Include sqlite3ext.h in the middle of loadext.c **************/
   90836                 : /************** Begin file sqlite3ext.h **************************************/
   90837                 : /*
   90838                 : ** 2006 June 7
   90839                 : **
   90840                 : ** The author disclaims copyright to this source code.  In place of
   90841                 : ** a legal notice, here is a blessing:
   90842                 : **
   90843                 : **    May you do good and not evil.
   90844                 : **    May you find forgiveness for yourself and forgive others.
   90845                 : **    May you share freely, never taking more than you give.
   90846                 : **
   90847                 : *************************************************************************
   90848                 : ** This header file defines the SQLite interface for use by
   90849                 : ** shared libraries that want to be imported as extensions into
   90850                 : ** an SQLite instance.  Shared libraries that intend to be loaded
   90851                 : ** as extensions by SQLite should #include this file instead of 
   90852                 : ** sqlite3.h.
   90853                 : */
   90854                 : #ifndef _SQLITE3EXT_H_
   90855                 : #define _SQLITE3EXT_H_
   90856                 : 
   90857                 : typedef struct sqlite3_api_routines sqlite3_api_routines;
   90858                 : 
   90859                 : /*
   90860                 : ** The following structure holds pointers to all of the SQLite API
   90861                 : ** routines.
   90862                 : **
   90863                 : ** WARNING:  In order to maintain backwards compatibility, add new
   90864                 : ** interfaces to the end of this structure only.  If you insert new
   90865                 : ** interfaces in the middle of this structure, then older different
   90866                 : ** versions of SQLite will not be able to load each others' shared
   90867                 : ** libraries!
   90868                 : */
   90869                 : struct sqlite3_api_routines {
   90870                 :   void * (*aggregate_context)(sqlite3_context*,int nBytes);
   90871                 :   int  (*aggregate_count)(sqlite3_context*);
   90872                 :   int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
   90873                 :   int  (*bind_double)(sqlite3_stmt*,int,double);
   90874                 :   int  (*bind_int)(sqlite3_stmt*,int,int);
   90875                 :   int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
   90876                 :   int  (*bind_null)(sqlite3_stmt*,int);
   90877                 :   int  (*bind_parameter_count)(sqlite3_stmt*);
   90878                 :   int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
   90879                 :   const char * (*bind_parameter_name)(sqlite3_stmt*,int);
   90880                 :   int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
   90881                 :   int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
   90882                 :   int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
   90883                 :   int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
   90884                 :   int  (*busy_timeout)(sqlite3*,int ms);
   90885                 :   int  (*changes)(sqlite3*);
   90886                 :   int  (*close)(sqlite3*);
   90887                 :   int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
   90888                 :                            int eTextRep,const char*));
   90889                 :   int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
   90890                 :                              int eTextRep,const void*));
   90891                 :   const void * (*column_blob)(sqlite3_stmt*,int iCol);
   90892                 :   int  (*column_bytes)(sqlite3_stmt*,int iCol);
   90893                 :   int  (*column_bytes16)(sqlite3_stmt*,int iCol);
   90894                 :   int  (*column_count)(sqlite3_stmt*pStmt);
   90895                 :   const char * (*column_database_name)(sqlite3_stmt*,int);
   90896                 :   const void * (*column_database_name16)(sqlite3_stmt*,int);
   90897                 :   const char * (*column_decltype)(sqlite3_stmt*,int i);
   90898                 :   const void * (*column_decltype16)(sqlite3_stmt*,int);
   90899                 :   double  (*column_double)(sqlite3_stmt*,int iCol);
   90900                 :   int  (*column_int)(sqlite3_stmt*,int iCol);
   90901                 :   sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
   90902                 :   const char * (*column_name)(sqlite3_stmt*,int);
   90903                 :   const void * (*column_name16)(sqlite3_stmt*,int);
   90904                 :   const char * (*column_origin_name)(sqlite3_stmt*,int);
   90905                 :   const void * (*column_origin_name16)(sqlite3_stmt*,int);
   90906                 :   const char * (*column_table_name)(sqlite3_stmt*,int);
   90907                 :   const void * (*column_table_name16)(sqlite3_stmt*,int);
   90908                 :   const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
   90909                 :   const void * (*column_text16)(sqlite3_stmt*,int iCol);
   90910                 :   int  (*column_type)(sqlite3_stmt*,int iCol);
   90911                 :   sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
   90912                 :   void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
   90913                 :   int  (*complete)(const char*sql);
   90914                 :   int  (*complete16)(const void*sql);
   90915                 :   int  (*create_collation)(sqlite3*,const char*,int,void*,
   90916                 :                            int(*)(void*,int,const void*,int,const void*));
   90917                 :   int  (*create_collation16)(sqlite3*,const void*,int,void*,
   90918                 :                              int(*)(void*,int,const void*,int,const void*));
   90919                 :   int  (*create_function)(sqlite3*,const char*,int,int,void*,
   90920                 :                           void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   90921                 :                           void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   90922                 :                           void (*xFinal)(sqlite3_context*));
   90923                 :   int  (*create_function16)(sqlite3*,const void*,int,int,void*,
   90924                 :                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   90925                 :                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   90926                 :                             void (*xFinal)(sqlite3_context*));
   90927                 :   int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
   90928                 :   int  (*data_count)(sqlite3_stmt*pStmt);
   90929                 :   sqlite3 * (*db_handle)(sqlite3_stmt*);
   90930                 :   int (*declare_vtab)(sqlite3*,const char*);
   90931                 :   int  (*enable_shared_cache)(int);
   90932                 :   int  (*errcode)(sqlite3*db);
   90933                 :   const char * (*errmsg)(sqlite3*);
   90934                 :   const void * (*errmsg16)(sqlite3*);
   90935                 :   int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
   90936                 :   int  (*expired)(sqlite3_stmt*);
   90937                 :   int  (*finalize)(sqlite3_stmt*pStmt);
   90938                 :   void  (*free)(void*);
   90939                 :   void  (*free_table)(char**result);
   90940                 :   int  (*get_autocommit)(sqlite3*);
   90941                 :   void * (*get_auxdata)(sqlite3_context*,int);
   90942                 :   int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
   90943                 :   int  (*global_recover)(void);
   90944                 :   void  (*interruptx)(sqlite3*);
   90945                 :   sqlite_int64  (*last_insert_rowid)(sqlite3*);
   90946                 :   const char * (*libversion)(void);
   90947                 :   int  (*libversion_number)(void);
   90948                 :   void *(*malloc)(int);
   90949                 :   char * (*mprintf)(const char*,...);
   90950                 :   int  (*open)(const char*,sqlite3**);
   90951                 :   int  (*open16)(const void*,sqlite3**);
   90952                 :   int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
   90953                 :   int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
   90954                 :   void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
   90955                 :   void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
   90956                 :   void *(*realloc)(void*,int);
   90957                 :   int  (*reset)(sqlite3_stmt*pStmt);
   90958                 :   void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
   90959                 :   void  (*result_double)(sqlite3_context*,double);
   90960                 :   void  (*result_error)(sqlite3_context*,const char*,int);
   90961                 :   void  (*result_error16)(sqlite3_context*,const void*,int);
   90962                 :   void  (*result_int)(sqlite3_context*,int);
   90963                 :   void  (*result_int64)(sqlite3_context*,sqlite_int64);
   90964                 :   void  (*result_null)(sqlite3_context*);
   90965                 :   void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
   90966                 :   void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
   90967                 :   void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
   90968                 :   void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
   90969                 :   void  (*result_value)(sqlite3_context*,sqlite3_value*);
   90970                 :   void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
   90971                 :   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
   90972                 :                          const char*,const char*),void*);
   90973                 :   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
   90974                 :   char * (*snprintf)(int,char*,const char*,...);
   90975                 :   int  (*step)(sqlite3_stmt*);
   90976                 :   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
   90977                 :                                 char const**,char const**,int*,int*,int*);
   90978                 :   void  (*thread_cleanup)(void);
   90979                 :   int  (*total_changes)(sqlite3*);
   90980                 :   void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
   90981                 :   int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
   90982                 :   void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
   90983                 :                                          sqlite_int64),void*);
   90984                 :   void * (*user_data)(sqlite3_context*);
   90985                 :   const void * (*value_blob)(sqlite3_value*);
   90986                 :   int  (*value_bytes)(sqlite3_value*);
   90987                 :   int  (*value_bytes16)(sqlite3_value*);
   90988                 :   double  (*value_double)(sqlite3_value*);
   90989                 :   int  (*value_int)(sqlite3_value*);
   90990                 :   sqlite_int64  (*value_int64)(sqlite3_value*);
   90991                 :   int  (*value_numeric_type)(sqlite3_value*);
   90992                 :   const unsigned char * (*value_text)(sqlite3_value*);
   90993                 :   const void * (*value_text16)(sqlite3_value*);
   90994                 :   const void * (*value_text16be)(sqlite3_value*);
   90995                 :   const void * (*value_text16le)(sqlite3_value*);
   90996                 :   int  (*value_type)(sqlite3_value*);
   90997                 :   char *(*vmprintf)(const char*,va_list);
   90998                 :   /* Added ??? */
   90999                 :   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
   91000                 :   /* Added by 3.3.13 */
   91001                 :   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
   91002                 :   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
   91003                 :   int (*clear_bindings)(sqlite3_stmt*);
   91004                 :   /* Added by 3.4.1 */
   91005                 :   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
   91006                 :                           void (*xDestroy)(void *));
   91007                 :   /* Added by 3.5.0 */
   91008                 :   int (*bind_zeroblob)(sqlite3_stmt*,int,int);
   91009                 :   int (*blob_bytes)(sqlite3_blob*);
   91010                 :   int (*blob_close)(sqlite3_blob*);
   91011                 :   int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
   91012                 :                    int,sqlite3_blob**);
   91013                 :   int (*blob_read)(sqlite3_blob*,void*,int,int);
   91014                 :   int (*blob_write)(sqlite3_blob*,const void*,int,int);
   91015                 :   int (*create_collation_v2)(sqlite3*,const char*,int,void*,
   91016                 :                              int(*)(void*,int,const void*,int,const void*),
   91017                 :                              void(*)(void*));
   91018                 :   int (*file_control)(sqlite3*,const char*,int,void*);
   91019                 :   sqlite3_int64 (*memory_highwater)(int);
   91020                 :   sqlite3_int64 (*memory_used)(void);
   91021                 :   sqlite3_mutex *(*mutex_alloc)(int);
   91022                 :   void (*mutex_enter)(sqlite3_mutex*);
   91023                 :   void (*mutex_free)(sqlite3_mutex*);
   91024                 :   void (*mutex_leave)(sqlite3_mutex*);
   91025                 :   int (*mutex_try)(sqlite3_mutex*);
   91026                 :   int (*open_v2)(const char*,sqlite3**,int,const char*);
   91027                 :   int (*release_memory)(int);
   91028                 :   void (*result_error_nomem)(sqlite3_context*);
   91029                 :   void (*result_error_toobig)(sqlite3_context*);
   91030                 :   int (*sleep)(int);
   91031                 :   void (*soft_heap_limit)(int);
   91032                 :   sqlite3_vfs *(*vfs_find)(const char*);
   91033                 :   int (*vfs_register)(sqlite3_vfs*,int);
   91034                 :   int (*vfs_unregister)(sqlite3_vfs*);
   91035                 :   int (*xthreadsafe)(void);
   91036                 :   void (*result_zeroblob)(sqlite3_context*,int);
   91037                 :   void (*result_error_code)(sqlite3_context*,int);
   91038                 :   int (*test_control)(int, ...);
   91039                 :   void (*randomness)(int,void*);
   91040                 :   sqlite3 *(*context_db_handle)(sqlite3_context*);
   91041                 :   int (*extended_result_codes)(sqlite3*,int);
   91042                 :   int (*limit)(sqlite3*,int,int);
   91043                 :   sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
   91044                 :   const char *(*sql)(sqlite3_stmt*);
   91045                 :   int (*status)(int,int*,int*,int);
   91046                 :   int (*backup_finish)(sqlite3_backup*);
   91047                 :   sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
   91048                 :   int (*backup_pagecount)(sqlite3_backup*);
   91049                 :   int (*backup_remaining)(sqlite3_backup*);
   91050                 :   int (*backup_step)(sqlite3_backup*,int);
   91051                 :   const char *(*compileoption_get)(int);
   91052                 :   int (*compileoption_used)(const char*);
   91053                 :   int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
   91054                 :                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   91055                 :                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   91056                 :                             void (*xFinal)(sqlite3_context*),
   91057                 :                             void(*xDestroy)(void*));
   91058                 :   int (*db_config)(sqlite3*,int,...);
   91059                 :   sqlite3_mutex *(*db_mutex)(sqlite3*);
   91060                 :   int (*db_status)(sqlite3*,int,int*,int*,int);
   91061                 :   int (*extended_errcode)(sqlite3*);
   91062                 :   void (*log)(int,const char*,...);
   91063                 :   sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
   91064                 :   const char *(*sourceid)(void);
   91065                 :   int (*stmt_status)(sqlite3_stmt*,int,int);
   91066                 :   int (*strnicmp)(const char*,const char*,int);
   91067                 :   int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
   91068                 :   int (*wal_autocheckpoint)(sqlite3*,int);
   91069                 :   int (*wal_checkpoint)(sqlite3*,const char*);
   91070                 :   void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
   91071                 :   int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
   91072                 :   int (*vtab_config)(sqlite3*,int op,...);
   91073                 :   int (*vtab_on_conflict)(sqlite3*);
   91074                 : };
   91075                 : 
   91076                 : /*
   91077                 : ** The following macros redefine the API routines so that they are
   91078                 : ** redirected throught the global sqlite3_api structure.
   91079                 : **
   91080                 : ** This header file is also used by the loadext.c source file
   91081                 : ** (part of the main SQLite library - not an extension) so that
   91082                 : ** it can get access to the sqlite3_api_routines structure
   91083                 : ** definition.  But the main library does not want to redefine
   91084                 : ** the API.  So the redefinition macros are only valid if the
   91085                 : ** SQLITE_CORE macros is undefined.
   91086                 : */
   91087                 : #ifndef SQLITE_CORE
   91088                 : #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
   91089                 : #ifndef SQLITE_OMIT_DEPRECATED
   91090                 : #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
   91091                 : #endif
   91092                 : #define sqlite3_bind_blob              sqlite3_api->bind_blob
   91093                 : #define sqlite3_bind_double            sqlite3_api->bind_double
   91094                 : #define sqlite3_bind_int               sqlite3_api->bind_int
   91095                 : #define sqlite3_bind_int64             sqlite3_api->bind_int64
   91096                 : #define sqlite3_bind_null              sqlite3_api->bind_null
   91097                 : #define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
   91098                 : #define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
   91099                 : #define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
   91100                 : #define sqlite3_bind_text              sqlite3_api->bind_text
   91101                 : #define sqlite3_bind_text16            sqlite3_api->bind_text16
   91102                 : #define sqlite3_bind_value             sqlite3_api->bind_value
   91103                 : #define sqlite3_busy_handler           sqlite3_api->busy_handler
   91104                 : #define sqlite3_busy_timeout           sqlite3_api->busy_timeout
   91105                 : #define sqlite3_changes                sqlite3_api->changes
   91106                 : #define sqlite3_close                  sqlite3_api->close
   91107                 : #define sqlite3_collation_needed       sqlite3_api->collation_needed
   91108                 : #define sqlite3_collation_needed16     sqlite3_api->collation_needed16
   91109                 : #define sqlite3_column_blob            sqlite3_api->column_blob
   91110                 : #define sqlite3_column_bytes           sqlite3_api->column_bytes
   91111                 : #define sqlite3_column_bytes16         sqlite3_api->column_bytes16
   91112                 : #define sqlite3_column_count           sqlite3_api->column_count
   91113                 : #define sqlite3_column_database_name   sqlite3_api->column_database_name
   91114                 : #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
   91115                 : #define sqlite3_column_decltype        sqlite3_api->column_decltype
   91116                 : #define sqlite3_column_decltype16      sqlite3_api->column_decltype16
   91117                 : #define sqlite3_column_double          sqlite3_api->column_double
   91118                 : #define sqlite3_column_int             sqlite3_api->column_int
   91119                 : #define sqlite3_column_int64           sqlite3_api->column_int64
   91120                 : #define sqlite3_column_name            sqlite3_api->column_name
   91121                 : #define sqlite3_column_name16          sqlite3_api->column_name16
   91122                 : #define sqlite3_column_origin_name     sqlite3_api->column_origin_name
   91123                 : #define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
   91124                 : #define sqlite3_column_table_name      sqlite3_api->column_table_name
   91125                 : #define sqlite3_column_table_name16    sqlite3_api->column_table_name16
   91126                 : #define sqlite3_column_text            sqlite3_api->column_text
   91127                 : #define sqlite3_column_text16          sqlite3_api->column_text16
   91128                 : #define sqlite3_column_type            sqlite3_api->column_type
   91129                 : #define sqlite3_column_value           sqlite3_api->column_value
   91130                 : #define sqlite3_commit_hook            sqlite3_api->commit_hook
   91131                 : #define sqlite3_complete               sqlite3_api->complete
   91132                 : #define sqlite3_complete16             sqlite3_api->complete16
   91133                 : #define sqlite3_create_collation       sqlite3_api->create_collation
   91134                 : #define sqlite3_create_collation16     sqlite3_api->create_collation16
   91135                 : #define sqlite3_create_function        sqlite3_api->create_function
   91136                 : #define sqlite3_create_function16      sqlite3_api->create_function16
   91137                 : #define sqlite3_create_module          sqlite3_api->create_module
   91138                 : #define sqlite3_create_module_v2       sqlite3_api->create_module_v2
   91139                 : #define sqlite3_data_count             sqlite3_api->data_count
   91140                 : #define sqlite3_db_handle              sqlite3_api->db_handle
   91141                 : #define sqlite3_declare_vtab           sqlite3_api->declare_vtab
   91142                 : #define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
   91143                 : #define sqlite3_errcode                sqlite3_api->errcode
   91144                 : #define sqlite3_errmsg                 sqlite3_api->errmsg
   91145                 : #define sqlite3_errmsg16               sqlite3_api->errmsg16
   91146                 : #define sqlite3_exec                   sqlite3_api->exec
   91147                 : #ifndef SQLITE_OMIT_DEPRECATED
   91148                 : #define sqlite3_expired                sqlite3_api->expired
   91149                 : #endif
   91150                 : #define sqlite3_finalize               sqlite3_api->finalize
   91151                 : #define sqlite3_free                   sqlite3_api->free
   91152                 : #define sqlite3_free_table             sqlite3_api->free_table
   91153                 : #define sqlite3_get_autocommit         sqlite3_api->get_autocommit
   91154                 : #define sqlite3_get_auxdata            sqlite3_api->get_auxdata
   91155                 : #define sqlite3_get_table              sqlite3_api->get_table
   91156                 : #ifndef SQLITE_OMIT_DEPRECATED
   91157                 : #define sqlite3_global_recover         sqlite3_api->global_recover
   91158                 : #endif
   91159                 : #define sqlite3_interrupt              sqlite3_api->interruptx
   91160                 : #define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
   91161                 : #define sqlite3_libversion             sqlite3_api->libversion
   91162                 : #define sqlite3_libversion_number      sqlite3_api->libversion_number
   91163                 : #define sqlite3_malloc                 sqlite3_api->malloc
   91164                 : #define sqlite3_mprintf                sqlite3_api->mprintf
   91165                 : #define sqlite3_open                   sqlite3_api->open
   91166                 : #define sqlite3_open16                 sqlite3_api->open16
   91167                 : #define sqlite3_prepare                sqlite3_api->prepare
   91168                 : #define sqlite3_prepare16              sqlite3_api->prepare16
   91169                 : #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
   91170                 : #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
   91171                 : #define sqlite3_profile                sqlite3_api->profile
   91172                 : #define sqlite3_progress_handler       sqlite3_api->progress_handler
   91173                 : #define sqlite3_realloc                sqlite3_api->realloc
   91174                 : #define sqlite3_reset                  sqlite3_api->reset
   91175                 : #define sqlite3_result_blob            sqlite3_api->result_blob
   91176                 : #define sqlite3_result_double          sqlite3_api->result_double
   91177                 : #define sqlite3_result_error           sqlite3_api->result_error
   91178                 : #define sqlite3_result_error16         sqlite3_api->result_error16
   91179                 : #define sqlite3_result_int             sqlite3_api->result_int
   91180                 : #define sqlite3_result_int64           sqlite3_api->result_int64
   91181                 : #define sqlite3_result_null            sqlite3_api->result_null
   91182                 : #define sqlite3_result_text            sqlite3_api->result_text
   91183                 : #define sqlite3_result_text16          sqlite3_api->result_text16
   91184                 : #define sqlite3_result_text16be        sqlite3_api->result_text16be
   91185                 : #define sqlite3_result_text16le        sqlite3_api->result_text16le
   91186                 : #define sqlite3_result_value           sqlite3_api->result_value
   91187                 : #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
   91188                 : #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
   91189                 : #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
   91190                 : #define sqlite3_snprintf               sqlite3_api->snprintf
   91191                 : #define sqlite3_step                   sqlite3_api->step
   91192                 : #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
   91193                 : #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
   91194                 : #define sqlite3_total_changes          sqlite3_api->total_changes
   91195                 : #define sqlite3_trace                  sqlite3_api->trace
   91196                 : #ifndef SQLITE_OMIT_DEPRECATED
   91197                 : #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
   91198                 : #endif
   91199                 : #define sqlite3_update_hook            sqlite3_api->update_hook
   91200                 : #define sqlite3_user_data              sqlite3_api->user_data
   91201                 : #define sqlite3_value_blob             sqlite3_api->value_blob
   91202                 : #define sqlite3_value_bytes            sqlite3_api->value_bytes
   91203                 : #define sqlite3_value_bytes16          sqlite3_api->value_bytes16
   91204                 : #define sqlite3_value_double           sqlite3_api->value_double
   91205                 : #define sqlite3_value_int              sqlite3_api->value_int
   91206                 : #define sqlite3_value_int64            sqlite3_api->value_int64
   91207                 : #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
   91208                 : #define sqlite3_value_text             sqlite3_api->value_text
   91209                 : #define sqlite3_value_text16           sqlite3_api->value_text16
   91210                 : #define sqlite3_value_text16be         sqlite3_api->value_text16be
   91211                 : #define sqlite3_value_text16le         sqlite3_api->value_text16le
   91212                 : #define sqlite3_value_type             sqlite3_api->value_type
   91213                 : #define sqlite3_vmprintf               sqlite3_api->vmprintf
   91214                 : #define sqlite3_overload_function      sqlite3_api->overload_function
   91215                 : #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
   91216                 : #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
   91217                 : #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
   91218                 : #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
   91219                 : #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
   91220                 : #define sqlite3_blob_close             sqlite3_api->blob_close
   91221                 : #define sqlite3_blob_open              sqlite3_api->blob_open
   91222                 : #define sqlite3_blob_read              sqlite3_api->blob_read
   91223                 : #define sqlite3_blob_write             sqlite3_api->blob_write
   91224                 : #define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
   91225                 : #define sqlite3_file_control           sqlite3_api->file_control
   91226                 : #define sqlite3_memory_highwater       sqlite3_api->memory_highwater
   91227                 : #define sqlite3_memory_used            sqlite3_api->memory_used
   91228                 : #define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
   91229                 : #define sqlite3_mutex_enter            sqlite3_api->mutex_enter
   91230                 : #define sqlite3_mutex_free             sqlite3_api->mutex_free
   91231                 : #define sqlite3_mutex_leave            sqlite3_api->mutex_leave
   91232                 : #define sqlite3_mutex_try              sqlite3_api->mutex_try
   91233                 : #define sqlite3_open_v2                sqlite3_api->open_v2
   91234                 : #define sqlite3_release_memory         sqlite3_api->release_memory
   91235                 : #define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
   91236                 : #define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
   91237                 : #define sqlite3_sleep                  sqlite3_api->sleep
   91238                 : #define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
   91239                 : #define sqlite3_vfs_find               sqlite3_api->vfs_find
   91240                 : #define sqlite3_vfs_register           sqlite3_api->vfs_register
   91241                 : #define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
   91242                 : #define sqlite3_threadsafe             sqlite3_api->xthreadsafe
   91243                 : #define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
   91244                 : #define sqlite3_result_error_code      sqlite3_api->result_error_code
   91245                 : #define sqlite3_test_control           sqlite3_api->test_control
   91246                 : #define sqlite3_randomness             sqlite3_api->randomness
   91247                 : #define sqlite3_context_db_handle      sqlite3_api->context_db_handle
   91248                 : #define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
   91249                 : #define sqlite3_limit                  sqlite3_api->limit
   91250                 : #define sqlite3_next_stmt              sqlite3_api->next_stmt
   91251                 : #define sqlite3_sql                    sqlite3_api->sql
   91252                 : #define sqlite3_status                 sqlite3_api->status
   91253                 : #define sqlite3_backup_finish          sqlite3_api->backup_finish
   91254                 : #define sqlite3_backup_init            sqlite3_api->backup_init
   91255                 : #define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
   91256                 : #define sqlite3_backup_remaining       sqlite3_api->backup_remaining
   91257                 : #define sqlite3_backup_step            sqlite3_api->backup_step
   91258                 : #define sqlite3_compileoption_get      sqlite3_api->compileoption_get
   91259                 : #define sqlite3_compileoption_used     sqlite3_api->compileoption_used
   91260                 : #define sqlite3_create_function_v2     sqlite3_api->create_function_v2
   91261                 : #define sqlite3_db_config              sqlite3_api->db_config
   91262                 : #define sqlite3_db_mutex               sqlite3_api->db_mutex
   91263                 : #define sqlite3_db_status              sqlite3_api->db_status
   91264                 : #define sqlite3_extended_errcode       sqlite3_api->extended_errcode
   91265                 : #define sqlite3_log                    sqlite3_api->log
   91266                 : #define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
   91267                 : #define sqlite3_sourceid               sqlite3_api->sourceid
   91268                 : #define sqlite3_stmt_status            sqlite3_api->stmt_status
   91269                 : #define sqlite3_strnicmp               sqlite3_api->strnicmp
   91270                 : #define sqlite3_unlock_notify          sqlite3_api->unlock_notify
   91271                 : #define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
   91272                 : #define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
   91273                 : #define sqlite3_wal_hook               sqlite3_api->wal_hook
   91274                 : #define sqlite3_blob_reopen            sqlite3_api->blob_reopen
   91275                 : #define sqlite3_vtab_config            sqlite3_api->vtab_config
   91276                 : #define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
   91277                 : #endif /* SQLITE_CORE */
   91278                 : 
   91279                 : #define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api = 0;
   91280                 : #define SQLITE_EXTENSION_INIT2(v)  sqlite3_api = v;
   91281                 : 
   91282                 : #endif /* _SQLITE3EXT_H_ */
   91283                 : 
   91284                 : /************** End of sqlite3ext.h ******************************************/
   91285                 : /************** Continuing where we left off in loadext.c ********************/
   91286                 : /* #include <string.h> */
   91287                 : 
   91288                 : #ifndef SQLITE_OMIT_LOAD_EXTENSION
   91289                 : 
   91290                 : /*
   91291                 : ** Some API routines are omitted when various features are
   91292                 : ** excluded from a build of SQLite.  Substitute a NULL pointer
   91293                 : ** for any missing APIs.
   91294                 : */
   91295                 : #ifndef SQLITE_ENABLE_COLUMN_METADATA
   91296                 : # define sqlite3_column_database_name   0
   91297                 : # define sqlite3_column_database_name16 0
   91298                 : # define sqlite3_column_table_name      0
   91299                 : # define sqlite3_column_table_name16    0
   91300                 : # define sqlite3_column_origin_name     0
   91301                 : # define sqlite3_column_origin_name16   0
   91302                 : # define sqlite3_table_column_metadata  0
   91303                 : #endif
   91304                 : 
   91305                 : #ifdef SQLITE_OMIT_AUTHORIZATION
   91306                 : # define sqlite3_set_authorizer         0
   91307                 : #endif
   91308                 : 
   91309                 : #ifdef SQLITE_OMIT_UTF16
   91310                 : # define sqlite3_bind_text16            0
   91311                 : # define sqlite3_collation_needed16     0
   91312                 : # define sqlite3_column_decltype16      0
   91313                 : # define sqlite3_column_name16          0
   91314                 : # define sqlite3_column_text16          0
   91315                 : # define sqlite3_complete16             0
   91316                 : # define sqlite3_create_collation16     0
   91317                 : # define sqlite3_create_function16      0
   91318                 : # define sqlite3_errmsg16               0
   91319                 : # define sqlite3_open16                 0
   91320                 : # define sqlite3_prepare16              0
   91321                 : # define sqlite3_prepare16_v2           0
   91322                 : # define sqlite3_result_error16         0
   91323                 : # define sqlite3_result_text16          0
   91324                 : # define sqlite3_result_text16be        0
   91325                 : # define sqlite3_result_text16le        0
   91326                 : # define sqlite3_value_text16           0
   91327                 : # define sqlite3_value_text16be         0
   91328                 : # define sqlite3_value_text16le         0
   91329                 : # define sqlite3_column_database_name16 0
   91330                 : # define sqlite3_column_table_name16    0
   91331                 : # define sqlite3_column_origin_name16   0
   91332                 : #endif
   91333                 : 
   91334                 : #ifdef SQLITE_OMIT_COMPLETE
   91335                 : # define sqlite3_complete 0
   91336                 : # define sqlite3_complete16 0
   91337                 : #endif
   91338                 : 
   91339                 : #ifdef SQLITE_OMIT_DECLTYPE
   91340                 : # define sqlite3_column_decltype16      0
   91341                 : # define sqlite3_column_decltype        0
   91342                 : #endif
   91343                 : 
   91344                 : #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
   91345                 : # define sqlite3_progress_handler 0
   91346                 : #endif
   91347                 : 
   91348                 : #ifdef SQLITE_OMIT_VIRTUALTABLE
   91349                 : # define sqlite3_create_module 0
   91350                 : # define sqlite3_create_module_v2 0
   91351                 : # define sqlite3_declare_vtab 0
   91352                 : # define sqlite3_vtab_config 0
   91353                 : # define sqlite3_vtab_on_conflict 0
   91354                 : #endif
   91355                 : 
   91356                 : #ifdef SQLITE_OMIT_SHARED_CACHE
   91357                 : # define sqlite3_enable_shared_cache 0
   91358                 : #endif
   91359                 : 
   91360                 : #ifdef SQLITE_OMIT_TRACE
   91361                 : # define sqlite3_profile       0
   91362                 : # define sqlite3_trace         0
   91363                 : #endif
   91364                 : 
   91365                 : #ifdef SQLITE_OMIT_GET_TABLE
   91366                 : # define sqlite3_free_table    0
   91367                 : # define sqlite3_get_table     0
   91368                 : #endif
   91369                 : 
   91370                 : #ifdef SQLITE_OMIT_INCRBLOB
   91371                 : #define sqlite3_bind_zeroblob  0
   91372                 : #define sqlite3_blob_bytes     0
   91373                 : #define sqlite3_blob_close     0
   91374                 : #define sqlite3_blob_open      0
   91375                 : #define sqlite3_blob_read      0
   91376                 : #define sqlite3_blob_write     0
   91377                 : #define sqlite3_blob_reopen    0
   91378                 : #endif
   91379                 : 
   91380                 : /*
   91381                 : ** The following structure contains pointers to all SQLite API routines.
   91382                 : ** A pointer to this structure is passed into extensions when they are
   91383                 : ** loaded so that the extension can make calls back into the SQLite
   91384                 : ** library.
   91385                 : **
   91386                 : ** When adding new APIs, add them to the bottom of this structure
   91387                 : ** in order to preserve backwards compatibility.
   91388                 : **
   91389                 : ** Extensions that use newer APIs should first call the
   91390                 : ** sqlite3_libversion_number() to make sure that the API they
   91391                 : ** intend to use is supported by the library.  Extensions should
   91392                 : ** also check to make sure that the pointer to the function is
   91393                 : ** not NULL before calling it.
   91394                 : */
   91395                 : static const sqlite3_api_routines sqlite3Apis = {
   91396                 :   sqlite3_aggregate_context,
   91397                 : #ifndef SQLITE_OMIT_DEPRECATED
   91398                 :   sqlite3_aggregate_count,
   91399                 : #else
   91400                 :   0,
   91401                 : #endif
   91402                 :   sqlite3_bind_blob,
   91403                 :   sqlite3_bind_double,
   91404                 :   sqlite3_bind_int,
   91405                 :   sqlite3_bind_int64,
   91406                 :   sqlite3_bind_null,
   91407                 :   sqlite3_bind_parameter_count,
   91408                 :   sqlite3_bind_parameter_index,
   91409                 :   sqlite3_bind_parameter_name,
   91410                 :   sqlite3_bind_text,
   91411                 :   sqlite3_bind_text16,
   91412                 :   sqlite3_bind_value,
   91413                 :   sqlite3_busy_handler,
   91414                 :   sqlite3_busy_timeout,
   91415                 :   sqlite3_changes,
   91416                 :   sqlite3_close,
   91417                 :   sqlite3_collation_needed,
   91418                 :   sqlite3_collation_needed16,
   91419                 :   sqlite3_column_blob,
   91420                 :   sqlite3_column_bytes,
   91421                 :   sqlite3_column_bytes16,
   91422                 :   sqlite3_column_count,
   91423                 :   sqlite3_column_database_name,
   91424                 :   sqlite3_column_database_name16,
   91425                 :   sqlite3_column_decltype,
   91426                 :   sqlite3_column_decltype16,
   91427                 :   sqlite3_column_double,
   91428                 :   sqlite3_column_int,
   91429                 :   sqlite3_column_int64,
   91430                 :   sqlite3_column_name,
   91431                 :   sqlite3_column_name16,
   91432                 :   sqlite3_column_origin_name,
   91433                 :   sqlite3_column_origin_name16,
   91434                 :   sqlite3_column_table_name,
   91435                 :   sqlite3_column_table_name16,
   91436                 :   sqlite3_column_text,
   91437                 :   sqlite3_column_text16,
   91438                 :   sqlite3_column_type,
   91439                 :   sqlite3_column_value,
   91440                 :   sqlite3_commit_hook,
   91441                 :   sqlite3_complete,
   91442                 :   sqlite3_complete16,
   91443                 :   sqlite3_create_collation,
   91444                 :   sqlite3_create_collation16,
   91445                 :   sqlite3_create_function,
   91446                 :   sqlite3_create_function16,
   91447                 :   sqlite3_create_module,
   91448                 :   sqlite3_data_count,
   91449                 :   sqlite3_db_handle,
   91450                 :   sqlite3_declare_vtab,
   91451                 :   sqlite3_enable_shared_cache,
   91452                 :   sqlite3_errcode,
   91453                 :   sqlite3_errmsg,
   91454                 :   sqlite3_errmsg16,
   91455                 :   sqlite3_exec,
   91456                 : #ifndef SQLITE_OMIT_DEPRECATED
   91457                 :   sqlite3_expired,
   91458                 : #else
   91459                 :   0,
   91460                 : #endif
   91461                 :   sqlite3_finalize,
   91462                 :   sqlite3_free,
   91463                 :   sqlite3_free_table,
   91464                 :   sqlite3_get_autocommit,
   91465                 :   sqlite3_get_auxdata,
   91466                 :   sqlite3_get_table,
   91467                 :   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
   91468                 :   sqlite3_interrupt,
   91469                 :   sqlite3_last_insert_rowid,
   91470                 :   sqlite3_libversion,
   91471                 :   sqlite3_libversion_number,
   91472                 :   sqlite3_malloc,
   91473                 :   sqlite3_mprintf,
   91474                 :   sqlite3_open,
   91475                 :   sqlite3_open16,
   91476                 :   sqlite3_prepare,
   91477                 :   sqlite3_prepare16,
   91478                 :   sqlite3_profile,
   91479                 :   sqlite3_progress_handler,
   91480                 :   sqlite3_realloc,
   91481                 :   sqlite3_reset,
   91482                 :   sqlite3_result_blob,
   91483                 :   sqlite3_result_double,
   91484                 :   sqlite3_result_error,
   91485                 :   sqlite3_result_error16,
   91486                 :   sqlite3_result_int,
   91487                 :   sqlite3_result_int64,
   91488                 :   sqlite3_result_null,
   91489                 :   sqlite3_result_text,
   91490                 :   sqlite3_result_text16,
   91491                 :   sqlite3_result_text16be,
   91492                 :   sqlite3_result_text16le,
   91493                 :   sqlite3_result_value,
   91494                 :   sqlite3_rollback_hook,
   91495                 :   sqlite3_set_authorizer,
   91496                 :   sqlite3_set_auxdata,
   91497                 :   sqlite3_snprintf,
   91498                 :   sqlite3_step,
   91499                 :   sqlite3_table_column_metadata,
   91500                 : #ifndef SQLITE_OMIT_DEPRECATED
   91501                 :   sqlite3_thread_cleanup,
   91502                 : #else
   91503                 :   0,
   91504                 : #endif
   91505                 :   sqlite3_total_changes,
   91506                 :   sqlite3_trace,
   91507                 : #ifndef SQLITE_OMIT_DEPRECATED
   91508                 :   sqlite3_transfer_bindings,
   91509                 : #else
   91510                 :   0,
   91511                 : #endif
   91512                 :   sqlite3_update_hook,
   91513                 :   sqlite3_user_data,
   91514                 :   sqlite3_value_blob,
   91515                 :   sqlite3_value_bytes,
   91516                 :   sqlite3_value_bytes16,
   91517                 :   sqlite3_value_double,
   91518                 :   sqlite3_value_int,
   91519                 :   sqlite3_value_int64,
   91520                 :   sqlite3_value_numeric_type,
   91521                 :   sqlite3_value_text,
   91522                 :   sqlite3_value_text16,
   91523                 :   sqlite3_value_text16be,
   91524                 :   sqlite3_value_text16le,
   91525                 :   sqlite3_value_type,
   91526                 :   sqlite3_vmprintf,
   91527                 :   /*
   91528                 :   ** The original API set ends here.  All extensions can call any
   91529                 :   ** of the APIs above provided that the pointer is not NULL.  But
   91530                 :   ** before calling APIs that follow, extension should check the
   91531                 :   ** sqlite3_libversion_number() to make sure they are dealing with
   91532                 :   ** a library that is new enough to support that API.
   91533                 :   *************************************************************************
   91534                 :   */
   91535                 :   sqlite3_overload_function,
   91536                 : 
   91537                 :   /*
   91538                 :   ** Added after 3.3.13
   91539                 :   */
   91540                 :   sqlite3_prepare_v2,
   91541                 :   sqlite3_prepare16_v2,
   91542                 :   sqlite3_clear_bindings,
   91543                 : 
   91544                 :   /*
   91545                 :   ** Added for 3.4.1
   91546                 :   */
   91547                 :   sqlite3_create_module_v2,
   91548                 : 
   91549                 :   /*
   91550                 :   ** Added for 3.5.0
   91551                 :   */
   91552                 :   sqlite3_bind_zeroblob,
   91553                 :   sqlite3_blob_bytes,
   91554                 :   sqlite3_blob_close,
   91555                 :   sqlite3_blob_open,
   91556                 :   sqlite3_blob_read,
   91557                 :   sqlite3_blob_write,
   91558                 :   sqlite3_create_collation_v2,
   91559                 :   sqlite3_file_control,
   91560                 :   sqlite3_memory_highwater,
   91561                 :   sqlite3_memory_used,
   91562                 : #ifdef SQLITE_MUTEX_OMIT
   91563                 :   0, 
   91564                 :   0, 
   91565                 :   0,
   91566                 :   0,
   91567                 :   0,
   91568                 : #else
   91569                 :   sqlite3_mutex_alloc,
   91570                 :   sqlite3_mutex_enter,
   91571                 :   sqlite3_mutex_free,
   91572                 :   sqlite3_mutex_leave,
   91573                 :   sqlite3_mutex_try,
   91574                 : #endif
   91575                 :   sqlite3_open_v2,
   91576                 :   sqlite3_release_memory,
   91577                 :   sqlite3_result_error_nomem,
   91578                 :   sqlite3_result_error_toobig,
   91579                 :   sqlite3_sleep,
   91580                 :   sqlite3_soft_heap_limit,
   91581                 :   sqlite3_vfs_find,
   91582                 :   sqlite3_vfs_register,
   91583                 :   sqlite3_vfs_unregister,
   91584                 : 
   91585                 :   /*
   91586                 :   ** Added for 3.5.8
   91587                 :   */
   91588                 :   sqlite3_threadsafe,
   91589                 :   sqlite3_result_zeroblob,
   91590                 :   sqlite3_result_error_code,
   91591                 :   sqlite3_test_control,
   91592                 :   sqlite3_randomness,
   91593                 :   sqlite3_context_db_handle,
   91594                 : 
   91595                 :   /*
   91596                 :   ** Added for 3.6.0
   91597                 :   */
   91598                 :   sqlite3_extended_result_codes,
   91599                 :   sqlite3_limit,
   91600                 :   sqlite3_next_stmt,
   91601                 :   sqlite3_sql,
   91602                 :   sqlite3_status,
   91603                 : 
   91604                 :   /*
   91605                 :   ** Added for 3.7.4
   91606                 :   */
   91607                 :   sqlite3_backup_finish,
   91608                 :   sqlite3_backup_init,
   91609                 :   sqlite3_backup_pagecount,
   91610                 :   sqlite3_backup_remaining,
   91611                 :   sqlite3_backup_step,
   91612                 : #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   91613                 :   sqlite3_compileoption_get,
   91614                 :   sqlite3_compileoption_used,
   91615                 : #else
   91616                 :   0,
   91617                 :   0,
   91618                 : #endif
   91619                 :   sqlite3_create_function_v2,
   91620                 :   sqlite3_db_config,
   91621                 :   sqlite3_db_mutex,
   91622                 :   sqlite3_db_status,
   91623                 :   sqlite3_extended_errcode,
   91624                 :   sqlite3_log,
   91625                 :   sqlite3_soft_heap_limit64,
   91626                 :   sqlite3_sourceid,
   91627                 :   sqlite3_stmt_status,
   91628                 :   sqlite3_strnicmp,
   91629                 : #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
   91630                 :   sqlite3_unlock_notify,
   91631                 : #else
   91632                 :   0,
   91633                 : #endif
   91634                 : #ifndef SQLITE_OMIT_WAL
   91635                 :   sqlite3_wal_autocheckpoint,
   91636                 :   sqlite3_wal_checkpoint,
   91637                 :   sqlite3_wal_hook,
   91638                 : #else
   91639                 :   0,
   91640                 :   0,
   91641                 :   0,
   91642                 : #endif
   91643                 :   sqlite3_blob_reopen,
   91644                 :   sqlite3_vtab_config,
   91645                 :   sqlite3_vtab_on_conflict,
   91646                 : };
   91647                 : 
   91648                 : /*
   91649                 : ** Attempt to load an SQLite extension library contained in the file
   91650                 : ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   91651                 : ** default entry point name (sqlite3_extension_init) is used.  Use
   91652                 : ** of the default name is recommended.
   91653                 : **
   91654                 : ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
   91655                 : **
   91656                 : ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
   91657                 : ** error message text.  The calling function should free this memory
   91658                 : ** by calling sqlite3DbFree(db, ).
   91659                 : */
   91660               0 : static int sqlite3LoadExtension(
   91661                 :   sqlite3 *db,          /* Load the extension into this database connection */
   91662                 :   const char *zFile,    /* Name of the shared library containing extension */
   91663                 :   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
   91664                 :   char **pzErrMsg       /* Put error message here if not 0 */
   91665                 : ){
   91666               0 :   sqlite3_vfs *pVfs = db->pVfs;
   91667                 :   void *handle;
   91668                 :   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   91669               0 :   char *zErrmsg = 0;
   91670                 :   void **aHandle;
   91671               0 :   int nMsg = 300 + sqlite3Strlen30(zFile);
   91672                 : 
   91673               0 :   if( pzErrMsg ) *pzErrMsg = 0;
   91674                 : 
   91675                 :   /* Ticket #1863.  To avoid a creating security problems for older
   91676                 :   ** applications that relink against newer versions of SQLite, the
   91677                 :   ** ability to run load_extension is turned off by default.  One
   91678                 :   ** must call sqlite3_enable_load_extension() to turn on extension
   91679                 :   ** loading.  Otherwise you get the following error.
   91680                 :   */
   91681               0 :   if( (db->flags & SQLITE_LoadExtension)==0 ){
   91682               0 :     if( pzErrMsg ){
   91683               0 :       *pzErrMsg = sqlite3_mprintf("not authorized");
   91684                 :     }
   91685               0 :     return SQLITE_ERROR;
   91686                 :   }
   91687                 : 
   91688               0 :   if( zProc==0 ){
   91689               0 :     zProc = "sqlite3_extension_init";
   91690                 :   }
   91691                 : 
   91692               0 :   handle = sqlite3OsDlOpen(pVfs, zFile);
   91693               0 :   if( handle==0 ){
   91694               0 :     if( pzErrMsg ){
   91695               0 :       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
   91696               0 :       if( zErrmsg ){
   91697               0 :         sqlite3_snprintf(nMsg, zErrmsg, 
   91698                 :             "unable to open shared library [%s]", zFile);
   91699               0 :         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
   91700                 :       }
   91701                 :     }
   91702               0 :     return SQLITE_ERROR;
   91703                 :   }
   91704               0 :   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   91705               0 :                    sqlite3OsDlSym(pVfs, handle, zProc);
   91706               0 :   if( xInit==0 ){
   91707               0 :     if( pzErrMsg ){
   91708               0 :       nMsg += sqlite3Strlen30(zProc);
   91709               0 :       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
   91710               0 :       if( zErrmsg ){
   91711               0 :         sqlite3_snprintf(nMsg, zErrmsg,
   91712                 :             "no entry point [%s] in shared library [%s]", zProc,zFile);
   91713               0 :         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
   91714                 :       }
   91715               0 :       sqlite3OsDlClose(pVfs, handle);
   91716                 :     }
   91717               0 :     return SQLITE_ERROR;
   91718               0 :   }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
   91719               0 :     if( pzErrMsg ){
   91720               0 :       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
   91721                 :     }
   91722               0 :     sqlite3_free(zErrmsg);
   91723               0 :     sqlite3OsDlClose(pVfs, handle);
   91724               0 :     return SQLITE_ERROR;
   91725                 :   }
   91726                 : 
   91727                 :   /* Append the new shared library handle to the db->aExtension array. */
   91728               0 :   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
   91729               0 :   if( aHandle==0 ){
   91730               0 :     return SQLITE_NOMEM;
   91731                 :   }
   91732               0 :   if( db->nExtension>0 ){
   91733               0 :     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
   91734                 :   }
   91735               0 :   sqlite3DbFree(db, db->aExtension);
   91736               0 :   db->aExtension = aHandle;
   91737                 : 
   91738               0 :   db->aExtension[db->nExtension++] = handle;
   91739               0 :   return SQLITE_OK;
   91740                 : }
   91741               0 : SQLITE_API int sqlite3_load_extension(
   91742                 :   sqlite3 *db,          /* Load the extension into this database connection */
   91743                 :   const char *zFile,    /* Name of the shared library containing extension */
   91744                 :   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
   91745                 :   char **pzErrMsg       /* Put error message here if not 0 */
   91746                 : ){
   91747                 :   int rc;
   91748               0 :   sqlite3_mutex_enter(db->mutex);
   91749               0 :   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
   91750               0 :   rc = sqlite3ApiExit(db, rc);
   91751               0 :   sqlite3_mutex_leave(db->mutex);
   91752               0 :   return rc;
   91753                 : }
   91754                 : 
   91755                 : /*
   91756                 : ** Call this routine when the database connection is closing in order
   91757                 : ** to clean up loaded extensions
   91758                 : */
   91759            3277 : SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
   91760                 :   int i;
   91761            3277 :   assert( sqlite3_mutex_held(db->mutex) );
   91762            3277 :   for(i=0; i<db->nExtension; i++){
   91763               0 :     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
   91764                 :   }
   91765            3277 :   sqlite3DbFree(db, db->aExtension);
   91766            3277 : }
   91767                 : 
   91768                 : /*
   91769                 : ** Enable or disable extension loading.  Extension loading is disabled by
   91770                 : ** default so as not to open security holes in older applications.
   91771                 : */
   91772               0 : SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
   91773               0 :   sqlite3_mutex_enter(db->mutex);
   91774               0 :   if( onoff ){
   91775               0 :     db->flags |= SQLITE_LoadExtension;
   91776                 :   }else{
   91777               0 :     db->flags &= ~SQLITE_LoadExtension;
   91778                 :   }
   91779               0 :   sqlite3_mutex_leave(db->mutex);
   91780               0 :   return SQLITE_OK;
   91781                 : }
   91782                 : 
   91783                 : #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   91784                 : 
   91785                 : /*
   91786                 : ** The auto-extension code added regardless of whether or not extension
   91787                 : ** loading is supported.  We need a dummy sqlite3Apis pointer for that
   91788                 : ** code if regular extension loading is not available.  This is that
   91789                 : ** dummy pointer.
   91790                 : */
   91791                 : #ifdef SQLITE_OMIT_LOAD_EXTENSION
   91792                 : static const sqlite3_api_routines sqlite3Apis = { 0 };
   91793                 : #endif
   91794                 : 
   91795                 : 
   91796                 : /*
   91797                 : ** The following object holds the list of automatically loaded
   91798                 : ** extensions.
   91799                 : **
   91800                 : ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
   91801                 : ** mutex must be held while accessing this list.
   91802                 : */
   91803                 : typedef struct sqlite3AutoExtList sqlite3AutoExtList;
   91804                 : static SQLITE_WSD struct sqlite3AutoExtList {
   91805                 :   int nExt;              /* Number of entries in aExt[] */          
   91806                 :   void (**aExt)(void);   /* Pointers to the extension init functions */
   91807                 : } sqlite3Autoext = { 0, 0 };
   91808                 : 
   91809                 : /* The "wsdAutoext" macro will resolve to the autoextension
   91810                 : ** state vector.  If writable static data is unsupported on the target,
   91811                 : ** we have to locate the state vector at run-time.  In the more common
   91812                 : ** case where writable static data is supported, wsdStat can refer directly
   91813                 : ** to the "sqlite3Autoext" state vector declared above.
   91814                 : */
   91815                 : #ifdef SQLITE_OMIT_WSD
   91816                 : # define wsdAutoextInit \
   91817                 :   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
   91818                 : # define wsdAutoext x[0]
   91819                 : #else
   91820                 : # define wsdAutoextInit
   91821                 : # define wsdAutoext sqlite3Autoext
   91822                 : #endif
   91823                 : 
   91824                 : 
   91825                 : /*
   91826                 : ** Register a statically linked extension that is automatically
   91827                 : ** loaded by every new database connection.
   91828                 : */
   91829               0 : SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
   91830               0 :   int rc = SQLITE_OK;
   91831                 : #ifndef SQLITE_OMIT_AUTOINIT
   91832               0 :   rc = sqlite3_initialize();
   91833               0 :   if( rc ){
   91834               0 :     return rc;
   91835                 :   }else
   91836                 : #endif
   91837                 :   {
   91838                 :     int i;
   91839                 : #if SQLITE_THREADSAFE
   91840               0 :     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   91841                 : #endif
   91842                 :     wsdAutoextInit;
   91843               0 :     sqlite3_mutex_enter(mutex);
   91844               0 :     for(i=0; i<wsdAutoext.nExt; i++){
   91845               0 :       if( wsdAutoext.aExt[i]==xInit ) break;
   91846                 :     }
   91847               0 :     if( i==wsdAutoext.nExt ){
   91848               0 :       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
   91849                 :       void (**aNew)(void);
   91850               0 :       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
   91851               0 :       if( aNew==0 ){
   91852               0 :         rc = SQLITE_NOMEM;
   91853                 :       }else{
   91854               0 :         wsdAutoext.aExt = aNew;
   91855               0 :         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
   91856               0 :         wsdAutoext.nExt++;
   91857                 :       }
   91858                 :     }
   91859               0 :     sqlite3_mutex_leave(mutex);
   91860               0 :     assert( (rc&0xff)==rc );
   91861               0 :     return rc;
   91862                 :   }
   91863                 : }
   91864                 : 
   91865                 : /*
   91866                 : ** Reset the automatic extension loading mechanism.
   91867                 : */
   91868             795 : SQLITE_API void sqlite3_reset_auto_extension(void){
   91869                 : #ifndef SQLITE_OMIT_AUTOINIT
   91870             795 :   if( sqlite3_initialize()==SQLITE_OK )
   91871                 : #endif
   91872                 :   {
   91873                 : #if SQLITE_THREADSAFE
   91874             795 :     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   91875                 : #endif
   91876                 :     wsdAutoextInit;
   91877             795 :     sqlite3_mutex_enter(mutex);
   91878             795 :     sqlite3_free(wsdAutoext.aExt);
   91879             795 :     wsdAutoext.aExt = 0;
   91880             795 :     wsdAutoext.nExt = 0;
   91881             795 :     sqlite3_mutex_leave(mutex);
   91882                 :   }
   91883             795 : }
   91884                 : 
   91885                 : /*
   91886                 : ** Load all automatic extensions.
   91887                 : **
   91888                 : ** If anything goes wrong, set an error in the database connection.
   91889                 : */
   91890            3277 : SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
   91891                 :   int i;
   91892            3277 :   int go = 1;
   91893                 :   int rc;
   91894                 :   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   91895                 : 
   91896                 :   wsdAutoextInit;
   91897            3277 :   if( wsdAutoext.nExt==0 ){
   91898                 :     /* Common case: early out without every having to acquire a mutex */
   91899            3277 :     return;
   91900                 :   }
   91901               0 :   for(i=0; go; i++){
   91902                 :     char *zErrmsg;
   91903                 : #if SQLITE_THREADSAFE
   91904               0 :     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   91905                 : #endif
   91906               0 :     sqlite3_mutex_enter(mutex);
   91907               0 :     if( i>=wsdAutoext.nExt ){
   91908               0 :       xInit = 0;
   91909               0 :       go = 0;
   91910                 :     }else{
   91911               0 :       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   91912               0 :               wsdAutoext.aExt[i];
   91913                 :     }
   91914               0 :     sqlite3_mutex_leave(mutex);
   91915               0 :     zErrmsg = 0;
   91916               0 :     if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
   91917               0 :       sqlite3Error(db, rc,
   91918                 :             "automatic extension loading failed: %s", zErrmsg);
   91919               0 :       go = 0;
   91920                 :     }
   91921               0 :     sqlite3_free(zErrmsg);
   91922                 :   }
   91923                 : }
   91924                 : 
   91925                 : /************** End of loadext.c *********************************************/
   91926                 : /************** Begin file pragma.c ******************************************/
   91927                 : /*
   91928                 : ** 2003 April 6
   91929                 : **
   91930                 : ** The author disclaims copyright to this source code.  In place of
   91931                 : ** a legal notice, here is a blessing:
   91932                 : **
   91933                 : **    May you do good and not evil.
   91934                 : **    May you find forgiveness for yourself and forgive others.
   91935                 : **    May you share freely, never taking more than you give.
   91936                 : **
   91937                 : *************************************************************************
   91938                 : ** This file contains code used to implement the PRAGMA command.
   91939                 : */
   91940                 : 
   91941                 : /*
   91942                 : ** Interpret the given string as a safety level.  Return 0 for OFF,
   91943                 : ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or 
   91944                 : ** unrecognized string argument.
   91945                 : **
   91946                 : ** Note that the values returned are one less that the values that
   91947                 : ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
   91948                 : ** to support legacy SQL code.  The safety level used to be boolean
   91949                 : ** and older scripts may have used numbers 0 for OFF and 1 for ON.
   91950                 : */
   91951            5601 : static u8 getSafetyLevel(const char *z){
   91952                 :                              /* 123456789 123456789 */
   91953                 :   static const char zText[] = "onoffalseyestruefull";
   91954                 :   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
   91955                 :   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
   91956                 :   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
   91957                 :   int i, n;
   91958            5601 :   if( sqlite3Isdigit(*z) ){
   91959              26 :     return (u8)sqlite3Atoi(z);
   91960                 :   }
   91961            5575 :   n = sqlite3Strlen30(z);
   91962           35790 :   for(i=0; i<ArraySize(iLength); i++){
   91963           32523 :     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
   91964            2308 :       return iValue[i];
   91965                 :     }
   91966                 :   }
   91967            3267 :   return 1;
   91968                 : }
   91969                 : 
   91970                 : /*
   91971                 : ** Interpret the given string as a boolean value.
   91972                 : */
   91973             619 : SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z){
   91974             619 :   return getSafetyLevel(z)&1;
   91975                 : }
   91976                 : 
   91977                 : /* The sqlite3GetBoolean() function is used by other modules but the
   91978                 : ** remainder of this file is specific to PRAGMA processing.  So omit
   91979                 : ** the rest of the file if PRAGMAs are omitted from the build.
   91980                 : */
   91981                 : #if !defined(SQLITE_OMIT_PRAGMA)
   91982                 : 
   91983                 : /*
   91984                 : ** Interpret the given string as a locking mode value.
   91985                 : */
   91986            1060 : static int getLockingMode(const char *z){
   91987            1060 :   if( z ){
   91988            1060 :     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
   91989               0 :     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
   91990                 :   }
   91991               0 :   return PAGER_LOCKINGMODE_QUERY;
   91992                 : }
   91993                 : 
   91994                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   91995                 : /*
   91996                 : ** Interpret the given string as an auto-vacuum mode value.
   91997                 : **
   91998                 : ** The following strings, "none", "full" and "incremental" are 
   91999                 : ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
   92000                 : */
   92001               0 : static int getAutoVacuum(const char *z){
   92002                 :   int i;
   92003               0 :   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
   92004               0 :   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
   92005               0 :   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
   92006               0 :   i = sqlite3Atoi(z);
   92007               0 :   return (u8)((i>=0&&i<=2)?i:0);
   92008                 : }
   92009                 : #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
   92010                 : 
   92011                 : #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   92012                 : /*
   92013                 : ** Interpret the given string as a temp db location. Return 1 for file
   92014                 : ** backed temporary databases, 2 for the Red-Black tree in memory database
   92015                 : ** and 0 to use the compile-time default.
   92016                 : */
   92017             397 : static int getTempStore(const char *z){
   92018             397 :   if( z[0]>='0' && z[0]<='2' ){
   92019              52 :     return z[0] - '0';
   92020             345 :   }else if( sqlite3StrICmp(z, "file")==0 ){
   92021               0 :     return 1;
   92022             345 :   }else if( sqlite3StrICmp(z, "memory")==0 ){
   92023             345 :     return 2;
   92024                 :   }else{
   92025               0 :     return 0;
   92026                 :   }
   92027                 : }
   92028                 : #endif /* SQLITE_PAGER_PRAGMAS */
   92029                 : 
   92030                 : #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   92031                 : /*
   92032                 : ** Invalidate temp storage, either when the temp storage is changed
   92033                 : ** from default, or when 'file' and the temp_store_directory has changed
   92034                 : */
   92035             379 : static int invalidateTempStorage(Parse *pParse){
   92036             379 :   sqlite3 *db = pParse->db;
   92037             379 :   if( db->aDb[1].pBt!=0 ){
   92038               0 :     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
   92039               0 :       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
   92040                 :         "from within a transaction");
   92041               0 :       return SQLITE_ERROR;
   92042                 :     }
   92043               0 :     sqlite3BtreeClose(db->aDb[1].pBt);
   92044               0 :     db->aDb[1].pBt = 0;
   92045               0 :     sqlite3ResetInternalSchema(db, -1);
   92046                 :   }
   92047             379 :   return SQLITE_OK;
   92048                 : }
   92049                 : #endif /* SQLITE_PAGER_PRAGMAS */
   92050                 : 
   92051                 : #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   92052                 : /*
   92053                 : ** If the TEMP database is open, close it and mark the database schema
   92054                 : ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
   92055                 : ** or DEFAULT_TEMP_STORE pragmas.
   92056                 : */
   92057             397 : static int changeTempStorage(Parse *pParse, const char *zStorageType){
   92058             397 :   int ts = getTempStore(zStorageType);
   92059             397 :   sqlite3 *db = pParse->db;
   92060             397 :   if( db->temp_store==ts ) return SQLITE_OK;
   92061             379 :   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
   92062               0 :     return SQLITE_ERROR;
   92063                 :   }
   92064             379 :   db->temp_store = (u8)ts;
   92065             379 :   return SQLITE_OK;
   92066                 : }
   92067                 : #endif /* SQLITE_PAGER_PRAGMAS */
   92068                 : 
   92069                 : /*
   92070                 : ** Generate code to return a single integer value.
   92071                 : */
   92072            4769 : static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
   92073            4769 :   Vdbe *v = sqlite3GetVdbe(pParse);
   92074            4769 :   int mem = ++pParse->nMem;
   92075            4769 :   i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
   92076            4769 :   if( pI64 ){
   92077            4769 :     memcpy(pI64, &value, sizeof(value));
   92078                 :   }
   92079            4769 :   sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
   92080            4769 :   sqlite3VdbeSetNumCols(v, 1);
   92081            4769 :   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
   92082            4769 :   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
   92083            4769 : }
   92084                 : 
   92085                 : #ifndef SQLITE_OMIT_FLAG_PRAGMAS
   92086                 : /*
   92087                 : ** Check to see if zRight and zLeft refer to a pragma that queries
   92088                 : ** or changes one of the flags in db->flags.  Return 1 if so and 0 if not.
   92089                 : ** Also, implement the pragma.
   92090                 : */
   92091            4634 : static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){
   92092                 :   static const struct sPragmaType {
   92093                 :     const char *zName;  /* Name of the pragma */
   92094                 :     int mask;           /* Mask for the db->flags value */
   92095                 :   } aPragma[] = {
   92096                 :     { "full_column_names",        SQLITE_FullColNames  },
   92097                 :     { "short_column_names",       SQLITE_ShortColNames },
   92098                 :     { "count_changes",            SQLITE_CountRows     },
   92099                 :     { "empty_result_callbacks",   SQLITE_NullCallback  },
   92100                 :     { "legacy_file_format",       SQLITE_LegacyFileFmt },
   92101                 :     { "fullfsync",                SQLITE_FullFSync     },
   92102                 :     { "checkpoint_fullfsync",     SQLITE_CkptFullFSync },
   92103                 :     { "reverse_unordered_selects", SQLITE_ReverseOrder  },
   92104                 : #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
   92105                 :     { "automatic_index",          SQLITE_AutoIndex     },
   92106                 : #endif
   92107                 : #ifdef SQLITE_DEBUG
   92108                 :     { "sql_trace",                SQLITE_SqlTrace      },
   92109                 :     { "vdbe_listing",             SQLITE_VdbeListing   },
   92110                 :     { "vdbe_trace",               SQLITE_VdbeTrace     },
   92111                 : #endif
   92112                 : #ifndef SQLITE_OMIT_CHECK
   92113                 :     { "ignore_check_constraints", SQLITE_IgnoreChecks  },
   92114                 : #endif
   92115                 :     /* The following is VERY experimental */
   92116                 :     { "writable_schema",          SQLITE_WriteSchema|SQLITE_RecoveryMode },
   92117                 :     { "omit_readlock",            SQLITE_NoReadlock    },
   92118                 : 
   92119                 :     /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted
   92120                 :     ** flag if there are any active statements. */
   92121                 :     { "read_uncommitted",         SQLITE_ReadUncommitted },
   92122                 :     { "recursive_triggers",       SQLITE_RecTriggers },
   92123                 : 
   92124                 :     /* This flag may only be set if both foreign-key and trigger support
   92125                 :     ** are present in the build.  */
   92126                 : #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
   92127                 :     { "foreign_keys",             SQLITE_ForeignKeys },
   92128                 : #endif
   92129                 :   };
   92130                 :   int i;
   92131                 :   const struct sPragmaType *p;
   92132           87379 :   for(i=0, p=aPragma; i<ArraySize(aPragma); i++, p++){
   92133           83382 :     if( sqlite3StrICmp(zLeft, p->zName)==0 ){
   92134             637 :       sqlite3 *db = pParse->db;
   92135                 :       Vdbe *v;
   92136             637 :       v = sqlite3GetVdbe(pParse);
   92137             637 :       assert( v!=0 );  /* Already allocated by sqlite3Pragma() */
   92138             637 :       if( ALWAYS(v) ){
   92139             637 :         if( zRight==0 ){
   92140              18 :           returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 );
   92141                 :         }else{
   92142             619 :           int mask = p->mask;          /* Mask of bits to set or clear. */
   92143             619 :           if( db->autoCommit==0 ){
   92144                 :             /* Foreign key support may not be enabled or disabled while not
   92145                 :             ** in auto-commit mode.  */
   92146               0 :             mask &= ~(SQLITE_ForeignKeys);
   92147                 :           }
   92148                 : 
   92149             619 :           if( sqlite3GetBoolean(zRight) ){
   92150             610 :             db->flags |= mask;
   92151                 :           }else{
   92152               9 :             db->flags &= ~mask;
   92153                 :           }
   92154                 : 
   92155                 :           /* Many of the flag-pragmas modify the code generated by the SQL 
   92156                 :           ** compiler (eg. count_changes). So add an opcode to expire all
   92157                 :           ** compiled SQL statements after modifying a pragma value.
   92158                 :           */
   92159             619 :           sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
   92160                 :         }
   92161                 :       }
   92162                 : 
   92163             637 :       return 1;
   92164                 :     }
   92165                 :   }
   92166            3997 :   return 0;
   92167                 : }
   92168                 : #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
   92169                 : 
   92170                 : /*
   92171                 : ** Return a human-readable name for a constraint resolution action.
   92172                 : */
   92173                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   92174               0 : static const char *actionName(u8 action){
   92175                 :   const char *zName;
   92176               0 :   switch( action ){
   92177               0 :     case OE_SetNull:  zName = "SET NULL";        break;
   92178               0 :     case OE_SetDflt:  zName = "SET DEFAULT";     break;
   92179               0 :     case OE_Cascade:  zName = "CASCADE";         break;
   92180               0 :     case OE_Restrict: zName = "RESTRICT";        break;
   92181               0 :     default:          zName = "NO ACTION";  
   92182               0 :                       assert( action==OE_None ); break;
   92183                 :   }
   92184               0 :   return zName;
   92185                 : }
   92186                 : #endif
   92187                 : 
   92188                 : 
   92189                 : /*
   92190                 : ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
   92191                 : ** defined in pager.h. This function returns the associated lowercase
   92192                 : ** journal-mode name.
   92193                 : */
   92194            3589 : SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
   92195                 :   static char * const azModeName[] = {
   92196                 :     "delete", "persist", "off", "truncate", "memory"
   92197                 : #ifndef SQLITE_OMIT_WAL
   92198                 :      , "wal"
   92199                 : #endif
   92200                 :   };
   92201                 :   assert( PAGER_JOURNALMODE_DELETE==0 );
   92202                 :   assert( PAGER_JOURNALMODE_PERSIST==1 );
   92203                 :   assert( PAGER_JOURNALMODE_OFF==2 );
   92204                 :   assert( PAGER_JOURNALMODE_TRUNCATE==3 );
   92205                 :   assert( PAGER_JOURNALMODE_MEMORY==4 );
   92206                 :   assert( PAGER_JOURNALMODE_WAL==5 );
   92207            3589 :   assert( eMode>=0 && eMode<=ArraySize(azModeName) );
   92208                 : 
   92209            3589 :   if( eMode==ArraySize(azModeName) ) return 0;
   92210            3589 :   return azModeName[eMode];
   92211                 : }
   92212                 : 
   92213                 : /*
   92214                 : ** Process a pragma statement.  
   92215                 : **
   92216                 : ** Pragmas are of this form:
   92217                 : **
   92218                 : **      PRAGMA [database.]id [= value]
   92219                 : **
   92220                 : ** The identifier might also be a string.  The value is a string, and
   92221                 : ** identifier, or a number.  If minusFlag is true, then the value is
   92222                 : ** a number that was preceded by a minus sign.
   92223                 : **
   92224                 : ** If the left side is "database.id" then pId1 is the database name
   92225                 : ** and pId2 is the id.  If the left side is just "id" then pId1 is the
   92226                 : ** id and pId2 is any empty string.
   92227                 : */
   92228           22669 : SQLITE_PRIVATE void sqlite3Pragma(
   92229                 :   Parse *pParse, 
   92230                 :   Token *pId1,        /* First part of [database.]id field */
   92231                 :   Token *pId2,        /* Second part of [database.]id field, or NULL */
   92232                 :   Token *pValue,      /* Token for <value>, or NULL */
   92233                 :   int minusFlag       /* True if a '-' sign preceded <value> */
   92234                 : ){
   92235           22669 :   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
   92236           22669 :   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
   92237           22669 :   const char *zDb = 0;   /* The database name */
   92238                 :   Token *pId;            /* Pointer to <id> token */
   92239                 :   int iDb;               /* Database index for <database> */
   92240           22669 :   sqlite3 *db = pParse->db;
   92241                 :   Db *pDb;
   92242           22669 :   Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db);
   92243           22669 :   if( v==0 ) return;
   92244           22669 :   sqlite3VdbeRunOnlyOnce(v);
   92245           22669 :   pParse->nMem = 2;
   92246                 : 
   92247                 :   /* Interpret the [database.] part of the pragma statement. iDb is the
   92248                 :   ** index of the database this pragma is being applied to in db.aDb[]. */
   92249           22669 :   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
   92250           22669 :   if( iDb<0 ) return;
   92251           22669 :   pDb = &db->aDb[iDb];
   92252                 : 
   92253                 :   /* If the temp database has been explicitly named as part of the 
   92254                 :   ** pragma, make sure it is open. 
   92255                 :   */
   92256           22669 :   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
   92257               0 :     return;
   92258                 :   }
   92259                 : 
   92260           22669 :   zLeft = sqlite3NameFromToken(db, pId);
   92261           22669 :   if( !zLeft ) return;
   92262           22669 :   if( minusFlag ){
   92263              35 :     zRight = sqlite3MPrintf(db, "-%T", pValue);
   92264                 :   }else{
   92265           22634 :     zRight = sqlite3NameFromToken(db, pValue);
   92266                 :   }
   92267                 : 
   92268           22669 :   assert( pId2 );
   92269           22669 :   zDb = pId2->n>0 ? pDb->zName : 0;
   92270           22669 :   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
   92271               0 :     goto pragma_out;
   92272                 :   }
   92273                 :  
   92274                 : #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
   92275                 :   /*
   92276                 :   **  PRAGMA [database.]default_cache_size
   92277                 :   **  PRAGMA [database.]default_cache_size=N
   92278                 :   **
   92279                 :   ** The first form reports the current persistent setting for the
   92280                 :   ** page cache size.  The value returned is the maximum number of
   92281                 :   ** pages in the page cache.  The second form sets both the current
   92282                 :   ** page cache size value and the persistent page cache size value
   92283                 :   ** stored in the database file.
   92284                 :   **
   92285                 :   ** Older versions of SQLite would set the default cache size to a
   92286                 :   ** negative number to indicate synchronous=OFF.  These days, synchronous
   92287                 :   ** is always on by default regardless of the sign of the default cache
   92288                 :   ** size.  But continue to take the absolute value of the default cache
   92289                 :   ** size of historical compatibility.
   92290                 :   */
   92291           22669 :   if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
   92292                 :     static const VdbeOpList getCacheSize[] = {
   92293                 :       { OP_Transaction, 0, 0,        0},                         /* 0 */
   92294                 :       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
   92295                 :       { OP_IfPos,       1, 7,        0},
   92296                 :       { OP_Integer,     0, 2,        0},
   92297                 :       { OP_Subtract,    1, 2,        1},
   92298                 :       { OP_IfPos,       1, 7,        0},
   92299                 :       { OP_Integer,     0, 1,        0},                         /* 6 */
   92300                 :       { OP_ResultRow,   1, 1,        0},
   92301                 :     };
   92302                 :     int addr;
   92303               0 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92304               0 :     sqlite3VdbeUsesBtree(v, iDb);
   92305               0 :     if( !zRight ){
   92306               0 :       sqlite3VdbeSetNumCols(v, 1);
   92307               0 :       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
   92308               0 :       pParse->nMem += 2;
   92309               0 :       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
   92310               0 :       sqlite3VdbeChangeP1(v, addr, iDb);
   92311               0 :       sqlite3VdbeChangeP1(v, addr+1, iDb);
   92312               0 :       sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
   92313                 :     }else{
   92314               0 :       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
   92315               0 :       sqlite3BeginWriteOperation(pParse, 0, iDb);
   92316               0 :       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
   92317               0 :       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
   92318               0 :       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   92319               0 :       pDb->pSchema->cache_size = size;
   92320               0 :       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
   92321                 :     }
   92322                 :   }else
   92323                 : #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
   92324                 : 
   92325                 : #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
   92326                 :   /*
   92327                 :   **  PRAGMA [database.]page_size
   92328                 :   **  PRAGMA [database.]page_size=N
   92329                 :   **
   92330                 :   ** The first form reports the current setting for the
   92331                 :   ** database page size in bytes.  The second form sets the
   92332                 :   ** database page size value.  The value can only be set if
   92333                 :   ** the database has not yet been created.
   92334                 :   */
   92335           22669 :   if( sqlite3StrICmp(zLeft,"page_size")==0 ){
   92336            7069 :     Btree *pBt = pDb->pBt;
   92337            7069 :     assert( pBt!=0 );
   92338            7069 :     if( !zRight ){
   92339            3785 :       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
   92340            3785 :       returnSingleInt(pParse, "page_size", size);
   92341                 :     }else{
   92342                 :       /* Malloc may fail when setting the page-size, as there is an internal
   92343                 :       ** buffer that the pager module resizes using sqlite3_realloc().
   92344                 :       */
   92345            3284 :       db->nextPagesize = sqlite3Atoi(zRight);
   92346            3284 :       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
   92347               0 :         db->mallocFailed = 1;
   92348                 :       }
   92349                 :     }
   92350                 :   }else
   92351                 : 
   92352                 :   /*
   92353                 :   **  PRAGMA [database.]secure_delete
   92354                 :   **  PRAGMA [database.]secure_delete=ON/OFF
   92355                 :   **
   92356                 :   ** The first form reports the current setting for the
   92357                 :   ** secure_delete flag.  The second form changes the secure_delete
   92358                 :   ** flag setting and reports thenew value.
   92359                 :   */
   92360           15600 :   if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){
   92361               0 :     Btree *pBt = pDb->pBt;
   92362               0 :     int b = -1;
   92363               0 :     assert( pBt!=0 );
   92364               0 :     if( zRight ){
   92365               0 :       b = sqlite3GetBoolean(zRight);
   92366                 :     }
   92367               0 :     if( pId2->n==0 && b>=0 ){
   92368                 :       int ii;
   92369               0 :       for(ii=0; ii<db->nDb; ii++){
   92370               0 :         sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
   92371                 :       }
   92372                 :     }
   92373               0 :     b = sqlite3BtreeSecureDelete(pBt, b);
   92374               0 :     returnSingleInt(pParse, "secure_delete", b);
   92375                 :   }else
   92376                 : 
   92377                 :   /*
   92378                 :   **  PRAGMA [database.]max_page_count
   92379                 :   **  PRAGMA [database.]max_page_count=N
   92380                 :   **
   92381                 :   ** The first form reports the current setting for the
   92382                 :   ** maximum number of pages in the database file.  The 
   92383                 :   ** second form attempts to change this setting.  Both
   92384                 :   ** forms return the current setting.
   92385                 :   **
   92386                 :   ** The absolute value of N is used.  This is undocumented and might
   92387                 :   ** change.  The only purpose is to provide an easy way to test
   92388                 :   ** the sqlite3AbsInt32() function.
   92389                 :   **
   92390                 :   **  PRAGMA [database.]page_count
   92391                 :   **
   92392                 :   ** Return the number of pages in the specified database.
   92393                 :   */
   92394           15600 :   if( sqlite3StrICmp(zLeft,"page_count")==0
   92395           15599 :    || sqlite3StrICmp(zLeft,"max_page_count")==0
   92396               1 :   ){
   92397                 :     int iReg;
   92398               1 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92399               1 :     sqlite3CodeVerifySchema(pParse, iDb);
   92400               1 :     iReg = ++pParse->nMem;
   92401               1 :     if( sqlite3Tolower(zLeft[0])=='p' ){
   92402               1 :       sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
   92403                 :     }else{
   92404               0 :       sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, 
   92405                 :                         sqlite3AbsInt32(sqlite3Atoi(zRight)));
   92406                 :     }
   92407               1 :     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
   92408               1 :     sqlite3VdbeSetNumCols(v, 1);
   92409               1 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
   92410                 :   }else
   92411                 : 
   92412                 :   /*
   92413                 :   **  PRAGMA [database.]locking_mode
   92414                 :   **  PRAGMA [database.]locking_mode = (normal|exclusive)
   92415                 :   */
   92416           15599 :   if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){
   92417            1060 :     const char *zRet = "normal";
   92418            1060 :     int eMode = getLockingMode(zRight);
   92419                 : 
   92420            1060 :     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
   92421                 :       /* Simple "PRAGMA locking_mode;" statement. This is a query for
   92422                 :       ** the current default locking mode (which may be different to
   92423                 :       ** the locking-mode of the main database).
   92424                 :       */
   92425               0 :       eMode = db->dfltLockMode;
   92426                 :     }else{
   92427                 :       Pager *pPager;
   92428            1060 :       if( pId2->n==0 ){
   92429                 :         /* This indicates that no database name was specified as part
   92430                 :         ** of the PRAGMA command. In this case the locking-mode must be
   92431                 :         ** set on all attached databases, as well as the main db file.
   92432                 :         **
   92433                 :         ** Also, the sqlite3.dfltLockMode variable is set so that
   92434                 :         ** any subsequently attached databases also use the specified
   92435                 :         ** locking mode.
   92436                 :         */
   92437                 :         int ii;
   92438            1060 :         assert(pDb==&db->aDb[0]);
   92439            1060 :         for(ii=2; ii<db->nDb; ii++){
   92440               0 :           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
   92441               0 :           sqlite3PagerLockingMode(pPager, eMode);
   92442                 :         }
   92443            1060 :         db->dfltLockMode = (u8)eMode;
   92444                 :       }
   92445            1060 :       pPager = sqlite3BtreePager(pDb->pBt);
   92446            1060 :       eMode = sqlite3PagerLockingMode(pPager, eMode);
   92447                 :     }
   92448                 : 
   92449            1060 :     assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE);
   92450            1060 :     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
   92451            1060 :       zRet = "exclusive";
   92452                 :     }
   92453            1060 :     sqlite3VdbeSetNumCols(v, 1);
   92454            1060 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
   92455            1060 :     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
   92456            1060 :     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   92457                 :   }else
   92458                 : 
   92459                 :   /*
   92460                 :   **  PRAGMA [database.]journal_mode
   92461                 :   **  PRAGMA [database.]journal_mode =
   92462                 :   **                      (delete|persist|off|truncate|memory|wal|off)
   92463                 :   */
   92464           14539 :   if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
   92465                 :     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
   92466                 :     int ii;           /* Loop counter */
   92467                 : 
   92468                 :     /* Force the schema to be loaded on all databases.  This causes all
   92469                 :     ** database files to be opened and the journal_modes set.  This is
   92470                 :     ** necessary because subsequent processing must know if the databases
   92471                 :     ** are in WAL mode. */
   92472             517 :     if( sqlite3ReadSchema(pParse) ){
   92473               0 :       goto pragma_out;
   92474                 :     }
   92475                 : 
   92476             517 :     sqlite3VdbeSetNumCols(v, 1);
   92477             517 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
   92478                 : 
   92479             517 :     if( zRight==0 ){
   92480                 :       /* If there is no "=MODE" part of the pragma, do a query for the
   92481                 :       ** current mode */
   92482               5 :       eMode = PAGER_JOURNALMODE_QUERY;
   92483                 :     }else{
   92484                 :       const char *zMode;
   92485             512 :       int n = sqlite3Strlen30(zRight);
   92486            3072 :       for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
   92487            3072 :         if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
   92488                 :       }
   92489             512 :       if( !zMode ){
   92490                 :         /* If the "=MODE" part does not match any known journal mode,
   92491                 :         ** then do a query */
   92492               0 :         eMode = PAGER_JOURNALMODE_QUERY;
   92493                 :       }
   92494                 :     }
   92495             517 :     if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
   92496                 :       /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
   92497               5 :       iDb = 0;
   92498               5 :       pId2->n = 1;
   92499                 :     }
   92500            1551 :     for(ii=db->nDb-1; ii>=0; ii--){
   92501            1034 :       if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
   92502             517 :         sqlite3VdbeUsesBtree(v, ii);
   92503             517 :         sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
   92504                 :       }
   92505                 :     }
   92506             517 :     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   92507                 :   }else
   92508                 : 
   92509                 :   /*
   92510                 :   **  PRAGMA [database.]journal_size_limit
   92511                 :   **  PRAGMA [database.]journal_size_limit=N
   92512                 :   **
   92513                 :   ** Get or set the size limit on rollback journal files.
   92514                 :   */
   92515           14022 :   if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
   92516             293 :     Pager *pPager = sqlite3BtreePager(pDb->pBt);
   92517             293 :     i64 iLimit = -2;
   92518             293 :     if( zRight ){
   92519             279 :       sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
   92520             279 :       if( iLimit<-1 ) iLimit = -1;
   92521                 :     }
   92522             293 :     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
   92523             293 :     returnSingleInt(pParse, "journal_size_limit", iLimit);
   92524                 :   }else
   92525                 : 
   92526                 : #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
   92527                 : 
   92528                 :   /*
   92529                 :   **  PRAGMA [database.]auto_vacuum
   92530                 :   **  PRAGMA [database.]auto_vacuum=N
   92531                 :   **
   92532                 :   ** Get or set the value of the database 'auto-vacuum' parameter.
   92533                 :   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
   92534                 :   */
   92535                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   92536           13729 :   if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){
   92537               0 :     Btree *pBt = pDb->pBt;
   92538               0 :     assert( pBt!=0 );
   92539               0 :     if( sqlite3ReadSchema(pParse) ){
   92540               0 :       goto pragma_out;
   92541                 :     }
   92542               0 :     if( !zRight ){
   92543                 :       int auto_vacuum;
   92544               0 :       if( ALWAYS(pBt) ){
   92545               0 :          auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt);
   92546                 :       }else{
   92547               0 :          auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM;
   92548                 :       }
   92549               0 :       returnSingleInt(pParse, "auto_vacuum", auto_vacuum);
   92550                 :     }else{
   92551               0 :       int eAuto = getAutoVacuum(zRight);
   92552               0 :       assert( eAuto>=0 && eAuto<=2 );
   92553               0 :       db->nextAutovac = (u8)eAuto;
   92554               0 :       if( ALWAYS(eAuto>=0) ){
   92555                 :         /* Call SetAutoVacuum() to set initialize the internal auto and
   92556                 :         ** incr-vacuum flags. This is required in case this connection
   92557                 :         ** creates the database file. It is important that it is created
   92558                 :         ** as an auto-vacuum capable db.
   92559                 :         */
   92560               0 :         int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
   92561               0 :         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
   92562                 :           /* When setting the auto_vacuum mode to either "full" or 
   92563                 :           ** "incremental", write the value of meta[6] in the database
   92564                 :           ** file. Before writing to meta[6], check that meta[3] indicates
   92565                 :           ** that this really is an auto-vacuum capable database.
   92566                 :           */
   92567                 :           static const VdbeOpList setMeta6[] = {
   92568                 :             { OP_Transaction,    0,         1,                 0},    /* 0 */
   92569                 :             { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
   92570                 :             { OP_If,             1,         0,                 0},    /* 2 */
   92571                 :             { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
   92572                 :             { OP_Integer,        0,         1,                 0},    /* 4 */
   92573                 :             { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
   92574                 :           };
   92575                 :           int iAddr;
   92576               0 :           iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
   92577               0 :           sqlite3VdbeChangeP1(v, iAddr, iDb);
   92578               0 :           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
   92579               0 :           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
   92580               0 :           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
   92581               0 :           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
   92582               0 :           sqlite3VdbeUsesBtree(v, iDb);
   92583                 :         }
   92584                 :       }
   92585                 :     }
   92586                 :   }else
   92587                 : #endif
   92588                 : 
   92589                 :   /*
   92590                 :   **  PRAGMA [database.]incremental_vacuum(N)
   92591                 :   **
   92592                 :   ** Do N steps of incremental vacuuming on a database.
   92593                 :   */
   92594                 : #ifndef SQLITE_OMIT_AUTOVACUUM
   92595           13729 :   if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){
   92596                 :     int iLimit, addr;
   92597               0 :     if( sqlite3ReadSchema(pParse) ){
   92598               0 :       goto pragma_out;
   92599                 :     }
   92600               0 :     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
   92601               0 :       iLimit = 0x7fffffff;
   92602                 :     }
   92603               0 :     sqlite3BeginWriteOperation(pParse, 0, iDb);
   92604               0 :     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
   92605               0 :     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
   92606               0 :     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
   92607               0 :     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
   92608               0 :     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
   92609               0 :     sqlite3VdbeJumpHere(v, addr);
   92610                 :   }else
   92611                 : #endif
   92612                 : 
   92613                 : #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   92614                 :   /*
   92615                 :   **  PRAGMA [database.]cache_size
   92616                 :   **  PRAGMA [database.]cache_size=N
   92617                 :   **
   92618                 :   ** The first form reports the current local setting for the
   92619                 :   ** page cache size. The second form sets the local
   92620                 :   ** page cache size value.  If N is positive then that is the
   92621                 :   ** number of pages in the cache.  If N is negative, then the
   92622                 :   ** number of pages is adjusted so that the cache uses -N kibibytes
   92623                 :   ** of memory.
   92624                 :   */
   92625           13729 :   if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
   92626            3644 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92627            3634 :     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   92628            3634 :     if( !zRight ){
   92629              63 :       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
   92630                 :     }else{
   92631            3571 :       int size = sqlite3Atoi(zRight);
   92632            3571 :       pDb->pSchema->cache_size = size;
   92633            3571 :       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
   92634                 :     }
   92635                 :   }else
   92636                 : 
   92637                 :   /*
   92638                 :   **   PRAGMA temp_store
   92639                 :   **   PRAGMA temp_store = "default"|"memory"|"file"
   92640                 :   **
   92641                 :   ** Return or set the local value of the temp_store flag.  Changing
   92642                 :   ** the local value does not make changes to the disk file and the default
   92643                 :   ** value will be restored the next time the database is opened.
   92644                 :   **
   92645                 :   ** Note that it is possible for the library compile-time options to
   92646                 :   ** override this setting
   92647                 :   */
   92648           10085 :   if( sqlite3StrICmp(zLeft, "temp_store")==0 ){
   92649             451 :     if( !zRight ){
   92650              54 :       returnSingleInt(pParse, "temp_store", db->temp_store);
   92651                 :     }else{
   92652             397 :       changeTempStorage(pParse, zRight);
   92653                 :     }
   92654                 :   }else
   92655                 : 
   92656                 :   /*
   92657                 :   **   PRAGMA temp_store_directory
   92658                 :   **   PRAGMA temp_store_directory = ""|"directory_name"
   92659                 :   **
   92660                 :   ** Return or set the local value of the temp_store_directory flag.  Changing
   92661                 :   ** the value sets a specific directory to be used for temporary files.
   92662                 :   ** Setting to a null string reverts to the default temporary directory search.
   92663                 :   ** If temporary directory is changed, then invalidateTempStorage.
   92664                 :   **
   92665                 :   */
   92666            9634 :   if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){
   92667               0 :     if( !zRight ){
   92668               0 :       if( sqlite3_temp_directory ){
   92669               0 :         sqlite3VdbeSetNumCols(v, 1);
   92670               0 :         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
   92671                 :             "temp_store_directory", SQLITE_STATIC);
   92672               0 :         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
   92673               0 :         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   92674                 :       }
   92675                 :     }else{
   92676                 : #ifndef SQLITE_OMIT_WSD
   92677               0 :       if( zRight[0] ){
   92678                 :         int rc;
   92679                 :         int res;
   92680               0 :         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
   92681               0 :         if( rc!=SQLITE_OK || res==0 ){
   92682               0 :           sqlite3ErrorMsg(pParse, "not a writable directory");
   92683               0 :           goto pragma_out;
   92684                 :         }
   92685                 :       }
   92686               0 :       if( SQLITE_TEMP_STORE==0
   92687               0 :        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
   92688                 :        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
   92689                 :       ){
   92690               0 :         invalidateTempStorage(pParse);
   92691                 :       }
   92692               0 :       sqlite3_free(sqlite3_temp_directory);
   92693               0 :       if( zRight[0] ){
   92694               0 :         sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
   92695                 :       }else{
   92696               0 :         sqlite3_temp_directory = 0;
   92697                 :       }
   92698                 : #endif /* SQLITE_OMIT_WSD */
   92699                 :     }
   92700                 :   }else
   92701                 : 
   92702                 : #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
   92703                 : #  if defined(__APPLE__)
   92704                 : #    define SQLITE_ENABLE_LOCKING_STYLE 1
   92705                 : #  else
   92706                 : #    define SQLITE_ENABLE_LOCKING_STYLE 0
   92707                 : #  endif
   92708                 : #endif
   92709                 : #if SQLITE_ENABLE_LOCKING_STYLE
   92710                 :   /*
   92711                 :    **   PRAGMA [database.]lock_proxy_file
   92712                 :    **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
   92713                 :    **
   92714                 :    ** Return or set the value of the lock_proxy_file flag.  Changing
   92715                 :    ** the value sets a specific file to be used for database access locks.
   92716                 :    **
   92717                 :    */
   92718                 :   if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
   92719                 :     if( !zRight ){
   92720                 :       Pager *pPager = sqlite3BtreePager(pDb->pBt);
   92721                 :       char *proxy_file_path = NULL;
   92722                 :       sqlite3_file *pFile = sqlite3PagerFile(pPager);
   92723                 :       sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE, 
   92724                 :                            &proxy_file_path);
   92725                 :       
   92726                 :       if( proxy_file_path ){
   92727                 :         sqlite3VdbeSetNumCols(v, 1);
   92728                 :         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, 
   92729                 :                               "lock_proxy_file", SQLITE_STATIC);
   92730                 :         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
   92731                 :         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   92732                 :       }
   92733                 :     }else{
   92734                 :       Pager *pPager = sqlite3BtreePager(pDb->pBt);
   92735                 :       sqlite3_file *pFile = sqlite3PagerFile(pPager);
   92736                 :       int res;
   92737                 :       if( zRight[0] ){
   92738                 :         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
   92739                 :                                      zRight);
   92740                 :       } else {
   92741                 :         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, 
   92742                 :                                      NULL);
   92743                 :       }
   92744                 :       if( res!=SQLITE_OK ){
   92745                 :         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
   92746                 :         goto pragma_out;
   92747                 :       }
   92748                 :     }
   92749                 :   }else
   92750                 : #endif /* SQLITE_ENABLE_LOCKING_STYLE */      
   92751                 :     
   92752                 :   /*
   92753                 :   **   PRAGMA [database.]synchronous
   92754                 :   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
   92755                 :   **
   92756                 :   ** Return or set the local value of the synchronous flag.  Changing
   92757                 :   ** the local value does not make changes to the disk file and the
   92758                 :   ** default value will be restored the next time the database is
   92759                 :   ** opened.
   92760                 :   */
   92761            9634 :   if( sqlite3StrICmp(zLeft,"synchronous")==0 ){
   92762            5000 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92763            5000 :     if( !zRight ){
   92764              18 :       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
   92765                 :     }else{
   92766            4982 :       if( !db->autoCommit ){
   92767               0 :         sqlite3ErrorMsg(pParse, 
   92768                 :             "Safety level may not be changed inside a transaction");
   92769                 :       }else{
   92770            4982 :         pDb->safety_level = getSafetyLevel(zRight)+1;
   92771                 :       }
   92772                 :     }
   92773                 :   }else
   92774                 : #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
   92775                 : 
   92776                 : #ifndef SQLITE_OMIT_FLAG_PRAGMAS
   92777            4634 :   if( flagPragma(pParse, zLeft, zRight) ){
   92778                 :     /* The flagPragma() subroutine also generates any necessary code
   92779                 :     ** there is nothing more to do here */
   92780                 :   }else
   92781                 : #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
   92782                 : 
   92783                 : #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
   92784                 :   /*
   92785                 :   **   PRAGMA table_info(<table>)
   92786                 :   **
   92787                 :   ** Return a single row for each column of the named table. The columns of
   92788                 :   ** the returned data set are:
   92789                 :   **
   92790                 :   ** cid:        Column id (numbered from left to right, starting at 0)
   92791                 :   ** name:       Column name
   92792                 :   ** type:       Column declaration type.
   92793                 :   ** notnull:    True if 'NOT NULL' is part of column declaration
   92794                 :   ** dflt_value: The default value for the column, if any.
   92795                 :   */
   92796            4017 :   if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){
   92797                 :     Table *pTab;
   92798              20 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92799              20 :     pTab = sqlite3FindTable(db, zRight, zDb);
   92800              20 :     if( pTab ){
   92801                 :       int i;
   92802              20 :       int nHidden = 0;
   92803                 :       Column *pCol;
   92804              20 :       sqlite3VdbeSetNumCols(v, 6);
   92805              20 :       pParse->nMem = 6;
   92806              20 :       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
   92807              20 :       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
   92808              20 :       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
   92809              20 :       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
   92810              20 :       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
   92811              20 :       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
   92812              20 :       sqlite3ViewGetColumnNames(pParse, pTab);
   92813             574 :       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
   92814             554 :         if( IsHiddenColumn(pCol) ){
   92815               0 :           nHidden++;
   92816               0 :           continue;
   92817                 :         }
   92818             554 :         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
   92819             554 :         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
   92820             554 :         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   92821             554 :            pCol->zType ? pCol->zType : "", 0);
   92822             554 :         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
   92823             554 :         if( pCol->zDflt ){
   92824               0 :           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
   92825                 :         }else{
   92826             554 :           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
   92827                 :         }
   92828             554 :         sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
   92829             554 :         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
   92830                 :       }
   92831                 :     }
   92832                 :   }else
   92833                 : 
   92834            3977 :   if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
   92835                 :     Index *pIdx;
   92836                 :     Table *pTab;
   92837               0 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92838               0 :     pIdx = sqlite3FindIndex(db, zRight, zDb);
   92839               0 :     if( pIdx ){
   92840                 :       int i;
   92841               0 :       pTab = pIdx->pTable;
   92842               0 :       sqlite3VdbeSetNumCols(v, 3);
   92843               0 :       pParse->nMem = 3;
   92844               0 :       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
   92845               0 :       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
   92846               0 :       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
   92847               0 :       for(i=0; i<pIdx->nColumn; i++){
   92848               0 :         int cnum = pIdx->aiColumn[i];
   92849               0 :         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   92850               0 :         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
   92851               0 :         assert( pTab->nCol>cnum );
   92852               0 :         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
   92853               0 :         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   92854                 :       }
   92855                 :     }
   92856                 :   }else
   92857                 : 
   92858            3977 :   if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){
   92859                 :     Index *pIdx;
   92860                 :     Table *pTab;
   92861               0 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92862               0 :     pTab = sqlite3FindTable(db, zRight, zDb);
   92863               0 :     if( pTab ){
   92864               0 :       v = sqlite3GetVdbe(pParse);
   92865               0 :       pIdx = pTab->pIndex;
   92866               0 :       if( pIdx ){
   92867               0 :         int i = 0; 
   92868               0 :         sqlite3VdbeSetNumCols(v, 3);
   92869               0 :         pParse->nMem = 3;
   92870               0 :         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
   92871               0 :         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
   92872               0 :         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
   92873               0 :         while(pIdx){
   92874               0 :           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   92875               0 :           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
   92876               0 :           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
   92877               0 :           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   92878               0 :           ++i;
   92879               0 :           pIdx = pIdx->pNext;
   92880                 :         }
   92881                 :       }
   92882                 :     }
   92883                 :   }else
   92884                 : 
   92885            3977 :   if( sqlite3StrICmp(zLeft, "database_list")==0 ){
   92886                 :     int i;
   92887               0 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92888               0 :     sqlite3VdbeSetNumCols(v, 3);
   92889               0 :     pParse->nMem = 3;
   92890               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
   92891               0 :     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
   92892               0 :     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
   92893               0 :     for(i=0; i<db->nDb; i++){
   92894               0 :       if( db->aDb[i].pBt==0 ) continue;
   92895               0 :       assert( db->aDb[i].zName!=0 );
   92896               0 :       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   92897               0 :       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
   92898               0 :       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   92899               0 :            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
   92900               0 :       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   92901                 :     }
   92902                 :   }else
   92903                 : 
   92904            3977 :   if( sqlite3StrICmp(zLeft, "collation_list")==0 ){
   92905               0 :     int i = 0;
   92906                 :     HashElem *p;
   92907               0 :     sqlite3VdbeSetNumCols(v, 2);
   92908               0 :     pParse->nMem = 2;
   92909               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
   92910               0 :     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
   92911               0 :     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
   92912               0 :       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
   92913               0 :       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
   92914               0 :       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
   92915               0 :       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
   92916                 :     }
   92917                 :   }else
   92918                 : #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
   92919                 : 
   92920                 : #ifndef SQLITE_OMIT_FOREIGN_KEY
   92921            3977 :   if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){
   92922                 :     FKey *pFK;
   92923                 :     Table *pTab;
   92924               0 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   92925               0 :     pTab = sqlite3FindTable(db, zRight, zDb);
   92926               0 :     if( pTab ){
   92927               0 :       v = sqlite3GetVdbe(pParse);
   92928               0 :       pFK = pTab->pFKey;
   92929               0 :       if( pFK ){
   92930               0 :         int i = 0; 
   92931               0 :         sqlite3VdbeSetNumCols(v, 8);
   92932               0 :         pParse->nMem = 8;
   92933               0 :         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
   92934               0 :         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
   92935               0 :         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
   92936               0 :         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
   92937               0 :         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
   92938               0 :         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
   92939               0 :         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
   92940               0 :         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
   92941               0 :         while(pFK){
   92942                 :           int j;
   92943               0 :           for(j=0; j<pFK->nCol; j++){
   92944               0 :             char *zCol = pFK->aCol[j].zCol;
   92945               0 :             char *zOnDelete = (char *)actionName(pFK->aAction[0]);
   92946               0 :             char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
   92947               0 :             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
   92948               0 :             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
   92949               0 :             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
   92950               0 :             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
   92951               0 :                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
   92952               0 :             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
   92953               0 :             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
   92954               0 :             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
   92955               0 :             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
   92956               0 :             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
   92957                 :           }
   92958               0 :           ++i;
   92959               0 :           pFK = pFK->pNextFrom;
   92960                 :         }
   92961                 :       }
   92962                 :     }
   92963                 :   }else
   92964                 : #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
   92965                 : 
   92966                 : #ifndef NDEBUG
   92967            3977 :   if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){
   92968               0 :     if( zRight ){
   92969               0 :       if( sqlite3GetBoolean(zRight) ){
   92970               0 :         sqlite3ParserTrace(stderr, "parser: ");
   92971                 :       }else{
   92972               0 :         sqlite3ParserTrace(0, 0);
   92973                 :       }
   92974                 :     }
   92975                 :   }else
   92976                 : #endif
   92977                 : 
   92978                 :   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
   92979                 :   ** used will be case sensitive or not depending on the RHS.
   92980                 :   */
   92981            3977 :   if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){
   92982               0 :     if( zRight ){
   92983               0 :       sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight));
   92984                 :     }
   92985                 :   }else
   92986                 : 
   92987                 : #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
   92988                 : # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
   92989                 : #endif
   92990                 : 
   92991                 : #ifndef SQLITE_OMIT_INTEGRITY_CHECK
   92992                 :   /* Pragma "quick_check" is an experimental reduced version of 
   92993                 :   ** integrity_check designed to detect most database corruption
   92994                 :   ** without most of the overhead of a full integrity-check.
   92995                 :   */
   92996            3977 :   if( sqlite3StrICmp(zLeft, "integrity_check")==0
   92997            3951 :    || sqlite3StrICmp(zLeft, "quick_check")==0 
   92998              26 :   ){
   92999                 :     int i, j, addr, mxErr;
   93000                 : 
   93001                 :     /* Code that appears at the end of the integrity check.  If no error
   93002                 :     ** messages have been generated, output OK.  Otherwise output the
   93003                 :     ** error message
   93004                 :     */
   93005                 :     static const VdbeOpList endCode[] = {
   93006                 :       { OP_AddImm,      1, 0,        0},    /* 0 */
   93007                 :       { OP_IfNeg,       1, 0,        0},    /* 1 */
   93008                 :       { OP_String8,     0, 3,        0},    /* 2 */
   93009                 :       { OP_ResultRow,   3, 1,        0},
   93010                 :     };
   93011                 : 
   93012              26 :     int isQuick = (sqlite3Tolower(zLeft[0])=='q');
   93013                 : 
   93014                 :     /* Initialize the VDBE program */
   93015              26 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   93016              26 :     pParse->nMem = 6;
   93017              26 :     sqlite3VdbeSetNumCols(v, 1);
   93018              26 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
   93019                 : 
   93020                 :     /* Set the maximum error count */
   93021              26 :     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
   93022              26 :     if( zRight ){
   93023              26 :       sqlite3GetInt32(zRight, &mxErr);
   93024              26 :       if( mxErr<=0 ){
   93025               0 :         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
   93026                 :       }
   93027                 :     }
   93028              26 :     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
   93029                 : 
   93030                 :     /* Do an integrity check on each database file */
   93031              78 :     for(i=0; i<db->nDb; i++){
   93032                 :       HashElem *x;
   93033                 :       Hash *pTbls;
   93034              52 :       int cnt = 0;
   93035                 : 
   93036                 :       if( OMIT_TEMPDB && i==1 ) continue;
   93037                 : 
   93038              52 :       sqlite3CodeVerifySchema(pParse, i);
   93039              52 :       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
   93040              52 :       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
   93041              52 :       sqlite3VdbeJumpHere(v, addr);
   93042                 : 
   93043                 :       /* Do an integrity check of the B-Tree
   93044                 :       **
   93045                 :       ** Begin by filling registers 2, 3, ... with the root pages numbers
   93046                 :       ** for all tables and indices in the database.
   93047                 :       */
   93048              52 :       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   93049              52 :       pTbls = &db->aDb[i].pSchema->tblHash;
   93050             467 :       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
   93051             415 :         Table *pTab = sqliteHashData(x);
   93052                 :         Index *pIdx;
   93053             415 :         sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
   93054             415 :         cnt++;
   93055             987 :         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   93056             572 :           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
   93057             572 :           cnt++;
   93058                 :         }
   93059                 :       }
   93060                 : 
   93061                 :       /* Make sure sufficient number of registers have been allocated */
   93062              52 :       if( pParse->nMem < cnt+4 ){
   93063              26 :         pParse->nMem = cnt+4;
   93064                 :       }
   93065                 : 
   93066                 :       /* Do the b-tree integrity checks */
   93067              52 :       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
   93068              52 :       sqlite3VdbeChangeP5(v, (u8)i);
   93069              52 :       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
   93070              52 :       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   93071              52 :          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
   93072                 :          P4_DYNAMIC);
   93073              52 :       sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
   93074              52 :       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
   93075              52 :       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
   93076              52 :       sqlite3VdbeJumpHere(v, addr);
   93077                 : 
   93078                 :       /* Make sure all the indices are constructed correctly.
   93079                 :       */
   93080             467 :       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
   93081             415 :         Table *pTab = sqliteHashData(x);
   93082                 :         Index *pIdx;
   93083                 :         int loopTop;
   93084                 : 
   93085             415 :         if( pTab->pIndex==0 ) continue;
   93086             286 :         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
   93087             286 :         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
   93088             286 :         sqlite3VdbeJumpHere(v, addr);
   93089             286 :         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
   93090             286 :         sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
   93091             286 :         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
   93092             286 :         sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
   93093             858 :         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
   93094                 :           int jmp2;
   93095                 :           int r1;
   93096                 :           static const VdbeOpList idxErr[] = {
   93097                 :             { OP_AddImm,      1, -1,  0},
   93098                 :             { OP_String8,     0,  3,  0},    /* 1 */
   93099                 :             { OP_Rowid,       1,  4,  0},
   93100                 :             { OP_String8,     0,  5,  0},    /* 3 */
   93101                 :             { OP_String8,     0,  6,  0},    /* 4 */
   93102                 :             { OP_Concat,      4,  3,  3},
   93103                 :             { OP_Concat,      5,  3,  3},
   93104                 :             { OP_Concat,      6,  3,  3},
   93105                 :             { OP_ResultRow,   3,  1,  0},
   93106                 :             { OP_IfPos,       1,  0,  0},    /* 9 */
   93107                 :             { OP_Halt,        0,  0,  0},
   93108                 :           };
   93109             572 :           r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
   93110             572 :           jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
   93111             572 :           addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
   93112             572 :           sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
   93113             572 :           sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
   93114             572 :           sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
   93115             572 :           sqlite3VdbeJumpHere(v, addr+9);
   93116             572 :           sqlite3VdbeJumpHere(v, jmp2);
   93117                 :         }
   93118             286 :         sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
   93119             286 :         sqlite3VdbeJumpHere(v, loopTop);
   93120             858 :         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
   93121                 :           static const VdbeOpList cntIdx[] = {
   93122                 :              { OP_Integer,      0,  3,  0},
   93123                 :              { OP_Rewind,       0,  0,  0},  /* 1 */
   93124                 :              { OP_AddImm,       3,  1,  0},
   93125                 :              { OP_Next,         0,  0,  0},  /* 3 */
   93126                 :              { OP_Eq,           2,  0,  3},  /* 4 */
   93127                 :              { OP_AddImm,       1, -1,  0},
   93128                 :              { OP_String8,      0,  2,  0},  /* 6 */
   93129                 :              { OP_String8,      0,  3,  0},  /* 7 */
   93130                 :              { OP_Concat,       3,  2,  2},
   93131                 :              { OP_ResultRow,    2,  1,  0},
   93132                 :           };
   93133             572 :           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
   93134             572 :           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
   93135             572 :           sqlite3VdbeJumpHere(v, addr);
   93136             572 :           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
   93137             572 :           sqlite3VdbeChangeP1(v, addr+1, j+2);
   93138             572 :           sqlite3VdbeChangeP2(v, addr+1, addr+4);
   93139             572 :           sqlite3VdbeChangeP1(v, addr+3, j+2);
   93140             572 :           sqlite3VdbeChangeP2(v, addr+3, addr+2);
   93141             572 :           sqlite3VdbeJumpHere(v, addr+4);
   93142             572 :           sqlite3VdbeChangeP4(v, addr+6, 
   93143                 :                      "wrong # of entries in index ", P4_STATIC);
   93144             572 :           sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
   93145                 :         }
   93146                 :       } 
   93147                 :     }
   93148              26 :     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
   93149              26 :     sqlite3VdbeChangeP2(v, addr, -mxErr);
   93150              26 :     sqlite3VdbeJumpHere(v, addr+1);
   93151              26 :     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
   93152                 :   }else
   93153                 : #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
   93154                 : 
   93155                 : #ifndef SQLITE_OMIT_UTF16
   93156                 :   /*
   93157                 :   **   PRAGMA encoding
   93158                 :   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
   93159                 :   **
   93160                 :   ** In its first form, this pragma returns the encoding of the main
   93161                 :   ** database. If the database is not initialized, it is initialized now.
   93162                 :   **
   93163                 :   ** The second form of this pragma is a no-op if the main database file
   93164                 :   ** has not already been initialized. In this case it sets the default
   93165                 :   ** encoding that will be used for the main database file if a new file
   93166                 :   ** is created. If an existing main database file is opened, then the
   93167                 :   ** default text encoding for the existing database is used.
   93168                 :   ** 
   93169                 :   ** In all cases new databases created using the ATTACH command are
   93170                 :   ** created to use the same default text encoding as the main database. If
   93171                 :   ** the main database has not been initialized and/or created when ATTACH
   93172                 :   ** is executed, this is done before the ATTACH operation.
   93173                 :   **
   93174                 :   ** In the second form this pragma sets the text encoding to be used in
   93175                 :   ** new database files created using this database handle. It is only
   93176                 :   ** useful if invoked immediately after the main database i
   93177                 :   */
   93178            3951 :   if( sqlite3StrICmp(zLeft, "encoding")==0 ){
   93179                 :     static const struct EncName {
   93180                 :       char *zName;
   93181                 :       u8 enc;
   93182                 :     } encnames[] = {
   93183                 :       { "UTF8",     SQLITE_UTF8        },
   93184                 :       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
   93185                 :       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
   93186                 :       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
   93187                 :       { "UTF16le",  SQLITE_UTF16LE     },
   93188                 :       { "UTF16be",  SQLITE_UTF16BE     },
   93189                 :       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
   93190                 :       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
   93191                 :       { 0, 0 }
   93192                 :     };
   93193                 :     const struct EncName *pEnc;
   93194              28 :     if( !zRight ){    /* "PRAGMA encoding" */
   93195              26 :       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   93196              26 :       sqlite3VdbeSetNumCols(v, 1);
   93197              26 :       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
   93198              26 :       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
   93199              26 :       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
   93200              26 :       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
   93201              26 :       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
   93202              26 :       sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
   93203              26 :       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   93204                 :     }else{                        /* "PRAGMA encoding = XXX" */
   93205                 :       /* Only change the value of sqlite.enc if the database handle is not
   93206                 :       ** initialized. If the main database exists, the new sqlite.enc value
   93207                 :       ** will be overwritten when the schema is next loaded. If it does not
   93208                 :       ** already exists, it will be created to use the new encoding value.
   93209                 :       */
   93210               2 :       if( 
   93211               4 :         !(DbHasProperty(db, 0, DB_SchemaLoaded)) || 
   93212               2 :         DbHasProperty(db, 0, DB_Empty) 
   93213                 :       ){
   93214              14 :         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
   93215              14 :           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
   93216               2 :             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
   93217               2 :             break;
   93218                 :           }
   93219                 :         }
   93220               2 :         if( !pEnc->zName ){
   93221               0 :           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
   93222                 :         }
   93223                 :       }
   93224                 :     }
   93225                 :   }else
   93226                 : #endif /* SQLITE_OMIT_UTF16 */
   93227                 : 
   93228                 : #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
   93229                 :   /*
   93230                 :   **   PRAGMA [database.]schema_version
   93231                 :   **   PRAGMA [database.]schema_version = <integer>
   93232                 :   **
   93233                 :   **   PRAGMA [database.]user_version
   93234                 :   **   PRAGMA [database.]user_version = <integer>
   93235                 :   **
   93236                 :   ** The pragma's schema_version and user_version are used to set or get
   93237                 :   ** the value of the schema-version and user-version, respectively. Both
   93238                 :   ** the schema-version and the user-version are 32-bit signed integers
   93239                 :   ** stored in the database header.
   93240                 :   **
   93241                 :   ** The schema-cookie is usually only manipulated internally by SQLite. It
   93242                 :   ** is incremented by SQLite whenever the database schema is modified (by
   93243                 :   ** creating or dropping a table or index). The schema version is used by
   93244                 :   ** SQLite each time a query is executed to ensure that the internal cache
   93245                 :   ** of the schema used when compiling the SQL query matches the schema of
   93246                 :   ** the database against which the compiled query is actually executed.
   93247                 :   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
   93248                 :   ** the schema-version is potentially dangerous and may lead to program
   93249                 :   ** crashes or database corruption. Use with caution!
   93250                 :   **
   93251                 :   ** The user-version is not used internally by SQLite. It may be used by
   93252                 :   ** applications for any purpose.
   93253                 :   */
   93254            3923 :   if( sqlite3StrICmp(zLeft, "schema_version")==0 
   93255            3922 :    || sqlite3StrICmp(zLeft, "user_version")==0 
   93256             804 :    || sqlite3StrICmp(zLeft, "freelist_count")==0 
   93257            3119 :   ){
   93258                 :     int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
   93259            3119 :     sqlite3VdbeUsesBtree(v, iDb);
   93260            3119 :     switch( zLeft[0] ){
   93261                 :       case 'f': case 'F':
   93262               0 :         iCookie = BTREE_FREE_PAGE_COUNT;
   93263               0 :         break;
   93264                 :       case 's': case 'S':
   93265               1 :         iCookie = BTREE_SCHEMA_VERSION;
   93266               1 :         break;
   93267                 :       default:
   93268            3118 :         iCookie = BTREE_USER_VERSION;
   93269            3118 :         break;
   93270                 :     }
   93271                 : 
   93272            4437 :     if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
   93273                 :       /* Write the specified cookie value */
   93274                 :       static const VdbeOpList setCookie[] = {
   93275                 :         { OP_Transaction,    0,  1,  0},    /* 0 */
   93276                 :         { OP_Integer,        0,  1,  0},    /* 1 */
   93277                 :         { OP_SetCookie,      0,  0,  1},    /* 2 */
   93278                 :       };
   93279            1318 :       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
   93280            1318 :       sqlite3VdbeChangeP1(v, addr, iDb);
   93281            1318 :       sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
   93282            1318 :       sqlite3VdbeChangeP1(v, addr+2, iDb);
   93283            1318 :       sqlite3VdbeChangeP2(v, addr+2, iCookie);
   93284                 :     }else{
   93285                 :       /* Read the specified cookie value */
   93286                 :       static const VdbeOpList readCookie[] = {
   93287                 :         { OP_Transaction,     0,  0,  0},    /* 0 */
   93288                 :         { OP_ReadCookie,      0,  1,  0},    /* 1 */
   93289                 :         { OP_ResultRow,       1,  1,  0}
   93290                 :       };
   93291            1801 :       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
   93292            1801 :       sqlite3VdbeChangeP1(v, addr, iDb);
   93293            1801 :       sqlite3VdbeChangeP1(v, addr+1, iDb);
   93294            1801 :       sqlite3VdbeChangeP3(v, addr+1, iCookie);
   93295            1801 :       sqlite3VdbeSetNumCols(v, 1);
   93296            1801 :       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
   93297                 :     }
   93298                 :   }else
   93299                 : #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
   93300                 : 
   93301                 : #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
   93302                 :   /*
   93303                 :   **   PRAGMA compile_options
   93304                 :   **
   93305                 :   ** Return the names of all compile-time options used in this build,
   93306                 :   ** one option per row.
   93307                 :   */
   93308             804 :   if( sqlite3StrICmp(zLeft, "compile_options")==0 ){
   93309               0 :     int i = 0;
   93310                 :     const char *zOpt;
   93311               0 :     sqlite3VdbeSetNumCols(v, 1);
   93312               0 :     pParse->nMem = 1;
   93313               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
   93314               0 :     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
   93315               0 :       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
   93316               0 :       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
   93317                 :     }
   93318                 :   }else
   93319                 : #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
   93320                 : 
   93321                 : #ifndef SQLITE_OMIT_WAL
   93322                 :   /*
   93323                 :   **   PRAGMA [database.]wal_checkpoint = passive|full|restart
   93324                 :   **
   93325                 :   ** Checkpoint the database.
   93326                 :   */
   93327             804 :   if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){
   93328             266 :     int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
   93329             266 :     int eMode = SQLITE_CHECKPOINT_PASSIVE;
   93330             266 :     if( zRight ){
   93331               0 :       if( sqlite3StrICmp(zRight, "full")==0 ){
   93332               0 :         eMode = SQLITE_CHECKPOINT_FULL;
   93333               0 :       }else if( sqlite3StrICmp(zRight, "restart")==0 ){
   93334               0 :         eMode = SQLITE_CHECKPOINT_RESTART;
   93335                 :       }
   93336                 :     }
   93337             266 :     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
   93338             266 :     sqlite3VdbeSetNumCols(v, 3);
   93339             266 :     pParse->nMem = 3;
   93340             266 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
   93341             266 :     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
   93342             266 :     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
   93343                 : 
   93344             266 :     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
   93345             266 :     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
   93346                 :   }else
   93347                 : 
   93348                 :   /*
   93349                 :   **   PRAGMA wal_autocheckpoint
   93350                 :   **   PRAGMA wal_autocheckpoint = N
   93351                 :   **
   93352                 :   ** Configure a database connection to automatically checkpoint a database
   93353                 :   ** after accumulating N frames in the log. Or query for the current value
   93354                 :   ** of N.
   93355                 :   */
   93356             538 :   if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){
   93357             538 :     if( zRight ){
   93358             524 :       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
   93359                 :     }
   93360            1076 :     returnSingleInt(pParse, "wal_autocheckpoint", 
   93361             538 :        db->xWalCallback==sqlite3WalDefaultHook ? 
   93362             538 :            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
   93363                 :   }else
   93364                 : #endif
   93365                 : 
   93366                 :   /*
   93367                 :   **  PRAGMA shrink_memory
   93368                 :   **
   93369                 :   ** This pragma attempts to free as much memory as possible from the
   93370                 :   ** current database connection.
   93371                 :   */
   93372               0 :   if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
   93373               0 :     sqlite3_db_release_memory(db);
   93374                 :   }else
   93375                 : 
   93376                 : #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
   93377                 :   /*
   93378                 :   ** Report the current state of file logs for all databases
   93379                 :   */
   93380               0 :   if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
   93381                 :     static const char *const azLockName[] = {
   93382                 :       "unlocked", "shared", "reserved", "pending", "exclusive"
   93383                 :     };
   93384                 :     int i;
   93385               0 :     sqlite3VdbeSetNumCols(v, 2);
   93386               0 :     pParse->nMem = 2;
   93387               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
   93388               0 :     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
   93389               0 :     for(i=0; i<db->nDb; i++){
   93390                 :       Btree *pBt;
   93391                 :       Pager *pPager;
   93392               0 :       const char *zState = "unknown";
   93393                 :       int j;
   93394               0 :       if( db->aDb[i].zName==0 ) continue;
   93395               0 :       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
   93396               0 :       pBt = db->aDb[i].pBt;
   93397               0 :       if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){
   93398               0 :         zState = "closed";
   93399               0 :       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, 
   93400                 :                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
   93401               0 :          zState = azLockName[j];
   93402                 :       }
   93403               0 :       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
   93404               0 :       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
   93405                 :     }
   93406                 : 
   93407                 :   }else
   93408                 : #endif
   93409                 : 
   93410                 : #ifdef SQLITE_HAS_CODEC
   93411                 :   if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){
   93412                 :     sqlite3_key(db, zRight, sqlite3Strlen30(zRight));
   93413                 :   }else
   93414                 :   if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){
   93415                 :     sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight));
   93416                 :   }else
   93417                 :   if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 ||
   93418                 :                  sqlite3StrICmp(zLeft, "hexrekey")==0) ){
   93419                 :     int i, h1, h2;
   93420                 :     char zKey[40];
   93421                 :     for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){
   93422                 :       h1 += 9*(1&(h1>>6));
   93423                 :       h2 += 9*(1&(h2>>6));
   93424                 :       zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4);
   93425                 :     }
   93426                 :     if( (zLeft[3] & 0xf)==0xb ){
   93427                 :       sqlite3_key(db, zKey, i/2);
   93428                 :     }else{
   93429                 :       sqlite3_rekey(db, zKey, i/2);
   93430                 :     }
   93431                 :   }else
   93432                 : #endif
   93433                 : #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
   93434                 :   if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){
   93435                 : #ifdef SQLITE_HAS_CODEC
   93436                 :     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
   93437                 :       sqlite3_activate_see(&zRight[4]);
   93438                 :     }
   93439                 : #endif
   93440                 : #ifdef SQLITE_ENABLE_CEROD
   93441                 :     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
   93442                 :       sqlite3_activate_cerod(&zRight[6]);
   93443                 :     }
   93444                 : #endif
   93445                 :   }else
   93446                 : #endif
   93447                 : 
   93448                 :  
   93449                 :   {/* Empty ELSE clause */}
   93450                 : 
   93451                 :   /*
   93452                 :   ** Reset the safety level, in case the fullfsync flag or synchronous
   93453                 :   ** setting changed.
   93454                 :   */
   93455                 : #ifndef SQLITE_OMIT_PAGER_PRAGMAS
   93456           22659 :   if( db->autoCommit ){
   93457           43600 :     sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
   93458           21800 :                (db->flags&SQLITE_FullFSync)!=0,
   93459           21800 :                (db->flags&SQLITE_CkptFullFSync)!=0);
   93460                 :   }
   93461                 : #endif
   93462                 : pragma_out:
   93463           22669 :   sqlite3DbFree(db, zLeft);
   93464           22669 :   sqlite3DbFree(db, zRight);
   93465                 : }
   93466                 : 
   93467                 : #endif /* SQLITE_OMIT_PRAGMA */
   93468                 : 
   93469                 : /************** End of pragma.c **********************************************/
   93470                 : /************** Begin file prepare.c *****************************************/
   93471                 : /*
   93472                 : ** 2005 May 25
   93473                 : **
   93474                 : ** The author disclaims copyright to this source code.  In place of
   93475                 : ** a legal notice, here is a blessing:
   93476                 : **
   93477                 : **    May you do good and not evil.
   93478                 : **    May you find forgiveness for yourself and forgive others.
   93479                 : **    May you share freely, never taking more than you give.
   93480                 : **
   93481                 : *************************************************************************
   93482                 : ** This file contains the implementation of the sqlite3_prepare()
   93483                 : ** interface, and routines that contribute to loading the database schema
   93484                 : ** from disk.
   93485                 : */
   93486                 : 
   93487                 : /*
   93488                 : ** Fill the InitData structure with an error message that indicates
   93489                 : ** that the database is corrupt.
   93490                 : */
   93491               0 : static void corruptSchema(
   93492                 :   InitData *pData,     /* Initialization context */
   93493                 :   const char *zObj,    /* Object being parsed at the point of error */
   93494                 :   const char *zExtra   /* Error information */
   93495                 : ){
   93496               0 :   sqlite3 *db = pData->db;
   93497               0 :   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
   93498               0 :     if( zObj==0 ) zObj = "?";
   93499               0 :     sqlite3SetString(pData->pzErrMsg, db,
   93500                 :       "malformed database schema (%s)", zObj);
   93501               0 :     if( zExtra ){
   93502               0 :       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, 
   93503               0 :                                  "%s - %s", *pData->pzErrMsg, zExtra);
   93504                 :     }
   93505                 :   }
   93506               0 :   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
   93507               0 : }
   93508                 : 
   93509                 : /*
   93510                 : ** This is the callback routine for the code that initializes the
   93511                 : ** database.  See sqlite3Init() below for additional information.
   93512                 : ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
   93513                 : **
   93514                 : ** Each callback contains the following information:
   93515                 : **
   93516                 : **     argv[0] = name of thing being created
   93517                 : **     argv[1] = root page number for table or index. 0 for trigger or view.
   93518                 : **     argv[2] = SQL text for the CREATE statement.
   93519                 : **
   93520                 : */
   93521           62783 : SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
   93522           62783 :   InitData *pData = (InitData*)pInit;
   93523           62783 :   sqlite3 *db = pData->db;
   93524           62783 :   int iDb = pData->iDb;
   93525                 : 
   93526           62783 :   assert( argc==3 );
   93527                 :   UNUSED_PARAMETER2(NotUsed, argc);
   93528           62783 :   assert( sqlite3_mutex_held(db->mutex) );
   93529           62783 :   DbClearProperty(db, iDb, DB_Empty);
   93530           62783 :   if( db->mallocFailed ){
   93531               0 :     corruptSchema(pData, argv[0], 0);
   93532               0 :     return 1;
   93533                 :   }
   93534                 : 
   93535           62783 :   assert( iDb>=0 && iDb<db->nDb );
   93536           62783 :   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
   93537           62783 :   if( argv[1]==0 ){
   93538               0 :     corruptSchema(pData, argv[0], 0);
   93539          112816 :   }else if( argv[2] && argv[2][0] ){
   93540                 :     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
   93541                 :     ** But because db->init.busy is set to 1, no VDBE code is generated
   93542                 :     ** or executed.  All the parser does is build the internal data
   93543                 :     ** structures that describe the table, index, or view.
   93544                 :     */
   93545                 :     int rc;
   93546                 :     sqlite3_stmt *pStmt;
   93547                 :     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
   93548                 : 
   93549           50033 :     assert( db->init.busy );
   93550           50033 :     db->init.iDb = iDb;
   93551           50033 :     db->init.newTnum = sqlite3Atoi(argv[1]);
   93552           50033 :     db->init.orphanTrigger = 0;
   93553           50033 :     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
   93554           50033 :     rc = db->errCode;
   93555           50033 :     assert( (rc&0xFF)==(rcp&0xFF) );
   93556           50033 :     db->init.iDb = 0;
   93557           50033 :     if( SQLITE_OK!=rc ){
   93558               0 :       if( db->init.orphanTrigger ){
   93559               0 :         assert( iDb==1 );
   93560                 :       }else{
   93561               0 :         pData->rc = rc;
   93562               0 :         if( rc==SQLITE_NOMEM ){
   93563               0 :           db->mallocFailed = 1;
   93564               0 :         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
   93565               0 :           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
   93566                 :         }
   93567                 :       }
   93568                 :     }
   93569           50033 :     sqlite3_finalize(pStmt);
   93570           12750 :   }else if( argv[0]==0 ){
   93571               0 :     corruptSchema(pData, 0, 0);
   93572                 :   }else{
   93573                 :     /* If the SQL column is blank it means this is an index that
   93574                 :     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
   93575                 :     ** constraint for a CREATE TABLE.  The index should have already
   93576                 :     ** been created when we processed the CREATE TABLE.  All we have
   93577                 :     ** to do here is record the root page number for that index.
   93578                 :     */
   93579                 :     Index *pIndex;
   93580           12750 :     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
   93581           12750 :     if( pIndex==0 ){
   93582                 :       /* This can occur if there exists an index on a TEMP table which
   93583                 :       ** has the same name as another index on a permanent index.  Since
   93584                 :       ** the permanent table is hidden by the TEMP table, we can also
   93585                 :       ** safely ignore the index on the permanent table.
   93586                 :       */
   93587                 :       /* Do Nothing */;
   93588           12750 :     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
   93589               0 :       corruptSchema(pData, argv[0], "invalid rootpage");
   93590                 :     }
   93591                 :   }
   93592           62783 :   return 0;
   93593                 : }
   93594                 : 
   93595                 : /*
   93596                 : ** Attempt to read the database schema and initialize internal
   93597                 : ** data structures for a single database file.  The index of the
   93598                 : ** database file is given by iDb.  iDb==0 is used for the main
   93599                 : ** database.  iDb==1 should never be used.  iDb>=2 is used for
   93600                 : ** auxiliary databases.  Return one of the SQLITE_ error codes to
   93601                 : ** indicate success or failure.
   93602                 : */
   93603            7048 : static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
   93604                 :   int rc;
   93605                 :   int i;
   93606                 :   int size;
   93607                 :   Table *pTab;
   93608                 :   Db *pDb;
   93609                 :   char const *azArg[4];
   93610                 :   int meta[5];
   93611                 :   InitData initData;
   93612                 :   char const *zMasterSchema;
   93613                 :   char const *zMasterName;
   93614            7048 :   int openedTransaction = 0;
   93615                 : 
   93616                 :   /*
   93617                 :   ** The master database table has a structure like this
   93618                 :   */
   93619                 :   static const char master_schema[] = 
   93620                 :      "CREATE TABLE sqlite_master(\n"
   93621                 :      "  type text,\n"
   93622                 :      "  name text,\n"
   93623                 :      "  tbl_name text,\n"
   93624                 :      "  rootpage integer,\n"
   93625                 :      "  sql text\n"
   93626                 :      ")"
   93627                 :   ;
   93628                 : #ifndef SQLITE_OMIT_TEMPDB
   93629                 :   static const char temp_master_schema[] = 
   93630                 :      "CREATE TEMP TABLE sqlite_temp_master(\n"
   93631                 :      "  type text,\n"
   93632                 :      "  name text,\n"
   93633                 :      "  tbl_name text,\n"
   93634                 :      "  rootpage integer,\n"
   93635                 :      "  sql text\n"
   93636                 :      ")"
   93637                 :   ;
   93638                 : #else
   93639                 :   #define temp_master_schema 0
   93640                 : #endif
   93641                 : 
   93642            7048 :   assert( iDb>=0 && iDb<db->nDb );
   93643            7048 :   assert( db->aDb[iDb].pSchema );
   93644            7048 :   assert( sqlite3_mutex_held(db->mutex) );
   93645            7048 :   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
   93646                 : 
   93647                 :   /* zMasterSchema and zInitScript are set to point at the master schema
   93648                 :   ** and initialisation script appropriate for the database being
   93649                 :   ** initialised. zMasterName is the name of the master table.
   93650                 :   */
   93651            7048 :   if( !OMIT_TEMPDB && iDb==1 ){
   93652            3553 :     zMasterSchema = temp_master_schema;
   93653                 :   }else{
   93654            3495 :     zMasterSchema = master_schema;
   93655                 :   }
   93656            7048 :   zMasterName = SCHEMA_TABLE(iDb);
   93657                 : 
   93658                 :   /* Construct the schema tables.  */
   93659            7048 :   azArg[0] = zMasterName;
   93660            7048 :   azArg[1] = "1";
   93661            7048 :   azArg[2] = zMasterSchema;
   93662            7048 :   azArg[3] = 0;
   93663            7048 :   initData.db = db;
   93664            7048 :   initData.iDb = iDb;
   93665            7048 :   initData.rc = SQLITE_OK;
   93666            7048 :   initData.pzErrMsg = pzErrMsg;
   93667            7048 :   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
   93668            7048 :   if( initData.rc ){
   93669               0 :     rc = initData.rc;
   93670               0 :     goto error_out;
   93671                 :   }
   93672            7048 :   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
   93673            7048 :   if( ALWAYS(pTab) ){
   93674            7048 :     pTab->tabFlags |= TF_Readonly;
   93675                 :   }
   93676                 : 
   93677                 :   /* Create a cursor to hold the database open
   93678                 :   */
   93679            7048 :   pDb = &db->aDb[iDb];
   93680            7048 :   if( pDb->pBt==0 ){
   93681            3278 :     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
   93682            3278 :       DbSetProperty(db, 1, DB_SchemaLoaded);
   93683                 :     }
   93684            3278 :     return SQLITE_OK;
   93685                 :   }
   93686                 : 
   93687                 :   /* If there is not already a read-only (or read-write) transaction opened
   93688                 :   ** on the b-tree database, open one now. If a transaction is opened, it 
   93689                 :   ** will be closed before this function returns.  */
   93690            3770 :   sqlite3BtreeEnter(pDb->pBt);
   93691            3770 :   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
   93692            3213 :     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
   93693            3213 :     if( rc!=SQLITE_OK ){
   93694              10 :       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
   93695              10 :       goto initone_error_out;
   93696                 :     }
   93697            3203 :     openedTransaction = 1;
   93698                 :   }
   93699                 : 
   93700                 :   /* Get the database meta information.
   93701                 :   **
   93702                 :   ** Meta values are as follows:
   93703                 :   **    meta[0]   Schema cookie.  Changes with each schema change.
   93704                 :   **    meta[1]   File format of schema layer.
   93705                 :   **    meta[2]   Size of the page cache.
   93706                 :   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
   93707                 :   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
   93708                 :   **    meta[5]   User version
   93709                 :   **    meta[6]   Incremental vacuum mode
   93710                 :   **    meta[7]   unused
   93711                 :   **    meta[8]   unused
   93712                 :   **    meta[9]   unused
   93713                 :   **
   93714                 :   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
   93715                 :   ** the possible values of meta[4].
   93716                 :   */
   93717           22560 :   for(i=0; i<ArraySize(meta); i++){
   93718           18800 :     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
   93719                 :   }
   93720            3760 :   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
   93721                 : 
   93722                 :   /* If opening a non-empty database, check the text encoding. For the
   93723                 :   ** main database, set sqlite3.enc to the encoding of the main database.
   93724                 :   ** For an attached db, it is an error if the encoding is not the same
   93725                 :   ** as sqlite3.enc.
   93726                 :   */
   93727            3760 :   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
   93728            2029 :     if( iDb==0 ){
   93729                 :       u8 encoding;
   93730                 :       /* If opening the main database, set ENC(db). */
   93731            2028 :       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
   93732            2028 :       if( encoding==0 ) encoding = SQLITE_UTF8;
   93733            2028 :       ENC(db) = encoding;
   93734            2028 :       db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
   93735                 :     }else{
   93736                 :       /* If opening an attached database, the encoding much match ENC(db) */
   93737               1 :       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
   93738               0 :         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
   93739                 :             " text encoding as main database");
   93740               0 :         rc = SQLITE_ERROR;
   93741               0 :         goto initone_error_out;
   93742                 :       }
   93743                 :     }
   93744                 :   }else{
   93745            1731 :     DbSetProperty(db, iDb, DB_Empty);
   93746                 :   }
   93747            3760 :   pDb->pSchema->enc = ENC(db);
   93748                 : 
   93749            3760 :   if( pDb->pSchema->cache_size==0 ){
   93750                 : #ifndef SQLITE_OMIT_DEPRECATED
   93751            3362 :     size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
   93752            3362 :     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
   93753            3362 :     pDb->pSchema->cache_size = size;
   93754                 : #else
   93755                 :     pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
   93756                 : #endif
   93757            3362 :     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
   93758                 :   }
   93759                 : 
   93760                 :   /*
   93761                 :   ** file_format==1    Version 3.0.0.
   93762                 :   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
   93763                 :   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
   93764                 :   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
   93765                 :   */
   93766            3760 :   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
   93767            3760 :   if( pDb->pSchema->file_format==0 ){
   93768            1731 :     pDb->pSchema->file_format = 1;
   93769                 :   }
   93770            3760 :   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
   93771               0 :     sqlite3SetString(pzErrMsg, db, "unsupported file format");
   93772               0 :     rc = SQLITE_ERROR;
   93773               0 :     goto initone_error_out;
   93774                 :   }
   93775                 : 
   93776                 :   /* Ticket #2804:  When we open a database in the newer file format,
   93777                 :   ** clear the legacy_file_format pragma flag so that a VACUUM will
   93778                 :   ** not downgrade the database and thus invalidate any descending
   93779                 :   ** indices that the user might have created.
   93780                 :   */
   93781            3760 :   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
   93782            1967 :     db->flags &= ~SQLITE_LegacyFileFmt;
   93783                 :   }
   93784                 : 
   93785                 :   /* Read the schema information out of the schema tables
   93786                 :   */
   93787            3760 :   assert( db->init.busy );
   93788                 :   {
   93789                 :     char *zSql;
   93790            3760 :     zSql = sqlite3MPrintf(db, 
   93791                 :         "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
   93792            3760 :         db->aDb[iDb].zName, zMasterName);
   93793                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   93794                 :     {
   93795                 :       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
   93796            3760 :       xAuth = db->xAuth;
   93797            3760 :       db->xAuth = 0;
   93798                 : #endif
   93799            3760 :       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
   93800                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   93801            3760 :       db->xAuth = xAuth;
   93802                 :     }
   93803                 : #endif
   93804            3760 :     if( rc==SQLITE_OK ) rc = initData.rc;
   93805            3760 :     sqlite3DbFree(db, zSql);
   93806                 : #ifndef SQLITE_OMIT_ANALYZE
   93807            3760 :     if( rc==SQLITE_OK ){
   93808            3760 :       sqlite3AnalysisLoad(db, iDb);
   93809                 :     }
   93810                 : #endif
   93811                 :   }
   93812            3760 :   if( db->mallocFailed ){
   93813               0 :     rc = SQLITE_NOMEM;
   93814               0 :     sqlite3ResetInternalSchema(db, -1);
   93815                 :   }
   93816            3760 :   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
   93817                 :     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
   93818                 :     ** the schema loaded, even if errors occurred. In this situation the 
   93819                 :     ** current sqlite3_prepare() operation will fail, but the following one
   93820                 :     ** will attempt to compile the supplied statement against whatever subset
   93821                 :     ** of the schema was loaded before the error occurred. The primary
   93822                 :     ** purpose of this is to allow access to the sqlite_master table
   93823                 :     ** even when its contents have been corrupted.
   93824                 :     */
   93825            3760 :     DbSetProperty(db, iDb, DB_SchemaLoaded);
   93826            3760 :     rc = SQLITE_OK;
   93827                 :   }
   93828                 : 
   93829                 :   /* Jump here for an error that occurs after successfully allocating
   93830                 :   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
   93831                 :   ** before that point, jump to error_out.
   93832                 :   */
   93833                 : initone_error_out:
   93834            3770 :   if( openedTransaction ){
   93835            3203 :     sqlite3BtreeCommit(pDb->pBt);
   93836                 :   }
   93837            3770 :   sqlite3BtreeLeave(pDb->pBt);
   93838                 : 
   93839                 : error_out:
   93840            3770 :   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
   93841               0 :     db->mallocFailed = 1;
   93842                 :   }
   93843            3770 :   return rc;
   93844                 : }
   93845                 : 
   93846                 : /*
   93847                 : ** Initialize all database files - the main database file, the file
   93848                 : ** used to store temporary tables, and any additional database files
   93849                 : ** created using ATTACH statements.  Return a success code.  If an
   93850                 : ** error occurs, write an error message into *pzErrMsg.
   93851                 : **
   93852                 : ** After a database is initialized, the DB_SchemaLoaded bit is set
   93853                 : ** bit is set in the flags field of the Db structure. If the database
   93854                 : ** file was of zero-length, then the DB_Empty flag is also set.
   93855                 : */
   93856          133226 : SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
   93857                 :   int i, rc;
   93858          133226 :   int commit_internal = !(db->flags&SQLITE_InternChanges);
   93859                 :   
   93860          133226 :   assert( sqlite3_mutex_held(db->mutex) );
   93861          133226 :   rc = SQLITE_OK;
   93862          133226 :   db->init.busy = 1;
   93863          399918 :   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
   93864          266692 :     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
   93865            3495 :     rc = sqlite3InitOne(db, i, pzErrMsg);
   93866            3495 :     if( rc ){
   93867              10 :       sqlite3ResetInternalSchema(db, i);
   93868                 :     }
   93869                 :   }
   93870                 : 
   93871                 :   /* Once all the other databases have been initialised, load the schema
   93872                 :   ** for the TEMP database. This is loaded last, as the TEMP database
   93873                 :   ** schema may contain references to objects in other databases.
   93874                 :   */
   93875                 : #ifndef SQLITE_OMIT_TEMPDB
   93876          133226 :   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
   93877          133216 :                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
   93878            3553 :     rc = sqlite3InitOne(db, 1, pzErrMsg);
   93879            3553 :     if( rc ){
   93880               0 :       sqlite3ResetInternalSchema(db, 1);
   93881                 :     }
   93882                 :   }
   93883                 : #endif
   93884                 : 
   93885          133226 :   db->init.busy = 0;
   93886          133226 :   if( rc==SQLITE_OK && commit_internal ){
   93887           83332 :     sqlite3CommitInternalChanges(db);
   93888                 :   }
   93889                 : 
   93890          133226 :   return rc; 
   93891                 : }
   93892                 : 
   93893                 : /*
   93894                 : ** This routine is a no-op if the database schema is already initialised.
   93895                 : ** Otherwise, the schema is loaded. An error code is returned.
   93896                 : */
   93897          236792 : SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
   93898          236792 :   int rc = SQLITE_OK;
   93899          236792 :   sqlite3 *db = pParse->db;
   93900          236792 :   assert( sqlite3_mutex_held(db->mutex) );
   93901          236792 :   if( !db->init.busy ){
   93902          133220 :     rc = sqlite3Init(db, &pParse->zErrMsg);
   93903                 :   }
   93904          236792 :   if( rc!=SQLITE_OK ){
   93905              10 :     pParse->rc = rc;
   93906              10 :     pParse->nErr++;
   93907                 :   }
   93908          236792 :   return rc;
   93909                 : }
   93910                 : 
   93911                 : 
   93912                 : /*
   93913                 : ** Check schema cookies in all databases.  If any cookie is out
   93914                 : ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
   93915                 : ** make no changes to pParse->rc.
   93916                 : */
   93917             113 : static void schemaIsValid(Parse *pParse){
   93918             113 :   sqlite3 *db = pParse->db;
   93919                 :   int iDb;
   93920                 :   int rc;
   93921                 :   int cookie;
   93922                 : 
   93923             113 :   assert( pParse->checkSchema );
   93924             113 :   assert( sqlite3_mutex_held(db->mutex) );
   93925             339 :   for(iDb=0; iDb<db->nDb; iDb++){
   93926             226 :     int openedTransaction = 0;         /* True if a transaction is opened */
   93927             226 :     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
   93928             226 :     if( pBt==0 ) continue;
   93929                 : 
   93930                 :     /* If there is not already a read-only (or read-write) transaction opened
   93931                 :     ** on the b-tree database, open one now. If a transaction is opened, it 
   93932                 :     ** will be closed immediately after reading the meta-value. */
   93933             114 :     if( !sqlite3BtreeIsInReadTrans(pBt) ){
   93934              37 :       rc = sqlite3BtreeBeginTrans(pBt, 0);
   93935              37 :       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
   93936               0 :         db->mallocFailed = 1;
   93937                 :       }
   93938              37 :       if( rc!=SQLITE_OK ) return;
   93939              37 :       openedTransaction = 1;
   93940                 :     }
   93941                 : 
   93942                 :     /* Read the schema cookie from the database. If it does not match the 
   93943                 :     ** value stored as part of the in-memory schema representation,
   93944                 :     ** set Parse.rc to SQLITE_SCHEMA. */
   93945             114 :     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
   93946             114 :     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   93947             114 :     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
   93948               0 :       sqlite3ResetInternalSchema(db, iDb);
   93949               0 :       pParse->rc = SQLITE_SCHEMA;
   93950                 :     }
   93951                 : 
   93952                 :     /* Close the transaction, if one was opened. */
   93953             114 :     if( openedTransaction ){
   93954              37 :       sqlite3BtreeCommit(pBt);
   93955                 :     }
   93956                 :   }
   93957                 : }
   93958                 : 
   93959                 : /*
   93960                 : ** Convert a schema pointer into the iDb index that indicates
   93961                 : ** which database file in db->aDb[] the schema refers to.
   93962                 : **
   93963                 : ** If the same database is attached more than once, the first
   93964                 : ** attached database is returned.
   93965                 : */
   93966         1086809 : SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
   93967         1086809 :   int i = -1000000;
   93968                 : 
   93969                 :   /* If pSchema is NULL, then return -1000000. This happens when code in 
   93970                 :   ** expr.c is trying to resolve a reference to a transient table (i.e. one
   93971                 :   ** created by a sub-select). In this case the return value of this 
   93972                 :   ** function should never be used.
   93973                 :   **
   93974                 :   ** We return -1000000 instead of the more usual -1 simply because using
   93975                 :   ** -1000000 as the incorrect index into db->aDb[] is much 
   93976                 :   ** more likely to cause a segfault than -1 (of course there are assert()
   93977                 :   ** statements too, but it never hurts to play the odds).
   93978                 :   */
   93979         1086809 :   assert( sqlite3_mutex_held(db->mutex) );
   93980         1086809 :   if( pSchema ){
   93981         1173760 :     for(i=0; ALWAYS(i<db->nDb); i++){
   93982         1173760 :       if( db->aDb[i].pSchema==pSchema ){
   93983         1085559 :         break;
   93984                 :       }
   93985                 :     }
   93986         1085559 :     assert( i>=0 && i<db->nDb );
   93987                 :   }
   93988         1086809 :   return i;
   93989                 : }
   93990                 : 
   93991                 : /*
   93992                 : ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
   93993                 : */
   93994          172958 : static int sqlite3Prepare(
   93995                 :   sqlite3 *db,              /* Database handle. */
   93996                 :   const char *zSql,         /* UTF-8 encoded SQL statement. */
   93997                 :   int nBytes,               /* Length of zSql in bytes. */
   93998                 :   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
   93999                 :   Vdbe *pReprepare,         /* VM being reprepared */
   94000                 :   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   94001                 :   const char **pzTail       /* OUT: End of parsed string */
   94002                 : ){
   94003                 :   Parse *pParse;            /* Parsing context */
   94004          172958 :   char *zErrMsg = 0;        /* Error message */
   94005          172958 :   int rc = SQLITE_OK;       /* Result code */
   94006                 :   int i;                    /* Loop counter */
   94007                 : 
   94008                 :   /* Allocate the parsing context */
   94009          172958 :   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
   94010          172958 :   if( pParse==0 ){
   94011               0 :     rc = SQLITE_NOMEM;
   94012               0 :     goto end_prepare;
   94013                 :   }
   94014          172958 :   pParse->pReprepare = pReprepare;
   94015          172958 :   assert( ppStmt && *ppStmt==0 );
   94016          172958 :   assert( !db->mallocFailed );
   94017          172958 :   assert( sqlite3_mutex_held(db->mutex) );
   94018                 : 
   94019                 :   /* Check to verify that it is possible to get a read lock on all
   94020                 :   ** database schemas.  The inability to get a read lock indicates that
   94021                 :   ** some other database connection is holding a write-lock, which in
   94022                 :   ** turn means that the other connection has made uncommitted changes
   94023                 :   ** to the schema.
   94024                 :   **
   94025                 :   ** Were we to proceed and prepare the statement against the uncommitted
   94026                 :   ** schema changes and if those schema changes are subsequently rolled
   94027                 :   ** back and different changes are made in their place, then when this
   94028                 :   ** prepared statement goes to run the schema cookie would fail to detect
   94029                 :   ** the schema change.  Disaster would follow.
   94030                 :   **
   94031                 :   ** This thread is currently holding mutexes on all Btrees (because
   94032                 :   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
   94033                 :   ** is not possible for another thread to start a new schema change
   94034                 :   ** while this routine is running.  Hence, we do not need to hold 
   94035                 :   ** locks on the schema, we just need to make sure nobody else is 
   94036                 :   ** holding them.
   94037                 :   **
   94038                 :   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
   94039                 :   ** but it does *not* override schema lock detection, so this all still
   94040                 :   ** works even if READ_UNCOMMITTED is set.
   94041                 :   */
   94042          519082 :   for(i=0; i<db->nDb; i++) {
   94043          346124 :     Btree *pBt = db->aDb[i].pBt;
   94044          346124 :     if( pBt ){
   94045          219776 :       assert( sqlite3BtreeHoldsMutex(pBt) );
   94046          219776 :       rc = sqlite3BtreeSchemaLocked(pBt);
   94047          219776 :       if( rc ){
   94048               0 :         const char *zDb = db->aDb[i].zName;
   94049               0 :         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
   94050                 :         testcase( db->flags & SQLITE_ReadUncommitted );
   94051               0 :         goto end_prepare;
   94052                 :       }
   94053                 :     }
   94054                 :   }
   94055                 : 
   94056          172958 :   sqlite3VtabUnlockList(db);
   94057                 : 
   94058          172958 :   pParse->db = db;
   94059          172958 :   pParse->nQueryLoop = (double)1;
   94060          172958 :   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
   94061                 :     char *zSqlCopy;
   94062               0 :     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
   94063                 :     testcase( nBytes==mxLen );
   94064                 :     testcase( nBytes==mxLen+1 );
   94065               0 :     if( nBytes>mxLen ){
   94066               0 :       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
   94067               0 :       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
   94068               0 :       goto end_prepare;
   94069                 :     }
   94070               0 :     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
   94071               0 :     if( zSqlCopy ){
   94072               0 :       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
   94073               0 :       sqlite3DbFree(db, zSqlCopy);
   94074               0 :       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
   94075                 :     }else{
   94076               0 :       pParse->zTail = &zSql[nBytes];
   94077                 :     }
   94078                 :   }else{
   94079          172958 :     sqlite3RunParser(pParse, zSql, &zErrMsg);
   94080                 :   }
   94081          172958 :   assert( 1==(int)pParse->nQueryLoop );
   94082                 : 
   94083          172958 :   if( db->mallocFailed ){
   94084               0 :     pParse->rc = SQLITE_NOMEM;
   94085                 :   }
   94086          172958 :   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
   94087          172958 :   if( pParse->checkSchema ){
   94088             113 :     schemaIsValid(pParse);
   94089                 :   }
   94090          172958 :   if( db->mallocFailed ){
   94091               0 :     pParse->rc = SQLITE_NOMEM;
   94092                 :   }
   94093          172958 :   if( pzTail ){
   94094           70380 :     *pzTail = pParse->zTail;
   94095                 :   }
   94096          172958 :   rc = pParse->rc;
   94097                 : 
   94098                 : #ifndef SQLITE_OMIT_EXPLAIN
   94099          172958 :   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
   94100                 :     static const char * const azColName[] = {
   94101                 :        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
   94102                 :        "selectid", "order", "from", "detail"
   94103                 :     };
   94104                 :     int iFirst, mx;
   94105               0 :     if( pParse->explain==2 ){
   94106               0 :       sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
   94107               0 :       iFirst = 8;
   94108               0 :       mx = 12;
   94109                 :     }else{
   94110               0 :       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
   94111               0 :       iFirst = 0;
   94112               0 :       mx = 8;
   94113                 :     }
   94114               0 :     for(i=iFirst; i<mx; i++){
   94115               0 :       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
   94116                 :                             azColName[i], SQLITE_STATIC);
   94117                 :     }
   94118                 :   }
   94119                 : #endif
   94120                 : 
   94121          172958 :   assert( db->init.busy==0 || saveSqlFlag==0 );
   94122          172958 :   if( db->init.busy==0 ){
   94123          103717 :     Vdbe *pVdbe = pParse->pVdbe;
   94124          103717 :     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
   94125                 :   }
   94126          172958 :   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
   94127               0 :     sqlite3VdbeFinalize(pParse->pVdbe);
   94128               0 :     assert(!(*ppStmt));
   94129                 :   }else{
   94130          172958 :     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
   94131                 :   }
   94132                 : 
   94133          172958 :   if( zErrMsg ){
   94134              89 :     sqlite3Error(db, rc, "%s", zErrMsg);
   94135              89 :     sqlite3DbFree(db, zErrMsg);
   94136                 :   }else{
   94137          172869 :     sqlite3Error(db, rc, 0);
   94138                 :   }
   94139                 : 
   94140                 :   /* Delete any TriggerPrg structures allocated while parsing this statement. */
   94141          349013 :   while( pParse->pTriggerPrg ){
   94142            3097 :     TriggerPrg *pT = pParse->pTriggerPrg;
   94143            3097 :     pParse->pTriggerPrg = pT->pNext;
   94144            3097 :     sqlite3DbFree(db, pT);
   94145                 :   }
   94146                 : 
   94147                 : end_prepare:
   94148                 : 
   94149          172958 :   sqlite3StackFree(db, pParse);
   94150          172958 :   rc = sqlite3ApiExit(db, rc);
   94151          172958 :   assert( (rc&db->errMask)==rc );
   94152          172958 :   return rc;
   94153                 : }
   94154          172958 : static int sqlite3LockAndPrepare(
   94155                 :   sqlite3 *db,              /* Database handle. */
   94156                 :   const char *zSql,         /* UTF-8 encoded SQL statement. */
   94157                 :   int nBytes,               /* Length of zSql in bytes. */
   94158                 :   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
   94159                 :   Vdbe *pOld,               /* VM being reprepared */
   94160                 :   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   94161                 :   const char **pzTail       /* OUT: End of parsed string */
   94162                 : ){
   94163                 :   int rc;
   94164          172958 :   assert( ppStmt!=0 );
   94165          172958 :   *ppStmt = 0;
   94166          172958 :   if( !sqlite3SafetyCheckOk(db) ){
   94167               0 :     return SQLITE_MISUSE_BKPT;
   94168                 :   }
   94169          172958 :   sqlite3_mutex_enter(db->mutex);
   94170          172958 :   sqlite3BtreeEnterAll(db);
   94171          172958 :   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
   94172          172958 :   if( rc==SQLITE_SCHEMA ){
   94173               0 :     sqlite3_finalize(*ppStmt);
   94174               0 :     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
   94175                 :   }
   94176          172958 :   sqlite3BtreeLeaveAll(db);
   94177          172958 :   sqlite3_mutex_leave(db->mutex);
   94178          172958 :   return rc;
   94179                 : }
   94180                 : 
   94181                 : /*
   94182                 : ** Rerun the compilation of a statement after a schema change.
   94183                 : **
   94184                 : ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
   94185                 : ** if the statement cannot be recompiled because another connection has
   94186                 : ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
   94187                 : ** occurs, return SQLITE_SCHEMA.
   94188                 : */
   94189            1521 : SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
   94190                 :   int rc;
   94191                 :   sqlite3_stmt *pNew;
   94192                 :   const char *zSql;
   94193                 :   sqlite3 *db;
   94194                 : 
   94195            1521 :   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
   94196            1521 :   zSql = sqlite3_sql((sqlite3_stmt *)p);
   94197            1521 :   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
   94198            1521 :   db = sqlite3VdbeDb(p);
   94199            1521 :   assert( sqlite3_mutex_held(db->mutex) );
   94200            1521 :   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
   94201            1521 :   if( rc ){
   94202               0 :     if( rc==SQLITE_NOMEM ){
   94203               0 :       db->mallocFailed = 1;
   94204                 :     }
   94205               0 :     assert( pNew==0 );
   94206               0 :     return rc;
   94207                 :   }else{
   94208            1521 :     assert( pNew!=0 );
   94209                 :   }
   94210            1521 :   sqlite3VdbeSwap((Vdbe*)pNew, p);
   94211            1521 :   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
   94212            1521 :   sqlite3VdbeResetStepResult((Vdbe*)pNew);
   94213            1521 :   sqlite3VdbeFinalize((Vdbe*)pNew);
   94214            1521 :   return SQLITE_OK;
   94215                 : }
   94216                 : 
   94217                 : 
   94218                 : /*
   94219                 : ** Two versions of the official API.  Legacy and new use.  In the legacy
   94220                 : ** version, the original SQL text is not saved in the prepared statement
   94221                 : ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
   94222                 : ** sqlite3_step().  In the new version, the original SQL text is retained
   94223                 : ** and the statement is automatically recompiled if an schema change
   94224                 : ** occurs.
   94225                 : */
   94226          120534 : SQLITE_API int sqlite3_prepare(
   94227                 :   sqlite3 *db,              /* Database handle. */
   94228                 :   const char *zSql,         /* UTF-8 encoded SQL statement. */
   94229                 :   int nBytes,               /* Length of zSql in bytes. */
   94230                 :   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   94231                 :   const char **pzTail       /* OUT: End of parsed string */
   94232                 : ){
   94233                 :   int rc;
   94234          120534 :   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
   94235          120534 :   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
   94236          120534 :   return rc;
   94237                 : }
   94238           50903 : SQLITE_API int sqlite3_prepare_v2(
   94239                 :   sqlite3 *db,              /* Database handle. */
   94240                 :   const char *zSql,         /* UTF-8 encoded SQL statement. */
   94241                 :   int nBytes,               /* Length of zSql in bytes. */
   94242                 :   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   94243                 :   const char **pzTail       /* OUT: End of parsed string */
   94244                 : ){
   94245                 :   int rc;
   94246           50903 :   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
   94247           50903 :   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
   94248           50903 :   return rc;
   94249                 : }
   94250                 : 
   94251                 : 
   94252                 : #ifndef SQLITE_OMIT_UTF16
   94253                 : /*
   94254                 : ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
   94255                 : */
   94256               0 : static int sqlite3Prepare16(
   94257                 :   sqlite3 *db,              /* Database handle. */ 
   94258                 :   const void *zSql,         /* UTF-16 encoded SQL statement. */
   94259                 :   int nBytes,               /* Length of zSql in bytes. */
   94260                 :   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
   94261                 :   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   94262                 :   const void **pzTail       /* OUT: End of parsed string */
   94263                 : ){
   94264                 :   /* This function currently works by first transforming the UTF-16
   94265                 :   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
   94266                 :   ** tricky bit is figuring out the pointer to return in *pzTail.
   94267                 :   */
   94268                 :   char *zSql8;
   94269               0 :   const char *zTail8 = 0;
   94270               0 :   int rc = SQLITE_OK;
   94271                 : 
   94272               0 :   assert( ppStmt );
   94273               0 :   *ppStmt = 0;
   94274               0 :   if( !sqlite3SafetyCheckOk(db) ){
   94275               0 :     return SQLITE_MISUSE_BKPT;
   94276                 :   }
   94277               0 :   sqlite3_mutex_enter(db->mutex);
   94278               0 :   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
   94279               0 :   if( zSql8 ){
   94280               0 :     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
   94281                 :   }
   94282                 : 
   94283               0 :   if( zTail8 && pzTail ){
   94284                 :     /* If sqlite3_prepare returns a tail pointer, we calculate the
   94285                 :     ** equivalent pointer into the UTF-16 string by counting the unicode
   94286                 :     ** characters between zSql8 and zTail8, and then returning a pointer
   94287                 :     ** the same number of characters into the UTF-16 string.
   94288                 :     */
   94289               0 :     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
   94290               0 :     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
   94291                 :   }
   94292               0 :   sqlite3DbFree(db, zSql8); 
   94293               0 :   rc = sqlite3ApiExit(db, rc);
   94294               0 :   sqlite3_mutex_leave(db->mutex);
   94295               0 :   return rc;
   94296                 : }
   94297                 : 
   94298                 : /*
   94299                 : ** Two versions of the official API.  Legacy and new use.  In the legacy
   94300                 : ** version, the original SQL text is not saved in the prepared statement
   94301                 : ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
   94302                 : ** sqlite3_step().  In the new version, the original SQL text is retained
   94303                 : ** and the statement is automatically recompiled if an schema change
   94304                 : ** occurs.
   94305                 : */
   94306               0 : SQLITE_API int sqlite3_prepare16(
   94307                 :   sqlite3 *db,              /* Database handle. */ 
   94308                 :   const void *zSql,         /* UTF-16 encoded SQL statement. */
   94309                 :   int nBytes,               /* Length of zSql in bytes. */
   94310                 :   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   94311                 :   const void **pzTail       /* OUT: End of parsed string */
   94312                 : ){
   94313                 :   int rc;
   94314               0 :   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
   94315               0 :   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
   94316               0 :   return rc;
   94317                 : }
   94318               0 : SQLITE_API int sqlite3_prepare16_v2(
   94319                 :   sqlite3 *db,              /* Database handle. */ 
   94320                 :   const void *zSql,         /* UTF-16 encoded SQL statement. */
   94321                 :   int nBytes,               /* Length of zSql in bytes. */
   94322                 :   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   94323                 :   const void **pzTail       /* OUT: End of parsed string */
   94324                 : ){
   94325                 :   int rc;
   94326               0 :   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
   94327               0 :   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
   94328               0 :   return rc;
   94329                 : }
   94330                 : 
   94331                 : #endif /* SQLITE_OMIT_UTF16 */
   94332                 : 
   94333                 : /************** End of prepare.c *********************************************/
   94334                 : /************** Begin file select.c ******************************************/
   94335                 : /*
   94336                 : ** 2001 September 15
   94337                 : **
   94338                 : ** The author disclaims copyright to this source code.  In place of
   94339                 : ** a legal notice, here is a blessing:
   94340                 : **
   94341                 : **    May you do good and not evil.
   94342                 : **    May you find forgiveness for yourself and forgive others.
   94343                 : **    May you share freely, never taking more than you give.
   94344                 : **
   94345                 : *************************************************************************
   94346                 : ** This file contains C code routines that are called by the parser
   94347                 : ** to handle SELECT statements in SQLite.
   94348                 : */
   94349                 : 
   94350                 : 
   94351                 : /*
   94352                 : ** Delete all the content of a Select structure but do not deallocate
   94353                 : ** the select structure itself.
   94354                 : */
   94355           78687 : static void clearSelect(sqlite3 *db, Select *p){
   94356           78687 :   sqlite3ExprListDelete(db, p->pEList);
   94357           78687 :   sqlite3SrcListDelete(db, p->pSrc);
   94358           78687 :   sqlite3ExprDelete(db, p->pWhere);
   94359           78687 :   sqlite3ExprListDelete(db, p->pGroupBy);
   94360           78687 :   sqlite3ExprDelete(db, p->pHaving);
   94361           78687 :   sqlite3ExprListDelete(db, p->pOrderBy);
   94362           78687 :   sqlite3SelectDelete(db, p->pPrior);
   94363           78687 :   sqlite3ExprDelete(db, p->pLimit);
   94364           78687 :   sqlite3ExprDelete(db, p->pOffset);
   94365           78687 : }
   94366                 : 
   94367                 : /*
   94368                 : ** Initialize a SelectDest structure.
   94369                 : */
   94370           14133 : SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
   94371           14133 :   pDest->eDest = (u8)eDest;
   94372           14133 :   pDest->iParm = iParm;
   94373           14133 :   pDest->affinity = 0;
   94374           14133 :   pDest->iMem = 0;
   94375           14133 :   pDest->nMem = 0;
   94376           14133 : }
   94377                 : 
   94378                 : 
   94379                 : /*
   94380                 : ** Allocate a new Select structure and return a pointer to that
   94381                 : ** structure.
   94382                 : */
   94383           66571 : SQLITE_PRIVATE Select *sqlite3SelectNew(
   94384                 :   Parse *pParse,        /* Parsing context */
   94385                 :   ExprList *pEList,     /* which columns to include in the result */
   94386                 :   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
   94387                 :   Expr *pWhere,         /* the WHERE clause */
   94388                 :   ExprList *pGroupBy,   /* the GROUP BY clause */
   94389                 :   Expr *pHaving,        /* the HAVING clause */
   94390                 :   ExprList *pOrderBy,   /* the ORDER BY clause */
   94391                 :   int isDistinct,       /* true if the DISTINCT keyword is present */
   94392                 :   Expr *pLimit,         /* LIMIT value.  NULL means not used */
   94393                 :   Expr *pOffset         /* OFFSET value.  NULL means no offset */
   94394                 : ){
   94395                 :   Select *pNew;
   94396                 :   Select standin;
   94397           66571 :   sqlite3 *db = pParse->db;
   94398           66571 :   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
   94399           66571 :   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
   94400           66571 :   if( pNew==0 ){
   94401               0 :     assert( db->mallocFailed );
   94402               0 :     pNew = &standin;
   94403               0 :     memset(pNew, 0, sizeof(*pNew));
   94404                 :   }
   94405           66571 :   if( pEList==0 ){
   94406              54 :     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
   94407                 :   }
   94408           66571 :   pNew->pEList = pEList;
   94409           66571 :   pNew->pSrc = pSrc;
   94410           66571 :   pNew->pWhere = pWhere;
   94411           66571 :   pNew->pGroupBy = pGroupBy;
   94412           66571 :   pNew->pHaving = pHaving;
   94413           66571 :   pNew->pOrderBy = pOrderBy;
   94414           66571 :   pNew->selFlags = isDistinct ? SF_Distinct : 0;
   94415           66571 :   pNew->op = TK_SELECT;
   94416           66571 :   pNew->pLimit = pLimit;
   94417           66571 :   pNew->pOffset = pOffset;
   94418           66571 :   assert( pOffset==0 || pLimit!=0 );
   94419           66571 :   pNew->addrOpenEphm[0] = -1;
   94420           66571 :   pNew->addrOpenEphm[1] = -1;
   94421           66571 :   pNew->addrOpenEphm[2] = -1;
   94422           66571 :   if( db->mallocFailed ) {
   94423               0 :     clearSelect(db, pNew);
   94424               0 :     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
   94425               0 :     pNew = 0;
   94426                 :   }else{
   94427           66571 :     assert( pNew->pSrc!=0 || pParse->nErr>0 );
   94428                 :   }
   94429           66571 :   assert( pNew!=&standin );
   94430           66571 :   return pNew;
   94431                 : }
   94432                 : 
   94433                 : /*
   94434                 : ** Delete the given Select structure and all of its substructures.
   94435                 : */
   94436          377317 : SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
   94437          377317 :   if( p ){
   94438           78687 :     clearSelect(db, p);
   94439           78687 :     sqlite3DbFree(db, p);
   94440                 :   }
   94441          377317 : }
   94442                 : 
   94443                 : /*
   94444                 : ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
   94445                 : ** type of join.  Return an integer constant that expresses that type
   94446                 : ** in terms of the following bit values:
   94447                 : **
   94448                 : **     JT_INNER
   94449                 : **     JT_CROSS
   94450                 : **     JT_OUTER
   94451                 : **     JT_NATURAL
   94452                 : **     JT_LEFT
   94453                 : **     JT_RIGHT
   94454                 : **
   94455                 : ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
   94456                 : **
   94457                 : ** If an illegal or unsupported join type is seen, then still return
   94458                 : ** a join type, but put an error in the pParse structure.
   94459                 : */
   94460            2803 : SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
   94461            2803 :   int jointype = 0;
   94462                 :   Token *apAll[3];
   94463                 :   Token *p;
   94464                 :                              /*   0123456789 123456789 123456789 123 */
   94465                 :   static const char zKeyText[] = "naturaleftouterightfullinnercross";
   94466                 :   static const struct {
   94467                 :     u8 i;        /* Beginning of keyword text in zKeyText[] */
   94468                 :     u8 nChar;    /* Length of the keyword in characters */
   94469                 :     u8 code;     /* Join type mask */
   94470                 :   } aKeyword[] = {
   94471                 :     /* natural */ { 0,  7, JT_NATURAL                },
   94472                 :     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
   94473                 :     /* outer   */ { 10, 5, JT_OUTER                  },
   94474                 :     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
   94475                 :     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
   94476                 :     /* inner   */ { 23, 5, JT_INNER                  },
   94477                 :     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
   94478                 :   };
   94479                 :   int i, j;
   94480            2803 :   apAll[0] = pA;
   94481            2803 :   apAll[1] = pB;
   94482            2803 :   apAll[2] = pC;
   94483            5726 :   for(i=0; i<3 && apAll[i]; i++){
   94484            2923 :     p = apAll[i];
   94485            6362 :     for(j=0; j<ArraySize(aKeyword); j++){
   94486            6362 :       if( p->n==aKeyword[j].nChar 
   94487            3121 :           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
   94488            2923 :         jointype |= aKeyword[j].code;
   94489            2923 :         break;
   94490                 :       }
   94491                 :     }
   94492                 :     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
   94493            2923 :     if( j>=ArraySize(aKeyword) ){
   94494               0 :       jointype |= JT_ERROR;
   94495               0 :       break;
   94496                 :     }
   94497                 :   }
   94498            2803 :   if(
   94499            5606 :      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
   94500            2803 :      (jointype & JT_ERROR)!=0
   94501               0 :   ){
   94502               0 :     const char *zSp = " ";
   94503               0 :     assert( pB!=0 );
   94504               0 :     if( pC==0 ){ zSp++; }
   94505               0 :     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
   94506                 :        "%T %T%s%T", pA, pB, zSp, pC);
   94507               0 :     jointype = JT_INNER;
   94508            2803 :   }else if( (jointype & JT_OUTER)!=0 
   94509            2704 :          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
   94510               0 :     sqlite3ErrorMsg(pParse, 
   94511                 :       "RIGHT and FULL OUTER JOINs are not currently supported");
   94512               0 :     jointype = JT_INNER;
   94513                 :   }
   94514            2803 :   return jointype;
   94515                 : }
   94516                 : 
   94517                 : /*
   94518                 : ** Return the index of a column in a table.  Return -1 if the column
   94519                 : ** is not contained in the table.
   94520                 : */
   94521               0 : static int columnIndex(Table *pTab, const char *zCol){
   94522                 :   int i;
   94523               0 :   for(i=0; i<pTab->nCol; i++){
   94524               0 :     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
   94525                 :   }
   94526               0 :   return -1;
   94527                 : }
   94528                 : 
   94529                 : /*
   94530                 : ** Search the first N tables in pSrc, from left to right, looking for a
   94531                 : ** table that has a column named zCol.  
   94532                 : **
   94533                 : ** When found, set *piTab and *piCol to the table index and column index
   94534                 : ** of the matching column and return TRUE.
   94535                 : **
   94536                 : ** If not found, return FALSE.
   94537                 : */
   94538               0 : static int tableAndColumnIndex(
   94539                 :   SrcList *pSrc,       /* Array of tables to search */
   94540                 :   int N,               /* Number of tables in pSrc->a[] to search */
   94541                 :   const char *zCol,    /* Name of the column we are looking for */
   94542                 :   int *piTab,          /* Write index of pSrc->a[] here */
   94543                 :   int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
   94544                 : ){
   94545                 :   int i;               /* For looping over tables in pSrc */
   94546                 :   int iCol;            /* Index of column matching zCol */
   94547                 : 
   94548               0 :   assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
   94549               0 :   for(i=0; i<N; i++){
   94550               0 :     iCol = columnIndex(pSrc->a[i].pTab, zCol);
   94551               0 :     if( iCol>=0 ){
   94552               0 :       if( piTab ){
   94553               0 :         *piTab = i;
   94554               0 :         *piCol = iCol;
   94555                 :       }
   94556               0 :       return 1;
   94557                 :     }
   94558                 :   }
   94559               0 :   return 0;
   94560                 : }
   94561                 : 
   94562                 : /*
   94563                 : ** This function is used to add terms implied by JOIN syntax to the
   94564                 : ** WHERE clause expression of a SELECT statement. The new term, which
   94565                 : ** is ANDed with the existing WHERE clause, is of the form:
   94566                 : **
   94567                 : **    (tab1.col1 = tab2.col2)
   94568                 : **
   94569                 : ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the 
   94570                 : ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
   94571                 : ** column iColRight of tab2.
   94572                 : */
   94573               0 : static void addWhereTerm(
   94574                 :   Parse *pParse,                  /* Parsing context */
   94575                 :   SrcList *pSrc,                  /* List of tables in FROM clause */
   94576                 :   int iLeft,                      /* Index of first table to join in pSrc */
   94577                 :   int iColLeft,                   /* Index of column in first table */
   94578                 :   int iRight,                     /* Index of second table in pSrc */
   94579                 :   int iColRight,                  /* Index of column in second table */
   94580                 :   int isOuterJoin,                /* True if this is an OUTER join */
   94581                 :   Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
   94582                 : ){
   94583               0 :   sqlite3 *db = pParse->db;
   94584                 :   Expr *pE1;
   94585                 :   Expr *pE2;
   94586                 :   Expr *pEq;
   94587                 : 
   94588               0 :   assert( iLeft<iRight );
   94589               0 :   assert( pSrc->nSrc>iRight );
   94590               0 :   assert( pSrc->a[iLeft].pTab );
   94591               0 :   assert( pSrc->a[iRight].pTab );
   94592                 : 
   94593               0 :   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
   94594               0 :   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
   94595                 : 
   94596               0 :   pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
   94597               0 :   if( pEq && isOuterJoin ){
   94598               0 :     ExprSetProperty(pEq, EP_FromJoin);
   94599               0 :     assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) );
   94600               0 :     ExprSetIrreducible(pEq);
   94601               0 :     pEq->iRightJoinTable = (i16)pE2->iTable;
   94602                 :   }
   94603               0 :   *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
   94604               0 : }
   94605                 : 
   94606                 : /*
   94607                 : ** Set the EP_FromJoin property on all terms of the given expression.
   94608                 : ** And set the Expr.iRightJoinTable to iTable for every term in the
   94609                 : ** expression.
   94610                 : **
   94611                 : ** The EP_FromJoin property is used on terms of an expression to tell
   94612                 : ** the LEFT OUTER JOIN processing logic that this term is part of the
   94613                 : ** join restriction specified in the ON or USING clause and not a part
   94614                 : ** of the more general WHERE clause.  These terms are moved over to the
   94615                 : ** WHERE clause during join processing but we need to remember that they
   94616                 : ** originated in the ON or USING clause.
   94617                 : **
   94618                 : ** The Expr.iRightJoinTable tells the WHERE clause processing that the
   94619                 : ** expression depends on table iRightJoinTable even if that table is not
   94620                 : ** explicitly mentioned in the expression.  That information is needed
   94621                 : ** for cases like this:
   94622                 : **
   94623                 : **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
   94624                 : **
   94625                 : ** The where clause needs to defer the handling of the t1.x=5
   94626                 : ** term until after the t2 loop of the join.  In that way, a
   94627                 : ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
   94628                 : ** defer the handling of t1.x=5, it will be processed immediately
   94629                 : ** after the t1 loop and rows with t1.x!=5 will never appear in
   94630                 : ** the output, which is incorrect.
   94631                 : */
   94632           23704 : static void setJoinExpr(Expr *p, int iTable){
   94633           68408 :   while( p ){
   94634           21000 :     ExprSetProperty(p, EP_FromJoin);
   94635           21000 :     assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) );
   94636           21000 :     ExprSetIrreducible(p);
   94637           21000 :     p->iRightJoinTable = (i16)iTable;
   94638           21000 :     setJoinExpr(p->pLeft, iTable);
   94639           21000 :     p = p->pRight;
   94640                 :   } 
   94641           23704 : }
   94642                 : 
   94643                 : /*
   94644                 : ** This routine processes the join information for a SELECT statement.
   94645                 : ** ON and USING clauses are converted into extra terms of the WHERE clause.
   94646                 : ** NATURAL joins also create extra WHERE clause terms.
   94647                 : **
   94648                 : ** The terms of a FROM clause are contained in the Select.pSrc structure.
   94649                 : ** The left most table is the first entry in Select.pSrc.  The right-most
   94650                 : ** table is the last entry.  The join operator is held in the entry to
   94651                 : ** the left.  Thus entry 0 contains the join operator for the join between
   94652                 : ** entries 0 and 1.  Any ON or USING clauses associated with the join are
   94653                 : ** also attached to the left entry.
   94654                 : **
   94655                 : ** This routine returns the number of errors encountered.
   94656                 : */
   94657           60084 : static int sqliteProcessJoin(Parse *pParse, Select *p){
   94658                 :   SrcList *pSrc;                  /* All tables in the FROM clause */
   94659                 :   int i, j;                       /* Loop counters */
   94660                 :   struct SrcList_item *pLeft;     /* Left table being joined */
   94661                 :   struct SrcList_item *pRight;    /* Right table being joined */
   94662                 : 
   94663           60084 :   pSrc = p->pSrc;
   94664           60084 :   pLeft = &pSrc->a[0];
   94665           60084 :   pRight = &pLeft[1];
   94666           65422 :   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
   94667            5338 :     Table *pLeftTab = pLeft->pTab;
   94668            5338 :     Table *pRightTab = pRight->pTab;
   94669                 :     int isOuter;
   94670                 : 
   94671            5338 :     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
   94672            5338 :     isOuter = (pRight->jointype & JT_OUTER)!=0;
   94673                 : 
   94674                 :     /* When the NATURAL keyword is present, add WHERE clause terms for
   94675                 :     ** every column that the two tables have in common.
   94676                 :     */
   94677            5338 :     if( pRight->jointype & JT_NATURAL ){
   94678               0 :       if( pRight->pOn || pRight->pUsing ){
   94679               0 :         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
   94680                 :            "an ON or USING clause", 0);
   94681               0 :         return 1;
   94682                 :       }
   94683               0 :       for(j=0; j<pRightTab->nCol; j++){
   94684                 :         char *zName;   /* Name of column in the right table */
   94685                 :         int iLeft;     /* Matching left table */
   94686                 :         int iLeftCol;  /* Matching column in the left table */
   94687                 : 
   94688               0 :         zName = pRightTab->aCol[j].zName;
   94689               0 :         if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
   94690               0 :           addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
   94691                 :                        isOuter, &p->pWhere);
   94692                 :         }
   94693                 :       }
   94694                 :     }
   94695                 : 
   94696                 :     /* Disallow both ON and USING clauses in the same join
   94697                 :     */
   94698            5338 :     if( pRight->pOn && pRight->pUsing ){
   94699               0 :       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
   94700                 :         "clauses in the same join");
   94701               0 :       return 1;
   94702                 :     }
   94703                 : 
   94704                 :     /* Add the ON clause to the end of the WHERE clause, connected by
   94705                 :     ** an AND operator.
   94706                 :     */
   94707            5338 :     if( pRight->pOn ){
   94708            5326 :       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
   94709            5326 :       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
   94710            5326 :       pRight->pOn = 0;
   94711                 :     }
   94712                 : 
   94713                 :     /* Create extra terms on the WHERE clause for each column named
   94714                 :     ** in the USING clause.  Example: If the two tables to be joined are 
   94715                 :     ** A and B and the USING clause names X, Y, and Z, then add this
   94716                 :     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
   94717                 :     ** Report an error if any column mentioned in the USING clause is
   94718                 :     ** not contained in both tables to be joined.
   94719                 :     */
   94720            5338 :     if( pRight->pUsing ){
   94721               0 :       IdList *pList = pRight->pUsing;
   94722               0 :       for(j=0; j<pList->nId; j++){
   94723                 :         char *zName;     /* Name of the term in the USING clause */
   94724                 :         int iLeft;       /* Table on the left with matching column name */
   94725                 :         int iLeftCol;    /* Column number of matching column on the left */
   94726                 :         int iRightCol;   /* Column number of matching column on the right */
   94727                 : 
   94728               0 :         zName = pList->a[j].zName;
   94729               0 :         iRightCol = columnIndex(pRightTab, zName);
   94730               0 :         if( iRightCol<0
   94731               0 :          || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
   94732                 :         ){
   94733               0 :           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
   94734                 :             "not present in both tables", zName);
   94735               0 :           return 1;
   94736                 :         }
   94737               0 :         addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
   94738                 :                      isOuter, &p->pWhere);
   94739                 :       }
   94740                 :     }
   94741                 :   }
   94742           60084 :   return 0;
   94743                 : }
   94744                 : 
   94745                 : /*
   94746                 : ** Insert code into "v" that will push the record on the top of the
   94747                 : ** stack into the sorter.
   94748                 : */
   94749             378 : static void pushOntoSorter(
   94750                 :   Parse *pParse,         /* Parser context */
   94751                 :   ExprList *pOrderBy,    /* The ORDER BY clause */
   94752                 :   Select *pSelect,       /* The whole SELECT statement */
   94753                 :   int regData            /* Register holding data to be sorted */
   94754                 : ){
   94755             378 :   Vdbe *v = pParse->pVdbe;
   94756             378 :   int nExpr = pOrderBy->nExpr;
   94757             378 :   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
   94758             378 :   int regRecord = sqlite3GetTempReg(pParse);
   94759                 :   int op;
   94760             378 :   sqlite3ExprCacheClear(pParse);
   94761             378 :   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
   94762             378 :   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
   94763             378 :   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
   94764             378 :   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
   94765             378 :   if( pSelect->selFlags & SF_UseSorter ){
   94766             173 :     op = OP_SorterInsert;
   94767                 :   }else{
   94768             205 :     op = OP_IdxInsert;
   94769                 :   }
   94770             378 :   sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
   94771             378 :   sqlite3ReleaseTempReg(pParse, regRecord);
   94772             378 :   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
   94773             378 :   if( pSelect->iLimit ){
   94774                 :     int addr1, addr2;
   94775                 :     int iLimit;
   94776             205 :     if( pSelect->iOffset ){
   94777               0 :       iLimit = pSelect->iOffset+1;
   94778                 :     }else{
   94779             205 :       iLimit = pSelect->iLimit;
   94780                 :     }
   94781             205 :     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
   94782             205 :     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
   94783             205 :     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
   94784             205 :     sqlite3VdbeJumpHere(v, addr1);
   94785             205 :     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
   94786             205 :     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
   94787             205 :     sqlite3VdbeJumpHere(v, addr2);
   94788                 :   }
   94789             378 : }
   94790                 : 
   94791                 : /*
   94792                 : ** Add code to implement the OFFSET
   94793                 : */
   94794           60022 : static void codeOffset(
   94795                 :   Vdbe *v,          /* Generate code into this VM */
   94796                 :   Select *p,        /* The SELECT statement being coded */
   94797                 :   int iContinue     /* Jump here to skip the current record */
   94798                 : ){
   94799           60022 :   if( p->iOffset && iContinue!=0 ){
   94800                 :     int addr;
   94801               0 :     sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1);
   94802               0 :     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset);
   94803               0 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
   94804               0 :     VdbeComment((v, "skip OFFSET records"));
   94805               0 :     sqlite3VdbeJumpHere(v, addr);
   94806                 :   }
   94807           60022 : }
   94808                 : 
   94809                 : /*
   94810                 : ** Add code that will check to make sure the N registers starting at iMem
   94811                 : ** form a distinct entry.  iTab is a sorting index that holds previously
   94812                 : ** seen combinations of the N values.  A new entry is made in iTab
   94813                 : ** if the current N values are new.
   94814                 : **
   94815                 : ** A jump to addrRepeat is made and the N+1 values are popped from the
   94816                 : ** stack if the top N elements are not distinct.
   94817                 : */
   94818             750 : static void codeDistinct(
   94819                 :   Parse *pParse,     /* Parsing and code generating context */
   94820                 :   int iTab,          /* A sorting index used to test for distinctness */
   94821                 :   int addrRepeat,    /* Jump to here if not distinct */
   94822                 :   int N,             /* Number of elements */
   94823                 :   int iMem           /* First element */
   94824                 : ){
   94825                 :   Vdbe *v;
   94826                 :   int r1;
   94827                 : 
   94828             750 :   v = pParse->pVdbe;
   94829             750 :   r1 = sqlite3GetTempReg(pParse);
   94830             750 :   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
   94831             750 :   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
   94832             750 :   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
   94833             750 :   sqlite3ReleaseTempReg(pParse, r1);
   94834             750 : }
   94835                 : 
   94836                 : #ifndef SQLITE_OMIT_SUBQUERY
   94837                 : /*
   94838                 : ** Generate an error message when a SELECT is used within a subexpression
   94839                 : ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
   94840                 : ** column.  We do this in a subroutine because the error used to occur
   94841                 : ** in multiple places.  (The error only occurs in one place now, but we
   94842                 : ** retain the subroutine to minimize code disruption.)
   94843                 : */
   94844           60171 : static int checkForMultiColumnSelectError(
   94845                 :   Parse *pParse,       /* Parse context. */
   94846                 :   SelectDest *pDest,   /* Destination of SELECT results */
   94847                 :   int nExpr            /* Number of result columns returned by SELECT */
   94848                 : ){
   94849           60171 :   int eDest = pDest->eDest;
   94850           60171 :   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
   94851               0 :     sqlite3ErrorMsg(pParse, "only a single result allowed for "
   94852                 :        "a SELECT that is part of an expression");
   94853               0 :     return 1;
   94854                 :   }else{
   94855           60171 :     return 0;
   94856                 :   }
   94857                 : }
   94858                 : #endif
   94859                 : 
   94860                 : /*
   94861                 : ** This routine generates the code for the inside of the inner loop
   94862                 : ** of a SELECT.
   94863                 : **
   94864                 : ** If srcTab and nColumn are both zero, then the pEList expressions
   94865                 : ** are evaluated in order to get the data for this row.  If nColumn>0
   94866                 : ** then data is pulled from srcTab and pEList is used only to get the
   94867                 : ** datatypes for each column.
   94868                 : */
   94869           60006 : static void selectInnerLoop(
   94870                 :   Parse *pParse,          /* The parser context */
   94871                 :   Select *p,              /* The complete select statement being coded */
   94872                 :   ExprList *pEList,       /* List of values being extracted */
   94873                 :   int srcTab,             /* Pull data from this table */
   94874                 :   int nColumn,            /* Number of columns in the source table */
   94875                 :   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
   94876                 :   int distinct,           /* If >=0, make sure results are distinct */
   94877                 :   SelectDest *pDest,      /* How to dispose of the results */
   94878                 :   int iContinue,          /* Jump here to continue with next row */
   94879                 :   int iBreak              /* Jump here to break out of the inner loop */
   94880                 : ){
   94881           60006 :   Vdbe *v = pParse->pVdbe;
   94882                 :   int i;
   94883                 :   int hasDistinct;        /* True if the DISTINCT keyword is present */
   94884                 :   int regResult;              /* Start of memory holding result set */
   94885           60006 :   int eDest = pDest->eDest;   /* How to dispose of results */
   94886           60006 :   int iParm = pDest->iParm;   /* First argument to disposal method */
   94887                 :   int nResultCol;             /* Number of result columns */
   94888                 : 
   94889           60006 :   assert( v );
   94890           60006 :   if( NEVER(v==0) ) return;
   94891           60006 :   assert( pEList!=0 );
   94892           60006 :   hasDistinct = distinct>=0;
   94893           60006 :   if( pOrderBy==0 && !hasDistinct ){
   94894           58905 :     codeOffset(v, p, iContinue);
   94895                 :   }
   94896                 : 
   94897                 :   /* Pull the requested columns.
   94898                 :   */
   94899           60006 :   if( nColumn>0 ){
   94900               4 :     nResultCol = nColumn;
   94901                 :   }else{
   94902           60002 :     nResultCol = pEList->nExpr;
   94903                 :   }
   94904           60006 :   if( pDest->iMem==0 ){
   94905           59845 :     pDest->iMem = pParse->nMem+1;
   94906           59845 :     pDest->nMem = nResultCol;
   94907           59845 :     pParse->nMem += nResultCol;
   94908                 :   }else{ 
   94909             161 :     assert( pDest->nMem==nResultCol );
   94910                 :   }
   94911           60006 :   regResult = pDest->iMem;
   94912           60006 :   if( nColumn>0 ){
   94913              10 :     for(i=0; i<nColumn; i++){
   94914               6 :       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
   94915                 :     }
   94916           60002 :   }else if( eDest!=SRT_Exists ){
   94917                 :     /* If the destination is an EXISTS(...) expression, the actual
   94918                 :     ** values returned by the SELECT are not required.
   94919                 :     */
   94920           58243 :     sqlite3ExprCacheClear(pParse);
   94921           58243 :     sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
   94922                 :   }
   94923           60006 :   nColumn = nResultCol;
   94924                 : 
   94925                 :   /* If the DISTINCT keyword was present on the SELECT statement
   94926                 :   ** and this row has been seen before, then do not make this row
   94927                 :   ** part of the result.
   94928                 :   */
   94929           60006 :   if( hasDistinct ){
   94930             723 :     assert( pEList!=0 );
   94931             723 :     assert( pEList->nExpr==nColumn );
   94932             723 :     codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
   94933             723 :     if( pOrderBy==0 ){
   94934             723 :       codeOffset(v, p, iContinue);
   94935                 :     }
   94936                 :   }
   94937                 : 
   94938           60006 :   switch( eDest ){
   94939                 :     /* In this mode, write each query result to the key of the temporary
   94940                 :     ** table iParm.
   94941                 :     */
   94942                 : #ifndef SQLITE_OMIT_COMPOUND_SELECT
   94943                 :     case SRT_Union: {
   94944                 :       int r1;
   94945               6 :       r1 = sqlite3GetTempReg(pParse);
   94946               6 :       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
   94947               6 :       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
   94948               6 :       sqlite3ReleaseTempReg(pParse, r1);
   94949               6 :       break;
   94950                 :     }
   94951                 : 
   94952                 :     /* Construct a record from the query result, but instead of
   94953                 :     ** saving that record, use it as a key to delete elements from
   94954                 :     ** the temporary table iParm.
   94955                 :     */
   94956                 :     case SRT_Except: {
   94957               2 :       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn);
   94958               2 :       break;
   94959                 :     }
   94960                 : #endif
   94961                 : 
   94962                 :     /* Store the result as data using a unique key.
   94963                 :     */
   94964                 :     case SRT_Table:
   94965                 :     case SRT_EphemTab: {
   94966             263 :       int r1 = sqlite3GetTempReg(pParse);
   94967                 :       testcase( eDest==SRT_Table );
   94968                 :       testcase( eDest==SRT_EphemTab );
   94969             263 :       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
   94970             263 :       if( pOrderBy ){
   94971              13 :         pushOntoSorter(pParse, pOrderBy, p, r1);
   94972                 :       }else{
   94973             250 :         int r2 = sqlite3GetTempReg(pParse);
   94974             250 :         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
   94975             250 :         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
   94976             250 :         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   94977             250 :         sqlite3ReleaseTempReg(pParse, r2);
   94978                 :       }
   94979             263 :       sqlite3ReleaseTempReg(pParse, r1);
   94980             263 :       break;
   94981                 :     }
   94982                 : 
   94983                 : #ifndef SQLITE_OMIT_SUBQUERY
   94984                 :     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
   94985                 :     ** then there should be a single item on the stack.  Write this
   94986                 :     ** item into the set table with bogus data.
   94987                 :     */
   94988                 :     case SRT_Set: {
   94989            2349 :       assert( nColumn==1 );
   94990            2349 :       p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity);
   94991            2349 :       if( pOrderBy ){
   94992                 :         /* At first glance you would think we could optimize out the
   94993                 :         ** ORDER BY in this case since the order of entries in the set
   94994                 :         ** does not matter.  But there might be a LIMIT clause, in which
   94995                 :         ** case the order does matter */
   94996               2 :         pushOntoSorter(pParse, pOrderBy, p, regResult);
   94997                 :       }else{
   94998            2347 :         int r1 = sqlite3GetTempReg(pParse);
   94999            2347 :         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
   95000            2347 :         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
   95001            2347 :         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
   95002            2347 :         sqlite3ReleaseTempReg(pParse, r1);
   95003                 :       }
   95004            2349 :       break;
   95005                 :     }
   95006                 : 
   95007                 :     /* If any row exist in the result set, record that fact and abort.
   95008                 :     */
   95009                 :     case SRT_Exists: {
   95010            1759 :       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
   95011                 :       /* The LIMIT clause will terminate the loop for us */
   95012            1759 :       break;
   95013                 :     }
   95014                 : 
   95015                 :     /* If this is a scalar select that is part of an expression, then
   95016                 :     ** store the results in the appropriate memory cell and break out
   95017                 :     ** of the scan loop.
   95018                 :     */
   95019                 :     case SRT_Mem: {
   95020            9412 :       assert( nColumn==1 );
   95021            9412 :       if( pOrderBy ){
   95022             170 :         pushOntoSorter(pParse, pOrderBy, p, regResult);
   95023                 :       }else{
   95024            9242 :         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
   95025                 :         /* The LIMIT clause will jump out of the loop for us */
   95026                 :       }
   95027            9412 :       break;
   95028                 :     }
   95029                 : #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
   95030                 : 
   95031                 :     /* Send the data to the callback function or to a subroutine.  In the
   95032                 :     ** case of a subroutine, the subroutine itself is responsible for
   95033                 :     ** popping the data from the stack.
   95034                 :     */
   95035                 :     case SRT_Coroutine:
   95036                 :     case SRT_Output: {
   95037                 :       testcase( eDest==SRT_Coroutine );
   95038                 :       testcase( eDest==SRT_Output );
   95039           45823 :       if( pOrderBy ){
   95040             193 :         int r1 = sqlite3GetTempReg(pParse);
   95041             193 :         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1);
   95042             193 :         pushOntoSorter(pParse, pOrderBy, p, r1);
   95043             193 :         sqlite3ReleaseTempReg(pParse, r1);
   95044           45630 :       }else if( eDest==SRT_Coroutine ){
   95045              72 :         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
   95046                 :       }else{
   95047           45558 :         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn);
   95048           45558 :         sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn);
   95049                 :       }
   95050           45823 :       break;
   95051                 :     }
   95052                 : 
   95053                 : #if !defined(SQLITE_OMIT_TRIGGER)
   95054                 :     /* Discard the results.  This is used for SELECT statements inside
   95055                 :     ** the body of a TRIGGER.  The purpose of such selects is to call
   95056                 :     ** user-defined functions that have side effects.  We do not care
   95057                 :     ** about the actual results of the select.
   95058                 :     */
   95059                 :     default: {
   95060             392 :       assert( eDest==SRT_Discard );
   95061             392 :       break;
   95062                 :     }
   95063                 : #endif
   95064                 :   }
   95065                 : 
   95066                 :   /* Jump to the end of the loop if the LIMIT is reached.  Except, if
   95067                 :   ** there is a sorter, in which case the sorter has already limited
   95068                 :   ** the output for us.
   95069                 :   */
   95070           60006 :   if( pOrderBy==0 && p->iLimit ){
   95071           13125 :     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
   95072                 :   }
   95073                 : }
   95074                 : 
   95075                 : /*
   95076                 : ** Given an expression list, generate a KeyInfo structure that records
   95077                 : ** the collating sequence for each expression in that expression list.
   95078                 : **
   95079                 : ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
   95080                 : ** KeyInfo structure is appropriate for initializing a virtual index to
   95081                 : ** implement that clause.  If the ExprList is the result set of a SELECT
   95082                 : ** then the KeyInfo structure is appropriate for initializing a virtual
   95083                 : ** index to implement a DISTINCT test.
   95084                 : **
   95085                 : ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
   95086                 : ** function is responsible for seeing that this structure is eventually
   95087                 : ** freed.  Add the KeyInfo structure to the P4 field of an opcode using
   95088                 : ** P4_KEYINFO_HANDOFF is the usual way of dealing with this.
   95089                 : */
   95090           22762 : static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){
   95091           22762 :   sqlite3 *db = pParse->db;
   95092                 :   int nExpr;
   95093                 :   KeyInfo *pInfo;
   95094                 :   struct ExprList_item *pItem;
   95095                 :   int i;
   95096                 : 
   95097           22762 :   nExpr = pList->nExpr;
   95098           22762 :   pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) );
   95099           22762 :   if( pInfo ){
   95100           22762 :     pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr];
   95101           22762 :     pInfo->nField = (u16)nExpr;
   95102           22762 :     pInfo->enc = ENC(db);
   95103           22762 :     pInfo->db = db;
   95104           46177 :     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
   95105                 :       CollSeq *pColl;
   95106           23415 :       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
   95107           23415 :       if( !pColl ){
   95108           20054 :         pColl = db->pDfltColl;
   95109                 :       }
   95110           23415 :       pInfo->aColl[i] = pColl;
   95111           23415 :       pInfo->aSortOrder[i] = pItem->sortOrder;
   95112                 :     }
   95113                 :   }
   95114           22762 :   return pInfo;
   95115                 : }
   95116                 : 
   95117                 : #ifndef SQLITE_OMIT_COMPOUND_SELECT
   95118                 : /*
   95119                 : ** Name of the connection operator, used for error messages.
   95120                 : */
   95121               0 : static const char *selectOpName(int id){
   95122                 :   char *z;
   95123               0 :   switch( id ){
   95124               0 :     case TK_ALL:       z = "UNION ALL";   break;
   95125               0 :     case TK_INTERSECT: z = "INTERSECT";   break;
   95126               0 :     case TK_EXCEPT:    z = "EXCEPT";      break;
   95127               0 :     default:           z = "UNION";       break;
   95128                 :   }
   95129               0 :   return z;
   95130                 : }
   95131                 : #endif /* SQLITE_OMIT_COMPOUND_SELECT */
   95132                 : 
   95133                 : #ifndef SQLITE_OMIT_EXPLAIN
   95134                 : /*
   95135                 : ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
   95136                 : ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
   95137                 : ** where the caption is of the form:
   95138                 : **
   95139                 : **   "USE TEMP B-TREE FOR xxx"
   95140                 : **
   95141                 : ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
   95142                 : ** is determined by the zUsage argument.
   95143                 : */
   95144            1124 : static void explainTempTable(Parse *pParse, const char *zUsage){
   95145            1124 :   if( pParse->explain==2 ){
   95146               0 :     Vdbe *v = pParse->pVdbe;
   95147               0 :     char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
   95148               0 :     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
   95149                 :   }
   95150            1124 : }
   95151                 : 
   95152                 : /*
   95153                 : ** Assign expression b to lvalue a. A second, no-op, version of this macro
   95154                 : ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
   95155                 : ** in sqlite3Select() to assign values to structure member variables that
   95156                 : ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
   95157                 : ** code with #ifndef directives.
   95158                 : */
   95159                 : # define explainSetInteger(a, b) a = b
   95160                 : 
   95161                 : #else
   95162                 : /* No-op versions of the explainXXX() functions and macros. */
   95163                 : # define explainTempTable(y,z)
   95164                 : # define explainSetInteger(y,z)
   95165                 : #endif
   95166                 : 
   95167                 : #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
   95168                 : /*
   95169                 : ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
   95170                 : ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
   95171                 : ** where the caption is of one of the two forms:
   95172                 : **
   95173                 : **   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
   95174                 : **   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
   95175                 : **
   95176                 : ** where iSub1 and iSub2 are the integers passed as the corresponding
   95177                 : ** function parameters, and op is the text representation of the parameter
   95178                 : ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
   95179                 : ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is 
   95180                 : ** false, or the second form if it is true.
   95181                 : */
   95182             169 : static void explainComposite(
   95183                 :   Parse *pParse,                  /* Parse context */
   95184                 :   int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
   95185                 :   int iSub1,                      /* Subquery id 1 */
   95186                 :   int iSub2,                      /* Subquery id 2 */
   95187                 :   int bUseTmp                     /* True if a temp table was used */
   95188                 : ){
   95189             169 :   assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
   95190             169 :   if( pParse->explain==2 ){
   95191               0 :     Vdbe *v = pParse->pVdbe;
   95192               0 :     char *zMsg = sqlite3MPrintf(
   95193                 :         pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
   95194                 :         bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
   95195                 :     );
   95196               0 :     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
   95197                 :   }
   95198             169 : }
   95199                 : #else
   95200                 : /* No-op versions of the explainXXX() functions and macros. */
   95201                 : # define explainComposite(v,w,x,y,z)
   95202                 : #endif
   95203                 : 
   95204                 : /*
   95205                 : ** If the inner loop was generated using a non-null pOrderBy argument,
   95206                 : ** then the results were placed in a sorter.  After the loop is terminated
   95207                 : ** we need to run the sorter and output the results.  The following
   95208                 : ** routine generates the code needed to do that.
   95209                 : */
   95210             378 : static void generateSortTail(
   95211                 :   Parse *pParse,    /* Parsing context */
   95212                 :   Select *p,        /* The SELECT statement */
   95213                 :   Vdbe *v,          /* Generate code into this VDBE */
   95214                 :   int nColumn,      /* Number of columns of data */
   95215                 :   SelectDest *pDest /* Write the sorted results here */
   95216                 : ){
   95217             378 :   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
   95218             378 :   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
   95219                 :   int addr;
   95220                 :   int iTab;
   95221             378 :   int pseudoTab = 0;
   95222             378 :   ExprList *pOrderBy = p->pOrderBy;
   95223                 : 
   95224             378 :   int eDest = pDest->eDest;
   95225             378 :   int iParm = pDest->iParm;
   95226                 : 
   95227                 :   int regRow;
   95228                 :   int regRowid;
   95229                 : 
   95230             378 :   iTab = pOrderBy->iECursor;
   95231             378 :   regRow = sqlite3GetTempReg(pParse);
   95232             378 :   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
   95233             193 :     pseudoTab = pParse->nTab++;
   95234             193 :     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
   95235             193 :     regRowid = 0;
   95236                 :   }else{
   95237             185 :     regRowid = sqlite3GetTempReg(pParse);
   95238                 :   }
   95239             378 :   if( p->selFlags & SF_UseSorter ){
   95240             173 :     int regSortOut = ++pParse->nMem;
   95241             173 :     int ptab2 = pParse->nTab++;
   95242             173 :     sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
   95243             173 :     addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
   95244             173 :     codeOffset(v, p, addrContinue);
   95245             173 :     sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
   95246             173 :     sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
   95247             173 :     sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
   95248                 :   }else{
   95249             205 :     addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
   95250             205 :     codeOffset(v, p, addrContinue);
   95251             205 :     sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
   95252                 :   }
   95253             378 :   switch( eDest ){
   95254                 :     case SRT_Table:
   95255                 :     case SRT_EphemTab: {
   95256                 :       testcase( eDest==SRT_Table );
   95257                 :       testcase( eDest==SRT_EphemTab );
   95258              13 :       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
   95259              13 :       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
   95260              13 :       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   95261              13 :       break;
   95262                 :     }
   95263                 : #ifndef SQLITE_OMIT_SUBQUERY
   95264                 :     case SRT_Set: {
   95265               2 :       assert( nColumn==1 );
   95266               2 :       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
   95267               2 :       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
   95268               2 :       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
   95269               2 :       break;
   95270                 :     }
   95271                 :     case SRT_Mem: {
   95272             170 :       assert( nColumn==1 );
   95273             170 :       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
   95274                 :       /* The LIMIT clause will terminate the loop for us */
   95275             170 :       break;
   95276                 :     }
   95277                 : #endif
   95278                 :     default: {
   95279                 :       int i;
   95280             193 :       assert( eDest==SRT_Output || eDest==SRT_Coroutine ); 
   95281                 :       testcase( eDest==SRT_Output );
   95282                 :       testcase( eDest==SRT_Coroutine );
   95283            1594 :       for(i=0; i<nColumn; i++){
   95284            1401 :         assert( regRow!=pDest->iMem+i );
   95285            1401 :         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i);
   95286            1401 :         if( i==0 ){
   95287             193 :           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
   95288                 :         }
   95289                 :       }
   95290             193 :       if( eDest==SRT_Output ){
   95291             159 :         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn);
   95292             159 :         sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn);
   95293                 :       }else{
   95294              34 :         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
   95295                 :       }
   95296             193 :       break;
   95297                 :     }
   95298                 :   }
   95299             378 :   sqlite3ReleaseTempReg(pParse, regRow);
   95300             378 :   sqlite3ReleaseTempReg(pParse, regRowid);
   95301                 : 
   95302                 :   /* The bottom of the loop
   95303                 :   */
   95304             378 :   sqlite3VdbeResolveLabel(v, addrContinue);
   95305             378 :   if( p->selFlags & SF_UseSorter ){
   95306             173 :     sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
   95307                 :   }else{
   95308             205 :     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
   95309                 :   }
   95310             378 :   sqlite3VdbeResolveLabel(v, addrBreak);
   95311             378 :   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
   95312             193 :     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
   95313                 :   }
   95314             378 : }
   95315                 : 
   95316                 : /*
   95317                 : ** Return a pointer to a string containing the 'declaration type' of the
   95318                 : ** expression pExpr. The string may be treated as static by the caller.
   95319                 : **
   95320                 : ** The declaration type is the exact datatype definition extracted from the
   95321                 : ** original CREATE TABLE statement if the expression is a column. The
   95322                 : ** declaration type for a ROWID field is INTEGER. Exactly when an expression
   95323                 : ** is considered a column can be complex in the presence of subqueries. The
   95324                 : ** result-set expression in all of the following SELECT statements is 
   95325                 : ** considered a column by this function.
   95326                 : **
   95327                 : **   SELECT col FROM tbl;
   95328                 : **   SELECT (SELECT col FROM tbl;
   95329                 : **   SELECT (SELECT col FROM tbl);
   95330                 : **   SELECT abc FROM (SELECT col AS abc FROM tbl);
   95331                 : ** 
   95332                 : ** The declaration type for any expression other than a column is NULL.
   95333                 : */
   95334          284390 : static const char *columnType(
   95335                 :   NameContext *pNC, 
   95336                 :   Expr *pExpr,
   95337                 :   const char **pzOriginDb,
   95338                 :   const char **pzOriginTab,
   95339                 :   const char **pzOriginCol
   95340                 : ){
   95341          284390 :   char const *zType = 0;
   95342          284390 :   char const *zOriginDb = 0;
   95343          284390 :   char const *zOriginTab = 0;
   95344          284390 :   char const *zOriginCol = 0;
   95345                 :   int j;
   95346          284390 :   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
   95347                 : 
   95348          284300 :   switch( pExpr->op ){
   95349                 :     case TK_AGG_COLUMN:
   95350                 :     case TK_COLUMN: {
   95351                 :       /* The expression is a column. Locate the table the column is being
   95352                 :       ** extracted from in NameContext.pSrcList. This table may be real
   95353                 :       ** database table or a subquery.
   95354                 :       */
   95355          275108 :       Table *pTab = 0;            /* Table structure column is extracted from */
   95356          275108 :       Select *pS = 0;             /* Select the column is extracted from */
   95357          275108 :       int iCol = pExpr->iColumn;  /* Index of column in pTab */
   95358                 :       testcase( pExpr->op==TK_AGG_COLUMN );
   95359                 :       testcase( pExpr->op==TK_COLUMN );
   95360          825324 :       while( pNC && !pTab ){
   95361          275108 :         SrcList *pTabList = pNC->pSrcList;
   95362          275108 :         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
   95363          275108 :         if( j<pTabList->nSrc ){
   95364          275108 :           pTab = pTabList->a[j].pTab;
   95365          275108 :           pS = pTabList->a[j].pSelect;
   95366                 :         }else{
   95367               0 :           pNC = pNC->pNext;
   95368                 :         }
   95369                 :       }
   95370                 : 
   95371          275108 :       if( pTab==0 ){
   95372                 :         /* At one time, code such as "SELECT new.x" within a trigger would
   95373                 :         ** cause this condition to run.  Since then, we have restructured how
   95374                 :         ** trigger code is generated and so this condition is no longer 
   95375                 :         ** possible. However, it can still be true for statements like
   95376                 :         ** the following:
   95377                 :         **
   95378                 :         **   CREATE TABLE t1(col INTEGER);
   95379                 :         **   SELECT (SELECT t1.col) FROM FROM t1;
   95380                 :         **
   95381                 :         ** when columnType() is called on the expression "t1.col" in the 
   95382                 :         ** sub-select. In this case, set the column type to NULL, even
   95383                 :         ** though it should really be "INTEGER".
   95384                 :         **
   95385                 :         ** This is not a problem, as the column type of "t1.col" is never
   95386                 :         ** used. When columnType() is called on the expression 
   95387                 :         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
   95388                 :         ** branch below.  */
   95389               0 :         break;
   95390                 :       }
   95391                 : 
   95392          275108 :       assert( pTab && pExpr->pTab==pTab );
   95393          275108 :       if( pS ){
   95394                 :         /* The "table" is actually a sub-select or a view in the FROM clause
   95395                 :         ** of the SELECT statement. Return the declaration type and origin
   95396                 :         ** data for the result-set column of the sub-select.
   95397                 :         */
   95398              32 :         if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
   95399                 :           /* If iCol is less than zero, then the expression requests the
   95400                 :           ** rowid of the sub-select or view. This expression is legal (see 
   95401                 :           ** test case misc2.2.2) - it always evaluates to NULL.
   95402                 :           */
   95403                 :           NameContext sNC;
   95404              32 :           Expr *p = pS->pEList->a[iCol].pExpr;
   95405              32 :           sNC.pSrcList = pS->pSrc;
   95406              32 :           sNC.pNext = pNC;
   95407              32 :           sNC.pParse = pNC->pParse;
   95408              32 :           zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
   95409                 :         }
   95410          275076 :       }else if( ALWAYS(pTab->pSchema) ){
   95411                 :         /* A real table */
   95412          275076 :         assert( !pS );
   95413          275076 :         if( iCol<0 ) iCol = pTab->iPKey;
   95414          275076 :         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
   95415          275076 :         if( iCol<0 ){
   95416               2 :           zType = "INTEGER";
   95417               2 :           zOriginCol = "rowid";
   95418                 :         }else{
   95419          275074 :           zType = pTab->aCol[iCol].zType;
   95420          275074 :           zOriginCol = pTab->aCol[iCol].zName;
   95421                 :         }
   95422          275076 :         zOriginTab = pTab->zName;
   95423          275076 :         if( pNC->pParse ){
   95424          274317 :           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
   95425          274317 :           zOriginDb = pNC->pParse->db->aDb[iDb].zName;
   95426                 :         }
   95427                 :       }
   95428          275108 :       break;
   95429                 :     }
   95430                 : #ifndef SQLITE_OMIT_SUBQUERY
   95431                 :     case TK_SELECT: {
   95432                 :       /* The expression is a sub-select. Return the declaration type and
   95433                 :       ** origin info for the single column in the result set of the SELECT
   95434                 :       ** statement.
   95435                 :       */
   95436                 :       NameContext sNC;
   95437            1785 :       Select *pS = pExpr->x.pSelect;
   95438            1785 :       Expr *p = pS->pEList->a[0].pExpr;
   95439            1785 :       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
   95440            1785 :       sNC.pSrcList = pS->pSrc;
   95441            1785 :       sNC.pNext = pNC;
   95442            1785 :       sNC.pParse = pNC->pParse;
   95443            1785 :       zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 
   95444            1785 :       break;
   95445                 :     }
   95446                 : #endif
   95447                 :   }
   95448                 :   
   95449          284300 :   if( pzOriginDb ){
   95450            1817 :     assert( pzOriginTab && pzOriginCol );
   95451            1817 :     *pzOriginDb = zOriginDb;
   95452            1817 :     *pzOriginTab = zOriginTab;
   95453            1817 :     *pzOriginCol = zOriginCol;
   95454                 :   }
   95455          284300 :   return zType;
   95456                 : }
   95457                 : 
   95458                 : /*
   95459                 : ** Generate code that will tell the VDBE the declaration types of columns
   95460                 : ** in the result set.
   95461                 : */
   95462           45720 : static void generateColumnTypes(
   95463                 :   Parse *pParse,      /* Parser context */
   95464                 :   SrcList *pTabList,  /* List of tables */
   95465                 :   ExprList *pEList    /* Expressions defining the result set */
   95466                 : ){
   95467                 : #ifndef SQLITE_OMIT_DECLTYPE
   95468           45720 :   Vdbe *v = pParse->pVdbe;
   95469                 :   int i;
   95470                 :   NameContext sNC;
   95471           45720 :   sNC.pSrcList = pTabList;
   95472           45720 :   sNC.pParse = pParse;
   95473          327453 :   for(i=0; i<pEList->nExpr; i++){
   95474          281733 :     Expr *p = pEList->a[i].pExpr;
   95475                 :     const char *zType;
   95476                 : #ifdef SQLITE_ENABLE_COLUMN_METADATA
   95477                 :     const char *zOrigDb = 0;
   95478                 :     const char *zOrigTab = 0;
   95479                 :     const char *zOrigCol = 0;
   95480                 :     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
   95481                 : 
   95482                 :     /* The vdbe must make its own copy of the column-type and other 
   95483                 :     ** column specific strings, in case the schema is reset before this
   95484                 :     ** virtual machine is deleted.
   95485                 :     */
   95486                 :     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
   95487                 :     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
   95488                 :     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
   95489                 : #else
   95490          281733 :     zType = columnType(&sNC, p, 0, 0, 0);
   95491                 : #endif
   95492          281733 :     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
   95493                 :   }
   95494                 : #endif /* SQLITE_OMIT_DECLTYPE */
   95495           45720 : }
   95496                 : 
   95497                 : /*
   95498                 : ** Generate code that will tell the VDBE the names of columns
   95499                 : ** in the result set.  This information is used to provide the
   95500                 : ** azCol[] values in the callback.
   95501                 : */
   95502           45725 : static void generateColumnNames(
   95503                 :   Parse *pParse,      /* Parser context */
   95504                 :   SrcList *pTabList,  /* List of tables */
   95505                 :   ExprList *pEList    /* Expressions defining the result set */
   95506                 : ){
   95507           45725 :   Vdbe *v = pParse->pVdbe;
   95508                 :   int i, j;
   95509           45725 :   sqlite3 *db = pParse->db;
   95510                 :   int fullNames, shortNames;
   95511                 : 
   95512                 : #ifndef SQLITE_OMIT_EXPLAIN
   95513                 :   /* If this is an EXPLAIN, skip this step */
   95514           45725 :   if( pParse->explain ){
   95515               0 :     return;
   95516                 :   }
   95517                 : #endif
   95518                 : 
   95519           45725 :   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
   95520           45720 :   pParse->colNamesSet = 1;
   95521           45720 :   fullNames = (db->flags & SQLITE_FullColNames)!=0;
   95522           45720 :   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
   95523           45720 :   sqlite3VdbeSetNumCols(v, pEList->nExpr);
   95524          327453 :   for(i=0; i<pEList->nExpr; i++){
   95525                 :     Expr *p;
   95526          281733 :     p = pEList->a[i].pExpr;
   95527          281733 :     if( NEVER(p==0) ) continue;
   95528          281733 :     if( pEList->a[i].zName ){
   95529           17323 :       char *zName = pEList->a[i].zName;
   95530           17323 :       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
   95531          521921 :     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
   95532                 :       Table *pTab;
   95533                 :       char *zCol;
   95534          257511 :       int iCol = p->iColumn;
   95535          268822 :       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
   95536          268822 :         if( pTabList->a[j].iCursor==p->iTable ) break;
   95537                 :       }
   95538          257511 :       assert( j<pTabList->nSrc );
   95539          257511 :       pTab = pTabList->a[j].pTab;
   95540          257511 :       if( iCol<0 ) iCol = pTab->iPKey;
   95541          257511 :       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
   95542          257511 :       if( iCol<0 ){
   95543               2 :         zCol = "rowid";
   95544                 :       }else{
   95545          257509 :         zCol = pTab->aCol[iCol].zName;
   95546                 :       }
   95547          257511 :       if( !shortNames && !fullNames ){
   95548               0 :         sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
   95549               0 :             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
   95550          257511 :       }else if( fullNames ){
   95551               0 :         char *zName = 0;
   95552               0 :         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
   95553               0 :         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
   95554                 :       }else{
   95555          257511 :         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
   95556                 :       }
   95557                 :     }else{
   95558           13798 :       sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
   95559            6899 :           sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
   95560                 :     }
   95561                 :   }
   95562           45720 :   generateColumnTypes(pParse, pTabList, pEList);
   95563                 : }
   95564                 : 
   95565                 : /*
   95566                 : ** Given a an expression list (which is really the list of expressions
   95567                 : ** that form the result set of a SELECT statement) compute appropriate
   95568                 : ** column names for a table that would hold the expression list.
   95569                 : **
   95570                 : ** All column names will be unique.
   95571                 : **
   95572                 : ** Only the column names are computed.  Column.zType, Column.zColl,
   95573                 : ** and other fields of Column are zeroed.
   95574                 : **
   95575                 : ** Return SQLITE_OK on success.  If a memory allocation error occurs,
   95576                 : ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
   95577                 : */
   95578             210 : static int selectColumnsFromExprList(
   95579                 :   Parse *pParse,          /* Parsing context */
   95580                 :   ExprList *pEList,       /* Expr list from which to derive column names */
   95581                 :   int *pnCol,             /* Write the number of columns here */
   95582                 :   Column **paCol          /* Write the new column list here */
   95583                 : ){
   95584             210 :   sqlite3 *db = pParse->db;   /* Database connection */
   95585                 :   int i, j;                   /* Loop counters */
   95586                 :   int cnt;                    /* Index added to make the name unique */
   95587                 :   Column *aCol, *pCol;        /* For looping over result columns */
   95588                 :   int nCol;                   /* Number of columns in the result set */
   95589                 :   Expr *p;                    /* Expression for a single result column */
   95590                 :   char *zName;                /* Column name */
   95591                 :   int nName;                  /* Size of name in zName[] */
   95592                 : 
   95593             210 :   *pnCol = nCol = pEList->nExpr;
   95594             210 :   aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
   95595             210 :   if( aCol==0 ) return SQLITE_NOMEM;
   95596            1050 :   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
   95597                 :     /* Get an appropriate name for the column
   95598                 :     */
   95599             840 :     p = pEList->a[i].pExpr;
   95600             840 :     assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue)
   95601                 :                || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 );
   95602             840 :     if( (zName = pEList->a[i].zName)!=0 ){
   95603                 :       /* If the column contains an "AS <name>" phrase, use <name> as the name */
   95604              87 :       zName = sqlite3DbStrDup(db, zName);
   95605                 :     }else{
   95606             753 :       Expr *pColExpr = p;  /* The expression that is the result column name */
   95607                 :       Table *pTab;         /* Table associated with this expression */
   95608            1512 :       while( pColExpr->op==TK_DOT ){
   95609               6 :         pColExpr = pColExpr->pRight;
   95610               6 :         assert( pColExpr!=0 );
   95611                 :       }
   95612            1193 :       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
   95613                 :         /* For columns use the column name name */
   95614             440 :         int iCol = pColExpr->iColumn;
   95615             440 :         pTab = pColExpr->pTab;
   95616             440 :         if( iCol<0 ) iCol = pTab->iPKey;
   95617             880 :         zName = sqlite3MPrintf(db, "%s",
   95618             440 :                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
   95619             313 :       }else if( pColExpr->op==TK_ID ){
   95620             313 :         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
   95621             313 :         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
   95622                 :       }else{
   95623                 :         /* Use the original text of the column expression as its name */
   95624               0 :         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
   95625                 :       }
   95626                 :     }
   95627             840 :     if( db->mallocFailed ){
   95628               0 :       sqlite3DbFree(db, zName);
   95629               0 :       break;
   95630                 :     }
   95631                 : 
   95632                 :     /* Make sure the column name is unique.  If the name is not unique,
   95633                 :     ** append a integer to the name so that it becomes unique.
   95634                 :     */
   95635             840 :     nName = sqlite3Strlen30(zName);
   95636            2338 :     for(j=cnt=0; j<i; j++){
   95637            1498 :       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
   95638                 :         char *zNewName;
   95639               0 :         zName[nName] = 0;
   95640               0 :         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
   95641               0 :         sqlite3DbFree(db, zName);
   95642               0 :         zName = zNewName;
   95643               0 :         j = -1;
   95644               0 :         if( zName==0 ) break;
   95645                 :       }
   95646                 :     }
   95647             840 :     pCol->zName = zName;
   95648                 :   }
   95649             210 :   if( db->mallocFailed ){
   95650               0 :     for(j=0; j<i; j++){
   95651               0 :       sqlite3DbFree(db, aCol[j].zName);
   95652                 :     }
   95653               0 :     sqlite3DbFree(db, aCol);
   95654               0 :     *paCol = 0;
   95655               0 :     *pnCol = 0;
   95656               0 :     return SQLITE_NOMEM;
   95657                 :   }
   95658             210 :   return SQLITE_OK;
   95659                 : }
   95660                 : 
   95661                 : /*
   95662                 : ** Add type and collation information to a column list based on
   95663                 : ** a SELECT statement.
   95664                 : ** 
   95665                 : ** The column list presumably came from selectColumnNamesFromExprList().
   95666                 : ** The column list has only names, not types or collations.  This
   95667                 : ** routine goes through and adds the types and collations.
   95668                 : **
   95669                 : ** This routine requires that all identifiers in the SELECT
   95670                 : ** statement be resolved.
   95671                 : */
   95672             210 : static void selectAddColumnTypeAndCollation(
   95673                 :   Parse *pParse,        /* Parsing contexts */
   95674                 :   int nCol,             /* Number of columns */
   95675                 :   Column *aCol,         /* List of columns */
   95676                 :   Select *pSelect       /* SELECT used to determine types and collations */
   95677                 : ){
   95678             210 :   sqlite3 *db = pParse->db;
   95679                 :   NameContext sNC;
   95680                 :   Column *pCol;
   95681                 :   CollSeq *pColl;
   95682                 :   int i;
   95683                 :   Expr *p;
   95684                 :   struct ExprList_item *a;
   95685                 : 
   95686             210 :   assert( pSelect!=0 );
   95687             210 :   assert( (pSelect->selFlags & SF_Resolved)!=0 );
   95688             210 :   assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
   95689             210 :   if( db->mallocFailed ) return;
   95690             210 :   memset(&sNC, 0, sizeof(sNC));
   95691             210 :   sNC.pSrcList = pSelect->pSrc;
   95692             210 :   a = pSelect->pEList->a;
   95693            1050 :   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
   95694             840 :     p = a[i].pExpr;
   95695             840 :     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
   95696             840 :     pCol->affinity = sqlite3ExprAffinity(p);
   95697             840 :     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
   95698             840 :     pColl = sqlite3ExprCollSeq(pParse, p);
   95699             840 :     if( pColl ){
   95700             758 :       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
   95701                 :     }
   95702                 :   }
   95703                 : }
   95704                 : 
   95705                 : /*
   95706                 : ** Given a SELECT statement, generate a Table structure that describes
   95707                 : ** the result set of that SELECT.
   95708                 : */
   95709              88 : SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
   95710                 :   Table *pTab;
   95711              88 :   sqlite3 *db = pParse->db;
   95712                 :   int savedFlags;
   95713                 : 
   95714              88 :   savedFlags = db->flags;
   95715              88 :   db->flags &= ~SQLITE_FullColNames;
   95716              88 :   db->flags |= SQLITE_ShortColNames;
   95717              88 :   sqlite3SelectPrep(pParse, pSelect, 0);
   95718              88 :   if( pParse->nErr ) return 0;
   95719              88 :   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
   95720              88 :   db->flags = savedFlags;
   95721              88 :   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
   95722              88 :   if( pTab==0 ){
   95723               0 :     return 0;
   95724                 :   }
   95725                 :   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
   95726                 :   ** is disabled */
   95727              88 :   assert( db->lookaside.bEnabled==0 );
   95728              88 :   pTab->nRef = 1;
   95729              88 :   pTab->zName = 0;
   95730              88 :   pTab->nRowEst = 1000000;
   95731              88 :   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
   95732              88 :   selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
   95733              88 :   pTab->iPKey = -1;
   95734              88 :   if( db->mallocFailed ){
   95735               0 :     sqlite3DeleteTable(db, pTab);
   95736               0 :     return 0;
   95737                 :   }
   95738              88 :   return pTab;
   95739                 : }
   95740                 : 
   95741                 : /*
   95742                 : ** Get a VDBE for the given parser context.  Create a new one if necessary.
   95743                 : ** If an error occurs, return NULL and leave a message in pParse.
   95744                 : */
   95745          775555 : SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
   95746          775555 :   Vdbe *v = pParse->pVdbe;
   95747          775555 :   if( v==0 ){
   95748          153381 :     v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
   95749                 : #ifndef SQLITE_OMIT_TRACE
   95750          153381 :     if( v ){
   95751          153381 :       sqlite3VdbeAddOp0(v, OP_Trace);
   95752                 :     }
   95753                 : #endif
   95754                 :   }
   95755          775555 :   return v;
   95756                 : }
   95757                 : 
   95758                 : 
   95759                 : /*
   95760                 : ** Compute the iLimit and iOffset fields of the SELECT based on the
   95761                 : ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
   95762                 : ** that appear in the original SQL statement after the LIMIT and OFFSET
   95763                 : ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
   95764                 : ** are the integer memory register numbers for counters used to compute 
   95765                 : ** the limit and offset.  If there is no limit and/or offset, then 
   95766                 : ** iLimit and iOffset are negative.
   95767                 : **
   95768                 : ** This routine changes the values of iLimit and iOffset only if
   95769                 : ** a limit or offset is defined by pLimit and pOffset.  iLimit and
   95770                 : ** iOffset should have been preset to appropriate default values
   95771                 : ** (usually but not always -1) prior to calling this routine.
   95772                 : ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
   95773                 : ** redefined.  The UNION ALL operator uses this property to force
   95774                 : ** the reuse of the same limit and offset registers across multiple
   95775                 : ** SELECT statements.
   95776                 : */
   95777           60014 : static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
   95778           60014 :   Vdbe *v = 0;
   95779           60014 :   int iLimit = 0;
   95780                 :   int iOffset;
   95781                 :   int addr1, n;
   95782           60014 :   if( p->iLimit ) return;
   95783                 : 
   95784                 :   /* 
   95785                 :   ** "LIMIT -1" always shows all rows.  There is some
   95786                 :   ** contraversy about what the correct behavior should be.
   95787                 :   ** The current implementation interprets "LIMIT 0" to mean
   95788                 :   ** no rows.
   95789                 :   */
   95790           60005 :   sqlite3ExprCacheClear(pParse);
   95791           60005 :   assert( p->pOffset==0 || p->pLimit!=0 );
   95792           60005 :   if( p->pLimit ){
   95793           13321 :     p->iLimit = iLimit = ++pParse->nMem;
   95794           13321 :     v = sqlite3GetVdbe(pParse);
   95795           13321 :     if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
   95796           13321 :     if( sqlite3ExprIsInteger(p->pLimit, &n) ){
   95797           11843 :       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
   95798           11843 :       VdbeComment((v, "LIMIT counter"));
   95799           11843 :       if( n==0 ){
   95800               0 :         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
   95801                 :       }else{
   95802           11843 :         if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
   95803                 :       }
   95804                 :     }else{
   95805            1478 :       sqlite3ExprCode(pParse, p->pLimit, iLimit);
   95806            1478 :       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
   95807            1478 :       VdbeComment((v, "LIMIT counter"));
   95808            1478 :       sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
   95809                 :     }
   95810           13321 :     if( p->pOffset ){
   95811               0 :       p->iOffset = iOffset = ++pParse->nMem;
   95812               0 :       pParse->nMem++;   /* Allocate an extra register for limit+offset */
   95813               0 :       sqlite3ExprCode(pParse, p->pOffset, iOffset);
   95814               0 :       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
   95815               0 :       VdbeComment((v, "OFFSET counter"));
   95816               0 :       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
   95817               0 :       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
   95818               0 :       sqlite3VdbeJumpHere(v, addr1);
   95819               0 :       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
   95820               0 :       VdbeComment((v, "LIMIT+OFFSET"));
   95821               0 :       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
   95822               0 :       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
   95823               0 :       sqlite3VdbeJumpHere(v, addr1);
   95824                 :     }
   95825                 :   }
   95826                 : }
   95827                 : 
   95828                 : #ifndef SQLITE_OMIT_COMPOUND_SELECT
   95829                 : /*
   95830                 : ** Return the appropriate collating sequence for the iCol-th column of
   95831                 : ** the result set for the compound-select statement "p".  Return NULL if
   95832                 : ** the column has no default collating sequence.
   95833                 : **
   95834                 : ** The collating sequence for the compound select is taken from the
   95835                 : ** left-most term of the select that has a collating sequence.
   95836                 : */
   95837              16 : static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
   95838                 :   CollSeq *pRet;
   95839              16 :   if( p->pPrior ){
   95840               8 :     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
   95841                 :   }else{
   95842               8 :     pRet = 0;
   95843                 :   }
   95844              16 :   assert( iCol>=0 );
   95845              16 :   if( pRet==0 && iCol<p->pEList->nExpr ){
   95846              14 :     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
   95847                 :   }
   95848              16 :   return pRet;
   95849                 : }
   95850                 : #endif /* SQLITE_OMIT_COMPOUND_SELECT */
   95851                 : 
   95852                 : /* Forward reference */
   95853                 : static int multiSelectOrderBy(
   95854                 :   Parse *pParse,        /* Parsing context */
   95855                 :   Select *p,            /* The right-most of SELECTs to be coded */
   95856                 :   SelectDest *pDest     /* What to do with query results */
   95857                 : );
   95858                 : 
   95859                 : 
   95860                 : #ifndef SQLITE_OMIT_COMPOUND_SELECT
   95861                 : /*
   95862                 : ** This routine is called to process a compound query form from
   95863                 : ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
   95864                 : ** INTERSECT
   95865                 : **
   95866                 : ** "p" points to the right-most of the two queries.  the query on the
   95867                 : ** left is p->pPrior.  The left query could also be a compound query
   95868                 : ** in which case this routine will be called recursively. 
   95869                 : **
   95870                 : ** The results of the total query are to be written into a destination
   95871                 : ** of type eDest with parameter iParm.
   95872                 : **
   95873                 : ** Example 1:  Consider a three-way compound SQL statement.
   95874                 : **
   95875                 : **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
   95876                 : **
   95877                 : ** This statement is parsed up as follows:
   95878                 : **
   95879                 : **     SELECT c FROM t3
   95880                 : **      |
   95881                 : **      `----->  SELECT b FROM t2
   95882                 : **                |
   95883                 : **                `------>  SELECT a FROM t1
   95884                 : **
   95885                 : ** The arrows in the diagram above represent the Select.pPrior pointer.
   95886                 : ** So if this routine is called with p equal to the t3 query, then
   95887                 : ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
   95888                 : **
   95889                 : ** Notice that because of the way SQLite parses compound SELECTs, the
   95890                 : ** individual selects always group from left to right.
   95891                 : */
   95892             169 : static int multiSelect(
   95893                 :   Parse *pParse,        /* Parsing context */
   95894                 :   Select *p,            /* The right-most of SELECTs to be coded */
   95895                 :   SelectDest *pDest     /* What to do with query results */
   95896                 : ){
   95897             169 :   int rc = SQLITE_OK;   /* Success code from a subroutine */
   95898                 :   Select *pPrior;       /* Another SELECT immediately to our left */
   95899                 :   Vdbe *v;              /* Generate code to this VDBE */
   95900                 :   SelectDest dest;      /* Alternative data destination */
   95901             169 :   Select *pDelete = 0;  /* Chain of simple selects to delete */
   95902                 :   sqlite3 *db;          /* Database connection */
   95903                 : #ifndef SQLITE_OMIT_EXPLAIN
   95904                 :   int iSub1;            /* EQP id of left-hand query */
   95905                 :   int iSub2;            /* EQP id of right-hand query */
   95906                 : #endif
   95907                 : 
   95908                 :   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
   95909                 :   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
   95910                 :   */
   95911             169 :   assert( p && p->pPrior );  /* Calling function guarantees this much */
   95912             169 :   db = pParse->db;
   95913             169 :   pPrior = p->pPrior;
   95914             169 :   assert( pPrior->pRightmost!=pPrior );
   95915             169 :   assert( pPrior->pRightmost==p->pRightmost );
   95916             169 :   dest = *pDest;
   95917             169 :   if( pPrior->pOrderBy ){
   95918               0 :     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
   95919               0 :       selectOpName(p->op));
   95920               0 :     rc = 1;
   95921               0 :     goto multi_select_end;
   95922                 :   }
   95923             169 :   if( pPrior->pLimit ){
   95924               0 :     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
   95925               0 :       selectOpName(p->op));
   95926               0 :     rc = 1;
   95927               0 :     goto multi_select_end;
   95928                 :   }
   95929                 : 
   95930             169 :   v = sqlite3GetVdbe(pParse);
   95931             169 :   assert( v!=0 );  /* The VDBE already created by calling function */
   95932                 : 
   95933                 :   /* Create the destination temporary table if necessary
   95934                 :   */
   95935             169 :   if( dest.eDest==SRT_EphemTab ){
   95936              81 :     assert( p->pEList );
   95937              81 :     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr);
   95938              81 :     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
   95939              81 :     dest.eDest = SRT_Table;
   95940                 :   }
   95941                 : 
   95942                 :   /* Make sure all SELECTs in the statement have the same number of elements
   95943                 :   ** in their result sets.
   95944                 :   */
   95945             169 :   assert( p->pEList && pPrior->pEList );
   95946             169 :   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
   95947               0 :     sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
   95948               0 :       " do not have the same number of result columns", selectOpName(p->op));
   95949               0 :     rc = 1;
   95950               0 :     goto multi_select_end;
   95951                 :   }
   95952                 : 
   95953                 :   /* Compound SELECTs that have an ORDER BY clause are handled separately.
   95954                 :   */
   95955             169 :   if( p->pOrderBy ){
   95956               8 :     return multiSelectOrderBy(pParse, p, pDest);
   95957                 :   }
   95958                 : 
   95959                 :   /* Generate code for the left and right SELECT statements.
   95960                 :   */
   95961             161 :   switch( p->op ){
   95962                 :     case TK_ALL: {
   95963             157 :       int addr = 0;
   95964                 :       int nLimit;
   95965             157 :       assert( !pPrior->pLimit );
   95966             157 :       pPrior->pLimit = p->pLimit;
   95967             157 :       pPrior->pOffset = p->pOffset;
   95968             157 :       explainSetInteger(iSub1, pParse->iNextSelectId);
   95969             157 :       rc = sqlite3Select(pParse, pPrior, &dest);
   95970             157 :       p->pLimit = 0;
   95971             157 :       p->pOffset = 0;
   95972             157 :       if( rc ){
   95973               0 :         goto multi_select_end;
   95974                 :       }
   95975             157 :       p->pPrior = 0;
   95976             157 :       p->iLimit = pPrior->iLimit;
   95977             157 :       p->iOffset = pPrior->iOffset;
   95978             157 :       if( p->iLimit ){
   95979               9 :         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
   95980               9 :         VdbeComment((v, "Jump ahead if LIMIT reached"));
   95981                 :       }
   95982             157 :       explainSetInteger(iSub2, pParse->iNextSelectId);
   95983             157 :       rc = sqlite3Select(pParse, p, &dest);
   95984                 :       testcase( rc!=SQLITE_OK );
   95985             157 :       pDelete = p->pPrior;
   95986             157 :       p->pPrior = pPrior;
   95987             157 :       p->nSelectRow += pPrior->nSelectRow;
   95988             157 :       if( pPrior->pLimit
   95989               9 :        && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
   95990               9 :        && p->nSelectRow > (double)nLimit 
   95991                 :       ){
   95992               9 :         p->nSelectRow = (double)nLimit;
   95993                 :       }
   95994             157 :       if( addr ){
   95995               9 :         sqlite3VdbeJumpHere(v, addr);
   95996                 :       }
   95997             157 :       break;
   95998                 :     }
   95999                 :     case TK_EXCEPT:
   96000                 :     case TK_UNION: {
   96001                 :       int unionTab;    /* Cursor number of the temporary table holding result */
   96002               4 :       u8 op = 0;       /* One of the SRT_ operations to apply to self */
   96003                 :       int priorOp;     /* The SRT_ operation to apply to prior selects */
   96004                 :       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
   96005                 :       int addr;
   96006                 :       SelectDest uniondest;
   96007                 : 
   96008                 :       testcase( p->op==TK_EXCEPT );
   96009                 :       testcase( p->op==TK_UNION );
   96010               4 :       priorOp = SRT_Union;
   96011               4 :       if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
   96012                 :         /* We can reuse a temporary table generated by a SELECT to our
   96013                 :         ** right.
   96014                 :         */
   96015               0 :         assert( p->pRightmost!=p );  /* Can only happen for leftward elements
   96016                 :                                      ** of a 3-way or more compound */
   96017               0 :         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
   96018               0 :         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
   96019               0 :         unionTab = dest.iParm;
   96020                 :       }else{
   96021                 :         /* We will need to create our own temporary table to hold the
   96022                 :         ** intermediate results.
   96023                 :         */
   96024               4 :         unionTab = pParse->nTab++;
   96025               4 :         assert( p->pOrderBy==0 );
   96026               4 :         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
   96027               4 :         assert( p->addrOpenEphm[0] == -1 );
   96028               4 :         p->addrOpenEphm[0] = addr;
   96029               4 :         p->pRightmost->selFlags |= SF_UsesEphemeral;
   96030               4 :         assert( p->pEList );
   96031                 :       }
   96032                 : 
   96033                 :       /* Code the SELECT statements to our left
   96034                 :       */
   96035               4 :       assert( !pPrior->pOrderBy );
   96036               4 :       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
   96037               4 :       explainSetInteger(iSub1, pParse->iNextSelectId);
   96038               4 :       rc = sqlite3Select(pParse, pPrior, &uniondest);
   96039               4 :       if( rc ){
   96040               0 :         goto multi_select_end;
   96041                 :       }
   96042                 : 
   96043                 :       /* Code the current SELECT statement
   96044                 :       */
   96045               4 :       if( p->op==TK_EXCEPT ){
   96046               2 :         op = SRT_Except;
   96047                 :       }else{
   96048               2 :         assert( p->op==TK_UNION );
   96049               2 :         op = SRT_Union;
   96050                 :       }
   96051               4 :       p->pPrior = 0;
   96052               4 :       pLimit = p->pLimit;
   96053               4 :       p->pLimit = 0;
   96054               4 :       pOffset = p->pOffset;
   96055               4 :       p->pOffset = 0;
   96056               4 :       uniondest.eDest = op;
   96057               4 :       explainSetInteger(iSub2, pParse->iNextSelectId);
   96058               4 :       rc = sqlite3Select(pParse, p, &uniondest);
   96059                 :       testcase( rc!=SQLITE_OK );
   96060                 :       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
   96061                 :       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
   96062               4 :       sqlite3ExprListDelete(db, p->pOrderBy);
   96063               4 :       pDelete = p->pPrior;
   96064               4 :       p->pPrior = pPrior;
   96065               4 :       p->pOrderBy = 0;
   96066               4 :       if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
   96067               4 :       sqlite3ExprDelete(db, p->pLimit);
   96068               4 :       p->pLimit = pLimit;
   96069               4 :       p->pOffset = pOffset;
   96070               4 :       p->iLimit = 0;
   96071               4 :       p->iOffset = 0;
   96072                 : 
   96073                 :       /* Convert the data in the temporary table into whatever form
   96074                 :       ** it is that we currently need.
   96075                 :       */
   96076               4 :       assert( unionTab==dest.iParm || dest.eDest!=priorOp );
   96077               4 :       if( dest.eDest!=priorOp ){
   96078                 :         int iCont, iBreak, iStart;
   96079               4 :         assert( p->pEList );
   96080               4 :         if( dest.eDest==SRT_Output ){
   96081               2 :           Select *pFirst = p;
   96082               2 :           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
   96083               2 :           generateColumnNames(pParse, 0, pFirst->pEList);
   96084                 :         }
   96085               4 :         iBreak = sqlite3VdbeMakeLabel(v);
   96086               4 :         iCont = sqlite3VdbeMakeLabel(v);
   96087               4 :         computeLimitRegisters(pParse, p, iBreak);
   96088               4 :         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
   96089               4 :         iStart = sqlite3VdbeCurrentAddr(v);
   96090               4 :         selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
   96091                 :                         0, -1, &dest, iCont, iBreak);
   96092               4 :         sqlite3VdbeResolveLabel(v, iCont);
   96093               4 :         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
   96094               4 :         sqlite3VdbeResolveLabel(v, iBreak);
   96095               4 :         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
   96096                 :       }
   96097               4 :       break;
   96098                 :     }
   96099               0 :     default: assert( p->op==TK_INTERSECT ); {
   96100                 :       int tab1, tab2;
   96101                 :       int iCont, iBreak, iStart;
   96102                 :       Expr *pLimit, *pOffset;
   96103                 :       int addr;
   96104                 :       SelectDest intersectdest;
   96105                 :       int r1;
   96106                 : 
   96107                 :       /* INTERSECT is different from the others since it requires
   96108                 :       ** two temporary tables.  Hence it has its own case.  Begin
   96109                 :       ** by allocating the tables we will need.
   96110                 :       */
   96111               0 :       tab1 = pParse->nTab++;
   96112               0 :       tab2 = pParse->nTab++;
   96113               0 :       assert( p->pOrderBy==0 );
   96114                 : 
   96115               0 :       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
   96116               0 :       assert( p->addrOpenEphm[0] == -1 );
   96117               0 :       p->addrOpenEphm[0] = addr;
   96118               0 :       p->pRightmost->selFlags |= SF_UsesEphemeral;
   96119               0 :       assert( p->pEList );
   96120                 : 
   96121                 :       /* Code the SELECTs to our left into temporary table "tab1".
   96122                 :       */
   96123               0 :       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
   96124               0 :       explainSetInteger(iSub1, pParse->iNextSelectId);
   96125               0 :       rc = sqlite3Select(pParse, pPrior, &intersectdest);
   96126               0 :       if( rc ){
   96127               0 :         goto multi_select_end;
   96128                 :       }
   96129                 : 
   96130                 :       /* Code the current SELECT into temporary table "tab2"
   96131                 :       */
   96132               0 :       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
   96133               0 :       assert( p->addrOpenEphm[1] == -1 );
   96134               0 :       p->addrOpenEphm[1] = addr;
   96135               0 :       p->pPrior = 0;
   96136               0 :       pLimit = p->pLimit;
   96137               0 :       p->pLimit = 0;
   96138               0 :       pOffset = p->pOffset;
   96139               0 :       p->pOffset = 0;
   96140               0 :       intersectdest.iParm = tab2;
   96141               0 :       explainSetInteger(iSub2, pParse->iNextSelectId);
   96142               0 :       rc = sqlite3Select(pParse, p, &intersectdest);
   96143                 :       testcase( rc!=SQLITE_OK );
   96144               0 :       pDelete = p->pPrior;
   96145               0 :       p->pPrior = pPrior;
   96146               0 :       if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
   96147               0 :       sqlite3ExprDelete(db, p->pLimit);
   96148               0 :       p->pLimit = pLimit;
   96149               0 :       p->pOffset = pOffset;
   96150                 : 
   96151                 :       /* Generate code to take the intersection of the two temporary
   96152                 :       ** tables.
   96153                 :       */
   96154               0 :       assert( p->pEList );
   96155               0 :       if( dest.eDest==SRT_Output ){
   96156               0 :         Select *pFirst = p;
   96157               0 :         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
   96158               0 :         generateColumnNames(pParse, 0, pFirst->pEList);
   96159                 :       }
   96160               0 :       iBreak = sqlite3VdbeMakeLabel(v);
   96161               0 :       iCont = sqlite3VdbeMakeLabel(v);
   96162               0 :       computeLimitRegisters(pParse, p, iBreak);
   96163               0 :       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
   96164               0 :       r1 = sqlite3GetTempReg(pParse);
   96165               0 :       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
   96166               0 :       sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
   96167               0 :       sqlite3ReleaseTempReg(pParse, r1);
   96168               0 :       selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
   96169                 :                       0, -1, &dest, iCont, iBreak);
   96170               0 :       sqlite3VdbeResolveLabel(v, iCont);
   96171               0 :       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
   96172               0 :       sqlite3VdbeResolveLabel(v, iBreak);
   96173               0 :       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
   96174               0 :       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
   96175               0 :       break;
   96176                 :     }
   96177                 :   }
   96178                 : 
   96179             161 :   explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
   96180                 : 
   96181                 :   /* Compute collating sequences used by 
   96182                 :   ** temporary tables needed to implement the compound select.
   96183                 :   ** Attach the KeyInfo structure to all temporary tables.
   96184                 :   **
   96185                 :   ** This section is run by the right-most SELECT statement only.
   96186                 :   ** SELECT statements to the left always skip this part.  The right-most
   96187                 :   ** SELECT might also skip this part if it has no ORDER BY clause and
   96188                 :   ** no temp tables are required.
   96189                 :   */
   96190             161 :   if( p->selFlags & SF_UsesEphemeral ){
   96191                 :     int i;                        /* Loop counter */
   96192                 :     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
   96193                 :     Select *pLoop;                /* For looping through SELECT statements */
   96194                 :     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
   96195                 :     int nCol;                     /* Number of columns in result set */
   96196                 : 
   96197               4 :     assert( p->pRightmost==p );
   96198               4 :     nCol = p->pEList->nExpr;
   96199               4 :     pKeyInfo = sqlite3DbMallocZero(db,
   96200               4 :                        sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1));
   96201               4 :     if( !pKeyInfo ){
   96202               0 :       rc = SQLITE_NOMEM;
   96203               0 :       goto multi_select_end;
   96204                 :     }
   96205                 : 
   96206               4 :     pKeyInfo->enc = ENC(db);
   96207               4 :     pKeyInfo->nField = (u16)nCol;
   96208                 : 
   96209              10 :     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
   96210               6 :       *apColl = multiSelectCollSeq(pParse, p, i);
   96211               6 :       if( 0==*apColl ){
   96212               4 :         *apColl = db->pDfltColl;
   96213                 :       }
   96214                 :     }
   96215                 : 
   96216              12 :     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
   96217              12 :       for(i=0; i<2; i++){
   96218              12 :         int addr = pLoop->addrOpenEphm[i];
   96219              12 :         if( addr<0 ){
   96220                 :           /* If [0] is unused then [1] is also unused.  So we can
   96221                 :           ** always safely abort as soon as the first unused slot is found */
   96222               8 :           assert( pLoop->addrOpenEphm[1]<0 );
   96223               8 :           break;
   96224                 :         }
   96225               4 :         sqlite3VdbeChangeP2(v, addr, nCol);
   96226               4 :         sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO);
   96227               4 :         pLoop->addrOpenEphm[i] = -1;
   96228                 :       }
   96229                 :     }
   96230               4 :     sqlite3DbFree(db, pKeyInfo);
   96231                 :   }
   96232                 : 
   96233                 : multi_select_end:
   96234             161 :   pDest->iMem = dest.iMem;
   96235             161 :   pDest->nMem = dest.nMem;
   96236             161 :   sqlite3SelectDelete(db, pDelete);
   96237             161 :   return rc;
   96238                 : }
   96239                 : #endif /* SQLITE_OMIT_COMPOUND_SELECT */
   96240                 : 
   96241                 : /*
   96242                 : ** Code an output subroutine for a coroutine implementation of a
   96243                 : ** SELECT statment.
   96244                 : **
   96245                 : ** The data to be output is contained in pIn->iMem.  There are
   96246                 : ** pIn->nMem columns to be output.  pDest is where the output should
   96247                 : ** be sent.
   96248                 : **
   96249                 : ** regReturn is the number of the register holding the subroutine
   96250                 : ** return address.
   96251                 : **
   96252                 : ** If regPrev>0 then it is the first register in a vector that
   96253                 : ** records the previous output.  mem[regPrev] is a flag that is false
   96254                 : ** if there has been no previous output.  If regPrev>0 then code is
   96255                 : ** generated to suppress duplicates.  pKeyInfo is used for comparing
   96256                 : ** keys.
   96257                 : **
   96258                 : ** If the LIMIT found in p->iLimit is reached, jump immediately to
   96259                 : ** iBreak.
   96260                 : */
   96261              16 : static int generateOutputSubroutine(
   96262                 :   Parse *pParse,          /* Parsing context */
   96263                 :   Select *p,              /* The SELECT statement */
   96264                 :   SelectDest *pIn,        /* Coroutine supplying data */
   96265                 :   SelectDest *pDest,      /* Where to send the data */
   96266                 :   int regReturn,          /* The return address register */
   96267                 :   int regPrev,            /* Previous result register.  No uniqueness if 0 */
   96268                 :   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
   96269                 :   int p4type,             /* The p4 type for pKeyInfo */
   96270                 :   int iBreak              /* Jump here if we hit the LIMIT */
   96271                 : ){
   96272              16 :   Vdbe *v = pParse->pVdbe;
   96273                 :   int iContinue;
   96274                 :   int addr;
   96275                 : 
   96276              16 :   addr = sqlite3VdbeCurrentAddr(v);
   96277              16 :   iContinue = sqlite3VdbeMakeLabel(v);
   96278                 : 
   96279                 :   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
   96280                 :   */
   96281              16 :   if( regPrev ){
   96282                 :     int j1, j2;
   96283               0 :     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
   96284               0 :     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem,
   96285                 :                               (char*)pKeyInfo, p4type);
   96286               0 :     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
   96287               0 :     sqlite3VdbeJumpHere(v, j1);
   96288               0 :     sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem);
   96289               0 :     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
   96290                 :   }
   96291              16 :   if( pParse->db->mallocFailed ) return 0;
   96292                 : 
   96293                 :   /* Suppress the the first OFFSET entries if there is an OFFSET clause
   96294                 :   */
   96295              16 :   codeOffset(v, p, iContinue);
   96296                 : 
   96297              16 :   switch( pDest->eDest ){
   96298                 :     /* Store the result as data using a unique key.
   96299                 :     */
   96300                 :     case SRT_Table:
   96301                 :     case SRT_EphemTab: {
   96302               0 :       int r1 = sqlite3GetTempReg(pParse);
   96303               0 :       int r2 = sqlite3GetTempReg(pParse);
   96304                 :       testcase( pDest->eDest==SRT_Table );
   96305                 :       testcase( pDest->eDest==SRT_EphemTab );
   96306               0 :       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1);
   96307               0 :       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2);
   96308               0 :       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2);
   96309               0 :       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   96310               0 :       sqlite3ReleaseTempReg(pParse, r2);
   96311               0 :       sqlite3ReleaseTempReg(pParse, r1);
   96312               0 :       break;
   96313                 :     }
   96314                 : 
   96315                 : #ifndef SQLITE_OMIT_SUBQUERY
   96316                 :     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
   96317                 :     ** then there should be a single item on the stack.  Write this
   96318                 :     ** item into the set table with bogus data.
   96319                 :     */
   96320                 :     case SRT_Set: {
   96321                 :       int r1;
   96322               0 :       assert( pIn->nMem==1 );
   96323               0 :       p->affinity = 
   96324               0 :          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity);
   96325               0 :       r1 = sqlite3GetTempReg(pParse);
   96326               0 :       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1);
   96327               0 :       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1);
   96328               0 :       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1);
   96329               0 :       sqlite3ReleaseTempReg(pParse, r1);
   96330               0 :       break;
   96331                 :     }
   96332                 : 
   96333                 : #if 0  /* Never occurs on an ORDER BY query */
   96334                 :     /* If any row exist in the result set, record that fact and abort.
   96335                 :     */
   96336                 :     case SRT_Exists: {
   96337                 :       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm);
   96338                 :       /* The LIMIT clause will terminate the loop for us */
   96339                 :       break;
   96340                 :     }
   96341                 : #endif
   96342                 : 
   96343                 :     /* If this is a scalar select that is part of an expression, then
   96344                 :     ** store the results in the appropriate memory cell and break out
   96345                 :     ** of the scan loop.
   96346                 :     */
   96347                 :     case SRT_Mem: {
   96348               0 :       assert( pIn->nMem==1 );
   96349               0 :       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1);
   96350                 :       /* The LIMIT clause will jump out of the loop for us */
   96351               0 :       break;
   96352                 :     }
   96353                 : #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
   96354                 : 
   96355                 :     /* The results are stored in a sequence of registers
   96356                 :     ** starting at pDest->iMem.  Then the co-routine yields.
   96357                 :     */
   96358                 :     case SRT_Coroutine: {
   96359               0 :       if( pDest->iMem==0 ){
   96360               0 :         pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem);
   96361               0 :         pDest->nMem = pIn->nMem;
   96362                 :       }
   96363               0 :       sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem);
   96364               0 :       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm);
   96365               0 :       break;
   96366                 :     }
   96367                 : 
   96368                 :     /* If none of the above, then the result destination must be
   96369                 :     ** SRT_Output.  This routine is never called with any other
   96370                 :     ** destination other than the ones handled above or SRT_Output.
   96371                 :     **
   96372                 :     ** For SRT_Output, results are stored in a sequence of registers.  
   96373                 :     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
   96374                 :     ** return the next row of result.
   96375                 :     */
   96376                 :     default: {
   96377              16 :       assert( pDest->eDest==SRT_Output );
   96378              16 :       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem);
   96379              16 :       sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem);
   96380              16 :       break;
   96381                 :     }
   96382                 :   }
   96383                 : 
   96384                 :   /* Jump to the end of the loop if the LIMIT is reached.
   96385                 :   */
   96386              16 :   if( p->iLimit ){
   96387               0 :     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
   96388                 :   }
   96389                 : 
   96390                 :   /* Generate the subroutine return
   96391                 :   */
   96392              16 :   sqlite3VdbeResolveLabel(v, iContinue);
   96393              16 :   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
   96394                 : 
   96395              16 :   return addr;
   96396                 : }
   96397                 : 
   96398                 : /*
   96399                 : ** Alternative compound select code generator for cases when there
   96400                 : ** is an ORDER BY clause.
   96401                 : **
   96402                 : ** We assume a query of the following form:
   96403                 : **
   96404                 : **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
   96405                 : **
   96406                 : ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
   96407                 : ** is to code both <selectA> and <selectB> with the ORDER BY clause as
   96408                 : ** co-routines.  Then run the co-routines in parallel and merge the results
   96409                 : ** into the output.  In addition to the two coroutines (called selectA and
   96410                 : ** selectB) there are 7 subroutines:
   96411                 : **
   96412                 : **    outA:    Move the output of the selectA coroutine into the output
   96413                 : **             of the compound query.
   96414                 : **
   96415                 : **    outB:    Move the output of the selectB coroutine into the output
   96416                 : **             of the compound query.  (Only generated for UNION and
   96417                 : **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
   96418                 : **             appears only in B.)
   96419                 : **
   96420                 : **    AltB:    Called when there is data from both coroutines and A<B.
   96421                 : **
   96422                 : **    AeqB:    Called when there is data from both coroutines and A==B.
   96423                 : **
   96424                 : **    AgtB:    Called when there is data from both coroutines and A>B.
   96425                 : **
   96426                 : **    EofA:    Called when data is exhausted from selectA.
   96427                 : **
   96428                 : **    EofB:    Called when data is exhausted from selectB.
   96429                 : **
   96430                 : ** The implementation of the latter five subroutines depend on which 
   96431                 : ** <operator> is used:
   96432                 : **
   96433                 : **
   96434                 : **             UNION ALL         UNION            EXCEPT          INTERSECT
   96435                 : **          -------------  -----------------  --------------  -----------------
   96436                 : **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
   96437                 : **
   96438                 : **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
   96439                 : **
   96440                 : **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
   96441                 : **
   96442                 : **   EofA:   outB, nextB      outB, nextB          halt             halt
   96443                 : **
   96444                 : **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
   96445                 : **
   96446                 : ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
   96447                 : ** causes an immediate jump to EofA and an EOF on B following nextB causes
   96448                 : ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
   96449                 : ** following nextX causes a jump to the end of the select processing.
   96450                 : **
   96451                 : ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
   96452                 : ** within the output subroutine.  The regPrev register set holds the previously
   96453                 : ** output value.  A comparison is made against this value and the output
   96454                 : ** is skipped if the next results would be the same as the previous.
   96455                 : **
   96456                 : ** The implementation plan is to implement the two coroutines and seven
   96457                 : ** subroutines first, then put the control logic at the bottom.  Like this:
   96458                 : **
   96459                 : **          goto Init
   96460                 : **     coA: coroutine for left query (A)
   96461                 : **     coB: coroutine for right query (B)
   96462                 : **    outA: output one row of A
   96463                 : **    outB: output one row of B (UNION and UNION ALL only)
   96464                 : **    EofA: ...
   96465                 : **    EofB: ...
   96466                 : **    AltB: ...
   96467                 : **    AeqB: ...
   96468                 : **    AgtB: ...
   96469                 : **    Init: initialize coroutine registers
   96470                 : **          yield coA
   96471                 : **          if eof(A) goto EofA
   96472                 : **          yield coB
   96473                 : **          if eof(B) goto EofB
   96474                 : **    Cmpr: Compare A, B
   96475                 : **          Jump AltB, AeqB, AgtB
   96476                 : **     End: ...
   96477                 : **
   96478                 : ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
   96479                 : ** actually called using Gosub and they do not Return.  EofA and EofB loop
   96480                 : ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
   96481                 : ** and AgtB jump to either L2 or to one of EofA or EofB.
   96482                 : */
   96483                 : #ifndef SQLITE_OMIT_COMPOUND_SELECT
   96484               8 : static int multiSelectOrderBy(
   96485                 :   Parse *pParse,        /* Parsing context */
   96486                 :   Select *p,            /* The right-most of SELECTs to be coded */
   96487                 :   SelectDest *pDest     /* What to do with query results */
   96488                 : ){
   96489                 :   int i, j;             /* Loop counters */
   96490                 :   Select *pPrior;       /* Another SELECT immediately to our left */
   96491                 :   Vdbe *v;              /* Generate code to this VDBE */
   96492                 :   SelectDest destA;     /* Destination for coroutine A */
   96493                 :   SelectDest destB;     /* Destination for coroutine B */
   96494                 :   int regAddrA;         /* Address register for select-A coroutine */
   96495                 :   int regEofA;          /* Flag to indicate when select-A is complete */
   96496                 :   int regAddrB;         /* Address register for select-B coroutine */
   96497                 :   int regEofB;          /* Flag to indicate when select-B is complete */
   96498                 :   int addrSelectA;      /* Address of the select-A coroutine */
   96499                 :   int addrSelectB;      /* Address of the select-B coroutine */
   96500                 :   int regOutA;          /* Address register for the output-A subroutine */
   96501                 :   int regOutB;          /* Address register for the output-B subroutine */
   96502                 :   int addrOutA;         /* Address of the output-A subroutine */
   96503               8 :   int addrOutB = 0;     /* Address of the output-B subroutine */
   96504                 :   int addrEofA;         /* Address of the select-A-exhausted subroutine */
   96505                 :   int addrEofB;         /* Address of the select-B-exhausted subroutine */
   96506                 :   int addrAltB;         /* Address of the A<B subroutine */
   96507                 :   int addrAeqB;         /* Address of the A==B subroutine */
   96508                 :   int addrAgtB;         /* Address of the A>B subroutine */
   96509                 :   int regLimitA;        /* Limit register for select-A */
   96510                 :   int regLimitB;        /* Limit register for select-A */
   96511                 :   int regPrev;          /* A range of registers to hold previous output */
   96512                 :   int savedLimit;       /* Saved value of p->iLimit */
   96513                 :   int savedOffset;      /* Saved value of p->iOffset */
   96514                 :   int labelCmpr;        /* Label for the start of the merge algorithm */
   96515                 :   int labelEnd;         /* Label for the end of the overall SELECT stmt */
   96516                 :   int j1;               /* Jump instructions that get retargetted */
   96517                 :   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
   96518               8 :   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
   96519                 :   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
   96520                 :   sqlite3 *db;          /* Database connection */
   96521                 :   ExprList *pOrderBy;   /* The ORDER BY clause */
   96522                 :   int nOrderBy;         /* Number of terms in the ORDER BY clause */
   96523                 :   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
   96524                 : #ifndef SQLITE_OMIT_EXPLAIN
   96525                 :   int iSub1;            /* EQP id of left-hand query */
   96526                 :   int iSub2;            /* EQP id of right-hand query */
   96527                 : #endif
   96528                 : 
   96529               8 :   assert( p->pOrderBy!=0 );
   96530               8 :   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
   96531               8 :   db = pParse->db;
   96532               8 :   v = pParse->pVdbe;
   96533               8 :   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
   96534               8 :   labelEnd = sqlite3VdbeMakeLabel(v);
   96535               8 :   labelCmpr = sqlite3VdbeMakeLabel(v);
   96536                 : 
   96537                 : 
   96538                 :   /* Patch up the ORDER BY clause
   96539                 :   */
   96540               8 :   op = p->op;  
   96541               8 :   pPrior = p->pPrior;
   96542               8 :   assert( pPrior->pOrderBy==0 );
   96543               8 :   pOrderBy = p->pOrderBy;
   96544               8 :   assert( pOrderBy );
   96545               8 :   nOrderBy = pOrderBy->nExpr;
   96546                 : 
   96547                 :   /* For operators other than UNION ALL we have to make sure that
   96548                 :   ** the ORDER BY clause covers every term of the result set.  Add
   96549                 :   ** terms to the ORDER BY clause as necessary.
   96550                 :   */
   96551               8 :   if( op!=TK_ALL ){
   96552               0 :     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
   96553                 :       struct ExprList_item *pItem;
   96554               0 :       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
   96555               0 :         assert( pItem->iOrderByCol>0 );
   96556               0 :         if( pItem->iOrderByCol==i ) break;
   96557                 :       }
   96558               0 :       if( j==nOrderBy ){
   96559               0 :         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
   96560               0 :         if( pNew==0 ) return SQLITE_NOMEM;
   96561               0 :         pNew->flags |= EP_IntValue;
   96562               0 :         pNew->u.iValue = i;
   96563               0 :         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
   96564               0 :         pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
   96565                 :       }
   96566                 :     }
   96567                 :   }
   96568                 : 
   96569                 :   /* Compute the comparison permutation and keyinfo that is used with
   96570                 :   ** the permutation used to determine if the next
   96571                 :   ** row of results comes from selectA or selectB.  Also add explicit
   96572                 :   ** collations to the ORDER BY clause terms so that when the subqueries
   96573                 :   ** to the right and the left are evaluated, they use the correct
   96574                 :   ** collation.
   96575                 :   */
   96576               8 :   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
   96577               8 :   if( aPermute ){
   96578                 :     struct ExprList_item *pItem;
   96579              16 :     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
   96580               8 :       assert( pItem->iOrderByCol>0  && pItem->iOrderByCol<=p->pEList->nExpr );
   96581               8 :       aPermute[i] = pItem->iOrderByCol - 1;
   96582                 :     }
   96583               8 :     pKeyMerge =
   96584               8 :       sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
   96585               8 :     if( pKeyMerge ){
   96586               8 :       pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
   96587               8 :       pKeyMerge->nField = (u16)nOrderBy;
   96588               8 :       pKeyMerge->enc = ENC(db);
   96589              16 :       for(i=0; i<nOrderBy; i++){
   96590                 :         CollSeq *pColl;
   96591               8 :         Expr *pTerm = pOrderBy->a[i].pExpr;
   96592               8 :         if( pTerm->flags & EP_ExpCollate ){
   96593               6 :           pColl = pTerm->pColl;
   96594                 :         }else{
   96595               2 :           pColl = multiSelectCollSeq(pParse, p, aPermute[i]);
   96596               2 :           pTerm->flags |= EP_ExpCollate;
   96597               2 :           pTerm->pColl = pColl;
   96598                 :         }
   96599               8 :         pKeyMerge->aColl[i] = pColl;
   96600               8 :         pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder;
   96601                 :       }
   96602                 :     }
   96603                 :   }else{
   96604               0 :     pKeyMerge = 0;
   96605                 :   }
   96606                 : 
   96607                 :   /* Reattach the ORDER BY clause to the query.
   96608                 :   */
   96609               8 :   p->pOrderBy = pOrderBy;
   96610               8 :   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
   96611                 : 
   96612                 :   /* Allocate a range of temporary registers and the KeyInfo needed
   96613                 :   ** for the logic that removes duplicate result rows when the
   96614                 :   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
   96615                 :   */
   96616               8 :   if( op==TK_ALL ){
   96617               8 :     regPrev = 0;
   96618                 :   }else{
   96619               0 :     int nExpr = p->pEList->nExpr;
   96620               0 :     assert( nOrderBy>=nExpr || db->mallocFailed );
   96621               0 :     regPrev = sqlite3GetTempRange(pParse, nExpr+1);
   96622               0 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
   96623               0 :     pKeyDup = sqlite3DbMallocZero(db,
   96624               0 :                   sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) );
   96625               0 :     if( pKeyDup ){
   96626               0 :       pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr];
   96627               0 :       pKeyDup->nField = (u16)nExpr;
   96628               0 :       pKeyDup->enc = ENC(db);
   96629               0 :       for(i=0; i<nExpr; i++){
   96630               0 :         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
   96631               0 :         pKeyDup->aSortOrder[i] = 0;
   96632                 :       }
   96633                 :     }
   96634                 :   }
   96635                 :  
   96636                 :   /* Separate the left and the right query from one another
   96637                 :   */
   96638               8 :   p->pPrior = 0;
   96639               8 :   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
   96640               8 :   if( pPrior->pPrior==0 ){
   96641               8 :     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
   96642                 :   }
   96643                 : 
   96644                 :   /* Compute the limit registers */
   96645               8 :   computeLimitRegisters(pParse, p, labelEnd);
   96646               8 :   if( p->iLimit && op==TK_ALL ){
   96647               0 :     regLimitA = ++pParse->nMem;
   96648               0 :     regLimitB = ++pParse->nMem;
   96649               0 :     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
   96650                 :                                   regLimitA);
   96651               0 :     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
   96652                 :   }else{
   96653               8 :     regLimitA = regLimitB = 0;
   96654                 :   }
   96655               8 :   sqlite3ExprDelete(db, p->pLimit);
   96656               8 :   p->pLimit = 0;
   96657               8 :   sqlite3ExprDelete(db, p->pOffset);
   96658               8 :   p->pOffset = 0;
   96659                 : 
   96660               8 :   regAddrA = ++pParse->nMem;
   96661               8 :   regEofA = ++pParse->nMem;
   96662               8 :   regAddrB = ++pParse->nMem;
   96663               8 :   regEofB = ++pParse->nMem;
   96664               8 :   regOutA = ++pParse->nMem;
   96665               8 :   regOutB = ++pParse->nMem;
   96666               8 :   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
   96667               8 :   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
   96668                 : 
   96669                 :   /* Jump past the various subroutines and coroutines to the main
   96670                 :   ** merge loop
   96671                 :   */
   96672               8 :   j1 = sqlite3VdbeAddOp0(v, OP_Goto);
   96673               8 :   addrSelectA = sqlite3VdbeCurrentAddr(v);
   96674                 : 
   96675                 : 
   96676                 :   /* Generate a coroutine to evaluate the SELECT statement to the
   96677                 :   ** left of the compound operator - the "A" select.
   96678                 :   */
   96679               8 :   VdbeNoopComment((v, "Begin coroutine for left SELECT"));
   96680               8 :   pPrior->iLimit = regLimitA;
   96681               8 :   explainSetInteger(iSub1, pParse->iNextSelectId);
   96682               8 :   sqlite3Select(pParse, pPrior, &destA);
   96683               8 :   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
   96684               8 :   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
   96685               8 :   VdbeNoopComment((v, "End coroutine for left SELECT"));
   96686                 : 
   96687                 :   /* Generate a coroutine to evaluate the SELECT statement on 
   96688                 :   ** the right - the "B" select
   96689                 :   */
   96690               8 :   addrSelectB = sqlite3VdbeCurrentAddr(v);
   96691               8 :   VdbeNoopComment((v, "Begin coroutine for right SELECT"));
   96692               8 :   savedLimit = p->iLimit;
   96693               8 :   savedOffset = p->iOffset;
   96694               8 :   p->iLimit = regLimitB;
   96695               8 :   p->iOffset = 0;  
   96696               8 :   explainSetInteger(iSub2, pParse->iNextSelectId);
   96697               8 :   sqlite3Select(pParse, p, &destB);
   96698               8 :   p->iLimit = savedLimit;
   96699               8 :   p->iOffset = savedOffset;
   96700               8 :   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
   96701               8 :   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
   96702               8 :   VdbeNoopComment((v, "End coroutine for right SELECT"));
   96703                 : 
   96704                 :   /* Generate a subroutine that outputs the current row of the A
   96705                 :   ** select as the next output row of the compound select.
   96706                 :   */
   96707               8 :   VdbeNoopComment((v, "Output routine for A"));
   96708               8 :   addrOutA = generateOutputSubroutine(pParse,
   96709                 :                  p, &destA, pDest, regOutA,
   96710                 :                  regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd);
   96711                 :   
   96712                 :   /* Generate a subroutine that outputs the current row of the B
   96713                 :   ** select as the next output row of the compound select.
   96714                 :   */
   96715               8 :   if( op==TK_ALL || op==TK_UNION ){
   96716               8 :     VdbeNoopComment((v, "Output routine for B"));
   96717               8 :     addrOutB = generateOutputSubroutine(pParse,
   96718                 :                  p, &destB, pDest, regOutB,
   96719                 :                  regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd);
   96720                 :   }
   96721                 : 
   96722                 :   /* Generate a subroutine to run when the results from select A
   96723                 :   ** are exhausted and only data in select B remains.
   96724                 :   */
   96725               8 :   VdbeNoopComment((v, "eof-A subroutine"));
   96726               8 :   if( op==TK_EXCEPT || op==TK_INTERSECT ){
   96727               0 :     addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
   96728                 :   }else{  
   96729               8 :     addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
   96730               8 :     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
   96731               8 :     sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
   96732               8 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
   96733               8 :     p->nSelectRow += pPrior->nSelectRow;
   96734                 :   }
   96735                 : 
   96736                 :   /* Generate a subroutine to run when the results from select B
   96737                 :   ** are exhausted and only data in select A remains.
   96738                 :   */
   96739               8 :   if( op==TK_INTERSECT ){
   96740               0 :     addrEofB = addrEofA;
   96741               0 :     if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
   96742                 :   }else{  
   96743               8 :     VdbeNoopComment((v, "eof-B subroutine"));
   96744               8 :     addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
   96745               8 :     sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
   96746               8 :     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
   96747               8 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
   96748                 :   }
   96749                 : 
   96750                 :   /* Generate code to handle the case of A<B
   96751                 :   */
   96752               8 :   VdbeNoopComment((v, "A-lt-B subroutine"));
   96753               8 :   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
   96754               8 :   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
   96755               8 :   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
   96756               8 :   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
   96757                 : 
   96758                 :   /* Generate code to handle the case of A==B
   96759                 :   */
   96760               8 :   if( op==TK_ALL ){
   96761               8 :     addrAeqB = addrAltB;
   96762               0 :   }else if( op==TK_INTERSECT ){
   96763               0 :     addrAeqB = addrAltB;
   96764               0 :     addrAltB++;
   96765                 :   }else{
   96766               0 :     VdbeNoopComment((v, "A-eq-B subroutine"));
   96767               0 :     addrAeqB =
   96768                 :     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
   96769               0 :     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
   96770               0 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
   96771                 :   }
   96772                 : 
   96773                 :   /* Generate code to handle the case of A>B
   96774                 :   */
   96775               8 :   VdbeNoopComment((v, "A-gt-B subroutine"));
   96776               8 :   addrAgtB = sqlite3VdbeCurrentAddr(v);
   96777               8 :   if( op==TK_ALL || op==TK_UNION ){
   96778               8 :     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
   96779                 :   }
   96780               8 :   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
   96781               8 :   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
   96782               8 :   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
   96783                 : 
   96784                 :   /* This code runs once to initialize everything.
   96785                 :   */
   96786               8 :   sqlite3VdbeJumpHere(v, j1);
   96787               8 :   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
   96788               8 :   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
   96789               8 :   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
   96790               8 :   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
   96791               8 :   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
   96792               8 :   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
   96793                 : 
   96794                 :   /* Implement the main merge loop
   96795                 :   */
   96796               8 :   sqlite3VdbeResolveLabel(v, labelCmpr);
   96797               8 :   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
   96798               8 :   sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy,
   96799                 :                          (char*)pKeyMerge, P4_KEYINFO_HANDOFF);
   96800               8 :   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
   96801                 : 
   96802                 :   /* Release temporary registers
   96803                 :   */
   96804               8 :   if( regPrev ){
   96805               0 :     sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1);
   96806                 :   }
   96807                 : 
   96808                 :   /* Jump to the this point in order to terminate the query.
   96809                 :   */
   96810               8 :   sqlite3VdbeResolveLabel(v, labelEnd);
   96811                 : 
   96812                 :   /* Set the number of output columns
   96813                 :   */
   96814               8 :   if( pDest->eDest==SRT_Output ){
   96815               8 :     Select *pFirst = pPrior;
   96816               8 :     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
   96817               8 :     generateColumnNames(pParse, 0, pFirst->pEList);
   96818                 :   }
   96819                 : 
   96820                 :   /* Reassembly the compound query so that it will be freed correctly
   96821                 :   ** by the calling function */
   96822               8 :   if( p->pPrior ){
   96823               0 :     sqlite3SelectDelete(db, p->pPrior);
   96824                 :   }
   96825               8 :   p->pPrior = pPrior;
   96826                 : 
   96827                 :   /*** TBD:  Insert subroutine calls to close cursors on incomplete
   96828                 :   **** subqueries ****/
   96829               8 :   explainComposite(pParse, p->op, iSub1, iSub2, 0);
   96830               8 :   return SQLITE_OK;
   96831                 : }
   96832                 : #endif
   96833                 : 
   96834                 : #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
   96835                 : /* Forward Declarations */
   96836                 : static void substExprList(sqlite3*, ExprList*, int, ExprList*);
   96837                 : static void substSelect(sqlite3*, Select *, int, ExprList *);
   96838                 : 
   96839                 : /*
   96840                 : ** Scan through the expression pExpr.  Replace every reference to
   96841                 : ** a column in table number iTable with a copy of the iColumn-th
   96842                 : ** entry in pEList.  (But leave references to the ROWID column 
   96843                 : ** unchanged.)
   96844                 : **
   96845                 : ** This routine is part of the flattening procedure.  A subquery
   96846                 : ** whose result set is defined by pEList appears as entry in the
   96847                 : ** FROM clause of a SELECT such that the VDBE cursor assigned to that
   96848                 : ** FORM clause entry is iTable.  This routine make the necessary 
   96849                 : ** changes to pExpr so that it refers directly to the source table
   96850                 : ** of the subquery rather the result set of the subquery.
   96851                 : */
   96852            1940 : static Expr *substExpr(
   96853                 :   sqlite3 *db,        /* Report malloc errors to this connection */
   96854                 :   Expr *pExpr,        /* Expr in which substitution occurs */
   96855                 :   int iTable,         /* Table to be substituted */
   96856                 :   ExprList *pEList    /* Substitute expressions */
   96857                 : ){
   96858            1940 :   if( pExpr==0 ) return 0;
   96859            1129 :   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
   96860            1322 :     if( pExpr->iColumn<0 ){
   96861               0 :       pExpr->op = TK_NULL;
   96862                 :     }else{
   96863                 :       Expr *pNew;
   96864             661 :       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
   96865             661 :       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
   96866             661 :       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
   96867             661 :       if( pNew && pExpr->pColl ){
   96868               0 :         pNew->pColl = pExpr->pColl;
   96869                 :       }
   96870             661 :       sqlite3ExprDelete(db, pExpr);
   96871             661 :       pExpr = pNew;
   96872                 :     }
   96873                 :   }else{
   96874             468 :     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
   96875             468 :     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
   96876             468 :     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   96877               6 :       substSelect(db, pExpr->x.pSelect, iTable, pEList);
   96878                 :     }else{
   96879             462 :       substExprList(db, pExpr->x.pList, iTable, pEList);
   96880                 :     }
   96881                 :   }
   96882            1129 :   return pExpr;
   96883                 : }
   96884             603 : static void substExprList(
   96885                 :   sqlite3 *db,         /* Report malloc errors here */
   96886                 :   ExprList *pList,     /* List to scan and in which to make substitutes */
   96887                 :   int iTable,          /* Table to be substituted */
   96888                 :   ExprList *pEList     /* Substitute values */
   96889                 : ){
   96890                 :   int i;
   96891             603 :   if( pList==0 ) return;
   96892            1114 :   for(i=0; i<pList->nExpr; i++){
   96893             875 :     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
   96894                 :   }
   96895                 : }
   96896              18 : static void substSelect(
   96897                 :   sqlite3 *db,         /* Report malloc errors here */
   96898                 :   Select *p,           /* SELECT statement in which to make substitutions */
   96899                 :   int iTable,          /* Table to be replaced */
   96900                 :   ExprList *pEList     /* Substitute values */
   96901                 : ){
   96902                 :   SrcList *pSrc;
   96903                 :   struct SrcList_item *pItem;
   96904                 :   int i;
   96905              18 :   if( !p ) return;
   96906               6 :   substExprList(db, p->pEList, iTable, pEList);
   96907               6 :   substExprList(db, p->pGroupBy, iTable, pEList);
   96908               6 :   substExprList(db, p->pOrderBy, iTable, pEList);
   96909               6 :   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
   96910               6 :   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
   96911               6 :   substSelect(db, p->pPrior, iTable, pEList);
   96912               6 :   pSrc = p->pSrc;
   96913               6 :   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
   96914               6 :   if( ALWAYS(pSrc) ){
   96915              12 :     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
   96916               6 :       substSelect(db, pItem->pSelect, iTable, pEList);
   96917                 :     }
   96918                 :   }
   96919                 : }
   96920                 : #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
   96921                 : 
   96922                 : #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
   96923                 : /*
   96924                 : ** This routine attempts to flatten subqueries as a performance optimization.
   96925                 : ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
   96926                 : **
   96927                 : ** To understand the concept of flattening, consider the following
   96928                 : ** query:
   96929                 : **
   96930                 : **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
   96931                 : **
   96932                 : ** The default way of implementing this query is to execute the
   96933                 : ** subquery first and store the results in a temporary table, then
   96934                 : ** run the outer query on that temporary table.  This requires two
   96935                 : ** passes over the data.  Furthermore, because the temporary table
   96936                 : ** has no indices, the WHERE clause on the outer query cannot be
   96937                 : ** optimized.
   96938                 : **
   96939                 : ** This routine attempts to rewrite queries such as the above into
   96940                 : ** a single flat select, like this:
   96941                 : **
   96942                 : **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
   96943                 : **
   96944                 : ** The code generated for this simpification gives the same result
   96945                 : ** but only has to scan the data once.  And because indices might 
   96946                 : ** exist on the table t1, a complete scan of the data might be
   96947                 : ** avoided.
   96948                 : **
   96949                 : ** Flattening is only attempted if all of the following are true:
   96950                 : **
   96951                 : **   (1)  The subquery and the outer query do not both use aggregates.
   96952                 : **
   96953                 : **   (2)  The subquery is not an aggregate or the outer query is not a join.
   96954                 : **
   96955                 : **   (3)  The subquery is not the right operand of a left outer join
   96956                 : **        (Originally ticket #306.  Strengthened by ticket #3300)
   96957                 : **
   96958                 : **   (4)  The subquery is not DISTINCT.
   96959                 : **
   96960                 : **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
   96961                 : **        sub-queries that were excluded from this optimization. Restriction 
   96962                 : **        (4) has since been expanded to exclude all DISTINCT subqueries.
   96963                 : **
   96964                 : **   (6)  The subquery does not use aggregates or the outer query is not
   96965                 : **        DISTINCT.
   96966                 : **
   96967                 : **   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
   96968                 : **        A FROM clause, consider adding a FROM close with the special
   96969                 : **        table sqlite_once that consists of a single row containing a
   96970                 : **        single NULL.
   96971                 : **
   96972                 : **   (8)  The subquery does not use LIMIT or the outer query is not a join.
   96973                 : **
   96974                 : **   (9)  The subquery does not use LIMIT or the outer query does not use
   96975                 : **        aggregates.
   96976                 : **
   96977                 : **  (10)  The subquery does not use aggregates or the outer query does not
   96978                 : **        use LIMIT.
   96979                 : **
   96980                 : **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
   96981                 : **
   96982                 : **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
   96983                 : **        a separate restriction deriving from ticket #350.
   96984                 : **
   96985                 : **  (13)  The subquery and outer query do not both use LIMIT.
   96986                 : **
   96987                 : **  (14)  The subquery does not use OFFSET.
   96988                 : **
   96989                 : **  (15)  The outer query is not part of a compound select or the
   96990                 : **        subquery does not have a LIMIT clause.
   96991                 : **        (See ticket #2339 and ticket [02a8e81d44]).
   96992                 : **
   96993                 : **  (16)  The outer query is not an aggregate or the subquery does
   96994                 : **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
   96995                 : **        until we introduced the group_concat() function.  
   96996                 : **
   96997                 : **  (17)  The sub-query is not a compound select, or it is a UNION ALL 
   96998                 : **        compound clause made up entirely of non-aggregate queries, and 
   96999                 : **        the parent query:
   97000                 : **
   97001                 : **          * is not itself part of a compound select,
   97002                 : **          * is not an aggregate or DISTINCT query, and
   97003                 : **          * is not a join
   97004                 : **
   97005                 : **        The parent and sub-query may contain WHERE clauses. Subject to
   97006                 : **        rules (11), (13) and (14), they may also contain ORDER BY,
   97007                 : **        LIMIT and OFFSET clauses.  The subquery cannot use any compound
   97008                 : **        operator other than UNION ALL because all the other compound
   97009                 : **        operators have an implied DISTINCT which is disallowed by
   97010                 : **        restriction (4).
   97011                 : **
   97012                 : **  (18)  If the sub-query is a compound select, then all terms of the
   97013                 : **        ORDER by clause of the parent must be simple references to 
   97014                 : **        columns of the sub-query.
   97015                 : **
   97016                 : **  (19)  The subquery does not use LIMIT or the outer query does not
   97017                 : **        have a WHERE clause.
   97018                 : **
   97019                 : **  (20)  If the sub-query is a compound select, then it must not use
   97020                 : **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
   97021                 : **        somewhat by saying that the terms of the ORDER BY clause must
   97022                 : **        appear as unmodified result columns in the outer query.  But we
   97023                 : **        have other optimizations in mind to deal with that case.
   97024                 : **
   97025                 : **  (21)  The subquery does not use LIMIT or the outer query is not
   97026                 : **        DISTINCT.  (See ticket [752e1646fc]).
   97027                 : **
   97028                 : ** In this routine, the "p" parameter is a pointer to the outer query.
   97029                 : ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
   97030                 : ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
   97031                 : **
   97032                 : ** If flattening is not attempted, this routine is a no-op and returns 0.
   97033                 : ** If flattening is attempted this routine returns 1.
   97034                 : **
   97035                 : ** All of the expression analysis must occur on both the outer query and
   97036                 : ** the subquery before this routine runs.
   97037                 : */
   97038             122 : static int flattenSubquery(
   97039                 :   Parse *pParse,       /* Parsing context */
   97040                 :   Select *p,           /* The parent or outer SELECT statement */
   97041                 :   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
   97042                 :   int isAgg,           /* True if outer SELECT uses aggregate functions */
   97043                 :   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
   97044                 : ){
   97045             122 :   const char *zSavedAuthContext = pParse->zAuthContext;
   97046                 :   Select *pParent;
   97047                 :   Select *pSub;       /* The inner query or "subquery" */
   97048                 :   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
   97049                 :   SrcList *pSrc;      /* The FROM clause of the outer query */
   97050                 :   SrcList *pSubSrc;   /* The FROM clause of the subquery */
   97051                 :   ExprList *pList;    /* The result set of the outer query */
   97052                 :   int iParent;        /* VDBE cursor number of the pSub result set temp table */
   97053                 :   int i;              /* Loop counter */
   97054                 :   Expr *pWhere;                    /* The WHERE clause */
   97055                 :   struct SrcList_item *pSubitem;   /* The subquery */
   97056             122 :   sqlite3 *db = pParse->db;
   97057                 : 
   97058                 :   /* Check to see if flattening is permitted.  Return 0 if not.
   97059                 :   */
   97060             122 :   assert( p!=0 );
   97061             122 :   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
   97062             122 :   if( db->flags & SQLITE_QueryFlattener ) return 0;
   97063             122 :   pSrc = p->pSrc;
   97064             122 :   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
   97065             122 :   pSubitem = &pSrc->a[iFrom];
   97066             122 :   iParent = pSubitem->iCursor;
   97067             122 :   pSub = pSubitem->pSelect;
   97068             122 :   assert( pSub!=0 );
   97069             122 :   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
   97070             122 :   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
   97071              98 :   pSubSrc = pSub->pSrc;
   97072              98 :   assert( pSubSrc );
   97073                 :   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
   97074                 :   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
   97075                 :   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
   97076                 :   ** became arbitrary expressions, we were forced to add restrictions (13)
   97077                 :   ** and (14). */
   97078              98 :   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
   97079              98 :   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
   97080              98 :   if( p->pRightmost && pSub->pLimit ){
   97081               0 :     return 0;                                            /* Restriction (15) */
   97082                 :   }
   97083              98 :   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
   97084              82 :   if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
   97085              82 :   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
   97086               0 :      return 0;         /* Restrictions (8)(9) */
   97087                 :   }
   97088              82 :   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
   97089               0 :      return 0;         /* Restriction (6)  */
   97090                 :   }
   97091              82 :   if( p->pOrderBy && pSub->pOrderBy ){
   97092               8 :      return 0;                                           /* Restriction (11) */
   97093                 :   }
   97094              74 :   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
   97095              69 :   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
   97096              69 :   if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
   97097               0 :      return 0;         /* Restriction (21) */
   97098                 :   }
   97099                 : 
   97100                 :   /* OBSOLETE COMMENT 1:
   97101                 :   ** Restriction 3:  If the subquery is a join, make sure the subquery is 
   97102                 :   ** not used as the right operand of an outer join.  Examples of why this
   97103                 :   ** is not allowed:
   97104                 :   **
   97105                 :   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
   97106                 :   **
   97107                 :   ** If we flatten the above, we would get
   97108                 :   **
   97109                 :   **         (t1 LEFT OUTER JOIN t2) JOIN t3
   97110                 :   **
   97111                 :   ** which is not at all the same thing.
   97112                 :   **
   97113                 :   ** OBSOLETE COMMENT 2:
   97114                 :   ** Restriction 12:  If the subquery is the right operand of a left outer
   97115                 :   ** join, make sure the subquery has no WHERE clause.
   97116                 :   ** An examples of why this is not allowed:
   97117                 :   **
   97118                 :   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
   97119                 :   **
   97120                 :   ** If we flatten the above, we would get
   97121                 :   **
   97122                 :   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
   97123                 :   **
   97124                 :   ** But the t2.x>0 test will always fail on a NULL row of t2, which
   97125                 :   ** effectively converts the OUTER JOIN into an INNER JOIN.
   97126                 :   **
   97127                 :   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
   97128                 :   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
   97129                 :   ** is fraught with danger.  Best to avoid the whole thing.  If the
   97130                 :   ** subquery is the right term of a LEFT JOIN, then do not flatten.
   97131                 :   */
   97132              69 :   if( (pSubitem->jointype & JT_OUTER)!=0 ){
   97133               0 :     return 0;
   97134                 :   }
   97135                 : 
   97136                 :   /* Restriction 17: If the sub-query is a compound SELECT, then it must
   97137                 :   ** use only the UNION ALL operator. And none of the simple select queries
   97138                 :   ** that make up the compound SELECT are allowed to be aggregate or distinct
   97139                 :   ** queries.
   97140                 :   */
   97141              69 :   if( pSub->pPrior ){
   97142              60 :     if( pSub->pOrderBy ){
   97143               0 :       return 0;  /* Restriction 20 */
   97144                 :     }
   97145              60 :     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
   97146               6 :       return 0;
   97147                 :     }
   97148             162 :     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
   97149                 :       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
   97150                 :       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
   97151             108 :       assert( pSub->pSrc!=0 );
   97152             108 :       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
   97153             108 :        || (pSub1->pPrior && pSub1->op!=TK_ALL) 
   97154             108 :        || pSub1->pSrc->nSrc<1
   97155                 :       ){
   97156               0 :         return 0;
   97157                 :       }
   97158                 :       testcase( pSub1->pSrc->nSrc>1 );
   97159                 :     }
   97160                 : 
   97161                 :     /* Restriction 18. */
   97162              54 :     if( p->pOrderBy ){
   97163                 :       int ii;
   97164               0 :       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
   97165               0 :         if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
   97166                 :       }
   97167                 :     }
   97168                 :   }
   97169                 : 
   97170                 :   /***** If we reach this point, flattening is permitted. *****/
   97171                 : 
   97172                 :   /* Authorize the subquery */
   97173              63 :   pParse->zAuthContext = pSubitem->zName;
   97174              63 :   sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
   97175              63 :   pParse->zAuthContext = zSavedAuthContext;
   97176                 : 
   97177                 :   /* If the sub-query is a compound SELECT statement, then (by restrictions
   97178                 :   ** 17 and 18 above) it must be a UNION ALL and the parent query must 
   97179                 :   ** be of the form:
   97180                 :   **
   97181                 :   **     SELECT <expr-list> FROM (<sub-query>) <where-clause> 
   97182                 :   **
   97183                 :   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
   97184                 :   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or 
   97185                 :   ** OFFSET clauses and joins them to the left-hand-side of the original
   97186                 :   ** using UNION ALL operators. In this case N is the number of simple
   97187                 :   ** select statements in the compound sub-query.
   97188                 :   **
   97189                 :   ** Example:
   97190                 :   **
   97191                 :   **     SELECT a+1 FROM (
   97192                 :   **        SELECT x FROM tab
   97193                 :   **        UNION ALL
   97194                 :   **        SELECT y FROM tab
   97195                 :   **        UNION ALL
   97196                 :   **        SELECT abs(z*2) FROM tab2
   97197                 :   **     ) WHERE a!=5 ORDER BY 1
   97198                 :   **
   97199                 :   ** Transformed into:
   97200                 :   **
   97201                 :   **     SELECT x+1 FROM tab WHERE x+1!=5
   97202                 :   **     UNION ALL
   97203                 :   **     SELECT y+1 FROM tab WHERE y+1!=5
   97204                 :   **     UNION ALL
   97205                 :   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
   97206                 :   **     ORDER BY 1
   97207                 :   **
   97208                 :   ** We call this the "compound-subquery flattening".
   97209                 :   */
   97210             117 :   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
   97211                 :     Select *pNew;
   97212              54 :     ExprList *pOrderBy = p->pOrderBy;
   97213              54 :     Expr *pLimit = p->pLimit;
   97214              54 :     Select *pPrior = p->pPrior;
   97215              54 :     p->pOrderBy = 0;
   97216              54 :     p->pSrc = 0;
   97217              54 :     p->pPrior = 0;
   97218              54 :     p->pLimit = 0;
   97219              54 :     pNew = sqlite3SelectDup(db, p, 0);
   97220              54 :     p->pLimit = pLimit;
   97221              54 :     p->pOrderBy = pOrderBy;
   97222              54 :     p->pSrc = pSrc;
   97223              54 :     p->op = TK_ALL;
   97224              54 :     p->pRightmost = 0;
   97225              54 :     if( pNew==0 ){
   97226               0 :       pNew = pPrior;
   97227                 :     }else{
   97228              54 :       pNew->pPrior = pPrior;
   97229              54 :       pNew->pRightmost = 0;
   97230                 :     }
   97231              54 :     p->pPrior = pNew;
   97232              54 :     if( db->mallocFailed ) return 1;
   97233                 :   }
   97234                 : 
   97235                 :   /* Begin flattening the iFrom-th entry of the FROM clause 
   97236                 :   ** in the outer query.
   97237                 :   */
   97238              63 :   pSub = pSub1 = pSubitem->pSelect;
   97239                 : 
   97240                 :   /* Delete the transient table structure associated with the
   97241                 :   ** subquery
   97242                 :   */
   97243              63 :   sqlite3DbFree(db, pSubitem->zDatabase);
   97244              63 :   sqlite3DbFree(db, pSubitem->zName);
   97245              63 :   sqlite3DbFree(db, pSubitem->zAlias);
   97246              63 :   pSubitem->zDatabase = 0;
   97247              63 :   pSubitem->zName = 0;
   97248              63 :   pSubitem->zAlias = 0;
   97249              63 :   pSubitem->pSelect = 0;
   97250                 : 
   97251                 :   /* Defer deleting the Table object associated with the
   97252                 :   ** subquery until code generation is
   97253                 :   ** complete, since there may still exist Expr.pTab entries that
   97254                 :   ** refer to the subquery even after flattening.  Ticket #3346.
   97255                 :   **
   97256                 :   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
   97257                 :   */
   97258              63 :   if( ALWAYS(pSubitem->pTab!=0) ){
   97259              63 :     Table *pTabToDel = pSubitem->pTab;
   97260              63 :     if( pTabToDel->nRef==1 ){
   97261              63 :       Parse *pToplevel = sqlite3ParseToplevel(pParse);
   97262              63 :       pTabToDel->pNextZombie = pToplevel->pZombieTab;
   97263              63 :       pToplevel->pZombieTab = pTabToDel;
   97264                 :     }else{
   97265               0 :       pTabToDel->nRef--;
   97266                 :     }
   97267              63 :     pSubitem->pTab = 0;
   97268                 :   }
   97269                 : 
   97270                 :   /* The following loop runs once for each term in a compound-subquery
   97271                 :   ** flattening (as described above).  If we are doing a different kind
   97272                 :   ** of flattening - a flattening other than a compound-subquery flattening -
   97273                 :   ** then this loop only runs once.
   97274                 :   **
   97275                 :   ** This loop moves all of the FROM elements of the subquery into the
   97276                 :   ** the FROM clause of the outer query.  Before doing this, remember
   97277                 :   ** the cursor number for the original outer query FROM element in
   97278                 :   ** iParent.  The iParent cursor will never be used.  Subsequent code
   97279                 :   ** will scan expressions looking for iParent references and replace
   97280                 :   ** those references with expressions that resolve to the subquery FROM
   97281                 :   ** elements we are now copying in.
   97282                 :   */
   97283             180 :   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
   97284                 :     int nSubSrc;
   97285             117 :     u8 jointype = 0;
   97286             117 :     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
   97287             117 :     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
   97288             117 :     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
   97289                 : 
   97290             117 :     if( pSrc ){
   97291              63 :       assert( pParent==p );  /* First time through the loop */
   97292              63 :       jointype = pSubitem->jointype;
   97293                 :     }else{
   97294              54 :       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
   97295              54 :       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
   97296              54 :       if( pSrc==0 ){
   97297               0 :         assert( db->mallocFailed );
   97298               0 :         break;
   97299                 :       }
   97300                 :     }
   97301                 : 
   97302                 :     /* The subquery uses a single slot of the FROM clause of the outer
   97303                 :     ** query.  If the subquery has more than one element in its FROM clause,
   97304                 :     ** then expand the outer query to make space for it to hold all elements
   97305                 :     ** of the subquery.
   97306                 :     **
   97307                 :     ** Example:
   97308                 :     **
   97309                 :     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
   97310                 :     **
   97311                 :     ** The outer query has 3 slots in its FROM clause.  One slot of the
   97312                 :     ** outer query (the middle slot) is used by the subquery.  The next
   97313                 :     ** block of code will expand the out query to 4 slots.  The middle
   97314                 :     ** slot is expanded to two slots in order to make space for the
   97315                 :     ** two elements in the FROM clause of the subquery.
   97316                 :     */
   97317             117 :     if( nSubSrc>1 ){
   97318               0 :       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
   97319               0 :       if( db->mallocFailed ){
   97320               0 :         break;
   97321                 :       }
   97322                 :     }
   97323                 : 
   97324                 :     /* Transfer the FROM clause terms from the subquery into the
   97325                 :     ** outer query.
   97326                 :     */
   97327             234 :     for(i=0; i<nSubSrc; i++){
   97328             117 :       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
   97329             117 :       pSrc->a[i+iFrom] = pSubSrc->a[i];
   97330             117 :       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
   97331                 :     }
   97332             117 :     pSrc->a[iFrom].jointype = jointype;
   97333                 :   
   97334                 :     /* Now begin substituting subquery result set expressions for 
   97335                 :     ** references to the iParent in the outer query.
   97336                 :     ** 
   97337                 :     ** Example:
   97338                 :     **
   97339                 :     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
   97340                 :     **   \                     \_____________ subquery __________/          /
   97341                 :     **    \_____________________ outer query ______________________________/
   97342                 :     **
   97343                 :     ** We look at every expression in the outer query and every place we see
   97344                 :     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
   97345                 :     */
   97346             117 :     pList = pParent->pEList;
   97347             754 :     for(i=0; i<pList->nExpr; i++){
   97348             637 :       if( pList->a[i].zName==0 ){
   97349              84 :         const char *zSpan = pList->a[i].zSpan;
   97350              84 :         if( ALWAYS(zSpan) ){
   97351              84 :           pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
   97352                 :         }
   97353                 :       }
   97354                 :     }
   97355             117 :     substExprList(db, pParent->pEList, iParent, pSub->pEList);
   97356             117 :     if( isAgg ){
   97357               0 :       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
   97358               0 :       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
   97359                 :     }
   97360             117 :     if( pSub->pOrderBy ){
   97361               1 :       assert( pParent->pOrderBy==0 );
   97362               1 :       pParent->pOrderBy = pSub->pOrderBy;
   97363               1 :       pSub->pOrderBy = 0;
   97364             116 :     }else if( pParent->pOrderBy ){
   97365               6 :       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
   97366                 :     }
   97367             117 :     if( pSub->pWhere ){
   97368              62 :       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
   97369                 :     }else{
   97370              55 :       pWhere = 0;
   97371                 :     }
   97372             117 :     if( subqueryIsAgg ){
   97373               1 :       assert( pParent->pHaving==0 );
   97374               1 :       pParent->pHaving = pParent->pWhere;
   97375               1 :       pParent->pWhere = pWhere;
   97376               1 :       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
   97377               1 :       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
   97378                 :                                   sqlite3ExprDup(db, pSub->pHaving, 0));
   97379               1 :       assert( pParent->pGroupBy==0 );
   97380               1 :       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
   97381                 :     }else{
   97382             116 :       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
   97383             116 :       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
   97384                 :     }
   97385                 :   
   97386                 :     /* The flattened query is distinct if either the inner or the
   97387                 :     ** outer query is distinct. 
   97388                 :     */
   97389             117 :     pParent->selFlags |= pSub->selFlags & SF_Distinct;
   97390                 :   
   97391                 :     /*
   97392                 :     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
   97393                 :     **
   97394                 :     ** One is tempted to try to add a and b to combine the limits.  But this
   97395                 :     ** does not work if either limit is negative.
   97396                 :     */
   97397             117 :     if( pSub->pLimit ){
   97398               1 :       pParent->pLimit = pSub->pLimit;
   97399               1 :       pSub->pLimit = 0;
   97400                 :     }
   97401                 :   }
   97402                 : 
   97403                 :   /* Finially, delete what is left of the subquery and return
   97404                 :   ** success.
   97405                 :   */
   97406              63 :   sqlite3SelectDelete(db, pSub1);
   97407                 : 
   97408              63 :   return 1;
   97409                 : }
   97410                 : #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
   97411                 : 
   97412                 : /*
   97413                 : ** Analyze the SELECT statement passed as an argument to see if it
   97414                 : ** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if 
   97415                 : ** it is, or 0 otherwise. At present, a query is considered to be
   97416                 : ** a min()/max() query if:
   97417                 : **
   97418                 : **   1. There is a single object in the FROM clause.
   97419                 : **
   97420                 : **   2. There is a single expression in the result set, and it is
   97421                 : **      either min(x) or max(x), where x is a column reference.
   97422                 : */
   97423            3705 : static u8 minMaxQuery(Select *p){
   97424                 :   Expr *pExpr;
   97425            3705 :   ExprList *pEList = p->pEList;
   97426                 : 
   97427            3705 :   if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
   97428            3517 :   pExpr = pEList->a[0].pExpr;
   97429            3517 :   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
   97430            3515 :   if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
   97431            3515 :   pEList = pExpr->x.pList;
   97432            3515 :   if( pEList==0 || pEList->nExpr!=1 ) return 0;
   97433            2778 :   if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
   97434            2062 :   assert( !ExprHasProperty(pExpr, EP_IntValue) );
   97435            2062 :   if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){
   97436            1326 :     return WHERE_ORDERBY_MIN;
   97437             736 :   }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){
   97438             434 :     return WHERE_ORDERBY_MAX;
   97439                 :   }
   97440             302 :   return WHERE_ORDERBY_NORMAL;
   97441                 : }
   97442                 : 
   97443                 : /*
   97444                 : ** The select statement passed as the first argument is an aggregate query.
   97445                 : ** The second argment is the associated aggregate-info object. This 
   97446                 : ** function tests if the SELECT is of the form:
   97447                 : **
   97448                 : **   SELECT count(*) FROM <tbl>
   97449                 : **
   97450                 : ** where table is a database table, not a sub-select or view. If the query
   97451                 : ** does match this pattern, then a pointer to the Table object representing
   97452                 : ** <tbl> is returned. Otherwise, 0 is returned.
   97453                 : */
   97454            4337 : static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
   97455                 :   Table *pTab;
   97456                 :   Expr *pExpr;
   97457                 : 
   97458            4337 :   assert( !p->pGroupBy );
   97459                 : 
   97460            4337 :   if( p->pWhere || p->pEList->nExpr!=1 
   97461            2596 :    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
   97462                 :   ){
   97463            1754 :     return 0;
   97464                 :   }
   97465            2583 :   pTab = p->pSrc->a[0].pTab;
   97466            2583 :   pExpr = p->pEList->a[0].pExpr;
   97467            2583 :   assert( pTab && !pTab->pSelect && pExpr );
   97468                 : 
   97469            2583 :   if( IsVirtual(pTab) ) return 0;
   97470            2582 :   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
   97471            2580 :   if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0;
   97472             632 :   if( pExpr->flags&EP_Distinct ) return 0;
   97473                 : 
   97474             632 :   return pTab;
   97475                 : }
   97476                 : 
   97477                 : /*
   97478                 : ** If the source-list item passed as an argument was augmented with an
   97479                 : ** INDEXED BY clause, then try to locate the specified index. If there
   97480                 : ** was such a clause and the named index cannot be found, return 
   97481                 : ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
   97482                 : ** pFrom->pIndex and return SQLITE_OK.
   97483                 : */
   97484          133876 : SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
   97485          133876 :   if( pFrom->pTab && pFrom->zIndex ){
   97486               0 :     Table *pTab = pFrom->pTab;
   97487               0 :     char *zIndex = pFrom->zIndex;
   97488                 :     Index *pIdx;
   97489               0 :     for(pIdx=pTab->pIndex; 
   97490               0 :         pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
   97491               0 :         pIdx=pIdx->pNext
   97492                 :     );
   97493               0 :     if( !pIdx ){
   97494               0 :       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
   97495               0 :       pParse->checkSchema = 1;
   97496               0 :       return SQLITE_ERROR;
   97497                 :     }
   97498               0 :     pFrom->pIndex = pIdx;
   97499                 :   }
   97500          133876 :   return SQLITE_OK;
   97501                 : }
   97502                 : 
   97503                 : /*
   97504                 : ** This routine is a Walker callback for "expanding" a SELECT statement.
   97505                 : ** "Expanding" means to do the following:
   97506                 : **
   97507                 : **    (1)  Make sure VDBE cursor numbers have been assigned to every
   97508                 : **         element of the FROM clause.
   97509                 : **
   97510                 : **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that 
   97511                 : **         defines FROM clause.  When views appear in the FROM clause,
   97512                 : **         fill pTabList->a[].pSelect with a copy of the SELECT statement
   97513                 : **         that implements the view.  A copy is made of the view's SELECT
   97514                 : **         statement so that we can freely modify or delete that statement
   97515                 : **         without worrying about messing up the presistent representation
   97516                 : **         of the view.
   97517                 : **
   97518                 : **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
   97519                 : **         on joins and the ON and USING clause of joins.
   97520                 : **
   97521                 : **    (4)  Scan the list of columns in the result set (pEList) looking
   97522                 : **         for instances of the "*" operator or the TABLE.* operator.
   97523                 : **         If found, expand each "*" to be every column in every table
   97524                 : **         and TABLE.* to be every column in TABLE.
   97525                 : **
   97526                 : */
   97527           60208 : static int selectExpander(Walker *pWalker, Select *p){
   97528           60208 :   Parse *pParse = pWalker->pParse;
   97529                 :   int i, j, k;
   97530                 :   SrcList *pTabList;
   97531                 :   ExprList *pEList;
   97532                 :   struct SrcList_item *pFrom;
   97533           60208 :   sqlite3 *db = pParse->db;
   97534                 : 
   97535           60208 :   if( db->mallocFailed  ){
   97536               0 :     return WRC_Abort;
   97537                 :   }
   97538           60208 :   if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){
   97539             122 :     return WRC_Prune;
   97540                 :   }
   97541           60086 :   p->selFlags |= SF_Expanded;
   97542           60086 :   pTabList = p->pSrc;
   97543           60086 :   pEList = p->pEList;
   97544                 : 
   97545                 :   /* Make sure cursor numbers have been assigned to all entries in
   97546                 :   ** the FROM clause of the SELECT statement.
   97547                 :   */
   97548           60086 :   sqlite3SrcListAssignCursors(pParse, pTabList);
   97549                 : 
   97550                 :   /* Look up every table named in the FROM clause of the select.  If
   97551                 :   ** an entry of the FROM clause is a subquery instead of a table or view,
   97552                 :   ** then create a transient table structure to describe the subquery.
   97553                 :   */
   97554          123833 :   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
   97555                 :     Table *pTab;
   97556           63749 :     if( pFrom->pTab!=0 ){
   97557                 :       /* This statement has already been prepared.  There is no need
   97558                 :       ** to go further. */
   97559               0 :       assert( i==0 );
   97560               0 :       return WRC_Prune;
   97561                 :     }
   97562           63749 :     if( pFrom->zName==0 ){
   97563                 : #ifndef SQLITE_OMIT_SUBQUERY
   97564             122 :       Select *pSel = pFrom->pSelect;
   97565                 :       /* A sub-query in the FROM clause of a SELECT */
   97566             122 :       assert( pSel!=0 );
   97567             122 :       assert( pFrom->pTab==0 );
   97568             122 :       sqlite3WalkSelect(pWalker, pSel);
   97569             122 :       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
   97570             122 :       if( pTab==0 ) return WRC_Abort;
   97571             122 :       pTab->nRef = 1;
   97572             122 :       pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
   97573             122 :       while( pSel->pPrior ){ pSel = pSel->pPrior; }
   97574             122 :       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
   97575             122 :       pTab->iPKey = -1;
   97576             122 :       pTab->nRowEst = 1000000;
   97577             122 :       pTab->tabFlags |= TF_Ephemeral;
   97578                 : #endif
   97579                 :     }else{
   97580                 :       /* An ordinary table or view name in the FROM clause */
   97581           63627 :       assert( pFrom->pTab==0 );
   97582           63627 :       pFrom->pTab = pTab = 
   97583           63627 :         sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase);
   97584           63627 :       if( pTab==0 ) return WRC_Abort;
   97585           63625 :       pTab->nRef++;
   97586                 : #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
   97587           63625 :       if( pTab->pSelect || IsVirtual(pTab) ){
   97588                 :         /* We reach here if the named table is a really a view */
   97589              54 :         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
   97590              54 :         assert( pFrom->pSelect==0 );
   97591              54 :         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
   97592              54 :         sqlite3WalkSelect(pWalker, pFrom->pSelect);
   97593                 :       }
   97594                 : #endif
   97595                 :     }
   97596                 : 
   97597                 :     /* Locate the index named by the INDEXED BY clause, if any. */
   97598           63747 :     if( sqlite3IndexedByLookup(pParse, pFrom) ){
   97599               0 :       return WRC_Abort;
   97600                 :     }
   97601                 :   }
   97602                 : 
   97603                 :   /* Process NATURAL keywords, and ON and USING clauses of joins.
   97604                 :   */
   97605           60084 :   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
   97606               0 :     return WRC_Abort;
   97607                 :   }
   97608                 : 
   97609                 :   /* For every "*" that occurs in the column list, insert the names of
   97610                 :   ** all columns in all tables.  And for every TABLE.* insert the names
   97611                 :   ** of all columns in TABLE.  The parser inserted a special expression
   97612                 :   ** with the TK_ALL operator for each "*" that it found in the column list.
   97613                 :   ** The following code just has to locate the TK_ALL expressions and expand
   97614                 :   ** each one to the list of all columns in all tables.
   97615                 :   **
   97616                 :   ** The first loop just checks to see if there are any "*" operators
   97617                 :   ** that need expanding.
   97618                 :   */
   97619          341775 :   for(k=0; k<pEList->nExpr; k++){
   97620          284257 :     Expr *pE = pEList->a[k].pExpr;
   97621          284257 :     if( pE->op==TK_ALL ) break;
   97622          281691 :     assert( pE->op!=TK_DOT || pE->pRight!=0 );
   97623          281691 :     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
   97624          281691 :     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
   97625                 :   }
   97626           60084 :   if( k<pEList->nExpr ){
   97627                 :     /*
   97628                 :     ** If we get here it means the result set contains one or more "*"
   97629                 :     ** operators that need to be expanded.  Loop through each expression
   97630                 :     ** in the result set and expand them one by one.
   97631                 :     */
   97632            2566 :     struct ExprList_item *a = pEList->a;
   97633            2566 :     ExprList *pNew = 0;
   97634            2566 :     int flags = pParse->db->flags;
   97635            2566 :     int longNames = (flags & SQLITE_FullColNames)!=0
   97636               0 :                       && (flags & SQLITE_ShortColNames)==0;
   97637                 : 
   97638            5132 :     for(k=0; k<pEList->nExpr; k++){
   97639            2566 :       Expr *pE = a[k].pExpr;
   97640            2566 :       assert( pE->op!=TK_DOT || pE->pRight!=0 );
   97641            2566 :       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
   97642                 :         /* This particular expression does not need to be expanded.
   97643                 :         */
   97644               0 :         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
   97645               0 :         if( pNew ){
   97646               0 :           pNew->a[pNew->nExpr-1].zName = a[k].zName;
   97647               0 :           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
   97648               0 :           a[k].zName = 0;
   97649               0 :           a[k].zSpan = 0;
   97650                 :         }
   97651               0 :         a[k].pExpr = 0;
   97652                 :       }else{
   97653                 :         /* This expression is a "*" or a "TABLE.*" and needs to be
   97654                 :         ** expanded. */
   97655            2566 :         int tableSeen = 0;      /* Set to 1 when TABLE matches */
   97656                 :         char *zTName;            /* text of name of TABLE */
   97657            2566 :         if( pE->op==TK_DOT ){
   97658               0 :           assert( pE->pLeft!=0 );
   97659               0 :           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
   97660               0 :           zTName = pE->pLeft->u.zToken;
   97661                 :         }else{
   97662            2566 :           zTName = 0;
   97663                 :         }
   97664            5132 :         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
   97665            2566 :           Table *pTab = pFrom->pTab;
   97666            2566 :           char *zTabName = pFrom->zAlias;
   97667            2566 :           if( zTabName==0 ){
   97668            2512 :             zTabName = pTab->zName;
   97669                 :           }
   97670            2566 :           if( db->mallocFailed ) break;
   97671            2566 :           if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
   97672               0 :             continue;
   97673                 :           }
   97674            2566 :           tableSeen = 1;
   97675           19873 :           for(j=0; j<pTab->nCol; j++){
   97676                 :             Expr *pExpr, *pRight;
   97677           17307 :             char *zName = pTab->aCol[j].zName;
   97678                 :             char *zColname;  /* The computed column name */
   97679                 :             char *zToFree;   /* Malloced string that needs to be freed */
   97680                 :             Token sColname;  /* Computed column name as a token */
   97681                 : 
   97682                 :             /* If a column is marked as 'hidden' (currently only possible
   97683                 :             ** for virtual tables), do not include it in the expanded
   97684                 :             ** result-set list.
   97685                 :             */
   97686           17307 :             if( IsHiddenColumn(&pTab->aCol[j]) ){
   97687               0 :               assert(IsVirtual(pTab));
   97688               0 :               continue;
   97689                 :             }
   97690                 : 
   97691           17307 :             if( i>0 && zTName==0 ){
   97692               0 :               if( (pFrom->jointype & JT_NATURAL)!=0
   97693               0 :                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
   97694                 :               ){
   97695                 :                 /* In a NATURAL join, omit the join columns from the 
   97696                 :                 ** table to the right of the join */
   97697               0 :                 continue;
   97698                 :               }
   97699               0 :               if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
   97700                 :                 /* In a join with a USING clause, omit columns in the
   97701                 :                 ** using clause from the table on the right. */
   97702               0 :                 continue;
   97703                 :               }
   97704                 :             }
   97705           17307 :             pRight = sqlite3Expr(db, TK_ID, zName);
   97706           17307 :             zColname = zName;
   97707           17307 :             zToFree = 0;
   97708           17307 :             if( longNames || pTabList->nSrc>1 ){
   97709                 :               Expr *pLeft;
   97710               0 :               pLeft = sqlite3Expr(db, TK_ID, zTabName);
   97711               0 :               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
   97712               0 :               if( longNames ){
   97713               0 :                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
   97714               0 :                 zToFree = zColname;
   97715                 :               }
   97716                 :             }else{
   97717           17307 :               pExpr = pRight;
   97718                 :             }
   97719           17307 :             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
   97720           17307 :             sColname.z = zColname;
   97721           17307 :             sColname.n = sqlite3Strlen30(zColname);
   97722           17307 :             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
   97723           17307 :             sqlite3DbFree(db, zToFree);
   97724                 :           }
   97725                 :         }
   97726            2566 :         if( !tableSeen ){
   97727               0 :           if( zTName ){
   97728               0 :             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
   97729                 :           }else{
   97730               0 :             sqlite3ErrorMsg(pParse, "no tables specified");
   97731                 :           }
   97732                 :         }
   97733                 :       }
   97734                 :     }
   97735            2566 :     sqlite3ExprListDelete(db, pEList);
   97736            2566 :     p->pEList = pNew;
   97737                 :   }
   97738                 : #if SQLITE_MAX_COLUMN
   97739           60084 :   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
   97740               0 :     sqlite3ErrorMsg(pParse, "too many columns in result set");
   97741                 :   }
   97742                 : #endif
   97743           60084 :   return WRC_Continue;
   97744                 : }
   97745                 : 
   97746                 : /*
   97747                 : ** No-op routine for the parse-tree walker.
   97748                 : **
   97749                 : ** When this routine is the Walker.xExprCallback then expression trees
   97750                 : ** are walked without any actions being taken at each node.  Presumably,
   97751                 : ** when this routine is used for Walker.xExprCallback then 
   97752                 : ** Walker.xSelectCallback is set to do something useful for every 
   97753                 : ** subquery in the parser tree.
   97754                 : */
   97755         1271354 : static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
   97756                 :   UNUSED_PARAMETER2(NotUsed, NotUsed2);
   97757         1271354 :   return WRC_Continue;
   97758                 : }
   97759                 : 
   97760                 : /*
   97761                 : ** This routine "expands" a SELECT statement and all of its subqueries.
   97762                 : ** For additional information on what it means to "expand" a SELECT
   97763                 : ** statement, see the comment on the selectExpand worker callback above.
   97764                 : **
   97765                 : ** Expanding a SELECT statement is the first step in processing a
   97766                 : ** SELECT statement.  The SELECT statement must be expanded before
   97767                 : ** name resolution is performed.
   97768                 : **
   97769                 : ** If anything goes wrong, an error message is written into pParse.
   97770                 : ** The calling function can detect the problem by looking at pParse->nErr
   97771                 : ** and/or pParse->db->mallocFailed.
   97772                 : */
   97773           54114 : static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
   97774                 :   Walker w;
   97775           54114 :   w.xSelectCallback = selectExpander;
   97776           54114 :   w.xExprCallback = exprWalkNoop;
   97777           54114 :   w.pParse = pParse;
   97778           54114 :   sqlite3WalkSelect(&w, pSelect);
   97779           54114 : }
   97780                 : 
   97781                 : 
   97782                 : #ifndef SQLITE_OMIT_SUBQUERY
   97783                 : /*
   97784                 : ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
   97785                 : ** interface.
   97786                 : **
   97787                 : ** For each FROM-clause subquery, add Column.zType and Column.zColl
   97788                 : ** information to the Table structure that represents the result set
   97789                 : ** of that subquery.
   97790                 : **
   97791                 : ** The Table structure that represents the result set was constructed
   97792                 : ** by selectExpander() but the type and collation information was omitted
   97793                 : ** at that point because identifiers had not yet been resolved.  This
   97794                 : ** routine is called after identifier resolution.
   97795                 : */
   97796           60369 : static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
   97797                 :   Parse *pParse;
   97798                 :   int i;
   97799                 :   SrcList *pTabList;
   97800                 :   struct SrcList_item *pFrom;
   97801                 : 
   97802           60369 :   assert( p->selFlags & SF_Resolved );
   97803           60369 :   if( (p->selFlags & SF_HasTypeInfo)==0 ){
   97804           60369 :     p->selFlags |= SF_HasTypeInfo;
   97805           60369 :     pParse = pWalker->pParse;
   97806           60369 :     pTabList = p->pSrc;
   97807          124478 :     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
   97808           64109 :       Table *pTab = pFrom->pTab;
   97809           64109 :       if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
   97810                 :         /* A sub-query in the FROM clause of a SELECT */
   97811             122 :         Select *pSel = pFrom->pSelect;
   97812             122 :         assert( pSel );
   97813             122 :         while( pSel->pPrior ) pSel = pSel->pPrior;
   97814             122 :         selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
   97815                 :       }
   97816                 :     }
   97817                 :   }
   97818           60369 :   return WRC_Continue;
   97819                 : }
   97820                 : #endif
   97821                 : 
   97822                 : 
   97823                 : /*
   97824                 : ** This routine adds datatype and collating sequence information to
   97825                 : ** the Table structures of all FROM-clause subqueries in a
   97826                 : ** SELECT statement.
   97827                 : **
   97828                 : ** Use this routine after name resolution.
   97829                 : */
   97830           54063 : static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
   97831                 : #ifndef SQLITE_OMIT_SUBQUERY
   97832                 :   Walker w;
   97833           54063 :   w.xSelectCallback = selectAddSubqueryTypeInfo;
   97834           54063 :   w.xExprCallback = exprWalkNoop;
   97835           54063 :   w.pParse = pParse;
   97836           54063 :   sqlite3WalkSelect(&w, pSelect);
   97837                 : #endif
   97838           54063 : }
   97839                 : 
   97840                 : 
   97841                 : /*
   97842                 : ** This routine sets of a SELECT statement for processing.  The
   97843                 : ** following is accomplished:
   97844                 : **
   97845                 : **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
   97846                 : **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
   97847                 : **     *  ON and USING clauses are shifted into WHERE statements
   97848                 : **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
   97849                 : **     *  Identifiers in expression are matched to tables.
   97850                 : **
   97851                 : ** This routine acts recursively on all subqueries within the SELECT.
   97852                 : */
   97853           68024 : SQLITE_PRIVATE void sqlite3SelectPrep(
   97854                 :   Parse *pParse,         /* The parser context */
   97855                 :   Select *p,             /* The SELECT statement being coded. */
   97856                 :   NameContext *pOuterNC  /* Name context for container */
   97857                 : ){
   97858                 :   sqlite3 *db;
   97859           68024 :   if( NEVER(p==0) ) return;
   97860           68024 :   db = pParse->db;
   97861           68024 :   if( p->selFlags & SF_HasTypeInfo ) return;
   97862           54114 :   sqlite3SelectExpand(pParse, p);
   97863           54114 :   if( pParse->nErr || db->mallocFailed ) return;
   97864           54112 :   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
   97865           54112 :   if( pParse->nErr || db->mallocFailed ) return;
   97866           54063 :   sqlite3SelectAddTypeInfo(pParse, p);
   97867                 : }
   97868                 : 
   97869                 : /*
   97870                 : ** Reset the aggregate accumulator.
   97871                 : **
   97872                 : ** The aggregate accumulator is a set of memory cells that hold
   97873                 : ** intermediate results while calculating an aggregate.  This
   97874                 : ** routine simply stores NULLs in all of those memory cells.
   97875                 : */
   97876            3810 : static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
   97877            3810 :   Vdbe *v = pParse->pVdbe;
   97878                 :   int i;
   97879                 :   struct AggInfo_func *pFunc;
   97880            3810 :   if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){
   97881               0 :     return;
   97882                 :   }
   97883            6322 :   for(i=0; i<pAggInfo->nColumn; i++){
   97884            2512 :     sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem);
   97885                 :   }
   97886            7646 :   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
   97887            3836 :     sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem);
   97888            3836 :     if( pFunc->iDistinct>=0 ){
   97889              27 :       Expr *pE = pFunc->pExpr;
   97890              27 :       assert( !ExprHasProperty(pE, EP_xIsSelect) );
   97891              27 :       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
   97892               0 :         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
   97893                 :            "argument");
   97894               0 :         pFunc->iDistinct = -1;
   97895                 :       }else{
   97896              27 :         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList);
   97897              27 :         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
   97898                 :                           (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
   97899                 :       }
   97900                 :     }
   97901                 :   }
   97902                 : }
   97903                 : 
   97904                 : /*
   97905                 : ** Invoke the OP_AggFinalize opcode for every aggregate function
   97906                 : ** in the AggInfo structure.
   97907                 : */
   97908            3810 : static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
   97909            3810 :   Vdbe *v = pParse->pVdbe;
   97910                 :   int i;
   97911                 :   struct AggInfo_func *pF;
   97912            7646 :   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
   97913            3836 :     ExprList *pList = pF->pExpr->x.pList;
   97914            3836 :     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
   97915            3836 :     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
   97916            3836 :                       (void*)pF->pFunc, P4_FUNCDEF);
   97917                 :   }
   97918            3810 : }
   97919                 : 
   97920                 : /*
   97921                 : ** Update the accumulator memory cells for an aggregate based on
   97922                 : ** the current cursor position.
   97923                 : */
   97924            3810 : static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
   97925            3810 :   Vdbe *v = pParse->pVdbe;
   97926                 :   int i;
   97927                 :   struct AggInfo_func *pF;
   97928                 :   struct AggInfo_col *pC;
   97929                 : 
   97930            3810 :   pAggInfo->directMode = 1;
   97931            3810 :   sqlite3ExprCacheClear(pParse);
   97932            7646 :   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
   97933                 :     int nArg;
   97934            3836 :     int addrNext = 0;
   97935                 :     int regAgg;
   97936            3836 :     ExprList *pList = pF->pExpr->x.pList;
   97937            3836 :     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
   97938            3836 :     if( pList ){
   97939            3039 :       nArg = pList->nExpr;
   97940            3039 :       regAgg = sqlite3GetTempRange(pParse, nArg);
   97941            3039 :       sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
   97942                 :     }else{
   97943             797 :       nArg = 0;
   97944             797 :       regAgg = 0;
   97945                 :     }
   97946            3836 :     if( pF->iDistinct>=0 ){
   97947              27 :       addrNext = sqlite3VdbeMakeLabel(v);
   97948              27 :       assert( nArg==1 );
   97949              27 :       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
   97950                 :     }
   97951            3836 :     if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
   97952            1813 :       CollSeq *pColl = 0;
   97953                 :       struct ExprList_item *pItem;
   97954                 :       int j;
   97955            1813 :       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
   97956            3626 :       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
   97957            1813 :         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
   97958                 :       }
   97959            1813 :       if( !pColl ){
   97960              43 :         pColl = pParse->db->pDfltColl;
   97961                 :       }
   97962            1813 :       sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
   97963                 :     }
   97964            3836 :     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
   97965            3836 :                       (void*)pF->pFunc, P4_FUNCDEF);
   97966            3836 :     sqlite3VdbeChangeP5(v, (u8)nArg);
   97967            3836 :     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
   97968            3836 :     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
   97969            3836 :     if( addrNext ){
   97970              27 :       sqlite3VdbeResolveLabel(v, addrNext);
   97971              27 :       sqlite3ExprCacheClear(pParse);
   97972                 :     }
   97973                 :   }
   97974                 : 
   97975                 :   /* Before populating the accumulator registers, clear the column cache.
   97976                 :   ** Otherwise, if any of the required column values are already present 
   97977                 :   ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
   97978                 :   ** to pC->iMem. But by the time the value is used, the original register
   97979                 :   ** may have been used, invalidating the underlying buffer holding the
   97980                 :   ** text or blob value. See ticket [883034dcb5].
   97981                 :   **
   97982                 :   ** Another solution would be to change the OP_SCopy used to copy cached
   97983                 :   ** values to an OP_Copy.
   97984                 :   */
   97985            3810 :   sqlite3ExprCacheClear(pParse);
   97986            3969 :   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
   97987             159 :     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
   97988                 :   }
   97989            3810 :   pAggInfo->directMode = 0;
   97990            3810 :   sqlite3ExprCacheClear(pParse);
   97991            3810 : }
   97992                 : 
   97993                 : /*
   97994                 : ** Add a single OP_Explain instruction to the VDBE to explain a simple
   97995                 : ** count(*) query ("SELECT count(*) FROM pTab").
   97996                 : */
   97997                 : #ifndef SQLITE_OMIT_EXPLAIN
   97998             632 : static void explainSimpleCount(
   97999                 :   Parse *pParse,                  /* Parse context */
   98000                 :   Table *pTab,                    /* Table being queried */
   98001                 :   Index *pIdx                     /* Index used to optimize scan, or NULL */
   98002                 : ){
   98003             632 :   if( pParse->explain==2 ){
   98004               0 :     char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
   98005                 :         pTab->zName, 
   98006                 :         pIdx ? "USING COVERING INDEX " : "",
   98007                 :         pIdx ? pIdx->zName : "",
   98008                 :         pTab->nRowEst
   98009                 :     );
   98010               0 :     sqlite3VdbeAddOp4(
   98011                 :         pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
   98012                 :     );
   98013                 :   }
   98014             632 : }
   98015                 : #else
   98016                 : # define explainSimpleCount(a,b,c)
   98017                 : #endif
   98018                 : 
   98019                 : /*
   98020                 : ** Generate code for the SELECT statement given in the p argument.  
   98021                 : **
   98022                 : ** The results are distributed in various ways depending on the
   98023                 : ** contents of the SelectDest structure pointed to by argument pDest
   98024                 : ** as follows:
   98025                 : **
   98026                 : **     pDest->eDest    Result
   98027                 : **     ------------    -------------------------------------------
   98028                 : **     SRT_Output      Generate a row of output (using the OP_ResultRow
   98029                 : **                     opcode) for each row in the result set.
   98030                 : **
   98031                 : **     SRT_Mem         Only valid if the result is a single column.
   98032                 : **                     Store the first column of the first result row
   98033                 : **                     in register pDest->iParm then abandon the rest
   98034                 : **                     of the query.  This destination implies "LIMIT 1".
   98035                 : **
   98036                 : **     SRT_Set         The result must be a single column.  Store each
   98037                 : **                     row of result as the key in table pDest->iParm. 
   98038                 : **                     Apply the affinity pDest->affinity before storing
   98039                 : **                     results.  Used to implement "IN (SELECT ...)".
   98040                 : **
   98041                 : **     SRT_Union       Store results as a key in a temporary table pDest->iParm.
   98042                 : **
   98043                 : **     SRT_Except      Remove results from the temporary table pDest->iParm.
   98044                 : **
   98045                 : **     SRT_Table       Store results in temporary table pDest->iParm.
   98046                 : **                     This is like SRT_EphemTab except that the table
   98047                 : **                     is assumed to already be open.
   98048                 : **
   98049                 : **     SRT_EphemTab    Create an temporary table pDest->iParm and store
   98050                 : **                     the result there. The cursor is left open after
   98051                 : **                     returning.  This is like SRT_Table except that
   98052                 : **                     this destination uses OP_OpenEphemeral to create
   98053                 : **                     the table first.
   98054                 : **
   98055                 : **     SRT_Coroutine   Generate a co-routine that returns a new row of
   98056                 : **                     results each time it is invoked.  The entry point
   98057                 : **                     of the co-routine is stored in register pDest->iParm.
   98058                 : **
   98059                 : **     SRT_Exists      Store a 1 in memory cell pDest->iParm if the result
   98060                 : **                     set is not empty.
   98061                 : **
   98062                 : **     SRT_Discard     Throw the results away.  This is used by SELECT
   98063                 : **                     statements within triggers whose only purpose is
   98064                 : **                     the side-effects of functions.
   98065                 : **
   98066                 : ** This routine returns the number of errors.  If any errors are
   98067                 : ** encountered, then an appropriate error message is left in
   98068                 : ** pParse->zErrMsg.
   98069                 : **
   98070                 : ** This routine does NOT free the Select structure passed in.  The
   98071                 : ** calling function needs to do that.
   98072                 : */
   98073           60222 : SQLITE_PRIVATE int sqlite3Select(
   98074                 :   Parse *pParse,         /* The parser context */
   98075                 :   Select *p,             /* The SELECT statement being coded. */
   98076                 :   SelectDest *pDest      /* What to do with the query results */
   98077                 : ){
   98078                 :   int i, j;              /* Loop counters */
   98079                 :   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
   98080                 :   Vdbe *v;               /* The virtual machine under construction */
   98081                 :   int isAgg;             /* True for select lists like "count(*)" */
   98082                 :   ExprList *pEList;      /* List of columns to extract. */
   98083                 :   SrcList *pTabList;     /* List of tables to select from */
   98084                 :   Expr *pWhere;          /* The WHERE clause.  May be NULL */
   98085                 :   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
   98086                 :   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
   98087                 :   Expr *pHaving;         /* The HAVING clause.  May be NULL */
   98088                 :   int isDistinct;        /* True if the DISTINCT keyword is present */
   98089                 :   int distinct;          /* Table to use for the distinct set */
   98090           60222 :   int rc = 1;            /* Value to return from this function */
   98091                 :   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
   98092                 :   int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
   98093                 :   AggInfo sAggInfo;      /* Information used by aggregate queries */
   98094                 :   int iEnd;              /* Address of the end of the query */
   98095                 :   sqlite3 *db;           /* The database connection */
   98096                 : 
   98097                 : #ifndef SQLITE_OMIT_EXPLAIN
   98098           60222 :   int iRestoreSelectId = pParse->iSelectId;
   98099           60222 :   pParse->iSelectId = pParse->iNextSelectId++;
   98100                 : #endif
   98101                 : 
   98102           60222 :   db = pParse->db;
   98103           60222 :   if( p==0 || db->mallocFailed || pParse->nErr ){
   98104               0 :     return 1;
   98105                 :   }
   98106           60222 :   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
   98107           60222 :   memset(&sAggInfo, 0, sizeof(sAggInfo));
   98108                 : 
   98109           60222 :   if( IgnorableOrderby(pDest) ){
   98110            2159 :     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || 
   98111                 :            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
   98112                 :     /* If ORDER BY makes no difference in the output then neither does
   98113                 :     ** DISTINCT so it can be removed too. */
   98114            2159 :     sqlite3ExprListDelete(db, p->pOrderBy);
   98115            2159 :     p->pOrderBy = 0;
   98116            2159 :     p->selFlags &= ~SF_Distinct;
   98117                 :   }
   98118           60222 :   sqlite3SelectPrep(pParse, p, 0);
   98119           60222 :   pOrderBy = p->pOrderBy;
   98120           60222 :   pTabList = p->pSrc;
   98121           60222 :   pEList = p->pEList;
   98122           60222 :   if( pParse->nErr || db->mallocFailed ){
   98123                 :     goto select_end;
   98124                 :   }
   98125           60171 :   isAgg = (p->selFlags & SF_Aggregate)!=0;
   98126           60171 :   assert( pEList!=0 );
   98127                 : 
   98128                 :   /* Begin generating code.
   98129                 :   */
   98130           60171 :   v = sqlite3GetVdbe(pParse);
   98131           60171 :   if( v==0 ) goto select_end;
   98132                 : 
   98133                 :   /* If writing to memory or generating a set
   98134                 :   ** only a single column may be output.
   98135                 :   */
   98136                 : #ifndef SQLITE_OMIT_SUBQUERY
   98137           60171 :   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
   98138               0 :     goto select_end;
   98139                 :   }
   98140                 : #endif
   98141                 : 
   98142                 :   /* Generate code for all sub-queries in the FROM clause
   98143                 :   */
   98144                 : #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
   98145          123994 :   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
   98146           63823 :     struct SrcList_item *pItem = &pTabList->a[i];
   98147                 :     SelectDest dest;
   98148           63823 :     Select *pSub = pItem->pSelect;
   98149                 :     int isAggSub;
   98150                 : 
   98151           63823 :     if( pSub==0 ) continue;
   98152             122 :     if( pItem->addrFillSub ){
   98153               0 :       sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
   98154               0 :       continue;
   98155                 :     }
   98156                 : 
   98157                 :     /* Increment Parse.nHeight by the height of the largest expression
   98158                 :     ** tree refered to by this, the parent select. The child select
   98159                 :     ** may contain expression trees of at most
   98160                 :     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
   98161                 :     ** more conservative than necessary, but much easier than enforcing
   98162                 :     ** an exact limit.
   98163                 :     */
   98164             122 :     pParse->nHeight += sqlite3SelectExprHeight(p);
   98165                 : 
   98166             122 :     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
   98167             122 :     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
   98168                 :       /* This subquery can be absorbed into its parent. */
   98169              63 :       if( isAggSub ){
   98170               1 :         isAgg = 1;
   98171               1 :         p->selFlags |= SF_Aggregate;
   98172                 :       }
   98173              63 :       i = -1;
   98174                 :     }else{
   98175                 :       /* Generate a subroutine that will fill an ephemeral table with
   98176                 :       ** the content of this subquery.  pItem->addrFillSub will point
   98177                 :       ** to the address of the generated subroutine.  pItem->regReturn
   98178                 :       ** is a register allocated to hold the subroutine return address
   98179                 :       */
   98180                 :       int topAddr;
   98181              59 :       int onceAddr = 0;
   98182                 :       int retAddr;
   98183              59 :       assert( pItem->addrFillSub==0 );
   98184              59 :       pItem->regReturn = ++pParse->nMem;
   98185              59 :       topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
   98186              59 :       pItem->addrFillSub = topAddr+1;
   98187              59 :       VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
   98188              59 :       if( pItem->isCorrelated==0 ){
   98189                 :         /* If the subquery is no correlated and if we are not inside of
   98190                 :         ** a trigger, then we only need to compute the value of the subquery
   98191                 :         ** once. */
   98192              59 :         onceAddr = sqlite3CodeOnce(pParse);
   98193                 :       }
   98194              59 :       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
   98195              59 :       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
   98196              59 :       sqlite3Select(pParse, pSub, &dest);
   98197              59 :       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
   98198              59 :       if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
   98199              59 :       retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
   98200              59 :       VdbeComment((v, "end %s", pItem->pTab->zName));
   98201              59 :       sqlite3VdbeChangeP1(v, topAddr, retAddr);
   98202              59 :       sqlite3ClearTempRegCache(pParse);
   98203                 :     }
   98204             122 :     if( /*pParse->nErr ||*/ db->mallocFailed ){
   98205               0 :       goto select_end;
   98206                 :     }
   98207             122 :     pParse->nHeight -= sqlite3SelectExprHeight(p);
   98208             122 :     pTabList = p->pSrc;
   98209             122 :     if( !IgnorableOrderby(pDest) ){
   98210             122 :       pOrderBy = p->pOrderBy;
   98211                 :     }
   98212                 :   }
   98213           60171 :   pEList = p->pEList;
   98214                 : #endif
   98215           60171 :   pWhere = p->pWhere;
   98216           60171 :   pGroupBy = p->pGroupBy;
   98217           60171 :   pHaving = p->pHaving;
   98218           60171 :   isDistinct = (p->selFlags & SF_Distinct)!=0;
   98219                 : 
   98220                 : #ifndef SQLITE_OMIT_COMPOUND_SELECT
   98221                 :   /* If there is are a sequence of queries, do the earlier ones first.
   98222                 :   */
   98223           60171 :   if( p->pPrior ){
   98224             169 :     if( p->pRightmost==0 ){
   98225             105 :       Select *pLoop, *pRight = 0;
   98226             105 :       int cnt = 0;
   98227                 :       int mxSelect;
   98228             379 :       for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
   98229             274 :         pLoop->pRightmost = p;
   98230             274 :         pLoop->pNext = pRight;
   98231             274 :         pRight = pLoop;
   98232                 :       }
   98233             105 :       mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
   98234             105 :       if( mxSelect && cnt>mxSelect ){
   98235               0 :         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
   98236               0 :         goto select_end;
   98237                 :       }
   98238                 :     }
   98239             169 :     rc = multiSelect(pParse, p, pDest);
   98240             169 :     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
   98241             169 :     return rc;
   98242                 :   }
   98243                 : #endif
   98244                 : 
   98245                 :   /* If there is both a GROUP BY and an ORDER BY clause and they are
   98246                 :   ** identical, then disable the ORDER BY clause since the GROUP BY
   98247                 :   ** will cause elements to come out in the correct order.  This is
   98248                 :   ** an optimization - the correct answer should result regardless.
   98249                 :   ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
   98250                 :   ** to disable this optimization for testing purposes.
   98251                 :   */
   98252           60002 :   if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
   98253           38011 :          && (db->flags & SQLITE_GroupByOrder)==0 ){
   98254           38011 :     pOrderBy = 0;
   98255                 :   }
   98256                 : 
   98257                 :   /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
   98258                 :   ** if the select-list is the same as the ORDER BY list, then this query
   98259                 :   ** can be rewritten as a GROUP BY. In other words, this:
   98260                 :   **
   98261                 :   **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
   98262                 :   **
   98263                 :   ** is transformed to:
   98264                 :   **
   98265                 :   **     SELECT xyz FROM ... GROUP BY xyz
   98266                 :   **
   98267                 :   ** The second form is preferred as a single index (or temp-table) may be 
   98268                 :   ** used for both the ORDER BY and DISTINCT processing. As originally 
   98269                 :   ** written the query must use a temp-table for at least one of the ORDER 
   98270                 :   ** BY and DISTINCT, and an index or separate temp-table for the other.
   98271                 :   */
   98272           60002 :   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
   98273             735 :    && sqlite3ExprListCompare(pOrderBy, p->pEList)==0
   98274                 :   ){
   98275               0 :     p->selFlags &= ~SF_Distinct;
   98276               0 :     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
   98277               0 :     pGroupBy = p->pGroupBy;
   98278               0 :     pOrderBy = 0;
   98279                 :   }
   98280                 : 
   98281                 :   /* If there is an ORDER BY clause, then this sorting
   98282                 :   ** index might end up being unused if the data can be 
   98283                 :   ** extracted in pre-sorted order.  If that is the case, then the
   98284                 :   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
   98285                 :   ** we figure out that the sorting index is not needed.  The addrSortIndex
   98286                 :   ** variable is used to facilitate that change.
   98287                 :   */
   98288           60002 :   if( pOrderBy ){
   98289                 :     KeyInfo *pKeyInfo;
   98290           21895 :     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy);
   98291           21895 :     pOrderBy->iECursor = pParse->nTab++;
   98292           21895 :     p->addrOpenEphm[2] = addrSortIndex =
   98293           65685 :       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
   98294           43790 :                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
   98295                 :                            (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
   98296                 :   }else{
   98297           38107 :     addrSortIndex = -1;
   98298                 :   }
   98299                 : 
   98300                 :   /* If the output is destined for a temporary table, open that table.
   98301                 :   */
   98302           60002 :   if( pDest->eDest==SRT_EphemTab ){
   98303              37 :     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr);
   98304                 :   }
   98305                 : 
   98306                 :   /* Set the limiter.
   98307                 :   */
   98308           60002 :   iEnd = sqlite3VdbeMakeLabel(v);
   98309           60002 :   p->nSelectRow = (double)LARGEST_INT64;
   98310           60002 :   computeLimitRegisters(pParse, p, iEnd);
   98311           60002 :   if( p->iLimit==0 && addrSortIndex>=0 ){
   98312           20053 :     sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
   98313           20053 :     p->selFlags |= SF_UseSorter;
   98314                 :   }
   98315                 : 
   98316                 :   /* Open a virtual index to use for the distinct set.
   98317                 :   */
   98318           60002 :   if( p->selFlags & SF_Distinct ){
   98319                 :     KeyInfo *pKeyInfo;
   98320             735 :     distinct = pParse->nTab++;
   98321             735 :     pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
   98322             735 :     addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
   98323                 :         (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
   98324             735 :     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
   98325                 :   }else{
   98326           59267 :     distinct = addrDistinctIndex = -1;
   98327                 :   }
   98328                 : 
   98329                 :   /* Aggregate and non-aggregate queries are handled differently */
   98330          115562 :   if( !isAgg && pGroupBy==0 ){
   98331           55560 :     ExprList *pDist = (isDistinct ? p->pEList : 0);
   98332                 : 
   98333                 :     /* Begin the database scan. */
   98334           55560 :     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0);
   98335           55560 :     if( pWInfo==0 ) goto select_end;
   98336           55560 :     if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
   98337                 : 
   98338                 :     /* If sorting index that was created by a prior OP_OpenEphemeral 
   98339                 :     ** instruction ended up not being needed, then change the OP_OpenEphemeral
   98340                 :     ** into an OP_Noop.
   98341                 :     */
   98342           55560 :     if( addrSortIndex>=0 && pOrderBy==0 ){
   98343           21517 :       sqlite3VdbeChangeToNoop(v, addrSortIndex);
   98344           21517 :       p->addrOpenEphm[2] = -1;
   98345                 :     }
   98346                 : 
   98347           55560 :     if( pWInfo->eDistinct ){
   98348                 :       VdbeOp *pOp;                /* No longer required OpenEphemeral instr. */
   98349                 :      
   98350              12 :       assert( addrDistinctIndex>=0 );
   98351              12 :       pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
   98352                 : 
   98353              12 :       assert( isDistinct );
   98354              12 :       assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED 
   98355                 :            || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE 
   98356                 :       );
   98357              12 :       distinct = -1;
   98358              12 :       if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
   98359                 :         int iJump;
   98360                 :         int iExpr;
   98361              12 :         int iFlag = ++pParse->nMem;
   98362              12 :         int iBase = pParse->nMem+1;
   98363              12 :         int iBase2 = iBase + pEList->nExpr;
   98364              12 :         pParse->nMem += (pEList->nExpr*2);
   98365                 : 
   98366                 :         /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
   98367                 :         ** OP_Integer initializes the "first row" flag.  */
   98368              12 :         pOp->opcode = OP_Integer;
   98369              12 :         pOp->p1 = 1;
   98370              12 :         pOp->p2 = iFlag;
   98371                 : 
   98372              12 :         sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
   98373              12 :         iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
   98374              12 :         sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
   98375              24 :         for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
   98376              12 :           CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
   98377              12 :           sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
   98378              12 :           sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
   98379              12 :           sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
   98380                 :         }
   98381              12 :         sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
   98382                 : 
   98383              12 :         sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
   98384              12 :         assert( sqlite3VdbeCurrentAddr(v)==iJump );
   98385              12 :         sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
   98386                 :       }else{
   98387               0 :         pOp->opcode = OP_Noop;
   98388                 :       }
   98389                 :     }
   98390                 : 
   98391                 :     /* Use the standard inner loop. */
   98392           55560 :     selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
   98393                 :                     pWInfo->iContinue, pWInfo->iBreak);
   98394                 : 
   98395                 :     /* End the database scan loop.
   98396                 :     */
   98397           55560 :     sqlite3WhereEnd(pWInfo);
   98398                 :   }else{
   98399                 :     /* This is the processing for aggregate queries */
   98400                 :     NameContext sNC;    /* Name context for processing aggregate information */
   98401                 :     int iAMem;          /* First Mem address for storing current GROUP BY */
   98402                 :     int iBMem;          /* First Mem address for previous GROUP BY */
   98403                 :     int iUseFlag;       /* Mem address holding flag indicating that at least
   98404                 :                         ** one row of the input to the aggregator has been
   98405                 :                         ** processed */
   98406                 :     int iAbortFlag;     /* Mem address which causes query abort if positive */
   98407                 :     int groupBySort;    /* Rows come from source in GROUP BY order */
   98408                 :     int addrEnd;        /* End of processing for this SELECT */
   98409            4442 :     int sortPTab = 0;   /* Pseudotable used to decode sorting results */
   98410            4442 :     int sortOut = 0;    /* Output register from the sorter */
   98411                 : 
   98412                 :     /* Remove any and all aliases between the result set and the
   98413                 :     ** GROUP BY clause.
   98414                 :     */
   98415            4442 :     if( pGroupBy ){
   98416                 :       int k;                        /* Loop counter */
   98417                 :       struct ExprList_item *pItem;  /* For looping over expression in a list */
   98418                 : 
   98419             305 :       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
   98420             200 :         pItem->iAlias = 0;
   98421                 :       }
   98422             210 :       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
   98423             105 :         pItem->iAlias = 0;
   98424                 :       }
   98425             105 :       if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
   98426                 :     }else{
   98427            4337 :       p->nSelectRow = (double)1;
   98428                 :     }
   98429                 : 
   98430                 :  
   98431                 :     /* Create a label to jump to when we want to abort the query */
   98432            4442 :     addrEnd = sqlite3VdbeMakeLabel(v);
   98433                 : 
   98434                 :     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
   98435                 :     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
   98436                 :     ** SELECT statement.
   98437                 :     */
   98438            4442 :     memset(&sNC, 0, sizeof(sNC));
   98439            4442 :     sNC.pParse = pParse;
   98440            4442 :     sNC.pSrcList = pTabList;
   98441            4442 :     sNC.pAggInfo = &sAggInfo;
   98442            4442 :     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
   98443            4442 :     sAggInfo.pGroupBy = pGroupBy;
   98444            4442 :     sqlite3ExprAnalyzeAggList(&sNC, pEList);
   98445            4442 :     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
   98446            4442 :     if( pHaving ){
   98447              53 :       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
   98448                 :     }
   98449            4442 :     sAggInfo.nAccumulator = sAggInfo.nColumn;
   98450            8910 :     for(i=0; i<sAggInfo.nFunc; i++){
   98451            4468 :       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
   98452            4468 :       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
   98453                 :     }
   98454            4442 :     if( db->mallocFailed ) goto select_end;
   98455                 : 
   98456                 :     /* Processing for aggregates with GROUP BY is very different and
   98457                 :     ** much more complex than aggregates without a GROUP BY.
   98458                 :     */
   98459            4442 :     if( pGroupBy ){
   98460                 :       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
   98461                 :       int j1;             /* A-vs-B comparision jump */
   98462                 :       int addrOutputRow;  /* Start of subroutine that outputs a result row */
   98463                 :       int regOutputRow;   /* Return address register for output subroutine */
   98464                 :       int addrSetAbort;   /* Set the abort flag and return */
   98465                 :       int addrTopOfLoop;  /* Top of the input loop */
   98466                 :       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
   98467                 :       int addrReset;      /* Subroutine for resetting the accumulator */
   98468                 :       int regReset;       /* Return address register for reset subroutine */
   98469                 : 
   98470                 :       /* If there is a GROUP BY clause we might need a sorting index to
   98471                 :       ** implement it.  Allocate that sorting index now.  If it turns out
   98472                 :       ** that we do not need it after all, the OP_SorterOpen instruction
   98473                 :       ** will be converted into a Noop.  
   98474                 :       */
   98475             105 :       sAggInfo.sortingIdx = pParse->nTab++;
   98476             105 :       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
   98477             105 :       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
   98478                 :           sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
   98479                 :           0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
   98480                 : 
   98481                 :       /* Initialize memory locations used by GROUP BY aggregate processing
   98482                 :       */
   98483             105 :       iUseFlag = ++pParse->nMem;
   98484             105 :       iAbortFlag = ++pParse->nMem;
   98485             105 :       regOutputRow = ++pParse->nMem;
   98486             105 :       addrOutputRow = sqlite3VdbeMakeLabel(v);
   98487             105 :       regReset = ++pParse->nMem;
   98488             105 :       addrReset = sqlite3VdbeMakeLabel(v);
   98489             105 :       iAMem = pParse->nMem + 1;
   98490             105 :       pParse->nMem += pGroupBy->nExpr;
   98491             105 :       iBMem = pParse->nMem + 1;
   98492             105 :       pParse->nMem += pGroupBy->nExpr;
   98493             105 :       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
   98494             105 :       VdbeComment((v, "clear abort flag"));
   98495             105 :       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
   98496             105 :       VdbeComment((v, "indicate accumulator empty"));
   98497             105 :       sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
   98498                 : 
   98499                 :       /* Begin a loop that will extract all source rows in GROUP BY order.
   98500                 :       ** This might involve two separate loops with an OP_Sort in between, or
   98501                 :       ** it might be a single loop that uses an index to extract information
   98502                 :       ** in the right order to begin with.
   98503                 :       */
   98504             105 :       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
   98505             105 :       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0);
   98506             105 :       if( pWInfo==0 ) goto select_end;
   98507             105 :       if( pGroupBy==0 ){
   98508                 :         /* The optimizer is able to deliver rows in group by order so
   98509                 :         ** we do not have to sort.  The OP_OpenEphemeral table will be
   98510                 :         ** cancelled later because we still need to use the pKeyInfo
   98511                 :         */
   98512              82 :         pGroupBy = p->pGroupBy;
   98513              82 :         groupBySort = 0;
   98514                 :       }else{
   98515                 :         /* Rows are coming out in undetermined order.  We have to push
   98516                 :         ** each row into a sorting index, terminate the first loop,
   98517                 :         ** then loop over the sorting index in order to get the output
   98518                 :         ** in sorted order
   98519                 :         */
   98520                 :         int regBase;
   98521                 :         int regRecord;
   98522                 :         int nCol;
   98523                 :         int nGroupBy;
   98524                 : 
   98525              23 :         explainTempTable(pParse, 
   98526               0 :             isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
   98527                 : 
   98528              23 :         groupBySort = 1;
   98529              23 :         nGroupBy = pGroupBy->nExpr;
   98530              23 :         nCol = nGroupBy + 1;
   98531              23 :         j = nGroupBy+1;
   98532             116 :         for(i=0; i<sAggInfo.nColumn; i++){
   98533              93 :           if( sAggInfo.aCol[i].iSorterColumn>=j ){
   98534              70 :             nCol++;
   98535              70 :             j++;
   98536                 :           }
   98537                 :         }
   98538              23 :         regBase = sqlite3GetTempRange(pParse, nCol);
   98539              23 :         sqlite3ExprCacheClear(pParse);
   98540              23 :         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
   98541              23 :         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
   98542              23 :         j = nGroupBy+1;
   98543             116 :         for(i=0; i<sAggInfo.nColumn; i++){
   98544              93 :           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
   98545              93 :           if( pCol->iSorterColumn>=j ){
   98546              70 :             int r1 = j + regBase;
   98547                 :             int r2;
   98548                 : 
   98549              70 :             r2 = sqlite3ExprCodeGetColumn(pParse, 
   98550                 :                                pCol->pTab, pCol->iColumn, pCol->iTable, r1);
   98551              70 :             if( r1!=r2 ){
   98552               0 :               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
   98553                 :             }
   98554              70 :             j++;
   98555                 :           }
   98556                 :         }
   98557              23 :         regRecord = sqlite3GetTempReg(pParse);
   98558              23 :         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
   98559              23 :         sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
   98560              23 :         sqlite3ReleaseTempReg(pParse, regRecord);
   98561              23 :         sqlite3ReleaseTempRange(pParse, regBase, nCol);
   98562              23 :         sqlite3WhereEnd(pWInfo);
   98563              23 :         sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
   98564              23 :         sortOut = sqlite3GetTempReg(pParse);
   98565              23 :         sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
   98566              23 :         sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
   98567              23 :         VdbeComment((v, "GROUP BY sort"));
   98568              23 :         sAggInfo.useSortingIdx = 1;
   98569              23 :         sqlite3ExprCacheClear(pParse);
   98570                 :       }
   98571                 : 
   98572                 :       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
   98573                 :       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
   98574                 :       ** Then compare the current GROUP BY terms against the GROUP BY terms
   98575                 :       ** from the previous row currently stored in a0, a1, a2...
   98576                 :       */
   98577             105 :       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
   98578             105 :       sqlite3ExprCacheClear(pParse);
   98579             105 :       if( groupBySort ){
   98580              23 :         sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
   98581                 :       }
   98582             210 :       for(j=0; j<pGroupBy->nExpr; j++){
   98583             105 :         if( groupBySort ){
   98584              23 :           sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
   98585              23 :           if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
   98586                 :         }else{
   98587              82 :           sAggInfo.directMode = 1;
   98588              82 :           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
   98589                 :         }
   98590                 :       }
   98591             105 :       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
   98592                 :                           (char*)pKeyInfo, P4_KEYINFO);
   98593             105 :       j1 = sqlite3VdbeCurrentAddr(v);
   98594             105 :       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
   98595                 : 
   98596                 :       /* Generate code that runs whenever the GROUP BY changes.
   98597                 :       ** Changes in the GROUP BY are detected by the previous code
   98598                 :       ** block.  If there were no changes, this block is skipped.
   98599                 :       **
   98600                 :       ** This code copies current group by terms in b0,b1,b2,...
   98601                 :       ** over to a0,a1,a2.  It then calls the output subroutine
   98602                 :       ** and resets the aggregate accumulator registers in preparation
   98603                 :       ** for the next GROUP BY batch.
   98604                 :       */
   98605             105 :       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
   98606             105 :       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
   98607             105 :       VdbeComment((v, "output one row"));
   98608             105 :       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
   98609             105 :       VdbeComment((v, "check abort flag"));
   98610             105 :       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
   98611             105 :       VdbeComment((v, "reset accumulator"));
   98612                 : 
   98613                 :       /* Update the aggregate accumulators based on the content of
   98614                 :       ** the current row
   98615                 :       */
   98616             105 :       sqlite3VdbeJumpHere(v, j1);
   98617             105 :       updateAccumulator(pParse, &sAggInfo);
   98618             105 :       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
   98619             105 :       VdbeComment((v, "indicate data in accumulator"));
   98620                 : 
   98621                 :       /* End of the loop
   98622                 :       */
   98623             105 :       if( groupBySort ){
   98624              23 :         sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
   98625                 :       }else{
   98626              82 :         sqlite3WhereEnd(pWInfo);
   98627              82 :         sqlite3VdbeChangeToNoop(v, addrSortingIdx);
   98628                 :       }
   98629                 : 
   98630                 :       /* Output the final row of result
   98631                 :       */
   98632             105 :       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
   98633             105 :       VdbeComment((v, "output final row"));
   98634                 : 
   98635                 :       /* Jump over the subroutines
   98636                 :       */
   98637             105 :       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
   98638                 : 
   98639                 :       /* Generate a subroutine that outputs a single row of the result
   98640                 :       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
   98641                 :       ** is less than or equal to zero, the subroutine is a no-op.  If
   98642                 :       ** the processing calls for the query to abort, this subroutine
   98643                 :       ** increments the iAbortFlag memory location before returning in
   98644                 :       ** order to signal the caller to abort.
   98645                 :       */
   98646             105 :       addrSetAbort = sqlite3VdbeCurrentAddr(v);
   98647             105 :       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
   98648             105 :       VdbeComment((v, "set abort flag"));
   98649             105 :       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
   98650             105 :       sqlite3VdbeResolveLabel(v, addrOutputRow);
   98651             105 :       addrOutputRow = sqlite3VdbeCurrentAddr(v);
   98652             105 :       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
   98653             105 :       VdbeComment((v, "Groupby result generator entry point"));
   98654             105 :       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
   98655             105 :       finalizeAggFunctions(pParse, &sAggInfo);
   98656             105 :       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
   98657             105 :       selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
   98658                 :                       distinct, pDest,
   98659                 :                       addrOutputRow+1, addrSetAbort);
   98660             105 :       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
   98661             105 :       VdbeComment((v, "end groupby result generator"));
   98662                 : 
   98663                 :       /* Generate a subroutine that will reset the group-by accumulator
   98664                 :       */
   98665             105 :       sqlite3VdbeResolveLabel(v, addrReset);
   98666             105 :       resetAccumulator(pParse, &sAggInfo);
   98667             105 :       sqlite3VdbeAddOp1(v, OP_Return, regReset);
   98668                 :      
   98669                 :     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
   98670                 :     else {
   98671            4337 :       ExprList *pDel = 0;
   98672                 : #ifndef SQLITE_OMIT_BTREECOUNT
   98673                 :       Table *pTab;
   98674            4337 :       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
   98675                 :         /* If isSimpleCount() returns a pointer to a Table structure, then
   98676                 :         ** the SQL statement is of the form:
   98677                 :         **
   98678                 :         **   SELECT count(*) FROM <tbl>
   98679                 :         **
   98680                 :         ** where the Table structure returned represents table <tbl>.
   98681                 :         **
   98682                 :         ** This statement is so common that it is optimized specially. The
   98683                 :         ** OP_Count instruction is executed either on the intkey table that
   98684                 :         ** contains the data for table <tbl> or on one of its indexes. It
   98685                 :         ** is better to execute the op on an index, as indexes are almost
   98686                 :         ** always spread across less pages than their corresponding tables.
   98687                 :         */
   98688             632 :         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
   98689             632 :         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
   98690                 :         Index *pIdx;                         /* Iterator variable */
   98691             632 :         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
   98692             632 :         Index *pBest = 0;                    /* Best index found so far */
   98693             632 :         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
   98694                 : 
   98695             632 :         sqlite3CodeVerifySchema(pParse, iDb);
   98696             632 :         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
   98697                 : 
   98698                 :         /* Search for the index that has the least amount of columns. If
   98699                 :         ** there is such an index, and it has less columns than the table
   98700                 :         ** does, then we can assume that it consumes less space on disk and
   98701                 :         ** will therefore be cheaper to scan to determine the query result.
   98702                 :         ** In this case set iRoot to the root page number of the index b-tree
   98703                 :         ** and pKeyInfo to the KeyInfo structure required to navigate the
   98704                 :         ** index.
   98705                 :         **
   98706                 :         ** (2011-04-15) Do not do a full scan of an unordered index.
   98707                 :         **
   98708                 :         ** In practice the KeyInfo structure will not be used. It is only 
   98709                 :         ** passed to keep OP_OpenRead happy.
   98710                 :         */
   98711            2127 :         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   98712            1495 :           if( pIdx->bUnordered==0 && (!pBest || pIdx->nColumn<pBest->nColumn) ){
   98713             580 :             pBest = pIdx;
   98714                 :           }
   98715                 :         }
   98716             632 :         if( pBest && pBest->nColumn<pTab->nCol ){
   98717             580 :           iRoot = pBest->tnum;
   98718             580 :           pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
   98719                 :         }
   98720                 : 
   98721                 :         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
   98722             632 :         sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb);
   98723             632 :         if( pKeyInfo ){
   98724             580 :           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF);
   98725                 :         }
   98726             632 :         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
   98727             632 :         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
   98728             632 :         explainSimpleCount(pParse, pTab, pBest);
   98729                 :       }else
   98730                 : #endif /* SQLITE_OMIT_BTREECOUNT */
   98731                 :       {
   98732                 :         /* Check if the query is of one of the following forms:
   98733                 :         **
   98734                 :         **   SELECT min(x) FROM ...
   98735                 :         **   SELECT max(x) FROM ...
   98736                 :         **
   98737                 :         ** If it is, then ask the code in where.c to attempt to sort results
   98738                 :         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
   98739                 :         ** If where.c is able to produce results sorted in this order, then
   98740                 :         ** add vdbe code to break out of the processing loop after the 
   98741                 :         ** first iteration (since the first iteration of the loop is 
   98742                 :         ** guaranteed to operate on the row with the minimum or maximum 
   98743                 :         ** value of x, the only row required).
   98744                 :         **
   98745                 :         ** A special flag must be passed to sqlite3WhereBegin() to slightly
   98746                 :         ** modify behaviour as follows:
   98747                 :         **
   98748                 :         **   + If the query is a "SELECT min(x)", then the loop coded by
   98749                 :         **     where.c should not iterate over any values with a NULL value
   98750                 :         **     for x.
   98751                 :         **
   98752                 :         **   + The optimizer code in where.c (the thing that decides which
   98753                 :         **     index or indices to use) should place a different priority on 
   98754                 :         **     satisfying the 'ORDER BY' clause than it does in other cases.
   98755                 :         **     Refer to code and comments in where.c for details.
   98756                 :         */
   98757            3705 :         ExprList *pMinMax = 0;
   98758            3705 :         u8 flag = minMaxQuery(p);
   98759            3705 :         if( flag ){
   98760            1760 :           assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
   98761            1760 :           pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
   98762            1760 :           pDel = pMinMax;
   98763            1760 :           if( pMinMax && !db->mallocFailed ){
   98764            1760 :             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
   98765            1760 :             pMinMax->a[0].pExpr->op = TK_COLUMN;
   98766                 :           }
   98767                 :         }
   98768                 :   
   98769                 :         /* This case runs if the aggregate has no GROUP BY clause.  The
   98770                 :         ** processing is much simpler since there is only a single row
   98771                 :         ** of output.
   98772                 :         */
   98773            3705 :         resetAccumulator(pParse, &sAggInfo);
   98774            3705 :         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, 0, flag);
   98775            3705 :         if( pWInfo==0 ){
   98776               0 :           sqlite3ExprListDelete(db, pDel);
   98777               0 :           goto select_end;
   98778                 :         }
   98779            3705 :         updateAccumulator(pParse, &sAggInfo);
   98780            3705 :         if( !pMinMax && flag ){
   98781              93 :           sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
   98782              93 :           VdbeComment((v, "%s() by index",
   98783                 :                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
   98784                 :         }
   98785            3705 :         sqlite3WhereEnd(pWInfo);
   98786            3705 :         finalizeAggFunctions(pParse, &sAggInfo);
   98787                 :       }
   98788                 : 
   98789            4337 :       pOrderBy = 0;
   98790            4337 :       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
   98791            4337 :       selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 
   98792                 :                       pDest, addrEnd, addrEnd);
   98793            4337 :       sqlite3ExprListDelete(db, pDel);
   98794                 :     }
   98795            4442 :     sqlite3VdbeResolveLabel(v, addrEnd);
   98796                 :     
   98797                 :   } /* endif aggregate query */
   98798                 : 
   98799           60002 :   if( distinct>=0 ){
   98800             723 :     explainTempTable(pParse, "DISTINCT");
   98801                 :   }
   98802                 : 
   98803                 :   /* If there is an ORDER BY clause, then we need to sort the results
   98804                 :   ** and send them to the callback one by one.
   98805                 :   */
   98806           60002 :   if( pOrderBy ){
   98807             378 :     explainTempTable(pParse, "ORDER BY");
   98808             378 :     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
   98809                 :   }
   98810                 : 
   98811                 :   /* Jump here to skip this query
   98812                 :   */
   98813           60002 :   sqlite3VdbeResolveLabel(v, iEnd);
   98814                 : 
   98815                 :   /* The SELECT was successfully coded.   Set the return code to 0
   98816                 :   ** to indicate no errors.
   98817                 :   */
   98818           60002 :   rc = 0;
   98819                 : 
   98820                 :   /* Control jumps to here if an error is encountered above, or upon
   98821                 :   ** successful coding of the SELECT.
   98822                 :   */
   98823                 : select_end:
   98824           60053 :   explainSetInteger(pParse->iSelectId, iRestoreSelectId);
   98825                 : 
   98826                 :   /* Identify column names if results of the SELECT are to be output.
   98827                 :   */
   98828           60053 :   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
   98829           45715 :     generateColumnNames(pParse, pTabList, pEList);
   98830                 :   }
   98831                 : 
   98832           60053 :   sqlite3DbFree(db, sAggInfo.aCol);
   98833           60053 :   sqlite3DbFree(db, sAggInfo.aFunc);
   98834           60053 :   return rc;
   98835                 : }
   98836                 : 
   98837                 : #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
   98838                 : /*
   98839                 : ** Generate a human-readable description of a the Select object.
   98840                 : */
   98841                 : static void explainOneSelect(Vdbe *pVdbe, Select *p){
   98842                 :   sqlite3ExplainPrintf(pVdbe, "SELECT ");
   98843                 :   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
   98844                 :     if( p->selFlags & SF_Distinct ){
   98845                 :       sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
   98846                 :     }
   98847                 :     if( p->selFlags & SF_Aggregate ){
   98848                 :       sqlite3ExplainPrintf(pVdbe, "agg_flag ");
   98849                 :     }
   98850                 :     sqlite3ExplainNL(pVdbe);
   98851                 :     sqlite3ExplainPrintf(pVdbe, "   ");
   98852                 :   }
   98853                 :   sqlite3ExplainExprList(pVdbe, p->pEList);
   98854                 :   sqlite3ExplainNL(pVdbe);
   98855                 :   if( p->pSrc && p->pSrc->nSrc ){
   98856                 :     int i;
   98857                 :     sqlite3ExplainPrintf(pVdbe, "FROM ");
   98858                 :     sqlite3ExplainPush(pVdbe);
   98859                 :     for(i=0; i<p->pSrc->nSrc; i++){
   98860                 :       struct SrcList_item *pItem = &p->pSrc->a[i];
   98861                 :       sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
   98862                 :       if( pItem->pSelect ){
   98863                 :         sqlite3ExplainSelect(pVdbe, pItem->pSelect);
   98864                 :         if( pItem->pTab ){
   98865                 :           sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
   98866                 :         }
   98867                 :       }else if( pItem->zName ){
   98868                 :         sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
   98869                 :       }
   98870                 :       if( pItem->zAlias ){
   98871                 :         sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
   98872                 :       }
   98873                 :       if( pItem->jointype & JT_LEFT ){
   98874                 :         sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
   98875                 :       }
   98876                 :       sqlite3ExplainNL(pVdbe);
   98877                 :     }
   98878                 :     sqlite3ExplainPop(pVdbe);
   98879                 :   }
   98880                 :   if( p->pWhere ){
   98881                 :     sqlite3ExplainPrintf(pVdbe, "WHERE ");
   98882                 :     sqlite3ExplainExpr(pVdbe, p->pWhere);
   98883                 :     sqlite3ExplainNL(pVdbe);
   98884                 :   }
   98885                 :   if( p->pGroupBy ){
   98886                 :     sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
   98887                 :     sqlite3ExplainExprList(pVdbe, p->pGroupBy);
   98888                 :     sqlite3ExplainNL(pVdbe);
   98889                 :   }
   98890                 :   if( p->pHaving ){
   98891                 :     sqlite3ExplainPrintf(pVdbe, "HAVING ");
   98892                 :     sqlite3ExplainExpr(pVdbe, p->pHaving);
   98893                 :     sqlite3ExplainNL(pVdbe);
   98894                 :   }
   98895                 :   if( p->pOrderBy ){
   98896                 :     sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
   98897                 :     sqlite3ExplainExprList(pVdbe, p->pOrderBy);
   98898                 :     sqlite3ExplainNL(pVdbe);
   98899                 :   }
   98900                 :   if( p->pLimit ){
   98901                 :     sqlite3ExplainPrintf(pVdbe, "LIMIT ");
   98902                 :     sqlite3ExplainExpr(pVdbe, p->pLimit);
   98903                 :     sqlite3ExplainNL(pVdbe);
   98904                 :   }
   98905                 :   if( p->pOffset ){
   98906                 :     sqlite3ExplainPrintf(pVdbe, "OFFSET ");
   98907                 :     sqlite3ExplainExpr(pVdbe, p->pOffset);
   98908                 :     sqlite3ExplainNL(pVdbe);
   98909                 :   }
   98910                 : }
   98911                 : SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
   98912                 :   if( p==0 ){
   98913                 :     sqlite3ExplainPrintf(pVdbe, "(null-select)");
   98914                 :     return;
   98915                 :   }
   98916                 :   while( p->pPrior ) p = p->pPrior;
   98917                 :   sqlite3ExplainPush(pVdbe);
   98918                 :   while( p ){
   98919                 :     explainOneSelect(pVdbe, p);
   98920                 :     p = p->pNext;
   98921                 :     if( p==0 ) break;
   98922                 :     sqlite3ExplainNL(pVdbe);
   98923                 :     sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
   98924                 :   }
   98925                 :   sqlite3ExplainPrintf(pVdbe, "END");
   98926                 :   sqlite3ExplainPop(pVdbe);
   98927                 : }
   98928                 : 
   98929                 : /* End of the structure debug printing code
   98930                 : *****************************************************************************/
   98931                 : #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
   98932                 : 
   98933                 : /************** End of select.c **********************************************/
   98934                 : /************** Begin file table.c *******************************************/
   98935                 : /*
   98936                 : ** 2001 September 15
   98937                 : **
   98938                 : ** The author disclaims copyright to this source code.  In place of
   98939                 : ** a legal notice, here is a blessing:
   98940                 : **
   98941                 : **    May you do good and not evil.
   98942                 : **    May you find forgiveness for yourself and forgive others.
   98943                 : **    May you share freely, never taking more than you give.
   98944                 : **
   98945                 : *************************************************************************
   98946                 : ** This file contains the sqlite3_get_table() and sqlite3_free_table()
   98947                 : ** interface routines.  These are just wrappers around the main
   98948                 : ** interface routine of sqlite3_exec().
   98949                 : **
   98950                 : ** These routines are in a separate files so that they will not be linked
   98951                 : ** if they are not used.
   98952                 : */
   98953                 : /* #include <stdlib.h> */
   98954                 : /* #include <string.h> */
   98955                 : 
   98956                 : #ifndef SQLITE_OMIT_GET_TABLE
   98957                 : 
   98958                 : /*
   98959                 : ** This structure is used to pass data from sqlite3_get_table() through
   98960                 : ** to the callback function is uses to build the result.
   98961                 : */
   98962                 : typedef struct TabResult {
   98963                 :   char **azResult;   /* Accumulated output */
   98964                 :   char *zErrMsg;     /* Error message text, if an error occurs */
   98965                 :   int nAlloc;        /* Slots allocated for azResult[] */
   98966                 :   int nRow;          /* Number of rows in the result */
   98967                 :   int nColumn;       /* Number of columns in the result */
   98968                 :   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
   98969                 :   int rc;            /* Return code from sqlite3_exec() */
   98970                 : } TabResult;
   98971                 : 
   98972                 : /*
   98973                 : ** This routine is called once for each row in the result table.  Its job
   98974                 : ** is to fill in the TabResult structure appropriately, allocating new
   98975                 : ** memory as necessary.
   98976                 : */
   98977               0 : static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
   98978               0 :   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
   98979                 :   int need;                         /* Slots needed in p->azResult[] */
   98980                 :   int i;                            /* Loop counter */
   98981                 :   char *z;                          /* A single column of result */
   98982                 : 
   98983                 :   /* Make sure there is enough space in p->azResult to hold everything
   98984                 :   ** we need to remember from this invocation of the callback.
   98985                 :   */
   98986               0 :   if( p->nRow==0 && argv!=0 ){
   98987               0 :     need = nCol*2;
   98988                 :   }else{
   98989               0 :     need = nCol;
   98990                 :   }
   98991               0 :   if( p->nData + need > p->nAlloc ){
   98992                 :     char **azNew;
   98993               0 :     p->nAlloc = p->nAlloc*2 + need;
   98994               0 :     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
   98995               0 :     if( azNew==0 ) goto malloc_failed;
   98996               0 :     p->azResult = azNew;
   98997                 :   }
   98998                 : 
   98999                 :   /* If this is the first row, then generate an extra row containing
   99000                 :   ** the names of all columns.
   99001                 :   */
   99002               0 :   if( p->nRow==0 ){
   99003               0 :     p->nColumn = nCol;
   99004               0 :     for(i=0; i<nCol; i++){
   99005               0 :       z = sqlite3_mprintf("%s", colv[i]);
   99006               0 :       if( z==0 ) goto malloc_failed;
   99007               0 :       p->azResult[p->nData++] = z;
   99008                 :     }
   99009               0 :   }else if( p->nColumn!=nCol ){
   99010               0 :     sqlite3_free(p->zErrMsg);
   99011               0 :     p->zErrMsg = sqlite3_mprintf(
   99012                 :        "sqlite3_get_table() called with two or more incompatible queries"
   99013                 :     );
   99014               0 :     p->rc = SQLITE_ERROR;
   99015               0 :     return 1;
   99016                 :   }
   99017                 : 
   99018                 :   /* Copy over the row data
   99019                 :   */
   99020               0 :   if( argv!=0 ){
   99021               0 :     for(i=0; i<nCol; i++){
   99022               0 :       if( argv[i]==0 ){
   99023               0 :         z = 0;
   99024                 :       }else{
   99025               0 :         int n = sqlite3Strlen30(argv[i])+1;
   99026               0 :         z = sqlite3_malloc( n );
   99027               0 :         if( z==0 ) goto malloc_failed;
   99028               0 :         memcpy(z, argv[i], n);
   99029                 :       }
   99030               0 :       p->azResult[p->nData++] = z;
   99031                 :     }
   99032               0 :     p->nRow++;
   99033                 :   }
   99034               0 :   return 0;
   99035                 : 
   99036                 : malloc_failed:
   99037               0 :   p->rc = SQLITE_NOMEM;
   99038               0 :   return 1;
   99039                 : }
   99040                 : 
   99041                 : /*
   99042                 : ** Query the database.  But instead of invoking a callback for each row,
   99043                 : ** malloc() for space to hold the result and return the entire results
   99044                 : ** at the conclusion of the call.
   99045                 : **
   99046                 : ** The result that is written to ***pazResult is held in memory obtained
   99047                 : ** from malloc().  But the caller cannot free this memory directly.  
   99048                 : ** Instead, the entire table should be passed to sqlite3_free_table() when
   99049                 : ** the calling procedure is finished using it.
   99050                 : */
   99051               0 : SQLITE_API int sqlite3_get_table(
   99052                 :   sqlite3 *db,                /* The database on which the SQL executes */
   99053                 :   const char *zSql,           /* The SQL to be executed */
   99054                 :   char ***pazResult,          /* Write the result table here */
   99055                 :   int *pnRow,                 /* Write the number of rows in the result here */
   99056                 :   int *pnColumn,              /* Write the number of columns of result here */
   99057                 :   char **pzErrMsg             /* Write error messages here */
   99058                 : ){
   99059                 :   int rc;
   99060                 :   TabResult res;
   99061                 : 
   99062               0 :   *pazResult = 0;
   99063               0 :   if( pnColumn ) *pnColumn = 0;
   99064               0 :   if( pnRow ) *pnRow = 0;
   99065               0 :   if( pzErrMsg ) *pzErrMsg = 0;
   99066               0 :   res.zErrMsg = 0;
   99067               0 :   res.nRow = 0;
   99068               0 :   res.nColumn = 0;
   99069               0 :   res.nData = 1;
   99070               0 :   res.nAlloc = 20;
   99071               0 :   res.rc = SQLITE_OK;
   99072               0 :   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
   99073               0 :   if( res.azResult==0 ){
   99074               0 :      db->errCode = SQLITE_NOMEM;
   99075               0 :      return SQLITE_NOMEM;
   99076                 :   }
   99077               0 :   res.azResult[0] = 0;
   99078               0 :   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
   99079                 :   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
   99080               0 :   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
   99081               0 :   if( (rc&0xff)==SQLITE_ABORT ){
   99082               0 :     sqlite3_free_table(&res.azResult[1]);
   99083               0 :     if( res.zErrMsg ){
   99084               0 :       if( pzErrMsg ){
   99085               0 :         sqlite3_free(*pzErrMsg);
   99086               0 :         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
   99087                 :       }
   99088               0 :       sqlite3_free(res.zErrMsg);
   99089                 :     }
   99090               0 :     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
   99091               0 :     return res.rc;
   99092                 :   }
   99093               0 :   sqlite3_free(res.zErrMsg);
   99094               0 :   if( rc!=SQLITE_OK ){
   99095               0 :     sqlite3_free_table(&res.azResult[1]);
   99096               0 :     return rc;
   99097                 :   }
   99098               0 :   if( res.nAlloc>res.nData ){
   99099                 :     char **azNew;
   99100               0 :     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
   99101               0 :     if( azNew==0 ){
   99102               0 :       sqlite3_free_table(&res.azResult[1]);
   99103               0 :       db->errCode = SQLITE_NOMEM;
   99104               0 :       return SQLITE_NOMEM;
   99105                 :     }
   99106               0 :     res.azResult = azNew;
   99107                 :   }
   99108               0 :   *pazResult = &res.azResult[1];
   99109               0 :   if( pnColumn ) *pnColumn = res.nColumn;
   99110               0 :   if( pnRow ) *pnRow = res.nRow;
   99111               0 :   return rc;
   99112                 : }
   99113                 : 
   99114                 : /*
   99115                 : ** This routine frees the space the sqlite3_get_table() malloced.
   99116                 : */
   99117               0 : SQLITE_API void sqlite3_free_table(
   99118                 :   char **azResult            /* Result returned from from sqlite3_get_table() */
   99119                 : ){
   99120               0 :   if( azResult ){
   99121                 :     int i, n;
   99122               0 :     azResult--;
   99123               0 :     assert( azResult!=0 );
   99124               0 :     n = SQLITE_PTR_TO_INT(azResult[0]);
   99125               0 :     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
   99126               0 :     sqlite3_free(azResult);
   99127                 :   }
   99128               0 : }
   99129                 : 
   99130                 : #endif /* SQLITE_OMIT_GET_TABLE */
   99131                 : 
   99132                 : /************** End of table.c ***********************************************/
   99133                 : /************** Begin file trigger.c *****************************************/
   99134                 : /*
   99135                 : **
   99136                 : ** The author disclaims copyright to this source code.  In place of
   99137                 : ** a legal notice, here is a blessing:
   99138                 : **
   99139                 : **    May you do good and not evil.
   99140                 : **    May you find forgiveness for yourself and forgive others.
   99141                 : **    May you share freely, never taking more than you give.
   99142                 : **
   99143                 : *************************************************************************
   99144                 : ** This file contains the implementation for TRIGGERs
   99145                 : */
   99146                 : 
   99147                 : #ifndef SQLITE_OMIT_TRIGGER
   99148                 : /*
   99149                 : ** Delete a linked list of TriggerStep structures.
   99150                 : */
   99151           22665 : SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
   99152           60596 :   while( pTriggerStep ){
   99153           15266 :     TriggerStep * pTmp = pTriggerStep;
   99154           15266 :     pTriggerStep = pTriggerStep->pNext;
   99155                 : 
   99156           15266 :     sqlite3ExprDelete(db, pTmp->pWhere);
   99157           15266 :     sqlite3ExprListDelete(db, pTmp->pExprList);
   99158           15266 :     sqlite3SelectDelete(db, pTmp->pSelect);
   99159           15266 :     sqlite3IdListDelete(db, pTmp->pIdList);
   99160                 : 
   99161           15266 :     sqlite3DbFree(db, pTmp);
   99162                 :   }
   99163           22665 : }
   99164                 : 
   99165                 : /*
   99166                 : ** Given table pTab, return a list of all the triggers attached to 
   99167                 : ** the table. The list is connected by Trigger.pNext pointers.
   99168                 : **
   99169                 : ** All of the triggers on pTab that are in the same database as pTab
   99170                 : ** are already attached to pTab->pTrigger.  But there might be additional
   99171                 : ** triggers on pTab in the TEMP schema.  This routine prepends all
   99172                 : ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
   99173                 : ** and returns the combined list.
   99174                 : **
   99175                 : ** To state it another way:  This routine returns a list of all triggers
   99176                 : ** that fire off of pTab.  The list will include any TEMP triggers on
   99177                 : ** pTab as well as the triggers lised in pTab->pTrigger.
   99178                 : */
   99179           41952 : SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
   99180           41952 :   Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
   99181           41952 :   Trigger *pList = 0;                  /* List of triggers to return */
   99182                 : 
   99183           41952 :   if( pParse->disableTriggers ){
   99184               0 :     return 0;
   99185                 :   }
   99186                 : 
   99187           41952 :   if( pTmpSchema!=pTab->pSchema ){
   99188                 :     HashElem *p;
   99189           39287 :     assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
   99190           88904 :     for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
   99191           49617 :       Trigger *pTrig = (Trigger *)sqliteHashData(p);
   99192           49617 :       if( pTrig->pTabSchema==pTab->pSchema
   99193           49142 :        && 0==sqlite3StrICmp(pTrig->table, pTab->zName) 
   99194                 :       ){
   99195            8640 :         pTrig->pNext = (pList ? pList : pTab->pTrigger);
   99196            8640 :         pList = pTrig;
   99197                 :       }
   99198                 :     }
   99199                 :   }
   99200                 : 
   99201           41952 :   return (pList ? pList : pTab->pTrigger);
   99202                 : }
   99203                 : 
   99204                 : /*
   99205                 : ** This is called by the parser when it sees a CREATE TRIGGER statement
   99206                 : ** up to the point of the BEGIN before the trigger actions.  A Trigger
   99207                 : ** structure is generated based on the information available and stored
   99208                 : ** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
   99209                 : ** sqlite3FinishTrigger() function is called to complete the trigger
   99210                 : ** construction process.
   99211                 : */
   99212           11328 : SQLITE_PRIVATE void sqlite3BeginTrigger(
   99213                 :   Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
   99214                 :   Token *pName1,      /* The name of the trigger */
   99215                 :   Token *pName2,      /* The name of the trigger */
   99216                 :   int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
   99217                 :   int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
   99218                 :   IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
   99219                 :   SrcList *pTableName,/* The name of the table/view the trigger applies to */
   99220                 :   Expr *pWhen,        /* WHEN clause */
   99221                 :   int isTemp,         /* True if the TEMPORARY keyword is present */
   99222                 :   int noErr           /* Suppress errors if the trigger already exists */
   99223                 : ){
   99224           11328 :   Trigger *pTrigger = 0;  /* The new trigger */
   99225                 :   Table *pTab;            /* Table that the trigger fires off of */
   99226           11328 :   char *zName = 0;        /* Name of the trigger */
   99227           11328 :   sqlite3 *db = pParse->db;  /* The database connection */
   99228                 :   int iDb;                /* The database to store the trigger in */
   99229                 :   Token *pName;           /* The unqualified db name */
   99230                 :   DbFixer sFix;           /* State vector for the DB fixer */
   99231                 :   int iTabDb;             /* Index of the database holding pTab */
   99232                 : 
   99233           11328 :   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
   99234           11328 :   assert( pName2!=0 );
   99235           11328 :   assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
   99236           11328 :   assert( op>0 && op<0xff );
   99237           11328 :   if( isTemp ){
   99238                 :     /* If TEMP was specified, then the trigger name may not be qualified. */
   99239            1734 :     if( pName2->n>0 ){
   99240               0 :       sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
   99241               0 :       goto trigger_cleanup;
   99242                 :     }
   99243            1734 :     iDb = 1;
   99244            1734 :     pName = pName1;
   99245                 :   }else{
   99246                 :     /* Figure out the db that the the trigger will be created in */
   99247            9594 :     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
   99248            9594 :     if( iDb<0 ){
   99249               0 :       goto trigger_cleanup;
   99250                 :     }
   99251                 :   }
   99252           11328 :   if( !pTableName || db->mallocFailed ){
   99253                 :     goto trigger_cleanup;
   99254                 :   }
   99255                 : 
   99256                 :   /* A long-standing parser bug is that this syntax was allowed:
   99257                 :   **
   99258                 :   **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
   99259                 :   **                                                 ^^^^^^^^
   99260                 :   **
   99261                 :   ** To maintain backwards compatibility, ignore the database
   99262                 :   ** name on pTableName if we are reparsing our of SQLITE_MASTER.
   99263                 :   */
   99264           11328 :   if( db->init.busy && iDb!=1 ){
   99265            5409 :     sqlite3DbFree(db, pTableName->a[0].zDatabase);
   99266            5409 :     pTableName->a[0].zDatabase = 0;
   99267                 :   }
   99268                 : 
   99269                 :   /* If the trigger name was unqualified, and the table is a temp table,
   99270                 :   ** then set iDb to 1 to create the trigger in the temporary database.
   99271                 :   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
   99272                 :   ** exist, the error is caught by the block below.
   99273                 :   */
   99274           11328 :   pTab = sqlite3SrcListLookup(pParse, pTableName);
   99275           11328 :   if( db->init.busy==0 && pName2->n==0 && pTab
   99276            2535 :         && pTab->pSchema==db->aDb[1].pSchema ){
   99277             128 :     iDb = 1;
   99278                 :   }
   99279                 : 
   99280                 :   /* Ensure the table name matches database name and that the table exists */
   99281           11328 :   if( db->mallocFailed ) goto trigger_cleanup;
   99282           11328 :   assert( pTableName->nSrc==1 );
   99283           17538 :   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && 
   99284            6210 :       sqlite3FixSrcList(&sFix, pTableName) ){
   99285               0 :     goto trigger_cleanup;
   99286                 :   }
   99287           11328 :   pTab = sqlite3SrcListLookup(pParse, pTableName);
   99288           11328 :   if( !pTab ){
   99289                 :     /* The table does not exist. */
   99290               0 :     if( db->init.iDb==1 ){
   99291                 :       /* Ticket #3810.
   99292                 :       ** Normally, whenever a table is dropped, all associated triggers are
   99293                 :       ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
   99294                 :       ** and the table is dropped by a different database connection, the
   99295                 :       ** trigger is not visible to the database connection that does the
   99296                 :       ** drop so the trigger cannot be dropped.  This results in an
   99297                 :       ** "orphaned trigger" - a trigger whose associated table is missing.
   99298                 :       */
   99299               0 :       db->init.orphanTrigger = 1;
   99300                 :     }
   99301               0 :     goto trigger_cleanup;
   99302                 :   }
   99303           11328 :   if( IsVirtual(pTab) ){
   99304               0 :     sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
   99305               0 :     goto trigger_cleanup;
   99306                 :   }
   99307                 : 
   99308                 :   /* Check that the trigger name is not reserved and that no trigger of the
   99309                 :   ** specified name exists */
   99310           11328 :   zName = sqlite3NameFromToken(db, pName);
   99311           11328 :   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
   99312                 :     goto trigger_cleanup;
   99313                 :   }
   99314           11328 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   99315           11328 :   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
   99316                 :                       zName, sqlite3Strlen30(zName)) ){
   99317               0 :     if( !noErr ){
   99318               0 :       sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
   99319                 :     }else{
   99320               0 :       assert( !db->init.busy );
   99321               0 :       sqlite3CodeVerifySchema(pParse, iDb);
   99322                 :     }
   99323               0 :     goto trigger_cleanup;
   99324                 :   }
   99325                 : 
   99326                 :   /* Do not create a trigger on a system table */
   99327           11328 :   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
   99328               0 :     sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
   99329               0 :     pParse->nErr++;
   99330               0 :     goto trigger_cleanup;
   99331                 :   }
   99332                 : 
   99333                 :   /* INSTEAD of triggers are only for views and views only support INSTEAD
   99334                 :   ** of triggers.
   99335                 :   */
   99336           11328 :   if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
   99337               0 :     sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", 
   99338                 :         (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
   99339               0 :     goto trigger_cleanup;
   99340                 :   }
   99341           11328 :   if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
   99342               0 :     sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
   99343                 :         " trigger on table: %S", pTableName, 0);
   99344               0 :     goto trigger_cleanup;
   99345                 :   }
   99346           11328 :   iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   99347                 : 
   99348                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   99349                 :   {
   99350           11328 :     int code = SQLITE_CREATE_TRIGGER;
   99351           11328 :     const char *zDb = db->aDb[iTabDb].zName;
   99352           11328 :     const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
   99353           11328 :     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
   99354           11328 :     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
   99355               0 :       goto trigger_cleanup;
   99356                 :     }
   99357           11328 :     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
   99358               0 :       goto trigger_cleanup;
   99359                 :     }
   99360                 :   }
   99361                 : #endif
   99362                 : 
   99363                 :   /* INSTEAD OF triggers can only appear on views and BEFORE triggers
   99364                 :   ** cannot appear on views.  So we might as well translate every
   99365                 :   ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
   99366                 :   ** elsewhere.
   99367                 :   */
   99368           11328 :   if (tr_tm == TK_INSTEAD){
   99369             156 :     tr_tm = TK_BEFORE;
   99370                 :   }
   99371                 : 
   99372                 :   /* Build the Trigger object */
   99373           11328 :   pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
   99374           11328 :   if( pTrigger==0 ) goto trigger_cleanup;
   99375           11328 :   pTrigger->zName = zName;
   99376           11328 :   zName = 0;
   99377           11328 :   pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
   99378           11328 :   pTrigger->pSchema = db->aDb[iDb].pSchema;
   99379           11328 :   pTrigger->pTabSchema = pTab->pSchema;
   99380           11328 :   pTrigger->op = (u8)op;
   99381           11328 :   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
   99382           11328 :   pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
   99383           11328 :   pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
   99384           11328 :   assert( pParse->pNewTrigger==0 );
   99385           11328 :   pParse->pNewTrigger = pTrigger;
   99386                 : 
   99387                 : trigger_cleanup:
   99388           11328 :   sqlite3DbFree(db, zName);
   99389           11328 :   sqlite3SrcListDelete(db, pTableName);
   99390           11328 :   sqlite3IdListDelete(db, pColumns);
   99391           11328 :   sqlite3ExprDelete(db, pWhen);
   99392           11328 :   if( !pParse->pNewTrigger ){
   99393               0 :     sqlite3DeleteTrigger(db, pTrigger);
   99394                 :   }else{
   99395           11328 :     assert( pParse->pNewTrigger==pTrigger );
   99396                 :   }
   99397           11328 : }
   99398                 : 
   99399                 : /*
   99400                 : ** This routine is called after all of the trigger actions have been parsed
   99401                 : ** in order to complete the process of building the trigger.
   99402                 : */
   99403           11328 : SQLITE_PRIVATE void sqlite3FinishTrigger(
   99404                 :   Parse *pParse,          /* Parser context */
   99405                 :   TriggerStep *pStepList, /* The triggered program */
   99406                 :   Token *pAll             /* Token that describes the complete CREATE TRIGGER */
   99407                 : ){
   99408           11328 :   Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
   99409                 :   char *zName;                            /* Name of trigger */
   99410           11328 :   sqlite3 *db = pParse->db;               /* The database */
   99411                 :   DbFixer sFix;                           /* Fixer object */
   99412                 :   int iDb;                                /* Database containing the trigger */
   99413                 :   Token nameToken;                        /* Trigger name for error reporting */
   99414                 : 
   99415           11328 :   pParse->pNewTrigger = 0;
   99416           11328 :   if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
   99417           11328 :   zName = pTrig->zName;
   99418           11328 :   iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
   99419           11328 :   pTrig->step_list = pStepList;
   99420           37904 :   while( pStepList ){
   99421           15248 :     pStepList->pTrig = pTrig;
   99422           15248 :     pStepList = pStepList->pNext;
   99423                 :   }
   99424           11328 :   nameToken.z = pTrig->zName;
   99425           11328 :   nameToken.n = sqlite3Strlen30(nameToken.z);
   99426           11328 :   if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 
   99427            6210 :           && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
   99428               0 :     goto triggerfinish_cleanup;
   99429                 :   }
   99430                 : 
   99431                 :   /* if we are not initializing,
   99432                 :   ** build the sqlite_master entry
   99433                 :   */
   99434           11328 :   if( !db->init.busy ){
   99435                 :     Vdbe *v;
   99436                 :     char *z;
   99437                 : 
   99438                 :     /* Make an entry in the sqlite_master table */
   99439            2535 :     v = sqlite3GetVdbe(pParse);
   99440            2535 :     if( v==0 ) goto triggerfinish_cleanup;
   99441            2535 :     sqlite3BeginWriteOperation(pParse, 0, iDb);
   99442            2535 :     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
   99443            5070 :     sqlite3NestedParse(pParse,
   99444                 :        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
   99445            2535 :        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
   99446                 :        pTrig->table, z);
   99447            2535 :     sqlite3DbFree(db, z);
   99448            2535 :     sqlite3ChangeCookie(pParse, iDb);
   99449            2535 :     sqlite3VdbeAddParseSchemaOp(v, iDb,
   99450                 :         sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
   99451                 :   }
   99452                 : 
   99453           11328 :   if( db->init.busy ){
   99454            8793 :     Trigger *pLink = pTrig;
   99455            8793 :     Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
   99456            8793 :     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   99457            8793 :     pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
   99458            8793 :     if( pTrig ){
   99459               0 :       db->mallocFailed = 1;
   99460            8793 :     }else if( pLink->pSchema==pLink->pTabSchema ){
   99461                 :       Table *pTab;
   99462            5537 :       int n = sqlite3Strlen30(pLink->table);
   99463            5537 :       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
   99464            5537 :       assert( pTab!=0 );
   99465            5537 :       pLink->pNext = pTab->pTrigger;
   99466            5537 :       pTab->pTrigger = pLink;
   99467                 :     }
   99468                 :   }
   99469                 : 
   99470                 : triggerfinish_cleanup:
   99471           11328 :   sqlite3DeleteTrigger(db, pTrig);
   99472           11328 :   assert( !pParse->pNewTrigger );
   99473           11328 :   sqlite3DeleteTriggerStep(db, pStepList);
   99474           11328 : }
   99475                 : 
   99476                 : /*
   99477                 : ** Turn a SELECT statement (that the pSelect parameter points to) into
   99478                 : ** a trigger step.  Return a pointer to a TriggerStep structure.
   99479                 : **
   99480                 : ** The parser calls this routine when it finds a SELECT statement in
   99481                 : ** body of a TRIGGER.  
   99482                 : */
   99483            1928 : SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
   99484            1928 :   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   99485            1928 :   if( pTriggerStep==0 ) {
   99486               0 :     sqlite3SelectDelete(db, pSelect);
   99487               0 :     return 0;
   99488                 :   }
   99489            1928 :   pTriggerStep->op = TK_SELECT;
   99490            1928 :   pTriggerStep->pSelect = pSelect;
   99491            1928 :   pTriggerStep->orconf = OE_Default;
   99492            1928 :   return pTriggerStep;
   99493                 : }
   99494                 : 
   99495                 : /*
   99496                 : ** Allocate space to hold a new trigger step.  The allocated space
   99497                 : ** holds both the TriggerStep object and the TriggerStep.target.z string.
   99498                 : **
   99499                 : ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
   99500                 : */
   99501           13320 : static TriggerStep *triggerStepAllocate(
   99502                 :   sqlite3 *db,                /* Database connection */
   99503                 :   u8 op,                      /* Trigger opcode */
   99504                 :   Token *pName                /* The target name */
   99505                 : ){
   99506                 :   TriggerStep *pTriggerStep;
   99507                 : 
   99508           13320 :   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
   99509           13320 :   if( pTriggerStep ){
   99510           13320 :     char *z = (char*)&pTriggerStep[1];
   99511           13320 :     memcpy(z, pName->z, pName->n);
   99512           13320 :     pTriggerStep->target.z = z;
   99513           13320 :     pTriggerStep->target.n = pName->n;
   99514           13320 :     pTriggerStep->op = op;
   99515                 :   }
   99516           13320 :   return pTriggerStep;
   99517                 : }
   99518                 : 
   99519                 : /*
   99520                 : ** Build a trigger step out of an INSERT statement.  Return a pointer
   99521                 : ** to the new trigger step.
   99522                 : **
   99523                 : ** The parser calls this routine when it sees an INSERT inside the
   99524                 : ** body of a trigger.
   99525                 : */
   99526             805 : SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
   99527                 :   sqlite3 *db,        /* The database connection */
   99528                 :   Token *pTableName,  /* Name of the table into which we insert */
   99529                 :   IdList *pColumn,    /* List of columns in pTableName to insert into */
   99530                 :   ExprList *pEList,   /* The VALUE clause: a list of values to be inserted */
   99531                 :   Select *pSelect,    /* A SELECT statement that supplies values */
   99532                 :   u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
   99533                 : ){
   99534                 :   TriggerStep *pTriggerStep;
   99535                 : 
   99536             805 :   assert(pEList == 0 || pSelect == 0);
   99537             805 :   assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
   99538                 : 
   99539             805 :   pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
   99540             805 :   if( pTriggerStep ){
   99541             805 :     pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
   99542             805 :     pTriggerStep->pIdList = pColumn;
   99543             805 :     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
   99544             805 :     pTriggerStep->orconf = orconf;
   99545                 :   }else{
   99546               0 :     sqlite3IdListDelete(db, pColumn);
   99547                 :   }
   99548             805 :   sqlite3ExprListDelete(db, pEList);
   99549             805 :   sqlite3SelectDelete(db, pSelect);
   99550                 : 
   99551             805 :   return pTriggerStep;
   99552                 : }
   99553                 : 
   99554                 : /*
   99555                 : ** Construct a trigger step that implements an UPDATE statement and return
   99556                 : ** a pointer to that trigger step.  The parser calls this routine when it
   99557                 : ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
   99558                 : */
   99559            3276 : SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
   99560                 :   sqlite3 *db,         /* The database connection */
   99561                 :   Token *pTableName,   /* Name of the table to be updated */
   99562                 :   ExprList *pEList,    /* The SET clause: list of column and new values */
   99563                 :   Expr *pWhere,        /* The WHERE clause */
   99564                 :   u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
   99565                 : ){
   99566                 :   TriggerStep *pTriggerStep;
   99567                 : 
   99568            3276 :   pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
   99569            3276 :   if( pTriggerStep ){
   99570            3276 :     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
   99571            3276 :     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
   99572            3276 :     pTriggerStep->orconf = orconf;
   99573                 :   }
   99574            3276 :   sqlite3ExprListDelete(db, pEList);
   99575            3276 :   sqlite3ExprDelete(db, pWhere);
   99576            3276 :   return pTriggerStep;
   99577                 : }
   99578                 : 
   99579                 : /*
   99580                 : ** Construct a trigger step that implements a DELETE statement and return
   99581                 : ** a pointer to that trigger step.  The parser calls this routine when it
   99582                 : ** sees a DELETE statement inside the body of a CREATE TRIGGER.
   99583                 : */
   99584            9239 : SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
   99585                 :   sqlite3 *db,            /* Database connection */
   99586                 :   Token *pTableName,      /* The table from which rows are deleted */
   99587                 :   Expr *pWhere            /* The WHERE clause */
   99588                 : ){
   99589                 :   TriggerStep *pTriggerStep;
   99590                 : 
   99591            9239 :   pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
   99592            9239 :   if( pTriggerStep ){
   99593            9239 :     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
   99594            9239 :     pTriggerStep->orconf = OE_Default;
   99595                 :   }
   99596            9239 :   sqlite3ExprDelete(db, pWhere);
   99597            9239 :   return pTriggerStep;
   99598                 : }
   99599                 : 
   99600                 : /* 
   99601                 : ** Recursively delete a Trigger structure
   99602                 : */
   99603          213989 : SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
   99604          213989 :   if( pTrigger==0 ) return;
   99605           11337 :   sqlite3DeleteTriggerStep(db, pTrigger->step_list);
   99606           11337 :   sqlite3DbFree(db, pTrigger->zName);
   99607           11337 :   sqlite3DbFree(db, pTrigger->table);
   99608           11337 :   sqlite3ExprDelete(db, pTrigger->pWhen);
   99609           11337 :   sqlite3IdListDelete(db, pTrigger->pColumns);
   99610           11337 :   sqlite3DbFree(db, pTrigger);
   99611                 : }
   99612                 : 
   99613                 : /*
   99614                 : ** This function is called to drop a trigger from the database schema. 
   99615                 : **
   99616                 : ** This may be called directly from the parser and therefore identifies
   99617                 : ** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
   99618                 : ** same job as this routine except it takes a pointer to the trigger
   99619                 : ** instead of the trigger name.
   99620                 : **/
   99621              71 : SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
   99622              71 :   Trigger *pTrigger = 0;
   99623                 :   int i;
   99624                 :   const char *zDb;
   99625                 :   const char *zName;
   99626                 :   int nName;
   99627              71 :   sqlite3 *db = pParse->db;
   99628                 : 
   99629              71 :   if( db->mallocFailed ) goto drop_trigger_cleanup;
   99630              71 :   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
   99631               0 :     goto drop_trigger_cleanup;
   99632                 :   }
   99633                 : 
   99634              71 :   assert( pName->nSrc==1 );
   99635              71 :   zDb = pName->a[0].zDatabase;
   99636              71 :   zName = pName->a[0].zName;
   99637              71 :   nName = sqlite3Strlen30(zName);
   99638              71 :   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
   99639             120 :   for(i=OMIT_TEMPDB; i<db->nDb; i++){
   99640             100 :     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
   99641             100 :     if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
   99642             100 :     assert( sqlite3SchemaMutexHeld(db, j, 0) );
   99643             100 :     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
   99644             100 :     if( pTrigger ) break;
   99645                 :   }
   99646              71 :   if( !pTrigger ){
   99647              20 :     if( !noErr ){
   99648               0 :       sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
   99649                 :     }else{
   99650              20 :       sqlite3CodeVerifyNamedSchema(pParse, zDb);
   99651                 :     }
   99652              20 :     pParse->checkSchema = 1;
   99653              20 :     goto drop_trigger_cleanup;
   99654                 :   }
   99655              51 :   sqlite3DropTriggerPtr(pParse, pTrigger);
   99656                 : 
   99657                 : drop_trigger_cleanup:
   99658              71 :   sqlite3SrcListDelete(db, pName);
   99659              71 : }
   99660                 : 
   99661                 : /*
   99662                 : ** Return a pointer to the Table structure for the table that a trigger
   99663                 : ** is set on.
   99664                 : */
   99665            8772 : static Table *tableOfTrigger(Trigger *pTrigger){
   99666            8772 :   int n = sqlite3Strlen30(pTrigger->table);
   99667            8772 :   return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
   99668                 : }
   99669                 : 
   99670                 : 
   99671                 : /*
   99672                 : ** Drop a trigger given a pointer to that trigger. 
   99673                 : */
   99674              51 : SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
   99675                 :   Table   *pTable;
   99676                 :   Vdbe *v;
   99677              51 :   sqlite3 *db = pParse->db;
   99678                 :   int iDb;
   99679                 : 
   99680              51 :   iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
   99681              51 :   assert( iDb>=0 && iDb<db->nDb );
   99682              51 :   pTable = tableOfTrigger(pTrigger);
   99683              51 :   assert( pTable );
   99684              51 :   assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
   99685                 : #ifndef SQLITE_OMIT_AUTHORIZATION
   99686                 :   {
   99687              51 :     int code = SQLITE_DROP_TRIGGER;
   99688              51 :     const char *zDb = db->aDb[iDb].zName;
   99689              51 :     const char *zTab = SCHEMA_TABLE(iDb);
   99690              51 :     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
   99691             102 :     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
   99692              51 :       sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
   99693               0 :       return;
   99694                 :     }
   99695                 :   }
   99696                 : #endif
   99697                 : 
   99698                 :   /* Generate code to destroy the database record of the trigger.
   99699                 :   */
   99700              51 :   assert( pTable!=0 );
   99701              51 :   if( (v = sqlite3GetVdbe(pParse))!=0 ){
   99702                 :     int base;
   99703                 :     static const VdbeOpList dropTrigger[] = {
   99704                 :       { OP_Rewind,     0, ADDR(9),  0},
   99705                 :       { OP_String8,    0, 1,        0}, /* 1 */
   99706                 :       { OP_Column,     0, 1,        2},
   99707                 :       { OP_Ne,         2, ADDR(8),  1},
   99708                 :       { OP_String8,    0, 1,        0}, /* 4: "trigger" */
   99709                 :       { OP_Column,     0, 0,        2},
   99710                 :       { OP_Ne,         2, ADDR(8),  1},
   99711                 :       { OP_Delete,     0, 0,        0},
   99712                 :       { OP_Next,       0, ADDR(1),  0}, /* 8 */
   99713                 :     };
   99714                 : 
   99715              51 :     sqlite3BeginWriteOperation(pParse, 0, iDb);
   99716              51 :     sqlite3OpenMasterTable(pParse, iDb);
   99717              51 :     base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
   99718              51 :     sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
   99719              51 :     sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
   99720              51 :     sqlite3ChangeCookie(pParse, iDb);
   99721              51 :     sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
   99722              51 :     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
   99723              51 :     if( pParse->nMem<3 ){
   99724              51 :       pParse->nMem = 3;
   99725                 :     }
   99726                 :   }
   99727                 : }
   99728                 : 
   99729                 : /*
   99730                 : ** Remove a trigger from the hash tables of the sqlite* pointer.
   99731                 : */
   99732              54 : SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
   99733                 :   Trigger *pTrigger;
   99734                 :   Hash *pHash;
   99735                 : 
   99736              54 :   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   99737              54 :   pHash = &(db->aDb[iDb].pSchema->trigHash);
   99738              54 :   pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
   99739              54 :   if( ALWAYS(pTrigger) ){
   99740              54 :     if( pTrigger->pSchema==pTrigger->pTabSchema ){
   99741              38 :       Table *pTab = tableOfTrigger(pTrigger);
   99742                 :       Trigger **pp;
   99743              38 :       for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
   99744              38 :       *pp = (*pp)->pNext;
   99745                 :     }
   99746              54 :     sqlite3DeleteTrigger(db, pTrigger);
   99747              54 :     db->flags |= SQLITE_InternChanges;
   99748                 :   }
   99749              54 : }
   99750                 : 
   99751                 : /*
   99752                 : ** pEList is the SET clause of an UPDATE statement.  Each entry
   99753                 : ** in pEList is of the format <id>=<expr>.  If any of the entries
   99754                 : ** in pEList have an <id> which matches an identifier in pIdList,
   99755                 : ** then return TRUE.  If pIdList==NULL, then it is considered a
   99756                 : ** wildcard that matches anything.  Likewise if pEList==NULL then
   99757                 : ** it matches anything so always return true.  Return false only
   99758                 : ** if there is no match.
   99759                 : */
   99760           12301 : static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
   99761                 :   int e;
   99762           12301 :   if( pIdList==0 || NEVER(pEList==0) ) return 1;
   99763            9251 :   for(e=0; e<pEList->nExpr; e++){
   99764            5976 :     if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
   99765                 :   }
   99766            3275 :   return 0; 
   99767                 : }
   99768                 : 
   99769                 : /*
   99770                 : ** Return a list of all triggers on table pTab if there exists at least
   99771                 : ** one trigger that must be fired when an operation of type 'op' is 
   99772                 : ** performed on the table, and, if that operation is an UPDATE, if at
   99773                 : ** least one of the columns in pChanges is being modified.
   99774                 : */
   99775           41562 : SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
   99776                 :   Parse *pParse,          /* Parse context */
   99777                 :   Table *pTab,            /* The table the contains the triggers */
   99778                 :   int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
   99779                 :   ExprList *pChanges,     /* Columns that change in an UPDATE statement */
   99780                 :   int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
   99781                 : ){
   99782           41562 :   int mask = 0;
   99783           41562 :   Trigger *pList = 0;
   99784                 :   Trigger *p;
   99785                 : 
   99786           41562 :   if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
   99787           41562 :     pList = sqlite3TriggerList(pParse, pTab);
   99788                 :   }
   99789           41562 :   assert( pList==0 || IsVirtual(pTab)==0 );
   99790           55938 :   for(p=pList; p; p=p->pNext){
   99791           14376 :     if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
   99792            3286 :       mask |= p->tr_tm;
   99793                 :     }
   99794                 :   }
   99795           41562 :   if( pMask ){
   99796           33432 :     *pMask = mask;
   99797                 :   }
   99798           41562 :   return (mask ? pList : 0);
   99799                 : }
   99800                 : 
   99801                 : /*
   99802                 : ** Convert the pStep->target token into a SrcList and return a pointer
   99803                 : ** to that SrcList.
   99804                 : **
   99805                 : ** This routine adds a specific database name, if needed, to the target when
   99806                 : ** forming the SrcList.  This prevents a trigger in one database from
   99807                 : ** referring to a target in another database.  An exception is when the
   99808                 : ** trigger is in TEMP in which case it can refer to any other database it
   99809                 : ** wants.
   99810                 : */
   99811            3829 : static SrcList *targetSrcList(
   99812                 :   Parse *pParse,       /* The parsing context */
   99813                 :   TriggerStep *pStep   /* The trigger containing the target token */
   99814                 : ){
   99815                 :   int iDb;             /* Index of the database to use */
   99816                 :   SrcList *pSrc;       /* SrcList to be returned */
   99817                 : 
   99818            3829 :   pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
   99819            3829 :   if( pSrc ){
   99820            3829 :     assert( pSrc->nSrc>0 );
   99821            3829 :     assert( pSrc->a!=0 );
   99822            3829 :     iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
   99823            3829 :     if( iDb==0 || iDb>=2 ){
   99824            2267 :       sqlite3 *db = pParse->db;
   99825            2267 :       assert( iDb<pParse->db->nDb );
   99826            2267 :       pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
   99827                 :     }
   99828                 :   }
   99829            3829 :   return pSrc;
   99830                 : }
   99831                 : 
   99832                 : /*
   99833                 : ** Generate VDBE code for the statements inside the body of a single 
   99834                 : ** trigger.
   99835                 : */
   99836            3097 : static int codeTriggerProgram(
   99837                 :   Parse *pParse,            /* The parser context */
   99838                 :   TriggerStep *pStepList,   /* List of statements inside the trigger body */
   99839                 :   int orconf                /* Conflict algorithm. (OE_Abort, etc) */  
   99840                 : ){
   99841                 :   TriggerStep *pStep;
   99842            3097 :   Vdbe *v = pParse->pVdbe;
   99843            3097 :   sqlite3 *db = pParse->db;
   99844                 : 
   99845            3097 :   assert( pParse->pTriggerTab && pParse->pToplevel );
   99846            3097 :   assert( pStepList );
   99847            3097 :   assert( v!=0 );
   99848            7318 :   for(pStep=pStepList; pStep; pStep=pStep->pNext){
   99849                 :     /* Figure out the ON CONFLICT policy that will be used for this step
   99850                 :     ** of the trigger program. If the statement that caused this trigger
   99851                 :     ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
   99852                 :     ** the ON CONFLICT policy that was specified as part of the trigger
   99853                 :     ** step statement. Example:
   99854                 :     **
   99855                 :     **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
   99856                 :     **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
   99857                 :     **   END;
   99858                 :     **
   99859                 :     **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
   99860                 :     **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
   99861                 :     */
   99862            4221 :     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
   99863                 : 
   99864            4221 :     switch( pStep->op ){
   99865                 :       case TK_UPDATE: {
   99866            1009 :         sqlite3Update(pParse, 
   99867                 :           targetSrcList(pParse, pStep),
   99868                 :           sqlite3ExprListDup(db, pStep->pExprList, 0), 
   99869                 :           sqlite3ExprDup(db, pStep->pWhere, 0), 
   99870            1009 :           pParse->eOrconf
   99871                 :         );
   99872            1009 :         break;
   99873                 :       }
   99874                 :       case TK_INSERT: {
   99875             308 :         sqlite3Insert(pParse, 
   99876                 :           targetSrcList(pParse, pStep),
   99877                 :           sqlite3ExprListDup(db, pStep->pExprList, 0), 
   99878                 :           sqlite3SelectDup(db, pStep->pSelect, 0), 
   99879                 :           sqlite3IdListDup(db, pStep->pIdList), 
   99880             308 :           pParse->eOrconf
   99881                 :         );
   99882             308 :         break;
   99883                 :       }
   99884                 :       case TK_DELETE: {
   99885            2512 :         sqlite3DeleteFrom(pParse, 
   99886                 :           targetSrcList(pParse, pStep),
   99887                 :           sqlite3ExprDup(db, pStep->pWhere, 0)
   99888                 :         );
   99889            2512 :         break;
   99890                 :       }
   99891             392 :       default: assert( pStep->op==TK_SELECT ); {
   99892                 :         SelectDest sDest;
   99893             392 :         Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
   99894             392 :         sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
   99895             392 :         sqlite3Select(pParse, pSelect, &sDest);
   99896             392 :         sqlite3SelectDelete(db, pSelect);
   99897             392 :         break;
   99898                 :       }
   99899                 :     } 
   99900            4221 :     if( pStep->op!=TK_SELECT ){
   99901            3829 :       sqlite3VdbeAddOp0(v, OP_ResetCount);
   99902                 :     }
   99903                 :   }
   99904                 : 
   99905            3097 :   return 0;
   99906                 : }
   99907                 : 
   99908                 : #ifdef SQLITE_DEBUG
   99909                 : /*
   99910                 : ** This function is used to add VdbeComment() annotations to a VDBE
   99911                 : ** program. It is not used in production code, only for debugging.
   99912                 : */
   99913            9634 : static const char *onErrorText(int onError){
   99914            9634 :   switch( onError ){
   99915             462 :     case OE_Abort:    return "abort";
   99916               0 :     case OE_Rollback: return "rollback";
   99917               0 :     case OE_Fail:     return "fail";
   99918              81 :     case OE_Replace:  return "replace";
   99919             753 :     case OE_Ignore:   return "ignore";
   99920            8338 :     case OE_Default:  return "default";
   99921                 :   }
   99922               0 :   return "n/a";
   99923                 : }
   99924                 : #endif
   99925                 : 
   99926                 : /*
   99927                 : ** Parse context structure pFrom has just been used to create a sub-vdbe
   99928                 : ** (trigger program). If an error has occurred, transfer error information
   99929                 : ** from pFrom to pTo.
   99930                 : */
   99931            3097 : static void transferParseError(Parse *pTo, Parse *pFrom){
   99932            3097 :   assert( pFrom->zErrMsg==0 || pFrom->nErr );
   99933            3097 :   assert( pTo->zErrMsg==0 || pTo->nErr );
   99934            3097 :   if( pTo->nErr==0 ){
   99935            3097 :     pTo->zErrMsg = pFrom->zErrMsg;
   99936            3097 :     pTo->nErr = pFrom->nErr;
   99937                 :   }else{
   99938               0 :     sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
   99939                 :   }
   99940            3097 : }
   99941                 : 
   99942                 : /*
   99943                 : ** Create and populate a new TriggerPrg object with a sub-program 
   99944                 : ** implementing trigger pTrigger with ON CONFLICT policy orconf.
   99945                 : */
   99946            3097 : static TriggerPrg *codeRowTrigger(
   99947                 :   Parse *pParse,       /* Current parse context */
   99948                 :   Trigger *pTrigger,   /* Trigger to code */
   99949                 :   Table *pTab,         /* The table pTrigger is attached to */
   99950                 :   int orconf           /* ON CONFLICT policy to code trigger program with */
   99951                 : ){
   99952            3097 :   Parse *pTop = sqlite3ParseToplevel(pParse);
   99953            3097 :   sqlite3 *db = pParse->db;   /* Database handle */
   99954                 :   TriggerPrg *pPrg;           /* Value to return */
   99955            3097 :   Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
   99956                 :   Vdbe *v;                    /* Temporary VM */
   99957                 :   NameContext sNC;            /* Name context for sub-vdbe */
   99958            3097 :   SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
   99959                 :   Parse *pSubParse;           /* Parse context for sub-vdbe */
   99960            3097 :   int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
   99961                 : 
   99962            3097 :   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
   99963            3097 :   assert( pTop->pVdbe );
   99964                 : 
   99965                 :   /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
   99966                 :   ** are freed if an error occurs, link them into the Parse.pTriggerPrg 
   99967                 :   ** list of the top-level Parse object sooner rather than later.  */
   99968            3097 :   pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
   99969            3097 :   if( !pPrg ) return 0;
   99970            3097 :   pPrg->pNext = pTop->pTriggerPrg;
   99971            3097 :   pTop->pTriggerPrg = pPrg;
   99972            3097 :   pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
   99973            3097 :   if( !pProgram ) return 0;
   99974            3097 :   sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
   99975            3097 :   pPrg->pTrigger = pTrigger;
   99976            3097 :   pPrg->orconf = orconf;
   99977            3097 :   pPrg->aColmask[0] = 0xffffffff;
   99978            3097 :   pPrg->aColmask[1] = 0xffffffff;
   99979                 : 
   99980                 :   /* Allocate and populate a new Parse context to use for coding the 
   99981                 :   ** trigger sub-program.  */
   99982            3097 :   pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
   99983            3097 :   if( !pSubParse ) return 0;
   99984            3097 :   memset(&sNC, 0, sizeof(sNC));
   99985            3097 :   sNC.pParse = pSubParse;
   99986            3097 :   pSubParse->db = db;
   99987            3097 :   pSubParse->pTriggerTab = pTab;
   99988            3097 :   pSubParse->pToplevel = pTop;
   99989            3097 :   pSubParse->zAuthContext = pTrigger->zName;
   99990            3097 :   pSubParse->eTriggerOp = pTrigger->op;
   99991            3097 :   pSubParse->nQueryLoop = pParse->nQueryLoop;
   99992                 : 
   99993            3097 :   v = sqlite3GetVdbe(pSubParse);
   99994            3097 :   if( v ){
   99995            3097 :     VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", 
   99996                 :       pTrigger->zName, onErrorText(orconf),
   99997                 :       (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
   99998                 :         (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
   99999                 :         (pTrigger->op==TK_INSERT ? "INSERT" : ""),
  100000                 :         (pTrigger->op==TK_DELETE ? "DELETE" : ""),
  100001                 :       pTab->zName
  100002                 :     ));
  100003                 : #ifndef SQLITE_OMIT_TRACE
  100004            3097 :     sqlite3VdbeChangeP4(v, -1, 
  100005            3097 :       sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
  100006                 :     );
  100007                 : #endif
  100008                 : 
  100009                 :     /* If one was specified, code the WHEN clause. If it evaluates to false
  100010                 :     ** (or NULL) the sub-vdbe is immediately halted by jumping to the 
  100011                 :     ** OP_Halt inserted at the end of the program.  */
  100012            3097 :     if( pTrigger->pWhen ){
  100013            1436 :       pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
  100014            1436 :       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) 
  100015            1436 :        && db->mallocFailed==0 
  100016                 :       ){
  100017            1436 :         iEndTrigger = sqlite3VdbeMakeLabel(v);
  100018            1436 :         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
  100019                 :       }
  100020            1436 :       sqlite3ExprDelete(db, pWhen);
  100021                 :     }
  100022                 : 
  100023                 :     /* Code the trigger program into the sub-vdbe. */
  100024            3097 :     codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
  100025                 : 
  100026                 :     /* Insert an OP_Halt at the end of the sub-program. */
  100027            3097 :     if( iEndTrigger ){
  100028            1436 :       sqlite3VdbeResolveLabel(v, iEndTrigger);
  100029                 :     }
  100030            3097 :     sqlite3VdbeAddOp0(v, OP_Halt);
  100031            3097 :     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
  100032                 : 
  100033            3097 :     transferParseError(pParse, pSubParse);
  100034            3097 :     if( db->mallocFailed==0 ){
  100035            3097 :       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
  100036                 :     }
  100037            3097 :     pProgram->nMem = pSubParse->nMem;
  100038            3097 :     pProgram->nCsr = pSubParse->nTab;
  100039            3097 :     pProgram->nOnce = pSubParse->nOnce;
  100040            3097 :     pProgram->token = (void *)pTrigger;
  100041            3097 :     pPrg->aColmask[0] = pSubParse->oldmask;
  100042            3097 :     pPrg->aColmask[1] = pSubParse->newmask;
  100043            3097 :     sqlite3VdbeDelete(v);
  100044                 :   }
  100045                 : 
  100046            3097 :   assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
  100047            3097 :   assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
  100048            3097 :   sqlite3StackFree(db, pSubParse);
  100049                 : 
  100050            3097 :   return pPrg;
  100051                 : }
  100052                 :     
  100053                 : /*
  100054                 : ** Return a pointer to a TriggerPrg object containing the sub-program for
  100055                 : ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
  100056                 : ** TriggerPrg object exists, a new object is allocated and populated before
  100057                 : ** being returned.
  100058                 : */
  100059            5894 : static TriggerPrg *getRowTrigger(
  100060                 :   Parse *pParse,       /* Current parse context */
  100061                 :   Trigger *pTrigger,   /* Trigger to code */
  100062                 :   Table *pTab,         /* The table trigger pTrigger is attached to */
  100063                 :   int orconf           /* ON CONFLICT algorithm. */
  100064                 : ){
  100065            5894 :   Parse *pRoot = sqlite3ParseToplevel(pParse);
  100066                 :   TriggerPrg *pPrg;
  100067                 : 
  100068            5894 :   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
  100069                 : 
  100070                 :   /* It may be that this trigger has already been coded (or is in the
  100071                 :   ** process of being coded). If this is the case, then an entry with
  100072                 :   ** a matching TriggerPrg.pTrigger field will be present somewhere
  100073                 :   ** in the Parse.pTriggerPrg list. Search for such an entry.  */
  100074           14197 :   for(pPrg=pRoot->pTriggerPrg; 
  100075            5206 :       pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); 
  100076            2409 :       pPrg=pPrg->pNext
  100077                 :   );
  100078                 : 
  100079                 :   /* If an existing TriggerPrg could not be located, create a new one. */
  100080            5894 :   if( !pPrg ){
  100081            3097 :     pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
  100082                 :   }
  100083                 : 
  100084            5894 :   return pPrg;
  100085                 : }
  100086                 : 
  100087                 : /*
  100088                 : ** Generate code for the trigger program associated with trigger p on 
  100089                 : ** table pTab. The reg, orconf and ignoreJump parameters passed to this
  100090                 : ** function are the same as those described in the header function for
  100091                 : ** sqlite3CodeRowTrigger()
  100092                 : */
  100093            3440 : SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
  100094                 :   Parse *pParse,       /* Parse context */
  100095                 :   Trigger *p,          /* Trigger to code */
  100096                 :   Table *pTab,         /* The table to code triggers from */
  100097                 :   int reg,             /* Reg array containing OLD.* and NEW.* values */
  100098                 :   int orconf,          /* ON CONFLICT policy */
  100099                 :   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
  100100                 : ){
  100101            3440 :   Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
  100102                 :   TriggerPrg *pPrg;
  100103            3440 :   pPrg = getRowTrigger(pParse, p, pTab, orconf);
  100104            3440 :   assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
  100105                 : 
  100106                 :   /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program 
  100107                 :   ** is a pointer to the sub-vdbe containing the trigger program.  */
  100108            3440 :   if( pPrg ){
  100109            3440 :     int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
  100110                 : 
  100111            3440 :     sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
  100112            3440 :     sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
  100113            3440 :     VdbeComment(
  100114                 :         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
  100115                 : 
  100116                 :     /* Set the P5 operand of the OP_Program instruction to non-zero if
  100117                 :     ** recursive invocation of this trigger program is disallowed. Recursive
  100118                 :     ** invocation is disallowed if (a) the sub-program is really a trigger,
  100119                 :     ** not a foreign key action, and (b) the flag to enable recursive triggers
  100120                 :     ** is clear.  */
  100121            3440 :     sqlite3VdbeChangeP5(v, (u8)bRecursive);
  100122                 :   }
  100123            3440 : }
  100124                 : 
  100125                 : /*
  100126                 : ** This is called to code the required FOR EACH ROW triggers for an operation
  100127                 : ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
  100128                 : ** is given by the op paramater. The tr_tm parameter determines whether the
  100129                 : ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
  100130                 : ** parameter pChanges is passed the list of columns being modified.
  100131                 : **
  100132                 : ** If there are no triggers that fire at the specified time for the specified
  100133                 : ** operation on pTab, this function is a no-op.
  100134                 : **
  100135                 : ** The reg argument is the address of the first in an array of registers 
  100136                 : ** that contain the values substituted for the new.* and old.* references
  100137                 : ** in the trigger program. If N is the number of columns in table pTab
  100138                 : ** (a copy of pTab->nCol), then registers are populated as follows:
  100139                 : **
  100140                 : **   Register       Contains
  100141                 : **   ------------------------------------------------------
  100142                 : **   reg+0          OLD.rowid
  100143                 : **   reg+1          OLD.* value of left-most column of pTab
  100144                 : **   ...            ...
  100145                 : **   reg+N          OLD.* value of right-most column of pTab
  100146                 : **   reg+N+1        NEW.rowid
  100147                 : **   reg+N+2        OLD.* value of left-most column of pTab
  100148                 : **   ...            ...
  100149                 : **   reg+N+N+1      NEW.* value of right-most column of pTab
  100150                 : **
  100151                 : ** For ON DELETE triggers, the registers containing the NEW.* values will
  100152                 : ** never be accessed by the trigger program, so they are not allocated or 
  100153                 : ** populated by the caller (there is no data to populate them with anyway). 
  100154                 : ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
  100155                 : ** are never accessed, and so are not allocated by the caller. So, for an
  100156                 : ** ON INSERT trigger, the value passed to this function as parameter reg
  100157                 : ** is not a readable register, although registers (reg+N) through 
  100158                 : ** (reg+N+N+1) are.
  100159                 : **
  100160                 : ** Parameter orconf is the default conflict resolution algorithm for the
  100161                 : ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
  100162                 : ** is the instruction that control should jump to if a trigger program
  100163                 : ** raises an IGNORE exception.
  100164                 : */
  100165           24459 : SQLITE_PRIVATE void sqlite3CodeRowTrigger(
  100166                 :   Parse *pParse,       /* Parse context */
  100167                 :   Trigger *pTrigger,   /* List of triggers on table pTab */
  100168                 :   int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
  100169                 :   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
  100170                 :   int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
  100171                 :   Table *pTab,         /* The table to code triggers from */
  100172                 :   int reg,             /* The first in an array of registers (see above) */
  100173                 :   int orconf,          /* ON CONFLICT policy */
  100174                 :   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
  100175                 : ){
  100176                 :   Trigger *p;          /* Used to iterate through pTrigger list */
  100177                 : 
  100178           24459 :   assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
  100179           24459 :   assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
  100180           24459 :   assert( (op==TK_UPDATE)==(pChanges!=0) );
  100181                 : 
  100182           34487 :   for(p=pTrigger; p; p=p->pNext){
  100183                 : 
  100184                 :     /* Sanity checking:  The schema for the trigger and for the table are
  100185                 :     ** always defined.  The trigger must be in the same schema as the table
  100186                 :     ** or else it must be a TEMP trigger. */
  100187           10028 :     assert( p->pSchema!=0 );
  100188           10028 :     assert( p->pTabSchema!=0 );
  100189           10028 :     assert( p->pSchema==p->pTabSchema 
  100190                 :          || p->pSchema==pParse->db->aDb[1].pSchema );
  100191                 : 
  100192                 :     /* Determine whether we should code this trigger */
  100193           10028 :     if( p->op==op 
  100194            5739 :      && p->tr_tm==tr_tm 
  100195            3693 :      && checkColumnOverlap(p->pColumns, pChanges)
  100196                 :     ){
  100197            3286 :       sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
  100198                 :     }
  100199                 :   }
  100200           24459 : }
  100201                 : 
  100202                 : /*
  100203                 : ** Triggers may access values stored in the old.* or new.* pseudo-table. 
  100204                 : ** This function returns a 32-bit bitmask indicating which columns of the 
  100205                 : ** old.* or new.* tables actually are used by triggers. This information 
  100206                 : ** may be used by the caller, for example, to avoid having to load the entire
  100207                 : ** old.* record into memory when executing an UPDATE or DELETE command.
  100208                 : **
  100209                 : ** Bit 0 of the returned mask is set if the left-most column of the
  100210                 : ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
  100211                 : ** the second leftmost column value is required, and so on. If there
  100212                 : ** are more than 32 columns in the table, and at least one of the columns
  100213                 : ** with an index greater than 32 may be accessed, 0xffffffff is returned.
  100214                 : **
  100215                 : ** It is not possible to determine if the old.rowid or new.rowid column is 
  100216                 : ** accessed by triggers. The caller must always assume that it is.
  100217                 : **
  100218                 : ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
  100219                 : ** applies to the old.* table. If 1, the new.* table.
  100220                 : **
  100221                 : ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
  100222                 : ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
  100223                 : ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
  100224                 : ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
  100225                 : ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
  100226                 : */
  100227           15918 : SQLITE_PRIVATE u32 sqlite3TriggerColmask(
  100228                 :   Parse *pParse,       /* Parse context */
  100229                 :   Trigger *pTrigger,   /* List of triggers on table pTab */
  100230                 :   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
  100231                 :   int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
  100232                 :   int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
  100233                 :   Table *pTab,         /* The table to code triggers from */
  100234                 :   int orconf           /* Default ON CONFLICT policy for trigger steps */
  100235                 : ){
  100236           15918 :   const int op = pChanges ? TK_UPDATE : TK_DELETE;
  100237           15918 :   u32 mask = 0;
  100238                 :   Trigger *p;
  100239                 : 
  100240           15918 :   assert( isNew==1 || isNew==0 );
  100241           22067 :   for(p=pTrigger; p; p=p->pNext){
  100242            6149 :     if( p->op==op && (tr_tm&p->tr_tm)
  100243            2861 :      && checkColumnOverlap(p->pColumns,pChanges)
  100244                 :     ){
  100245                 :       TriggerPrg *pPrg;
  100246            2454 :       pPrg = getRowTrigger(pParse, p, pTab, orconf);
  100247            2454 :       if( pPrg ){
  100248            2454 :         mask |= pPrg->aColmask[isNew];
  100249                 :       }
  100250                 :     }
  100251                 :   }
  100252                 : 
  100253           15918 :   return mask;
  100254                 : }
  100255                 : 
  100256                 : #endif /* !defined(SQLITE_OMIT_TRIGGER) */
  100257                 : 
  100258                 : /************** End of trigger.c *********************************************/
  100259                 : /************** Begin file update.c ******************************************/
  100260                 : /*
  100261                 : ** 2001 September 15
  100262                 : **
  100263                 : ** The author disclaims copyright to this source code.  In place of
  100264                 : ** a legal notice, here is a blessing:
  100265                 : **
  100266                 : **    May you do good and not evil.
  100267                 : **    May you find forgiveness for yourself and forgive others.
  100268                 : **    May you share freely, never taking more than you give.
  100269                 : **
  100270                 : *************************************************************************
  100271                 : ** This file contains C code routines that are called by the parser
  100272                 : ** to handle UPDATE statements.
  100273                 : */
  100274                 : 
  100275                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  100276                 : /* Forward declaration */
  100277                 : static void updateVirtualTable(
  100278                 :   Parse *pParse,       /* The parsing context */
  100279                 :   SrcList *pSrc,       /* The virtual table to be modified */
  100280                 :   Table *pTab,         /* The virtual table */
  100281                 :   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
  100282                 :   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
  100283                 :   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
  100284                 :   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
  100285                 :   int onError          /* ON CONFLICT strategy */
  100286                 : );
  100287                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
  100288                 : 
  100289                 : /*
  100290                 : ** The most recently coded instruction was an OP_Column to retrieve the
  100291                 : ** i-th column of table pTab. This routine sets the P4 parameter of the 
  100292                 : ** OP_Column to the default value, if any.
  100293                 : **
  100294                 : ** The default value of a column is specified by a DEFAULT clause in the 
  100295                 : ** column definition. This was either supplied by the user when the table
  100296                 : ** was created, or added later to the table definition by an ALTER TABLE
  100297                 : ** command. If the latter, then the row-records in the table btree on disk
  100298                 : ** may not contain a value for the column and the default value, taken
  100299                 : ** from the P4 parameter of the OP_Column instruction, is returned instead.
  100300                 : ** If the former, then all row-records are guaranteed to include a value
  100301                 : ** for the column and the P4 value is not required.
  100302                 : **
  100303                 : ** Column definitions created by an ALTER TABLE command may only have 
  100304                 : ** literal default values specified: a number, null or a string. (If a more
  100305                 : ** complicated default expression value was provided, it is evaluated 
  100306                 : ** when the ALTER TABLE is executed and one of the literal values written
  100307                 : ** into the sqlite_master table.)
  100308                 : **
  100309                 : ** Therefore, the P4 parameter is only required if the default value for
  100310                 : ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
  100311                 : ** function is capable of transforming these types of expressions into
  100312                 : ** sqlite3_value objects.
  100313                 : **
  100314                 : ** If parameter iReg is not negative, code an OP_RealAffinity instruction
  100315                 : ** on register iReg. This is used when an equivalent integer value is 
  100316                 : ** stored in place of an 8-byte floating point value in order to save 
  100317                 : ** space.
  100318                 : */
  100319          430246 : SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
  100320          430246 :   assert( pTab!=0 );
  100321          430246 :   if( !pTab->pSelect ){
  100322                 :     sqlite3_value *pValue;
  100323          430073 :     u8 enc = ENC(sqlite3VdbeDb(v));
  100324          430073 :     Column *pCol = &pTab->aCol[i];
  100325          430073 :     VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
  100326          430073 :     assert( i<pTab->nCol );
  100327          430073 :     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
  100328          430073 :                          pCol->affinity, &pValue);
  100329          430073 :     if( pValue ){
  100330           19889 :       sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
  100331                 :     }
  100332                 : #ifndef SQLITE_OMIT_FLOATING_POINT
  100333          430073 :     if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
  100334              23 :       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
  100335                 :     }
  100336                 : #endif
  100337                 :   }
  100338          430246 : }
  100339                 : 
  100340                 : /*
  100341                 : ** Process an UPDATE statement.
  100342                 : **
  100343                 : **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
  100344                 : **          \_______/ \________/     \______/       \________________/
  100345                 : *            onError   pTabList      pChanges             pWhere
  100346                 : */
  100347           13252 : SQLITE_PRIVATE void sqlite3Update(
  100348                 :   Parse *pParse,         /* The parser context */
  100349                 :   SrcList *pTabList,     /* The table in which we should change things */
  100350                 :   ExprList *pChanges,    /* Things to be changed */
  100351                 :   Expr *pWhere,          /* The WHERE clause.  May be null */
  100352                 :   int onError            /* How to handle constraint errors */
  100353                 : ){
  100354                 :   int i, j;              /* Loop counters */
  100355                 :   Table *pTab;           /* The table to be updated */
  100356           13252 :   int addr = 0;          /* VDBE instruction address of the start of the loop */
  100357                 :   WhereInfo *pWInfo;     /* Information about the WHERE clause */
  100358                 :   Vdbe *v;               /* The virtual database engine */
  100359                 :   Index *pIdx;           /* For looping over indices */
  100360                 :   int nIdx;              /* Number of indices that need updating */
  100361                 :   int iCur;              /* VDBE Cursor number of pTab */
  100362                 :   sqlite3 *db;           /* The database structure */
  100363           13252 :   int *aRegIdx = 0;      /* One register assigned to each index to be updated */
  100364           13252 :   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
  100365                 :                          ** an expression for the i-th column of the table.
  100366                 :                          ** aXRef[i]==-1 if the i-th column is not changed. */
  100367                 :   int chngRowid;         /* True if the record number is being changed */
  100368           13252 :   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
  100369           13252 :   int openAll = 0;       /* True if all indices need to be opened */
  100370                 :   AuthContext sContext;  /* The authorization context */
  100371                 :   NameContext sNC;       /* The name-context to resolve expressions in */
  100372                 :   int iDb;               /* Database containing the table being updated */
  100373                 :   int okOnePass;         /* True for one-pass algorithm without the FIFO */
  100374                 :   int hasFK;             /* True if foreign key processing is required */
  100375                 : 
  100376                 : #ifndef SQLITE_OMIT_TRIGGER
  100377                 :   int isView;            /* True when updating a view (INSTEAD OF trigger) */
  100378                 :   Trigger *pTrigger;     /* List of triggers on pTab, if required */
  100379                 :   int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
  100380                 : #endif
  100381                 :   int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
  100382                 : 
  100383                 :   /* Register Allocations */
  100384           13252 :   int regRowCount = 0;   /* A count of rows changed */
  100385                 :   int regOldRowid;       /* The old rowid */
  100386                 :   int regNewRowid;       /* The new rowid */
  100387                 :   int regNew;            /* Content of the NEW.* table in triggers */
  100388           13252 :   int regOld = 0;        /* Content of OLD.* table in triggers */
  100389           13252 :   int regRowSet = 0;     /* Rowset of rows to be updated */
  100390                 : 
  100391           13252 :   memset(&sContext, 0, sizeof(sContext));
  100392           13252 :   db = pParse->db;
  100393           13252 :   if( pParse->nErr || db->mallocFailed ){
  100394                 :     goto update_cleanup;
  100395                 :   }
  100396           13252 :   assert( pTabList->nSrc==1 );
  100397                 : 
  100398                 :   /* Locate the table which we want to update. 
  100399                 :   */
  100400           13252 :   pTab = sqlite3SrcListLookup(pParse, pTabList);
  100401           13252 :   if( pTab==0 ) goto update_cleanup;
  100402           13252 :   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  100403                 : 
  100404                 :   /* Figure out if we have any triggers and if the table being
  100405                 :   ** updated is a view.
  100406                 :   */
  100407                 : #ifndef SQLITE_OMIT_TRIGGER
  100408           13252 :   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
  100409           13252 :   isView = pTab->pSelect!=0;
  100410           13252 :   assert( pTrigger || tmask==0 );
  100411                 : #else
  100412                 : # define pTrigger 0
  100413                 : # define isView 0
  100414                 : # define tmask 0
  100415                 : #endif
  100416                 : #ifdef SQLITE_OMIT_VIEW
  100417                 : # undef isView
  100418                 : # define isView 0
  100419                 : #endif
  100420                 : 
  100421           13252 :   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
  100422               0 :     goto update_cleanup;
  100423                 :   }
  100424           13252 :   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
  100425               0 :     goto update_cleanup;
  100426                 :   }
  100427           13252 :   aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
  100428           13252 :   if( aXRef==0 ) goto update_cleanup;
  100429           13252 :   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
  100430                 : 
  100431                 :   /* Allocate a cursors for the main database table and for all indices.
  100432                 :   ** The index cursors might not be used, but if they are used they
  100433                 :   ** need to occur right after the database cursor.  So go ahead and
  100434                 :   ** allocate enough space, just in case.
  100435                 :   */
  100436           13252 :   pTabList->a[0].iCursor = iCur = pParse->nTab++;
  100437           31270 :   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  100438           18018 :     pParse->nTab++;
  100439                 :   }
  100440                 : 
  100441                 :   /* Initialize the name-context */
  100442           13252 :   memset(&sNC, 0, sizeof(sNC));
  100443           13252 :   sNC.pParse = pParse;
  100444           13252 :   sNC.pSrcList = pTabList;
  100445                 : 
  100446                 :   /* Resolve the column names in all the expressions of the
  100447                 :   ** of the UPDATE statement.  Also find the column index
  100448                 :   ** for each column to be updated in the pChanges array.  For each
  100449                 :   ** column to be updated, make sure we have authorization to change
  100450                 :   ** that column.
  100451                 :   */
  100452           13252 :   chngRowid = 0;
  100453           60448 :   for(i=0; i<pChanges->nExpr; i++){
  100454           47196 :     if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
  100455               0 :       goto update_cleanup;
  100456                 :     }
  100457          216980 :     for(j=0; j<pTab->nCol; j++){
  100458          216980 :       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
  100459           47196 :         if( j==pTab->iPKey ){
  100460               0 :           chngRowid = 1;
  100461               0 :           pRowidExpr = pChanges->a[i].pExpr;
  100462                 :         }
  100463           47196 :         aXRef[j] = i;
  100464           47196 :         break;
  100465                 :       }
  100466                 :     }
  100467           47196 :     if( j>=pTab->nCol ){
  100468               0 :       if( sqlite3IsRowid(pChanges->a[i].zName) ){
  100469               0 :         chngRowid = 1;
  100470               0 :         pRowidExpr = pChanges->a[i].pExpr;
  100471                 :       }else{
  100472               0 :         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
  100473               0 :         pParse->checkSchema = 1;
  100474               0 :         goto update_cleanup;
  100475                 :       }
  100476                 :     }
  100477                 : #ifndef SQLITE_OMIT_AUTHORIZATION
  100478                 :     {
  100479                 :       int rc;
  100480           47196 :       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
  100481           47196 :                            pTab->aCol[j].zName, db->aDb[iDb].zName);
  100482           47196 :       if( rc==SQLITE_DENY ){
  100483               0 :         goto update_cleanup;
  100484           47196 :       }else if( rc==SQLITE_IGNORE ){
  100485               0 :         aXRef[j] = -1;
  100486                 :       }
  100487                 :     }
  100488                 : #endif
  100489                 :   }
  100490                 : 
  100491           13252 :   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid);
  100492                 : 
  100493                 :   /* Allocate memory for the array aRegIdx[].  There is one entry in the
  100494                 :   ** array for each index associated with table being updated.  Fill in
  100495                 :   ** the value with a register number for indices that are to be used
  100496                 :   ** and with zero for unused indices.
  100497                 :   */
  100498           13252 :   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
  100499           13252 :   if( nIdx>0 ){
  100500            5131 :     aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
  100501            5131 :     if( aRegIdx==0 ) goto update_cleanup;
  100502                 :   }
  100503           31270 :   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  100504                 :     int reg;
  100505           18018 :     if( hasFK || chngRowid ){
  100506               0 :       reg = ++pParse->nMem;
  100507                 :     }else{
  100508           18018 :       reg = 0;
  100509           37210 :       for(i=0; i<pIdx->nColumn; i++){
  100510           22047 :         if( aXRef[pIdx->aiColumn[i]]>=0 ){
  100511            2855 :           reg = ++pParse->nMem;
  100512            2855 :           break;
  100513                 :         }
  100514                 :       }
  100515                 :     }
  100516           18018 :     aRegIdx[j] = reg;
  100517                 :   }
  100518                 : 
  100519                 :   /* Begin generating code. */
  100520           13252 :   v = sqlite3GetVdbe(pParse);
  100521           13252 :   if( v==0 ) goto update_cleanup;
  100522           13252 :   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
  100523           13252 :   sqlite3BeginWriteOperation(pParse, 1, iDb);
  100524                 : 
  100525                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  100526                 :   /* Virtual tables must be handled separately */
  100527           13252 :   if( IsVirtual(pTab) ){
  100528               0 :     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
  100529                 :                        pWhere, onError);
  100530               0 :     pWhere = 0;
  100531               0 :     pTabList = 0;
  100532               0 :     goto update_cleanup;
  100533                 :   }
  100534                 : #endif
  100535                 : 
  100536                 :   /* Allocate required registers. */
  100537           13252 :   regRowSet = ++pParse->nMem;
  100538           13252 :   regOldRowid = regNewRowid = ++pParse->nMem;
  100539           13252 :   if( pTrigger || hasFK ){
  100540             408 :     regOld = pParse->nMem + 1;
  100541             408 :     pParse->nMem += pTab->nCol;
  100542                 :   }
  100543           13252 :   if( chngRowid || pTrigger || hasFK ){
  100544             408 :     regNewRowid = ++pParse->nMem;
  100545                 :   }
  100546           13252 :   regNew = pParse->nMem + 1;
  100547           13252 :   pParse->nMem += pTab->nCol;
  100548                 : 
  100549                 :   /* Start the view context. */
  100550           13252 :   if( isView ){
  100551               0 :     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
  100552                 :   }
  100553                 : 
  100554                 :   /* If we are trying to update a view, realize that view into
  100555                 :   ** a ephemeral table.
  100556                 :   */
  100557                 : #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
  100558           13252 :   if( isView ){
  100559               0 :     sqlite3MaterializeView(pParse, pTab, pWhere, iCur);
  100560                 :   }
  100561                 : #endif
  100562                 : 
  100563                 :   /* Resolve the column names in all the expressions in the
  100564                 :   ** WHERE clause.
  100565                 :   */
  100566           13252 :   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
  100567               0 :     goto update_cleanup;
  100568                 :   }
  100569                 : 
  100570                 :   /* Begin the database scan
  100571                 :   */
  100572           13252 :   sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
  100573           13252 :   pWInfo = sqlite3WhereBegin(
  100574                 :       pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
  100575                 :   );
  100576           13252 :   if( pWInfo==0 ) goto update_cleanup;
  100577           13252 :   okOnePass = pWInfo->okOnePass;
  100578                 : 
  100579                 :   /* Remember the rowid of every item to be updated.
  100580                 :   */
  100581           13252 :   sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
  100582           13252 :   if( !okOnePass ){
  100583            1951 :     sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
  100584                 :   }
  100585                 : 
  100586                 :   /* End the database scan loop.
  100587                 :   */
  100588           13252 :   sqlite3WhereEnd(pWInfo);
  100589                 : 
  100590                 :   /* Initialize the count of updated rows
  100591                 :   */
  100592           13252 :   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
  100593               0 :     regRowCount = ++pParse->nMem;
  100594               0 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
  100595                 :   }
  100596                 : 
  100597           13252 :   if( !isView ){
  100598                 :     /* 
  100599                 :     ** Open every index that needs updating.  Note that if any
  100600                 :     ** index could potentially invoke a REPLACE conflict resolution 
  100601                 :     ** action, then we need to open all indices because we might need
  100602                 :     ** to be deleting some records.
  100603                 :     */
  100604           13252 :     if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); 
  100605           13252 :     if( onError==OE_Replace ){
  100606               0 :       openAll = 1;
  100607                 :     }else{
  100608           13252 :       openAll = 0;
  100609           31270 :       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  100610           18018 :         if( pIdx->onError==OE_Replace ){
  100611               0 :           openAll = 1;
  100612               0 :           break;
  100613                 :         }
  100614                 :       }
  100615                 :     }
  100616           31270 :     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  100617           18018 :       assert( aRegIdx );
  100618           18018 :       if( openAll || aRegIdx[i]>0 ){
  100619            2855 :         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  100620            2855 :         sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
  100621                 :                        (char*)pKey, P4_KEYINFO_HANDOFF);
  100622            2855 :         assert( pParse->nTab>iCur+i+1 );
  100623                 :       }
  100624                 :     }
  100625                 :   }
  100626                 : 
  100627                 :   /* Top of the update loop */
  100628           13252 :   if( okOnePass ){
  100629           11301 :     int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid);
  100630           11301 :     addr = sqlite3VdbeAddOp0(v, OP_Goto);
  100631           11301 :     sqlite3VdbeJumpHere(v, a1);
  100632                 :   }else{
  100633            1951 :     addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid);
  100634                 :   }
  100635                 : 
  100636                 :   /* Make cursor iCur point to the record that is being updated. If
  100637                 :   ** this record does not exist for some reason (deleted by a trigger,
  100638                 :   ** for example, then jump to the next iteration of the RowSet loop.  */
  100639           13252 :   sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
  100640                 : 
  100641                 :   /* If the record number will change, set register regNewRowid to
  100642                 :   ** contain the new value. If the record number is not being modified,
  100643                 :   ** then regNewRowid is the same register as regOldRowid, which is
  100644                 :   ** already populated.  */
  100645           13252 :   assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid );
  100646           13252 :   if( chngRowid ){
  100647               0 :     sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
  100648               0 :     sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
  100649                 :   }
  100650                 : 
  100651                 :   /* If there are triggers on this table, populate an array of registers 
  100652                 :   ** with the required old.* column data.  */
  100653           13252 :   if( hasFK || pTrigger ){
  100654             408 :     u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
  100655             408 :     oldmask |= sqlite3TriggerColmask(pParse, 
  100656                 :         pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
  100657                 :     );
  100658            4887 :     for(i=0; i<pTab->nCol; i++){
  100659            4479 :       if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<<i))) ){
  100660            4392 :         sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, i, regOld+i);
  100661                 :       }else{
  100662              87 :         sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
  100663                 :       }
  100664                 :     }
  100665             408 :     if( chngRowid==0 ){
  100666             408 :       sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
  100667                 :     }
  100668                 :   }
  100669                 : 
  100670                 :   /* Populate the array of registers beginning at regNew with the new
  100671                 :   ** row data. This array is used to check constaints, create the new
  100672                 :   ** table and index records, and as the values for any new.* references
  100673                 :   ** made by triggers.
  100674                 :   **
  100675                 :   ** If there are one or more BEFORE triggers, then do not populate the
  100676                 :   ** registers associated with columns that are (a) not modified by
  100677                 :   ** this UPDATE statement and (b) not accessed by new.* references. The
  100678                 :   ** values for registers not modified by the UPDATE must be reloaded from 
  100679                 :   ** the database after the BEFORE triggers are fired anyway (as the trigger 
  100680                 :   ** may have modified them). So not loading those that are not going to
  100681                 :   ** be used eliminates some redundant opcodes.
  100682                 :   */
  100683           13252 :   newmask = sqlite3TriggerColmask(
  100684                 :       pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
  100685                 :   );
  100686           13252 :   sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);
  100687          139514 :   for(i=0; i<pTab->nCol; i++){
  100688          126262 :     if( i==pTab->iPKey ){
  100689                 :       /*sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);*/
  100690                 :     }else{
  100691          120676 :       j = aXRef[i];
  100692          120676 :       if( j>=0 ){
  100693           47196 :         sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
  100694           73480 :       }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
  100695                 :         /* This branch loads the value of a column that will not be changed 
  100696                 :         ** into a register. This is done if there are no BEFORE triggers, or
  100697                 :         ** if there are one or more BEFORE triggers that use this value via
  100698                 :         ** a new.* reference in a trigger program.
  100699                 :         */
  100700                 :         testcase( i==31 );
  100701                 :         testcase( i==32 );
  100702           73480 :         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
  100703           73480 :         sqlite3ColumnDefault(v, pTab, i, regNew+i);
  100704                 :       }
  100705                 :     }
  100706                 :   }
  100707                 : 
  100708                 :   /* Fire any BEFORE UPDATE triggers. This happens before constraints are
  100709                 :   ** verified. One could argue that this is wrong.
  100710                 :   */
  100711           13252 :   if( tmask&TRIGGER_BEFORE ){
  100712               0 :     sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
  100713               0 :     sqlite3TableAffinityStr(v, pTab);
  100714               0 :     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
  100715                 :         TRIGGER_BEFORE, pTab, regOldRowid, onError, addr);
  100716                 : 
  100717                 :     /* The row-trigger may have deleted the row being updated. In this
  100718                 :     ** case, jump to the next row. No updates or AFTER triggers are 
  100719                 :     ** required. This behaviour - what happens when the row being updated
  100720                 :     ** is deleted or renamed by a BEFORE trigger - is left undefined in the
  100721                 :     ** documentation.
  100722                 :     */
  100723               0 :     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid);
  100724                 : 
  100725                 :     /* If it did not delete it, the row-trigger may still have modified 
  100726                 :     ** some of the columns of the row being updated. Load the values for 
  100727                 :     ** all columns not modified by the update statement into their 
  100728                 :     ** registers in case this has happened.
  100729                 :     */
  100730               0 :     for(i=0; i<pTab->nCol; i++){
  100731               0 :       if( aXRef[i]<0 && i!=pTab->iPKey ){
  100732               0 :         sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i);
  100733               0 :         sqlite3ColumnDefault(v, pTab, i, regNew+i);
  100734                 :       }
  100735                 :     }
  100736                 :   }
  100737                 : 
  100738           13252 :   if( !isView ){
  100739                 :     int j1;                       /* Address of jump instruction */
  100740                 : 
  100741                 :     /* Do constraint checks. */
  100742           13252 :     sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid,
  100743                 :         aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0);
  100744                 : 
  100745                 :     /* Do FK constraint checks. */
  100746           13252 :     if( hasFK ){
  100747               0 :       sqlite3FkCheck(pParse, pTab, regOldRowid, 0);
  100748                 :     }
  100749                 : 
  100750                 :     /* Delete the index entries associated with the current record.  */
  100751           13252 :     j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid);
  100752           13252 :     sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx);
  100753                 :   
  100754                 :     /* If changing the record number, delete the old record.  */
  100755           13252 :     if( hasFK || chngRowid ){
  100756               0 :       sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0);
  100757                 :     }
  100758           13252 :     sqlite3VdbeJumpHere(v, j1);
  100759                 : 
  100760           13252 :     if( hasFK ){
  100761               0 :       sqlite3FkCheck(pParse, pTab, 0, regNewRowid);
  100762                 :     }
  100763                 :   
  100764                 :     /* Insert the new index entries and the new record. */
  100765           13252 :     sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0);
  100766                 : 
  100767                 :     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
  100768                 :     ** handle rows (possibly in other tables) that refer via a foreign key
  100769                 :     ** to the row just updated. */ 
  100770           13252 :     if( hasFK ){
  100771               0 :       sqlite3FkActions(pParse, pTab, pChanges, regOldRowid);
  100772                 :     }
  100773                 :   }
  100774                 : 
  100775                 :   /* Increment the row counter 
  100776                 :   */
  100777           13252 :   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
  100778               0 :     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
  100779                 :   }
  100780                 : 
  100781           13252 :   sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
  100782                 :       TRIGGER_AFTER, pTab, regOldRowid, onError, addr);
  100783                 : 
  100784                 :   /* Repeat the above with the next record to be updated, until
  100785                 :   ** all record selected by the WHERE clause have been updated.
  100786                 :   */
  100787           13252 :   sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
  100788           13252 :   sqlite3VdbeJumpHere(v, addr);
  100789                 : 
  100790                 :   /* Close all tables */
  100791           31270 :   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  100792           18018 :     assert( aRegIdx );
  100793           18018 :     if( openAll || aRegIdx[i]>0 ){
  100794            2855 :       sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
  100795                 :     }
  100796                 :   }
  100797           13252 :   sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
  100798                 : 
  100799                 :   /* Update the sqlite_sequence table by storing the content of the
  100800                 :   ** maximum rowid counter values recorded while inserting into
  100801                 :   ** autoincrement tables.
  100802                 :   */
  100803           13252 :   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
  100804            4815 :     sqlite3AutoincrementEnd(pParse);
  100805                 :   }
  100806                 : 
  100807                 :   /*
  100808                 :   ** Return the number of rows that were changed. If this routine is 
  100809                 :   ** generating code because of a call to sqlite3NestedParse(), do not
  100810                 :   ** invoke the callback function.
  100811                 :   */
  100812           13252 :   if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
  100813               0 :     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
  100814               0 :     sqlite3VdbeSetNumCols(v, 1);
  100815               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
  100816                 :   }
  100817                 : 
  100818                 : update_cleanup:
  100819           13252 :   sqlite3AuthContextPop(&sContext);
  100820           13252 :   sqlite3DbFree(db, aRegIdx);
  100821           13252 :   sqlite3DbFree(db, aXRef);
  100822           13252 :   sqlite3SrcListDelete(db, pTabList);
  100823           13252 :   sqlite3ExprListDelete(db, pChanges);
  100824           13252 :   sqlite3ExprDelete(db, pWhere);
  100825                 :   return;
  100826                 : }
  100827                 : /* Make sure "isView" and other macros defined above are undefined. Otherwise
  100828                 : ** thely may interfere with compilation of other functions in this file
  100829                 : ** (or in another file, if this file becomes part of the amalgamation).  */
  100830                 : #ifdef isView
  100831                 :  #undef isView
  100832                 : #endif
  100833                 : #ifdef pTrigger
  100834                 :  #undef pTrigger
  100835                 : #endif
  100836                 : 
  100837                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  100838                 : /*
  100839                 : ** Generate code for an UPDATE of a virtual table.
  100840                 : **
  100841                 : ** The strategy is that we create an ephemerial table that contains
  100842                 : ** for each row to be changed:
  100843                 : **
  100844                 : **   (A)  The original rowid of that row.
  100845                 : **   (B)  The revised rowid for the row. (note1)
  100846                 : **   (C)  The content of every column in the row.
  100847                 : **
  100848                 : ** Then we loop over this ephemeral table and for each row in
  100849                 : ** the ephermeral table call VUpdate.
  100850                 : **
  100851                 : ** When finished, drop the ephemeral table.
  100852                 : **
  100853                 : ** (note1) Actually, if we know in advance that (A) is always the same
  100854                 : ** as (B) we only store (A), then duplicate (A) when pulling
  100855                 : ** it out of the ephemeral table before calling VUpdate.
  100856                 : */
  100857               0 : static void updateVirtualTable(
  100858                 :   Parse *pParse,       /* The parsing context */
  100859                 :   SrcList *pSrc,       /* The virtual table to be modified */
  100860                 :   Table *pTab,         /* The virtual table */
  100861                 :   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
  100862                 :   Expr *pRowid,        /* Expression used to recompute the rowid */
  100863                 :   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
  100864                 :   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
  100865                 :   int onError          /* ON CONFLICT strategy */
  100866                 : ){
  100867               0 :   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
  100868               0 :   ExprList *pEList = 0;     /* The result set of the SELECT statement */
  100869               0 :   Select *pSelect = 0;      /* The SELECT statement */
  100870                 :   Expr *pExpr;              /* Temporary expression */
  100871                 :   int ephemTab;             /* Table holding the result of the SELECT */
  100872                 :   int i;                    /* Loop counter */
  100873                 :   int addr;                 /* Address of top of loop */
  100874                 :   int iReg;                 /* First register in set passed to OP_VUpdate */
  100875               0 :   sqlite3 *db = pParse->db; /* Database connection */
  100876               0 :   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
  100877                 :   SelectDest dest;
  100878                 : 
  100879                 :   /* Construct the SELECT statement that will find the new values for
  100880                 :   ** all updated rows. 
  100881                 :   */
  100882               0 :   pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
  100883               0 :   if( pRowid ){
  100884               0 :     pEList = sqlite3ExprListAppend(pParse, pEList,
  100885                 :                                    sqlite3ExprDup(db, pRowid, 0));
  100886                 :   }
  100887               0 :   assert( pTab->iPKey<0 );
  100888               0 :   for(i=0; i<pTab->nCol; i++){
  100889               0 :     if( aXRef[i]>=0 ){
  100890               0 :       pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
  100891                 :     }else{
  100892               0 :       pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
  100893                 :     }
  100894               0 :     pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
  100895                 :   }
  100896               0 :   pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
  100897                 :   
  100898                 :   /* Create the ephemeral table into which the update results will
  100899                 :   ** be stored.
  100900                 :   */
  100901               0 :   assert( v );
  100902               0 :   ephemTab = pParse->nTab++;
  100903               0 :   sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
  100904               0 :   sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  100905                 : 
  100906                 :   /* fill the ephemeral table 
  100907                 :   */
  100908               0 :   sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
  100909               0 :   sqlite3Select(pParse, pSelect, &dest);
  100910                 : 
  100911                 :   /* Generate code to scan the ephemeral table and call VUpdate. */
  100912               0 :   iReg = ++pParse->nMem;
  100913               0 :   pParse->nMem += pTab->nCol+1;
  100914               0 :   addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
  100915               0 :   sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
  100916               0 :   sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
  100917               0 :   for(i=0; i<pTab->nCol; i++){
  100918               0 :     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
  100919                 :   }
  100920               0 :   sqlite3VtabMakeWritable(pParse, pTab);
  100921               0 :   sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
  100922               0 :   sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
  100923               0 :   sqlite3MayAbort(pParse);
  100924               0 :   sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
  100925               0 :   sqlite3VdbeJumpHere(v, addr);
  100926               0 :   sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
  100927                 : 
  100928                 :   /* Cleanup */
  100929               0 :   sqlite3SelectDelete(db, pSelect);  
  100930               0 : }
  100931                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
  100932                 : 
  100933                 : /************** End of update.c **********************************************/
  100934                 : /************** Begin file vacuum.c ******************************************/
  100935                 : /*
  100936                 : ** 2003 April 6
  100937                 : **
  100938                 : ** The author disclaims copyright to this source code.  In place of
  100939                 : ** a legal notice, here is a blessing:
  100940                 : **
  100941                 : **    May you do good and not evil.
  100942                 : **    May you find forgiveness for yourself and forgive others.
  100943                 : **    May you share freely, never taking more than you give.
  100944                 : **
  100945                 : *************************************************************************
  100946                 : ** This file contains code used to implement the VACUUM command.
  100947                 : **
  100948                 : ** Most of the code in this file may be omitted by defining the
  100949                 : ** SQLITE_OMIT_VACUUM macro.
  100950                 : */
  100951                 : 
  100952                 : #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
  100953                 : /*
  100954                 : ** Finalize a prepared statement.  If there was an error, store the
  100955                 : ** text of the error message in *pzErrMsg.  Return the result code.
  100956                 : */
  100957             120 : static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
  100958                 :   int rc;
  100959             120 :   rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
  100960             120 :   if( rc ){
  100961               0 :     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
  100962                 :   }
  100963             120 :   return rc;
  100964                 : }
  100965                 : 
  100966                 : /*
  100967                 : ** Execute zSql on database db. Return an error code.
  100968                 : */
  100969              84 : static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
  100970                 :   sqlite3_stmt *pStmt;
  100971                 :   VVA_ONLY( int rc; )
  100972              84 :   if( !zSql ){
  100973               0 :     return SQLITE_NOMEM;
  100974                 :   }
  100975              84 :   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
  100976               0 :     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
  100977               0 :     return sqlite3_errcode(db);
  100978                 :   }
  100979              84 :   VVA_ONLY( rc = ) sqlite3_step(pStmt);
  100980              84 :   assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
  100981              84 :   return vacuumFinalize(db, pStmt, pzErrMsg);
  100982                 : }
  100983                 : 
  100984                 : /*
  100985                 : ** Execute zSql on database db. The statement returns exactly
  100986                 : ** one column. Execute this as SQL on the same database.
  100987                 : */
  100988              36 : static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
  100989                 :   sqlite3_stmt *pStmt;
  100990                 :   int rc;
  100991                 : 
  100992              36 :   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
  100993              36 :   if( rc!=SQLITE_OK ) return rc;
  100994                 : 
  100995             132 :   while( SQLITE_ROW==sqlite3_step(pStmt) ){
  100996              60 :     rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
  100997              60 :     if( rc!=SQLITE_OK ){
  100998               0 :       vacuumFinalize(db, pStmt, pzErrMsg);
  100999               0 :       return rc;
  101000                 :     }
  101001                 :   }
  101002                 : 
  101003              36 :   return vacuumFinalize(db, pStmt, pzErrMsg);
  101004                 : }
  101005                 : 
  101006                 : /*
  101007                 : ** The non-standard VACUUM command is used to clean up the database,
  101008                 : ** collapse free space, etc.  It is modelled after the VACUUM command
  101009                 : ** in PostgreSQL.
  101010                 : **
  101011                 : ** In version 1.0.x of SQLite, the VACUUM command would call
  101012                 : ** gdbm_reorganize() on all the database tables.  But beginning
  101013                 : ** with 2.0.0, SQLite no longer uses GDBM so this command has
  101014                 : ** become a no-op.
  101015                 : */
  101016               6 : SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
  101017               6 :   Vdbe *v = sqlite3GetVdbe(pParse);
  101018               6 :   if( v ){
  101019               6 :     sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
  101020                 :   }
  101021                 :   return;
  101022                 : }
  101023                 : 
  101024                 : /*
  101025                 : ** This routine implements the OP_Vacuum opcode of the VDBE.
  101026                 : */
  101027               6 : SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
  101028               6 :   int rc = SQLITE_OK;     /* Return code from service routines */
  101029                 :   Btree *pMain;           /* The database being vacuumed */
  101030                 :   Btree *pTemp;           /* The temporary database we vacuum into */
  101031               6 :   char *zSql = 0;         /* SQL statements */
  101032                 :   int saved_flags;        /* Saved value of the db->flags */
  101033                 :   int saved_nChange;      /* Saved value of db->nChange */
  101034                 :   int saved_nTotalChange; /* Saved value of db->nTotalChange */
  101035                 :   void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
  101036               6 :   Db *pDb = 0;            /* Database to detach at end of vacuum */
  101037                 :   int isMemDb;            /* True if vacuuming a :memory: database */
  101038                 :   int nRes;               /* Bytes of reserved space at the end of each page */
  101039                 :   int nDb;                /* Number of attached databases */
  101040                 : 
  101041               6 :   if( !db->autoCommit ){
  101042               0 :     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
  101043               0 :     return SQLITE_ERROR;
  101044                 :   }
  101045               6 :   if( db->activeVdbeCnt>1 ){
  101046               0 :     sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
  101047               0 :     return SQLITE_ERROR;
  101048                 :   }
  101049                 : 
  101050                 :   /* Save the current value of the database flags so that it can be 
  101051                 :   ** restored before returning. Then set the writable-schema flag, and
  101052                 :   ** disable CHECK and foreign key constraints.  */
  101053               6 :   saved_flags = db->flags;
  101054               6 :   saved_nChange = db->nChange;
  101055               6 :   saved_nTotalChange = db->nTotalChange;
  101056               6 :   saved_xTrace = db->xTrace;
  101057               6 :   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
  101058               6 :   db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
  101059               6 :   db->xTrace = 0;
  101060                 : 
  101061               6 :   pMain = db->aDb[0].pBt;
  101062               6 :   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
  101063                 : 
  101064                 :   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
  101065                 :   ** can be set to 'off' for this file, as it is not recovered if a crash
  101066                 :   ** occurs anyway. The integrity of the database is maintained by a
  101067                 :   ** (possibly synchronous) transaction opened on the main database before
  101068                 :   ** sqlite3BtreeCopyFile() is called.
  101069                 :   **
  101070                 :   ** An optimisation would be to use a non-journaled pager.
  101071                 :   ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
  101072                 :   ** that actually made the VACUUM run slower.  Very little journalling
  101073                 :   ** actually occurs when doing a vacuum since the vacuum_db is initially
  101074                 :   ** empty.  Only the journal header is written.  Apparently it takes more
  101075                 :   ** time to parse and run the PRAGMA to turn journalling off than it does
  101076                 :   ** to write the journal header file.
  101077                 :   */
  101078               6 :   nDb = db->nDb;
  101079               6 :   if( sqlite3TempInMemory(db) ){
  101080               1 :     zSql = "ATTACH ':memory:' AS vacuum_db;";
  101081                 :   }else{
  101082               5 :     zSql = "ATTACH '' AS vacuum_db;";
  101083                 :   }
  101084               6 :   rc = execSql(db, pzErrMsg, zSql);
  101085               6 :   if( db->nDb>nDb ){
  101086               6 :     pDb = &db->aDb[db->nDb-1];
  101087               6 :     assert( strcmp(pDb->zName,"vacuum_db")==0 );
  101088                 :   }
  101089               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101090               6 :   pTemp = db->aDb[db->nDb-1].pBt;
  101091                 : 
  101092                 :   /* The call to execSql() to attach the temp database has left the file
  101093                 :   ** locked (as there was more than one active statement when the transaction
  101094                 :   ** to read the schema was concluded. Unlock it here so that this doesn't
  101095                 :   ** cause problems for the call to BtreeSetPageSize() below.  */
  101096               6 :   sqlite3BtreeCommit(pTemp);
  101097                 : 
  101098               6 :   nRes = sqlite3BtreeGetReserve(pMain);
  101099                 : 
  101100                 :   /* A VACUUM cannot change the pagesize of an encrypted database. */
  101101                 : #ifdef SQLITE_HAS_CODEC
  101102                 :   if( db->nextPagesize ){
  101103                 :     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
  101104                 :     int nKey;
  101105                 :     char *zKey;
  101106                 :     sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
  101107                 :     if( nKey ) db->nextPagesize = 0;
  101108                 :   }
  101109                 : #endif
  101110                 : 
  101111                 :   /* Do not attempt to change the page size for a WAL database */
  101112               6 :   if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
  101113                 :                                                ==PAGER_JOURNALMODE_WAL ){
  101114               1 :     db->nextPagesize = 0;
  101115                 :   }
  101116                 : 
  101117              12 :   if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
  101118               6 :    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
  101119               6 :    || NEVER(db->mallocFailed)
  101120                 :   ){
  101121               0 :     rc = SQLITE_NOMEM;
  101122               0 :     goto end_of_vacuum;
  101123                 :   }
  101124               6 :   rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
  101125               6 :   if( rc!=SQLITE_OK ){
  101126               0 :     goto end_of_vacuum;
  101127                 :   }
  101128                 : 
  101129                 : #ifndef SQLITE_OMIT_AUTOVACUUM
  101130               6 :   sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
  101131                 :                                            sqlite3BtreeGetAutoVacuum(pMain));
  101132                 : #endif
  101133                 : 
  101134                 :   /* Begin a transaction */
  101135               6 :   rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;");
  101136               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101137                 : 
  101138                 :   /* Query the schema of the main database. Create a mirror schema
  101139                 :   ** in the temporary database.
  101140                 :   */
  101141               6 :   rc = execExecSql(db, pzErrMsg,
  101142                 :       "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
  101143                 :       "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
  101144                 :       "   AND rootpage>0"
  101145                 :   );
  101146               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101147               6 :   rc = execExecSql(db, pzErrMsg,
  101148                 :       "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
  101149                 :       "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
  101150               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101151               6 :   rc = execExecSql(db, pzErrMsg,
  101152                 :       "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
  101153                 :       "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
  101154               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101155                 : 
  101156                 :   /* Loop through the tables in the main database. For each, do
  101157                 :   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
  101158                 :   ** the contents to the temporary database.
  101159                 :   */
  101160               6 :   rc = execExecSql(db, pzErrMsg,
  101161                 :       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
  101162                 :       "|| ' SELECT * FROM main.' || quote(name) || ';'"
  101163                 :       "FROM main.sqlite_master "
  101164                 :       "WHERE type = 'table' AND name!='sqlite_sequence' "
  101165                 :       "  AND rootpage>0"
  101166                 :   );
  101167               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101168                 : 
  101169                 :   /* Copy over the sequence table
  101170                 :   */
  101171               6 :   rc = execExecSql(db, pzErrMsg,
  101172                 :       "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
  101173                 :       "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
  101174                 :   );
  101175               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101176               6 :   rc = execExecSql(db, pzErrMsg,
  101177                 :       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
  101178                 :       "|| ' SELECT * FROM main.' || quote(name) || ';' "
  101179                 :       "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
  101180                 :   );
  101181               6 :   if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101182                 : 
  101183                 : 
  101184                 :   /* Copy the triggers, views, and virtual tables from the main database
  101185                 :   ** over to the temporary database.  None of these objects has any
  101186                 :   ** associated storage, so all we have to do is copy their entries
  101187                 :   ** from the SQLITE_MASTER table.
  101188                 :   */
  101189               6 :   rc = execSql(db, pzErrMsg,
  101190                 :       "INSERT INTO vacuum_db.sqlite_master "
  101191                 :       "  SELECT type, name, tbl_name, rootpage, sql"
  101192                 :       "    FROM main.sqlite_master"
  101193                 :       "   WHERE type='view' OR type='trigger'"
  101194                 :       "      OR (type='table' AND rootpage=0)"
  101195                 :   );
  101196               6 :   if( rc ) goto end_of_vacuum;
  101197                 : 
  101198                 :   /* At this point, there is a write transaction open on both the 
  101199                 :   ** vacuum database and the main database. Assuming no error occurs,
  101200                 :   ** both transactions are closed by this block - the main database
  101201                 :   ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
  101202                 :   ** call to sqlite3BtreeCommit().
  101203                 :   */
  101204                 :   {
  101205                 :     u32 meta;
  101206                 :     int i;
  101207                 : 
  101208                 :     /* This array determines which meta meta values are preserved in the
  101209                 :     ** vacuum.  Even entries are the meta value number and odd entries
  101210                 :     ** are an increment to apply to the meta value after the vacuum.
  101211                 :     ** The increment is used to increase the schema cookie so that other
  101212                 :     ** connections to the same database will know to reread the schema.
  101213                 :     */
  101214                 :     static const unsigned char aCopy[] = {
  101215                 :        BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
  101216                 :        BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
  101217                 :        BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
  101218                 :        BTREE_USER_VERSION,       0,  /* Preserve the user version */
  101219                 :     };
  101220                 : 
  101221               6 :     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
  101222               6 :     assert( 1==sqlite3BtreeIsInTrans(pMain) );
  101223                 : 
  101224                 :     /* Copy Btree meta values */
  101225              30 :     for(i=0; i<ArraySize(aCopy); i+=2){
  101226                 :       /* GetMeta() and UpdateMeta() cannot fail in this context because
  101227                 :       ** we already have page 1 loaded into cache and marked dirty. */
  101228              24 :       sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
  101229              24 :       rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
  101230              24 :       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
  101231                 :     }
  101232                 : 
  101233               6 :     rc = sqlite3BtreeCopyFile(pMain, pTemp);
  101234               6 :     if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101235               6 :     rc = sqlite3BtreeCommit(pTemp);
  101236               6 :     if( rc!=SQLITE_OK ) goto end_of_vacuum;
  101237                 : #ifndef SQLITE_OMIT_AUTOVACUUM
  101238               6 :     sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
  101239                 : #endif
  101240                 :   }
  101241                 : 
  101242               6 :   assert( rc==SQLITE_OK );
  101243               6 :   rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
  101244                 : 
  101245                 : end_of_vacuum:
  101246                 :   /* Restore the original value of db->flags */
  101247               6 :   db->flags = saved_flags;
  101248               6 :   db->nChange = saved_nChange;
  101249               6 :   db->nTotalChange = saved_nTotalChange;
  101250               6 :   db->xTrace = saved_xTrace;
  101251               6 :   sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
  101252                 : 
  101253                 :   /* Currently there is an SQL level transaction open on the vacuum
  101254                 :   ** database. No locks are held on any other files (since the main file
  101255                 :   ** was committed at the btree level). So it safe to end the transaction
  101256                 :   ** by manually setting the autoCommit flag to true and detaching the
  101257                 :   ** vacuum database. The vacuum_db journal file is deleted when the pager
  101258                 :   ** is closed by the DETACH.
  101259                 :   */
  101260               6 :   db->autoCommit = 1;
  101261                 : 
  101262               6 :   if( pDb ){
  101263               6 :     sqlite3BtreeClose(pDb->pBt);
  101264               6 :     pDb->pBt = 0;
  101265               6 :     pDb->pSchema = 0;
  101266                 :   }
  101267                 : 
  101268                 :   /* This both clears the schemas and reduces the size of the db->aDb[]
  101269                 :   ** array. */ 
  101270               6 :   sqlite3ResetInternalSchema(db, -1);
  101271                 : 
  101272               6 :   return rc;
  101273                 : }
  101274                 : 
  101275                 : #endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
  101276                 : 
  101277                 : /************** End of vacuum.c **********************************************/
  101278                 : /************** Begin file vtab.c ********************************************/
  101279                 : /*
  101280                 : ** 2006 June 10
  101281                 : **
  101282                 : ** The author disclaims copyright to this source code.  In place of
  101283                 : ** a legal notice, here is a blessing:
  101284                 : **
  101285                 : **    May you do good and not evil.
  101286                 : **    May you find forgiveness for yourself and forgive others.
  101287                 : **    May you share freely, never taking more than you give.
  101288                 : **
  101289                 : *************************************************************************
  101290                 : ** This file contains code used to help implement virtual tables.
  101291                 : */
  101292                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  101293                 : 
  101294                 : /*
  101295                 : ** Before a virtual table xCreate() or xConnect() method is invoked, the
  101296                 : ** sqlite3.pVtabCtx member variable is set to point to an instance of
  101297                 : ** this struct allocated on the stack. It is used by the implementation of 
  101298                 : ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
  101299                 : ** are invoked only from within xCreate and xConnect methods.
  101300                 : */
  101301                 : struct VtabCtx {
  101302                 :   Table *pTab;
  101303                 :   VTable *pVTable;
  101304                 : };
  101305                 : 
  101306                 : /*
  101307                 : ** The actual function that does the work of creating a new module.
  101308                 : ** This function implements the sqlite3_create_module() and
  101309                 : ** sqlite3_create_module_v2() interfaces.
  101310                 : */
  101311            9907 : static int createModule(
  101312                 :   sqlite3 *db,                    /* Database in which module is registered */
  101313                 :   const char *zName,              /* Name assigned to this module */
  101314                 :   const sqlite3_module *pModule,  /* The definition of the module */
  101315                 :   void *pAux,                     /* Context pointer for xCreate/xConnect */
  101316                 :   void (*xDestroy)(void *)        /* Module destructor function */
  101317                 : ){
  101318                 :   int rc, nName;
  101319                 :   Module *pMod;
  101320                 : 
  101321            9907 :   sqlite3_mutex_enter(db->mutex);
  101322            9907 :   nName = sqlite3Strlen30(zName);
  101323            9907 :   pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
  101324            9907 :   if( pMod ){
  101325                 :     Module *pDel;
  101326            9907 :     char *zCopy = (char *)(&pMod[1]);
  101327            9907 :     memcpy(zCopy, zName, nName+1);
  101328            9907 :     pMod->zName = zCopy;
  101329            9907 :     pMod->pModule = pModule;
  101330            9907 :     pMod->pAux = pAux;
  101331            9907 :     pMod->xDestroy = xDestroy;
  101332            9907 :     pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod);
  101333            9907 :     if( pDel && pDel->xDestroy ){
  101334               0 :       sqlite3ResetInternalSchema(db, -1);
  101335               0 :       pDel->xDestroy(pDel->pAux);
  101336                 :     }
  101337            9907 :     sqlite3DbFree(db, pDel);
  101338            9907 :     if( pDel==pMod ){
  101339               0 :       db->mallocFailed = 1;
  101340                 :     }
  101341               0 :   }else if( xDestroy ){
  101342               0 :     xDestroy(pAux);
  101343                 :   }
  101344            9907 :   rc = sqlite3ApiExit(db, SQLITE_OK);
  101345            9907 :   sqlite3_mutex_leave(db->mutex);
  101346            9907 :   return rc;
  101347                 : }
  101348                 : 
  101349                 : 
  101350                 : /*
  101351                 : ** External API function used to create a new virtual-table module.
  101352                 : */
  101353            3353 : SQLITE_API int sqlite3_create_module(
  101354                 :   sqlite3 *db,                    /* Database in which module is registered */
  101355                 :   const char *zName,              /* Name assigned to this module */
  101356                 :   const sqlite3_module *pModule,  /* The definition of the module */
  101357                 :   void *pAux                      /* Context pointer for xCreate/xConnect */
  101358                 : ){
  101359            3353 :   return createModule(db, zName, pModule, pAux, 0);
  101360                 : }
  101361                 : 
  101362                 : /*
  101363                 : ** External API function used to create a new virtual-table module.
  101364                 : */
  101365            6554 : SQLITE_API int sqlite3_create_module_v2(
  101366                 :   sqlite3 *db,                    /* Database in which module is registered */
  101367                 :   const char *zName,              /* Name assigned to this module */
  101368                 :   const sqlite3_module *pModule,  /* The definition of the module */
  101369                 :   void *pAux,                     /* Context pointer for xCreate/xConnect */
  101370                 :   void (*xDestroy)(void *)        /* Module destructor function */
  101371                 : ){
  101372            6554 :   return createModule(db, zName, pModule, pAux, xDestroy);
  101373                 : }
  101374                 : 
  101375                 : /*
  101376                 : ** Lock the virtual table so that it cannot be disconnected.
  101377                 : ** Locks nest.  Every lock should have a corresponding unlock.
  101378                 : ** If an unlock is omitted, resources leaks will occur.  
  101379                 : **
  101380                 : ** If a disconnect is attempted while a virtual table is locked,
  101381                 : ** the disconnect is deferred until all locks have been removed.
  101382                 : */
  101383             119 : SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
  101384             119 :   pVTab->nRef++;
  101385             119 : }
  101386                 : 
  101387                 : 
  101388                 : /*
  101389                 : ** pTab is a pointer to a Table structure representing a virtual-table.
  101390                 : ** Return a pointer to the VTable object used by connection db to access 
  101391                 : ** this virtual-table, if one has been created, or NULL otherwise.
  101392                 : */
  101393             442 : SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
  101394                 :   VTable *pVtab;
  101395             442 :   assert( IsVirtual(pTab) );
  101396             442 :   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
  101397             442 :   return pVtab;
  101398                 : }
  101399                 : 
  101400                 : /*
  101401                 : ** Decrement the ref-count on a virtual table object. When the ref-count
  101402                 : ** reaches zero, call the xDisconnect() method to delete the object.
  101403                 : */
  101404             172 : SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
  101405             172 :   sqlite3 *db = pVTab->db;
  101406                 : 
  101407             172 :   assert( db );
  101408             172 :   assert( pVTab->nRef>0 );
  101409             172 :   assert( sqlite3SafetyCheckOk(db) );
  101410                 : 
  101411             172 :   pVTab->nRef--;
  101412             172 :   if( pVTab->nRef==0 ){
  101413              53 :     sqlite3_vtab *p = pVTab->pVtab;
  101414              53 :     if( p ){
  101415               1 :       p->pModule->xDisconnect(p);
  101416                 :     }
  101417              53 :     sqlite3DbFree(db, pVTab);
  101418                 :   }
  101419             172 : }
  101420                 : 
  101421                 : /*
  101422                 : ** Table p is a virtual table. This function moves all elements in the
  101423                 : ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
  101424                 : ** database connections to be disconnected at the next opportunity. 
  101425                 : ** Except, if argument db is not NULL, then the entry associated with
  101426                 : ** connection db is left in the p->pVTable list.
  101427                 : */
  101428           36094 : static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
  101429           36094 :   VTable *pRet = 0;
  101430           36094 :   VTable *pVTable = p->pVTable;
  101431           36094 :   p->pVTable = 0;
  101432                 : 
  101433                 :   /* Assert that the mutex (if any) associated with the BtShared database 
  101434                 :   ** that contains table p is held by the caller. See header comments 
  101435                 :   ** above function sqlite3VtabUnlockList() for an explanation of why
  101436                 :   ** this makes it safe to access the sqlite3.pDisconnect list of any
  101437                 :   ** database connection that may have an entry in the p->pVTable list.
  101438                 :   */
  101439           36094 :   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
  101440                 : 
  101441           72241 :   while( pVTable ){
  101442              53 :     sqlite3 *db2 = pVTable->db;
  101443              53 :     VTable *pNext = pVTable->pNext;
  101444              53 :     assert( db2 );
  101445              53 :     if( db2==db ){
  101446              52 :       pRet = pVTable;
  101447              52 :       p->pVTable = pRet;
  101448              52 :       pRet->pNext = 0;
  101449                 :     }else{
  101450               1 :       pVTable->pNext = db2->pDisconnect;
  101451               1 :       db2->pDisconnect = pVTable;
  101452                 :     }
  101453              53 :     pVTable = pNext;
  101454                 :   }
  101455                 : 
  101456           36094 :   assert( !db || pRet );
  101457           36094 :   return pRet;
  101458                 : }
  101459                 : 
  101460                 : 
  101461                 : /*
  101462                 : ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
  101463                 : **
  101464                 : ** This function may only be called when the mutexes associated with all
  101465                 : ** shared b-tree databases opened using connection db are held by the 
  101466                 : ** caller. This is done to protect the sqlite3.pDisconnect list. The
  101467                 : ** sqlite3.pDisconnect list is accessed only as follows:
  101468                 : **
  101469                 : **   1) By this function. In this case, all BtShared mutexes and the mutex
  101470                 : **      associated with the database handle itself must be held.
  101471                 : **
  101472                 : **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
  101473                 : **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
  101474                 : **      associated with the database the virtual table is stored in is held
  101475                 : **      or, if the virtual table is stored in a non-sharable database, then
  101476                 : **      the database handle mutex is held.
  101477                 : **
  101478                 : ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously 
  101479                 : ** by multiple threads. It is thread-safe.
  101480                 : */
  101481          179801 : SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
  101482          179801 :   VTable *p = db->pDisconnect;
  101483          179801 :   db->pDisconnect = 0;
  101484                 : 
  101485          179801 :   assert( sqlite3BtreeHoldsAllMutexes(db) );
  101486          179801 :   assert( sqlite3_mutex_held(db->mutex) );
  101487                 : 
  101488          179801 :   if( p ){
  101489               1 :     sqlite3ExpirePreparedStatements(db);
  101490                 :     do {
  101491               1 :       VTable *pNext = p->pNext;
  101492               1 :       sqlite3VtabUnlock(p);
  101493               1 :       p = pNext;
  101494               1 :     }while( p );
  101495                 :   }
  101496          179801 : }
  101497                 : 
  101498                 : /*
  101499                 : ** Clear any and all virtual-table information from the Table record.
  101500                 : ** This routine is called, for example, just before deleting the Table
  101501                 : ** record.
  101502                 : **
  101503                 : ** Since it is a virtual-table, the Table structure contains a pointer
  101504                 : ** to the head of a linked list of VTable structures. Each VTable 
  101505                 : ** structure is associated with a single sqlite3* user of the schema.
  101506                 : ** The reference count of the VTable structure associated with database 
  101507                 : ** connection db is decremented immediately (which may lead to the 
  101508                 : ** structure being xDisconnected and free). Any other VTable structures
  101509                 : ** in the list are moved to the sqlite3.pDisconnect list of the associated 
  101510                 : ** database connection.
  101511                 : */
  101512           36087 : SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
  101513           36087 :   if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
  101514           36087 :   if( p->azModuleArg ){
  101515                 :     int i;
  101516             428 :     for(i=0; i<p->nModuleArg; i++){
  101517             322 :       sqlite3DbFree(db, p->azModuleArg[i]);
  101518                 :     }
  101519             106 :     sqlite3DbFree(db, p->azModuleArg);
  101520                 :   }
  101521           36087 : }
  101522                 : 
  101523                 : /*
  101524                 : ** Add a new module argument to pTable->azModuleArg[].
  101525                 : ** The string is not copied - the pointer is stored.  The
  101526                 : ** string will be freed automatically when the table is
  101527                 : ** deleted.
  101528                 : */
  101529             322 : static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
  101530             322 :   int i = pTable->nModuleArg++;
  101531             322 :   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
  101532                 :   char **azModuleArg;
  101533             322 :   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
  101534             322 :   if( azModuleArg==0 ){
  101535                 :     int j;
  101536               0 :     for(j=0; j<i; j++){
  101537               0 :       sqlite3DbFree(db, pTable->azModuleArg[j]);
  101538                 :     }
  101539               0 :     sqlite3DbFree(db, zArg);
  101540               0 :     sqlite3DbFree(db, pTable->azModuleArg);
  101541               0 :     pTable->nModuleArg = 0;
  101542                 :   }else{
  101543             322 :     azModuleArg[i] = zArg;
  101544             322 :     azModuleArg[i+1] = 0;
  101545                 :   }
  101546             322 :   pTable->azModuleArg = azModuleArg;
  101547             322 : }
  101548                 : 
  101549                 : /*
  101550                 : ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
  101551                 : ** statement.  The module name has been parsed, but the optional list
  101552                 : ** of parameters that follow the module name are still pending.
  101553                 : */
  101554             106 : SQLITE_PRIVATE void sqlite3VtabBeginParse(
  101555                 :   Parse *pParse,        /* Parsing context */
  101556                 :   Token *pName1,        /* Name of new table, or database name */
  101557                 :   Token *pName2,        /* Name of new table or NULL */
  101558                 :   Token *pModuleName    /* Name of the module for the virtual table */
  101559                 : ){
  101560                 :   int iDb;              /* The database the table is being created in */
  101561                 :   Table *pTable;        /* The new virtual table */
  101562                 :   sqlite3 *db;          /* Database connection */
  101563                 : 
  101564             106 :   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0);
  101565             106 :   pTable = pParse->pNewTable;
  101566             106 :   if( pTable==0 ) return;
  101567             106 :   assert( 0==pTable->pIndex );
  101568                 : 
  101569             106 :   db = pParse->db;
  101570             106 :   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
  101571             106 :   assert( iDb>=0 );
  101572                 : 
  101573             106 :   pTable->tabFlags |= TF_Virtual;
  101574             106 :   pTable->nModuleArg = 0;
  101575             106 :   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
  101576             106 :   addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName));
  101577             106 :   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
  101578             106 :   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
  101579                 : 
  101580                 : #ifndef SQLITE_OMIT_AUTHORIZATION
  101581                 :   /* Creating a virtual table invokes the authorization callback twice.
  101582                 :   ** The first invocation, to obtain permission to INSERT a row into the
  101583                 :   ** sqlite_master table, has already been made by sqlite3StartTable().
  101584                 :   ** The second call, to obtain permission to create the table, is made now.
  101585                 :   */
  101586             106 :   if( pTable->azModuleArg ){
  101587             106 :     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, 
  101588             106 :             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
  101589                 :   }
  101590                 : #endif
  101591                 : }
  101592                 : 
  101593                 : /*
  101594                 : ** This routine takes the module argument that has been accumulating
  101595                 : ** in pParse->zArg[] and appends it to the list of arguments on the
  101596                 : ** virtual table currently under construction in pParse->pTable.
  101597                 : */
  101598             110 : static void addArgumentToVtab(Parse *pParse){
  101599             110 :   if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){
  101600               4 :     const char *z = (const char*)pParse->sArg.z;
  101601               4 :     int n = pParse->sArg.n;
  101602               4 :     sqlite3 *db = pParse->db;
  101603               4 :     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
  101604                 :   }
  101605             110 : }
  101606                 : 
  101607                 : /*
  101608                 : ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
  101609                 : ** has been completely parsed.
  101610                 : */
  101611             106 : SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
  101612             106 :   Table *pTab = pParse->pNewTable;  /* The table being constructed */
  101613             106 :   sqlite3 *db = pParse->db;         /* The database connection */
  101614                 : 
  101615             106 :   if( pTab==0 ) return;
  101616             106 :   addArgumentToVtab(pParse);
  101617             106 :   pParse->sArg.z = 0;
  101618             106 :   if( pTab->nModuleArg<1 ) return;
  101619                 :   
  101620                 :   /* If the CREATE VIRTUAL TABLE statement is being entered for the
  101621                 :   ** first time (in other words if the virtual table is actually being
  101622                 :   ** created now instead of just being read out of sqlite_master) then
  101623                 :   ** do additional initialization work and store the statement text
  101624                 :   ** in the sqlite_master table.
  101625                 :   */
  101626             106 :   if( !db->init.busy ){
  101627                 :     char *zStmt;
  101628                 :     char *zWhere;
  101629                 :     int iDb;
  101630                 :     Vdbe *v;
  101631                 : 
  101632                 :     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
  101633              53 :     if( pEnd ){
  101634               1 :       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
  101635                 :     }
  101636              53 :     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
  101637                 : 
  101638                 :     /* A slot for the record has already been allocated in the 
  101639                 :     ** SQLITE_MASTER table.  We just need to update that slot with all
  101640                 :     ** the information we've collected.  
  101641                 :     **
  101642                 :     ** The VM register number pParse->regRowid holds the rowid of an
  101643                 :     ** entry in the sqlite_master table tht was created for this vtab
  101644                 :     ** by sqlite3StartTable().
  101645                 :     */
  101646              53 :     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  101647             106 :     sqlite3NestedParse(pParse,
  101648                 :       "UPDATE %Q.%s "
  101649                 :          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
  101650                 :        "WHERE rowid=#%d",
  101651              53 :       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
  101652                 :       pTab->zName,
  101653                 :       pTab->zName,
  101654                 :       zStmt,
  101655                 :       pParse->regRowid
  101656                 :     );
  101657              53 :     sqlite3DbFree(db, zStmt);
  101658              53 :     v = sqlite3GetVdbe(pParse);
  101659              53 :     sqlite3ChangeCookie(pParse, iDb);
  101660                 : 
  101661              53 :     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
  101662              53 :     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
  101663              53 :     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
  101664             106 :     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, 
  101665             106 :                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
  101666                 :   }
  101667                 : 
  101668                 :   /* If we are rereading the sqlite_master table create the in-memory
  101669                 :   ** record of the table. The xConnect() method is not called until
  101670                 :   ** the first time the virtual table is used in an SQL statement. This
  101671                 :   ** allows a schema that contains virtual tables to be loaded before
  101672                 :   ** the required virtual table implementations are registered.  */
  101673                 :   else {
  101674                 :     Table *pOld;
  101675              53 :     Schema *pSchema = pTab->pSchema;
  101676              53 :     const char *zName = pTab->zName;
  101677              53 :     int nName = sqlite3Strlen30(zName);
  101678              53 :     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
  101679              53 :     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
  101680              53 :     if( pOld ){
  101681               0 :       db->mallocFailed = 1;
  101682               0 :       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
  101683               0 :       return;
  101684                 :     }
  101685              53 :     pParse->pNewTable = 0;
  101686                 :   }
  101687                 : }
  101688                 : 
  101689                 : /*
  101690                 : ** The parser calls this routine when it sees the first token
  101691                 : ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
  101692                 : */
  101693               4 : SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
  101694               4 :   addArgumentToVtab(pParse);
  101695               4 :   pParse->sArg.z = 0;
  101696               4 :   pParse->sArg.n = 0;
  101697               4 : }
  101698                 : 
  101699                 : /*
  101700                 : ** The parser calls this routine for each token after the first token
  101701                 : ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
  101702                 : */
  101703               4 : SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
  101704               4 :   Token *pArg = &pParse->sArg;
  101705               4 :   if( pArg->z==0 ){
  101706               4 :     pArg->z = p->z;
  101707               4 :     pArg->n = p->n;
  101708                 :   }else{
  101709               0 :     assert(pArg->z < p->z);
  101710               0 :     pArg->n = (int)(&p->z[p->n] - pArg->z);
  101711                 :   }
  101712               4 : }
  101713                 : 
  101714                 : /*
  101715                 : ** Invoke a virtual table constructor (either xCreate or xConnect). The
  101716                 : ** pointer to the function to invoke is passed as the fourth parameter
  101717                 : ** to this procedure.
  101718                 : */
  101719              53 : static int vtabCallConstructor(
  101720                 :   sqlite3 *db, 
  101721                 :   Table *pTab,
  101722                 :   Module *pMod,
  101723                 :   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
  101724                 :   char **pzErr
  101725                 : ){
  101726                 :   VtabCtx sCtx;
  101727                 :   VTable *pVTable;
  101728                 :   int rc;
  101729              53 :   const char *const*azArg = (const char *const*)pTab->azModuleArg;
  101730              53 :   int nArg = pTab->nModuleArg;
  101731              53 :   char *zErr = 0;
  101732              53 :   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
  101733                 : 
  101734              53 :   if( !zModuleName ){
  101735               0 :     return SQLITE_NOMEM;
  101736                 :   }
  101737                 : 
  101738              53 :   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
  101739              53 :   if( !pVTable ){
  101740               0 :     sqlite3DbFree(db, zModuleName);
  101741               0 :     return SQLITE_NOMEM;
  101742                 :   }
  101743              53 :   pVTable->db = db;
  101744              53 :   pVTable->pMod = pMod;
  101745                 : 
  101746                 :   /* Invoke the virtual table constructor */
  101747              53 :   assert( &db->pVtabCtx );
  101748              53 :   assert( xConstruct );
  101749              53 :   sCtx.pTab = pTab;
  101750              53 :   sCtx.pVTable = pVTable;
  101751              53 :   db->pVtabCtx = &sCtx;
  101752              53 :   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
  101753              53 :   db->pVtabCtx = 0;
  101754              53 :   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
  101755                 : 
  101756              53 :   if( SQLITE_OK!=rc ){
  101757               0 :     if( zErr==0 ){
  101758               0 :       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
  101759                 :     }else {
  101760               0 :       *pzErr = sqlite3MPrintf(db, "%s", zErr);
  101761               0 :       sqlite3_free(zErr);
  101762                 :     }
  101763               0 :     sqlite3DbFree(db, pVTable);
  101764              53 :   }else if( ALWAYS(pVTable->pVtab) ){
  101765                 :     /* Justification of ALWAYS():  A correct vtab constructor must allocate
  101766                 :     ** the sqlite3_vtab object if successful.  */
  101767              53 :     pVTable->pVtab->pModule = pMod->pModule;
  101768              53 :     pVTable->nRef = 1;
  101769              53 :     if( sCtx.pTab ){
  101770               0 :       const char *zFormat = "vtable constructor did not declare schema: %s";
  101771               0 :       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
  101772               0 :       sqlite3VtabUnlock(pVTable);
  101773               0 :       rc = SQLITE_ERROR;
  101774                 :     }else{
  101775                 :       int iCol;
  101776                 :       /* If everything went according to plan, link the new VTable structure
  101777                 :       ** into the linked list headed by pTab->pVTable. Then loop through the 
  101778                 :       ** columns of the table to see if any of them contain the token "hidden".
  101779                 :       ** If so, set the Column.isHidden flag and remove the token from
  101780                 :       ** the type string.  */
  101781              53 :       pVTable->pNext = pTab->pVTable;
  101782              53 :       pTab->pVTable = pVTable;
  101783                 : 
  101784             161 :       for(iCol=0; iCol<pTab->nCol; iCol++){
  101785             108 :         char *zType = pTab->aCol[iCol].zType;
  101786                 :         int nType;
  101787             108 :         int i = 0;
  101788             108 :         if( !zType ) continue;
  101789             106 :         nType = sqlite3Strlen30(zType);
  101790             106 :         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
  101791             520 :           for(i=0; i<nType; i++){
  101792             416 :             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
  101793               0 :              && (zType[i+7]=='\0' || zType[i+7]==' ')
  101794                 :             ){
  101795               0 :               i++;
  101796               0 :               break;
  101797                 :             }
  101798                 :           }
  101799                 :         }
  101800             106 :         if( i<nType ){
  101801                 :           int j;
  101802               2 :           int nDel = 6 + (zType[i+6] ? 1 : 0);
  101803               4 :           for(j=i; (j+nDel)<=nType; j++){
  101804               2 :             zType[j] = zType[j+nDel];
  101805                 :           }
  101806               2 :           if( zType[i]=='\0' && i>0 ){
  101807               0 :             assert(zType[i-1]==' ');
  101808               0 :             zType[i-1] = '\0';
  101809                 :           }
  101810               2 :           pTab->aCol[iCol].isHidden = 1;
  101811                 :         }
  101812                 :       }
  101813                 :     }
  101814                 :   }
  101815                 : 
  101816              53 :   sqlite3DbFree(db, zModuleName);
  101817              53 :   return rc;
  101818                 : }
  101819                 : 
  101820                 : /*
  101821                 : ** This function is invoked by the parser to call the xConnect() method
  101822                 : ** of the virtual table pTab. If an error occurs, an error code is returned 
  101823                 : ** and an error left in pParse.
  101824                 : **
  101825                 : ** This call is a no-op if table pTab is not a virtual table.
  101826                 : */
  101827           41766 : SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
  101828           41766 :   sqlite3 *db = pParse->db;
  101829                 :   const char *zMod;
  101830                 :   Module *pMod;
  101831                 :   int rc;
  101832                 : 
  101833           41766 :   assert( pTab );
  101834           41766 :   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
  101835           41766 :     return SQLITE_OK;
  101836                 :   }
  101837                 : 
  101838                 :   /* Locate the required virtual table module */
  101839               0 :   zMod = pTab->azModuleArg[0];
  101840               0 :   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
  101841                 : 
  101842               0 :   if( !pMod ){
  101843               0 :     const char *zModule = pTab->azModuleArg[0];
  101844               0 :     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
  101845               0 :     rc = SQLITE_ERROR;
  101846                 :   }else{
  101847               0 :     char *zErr = 0;
  101848               0 :     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
  101849               0 :     if( rc!=SQLITE_OK ){
  101850               0 :       sqlite3ErrorMsg(pParse, "%s", zErr);
  101851                 :     }
  101852               0 :     sqlite3DbFree(db, zErr);
  101853                 :   }
  101854                 : 
  101855               0 :   return rc;
  101856                 : }
  101857                 : /*
  101858                 : ** Grow the db->aVTrans[] array so that there is room for at least one
  101859                 : ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
  101860                 : */
  101861              57 : static int growVTrans(sqlite3 *db){
  101862              57 :   const int ARRAY_INCR = 5;
  101863                 : 
  101864                 :   /* Grow the sqlite3.aVTrans array if required */
  101865              57 :   if( (db->nVTrans%ARRAY_INCR)==0 ){
  101866                 :     VTable **aVTrans;
  101867              57 :     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
  101868              57 :     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
  101869              57 :     if( !aVTrans ){
  101870               0 :       return SQLITE_NOMEM;
  101871                 :     }
  101872              57 :     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
  101873              57 :     db->aVTrans = aVTrans;
  101874                 :   }
  101875                 : 
  101876              57 :   return SQLITE_OK;
  101877                 : }
  101878                 : 
  101879                 : /*
  101880                 : ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
  101881                 : ** have already been reserved using growVTrans().
  101882                 : */
  101883              57 : static void addToVTrans(sqlite3 *db, VTable *pVTab){
  101884                 :   /* Add pVtab to the end of sqlite3.aVTrans */
  101885              57 :   db->aVTrans[db->nVTrans++] = pVTab;
  101886              57 :   sqlite3VtabLock(pVTab);
  101887              57 : }
  101888                 : 
  101889                 : /*
  101890                 : ** This function is invoked by the vdbe to call the xCreate method
  101891                 : ** of the virtual table named zTab in database iDb. 
  101892                 : **
  101893                 : ** If an error occurs, *pzErr is set to point an an English language
  101894                 : ** description of the error and an SQLITE_XXX error code is returned.
  101895                 : ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
  101896                 : */
  101897              53 : SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
  101898              53 :   int rc = SQLITE_OK;
  101899                 :   Table *pTab;
  101900                 :   Module *pMod;
  101901                 :   const char *zMod;
  101902                 : 
  101903              53 :   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
  101904              53 :   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
  101905                 : 
  101906                 :   /* Locate the required virtual table module */
  101907              53 :   zMod = pTab->azModuleArg[0];
  101908              53 :   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
  101909                 : 
  101910                 :   /* If the module has been registered and includes a Create method, 
  101911                 :   ** invoke it now. If the module has not been registered, return an 
  101912                 :   ** error. Otherwise, do nothing.
  101913                 :   */
  101914              53 :   if( !pMod ){
  101915               0 :     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
  101916               0 :     rc = SQLITE_ERROR;
  101917                 :   }else{
  101918              53 :     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
  101919                 :   }
  101920                 : 
  101921                 :   /* Justification of ALWAYS():  The xConstructor method is required to
  101922                 :   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
  101923              53 :   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
  101924              53 :     rc = growVTrans(db);
  101925              53 :     if( rc==SQLITE_OK ){
  101926              53 :       addToVTrans(db, sqlite3GetVTable(db, pTab));
  101927                 :     }
  101928                 :   }
  101929                 : 
  101930              53 :   return rc;
  101931                 : }
  101932                 : 
  101933                 : /*
  101934                 : ** This function is used to set the schema of a virtual table.  It is only
  101935                 : ** valid to call this function from within the xCreate() or xConnect() of a
  101936                 : ** virtual table module.
  101937                 : */
  101938              53 : SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
  101939                 :   Parse *pParse;
  101940                 : 
  101941              53 :   int rc = SQLITE_OK;
  101942                 :   Table *pTab;
  101943              53 :   char *zErr = 0;
  101944                 : 
  101945              53 :   sqlite3_mutex_enter(db->mutex);
  101946              53 :   if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
  101947               0 :     sqlite3Error(db, SQLITE_MISUSE, 0);
  101948               0 :     sqlite3_mutex_leave(db->mutex);
  101949               0 :     return SQLITE_MISUSE_BKPT;
  101950                 :   }
  101951              53 :   assert( (pTab->tabFlags & TF_Virtual)!=0 );
  101952                 : 
  101953              53 :   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
  101954              53 :   if( pParse==0 ){
  101955               0 :     rc = SQLITE_NOMEM;
  101956                 :   }else{
  101957              53 :     pParse->declareVtab = 1;
  101958              53 :     pParse->db = db;
  101959              53 :     pParse->nQueryLoop = 1;
  101960                 :   
  101961              53 :     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) 
  101962              53 :      && pParse->pNewTable
  101963              53 :      && !db->mallocFailed
  101964              53 :      && !pParse->pNewTable->pSelect
  101965              53 :      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
  101966                 :     ){
  101967              53 :       if( !pTab->aCol ){
  101968              53 :         pTab->aCol = pParse->pNewTable->aCol;
  101969              53 :         pTab->nCol = pParse->pNewTable->nCol;
  101970              53 :         pParse->pNewTable->nCol = 0;
  101971              53 :         pParse->pNewTable->aCol = 0;
  101972                 :       }
  101973              53 :       db->pVtabCtx->pTab = 0;
  101974                 :     }else{
  101975               0 :       sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
  101976               0 :       sqlite3DbFree(db, zErr);
  101977               0 :       rc = SQLITE_ERROR;
  101978                 :     }
  101979              53 :     pParse->declareVtab = 0;
  101980                 :   
  101981              53 :     if( pParse->pVdbe ){
  101982              53 :       sqlite3VdbeFinalize(pParse->pVdbe);
  101983                 :     }
  101984              53 :     sqlite3DeleteTable(db, pParse->pNewTable);
  101985              53 :     sqlite3StackFree(db, pParse);
  101986                 :   }
  101987                 : 
  101988              53 :   assert( (rc&0xff)==rc );
  101989              53 :   rc = sqlite3ApiExit(db, rc);
  101990              53 :   sqlite3_mutex_leave(db->mutex);
  101991              53 :   return rc;
  101992                 : }
  101993                 : 
  101994                 : /*
  101995                 : ** This function is invoked by the vdbe to call the xDestroy method
  101996                 : ** of the virtual table named zTab in database iDb. This occurs
  101997                 : ** when a DROP TABLE is mentioned.
  101998                 : **
  101999                 : ** This call is a no-op if zTab is not a virtual table.
  102000                 : */
  102001              52 : SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
  102002              52 :   int rc = SQLITE_OK;
  102003                 :   Table *pTab;
  102004                 : 
  102005              52 :   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
  102006              52 :   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
  102007              52 :     VTable *p = vtabDisconnectAll(db, pTab);
  102008                 : 
  102009              52 :     assert( rc==SQLITE_OK );
  102010              52 :     rc = p->pMod->pModule->xDestroy(p->pVtab);
  102011                 : 
  102012                 :     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
  102013              52 :     if( rc==SQLITE_OK ){
  102014              52 :       assert( pTab->pVTable==p && p->pNext==0 );
  102015              52 :       p->pVtab = 0;
  102016              52 :       pTab->pVTable = 0;
  102017              52 :       sqlite3VtabUnlock(p);
  102018                 :     }
  102019                 :   }
  102020                 : 
  102021              52 :   return rc;
  102022                 : }
  102023                 : 
  102024                 : /*
  102025                 : ** This function invokes either the xRollback or xCommit method
  102026                 : ** of each of the virtual tables in the sqlite3.aVTrans array. The method
  102027                 : ** called is identified by the second argument, "offset", which is
  102028                 : ** the offset of the method to call in the sqlite3_module structure.
  102029                 : **
  102030                 : ** The array is cleared after invoking the callbacks. 
  102031                 : */
  102032          103773 : static void callFinaliser(sqlite3 *db, int offset){
  102033                 :   int i;
  102034          103773 :   if( db->aVTrans ){
  102035             114 :     for(i=0; i<db->nVTrans; i++){
  102036              57 :       VTable *pVTab = db->aVTrans[i];
  102037              57 :       sqlite3_vtab *p = pVTab->pVtab;
  102038              57 :       if( p ){
  102039                 :         int (*x)(sqlite3_vtab *);
  102040               5 :         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
  102041               5 :         if( x ) x(p);
  102042                 :       }
  102043              57 :       pVTab->iSavepoint = 0;
  102044              57 :       sqlite3VtabUnlock(pVTab);
  102045                 :     }
  102046              57 :     sqlite3DbFree(db, db->aVTrans);
  102047              57 :     db->nVTrans = 0;
  102048              57 :     db->aVTrans = 0;
  102049                 :   }
  102050          103773 : }
  102051                 : 
  102052                 : /*
  102053                 : ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
  102054                 : ** array. Return the error code for the first error that occurs, or
  102055                 : ** SQLITE_OK if all xSync operations are successful.
  102056                 : **
  102057                 : ** Set *pzErrmsg to point to a buffer that should be released using 
  102058                 : ** sqlite3DbFree() containing an error message, if one is available.
  102059                 : */
  102060          100131 : SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
  102061                 :   int i;
  102062          100131 :   int rc = SQLITE_OK;
  102063          100131 :   VTable **aVTrans = db->aVTrans;
  102064                 : 
  102065          100131 :   db->aVTrans = 0;
  102066          100188 :   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
  102067                 :     int (*x)(sqlite3_vtab *);
  102068              57 :     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
  102069              57 :     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
  102070               5 :       rc = x(pVtab);
  102071               5 :       sqlite3DbFree(db, *pzErrmsg);
  102072               5 :       *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
  102073               5 :       sqlite3_free(pVtab->zErrMsg);
  102074                 :     }
  102075                 :   }
  102076          100131 :   db->aVTrans = aVTrans;
  102077          100131 :   return rc;
  102078                 : }
  102079                 : 
  102080                 : /*
  102081                 : ** Invoke the xRollback method of all virtual tables in the 
  102082                 : ** sqlite3.aVTrans array. Then clear the array itself.
  102083                 : */
  102084            3642 : SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
  102085            3642 :   callFinaliser(db, offsetof(sqlite3_module,xRollback));
  102086            3642 :   return SQLITE_OK;
  102087                 : }
  102088                 : 
  102089                 : /*
  102090                 : ** Invoke the xCommit method of all virtual tables in the 
  102091                 : ** sqlite3.aVTrans array. Then clear the array itself.
  102092                 : */
  102093          100131 : SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
  102094          100131 :   callFinaliser(db, offsetof(sqlite3_module,xCommit));
  102095          100131 :   return SQLITE_OK;
  102096                 : }
  102097                 : 
  102098                 : /*
  102099                 : ** If the virtual table pVtab supports the transaction interface
  102100                 : ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
  102101                 : ** not currently open, invoke the xBegin method now.
  102102                 : **
  102103                 : ** If the xBegin call is successful, place the sqlite3_vtab pointer
  102104                 : ** in the sqlite3.aVTrans array.
  102105                 : */
  102106             109 : SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
  102107             109 :   int rc = SQLITE_OK;
  102108                 :   const sqlite3_module *pModule;
  102109                 : 
  102110                 :   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
  102111                 :   ** than zero, then this function is being called from within a
  102112                 :   ** virtual module xSync() callback. It is illegal to write to 
  102113                 :   ** virtual module tables in this case, so return SQLITE_LOCKED.
  102114                 :   */
  102115             109 :   if( sqlite3VtabInSync(db) ){
  102116               0 :     return SQLITE_LOCKED;
  102117                 :   }
  102118             109 :   if( !pVTab ){
  102119             105 :     return SQLITE_OK;
  102120                 :   } 
  102121               4 :   pModule = pVTab->pVtab->pModule;
  102122                 : 
  102123               4 :   if( pModule->xBegin ){
  102124                 :     int i;
  102125                 : 
  102126                 :     /* If pVtab is already in the aVTrans array, return early */
  102127               4 :     for(i=0; i<db->nVTrans; i++){
  102128               0 :       if( db->aVTrans[i]==pVTab ){
  102129               0 :         return SQLITE_OK;
  102130                 :       }
  102131                 :     }
  102132                 : 
  102133                 :     /* Invoke the xBegin method. If successful, add the vtab to the 
  102134                 :     ** sqlite3.aVTrans[] array. */
  102135               4 :     rc = growVTrans(db);
  102136               4 :     if( rc==SQLITE_OK ){
  102137               4 :       rc = pModule->xBegin(pVTab->pVtab);
  102138               4 :       if( rc==SQLITE_OK ){
  102139               4 :         addToVTrans(db, pVTab);
  102140                 :       }
  102141                 :     }
  102142                 :   }
  102143               4 :   return rc;
  102144                 : }
  102145                 : 
  102146                 : /*
  102147                 : ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
  102148                 : ** virtual tables that currently have an open transaction. Pass iSavepoint
  102149                 : ** as the second argument to the virtual table method invoked.
  102150                 : **
  102151                 : ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
  102152                 : ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is 
  102153                 : ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
  102154                 : ** an open transaction is invoked.
  102155                 : **
  102156                 : ** If any virtual table method returns an error code other than SQLITE_OK, 
  102157                 : ** processing is abandoned and the error returned to the caller of this
  102158                 : ** function immediately. If all calls to virtual table methods are successful,
  102159                 : ** SQLITE_OK is returned.
  102160                 : */
  102161           50992 : SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
  102162           50992 :   int rc = SQLITE_OK;
  102163                 : 
  102164           50992 :   assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
  102165           50992 :   assert( iSavepoint>=0 );
  102166           50992 :   if( db->aVTrans ){
  102167                 :     int i;
  102168               0 :     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
  102169               0 :       VTable *pVTab = db->aVTrans[i];
  102170               0 :       const sqlite3_module *pMod = pVTab->pMod->pModule;
  102171               0 :       if( pVTab->pVtab && pMod->iVersion>=2 ){
  102172                 :         int (*xMethod)(sqlite3_vtab *, int);
  102173               0 :         switch( op ){
  102174                 :           case SAVEPOINT_BEGIN:
  102175               0 :             xMethod = pMod->xSavepoint;
  102176               0 :             pVTab->iSavepoint = iSavepoint+1;
  102177               0 :             break;
  102178                 :           case SAVEPOINT_ROLLBACK:
  102179               0 :             xMethod = pMod->xRollbackTo;
  102180               0 :             break;
  102181                 :           default:
  102182               0 :             xMethod = pMod->xRelease;
  102183               0 :             break;
  102184                 :         }
  102185               0 :         if( xMethod && pVTab->iSavepoint>iSavepoint ){
  102186               0 :           rc = xMethod(pVTab->pVtab, iSavepoint);
  102187                 :         }
  102188                 :       }
  102189                 :     }
  102190                 :   }
  102191           50992 :   return rc;
  102192                 : }
  102193                 : 
  102194                 : /*
  102195                 : ** The first parameter (pDef) is a function implementation.  The
  102196                 : ** second parameter (pExpr) is the first argument to this function.
  102197                 : ** If pExpr is a column in a virtual table, then let the virtual
  102198                 : ** table implementation have an opportunity to overload the function.
  102199                 : **
  102200                 : ** This routine is used to allow virtual table implementations to
  102201                 : ** overload MATCH, LIKE, GLOB, and REGEXP operators.
  102202                 : **
  102203                 : ** Return either the pDef argument (indicating no change) or a 
  102204                 : ** new FuncDef structure that is marked as ephemeral using the
  102205                 : ** SQLITE_FUNC_EPHEM flag.
  102206                 : */
  102207            9907 : SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
  102208                 :   sqlite3 *db,    /* Database connection for reporting malloc problems */
  102209                 :   FuncDef *pDef,  /* Function to possibly overload */
  102210                 :   int nArg,       /* Number of arguments to the function */
  102211                 :   Expr *pExpr     /* First argument to the function */
  102212                 : ){
  102213                 :   Table *pTab;
  102214                 :   sqlite3_vtab *pVtab;
  102215                 :   sqlite3_module *pMod;
  102216            9907 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
  102217            9907 :   void *pArg = 0;
  102218                 :   FuncDef *pNew;
  102219            9907 :   int rc = 0;
  102220                 :   char *zLowerName;
  102221                 :   unsigned char *z;
  102222                 : 
  102223                 : 
  102224                 :   /* Check to see the left operand is a column in a virtual table */
  102225            9907 :   if( NEVER(pExpr==0) ) return pDef;
  102226            9907 :   if( pExpr->op!=TK_COLUMN ) return pDef;
  102227            1623 :   pTab = pExpr->pTab;
  102228            1623 :   if( NEVER(pTab==0) ) return pDef;
  102229            1623 :   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
  102230               0 :   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
  102231               0 :   assert( pVtab!=0 );
  102232               0 :   assert( pVtab->pModule!=0 );
  102233               0 :   pMod = (sqlite3_module *)pVtab->pModule;
  102234               0 :   if( pMod->xFindFunction==0 ) return pDef;
  102235                 :  
  102236                 :   /* Call the xFindFunction method on the virtual table implementation
  102237                 :   ** to see if the implementation wants to overload this function 
  102238                 :   */
  102239               0 :   zLowerName = sqlite3DbStrDup(db, pDef->zName);
  102240               0 :   if( zLowerName ){
  102241               0 :     for(z=(unsigned char*)zLowerName; *z; z++){
  102242               0 :       *z = sqlite3UpperToLower[*z];
  102243                 :     }
  102244               0 :     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
  102245               0 :     sqlite3DbFree(db, zLowerName);
  102246                 :   }
  102247               0 :   if( rc==0 ){
  102248               0 :     return pDef;
  102249                 :   }
  102250                 : 
  102251                 :   /* Create a new ephemeral function definition for the overloaded
  102252                 :   ** function */
  102253               0 :   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
  102254               0 :                              + sqlite3Strlen30(pDef->zName) + 1);
  102255               0 :   if( pNew==0 ){
  102256               0 :     return pDef;
  102257                 :   }
  102258               0 :   *pNew = *pDef;
  102259               0 :   pNew->zName = (char *)&pNew[1];
  102260               0 :   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
  102261               0 :   pNew->xFunc = xFunc;
  102262               0 :   pNew->pUserData = pArg;
  102263               0 :   pNew->flags |= SQLITE_FUNC_EPHEM;
  102264               0 :   return pNew;
  102265                 : }
  102266                 : 
  102267                 : /*
  102268                 : ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
  102269                 : ** array so that an OP_VBegin will get generated for it.  Add pTab to the
  102270                 : ** array if it is missing.  If pTab is already in the array, this routine
  102271                 : ** is a no-op.
  102272                 : */
  102273               4 : SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
  102274               4 :   Parse *pToplevel = sqlite3ParseToplevel(pParse);
  102275                 :   int i, n;
  102276                 :   Table **apVtabLock;
  102277                 : 
  102278               4 :   assert( IsVirtual(pTab) );
  102279               4 :   for(i=0; i<pToplevel->nVtabLock; i++){
  102280               0 :     if( pTab==pToplevel->apVtabLock[i] ) return;
  102281                 :   }
  102282               4 :   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
  102283               4 :   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
  102284               4 :   if( apVtabLock ){
  102285               4 :     pToplevel->apVtabLock = apVtabLock;
  102286               4 :     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
  102287                 :   }else{
  102288               0 :     pToplevel->db->mallocFailed = 1;
  102289                 :   }
  102290                 : }
  102291                 : 
  102292                 : /*
  102293                 : ** Return the ON CONFLICT resolution mode in effect for the virtual
  102294                 : ** table update operation currently in progress.
  102295                 : **
  102296                 : ** The results of this routine are undefined unless it is called from
  102297                 : ** within an xUpdate method.
  102298                 : */
  102299               0 : SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
  102300                 :   static const unsigned char aMap[] = { 
  102301                 :     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE 
  102302                 :   };
  102303                 :   assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
  102304                 :   assert( OE_Ignore==4 && OE_Replace==5 );
  102305               0 :   assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
  102306               0 :   return (int)aMap[db->vtabOnConflict-1];
  102307                 : }
  102308                 : 
  102309                 : /*
  102310                 : ** Call from within the xCreate() or xConnect() methods to provide 
  102311                 : ** the SQLite core with additional information about the behavior
  102312                 : ** of the virtual table being implemented.
  102313                 : */
  102314               1 : SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
  102315                 :   va_list ap;
  102316               1 :   int rc = SQLITE_OK;
  102317                 : 
  102318               1 :   sqlite3_mutex_enter(db->mutex);
  102319                 : 
  102320               1 :   va_start(ap, op);
  102321               1 :   switch( op ){
  102322                 :     case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
  102323               1 :       VtabCtx *p = db->pVtabCtx;
  102324               1 :       if( !p ){
  102325               0 :         rc = SQLITE_MISUSE_BKPT;
  102326                 :       }else{
  102327               1 :         assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
  102328               1 :         p->pVTable->bConstraint = (u8)va_arg(ap, int);
  102329                 :       }
  102330               1 :       break;
  102331                 :     }
  102332                 :     default:
  102333               0 :       rc = SQLITE_MISUSE_BKPT;
  102334               0 :       break;
  102335                 :   }
  102336               1 :   va_end(ap);
  102337                 : 
  102338               1 :   if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
  102339               1 :   sqlite3_mutex_leave(db->mutex);
  102340               1 :   return rc;
  102341                 : }
  102342                 : 
  102343                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
  102344                 : 
  102345                 : /************** End of vtab.c ************************************************/
  102346                 : /************** Begin file where.c *******************************************/
  102347                 : /*
  102348                 : ** 2001 September 15
  102349                 : **
  102350                 : ** The author disclaims copyright to this source code.  In place of
  102351                 : ** a legal notice, here is a blessing:
  102352                 : **
  102353                 : **    May you do good and not evil.
  102354                 : **    May you find forgiveness for yourself and forgive others.
  102355                 : **    May you share freely, never taking more than you give.
  102356                 : **
  102357                 : *************************************************************************
  102358                 : ** This module contains C code that generates VDBE code used to process
  102359                 : ** the WHERE clause of SQL statements.  This module is responsible for
  102360                 : ** generating the code that loops through a table looking for applicable
  102361                 : ** rows.  Indices are selected and used to speed the search when doing
  102362                 : ** so is applicable.  Because this module is responsible for selecting
  102363                 : ** indices, you might also think of this module as the "query optimizer".
  102364                 : */
  102365                 : 
  102366                 : 
  102367                 : /*
  102368                 : ** Trace output macros
  102369                 : */
  102370                 : #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
  102371                 : SQLITE_PRIVATE int sqlite3WhereTrace = 0;
  102372                 : #endif
  102373                 : #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  102374                 : # define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
  102375                 : #else
  102376                 : # define WHERETRACE(X)
  102377                 : #endif
  102378                 : 
  102379                 : /* Forward reference
  102380                 : */
  102381                 : typedef struct WhereClause WhereClause;
  102382                 : typedef struct WhereMaskSet WhereMaskSet;
  102383                 : typedef struct WhereOrInfo WhereOrInfo;
  102384                 : typedef struct WhereAndInfo WhereAndInfo;
  102385                 : typedef struct WhereCost WhereCost;
  102386                 : 
  102387                 : /*
  102388                 : ** The query generator uses an array of instances of this structure to
  102389                 : ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
  102390                 : ** clause subexpression is separated from the others by AND operators,
  102391                 : ** usually, or sometimes subexpressions separated by OR.
  102392                 : **
  102393                 : ** All WhereTerms are collected into a single WhereClause structure.  
  102394                 : ** The following identity holds:
  102395                 : **
  102396                 : **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
  102397                 : **
  102398                 : ** When a term is of the form:
  102399                 : **
  102400                 : **              X <op> <expr>
  102401                 : **
  102402                 : ** where X is a column name and <op> is one of certain operators,
  102403                 : ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
  102404                 : ** cursor number and column number for X.  WhereTerm.eOperator records
  102405                 : ** the <op> using a bitmask encoding defined by WO_xxx below.  The
  102406                 : ** use of a bitmask encoding for the operator allows us to search
  102407                 : ** quickly for terms that match any of several different operators.
  102408                 : **
  102409                 : ** A WhereTerm might also be two or more subterms connected by OR:
  102410                 : **
  102411                 : **         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
  102412                 : **
  102413                 : ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
  102414                 : ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
  102415                 : ** is collected about the
  102416                 : **
  102417                 : ** If a term in the WHERE clause does not match either of the two previous
  102418                 : ** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
  102419                 : ** to the original subexpression content and wtFlags is set up appropriately
  102420                 : ** but no other fields in the WhereTerm object are meaningful.
  102421                 : **
  102422                 : ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
  102423                 : ** but they do so indirectly.  A single WhereMaskSet structure translates
  102424                 : ** cursor number into bits and the translated bit is stored in the prereq
  102425                 : ** fields.  The translation is used in order to maximize the number of
  102426                 : ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
  102427                 : ** spread out over the non-negative integers.  For example, the cursor
  102428                 : ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
  102429                 : ** translates these sparse cursor numbers into consecutive integers
  102430                 : ** beginning with 0 in order to make the best possible use of the available
  102431                 : ** bits in the Bitmask.  So, in the example above, the cursor numbers
  102432                 : ** would be mapped into integers 0 through 7.
  102433                 : **
  102434                 : ** The number of terms in a join is limited by the number of bits
  102435                 : ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
  102436                 : ** is only able to process joins with 64 or fewer tables.
  102437                 : */
  102438                 : typedef struct WhereTerm WhereTerm;
  102439                 : struct WhereTerm {
  102440                 :   Expr *pExpr;            /* Pointer to the subexpression that is this term */
  102441                 :   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
  102442                 :   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
  102443                 :   union {
  102444                 :     int leftColumn;         /* Column number of X in "X <op> <expr>" */
  102445                 :     WhereOrInfo *pOrInfo;   /* Extra information if eOperator==WO_OR */
  102446                 :     WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */
  102447                 :   } u;
  102448                 :   u16 eOperator;          /* A WO_xx value describing <op> */
  102449                 :   u8 wtFlags;             /* TERM_xxx bit flags.  See below */
  102450                 :   u8 nChild;              /* Number of children that must disable us */
  102451                 :   WhereClause *pWC;       /* The clause this term is part of */
  102452                 :   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
  102453                 :   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
  102454                 : };
  102455                 : 
  102456                 : /*
  102457                 : ** Allowed values of WhereTerm.wtFlags
  102458                 : */
  102459                 : #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
  102460                 : #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
  102461                 : #define TERM_CODED      0x04   /* This term is already coded */
  102462                 : #define TERM_COPIED     0x08   /* Has a child */
  102463                 : #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
  102464                 : #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
  102465                 : #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
  102466                 : #ifdef SQLITE_ENABLE_STAT3
  102467                 : #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
  102468                 : #else
  102469                 : #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
  102470                 : #endif
  102471                 : 
  102472                 : /*
  102473                 : ** An instance of the following structure holds all information about a
  102474                 : ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
  102475                 : **
  102476                 : ** Explanation of pOuter:  For a WHERE clause of the form
  102477                 : **
  102478                 : **           a AND ((b AND c) OR (d AND e)) AND f
  102479                 : **
  102480                 : ** There are separate WhereClause objects for the whole clause and for
  102481                 : ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
  102482                 : ** subclauses points to the WhereClause object for the whole clause.
  102483                 : */
  102484                 : struct WhereClause {
  102485                 :   Parse *pParse;           /* The parser context */
  102486                 :   WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
  102487                 :   Bitmask vmask;           /* Bitmask identifying virtual table cursors */
  102488                 :   WhereClause *pOuter;     /* Outer conjunction */
  102489                 :   u8 op;                   /* Split operator.  TK_AND or TK_OR */
  102490                 :   u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
  102491                 :   int nTerm;               /* Number of terms */
  102492                 :   int nSlot;               /* Number of entries in a[] */
  102493                 :   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
  102494                 : #if defined(SQLITE_SMALL_STACK)
  102495                 :   WhereTerm aStatic[1];    /* Initial static space for a[] */
  102496                 : #else
  102497                 :   WhereTerm aStatic[8];    /* Initial static space for a[] */
  102498                 : #endif
  102499                 : };
  102500                 : 
  102501                 : /*
  102502                 : ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
  102503                 : ** a dynamically allocated instance of the following structure.
  102504                 : */
  102505                 : struct WhereOrInfo {
  102506                 :   WhereClause wc;          /* Decomposition into subterms */
  102507                 :   Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
  102508                 : };
  102509                 : 
  102510                 : /*
  102511                 : ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
  102512                 : ** a dynamically allocated instance of the following structure.
  102513                 : */
  102514                 : struct WhereAndInfo {
  102515                 :   WhereClause wc;          /* The subexpression broken out */
  102516                 : };
  102517                 : 
  102518                 : /*
  102519                 : ** An instance of the following structure keeps track of a mapping
  102520                 : ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
  102521                 : **
  102522                 : ** The VDBE cursor numbers are small integers contained in 
  102523                 : ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE 
  102524                 : ** clause, the cursor numbers might not begin with 0 and they might
  102525                 : ** contain gaps in the numbering sequence.  But we want to make maximum
  102526                 : ** use of the bits in our bitmasks.  This structure provides a mapping
  102527                 : ** from the sparse cursor numbers into consecutive integers beginning
  102528                 : ** with 0.
  102529                 : **
  102530                 : ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
  102531                 : ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
  102532                 : **
  102533                 : ** For example, if the WHERE clause expression used these VDBE
  102534                 : ** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
  102535                 : ** would map those cursor numbers into bits 0 through 5.
  102536                 : **
  102537                 : ** Note that the mapping is not necessarily ordered.  In the example
  102538                 : ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
  102539                 : ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
  102540                 : ** does not really matter.  What is important is that sparse cursor
  102541                 : ** numbers all get mapped into bit numbers that begin with 0 and contain
  102542                 : ** no gaps.
  102543                 : */
  102544                 : struct WhereMaskSet {
  102545                 :   int n;                        /* Number of assigned cursor values */
  102546                 :   int ix[BMS];                  /* Cursor assigned to each bit */
  102547                 : };
  102548                 : 
  102549                 : /*
  102550                 : ** A WhereCost object records a lookup strategy and the estimated
  102551                 : ** cost of pursuing that strategy.
  102552                 : */
  102553                 : struct WhereCost {
  102554                 :   WherePlan plan;    /* The lookup strategy */
  102555                 :   double rCost;      /* Overall cost of pursuing this search strategy */
  102556                 :   Bitmask used;      /* Bitmask of cursors used by this plan */
  102557                 : };
  102558                 : 
  102559                 : /*
  102560                 : ** Bitmasks for the operators that indices are able to exploit.  An
  102561                 : ** OR-ed combination of these values can be used when searching for
  102562                 : ** terms in the where clause.
  102563                 : */
  102564                 : #define WO_IN     0x001
  102565                 : #define WO_EQ     0x002
  102566                 : #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
  102567                 : #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
  102568                 : #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
  102569                 : #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
  102570                 : #define WO_MATCH  0x040
  102571                 : #define WO_ISNULL 0x080
  102572                 : #define WO_OR     0x100       /* Two or more OR-connected terms */
  102573                 : #define WO_AND    0x200       /* Two or more AND-connected terms */
  102574                 : #define WO_NOOP   0x800       /* This term does not restrict search space */
  102575                 : 
  102576                 : #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
  102577                 : #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
  102578                 : 
  102579                 : /*
  102580                 : ** Value for wsFlags returned by bestIndex() and stored in
  102581                 : ** WhereLevel.wsFlags.  These flags determine which search
  102582                 : ** strategies are appropriate.
  102583                 : **
  102584                 : ** The least significant 12 bits is reserved as a mask for WO_ values above.
  102585                 : ** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
  102586                 : ** But if the table is the right table of a left join, WhereLevel.wsFlags
  102587                 : ** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
  102588                 : ** the "op" parameter to findTerm when we are resolving equality constraints.
  102589                 : ** ISNULL constraints will then not be used on the right table of a left
  102590                 : ** join.  Tickets #2177 and #2189.
  102591                 : */
  102592                 : #define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
  102593                 : #define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
  102594                 : #define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
  102595                 : #define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
  102596                 : #define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
  102597                 : #define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
  102598                 : #define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
  102599                 : #define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
  102600                 : #define WHERE_IN_ABLE      0x000f1000  /* Able to support an IN operator */
  102601                 : #define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
  102602                 : #define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
  102603                 : #define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
  102604                 : #define WHERE_IDX_ONLY     0x00800000  /* Use index only - omit table */
  102605                 : #define WHERE_ORDERBY      0x01000000  /* Output will appear in correct order */
  102606                 : #define WHERE_REVERSE      0x02000000  /* Scan in reverse order */
  102607                 : #define WHERE_UNIQUE       0x04000000  /* Selects no more than one row */
  102608                 : #define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
  102609                 : #define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
  102610                 : #define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
  102611                 : #define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
  102612                 : 
  102613                 : /*
  102614                 : ** Initialize a preallocated WhereClause structure.
  102615                 : */
  102616           82723 : static void whereClauseInit(
  102617                 :   WhereClause *pWC,        /* The WhereClause to be initialized */
  102618                 :   Parse *pParse,           /* The parsing context */
  102619                 :   WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
  102620                 :   u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
  102621                 : ){
  102622           82723 :   pWC->pParse = pParse;
  102623           82723 :   pWC->pMaskSet = pMaskSet;
  102624           82723 :   pWC->pOuter = 0;
  102625           82723 :   pWC->nTerm = 0;
  102626           82723 :   pWC->nSlot = ArraySize(pWC->aStatic);
  102627           82723 :   pWC->a = pWC->aStatic;
  102628           82723 :   pWC->vmask = 0;
  102629           82723 :   pWC->wctrlFlags = wctrlFlags;
  102630           82723 : }
  102631                 : 
  102632                 : /* Forward reference */
  102633                 : static void whereClauseClear(WhereClause*);
  102634                 : 
  102635                 : /*
  102636                 : ** Deallocate all memory associated with a WhereOrInfo object.
  102637                 : */
  102638             872 : static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
  102639             872 :   whereClauseClear(&p->wc);
  102640             872 :   sqlite3DbFree(db, p);
  102641             872 : }
  102642                 : 
  102643                 : /*
  102644                 : ** Deallocate all memory associated with a WhereAndInfo object.
  102645                 : */
  102646             453 : static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
  102647             453 :   whereClauseClear(&p->wc);
  102648             453 :   sqlite3DbFree(db, p);
  102649             453 : }
  102650                 : 
  102651                 : /*
  102652                 : ** Deallocate a WhereClause structure.  The WhereClause structure
  102653                 : ** itself is not freed.  This routine is the inverse of whereClauseInit().
  102654                 : */
  102655           82723 : static void whereClauseClear(WhereClause *pWC){
  102656                 :   int i;
  102657                 :   WhereTerm *a;
  102658           82723 :   sqlite3 *db = pWC->pParse->db;
  102659          188807 :   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
  102660          106084 :     if( a->wtFlags & TERM_DYNAMIC ){
  102661            8584 :       sqlite3ExprDelete(db, a->pExpr);
  102662                 :     }
  102663          106084 :     if( a->wtFlags & TERM_ORINFO ){
  102664             872 :       whereOrInfoDelete(db, a->u.pOrInfo);
  102665          105212 :     }else if( a->wtFlags & TERM_ANDINFO ){
  102666             453 :       whereAndInfoDelete(db, a->u.pAndInfo);
  102667                 :     }
  102668                 :   }
  102669           82723 :   if( pWC->a!=pWC->aStatic ){
  102670              25 :     sqlite3DbFree(db, pWC->a);
  102671                 :   }
  102672           82723 : }
  102673                 : 
  102674                 : /*
  102675                 : ** Add a single new WhereTerm entry to the WhereClause object pWC.
  102676                 : ** The new WhereTerm object is constructed from Expr p and with wtFlags.
  102677                 : ** The index in pWC->a[] of the new WhereTerm is returned on success.
  102678                 : ** 0 is returned if the new WhereTerm could not be added due to a memory
  102679                 : ** allocation error.  The memory allocation failure will be recorded in
  102680                 : ** the db->mallocFailed flag so that higher-level functions can detect it.
  102681                 : **
  102682                 : ** This routine will increase the size of the pWC->a[] array as necessary.
  102683                 : **
  102684                 : ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
  102685                 : ** for freeing the expression p is assumed by the WhereClause object pWC.
  102686                 : ** This is true even if this routine fails to allocate a new WhereTerm.
  102687                 : **
  102688                 : ** WARNING:  This routine might reallocate the space used to store
  102689                 : ** WhereTerms.  All pointers to WhereTerms should be invalidated after
  102690                 : ** calling this routine.  Such pointers may be reinitialized by referencing
  102691                 : ** the pWC->a[] array.
  102692                 : */
  102693          106084 : static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
  102694                 :   WhereTerm *pTerm;
  102695                 :   int idx;
  102696                 :   testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
  102697          106084 :   if( pWC->nTerm>=pWC->nSlot ){
  102698              25 :     WhereTerm *pOld = pWC->a;
  102699              25 :     sqlite3 *db = pWC->pParse->db;
  102700              25 :     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
  102701              25 :     if( pWC->a==0 ){
  102702               0 :       if( wtFlags & TERM_DYNAMIC ){
  102703               0 :         sqlite3ExprDelete(db, p);
  102704                 :       }
  102705               0 :       pWC->a = pOld;
  102706               0 :       return 0;
  102707                 :     }
  102708              25 :     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
  102709              25 :     if( pOld!=pWC->aStatic ){
  102710               0 :       sqlite3DbFree(db, pOld);
  102711                 :     }
  102712              25 :     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
  102713                 :   }
  102714          106084 :   pTerm = &pWC->a[idx = pWC->nTerm++];
  102715          106084 :   pTerm->pExpr = p;
  102716          106084 :   pTerm->wtFlags = wtFlags;
  102717          106084 :   pTerm->pWC = pWC;
  102718          106084 :   pTerm->iParent = -1;
  102719          106084 :   return idx;
  102720                 : }
  102721                 : 
  102722                 : /*
  102723                 : ** This routine identifies subexpressions in the WHERE clause where
  102724                 : ** each subexpression is separated by the AND operator or some other
  102725                 : ** operator specified in the op parameter.  The WhereClause structure
  102726                 : ** is filled with pointers to subexpressions.  For example:
  102727                 : **
  102728                 : **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
  102729                 : **           \________/     \_______________/     \________________/
  102730                 : **            slot[0]            slot[1]               slot[2]
  102731                 : **
  102732                 : ** The original WHERE clause in pExpr is unaltered.  All this routine
  102733                 : ** does is make slot[] entries point to substructure within pExpr.
  102734                 : **
  102735                 : ** In the previous sentence and in the diagram, "slot[]" refers to
  102736                 : ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
  102737                 : ** all terms of the WHERE clause.
  102738                 : */
  102739          139959 : static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
  102740          139959 :   pWC->op = (u8)op;
  102741          139959 :   if( pExpr==0 ) return;
  102742          126118 :   if( pExpr->op!=op ){
  102743           97500 :     whereClauseInsert(pWC, pExpr, 0);
  102744                 :   }else{
  102745           28618 :     whereSplit(pWC, pExpr->pLeft, op);
  102746           28618 :     whereSplit(pWC, pExpr->pRight, op);
  102747                 :   }
  102748                 : }
  102749                 : 
  102750                 : /*
  102751                 : ** Initialize an expression mask set (a WhereMaskSet object)
  102752                 : */
  102753                 : #define initMaskSet(P)  memset(P, 0, sizeof(*P))
  102754                 : 
  102755                 : /*
  102756                 : ** Return the bitmask for the given cursor number.  Return 0 if
  102757                 : ** iCursor is not in the set.
  102758                 : */
  102759         1023097 : static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
  102760                 :   int i;
  102761         1023097 :   assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
  102762         1150286 :   for(i=0; i<pMaskSet->n; i++){
  102763         1110335 :     if( pMaskSet->ix[i]==iCursor ){
  102764          983146 :       return ((Bitmask)1)<<i;
  102765                 :     }
  102766                 :   }
  102767           39951 :   return 0;
  102768                 : }
  102769                 : 
  102770                 : /*
  102771                 : ** Create a new mask for cursor iCursor.
  102772                 : **
  102773                 : ** There is one cursor per table in the FROM clause.  The number of
  102774                 : ** tables in the FROM clause is limited by a test early in the
  102775                 : ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
  102776                 : ** array will never overflow.
  102777                 : */
  102778           85154 : static void createMask(WhereMaskSet *pMaskSet, int iCursor){
  102779           85154 :   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
  102780           85154 :   pMaskSet->ix[pMaskSet->n++] = iCursor;
  102781           85154 : }
  102782                 : 
  102783                 : /*
  102784                 : ** This routine walks (recursively) an expression tree and generates
  102785                 : ** a bitmask indicating which tables are used in that expression
  102786                 : ** tree.
  102787                 : **
  102788                 : ** In order for this routine to work, the calling function must have
  102789                 : ** previously invoked sqlite3ResolveExprNames() on the expression.  See
  102790                 : ** the header comment on that routine for additional information.
  102791                 : ** The sqlite3ResolveExprNames() routines looks for column names and
  102792                 : ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
  102793                 : ** the VDBE cursor number of the table.  This routine just has to
  102794                 : ** translate the cursor numbers into bitmask values and OR all
  102795                 : ** the bitmasks together.
  102796                 : */
  102797                 : static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
  102798                 : static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
  102799         1114857 : static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
  102800         1114857 :   Bitmask mask = 0;
  102801         1114857 :   if( p==0 ) return 0;
  102802          618594 :   if( p->op==TK_COLUMN ){
  102803          254721 :     mask = getMask(pMaskSet, p->iTable);
  102804          254721 :     return mask;
  102805                 :   }
  102806          363873 :   mask = exprTableUsage(pMaskSet, p->pRight);
  102807          363873 :   mask |= exprTableUsage(pMaskSet, p->pLeft);
  102808          363873 :   if( ExprHasProperty(p, EP_xIsSelect) ){
  102809           10646 :     mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
  102810                 :   }else{
  102811          353227 :     mask |= exprListTableUsage(pMaskSet, p->x.pList);
  102812                 :   }
  102813          363873 :   return mask;
  102814                 : }
  102815          392573 : static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
  102816                 :   int i;
  102817          392573 :   Bitmask mask = 0;
  102818          392573 :   if( pList ){
  102819           93030 :     for(i=0; i<pList->nExpr; i++){
  102820           54323 :       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
  102821                 :     }
  102822                 :   }
  102823          392573 :   return mask;
  102824                 : }
  102825           28500 : static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
  102826           28500 :   Bitmask mask = 0;
  102827           69748 :   while( pS ){
  102828           12748 :     SrcList *pSrc = pS->pSrc;
  102829           12748 :     mask |= exprListTableUsage(pMaskSet, pS->pEList);
  102830           12748 :     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
  102831           12748 :     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
  102832           12748 :     mask |= exprTableUsage(pMaskSet, pS->pWhere);
  102833           12748 :     mask |= exprTableUsage(pMaskSet, pS->pHaving);
  102834           12748 :     if( ALWAYS(pSrc!=0) ){
  102835                 :       int i;
  102836           28500 :       for(i=0; i<pSrc->nSrc; i++){
  102837           15752 :         mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
  102838           15752 :         mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
  102839                 :       }
  102840                 :     }
  102841           12748 :     pS = pS->pPrior;
  102842                 :   }
  102843           28500 :   return mask;
  102844                 : }
  102845                 : 
  102846                 : /*
  102847                 : ** Return TRUE if the given operator is one of the operators that is
  102848                 : ** allowed for an indexable WHERE clause term.  The allowed operators are
  102849                 : ** "=", "<", ">", "<=", ">=", and "IN".
  102850                 : **
  102851                 : ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
  102852                 : ** of one of the following forms: column = expression column > expression
  102853                 : ** column >= expression column < expression column <= expression
  102854                 : ** expression = column expression > column expression >= column
  102855                 : ** expression < column expression <= column column IN
  102856                 : ** (expression-list) column IN (subquery) column IS NULL
  102857                 : */
  102858          207680 : static int allowedOp(int op){
  102859                 :   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
  102860                 :   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
  102861                 :   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
  102862                 :   assert( TK_GE==TK_EQ+4 );
  102863          207680 :   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
  102864                 : }
  102865                 : 
  102866                 : /*
  102867                 : ** Swap two objects of type TYPE.
  102868                 : */
  102869                 : #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
  102870                 : 
  102871                 : /*
  102872                 : ** Commute a comparison operator.  Expressions of the form "X op Y"
  102873                 : ** are converted into "Y op X".
  102874                 : **
  102875                 : ** If a collation sequence is associated with either the left or right
  102876                 : ** side of the comparison, it remains associated with the same side after
  102877                 : ** the commutation. So "Y collate NOCASE op X" becomes 
  102878                 : ** "X collate NOCASE op Y". This is because any collation sequence on
  102879                 : ** the left hand side of a comparison overrides any collation sequence 
  102880                 : ** attached to the right. For the same reason the EP_ExpCollate flag
  102881                 : ** is not commuted.
  102882                 : */
  102883            7706 : static void exprCommute(Parse *pParse, Expr *pExpr){
  102884            7706 :   u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
  102885            7706 :   u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
  102886            7706 :   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
  102887            7706 :   pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
  102888            7706 :   pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  102889            7706 :   SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
  102890            7706 :   pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
  102891            7706 :   pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
  102892            7706 :   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
  102893            7706 :   if( pExpr->op>=TK_GT ){
  102894                 :     assert( TK_LT==TK_GT+2 );
  102895                 :     assert( TK_GE==TK_LE+2 );
  102896                 :     assert( TK_GT>TK_EQ );
  102897                 :     assert( TK_GT<TK_LE );
  102898               6 :     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
  102899               6 :     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
  102900                 :   }
  102901            7706 : }
  102902                 : 
  102903                 : /*
  102904                 : ** Translate from TK_xx operator to WO_xx bitmask.
  102905                 : */
  102906           99779 : static u16 operatorMask(int op){
  102907                 :   u16 c;
  102908           99779 :   assert( allowedOp(op) );
  102909           99779 :   if( op==TK_IN ){
  102910            3204 :     c = WO_IN;
  102911           96575 :   }else if( op==TK_ISNULL ){
  102912            3479 :     c = WO_ISNULL;
  102913                 :   }else{
  102914           93096 :     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
  102915           93096 :     c = (u16)(WO_EQ<<(op-TK_EQ));
  102916                 :   }
  102917           99779 :   assert( op!=TK_ISNULL || c==WO_ISNULL );
  102918           99779 :   assert( op!=TK_IN || c==WO_IN );
  102919           99779 :   assert( op!=TK_EQ || c==WO_EQ );
  102920           99779 :   assert( op!=TK_LT || c==WO_LT );
  102921           99779 :   assert( op!=TK_LE || c==WO_LE );
  102922           99779 :   assert( op!=TK_GT || c==WO_GT );
  102923           99779 :   assert( op!=TK_GE || c==WO_GE );
  102924           99779 :   return c;
  102925                 : }
  102926                 : 
  102927                 : /*
  102928                 : ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
  102929                 : ** where X is a reference to the iColumn of table iCur and <op> is one of
  102930                 : ** the WO_xx operator codes specified by the op parameter.
  102931                 : ** Return a pointer to the term.  Return 0 if not found.
  102932                 : */
  102933          529948 : static WhereTerm *findTerm(
  102934                 :   WhereClause *pWC,     /* The WHERE clause to be searched */
  102935                 :   int iCur,             /* Cursor number of LHS */
  102936                 :   int iColumn,          /* Column number of LHS */
  102937                 :   Bitmask notReady,     /* RHS must not overlap with this mask */
  102938                 :   u32 op,               /* Mask of WO_xx values describing operator */
  102939                 :   Index *pIdx           /* Must be compatible with this index, if not NULL */
  102940                 : ){
  102941                 :   WhereTerm *pTerm;
  102942                 :   int k;
  102943          529948 :   assert( iCur>=0 );
  102944          529948 :   op &= WO_ALL;
  102945          949975 :   for(; pWC; pWC=pWC->pOuter){
  102946         1323765 :     for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
  102947          903738 :       if( pTerm->leftCursor==iCur
  102948          639714 :          && (pTerm->prereqRight & notReady)==0
  102949          607866 :          && pTerm->u.leftColumn==iColumn
  102950          118201 :          && (pTerm->eOperator & op)!=0
  102951                 :       ){
  102952          111033 :         if( iColumn>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
  102953           60004 :           Expr *pX = pTerm->pExpr;
  102954                 :           CollSeq *pColl;
  102955                 :           char idxaff;
  102956                 :           int j;
  102957           60004 :           Parse *pParse = pWC->pParse;
  102958                 :   
  102959           60004 :           idxaff = pIdx->pTable->aCol[iColumn].affinity;
  102960           60004 :           if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
  102961                 :   
  102962                 :           /* Figure out the collation sequence required from an index for
  102963                 :           ** it to be useful for optimising expression pX. Store this
  102964                 :           ** value in variable pColl.
  102965                 :           */
  102966           60004 :           assert(pX->pLeft);
  102967           60004 :           pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
  102968           60004 :           assert(pColl || pParse->nErr);
  102969                 :   
  102970           67610 :           for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
  102971            7606 :             if( NEVER(j>=pIdx->nColumn) ) return 0;
  102972                 :           }
  102973           60004 :           if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
  102974                 :         }
  102975          111033 :         return pTerm;
  102976                 :       }
  102977                 :     }
  102978                 :   }
  102979          418915 :   return 0;
  102980                 : }
  102981                 : 
  102982                 : /* Forward reference */
  102983                 : static void exprAnalyze(SrcList*, WhereClause*, int);
  102984                 : 
  102985                 : /*
  102986                 : ** Call exprAnalyze on all terms in a WHERE clause.  
  102987                 : **
  102988                 : **
  102989                 : */
  102990           82723 : static void exprAnalyzeAll(
  102991                 :   SrcList *pTabList,       /* the FROM clause */
  102992                 :   WhereClause *pWC         /* the WHERE clause to be analyzed */
  102993                 : ){
  102994                 :   int i;
  102995          180223 :   for(i=pWC->nTerm-1; i>=0; i--){
  102996           97500 :     exprAnalyze(pTabList, pWC, i);
  102997                 :   }
  102998           82723 : }
  102999                 : 
  103000                 : #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
  103001                 : /*
  103002                 : ** Check to see if the given expression is a LIKE or GLOB operator that
  103003                 : ** can be optimized using inequality constraints.  Return TRUE if it is
  103004                 : ** so and false if not.
  103005                 : **
  103006                 : ** In order for the operator to be optimizible, the RHS must be a string
  103007                 : ** literal that does not begin with a wildcard.  
  103008                 : */
  103009           97401 : static int isLikeOrGlob(
  103010                 :   Parse *pParse,    /* Parsing and code generating context */
  103011                 :   Expr *pExpr,      /* Test this expression */
  103012                 :   Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
  103013                 :   int *pisComplete, /* True if the only wildcard is % in the last character */
  103014                 :   int *pnoCase      /* True if uppercase is equivalent to lowercase */
  103015                 : ){
  103016           97401 :   const char *z = 0;         /* String on RHS of LIKE operator */
  103017                 :   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
  103018                 :   ExprList *pList;           /* List of operands to the LIKE operator */
  103019                 :   int c;                     /* One character in z[] */
  103020                 :   int cnt;                   /* Number of non-wildcard prefix characters */
  103021                 :   char wc[3];                /* Wildcard characters */
  103022           97401 :   sqlite3 *db = pParse->db;  /* Database connection */
  103023           97401 :   sqlite3_value *pVal = 0;
  103024                 :   int op;                    /* Opcode of pRight */
  103025                 : 
  103026           97401 :   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
  103027           97191 :     return 0;
  103028                 :   }
  103029                 : #ifdef SQLITE_EBCDIC
  103030                 :   if( *pnoCase ) return 0;
  103031                 : #endif
  103032             210 :   pList = pExpr->x.pList;
  103033             210 :   pLeft = pList->a[1].pExpr;
  103034             210 :   if( pLeft->op!=TK_COLUMN || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ){
  103035                 :     /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
  103036                 :     ** be the name of an indexed column with TEXT affinity. */
  103037              30 :     return 0;
  103038                 :   }
  103039             180 :   assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
  103040                 : 
  103041             180 :   pRight = pList->a[0].pExpr;
  103042             180 :   op = pRight->op;
  103043             180 :   if( op==TK_REGISTER ){
  103044               0 :     op = pRight->op2;
  103045                 :   }
  103046             180 :   if( op==TK_VARIABLE ){
  103047             168 :     Vdbe *pReprepare = pParse->pReprepare;
  103048             168 :     int iCol = pRight->iColumn;
  103049             168 :     pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
  103050             168 :     if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
  103051             152 :       z = (char *)sqlite3_value_text(pVal);
  103052                 :     }
  103053             168 :     sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
  103054             168 :     assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
  103055              12 :   }else if( op==TK_STRING ){
  103056              12 :     z = pRight->u.zToken;
  103057                 :   }
  103058             180 :   if( z ){
  103059             164 :     cnt = 0;
  103060            2370 :     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
  103061            2042 :       cnt++;
  103062                 :     }
  103063             328 :     if( cnt!=0 && 255!=(u8)z[cnt-1] ){
  103064                 :       Expr *pPrefix;
  103065             164 :       *pisComplete = c==wc[0] && z[cnt+1]==0;
  103066             164 :       pPrefix = sqlite3Expr(db, TK_STRING, z);
  103067             164 :       if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
  103068             164 :       *ppPrefix = pPrefix;
  103069             164 :       if( op==TK_VARIABLE ){
  103070             152 :         Vdbe *v = pParse->pVdbe;
  103071             152 :         sqlite3VdbeSetVarmask(v, pRight->iColumn);
  103072             152 :         if( *pisComplete && pRight->u.zToken[1] ){
  103073                 :           /* If the rhs of the LIKE expression is a variable, and the current
  103074                 :           ** value of the variable means there is no need to invoke the LIKE
  103075                 :           ** function, then no OP_Variable will be added to the program.
  103076                 :           ** This causes problems for the sqlite3_bind_parameter_name()
  103077                 :           ** API. To workaround them, add a dummy OP_Variable here.
  103078                 :           */ 
  103079             152 :           int r1 = sqlite3GetTempReg(pParse);
  103080             152 :           sqlite3ExprCodeTarget(pParse, pRight, r1);
  103081             152 :           sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
  103082             152 :           sqlite3ReleaseTempReg(pParse, r1);
  103083                 :         }
  103084                 :       }
  103085                 :     }else{
  103086               0 :       z = 0;
  103087                 :     }
  103088                 :   }
  103089                 : 
  103090             180 :   sqlite3ValueFree(pVal);
  103091             180 :   return (z!=0);
  103092                 : }
  103093                 : #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
  103094                 : 
  103095                 : 
  103096                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  103097                 : /*
  103098                 : ** Check to see if the given expression is of the form
  103099                 : **
  103100                 : **         column MATCH expr
  103101                 : **
  103102                 : ** If it is then return TRUE.  If not, return FALSE.
  103103                 : */
  103104           99365 : static int isMatchOfColumn(
  103105                 :   Expr *pExpr      /* Test this expression */
  103106                 : ){
  103107                 :   ExprList *pList;
  103108                 : 
  103109           99365 :   if( pExpr->op!=TK_FUNCTION ){
  103110           98941 :     return 0;
  103111                 :   }
  103112             424 :   if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
  103113             423 :     return 0;
  103114                 :   }
  103115               1 :   pList = pExpr->x.pList;
  103116               1 :   if( pList->nExpr!=2 ){
  103117               0 :     return 0;
  103118                 :   }
  103119               1 :   if( pList->a[1].pExpr->op != TK_COLUMN ){
  103120               0 :     return 0;
  103121                 :   }
  103122               1 :   return 1;
  103123                 : }
  103124                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
  103125                 : 
  103126                 : /*
  103127                 : ** If the pBase expression originated in the ON or USING clause of
  103128                 : ** a join, then transfer the appropriate markings over to derived.
  103129                 : */
  103130             579 : static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
  103131             579 :   pDerived->flags |= pBase->flags & EP_FromJoin;
  103132             579 :   pDerived->iRightJoinTable = pBase->iRightJoinTable;
  103133             579 : }
  103134                 : 
  103135                 : #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
  103136                 : /*
  103137                 : ** Analyze a term that consists of two or more OR-connected
  103138                 : ** subterms.  So in:
  103139                 : **
  103140                 : **     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
  103141                 : **                          ^^^^^^^^^^^^^^^^^^^^
  103142                 : **
  103143                 : ** This routine analyzes terms such as the middle term in the above example.
  103144                 : ** A WhereOrTerm object is computed and attached to the term under
  103145                 : ** analysis, regardless of the outcome of the analysis.  Hence:
  103146                 : **
  103147                 : **     WhereTerm.wtFlags   |=  TERM_ORINFO
  103148                 : **     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
  103149                 : **
  103150                 : ** The term being analyzed must have two or more of OR-connected subterms.
  103151                 : ** A single subterm might be a set of AND-connected sub-subterms.
  103152                 : ** Examples of terms under analysis:
  103153                 : **
  103154                 : **     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
  103155                 : **     (B)     x=expr1 OR expr2=x OR x=expr3
  103156                 : **     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
  103157                 : **     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
  103158                 : **     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
  103159                 : **
  103160                 : ** CASE 1:
  103161                 : **
  103162                 : ** If all subterms are of the form T.C=expr for some single column of C
  103163                 : ** a single table T (as shown in example B above) then create a new virtual
  103164                 : ** term that is an equivalent IN expression.  In other words, if the term
  103165                 : ** being analyzed is:
  103166                 : **
  103167                 : **      x = expr1  OR  expr2 = x  OR  x = expr3
  103168                 : **
  103169                 : ** then create a new virtual term like this:
  103170                 : **
  103171                 : **      x IN (expr1,expr2,expr3)
  103172                 : **
  103173                 : ** CASE 2:
  103174                 : **
  103175                 : ** If all subterms are indexable by a single table T, then set
  103176                 : **
  103177                 : **     WhereTerm.eOperator              =  WO_OR
  103178                 : **     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
  103179                 : **
  103180                 : ** A subterm is "indexable" if it is of the form
  103181                 : ** "T.C <op> <expr>" where C is any column of table T and 
  103182                 : ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
  103183                 : ** A subterm is also indexable if it is an AND of two or more
  103184                 : ** subsubterms at least one of which is indexable.  Indexable AND 
  103185                 : ** subterms have their eOperator set to WO_AND and they have
  103186                 : ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
  103187                 : **
  103188                 : ** From another point of view, "indexable" means that the subterm could
  103189                 : ** potentially be used with an index if an appropriate index exists.
  103190                 : ** This analysis does not consider whether or not the index exists; that
  103191                 : ** is something the bestIndex() routine will determine.  This analysis
  103192                 : ** only looks at whether subterms appropriate for indexing exist.
  103193                 : **
  103194                 : ** All examples A through E above all satisfy case 2.  But if a term
  103195                 : ** also statisfies case 1 (such as B) we know that the optimizer will
  103196                 : ** always prefer case 1, so in that case we pretend that case 2 is not
  103197                 : ** satisfied.
  103198                 : **
  103199                 : ** It might be the case that multiple tables are indexable.  For example,
  103200                 : ** (E) above is indexable on tables P, Q, and R.
  103201                 : **
  103202                 : ** Terms that satisfy case 2 are candidates for lookup by using
  103203                 : ** separate indices to find rowids for each subterm and composing
  103204                 : ** the union of all rowids using a RowSet object.  This is similar
  103205                 : ** to "bitmap indices" in other database engines.
  103206                 : **
  103207                 : ** OTHERWISE:
  103208                 : **
  103209                 : ** If neither case 1 nor case 2 apply, then leave the eOperator set to
  103210                 : ** zero.  This term is not useful for search.
  103211                 : */
  103212             872 : static void exprAnalyzeOrTerm(
  103213                 :   SrcList *pSrc,            /* the FROM clause */
  103214                 :   WhereClause *pWC,         /* the complete WHERE clause */
  103215                 :   int idxTerm               /* Index of the OR-term to be analyzed */
  103216                 : ){
  103217             872 :   Parse *pParse = pWC->pParse;            /* Parser context */
  103218             872 :   sqlite3 *db = pParse->db;               /* Database connection */
  103219             872 :   WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
  103220             872 :   Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
  103221             872 :   WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
  103222                 :   int i;                                  /* Loop counters */
  103223                 :   WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
  103224                 :   WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
  103225                 :   WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
  103226                 :   Bitmask chngToIN;         /* Tables that might satisfy case 1 */
  103227                 :   Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
  103228                 : 
  103229                 :   /*
  103230                 :   ** Break the OR clause into its separate subterms.  The subterms are
  103231                 :   ** stored in a WhereClause structure containing within the WhereOrInfo
  103232                 :   ** object that is attached to the original OR clause term.
  103233                 :   */
  103234             872 :   assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
  103235             872 :   assert( pExpr->op==TK_OR );
  103236             872 :   pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
  103237             872 :   if( pOrInfo==0 ) return;
  103238             872 :   pTerm->wtFlags |= TERM_ORINFO;
  103239             872 :   pOrWc = &pOrInfo->wc;
  103240             872 :   whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
  103241             872 :   whereSplit(pOrWc, pExpr, TK_OR);
  103242             872 :   exprAnalyzeAll(pSrc, pOrWc);
  103243             872 :   if( db->mallocFailed ) return;
  103244             872 :   assert( pOrWc->nTerm>=2 );
  103245                 : 
  103246                 :   /*
  103247                 :   ** Compute the set of tables that might satisfy cases 1 or 2.
  103248                 :   */
  103249             872 :   indexable = ~(Bitmask)0;
  103250             872 :   chngToIN = ~(pWC->vmask);
  103251            2808 :   for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
  103252            1936 :     if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
  103253                 :       WhereAndInfo *pAndInfo;
  103254             453 :       assert( pOrTerm->eOperator==0 );
  103255             453 :       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
  103256             453 :       chngToIN = 0;
  103257             453 :       pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
  103258             453 :       if( pAndInfo ){
  103259                 :         WhereClause *pAndWC;
  103260                 :         WhereTerm *pAndTerm;
  103261                 :         int j;
  103262             453 :         Bitmask b = 0;
  103263             453 :         pOrTerm->u.pAndInfo = pAndInfo;
  103264             453 :         pOrTerm->wtFlags |= TERM_ANDINFO;
  103265             453 :         pOrTerm->eOperator = WO_AND;
  103266             453 :         pAndWC = &pAndInfo->wc;
  103267             453 :         whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
  103268             453 :         whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
  103269             453 :         exprAnalyzeAll(pSrc, pAndWC);
  103270             453 :         pAndWC->pOuter = pWC;
  103271                 :         testcase( db->mallocFailed );
  103272             453 :         if( !db->mallocFailed ){
  103273            1283 :           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
  103274             830 :             assert( pAndTerm->pExpr );
  103275             830 :             if( allowedOp(pAndTerm->pExpr->op) ){
  103276             728 :               b |= getMask(pMaskSet, pAndTerm->leftCursor);
  103277                 :             }
  103278                 :           }
  103279                 :         }
  103280             453 :         indexable &= b;
  103281                 :       }
  103282            1483 :     }else if( pOrTerm->wtFlags & TERM_COPIED ){
  103283                 :       /* Skip this term for now.  We revisit it when we process the
  103284                 :       ** corresponding TERM_VIRTUAL term */
  103285                 :     }else{
  103286                 :       Bitmask b;
  103287            1483 :       b = getMask(pMaskSet, pOrTerm->leftCursor);
  103288            1483 :       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
  103289               0 :         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
  103290               0 :         b |= getMask(pMaskSet, pOther->leftCursor);
  103291                 :       }
  103292            1483 :       indexable &= b;
  103293            1483 :       if( pOrTerm->eOperator!=WO_EQ ){
  103294             145 :         chngToIN = 0;
  103295                 :       }else{
  103296            1338 :         chngToIN &= b;
  103297                 :       }
  103298                 :     }
  103299                 :   }
  103300                 : 
  103301                 :   /*
  103302                 :   ** Record the set of tables that satisfy case 2.  The set might be
  103303                 :   ** empty.
  103304                 :   */
  103305             872 :   pOrInfo->indexable = indexable;
  103306             872 :   pTerm->eOperator = indexable==0 ? 0 : WO_OR;
  103307                 : 
  103308                 :   /*
  103309                 :   ** chngToIN holds a set of tables that *might* satisfy case 1.  But
  103310                 :   ** we have to do some additional checking to see if case 1 really
  103311                 :   ** is satisfied.
  103312                 :   **
  103313                 :   ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
  103314                 :   ** that there is no possibility of transforming the OR clause into an
  103315                 :   ** IN operator because one or more terms in the OR clause contain
  103316                 :   ** something other than == on a column in the single table.  The 1-bit
  103317                 :   ** case means that every term of the OR clause is of the form
  103318                 :   ** "table.column=expr" for some single table.  The one bit that is set
  103319                 :   ** will correspond to the common table.  We still need to check to make
  103320                 :   ** sure the same column is used on all terms.  The 2-bit case is when
  103321                 :   ** the all terms are of the form "table1.column=table2.column".  It
  103322                 :   ** might be possible to form an IN operator with either table1.column
  103323                 :   ** or table2.column as the LHS if either is common to every term of
  103324                 :   ** the OR clause.
  103325                 :   **
  103326                 :   ** Note that terms of the form "table.column1=table.column2" (the
  103327                 :   ** same table on both sizes of the ==) cannot be optimized.
  103328                 :   */
  103329             872 :   if( chngToIN ){
  103330             582 :     int okToChngToIN = 0;     /* True if the conversion to IN is valid */
  103331             582 :     int iColumn = -1;         /* Column index on lhs of IN operator */
  103332             582 :     int iCursor = -1;         /* Table cursor common to all terms */
  103333             582 :     int j = 0;                /* Loop counter */
  103334                 : 
  103335                 :     /* Search for a table and column that appears on one side or the
  103336                 :     ** other of the == operator in every subterm.  That table and column
  103337                 :     ** will be recorded in iCursor and iColumn.  There might not be any
  103338                 :     ** such table and column.  Set okToChngToIN if an appropriate table
  103339                 :     ** and column is found but leave okToChngToIN false if not found.
  103340                 :     */
  103341            1164 :     for(j=0; j<2 && !okToChngToIN; j++){
  103342             585 :       pOrTerm = pOrWc->a;
  103343             591 :       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
  103344             588 :         assert( pOrTerm->eOperator==WO_EQ );
  103345             588 :         pOrTerm->wtFlags &= ~TERM_OR_OK;
  103346             588 :         if( pOrTerm->leftCursor==iCursor ){
  103347                 :           /* This is the 2-bit case and we are on the second iteration and
  103348                 :           ** current term is from the first iteration.  So skip this term. */
  103349               6 :           assert( j==1 );
  103350               6 :           continue;
  103351                 :         }
  103352             582 :         if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
  103353                 :           /* This term must be of the form t1.a==t2.b where t2 is in the
  103354                 :           ** chngToIN set but t1 is not.  This term will be either preceeded
  103355                 :           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
  103356                 :           ** and use its inversion. */
  103357                 :           testcase( pOrTerm->wtFlags & TERM_COPIED );
  103358                 :           testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
  103359               0 :           assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
  103360               0 :           continue;
  103361                 :         }
  103362             582 :         iColumn = pOrTerm->u.leftColumn;
  103363             582 :         iCursor = pOrTerm->leftCursor;
  103364             582 :         break;
  103365                 :       }
  103366             585 :       if( i<0 ){
  103367                 :         /* No candidate table+column was found.  This can only occur
  103368                 :         ** on the second iteration */
  103369               3 :         assert( j==1 );
  103370               3 :         assert( (chngToIN&(chngToIN-1))==0 );
  103371               3 :         assert( chngToIN==getMask(pMaskSet, iCursor) );
  103372               3 :         break;
  103373                 :       }
  103374                 :       testcase( j==1 );
  103375                 : 
  103376                 :       /* We have found a candidate table and column.  Check to see if that
  103377                 :       ** table and column is common to every term in the OR clause */
  103378             582 :       okToChngToIN = 1;
  103379            1854 :       for(; i>=0 && okToChngToIN; i--, pOrTerm++){
  103380            1272 :         assert( pOrTerm->eOperator==WO_EQ );
  103381            1272 :         if( pOrTerm->leftCursor!=iCursor ){
  103382               0 :           pOrTerm->wtFlags &= ~TERM_OR_OK;
  103383            1272 :         }else if( pOrTerm->u.leftColumn!=iColumn ){
  103384               3 :           okToChngToIN = 0;
  103385                 :         }else{
  103386                 :           int affLeft, affRight;
  103387                 :           /* If the right-hand side is also a column, then the affinities
  103388                 :           ** of both right and left sides must be such that no type
  103389                 :           ** conversions are required on the right.  (Ticket #2249)
  103390                 :           */
  103391            1269 :           affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
  103392            1269 :           affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
  103393            1269 :           if( affRight!=0 && affRight!=affLeft ){
  103394               0 :             okToChngToIN = 0;
  103395                 :           }else{
  103396            1269 :             pOrTerm->wtFlags |= TERM_OR_OK;
  103397                 :           }
  103398                 :         }
  103399                 :       }
  103400                 :     }
  103401                 : 
  103402                 :     /* At this point, okToChngToIN is true if original pTerm satisfies
  103403                 :     ** case 1.  In that case, construct a new virtual term that is 
  103404                 :     ** pTerm converted into an IN operator.
  103405                 :     **
  103406                 :     ** EV: R-00211-15100
  103407                 :     */
  103408             582 :     if( okToChngToIN ){
  103409                 :       Expr *pDup;            /* A transient duplicate expression */
  103410             579 :       ExprList *pList = 0;   /* The RHS of the IN operator */
  103411             579 :       Expr *pLeft = 0;       /* The LHS of the IN operator */
  103412                 :       Expr *pNew;            /* The complete IN operator */
  103413                 : 
  103414            1845 :       for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
  103415            1266 :         if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
  103416            1266 :         assert( pOrTerm->eOperator==WO_EQ );
  103417            1266 :         assert( pOrTerm->leftCursor==iCursor );
  103418            1266 :         assert( pOrTerm->u.leftColumn==iColumn );
  103419            1266 :         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
  103420            1266 :         pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
  103421            1266 :         pLeft = pOrTerm->pExpr->pLeft;
  103422                 :       }
  103423             579 :       assert( pLeft!=0 );
  103424             579 :       pDup = sqlite3ExprDup(db, pLeft, 0);
  103425             579 :       pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
  103426             579 :       if( pNew ){
  103427                 :         int idxNew;
  103428             579 :         transferJoinMarkings(pNew, pExpr);
  103429             579 :         assert( !ExprHasProperty(pNew, EP_xIsSelect) );
  103430             579 :         pNew->x.pList = pList;
  103431             579 :         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
  103432                 :         testcase( idxNew==0 );
  103433             579 :         exprAnalyze(pSrc, pWC, idxNew);
  103434             579 :         pTerm = &pWC->a[idxTerm];
  103435             579 :         pWC->a[idxNew].iParent = idxTerm;
  103436             579 :         pTerm->nChild = 1;
  103437                 :       }else{
  103438               0 :         sqlite3ExprListDelete(db, pList);
  103439                 :       }
  103440             579 :       pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
  103441                 :     }
  103442                 :   }
  103443                 : }
  103444                 : #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
  103445                 : 
  103446                 : 
  103447                 : /*
  103448                 : ** The input to this routine is an WhereTerm structure with only the
  103449                 : ** "pExpr" field filled in.  The job of this routine is to analyze the
  103450                 : ** subexpression and populate all the other fields of the WhereTerm
  103451                 : ** structure.
  103452                 : **
  103453                 : ** If the expression is of the form "<expr> <op> X" it gets commuted
  103454                 : ** to the standard form of "X <op> <expr>".
  103455                 : **
  103456                 : ** If the expression is of the form "X <op> Y" where both X and Y are
  103457                 : ** columns, then the original expression is unchanged and a new virtual
  103458                 : ** term of the form "Y <op> X" is added to the WHERE clause and
  103459                 : ** analyzed separately.  The original term is marked with TERM_COPIED
  103460                 : ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
  103461                 : ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
  103462                 : ** is a commuted copy of a prior term.)  The original term has nChild=1
  103463                 : ** and the copy has idxParent set to the index of the original term.
  103464                 : */
  103465           99365 : static void exprAnalyze(
  103466                 :   SrcList *pSrc,            /* the FROM clause */
  103467                 :   WhereClause *pWC,         /* the WHERE clause */
  103468                 :   int idxTerm               /* Index of the term to be analyzed */
  103469                 : ){
  103470                 :   WhereTerm *pTerm;                /* The term to be analyzed */
  103471                 :   WhereMaskSet *pMaskSet;          /* Set of table index masks */
  103472                 :   Expr *pExpr;                     /* The expression to be analyzed */
  103473                 :   Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
  103474                 :   Bitmask prereqAll;               /* Prerequesites of pExpr */
  103475           99365 :   Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
  103476           99365 :   Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
  103477           99365 :   int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
  103478           99365 :   int noCase = 0;                  /* LIKE/GLOB distinguishes case */
  103479                 :   int op;                          /* Top-level operator.  pExpr->op */
  103480           99365 :   Parse *pParse = pWC->pParse;     /* Parsing context */
  103481           99365 :   sqlite3 *db = pParse->db;        /* Database connection */
  103482                 : 
  103483           99365 :   if( db->mallocFailed ){
  103484               0 :     return;
  103485                 :   }
  103486           99365 :   pTerm = &pWC->a[idxTerm];
  103487           99365 :   pMaskSet = pWC->pMaskSet;
  103488           99365 :   pExpr = pTerm->pExpr;
  103489           99365 :   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
  103490           99365 :   op = pExpr->op;
  103491           99365 :   if( op==TK_IN ){
  103492            3204 :     assert( pExpr->pRight==0 );
  103493            3204 :     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  103494            2102 :       pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
  103495                 :     }else{
  103496            1102 :       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
  103497                 :     }
  103498           96161 :   }else if( op==TK_ISNULL ){
  103499            3479 :     pTerm->prereqRight = 0;
  103500                 :   }else{
  103501           92682 :     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
  103502                 :   }
  103503           99365 :   prereqAll = exprTableUsage(pMaskSet, pExpr);
  103504           99365 :   if( ExprHasProperty(pExpr, EP_FromJoin) ){
  103505            3121 :     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
  103506            3121 :     prereqAll |= x;
  103507            3121 :     extraRight = x-1;  /* ON clause terms may not be used with an index
  103508                 :                        ** on left table of a LEFT JOIN.  Ticket #3015 */
  103509                 :   }
  103510           99365 :   pTerm->prereqAll = prereqAll;
  103511           99365 :   pTerm->leftCursor = -1;
  103512           99365 :   pTerm->iParent = -1;
  103513           99365 :   pTerm->eOperator = 0;
  103514          192829 :   if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
  103515           93464 :     Expr *pLeft = pExpr->pLeft;
  103516           93464 :     Expr *pRight = pExpr->pRight;
  103517           93464 :     if( pLeft->op==TK_COLUMN ){
  103518           92073 :       pTerm->leftCursor = pLeft->iTable;
  103519           92073 :       pTerm->u.leftColumn = pLeft->iColumn;
  103520           92073 :       pTerm->eOperator = operatorMask(op);
  103521                 :     }
  103522           93464 :     if( pRight && pRight->op==TK_COLUMN ){
  103523                 :       WhereTerm *pNew;
  103524                 :       Expr *pDup;
  103525            7706 :       if( pTerm->leftCursor>=0 ){
  103526                 :         int idxNew;
  103527            6718 :         pDup = sqlite3ExprDup(db, pExpr, 0);
  103528            6718 :         if( db->mallocFailed ){
  103529               0 :           sqlite3ExprDelete(db, pDup);
  103530               0 :           return;
  103531                 :         }
  103532            6718 :         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
  103533            6718 :         if( idxNew==0 ) return;
  103534            6718 :         pNew = &pWC->a[idxNew];
  103535            6718 :         pNew->iParent = idxTerm;
  103536            6718 :         pTerm = &pWC->a[idxTerm];
  103537            6718 :         pTerm->nChild = 1;
  103538            6718 :         pTerm->wtFlags |= TERM_COPIED;
  103539                 :       }else{
  103540             988 :         pDup = pExpr;
  103541             988 :         pNew = pTerm;
  103542                 :       }
  103543            7706 :       exprCommute(pParse, pDup);
  103544            7706 :       pLeft = pDup->pLeft;
  103545            7706 :       pNew->leftCursor = pLeft->iTable;
  103546            7706 :       pNew->u.leftColumn = pLeft->iColumn;
  103547                 :       testcase( (prereqLeft | extraRight) != prereqLeft );
  103548            7706 :       pNew->prereqRight = prereqLeft | extraRight;
  103549            7706 :       pNew->prereqAll = prereqAll;
  103550            7706 :       pNew->eOperator = operatorMask(pDup->op);
  103551                 :     }
  103552                 :   }
  103553                 : 
  103554                 : #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
  103555                 :   /* If a term is the BETWEEN operator, create two new virtual terms
  103556                 :   ** that define the range that the BETWEEN implements.  For example:
  103557                 :   **
  103558                 :   **      a BETWEEN b AND c
  103559                 :   **
  103560                 :   ** is converted into:
  103561                 :   **
  103562                 :   **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
  103563                 :   **
  103564                 :   ** The two new terms are added onto the end of the WhereClause object.
  103565                 :   ** The new terms are "dynamic" and are children of the original BETWEEN
  103566                 :   ** term.  That means that if the BETWEEN term is coded, the children are
  103567                 :   ** skipped.  Or, if the children are satisfied by an index, the original
  103568                 :   ** BETWEEN term is skipped.
  103569                 :   */
  103570            6380 :   else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
  103571             479 :     ExprList *pList = pExpr->x.pList;
  103572                 :     int i;
  103573                 :     static const u8 ops[] = {TK_GE, TK_LE};
  103574             479 :     assert( pList!=0 );
  103575             479 :     assert( pList->nExpr==2 );
  103576            1437 :     for(i=0; i<2; i++){
  103577                 :       Expr *pNewExpr;
  103578                 :       int idxNew;
  103579             958 :       pNewExpr = sqlite3PExpr(pParse, ops[i], 
  103580                 :                              sqlite3ExprDup(db, pExpr->pLeft, 0),
  103581             958 :                              sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
  103582             958 :       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  103583                 :       testcase( idxNew==0 );
  103584             958 :       exprAnalyze(pSrc, pWC, idxNew);
  103585             958 :       pTerm = &pWC->a[idxTerm];
  103586             958 :       pWC->a[idxNew].iParent = idxTerm;
  103587                 :     }
  103588             479 :     pTerm->nChild = 2;
  103589                 :   }
  103590                 : #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
  103591                 : 
  103592                 : #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
  103593                 :   /* Analyze a term that is composed of two or more subterms connected by
  103594                 :   ** an OR operator.
  103595                 :   */
  103596            5422 :   else if( pExpr->op==TK_OR ){
  103597             872 :     assert( pWC->op==TK_AND );
  103598             872 :     exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
  103599             872 :     pTerm = &pWC->a[idxTerm];
  103600                 :   }
  103601                 : #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  103602                 : 
  103603                 : #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
  103604                 :   /* Add constraints to reduce the search space on a LIKE or GLOB
  103605                 :   ** operator.
  103606                 :   **
  103607                 :   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
  103608                 :   **
  103609                 :   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
  103610                 :   **
  103611                 :   ** The last character of the prefix "abc" is incremented to form the
  103612                 :   ** termination condition "abd".
  103613                 :   */
  103614           99365 :   if( pWC->op==TK_AND 
  103615           97401 :    && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
  103616                 :   ){
  103617                 :     Expr *pLeft;       /* LHS of LIKE/GLOB operator */
  103618                 :     Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
  103619                 :     Expr *pNewExpr1;
  103620                 :     Expr *pNewExpr2;
  103621                 :     int idxNew1;
  103622                 :     int idxNew2;
  103623                 :     CollSeq *pColl;    /* Collating sequence to use */
  103624                 : 
  103625             164 :     pLeft = pExpr->x.pList->a[1].pExpr;
  103626             164 :     pStr2 = sqlite3ExprDup(db, pStr1, 0);
  103627             164 :     if( !db->mallocFailed ){
  103628                 :       u8 c, *pC;       /* Last character before the first wildcard */
  103629             164 :       pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
  103630             164 :       c = *pC;
  103631             164 :       if( noCase ){
  103632                 :         /* The point is to increment the last character before the first
  103633                 :         ** wildcard.  But if we increment '@', that will push it into the
  103634                 :         ** alphabetic range where case conversions will mess up the 
  103635                 :         ** inequality.  To avoid this, make sure to also run the full
  103636                 :         ** LIKE on all candidate expressions by clearing the isComplete flag
  103637                 :         */
  103638              12 :         if( c=='A'-1 ) isComplete = 0;   /* EV: R-64339-08207 */
  103639                 : 
  103640                 : 
  103641              12 :         c = sqlite3UpperToLower[c];
  103642                 :       }
  103643             164 :       *pC = c + 1;
  103644                 :     }
  103645             164 :     pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0);
  103646             164 :     pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 
  103647                 :                      sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
  103648                 :                      pStr1, 0);
  103649             164 :     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
  103650                 :     testcase( idxNew1==0 );
  103651             164 :     exprAnalyze(pSrc, pWC, idxNew1);
  103652             164 :     pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
  103653                 :                      sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl),
  103654                 :                      pStr2, 0);
  103655             164 :     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
  103656                 :     testcase( idxNew2==0 );
  103657             164 :     exprAnalyze(pSrc, pWC, idxNew2);
  103658             164 :     pTerm = &pWC->a[idxTerm];
  103659             164 :     if( isComplete ){
  103660             164 :       pWC->a[idxNew1].iParent = idxTerm;
  103661             164 :       pWC->a[idxNew2].iParent = idxTerm;
  103662             164 :       pTerm->nChild = 2;
  103663                 :     }
  103664                 :   }
  103665                 : #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
  103666                 : 
  103667                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  103668                 :   /* Add a WO_MATCH auxiliary term to the constraint set if the
  103669                 :   ** current expression is of the form:  column MATCH expr.
  103670                 :   ** This information is used by the xBestIndex methods of
  103671                 :   ** virtual tables.  The native query optimizer does not attempt
  103672                 :   ** to do anything with MATCH functions.
  103673                 :   */
  103674           99365 :   if( isMatchOfColumn(pExpr) ){
  103675                 :     int idxNew;
  103676                 :     Expr *pRight, *pLeft;
  103677                 :     WhereTerm *pNewTerm;
  103678                 :     Bitmask prereqColumn, prereqExpr;
  103679                 : 
  103680               1 :     pRight = pExpr->x.pList->a[0].pExpr;
  103681               1 :     pLeft = pExpr->x.pList->a[1].pExpr;
  103682               1 :     prereqExpr = exprTableUsage(pMaskSet, pRight);
  103683               1 :     prereqColumn = exprTableUsage(pMaskSet, pLeft);
  103684               1 :     if( (prereqExpr & prereqColumn)==0 ){
  103685                 :       Expr *pNewExpr;
  103686               1 :       pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
  103687                 :                               0, sqlite3ExprDup(db, pRight, 0), 0);
  103688               1 :       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  103689                 :       testcase( idxNew==0 );
  103690               1 :       pNewTerm = &pWC->a[idxNew];
  103691               1 :       pNewTerm->prereqRight = prereqExpr;
  103692               1 :       pNewTerm->leftCursor = pLeft->iTable;
  103693               1 :       pNewTerm->u.leftColumn = pLeft->iColumn;
  103694               1 :       pNewTerm->eOperator = WO_MATCH;
  103695               1 :       pNewTerm->iParent = idxTerm;
  103696               1 :       pTerm = &pWC->a[idxTerm];
  103697               1 :       pTerm->nChild = 1;
  103698               1 :       pTerm->wtFlags |= TERM_COPIED;
  103699               1 :       pNewTerm->prereqAll = pTerm->prereqAll;
  103700                 :     }
  103701                 :   }
  103702                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
  103703                 : 
  103704                 : #ifdef SQLITE_ENABLE_STAT3
  103705                 :   /* When sqlite_stat3 histogram data is available an operator of the
  103706                 :   ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
  103707                 :   ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
  103708                 :   ** virtual term of that form.
  103709                 :   **
  103710                 :   ** Note that the virtual term must be tagged with TERM_VNULL.  This
  103711                 :   ** TERM_VNULL tag will suppress the not-null check at the beginning
  103712                 :   ** of the loop.  Without the TERM_VNULL flag, the not-null check at
  103713                 :   ** the start of the loop will prevent any results from being returned.
  103714                 :   */
  103715                 :   if( pExpr->op==TK_NOTNULL
  103716                 :    && pExpr->pLeft->op==TK_COLUMN
  103717                 :    && pExpr->pLeft->iColumn>=0
  103718                 :   ){
  103719                 :     Expr *pNewExpr;
  103720                 :     Expr *pLeft = pExpr->pLeft;
  103721                 :     int idxNew;
  103722                 :     WhereTerm *pNewTerm;
  103723                 : 
  103724                 :     pNewExpr = sqlite3PExpr(pParse, TK_GT,
  103725                 :                             sqlite3ExprDup(db, pLeft, 0),
  103726                 :                             sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
  103727                 : 
  103728                 :     idxNew = whereClauseInsert(pWC, pNewExpr,
  103729                 :                               TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
  103730                 :     if( idxNew ){
  103731                 :       pNewTerm = &pWC->a[idxNew];
  103732                 :       pNewTerm->prereqRight = 0;
  103733                 :       pNewTerm->leftCursor = pLeft->iTable;
  103734                 :       pNewTerm->u.leftColumn = pLeft->iColumn;
  103735                 :       pNewTerm->eOperator = WO_GT;
  103736                 :       pNewTerm->iParent = idxTerm;
  103737                 :       pTerm = &pWC->a[idxTerm];
  103738                 :       pTerm->nChild = 1;
  103739                 :       pTerm->wtFlags |= TERM_COPIED;
  103740                 :       pNewTerm->prereqAll = pTerm->prereqAll;
  103741                 :     }
  103742                 :   }
  103743                 : #endif /* SQLITE_ENABLE_STAT */
  103744                 : 
  103745                 :   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  103746                 :   ** an index for tables to the left of the join.
  103747                 :   */
  103748           99365 :   pTerm->prereqRight |= extraRight;
  103749                 : }
  103750                 : 
  103751                 : /*
  103752                 : ** Return TRUE if any of the expressions in pList->a[iFirst...] contain
  103753                 : ** a reference to any table other than the iBase table.
  103754                 : */
  103755           20162 : static int referencesOtherTables(
  103756                 :   ExprList *pList,          /* Search expressions in ths list */
  103757                 :   WhereMaskSet *pMaskSet,   /* Mapping from tables to bitmaps */
  103758                 :   int iFirst,               /* Be searching with the iFirst-th expression */
  103759                 :   int iBase                 /* Ignore references to this table */
  103760                 : ){
  103761           20162 :   Bitmask allowed = ~getMask(pMaskSet, iBase);
  103762           40435 :   while( iFirst<pList->nExpr ){
  103763             126 :     if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
  103764              15 :       return 1;
  103765                 :     }
  103766                 :   }
  103767           20147 :   return 0;
  103768                 : }
  103769                 : 
  103770                 : /*
  103771                 : ** This function searches the expression list passed as the second argument
  103772                 : ** for an expression of type TK_COLUMN that refers to the same column and
  103773                 : ** uses the same collation sequence as the iCol'th column of index pIdx.
  103774                 : ** Argument iBase is the cursor number used for the table that pIdx refers
  103775                 : ** to.
  103776                 : **
  103777                 : ** If such an expression is found, its index in pList->a[] is returned. If
  103778                 : ** no expression is found, -1 is returned.
  103779                 : */
  103780            2843 : static int findIndexCol(
  103781                 :   Parse *pParse,                  /* Parse context */
  103782                 :   ExprList *pList,                /* Expression list to search */
  103783                 :   int iBase,                      /* Cursor for table associated with pIdx */
  103784                 :   Index *pIdx,                    /* Index to match column of */
  103785                 :   int iCol                        /* Column of index to match */
  103786                 : ){
  103787                 :   int i;
  103788            2843 :   const char *zColl = pIdx->azColl[iCol];
  103789                 : 
  103790            5674 :   for(i=0; i<pList->nExpr; i++){
  103791            2843 :     Expr *p = pList->a[i].pExpr;
  103792            2843 :     if( p->op==TK_COLUMN
  103793            2843 :      && p->iColumn==pIdx->aiColumn[iCol]
  103794              12 :      && p->iTable==iBase
  103795                 :     ){
  103796              12 :       CollSeq *pColl = sqlite3ExprCollSeq(pParse, p);
  103797              12 :       if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
  103798              12 :         return i;
  103799                 :       }
  103800                 :     }
  103801                 :   }
  103802                 : 
  103803            2831 :   return -1;
  103804                 : }
  103805                 : 
  103806                 : /*
  103807                 : ** This routine determines if pIdx can be used to assist in processing a
  103808                 : ** DISTINCT qualifier. In other words, it tests whether or not using this
  103809                 : ** index for the outer loop guarantees that rows with equal values for
  103810                 : ** all expressions in the pDistinct list are delivered grouped together.
  103811                 : **
  103812                 : ** For example, the query 
  103813                 : **
  103814                 : **   SELECT DISTINCT a, b, c FROM tbl WHERE a = ?
  103815                 : **
  103816                 : ** can benefit from any index on columns "b" and "c".
  103817                 : */
  103818          222981 : static int isDistinctIndex(
  103819                 :   Parse *pParse,                  /* Parsing context */
  103820                 :   WhereClause *pWC,               /* The WHERE clause */
  103821                 :   Index *pIdx,                    /* The index being considered */
  103822                 :   int base,                       /* Cursor number for the table pIdx is on */
  103823                 :   ExprList *pDistinct,            /* The DISTINCT expressions */
  103824                 :   int nEqCol                      /* Number of index columns with == */
  103825                 : ){
  103826          222981 :   Bitmask mask = 0;               /* Mask of unaccounted for pDistinct exprs */
  103827                 :   int i;                          /* Iterator variable */
  103828                 : 
  103829          222981 :   if( pIdx->zName==0 || pDistinct==0 || pDistinct->nExpr>=BMS ) return 0;
  103830                 :   testcase( pDistinct->nExpr==BMS-1 );
  103831                 : 
  103832                 :   /* Loop through all the expressions in the distinct list. If any of them
  103833                 :   ** are not simple column references, return early. Otherwise, test if the
  103834                 :   ** WHERE clause contains a "col=X" clause. If it does, the expression
  103835                 :   ** can be ignored. If it does not, and the column does not belong to the
  103836                 :   ** same table as index pIdx, return early. Finally, if there is no
  103837                 :   ** matching "col=X" expression and the column is on the same table as pIdx,
  103838                 :   ** set the corresponding bit in variable mask.
  103839                 :   */
  103840            2874 :   for(i=0; i<pDistinct->nExpr; i++){
  103841                 :     WhereTerm *pTerm;
  103842            1437 :     Expr *p = pDistinct->a[i].pExpr;
  103843            1437 :     if( p->op!=TK_COLUMN ) return 0;
  103844            1437 :     pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
  103845            1437 :     if( pTerm ){
  103846               0 :       Expr *pX = pTerm->pExpr;
  103847               0 :       CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
  103848               0 :       CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
  103849               0 :       if( p1==p2 ) continue;
  103850                 :     }
  103851            1437 :     if( p->iTable!=base ) return 0;
  103852            1437 :     mask |= (((Bitmask)1) << i);
  103853                 :   }
  103854                 : 
  103855            1449 :   for(i=nEqCol; mask && i<pIdx->nColumn; i++){
  103856            1437 :     int iExpr = findIndexCol(pParse, pDistinct, base, pIdx, i);
  103857            1437 :     if( iExpr<0 ) break;
  103858              12 :     mask &= ~(((Bitmask)1) << iExpr);
  103859                 :   }
  103860                 : 
  103861            1437 :   return (mask==0);
  103862                 : }
  103863                 : 
  103864                 : 
  103865                 : /*
  103866                 : ** Return true if the DISTINCT expression-list passed as the third argument
  103867                 : ** is redundant. A DISTINCT list is redundant if the database contains a
  103868                 : ** UNIQUE index that guarantees that the result of the query will be distinct
  103869                 : ** anyway.
  103870                 : */
  103871             735 : static int isDistinctRedundant(
  103872                 :   Parse *pParse,
  103873                 :   SrcList *pTabList,
  103874                 :   WhereClause *pWC,
  103875                 :   ExprList *pDistinct
  103876                 : ){
  103877                 :   Table *pTab;
  103878                 :   Index *pIdx;
  103879                 :   int i;                          
  103880                 :   int iBase;
  103881                 : 
  103882                 :   /* If there is more than one table or sub-select in the FROM clause of
  103883                 :   ** this query, then it will not be possible to show that the DISTINCT 
  103884                 :   ** clause is redundant. */
  103885             735 :   if( pTabList->nSrc!=1 ) return 0;
  103886             735 :   iBase = pTabList->a[0].iCursor;
  103887             735 :   pTab = pTabList->a[0].pTab;
  103888                 : 
  103889                 :   /* If any of the expressions is an IPK column on table iBase, then return 
  103890                 :   ** true. Note: The (p->iTable==iBase) part of this test may be false if the
  103891                 :   ** current SELECT is a correlated sub-query.
  103892                 :   */
  103893            1470 :   for(i=0; i<pDistinct->nExpr; i++){
  103894             735 :     Expr *p = pDistinct->a[i].pExpr;
  103895             735 :     if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
  103896                 :   }
  103897                 : 
  103898                 :   /* Loop through all indices on the table, checking each to see if it makes
  103899                 :   ** the DISTINCT qualifier redundant. It does so if:
  103900                 :   **
  103901                 :   **   1. The index is itself UNIQUE, and
  103902                 :   **
  103903                 :   **   2. All of the columns in the index are either part of the pDistinct
  103904                 :   **      list, or else the WHERE clause contains a term of the form "col=X",
  103905                 :   **      where X is a constant value. The collation sequences of the
  103906                 :   **      comparison and select-list expressions must match those of the index.
  103907                 :   */
  103908            2172 :   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  103909            1437 :     if( pIdx->onError==OE_None ) continue;
  103910            1406 :     for(i=0; i<pIdx->nColumn; i++){
  103911            1406 :       int iCol = pIdx->aiColumn[i];
  103912            1406 :       if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) 
  103913            1406 :        && 0>findIndexCol(pParse, pDistinct, iBase, pIdx, i)
  103914                 :       ){
  103915            1406 :         break;
  103916                 :       }
  103917                 :     }
  103918            1406 :     if( i==pIdx->nColumn ){
  103919                 :       /* This index implies that the DISTINCT qualifier is redundant. */
  103920               0 :       return 1;
  103921                 :     }
  103922                 :   }
  103923                 : 
  103924             735 :   return 0;
  103925                 : }
  103926                 : 
  103927                 : /*
  103928                 : ** This routine decides if pIdx can be used to satisfy the ORDER BY
  103929                 : ** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
  103930                 : ** ORDER BY clause, this routine returns 0.
  103931                 : **
  103932                 : ** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
  103933                 : ** left-most table in the FROM clause of that same SELECT statement and
  103934                 : ** the table has a cursor number of "base".  pIdx is an index on pTab.
  103935                 : **
  103936                 : ** nEqCol is the number of columns of pIdx that are used as equality
  103937                 : ** constraints.  Any of these columns may be missing from the ORDER BY
  103938                 : ** clause and the match can still be a success.
  103939                 : **
  103940                 : ** All terms of the ORDER BY that match against the index must be either
  103941                 : ** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
  103942                 : ** index do not need to satisfy this constraint.)  The *pbRev value is
  103943                 : ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
  103944                 : ** the ORDER BY clause is all ASC.
  103945                 : */
  103946          222981 : static int isSortingIndex(
  103947                 :   Parse *pParse,          /* Parsing context */
  103948                 :   WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
  103949                 :   Index *pIdx,            /* The index we are testing */
  103950                 :   int base,               /* Cursor number for the table to be sorted */
  103951                 :   ExprList *pOrderBy,     /* The ORDER BY clause */
  103952                 :   int nEqCol,             /* Number of index columns with == constraints */
  103953                 :   int wsFlags,            /* Index usages flags */
  103954                 :   int *pbRev              /* Set to 1 if ORDER BY is DESC */
  103955                 : ){
  103956                 :   int i, j;                       /* Loop counters */
  103957          222981 :   int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
  103958                 :   int nTerm;                      /* Number of ORDER BY terms */
  103959                 :   struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
  103960          222981 :   sqlite3 *db = pParse->db;
  103961                 : 
  103962          222981 :   if( !pOrderBy ) return 0;
  103963           40385 :   if( wsFlags & WHERE_COLUMN_IN ) return 0;
  103964           39921 :   if( pIdx->bUnordered ) return 0;
  103965                 : 
  103966           39921 :   nTerm = pOrderBy->nExpr;
  103967           39921 :   assert( nTerm>0 );
  103968                 : 
  103969                 :   /* Argument pIdx must either point to a 'real' named index structure, 
  103970                 :   ** or an index structure allocated on the stack by bestBtreeIndex() to
  103971                 :   ** represent the rowid index that is part of every table.  */
  103972           39921 :   assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
  103973                 : 
  103974                 :   /* Match terms of the ORDER BY clause against columns of
  103975                 :   ** the index.
  103976                 :   **
  103977                 :   ** Note that indices have pIdx->nColumn regular columns plus
  103978                 :   ** one additional column containing the rowid.  The rowid column
  103979                 :   ** of the index is also allowed to match against the ORDER BY
  103980                 :   ** clause.
  103981                 :   */
  103982           66061 :   for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
  103983                 :     Expr *pExpr;       /* The expression of the ORDER BY pTerm */
  103984                 :     CollSeq *pColl;    /* The collating sequence of pExpr */
  103985                 :     int termSortOrder; /* Sort order for this term */
  103986                 :     int iColumn;       /* The i-th column of the index.  -1 for rowid */
  103987                 :     int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
  103988                 :     const char *zColl; /* Name of the collating sequence for i-th index term */
  103989                 : 
  103990           43112 :     pExpr = pTerm->pExpr;
  103991           43112 :     if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
  103992                 :       /* Can not use an index sort on anything that is not a column in the
  103993                 :       ** left-most table of the FROM clause */
  103994                 :       break;
  103995                 :     }
  103996           39899 :     pColl = sqlite3ExprCollSeq(pParse, pExpr);
  103997           39899 :     if( !pColl ){
  103998           22732 :       pColl = db->pDfltColl;
  103999                 :     }
  104000           39899 :     if( pIdx->zName && i<pIdx->nColumn ){
  104001           15445 :       iColumn = pIdx->aiColumn[i];
  104002           15445 :       if( iColumn==pIdx->pTable->iPKey ){
  104003               0 :         iColumn = -1;
  104004                 :       }
  104005           15445 :       iSortOrder = pIdx->aSortOrder[i];
  104006           15445 :       zColl = pIdx->azColl[i];
  104007                 :     }else{
  104008           24454 :       iColumn = -1;
  104009           24454 :       iSortOrder = 0;
  104010           24454 :       zColl = pColl->zName;
  104011                 :     }
  104012           39899 :     if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
  104013                 :       /* Term j of the ORDER BY clause does not match column i of the index */
  104014           16103 :       if( i<nEqCol ){
  104015                 :         /* If an index column that is constrained by == fails to match an
  104016                 :         ** ORDER BY term, that is OK.  Just ignore that column of the index
  104017                 :         */
  104018            2350 :         continue;
  104019           13753 :       }else if( i==pIdx->nColumn ){
  104020                 :         /* Index column i is the rowid.  All other terms match. */
  104021             456 :         break;
  104022                 :       }else{
  104023                 :         /* If an index column fails to match and is not constrained by ==
  104024                 :         ** then the index cannot satisfy the ORDER BY constraint.
  104025                 :         */
  104026           13297 :         return 0;
  104027                 :       }
  104028                 :     }
  104029           23796 :     assert( pIdx->aSortOrder!=0 || iColumn==-1 );
  104030           23796 :     assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
  104031           23796 :     assert( iSortOrder==0 || iSortOrder==1 );
  104032           23796 :     termSortOrder = iSortOrder ^ pTerm->sortOrder;
  104033           23796 :     if( i>nEqCol ){
  104034             515 :       if( termSortOrder!=sortOrder ){
  104035                 :         /* Indices can only be used if all ORDER BY terms past the
  104036                 :         ** equality constraints are all either DESC or ASC. */
  104037               6 :         return 0;
  104038                 :       }
  104039                 :     }else{
  104040           23281 :       sortOrder = termSortOrder;
  104041                 :     }
  104042           23790 :     j++;
  104043           23790 :     pTerm++;
  104044           23790 :     if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
  104045                 :       /* If the indexed column is the primary key and everything matches
  104046                 :       ** so far and none of the ORDER BY terms to the right reference other
  104047                 :       ** tables in the join, then we are assured that the index can be used 
  104048                 :       ** to sort because the primary key is unique and so none of the other
  104049                 :       ** columns will make any difference
  104050                 :       */
  104051           20036 :       j = nTerm;
  104052                 :     }
  104053                 :   }
  104054                 : 
  104055           26618 :   *pbRev = sortOrder!=0;
  104056           26618 :   if( j>=nTerm ){
  104057                 :     /* All terms of the ORDER BY clause are covered by this index so
  104058                 :     ** this index can be used for sorting. */
  104059           22949 :     return 1;
  104060                 :   }
  104061            3669 :   if( pIdx->onError!=OE_None && i==pIdx->nColumn
  104062             126 :       && (wsFlags & WHERE_COLUMN_NULL)==0
  104063             126 :       && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
  104064                 :     /* All terms of this index match some prefix of the ORDER BY clause
  104065                 :     ** and the index is UNIQUE and no terms on the tail of the ORDER BY
  104066                 :     ** clause reference other tables in a join.  If this is all true then
  104067                 :     ** the order by clause is superfluous.  Not that if the matching
  104068                 :     ** condition is IS NULL then the result is not necessarily unique
  104069                 :     ** even on a UNIQUE index, so disallow those cases. */
  104070             111 :     return 1;
  104071                 :   }
  104072            3558 :   return 0;
  104073                 : }
  104074                 : 
  104075                 : /*
  104076                 : ** Prepare a crude estimate of the logarithm of the input value.
  104077                 : ** The results need not be exact.  This is only used for estimating
  104078                 : ** the total cost of performing operations with O(logN) or O(NlogN)
  104079                 : ** complexity.  Because N is just a guess, it is no great tragedy if
  104080                 : ** logN is a little off.
  104081                 : */
  104082           97233 : static double estLog(double N){
  104083           97233 :   double logN = 1;
  104084           97233 :   double x = 10;
  104085          606702 :   while( N>x ){
  104086          412236 :     logN += 1;
  104087          412236 :     x *= 10;
  104088                 :   }
  104089           97233 :   return logN;
  104090                 : }
  104091                 : 
  104092                 : /*
  104093                 : ** Two routines for printing the content of an sqlite3_index_info
  104094                 : ** structure.  Used for testing and debugging only.  If neither
  104095                 : ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
  104096                 : ** are no-ops.
  104097                 : */
  104098                 : #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
  104099              54 : static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
  104100                 :   int i;
  104101              54 :   if( !sqlite3WhereTrace ) return;
  104102               0 :   for(i=0; i<p->nConstraint; i++){
  104103               0 :     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
  104104                 :        i,
  104105               0 :        p->aConstraint[i].iColumn,
  104106               0 :        p->aConstraint[i].iTermOffset,
  104107               0 :        p->aConstraint[i].op,
  104108               0 :        p->aConstraint[i].usable);
  104109                 :   }
  104110               0 :   for(i=0; i<p->nOrderBy; i++){
  104111               0 :     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
  104112                 :        i,
  104113               0 :        p->aOrderBy[i].iColumn,
  104114               0 :        p->aOrderBy[i].desc);
  104115                 :   }
  104116                 : }
  104117              54 : static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
  104118                 :   int i;
  104119              54 :   if( !sqlite3WhereTrace ) return;
  104120               0 :   for(i=0; i<p->nConstraint; i++){
  104121               0 :     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
  104122                 :        i,
  104123               0 :        p->aConstraintUsage[i].argvIndex,
  104124               0 :        p->aConstraintUsage[i].omit);
  104125                 :   }
  104126               0 :   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
  104127               0 :   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
  104128               0 :   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
  104129               0 :   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
  104130                 : }
  104131                 : #else
  104132                 : #define TRACE_IDX_INPUTS(A)
  104133                 : #define TRACE_IDX_OUTPUTS(A)
  104134                 : #endif
  104135                 : 
  104136                 : /* 
  104137                 : ** Required because bestIndex() is called by bestOrClauseIndex() 
  104138                 : */
  104139                 : static void bestIndex(
  104140                 :     Parse*, WhereClause*, struct SrcList_item*,
  104141                 :     Bitmask, Bitmask, ExprList*, WhereCost*);
  104142                 : 
  104143                 : /*
  104144                 : ** This routine attempts to find an scanning strategy that can be used 
  104145                 : ** to optimize an 'OR' expression that is part of a WHERE clause. 
  104146                 : **
  104147                 : ** The table associated with FROM clause term pSrc may be either a
  104148                 : ** regular B-Tree table or a virtual table.
  104149                 : */
  104150           89316 : static void bestOrClauseIndex(
  104151                 :   Parse *pParse,              /* The parsing context */
  104152                 :   WhereClause *pWC,           /* The WHERE clause */
  104153                 :   struct SrcList_item *pSrc,  /* The FROM clause term to search */
  104154                 :   Bitmask notReady,           /* Mask of cursors not available for indexing */
  104155                 :   Bitmask notValid,           /* Cursors not available for any purpose */
  104156                 :   ExprList *pOrderBy,         /* The ORDER BY clause */
  104157                 :   WhereCost *pCost            /* Lowest cost query plan */
  104158                 : ){
  104159                 : #ifndef SQLITE_OMIT_OR_OPTIMIZATION
  104160           89316 :   const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
  104161           89316 :   const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
  104162           89316 :   WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
  104163                 :   WhereTerm *pTerm;                 /* A single term of the WHERE clause */
  104164                 : 
  104165                 :   /* The OR-clause optimization is disallowed if the INDEXED BY or
  104166                 :   ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
  104167           89316 :   if( pSrc->notIndexed || pSrc->pIndex!=0 ){
  104168               0 :     return;
  104169                 :   }
  104170           89316 :   if( pWC->wctrlFlags & WHERE_AND_ONLY ){
  104171              60 :     return;
  104172                 :   }
  104173                 : 
  104174                 :   /* Search the WHERE clause terms for a usable WO_OR term. */
  104175          231502 :   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  104176          142246 :     if( pTerm->eOperator==WO_OR 
  104177             253 :      && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
  104178             253 :      && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
  104179                 :     ){
  104180             186 :       WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
  104181             186 :       WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
  104182                 :       WhereTerm *pOrTerm;
  104183             186 :       int flags = WHERE_MULTI_OR;
  104184             186 :       double rTotal = 0;
  104185             186 :       double nRow = 0;
  104186             186 :       Bitmask used = 0;
  104187                 : 
  104188             251 :       for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
  104189                 :         WhereCost sTermCost;
  104190                 :         WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
  104191                 :           (pOrTerm - pOrWC->a), (pTerm - pWC->a)
  104192                 :         ));
  104193             220 :         if( pOrTerm->eOperator==WO_AND ){
  104194             118 :           WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
  104195             118 :           bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
  104196             102 :         }else if( pOrTerm->leftCursor==iCur ){
  104197                 :           WhereClause tempWC;
  104198             102 :           tempWC.pParse = pWC->pParse;
  104199             102 :           tempWC.pMaskSet = pWC->pMaskSet;
  104200             102 :           tempWC.pOuter = pWC;
  104201             102 :           tempWC.op = TK_AND;
  104202             102 :           tempWC.a = pOrTerm;
  104203             102 :           tempWC.wctrlFlags = 0;
  104204             102 :           tempWC.nTerm = 1;
  104205             102 :           bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
  104206                 :         }else{
  104207               0 :           continue;
  104208                 :         }
  104209             220 :         rTotal += sTermCost.rCost;
  104210             220 :         nRow += sTermCost.plan.nRow;
  104211             220 :         used |= sTermCost.used;
  104212             220 :         if( rTotal>=pCost->rCost ) break;
  104213                 :       }
  104214                 : 
  104215                 :       /* If there is an ORDER BY clause, increase the scan cost to account 
  104216                 :       ** for the cost of the sort. */
  104217             186 :       if( pOrderBy!=0 ){
  104218                 :         WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
  104219                 :                     rTotal, rTotal+nRow*estLog(nRow)));
  104220              51 :         rTotal += nRow*estLog(nRow);
  104221                 :       }
  104222                 : 
  104223                 :       /* If the cost of scanning using this OR term for optimization is
  104224                 :       ** less than the current cost stored in pCost, replace the contents
  104225                 :       ** of pCost. */
  104226                 :       WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
  104227             186 :       if( rTotal<pCost->rCost ){
  104228              30 :         pCost->rCost = rTotal;
  104229              30 :         pCost->used = used;
  104230              30 :         pCost->plan.nRow = nRow;
  104231              30 :         pCost->plan.wsFlags = flags;
  104232              30 :         pCost->plan.u.pTerm = pTerm;
  104233                 :       }
  104234                 :     }
  104235                 :   }
  104236                 : #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  104237                 : }
  104238                 : 
  104239                 : #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  104240                 : /*
  104241                 : ** Return TRUE if the WHERE clause term pTerm is of a form where it
  104242                 : ** could be used with an index to access pSrc, assuming an appropriate
  104243                 : ** index existed.
  104244                 : */
  104245            2423 : static int termCanDriveIndex(
  104246                 :   WhereTerm *pTerm,              /* WHERE clause term to check */
  104247                 :   struct SrcList_item *pSrc,     /* Table we are trying to access */
  104248                 :   Bitmask notReady               /* Tables in outer loops of the join */
  104249                 : ){
  104250                 :   char aff;
  104251            2423 :   if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
  104252             999 :   if( pTerm->eOperator!=WO_EQ ) return 0;
  104253             645 :   if( (pTerm->prereqRight & notReady)!=0 ) return 0;
  104254             621 :   aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
  104255             621 :   if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
  104256             621 :   return 1;
  104257                 : }
  104258                 : #endif
  104259                 : 
  104260                 : #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  104261                 : /*
  104262                 : ** If the query plan for pSrc specified in pCost is a full table scan
  104263                 : ** and indexing is allows (if there is no NOT INDEXED clause) and it
  104264                 : ** possible to construct a transient index that would perform better
  104265                 : ** than a full table scan even when the cost of constructing the index
  104266                 : ** is taken into account, then alter the query plan to use the
  104267                 : ** transient index.
  104268                 : */
  104269           89262 : static void bestAutomaticIndex(
  104270                 :   Parse *pParse,              /* The parsing context */
  104271                 :   WhereClause *pWC,           /* The WHERE clause */
  104272                 :   struct SrcList_item *pSrc,  /* The FROM clause term to search */
  104273                 :   Bitmask notReady,           /* Mask of cursors that are not available */
  104274                 :   WhereCost *pCost            /* Lowest cost query plan */
  104275                 : ){
  104276                 :   double nTableRow;           /* Rows in the input table */
  104277                 :   double logN;                /* log(nTableRow) */
  104278                 :   double costTempIdx;         /* per-query cost of the transient index */
  104279                 :   WhereTerm *pTerm;           /* A single term of the WHERE clause */
  104280                 :   WhereTerm *pWCEnd;          /* End of pWC->a[] */
  104281                 :   Table *pTable;              /* Table tht might be indexed */
  104282                 : 
  104283           89262 :   if( pParse->nQueryLoop<=(double)1 ){
  104284                 :     /* There is no point in building an automatic index for a single scan */
  104285           80370 :     return;
  104286                 :   }
  104287            8892 :   if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
  104288                 :     /* Automatic indices are disabled at run-time */
  104289               0 :     return;
  104290                 :   }
  104291            8892 :   if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
  104292                 :     /* We already have some kind of index in use for this query. */
  104293            7137 :     return;
  104294                 :   }
  104295            1755 :   if( pSrc->notIndexed ){
  104296                 :     /* The NOT INDEXED clause appears in the SQL. */
  104297               0 :     return;
  104298                 :   }
  104299            1755 :   if( pSrc->isCorrelated ){
  104300                 :     /* The source is a correlated sub-query. No point in indexing it. */
  104301               0 :     return;
  104302                 :   }
  104303                 : 
  104304            1755 :   assert( pParse->nQueryLoop >= (double)1 );
  104305            1755 :   pTable = pSrc->pTab;
  104306            1755 :   nTableRow = pTable->nRowEst;
  104307            1755 :   logN = estLog(nTableRow);
  104308            1755 :   costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
  104309            1755 :   if( costTempIdx>=pCost->rCost ){
  104310                 :     /* The cost of creating the transient table would be greater than
  104311                 :     ** doing the full table scan */
  104312             208 :     return;
  104313                 :   }
  104314                 : 
  104315                 :   /* Search for any equality comparison term */
  104316            1547 :   pWCEnd = &pWC->a[pWC->nTerm];
  104317            2165 :   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  104318             825 :     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
  104319                 :       WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
  104320                 :                     pCost->rCost, costTempIdx));
  104321             207 :       pCost->rCost = costTempIdx;
  104322             207 :       pCost->plan.nRow = logN + 1;
  104323             207 :       pCost->plan.wsFlags = WHERE_TEMP_INDEX;
  104324             207 :       pCost->used = pTerm->prereqRight;
  104325             207 :       break;
  104326                 :     }
  104327                 :   }
  104328                 : }
  104329                 : #else
  104330                 : # define bestAutomaticIndex(A,B,C,D,E)  /* no-op */
  104331                 : #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
  104332                 : 
  104333                 : 
  104334                 : #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  104335                 : /*
  104336                 : ** Generate code to construct the Index object for an automatic index
  104337                 : ** and to set up the WhereLevel object pLevel so that the code generator
  104338                 : ** makes use of the automatic index.
  104339                 : */
  104340             207 : static void constructAutomaticIndex(
  104341                 :   Parse *pParse,              /* The parsing context */
  104342                 :   WhereClause *pWC,           /* The WHERE clause */
  104343                 :   struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
  104344                 :   Bitmask notReady,           /* Mask of cursors that are not available */
  104345                 :   WhereLevel *pLevel          /* Write new index here */
  104346                 : ){
  104347                 :   int nColumn;                /* Number of columns in the constructed index */
  104348                 :   WhereTerm *pTerm;           /* A single term of the WHERE clause */
  104349                 :   WhereTerm *pWCEnd;          /* End of pWC->a[] */
  104350                 :   int nByte;                  /* Byte of memory needed for pIdx */
  104351                 :   Index *pIdx;                /* Object describing the transient index */
  104352                 :   Vdbe *v;                    /* Prepared statement under construction */
  104353                 :   int addrInit;               /* Address of the initialization bypass jump */
  104354                 :   Table *pTable;              /* The table being indexed */
  104355                 :   KeyInfo *pKeyinfo;          /* Key information for the index */   
  104356                 :   int addrTop;                /* Top of the index fill loop */
  104357                 :   int regRecord;              /* Register holding an index record */
  104358                 :   int n;                      /* Column counter */
  104359                 :   int i;                      /* Loop counter */
  104360                 :   int mxBitCol;               /* Maximum column in pSrc->colUsed */
  104361                 :   CollSeq *pColl;             /* Collating sequence to on a column */
  104362                 :   Bitmask idxCols;            /* Bitmap of columns used for indexing */
  104363                 :   Bitmask extraCols;          /* Bitmap of additional columns */
  104364                 : 
  104365                 :   /* Generate code to skip over the creation and initialization of the
  104366                 :   ** transient index on 2nd and subsequent iterations of the loop. */
  104367             207 :   v = pParse->pVdbe;
  104368             207 :   assert( v!=0 );
  104369             207 :   addrInit = sqlite3CodeOnce(pParse);
  104370                 : 
  104371                 :   /* Count the number of columns that will be added to the index
  104372                 :   ** and used to match WHERE clause constraints */
  104373             207 :   nColumn = 0;
  104374             207 :   pTable = pSrc->pTab;
  104375             207 :   pWCEnd = &pWC->a[pWC->nTerm];
  104376             207 :   idxCols = 0;
  104377            1006 :   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  104378             799 :     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
  104379             207 :       int iCol = pTerm->u.leftColumn;
  104380             207 :       Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
  104381                 :       testcase( iCol==BMS );
  104382                 :       testcase( iCol==BMS-1 );
  104383             207 :       if( (idxCols & cMask)==0 ){
  104384             207 :         nColumn++;
  104385             207 :         idxCols |= cMask;
  104386                 :       }
  104387                 :     }
  104388                 :   }
  104389             207 :   assert( nColumn>0 );
  104390             207 :   pLevel->plan.nEq = nColumn;
  104391                 : 
  104392                 :   /* Count the number of additional columns needed to create a
  104393                 :   ** covering index.  A "covering index" is an index that contains all
  104394                 :   ** columns that are needed by the query.  With a covering index, the
  104395                 :   ** original table never needs to be accessed.  Automatic indices must
  104396                 :   ** be a covering index because the index will not be updated if the
  104397                 :   ** original table changes and the index and table cannot both be used
  104398                 :   ** if they go out of sync.
  104399                 :   */
  104400             207 :   extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
  104401             207 :   mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
  104402                 :   testcase( pTable->nCol==BMS-1 );
  104403                 :   testcase( pTable->nCol==BMS-2 );
  104404            2097 :   for(i=0; i<mxBitCol; i++){
  104405            1890 :     if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
  104406                 :   }
  104407             207 :   if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
  104408               0 :     nColumn += pTable->nCol - BMS + 1;
  104409                 :   }
  104410             207 :   pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
  104411                 : 
  104412                 :   /* Construct the Index object to describe this index */
  104413             207 :   nByte = sizeof(Index);
  104414             207 :   nByte += nColumn*sizeof(int);     /* Index.aiColumn */
  104415             207 :   nByte += nColumn*sizeof(char*);   /* Index.azColl */
  104416             207 :   nByte += nColumn;                 /* Index.aSortOrder */
  104417             207 :   pIdx = sqlite3DbMallocZero(pParse->db, nByte);
  104418             207 :   if( pIdx==0 ) return;
  104419             207 :   pLevel->plan.u.pIdx = pIdx;
  104420             207 :   pIdx->azColl = (char**)&pIdx[1];
  104421             207 :   pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
  104422             207 :   pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
  104423             207 :   pIdx->zName = "auto-index";
  104424             207 :   pIdx->nColumn = nColumn;
  104425             207 :   pIdx->pTable = pTable;
  104426             207 :   n = 0;
  104427             207 :   idxCols = 0;
  104428            1006 :   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  104429             799 :     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
  104430             207 :       int iCol = pTerm->u.leftColumn;
  104431             207 :       Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
  104432             207 :       if( (idxCols & cMask)==0 ){
  104433             207 :         Expr *pX = pTerm->pExpr;
  104434             207 :         idxCols |= cMask;
  104435             207 :         pIdx->aiColumn[n] = pTerm->u.leftColumn;
  104436             207 :         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
  104437             207 :         pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
  104438             207 :         n++;
  104439                 :       }
  104440                 :     }
  104441                 :   }
  104442             207 :   assert( (u32)n==pLevel->plan.nEq );
  104443                 : 
  104444                 :   /* Add additional columns needed to make the automatic index into
  104445                 :   ** a covering index */
  104446            2097 :   for(i=0; i<mxBitCol; i++){
  104447            1890 :     if( extraCols & (((Bitmask)1)<<i) ){
  104448              32 :       pIdx->aiColumn[n] = i;
  104449              32 :       pIdx->azColl[n] = "BINARY";
  104450              32 :       n++;
  104451                 :     }
  104452                 :   }
  104453             207 :   if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
  104454               0 :     for(i=BMS-1; i<pTable->nCol; i++){
  104455               0 :       pIdx->aiColumn[n] = i;
  104456               0 :       pIdx->azColl[n] = "BINARY";
  104457               0 :       n++;
  104458                 :     }
  104459                 :   }
  104460             207 :   assert( n==nColumn );
  104461                 : 
  104462                 :   /* Create the automatic index */
  104463             207 :   pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
  104464             207 :   assert( pLevel->iIdxCur>=0 );
  104465             207 :   sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
  104466                 :                     (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
  104467             207 :   VdbeComment((v, "for %s", pTable->zName));
  104468                 : 
  104469                 :   /* Fill the automatic index with content */
  104470             207 :   addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
  104471             207 :   regRecord = sqlite3GetTempReg(pParse);
  104472             207 :   sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
  104473             207 :   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
  104474             207 :   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  104475             207 :   sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
  104476             207 :   sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
  104477             207 :   sqlite3VdbeJumpHere(v, addrTop);
  104478             207 :   sqlite3ReleaseTempReg(pParse, regRecord);
  104479                 :   
  104480                 :   /* Jump here when skipping the initialization */
  104481             207 :   sqlite3VdbeJumpHere(v, addrInit);
  104482                 : }
  104483                 : #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
  104484                 : 
  104485                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  104486                 : /*
  104487                 : ** Allocate and populate an sqlite3_index_info structure. It is the 
  104488                 : ** responsibility of the caller to eventually release the structure
  104489                 : ** by passing the pointer returned by this function to sqlite3_free().
  104490                 : */
  104491              54 : static sqlite3_index_info *allocateIndexInfo(
  104492                 :   Parse *pParse, 
  104493                 :   WhereClause *pWC,
  104494                 :   struct SrcList_item *pSrc,
  104495                 :   ExprList *pOrderBy
  104496                 : ){
  104497                 :   int i, j;
  104498                 :   int nTerm;
  104499                 :   struct sqlite3_index_constraint *pIdxCons;
  104500                 :   struct sqlite3_index_orderby *pIdxOrderBy;
  104501                 :   struct sqlite3_index_constraint_usage *pUsage;
  104502                 :   WhereTerm *pTerm;
  104503                 :   int nOrderBy;
  104504                 :   sqlite3_index_info *pIdxInfo;
  104505                 : 
  104506                 :   WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
  104507                 : 
  104508                 :   /* Count the number of possible WHERE clause constraints referring
  104509                 :   ** to this virtual table */
  104510             108 :   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  104511              54 :     if( pTerm->leftCursor != pSrc->iCursor ) continue;
  104512              53 :     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
  104513                 :     testcase( pTerm->eOperator==WO_IN );
  104514                 :     testcase( pTerm->eOperator==WO_ISNULL );
  104515              53 :     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
  104516                 :     if( pTerm->wtFlags & TERM_VNULL ) continue;
  104517              53 :     nTerm++;
  104518                 :   }
  104519                 : 
  104520                 :   /* If the ORDER BY clause contains only columns in the current 
  104521                 :   ** virtual table then allocate space for the aOrderBy part of
  104522                 :   ** the sqlite3_index_info structure.
  104523                 :   */
  104524              54 :   nOrderBy = 0;
  104525              54 :   if( pOrderBy ){
  104526               0 :     for(i=0; i<pOrderBy->nExpr; i++){
  104527               0 :       Expr *pExpr = pOrderBy->a[i].pExpr;
  104528               0 :       if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
  104529                 :     }
  104530               0 :     if( i==pOrderBy->nExpr ){
  104531               0 :       nOrderBy = pOrderBy->nExpr;
  104532                 :     }
  104533                 :   }
  104534                 : 
  104535                 :   /* Allocate the sqlite3_index_info structure
  104536                 :   */
  104537              54 :   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
  104538              54 :                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
  104539              54 :                            + sizeof(*pIdxOrderBy)*nOrderBy );
  104540              54 :   if( pIdxInfo==0 ){
  104541               0 :     sqlite3ErrorMsg(pParse, "out of memory");
  104542                 :     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  104543               0 :     return 0;
  104544                 :   }
  104545                 : 
  104546                 :   /* Initialize the structure.  The sqlite3_index_info structure contains
  104547                 :   ** many fields that are declared "const" to prevent xBestIndex from
  104548                 :   ** changing them.  We have to do some funky casting in order to
  104549                 :   ** initialize those fields.
  104550                 :   */
  104551              54 :   pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
  104552              54 :   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
  104553              54 :   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
  104554              54 :   *(int*)&pIdxInfo->nConstraint = nTerm;
  104555              54 :   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
  104556              54 :   *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
  104557              54 :   *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
  104558              54 :   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
  104559                 :                                                                    pUsage;
  104560                 : 
  104561             108 :   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  104562              54 :     if( pTerm->leftCursor != pSrc->iCursor ) continue;
  104563              53 :     assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
  104564                 :     testcase( pTerm->eOperator==WO_IN );
  104565                 :     testcase( pTerm->eOperator==WO_ISNULL );
  104566              53 :     if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
  104567                 :     if( pTerm->wtFlags & TERM_VNULL ) continue;
  104568              53 :     pIdxCons[j].iColumn = pTerm->u.leftColumn;
  104569              53 :     pIdxCons[j].iTermOffset = i;
  104570              53 :     pIdxCons[j].op = (u8)pTerm->eOperator;
  104571                 :     /* The direct assignment in the previous line is possible only because
  104572                 :     ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
  104573                 :     ** following asserts verify this fact. */
  104574                 :     assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
  104575                 :     assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
  104576                 :     assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
  104577                 :     assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
  104578                 :     assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
  104579                 :     assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
  104580              53 :     assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
  104581              53 :     j++;
  104582                 :   }
  104583              54 :   for(i=0; i<nOrderBy; i++){
  104584               0 :     Expr *pExpr = pOrderBy->a[i].pExpr;
  104585               0 :     pIdxOrderBy[i].iColumn = pExpr->iColumn;
  104586               0 :     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
  104587                 :   }
  104588                 : 
  104589              54 :   return pIdxInfo;
  104590                 : }
  104591                 : 
  104592                 : /*
  104593                 : ** The table object reference passed as the second argument to this function
  104594                 : ** must represent a virtual table. This function invokes the xBestIndex()
  104595                 : ** method of the virtual table with the sqlite3_index_info pointer passed
  104596                 : ** as the argument.
  104597                 : **
  104598                 : ** If an error occurs, pParse is populated with an error message and a
  104599                 : ** non-zero value is returned. Otherwise, 0 is returned and the output
  104600                 : ** part of the sqlite3_index_info structure is left populated.
  104601                 : **
  104602                 : ** Whether or not an error is returned, it is the responsibility of the
  104603                 : ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
  104604                 : ** that this is required.
  104605                 : */
  104606              54 : static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
  104607              54 :   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
  104608                 :   int i;
  104609                 :   int rc;
  104610                 : 
  104611                 :   WHERETRACE(("xBestIndex for %s\n", pTab->zName));
  104612              54 :   TRACE_IDX_INPUTS(p);
  104613              54 :   rc = pVtab->pModule->xBestIndex(pVtab, p);
  104614              54 :   TRACE_IDX_OUTPUTS(p);
  104615                 : 
  104616              54 :   if( rc!=SQLITE_OK ){
  104617               0 :     if( rc==SQLITE_NOMEM ){
  104618               0 :       pParse->db->mallocFailed = 1;
  104619               0 :     }else if( !pVtab->zErrMsg ){
  104620               0 :       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
  104621                 :     }else{
  104622               0 :       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
  104623                 :     }
  104624                 :   }
  104625              54 :   sqlite3_free(pVtab->zErrMsg);
  104626              54 :   pVtab->zErrMsg = 0;
  104627                 : 
  104628             107 :   for(i=0; i<p->nConstraint; i++){
  104629              53 :     if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
  104630               0 :       sqlite3ErrorMsg(pParse, 
  104631                 :           "table %s: xBestIndex returned an invalid plan", pTab->zName);
  104632                 :     }
  104633                 :   }
  104634                 : 
  104635              54 :   return pParse->nErr;
  104636                 : }
  104637                 : 
  104638                 : 
  104639                 : /*
  104640                 : ** Compute the best index for a virtual table.
  104641                 : **
  104642                 : ** The best index is computed by the xBestIndex method of the virtual
  104643                 : ** table module.  This routine is really just a wrapper that sets up
  104644                 : ** the sqlite3_index_info structure that is used to communicate with
  104645                 : ** xBestIndex.
  104646                 : **
  104647                 : ** In a join, this routine might be called multiple times for the
  104648                 : ** same virtual table.  The sqlite3_index_info structure is created
  104649                 : ** and initialized on the first invocation and reused on all subsequent
  104650                 : ** invocations.  The sqlite3_index_info structure is also used when
  104651                 : ** code is generated to access the virtual table.  The whereInfoDelete() 
  104652                 : ** routine takes care of freeing the sqlite3_index_info structure after
  104653                 : ** everybody has finished with it.
  104654                 : */
  104655              54 : static void bestVirtualIndex(
  104656                 :   Parse *pParse,                  /* The parsing context */
  104657                 :   WhereClause *pWC,               /* The WHERE clause */
  104658                 :   struct SrcList_item *pSrc,      /* The FROM clause term to search */
  104659                 :   Bitmask notReady,               /* Mask of cursors not available for index */
  104660                 :   Bitmask notValid,               /* Cursors not valid for any purpose */
  104661                 :   ExprList *pOrderBy,             /* The order by clause */
  104662                 :   WhereCost *pCost,               /* Lowest cost query plan */
  104663                 :   sqlite3_index_info **ppIdxInfo  /* Index information passed to xBestIndex */
  104664                 : ){
  104665              54 :   Table *pTab = pSrc->pTab;
  104666                 :   sqlite3_index_info *pIdxInfo;
  104667                 :   struct sqlite3_index_constraint *pIdxCons;
  104668                 :   struct sqlite3_index_constraint_usage *pUsage;
  104669                 :   WhereTerm *pTerm;
  104670                 :   int i, j;
  104671                 :   int nOrderBy;
  104672                 :   double rCost;
  104673                 : 
  104674                 :   /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
  104675                 :   ** malloc in allocateIndexInfo() fails and this function returns leaving
  104676                 :   ** wsFlags in an uninitialized state, the caller may behave unpredictably.
  104677                 :   */
  104678              54 :   memset(pCost, 0, sizeof(*pCost));
  104679              54 :   pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
  104680                 : 
  104681                 :   /* If the sqlite3_index_info structure has not been previously
  104682                 :   ** allocated and initialized, then allocate and initialize it now.
  104683                 :   */
  104684              54 :   pIdxInfo = *ppIdxInfo;
  104685              54 :   if( pIdxInfo==0 ){
  104686              54 :     *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
  104687                 :   }
  104688              54 :   if( pIdxInfo==0 ){
  104689               0 :     return;
  104690                 :   }
  104691                 : 
  104692                 :   /* At this point, the sqlite3_index_info structure that pIdxInfo points
  104693                 :   ** to will have been initialized, either during the current invocation or
  104694                 :   ** during some prior invocation.  Now we just have to customize the
  104695                 :   ** details of pIdxInfo for the current invocation and pass it to
  104696                 :   ** xBestIndex.
  104697                 :   */
  104698                 : 
  104699                 :   /* The module name must be defined. Also, by this point there must
  104700                 :   ** be a pointer to an sqlite3_vtab structure. Otherwise
  104701                 :   ** sqlite3ViewGetColumnNames() would have picked up the error. 
  104702                 :   */
  104703              54 :   assert( pTab->azModuleArg && pTab->azModuleArg[0] );
  104704              54 :   assert( sqlite3GetVTable(pParse->db, pTab) );
  104705                 : 
  104706                 :   /* Set the aConstraint[].usable fields and initialize all 
  104707                 :   ** output variables to zero.
  104708                 :   **
  104709                 :   ** aConstraint[].usable is true for constraints where the right-hand
  104710                 :   ** side contains only references to tables to the left of the current
  104711                 :   ** table.  In other words, if the constraint is of the form:
  104712                 :   **
  104713                 :   **           column = expr
  104714                 :   **
  104715                 :   ** and we are evaluating a join, then the constraint on column is 
  104716                 :   ** only valid if all tables referenced in expr occur to the left
  104717                 :   ** of the table containing column.
  104718                 :   **
  104719                 :   ** The aConstraints[] array contains entries for all constraints
  104720                 :   ** on the current table.  That way we only have to compute it once
  104721                 :   ** even though we might try to pick the best index multiple times.
  104722                 :   ** For each attempt at picking an index, the order of tables in the
  104723                 :   ** join might be different so we have to recompute the usable flag
  104724                 :   ** each time.
  104725                 :   */
  104726              54 :   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
  104727              54 :   pUsage = pIdxInfo->aConstraintUsage;
  104728             107 :   for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
  104729              53 :     j = pIdxCons->iTermOffset;
  104730              53 :     pTerm = &pWC->a[j];
  104731              53 :     pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
  104732                 :   }
  104733              54 :   memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
  104734              54 :   if( pIdxInfo->needToFreeIdxStr ){
  104735               0 :     sqlite3_free(pIdxInfo->idxStr);
  104736                 :   }
  104737              54 :   pIdxInfo->idxStr = 0;
  104738              54 :   pIdxInfo->idxNum = 0;
  104739              54 :   pIdxInfo->needToFreeIdxStr = 0;
  104740              54 :   pIdxInfo->orderByConsumed = 0;
  104741                 :   /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
  104742              54 :   pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
  104743              54 :   nOrderBy = pIdxInfo->nOrderBy;
  104744              54 :   if( !pOrderBy ){
  104745              54 :     pIdxInfo->nOrderBy = 0;
  104746                 :   }
  104747                 : 
  104748              54 :   if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
  104749               0 :     return;
  104750                 :   }
  104751                 : 
  104752              54 :   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
  104753             107 :   for(i=0; i<pIdxInfo->nConstraint; i++){
  104754              53 :     if( pUsage[i].argvIndex>0 ){
  104755              53 :       pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
  104756                 :     }
  104757                 :   }
  104758                 : 
  104759                 :   /* If there is an ORDER BY clause, and the selected virtual table index
  104760                 :   ** does not satisfy it, increase the cost of the scan accordingly. This
  104761                 :   ** matches the processing for non-virtual tables in bestBtreeIndex().
  104762                 :   */
  104763              54 :   rCost = pIdxInfo->estimatedCost;
  104764              54 :   if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
  104765               0 :     rCost += estLog(rCost)*rCost;
  104766                 :   }
  104767                 : 
  104768                 :   /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
  104769                 :   ** inital value of lowestCost in this loop. If it is, then the
  104770                 :   ** (cost<lowestCost) test below will never be true.
  104771                 :   ** 
  104772                 :   ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
  104773                 :   ** is defined.
  104774                 :   */
  104775              54 :   if( (SQLITE_BIG_DBL/((double)2))<rCost ){
  104776               0 :     pCost->rCost = (SQLITE_BIG_DBL/((double)2));
  104777                 :   }else{
  104778              54 :     pCost->rCost = rCost;
  104779                 :   }
  104780              54 :   pCost->plan.u.pVtabIdx = pIdxInfo;
  104781              54 :   if( pIdxInfo->orderByConsumed ){
  104782               0 :     pCost->plan.wsFlags |= WHERE_ORDERBY;
  104783                 :   }
  104784              54 :   pCost->plan.nEq = 0;
  104785              54 :   pIdxInfo->nOrderBy = nOrderBy;
  104786                 : 
  104787                 :   /* Try to find a more efficient access pattern by using multiple indexes
  104788                 :   ** to optimize an OR expression within the WHERE clause. 
  104789                 :   */
  104790              54 :   bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
  104791                 : }
  104792                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
  104793                 : 
  104794                 : #ifdef SQLITE_ENABLE_STAT3
  104795                 : /*
  104796                 : ** Estimate the location of a particular key among all keys in an
  104797                 : ** index.  Store the results in aStat as follows:
  104798                 : **
  104799                 : **    aStat[0]      Est. number of rows less than pVal
  104800                 : **    aStat[1]      Est. number of rows equal to pVal
  104801                 : **
  104802                 : ** Return SQLITE_OK on success.
  104803                 : */
  104804                 : static int whereKeyStats(
  104805                 :   Parse *pParse,              /* Database connection */
  104806                 :   Index *pIdx,                /* Index to consider domain of */
  104807                 :   sqlite3_value *pVal,        /* Value to consider */
  104808                 :   int roundUp,                /* Round up if true.  Round down if false */
  104809                 :   tRowcnt *aStat              /* OUT: stats written here */
  104810                 : ){
  104811                 :   tRowcnt n;
  104812                 :   IndexSample *aSample;
  104813                 :   int i, eType;
  104814                 :   int isEq = 0;
  104815                 :   i64 v;
  104816                 :   double r, rS;
  104817                 : 
  104818                 :   assert( roundUp==0 || roundUp==1 );
  104819                 :   assert( pIdx->nSample>0 );
  104820                 :   if( pVal==0 ) return SQLITE_ERROR;
  104821                 :   n = pIdx->aiRowEst[0];
  104822                 :   aSample = pIdx->aSample;
  104823                 :   eType = sqlite3_value_type(pVal);
  104824                 : 
  104825                 :   if( eType==SQLITE_INTEGER ){
  104826                 :     v = sqlite3_value_int64(pVal);
  104827                 :     r = (i64)v;
  104828                 :     for(i=0; i<pIdx->nSample; i++){
  104829                 :       if( aSample[i].eType==SQLITE_NULL ) continue;
  104830                 :       if( aSample[i].eType>=SQLITE_TEXT ) break;
  104831                 :       if( aSample[i].eType==SQLITE_INTEGER ){
  104832                 :         if( aSample[i].u.i>=v ){
  104833                 :           isEq = aSample[i].u.i==v;
  104834                 :           break;
  104835                 :         }
  104836                 :       }else{
  104837                 :         assert( aSample[i].eType==SQLITE_FLOAT );
  104838                 :         if( aSample[i].u.r>=r ){
  104839                 :           isEq = aSample[i].u.r==r;
  104840                 :           break;
  104841                 :         }
  104842                 :       }
  104843                 :     }
  104844                 :   }else if( eType==SQLITE_FLOAT ){
  104845                 :     r = sqlite3_value_double(pVal);
  104846                 :     for(i=0; i<pIdx->nSample; i++){
  104847                 :       if( aSample[i].eType==SQLITE_NULL ) continue;
  104848                 :       if( aSample[i].eType>=SQLITE_TEXT ) break;
  104849                 :       if( aSample[i].eType==SQLITE_FLOAT ){
  104850                 :         rS = aSample[i].u.r;
  104851                 :       }else{
  104852                 :         rS = aSample[i].u.i;
  104853                 :       }
  104854                 :       if( rS>=r ){
  104855                 :         isEq = rS==r;
  104856                 :         break;
  104857                 :       }
  104858                 :     }
  104859                 :   }else if( eType==SQLITE_NULL ){
  104860                 :     i = 0;
  104861                 :     if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
  104862                 :   }else{
  104863                 :     assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
  104864                 :     for(i=0; i<pIdx->nSample; i++){
  104865                 :       if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
  104866                 :         break;
  104867                 :       }
  104868                 :     }
  104869                 :     if( i<pIdx->nSample ){      
  104870                 :       sqlite3 *db = pParse->db;
  104871                 :       CollSeq *pColl;
  104872                 :       const u8 *z;
  104873                 :       if( eType==SQLITE_BLOB ){
  104874                 :         z = (const u8 *)sqlite3_value_blob(pVal);
  104875                 :         pColl = db->pDfltColl;
  104876                 :         assert( pColl->enc==SQLITE_UTF8 );
  104877                 :       }else{
  104878                 :         pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl);
  104879                 :         if( pColl==0 ){
  104880                 :           sqlite3ErrorMsg(pParse, "no such collation sequence: %s",
  104881                 :                           *pIdx->azColl);
  104882                 :           return SQLITE_ERROR;
  104883                 :         }
  104884                 :         z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
  104885                 :         if( !z ){
  104886                 :           return SQLITE_NOMEM;
  104887                 :         }
  104888                 :         assert( z && pColl && pColl->xCmp );
  104889                 :       }
  104890                 :       n = sqlite3ValueBytes(pVal, pColl->enc);
  104891                 :   
  104892                 :       for(; i<pIdx->nSample; i++){
  104893                 :         int c;
  104894                 :         int eSampletype = aSample[i].eType;
  104895                 :         if( eSampletype<eType ) continue;
  104896                 :         if( eSampletype!=eType ) break;
  104897                 : #ifndef SQLITE_OMIT_UTF16
  104898                 :         if( pColl->enc!=SQLITE_UTF8 ){
  104899                 :           int nSample;
  104900                 :           char *zSample = sqlite3Utf8to16(
  104901                 :               db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
  104902                 :           );
  104903                 :           if( !zSample ){
  104904                 :             assert( db->mallocFailed );
  104905                 :             return SQLITE_NOMEM;
  104906                 :           }
  104907                 :           c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
  104908                 :           sqlite3DbFree(db, zSample);
  104909                 :         }else
  104910                 : #endif
  104911                 :         {
  104912                 :           c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
  104913                 :         }
  104914                 :         if( c>=0 ){
  104915                 :           if( c==0 ) isEq = 1;
  104916                 :           break;
  104917                 :         }
  104918                 :       }
  104919                 :     }
  104920                 :   }
  104921                 : 
  104922                 :   /* At this point, aSample[i] is the first sample that is greater than
  104923                 :   ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
  104924                 :   ** than pVal.  If aSample[i]==pVal, then isEq==1.
  104925                 :   */
  104926                 :   if( isEq ){
  104927                 :     assert( i<pIdx->nSample );
  104928                 :     aStat[0] = aSample[i].nLt;
  104929                 :     aStat[1] = aSample[i].nEq;
  104930                 :   }else{
  104931                 :     tRowcnt iLower, iUpper, iGap;
  104932                 :     if( i==0 ){
  104933                 :       iLower = 0;
  104934                 :       iUpper = aSample[0].nLt;
  104935                 :     }else{
  104936                 :       iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
  104937                 :       iLower = aSample[i-1].nEq + aSample[i-1].nLt;
  104938                 :     }
  104939                 :     aStat[1] = pIdx->avgEq;
  104940                 :     if( iLower>=iUpper ){
  104941                 :       iGap = 0;
  104942                 :     }else{
  104943                 :       iGap = iUpper - iLower;
  104944                 :     }
  104945                 :     if( roundUp ){
  104946                 :       iGap = (iGap*2)/3;
  104947                 :     }else{
  104948                 :       iGap = iGap/3;
  104949                 :     }
  104950                 :     aStat[0] = iLower + iGap;
  104951                 :   }
  104952                 :   return SQLITE_OK;
  104953                 : }
  104954                 : #endif /* SQLITE_ENABLE_STAT3 */
  104955                 : 
  104956                 : /*
  104957                 : ** If expression pExpr represents a literal value, set *pp to point to
  104958                 : ** an sqlite3_value structure containing the same value, with affinity
  104959                 : ** aff applied to it, before returning. It is the responsibility of the 
  104960                 : ** caller to eventually release this structure by passing it to 
  104961                 : ** sqlite3ValueFree().
  104962                 : **
  104963                 : ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
  104964                 : ** is an SQL variable that currently has a non-NULL value bound to it,
  104965                 : ** create an sqlite3_value structure containing this value, again with
  104966                 : ** affinity aff applied to it, instead.
  104967                 : **
  104968                 : ** If neither of the above apply, set *pp to NULL.
  104969                 : **
  104970                 : ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
  104971                 : */
  104972                 : #ifdef SQLITE_ENABLE_STAT3
  104973                 : static int valueFromExpr(
  104974                 :   Parse *pParse, 
  104975                 :   Expr *pExpr, 
  104976                 :   u8 aff, 
  104977                 :   sqlite3_value **pp
  104978                 : ){
  104979                 :   if( pExpr->op==TK_VARIABLE
  104980                 :    || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
  104981                 :   ){
  104982                 :     int iVar = pExpr->iColumn;
  104983                 :     sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
  104984                 :     *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
  104985                 :     return SQLITE_OK;
  104986                 :   }
  104987                 :   return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
  104988                 : }
  104989                 : #endif
  104990                 : 
  104991                 : /*
  104992                 : ** This function is used to estimate the number of rows that will be visited
  104993                 : ** by scanning an index for a range of values. The range may have an upper
  104994                 : ** bound, a lower bound, or both. The WHERE clause terms that set the upper
  104995                 : ** and lower bounds are represented by pLower and pUpper respectively. For
  104996                 : ** example, assuming that index p is on t1(a):
  104997                 : **
  104998                 : **   ... FROM t1 WHERE a > ? AND a < ? ...
  104999                 : **                    |_____|   |_____|
  105000                 : **                       |         |
  105001                 : **                     pLower    pUpper
  105002                 : **
  105003                 : ** If either of the upper or lower bound is not present, then NULL is passed in
  105004                 : ** place of the corresponding WhereTerm.
  105005                 : **
  105006                 : ** The nEq parameter is passed the index of the index column subject to the
  105007                 : ** range constraint. Or, equivalently, the number of equality constraints
  105008                 : ** optimized by the proposed index scan. For example, assuming index p is
  105009                 : ** on t1(a, b), and the SQL query is:
  105010                 : **
  105011                 : **   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
  105012                 : **
  105013                 : ** then nEq should be passed the value 1 (as the range restricted column,
  105014                 : ** b, is the second left-most column of the index). Or, if the query is:
  105015                 : **
  105016                 : **   ... FROM t1 WHERE a > ? AND a < ? ...
  105017                 : **
  105018                 : ** then nEq should be passed 0.
  105019                 : **
  105020                 : ** The returned value is an integer divisor to reduce the estimated
  105021                 : ** search space.  A return value of 1 means that range constraints are
  105022                 : ** no help at all.  A return value of 2 means range constraints are
  105023                 : ** expected to reduce the search space by half.  And so forth...
  105024                 : **
  105025                 : ** In the absence of sqlite_stat3 ANALYZE data, each range inequality
  105026                 : ** reduces the search space by a factor of 4.  Hence a single constraint (x>?)
  105027                 : ** results in a return of 4 and a range constraint (x>? AND x<?) results
  105028                 : ** in a return of 16.
  105029                 : */
  105030            1473 : static int whereRangeScanEst(
  105031                 :   Parse *pParse,       /* Parsing & code generating context */
  105032                 :   Index *p,            /* The index containing the range-compared column; "x" */
  105033                 :   int nEq,             /* index into p->aCol[] of the range-compared column */
  105034                 :   WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
  105035                 :   WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
  105036                 :   double *pRangeDiv   /* OUT: Reduce search space by this divisor */
  105037                 : ){
  105038            1473 :   int rc = SQLITE_OK;
  105039                 : 
  105040                 : #ifdef SQLITE_ENABLE_STAT3
  105041                 : 
  105042                 :   if( nEq==0 && p->nSample ){
  105043                 :     sqlite3_value *pRangeVal;
  105044                 :     tRowcnt iLower = 0;
  105045                 :     tRowcnt iUpper = p->aiRowEst[0];
  105046                 :     tRowcnt a[2];
  105047                 :     u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
  105048                 : 
  105049                 :     if( pLower ){
  105050                 :       Expr *pExpr = pLower->pExpr->pRight;
  105051                 :       rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
  105052                 :       assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
  105053                 :       if( rc==SQLITE_OK
  105054                 :        && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
  105055                 :       ){
  105056                 :         iLower = a[0];
  105057                 :         if( pLower->eOperator==WO_GT ) iLower += a[1];
  105058                 :       }
  105059                 :       sqlite3ValueFree(pRangeVal);
  105060                 :     }
  105061                 :     if( rc==SQLITE_OK && pUpper ){
  105062                 :       Expr *pExpr = pUpper->pExpr->pRight;
  105063                 :       rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
  105064                 :       assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
  105065                 :       if( rc==SQLITE_OK
  105066                 :        && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
  105067                 :       ){
  105068                 :         iUpper = a[0];
  105069                 :         if( pUpper->eOperator==WO_LE ) iUpper += a[1];
  105070                 :       }
  105071                 :       sqlite3ValueFree(pRangeVal);
  105072                 :     }
  105073                 :     if( rc==SQLITE_OK ){
  105074                 :       if( iUpper<=iLower ){
  105075                 :         *pRangeDiv = (double)p->aiRowEst[0];
  105076                 :       }else{
  105077                 :         *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
  105078                 :       }
  105079                 :       WHERETRACE(("range scan regions: %u..%u  div=%g\n",
  105080                 :                   (u32)iLower, (u32)iUpper, *pRangeDiv));
  105081                 :       return SQLITE_OK;
  105082                 :     }
  105083                 :   }
  105084                 : #else
  105085                 :   UNUSED_PARAMETER(pParse);
  105086                 :   UNUSED_PARAMETER(p);
  105087                 :   UNUSED_PARAMETER(nEq);
  105088                 : #endif
  105089            1473 :   assert( pLower || pUpper );
  105090            1473 :   *pRangeDiv = (double)1;
  105091            1473 :   if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
  105092            1473 :   if( pUpper ) *pRangeDiv *= (double)4;
  105093            1473 :   return rc;
  105094                 : }
  105095                 : 
  105096                 : #ifdef SQLITE_ENABLE_STAT3
  105097                 : /*
  105098                 : ** Estimate the number of rows that will be returned based on
  105099                 : ** an equality constraint x=VALUE and where that VALUE occurs in
  105100                 : ** the histogram data.  This only works when x is the left-most
  105101                 : ** column of an index and sqlite_stat3 histogram data is available
  105102                 : ** for that index.  When pExpr==NULL that means the constraint is
  105103                 : ** "x IS NULL" instead of "x=VALUE".
  105104                 : **
  105105                 : ** Write the estimated row count into *pnRow and return SQLITE_OK. 
  105106                 : ** If unable to make an estimate, leave *pnRow unchanged and return
  105107                 : ** non-zero.
  105108                 : **
  105109                 : ** This routine can fail if it is unable to load a collating sequence
  105110                 : ** required for string comparison, or if unable to allocate memory
  105111                 : ** for a UTF conversion required for comparison.  The error is stored
  105112                 : ** in the pParse structure.
  105113                 : */
  105114                 : static int whereEqualScanEst(
  105115                 :   Parse *pParse,       /* Parsing & code generating context */
  105116                 :   Index *p,            /* The index whose left-most column is pTerm */
  105117                 :   Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
  105118                 :   double *pnRow        /* Write the revised row estimate here */
  105119                 : ){
  105120                 :   sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
  105121                 :   u8 aff;                   /* Column affinity */
  105122                 :   int rc;                   /* Subfunction return code */
  105123                 :   tRowcnt a[2];             /* Statistics */
  105124                 : 
  105125                 :   assert( p->aSample!=0 );
  105126                 :   assert( p->nSample>0 );
  105127                 :   aff = p->pTable->aCol[p->aiColumn[0]].affinity;
  105128                 :   if( pExpr ){
  105129                 :     rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
  105130                 :     if( rc ) goto whereEqualScanEst_cancel;
  105131                 :   }else{
  105132                 :     pRhs = sqlite3ValueNew(pParse->db);
  105133                 :   }
  105134                 :   if( pRhs==0 ) return SQLITE_NOTFOUND;
  105135                 :   rc = whereKeyStats(pParse, p, pRhs, 0, a);
  105136                 :   if( rc==SQLITE_OK ){
  105137                 :     WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
  105138                 :     *pnRow = a[1];
  105139                 :   }
  105140                 : whereEqualScanEst_cancel:
  105141                 :   sqlite3ValueFree(pRhs);
  105142                 :   return rc;
  105143                 : }
  105144                 : #endif /* defined(SQLITE_ENABLE_STAT3) */
  105145                 : 
  105146                 : #ifdef SQLITE_ENABLE_STAT3
  105147                 : /*
  105148                 : ** Estimate the number of rows that will be returned based on
  105149                 : ** an IN constraint where the right-hand side of the IN operator
  105150                 : ** is a list of values.  Example:
  105151                 : **
  105152                 : **        WHERE x IN (1,2,3,4)
  105153                 : **
  105154                 : ** Write the estimated row count into *pnRow and return SQLITE_OK. 
  105155                 : ** If unable to make an estimate, leave *pnRow unchanged and return
  105156                 : ** non-zero.
  105157                 : **
  105158                 : ** This routine can fail if it is unable to load a collating sequence
  105159                 : ** required for string comparison, or if unable to allocate memory
  105160                 : ** for a UTF conversion required for comparison.  The error is stored
  105161                 : ** in the pParse structure.
  105162                 : */
  105163                 : static int whereInScanEst(
  105164                 :   Parse *pParse,       /* Parsing & code generating context */
  105165                 :   Index *p,            /* The index whose left-most column is pTerm */
  105166                 :   ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
  105167                 :   double *pnRow        /* Write the revised row estimate here */
  105168                 : ){
  105169                 :   int rc = SQLITE_OK;         /* Subfunction return code */
  105170                 :   double nEst;                /* Number of rows for a single term */
  105171                 :   double nRowEst = (double)0; /* New estimate of the number of rows */
  105172                 :   int i;                      /* Loop counter */
  105173                 : 
  105174                 :   assert( p->aSample!=0 );
  105175                 :   for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
  105176                 :     nEst = p->aiRowEst[0];
  105177                 :     rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
  105178                 :     nRowEst += nEst;
  105179                 :   }
  105180                 :   if( rc==SQLITE_OK ){
  105181                 :     if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
  105182                 :     *pnRow = nRowEst;
  105183                 :     WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
  105184                 :   }
  105185                 :   return rc;
  105186                 : }
  105187                 : #endif /* defined(SQLITE_ENABLE_STAT3) */
  105188                 : 
  105189                 : 
  105190                 : /*
  105191                 : ** Find the best query plan for accessing a particular table.  Write the
  105192                 : ** best query plan and its cost into the WhereCost object supplied as the
  105193                 : ** last parameter.
  105194                 : **
  105195                 : ** The lowest cost plan wins.  The cost is an estimate of the amount of
  105196                 : ** CPU and disk I/O needed to process the requested result.
  105197                 : ** Factors that influence cost include:
  105198                 : **
  105199                 : **    *  The estimated number of rows that will be retrieved.  (The
  105200                 : **       fewer the better.)
  105201                 : **
  105202                 : **    *  Whether or not sorting must occur.
  105203                 : **
  105204                 : **    *  Whether or not there must be separate lookups in the
  105205                 : **       index and in the main table.
  105206                 : **
  105207                 : ** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
  105208                 : ** the SQL statement, then this function only considers plans using the 
  105209                 : ** named index. If no such plan is found, then the returned cost is
  105210                 : ** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
  105211                 : ** then the cost is calculated in the usual way.
  105212                 : **
  105213                 : ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table 
  105214                 : ** in the SELECT statement, then no indexes are considered. However, the 
  105215                 : ** selected plan may still take advantage of the built-in rowid primary key
  105216                 : ** index.
  105217                 : */
  105218           89262 : static void bestBtreeIndex(
  105219                 :   Parse *pParse,              /* The parsing context */
  105220                 :   WhereClause *pWC,           /* The WHERE clause */
  105221                 :   struct SrcList_item *pSrc,  /* The FROM clause term to search */
  105222                 :   Bitmask notReady,           /* Mask of cursors not available for indexing */
  105223                 :   Bitmask notValid,           /* Cursors not available for any purpose */
  105224                 :   ExprList *pOrderBy,         /* The ORDER BY clause */
  105225                 :   ExprList *pDistinct,        /* The select-list if query is DISTINCT */
  105226                 :   WhereCost *pCost            /* Lowest cost query plan */
  105227                 : ){
  105228           89262 :   int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
  105229                 :   Index *pProbe;              /* An index we are evaluating */
  105230                 :   Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
  105231                 :   int eqTermMask;             /* Current mask of valid equality operators */
  105232                 :   int idxEqTermMask;          /* Index mask of valid equality operators */
  105233                 :   Index sPk;                  /* A fake index object for the primary key */
  105234                 :   tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
  105235           89262 :   int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
  105236                 :   int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
  105237                 : 
  105238                 :   /* Initialize the cost to a worst-case value */
  105239           89262 :   memset(pCost, 0, sizeof(*pCost));
  105240           89262 :   pCost->rCost = SQLITE_BIG_DBL;
  105241                 : 
  105242                 :   /* If the pSrc table is the right table of a LEFT JOIN then we may not
  105243                 :   ** use an index to satisfy IS NULL constraints on that table.  This is
  105244                 :   ** because columns might end up being NULL if the table does not match -
  105245                 :   ** a circumstance which the index cannot help us discover.  Ticket #2177.
  105246                 :   */
  105247           89262 :   if( pSrc->jointype & JT_LEFT ){
  105248            2704 :     idxEqTermMask = WO_EQ|WO_IN;
  105249                 :   }else{
  105250           86558 :     idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
  105251                 :   }
  105252                 : 
  105253           89262 :   if( pSrc->pIndex ){
  105254                 :     /* An INDEXED BY clause specifies a particular index to use */
  105255               0 :     pIdx = pProbe = pSrc->pIndex;
  105256               0 :     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
  105257               0 :     eqTermMask = idxEqTermMask;
  105258                 :   }else{
  105259                 :     /* There is no INDEXED BY clause.  Create a fake Index object in local
  105260                 :     ** variable sPk to represent the rowid primary key index.  Make this
  105261                 :     ** fake index the first in a chain of Index objects with all of the real
  105262                 :     ** indices to follow */
  105263                 :     Index *pFirst;                  /* First of real indices on the table */
  105264           89262 :     memset(&sPk, 0, sizeof(Index));
  105265           89262 :     sPk.nColumn = 1;
  105266           89262 :     sPk.aiColumn = &aiColumnPk;
  105267           89262 :     sPk.aiRowEst = aiRowEstPk;
  105268           89262 :     sPk.onError = OE_Replace;
  105269           89262 :     sPk.pTable = pSrc->pTab;
  105270           89262 :     aiRowEstPk[0] = pSrc->pTab->nRowEst;
  105271           89262 :     aiRowEstPk[1] = 1;
  105272           89262 :     pFirst = pSrc->pTab->pIndex;
  105273           89262 :     if( pSrc->notIndexed==0 ){
  105274                 :       /* The real indices of the table are only considered if the
  105275                 :       ** NOT INDEXED qualifier is omitted from the FROM clause */
  105276           89262 :       sPk.pNext = pFirst;
  105277                 :     }
  105278           89262 :     pProbe = &sPk;
  105279           89262 :     wsFlagMask = ~(
  105280                 :         WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
  105281                 :     );
  105282           89262 :     eqTermMask = WO_EQ|WO_IN;
  105283           89262 :     pIdx = 0;
  105284                 :   }
  105285                 : 
  105286                 :   /* Loop over all indices looking for the best one to use
  105287                 :   */
  105288          312243 :   for(; pProbe; pIdx=pProbe=pProbe->pNext){
  105289          222981 :     const tRowcnt * const aiRowEst = pProbe->aiRowEst;
  105290                 :     double cost;                /* Cost of using pProbe */
  105291                 :     double nRow;                /* Estimated number of rows in result set */
  105292          222981 :     double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
  105293                 :     int rev;                    /* True to scan in reverse order */
  105294          222981 :     int wsFlags = 0;
  105295          222981 :     Bitmask used = 0;
  105296                 : 
  105297                 :     /* The following variables are populated based on the properties of
  105298                 :     ** index being evaluated. They are then used to determine the expected
  105299                 :     ** cost and number of rows returned.
  105300                 :     **
  105301                 :     **  nEq: 
  105302                 :     **    Number of equality terms that can be implemented using the index.
  105303                 :     **    In other words, the number of initial fields in the index that
  105304                 :     **    are used in == or IN or NOT NULL constraints of the WHERE clause.
  105305                 :     **
  105306                 :     **  nInMul:  
  105307                 :     **    The "in-multiplier". This is an estimate of how many seek operations 
  105308                 :     **    SQLite must perform on the index in question. For example, if the 
  105309                 :     **    WHERE clause is:
  105310                 :     **
  105311                 :     **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
  105312                 :     **
  105313                 :     **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is 
  105314                 :     **    set to 9. Given the same schema and either of the following WHERE 
  105315                 :     **    clauses:
  105316                 :     **
  105317                 :     **      WHERE a =  1
  105318                 :     **      WHERE a >= 2
  105319                 :     **
  105320                 :     **    nInMul is set to 1.
  105321                 :     **
  105322                 :     **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
  105323                 :     **    the sub-select is assumed to return 25 rows for the purposes of 
  105324                 :     **    determining nInMul.
  105325                 :     **
  105326                 :     **  bInEst:  
  105327                 :     **    Set to true if there was at least one "x IN (SELECT ...)" term used 
  105328                 :     **    in determining the value of nInMul.  Note that the RHS of the
  105329                 :     **    IN operator must be a SELECT, not a value list, for this variable
  105330                 :     **    to be true.
  105331                 :     **
  105332                 :     **  rangeDiv:
  105333                 :     **    An estimate of a divisor by which to reduce the search space due
  105334                 :     **    to inequality constraints.  In the absence of sqlite_stat3 ANALYZE
  105335                 :     **    data, a single inequality reduces the search space to 1/4rd its
  105336                 :     **    original size (rangeDiv==4).  Two inequalities reduce the search
  105337                 :     **    space to 1/16th of its original size (rangeDiv==16).
  105338                 :     **
  105339                 :     **  bSort:   
  105340                 :     **    Boolean. True if there is an ORDER BY clause that will require an 
  105341                 :     **    external sort (i.e. scanning the index being evaluated will not 
  105342                 :     **    correctly order records).
  105343                 :     **
  105344                 :     **  bLookup: 
  105345                 :     **    Boolean. True if a table lookup is required for each index entry
  105346                 :     **    visited.  In other words, true if this is not a covering index.
  105347                 :     **    This is always false for the rowid primary key index of a table.
  105348                 :     **    For other indexes, it is true unless all the columns of the table
  105349                 :     **    used by the SELECT statement are present in the index (such an
  105350                 :     **    index is sometimes described as a covering index).
  105351                 :     **    For example, given the index on (a, b), the second of the following 
  105352                 :     **    two queries requires table b-tree lookups in order to find the value
  105353                 :     **    of column c, but the first does not because columns a and b are
  105354                 :     **    both available in the index.
  105355                 :     **
  105356                 :     **             SELECT a, b    FROM tbl WHERE a = 1;
  105357                 :     **             SELECT a, b, c FROM tbl WHERE a = 1;
  105358                 :     */
  105359                 :     int nEq;                      /* Number of == or IN terms matching index */
  105360          222981 :     int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
  105361          222981 :     int nInMul = 1;               /* Number of distinct equalities to lookup */
  105362          222981 :     double rangeDiv = (double)1;  /* Estimated reduction in search space */
  105363          222981 :     int nBound = 0;               /* Number of range constraints seen */
  105364          222981 :     int bSort = !!pOrderBy;       /* True if external sort required */
  105365          222981 :     int bDist = !!pDistinct;      /* True if index cannot help with DISTINCT */
  105366          222981 :     int bLookup = 0;              /* True if not a covering index */
  105367                 :     WhereTerm *pTerm;             /* A single term of the WHERE clause */
  105368                 : #ifdef SQLITE_ENABLE_STAT3
  105369                 :     WhereTerm *pFirstTerm = 0;    /* First term matching the index */
  105370                 : #endif
  105371                 : 
  105372                 :     /* Determine the values of nEq and nInMul */
  105373          279962 :     for(nEq=0; nEq<pProbe->nColumn; nEq++){
  105374          242854 :       int j = pProbe->aiColumn[nEq];
  105375          242854 :       pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
  105376          242854 :       if( pTerm==0 ) break;
  105377           56981 :       wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
  105378                 :       testcase( pTerm->pWC!=pWC );
  105379           56981 :       if( pTerm->eOperator & WO_IN ){
  105380            2871 :         Expr *pExpr = pTerm->pExpr;
  105381            2871 :         wsFlags |= WHERE_COLUMN_IN;
  105382            2871 :         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  105383                 :           /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
  105384            2040 :           nInMul *= 25;
  105385            2040 :           bInEst = 1;
  105386             831 :         }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
  105387                 :           /* "x IN (value, value, ...)" */
  105388             831 :           nInMul *= pExpr->x.pList->nExpr;
  105389                 :         }
  105390           54110 :       }else if( pTerm->eOperator & WO_ISNULL ){
  105391             816 :         wsFlags |= WHERE_COLUMN_NULL;
  105392                 :       }
  105393                 : #ifdef SQLITE_ENABLE_STAT3
  105394                 :       if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
  105395                 : #endif
  105396           56981 :       used |= pTerm->prereqRight;
  105397                 :     }
  105398                 :  
  105399                 :     /* If the index being considered is UNIQUE, and there is an equality 
  105400                 :     ** constraint for all columns in the index, then this search will find
  105401                 :     ** at most a single row. In this case set the WHERE_UNIQUE flag to 
  105402                 :     ** indicate this to the caller.
  105403                 :     **
  105404                 :     ** Otherwise, if the search may find more than one row, test to see if
  105405                 :     ** there is a range constraint on indexed column (nEq+1) that can be 
  105406                 :     ** optimized using the index. 
  105407                 :     */
  105408          222981 :     if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
  105409                 :       testcase( wsFlags & WHERE_COLUMN_IN );
  105410                 :       testcase( wsFlags & WHERE_COLUMN_NULL );
  105411           63650 :       if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
  105412           29236 :         wsFlags |= WHERE_UNIQUE;
  105413                 :       }
  105414          191156 :     }else if( pProbe->bUnordered==0 ){
  105415          191156 :       int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
  105416          191156 :       if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
  105417            1473 :         WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
  105418            1473 :         WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
  105419            1473 :         whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
  105420            1473 :         if( pTop ){
  105421            1009 :           nBound = 1;
  105422            1009 :           wsFlags |= WHERE_TOP_LIMIT;
  105423            1009 :           used |= pTop->prereqRight;
  105424                 :           testcase( pTop->pWC!=pWC );
  105425                 :         }
  105426            1473 :         if( pBtm ){
  105427            1015 :           nBound++;
  105428            1015 :           wsFlags |= WHERE_BTM_LIMIT;
  105429            1015 :           used |= pBtm->prereqRight;
  105430                 :           testcase( pBtm->pWC!=pWC );
  105431                 :         }
  105432            1473 :         wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
  105433                 :       }
  105434                 :     }
  105435                 : 
  105436                 :     /* If there is an ORDER BY clause and the index being considered will
  105437                 :     ** naturally scan rows in the required order, set the appropriate flags
  105438                 :     ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
  105439                 :     ** will scan rows in a different order, set the bSort variable.  */
  105440          222981 :     if( isSortingIndex(
  105441                 :           pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
  105442                 :     ){
  105443           23060 :       bSort = 0;
  105444           23060 :       wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
  105445           23060 :       wsFlags |= (rev ? WHERE_REVERSE : 0);
  105446                 :     }
  105447                 : 
  105448                 :     /* If there is a DISTINCT qualifier and this index will scan rows in
  105449                 :     ** order of the DISTINCT expressions, clear bDist and set the appropriate
  105450                 :     ** flags in wsFlags. */
  105451          222981 :     if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq) ){
  105452              12 :       bDist = 0;
  105453              12 :       wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
  105454                 :     }
  105455                 : 
  105456                 :     /* If currently calculating the cost of using an index (not the IPK
  105457                 :     ** index), determine if all required column data may be obtained without 
  105458                 :     ** using the main table (i.e. if the index is a covering
  105459                 :     ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
  105460                 :     ** wsFlags. Otherwise, set the bLookup variable to true.  */
  105461          222981 :     if( pIdx && wsFlags ){
  105462           30555 :       Bitmask m = pSrc->colUsed;
  105463                 :       int j;
  105464           83471 :       for(j=0; j<pIdx->nColumn; j++){
  105465           52916 :         int x = pIdx->aiColumn[j];
  105466           52916 :         if( x<BMS-1 ){
  105467           52916 :           m &= ~(((Bitmask)1)<<x);
  105468                 :         }
  105469                 :       }
  105470           30555 :       if( m==0 ){
  105471           14636 :         wsFlags |= WHERE_IDX_ONLY;
  105472                 :       }else{
  105473           15919 :         bLookup = 1;
  105474                 :       }
  105475                 :     }
  105476                 : 
  105477                 :     /*
  105478                 :     ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
  105479                 :     ** constraint, do not let the estimate exceed half the rows in the table.
  105480                 :     */
  105481          222981 :     nRow = (double)(aiRowEst[nEq] * nInMul);
  105482          222981 :     if( bInEst && nRow*2>aiRowEst[0] ){
  105483             298 :       nRow = aiRowEst[0]/2;
  105484             298 :       nInMul = (int)(nRow / aiRowEst[nEq]);
  105485                 :     }
  105486                 : 
  105487                 : #ifdef SQLITE_ENABLE_STAT3
  105488                 :     /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
  105489                 :     ** and we do not think that values of x are unique and if histogram
  105490                 :     ** data is available for column x, then it might be possible
  105491                 :     ** to get a better estimate on the number of rows based on
  105492                 :     ** VALUE and how common that value is according to the histogram.
  105493                 :     */
  105494                 :     if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
  105495                 :       assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
  105496                 :       if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
  105497                 :         testcase( pFirstTerm->eOperator==WO_EQ );
  105498                 :         testcase( pFirstTerm->eOperator==WO_ISNULL );
  105499                 :         whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
  105500                 :       }else if( bInEst==0 ){
  105501                 :         assert( pFirstTerm->eOperator==WO_IN );
  105502                 :         whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
  105503                 :       }
  105504                 :     }
  105505                 : #endif /* SQLITE_ENABLE_STAT3 */
  105506                 : 
  105507                 :     /* Adjust the number of output rows and downward to reflect rows
  105508                 :     ** that are excluded by range constraints.
  105509                 :     */
  105510          222981 :     nRow = nRow/rangeDiv;
  105511          222981 :     if( nRow<1 ) nRow = 1;
  105512                 : 
  105513                 :     /* Experiments run on real SQLite databases show that the time needed
  105514                 :     ** to do a binary search to locate a row in a table or index is roughly
  105515                 :     ** log10(N) times the time to move from one row to the next row within
  105516                 :     ** a table or index.  The actual times can vary, with the size of
  105517                 :     ** records being an important factor.  Both moves and searches are
  105518                 :     ** slower with larger records, presumably because fewer records fit
  105519                 :     ** on one page and hence more pages have to be fetched.
  105520                 :     **
  105521                 :     ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
  105522                 :     ** not give us data on the relative sizes of table and index records.
  105523                 :     ** So this computation assumes table records are about twice as big
  105524                 :     ** as index records
  105525                 :     */
  105526          222981 :     if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
  105527                 :       /* The cost of a full table scan is a number of move operations equal
  105528                 :       ** to the number of rows in the table.
  105529                 :       **
  105530                 :       ** We add an additional 4x penalty to full table scans.  This causes
  105531                 :       ** the cost function to err on the side of choosing an index over
  105532                 :       ** choosing a full scan.  This 4x full-scan penalty is an arguable
  105533                 :       ** decision and one which we expect to revisit in the future.  But
  105534                 :       ** it seems to be working well enough at the moment.
  105535                 :       */
  105536          147039 :       cost = aiRowEst[0]*4;
  105537                 :     }else{
  105538           75942 :       log10N = estLog(aiRowEst[0]);
  105539           75942 :       cost = nRow;
  105540           75942 :       if( pIdx ){
  105541           30555 :         if( bLookup ){
  105542                 :           /* For an index lookup followed by a table lookup:
  105543                 :           **    nInMul index searches to find the start of each index range
  105544                 :           **  + nRow steps through the index
  105545                 :           **  + nRow table searches to lookup the table entry using the rowid
  105546                 :           */
  105547           15919 :           cost += (nInMul + nRow)*log10N;
  105548                 :         }else{
  105549                 :           /* For a covering index:
  105550                 :           **     nInMul index searches to find the initial entry 
  105551                 :           **   + nRow steps through the index
  105552                 :           */
  105553           14636 :           cost += nInMul*log10N;
  105554                 :         }
  105555                 :       }else{
  105556                 :         /* For a rowid primary key lookup:
  105557                 :         **    nInMult table searches to find the initial entry for each range
  105558                 :         **  + nRow steps through the table
  105559                 :         */
  105560           45387 :         cost += nInMul*log10N;
  105561                 :       }
  105562                 :     }
  105563                 : 
  105564                 :     /* Add in the estimated cost of sorting the result.  Actual experimental
  105565                 :     ** measurements of sorting performance in SQLite show that sorting time
  105566                 :     ** adds C*N*log10(N) to the cost, where N is the number of rows to be 
  105567                 :     ** sorted and C is a factor between 1.95 and 4.3.  We will split the
  105568                 :     ** difference and select C of 3.0.
  105569                 :     */
  105570          222981 :     if( bSort ){
  105571           17325 :       cost += nRow*estLog(nRow)*3;
  105572                 :     }
  105573          222981 :     if( bDist ){
  105574            2160 :       cost += nRow*estLog(nRow)*3;
  105575                 :     }
  105576                 : 
  105577                 :     /**** Cost of using this index has now been computed ****/
  105578                 : 
  105579                 :     /* If there are additional constraints on this table that cannot
  105580                 :     ** be used with the current index, but which might lower the number
  105581                 :     ** of output rows, adjust the nRow value accordingly.  This only 
  105582                 :     ** matters if the current index is the least costly, so do not bother
  105583                 :     ** with this step if we already know this index will not be chosen.
  105584                 :     ** Also, never reduce the output row count below 2 using this step.
  105585                 :     **
  105586                 :     ** It is critical that the notValid mask be used here instead of
  105587                 :     ** the notReady mask.  When computing an "optimal" index, the notReady
  105588                 :     ** mask will only have one bit set - the bit for the current table.
  105589                 :     ** The notValid mask, on the other hand, always has all bits set for
  105590                 :     ** tables that are not in outer loops.  If notReady is used here instead
  105591                 :     ** of notValid, then a optimal index that depends on inner joins loops
  105592                 :     ** might be selected even when there exists an optimal index that has
  105593                 :     ** no such dependency.
  105594                 :     */
  105595          222981 :     if( nRow>2 && cost<=pCost->rCost ){
  105596                 :       int k;                       /* Loop counter */
  105597          132516 :       int nSkipEq = nEq;           /* Number of == constraints to skip */
  105598          132516 :       int nSkipRange = nBound;     /* Number of < constraints to skip */
  105599                 :       Bitmask thisTab;             /* Bitmap for pSrc */
  105600                 : 
  105601          132516 :       thisTab = getMask(pWC->pMaskSet, iCur);
  105602          335519 :       for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
  105603          203003 :         if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
  105604          176431 :         if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
  105605          145215 :         if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
  105606          131317 :           if( nSkipEq ){
  105607                 :             /* Ignore the first nEq equality matches since the index
  105608                 :             ** has already accounted for these */
  105609           22448 :             nSkipEq--;
  105610                 :           }else{
  105611                 :             /* Assume each additional equality match reduces the result
  105612                 :             ** set size by a factor of 10 */
  105613          108869 :             nRow /= 10;
  105614                 :           }
  105615           13898 :         }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
  105616            2250 :           if( nSkipRange ){
  105617                 :             /* Ignore the first nSkipRange range constraints since the index
  105618                 :             ** has already accounted for these */
  105619            1161 :             nSkipRange--;
  105620                 :           }else{
  105621                 :             /* Assume each additional range constraint reduces the result
  105622                 :             ** set size by a factor of 3.  Indexed range constraints reduce
  105623                 :             ** the search space by a larger factor: 4.  We make indexed range
  105624                 :             ** more selective intentionally because of the subjective 
  105625                 :             ** observation that indexed range constraints really are more
  105626                 :             ** selective in practice, on average. */
  105627            1089 :             nRow /= 3;
  105628                 :           }
  105629           11648 :         }else if( pTerm->eOperator!=WO_NOOP ){
  105630                 :           /* Any other expression lowers the output row count by half */
  105631            9045 :           nRow /= 2;
  105632                 :         }
  105633                 :       }
  105634          132516 :       if( nRow<2 ) nRow = 2;
  105635                 :     }
  105636                 : 
  105637                 : 
  105638                 :     WHERETRACE((
  105639                 :       "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
  105640                 :       "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
  105641                 :       pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
  105642                 :       nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
  105643                 :       notReady, log10N, nRow, cost, used
  105644                 :     ));
  105645                 : 
  105646                 :     /* If this index is the best we have seen so far, then record this
  105647                 :     ** index and its cost in the pCost structure.
  105648                 :     */
  105649          222981 :     if( (!pIdx || wsFlags)
  105650          119817 :      && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
  105651                 :     ){
  105652          116288 :       pCost->rCost = cost;
  105653          116288 :       pCost->used = used;
  105654          116288 :       pCost->plan.nRow = nRow;
  105655          116288 :       pCost->plan.wsFlags = (wsFlags&wsFlagMask);
  105656          116288 :       pCost->plan.nEq = nEq;
  105657          116288 :       pCost->plan.u.pIdx = pIdx;
  105658                 :     }
  105659                 : 
  105660                 :     /* If there was an INDEXED BY clause, then only that one index is
  105661                 :     ** considered. */
  105662          222981 :     if( pSrc->pIndex ) break;
  105663                 : 
  105664                 :     /* Reset masks for the next index in the loop */
  105665          222981 :     wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
  105666          222981 :     eqTermMask = idxEqTermMask;
  105667                 :   }
  105668                 : 
  105669                 :   /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
  105670                 :   ** is set, then reverse the order that the index will be scanned
  105671                 :   ** in. This is used for application testing, to help find cases
  105672                 :   ** where application behaviour depends on the (undefined) order that
  105673                 :   ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
  105674           89262 :   if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
  105675               0 :     pCost->plan.wsFlags |= WHERE_REVERSE;
  105676                 :   }
  105677                 : 
  105678           89262 :   assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
  105679           89262 :   assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
  105680           89262 :   assert( pSrc->pIndex==0 
  105681                 :        || pCost->plan.u.pIdx==0 
  105682                 :        || pCost->plan.u.pIdx==pSrc->pIndex 
  105683                 :   );
  105684                 : 
  105685                 :   WHERETRACE(("best index is: %s\n", 
  105686                 :     ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" : 
  105687                 :          pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
  105688                 :   ));
  105689                 :   
  105690           89262 :   bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
  105691           89262 :   bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
  105692           89262 :   pCost->plan.wsFlags |= eqTermMask;
  105693           89262 : }
  105694                 : 
  105695                 : /*
  105696                 : ** Find the query plan for accessing table pSrc->pTab. Write the
  105697                 : ** best query plan and its cost into the WhereCost object supplied 
  105698                 : ** as the last parameter. This function may calculate the cost of
  105699                 : ** both real and virtual table scans.
  105700                 : */
  105701             220 : static void bestIndex(
  105702                 :   Parse *pParse,              /* The parsing context */
  105703                 :   WhereClause *pWC,           /* The WHERE clause */
  105704                 :   struct SrcList_item *pSrc,  /* The FROM clause term to search */
  105705                 :   Bitmask notReady,           /* Mask of cursors not available for indexing */
  105706                 :   Bitmask notValid,           /* Cursors not available for any purpose */
  105707                 :   ExprList *pOrderBy,         /* The ORDER BY clause */
  105708                 :   WhereCost *pCost            /* Lowest cost query plan */
  105709                 : ){
  105710                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  105711             220 :   if( IsVirtual(pSrc->pTab) ){
  105712               0 :     sqlite3_index_info *p = 0;
  105713               0 :     bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
  105714               0 :     if( p->needToFreeIdxStr ){
  105715               0 :       sqlite3_free(p->idxStr);
  105716                 :     }
  105717               0 :     sqlite3DbFree(pParse->db, p);
  105718                 :   }else
  105719                 : #endif
  105720                 :   {
  105721             220 :     bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
  105722                 :   }
  105723             220 : }
  105724                 : 
  105725                 : /*
  105726                 : ** Disable a term in the WHERE clause.  Except, do not disable the term
  105727                 : ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  105728                 : ** or USING clause of that join.
  105729                 : **
  105730                 : ** Consider the term t2.z='ok' in the following queries:
  105731                 : **
  105732                 : **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
  105733                 : **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
  105734                 : **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
  105735                 : **
  105736                 : ** The t2.z='ok' is disabled in the in (2) because it originates
  105737                 : ** in the ON clause.  The term is disabled in (3) because it is not part
  105738                 : ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
  105739                 : **
  105740                 : ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
  105741                 : ** completely satisfied by indices.
  105742                 : **
  105743                 : ** Disabling a term causes that term to not be tested in the inner loop
  105744                 : ** of the join.  Disabling is an optimization.  When terms are satisfied
  105745                 : ** by indices, we disable them to prevent redundant tests in the inner
  105746                 : ** loop.  We would get the correct results if nothing were ever disabled,
  105747                 : ** but joins might run a little slower.  The trick is to disable as much
  105748                 : ** as we can without disabling too much.  If we disabled in (1), we'd get
  105749                 : ** the wrong answer.  See ticket #813.
  105750                 : */
  105751          102682 : static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
  105752          102682 :   if( pTerm
  105753           54953 :       && (pTerm->wtFlags & TERM_CODED)==0
  105754           54953 :       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
  105755                 :   ){
  105756           54953 :     pTerm->wtFlags |= TERM_CODED;
  105757           54953 :     if( pTerm->iParent>=0 ){
  105758            4610 :       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
  105759            4610 :       if( (--pOther->nChild)==0 ){
  105760            4369 :         disableTerm(pLevel, pOther);
  105761                 :       }
  105762                 :     }
  105763                 :   }
  105764          102682 : }
  105765                 : 
  105766                 : /*
  105767                 : ** Code an OP_Affinity opcode to apply the column affinity string zAff
  105768                 : ** to the n registers starting at base. 
  105769                 : **
  105770                 : ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
  105771                 : ** beginning and end of zAff are ignored.  If all entries in zAff are
  105772                 : ** SQLITE_AFF_NONE, then no code gets generated.
  105773                 : **
  105774                 : ** This routine makes its own copy of zAff so that the caller is free
  105775                 : ** to modify zAff after this routine returns.
  105776                 : */
  105777           24898 : static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
  105778           24898 :   Vdbe *v = pParse->pVdbe;
  105779           24898 :   if( zAff==0 ){
  105780               0 :     assert( pParse->db->mallocFailed );
  105781               0 :     return;
  105782                 :   }
  105783           24898 :   assert( v!=0 );
  105784                 : 
  105785                 :   /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
  105786                 :   ** and end of the affinity string.
  105787                 :   */
  105788           53710 :   while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
  105789            3914 :     n--;
  105790            3914 :     base++;
  105791            3914 :     zAff++;
  105792                 :   }
  105793           50284 :   while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
  105794             488 :     n--;
  105795                 :   }
  105796                 : 
  105797                 :   /* Code the OP_Affinity opcode if there is anything left to do. */
  105798           24898 :   if( n>0 ){
  105799           20997 :     sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
  105800           20997 :     sqlite3VdbeChangeP4(v, -1, zAff, n);
  105801           20997 :     sqlite3ExprCacheAffinityChange(pParse, base, n);
  105802                 :   }
  105803                 : }
  105804                 : 
  105805                 : 
  105806                 : /*
  105807                 : ** Generate code for a single equality term of the WHERE clause.  An equality
  105808                 : ** term can be either X=expr or X IN (...).   pTerm is the term to be 
  105809                 : ** coded.
  105810                 : **
  105811                 : ** The current value for the constraint is left in register iReg.
  105812                 : **
  105813                 : ** For a constraint of the form X=expr, the expression is evaluated and its
  105814                 : ** result is left on the stack.  For constraints of the form X IN (...)
  105815                 : ** this routine sets up a loop that will iterate over all values of X.
  105816                 : */
  105817           48904 : static int codeEqualityTerm(
  105818                 :   Parse *pParse,      /* The parsing context */
  105819                 :   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
  105820                 :   WhereLevel *pLevel, /* When level of the FROM clause we are working on */
  105821                 :   int iTarget         /* Attempt to leave results in this register */
  105822                 : ){
  105823           48904 :   Expr *pX = pTerm->pExpr;
  105824           48904 :   Vdbe *v = pParse->pVdbe;
  105825                 :   int iReg;                  /* Register holding results */
  105826                 : 
  105827           48904 :   assert( iTarget>0 );
  105828           48904 :   if( pX->op==TK_EQ ){
  105829           45397 :     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
  105830            3507 :   }else if( pX->op==TK_ISNULL ){
  105831             738 :     iReg = iTarget;
  105832             738 :     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
  105833                 : #ifndef SQLITE_OMIT_SUBQUERY
  105834                 :   }else{
  105835                 :     int eType;
  105836                 :     int iTab;
  105837                 :     struct InLoop *pIn;
  105838                 : 
  105839            2769 :     assert( pX->op==TK_IN );
  105840            2769 :     iReg = iTarget;
  105841            2769 :     eType = sqlite3FindInIndex(pParse, pX, 0);
  105842            2769 :     iTab = pX->iTable;
  105843            2769 :     sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
  105844            2769 :     assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
  105845            2769 :     if( pLevel->u.in.nIn==0 ){
  105846            2769 :       pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  105847                 :     }
  105848            2769 :     pLevel->u.in.nIn++;
  105849            2769 :     pLevel->u.in.aInLoop =
  105850            2769 :        sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
  105851            2769 :                               sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
  105852            2769 :     pIn = pLevel->u.in.aInLoop;
  105853            2769 :     if( pIn ){
  105854            2769 :       pIn += pLevel->u.in.nIn - 1;
  105855            2769 :       pIn->iCur = iTab;
  105856            2769 :       if( eType==IN_INDEX_ROWID ){
  105857              99 :         pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
  105858                 :       }else{
  105859            2670 :         pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
  105860                 :       }
  105861            2769 :       sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
  105862                 :     }else{
  105863               0 :       pLevel->u.in.nIn = 0;
  105864                 :     }
  105865                 : #endif
  105866                 :   }
  105867           48904 :   disableTerm(pLevel, pTerm);
  105868           48904 :   return iReg;
  105869                 : }
  105870                 : 
  105871                 : /*
  105872                 : ** Generate code that will evaluate all == and IN constraints for an
  105873                 : ** index.
  105874                 : **
  105875                 : ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
  105876                 : ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
  105877                 : ** The index has as many as three equality constraints, but in this
  105878                 : ** example, the third "c" value is an inequality.  So only two 
  105879                 : ** constraints are coded.  This routine will generate code to evaluate
  105880                 : ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
  105881                 : ** in consecutive registers and the index of the first register is returned.
  105882                 : **
  105883                 : ** In the example above nEq==2.  But this subroutine works for any value
  105884                 : ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
  105885                 : ** The only thing it does is allocate the pLevel->iMem memory cell and
  105886                 : ** compute the affinity string.
  105887                 : **
  105888                 : ** This routine always allocates at least one memory cell and returns
  105889                 : ** the index of that memory cell. The code that
  105890                 : ** calls this routine will use that memory cell to store the termination
  105891                 : ** key value of the loop.  If one or more IN operators appear, then
  105892                 : ** this routine allocates an additional nEq memory cells for internal
  105893                 : ** use.
  105894                 : **
  105895                 : ** Before returning, *pzAff is set to point to a buffer containing a
  105896                 : ** copy of the column affinity string of the index allocated using
  105897                 : ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
  105898                 : ** with equality constraints that use NONE affinity are set to
  105899                 : ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
  105900                 : **
  105901                 : **   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
  105902                 : **   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
  105903                 : **
  105904                 : ** In the example above, the index on t1(a) has TEXT affinity. But since
  105905                 : ** the right hand side of the equality constraint (t2.b) has NONE affinity,
  105906                 : ** no conversion should be attempted before using a t2.b value as part of
  105907                 : ** a key to search the index. Hence the first byte in the returned affinity
  105908                 : ** string in this example would be set to SQLITE_AFF_NONE.
  105909                 : */
  105910           24396 : static int codeAllEqualityTerms(
  105911                 :   Parse *pParse,        /* Parsing context */
  105912                 :   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
  105913                 :   WhereClause *pWC,     /* The WHERE clause */
  105914                 :   Bitmask notReady,     /* Which parts of FROM have not yet been coded */
  105915                 :   int nExtraReg,        /* Number of extra registers to allocate */
  105916                 :   char **pzAff          /* OUT: Set to point to affinity string */
  105917                 : ){
  105918           24396 :   int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
  105919           24396 :   Vdbe *v = pParse->pVdbe;      /* The vm under construction */
  105920                 :   Index *pIdx;                  /* The index being used for this loop */
  105921           24396 :   int iCur = pLevel->iTabCur;   /* The cursor of the table */
  105922                 :   WhereTerm *pTerm;             /* A single constraint term */
  105923                 :   int j;                        /* Loop counter */
  105924                 :   int regBase;                  /* Base register */
  105925                 :   int nReg;                     /* Number of registers to allocate */
  105926                 :   char *zAff;                   /* Affinity string to return */
  105927                 : 
  105928                 :   /* This module is only called on query plans that use an index. */
  105929           24396 :   assert( pLevel->plan.wsFlags & WHERE_INDEXED );
  105930           24396 :   pIdx = pLevel->plan.u.pIdx;
  105931                 : 
  105932                 :   /* Figure out how many memory cells we will need then allocate them.
  105933                 :   */
  105934           24396 :   regBase = pParse->nMem + 1;
  105935           24396 :   nReg = pLevel->plan.nEq + nExtraReg;
  105936           24396 :   pParse->nMem += nReg;
  105937                 : 
  105938           24396 :   zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
  105939           24396 :   if( !zAff ){
  105940               0 :     pParse->db->mallocFailed = 1;
  105941                 :   }
  105942                 : 
  105943                 :   /* Evaluate the equality constraints
  105944                 :   */
  105945           24396 :   assert( pIdx->nColumn>=nEq );
  105946           50851 :   for(j=0; j<nEq; j++){
  105947                 :     int r1;
  105948           26455 :     int k = pIdx->aiColumn[j];
  105949           26455 :     pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
  105950           26455 :     if( NEVER(pTerm==0) ) break;
  105951                 :     /* The following true for indices with redundant columns. 
  105952                 :     ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  105953                 :     testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
  105954                 :     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  105955           26455 :     r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
  105956           26455 :     if( r1!=regBase+j ){
  105957           14216 :       if( nReg==1 ){
  105958           12018 :         sqlite3ReleaseTempReg(pParse, regBase);
  105959           12018 :         regBase = r1;
  105960                 :       }else{
  105961            2198 :         sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
  105962                 :       }
  105963                 :     }
  105964                 :     testcase( pTerm->eOperator & WO_ISNULL );
  105965                 :     testcase( pTerm->eOperator & WO_IN );
  105966           26455 :     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
  105967           24946 :       Expr *pRight = pTerm->pExpr->pRight;
  105968           24946 :       sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
  105969           24946 :       if( zAff ){
  105970           24946 :         if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
  105971             849 :           zAff[j] = SQLITE_AFF_NONE;
  105972                 :         }
  105973           24946 :         if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
  105974            3782 :           zAff[j] = SQLITE_AFF_NONE;
  105975                 :         }
  105976                 :       }
  105977                 :     }
  105978                 :   }
  105979           24396 :   *pzAff = zAff;
  105980           24396 :   return regBase;
  105981                 : }
  105982                 : 
  105983                 : #ifndef SQLITE_OMIT_EXPLAIN
  105984                 : /*
  105985                 : ** This routine is a helper for explainIndexRange() below
  105986                 : **
  105987                 : ** pStr holds the text of an expression that we are building up one term
  105988                 : ** at a time.  This routine adds a new term to the end of the expression.
  105989                 : ** Terms are separated by AND so add the "AND" text for second and subsequent
  105990                 : ** terms only.
  105991                 : */
  105992               0 : static void explainAppendTerm(
  105993                 :   StrAccum *pStr,             /* The text expression being built */
  105994                 :   int iTerm,                  /* Index of this term.  First is zero */
  105995                 :   const char *zColumn,        /* Name of the column */
  105996                 :   const char *zOp             /* Name of the operator */
  105997                 : ){
  105998               0 :   if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
  105999               0 :   sqlite3StrAccumAppend(pStr, zColumn, -1);
  106000               0 :   sqlite3StrAccumAppend(pStr, zOp, 1);
  106001               0 :   sqlite3StrAccumAppend(pStr, "?", 1);
  106002               0 : }
  106003                 : 
  106004                 : /*
  106005                 : ** Argument pLevel describes a strategy for scanning table pTab. This 
  106006                 : ** function returns a pointer to a string buffer containing a description
  106007                 : ** of the subset of table rows scanned by the strategy in the form of an
  106008                 : ** SQL expression. Or, if all rows are scanned, NULL is returned.
  106009                 : **
  106010                 : ** For example, if the query:
  106011                 : **
  106012                 : **   SELECT * FROM t1 WHERE a=1 AND b>2;
  106013                 : **
  106014                 : ** is run and there is an index on (a, b), then this function returns a
  106015                 : ** string similar to:
  106016                 : **
  106017                 : **   "a=? AND b>?"
  106018                 : **
  106019                 : ** The returned pointer points to memory obtained from sqlite3DbMalloc().
  106020                 : ** It is the responsibility of the caller to free the buffer when it is
  106021                 : ** no longer required.
  106022                 : */
  106023               0 : static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
  106024               0 :   WherePlan *pPlan = &pLevel->plan;
  106025               0 :   Index *pIndex = pPlan->u.pIdx;
  106026               0 :   int nEq = pPlan->nEq;
  106027                 :   int i, j;
  106028               0 :   Column *aCol = pTab->aCol;
  106029               0 :   int *aiColumn = pIndex->aiColumn;
  106030                 :   StrAccum txt;
  106031                 : 
  106032               0 :   if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
  106033               0 :     return 0;
  106034                 :   }
  106035               0 :   sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
  106036               0 :   txt.db = db;
  106037               0 :   sqlite3StrAccumAppend(&txt, " (", 2);
  106038               0 :   for(i=0; i<nEq; i++){
  106039               0 :     explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
  106040                 :   }
  106041                 : 
  106042               0 :   j = i;
  106043               0 :   if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
  106044               0 :     char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
  106045               0 :     explainAppendTerm(&txt, i++, z, ">");
  106046                 :   }
  106047               0 :   if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
  106048               0 :     char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
  106049               0 :     explainAppendTerm(&txt, i, z, "<");
  106050                 :   }
  106051               0 :   sqlite3StrAccumAppend(&txt, ")", 1);
  106052               0 :   return sqlite3StrAccumFinish(&txt);
  106053                 : }
  106054                 : 
  106055                 : /*
  106056                 : ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
  106057                 : ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
  106058                 : ** record is added to the output to describe the table scan strategy in 
  106059                 : ** pLevel.
  106060                 : */
  106061           85210 : static void explainOneScan(
  106062                 :   Parse *pParse,                  /* Parse context */
  106063                 :   SrcList *pTabList,              /* Table list this loop refers to */
  106064                 :   WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
  106065                 :   int iLevel,                     /* Value for "level" column of output */
  106066                 :   int iFrom,                      /* Value for "from" column of output */
  106067                 :   u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
  106068                 : ){
  106069           85210 :   if( pParse->explain==2 ){
  106070               0 :     u32 flags = pLevel->plan.wsFlags;
  106071               0 :     struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
  106072               0 :     Vdbe *v = pParse->pVdbe;      /* VM being constructed */
  106073               0 :     sqlite3 *db = pParse->db;     /* Database handle */
  106074                 :     char *zMsg;                   /* Text to add to EQP output */
  106075                 :     sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
  106076               0 :     int iId = pParse->iSelectId;  /* Select id (left-most output column) */
  106077                 :     int isSearch;                 /* True for a SEARCH. False for SCAN. */
  106078                 : 
  106079               0 :     if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
  106080                 : 
  106081               0 :     isSearch = (pLevel->plan.nEq>0)
  106082               0 :              || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
  106083               0 :              || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
  106084                 : 
  106085               0 :     zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
  106086               0 :     if( pItem->pSelect ){
  106087               0 :       zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
  106088                 :     }else{
  106089               0 :       zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
  106090                 :     }
  106091                 : 
  106092               0 :     if( pItem->zAlias ){
  106093               0 :       zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
  106094                 :     }
  106095               0 :     if( (flags & WHERE_INDEXED)!=0 ){
  106096               0 :       char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
  106097               0 :       zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, 
  106098               0 :           ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
  106099               0 :           ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
  106100               0 :           ((flags & WHERE_TEMP_INDEX)?"":" "),
  106101               0 :           ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
  106102                 :           zWhere
  106103                 :       );
  106104               0 :       sqlite3DbFree(db, zWhere);
  106105               0 :     }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
  106106               0 :       zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
  106107                 : 
  106108               0 :       if( flags&WHERE_ROWID_EQ ){
  106109               0 :         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
  106110               0 :       }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
  106111               0 :         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
  106112               0 :       }else if( flags&WHERE_BTM_LIMIT ){
  106113               0 :         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
  106114               0 :       }else if( flags&WHERE_TOP_LIMIT ){
  106115               0 :         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
  106116                 :       }
  106117                 :     }
  106118                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  106119               0 :     else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
  106120               0 :       sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
  106121               0 :       zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
  106122                 :                   pVtabIdx->idxNum, pVtabIdx->idxStr);
  106123                 :     }
  106124                 : #endif
  106125               0 :     if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
  106126                 :       testcase( wctrlFlags & WHERE_ORDERBY_MIN );
  106127               0 :       nRow = 1;
  106128                 :     }else{
  106129               0 :       nRow = (sqlite3_int64)pLevel->plan.nRow;
  106130                 :     }
  106131               0 :     zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
  106132               0 :     sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
  106133                 :   }
  106134                 : }
  106135                 : #else
  106136                 : # define explainOneScan(u,v,w,x,y,z)
  106137                 : #endif /* SQLITE_OMIT_EXPLAIN */
  106138                 : 
  106139                 : 
  106140                 : /*
  106141                 : ** Generate code for the start of the iLevel-th loop in the WHERE clause
  106142                 : ** implementation described by pWInfo.
  106143                 : */
  106144           85150 : static Bitmask codeOneLoopStart(
  106145                 :   WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
  106146                 :   int iLevel,          /* Which level of pWInfo->a[] should be coded */
  106147                 :   u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
  106148                 :   Bitmask notReady,    /* Which tables are currently available */
  106149                 :   Expr *pWhere         /* Complete WHERE clause */
  106150                 : ){
  106151                 :   int j, k;            /* Loop counters */
  106152                 :   int iCur;            /* The VDBE cursor for the table */
  106153                 :   int addrNxt;         /* Where to jump to continue with the next IN case */
  106154                 :   int omitTable;       /* True if we use the index only */
  106155                 :   int bRev;            /* True if we need to scan in reverse order */
  106156                 :   WhereLevel *pLevel;  /* The where level to be coded */
  106157                 :   WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
  106158                 :   WhereTerm *pTerm;               /* A WHERE clause term */
  106159                 :   Parse *pParse;                  /* Parsing context */
  106160                 :   Vdbe *v;                        /* The prepared stmt under constructions */
  106161                 :   struct SrcList_item *pTabItem;  /* FROM clause term being coded */
  106162                 :   int addrBrk;                    /* Jump here to break out of the loop */
  106163                 :   int addrCont;                   /* Jump here to continue with next cycle */
  106164           85150 :   int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
  106165           85150 :   int iReleaseReg = 0;      /* Temp register to free before returning */
  106166                 : 
  106167           85150 :   pParse = pWInfo->pParse;
  106168           85150 :   v = pParse->pVdbe;
  106169           85150 :   pWC = pWInfo->pWC;
  106170           85150 :   pLevel = &pWInfo->a[iLevel];
  106171           85150 :   pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
  106172           85150 :   iCur = pTabItem->iCursor;
  106173           85150 :   bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
  106174           97677 :   omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 
  106175           12579 :            && (wctrlFlags & WHERE_FORCE_TABLE)==0;
  106176                 : 
  106177                 :   /* Create labels for the "break" and "continue" instructions
  106178                 :   ** for the current loop.  Jump to addrBrk to break out of a loop.
  106179                 :   ** Jump to cont to go immediately to the next iteration of the
  106180                 :   ** loop.
  106181                 :   **
  106182                 :   ** When there is an IN operator, we also have a "addrNxt" label that
  106183                 :   ** means to continue with the next IN value combination.  When
  106184                 :   ** there are no IN operators in the constraints, the "addrNxt" label
  106185                 :   ** is the same as "addrBrk".
  106186                 :   */
  106187           85150 :   addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  106188           85150 :   addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
  106189                 : 
  106190                 :   /* If this is the right table of a LEFT OUTER JOIN, allocate and
  106191                 :   ** initialize a memory cell that records if this table matches any
  106192                 :   ** row of the left table of the join.
  106193                 :   */
  106194           85150 :   if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
  106195            2704 :     pLevel->iLeftJoin = ++pParse->nMem;
  106196            2704 :     sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
  106197            2704 :     VdbeComment((v, "init LEFT JOIN no-match flag"));
  106198                 :   }
  106199                 : 
  106200                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  106201           85150 :   if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  106202                 :     /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
  106203                 :     **          to access the data.
  106204                 :     */
  106205                 :     int iReg;   /* P3 Value for OP_VFilter */
  106206              54 :     sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
  106207              54 :     int nConstraint = pVtabIdx->nConstraint;
  106208              54 :     struct sqlite3_index_constraint_usage *aUsage =
  106209                 :                                                 pVtabIdx->aConstraintUsage;
  106210              54 :     const struct sqlite3_index_constraint *aConstraint =
  106211                 :                                                 pVtabIdx->aConstraint;
  106212                 : 
  106213              54 :     sqlite3ExprCachePush(pParse);
  106214              54 :     iReg = sqlite3GetTempRange(pParse, nConstraint+2);
  106215             107 :     for(j=1; j<=nConstraint; j++){
  106216              53 :       for(k=0; k<nConstraint; k++){
  106217              53 :         if( aUsage[k].argvIndex==j ){
  106218              53 :           int iTerm = aConstraint[k].iTermOffset;
  106219              53 :           sqlite3ExprCode(pParse, pWC->a[iTerm].pExpr->pRight, iReg+j+1);
  106220              53 :           break;
  106221                 :         }
  106222                 :       }
  106223              53 :       if( k==nConstraint ) break;
  106224                 :     }
  106225              54 :     sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
  106226              54 :     sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
  106227              54 :     sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr,
  106228              54 :                       pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
  106229              54 :     pVtabIdx->needToFreeIdxStr = 0;
  106230             107 :     for(j=0; j<nConstraint; j++){
  106231              53 :       if( aUsage[j].omit ){
  106232               1 :         int iTerm = aConstraint[j].iTermOffset;
  106233               1 :         disableTerm(pLevel, &pWC->a[iTerm]);
  106234                 :       }
  106235                 :     }
  106236              54 :     pLevel->op = OP_VNext;
  106237              54 :     pLevel->p1 = iCur;
  106238              54 :     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  106239              54 :     sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
  106240              54 :     sqlite3ExprCachePop(pParse, 1);
  106241                 :   }else
  106242                 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
  106243                 : 
  106244           85096 :   if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
  106245                 :     /* Case 1:  We can directly reference a single row using an
  106246                 :     **          equality comparison against the ROWID field.  Or
  106247                 :     **          we reference multiple rows using a "rowid IN (...)"
  106248                 :     **          construct.
  106249                 :     */
  106250           22449 :     iReleaseReg = sqlite3GetTempReg(pParse);
  106251           22449 :     pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
  106252           22449 :     assert( pTerm!=0 );
  106253           22449 :     assert( pTerm->pExpr!=0 );
  106254           22449 :     assert( pTerm->leftCursor==iCur );
  106255           22449 :     assert( omitTable==0 );
  106256                 :     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  106257           22449 :     iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg);
  106258           22449 :     addrNxt = pLevel->addrNxt;
  106259           22449 :     sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
  106260           22449 :     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
  106261           22449 :     sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  106262           22449 :     VdbeComment((v, "pk"));
  106263           22449 :     pLevel->op = OP_Noop;
  106264           62647 :   }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
  106265                 :     /* Case 2:  We have an inequality comparison against the ROWID field.
  106266                 :     */
  106267           20091 :     int testOp = OP_Noop;
  106268                 :     int start;
  106269           20091 :     int memEndValue = 0;
  106270                 :     WhereTerm *pStart, *pEnd;
  106271                 : 
  106272           20091 :     assert( omitTable==0 );
  106273           20091 :     pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
  106274           20091 :     pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
  106275           20091 :     if( bRev ){
  106276             779 :       pTerm = pStart;
  106277             779 :       pStart = pEnd;
  106278             779 :       pEnd = pTerm;
  106279                 :     }
  106280           20091 :     if( pStart ){
  106281                 :       Expr *pX;             /* The expression that defines the start bound */
  106282                 :       int r1, rTemp;        /* Registers for holding the start boundary */
  106283                 : 
  106284                 :       /* The following constant maps TK_xx codes into corresponding 
  106285                 :       ** seek opcodes.  It depends on a particular ordering of TK_xx
  106286                 :       */
  106287             588 :       const u8 aMoveOp[] = {
  106288                 :            /* TK_GT */  OP_SeekGt,
  106289                 :            /* TK_LE */  OP_SeekLe,
  106290                 :            /* TK_LT */  OP_SeekLt,
  106291                 :            /* TK_GE */  OP_SeekGe
  106292                 :       };
  106293                 :       assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
  106294                 :       assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
  106295                 :       assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
  106296                 : 
  106297                 :       testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  106298             588 :       pX = pStart->pExpr;
  106299             588 :       assert( pX!=0 );
  106300             588 :       assert( pStart->leftCursor==iCur );
  106301             588 :       r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
  106302             588 :       sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
  106303             588 :       VdbeComment((v, "pk"));
  106304             588 :       sqlite3ExprCacheAffinityChange(pParse, r1, 1);
  106305             588 :       sqlite3ReleaseTempReg(pParse, rTemp);
  106306             588 :       disableTerm(pLevel, pStart);
  106307                 :     }else{
  106308           19503 :       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
  106309                 :     }
  106310           20091 :     if( pEnd ){
  106311                 :       Expr *pX;
  106312               0 :       pX = pEnd->pExpr;
  106313               0 :       assert( pX!=0 );
  106314               0 :       assert( pEnd->leftCursor==iCur );
  106315                 :       testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  106316               0 :       memEndValue = ++pParse->nMem;
  106317               0 :       sqlite3ExprCode(pParse, pX->pRight, memEndValue);
  106318               0 :       if( pX->op==TK_LT || pX->op==TK_GT ){
  106319               0 :         testOp = bRev ? OP_Le : OP_Ge;
  106320                 :       }else{
  106321               0 :         testOp = bRev ? OP_Lt : OP_Gt;
  106322                 :       }
  106323               0 :       disableTerm(pLevel, pEnd);
  106324                 :     }
  106325           20091 :     start = sqlite3VdbeCurrentAddr(v);
  106326           20091 :     pLevel->op = bRev ? OP_Prev : OP_Next;
  106327           20091 :     pLevel->p1 = iCur;
  106328           20091 :     pLevel->p2 = start;
  106329           20091 :     if( pStart==0 && pEnd==0 ){
  106330           19503 :       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  106331                 :     }else{
  106332             588 :       assert( pLevel->p5==0 );
  106333                 :     }
  106334           20091 :     if( testOp!=OP_Noop ){
  106335               0 :       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
  106336               0 :       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
  106337               0 :       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  106338               0 :       sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
  106339               0 :       sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
  106340                 :     }
  106341           42556 :   }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
  106342                 :     /* Case 3: A scan using an index.
  106343                 :     **
  106344                 :     **         The WHERE clause may contain zero or more equality 
  106345                 :     **         terms ("==" or "IN" operators) that refer to the N
  106346                 :     **         left-most columns of the index. It may also contain
  106347                 :     **         inequality constraints (>, <, >= or <=) on the indexed
  106348                 :     **         column that immediately follows the N equalities. Only 
  106349                 :     **         the right-most column can be an inequality - the rest must
  106350                 :     **         use the "==" and "IN" operators. For example, if the 
  106351                 :     **         index is on (x,y,z), then the following clauses are all 
  106352                 :     **         optimized:
  106353                 :     **
  106354                 :     **            x=5
  106355                 :     **            x=5 AND y=10
  106356                 :     **            x=5 AND y<10
  106357                 :     **            x=5 AND y>5 AND y<10
  106358                 :     **            x=5 AND y=5 AND z<=10
  106359                 :     **
  106360                 :     **         The z<10 term of the following cannot be used, only
  106361                 :     **         the x=5 term:
  106362                 :     **
  106363                 :     **            x=5 AND z<10
  106364                 :     **
  106365                 :     **         N may be zero if there are inequality constraints.
  106366                 :     **         If there are no inequality constraints, then N is at
  106367                 :     **         least one.
  106368                 :     **
  106369                 :     **         This case is also used when there are no WHERE clause
  106370                 :     **         constraints but an index is selected anyway, in order
  106371                 :     **         to force the output order to conform to an ORDER BY.
  106372                 :     */  
  106373                 :     static const u8 aStartOp[] = {
  106374                 :       0,
  106375                 :       0,
  106376                 :       OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
  106377                 :       OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
  106378                 :       OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
  106379                 :       OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
  106380                 :       OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
  106381                 :       OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
  106382                 :     };
  106383                 :     static const u8 aEndOp[] = {
  106384                 :       OP_Noop,             /* 0: (!end_constraints) */
  106385                 :       OP_IdxGE,            /* 1: (end_constraints && !bRev) */
  106386                 :       OP_IdxLT             /* 2: (end_constraints && bRev) */
  106387                 :     };
  106388           24396 :     int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
  106389           24396 :     int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
  106390                 :     int regBase;                 /* Base register holding constraint values */
  106391                 :     int r1;                      /* Temp register */
  106392           24396 :     WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
  106393           24396 :     WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
  106394                 :     int startEq;                 /* True if range start uses ==, >= or <= */
  106395                 :     int endEq;                   /* True if range end uses ==, >= or <= */
  106396                 :     int start_constraints;       /* Start of range is constrained */
  106397                 :     int nConstraint;             /* Number of constraint terms */
  106398                 :     Index *pIdx;                 /* The index we will be using */
  106399                 :     int iIdxCur;                 /* The VDBE cursor for the index */
  106400           24396 :     int nExtraReg = 0;           /* Number of extra registers needed */
  106401                 :     int op;                      /* Instruction opcode */
  106402                 :     char *zStartAff;             /* Affinity for start of range constraint */
  106403                 :     char *zEndAff;               /* Affinity for end of range constraint */
  106404                 : 
  106405           24396 :     pIdx = pLevel->plan.u.pIdx;
  106406           24396 :     iIdxCur = pLevel->iIdxCur;
  106407           24396 :     k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
  106408                 : 
  106409                 :     /* If this loop satisfies a sort order (pOrderBy) request that 
  106410                 :     ** was passed to this function to implement a "SELECT min(x) ..." 
  106411                 :     ** query, then the caller will only allow the loop to run for
  106412                 :     ** a single iteration. This means that the first row returned
  106413                 :     ** should not have a NULL value stored in 'x'. If column 'x' is
  106414                 :     ** the first one after the nEq equality constraints in the index,
  106415                 :     ** this requires some special handling.
  106416                 :     */
  106417           24396 :     if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
  106418              26 :      && (pLevel->plan.wsFlags&WHERE_ORDERBY)
  106419              26 :      && (pIdx->nColumn>nEq)
  106420                 :     ){
  106421                 :       /* assert( pOrderBy->nExpr==1 ); */
  106422                 :       /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
  106423               0 :       isMinQuery = 1;
  106424               0 :       nExtraReg = 1;
  106425                 :     }
  106426                 : 
  106427                 :     /* Find any inequality constraint terms for the start and end 
  106428                 :     ** of the range. 
  106429                 :     */
  106430           24396 :     if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
  106431             524 :       pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
  106432             524 :       nExtraReg = 1;
  106433                 :     }
  106434           24396 :     if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
  106435             539 :       pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
  106436             539 :       nExtraReg = 1;
  106437                 :     }
  106438                 : 
  106439                 :     /* Generate code to evaluate all constraint terms using == or IN
  106440                 :     ** and store the values of those terms in an array of registers
  106441                 :     ** starting at regBase.
  106442                 :     */
  106443           24396 :     regBase = codeAllEqualityTerms(
  106444                 :         pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
  106445                 :     );
  106446           24396 :     zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
  106447           24396 :     addrNxt = pLevel->addrNxt;
  106448                 : 
  106449                 :     /* If we are doing a reverse order scan on an ascending index, or
  106450                 :     ** a forward order scan on a descending index, interchange the 
  106451                 :     ** start and end terms (pRangeStart and pRangeEnd).
  106452                 :     */
  106453           24396 :     if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
  106454           23216 :      || (bRev && pIdx->nColumn==nEq)
  106455                 :     ){
  106456            1180 :       SWAP(WhereTerm *, pRangeEnd, pRangeStart);
  106457                 :     }
  106458                 : 
  106459                 :     testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
  106460                 :     testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
  106461                 :     testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
  106462                 :     testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
  106463           24396 :     startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
  106464           24396 :     endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
  106465           24396 :     start_constraints = pRangeStart || nEq>0;
  106466                 : 
  106467                 :     /* Seek the index cursor to the start of the range. */
  106468           24396 :     nConstraint = nEq;
  106469           24396 :     if( pRangeStart ){
  106470             561 :       Expr *pRight = pRangeStart->pExpr->pRight;
  106471             561 :       sqlite3ExprCode(pParse, pRight, regBase+nEq);
  106472                 :       if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
  106473             561 :         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
  106474                 :       }
  106475             561 :       if( zStartAff ){
  106476             561 :         if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
  106477                 :           /* Since the comparison is to be performed with no conversions
  106478                 :           ** applied to the operands, set the affinity to apply to pRight to 
  106479                 :           ** SQLITE_AFF_NONE.  */
  106480             148 :           zStartAff[nEq] = SQLITE_AFF_NONE;
  106481                 :         }
  106482             561 :         if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
  106483             325 :           zStartAff[nEq] = SQLITE_AFF_NONE;
  106484                 :         }
  106485                 :       }  
  106486             561 :       nConstraint++;
  106487                 :       testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  106488           23835 :     }else if( isMinQuery ){
  106489               0 :       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  106490               0 :       nConstraint++;
  106491               0 :       startEq = 0;
  106492               0 :       start_constraints = 1;
  106493                 :     }
  106494           24396 :     codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
  106495           24396 :     op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
  106496           24396 :     assert( op!=0 );
  106497                 :     testcase( op==OP_Rewind );
  106498                 :     testcase( op==OP_Last );
  106499                 :     testcase( op==OP_SeekGt );
  106500                 :     testcase( op==OP_SeekGe );
  106501                 :     testcase( op==OP_SeekLe );
  106502                 :     testcase( op==OP_SeekLt );
  106503           24396 :     sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
  106504                 : 
  106505                 :     /* Load the value for the inequality constraint at the end of the
  106506                 :     ** range (if any).
  106507                 :     */
  106508           24396 :     nConstraint = nEq;
  106509           24396 :     if( pRangeEnd ){
  106510             502 :       Expr *pRight = pRangeEnd->pExpr->pRight;
  106511             502 :       sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
  106512             502 :       sqlite3ExprCode(pParse, pRight, regBase+nEq);
  106513                 :       if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
  106514             502 :         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
  106515                 :       }
  106516             502 :       if( zEndAff ){
  106517             502 :         if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
  106518                 :           /* Since the comparison is to be performed with no conversions
  106519                 :           ** applied to the operands, set the affinity to apply to pRight to 
  106520                 :           ** SQLITE_AFF_NONE.  */
  106521              90 :           zEndAff[nEq] = SQLITE_AFF_NONE;
  106522                 :         }
  106523             502 :         if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
  106524             258 :           zEndAff[nEq] = SQLITE_AFF_NONE;
  106525                 :         }
  106526                 :       }  
  106527             502 :       codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
  106528             502 :       nConstraint++;
  106529                 :       testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  106530                 :     }
  106531           24396 :     sqlite3DbFree(pParse->db, zStartAff);
  106532           24396 :     sqlite3DbFree(pParse->db, zEndAff);
  106533                 : 
  106534                 :     /* Top of the loop body */
  106535           24396 :     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  106536                 : 
  106537                 :     /* Check if the index cursor is past the end of the range. */
  106538           24396 :     op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
  106539                 :     testcase( op==OP_Noop );
  106540                 :     testcase( op==OP_IdxGE );
  106541                 :     testcase( op==OP_IdxLT );
  106542           24396 :     if( op!=OP_Noop ){
  106543           23903 :       sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
  106544           23903 :       sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
  106545                 :     }
  106546                 : 
  106547                 :     /* If there are inequality constraints, check that the value
  106548                 :     ** of the table column that the inequality contrains is not NULL.
  106549                 :     ** If it is, jump to the next iteration of the loop.
  106550                 :     */
  106551           24396 :     r1 = sqlite3GetTempReg(pParse);
  106552                 :     testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
  106553                 :     testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
  106554           24396 :     if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
  106555             617 :       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
  106556             617 :       sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
  106557                 :     }
  106558           24396 :     sqlite3ReleaseTempReg(pParse, r1);
  106559                 : 
  106560                 :     /* Seek the table cursor, if required */
  106561           24396 :     disableTerm(pLevel, pRangeStart);
  106562           24396 :     disableTerm(pLevel, pRangeEnd);
  106563           24396 :     if( !omitTable ){
  106564           11869 :       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
  106565           11869 :       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
  106566           11869 :       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  106567           11869 :       sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
  106568                 :     }
  106569                 : 
  106570                 :     /* Record the instruction used to terminate the loop. Disable 
  106571                 :     ** WHERE clause terms made redundant by the index range scan.
  106572                 :     */
  106573           24396 :     if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
  106574            4972 :       pLevel->op = OP_Noop;
  106575           19424 :     }else if( bRev ){
  106576            1180 :       pLevel->op = OP_Prev;
  106577                 :     }else{
  106578           18244 :       pLevel->op = OP_Next;
  106579                 :     }
  106580           24396 :     pLevel->p1 = iIdxCur;
  106581                 :   }else
  106582                 : 
  106583                 : #ifndef SQLITE_OMIT_OR_OPTIMIZATION
  106584           18160 :   if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
  106585                 :     /* Case 4:  Two or more separately indexed terms connected by OR
  106586                 :     **
  106587                 :     ** Example:
  106588                 :     **
  106589                 :     **   CREATE TABLE t1(a,b,c,d);
  106590                 :     **   CREATE INDEX i1 ON t1(a);
  106591                 :     **   CREATE INDEX i2 ON t1(b);
  106592                 :     **   CREATE INDEX i3 ON t1(c);
  106593                 :     **
  106594                 :     **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
  106595                 :     **
  106596                 :     ** In the example, there are three indexed terms connected by OR.
  106597                 :     ** The top of the loop looks like this:
  106598                 :     **
  106599                 :     **          Null       1                # Zero the rowset in reg 1
  106600                 :     **
  106601                 :     ** Then, for each indexed term, the following. The arguments to
  106602                 :     ** RowSetTest are such that the rowid of the current row is inserted
  106603                 :     ** into the RowSet. If it is already present, control skips the
  106604                 :     ** Gosub opcode and jumps straight to the code generated by WhereEnd().
  106605                 :     **
  106606                 :     **        sqlite3WhereBegin(<term>)
  106607                 :     **          RowSetTest                  # Insert rowid into rowset
  106608                 :     **          Gosub      2 A
  106609                 :     **        sqlite3WhereEnd()
  106610                 :     **
  106611                 :     ** Following the above, code to terminate the loop. Label A, the target
  106612                 :     ** of the Gosub above, jumps to the instruction right after the Goto.
  106613                 :     **
  106614                 :     **          Null       1                # Zero the rowset in reg 1
  106615                 :     **          Goto       B                # The loop is finished.
  106616                 :     **
  106617                 :     **       A: <loop body>                 # Return data, whatever.
  106618                 :     **
  106619                 :     **          Return     2                # Jump back to the Gosub
  106620                 :     **
  106621                 :     **       B: <after the loop>
  106622                 :     **
  106623                 :     */
  106624                 :     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
  106625                 :     SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
  106626                 : 
  106627              30 :     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
  106628              30 :     int regRowset = 0;                        /* Register for RowSet object */
  106629              30 :     int regRowid = 0;                         /* Register holding rowid */
  106630              30 :     int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
  106631                 :     int iRetInit;                             /* Address of regReturn init */
  106632              30 :     int untestedTerms = 0;             /* Some terms not completely tested */
  106633                 :     int ii;                            /* Loop counter */
  106634              30 :     Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
  106635                 :    
  106636              30 :     pTerm = pLevel->plan.u.pTerm;
  106637              30 :     assert( pTerm!=0 );
  106638              30 :     assert( pTerm->eOperator==WO_OR );
  106639              30 :     assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
  106640              30 :     pOrWc = &pTerm->u.pOrInfo->wc;
  106641              30 :     pLevel->op = OP_Return;
  106642              30 :     pLevel->p1 = regReturn;
  106643                 : 
  106644                 :     /* Set up a new SrcList ni pOrTab containing the table being scanned
  106645                 :     ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
  106646                 :     ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
  106647                 :     */
  106648              30 :     if( pWInfo->nLevel>1 ){
  106649                 :       int nNotReady;                 /* The number of notReady tables */
  106650                 :       struct SrcList_item *origSrc;     /* Original list of tables */
  106651               2 :       nNotReady = pWInfo->nLevel - iLevel - 1;
  106652               2 :       pOrTab = sqlite3StackAllocRaw(pParse->db,
  106653                 :                             sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
  106654               2 :       if( pOrTab==0 ) return notReady;
  106655               2 :       pOrTab->nAlloc = (i16)(nNotReady + 1);
  106656               2 :       pOrTab->nSrc = pOrTab->nAlloc;
  106657               2 :       memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
  106658               2 :       origSrc = pWInfo->pTabList->a;
  106659               4 :       for(k=1; k<=nNotReady; k++){
  106660               2 :         memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
  106661                 :       }
  106662                 :     }else{
  106663              28 :       pOrTab = pWInfo->pTabList;
  106664                 :     }
  106665                 : 
  106666                 :     /* Initialize the rowset register to contain NULL. An SQL NULL is 
  106667                 :     ** equivalent to an empty rowset.
  106668                 :     **
  106669                 :     ** Also initialize regReturn to contain the address of the instruction 
  106670                 :     ** immediately following the OP_Return at the bottom of the loop. This
  106671                 :     ** is required in a few obscure LEFT JOIN cases where control jumps
  106672                 :     ** over the top of the loop into the body of it. In this case the 
  106673                 :     ** correct response for the end-of-loop code (the OP_Return) is to 
  106674                 :     ** fall through to the next instruction, just as an OP_Next does if
  106675                 :     ** called on an uninitialized cursor.
  106676                 :     */
  106677              30 :     if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  106678              30 :       regRowset = ++pParse->nMem;
  106679              30 :       regRowid = ++pParse->nMem;
  106680              30 :       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
  106681                 :     }
  106682              30 :     iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
  106683                 : 
  106684                 :     /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
  106685                 :     ** Then for every term xN, evaluate as the subexpression: xN AND z
  106686                 :     ** That way, terms in y that are factored into the disjunction will
  106687                 :     ** be picked up by the recursive calls to sqlite3WhereBegin() below.
  106688                 :     */
  106689              30 :     if( pWC->nTerm>1 ){
  106690               2 :       pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0);
  106691               2 :       pAndExpr->pRight = pWhere;
  106692                 :     }
  106693                 : 
  106694              90 :     for(ii=0; ii<pOrWc->nTerm; ii++){
  106695              60 :       WhereTerm *pOrTerm = &pOrWc->a[ii];
  106696              60 :       if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
  106697                 :         WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
  106698              60 :         Expr *pOrExpr = pOrTerm->pExpr;
  106699              60 :         if( pAndExpr ){
  106700               4 :           pAndExpr->pLeft = pOrExpr;
  106701               4 :           pOrExpr = pAndExpr;
  106702                 :         }
  106703                 :         /* Loop through table entries that match term pOrTerm. */
  106704              60 :         pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  106705                 :                         WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
  106706                 :                         WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
  106707              60 :         if( pSubWInfo ){
  106708              60 :           explainOneScan(
  106709              60 :               pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  106710                 :           );
  106711              60 :           if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  106712              60 :             int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  106713                 :             int r;
  106714              60 :             r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
  106715                 :                                          regRowid);
  106716              60 :             sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
  106717              60 :                                  sqlite3VdbeCurrentAddr(v)+2, r, iSet);
  106718                 :           }
  106719              60 :           sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
  106720                 : 
  106721                 :           /* The pSubWInfo->untestedTerms flag means that this OR term
  106722                 :           ** contained one or more AND term from a notReady table.  The
  106723                 :           ** terms from the notReady table could not be tested and will
  106724                 :           ** need to be tested later.
  106725                 :           */
  106726              60 :           if( pSubWInfo->untestedTerms ) untestedTerms = 1;
  106727                 : 
  106728                 :           /* Finish the loop through table entries that match term pOrTerm. */
  106729              60 :           sqlite3WhereEnd(pSubWInfo);
  106730                 :         }
  106731                 :       }
  106732                 :     }
  106733              30 :     sqlite3DbFree(pParse->db, pAndExpr);
  106734              30 :     sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
  106735              30 :     sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
  106736              30 :     sqlite3VdbeResolveLabel(v, iLoopBody);
  106737                 : 
  106738              30 :     if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
  106739              30 :     if( !untestedTerms ) disableTerm(pLevel, pTerm);
  106740                 :   }else
  106741                 : #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  106742                 : 
  106743                 :   {
  106744                 :     /* Case 5:  There is no usable index.  We must do a complete
  106745                 :     **          scan of the entire table.
  106746                 :     */
  106747                 :     static const u8 aStep[] = { OP_Next, OP_Prev };
  106748                 :     static const u8 aStart[] = { OP_Rewind, OP_Last };
  106749           18130 :     assert( bRev==0 || bRev==1 );
  106750           18130 :     assert( omitTable==0 );
  106751           18130 :     pLevel->op = aStep[bRev];
  106752           18130 :     pLevel->p1 = iCur;
  106753           18130 :     pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
  106754           18130 :     pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  106755                 :   }
  106756           85150 :   notReady &= ~getMask(pWC->pMaskSet, iCur);
  106757                 : 
  106758                 :   /* Insert code to test every subexpression that can be completely
  106759                 :   ** computed using the current set of tables.
  106760                 :   **
  106761                 :   ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
  106762                 :   ** the use of indices become tests that are evaluated against each row of
  106763                 :   ** the relevant input tables.
  106764                 :   */
  106765          210695 :   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  106766                 :     Expr *pE;
  106767                 :     testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
  106768                 :     testcase( pTerm->wtFlags & TERM_CODED );
  106769          125545 :     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  106770           53411 :     if( (pTerm->prereqAll & notReady)!=0 ){
  106771                 :       testcase( pWInfo->untestedTerms==0
  106772                 :                && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
  106773            9109 :       pWInfo->untestedTerms = 1;
  106774            9109 :       continue;
  106775                 :     }
  106776           44302 :     pE = pTerm->pExpr;
  106777           44302 :     assert( pE!=0 );
  106778           44302 :     if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
  106779            1440 :       continue;
  106780                 :     }
  106781           42862 :     sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
  106782           42862 :     pTerm->wtFlags |= TERM_CODED;
  106783                 :   }
  106784                 : 
  106785                 :   /* For a LEFT OUTER JOIN, generate code that will record the fact that
  106786                 :   ** at least one row of the right table has matched the left table.  
  106787                 :   */
  106788           85150 :   if( pLevel->iLeftJoin ){
  106789            2704 :     pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
  106790            2704 :     sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
  106791            2704 :     VdbeComment((v, "record LEFT JOIN hit"));
  106792            2704 :     sqlite3ExprCacheClear(pParse);
  106793           15094 :     for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
  106794                 :       testcase( pTerm->wtFlags & TERM_VIRTUAL );  /* IMP: R-30575-11662 */
  106795                 :       testcase( pTerm->wtFlags & TERM_CODED );
  106796           12390 :       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  106797            2122 :       if( (pTerm->prereqAll & notReady)!=0 ){
  106798             682 :         assert( pWInfo->untestedTerms );
  106799             682 :         continue;
  106800                 :       }
  106801            1440 :       assert( pTerm->pExpr );
  106802            1440 :       sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
  106803            1440 :       pTerm->wtFlags |= TERM_CODED;
  106804                 :     }
  106805                 :   }
  106806           85150 :   sqlite3ReleaseTempReg(pParse, iReleaseReg);
  106807                 : 
  106808           85150 :   return notReady;
  106809                 : }
  106810                 : 
  106811                 : #if defined(SQLITE_TEST)
  106812                 : /*
  106813                 : ** The following variable holds a text description of query plan generated
  106814                 : ** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
  106815                 : ** overwrites the previous.  This information is used for testing and
  106816                 : ** analysis only.
  106817                 : */
  106818                 : SQLITE_API char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
  106819                 : static int nQPlan = 0;              /* Next free slow in _query_plan[] */
  106820                 : 
  106821                 : #endif /* SQLITE_TEST */
  106822                 : 
  106823                 : 
  106824                 : /*
  106825                 : ** Free a WhereInfo structure
  106826                 : */
  106827           81398 : static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
  106828           81398 :   if( ALWAYS(pWInfo) ){
  106829                 :     int i;
  106830          166548 :     for(i=0; i<pWInfo->nLevel; i++){
  106831           85150 :       sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
  106832           85150 :       if( pInfo ){
  106833                 :         /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
  106834              54 :         if( pInfo->needToFreeIdxStr ){
  106835               0 :           sqlite3_free(pInfo->idxStr);
  106836                 :         }
  106837              54 :         sqlite3DbFree(db, pInfo);
  106838                 :       }
  106839           85150 :       if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
  106840             207 :         Index *pIdx = pWInfo->a[i].plan.u.pIdx;
  106841             207 :         if( pIdx ){
  106842             207 :           sqlite3DbFree(db, pIdx->zColAff);
  106843             207 :           sqlite3DbFree(db, pIdx);
  106844                 :         }
  106845                 :       }
  106846                 :     }
  106847           81398 :     whereClauseClear(pWInfo->pWC);
  106848           81398 :     sqlite3DbFree(db, pWInfo);
  106849                 :   }
  106850           81398 : }
  106851                 : 
  106852                 : 
  106853                 : /*
  106854                 : ** Generate the beginning of the loop used for WHERE clause processing.
  106855                 : ** The return value is a pointer to an opaque structure that contains
  106856                 : ** information needed to terminate the loop.  Later, the calling routine
  106857                 : ** should invoke sqlite3WhereEnd() with the return value of this function
  106858                 : ** in order to complete the WHERE clause processing.
  106859                 : **
  106860                 : ** If an error occurs, this routine returns NULL.
  106861                 : **
  106862                 : ** The basic idea is to do a nested loop, one loop for each table in
  106863                 : ** the FROM clause of a select.  (INSERT and UPDATE statements are the
  106864                 : ** same as a SELECT with only a single table in the FROM clause.)  For
  106865                 : ** example, if the SQL is this:
  106866                 : **
  106867                 : **       SELECT * FROM t1, t2, t3 WHERE ...;
  106868                 : **
  106869                 : ** Then the code generated is conceptually like the following:
  106870                 : **
  106871                 : **      foreach row1 in t1 do       \    Code generated
  106872                 : **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
  106873                 : **          foreach row3 in t3 do   /
  106874                 : **            ...
  106875                 : **          end                     \    Code generated
  106876                 : **        end                        |-- by sqlite3WhereEnd()
  106877                 : **      end                         /
  106878                 : **
  106879                 : ** Note that the loops might not be nested in the order in which they
  106880                 : ** appear in the FROM clause if a different order is better able to make
  106881                 : ** use of indices.  Note also that when the IN operator appears in
  106882                 : ** the WHERE clause, it might result in additional nested loops for
  106883                 : ** scanning through all values on the right-hand side of the IN.
  106884                 : **
  106885                 : ** There are Btree cursors associated with each table.  t1 uses cursor
  106886                 : ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
  106887                 : ** And so forth.  This routine generates code to open those VDBE cursors
  106888                 : ** and sqlite3WhereEnd() generates the code to close them.
  106889                 : **
  106890                 : ** The code that sqlite3WhereBegin() generates leaves the cursors named
  106891                 : ** in pTabList pointing at their appropriate entries.  The [...] code
  106892                 : ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
  106893                 : ** data from the various tables of the loop.
  106894                 : **
  106895                 : ** If the WHERE clause is empty, the foreach loops must each scan their
  106896                 : ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
  106897                 : ** the tables have indices and there are terms in the WHERE clause that
  106898                 : ** refer to those indices, a complete table scan can be avoided and the
  106899                 : ** code will run much faster.  Most of the work of this routine is checking
  106900                 : ** to see if there are indices that can be used to speed up the loop.
  106901                 : **
  106902                 : ** Terms of the WHERE clause are also used to limit which rows actually
  106903                 : ** make it to the "..." in the middle of the loop.  After each "foreach",
  106904                 : ** terms of the WHERE clause that use only terms in that loop and outer
  106905                 : ** loops are evaluated and if false a jump is made around all subsequent
  106906                 : ** inner loops (or around the "..." if the test occurs within the inner-
  106907                 : ** most loop)
  106908                 : **
  106909                 : ** OUTER JOINS
  106910                 : **
  106911                 : ** An outer join of tables t1 and t2 is conceptally coded as follows:
  106912                 : **
  106913                 : **    foreach row1 in t1 do
  106914                 : **      flag = 0
  106915                 : **      foreach row2 in t2 do
  106916                 : **        start:
  106917                 : **          ...
  106918                 : **          flag = 1
  106919                 : **      end
  106920                 : **      if flag==0 then
  106921                 : **        move the row2 cursor to a null row
  106922                 : **        goto start
  106923                 : **      fi
  106924                 : **    end
  106925                 : **
  106926                 : ** ORDER BY CLAUSE PROCESSING
  106927                 : **
  106928                 : ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
  106929                 : ** if there is one.  If there is no ORDER BY clause or if this routine
  106930                 : ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
  106931                 : **
  106932                 : ** If an index can be used so that the natural output order of the table
  106933                 : ** scan is correct for the ORDER BY clause, then that index is used and
  106934                 : ** *ppOrderBy is set to NULL.  This is an optimization that prevents an
  106935                 : ** unnecessary sort of the result set if an index appropriate for the
  106936                 : ** ORDER BY clause already exists.
  106937                 : **
  106938                 : ** If the where clause loops cannot be arranged to provide the correct
  106939                 : ** output order, then the *ppOrderBy is unchanged.
  106940                 : */
  106941           81398 : SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
  106942                 :   Parse *pParse,        /* The parser context */
  106943                 :   SrcList *pTabList,    /* A list of all tables to be scanned */
  106944                 :   Expr *pWhere,         /* The WHERE clause */
  106945                 :   ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
  106946                 :   ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
  106947                 :   u16 wctrlFlags        /* One of the WHERE_* flags defined in sqliteInt.h */
  106948                 : ){
  106949                 :   int i;                     /* Loop counter */
  106950                 :   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
  106951                 :   int nTabList;              /* Number of elements in pTabList */
  106952                 :   WhereInfo *pWInfo;         /* Will become the return value of this function */
  106953           81398 :   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
  106954                 :   Bitmask notReady;          /* Cursors that are not yet positioned */
  106955                 :   WhereMaskSet *pMaskSet;    /* The expression mask set */
  106956                 :   WhereClause *pWC;               /* Decomposition of the WHERE clause */
  106957                 :   struct SrcList_item *pTabItem;  /* A single entry from pTabList */
  106958                 :   WhereLevel *pLevel;             /* A single level in the pWInfo list */
  106959                 :   int iFrom;                      /* First unused FROM clause element */
  106960                 :   int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
  106961                 :   sqlite3 *db;               /* Database connection */
  106962                 : 
  106963                 :   /* The number of tables in the FROM clause is limited by the number of
  106964                 :   ** bits in a Bitmask 
  106965                 :   */
  106966                 :   testcase( pTabList->nSrc==BMS );
  106967           81398 :   if( pTabList->nSrc>BMS ){
  106968               0 :     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
  106969               0 :     return 0;
  106970                 :   }
  106971                 : 
  106972                 :   /* This function normally generates a nested loop for all tables in 
  106973                 :   ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
  106974                 :   ** only generate code for the first table in pTabList and assume that
  106975                 :   ** any cursors associated with subsequent tables are uninitialized.
  106976                 :   */
  106977           81398 :   nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
  106978                 : 
  106979                 :   /* Allocate and initialize the WhereInfo structure that will become the
  106980                 :   ** return value. A single allocation is used to store the WhereInfo
  106981                 :   ** struct, the contents of WhereInfo.a[], the WhereClause structure
  106982                 :   ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
  106983                 :   ** field (type Bitmask) it must be aligned on an 8-byte boundary on
  106984                 :   ** some architectures. Hence the ROUND8() below.
  106985                 :   */
  106986           81398 :   db = pParse->db;
  106987           81398 :   nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
  106988           81398 :   pWInfo = sqlite3DbMallocZero(db, 
  106989                 :       nByteWInfo + 
  106990           81398 :       sizeof(WhereClause) +
  106991                 :       sizeof(WhereMaskSet)
  106992                 :   );
  106993           81398 :   if( db->mallocFailed ){
  106994               0 :     sqlite3DbFree(db, pWInfo);
  106995               0 :     pWInfo = 0;
  106996               0 :     goto whereBeginError;
  106997                 :   }
  106998           81398 :   pWInfo->nLevel = nTabList;
  106999           81398 :   pWInfo->pParse = pParse;
  107000           81398 :   pWInfo->pTabList = pTabList;
  107001           81398 :   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
  107002           81398 :   pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
  107003           81398 :   pWInfo->wctrlFlags = wctrlFlags;
  107004           81398 :   pWInfo->savedNQueryLoop = pParse->nQueryLoop;
  107005           81398 :   pMaskSet = (WhereMaskSet*)&pWC[1];
  107006                 : 
  107007                 :   /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
  107008                 :   ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
  107009           81398 :   if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
  107010                 : 
  107011                 :   /* Split the WHERE clause into separate subexpressions where each
  107012                 :   ** subexpression is separated by an AND operator.
  107013                 :   */
  107014           81398 :   initMaskSet(pMaskSet);
  107015           81398 :   whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
  107016           81398 :   sqlite3ExprCodeConstants(pParse, pWhere);
  107017           81398 :   whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
  107018                 :     
  107019                 :   /* Special case: a WHERE clause that is constant.  Evaluate the
  107020                 :   ** expression and either jump over all of the code or fall thru.
  107021                 :   */
  107022           81398 :   if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
  107023             122 :     sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
  107024             122 :     pWhere = 0;
  107025                 :   }
  107026                 : 
  107027                 :   /* Assign a bit from the bitmask to every term in the FROM clause.
  107028                 :   **
  107029                 :   ** When assigning bitmask values to FROM clause cursors, it must be
  107030                 :   ** the case that if X is the bitmask for the N-th FROM clause term then
  107031                 :   ** the bitmask for all FROM clause terms to the left of the N-th term
  107032                 :   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
  107033                 :   ** its Expr.iRightJoinTable value to find the bitmask of the right table
  107034                 :   ** of the join.  Subtracting one from the right table bitmask gives a
  107035                 :   ** bitmask for all tables to the left of the join.  Knowing the bitmask
  107036                 :   ** for all tables to the left of a left join is important.  Ticket #3015.
  107037                 :   **
  107038                 :   ** Configure the WhereClause.vmask variable so that bits that correspond
  107039                 :   ** to virtual table cursors are set. This is used to selectively disable 
  107040                 :   ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful 
  107041                 :   ** with virtual tables.
  107042                 :   **
  107043                 :   ** Note that bitmasks are created for all pTabList->nSrc tables in
  107044                 :   ** pTabList, not just the first nTabList tables.  nTabList is normally
  107045                 :   ** equal to pTabList->nSrc but might be shortened to 1 if the
  107046                 :   ** WHERE_ONETABLE_ONLY flag is set.
  107047                 :   */
  107048           81398 :   assert( pWC->vmask==0 && pMaskSet->n==0 );
  107049          166552 :   for(i=0; i<pTabList->nSrc; i++){
  107050           85154 :     createMask(pMaskSet, pTabList->a[i].iCursor);
  107051                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  107052           85154 :     if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
  107053              54 :       pWC->vmask |= ((Bitmask)1 << i);
  107054                 :     }
  107055                 : #endif
  107056                 :   }
  107057                 : #ifndef NDEBUG
  107058                 :   {
  107059           81398 :     Bitmask toTheLeft = 0;
  107060          166552 :     for(i=0; i<pTabList->nSrc; i++){
  107061           85154 :       Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
  107062           85154 :       assert( (m-1)==toTheLeft );
  107063           85154 :       toTheLeft |= m;
  107064                 :     }
  107065                 :   }
  107066                 : #endif
  107067                 : 
  107068                 :   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
  107069                 :   ** add new virtual terms onto the end of the WHERE clause.  We do not
  107070                 :   ** want to analyze these virtual terms, so start analyzing at the end
  107071                 :   ** and work forward so that the added virtual terms are never processed.
  107072                 :   */
  107073           81398 :   exprAnalyzeAll(pTabList, pWC);
  107074           81398 :   if( db->mallocFailed ){
  107075               0 :     goto whereBeginError;
  107076                 :   }
  107077                 : 
  107078                 :   /* Check if the DISTINCT qualifier, if there is one, is redundant. 
  107079                 :   ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
  107080                 :   ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
  107081                 :   */
  107082           81398 :   if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
  107083               0 :     pDistinct = 0;
  107084               0 :     pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  107085                 :   }
  107086                 : 
  107087                 :   /* Chose the best index to use for each table in the FROM clause.
  107088                 :   **
  107089                 :   ** This loop fills in the following fields:
  107090                 :   **
  107091                 :   **   pWInfo->a[].pIdx      The index to use for this level of the loop.
  107092                 :   **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
  107093                 :   **   pWInfo->a[].nEq       The number of == and IN constraints
  107094                 :   **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
  107095                 :   **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
  107096                 :   **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
  107097                 :   **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
  107098                 :   **
  107099                 :   ** This loop also figures out the nesting order of tables in the FROM
  107100                 :   ** clause.
  107101                 :   */
  107102           81398 :   notReady = ~(Bitmask)0;
  107103           81398 :   andFlags = ~0;
  107104                 :   WHERETRACE(("*** Optimizer Start ***\n"));
  107105          166548 :   for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
  107106                 :     WhereCost bestPlan;         /* Most efficient plan seen so far */
  107107                 :     Index *pIdx;                /* Index for FROM table at pTabItem */
  107108                 :     int j;                      /* For looping over FROM tables */
  107109           85150 :     int bestJ = -1;             /* The value of j */
  107110                 :     Bitmask m;                  /* Bitmask value for j or bestJ */
  107111                 :     int isOptimal;              /* Iterator for optimal/non-optimal search */
  107112                 :     int nUnconstrained;         /* Number tables without INDEXED BY */
  107113                 :     Bitmask notIndexed;         /* Mask of tables that cannot use an index */
  107114                 : 
  107115           85150 :     memset(&bestPlan, 0, sizeof(bestPlan));
  107116           85150 :     bestPlan.rCost = SQLITE_BIG_DBL;
  107117                 :     WHERETRACE(("*** Begin search for loop %d ***\n", i));
  107118                 : 
  107119                 :     /* Loop through the remaining entries in the FROM clause to find the
  107120                 :     ** next nested loop. The loop tests all FROM clause entries
  107121                 :     ** either once or twice. 
  107122                 :     **
  107123                 :     ** The first test is always performed if there are two or more entries
  107124                 :     ** remaining and never performed if there is only one FROM clause entry
  107125                 :     ** to choose from.  The first test looks for an "optimal" scan.  In
  107126                 :     ** this context an optimal scan is one that uses the same strategy
  107127                 :     ** for the given FROM clause entry as would be selected if the entry
  107128                 :     ** were used as the innermost nested loop.  In other words, a table
  107129                 :     ** is chosen such that the cost of running that table cannot be reduced
  107130                 :     ** by waiting for other tables to run first.  This "optimal" test works
  107131                 :     ** by first assuming that the FROM clause is on the inner loop and finding
  107132                 :     ** its query plan, then checking to see if that query plan uses any
  107133                 :     ** other FROM clause terms that are notReady.  If no notReady terms are
  107134                 :     ** used then the "optimal" query plan works.
  107135                 :     **
  107136                 :     ** Note that the WhereCost.nRow parameter for an optimal scan might
  107137                 :     ** not be as small as it would be if the table really were the innermost
  107138                 :     ** join.  The nRow value can be reduced by WHERE clause constraints
  107139                 :     ** that do not use indices.  But this nRow reduction only happens if the
  107140                 :     ** table really is the innermost join.  
  107141                 :     **
  107142                 :     ** The second loop iteration is only performed if no optimal scan
  107143                 :     ** strategies were found by the first iteration. This second iteration
  107144                 :     ** is used to search for the lowest cost scan overall.
  107145                 :     **
  107146                 :     ** Previous versions of SQLite performed only the second iteration -
  107147                 :     ** the next outermost loop was always that with the lowest overall
  107148                 :     ** cost. However, this meant that SQLite could select the wrong plan
  107149                 :     ** for scripts such as the following:
  107150                 :     **   
  107151                 :     **   CREATE TABLE t1(a, b); 
  107152                 :     **   CREATE TABLE t2(c, d);
  107153                 :     **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
  107154                 :     **
  107155                 :     ** The best strategy is to iterate through table t1 first. However it
  107156                 :     ** is not possible to determine this with a simple greedy algorithm.
  107157                 :     ** Since the cost of a linear scan through table t2 is the same 
  107158                 :     ** as the cost of a linear scan through table t1, a simple greedy 
  107159                 :     ** algorithm may choose to use t2 for the outer loop, which is a much
  107160                 :     ** costlier approach.
  107161                 :     */
  107162           85150 :     nUnconstrained = 0;
  107163           85150 :     notIndexed = 0;
  107164          170793 :     for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
  107165                 :       Bitmask mask;             /* Mask of tables not yet ready */
  107166          177653 :       for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
  107167                 :         int doNotReorder;    /* True if this table should not be reordered */
  107168                 :         WhereCost sCost;     /* Cost information from best[Virtual]Index() */
  107169                 :         ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
  107170                 :         ExprList *pDist;     /* DISTINCT clause for index to optimize */
  107171                 :   
  107172           97164 :         doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
  107173           97164 :         if( j!=iFrom && doNotReorder ) break;
  107174           94714 :         m = getMask(pMaskSet, pTabItem->iCursor);
  107175           94714 :         if( (m & notReady)==0 ){
  107176            5618 :           if( j==iFrom ) iFrom++;
  107177            5618 :           continue;
  107178                 :         }
  107179           89096 :         mask = (isOptimal ? m : notReady);
  107180           89096 :         pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
  107181           89096 :         pDist = (i==0 ? pDistinct : 0);
  107182           89096 :         if( pTabItem->pIndex==0 ) nUnconstrained++;
  107183                 :   
  107184                 :         WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
  107185                 :                     j, isOptimal));
  107186           89096 :         assert( pTabItem->pTab );
  107187                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  107188           89096 :         if( IsVirtual(pTabItem->pTab) ){
  107189              54 :           sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
  107190              54 :           bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
  107191                 :                            &sCost, pp);
  107192                 :         }else 
  107193                 : #endif
  107194                 :         {
  107195           89042 :           bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
  107196                 :               pDist, &sCost);
  107197                 :         }
  107198           89096 :         assert( isOptimal || (sCost.used&notReady)==0 );
  107199                 : 
  107200                 :         /* If an INDEXED BY clause is present, then the plan must use that
  107201                 :         ** index if it uses any index at all */
  107202           89096 :         assert( pTabItem->pIndex==0 
  107203                 :                   || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
  107204                 :                   || sCost.plan.u.pIdx==pTabItem->pIndex );
  107205                 : 
  107206           89096 :         if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
  107207            1564 :           notIndexed |= m;
  107208                 :         }
  107209                 : 
  107210                 :         /* Conditions under which this table becomes the best so far:
  107211                 :         **
  107212                 :         **   (1) The table must not depend on other tables that have not
  107213                 :         **       yet run.
  107214                 :         **
  107215                 :         **   (2) A full-table-scan plan cannot supercede indexed plan unless
  107216                 :         **       the full-table-scan is an "optimal" plan as defined above.
  107217                 :         **
  107218                 :         **   (3) All tables have an INDEXED BY clause or this table lacks an
  107219                 :         **       INDEXED BY clause or this table uses the specific
  107220                 :         **       index specified by its INDEXED BY clause.  This rule ensures
  107221                 :         **       that a best-so-far is always selected even if an impossible
  107222                 :         **       combination of INDEXED BY clauses are given.  The error
  107223                 :         **       will be detected and relayed back to the application later.
  107224                 :         **       The NEVER() comes about because rule (2) above prevents
  107225                 :         **       An indexable full-table-scan from reaching rule (3).
  107226                 :         **
  107227                 :         **   (4) The plan cost must be lower than prior plans or else the
  107228                 :         **       cost must be the same and the number of rows must be lower.
  107229                 :         */
  107230           89096 :         if( (sCost.used&notReady)==0                       /* (1) */
  107231           85771 :             && (bestJ<0 || (notIndexed&m)!=0               /* (2) */
  107232             619 :                 || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
  107233             323 :                 || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
  107234           85659 :             && (nUnconstrained==0 || pTabItem->pIndex==0   /* (3) */
  107235               0 :                 || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
  107236           85659 :             && (bestJ<0 || sCost.rCost<bestPlan.rCost      /* (4) */
  107237             208 :                 || (sCost.rCost<=bestPlan.rCost 
  107238             110 :                  && sCost.plan.nRow<bestPlan.plan.nRow))
  107239                 :         ){
  107240                 :           WHERETRACE(("=== table %d is best so far"
  107241                 :                       " with cost=%g and nRow=%g\n",
  107242                 :                       j, sCost.rCost, sCost.plan.nRow));
  107243           85451 :           bestPlan = sCost;
  107244           85451 :           bestJ = j;
  107245                 :         }
  107246           89096 :         if( doNotReorder ) break;
  107247                 :       }
  107248                 :     }
  107249           85150 :     assert( bestJ>=0 );
  107250           85150 :     assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
  107251                 :     WHERETRACE(("*** Optimizer selects table %d for loop %d"
  107252                 :                 " with cost=%g and nRow=%g\n",
  107253                 :                 bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
  107254                 :     /* The ALWAYS() that follows was added to hush up clang scan-build */
  107255           85150 :     if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
  107256           21639 :       *ppOrderBy = 0;
  107257                 :     }
  107258           85150 :     if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
  107259              12 :       assert( pWInfo->eDistinct==0 );
  107260              12 :       pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
  107261                 :     }
  107262           85150 :     andFlags &= bestPlan.plan.wsFlags;
  107263           85150 :     pLevel->plan = bestPlan.plan;
  107264                 :     testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
  107265                 :     testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
  107266           85150 :     if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
  107267           24396 :       pLevel->iIdxCur = pParse->nTab++;
  107268                 :     }else{
  107269           60754 :       pLevel->iIdxCur = -1;
  107270                 :     }
  107271           85150 :     notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
  107272           85150 :     pLevel->iFrom = (u8)bestJ;
  107273           85150 :     if( bestPlan.plan.nRow>=(double)1 ){
  107274           85096 :       pParse->nQueryLoop *= bestPlan.plan.nRow;
  107275                 :     }
  107276                 : 
  107277                 :     /* Check that if the table scanned by this loop iteration had an
  107278                 :     ** INDEXED BY clause attached to it, that the named index is being
  107279                 :     ** used for the scan. If not, then query compilation has failed.
  107280                 :     ** Return an error.
  107281                 :     */
  107282           85150 :     pIdx = pTabList->a[bestJ].pIndex;
  107283           85150 :     if( pIdx ){
  107284               0 :       if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
  107285               0 :         sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
  107286               0 :         goto whereBeginError;
  107287                 :       }else{
  107288                 :         /* If an INDEXED BY clause is used, the bestIndex() function is
  107289                 :         ** guaranteed to find the index specified in the INDEXED BY clause
  107290                 :         ** if it find an index at all. */
  107291               0 :         assert( bestPlan.plan.u.pIdx==pIdx );
  107292                 :       }
  107293                 :     }
  107294                 :   }
  107295                 :   WHERETRACE(("*** Optimizer Finished ***\n"));
  107296           81398 :   if( pParse->nErr || db->mallocFailed ){
  107297                 :     goto whereBeginError;
  107298                 :   }
  107299                 : 
  107300                 :   /* If the total query only selects a single row, then the ORDER BY
  107301                 :   ** clause is irrelevant.
  107302                 :   */
  107303           81398 :   if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
  107304            8610 :     *ppOrderBy = 0;
  107305                 :   }
  107306                 : 
  107307                 :   /* If the caller is an UPDATE or DELETE statement that is requesting
  107308                 :   ** to use a one-pass algorithm, determine if this is appropriate.
  107309                 :   ** The one-pass algorithm only works if the WHERE clause constraints
  107310                 :   ** the statement to update a single row.
  107311                 :   */
  107312           81398 :   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
  107313           81398 :   if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
  107314           11301 :     pWInfo->okOnePass = 1;
  107315           11301 :     pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
  107316                 :   }
  107317                 : 
  107318                 :   /* Open all tables in the pTabList and any indices selected for
  107319                 :   ** searching those tables.
  107320                 :   */
  107321           81398 :   sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
  107322           81398 :   notReady = ~(Bitmask)0;
  107323           81398 :   pWInfo->nRowOut = (double)1;
  107324          166548 :   for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
  107325                 :     Table *pTab;     /* Table to open */
  107326                 :     int iDb;         /* Index of database containing table/index */
  107327                 : 
  107328           85150 :     pTabItem = &pTabList->a[pLevel->iFrom];
  107329           85150 :     pTab = pTabItem->pTab;
  107330           85150 :     pLevel->iTabCur = pTabItem->iCursor;
  107331           85150 :     pWInfo->nRowOut *= pLevel->plan.nRow;
  107332           85150 :     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  107333           85150 :     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
  107334                 :       /* Do nothing */
  107335                 :     }else
  107336                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  107337           85032 :     if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  107338              54 :       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
  107339              54 :       int iCur = pTabItem->iCursor;
  107340              54 :       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
  107341                 :     }else
  107342                 : #endif
  107343           84978 :     if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
  107344          145204 :          && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
  107345           72598 :       int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
  107346           72598 :       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
  107347                 :       testcase( pTab->nCol==BMS-1 );
  107348                 :       testcase( pTab->nCol==BMS );
  107349           72598 :       if( !pWInfo->okOnePass && pTab->nCol<BMS ){
  107350           61297 :         Bitmask b = pTabItem->colUsed;
  107351           61297 :         int n = 0;
  107352           61297 :         for(; b; b=b>>1, n++){}
  107353           61297 :         sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
  107354                 :                             SQLITE_INT_TO_PTR(n), P4_INT32);
  107355           61297 :         assert( n<=pTab->nCol );
  107356                 :       }
  107357                 :     }else{
  107358           12380 :       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  107359                 :     }
  107360                 : #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  107361           85150 :     if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
  107362             207 :       constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
  107363                 :     }else
  107364                 : #endif
  107365           84943 :     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
  107366           24189 :       Index *pIx = pLevel->plan.u.pIdx;
  107367           24189 :       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
  107368           24189 :       int iIdxCur = pLevel->iIdxCur;
  107369           24189 :       assert( pIx->pSchema==pTab->pSchema );
  107370           24189 :       assert( iIdxCur>=0 );
  107371           24189 :       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
  107372                 :                         (char*)pKey, P4_KEYINFO_HANDOFF);
  107373           24189 :       VdbeComment((v, "%s", pIx->zName));
  107374                 :     }
  107375           85150 :     sqlite3CodeVerifySchema(pParse, iDb);
  107376           85150 :     notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
  107377                 :   }
  107378           81398 :   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
  107379           81398 :   if( db->mallocFailed ) goto whereBeginError;
  107380                 : 
  107381                 :   /* Generate the code to do the search.  Each iteration of the for
  107382                 :   ** loop below generates code for a single nested loop of the VM
  107383                 :   ** program.
  107384                 :   */
  107385           81398 :   notReady = ~(Bitmask)0;
  107386          166548 :   for(i=0; i<nTabList; i++){
  107387           85150 :     pLevel = &pWInfo->a[i];
  107388           85150 :     explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
  107389           85150 :     notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere);
  107390           85150 :     pWInfo->iContinue = pLevel->addrCont;
  107391                 :   }
  107392                 : 
  107393                 : #ifdef SQLITE_TEST  /* For testing and debugging use only */
  107394                 :   /* Record in the query plan information about the current table
  107395                 :   ** and the index used to access it (if any).  If the table itself
  107396                 :   ** is not used, its name is just '{}'.  If no index is used
  107397                 :   ** the index is listed as "{}".  If the primary key is used the
  107398                 :   ** index name is '*'.
  107399                 :   */
  107400                 :   for(i=0; i<nTabList; i++){
  107401                 :     char *z;
  107402                 :     int n;
  107403                 :     pLevel = &pWInfo->a[i];
  107404                 :     pTabItem = &pTabList->a[pLevel->iFrom];
  107405                 :     z = pTabItem->zAlias;
  107406                 :     if( z==0 ) z = pTabItem->pTab->zName;
  107407                 :     n = sqlite3Strlen30(z);
  107408                 :     if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
  107409                 :       if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
  107410                 :         memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
  107411                 :         nQPlan += 2;
  107412                 :       }else{
  107413                 :         memcpy(&sqlite3_query_plan[nQPlan], z, n);
  107414                 :         nQPlan += n;
  107415                 :       }
  107416                 :       sqlite3_query_plan[nQPlan++] = ' ';
  107417                 :     }
  107418                 :     testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
  107419                 :     testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
  107420                 :     if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
  107421                 :       memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
  107422                 :       nQPlan += 2;
  107423                 :     }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
  107424                 :       n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
  107425                 :       if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
  107426                 :         memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
  107427                 :         nQPlan += n;
  107428                 :         sqlite3_query_plan[nQPlan++] = ' ';
  107429                 :       }
  107430                 :     }else{
  107431                 :       memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
  107432                 :       nQPlan += 3;
  107433                 :     }
  107434                 :   }
  107435                 :   while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
  107436                 :     sqlite3_query_plan[--nQPlan] = 0;
  107437                 :   }
  107438                 :   sqlite3_query_plan[nQPlan] = 0;
  107439                 :   nQPlan = 0;
  107440                 : #endif /* SQLITE_TEST // Testing and debugging use only */
  107441                 : 
  107442                 :   /* Record the continuation address in the WhereInfo structure.  Then
  107443                 :   ** clean up and return.
  107444                 :   */
  107445           81398 :   return pWInfo;
  107446                 : 
  107447                 :   /* Jump here if malloc fails */
  107448                 : whereBeginError:
  107449               0 :   if( pWInfo ){
  107450               0 :     pParse->nQueryLoop = pWInfo->savedNQueryLoop;
  107451               0 :     whereInfoFree(db, pWInfo);
  107452                 :   }
  107453               0 :   return 0;
  107454                 : }
  107455                 : 
  107456                 : /*
  107457                 : ** Generate the end of the WHERE loop.  See comments on 
  107458                 : ** sqlite3WhereBegin() for additional information.
  107459                 : */
  107460           81398 : SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
  107461           81398 :   Parse *pParse = pWInfo->pParse;
  107462           81398 :   Vdbe *v = pParse->pVdbe;
  107463                 :   int i;
  107464                 :   WhereLevel *pLevel;
  107465           81398 :   SrcList *pTabList = pWInfo->pTabList;
  107466           81398 :   sqlite3 *db = pParse->db;
  107467                 : 
  107468                 :   /* Generate loop termination code.
  107469                 :   */
  107470           81398 :   sqlite3ExprCacheClear(pParse);
  107471          166548 :   for(i=pWInfo->nLevel-1; i>=0; i--){
  107472           85150 :     pLevel = &pWInfo->a[i];
  107473           85150 :     sqlite3VdbeResolveLabel(v, pLevel->addrCont);
  107474           85150 :     if( pLevel->op!=OP_Noop ){
  107475           57729 :       sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
  107476           57729 :       sqlite3VdbeChangeP5(v, pLevel->p5);
  107477                 :     }
  107478           85150 :     if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
  107479                 :       struct InLoop *pIn;
  107480                 :       int j;
  107481            2769 :       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
  107482            5538 :       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
  107483            2769 :         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
  107484            2769 :         sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop);
  107485            2769 :         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
  107486                 :       }
  107487            2769 :       sqlite3DbFree(db, pLevel->u.in.aInLoop);
  107488                 :     }
  107489           85150 :     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
  107490           85150 :     if( pLevel->iLeftJoin ){
  107491                 :       int addr;
  107492            2704 :       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
  107493            2704 :       assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
  107494                 :            || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
  107495            2704 :       if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
  107496            2413 :         sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
  107497                 :       }
  107498            2704 :       if( pLevel->iIdxCur>=0 ){
  107499             495 :         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
  107500                 :       }
  107501            2704 :       if( pLevel->op==OP_Return ){
  107502               0 :         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
  107503                 :       }else{
  107504            2704 :         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
  107505                 :       }
  107506            2704 :       sqlite3VdbeJumpHere(v, addr);
  107507                 :     }
  107508                 :   }
  107509                 : 
  107510                 :   /* The "break" point is here, just past the end of the outer loop.
  107511                 :   ** Set it.
  107512                 :   */
  107513           81398 :   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
  107514                 : 
  107515                 :   /* Close all of the cursors that were opened by sqlite3WhereBegin.
  107516                 :   */
  107517           81398 :   assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
  107518          166548 :   for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
  107519           85150 :     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
  107520           85150 :     Table *pTab = pTabItem->pTab;
  107521           85150 :     assert( pTab!=0 );
  107522           85150 :     if( (pTab->tabFlags & TF_Ephemeral)==0
  107523           85091 :      && pTab->pSelect==0
  107524           85032 :      && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
  107525                 :     ){
  107526           84972 :       int ws = pLevel->plan.wsFlags;
  107527           84972 :       if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
  107528           61144 :         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
  107529                 :       }
  107530           84972 :       if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
  107531           24133 :         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
  107532                 :       }
  107533                 :     }
  107534                 : 
  107535                 :     /* If this scan uses an index, make code substitutions to read data
  107536                 :     ** from the index in preference to the table. Sometimes, this means
  107537                 :     ** the table need never be read from. This is a performance boost,
  107538                 :     ** as the vdbe level waits until the table is read before actually
  107539                 :     ** seeking the table cursor to the record corresponding to the current
  107540                 :     ** position in the index.
  107541                 :     ** 
  107542                 :     ** Calls to the code generator in between sqlite3WhereBegin and
  107543                 :     ** sqlite3WhereEnd will have created code that references the table
  107544                 :     ** directly.  This loop scans all that code looking for opcodes
  107545                 :     ** that reference the table and converts them into opcodes that
  107546                 :     ** reference the index.
  107547                 :     */
  107548           85150 :     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){
  107549                 :       int k, j, last;
  107550                 :       VdbeOp *pOp;
  107551           24396 :       Index *pIdx = pLevel->plan.u.pIdx;
  107552                 : 
  107553           24396 :       assert( pIdx!=0 );
  107554           24396 :       pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
  107555           24396 :       last = sqlite3VdbeCurrentAddr(v);
  107556          502166 :       for(k=pWInfo->iTop; k<last; k++, pOp++){
  107557          477770 :         if( pOp->p1!=pLevel->iTabCur ) continue;
  107558          159178 :         if( pOp->opcode==OP_Column ){
  107559          268778 :           for(j=0; j<pIdx->nColumn; j++){
  107560          185381 :             if( pOp->p2==pIdx->aiColumn[j] ){
  107561           16050 :               pOp->p2 = j;
  107562           16050 :               pOp->p1 = pLevel->iIdxCur;
  107563           16050 :               break;
  107564                 :             }
  107565                 :           }
  107566           99447 :           assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
  107567                 :                || j<pIdx->nColumn );
  107568           59731 :         }else if( pOp->opcode==OP_Rowid ){
  107569           13103 :           pOp->p1 = pLevel->iIdxCur;
  107570           13103 :           pOp->opcode = OP_IdxRowid;
  107571                 :         }
  107572                 :       }
  107573                 :     }
  107574                 :   }
  107575                 : 
  107576                 :   /* Final cleanup
  107577                 :   */
  107578           81398 :   pParse->nQueryLoop = pWInfo->savedNQueryLoop;
  107579           81398 :   whereInfoFree(db, pWInfo);
  107580                 :   return;
  107581                 : }
  107582                 : 
  107583                 : /************** End of where.c ***********************************************/
  107584                 : /************** Begin file parse.c *******************************************/
  107585                 : /* Driver template for the LEMON parser generator.
  107586                 : ** The author disclaims copyright to this source code.
  107587                 : **
  107588                 : ** This version of "lempar.c" is modified, slightly, for use by SQLite.
  107589                 : ** The only modifications are the addition of a couple of NEVER()
  107590                 : ** macros to disable tests that are needed in the case of a general
  107591                 : ** LALR(1) grammar but which are always false in the
  107592                 : ** specific grammar used by SQLite.
  107593                 : */
  107594                 : /* First off, code is included that follows the "include" declaration
  107595                 : ** in the input grammar file. */
  107596                 : /* #include <stdio.h> */
  107597                 : 
  107598                 : 
  107599                 : /*
  107600                 : ** Disable all error recovery processing in the parser push-down
  107601                 : ** automaton.
  107602                 : */
  107603                 : #define YYNOERRORRECOVERY 1
  107604                 : 
  107605                 : /*
  107606                 : ** Make yytestcase() the same as testcase()
  107607                 : */
  107608                 : #define yytestcase(X) testcase(X)
  107609                 : 
  107610                 : /*
  107611                 : ** An instance of this structure holds information about the
  107612                 : ** LIMIT clause of a SELECT statement.
  107613                 : */
  107614                 : struct LimitVal {
  107615                 :   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
  107616                 :   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
  107617                 : };
  107618                 : 
  107619                 : /*
  107620                 : ** An instance of this structure is used to store the LIKE,
  107621                 : ** GLOB, NOT LIKE, and NOT GLOB operators.
  107622                 : */
  107623                 : struct LikeOp {
  107624                 :   Token eOperator;  /* "like" or "glob" or "regexp" */
  107625                 :   int not;         /* True if the NOT keyword is present */
  107626                 : };
  107627                 : 
  107628                 : /*
  107629                 : ** An instance of the following structure describes the event of a
  107630                 : ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
  107631                 : ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
  107632                 : **
  107633                 : **      UPDATE ON (a,b,c)
  107634                 : **
  107635                 : ** Then the "b" IdList records the list "a,b,c".
  107636                 : */
  107637                 : struct TrigEvent { int a; IdList * b; };
  107638                 : 
  107639                 : /*
  107640                 : ** An instance of this structure holds the ATTACH key and the key type.
  107641                 : */
  107642                 : struct AttachKey { int type;  Token key; };
  107643                 : 
  107644                 : 
  107645                 :   /* This is a utility routine used to set the ExprSpan.zStart and
  107646                 :   ** ExprSpan.zEnd values of pOut so that the span covers the complete
  107647                 :   ** range of text beginning with pStart and going to the end of pEnd.
  107648                 :   */
  107649          225136 :   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
  107650          225136 :     pOut->zStart = pStart->z;
  107651          225136 :     pOut->zEnd = &pEnd->z[pEnd->n];
  107652          225136 :   }
  107653                 : 
  107654                 :   /* Construct a new Expr object from a single identifier.  Use the
  107655                 :   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
  107656                 :   ** that created the expression.
  107657                 :   */
  107658          647460 :   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
  107659          647460 :     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
  107660          647460 :     pOut->zStart = pValue->z;
  107661          647460 :     pOut->zEnd = &pValue->z[pValue->n];
  107662          647460 :   }
  107663                 : 
  107664                 :   /* This routine constructs a binary expression node out of two ExprSpan
  107665                 :   ** objects and uses the result to populate a new ExprSpan object.
  107666                 :   */
  107667          150419 :   static void spanBinaryExpr(
  107668                 :     ExprSpan *pOut,     /* Write the result here */
  107669                 :     Parse *pParse,      /* The parsing context.  Errors accumulate here */
  107670                 :     int op,             /* The binary operation */
  107671                 :     ExprSpan *pLeft,    /* The left operand */
  107672                 :     ExprSpan *pRight    /* The right operand */
  107673                 :   ){
  107674          150419 :     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
  107675          150419 :     pOut->zStart = pLeft->zStart;
  107676          150419 :     pOut->zEnd = pRight->zEnd;
  107677          150419 :   }
  107678                 : 
  107679                 :   /* Construct an expression node for a unary postfix operator
  107680                 :   */
  107681            2600 :   static void spanUnaryPostfix(
  107682                 :     ExprSpan *pOut,        /* Write the new expression node here */
  107683                 :     Parse *pParse,         /* Parsing context to record errors */
  107684                 :     int op,                /* The operator */
  107685                 :     ExprSpan *pOperand,    /* The operand */
  107686                 :     Token *pPostOp         /* The operand token for setting the span */
  107687                 :   ){
  107688            2600 :     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
  107689            2600 :     pOut->zStart = pOperand->zStart;
  107690            2600 :     pOut->zEnd = &pPostOp->z[pPostOp->n];
  107691            2600 :   }                           
  107692                 : 
  107693                 :   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
  107694                 :   ** unary TK_ISNULL or TK_NOTNULL expression. */
  107695            3829 :   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
  107696            3829 :     sqlite3 *db = pParse->db;
  107697            3829 :     if( db->mallocFailed==0 && pY->op==TK_NULL ){
  107698            3829 :       pA->op = (u8)op;
  107699            3829 :       sqlite3ExprDelete(db, pA->pRight);
  107700            3829 :       pA->pRight = 0;
  107701                 :     }
  107702            3829 :   }
  107703                 : 
  107704                 :   /* Construct an expression node for a unary prefix operator
  107705                 :   */
  107706            3014 :   static void spanUnaryPrefix(
  107707                 :     ExprSpan *pOut,        /* Write the new expression node here */
  107708                 :     Parse *pParse,         /* Parsing context to record errors */
  107709                 :     int op,                /* The operator */
  107710                 :     ExprSpan *pOperand,    /* The operand */
  107711                 :     Token *pPreOp         /* The operand token for setting the span */
  107712                 :   ){
  107713            3014 :     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
  107714            3014 :     pOut->zStart = pPreOp->z;
  107715            3014 :     pOut->zEnd = pOperand->zEnd;
  107716            3014 :   }
  107717                 : /* Next is all token values, in a form suitable for use by makeheaders.
  107718                 : ** This section will be null unless lemon is run with the -m switch.
  107719                 : */
  107720                 : /* 
  107721                 : ** These constants (all generated automatically by the parser generator)
  107722                 : ** specify the various kinds of tokens (terminals) that the parser
  107723                 : ** understands. 
  107724                 : **
  107725                 : ** Each symbol here is a terminal symbol in the grammar.
  107726                 : */
  107727                 : /* Make sure the INTERFACE macro is defined.
  107728                 : */
  107729                 : #ifndef INTERFACE
  107730                 : # define INTERFACE 1
  107731                 : #endif
  107732                 : /* The next thing included is series of defines which control
  107733                 : ** various aspects of the generated parser.
  107734                 : **    YYCODETYPE         is the data type used for storing terminal
  107735                 : **                       and nonterminal numbers.  "unsigned char" is
  107736                 : **                       used if there are fewer than 250 terminals
  107737                 : **                       and nonterminals.  "int" is used otherwise.
  107738                 : **    YYNOCODE           is a number of type YYCODETYPE which corresponds
  107739                 : **                       to no legal terminal or nonterminal number.  This
  107740                 : **                       number is used to fill in empty slots of the hash 
  107741                 : **                       table.
  107742                 : **    YYFALLBACK         If defined, this indicates that one or more tokens
  107743                 : **                       have fall-back values which should be used if the
  107744                 : **                       original value of the token will not parse.
  107745                 : **    YYACTIONTYPE       is the data type used for storing terminal
  107746                 : **                       and nonterminal numbers.  "unsigned char" is
  107747                 : **                       used if there are fewer than 250 rules and
  107748                 : **                       states combined.  "int" is used otherwise.
  107749                 : **    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given 
  107750                 : **                       directly to the parser from the tokenizer.
  107751                 : **    YYMINORTYPE        is the data type used for all minor tokens.
  107752                 : **                       This is typically a union of many types, one of
  107753                 : **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
  107754                 : **                       for base tokens is called "yy0".
  107755                 : **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
  107756                 : **                       zero the stack is dynamically sized using realloc()
  107757                 : **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
  107758                 : **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
  107759                 : **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
  107760                 : **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
  107761                 : **    YYNSTATE           the combined number of states.
  107762                 : **    YYNRULE            the number of rules in the grammar
  107763                 : **    YYERRORSYMBOL      is the code number of the error symbol.  If not
  107764                 : **                       defined, then do no error processing.
  107765                 : */
  107766                 : #define YYCODETYPE unsigned char
  107767                 : #define YYNOCODE 253
  107768                 : #define YYACTIONTYPE unsigned short int
  107769                 : #define YYWILDCARD 67
  107770                 : #define sqlite3ParserTOKENTYPE Token
  107771                 : typedef union {
  107772                 :   int yyinit;
  107773                 :   sqlite3ParserTOKENTYPE yy0;
  107774                 :   int yy4;
  107775                 :   struct TrigEvent yy90;
  107776                 :   ExprSpan yy118;
  107777                 :   TriggerStep* yy203;
  107778                 :   u8 yy210;
  107779                 :   struct {int value; int mask;} yy215;
  107780                 :   SrcList* yy259;
  107781                 :   struct LimitVal yy292;
  107782                 :   Expr* yy314;
  107783                 :   ExprList* yy322;
  107784                 :   struct LikeOp yy342;
  107785                 :   IdList* yy384;
  107786                 :   Select* yy387;
  107787                 : } YYMINORTYPE;
  107788                 : #ifndef YYSTACKDEPTH
  107789                 : #define YYSTACKDEPTH 100
  107790                 : #endif
  107791                 : #define sqlite3ParserARG_SDECL Parse *pParse;
  107792                 : #define sqlite3ParserARG_PDECL ,Parse *pParse
  107793                 : #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
  107794                 : #define sqlite3ParserARG_STORE yypParser->pParse = pParse
  107795                 : #define YYNSTATE 630
  107796                 : #define YYNRULE 329
  107797                 : #define YYFALLBACK 1
  107798                 : #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
  107799                 : #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
  107800                 : #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
  107801                 : 
  107802                 : /* The yyzerominor constant is used to initialize instances of
  107803                 : ** YYMINORTYPE objects to zero. */
  107804                 : static const YYMINORTYPE yyzerominor = { 0 };
  107805                 : 
  107806                 : /* Define the yytestcase() macro to be a no-op if is not already defined
  107807                 : ** otherwise.
  107808                 : **
  107809                 : ** Applications can choose to define yytestcase() in the %include section
  107810                 : ** to a macro that can assist in verifying code coverage.  For production
  107811                 : ** code the yytestcase() macro should be turned off.  But it is useful
  107812                 : ** for testing.
  107813                 : */
  107814                 : #ifndef yytestcase
  107815                 : # define yytestcase(X)
  107816                 : #endif
  107817                 : 
  107818                 : 
  107819                 : /* Next are the tables used to determine what action to take based on the
  107820                 : ** current state and lookahead token.  These tables are used to implement
  107821                 : ** functions that take a state number and lookahead value and return an
  107822                 : ** action integer.  
  107823                 : **
  107824                 : ** Suppose the action integer is N.  Then the action is determined as
  107825                 : ** follows
  107826                 : **
  107827                 : **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
  107828                 : **                                      token onto the stack and goto state N.
  107829                 : **
  107830                 : **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
  107831                 : **
  107832                 : **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
  107833                 : **
  107834                 : **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
  107835                 : **
  107836                 : **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
  107837                 : **                                      slots in the yy_action[] table.
  107838                 : **
  107839                 : ** The action table is constructed as a single large table named yy_action[].
  107840                 : ** Given state S and lookahead X, the action is computed as
  107841                 : **
  107842                 : **      yy_action[ yy_shift_ofst[S] + X ]
  107843                 : **
  107844                 : ** If the index value yy_shift_ofst[S]+X is out of range or if the value
  107845                 : ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
  107846                 : ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
  107847                 : ** and that yy_default[S] should be used instead.  
  107848                 : **
  107849                 : ** The formula above is for computing the action when the lookahead is
  107850                 : ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
  107851                 : ** a reduce action) then the yy_reduce_ofst[] array is used in place of
  107852                 : ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
  107853                 : ** YY_SHIFT_USE_DFLT.
  107854                 : **
  107855                 : ** The following are the tables generated in this section:
  107856                 : **
  107857                 : **  yy_action[]        A single table containing all actions.
  107858                 : **  yy_lookahead[]     A table containing the lookahead for each entry in
  107859                 : **                     yy_action.  Used to detect hash collisions.
  107860                 : **  yy_shift_ofst[]    For each state, the offset into yy_action for
  107861                 : **                     shifting terminals.
  107862                 : **  yy_reduce_ofst[]   For each state, the offset into yy_action for
  107863                 : **                     shifting non-terminals after a reduce.
  107864                 : **  yy_default[]       Default action for each state.
  107865                 : */
  107866                 : #define YY_ACTTAB_COUNT (1557)
  107867                 : static const YYACTIONTYPE yy_action[] = {
  107868                 :  /*     0 */   313,  960,  186,  419,    2,  172,  627,  597,   55,   55,
  107869                 :  /*    10 */    55,   55,   48,   53,   53,   53,   53,   52,   52,   51,
  107870                 :  /*    20 */    51,   51,   50,  238,  302,  283,  623,  622,  516,  515,
  107871                 :  /*    30 */   590,  584,   55,   55,   55,   55,  282,   53,   53,   53,
  107872                 :  /*    40 */    53,   52,   52,   51,   51,   51,   50,  238,    6,   56,
  107873                 :  /*    50 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
  107874                 :  /*    60 */    55,   55,  608,   53,   53,   53,   53,   52,   52,   51,
  107875                 :  /*    70 */    51,   51,   50,  238,  313,  597,  409,  330,  579,  579,
  107876                 :  /*    80 */    32,   53,   53,   53,   53,   52,   52,   51,   51,   51,
  107877                 :  /*    90 */    50,  238,  330,  217,  620,  619,  166,  411,  624,  382,
  107878                 :  /*   100 */   379,  378,    7,  491,  590,  584,  200,  199,  198,   58,
  107879                 :  /*   110 */   377,  300,  414,  621,  481,   66,  623,  622,  621,  580,
  107880                 :  /*   120 */   254,  601,   94,   56,   57,   47,  582,  581,  583,  583,
  107881                 :  /*   130 */    54,   54,   55,   55,   55,   55,  671,   53,   53,   53,
  107882                 :  /*   140 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  532,
  107883                 :  /*   150 */   226,  506,  507,  133,  177,  139,  284,  385,  279,  384,
  107884                 :  /*   160 */   169,  197,  342,  398,  251,  226,  253,  275,  388,  167,
  107885                 :  /*   170 */   139,  284,  385,  279,  384,  169,  570,  236,  590,  584,
  107886                 :  /*   180 */   672,  240,  275,  157,  620,  619,  554,  437,   51,   51,
  107887                 :  /*   190 */    51,   50,  238,  343,  439,  553,  438,   56,   57,   47,
  107888                 :  /*   200 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
  107889                 :  /*   210 */   465,   53,   53,   53,   53,   52,   52,   51,   51,   51,
  107890                 :  /*   220 */    50,  238,  313,  390,   52,   52,   51,   51,   51,   50,
  107891                 :  /*   230 */   238,  391,  166,  491,  566,  382,  379,  378,  409,  440,
  107892                 :  /*   240 */   579,  579,  252,  440,  607,   66,  377,  513,  621,   49,
  107893                 :  /*   250 */    46,  147,  590,  584,  621,   16,  466,  189,  621,  441,
  107894                 :  /*   260 */   442,  673,  526,  441,  340,  577,  595,   64,  194,  482,
  107895                 :  /*   270 */   434,   56,   57,   47,  582,  581,  583,  583,   54,   54,
  107896                 :  /*   280 */    55,   55,   55,   55,   30,   53,   53,   53,   53,   52,
  107897                 :  /*   290 */    52,   51,   51,   51,   50,  238,  313,  593,  593,  593,
  107898                 :  /*   300 */   387,  578,  606,  493,  259,  351,  258,  411,    1,  623,
  107899                 :  /*   310 */   622,  496,  623,  622,   65,  240,  623,  622,  597,  443,
  107900                 :  /*   320 */   237,  239,  414,  341,  237,  602,  590,  584,   18,  603,
  107901                 :  /*   330 */   166,  601,   87,  382,  379,  378,   67,  623,  622,   38,
  107902                 :  /*   340 */   623,  622,  176,  270,  377,   56,   57,   47,  582,  581,
  107903                 :  /*   350 */   583,  583,   54,   54,   55,   55,   55,   55,  175,   53,
  107904                 :  /*   360 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
  107905                 :  /*   370 */   313,  396,  233,  411,  531,  565,  317,  620,  619,   44,
  107906                 :  /*   380 */   620,  619,  240,  206,  620,  619,  597,  266,  414,  268,
  107907                 :  /*   390 */   409,  597,  579,  579,  352,  184,  505,  601,   73,  533,
  107908                 :  /*   400 */   590,  584,  466,  548,  190,  620,  619,  576,  620,  619,
  107909                 :  /*   410 */   547,  383,  551,   35,  332,  575,  574,  600,  504,   56,
  107910                 :  /*   420 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
  107911                 :  /*   430 */    55,   55,  567,   53,   53,   53,   53,   52,   52,   51,
  107912                 :  /*   440 */    51,   51,   50,  238,  313,  411,  561,  561,  528,  364,
  107913                 :  /*   450 */   259,  351,  258,  183,  361,  549,  524,  374,  411,  597,
  107914                 :  /*   460 */   414,  240,  560,  560,  409,  604,  579,  579,  328,  601,
  107915                 :  /*   470 */    93,  623,  622,  414,  590,  584,  237,  564,  559,  559,
  107916                 :  /*   480 */   520,  402,  601,   87,  409,  210,  579,  579,  168,  421,
  107917                 :  /*   490 */   950,  519,  950,   56,   57,   47,  582,  581,  583,  583,
  107918                 :  /*   500 */    54,   54,   55,   55,   55,   55,  192,   53,   53,   53,
  107919                 :  /*   510 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  600,
  107920                 :  /*   520 */   293,  563,  511,  234,  357,  146,  475,  475,  367,  411,
  107921                 :  /*   530 */   562,  411,  358,  542,  425,  171,  411,  215,  144,  620,
  107922                 :  /*   540 */   619,  544,  318,  353,  414,  203,  414,  275,  590,  584,
  107923                 :  /*   550 */   549,  414,  174,  601,   94,  601,   79,  558,  471,   61,
  107924                 :  /*   560 */   601,   79,  421,  949,  350,  949,   34,   56,   57,   47,
  107925                 :  /*   570 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
  107926                 :  /*   580 */   535,   53,   53,   53,   53,   52,   52,   51,   51,   51,
  107927                 :  /*   590 */    50,  238,  313,  307,  424,  394,  272,   49,   46,  147,
  107928                 :  /*   600 */   349,  322,    4,  411,  491,  312,  321,  425,  568,  492,
  107929                 :  /*   610 */   216,  264,  407,  575,  574,  429,   66,  549,  414,  621,
  107930                 :  /*   620 */   540,  602,  590,  584,   13,  603,  621,  601,   72,   12,
  107931                 :  /*   630 */   618,  617,  616,  202,  210,  621,  546,  469,  422,  319,
  107932                 :  /*   640 */   148,   56,   57,   47,  582,  581,  583,  583,   54,   54,
  107933                 :  /*   650 */    55,   55,   55,   55,  338,   53,   53,   53,   53,   52,
  107934                 :  /*   660 */    52,   51,   51,   51,   50,  238,  313,  600,  600,  411,
  107935                 :  /*   670 */    39,   21,   37,  170,  237,  875,  411,  572,  572,  201,
  107936                 :  /*   680 */   144,  473,  538,  331,  414,  474,  143,  146,  630,  628,
  107937                 :  /*   690 */   334,  414,  353,  601,   68,  168,  590,  584,  132,  365,
  107938                 :  /*   700 */   601,   96,  307,  423,  530,  336,   49,   46,  147,  568,
  107939                 :  /*   710 */   406,  216,  549,  360,  529,   56,   57,   47,  582,  581,
  107940                 :  /*   720 */   583,  583,   54,   54,   55,   55,   55,   55,  411,   53,
  107941                 :  /*   730 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
  107942                 :  /*   740 */   313,  411,  605,  414,  484,  510,  172,  422,  597,  318,
  107943                 :  /*   750 */   496,  485,  601,   99,  411,  142,  414,  411,  231,  411,
  107944                 :  /*   760 */   540,  411,  359,  629,    2,  601,   97,  426,  308,  414,
  107945                 :  /*   770 */   590,  584,  414,   20,  414,  621,  414,  621,  601,  106,
  107946                 :  /*   780 */   503,  601,  105,  601,  108,  601,  109,  204,   28,   56,
  107947                 :  /*   790 */    57,   47,  582,  581,  583,  583,   54,   54,   55,   55,
  107948                 :  /*   800 */    55,   55,  411,   53,   53,   53,   53,   52,   52,   51,
  107949                 :  /*   810 */    51,   51,   50,  238,  313,  411,  597,  414,  411,  276,
  107950                 :  /*   820 */   214,  600,  411,  366,  213,  381,  601,  134,  274,  500,
  107951                 :  /*   830 */   414,  167,  130,  414,  621,  411,  354,  414,  376,  601,
  107952                 :  /*   840 */   135,  129,  601,  100,  590,  584,  601,  104,  522,  521,
  107953                 :  /*   850 */   414,  621,  224,  273,  600,  167,  327,  282,  600,  601,
  107954                 :  /*   860 */   103,  468,  521,   56,   57,   47,  582,  581,  583,  583,
  107955                 :  /*   870 */    54,   54,   55,   55,   55,   55,  411,   53,   53,   53,
  107956                 :  /*   880 */    53,   52,   52,   51,   51,   51,   50,  238,  313,  411,
  107957                 :  /*   890 */    27,  414,  411,  375,  276,  167,  359,  544,   50,  238,
  107958                 :  /*   900 */   601,   95,  128,  223,  414,  411,  165,  414,  411,  621,
  107959                 :  /*   910 */   411,  621,  612,  601,  102,  372,  601,   76,  590,  584,
  107960                 :  /*   920 */   414,  570,  236,  414,  470,  414,  167,  621,  188,  601,
  107961                 :  /*   930 */    98,  225,  601,  138,  601,  137,  232,   56,   45,   47,
  107962                 :  /*   940 */   582,  581,  583,  583,   54,   54,   55,   55,   55,   55,
  107963                 :  /*   950 */   411,   53,   53,   53,   53,   52,   52,   51,   51,   51,
  107964                 :  /*   960 */    50,  238,  313,  276,  276,  414,  411,  276,  544,  459,
  107965                 :  /*   970 */   359,  171,  209,  479,  601,  136,  628,  334,  621,  621,
  107966                 :  /*   980 */   125,  414,  621,  368,  411,  621,  257,  540,  589,  588,
  107967                 :  /*   990 */   601,   75,  590,  584,  458,  446,   23,   23,  124,  414,
  107968                 :  /*  1000 */   326,  325,  621,  427,  324,  309,  600,  288,  601,   92,
  107969                 :  /*  1010 */   586,  585,   57,   47,  582,  581,  583,  583,   54,   54,
  107970                 :  /*  1020 */    55,   55,   55,   55,  411,   53,   53,   53,   53,   52,
  107971                 :  /*  1030 */    52,   51,   51,   51,   50,  238,  313,  587,  411,  414,
  107972                 :  /*  1040 */   411,  207,  611,  476,  171,  472,  160,  123,  601,   91,
  107973                 :  /*  1050 */   323,  261,   15,  414,  464,  414,  411,  621,  411,  354,
  107974                 :  /*  1060 */   222,  411,  601,   74,  601,   90,  590,  584,  159,  264,
  107975                 :  /*  1070 */   158,  414,  461,  414,  621,  600,  414,  121,  120,   25,
  107976                 :  /*  1080 */   601,   89,  601,  101,  621,  601,   88,   47,  582,  581,
  107977                 :  /*  1090 */   583,  583,   54,   54,   55,   55,   55,   55,  544,   53,
  107978                 :  /*  1100 */    53,   53,   53,   52,   52,   51,   51,   51,   50,  238,
  107979                 :  /*  1110 */    43,  405,  263,    3,  610,  264,  140,  415,  622,   24,
  107980                 :  /*  1120 */   410,   11,  456,  594,  118,  155,  219,  452,  408,  621,
  107981                 :  /*  1130 */   621,  621,  156,   43,  405,  621,    3,  286,  621,  113,
  107982                 :  /*  1140 */   415,  622,  111,  445,  411,  400,  557,  403,  545,   10,
  107983                 :  /*  1150 */   411,  408,  264,  110,  205,  436,  541,  566,  453,  414,
  107984                 :  /*  1160 */   621,  621,   63,  621,  435,  414,  411,  621,  601,   94,
  107985                 :  /*  1170 */   403,  621,  411,  337,  601,   86,  150,   40,   41,  534,
  107986                 :  /*  1180 */   566,  414,  242,  264,   42,  413,  412,  414,  600,  595,
  107987                 :  /*  1190 */   601,   85,  191,  333,  107,  451,  601,   84,  621,  539,
  107988                 :  /*  1200 */    40,   41,  420,  230,  411,  149,  316,   42,  413,  412,
  107989                 :  /*  1210 */   398,  127,  595,  315,  621,  399,  278,  625,  181,  414,
  107990                 :  /*  1220 */   593,  593,  593,  592,  591,   14,  450,  411,  601,   71,
  107991                 :  /*  1230 */   240,  621,   43,  405,  264,    3,  615,  180,  264,  415,
  107992                 :  /*  1240 */   622,  614,  414,  593,  593,  593,  592,  591,   14,  621,
  107993                 :  /*  1250 */   408,  601,   70,  621,  417,   33,  405,  613,    3,  411,
  107994                 :  /*  1260 */   264,  411,  415,  622,  418,  626,  178,  509,    8,  403,
  107995                 :  /*  1270 */   241,  416,  126,  408,  414,  621,  414,  449,  208,  566,
  107996                 :  /*  1280 */   240,  221,  621,  601,   83,  601,   82,  599,  297,  277,
  107997                 :  /*  1290 */   296,   30,  403,   31,  395,  264,  295,  397,  489,   40,
  107998                 :  /*  1300 */    41,  411,  566,  220,  621,  294,   42,  413,  412,  271,
  107999                 :  /*  1310 */   621,  595,  600,  621,   59,   60,  414,  269,  267,  623,
  108000                 :  /*  1320 */   622,   36,   40,   41,  621,  601,   81,  598,  235,   42,
  108001                 :  /*  1330 */   413,  412,  621,  621,  595,  265,  344,  411,  248,  556,
  108002                 :  /*  1340 */   173,  185,  593,  593,  593,  592,  591,   14,  218,   29,
  108003                 :  /*  1350 */   621,  543,  414,  305,  304,  303,  179,  301,  411,  566,
  108004                 :  /*  1360 */   454,  601,   80,  289,  335,  593,  593,  593,  592,  591,
  108005                 :  /*  1370 */    14,  411,  287,  414,  151,  392,  246,  260,  411,  196,
  108006                 :  /*  1380 */   195,  523,  601,   69,  411,  245,  414,  526,  537,  285,
  108007                 :  /*  1390 */   389,  595,  621,  414,  536,  601,   17,  362,  153,  414,
  108008                 :  /*  1400 */   466,  463,  601,   78,  154,  414,  462,  152,  601,   77,
  108009                 :  /*  1410 */   355,  255,  621,  455,  601,    9,  621,  386,  444,  517,
  108010                 :  /*  1420 */   247,  621,  593,  593,  593,  621,  621,  244,  621,  243,
  108011                 :  /*  1430 */   430,  518,  292,  621,  329,  621,  145,  393,  280,  513,
  108012                 :  /*  1440 */   291,  131,  621,  514,  621,  621,  311,  621,  259,  346,
  108013                 :  /*  1450 */   249,  621,  621,  229,  314,  621,  228,  512,  227,  240,
  108014                 :  /*  1460 */   494,  488,  310,  164,  487,  486,  373,  480,  163,  262,
  108015                 :  /*  1470 */   369,  371,  162,   26,  212,  478,  477,  161,  141,  363,
  108016                 :  /*  1480 */   467,  122,  339,  187,  119,  348,  347,  117,  116,  115,
  108017                 :  /*  1490 */   114,  112,  182,  457,  320,   22,  433,  432,  448,   19,
  108018                 :  /*  1500 */   609,  431,  428,   62,  193,  596,  573,  298,  555,  552,
  108019                 :  /*  1510 */   571,  404,  290,  380,  498,  510,  495,  306,  281,  499,
  108020                 :  /*  1520 */   250,    5,  497,  460,  345,  447,  569,  550,  238,  299,
  108021                 :  /*  1530 */   527,  525,  508,  961,  502,  501,  961,  401,  961,  211,
  108022                 :  /*  1540 */   490,  356,  256,  961,  483,  961,  961,  961,  961,  961,
  108023                 :  /*  1550 */   961,  961,  961,  961,  961,  961,  370,
  108024                 : };
  108025                 : static const YYCODETYPE yy_lookahead[] = {
  108026                 :  /*     0 */    19,  142,  143,  144,  145,   24,    1,   26,   77,   78,
  108027                 :  /*    10 */    79,   80,   81,   82,   83,   84,   85,   86,   87,   88,
  108028                 :  /*    20 */    89,   90,   91,   92,   15,   98,   26,   27,    7,    8,
  108029                 :  /*    30 */    49,   50,   77,   78,   79,   80,  109,   82,   83,   84,
  108030                 :  /*    40 */    85,   86,   87,   88,   89,   90,   91,   92,   22,   68,
  108031                 :  /*    50 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
  108032                 :  /*    60 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
  108033                 :  /*    70 */    89,   90,   91,   92,   19,   94,  112,   19,  114,  115,
  108034                 :  /*    80 */    25,   82,   83,   84,   85,   86,   87,   88,   89,   90,
  108035                 :  /*    90 */    91,   92,   19,   22,   94,   95,   96,  150,  150,   99,
  108036                 :  /*   100 */   100,  101,   76,  150,   49,   50,  105,  106,  107,   54,
  108037                 :  /*   110 */   110,  158,  165,  165,  161,  162,   26,   27,  165,  113,
  108038                 :  /*   120 */    16,  174,  175,   68,   69,   70,   71,   72,   73,   74,
  108039                 :  /*   130 */    75,   76,   77,   78,   79,   80,  118,   82,   83,   84,
  108040                 :  /*   140 */    85,   86,   87,   88,   89,   90,   91,   92,   19,   23,
  108041                 :  /*   150 */    92,   97,   98,   24,   96,   97,   98,   99,  100,  101,
  108042                 :  /*   160 */   102,   25,   97,  216,   60,   92,   62,  109,  221,   25,
  108043                 :  /*   170 */    97,   98,   99,  100,  101,  102,   86,   87,   49,   50,
  108044                 :  /*   180 */   118,  116,  109,   25,   94,   95,   32,   97,   88,   89,
  108045                 :  /*   190 */    90,   91,   92,  128,  104,   41,  106,   68,   69,   70,
  108046                 :  /*   200 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
  108047                 :  /*   210 */    11,   82,   83,   84,   85,   86,   87,   88,   89,   90,
  108048                 :  /*   220 */    91,   92,   19,   19,   86,   87,   88,   89,   90,   91,
  108049                 :  /*   230 */    92,   27,   96,  150,   66,   99,  100,  101,  112,  150,
  108050                 :  /*   240 */   114,  115,  138,  150,  161,  162,  110,  103,  165,  222,
  108051                 :  /*   250 */   223,  224,   49,   50,  165,   22,   57,   24,  165,  170,
  108052                 :  /*   260 */   171,  118,   94,  170,  171,   23,   98,   25,  185,  186,
  108053                 :  /*   270 */   243,   68,   69,   70,   71,   72,   73,   74,   75,   76,
  108054                 :  /*   280 */    77,   78,   79,   80,  126,   82,   83,   84,   85,   86,
  108055                 :  /*   290 */    87,   88,   89,   90,   91,   92,   19,  129,  130,  131,
  108056                 :  /*   300 */    88,   23,  172,  173,  105,  106,  107,  150,   22,   26,
  108057                 :  /*   310 */    27,  181,   26,   27,   22,  116,   26,   27,   26,  230,
  108058                 :  /*   320 */   231,  197,  165,  230,  231,  113,   49,   50,  204,  117,
  108059                 :  /*   330 */    96,  174,  175,   99,  100,  101,   22,   26,   27,  136,
  108060                 :  /*   340 */    26,   27,  118,   16,  110,   68,   69,   70,   71,   72,
  108061                 :  /*   350 */    73,   74,   75,   76,   77,   78,   79,   80,  118,   82,
  108062                 :  /*   360 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
  108063                 :  /*   370 */    19,  214,  215,  150,   23,   23,  155,   94,   95,   22,
  108064                 :  /*   380 */    94,   95,  116,  160,   94,   95,   94,   60,  165,   62,
  108065                 :  /*   390 */   112,   26,  114,  115,  128,   23,   36,  174,  175,   88,
  108066                 :  /*   400 */    49,   50,   57,  120,   22,   94,   95,   23,   94,   95,
  108067                 :  /*   410 */   120,   51,   25,  136,  169,  170,  171,  194,   58,   68,
  108068                 :  /*   420 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
  108069                 :  /*   430 */    79,   80,   23,   82,   83,   84,   85,   86,   87,   88,
  108070                 :  /*   440 */    89,   90,   91,   92,   19,  150,   12,   12,   23,  228,
  108071                 :  /*   450 */   105,  106,  107,   23,  233,   25,  165,   19,  150,   94,
  108072                 :  /*   460 */   165,  116,   28,   28,  112,  174,  114,  115,  108,  174,
  108073                 :  /*   470 */   175,   26,   27,  165,   49,   50,  231,   11,   44,   44,
  108074                 :  /*   480 */    46,   46,  174,  175,  112,  160,  114,  115,   50,   22,
  108075                 :  /*   490 */    23,   57,   25,   68,   69,   70,   71,   72,   73,   74,
  108076                 :  /*   500 */    75,   76,   77,   78,   79,   80,  119,   82,   83,   84,
  108077                 :  /*   510 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  194,
  108078                 :  /*   520 */   225,   23,   23,  215,   19,   95,  105,  106,  107,  150,
  108079                 :  /*   530 */    23,  150,   27,   23,   67,   25,  150,  206,  207,   94,
  108080                 :  /*   540 */    95,  166,  104,  218,  165,   22,  165,  109,   49,   50,
  108081                 :  /*   550 */   120,  165,   25,  174,  175,  174,  175,   23,   21,  234,
  108082                 :  /*   560 */   174,  175,   22,   23,  239,   25,   25,   68,   69,   70,
  108083                 :  /*   570 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
  108084                 :  /*   580 */   205,   82,   83,   84,   85,   86,   87,   88,   89,   90,
  108085                 :  /*   590 */    91,   92,   19,   22,   23,  216,   23,  222,  223,  224,
  108086                 :  /*   600 */    63,  220,   35,  150,  150,  163,  220,   67,  166,  167,
  108087                 :  /*   610 */   168,  150,  169,  170,  171,  161,  162,   25,  165,  165,
  108088                 :  /*   620 */   150,  113,   49,   50,   25,  117,  165,  174,  175,   35,
  108089                 :  /*   630 */     7,    8,    9,  160,  160,  165,  120,  100,   67,  247,
  108090                 :  /*   640 */   248,   68,   69,   70,   71,   72,   73,   74,   75,   76,
  108091                 :  /*   650 */    77,   78,   79,   80,  193,   82,   83,   84,   85,   86,
  108092                 :  /*   660 */    87,   88,   89,   90,   91,   92,   19,  194,  194,  150,
  108093                 :  /*   670 */   135,   24,  137,   35,  231,  138,  150,  129,  130,  206,
  108094                 :  /*   680 */   207,   30,   27,  213,  165,   34,  118,   95,    0,    1,
  108095                 :  /*   690 */     2,  165,  218,  174,  175,   50,   49,   50,   22,   48,
  108096                 :  /*   700 */   174,  175,   22,   23,   23,  244,  222,  223,  224,  166,
  108097                 :  /*   710 */   167,  168,  120,  239,   23,   68,   69,   70,   71,   72,
  108098                 :  /*   720 */    73,   74,   75,   76,   77,   78,   79,   80,  150,   82,
  108099                 :  /*   730 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
  108100                 :  /*   740 */    19,  150,  173,  165,  181,  182,   24,   67,   26,  104,
  108101                 :  /*   750 */   181,  188,  174,  175,  150,   39,  165,  150,   52,  150,
  108102                 :  /*   760 */   150,  150,  150,  144,  145,  174,  175,  249,  250,  165,
  108103                 :  /*   770 */    49,   50,  165,   52,  165,  165,  165,  165,  174,  175,
  108104                 :  /*   780 */    29,  174,  175,  174,  175,  174,  175,  160,   22,   68,
  108105                 :  /*   790 */    69,   70,   71,   72,   73,   74,   75,   76,   77,   78,
  108106                 :  /*   800 */    79,   80,  150,   82,   83,   84,   85,   86,   87,   88,
  108107                 :  /*   810 */    89,   90,   91,   92,   19,  150,   94,  165,  150,  150,
  108108                 :  /*   820 */   160,  194,  150,  213,  160,   52,  174,  175,   23,   23,
  108109                 :  /*   830 */   165,   25,   22,  165,  165,  150,  150,  165,   52,  174,
  108110                 :  /*   840 */   175,   22,  174,  175,   49,   50,  174,  175,  190,  191,
  108111                 :  /*   850 */   165,  165,  240,   23,  194,   25,  187,  109,  194,  174,
  108112                 :  /*   860 */   175,  190,  191,   68,   69,   70,   71,   72,   73,   74,
  108113                 :  /*   870 */    75,   76,   77,   78,   79,   80,  150,   82,   83,   84,
  108114                 :  /*   880 */    85,   86,   87,   88,   89,   90,   91,   92,   19,  150,
  108115                 :  /*   890 */    22,  165,  150,   23,  150,   25,  150,  166,   91,   92,
  108116                 :  /*   900 */   174,  175,   22,  217,  165,  150,  102,  165,  150,  165,
  108117                 :  /*   910 */   150,  165,  150,  174,  175,   19,  174,  175,   49,   50,
  108118                 :  /*   920 */   165,   86,   87,  165,   23,  165,   25,  165,   24,  174,
  108119                 :  /*   930 */   175,  187,  174,  175,  174,  175,  205,   68,   69,   70,
  108120                 :  /*   940 */    71,   72,   73,   74,   75,   76,   77,   78,   79,   80,
  108121                 :  /*   950 */   150,   82,   83,   84,   85,   86,   87,   88,   89,   90,
  108122                 :  /*   960 */    91,   92,   19,  150,  150,  165,  150,  150,  166,   23,
  108123                 :  /*   970 */   150,   25,  160,   20,  174,  175,    1,    2,  165,  165,
  108124                 :  /*   980 */   104,  165,  165,   43,  150,  165,  240,  150,   49,   50,
  108125                 :  /*   990 */   174,  175,   49,   50,   23,   23,   25,   25,   53,  165,
  108126                 :  /*  1000 */   187,  187,  165,   23,  187,   25,  194,  205,  174,  175,
  108127                 :  /*  1010 */    71,   72,   69,   70,   71,   72,   73,   74,   75,   76,
  108128                 :  /*  1020 */    77,   78,   79,   80,  150,   82,   83,   84,   85,   86,
  108129                 :  /*  1030 */    87,   88,   89,   90,   91,   92,   19,   98,  150,  165,
  108130                 :  /*  1040 */   150,  160,  150,   59,   25,   53,  104,   22,  174,  175,
  108131                 :  /*  1050 */   213,  138,    5,  165,    1,  165,  150,  165,  150,  150,
  108132                 :  /*  1060 */   240,  150,  174,  175,  174,  175,   49,   50,  118,  150,
  108133                 :  /*  1070 */    35,  165,   27,  165,  165,  194,  165,  108,  127,   76,
  108134                 :  /*  1080 */   174,  175,  174,  175,  165,  174,  175,   70,   71,   72,
  108135                 :  /*  1090 */    73,   74,   75,   76,   77,   78,   79,   80,  166,   82,
  108136                 :  /*  1100 */    83,   84,   85,   86,   87,   88,   89,   90,   91,   92,
  108137                 :  /*  1110 */    19,   20,  193,   22,  150,  150,  150,   26,   27,   76,
  108138                 :  /*  1120 */   150,   22,    1,  150,  119,  121,  217,   20,   37,  165,
  108139                 :  /*  1130 */   165,  165,   16,   19,   20,  165,   22,  205,  165,  119,
  108140                 :  /*  1140 */    26,   27,  108,  128,  150,  150,  150,   56,  150,   22,
  108141                 :  /*  1150 */   150,   37,  150,  127,  160,   23,  150,   66,  193,  165,
  108142                 :  /*  1160 */   165,  165,   16,  165,   23,  165,  150,  165,  174,  175,
  108143                 :  /*  1170 */    56,  165,  150,   65,  174,  175,   15,   86,   87,   88,
  108144                 :  /*  1180 */    66,  165,  140,  150,   93,   94,   95,  165,  194,   98,
  108145                 :  /*  1190 */   174,  175,   22,    3,  164,  193,  174,  175,  165,  150,
  108146                 :  /*  1200 */    86,   87,    4,  180,  150,  248,  251,   93,   94,   95,
  108147                 :  /*  1210 */   216,  180,   98,  251,  165,  221,  150,  149,    6,  165,
  108148                 :  /*  1220 */   129,  130,  131,  132,  133,  134,  193,  150,  174,  175,
  108149                 :  /*  1230 */   116,  165,   19,   20,  150,   22,  149,  151,  150,   26,
  108150                 :  /*  1240 */    27,  149,  165,  129,  130,  131,  132,  133,  134,  165,
  108151                 :  /*  1250 */    37,  174,  175,  165,  149,   19,   20,   13,   22,  150,
  108152                 :  /*  1260 */   150,  150,   26,   27,  146,  147,  151,  150,   25,   56,
  108153                 :  /*  1270 */   152,  159,  154,   37,  165,  165,  165,  193,  160,   66,
  108154                 :  /*  1280 */   116,  193,  165,  174,  175,  174,  175,  194,  199,  150,
  108155                 :  /*  1290 */   200,  126,   56,  124,  123,  150,  201,  122,  150,   86,
  108156                 :  /*  1300 */    87,  150,   66,  193,  165,  202,   93,   94,   95,  150,
  108157                 :  /*  1310 */   165,   98,  194,  165,  125,   22,  165,  150,  150,   26,
  108158                 :  /*  1320 */    27,  135,   86,   87,  165,  174,  175,  203,  226,   93,
  108159                 :  /*  1330 */    94,   95,  165,  165,   98,  150,  218,  150,  193,  157,
  108160                 :  /*  1340 */   118,  157,  129,  130,  131,  132,  133,  134,    5,  104,
  108161                 :  /*  1350 */   165,  211,  165,   10,   11,   12,   13,   14,  150,   66,
  108162                 :  /*  1360 */    17,  174,  175,  210,  246,  129,  130,  131,  132,  133,
  108163                 :  /*  1370 */   134,  150,  210,  165,   31,  121,   33,  150,  150,   86,
  108164                 :  /*  1380 */    87,  176,  174,  175,  150,   42,  165,   94,  211,  210,
  108165                 :  /*  1390 */   150,   98,  165,  165,  211,  174,  175,  150,   55,  165,
  108166                 :  /*  1400 */    57,  150,  174,  175,   61,  165,  150,   64,  174,  175,
  108167                 :  /*  1410 */   150,  150,  165,  150,  174,  175,  165,  104,  150,  184,
  108168                 :  /*  1420 */   150,  165,  129,  130,  131,  165,  165,  150,  165,  150,
  108169                 :  /*  1430 */   150,  176,  150,  165,   47,  165,  150,  150,  176,  103,
  108170                 :  /*  1440 */   150,   22,  165,  178,  165,  165,  179,  165,  105,  106,
  108171                 :  /*  1450 */   107,  165,  165,  229,  111,  165,   92,  176,  229,  116,
  108172                 :  /*  1460 */   184,  176,  179,  156,  176,  176,   18,  157,  156,  237,
  108173                 :  /*  1470 */    45,  157,  156,  135,  157,  157,  238,  156,   68,  157,
  108174                 :  /*  1480 */   189,  189,  139,  219,   22,  157,   18,  192,  192,  192,
  108175                 :  /*  1490 */   192,  189,  219,  199,  157,  242,   40,  157,  199,  242,
  108176                 :  /*  1500 */   153,  157,   38,  245,  196,  166,  232,  198,  177,  177,
  108177                 :  /*  1510 */   232,  227,  209,  178,  166,  182,  166,  148,  177,  177,
  108178                 :  /*  1520 */   209,  196,  177,  199,  209,  199,  166,  208,   92,  195,
  108179                 :  /*  1530 */   174,  174,  183,  252,  183,  183,  252,  191,  252,  235,
  108180                 :  /*  1540 */   186,  241,  241,  252,  186,  252,  252,  252,  252,  252,
  108181                 :  /*  1550 */   252,  252,  252,  252,  252,  252,  236,
  108182                 : };
  108183                 : #define YY_SHIFT_USE_DFLT (-74)
  108184                 : #define YY_SHIFT_COUNT (418)
  108185                 : #define YY_SHIFT_MIN   (-73)
  108186                 : #define YY_SHIFT_MAX   (1468)
  108187                 : static const short yy_shift_ofst[] = {
  108188                 :  /*     0 */   975, 1114, 1343, 1114, 1213, 1213,   90,   90,    0,  -19,
  108189                 :  /*    10 */  1213, 1213, 1213, 1213, 1213,  345,  445,  721, 1091, 1213,
  108190                 :  /*    20 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
  108191                 :  /*    30 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
  108192                 :  /*    40 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1236, 1213, 1213,
  108193                 :  /*    50 */  1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213,
  108194                 :  /*    60 */  1213,  199,  445,  445,  835,  835,  365, 1164,   55,  647,
  108195                 :  /*    70 */   573,  499,  425,  351,  277,  203,  129,  795,  795,  795,
  108196                 :  /*    80 */   795,  795,  795,  795,  795,  795,  795,  795,  795,  795,
  108197                 :  /*    90 */   795,  795,  795,  795,  795,  869,  795,  943, 1017, 1017,
  108198                 :  /*   100 */   -69,  -45,  -45,  -45,  -45,  -45,   -1,   58,  138,  100,
  108199                 :  /*   110 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
  108200                 :  /*   120 */   445,  445,  445,  445,  445,  445,  537,  438,  445,  445,
  108201                 :  /*   130 */   445,  445,  445,  365,  807, 1436,  -74,  -74,  -74, 1293,
  108202                 :  /*   140 */    73,  434,  434,  311,  314,  290,  283,  286,  540,  467,
  108203                 :  /*   150 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
  108204                 :  /*   160 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
  108205                 :  /*   170 */   445,  445,  445,  445,  445,  445,  445,  445,  445,  445,
  108206                 :  /*   180 */   445,  445,   65,  722,  722,  722,  688,  266, 1164, 1164,
  108207                 :  /*   190 */  1164,  -74,  -74,  -74,  136,  168,  168,  234,  360,  360,
  108208                 :  /*   200 */   360,  430,  372,  435,  352,  278,  126,  -36,  -36,  -36,
  108209                 :  /*   210 */   -36,  421,  651,  -36,  -36,  592,  292,  212,  623,  158,
  108210                 :  /*   220 */   204,  204,  505,  158,  505,  144,  365,  154,  365,  154,
  108211                 :  /*   230 */   645,  154,  204,  154,  154,  535,  548,  548,  365,  387,
  108212                 :  /*   240 */   508,  233, 1464, 1222, 1222, 1456, 1456, 1222, 1462, 1410,
  108213                 :  /*   250 */  1165, 1468, 1468, 1468, 1468, 1222, 1165, 1462, 1410, 1410,
  108214                 :  /*   260 */  1222, 1448, 1338, 1425, 1222, 1222, 1448, 1222, 1448, 1222,
  108215                 :  /*   270 */  1448, 1419, 1313, 1313, 1313, 1387, 1364, 1364, 1419, 1313,
  108216                 :  /*   280 */  1336, 1313, 1387, 1313, 1313, 1254, 1245, 1254, 1245, 1254,
  108217                 :  /*   290 */  1245, 1222, 1222, 1186, 1189, 1175, 1169, 1171, 1165, 1164,
  108218                 :  /*   300 */  1243, 1244, 1244, 1212, 1212, 1212, 1212,  -74,  -74,  -74,
  108219                 :  /*   310 */   -74,  -74,  -74,  939,  104,  680,  571,  327,    1,  980,
  108220                 :  /*   320 */    26,  972,  971,  946,  901,  870,  830,  806,   54,   21,
  108221                 :  /*   330 */   -73,  510,  242, 1198, 1190, 1170, 1042, 1161, 1108, 1146,
  108222                 :  /*   340 */  1141, 1132, 1015, 1127, 1026, 1034, 1020, 1107, 1004, 1116,
  108223                 :  /*   350 */  1121, 1005, 1099,  951, 1043, 1003,  969, 1045, 1035,  950,
  108224                 :  /*   360 */  1053, 1047, 1025,  942,  913,  992, 1019,  945,  984,  940,
  108225                 :  /*   370 */   876,  904,  953,  896,  748,  804,  880,  786,  868,  819,
  108226                 :  /*   380 */   805,  810,  773,  751,  766,  706,  716,  691,  681,  568,
  108227                 :  /*   390 */   655,  638,  676,  516,  541,  594,  599,  567,  541,  534,
  108228                 :  /*   400 */   507,  527,  498,  523,  466,  382,  409,  384,  357,    6,
  108229                 :  /*   410 */   240,  224,  143,   62,   18,   71,   39,    9,    5,
  108230                 : };
  108231                 : #define YY_REDUCE_USE_DFLT (-142)
  108232                 : #define YY_REDUCE_COUNT (312)
  108233                 : #define YY_REDUCE_MIN   (-141)
  108234                 : #define YY_REDUCE_MAX   (1369)
  108235                 : static const short yy_reduce_ofst[] = {
  108236                 :  /*     0 */  -141,  994, 1118,  223,  157,  -53,   93,   89,   83,  375,
  108237                 :  /*    10 */   386,  381,  379,  308,  295,  325,  -47,   27, 1240, 1234,
  108238                 :  /*    20 */  1228, 1221, 1208, 1187, 1151, 1111, 1109, 1077, 1054, 1022,
  108239                 :  /*    30 */  1016, 1000,  911,  908,  906,  890,  888,  874,  834,  816,
  108240                 :  /*    40 */   800,  760,  758,  755,  742,  739,  726,  685,  672,  668,
  108241                 :  /*    50 */   665,  652,  611,  609,  607,  604,  591,  578,  526,  519,
  108242                 :  /*    60 */   453,  474,  454,  461,  443,  245,  442,  473,  484,  484,
  108243                 :  /*    70 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
  108244                 :  /*    80 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
  108245                 :  /*    90 */   484,  484,  484,  484,  484,  484,  484,  484,  484,  484,
  108246                 :  /*   100 */   484,  484,  484,  484,  484,  484,  484,  130,  484,  484,
  108247                 :  /*   110 */  1145,  909, 1110, 1088, 1084, 1033, 1002,  965,  820,  837,
  108248                 :  /*   120 */   746,  686,  612,  817,  610,  919,  221,  563,  814,  813,
  108249                 :  /*   130 */   744,  669,  470,  543,  484,  484,  484,  484,  484,  291,
  108250                 :  /*   140 */   569,  671,  658,  970, 1290, 1287, 1286, 1282,  518,  518,
  108251                 :  /*   150 */  1280, 1279, 1277, 1270, 1268, 1263, 1261, 1260, 1256, 1251,
  108252                 :  /*   160 */  1247, 1227, 1185, 1168, 1167, 1159, 1148, 1139, 1117, 1066,
  108253                 :  /*   170 */  1049, 1006,  998,  996,  995,  973,  970,  966,  964,  892,
  108254                 :  /*   180 */   762,  -52,  881,  932,  802,  731,  619,  812,  664,  660,
  108255                 :  /*   190 */   627,  392,  331,  124, 1358, 1357, 1356, 1354, 1352, 1351,
  108256                 :  /*   200 */  1349, 1319, 1334, 1346, 1334, 1334, 1334, 1334, 1334, 1334,
  108257                 :  /*   210 */  1334, 1320, 1304, 1334, 1334, 1319, 1360, 1325, 1369, 1326,
  108258                 :  /*   220 */  1315, 1311, 1301, 1324, 1300, 1335, 1350, 1345, 1348, 1342,
  108259                 :  /*   230 */  1333, 1341, 1303, 1332, 1331, 1284, 1278, 1274, 1339, 1309,
  108260                 :  /*   240 */  1308, 1347, 1258, 1344, 1340, 1257, 1253, 1337, 1273, 1302,
  108261                 :  /*   250 */  1299, 1298, 1297, 1296, 1295, 1328, 1294, 1264, 1292, 1291,
  108262                 :  /*   260 */  1322, 1321, 1238, 1232, 1318, 1317, 1316, 1314, 1312, 1310,
  108263                 :  /*   270 */  1307, 1283, 1289, 1288, 1285, 1276, 1229, 1224, 1267, 1281,
  108264                 :  /*   280 */  1265, 1262, 1235, 1255, 1205, 1183, 1179, 1177, 1162, 1140,
  108265                 :  /*   290 */  1153, 1184, 1182, 1102, 1124, 1103, 1095, 1090, 1089, 1093,
  108266                 :  /*   300 */  1112, 1115, 1086, 1105, 1092, 1087, 1068,  962,  955,  957,
  108267                 :  /*   310 */  1031, 1023, 1030,
  108268                 : };
  108269                 : static const YYACTIONTYPE yy_default[] = {
  108270                 :  /*     0 */   635,  870,  959,  959,  959,  870,  899,  899,  959,  759,
  108271                 :  /*    10 */   959,  959,  959,  959,  868,  959,  959,  933,  959,  959,
  108272                 :  /*    20 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108273                 :  /*    30 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108274                 :  /*    40 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108275                 :  /*    50 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108276                 :  /*    60 */   959,  959,  959,  959,  899,  899,  674,  763,  794,  959,
  108277                 :  /*    70 */   959,  959,  959,  959,  959,  959,  959,  932,  934,  809,
  108278                 :  /*    80 */   808,  802,  801,  912,  774,  799,  792,  785,  796,  871,
  108279                 :  /*    90 */   864,  865,  863,  867,  872,  959,  795,  831,  848,  830,
  108280                 :  /*   100 */   842,  847,  854,  846,  843,  833,  832,  666,  834,  835,
  108281                 :  /*   110 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108282                 :  /*   120 */   959,  959,  959,  959,  959,  959,  661,  728,  959,  959,
  108283                 :  /*   130 */   959,  959,  959,  959,  836,  837,  851,  850,  849,  959,
  108284                 :  /*   140 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108285                 :  /*   150 */   959,  939,  937,  959,  883,  959,  959,  959,  959,  959,
  108286                 :  /*   160 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108287                 :  /*   170 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108288                 :  /*   180 */   959,  641,  959,  759,  759,  759,  635,  959,  959,  959,
  108289                 :  /*   190 */   959,  951,  763,  753,  719,  959,  959,  959,  959,  959,
  108290                 :  /*   200 */   959,  959,  959,  959,  959,  959,  959,  804,  742,  922,
  108291                 :  /*   210 */   924,  959,  905,  740,  663,  761,  676,  751,  643,  798,
  108292                 :  /*   220 */   776,  776,  917,  798,  917,  700,  959,  788,  959,  788,
  108293                 :  /*   230 */   697,  788,  776,  788,  788,  866,  959,  959,  959,  760,
  108294                 :  /*   240 */   751,  959,  944,  767,  767,  936,  936,  767,  810,  732,
  108295                 :  /*   250 */   798,  739,  739,  739,  739,  767,  798,  810,  732,  732,
  108296                 :  /*   260 */   767,  658,  911,  909,  767,  767,  658,  767,  658,  767,
  108297                 :  /*   270 */   658,  876,  730,  730,  730,  715,  880,  880,  876,  730,
  108298                 :  /*   280 */   700,  730,  715,  730,  730,  780,  775,  780,  775,  780,
  108299                 :  /*   290 */   775,  767,  767,  959,  793,  781,  791,  789,  798,  959,
  108300                 :  /*   300 */   718,  651,  651,  640,  640,  640,  640,  956,  956,  951,
  108301                 :  /*   310 */   702,  702,  684,  959,  959,  959,  959,  959,  959,  959,
  108302                 :  /*   320 */   885,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108303                 :  /*   330 */   959,  959,  959,  959,  636,  946,  959,  959,  943,  959,
  108304                 :  /*   340 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108305                 :  /*   350 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  915,
  108306                 :  /*   360 */   959,  959,  959,  959,  959,  959,  908,  907,  959,  959,
  108307                 :  /*   370 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108308                 :  /*   380 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  959,
  108309                 :  /*   390 */   959,  959,  959,  959,  790,  959,  782,  959,  869,  959,
  108310                 :  /*   400 */   959,  959,  959,  959,  959,  959,  959,  959,  959,  745,
  108311                 :  /*   410 */   819,  959,  818,  822,  817,  668,  959,  649,  959,  632,
  108312                 :  /*   420 */   637,  955,  958,  957,  954,  953,  952,  947,  945,  942,
  108313                 :  /*   430 */   941,  940,  938,  935,  931,  889,  887,  894,  893,  892,
  108314                 :  /*   440 */   891,  890,  888,  886,  884,  805,  803,  800,  797,  930,
  108315                 :  /*   450 */   882,  741,  738,  737,  657,  948,  914,  923,  921,  811,
  108316                 :  /*   460 */   920,  919,  918,  916,  913,  900,  807,  806,  733,  874,
  108317                 :  /*   470 */   873,  660,  904,  903,  902,  906,  910,  901,  769,  659,
  108318                 :  /*   480 */   656,  665,  722,  721,  729,  727,  726,  725,  724,  723,
  108319                 :  /*   490 */   720,  667,  675,  686,  714,  699,  698,  879,  881,  878,
  108320                 :  /*   500 */   877,  707,  706,  712,  711,  710,  709,  708,  705,  704,
  108321                 :  /*   510 */   703,  696,  695,  701,  694,  717,  716,  713,  693,  736,
  108322                 :  /*   520 */   735,  734,  731,  692,  691,  690,  822,  689,  688,  828,
  108323                 :  /*   530 */   827,  815,  858,  756,  755,  754,  766,  765,  778,  777,
  108324                 :  /*   540 */   813,  812,  779,  764,  758,  757,  773,  772,  771,  770,
  108325                 :  /*   550 */   762,  752,  784,  787,  786,  783,  860,  768,  857,  929,
  108326                 :  /*   560 */   928,  927,  926,  925,  862,  861,  829,  826,  679,  680,
  108327                 :  /*   570 */   898,  896,  897,  895,  682,  681,  678,  677,  859,  747,
  108328                 :  /*   580 */   746,  855,  852,  844,  840,  856,  853,  845,  841,  839,
  108329                 :  /*   590 */   838,  824,  823,  821,  820,  816,  825,  670,  748,  744,
  108330                 :  /*   600 */   743,  814,  750,  749,  687,  685,  683,  664,  662,  655,
  108331                 :  /*   610 */   653,  652,  654,  650,  648,  647,  646,  645,  644,  673,
  108332                 :  /*   620 */   672,  671,  669,  668,  642,  639,  638,  634,  633,  631,
  108333                 : };
  108334                 : 
  108335                 : /* The next table maps tokens into fallback tokens.  If a construct
  108336                 : ** like the following:
  108337                 : ** 
  108338                 : **      %fallback ID X Y Z.
  108339                 : **
  108340                 : ** appears in the grammar, then ID becomes a fallback token for X, Y,
  108341                 : ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
  108342                 : ** but it does not parse, the type of the token is changed to ID and
  108343                 : ** the parse is retried before an error is thrown.
  108344                 : */
  108345                 : #ifdef YYFALLBACK
  108346                 : static const YYCODETYPE yyFallback[] = {
  108347                 :     0,  /*          $ => nothing */
  108348                 :     0,  /*       SEMI => nothing */
  108349                 :    26,  /*    EXPLAIN => ID */
  108350                 :    26,  /*      QUERY => ID */
  108351                 :    26,  /*       PLAN => ID */
  108352                 :    26,  /*      BEGIN => ID */
  108353                 :     0,  /* TRANSACTION => nothing */
  108354                 :    26,  /*   DEFERRED => ID */
  108355                 :    26,  /*  IMMEDIATE => ID */
  108356                 :    26,  /*  EXCLUSIVE => ID */
  108357                 :     0,  /*     COMMIT => nothing */
  108358                 :    26,  /*        END => ID */
  108359                 :    26,  /*   ROLLBACK => ID */
  108360                 :    26,  /*  SAVEPOINT => ID */
  108361                 :    26,  /*    RELEASE => ID */
  108362                 :     0,  /*         TO => nothing */
  108363                 :     0,  /*      TABLE => nothing */
  108364                 :     0,  /*     CREATE => nothing */
  108365                 :    26,  /*         IF => ID */
  108366                 :     0,  /*        NOT => nothing */
  108367                 :     0,  /*     EXISTS => nothing */
  108368                 :    26,  /*       TEMP => ID */
  108369                 :     0,  /*         LP => nothing */
  108370                 :     0,  /*         RP => nothing */
  108371                 :     0,  /*         AS => nothing */
  108372                 :     0,  /*      COMMA => nothing */
  108373                 :     0,  /*         ID => nothing */
  108374                 :     0,  /*    INDEXED => nothing */
  108375                 :    26,  /*      ABORT => ID */
  108376                 :    26,  /*     ACTION => ID */
  108377                 :    26,  /*      AFTER => ID */
  108378                 :    26,  /*    ANALYZE => ID */
  108379                 :    26,  /*        ASC => ID */
  108380                 :    26,  /*     ATTACH => ID */
  108381                 :    26,  /*     BEFORE => ID */
  108382                 :    26,  /*         BY => ID */
  108383                 :    26,  /*    CASCADE => ID */
  108384                 :    26,  /*       CAST => ID */
  108385                 :    26,  /*   COLUMNKW => ID */
  108386                 :    26,  /*   CONFLICT => ID */
  108387                 :    26,  /*   DATABASE => ID */
  108388                 :    26,  /*       DESC => ID */
  108389                 :    26,  /*     DETACH => ID */
  108390                 :    26,  /*       EACH => ID */
  108391                 :    26,  /*       FAIL => ID */
  108392                 :    26,  /*        FOR => ID */
  108393                 :    26,  /*     IGNORE => ID */
  108394                 :    26,  /*  INITIALLY => ID */
  108395                 :    26,  /*    INSTEAD => ID */
  108396                 :    26,  /*    LIKE_KW => ID */
  108397                 :    26,  /*      MATCH => ID */
  108398                 :    26,  /*         NO => ID */
  108399                 :    26,  /*        KEY => ID */
  108400                 :    26,  /*         OF => ID */
  108401                 :    26,  /*     OFFSET => ID */
  108402                 :    26,  /*     PRAGMA => ID */
  108403                 :    26,  /*      RAISE => ID */
  108404                 :    26,  /*    REPLACE => ID */
  108405                 :    26,  /*   RESTRICT => ID */
  108406                 :    26,  /*        ROW => ID */
  108407                 :    26,  /*    TRIGGER => ID */
  108408                 :    26,  /*     VACUUM => ID */
  108409                 :    26,  /*       VIEW => ID */
  108410                 :    26,  /*    VIRTUAL => ID */
  108411                 :    26,  /*    REINDEX => ID */
  108412                 :    26,  /*     RENAME => ID */
  108413                 :    26,  /*   CTIME_KW => ID */
  108414                 : };
  108415                 : #endif /* YYFALLBACK */
  108416                 : 
  108417                 : /* The following structure represents a single element of the
  108418                 : ** parser's stack.  Information stored includes:
  108419                 : **
  108420                 : **   +  The state number for the parser at this level of the stack.
  108421                 : **
  108422                 : **   +  The value of the token stored at this level of the stack.
  108423                 : **      (In other words, the "major" token.)
  108424                 : **
  108425                 : **   +  The semantic value stored at this level of the stack.  This is
  108426                 : **      the information used by the action routines in the grammar.
  108427                 : **      It is sometimes called the "minor" token.
  108428                 : */
  108429                 : struct yyStackEntry {
  108430                 :   YYACTIONTYPE stateno;  /* The state-number */
  108431                 :   YYCODETYPE major;      /* The major token value.  This is the code
  108432                 :                          ** number for the token at this stack level */
  108433                 :   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
  108434                 :                          ** is the value of the token  */
  108435                 : };
  108436                 : typedef struct yyStackEntry yyStackEntry;
  108437                 : 
  108438                 : /* The state of the parser is completely contained in an instance of
  108439                 : ** the following structure */
  108440                 : struct yyParser {
  108441                 :   int yyidx;                    /* Index of top element in stack */
  108442                 : #ifdef YYTRACKMAXSTACKDEPTH
  108443                 :   int yyidxMax;                 /* Maximum value of yyidx */
  108444                 : #endif
  108445                 :   int yyerrcnt;                 /* Shifts left before out of the error */
  108446                 :   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
  108447                 : #if YYSTACKDEPTH<=0
  108448                 :   int yystksz;                  /* Current side of the stack */
  108449                 :   yyStackEntry *yystack;        /* The parser's stack */
  108450                 : #else
  108451                 :   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
  108452                 : #endif
  108453                 : };
  108454                 : typedef struct yyParser yyParser;
  108455                 : 
  108456                 : #ifndef NDEBUG
  108457                 : /* #include <stdio.h> */
  108458                 : static FILE *yyTraceFILE = 0;
  108459                 : static char *yyTracePrompt = 0;
  108460                 : #endif /* NDEBUG */
  108461                 : 
  108462                 : #ifndef NDEBUG
  108463                 : /* 
  108464                 : ** Turn parser tracing on by giving a stream to which to write the trace
  108465                 : ** and a prompt to preface each trace message.  Tracing is turned off
  108466                 : ** by making either argument NULL 
  108467                 : **
  108468                 : ** Inputs:
  108469                 : ** <ul>
  108470                 : ** <li> A FILE* to which trace output should be written.
  108471                 : **      If NULL, then tracing is turned off.
  108472                 : ** <li> A prefix string written at the beginning of every
  108473                 : **      line of trace output.  If NULL, then tracing is
  108474                 : **      turned off.
  108475                 : ** </ul>
  108476                 : **
  108477                 : ** Outputs:
  108478                 : ** None.
  108479                 : */
  108480               0 : SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
  108481               0 :   yyTraceFILE = TraceFILE;
  108482               0 :   yyTracePrompt = zTracePrompt;
  108483               0 :   if( yyTraceFILE==0 ) yyTracePrompt = 0;
  108484               0 :   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
  108485               0 : }
  108486                 : #endif /* NDEBUG */
  108487                 : 
  108488                 : #ifndef NDEBUG
  108489                 : /* For tracing shifts, the names of all terminals and nonterminals
  108490                 : ** are required.  The following table supplies these names */
  108491                 : static const char *const yyTokenName[] = { 
  108492                 :   "$",             "SEMI",          "EXPLAIN",       "QUERY",       
  108493                 :   "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",    
  108494                 :   "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",         
  108495                 :   "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",          
  108496                 :   "TABLE",         "CREATE",        "IF",            "NOT",         
  108497                 :   "EXISTS",        "TEMP",          "LP",            "RP",          
  108498                 :   "AS",            "COMMA",         "ID",            "INDEXED",     
  108499                 :   "ABORT",         "ACTION",        "AFTER",         "ANALYZE",     
  108500                 :   "ASC",           "ATTACH",        "BEFORE",        "BY",          
  108501                 :   "CASCADE",       "CAST",          "COLUMNKW",      "CONFLICT",    
  108502                 :   "DATABASE",      "DESC",          "DETACH",        "EACH",        
  108503                 :   "FAIL",          "FOR",           "IGNORE",        "INITIALLY",   
  108504                 :   "INSTEAD",       "LIKE_KW",       "MATCH",         "NO",          
  108505                 :   "KEY",           "OF",            "OFFSET",        "PRAGMA",      
  108506                 :   "RAISE",         "REPLACE",       "RESTRICT",      "ROW",         
  108507                 :   "TRIGGER",       "VACUUM",        "VIEW",          "VIRTUAL",     
  108508                 :   "REINDEX",       "RENAME",        "CTIME_KW",      "ANY",         
  108509                 :   "OR",            "AND",           "IS",            "BETWEEN",     
  108510                 :   "IN",            "ISNULL",        "NOTNULL",       "NE",          
  108511                 :   "EQ",            "GT",            "LE",            "LT",          
  108512                 :   "GE",            "ESCAPE",        "BITAND",        "BITOR",       
  108513                 :   "LSHIFT",        "RSHIFT",        "PLUS",          "MINUS",       
  108514                 :   "STAR",          "SLASH",         "REM",           "CONCAT",      
  108515                 :   "COLLATE",       "BITNOT",        "STRING",        "JOIN_KW",     
  108516                 :   "CONSTRAINT",    "DEFAULT",       "NULL",          "PRIMARY",     
  108517                 :   "UNIQUE",        "CHECK",         "REFERENCES",    "AUTOINCR",    
  108518                 :   "ON",            "INSERT",        "DELETE",        "UPDATE",      
  108519                 :   "SET",           "DEFERRABLE",    "FOREIGN",       "DROP",        
  108520                 :   "UNION",         "ALL",           "EXCEPT",        "INTERSECT",   
  108521                 :   "SELECT",        "DISTINCT",      "DOT",           "FROM",        
  108522                 :   "JOIN",          "USING",         "ORDER",         "GROUP",       
  108523                 :   "HAVING",        "LIMIT",         "WHERE",         "INTO",        
  108524                 :   "VALUES",        "INTEGER",       "FLOAT",         "BLOB",        
  108525                 :   "REGISTER",      "VARIABLE",      "CASE",          "WHEN",        
  108526                 :   "THEN",          "ELSE",          "INDEX",         "ALTER",       
  108527                 :   "ADD",           "error",         "input",         "cmdlist",     
  108528                 :   "ecmd",          "explain",       "cmdx",          "cmd",         
  108529                 :   "transtype",     "trans_opt",     "nm",            "savepoint_opt",
  108530                 :   "create_table",  "create_table_args",  "createkw",      "temp",        
  108531                 :   "ifnotexists",   "dbnm",          "columnlist",    "conslist_opt",
  108532                 :   "select",        "column",        "columnid",      "type",        
  108533                 :   "carglist",      "id",            "ids",           "typetoken",   
  108534                 :   "typename",      "signed",        "plus_num",      "minus_num",   
  108535                 :   "carg",          "ccons",         "term",          "expr",        
  108536                 :   "onconf",        "sortorder",     "autoinc",       "idxlist_opt", 
  108537                 :   "refargs",       "defer_subclause",  "refarg",        "refact",      
  108538                 :   "init_deferred_pred_opt",  "conslist",      "tcons",         "idxlist",     
  108539                 :   "defer_subclause_opt",  "orconf",        "resolvetype",   "raisetype",   
  108540                 :   "ifexists",      "fullname",      "oneselect",     "multiselect_op",
  108541                 :   "distinct",      "selcollist",    "from",          "where_opt",   
  108542                 :   "groupby_opt",   "having_opt",    "orderby_opt",   "limit_opt",   
  108543                 :   "sclp",          "as",            "seltablist",    "stl_prefix",  
  108544                 :   "joinop",        "indexed_opt",   "on_opt",        "using_opt",   
  108545                 :   "joinop2",       "inscollist",    "sortlist",      "sortitem",    
  108546                 :   "nexprlist",     "setlist",       "insert_cmd",    "inscollist_opt",
  108547                 :   "itemlist",      "exprlist",      "likeop",        "between_op",  
  108548                 :   "in_op",         "case_operand",  "case_exprlist",  "case_else",   
  108549                 :   "uniqueflag",    "collate",       "nmnum",         "plus_opt",    
  108550                 :   "number",        "trigger_decl",  "trigger_cmd_list",  "trigger_time",
  108551                 :   "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd", 
  108552                 :   "trnm",          "tridxby",       "database_kw_opt",  "key_opt",     
  108553                 :   "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist", 
  108554                 :   "vtabarg",       "vtabargtoken",  "lp",            "anylist",     
  108555                 : };
  108556                 : #endif /* NDEBUG */
  108557                 : 
  108558                 : #ifndef NDEBUG
  108559                 : /* For tracing reduce actions, the names of all rules are required.
  108560                 : */
  108561                 : static const char *const yyRuleName[] = {
  108562                 :  /*   0 */ "input ::= cmdlist",
  108563                 :  /*   1 */ "cmdlist ::= cmdlist ecmd",
  108564                 :  /*   2 */ "cmdlist ::= ecmd",
  108565                 :  /*   3 */ "ecmd ::= SEMI",
  108566                 :  /*   4 */ "ecmd ::= explain cmdx SEMI",
  108567                 :  /*   5 */ "explain ::=",
  108568                 :  /*   6 */ "explain ::= EXPLAIN",
  108569                 :  /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
  108570                 :  /*   8 */ "cmdx ::= cmd",
  108571                 :  /*   9 */ "cmd ::= BEGIN transtype trans_opt",
  108572                 :  /*  10 */ "trans_opt ::=",
  108573                 :  /*  11 */ "trans_opt ::= TRANSACTION",
  108574                 :  /*  12 */ "trans_opt ::= TRANSACTION nm",
  108575                 :  /*  13 */ "transtype ::=",
  108576                 :  /*  14 */ "transtype ::= DEFERRED",
  108577                 :  /*  15 */ "transtype ::= IMMEDIATE",
  108578                 :  /*  16 */ "transtype ::= EXCLUSIVE",
  108579                 :  /*  17 */ "cmd ::= COMMIT trans_opt",
  108580                 :  /*  18 */ "cmd ::= END trans_opt",
  108581                 :  /*  19 */ "cmd ::= ROLLBACK trans_opt",
  108582                 :  /*  20 */ "savepoint_opt ::= SAVEPOINT",
  108583                 :  /*  21 */ "savepoint_opt ::=",
  108584                 :  /*  22 */ "cmd ::= SAVEPOINT nm",
  108585                 :  /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
  108586                 :  /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
  108587                 :  /*  25 */ "cmd ::= create_table create_table_args",
  108588                 :  /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
  108589                 :  /*  27 */ "createkw ::= CREATE",
  108590                 :  /*  28 */ "ifnotexists ::=",
  108591                 :  /*  29 */ "ifnotexists ::= IF NOT EXISTS",
  108592                 :  /*  30 */ "temp ::= TEMP",
  108593                 :  /*  31 */ "temp ::=",
  108594                 :  /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP",
  108595                 :  /*  33 */ "create_table_args ::= AS select",
  108596                 :  /*  34 */ "columnlist ::= columnlist COMMA column",
  108597                 :  /*  35 */ "columnlist ::= column",
  108598                 :  /*  36 */ "column ::= columnid type carglist",
  108599                 :  /*  37 */ "columnid ::= nm",
  108600                 :  /*  38 */ "id ::= ID",
  108601                 :  /*  39 */ "id ::= INDEXED",
  108602                 :  /*  40 */ "ids ::= ID|STRING",
  108603                 :  /*  41 */ "nm ::= id",
  108604                 :  /*  42 */ "nm ::= STRING",
  108605                 :  /*  43 */ "nm ::= JOIN_KW",
  108606                 :  /*  44 */ "type ::=",
  108607                 :  /*  45 */ "type ::= typetoken",
  108608                 :  /*  46 */ "typetoken ::= typename",
  108609                 :  /*  47 */ "typetoken ::= typename LP signed RP",
  108610                 :  /*  48 */ "typetoken ::= typename LP signed COMMA signed RP",
  108611                 :  /*  49 */ "typename ::= ids",
  108612                 :  /*  50 */ "typename ::= typename ids",
  108613                 :  /*  51 */ "signed ::= plus_num",
  108614                 :  /*  52 */ "signed ::= minus_num",
  108615                 :  /*  53 */ "carglist ::= carglist carg",
  108616                 :  /*  54 */ "carglist ::=",
  108617                 :  /*  55 */ "carg ::= CONSTRAINT nm ccons",
  108618                 :  /*  56 */ "carg ::= ccons",
  108619                 :  /*  57 */ "ccons ::= DEFAULT term",
  108620                 :  /*  58 */ "ccons ::= DEFAULT LP expr RP",
  108621                 :  /*  59 */ "ccons ::= DEFAULT PLUS term",
  108622                 :  /*  60 */ "ccons ::= DEFAULT MINUS term",
  108623                 :  /*  61 */ "ccons ::= DEFAULT id",
  108624                 :  /*  62 */ "ccons ::= NULL onconf",
  108625                 :  /*  63 */ "ccons ::= NOT NULL onconf",
  108626                 :  /*  64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
  108627                 :  /*  65 */ "ccons ::= UNIQUE onconf",
  108628                 :  /*  66 */ "ccons ::= CHECK LP expr RP",
  108629                 :  /*  67 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
  108630                 :  /*  68 */ "ccons ::= defer_subclause",
  108631                 :  /*  69 */ "ccons ::= COLLATE ids",
  108632                 :  /*  70 */ "autoinc ::=",
  108633                 :  /*  71 */ "autoinc ::= AUTOINCR",
  108634                 :  /*  72 */ "refargs ::=",
  108635                 :  /*  73 */ "refargs ::= refargs refarg",
  108636                 :  /*  74 */ "refarg ::= MATCH nm",
  108637                 :  /*  75 */ "refarg ::= ON INSERT refact",
  108638                 :  /*  76 */ "refarg ::= ON DELETE refact",
  108639                 :  /*  77 */ "refarg ::= ON UPDATE refact",
  108640                 :  /*  78 */ "refact ::= SET NULL",
  108641                 :  /*  79 */ "refact ::= SET DEFAULT",
  108642                 :  /*  80 */ "refact ::= CASCADE",
  108643                 :  /*  81 */ "refact ::= RESTRICT",
  108644                 :  /*  82 */ "refact ::= NO ACTION",
  108645                 :  /*  83 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
  108646                 :  /*  84 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
  108647                 :  /*  85 */ "init_deferred_pred_opt ::=",
  108648                 :  /*  86 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
  108649                 :  /*  87 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
  108650                 :  /*  88 */ "conslist_opt ::=",
  108651                 :  /*  89 */ "conslist_opt ::= COMMA conslist",
  108652                 :  /*  90 */ "conslist ::= conslist COMMA tcons",
  108653                 :  /*  91 */ "conslist ::= conslist tcons",
  108654                 :  /*  92 */ "conslist ::= tcons",
  108655                 :  /*  93 */ "tcons ::= CONSTRAINT nm",
  108656                 :  /*  94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
  108657                 :  /*  95 */ "tcons ::= UNIQUE LP idxlist RP onconf",
  108658                 :  /*  96 */ "tcons ::= CHECK LP expr RP onconf",
  108659                 :  /*  97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
  108660                 :  /*  98 */ "defer_subclause_opt ::=",
  108661                 :  /*  99 */ "defer_subclause_opt ::= defer_subclause",
  108662                 :  /* 100 */ "onconf ::=",
  108663                 :  /* 101 */ "onconf ::= ON CONFLICT resolvetype",
  108664                 :  /* 102 */ "orconf ::=",
  108665                 :  /* 103 */ "orconf ::= OR resolvetype",
  108666                 :  /* 104 */ "resolvetype ::= raisetype",
  108667                 :  /* 105 */ "resolvetype ::= IGNORE",
  108668                 :  /* 106 */ "resolvetype ::= REPLACE",
  108669                 :  /* 107 */ "cmd ::= DROP TABLE ifexists fullname",
  108670                 :  /* 108 */ "ifexists ::= IF EXISTS",
  108671                 :  /* 109 */ "ifexists ::=",
  108672                 :  /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
  108673                 :  /* 111 */ "cmd ::= DROP VIEW ifexists fullname",
  108674                 :  /* 112 */ "cmd ::= select",
  108675                 :  /* 113 */ "select ::= oneselect",
  108676                 :  /* 114 */ "select ::= select multiselect_op oneselect",
  108677                 :  /* 115 */ "multiselect_op ::= UNION",
  108678                 :  /* 116 */ "multiselect_op ::= UNION ALL",
  108679                 :  /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
  108680                 :  /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
  108681                 :  /* 119 */ "distinct ::= DISTINCT",
  108682                 :  /* 120 */ "distinct ::= ALL",
  108683                 :  /* 121 */ "distinct ::=",
  108684                 :  /* 122 */ "sclp ::= selcollist COMMA",
  108685                 :  /* 123 */ "sclp ::=",
  108686                 :  /* 124 */ "selcollist ::= sclp expr as",
  108687                 :  /* 125 */ "selcollist ::= sclp STAR",
  108688                 :  /* 126 */ "selcollist ::= sclp nm DOT STAR",
  108689                 :  /* 127 */ "as ::= AS nm",
  108690                 :  /* 128 */ "as ::= ids",
  108691                 :  /* 129 */ "as ::=",
  108692                 :  /* 130 */ "from ::=",
  108693                 :  /* 131 */ "from ::= FROM seltablist",
  108694                 :  /* 132 */ "stl_prefix ::= seltablist joinop",
  108695                 :  /* 133 */ "stl_prefix ::=",
  108696                 :  /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
  108697                 :  /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
  108698                 :  /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
  108699                 :  /* 137 */ "dbnm ::=",
  108700                 :  /* 138 */ "dbnm ::= DOT nm",
  108701                 :  /* 139 */ "fullname ::= nm dbnm",
  108702                 :  /* 140 */ "joinop ::= COMMA|JOIN",
  108703                 :  /* 141 */ "joinop ::= JOIN_KW JOIN",
  108704                 :  /* 142 */ "joinop ::= JOIN_KW nm JOIN",
  108705                 :  /* 143 */ "joinop ::= JOIN_KW nm nm JOIN",
  108706                 :  /* 144 */ "on_opt ::= ON expr",
  108707                 :  /* 145 */ "on_opt ::=",
  108708                 :  /* 146 */ "indexed_opt ::=",
  108709                 :  /* 147 */ "indexed_opt ::= INDEXED BY nm",
  108710                 :  /* 148 */ "indexed_opt ::= NOT INDEXED",
  108711                 :  /* 149 */ "using_opt ::= USING LP inscollist RP",
  108712                 :  /* 150 */ "using_opt ::=",
  108713                 :  /* 151 */ "orderby_opt ::=",
  108714                 :  /* 152 */ "orderby_opt ::= ORDER BY sortlist",
  108715                 :  /* 153 */ "sortlist ::= sortlist COMMA sortitem sortorder",
  108716                 :  /* 154 */ "sortlist ::= sortitem sortorder",
  108717                 :  /* 155 */ "sortitem ::= expr",
  108718                 :  /* 156 */ "sortorder ::= ASC",
  108719                 :  /* 157 */ "sortorder ::= DESC",
  108720                 :  /* 158 */ "sortorder ::=",
  108721                 :  /* 159 */ "groupby_opt ::=",
  108722                 :  /* 160 */ "groupby_opt ::= GROUP BY nexprlist",
  108723                 :  /* 161 */ "having_opt ::=",
  108724                 :  /* 162 */ "having_opt ::= HAVING expr",
  108725                 :  /* 163 */ "limit_opt ::=",
  108726                 :  /* 164 */ "limit_opt ::= LIMIT expr",
  108727                 :  /* 165 */ "limit_opt ::= LIMIT expr OFFSET expr",
  108728                 :  /* 166 */ "limit_opt ::= LIMIT expr COMMA expr",
  108729                 :  /* 167 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt",
  108730                 :  /* 168 */ "where_opt ::=",
  108731                 :  /* 169 */ "where_opt ::= WHERE expr",
  108732                 :  /* 170 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt",
  108733                 :  /* 171 */ "setlist ::= setlist COMMA nm EQ expr",
  108734                 :  /* 172 */ "setlist ::= nm EQ expr",
  108735                 :  /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP",
  108736                 :  /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select",
  108737                 :  /* 175 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
  108738                 :  /* 176 */ "insert_cmd ::= INSERT orconf",
  108739                 :  /* 177 */ "insert_cmd ::= REPLACE",
  108740                 :  /* 178 */ "itemlist ::= itemlist COMMA expr",
  108741                 :  /* 179 */ "itemlist ::= expr",
  108742                 :  /* 180 */ "inscollist_opt ::=",
  108743                 :  /* 181 */ "inscollist_opt ::= LP inscollist RP",
  108744                 :  /* 182 */ "inscollist ::= inscollist COMMA nm",
  108745                 :  /* 183 */ "inscollist ::= nm",
  108746                 :  /* 184 */ "expr ::= term",
  108747                 :  /* 185 */ "expr ::= LP expr RP",
  108748                 :  /* 186 */ "term ::= NULL",
  108749                 :  /* 187 */ "expr ::= id",
  108750                 :  /* 188 */ "expr ::= JOIN_KW",
  108751                 :  /* 189 */ "expr ::= nm DOT nm",
  108752                 :  /* 190 */ "expr ::= nm DOT nm DOT nm",
  108753                 :  /* 191 */ "term ::= INTEGER|FLOAT|BLOB",
  108754                 :  /* 192 */ "term ::= STRING",
  108755                 :  /* 193 */ "expr ::= REGISTER",
  108756                 :  /* 194 */ "expr ::= VARIABLE",
  108757                 :  /* 195 */ "expr ::= expr COLLATE ids",
  108758                 :  /* 196 */ "expr ::= CAST LP expr AS typetoken RP",
  108759                 :  /* 197 */ "expr ::= ID LP distinct exprlist RP",
  108760                 :  /* 198 */ "expr ::= ID LP STAR RP",
  108761                 :  /* 199 */ "term ::= CTIME_KW",
  108762                 :  /* 200 */ "expr ::= expr AND expr",
  108763                 :  /* 201 */ "expr ::= expr OR expr",
  108764                 :  /* 202 */ "expr ::= expr LT|GT|GE|LE expr",
  108765                 :  /* 203 */ "expr ::= expr EQ|NE expr",
  108766                 :  /* 204 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
  108767                 :  /* 205 */ "expr ::= expr PLUS|MINUS expr",
  108768                 :  /* 206 */ "expr ::= expr STAR|SLASH|REM expr",
  108769                 :  /* 207 */ "expr ::= expr CONCAT expr",
  108770                 :  /* 208 */ "likeop ::= LIKE_KW",
  108771                 :  /* 209 */ "likeop ::= NOT LIKE_KW",
  108772                 :  /* 210 */ "likeop ::= MATCH",
  108773                 :  /* 211 */ "likeop ::= NOT MATCH",
  108774                 :  /* 212 */ "expr ::= expr likeop expr",
  108775                 :  /* 213 */ "expr ::= expr likeop expr ESCAPE expr",
  108776                 :  /* 214 */ "expr ::= expr ISNULL|NOTNULL",
  108777                 :  /* 215 */ "expr ::= expr NOT NULL",
  108778                 :  /* 216 */ "expr ::= expr IS expr",
  108779                 :  /* 217 */ "expr ::= expr IS NOT expr",
  108780                 :  /* 218 */ "expr ::= NOT expr",
  108781                 :  /* 219 */ "expr ::= BITNOT expr",
  108782                 :  /* 220 */ "expr ::= MINUS expr",
  108783                 :  /* 221 */ "expr ::= PLUS expr",
  108784                 :  /* 222 */ "between_op ::= BETWEEN",
  108785                 :  /* 223 */ "between_op ::= NOT BETWEEN",
  108786                 :  /* 224 */ "expr ::= expr between_op expr AND expr",
  108787                 :  /* 225 */ "in_op ::= IN",
  108788                 :  /* 226 */ "in_op ::= NOT IN",
  108789                 :  /* 227 */ "expr ::= expr in_op LP exprlist RP",
  108790                 :  /* 228 */ "expr ::= LP select RP",
  108791                 :  /* 229 */ "expr ::= expr in_op LP select RP",
  108792                 :  /* 230 */ "expr ::= expr in_op nm dbnm",
  108793                 :  /* 231 */ "expr ::= EXISTS LP select RP",
  108794                 :  /* 232 */ "expr ::= CASE case_operand case_exprlist case_else END",
  108795                 :  /* 233 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
  108796                 :  /* 234 */ "case_exprlist ::= WHEN expr THEN expr",
  108797                 :  /* 235 */ "case_else ::= ELSE expr",
  108798                 :  /* 236 */ "case_else ::=",
  108799                 :  /* 237 */ "case_operand ::= expr",
  108800                 :  /* 238 */ "case_operand ::=",
  108801                 :  /* 239 */ "exprlist ::= nexprlist",
  108802                 :  /* 240 */ "exprlist ::=",
  108803                 :  /* 241 */ "nexprlist ::= nexprlist COMMA expr",
  108804                 :  /* 242 */ "nexprlist ::= expr",
  108805                 :  /* 243 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP",
  108806                 :  /* 244 */ "uniqueflag ::= UNIQUE",
  108807                 :  /* 245 */ "uniqueflag ::=",
  108808                 :  /* 246 */ "idxlist_opt ::=",
  108809                 :  /* 247 */ "idxlist_opt ::= LP idxlist RP",
  108810                 :  /* 248 */ "idxlist ::= idxlist COMMA nm collate sortorder",
  108811                 :  /* 249 */ "idxlist ::= nm collate sortorder",
  108812                 :  /* 250 */ "collate ::=",
  108813                 :  /* 251 */ "collate ::= COLLATE ids",
  108814                 :  /* 252 */ "cmd ::= DROP INDEX ifexists fullname",
  108815                 :  /* 253 */ "cmd ::= VACUUM",
  108816                 :  /* 254 */ "cmd ::= VACUUM nm",
  108817                 :  /* 255 */ "cmd ::= PRAGMA nm dbnm",
  108818                 :  /* 256 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
  108819                 :  /* 257 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
  108820                 :  /* 258 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
  108821                 :  /* 259 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
  108822                 :  /* 260 */ "nmnum ::= plus_num",
  108823                 :  /* 261 */ "nmnum ::= nm",
  108824                 :  /* 262 */ "nmnum ::= ON",
  108825                 :  /* 263 */ "nmnum ::= DELETE",
  108826                 :  /* 264 */ "nmnum ::= DEFAULT",
  108827                 :  /* 265 */ "plus_num ::= plus_opt number",
  108828                 :  /* 266 */ "minus_num ::= MINUS number",
  108829                 :  /* 267 */ "number ::= INTEGER|FLOAT",
  108830                 :  /* 268 */ "plus_opt ::= PLUS",
  108831                 :  /* 269 */ "plus_opt ::=",
  108832                 :  /* 270 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
  108833                 :  /* 271 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
  108834                 :  /* 272 */ "trigger_time ::= BEFORE",
  108835                 :  /* 273 */ "trigger_time ::= AFTER",
  108836                 :  /* 274 */ "trigger_time ::= INSTEAD OF",
  108837                 :  /* 275 */ "trigger_time ::=",
  108838                 :  /* 276 */ "trigger_event ::= DELETE|INSERT",
  108839                 :  /* 277 */ "trigger_event ::= UPDATE",
  108840                 :  /* 278 */ "trigger_event ::= UPDATE OF inscollist",
  108841                 :  /* 279 */ "foreach_clause ::=",
  108842                 :  /* 280 */ "foreach_clause ::= FOR EACH ROW",
  108843                 :  /* 281 */ "when_clause ::=",
  108844                 :  /* 282 */ "when_clause ::= WHEN expr",
  108845                 :  /* 283 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
  108846                 :  /* 284 */ "trigger_cmd_list ::= trigger_cmd SEMI",
  108847                 :  /* 285 */ "trnm ::= nm",
  108848                 :  /* 286 */ "trnm ::= nm DOT nm",
  108849                 :  /* 287 */ "tridxby ::=",
  108850                 :  /* 288 */ "tridxby ::= INDEXED BY nm",
  108851                 :  /* 289 */ "tridxby ::= NOT INDEXED",
  108852                 :  /* 290 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
  108853                 :  /* 291 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP",
  108854                 :  /* 292 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
  108855                 :  /* 293 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
  108856                 :  /* 294 */ "trigger_cmd ::= select",
  108857                 :  /* 295 */ "expr ::= RAISE LP IGNORE RP",
  108858                 :  /* 296 */ "expr ::= RAISE LP raisetype COMMA nm RP",
  108859                 :  /* 297 */ "raisetype ::= ROLLBACK",
  108860                 :  /* 298 */ "raisetype ::= ABORT",
  108861                 :  /* 299 */ "raisetype ::= FAIL",
  108862                 :  /* 300 */ "cmd ::= DROP TRIGGER ifexists fullname",
  108863                 :  /* 301 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
  108864                 :  /* 302 */ "cmd ::= DETACH database_kw_opt expr",
  108865                 :  /* 303 */ "key_opt ::=",
  108866                 :  /* 304 */ "key_opt ::= KEY expr",
  108867                 :  /* 305 */ "database_kw_opt ::= DATABASE",
  108868                 :  /* 306 */ "database_kw_opt ::=",
  108869                 :  /* 307 */ "cmd ::= REINDEX",
  108870                 :  /* 308 */ "cmd ::= REINDEX nm dbnm",
  108871                 :  /* 309 */ "cmd ::= ANALYZE",
  108872                 :  /* 310 */ "cmd ::= ANALYZE nm dbnm",
  108873                 :  /* 311 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
  108874                 :  /* 312 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
  108875                 :  /* 313 */ "add_column_fullname ::= fullname",
  108876                 :  /* 314 */ "kwcolumn_opt ::=",
  108877                 :  /* 315 */ "kwcolumn_opt ::= COLUMNKW",
  108878                 :  /* 316 */ "cmd ::= create_vtab",
  108879                 :  /* 317 */ "cmd ::= create_vtab LP vtabarglist RP",
  108880                 :  /* 318 */ "create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm",
  108881                 :  /* 319 */ "vtabarglist ::= vtabarg",
  108882                 :  /* 320 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
  108883                 :  /* 321 */ "vtabarg ::=",
  108884                 :  /* 322 */ "vtabarg ::= vtabarg vtabargtoken",
  108885                 :  /* 323 */ "vtabargtoken ::= ANY",
  108886                 :  /* 324 */ "vtabargtoken ::= lp anylist RP",
  108887                 :  /* 325 */ "lp ::= LP",
  108888                 :  /* 326 */ "anylist ::=",
  108889                 :  /* 327 */ "anylist ::= anylist LP anylist RP",
  108890                 :  /* 328 */ "anylist ::= anylist ANY",
  108891                 : };
  108892                 : #endif /* NDEBUG */
  108893                 : 
  108894                 : 
  108895                 : #if YYSTACKDEPTH<=0
  108896                 : /*
  108897                 : ** Try to increase the size of the parser stack.
  108898                 : */
  108899                 : static void yyGrowStack(yyParser *p){
  108900                 :   int newSize;
  108901                 :   yyStackEntry *pNew;
  108902                 : 
  108903                 :   newSize = p->yystksz*2 + 100;
  108904                 :   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
  108905                 :   if( pNew ){
  108906                 :     p->yystack = pNew;
  108907                 :     p->yystksz = newSize;
  108908                 : #ifndef NDEBUG
  108909                 :     if( yyTraceFILE ){
  108910                 :       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
  108911                 :               yyTracePrompt, p->yystksz);
  108912                 :     }
  108913                 : #endif
  108914                 :   }
  108915                 : }
  108916                 : #endif
  108917                 : 
  108918                 : /* 
  108919                 : ** This function allocates a new parser.
  108920                 : ** The only argument is a pointer to a function which works like
  108921                 : ** malloc.
  108922                 : **
  108923                 : ** Inputs:
  108924                 : ** A pointer to the function used to allocate memory.
  108925                 : **
  108926                 : ** Outputs:
  108927                 : ** A pointer to a parser.  This pointer is used in subsequent calls
  108928                 : ** to sqlite3Parser and sqlite3ParserFree.
  108929                 : */
  108930          193859 : SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
  108931                 :   yyParser *pParser;
  108932          193859 :   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
  108933          193859 :   if( pParser ){
  108934          193859 :     pParser->yyidx = -1;
  108935                 : #ifdef YYTRACKMAXSTACKDEPTH
  108936                 :     pParser->yyidxMax = 0;
  108937                 : #endif
  108938                 : #if YYSTACKDEPTH<=0
  108939                 :     pParser->yystack = NULL;
  108940                 :     pParser->yystksz = 0;
  108941                 :     yyGrowStack(pParser);
  108942                 : #endif
  108943                 :   }
  108944          193859 :   return pParser;
  108945                 : }
  108946                 : 
  108947                 : /* The following function deletes the value associated with a
  108948                 : ** symbol.  The symbol can be either a terminal or nonterminal.
  108949                 : ** "yymajor" is the symbol code, and "yypminor" is a pointer to
  108950                 : ** the value.
  108951                 : */
  108952          212674 : static void yy_destructor(
  108953                 :   yyParser *yypParser,    /* The parser */
  108954                 :   YYCODETYPE yymajor,     /* Type code for object to destroy */
  108955                 :   YYMINORTYPE *yypminor   /* The object to be destroyed */
  108956                 : ){
  108957          212674 :   sqlite3ParserARG_FETCH;
  108958          212674 :   switch( yymajor ){
  108959                 :     /* Here is inserted the actions which take place when a
  108960                 :     ** terminal or non-terminal is destroyed.  This can happen
  108961                 :     ** when the symbol is popped from the stack during a
  108962                 :     ** reduce or during error processing or when a parser is 
  108963                 :     ** being destroyed before it is finished parsing.
  108964                 :     **
  108965                 :     ** Note: during a reduce, the only symbols destroyed are those
  108966                 :     ** which appear on the RHS of the rule, but which are not used
  108967                 :     ** inside the C code.
  108968                 :     */
  108969                 :     case 160: /* select */
  108970                 :     case 194: /* oneselect */
  108971                 : {
  108972               0 : sqlite3SelectDelete(pParse->db, (yypminor->yy387));
  108973                 : }
  108974               0 :       break;
  108975                 :     case 174: /* term */
  108976                 :     case 175: /* expr */
  108977                 : {
  108978               0 : sqlite3ExprDelete(pParse->db, (yypminor->yy118).pExpr);
  108979                 : }
  108980               0 :       break;
  108981                 :     case 179: /* idxlist_opt */
  108982                 :     case 187: /* idxlist */
  108983                 :     case 197: /* selcollist */
  108984                 :     case 200: /* groupby_opt */
  108985                 :     case 202: /* orderby_opt */
  108986                 :     case 204: /* sclp */
  108987                 :     case 214: /* sortlist */
  108988                 :     case 216: /* nexprlist */
  108989                 :     case 217: /* setlist */
  108990                 :     case 220: /* itemlist */
  108991                 :     case 221: /* exprlist */
  108992                 :     case 226: /* case_exprlist */
  108993                 : {
  108994               0 : sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
  108995                 : }
  108996               0 :       break;
  108997                 :     case 193: /* fullname */
  108998                 :     case 198: /* from */
  108999                 :     case 206: /* seltablist */
  109000                 :     case 207: /* stl_prefix */
  109001                 : {
  109002               0 : sqlite3SrcListDelete(pParse->db, (yypminor->yy259));
  109003                 : }
  109004               0 :       break;
  109005                 :     case 199: /* where_opt */
  109006                 :     case 201: /* having_opt */
  109007                 :     case 210: /* on_opt */
  109008                 :     case 215: /* sortitem */
  109009                 :     case 225: /* case_operand */
  109010                 :     case 227: /* case_else */
  109011                 :     case 238: /* when_clause */
  109012                 :     case 243: /* key_opt */
  109013                 : {
  109014               0 : sqlite3ExprDelete(pParse->db, (yypminor->yy314));
  109015                 : }
  109016               0 :       break;
  109017                 :     case 211: /* using_opt */
  109018                 :     case 213: /* inscollist */
  109019                 :     case 219: /* inscollist_opt */
  109020                 : {
  109021               0 : sqlite3IdListDelete(pParse->db, (yypminor->yy384));
  109022                 : }
  109023               0 :       break;
  109024                 :     case 234: /* trigger_cmd_list */
  109025                 :     case 239: /* trigger_cmd */
  109026                 : {
  109027               0 : sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203));
  109028                 : }
  109029               0 :       break;
  109030                 :     case 236: /* trigger_event */
  109031                 : {
  109032               0 : sqlite3IdListDelete(pParse->db, (yypminor->yy90).b);
  109033                 : }
  109034               0 :       break;
  109035          212674 :     default:  break;   /* If no destructor action specified: do nothing */
  109036                 :   }
  109037          212674 : }
  109038                 : 
  109039                 : /*
  109040                 : ** Pop the parser's stack once.
  109041                 : **
  109042                 : ** If there is a destructor routine associated with the token which
  109043                 : ** is popped from the stack, then call it.
  109044                 : **
  109045                 : ** Return the major token number for the symbol popped.
  109046                 : */
  109047          212670 : static int yy_pop_parser_stack(yyParser *pParser){
  109048                 :   YYCODETYPE yymajor;
  109049          212670 :   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
  109050                 : 
  109051                 :   /* There is no mechanism by which the parser stack can be popped below
  109052                 :   ** empty in SQLite.  */
  109053          212670 :   if( NEVER(pParser->yyidx<0) ) return 0;
  109054                 : #ifndef NDEBUG
  109055          212670 :   if( yyTraceFILE && pParser->yyidx>=0 ){
  109056               0 :     fprintf(yyTraceFILE,"%sPopping %s\n",
  109057                 :       yyTracePrompt,
  109058               0 :       yyTokenName[yytos->major]);
  109059                 :   }
  109060                 : #endif
  109061          212670 :   yymajor = yytos->major;
  109062          212670 :   yy_destructor(pParser, yymajor, &yytos->minor);
  109063          212670 :   pParser->yyidx--;
  109064          212670 :   return yymajor;
  109065                 : }
  109066                 : 
  109067                 : /* 
  109068                 : ** Deallocate and destroy a parser.  Destructors are all called for
  109069                 : ** all stack elements before shutting the parser down.
  109070                 : **
  109071                 : ** Inputs:
  109072                 : ** <ul>
  109073                 : ** <li>  A pointer to the parser.  This should be a pointer
  109074                 : **       obtained from sqlite3ParserAlloc.
  109075                 : ** <li>  A pointer to a function used to reclaim memory obtained
  109076                 : **       from malloc.
  109077                 : ** </ul>
  109078                 : */
  109079          193859 : SQLITE_PRIVATE void sqlite3ParserFree(
  109080                 :   void *p,                    /* The parser to be deleted */
  109081                 :   void (*freeProc)(void*)     /* Function used to reclaim memory */
  109082                 : ){
  109083          193859 :   yyParser *pParser = (yyParser*)p;
  109084                 :   /* In SQLite, we never try to destroy a parser that was not successfully
  109085                 :   ** created in the first place. */
  109086          193859 :   if( NEVER(pParser==0) ) return;
  109087          193859 :   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
  109088                 : #if YYSTACKDEPTH<=0
  109089                 :   free(pParser->yystack);
  109090                 : #endif
  109091          193859 :   (*freeProc)((void*)pParser);
  109092                 : }
  109093                 : 
  109094                 : /*
  109095                 : ** Return the peak depth of the stack for a parser.
  109096                 : */
  109097                 : #ifdef YYTRACKMAXSTACKDEPTH
  109098                 : SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
  109099                 :   yyParser *pParser = (yyParser*)p;
  109100                 :   return pParser->yyidxMax;
  109101                 : }
  109102                 : #endif
  109103                 : 
  109104                 : /*
  109105                 : ** Find the appropriate action for a parser given the terminal
  109106                 : ** look-ahead token iLookAhead.
  109107                 : **
  109108                 : ** If the look-ahead token is YYNOCODE, then check to see if the action is
  109109                 : ** independent of the look-ahead.  If it is, return the action, otherwise
  109110                 : ** return YY_NO_ACTION.
  109111                 : */
  109112        13722390 : static int yy_find_shift_action(
  109113                 :   yyParser *pParser,        /* The parser */
  109114                 :   YYCODETYPE iLookAhead     /* The look-ahead token */
  109115                 : ){
  109116                 :   int i;
  109117        13722390 :   int stateno = pParser->yystack[pParser->yyidx].stateno;
  109118                 :  
  109119        13722390 :   if( stateno>YY_SHIFT_COUNT
  109120         8695443 :    || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
  109121         5368451 :     return yy_default[stateno];
  109122                 :   }
  109123         8353939 :   assert( iLookAhead!=YYNOCODE );
  109124         8353939 :   i += iLookAhead;
  109125         8353939 :   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
  109126         3972630 :     if( iLookAhead>0 ){
  109127                 : #ifdef YYFALLBACK
  109128                 :       YYCODETYPE iFallback;            /* Fallback token */
  109129         3972631 :       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
  109130         2716949 :              && (iFallback = yyFallback[iLookAhead])!=0 ){
  109131                 : #ifndef NDEBUG
  109132          104087 :         if( yyTraceFILE ){
  109133               0 :           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
  109134                 :              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
  109135                 :         }
  109136                 : #endif
  109137          104087 :         return yy_find_shift_action(pParser, iFallback);
  109138                 :       }
  109139                 : #endif
  109140                 : #ifdef YYWILDCARD
  109141                 :       {
  109142         3868544 :         int j = i - iLookAhead + YYWILDCARD;
  109143         3868544 :         if( 
  109144                 : #if YY_SHIFT_MIN+YYWILDCARD<0
  109145         3868425 :           j>=0 &&
  109146                 : #endif
  109147                 : #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
  109148                 :           j<YY_ACTTAB_COUNT &&
  109149                 : #endif
  109150         3868425 :           yy_lookahead[j]==YYWILDCARD
  109151                 :         ){
  109152                 : #ifndef NDEBUG
  109153               4 :           if( yyTraceFILE ){
  109154               0 :             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
  109155                 :                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
  109156                 :           }
  109157                 : #endif /* NDEBUG */
  109158               4 :           return yy_action[j];
  109159                 :         }
  109160                 :       }
  109161                 : #endif /* YYWILDCARD */
  109162                 :     }
  109163         3868539 :     return yy_default[stateno];
  109164                 :   }else{
  109165         4381309 :     return yy_action[i];
  109166                 :   }
  109167                 : }
  109168                 : 
  109169                 : /*
  109170                 : ** Find the appropriate action for a parser given the non-terminal
  109171                 : ** look-ahead token iLookAhead.
  109172                 : **
  109173                 : ** If the look-ahead token is YYNOCODE, then check to see if the action is
  109174                 : ** independent of the look-ahead.  If it is, return the action, otherwise
  109175                 : ** return YY_NO_ACTION.
  109176                 : */
  109177         9512120 : static int yy_find_reduce_action(
  109178                 :   int stateno,              /* Current state number */
  109179                 :   YYCODETYPE iLookAhead     /* The look-ahead token */
  109180                 : ){
  109181                 :   int i;
  109182                 : #ifdef YYERRORSYMBOL
  109183                 :   if( stateno>YY_REDUCE_COUNT ){
  109184                 :     return yy_default[stateno];
  109185                 :   }
  109186                 : #else
  109187         9512120 :   assert( stateno<=YY_REDUCE_COUNT );
  109188                 : #endif
  109189         9512120 :   i = yy_reduce_ofst[stateno];
  109190         9512120 :   assert( i!=YY_REDUCE_USE_DFLT );
  109191         9512120 :   assert( iLookAhead!=YYNOCODE );
  109192         9512120 :   i += iLookAhead;
  109193                 : #ifdef YYERRORSYMBOL
  109194                 :   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
  109195                 :     return yy_default[stateno];
  109196                 :   }
  109197                 : #else
  109198         9512120 :   assert( i>=0 && i<YY_ACTTAB_COUNT );
  109199         9512126 :   assert( yy_lookahead[i]==iLookAhead );
  109200                 : #endif
  109201         9512126 :   return yy_action[i];
  109202                 : }
  109203                 : 
  109204                 : /*
  109205                 : ** The following routine is called if the stack overflows.
  109206                 : */
  109207               0 : static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
  109208               0 :    sqlite3ParserARG_FETCH;
  109209               0 :    yypParser->yyidx--;
  109210                 : #ifndef NDEBUG
  109211               0 :    if( yyTraceFILE ){
  109212               0 :      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
  109213                 :    }
  109214                 : #endif
  109215               0 :    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  109216                 :    /* Here code is inserted which will execute if the parser
  109217                 :    ** stack every overflows */
  109218                 : 
  109219                 :   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
  109220               0 :   sqlite3ErrorMsg(pParse, "parser stack overflow");
  109221               0 :    sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
  109222               0 : }
  109223                 : 
  109224                 : /*
  109225                 : ** Perform a shift action.
  109226                 : */
  109227        13430662 : static void yy_shift(
  109228                 :   yyParser *yypParser,          /* The parser to be shifted */
  109229                 :   int yyNewState,               /* The new state to shift in */
  109230                 :   int yyMajor,                  /* The major token to shift in */
  109231                 :   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
  109232                 : ){
  109233                 :   yyStackEntry *yytos;
  109234        13430662 :   yypParser->yyidx++;
  109235                 : #ifdef YYTRACKMAXSTACKDEPTH
  109236                 :   if( yypParser->yyidx>yypParser->yyidxMax ){
  109237                 :     yypParser->yyidxMax = yypParser->yyidx;
  109238                 :   }
  109239                 : #endif
  109240                 : #if YYSTACKDEPTH>0 
  109241        13430662 :   if( yypParser->yyidx>=YYSTACKDEPTH ){
  109242               0 :     yyStackOverflow(yypParser, yypMinor);
  109243               0 :     return;
  109244                 :   }
  109245                 : #else
  109246                 :   if( yypParser->yyidx>=yypParser->yystksz ){
  109247                 :     yyGrowStack(yypParser);
  109248                 :     if( yypParser->yyidx>=yypParser->yystksz ){
  109249                 :       yyStackOverflow(yypParser, yypMinor);
  109250                 :       return;
  109251                 :     }
  109252                 :   }
  109253                 : #endif
  109254        13430662 :   yytos = &yypParser->yystack[yypParser->yyidx];
  109255        13430662 :   yytos->stateno = (YYACTIONTYPE)yyNewState;
  109256        13430662 :   yytos->major = (YYCODETYPE)yyMajor;
  109257        13430662 :   yytos->minor = *yypMinor;
  109258                 : #ifndef NDEBUG
  109259        13430662 :   if( yyTraceFILE && yypParser->yyidx>0 ){
  109260                 :     int i;
  109261               0 :     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
  109262               0 :     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
  109263               0 :     for(i=1; i<=yypParser->yyidx; i++)
  109264               0 :       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
  109265               0 :     fprintf(yyTraceFILE,"\n");
  109266                 :   }
  109267                 : #endif
  109268                 : }
  109269                 : 
  109270                 : /* The following table contains information about every rule that
  109271                 : ** is used during the reduce.
  109272                 : */
  109273                 : static const struct {
  109274                 :   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
  109275                 :   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
  109276                 : } yyRuleInfo[] = {
  109277                 :   { 142, 1 },
  109278                 :   { 143, 2 },
  109279                 :   { 143, 1 },
  109280                 :   { 144, 1 },
  109281                 :   { 144, 3 },
  109282                 :   { 145, 0 },
  109283                 :   { 145, 1 },
  109284                 :   { 145, 3 },
  109285                 :   { 146, 1 },
  109286                 :   { 147, 3 },
  109287                 :   { 149, 0 },
  109288                 :   { 149, 1 },
  109289                 :   { 149, 2 },
  109290                 :   { 148, 0 },
  109291                 :   { 148, 1 },
  109292                 :   { 148, 1 },
  109293                 :   { 148, 1 },
  109294                 :   { 147, 2 },
  109295                 :   { 147, 2 },
  109296                 :   { 147, 2 },
  109297                 :   { 151, 1 },
  109298                 :   { 151, 0 },
  109299                 :   { 147, 2 },
  109300                 :   { 147, 3 },
  109301                 :   { 147, 5 },
  109302                 :   { 147, 2 },
  109303                 :   { 152, 6 },
  109304                 :   { 154, 1 },
  109305                 :   { 156, 0 },
  109306                 :   { 156, 3 },
  109307                 :   { 155, 1 },
  109308                 :   { 155, 0 },
  109309                 :   { 153, 4 },
  109310                 :   { 153, 2 },
  109311                 :   { 158, 3 },
  109312                 :   { 158, 1 },
  109313                 :   { 161, 3 },
  109314                 :   { 162, 1 },
  109315                 :   { 165, 1 },
  109316                 :   { 165, 1 },
  109317                 :   { 166, 1 },
  109318                 :   { 150, 1 },
  109319                 :   { 150, 1 },
  109320                 :   { 150, 1 },
  109321                 :   { 163, 0 },
  109322                 :   { 163, 1 },
  109323                 :   { 167, 1 },
  109324                 :   { 167, 4 },
  109325                 :   { 167, 6 },
  109326                 :   { 168, 1 },
  109327                 :   { 168, 2 },
  109328                 :   { 169, 1 },
  109329                 :   { 169, 1 },
  109330                 :   { 164, 2 },
  109331                 :   { 164, 0 },
  109332                 :   { 172, 3 },
  109333                 :   { 172, 1 },
  109334                 :   { 173, 2 },
  109335                 :   { 173, 4 },
  109336                 :   { 173, 3 },
  109337                 :   { 173, 3 },
  109338                 :   { 173, 2 },
  109339                 :   { 173, 2 },
  109340                 :   { 173, 3 },
  109341                 :   { 173, 5 },
  109342                 :   { 173, 2 },
  109343                 :   { 173, 4 },
  109344                 :   { 173, 4 },
  109345                 :   { 173, 1 },
  109346                 :   { 173, 2 },
  109347                 :   { 178, 0 },
  109348                 :   { 178, 1 },
  109349                 :   { 180, 0 },
  109350                 :   { 180, 2 },
  109351                 :   { 182, 2 },
  109352                 :   { 182, 3 },
  109353                 :   { 182, 3 },
  109354                 :   { 182, 3 },
  109355                 :   { 183, 2 },
  109356                 :   { 183, 2 },
  109357                 :   { 183, 1 },
  109358                 :   { 183, 1 },
  109359                 :   { 183, 2 },
  109360                 :   { 181, 3 },
  109361                 :   { 181, 2 },
  109362                 :   { 184, 0 },
  109363                 :   { 184, 2 },
  109364                 :   { 184, 2 },
  109365                 :   { 159, 0 },
  109366                 :   { 159, 2 },
  109367                 :   { 185, 3 },
  109368                 :   { 185, 2 },
  109369                 :   { 185, 1 },
  109370                 :   { 186, 2 },
  109371                 :   { 186, 7 },
  109372                 :   { 186, 5 },
  109373                 :   { 186, 5 },
  109374                 :   { 186, 10 },
  109375                 :   { 188, 0 },
  109376                 :   { 188, 1 },
  109377                 :   { 176, 0 },
  109378                 :   { 176, 3 },
  109379                 :   { 189, 0 },
  109380                 :   { 189, 2 },
  109381                 :   { 190, 1 },
  109382                 :   { 190, 1 },
  109383                 :   { 190, 1 },
  109384                 :   { 147, 4 },
  109385                 :   { 192, 2 },
  109386                 :   { 192, 0 },
  109387                 :   { 147, 8 },
  109388                 :   { 147, 4 },
  109389                 :   { 147, 1 },
  109390                 :   { 160, 1 },
  109391                 :   { 160, 3 },
  109392                 :   { 195, 1 },
  109393                 :   { 195, 2 },
  109394                 :   { 195, 1 },
  109395                 :   { 194, 9 },
  109396                 :   { 196, 1 },
  109397                 :   { 196, 1 },
  109398                 :   { 196, 0 },
  109399                 :   { 204, 2 },
  109400                 :   { 204, 0 },
  109401                 :   { 197, 3 },
  109402                 :   { 197, 2 },
  109403                 :   { 197, 4 },
  109404                 :   { 205, 2 },
  109405                 :   { 205, 1 },
  109406                 :   { 205, 0 },
  109407                 :   { 198, 0 },
  109408                 :   { 198, 2 },
  109409                 :   { 207, 2 },
  109410                 :   { 207, 0 },
  109411                 :   { 206, 7 },
  109412                 :   { 206, 7 },
  109413                 :   { 206, 7 },
  109414                 :   { 157, 0 },
  109415                 :   { 157, 2 },
  109416                 :   { 193, 2 },
  109417                 :   { 208, 1 },
  109418                 :   { 208, 2 },
  109419                 :   { 208, 3 },
  109420                 :   { 208, 4 },
  109421                 :   { 210, 2 },
  109422                 :   { 210, 0 },
  109423                 :   { 209, 0 },
  109424                 :   { 209, 3 },
  109425                 :   { 209, 2 },
  109426                 :   { 211, 4 },
  109427                 :   { 211, 0 },
  109428                 :   { 202, 0 },
  109429                 :   { 202, 3 },
  109430                 :   { 214, 4 },
  109431                 :   { 214, 2 },
  109432                 :   { 215, 1 },
  109433                 :   { 177, 1 },
  109434                 :   { 177, 1 },
  109435                 :   { 177, 0 },
  109436                 :   { 200, 0 },
  109437                 :   { 200, 3 },
  109438                 :   { 201, 0 },
  109439                 :   { 201, 2 },
  109440                 :   { 203, 0 },
  109441                 :   { 203, 2 },
  109442                 :   { 203, 4 },
  109443                 :   { 203, 4 },
  109444                 :   { 147, 5 },
  109445                 :   { 199, 0 },
  109446                 :   { 199, 2 },
  109447                 :   { 147, 7 },
  109448                 :   { 217, 5 },
  109449                 :   { 217, 3 },
  109450                 :   { 147, 8 },
  109451                 :   { 147, 5 },
  109452                 :   { 147, 6 },
  109453                 :   { 218, 2 },
  109454                 :   { 218, 1 },
  109455                 :   { 220, 3 },
  109456                 :   { 220, 1 },
  109457                 :   { 219, 0 },
  109458                 :   { 219, 3 },
  109459                 :   { 213, 3 },
  109460                 :   { 213, 1 },
  109461                 :   { 175, 1 },
  109462                 :   { 175, 3 },
  109463                 :   { 174, 1 },
  109464                 :   { 175, 1 },
  109465                 :   { 175, 1 },
  109466                 :   { 175, 3 },
  109467                 :   { 175, 5 },
  109468                 :   { 174, 1 },
  109469                 :   { 174, 1 },
  109470                 :   { 175, 1 },
  109471                 :   { 175, 1 },
  109472                 :   { 175, 3 },
  109473                 :   { 175, 6 },
  109474                 :   { 175, 5 },
  109475                 :   { 175, 4 },
  109476                 :   { 174, 1 },
  109477                 :   { 175, 3 },
  109478                 :   { 175, 3 },
  109479                 :   { 175, 3 },
  109480                 :   { 175, 3 },
  109481                 :   { 175, 3 },
  109482                 :   { 175, 3 },
  109483                 :   { 175, 3 },
  109484                 :   { 175, 3 },
  109485                 :   { 222, 1 },
  109486                 :   { 222, 2 },
  109487                 :   { 222, 1 },
  109488                 :   { 222, 2 },
  109489                 :   { 175, 3 },
  109490                 :   { 175, 5 },
  109491                 :   { 175, 2 },
  109492                 :   { 175, 3 },
  109493                 :   { 175, 3 },
  109494                 :   { 175, 4 },
  109495                 :   { 175, 2 },
  109496                 :   { 175, 2 },
  109497                 :   { 175, 2 },
  109498                 :   { 175, 2 },
  109499                 :   { 223, 1 },
  109500                 :   { 223, 2 },
  109501                 :   { 175, 5 },
  109502                 :   { 224, 1 },
  109503                 :   { 224, 2 },
  109504                 :   { 175, 5 },
  109505                 :   { 175, 3 },
  109506                 :   { 175, 5 },
  109507                 :   { 175, 4 },
  109508                 :   { 175, 4 },
  109509                 :   { 175, 5 },
  109510                 :   { 226, 5 },
  109511                 :   { 226, 4 },
  109512                 :   { 227, 2 },
  109513                 :   { 227, 0 },
  109514                 :   { 225, 1 },
  109515                 :   { 225, 0 },
  109516                 :   { 221, 1 },
  109517                 :   { 221, 0 },
  109518                 :   { 216, 3 },
  109519                 :   { 216, 1 },
  109520                 :   { 147, 11 },
  109521                 :   { 228, 1 },
  109522                 :   { 228, 0 },
  109523                 :   { 179, 0 },
  109524                 :   { 179, 3 },
  109525                 :   { 187, 5 },
  109526                 :   { 187, 3 },
  109527                 :   { 229, 0 },
  109528                 :   { 229, 2 },
  109529                 :   { 147, 4 },
  109530                 :   { 147, 1 },
  109531                 :   { 147, 2 },
  109532                 :   { 147, 3 },
  109533                 :   { 147, 5 },
  109534                 :   { 147, 6 },
  109535                 :   { 147, 5 },
  109536                 :   { 147, 6 },
  109537                 :   { 230, 1 },
  109538                 :   { 230, 1 },
  109539                 :   { 230, 1 },
  109540                 :   { 230, 1 },
  109541                 :   { 230, 1 },
  109542                 :   { 170, 2 },
  109543                 :   { 171, 2 },
  109544                 :   { 232, 1 },
  109545                 :   { 231, 1 },
  109546                 :   { 231, 0 },
  109547                 :   { 147, 5 },
  109548                 :   { 233, 11 },
  109549                 :   { 235, 1 },
  109550                 :   { 235, 1 },
  109551                 :   { 235, 2 },
  109552                 :   { 235, 0 },
  109553                 :   { 236, 1 },
  109554                 :   { 236, 1 },
  109555                 :   { 236, 3 },
  109556                 :   { 237, 0 },
  109557                 :   { 237, 3 },
  109558                 :   { 238, 0 },
  109559                 :   { 238, 2 },
  109560                 :   { 234, 3 },
  109561                 :   { 234, 2 },
  109562                 :   { 240, 1 },
  109563                 :   { 240, 3 },
  109564                 :   { 241, 0 },
  109565                 :   { 241, 3 },
  109566                 :   { 241, 2 },
  109567                 :   { 239, 7 },
  109568                 :   { 239, 8 },
  109569                 :   { 239, 5 },
  109570                 :   { 239, 5 },
  109571                 :   { 239, 1 },
  109572                 :   { 175, 4 },
  109573                 :   { 175, 6 },
  109574                 :   { 191, 1 },
  109575                 :   { 191, 1 },
  109576                 :   { 191, 1 },
  109577                 :   { 147, 4 },
  109578                 :   { 147, 6 },
  109579                 :   { 147, 3 },
  109580                 :   { 243, 0 },
  109581                 :   { 243, 2 },
  109582                 :   { 242, 1 },
  109583                 :   { 242, 0 },
  109584                 :   { 147, 1 },
  109585                 :   { 147, 3 },
  109586                 :   { 147, 1 },
  109587                 :   { 147, 3 },
  109588                 :   { 147, 6 },
  109589                 :   { 147, 6 },
  109590                 :   { 244, 1 },
  109591                 :   { 245, 0 },
  109592                 :   { 245, 1 },
  109593                 :   { 147, 1 },
  109594                 :   { 147, 4 },
  109595                 :   { 246, 7 },
  109596                 :   { 247, 1 },
  109597                 :   { 247, 3 },
  109598                 :   { 248, 0 },
  109599                 :   { 248, 2 },
  109600                 :   { 249, 1 },
  109601                 :   { 249, 3 },
  109602                 :   { 250, 1 },
  109603                 :   { 251, 0 },
  109604                 :   { 251, 4 },
  109605                 :   { 251, 2 },
  109606                 : };
  109607                 : 
  109608                 : static void yy_accept(yyParser*);  /* Forward Declaration */
  109609                 : 
  109610                 : /*
  109611                 : ** Perform a reduce action and the shift that must immediately
  109612                 : ** follow the reduce.
  109613                 : */
  109614         9512115 : static void yy_reduce(
  109615                 :   yyParser *yypParser,         /* The parser */
  109616                 :   int yyruleno                 /* Number of the rule by which to reduce */
  109617                 : ){
  109618                 :   int yygoto;                     /* The next state */
  109619                 :   int yyact;                      /* The next action */
  109620                 :   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
  109621                 :   yyStackEntry *yymsp;            /* The top of the parser's stack */
  109622                 :   int yysize;                     /* Amount to pop the stack */
  109623         9512115 :   sqlite3ParserARG_FETCH;
  109624         9512115 :   yymsp = &yypParser->yystack[yypParser->yyidx];
  109625                 : #ifndef NDEBUG
  109626         9512115 :   if( yyTraceFILE && yyruleno>=0 
  109627               0 :         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
  109628               0 :     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
  109629                 :       yyRuleName[yyruleno]);
  109630                 :   }
  109631                 : #endif /* NDEBUG */
  109632                 : 
  109633                 :   /* Silence complaints from purify about yygotominor being uninitialized
  109634                 :   ** in some cases when it is copied into the stack after the following
  109635                 :   ** switch.  yygotominor is uninitialized when a rule reduces that does
  109636                 :   ** not set the value of its left-hand side nonterminal.  Leaving the
  109637                 :   ** value of the nonterminal uninitialized is utterly harmless as long
  109638                 :   ** as the value is never used.  So really the only thing this code
  109639                 :   ** accomplishes is to quieten purify.  
  109640                 :   **
  109641                 :   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
  109642                 :   ** without this code, their parser segfaults.  I'm not sure what there
  109643                 :   ** parser is doing to make this happen.  This is the second bug report
  109644                 :   ** from wireshark this week.  Clearly they are stressing Lemon in ways
  109645                 :   ** that it has not been previously stressed...  (SQLite ticket #2172)
  109646                 :   */
  109647                 :   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
  109648         9512138 :   yygotominor = yyzerominor;
  109649                 : 
  109650                 : 
  109651         9512138 :   switch( yyruleno ){
  109652                 :   /* Beginning here are the reduction cases.  A typical example
  109653                 :   ** follows:
  109654                 :   **   case 0:
  109655                 :   **  #line <lineno> <grammarfile>
  109656                 :   **     { ... }           // User supplied code
  109657                 :   **  #line <lineno> <thisfile>
  109658                 :   **     break;
  109659                 :   */
  109660                 :       case 5: /* explain ::= */
  109661          193857 : { sqlite3BeginParse(pParse, 0); }
  109662          193857 :         break;
  109663                 :       case 6: /* explain ::= EXPLAIN */
  109664               0 : { sqlite3BeginParse(pParse, 1); }
  109665               0 :         break;
  109666                 :       case 7: /* explain ::= EXPLAIN QUERY PLAN */
  109667               0 : { sqlite3BeginParse(pParse, 2); }
  109668               0 :         break;
  109669                 :       case 8: /* cmdx ::= cmd */
  109670          193834 : { sqlite3FinishCoding(pParse); }
  109671          193834 :         break;
  109672                 :       case 9: /* cmd ::= BEGIN transtype trans_opt */
  109673            8415 : {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);}
  109674            8415 :         break;
  109675                 :       case 13: /* transtype ::= */
  109676             162 : {yygotominor.yy4 = TK_DEFERRED;}
  109677             162 :         break;
  109678                 :       case 14: /* transtype ::= DEFERRED */
  109679                 :       case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
  109680                 :       case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
  109681                 :       case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
  109682                 :       case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
  109683            8257 : {yygotominor.yy4 = yymsp[0].major;}
  109684            8257 :         break;
  109685                 :       case 17: /* cmd ::= COMMIT trans_opt */
  109686                 :       case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
  109687            8365 : {sqlite3CommitTransaction(pParse);}
  109688            8365 :         break;
  109689                 :       case 19: /* cmd ::= ROLLBACK trans_opt */
  109690              44 : {sqlite3RollbackTransaction(pParse);}
  109691              44 :         break;
  109692                 :       case 22: /* cmd ::= SAVEPOINT nm */
  109693                 : {
  109694            1740 :   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
  109695                 : }
  109696            1740 :         break;
  109697                 :       case 23: /* cmd ::= RELEASE savepoint_opt nm */
  109698                 : {
  109699            1276 :   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
  109700                 : }
  109701            1276 :         break;
  109702                 :       case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
  109703                 : {
  109704              10 :   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
  109705                 : }
  109706              10 :         break;
  109707                 :       case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
  109708                 : {
  109709           35689 :    sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
  109710                 : }
  109711           35689 :         break;
  109712                 :       case 27: /* createkw ::= CREATE */
  109713                 : {
  109714           65978 :   pParse->db->lookaside.bEnabled = 0;
  109715           65978 :   yygotominor.yy0 = yymsp[0].minor.yy0;
  109716                 : }
  109717           65978 :         break;
  109718                 :       case 28: /* ifnotexists ::= */
  109719                 :       case 31: /* temp ::= */ yytestcase(yyruleno==31);
  109720                 :       case 70: /* autoinc ::= */ yytestcase(yyruleno==70);
  109721                 :       case 83: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83);
  109722                 :       case 85: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==85);
  109723                 :       case 87: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==87);
  109724                 :       case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98);
  109725                 :       case 109: /* ifexists ::= */ yytestcase(yyruleno==109);
  109726                 :       case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120);
  109727                 :       case 121: /* distinct ::= */ yytestcase(yyruleno==121);
  109728                 :       case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222);
  109729                 :       case 225: /* in_op ::= IN */ yytestcase(yyruleno==225);
  109730          224325 : {yygotominor.yy4 = 0;}
  109731          224325 :         break;
  109732                 :       case 29: /* ifnotexists ::= IF NOT EXISTS */
  109733                 :       case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
  109734                 :       case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71);
  109735                 :       case 86: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==86);
  109736                 :       case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108);
  109737                 :       case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119);
  109738                 :       case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223);
  109739                 :       case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226);
  109740           18053 : {yygotominor.yy4 = 1;}
  109741           18053 :         break;
  109742                 :       case 32: /* create_table_args ::= LP columnlist conslist_opt RP */
  109743                 : {
  109744           35687 :   sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0);
  109745                 : }
  109746           35687 :         break;
  109747                 :       case 33: /* create_table_args ::= AS select */
  109748                 : {
  109749               0 :   sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy387);
  109750               0 :   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
  109751                 : }
  109752               0 :         break;
  109753                 :       case 36: /* column ::= columnid type carglist */
  109754                 : {
  109755          208617 :   yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
  109756          208617 :   yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
  109757                 : }
  109758          208617 :         break;
  109759                 :       case 37: /* columnid ::= nm */
  109760                 : {
  109761          208635 :   sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
  109762          208635 :   yygotominor.yy0 = yymsp[0].minor.yy0;
  109763                 : }
  109764          208635 :         break;
  109765                 :       case 38: /* id ::= ID */
  109766                 :       case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39);
  109767                 :       case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40);
  109768                 :       case 41: /* nm ::= id */ yytestcase(yyruleno==41);
  109769                 :       case 42: /* nm ::= STRING */ yytestcase(yyruleno==42);
  109770                 :       case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43);
  109771                 :       case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46);
  109772                 :       case 49: /* typename ::= ids */ yytestcase(yyruleno==49);
  109773                 :       case 127: /* as ::= AS nm */ yytestcase(yyruleno==127);
  109774                 :       case 128: /* as ::= ids */ yytestcase(yyruleno==128);
  109775                 :       case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138);
  109776                 :       case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147);
  109777                 :       case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251);
  109778                 :       case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260);
  109779                 :       case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261);
  109780                 :       case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262);
  109781                 :       case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263);
  109782                 :       case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264);
  109783                 :       case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265);
  109784                 :       case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266);
  109785                 :       case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267);
  109786                 :       case 285: /* trnm ::= nm */ yytestcase(yyruleno==285);
  109787         2636685 : {yygotominor.yy0 = yymsp[0].minor.yy0;}
  109788         2636685 :         break;
  109789                 :       case 45: /* type ::= typetoken */
  109790          199788 : {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
  109791          199788 :         break;
  109792                 :       case 47: /* typetoken ::= typename LP signed RP */
  109793                 : {
  109794            4239 :   yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
  109795            4239 :   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
  109796                 : }
  109797            4239 :         break;
  109798                 :       case 48: /* typetoken ::= typename LP signed COMMA signed RP */
  109799                 : {
  109800               0 :   yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
  109801               0 :   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
  109802                 : }
  109803               0 :         break;
  109804                 :       case 50: /* typename ::= typename ids */
  109805              52 : {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);}
  109806              52 :         break;
  109807                 :       case 57: /* ccons ::= DEFAULT term */
  109808                 :       case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59);
  109809           16806 : {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy118);}
  109810           16806 :         break;
  109811                 :       case 58: /* ccons ::= DEFAULT LP expr RP */
  109812               0 : {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy118);}
  109813               0 :         break;
  109814                 :       case 60: /* ccons ::= DEFAULT MINUS term */
  109815                 : {
  109816                 :   ExprSpan v;
  109817             956 :   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy118.pExpr, 0, 0);
  109818             956 :   v.zStart = yymsp[-1].minor.yy0.z;
  109819             956 :   v.zEnd = yymsp[0].minor.yy118.zEnd;
  109820             956 :   sqlite3AddDefaultValue(pParse,&v);
  109821                 : }
  109822             956 :         break;
  109823                 :       case 61: /* ccons ::= DEFAULT id */
  109824                 : {
  109825                 :   ExprSpan v;
  109826               0 :   spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
  109827               0 :   sqlite3AddDefaultValue(pParse,&v);
  109828                 : }
  109829               0 :         break;
  109830                 :       case 63: /* ccons ::= NOT NULL onconf */
  109831           23903 : {sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);}
  109832           23903 :         break;
  109833                 :       case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
  109834           16196 : {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2].minor.yy4);}
  109835           16196 :         break;
  109836                 :       case 65: /* ccons ::= UNIQUE onconf */
  109837            4526 : {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy4,0,0,0,0);}
  109838            4526 :         break;
  109839                 :       case 66: /* ccons ::= CHECK LP expr RP */
  109840               0 : {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy118.pExpr);}
  109841               0 :         break;
  109842                 :       case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */
  109843              88 : {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy4);}
  109844              88 :         break;
  109845                 :       case 68: /* ccons ::= defer_subclause */
  109846               0 : {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy4);}
  109847               0 :         break;
  109848                 :       case 69: /* ccons ::= COLLATE ids */
  109849               0 : {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
  109850               0 :         break;
  109851                 :       case 72: /* refargs ::= */
  109852            3880 : { yygotominor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */}
  109853            3880 :         break;
  109854                 :       case 73: /* refargs ::= refargs refarg */
  109855            3792 : { yygotominor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0].minor.yy215.value; }
  109856            3792 :         break;
  109857                 :       case 74: /* refarg ::= MATCH nm */
  109858                 :       case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75);
  109859               0 : { yygotominor.yy215.value = 0;     yygotominor.yy215.mask = 0x000000; }
  109860               0 :         break;
  109861                 :       case 76: /* refarg ::= ON DELETE refact */
  109862            3792 : { yygotominor.yy215.value = yymsp[0].minor.yy4;     yygotominor.yy215.mask = 0x0000ff; }
  109863            3792 :         break;
  109864                 :       case 77: /* refarg ::= ON UPDATE refact */
  109865               0 : { yygotominor.yy215.value = yymsp[0].minor.yy4<<8;  yygotominor.yy215.mask = 0x00ff00; }
  109866               0 :         break;
  109867                 :       case 78: /* refact ::= SET NULL */
  109868               0 : { yygotominor.yy4 = OE_SetNull;  /* EV: R-33326-45252 */}
  109869               0 :         break;
  109870                 :       case 79: /* refact ::= SET DEFAULT */
  109871               0 : { yygotominor.yy4 = OE_SetDflt;  /* EV: R-33326-45252 */}
  109872               0 :         break;
  109873                 :       case 80: /* refact ::= CASCADE */
  109874            3792 : { yygotominor.yy4 = OE_Cascade;  /* EV: R-33326-45252 */}
  109875            3792 :         break;
  109876                 :       case 81: /* refact ::= RESTRICT */
  109877               0 : { yygotominor.yy4 = OE_Restrict; /* EV: R-33326-45252 */}
  109878               0 :         break;
  109879                 :       case 82: /* refact ::= NO ACTION */
  109880               0 : { yygotominor.yy4 = OE_None;     /* EV: R-33326-45252 */}
  109881               0 :         break;
  109882                 :       case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
  109883                 :       case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99);
  109884                 :       case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101);
  109885                 :       case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104);
  109886             184 : {yygotominor.yy4 = yymsp[0].minor.yy4;}
  109887             184 :         break;
  109888                 :       case 88: /* conslist_opt ::= */
  109889           26125 : {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
  109890           26125 :         break;
  109891                 :       case 89: /* conslist_opt ::= COMMA conslist */
  109892            9562 : {yygotominor.yy0 = yymsp[-1].minor.yy0;}
  109893            9562 :         break;
  109894                 :       case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
  109895            2423 : {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2].minor.yy4,0);}
  109896            2423 :         break;
  109897                 :       case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */
  109898            8959 : {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0,0);}
  109899            8959 :         break;
  109900                 :       case 96: /* tcons ::= CHECK LP expr RP onconf */
  109901               0 : {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy118.pExpr);}
  109902               0 :         break;
  109903                 :       case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
  109904                 : {
  109905            3792 :     sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy4);
  109906            3792 :     sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy4);
  109907                 : }
  109908            3792 :         break;
  109909                 :       case 100: /* onconf ::= */
  109910           55833 : {yygotominor.yy4 = OE_Default;}
  109911           55833 :         break;
  109912                 :       case 102: /* orconf ::= */
  109913           34565 : {yygotominor.yy210 = OE_Default;}
  109914           34565 :         break;
  109915                 :       case 103: /* orconf ::= OR resolvetype */
  109916            1631 : {yygotominor.yy210 = (u8)yymsp[0].minor.yy4;}
  109917            1631 :         break;
  109918                 :       case 105: /* resolvetype ::= IGNORE */
  109919             382 : {yygotominor.yy4 = OE_Ignore;}
  109920             382 :         break;
  109921                 :       case 106: /* resolvetype ::= REPLACE */
  109922            1433 : {yygotominor.yy4 = OE_Replace;}
  109923            1433 :         break;
  109924                 :       case 107: /* cmd ::= DROP TABLE ifexists fullname */
  109925                 : {
  109926              96 :   sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4);
  109927                 : }
  109928              96 :         break;
  109929                 :       case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
  109930                 : {
  109931             156 :   sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy387, yymsp[-6].minor.yy4, yymsp[-4].minor.yy4);
  109932                 : }
  109933             156 :         break;
  109934                 :       case 111: /* cmd ::= DROP VIEW ifexists fullname */
  109935                 : {
  109936               0 :   sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4);
  109937                 : }
  109938               0 :         break;
  109939                 :       case 112: /* cmd ::= select */
  109940                 : {
  109941           45771 :   SelectDest dest = {SRT_Output, 0, 0, 0, 0};
  109942           45771 :   sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
  109943                 :   sqlite3ExplainBegin(pParse->pVdbe);
  109944                 :   sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy387);
  109945                 :   sqlite3ExplainFinish(pParse->pVdbe);
  109946           45771 :   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
  109947                 : }
  109948           45771 :         break;
  109949                 :       case 113: /* select ::= oneselect */
  109950           66251 : {yygotominor.yy387 = yymsp[0].minor.yy387;}
  109951           66251 :         break;
  109952                 :       case 114: /* select ::= select multiselect_op oneselect */
  109953                 : {
  109954             266 :   if( yymsp[0].minor.yy387 ){
  109955             266 :     yymsp[0].minor.yy387->op = (u8)yymsp[-1].minor.yy4;
  109956             266 :     yymsp[0].minor.yy387->pPrior = yymsp[-2].minor.yy387;
  109957                 :   }else{
  109958               0 :     sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy387);
  109959                 :   }
  109960             266 :   yygotominor.yy387 = yymsp[0].minor.yy387;
  109961                 : }
  109962             266 :         break;
  109963                 :       case 116: /* multiselect_op ::= UNION ALL */
  109964             262 : {yygotominor.yy4 = TK_ALL;}
  109965             262 :         break;
  109966                 :       case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
  109967                 : {
  109968           66517 :   yygotominor.yy387 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy259,yymsp[-4].minor.yy314,yymsp[-3].minor.yy322,yymsp[-2].minor.yy314,yymsp[-1].minor.yy322,yymsp[-7].minor.yy4,yymsp[0].minor.yy292.pLimit,yymsp[0].minor.yy292.pOffset);
  109969                 : }
  109970           66517 :         break;
  109971                 :       case 122: /* sclp ::= selcollist COMMA */
  109972                 :       case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247);
  109973          228134 : {yygotominor.yy322 = yymsp[-1].minor.yy322;}
  109974          228134 :         break;
  109975                 :       case 123: /* sclp ::= */
  109976                 :       case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151);
  109977                 :       case 159: /* groupby_opt ::= */ yytestcase(yyruleno==159);
  109978                 :       case 240: /* exprlist ::= */ yytestcase(yyruleno==240);
  109979                 :       case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246);
  109980          179327 : {yygotominor.yy322 = 0;}
  109981          179327 :         break;
  109982                 :       case 124: /* selcollist ::= sclp expr as */
  109983                 : {
  109984          287406 :    yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, yymsp[-1].minor.yy118.pExpr);
  109985          287406 :    if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[0].minor.yy0, 1);
  109986          287406 :    sqlite3ExprListSetSpan(pParse,yygotominor.yy322,&yymsp[-1].minor.yy118);
  109987                 : }
  109988          287406 :         break;
  109989                 :       case 125: /* selcollist ::= sclp STAR */
  109990                 : {
  109991            3365 :   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
  109992            3365 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy322, p);
  109993                 : }
  109994            3365 :         break;
  109995                 :       case 126: /* selcollist ::= sclp nm DOT STAR */
  109996                 : {
  109997               0 :   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
  109998               0 :   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
  109999               0 :   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
  110000               0 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, pDot);
  110001                 : }
  110002               0 :         break;
  110003                 :       case 129: /* as ::= */
  110004          345805 : {yygotominor.yy0.n = 0;}
  110005          345805 :         break;
  110006                 :       case 130: /* from ::= */
  110007            4245 : {yygotominor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy259));}
  110008            4245 :         break;
  110009                 :       case 131: /* from ::= FROM seltablist */
  110010                 : {
  110011           62272 :   yygotominor.yy259 = yymsp[0].minor.yy259;
  110012           62272 :   sqlite3SrcListShiftJoinType(yygotominor.yy259);
  110013                 : }
  110014           62272 :         break;
  110015                 :       case 132: /* stl_prefix ::= seltablist joinop */
  110016                 : {
  110017            5338 :    yygotominor.yy259 = yymsp[-1].minor.yy259;
  110018            5338 :    if( ALWAYS(yygotominor.yy259 && yygotominor.yy259->nSrc>0) ) yygotominor.yy259->a[yygotominor.yy259->nSrc-1].jointype = (u8)yymsp[0].minor.yy4;
  110019                 : }
  110020            5338 :         break;
  110021                 :       case 133: /* stl_prefix ::= */
  110022           62272 : {yygotominor.yy259 = 0;}
  110023           62272 :         break;
  110024                 :       case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
  110025                 : {
  110026           67542 :   yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
  110027           67542 :   sqlite3SrcListIndexedBy(pParse, yygotominor.yy259, &yymsp[-2].minor.yy0);
  110028                 : }
  110029           67542 :         break;
  110030                 :       case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
  110031                 : {
  110032              68 :     yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy387,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
  110033                 :   }
  110034              68 :         break;
  110035                 :       case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
  110036                 : {
  110037               0 :     if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy314==0 && yymsp[0].minor.yy384==0 ){
  110038               0 :       yygotominor.yy259 = yymsp[-4].minor.yy259;
  110039                 :     }else{
  110040                 :       Select *pSubquery;
  110041               0 :       sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259);
  110042               0 :       pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,0,0,0);
  110043               0 :       yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy314,yymsp[0].minor.yy384);
  110044                 :     }
  110045                 :   }
  110046               0 :         break;
  110047                 :       case 137: /* dbnm ::= */
  110048                 :       case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146);
  110049          250276 : {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
  110050          250276 :         break;
  110051                 :       case 139: /* fullname ::= nm dbnm */
  110052           49425 : {yygotominor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
  110053           49425 :         break;
  110054                 :       case 140: /* joinop ::= COMMA|JOIN */
  110055            2535 : { yygotominor.yy4 = JT_INNER; }
  110056            2535 :         break;
  110057                 :       case 141: /* joinop ::= JOIN_KW JOIN */
  110058            2683 : { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
  110059            2683 :         break;
  110060                 :       case 142: /* joinop ::= JOIN_KW nm JOIN */
  110061             120 : { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
  110062             120 :         break;
  110063                 :       case 143: /* joinop ::= JOIN_KW nm nm JOIN */
  110064               0 : { yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
  110065               0 :         break;
  110066                 :       case 144: /* on_opt ::= ON expr */
  110067                 :       case 155: /* sortitem ::= expr */ yytestcase(yyruleno==155);
  110068                 :       case 162: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==162);
  110069                 :       case 169: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==169);
  110070                 :       case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235);
  110071                 :       case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237);
  110072          107601 : {yygotominor.yy314 = yymsp[0].minor.yy118.pExpr;}
  110073          107601 :         break;
  110074                 :       case 145: /* on_opt ::= */
  110075                 :       case 161: /* having_opt ::= */ yytestcase(yyruleno==161);
  110076                 :       case 168: /* where_opt ::= */ yytestcase(yyruleno==168);
  110077                 :       case 236: /* case_else ::= */ yytestcase(yyruleno==236);
  110078                 :       case 238: /* case_operand ::= */ yytestcase(yyruleno==238);
  110079          146460 : {yygotominor.yy314 = 0;}
  110080          146460 :         break;
  110081                 :       case 148: /* indexed_opt ::= NOT INDEXED */
  110082               0 : {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
  110083               0 :         break;
  110084                 :       case 149: /* using_opt ::= USING LP inscollist RP */
  110085                 :       case 181: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==181);
  110086            6330 : {yygotominor.yy384 = yymsp[-1].minor.yy384;}
  110087            6330 :         break;
  110088                 :       case 150: /* using_opt ::= */
  110089                 :       case 180: /* inscollist_opt ::= */ yytestcase(yyruleno==180);
  110090           81957 : {yygotominor.yy384 = 0;}
  110091           81957 :         break;
  110092                 :       case 152: /* orderby_opt ::= ORDER BY sortlist */
  110093                 :       case 160: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==160);
  110094                 :       case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239);
  110095           57469 : {yygotominor.yy322 = yymsp[0].minor.yy322;}
  110096           57469 :         break;
  110097                 :       case 153: /* sortlist ::= sortlist COMMA sortitem sortorder */
  110098                 : {
  110099             653 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[-1].minor.yy314);
  110100             653 :   if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
  110101                 : }
  110102             653 :         break;
  110103                 :       case 154: /* sortlist ::= sortitem sortorder */
  110104                 : {
  110105           22248 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy314);
  110106           22248 :   if( yygotominor.yy322 && ALWAYS(yygotominor.yy322->a) ) yygotominor.yy322->a[0].sortOrder = (u8)yymsp[0].minor.yy4;
  110107                 : }
  110108           22248 :         break;
  110109                 :       case 156: /* sortorder ::= ASC */
  110110                 :       case 158: /* sortorder ::= */ yytestcase(yyruleno==158);
  110111           92478 : {yygotominor.yy4 = SQLITE_SO_ASC;}
  110112           92478 :         break;
  110113                 :       case 157: /* sortorder ::= DESC */
  110114            2846 : {yygotominor.yy4 = SQLITE_SO_DESC;}
  110115            2846 :         break;
  110116                 :       case 163: /* limit_opt ::= */
  110117           62584 : {yygotominor.yy292.pLimit = 0; yygotominor.yy292.pOffset = 0;}
  110118           62584 :         break;
  110119                 :       case 164: /* limit_opt ::= LIMIT expr */
  110120            3933 : {yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr; yygotominor.yy292.pOffset = 0;}
  110121            3933 :         break;
  110122                 :       case 165: /* limit_opt ::= LIMIT expr OFFSET expr */
  110123               0 : {yygotominor.yy292.pLimit = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pOffset = yymsp[0].minor.yy118.pExpr;}
  110124               0 :         break;
  110125                 :       case 166: /* limit_opt ::= LIMIT expr COMMA expr */
  110126               0 : {yygotominor.yy292.pOffset = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr;}
  110127               0 :         break;
  110128                 :       case 167: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */
  110129                 : {
  110130            5618 :   sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0);
  110131            5618 :   sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314);
  110132                 : }
  110133            5618 :         break;
  110134                 :       case 170: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */
  110135                 : {
  110136           12243 :   sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0);
  110137           12243 :   sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list"); 
  110138           12243 :   sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].minor.yy314,yymsp[-5].minor.yy210);
  110139                 : }
  110140           12243 :         break;
  110141                 :       case 171: /* setlist ::= setlist COMMA nm EQ expr */
  110142                 : {
  110143           34978 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy118.pExpr);
  110144           34978 :   sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
  110145                 : }
  110146           34978 :         break;
  110147                 :       case 172: /* setlist ::= nm EQ expr */
  110148                 : {
  110149           15519 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy118.pExpr);
  110150           15519 :   sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
  110151                 : }
  110152           15519 :         break;
  110153                 :       case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */
  110154           19774 : {sqlite3Insert(pParse, yymsp[-5].minor.yy259, yymsp[-1].minor.yy322, 0, yymsp[-4].minor.yy384, yymsp[-7].minor.yy210);}
  110155           19774 :         break;
  110156                 :       case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */
  110157              98 : {sqlite3Insert(pParse, yymsp[-2].minor.yy259, 0, yymsp[0].minor.yy387, yymsp[-1].minor.yy384, yymsp[-4].minor.yy210);}
  110158              98 :         break;
  110159                 :       case 175: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
  110160               0 : {sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, 0, yymsp[-2].minor.yy384, yymsp[-5].minor.yy210);}
  110161               0 :         break;
  110162                 :       case 176: /* insert_cmd ::= INSERT orconf */
  110163           20677 : {yygotominor.yy210 = yymsp[0].minor.yy210;}
  110164           20677 :         break;
  110165                 :       case 177: /* insert_cmd ::= REPLACE */
  110166               0 : {yygotominor.yy210 = OE_Replace;}
  110167               0 :         break;
  110168                 :       case 178: /* itemlist ::= itemlist COMMA expr */
  110169                 :       case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241);
  110170          115360 : {yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy118.pExpr);}
  110171          115360 :         break;
  110172                 :       case 179: /* itemlist ::= expr */
  110173                 :       case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242);
  110174           55800 : {yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy118.pExpr);}
  110175           55800 :         break;
  110176                 :       case 182: /* inscollist ::= inscollist COMMA nm */
  110177           23743 : {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp[0].minor.yy0);}
  110178           23743 :         break;
  110179                 :       case 183: /* inscollist ::= nm */
  110180            8620 : {yygotominor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
  110181            8620 :         break;
  110182                 :       case 184: /* expr ::= term */
  110183          158436 : {yygotominor.yy118 = yymsp[0].minor.yy118;}
  110184          158436 :         break;
  110185                 :       case 185: /* expr ::= LP expr RP */
  110186            3992 : {yygotominor.yy118.pExpr = yymsp[-1].minor.yy118.pExpr; spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
  110187            3992 :         break;
  110188                 :       case 186: /* term ::= NULL */
  110189                 :       case 191: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==191);
  110190                 :       case 192: /* term ::= STRING */ yytestcase(yyruleno==192);
  110191          176198 : {spanExpr(&yygotominor.yy118, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
  110192          176198 :         break;
  110193                 :       case 187: /* expr ::= id */
  110194                 :       case 188: /* expr ::= JOIN_KW */ yytestcase(yyruleno==188);
  110195          384087 : {spanExpr(&yygotominor.yy118, pParse, TK_ID, &yymsp[0].minor.yy0);}
  110196          384087 :         break;
  110197                 :       case 189: /* expr ::= nm DOT nm */
  110198                 : {
  110199           73489 :   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
  110200           73489 :   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
  110201           73489 :   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
  110202           73489 :   spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
  110203                 : }
  110204           73489 :         break;
  110205                 :       case 190: /* expr ::= nm DOT nm DOT nm */
  110206                 : {
  110207               0 :   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
  110208               0 :   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
  110209               0 :   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
  110210               0 :   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
  110211               0 :   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
  110212               0 :   spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
  110213                 : }
  110214               0 :         break;
  110215                 :       case 193: /* expr ::= REGISTER */
  110216                 : {
  110217                 :   /* When doing a nested parse, one can include terms in an expression
  110218                 :   ** that look like this:   #1 #2 ...  These terms refer to registers
  110219                 :   ** in the virtual machine.  #N is the N-th register. */
  110220           23379 :   if( pParse->nested==0 ){
  110221               0 :     sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
  110222               0 :     yygotominor.yy118.pExpr = 0;
  110223                 :   }else{
  110224           23379 :     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
  110225           23379 :     if( yygotominor.yy118.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy118.pExpr->iTable);
  110226                 :   }
  110227           23379 :   spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
  110228                 : }
  110229           23379 :         break;
  110230                 :       case 194: /* expr ::= VARIABLE */
  110231                 : {
  110232           87175 :   spanExpr(&yygotominor.yy118, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
  110233           87175 :   sqlite3ExprAssignVarNumber(pParse, yygotominor.yy118.pExpr);
  110234           87175 :   spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
  110235                 : }
  110236           87175 :         break;
  110237                 :       case 195: /* expr ::= expr COLLATE ids */
  110238                 : {
  110239              31 :   yygotominor.yy118.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0);
  110240              31 :   yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
  110241              31 :   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110242                 : }
  110243              31 :         break;
  110244                 :       case 196: /* expr ::= CAST LP expr AS typetoken RP */
  110245                 : {
  110246             805 :   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy118.pExpr, 0, &yymsp[-1].minor.yy0);
  110247             805 :   spanSet(&yygotominor.yy118,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
  110248                 : }
  110249             805 :         break;
  110250                 :       case 197: /* expr ::= ID LP distinct exprlist RP */
  110251                 : {
  110252           34853 :   if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
  110253               0 :     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
  110254                 :   }
  110255           34853 :   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0);
  110256           34853 :   spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
  110257           34853 :   if( yymsp[-2].minor.yy4 && yygotominor.yy118.pExpr ){
  110258              27 :     yygotominor.yy118.pExpr->flags |= EP_Distinct;
  110259                 :   }
  110260                 : }
  110261           34853 :         break;
  110262                 :       case 198: /* expr ::= ID LP STAR RP */
  110263                 : {
  110264            1443 :   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
  110265            1443 :   spanSet(&yygotominor.yy118,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
  110266                 : }
  110267            1443 :         break;
  110268                 :       case 199: /* term ::= CTIME_KW */
  110269                 : {
  110270                 :   /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
  110271                 :   ** treated as functions that return constants */
  110272               0 :   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0);
  110273               0 :   if( yygotominor.yy118.pExpr ){
  110274               0 :     yygotominor.yy118.pExpr->op = TK_CONST_FUNC;  
  110275                 :   }
  110276               0 :   spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
  110277                 : }
  110278               0 :         break;
  110279                 :       case 200: /* expr ::= expr AND expr */
  110280                 :       case 201: /* expr ::= expr OR expr */ yytestcase(yyruleno==201);
  110281                 :       case 202: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==202);
  110282                 :       case 203: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==203);
  110283                 :       case 204: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==204);
  110284                 :       case 205: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==205);
  110285                 :       case 206: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==206);
  110286                 :       case 207: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==207);
  110287          146590 : {spanBinaryExpr(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);}
  110288          146590 :         break;
  110289                 :       case 208: /* likeop ::= LIKE_KW */
  110290                 :       case 210: /* likeop ::= MATCH */ yytestcase(yyruleno==210);
  110291             217 : {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 0;}
  110292             217 :         break;
  110293                 :       case 209: /* likeop ::= NOT LIKE_KW */
  110294                 :       case 211: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==211);
  110295               0 : {yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 1;}
  110296               0 :         break;
  110297                 :       case 212: /* expr ::= expr likeop expr */
  110298                 : {
  110299                 :   ExprList *pList;
  110300             118 :   pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy118.pExpr);
  110301             118 :   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy118.pExpr);
  110302             118 :   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy342.eOperator);
  110303             118 :   if( yymsp[-1].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
  110304             118 :   yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart;
  110305             118 :   yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
  110306             118 :   if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
  110307                 : }
  110308             118 :         break;
  110309                 :       case 213: /* expr ::= expr likeop expr ESCAPE expr */
  110310                 : {
  110311                 :   ExprList *pList;
  110312              99 :   pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
  110313              99 :   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy118.pExpr);
  110314              99 :   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
  110315              99 :   yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy342.eOperator);
  110316              99 :   if( yymsp[-3].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
  110317              99 :   yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
  110318              99 :   yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
  110319              99 :   if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc;
  110320                 : }
  110321              99 :         break;
  110322                 :       case 214: /* expr ::= expr ISNULL|NOTNULL */
  110323            2506 : {spanUnaryPostfix(&yygotominor.yy118,pParse,yymsp[0].major,&yymsp[-1].minor.yy118,&yymsp[0].minor.yy0);}
  110324            2506 :         break;
  110325                 :       case 215: /* expr ::= expr NOT NULL */
  110326              94 : {spanUnaryPostfix(&yygotominor.yy118,pParse,TK_NOTNULL,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy0);}
  110327              94 :         break;
  110328                 :       case 216: /* expr ::= expr IS expr */
  110329                 : {
  110330            1293 :   spanBinaryExpr(&yygotominor.yy118,pParse,TK_IS,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);
  110331            1293 :   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_ISNULL);
  110332                 : }
  110333            1293 :         break;
  110334                 :       case 217: /* expr ::= expr IS NOT expr */
  110335                 : {
  110336            2536 :   spanBinaryExpr(&yygotominor.yy118,pParse,TK_ISNOT,&yymsp[-3].minor.yy118,&yymsp[0].minor.yy118);
  110337            2536 :   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_NOTNULL);
  110338                 : }
  110339            2536 :         break;
  110340                 :       case 218: /* expr ::= NOT expr */
  110341                 :       case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219);
  110342            2825 : {spanUnaryPrefix(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
  110343            2825 :         break;
  110344                 :       case 220: /* expr ::= MINUS expr */
  110345              84 : {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UMINUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
  110346              84 :         break;
  110347                 :       case 221: /* expr ::= PLUS expr */
  110348             105 : {spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UPLUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);}
  110349             105 :         break;
  110350                 :       case 224: /* expr ::= expr between_op expr AND expr */
  110351                 : {
  110352             546 :   ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
  110353             546 :   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr);
  110354             546 :   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy118.pExpr, 0, 0);
  110355             546 :   if( yygotominor.yy118.pExpr ){
  110356             546 :     yygotominor.yy118.pExpr->x.pList = pList;
  110357                 :   }else{
  110358               0 :     sqlite3ExprListDelete(pParse->db, pList);
  110359                 :   } 
  110360             546 :   if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
  110361             546 :   yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
  110362             546 :   yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd;
  110363                 : }
  110364             546 :         break;
  110365                 :       case 227: /* expr ::= expr in_op LP exprlist RP */
  110366                 : {
  110367            2392 :     if( yymsp[-1].minor.yy322==0 ){
  110368                 :       /* Expressions of the form
  110369                 :       **
  110370                 :       **      expr1 IN ()
  110371                 :       **      expr1 NOT IN ()
  110372                 :       **
  110373                 :       ** simplify to constants 0 (false) and 1 (true), respectively,
  110374                 :       ** regardless of the value of expr1.
  110375                 :       */
  110376              13 :       yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy4]);
  110377              13 :       sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy118.pExpr);
  110378                 :     }else{
  110379            2379 :       yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
  110380            2379 :       if( yygotominor.yy118.pExpr ){
  110381            2379 :         yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy322;
  110382            2379 :         sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
  110383                 :       }else{
  110384               0 :         sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
  110385                 :       }
  110386            2379 :       if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
  110387                 :     }
  110388            2392 :     yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
  110389            2392 :     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110390                 :   }
  110391            2392 :         break;
  110392                 :       case 228: /* expr ::= LP select RP */
  110393                 : {
  110394           12504 :     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
  110395           12504 :     if( yygotominor.yy118.pExpr ){
  110396           12504 :       yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
  110397           12504 :       ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
  110398           12504 :       sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
  110399                 :     }else{
  110400               0 :       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
  110401                 :     }
  110402           12504 :     yygotominor.yy118.zStart = yymsp[-2].minor.yy0.z;
  110403           12504 :     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110404                 :   }
  110405           12504 :         break;
  110406                 :       case 229: /* expr ::= expr in_op LP select RP */
  110407                 : {
  110408            2389 :     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0);
  110409            2389 :     if( yygotominor.yy118.pExpr ){
  110410            2389 :       yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387;
  110411            2389 :       ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
  110412            2389 :       sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
  110413                 :     }else{
  110414               0 :       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
  110415                 :     }
  110416            2389 :     if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
  110417            2389 :     yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart;
  110418            2389 :     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110419                 :   }
  110420            2389 :         break;
  110421                 :       case 230: /* expr ::= expr in_op nm dbnm */
  110422                 : {
  110423               0 :     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
  110424               0 :     yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy118.pExpr, 0, 0);
  110425               0 :     if( yygotominor.yy118.pExpr ){
  110426               0 :       yygotominor.yy118.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
  110427               0 :       ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect);
  110428               0 :       sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
  110429                 :     }else{
  110430               0 :       sqlite3SrcListDelete(pParse->db, pSrc);
  110431                 :     }
  110432               0 :     if( yymsp[-2].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0);
  110433               0 :     yygotominor.yy118.zStart = yymsp[-3].minor.yy118.zStart;
  110434               0 :     yygotominor.yy118.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];
  110435                 :   }
  110436               0 :         break;
  110437                 :       case 231: /* expr ::= EXISTS LP select RP */
  110438                 : {
  110439            3337 :     Expr *p = yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
  110440            3337 :     if( p ){
  110441            3337 :       p->x.pSelect = yymsp[-1].minor.yy387;
  110442            3337 :       ExprSetProperty(p, EP_xIsSelect);
  110443            3337 :       sqlite3ExprSetHeight(pParse, p);
  110444                 :     }else{
  110445               0 :       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387);
  110446                 :     }
  110447            3337 :     yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
  110448            3337 :     yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110449                 :   }
  110450            3337 :         break;
  110451                 :       case 232: /* expr ::= CASE case_operand case_exprlist case_else END */
  110452                 : {
  110453              70 :   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, yymsp[-1].minor.yy314, 0);
  110454              70 :   if( yygotominor.yy118.pExpr ){
  110455              70 :     yygotominor.yy118.pExpr->x.pList = yymsp[-2].minor.yy322;
  110456              70 :     sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr);
  110457                 :   }else{
  110458               0 :     sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
  110459                 :   }
  110460              70 :   yygotominor.yy118.zStart = yymsp[-4].minor.yy0.z;
  110461              70 :   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110462                 : }
  110463              70 :         break;
  110464                 :       case 233: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
  110465                 : {
  110466               0 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy118.pExpr);
  110467               0 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
  110468                 : }
  110469               0 :         break;
  110470                 :       case 234: /* case_exprlist ::= WHEN expr THEN expr */
  110471                 : {
  110472              70 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr);
  110473              70 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr);
  110474                 : }
  110475              70 :         break;
  110476                 :       case 243: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */
  110477                 : {
  110478           93495 :   sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0, 
  110479           56097 :                      sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy322, yymsp[-9].minor.yy4,
  110480           37398 :                       &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy4);
  110481                 : }
  110482           18699 :         break;
  110483                 :       case 244: /* uniqueflag ::= UNIQUE */
  110484                 :       case 298: /* raisetype ::= ABORT */ yytestcase(yyruleno==298);
  110485            4659 : {yygotominor.yy4 = OE_Abort;}
  110486            4659 :         break;
  110487                 :       case 245: /* uniqueflag ::= */
  110488           14040 : {yygotominor.yy4 = OE_None;}
  110489           14040 :         break;
  110490                 :       case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */
  110491                 : {
  110492           18474 :   Expr *p = 0;
  110493           18474 :   if( yymsp[-1].minor.yy0.n>0 ){
  110494               0 :     p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
  110495               0 :     sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
  110496                 :   }
  110497           18474 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, p);
  110498           18474 :   sqlite3ExprListSetName(pParse,yygotominor.yy322,&yymsp[-2].minor.yy0,1);
  110499           18474 :   sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
  110500           18474 :   if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
  110501                 : }
  110502           18474 :         break;
  110503                 :       case 249: /* idxlist ::= nm collate sortorder */
  110504                 : {
  110505           37753 :   Expr *p = 0;
  110506           37753 :   if( yymsp[-1].minor.yy0.n>0 ){
  110507               0 :     p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
  110508               0 :     sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0);
  110509                 :   }
  110510           37753 :   yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, p);
  110511           37753 :   sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1);
  110512           37753 :   sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index");
  110513           37753 :   if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4;
  110514                 : }
  110515           37753 :         break;
  110516                 :       case 250: /* collate ::= */
  110517           56227 : {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
  110518           56227 :         break;
  110519                 :       case 252: /* cmd ::= DROP INDEX ifexists fullname */
  110520              58 : {sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);}
  110521              58 :         break;
  110522                 :       case 253: /* cmd ::= VACUUM */
  110523                 :       case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254);
  110524               6 : {sqlite3Vacuum(pParse);}
  110525               6 :         break;
  110526                 :       case 255: /* cmd ::= PRAGMA nm dbnm */
  110527            6065 : {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
  110528            6065 :         break;
  110529                 :       case 256: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
  110530           16523 : {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
  110531           16523 :         break;
  110532                 :       case 257: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
  110533              46 : {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
  110534              46 :         break;
  110535                 :       case 258: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
  110536              35 : {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
  110537              35 :         break;
  110538                 :       case 259: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
  110539               0 : {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
  110540               0 :         break;
  110541                 :       case 270: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
  110542                 : {
  110543                 :   Token all;
  110544           11328 :   all.z = yymsp[-3].minor.yy0.z;
  110545           11328 :   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
  110546           11328 :   sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all);
  110547                 : }
  110548           11328 :         break;
  110549                 :       case 271: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
  110550                 : {
  110551           11328 :   sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy4, yymsp[-4].minor.yy90.a, yymsp[-4].minor.yy90.b, yymsp[-2].minor.yy259, yymsp[0].minor.yy314, yymsp[-10].minor.yy4, yymsp[-8].minor.yy4);
  110552           11328 :   yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
  110553                 : }
  110554           11328 :         break;
  110555                 :       case 272: /* trigger_time ::= BEFORE */
  110556                 :       case 275: /* trigger_time ::= */ yytestcase(yyruleno==275);
  110557              66 : { yygotominor.yy4 = TK_BEFORE; }
  110558              66 :         break;
  110559                 :       case 273: /* trigger_time ::= AFTER */
  110560           11106 : { yygotominor.yy4 = TK_AFTER;  }
  110561           11106 :         break;
  110562                 :       case 274: /* trigger_time ::= INSTEAD OF */
  110563             156 : { yygotominor.yy4 = TK_INSTEAD;}
  110564             156 :         break;
  110565                 :       case 276: /* trigger_event ::= DELETE|INSERT */
  110566                 :       case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277);
  110567            9038 : {yygotominor.yy90.a = yymsp[0].major; yygotominor.yy90.b = 0;}
  110568            9038 :         break;
  110569                 :       case 278: /* trigger_event ::= UPDATE OF inscollist */
  110570            2290 : {yygotominor.yy90.a = TK_UPDATE; yygotominor.yy90.b = yymsp[0].minor.yy384;}
  110571            2290 :         break;
  110572                 :       case 281: /* when_clause ::= */
  110573                 :       case 303: /* key_opt ::= */ yytestcase(yyruleno==303);
  110574            5137 : { yygotominor.yy314 = 0; }
  110575            5137 :         break;
  110576                 :       case 282: /* when_clause ::= WHEN expr */
  110577                 :       case 304: /* key_opt ::= KEY expr */ yytestcase(yyruleno==304);
  110578            6197 : { yygotominor.yy314 = yymsp[0].minor.yy118.pExpr; }
  110579            6197 :         break;
  110580                 :       case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
  110581                 : {
  110582            3920 :   assert( yymsp[-2].minor.yy203!=0 );
  110583            3920 :   yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203;
  110584            3920 :   yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203;
  110585            3920 :   yygotominor.yy203 = yymsp[-2].minor.yy203;
  110586                 : }
  110587            3920 :         break;
  110588                 :       case 284: /* trigger_cmd_list ::= trigger_cmd SEMI */
  110589                 : { 
  110590           11328 :   assert( yymsp[-1].minor.yy203!=0 );
  110591           11328 :   yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203;
  110592           11328 :   yygotominor.yy203 = yymsp[-1].minor.yy203;
  110593                 : }
  110594           11328 :         break;
  110595                 :       case 286: /* trnm ::= nm DOT nm */
  110596                 : {
  110597               0 :   yygotominor.yy0 = yymsp[0].minor.yy0;
  110598               0 :   sqlite3ErrorMsg(pParse, 
  110599                 :         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
  110600                 :         "statements within triggers");
  110601                 : }
  110602               0 :         break;
  110603                 :       case 288: /* tridxby ::= INDEXED BY nm */
  110604                 : {
  110605               0 :   sqlite3ErrorMsg(pParse,
  110606                 :         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
  110607                 :         "within triggers");
  110608                 : }
  110609               0 :         break;
  110610                 :       case 289: /* tridxby ::= NOT INDEXED */
  110611                 : {
  110612               0 :   sqlite3ErrorMsg(pParse,
  110613                 :         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
  110614                 :         "within triggers");
  110615                 : }
  110616               0 :         break;
  110617                 :       case 290: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
  110618            3276 : { yygotominor.yy203 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy322, yymsp[0].minor.yy314, yymsp[-5].minor.yy210); }
  110619            3276 :         break;
  110620                 :       case 291: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP */
  110621             805 : {yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy384, yymsp[-1].minor.yy322, 0, yymsp[-7].minor.yy210);}
  110622             805 :         break;
  110623                 :       case 292: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
  110624               0 : {yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy384, 0, yymsp[0].minor.yy387, yymsp[-4].minor.yy210);}
  110625               0 :         break;
  110626                 :       case 293: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
  110627            9239 : {yygotominor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy314);}
  110628            9239 :         break;
  110629                 :       case 294: /* trigger_cmd ::= select */
  110630            1928 : {yygotominor.yy203 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy387); }
  110631            1928 :         break;
  110632                 :       case 295: /* expr ::= RAISE LP IGNORE RP */
  110633                 : {
  110634               0 :   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
  110635               0 :   if( yygotominor.yy118.pExpr ){
  110636               0 :     yygotominor.yy118.pExpr->affinity = OE_Ignore;
  110637                 :   }
  110638               0 :   yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z;
  110639               0 :   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110640                 : }
  110641               0 :         break;
  110642                 :       case 296: /* expr ::= RAISE LP raisetype COMMA nm RP */
  110643                 : {
  110644               0 :   yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); 
  110645               0 :   if( yygotominor.yy118.pExpr ) {
  110646               0 :     yygotominor.yy118.pExpr->affinity = (char)yymsp[-3].minor.yy4;
  110647                 :   }
  110648               0 :   yygotominor.yy118.zStart = yymsp[-5].minor.yy0.z;
  110649               0 :   yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
  110650                 : }
  110651               0 :         break;
  110652                 :       case 297: /* raisetype ::= ROLLBACK */
  110653               0 : {yygotominor.yy4 = OE_Rollback;}
  110654               0 :         break;
  110655                 :       case 299: /* raisetype ::= FAIL */
  110656               0 : {yygotominor.yy4 = OE_Fail;}
  110657               0 :         break;
  110658                 :       case 300: /* cmd ::= DROP TRIGGER ifexists fullname */
  110659                 : {
  110660              71 :   sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4);
  110661                 : }
  110662              71 :         break;
  110663                 :       case 301: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
  110664                 : {
  110665               6 :   sqlite3Attach(pParse, yymsp[-3].minor.yy118.pExpr, yymsp[-1].minor.yy118.pExpr, yymsp[0].minor.yy314);
  110666                 : }
  110667               6 :         break;
  110668                 :       case 302: /* cmd ::= DETACH database_kw_opt expr */
  110669                 : {
  110670               0 :   sqlite3Detach(pParse, yymsp[0].minor.yy118.pExpr);
  110671                 : }
  110672               0 :         break;
  110673                 :       case 307: /* cmd ::= REINDEX */
  110674               2 : {sqlite3Reindex(pParse, 0, 0);}
  110675               2 :         break;
  110676                 :       case 308: /* cmd ::= REINDEX nm dbnm */
  110677               0 : {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
  110678               0 :         break;
  110679                 :       case 309: /* cmd ::= ANALYZE */
  110680               0 : {sqlite3Analyze(pParse, 0, 0);}
  110681               0 :         break;
  110682                 :       case 310: /* cmd ::= ANALYZE nm dbnm */
  110683            1475 : {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
  110684            1475 :         break;
  110685                 :       case 311: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
  110686                 : {
  110687               0 :   sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0);
  110688                 : }
  110689               0 :         break;
  110690                 :       case 312: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
  110691                 : {
  110692             121 :   sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
  110693                 : }
  110694             121 :         break;
  110695                 :       case 313: /* add_column_fullname ::= fullname */
  110696                 : {
  110697             139 :   pParse->db->lookaside.bEnabled = 0;
  110698             139 :   sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
  110699                 : }
  110700             139 :         break;
  110701                 :       case 316: /* cmd ::= create_vtab */
  110702             104 : {sqlite3VtabFinishParse(pParse,0);}
  110703             104 :         break;
  110704                 :       case 317: /* cmd ::= create_vtab LP vtabarglist RP */
  110705               2 : {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
  110706               2 :         break;
  110707                 :       case 318: /* create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm */
  110708                 : {
  110709             106 :     sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
  110710                 : }
  110711             106 :         break;
  110712                 :       case 321: /* vtabarg ::= */
  110713               4 : {sqlite3VtabArgInit(pParse);}
  110714               4 :         break;
  110715                 :       case 323: /* vtabargtoken ::= ANY */
  110716                 :       case 324: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==324);
  110717                 :       case 325: /* lp ::= LP */ yytestcase(yyruleno==325);
  110718               4 : {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
  110719               4 :         break;
  110720                 :       default:
  110721                 :       /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
  110722                 :       /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
  110723                 :       /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
  110724                 :       /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
  110725                 :       /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
  110726                 :       /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
  110727                 :       /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
  110728                 :       /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
  110729                 :       /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
  110730                 :       /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
  110731                 :       /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
  110732                 :       /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34);
  110733                 :       /* (35) columnlist ::= column */ yytestcase(yyruleno==35);
  110734                 :       /* (44) type ::= */ yytestcase(yyruleno==44);
  110735                 :       /* (51) signed ::= plus_num */ yytestcase(yyruleno==51);
  110736                 :       /* (52) signed ::= minus_num */ yytestcase(yyruleno==52);
  110737                 :       /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53);
  110738                 :       /* (54) carglist ::= */ yytestcase(yyruleno==54);
  110739                 :       /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55);
  110740                 :       /* (56) carg ::= ccons */ yytestcase(yyruleno==56);
  110741                 :       /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62);
  110742                 :       /* (90) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==90);
  110743                 :       /* (91) conslist ::= conslist tcons */ yytestcase(yyruleno==91);
  110744                 :       /* (92) conslist ::= tcons */ yytestcase(yyruleno==92);
  110745                 :       /* (93) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93);
  110746                 :       /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268);
  110747                 :       /* (269) plus_opt ::= */ yytestcase(yyruleno==269);
  110748                 :       /* (279) foreach_clause ::= */ yytestcase(yyruleno==279);
  110749                 :       /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280);
  110750                 :       /* (287) tridxby ::= */ yytestcase(yyruleno==287);
  110751                 :       /* (305) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==305);
  110752                 :       /* (306) database_kw_opt ::= */ yytestcase(yyruleno==306);
  110753                 :       /* (314) kwcolumn_opt ::= */ yytestcase(yyruleno==314);
  110754                 :       /* (315) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==315);
  110755                 :       /* (319) vtabarglist ::= vtabarg */ yytestcase(yyruleno==319);
  110756                 :       /* (320) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==320);
  110757                 :       /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322);
  110758                 :       /* (326) anylist ::= */ yytestcase(yyruleno==326);
  110759                 :       /* (327) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==327);
  110760                 :       /* (328) anylist ::= anylist ANY */ yytestcase(yyruleno==328);
  110761         1225265 :         break;
  110762                 :   };
  110763         9512138 :   yygoto = yyRuleInfo[yyruleno].lhs;
  110764         9512138 :   yysize = yyRuleInfo[yyruleno].nrhs;
  110765         9512138 :   yypParser->yyidx -= yysize;
  110766         9512138 :   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
  110767         9512127 :   if( yyact < YYNSTATE ){
  110768                 : #ifdef NDEBUG
  110769                 :     /* If we are not debugging and the reduce action popped at least
  110770                 :     ** one element off the stack, then we can push the new element back
  110771                 :     ** onto the stack here, and skip the stack overflow test in yy_shift().
  110772                 :     ** That gives a significant speed improvement. */
  110773                 :     if( yysize ){
  110774                 :       yypParser->yyidx++;
  110775                 :       yymsp -= yysize-1;
  110776                 :       yymsp->stateno = (YYACTIONTYPE)yyact;
  110777                 :       yymsp->major = (YYCODETYPE)yygoto;
  110778                 :       yymsp->minor = yygotominor;
  110779                 :     }else
  110780                 : #endif
  110781                 :     {
  110782         9324510 :       yy_shift(yypParser,yyact,yygoto,&yygotominor);
  110783                 :     }
  110784                 :   }else{
  110785          187617 :     assert( yyact == YYNSTATE + YYNRULE + 1 );
  110786          187617 :     yy_accept(yypParser);
  110787                 :   }
  110788         9512123 : }
  110789                 : 
  110790                 : /*
  110791                 : ** The following code executes when the parse fails
  110792                 : */
  110793                 : #ifndef YYNOERRORRECOVERY
  110794                 : static void yy_parse_failed(
  110795                 :   yyParser *yypParser           /* The parser */
  110796                 : ){
  110797                 :   sqlite3ParserARG_FETCH;
  110798                 : #ifndef NDEBUG
  110799                 :   if( yyTraceFILE ){
  110800                 :     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
  110801                 :   }
  110802                 : #endif
  110803                 :   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  110804                 :   /* Here code is inserted which will be executed whenever the
  110805                 :   ** parser fails */
  110806                 :   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
  110807                 : }
  110808                 : #endif /* YYNOERRORRECOVERY */
  110809                 : 
  110810                 : /*
  110811                 : ** The following code executes when a syntax error first occurs.
  110812                 : */
  110813               4 : static void yy_syntax_error(
  110814                 :   yyParser *yypParser,           /* The parser */
  110815                 :   int yymajor,                   /* The major type of the error token */
  110816                 :   YYMINORTYPE yyminor            /* The minor type of the error token */
  110817                 : ){
  110818               4 :   sqlite3ParserARG_FETCH;
  110819                 : #define TOKEN (yyminor.yy0)
  110820                 : 
  110821                 :   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
  110822               4 :   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
  110823               4 :   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
  110824               4 :   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
  110825               4 : }
  110826                 : 
  110827                 : /*
  110828                 : ** The following is executed when the parser accepts
  110829                 : */
  110830          187617 : static void yy_accept(
  110831                 :   yyParser *yypParser           /* The parser */
  110832                 : ){
  110833          187617 :   sqlite3ParserARG_FETCH;
  110834                 : #ifndef NDEBUG
  110835          187617 :   if( yyTraceFILE ){
  110836               0 :     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
  110837                 :   }
  110838                 : #endif
  110839          187617 :   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
  110840                 :   /* Here code is inserted which will be executed whenever the
  110841                 :   ** parser accepts */
  110842          187617 :   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
  110843          187617 : }
  110844                 : 
  110845                 : /* The main parser program.
  110846                 : ** The first argument is a pointer to a structure obtained from
  110847                 : ** "sqlite3ParserAlloc" which describes the current state of the parser.
  110848                 : ** The second argument is the major token number.  The third is
  110849                 : ** the minor token.  The fourth optional argument is whatever the
  110850                 : ** user wants (and specified in the grammar) and is available for
  110851                 : ** use by the action routines.
  110852                 : **
  110853                 : ** Inputs:
  110854                 : ** <ul>
  110855                 : ** <li> A pointer to the parser (an opaque structure.)
  110856                 : ** <li> The major token number.
  110857                 : ** <li> The minor token number.
  110858                 : ** <li> An option argument of a grammar-specified type.
  110859                 : ** </ul>
  110860                 : **
  110861                 : ** Outputs:
  110862                 : ** None.
  110863                 : */
  110864         4293783 : SQLITE_PRIVATE void sqlite3Parser(
  110865                 :   void *yyp,                   /* The parser */
  110866                 :   int yymajor,                 /* The major token code number */
  110867                 :   sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
  110868                 :   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
  110869                 : ){
  110870                 :   YYMINORTYPE yyminorunion;
  110871                 :   int yyact;            /* The parser action. */
  110872                 : #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
  110873                 :   int yyendofinput;     /* True if we are at the end of input */
  110874                 : #endif
  110875                 : #ifdef YYERRORSYMBOL
  110876                 :   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
  110877                 : #endif
  110878                 :   yyParser *yypParser;  /* The parser */
  110879                 : 
  110880                 :   /* (re)initialize the parser, if necessary */
  110881         4293783 :   yypParser = (yyParser*)yyp;
  110882         4293783 :   if( yypParser->yyidx<0 ){
  110883                 : #if YYSTACKDEPTH<=0
  110884                 :     if( yypParser->yystksz <=0 ){
  110885                 :       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
  110886                 :       yyminorunion = yyzerominor;
  110887                 :       yyStackOverflow(yypParser, &yyminorunion);
  110888                 :       return;
  110889                 :     }
  110890                 : #endif
  110891          193859 :     yypParser->yyidx = 0;
  110892          193859 :     yypParser->yyerrcnt = -1;
  110893          193859 :     yypParser->yystack[0].stateno = 0;
  110894          193859 :     yypParser->yystack[0].major = 0;
  110895                 :   }
  110896         4293783 :   yyminorunion.yy0 = yyminor;
  110897                 : #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
  110898                 :   yyendofinput = (yymajor==0);
  110899                 : #endif
  110900         4293783 :   sqlite3ParserARG_STORE;
  110901                 : 
  110902                 : #ifndef NDEBUG
  110903         4293783 :   if( yyTraceFILE ){
  110904               0 :     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
  110905                 :   }
  110906                 : #endif
  110907                 : 
  110908                 :   do{
  110909        13618291 :     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
  110910        13618283 :     if( yyact<YYNSTATE ){
  110911         4106163 :       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
  110912         4106161 :       yypParser->yyerrcnt--;
  110913         4106161 :       yymajor = YYNOCODE;
  110914         9512120 :     }else if( yyact < YYNSTATE + YYNRULE ){
  110915         9512116 :       yy_reduce(yypParser,yyact-YYNSTATE);
  110916                 :     }else{
  110917               4 :       assert( yyact == YY_ERROR_ACTION );
  110918                 : #ifdef YYERRORSYMBOL
  110919                 :       int yymx;
  110920                 : #endif
  110921                 : #ifndef NDEBUG
  110922               4 :       if( yyTraceFILE ){
  110923               0 :         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
  110924                 :       }
  110925                 : #endif
  110926                 : #ifdef YYERRORSYMBOL
  110927                 :       /* A syntax error has occurred.
  110928                 :       ** The response to an error depends upon whether or not the
  110929                 :       ** grammar defines an error token "ERROR".  
  110930                 :       **
  110931                 :       ** This is what we do if the grammar does define ERROR:
  110932                 :       **
  110933                 :       **  * Call the %syntax_error function.
  110934                 :       **
  110935                 :       **  * Begin popping the stack until we enter a state where
  110936                 :       **    it is legal to shift the error symbol, then shift
  110937                 :       **    the error symbol.
  110938                 :       **
  110939                 :       **  * Set the error count to three.
  110940                 :       **
  110941                 :       **  * Begin accepting and shifting new tokens.  No new error
  110942                 :       **    processing will occur until three tokens have been
  110943                 :       **    shifted successfully.
  110944                 :       **
  110945                 :       */
  110946                 :       if( yypParser->yyerrcnt<0 ){
  110947                 :         yy_syntax_error(yypParser,yymajor,yyminorunion);
  110948                 :       }
  110949                 :       yymx = yypParser->yystack[yypParser->yyidx].major;
  110950                 :       if( yymx==YYERRORSYMBOL || yyerrorhit ){
  110951                 : #ifndef NDEBUG
  110952                 :         if( yyTraceFILE ){
  110953                 :           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
  110954                 :              yyTracePrompt,yyTokenName[yymajor]);
  110955                 :         }
  110956                 : #endif
  110957                 :         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
  110958                 :         yymajor = YYNOCODE;
  110959                 :       }else{
  110960                 :          while(
  110961                 :           yypParser->yyidx >= 0 &&
  110962                 :           yymx != YYERRORSYMBOL &&
  110963                 :           (yyact = yy_find_reduce_action(
  110964                 :                         yypParser->yystack[yypParser->yyidx].stateno,
  110965                 :                         YYERRORSYMBOL)) >= YYNSTATE
  110966                 :         ){
  110967                 :           yy_pop_parser_stack(yypParser);
  110968                 :         }
  110969                 :         if( yypParser->yyidx < 0 || yymajor==0 ){
  110970                 :           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  110971                 :           yy_parse_failed(yypParser);
  110972                 :           yymajor = YYNOCODE;
  110973                 :         }else if( yymx!=YYERRORSYMBOL ){
  110974                 :           YYMINORTYPE u2;
  110975                 :           u2.YYERRSYMDT = 0;
  110976                 :           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
  110977                 :         }
  110978                 :       }
  110979                 :       yypParser->yyerrcnt = 3;
  110980                 :       yyerrorhit = 1;
  110981                 : #elif defined(YYNOERRORRECOVERY)
  110982                 :       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
  110983                 :       ** do any kind of error recovery.  Instead, simply invoke the syntax
  110984                 :       ** error routine and continue going as if nothing had happened.
  110985                 :       **
  110986                 :       ** Applications can set this macro (for example inside %include) if
  110987                 :       ** they intend to abandon the parse upon the first syntax error seen.
  110988                 :       */
  110989               4 :       yy_syntax_error(yypParser,yymajor,yyminorunion);
  110990               4 :       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  110991               4 :       yymajor = YYNOCODE;
  110992                 :       
  110993                 : #else  /* YYERRORSYMBOL is not defined */
  110994                 :       /* This is what we do if the grammar does not define ERROR:
  110995                 :       **
  110996                 :       **  * Report an error message, and throw away the input token.
  110997                 :       **
  110998                 :       **  * If the input token is $, then fail the parse.
  110999                 :       **
  111000                 :       ** As before, subsequent error messages are suppressed until
  111001                 :       ** three input tokens have been successfully shifted.
  111002                 :       */
  111003                 :       if( yypParser->yyerrcnt<=0 ){
  111004                 :         yy_syntax_error(yypParser,yymajor,yyminorunion);
  111005                 :       }
  111006                 :       yypParser->yyerrcnt = 3;
  111007                 :       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
  111008                 :       if( yyendofinput ){
  111009                 :         yy_parse_failed(yypParser);
  111010                 :       }
  111011                 :       yymajor = YYNOCODE;
  111012                 : #endif
  111013                 :     }
  111014        13618290 :   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
  111015                 :   return;
  111016                 : }
  111017                 : 
  111018                 : /************** End of parse.c ***********************************************/
  111019                 : /************** Begin file tokenize.c ****************************************/
  111020                 : /*
  111021                 : ** 2001 September 15
  111022                 : **
  111023                 : ** The author disclaims copyright to this source code.  In place of
  111024                 : ** a legal notice, here is a blessing:
  111025                 : **
  111026                 : **    May you do good and not evil.
  111027                 : **    May you find forgiveness for yourself and forgive others.
  111028                 : **    May you share freely, never taking more than you give.
  111029                 : **
  111030                 : *************************************************************************
  111031                 : ** An tokenizer for SQL
  111032                 : **
  111033                 : ** This file contains C code that splits an SQL input string up into
  111034                 : ** individual tokens and sends those tokens one-by-one over to the
  111035                 : ** parser for analysis.
  111036                 : */
  111037                 : /* #include <stdlib.h> */
  111038                 : 
  111039                 : /*
  111040                 : ** The charMap() macro maps alphabetic characters into their
  111041                 : ** lower-case ASCII equivalent.  On ASCII machines, this is just
  111042                 : ** an upper-to-lower case map.  On EBCDIC machines we also need
  111043                 : ** to adjust the encoding.  Only alphabetic characters and underscores
  111044                 : ** need to be translated.
  111045                 : */
  111046                 : #ifdef SQLITE_ASCII
  111047                 : # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
  111048                 : #endif
  111049                 : #ifdef SQLITE_EBCDIC
  111050                 : # define charMap(X) ebcdicToAscii[(unsigned char)X]
  111051                 : const unsigned char ebcdicToAscii[] = {
  111052                 : /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
  111053                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
  111054                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
  111055                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
  111056                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
  111057                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
  111058                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
  111059                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
  111060                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
  111061                 :    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
  111062                 :    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
  111063                 :    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
  111064                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
  111065                 :    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
  111066                 :    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
  111067                 :    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
  111068                 :    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
  111069                 : };
  111070                 : #endif
  111071                 : 
  111072                 : /*
  111073                 : ** The sqlite3KeywordCode function looks up an identifier to determine if
  111074                 : ** it is a keyword.  If it is a keyword, the token code of that keyword is 
  111075                 : ** returned.  If the input is not a keyword, TK_ID is returned.
  111076                 : **
  111077                 : ** The implementation of this routine was generated by a program,
  111078                 : ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
  111079                 : ** The output of the mkkeywordhash.c program is written into a file
  111080                 : ** named keywordhash.h and then included into this source file by
  111081                 : ** the #include below.
  111082                 : */
  111083                 : /************** Include keywordhash.h in the middle of tokenize.c ************/
  111084                 : /************** Begin file keywordhash.h *************************************/
  111085                 : /***** This file contains automatically generated code ******
  111086                 : **
  111087                 : ** The code in this file has been automatically generated by
  111088                 : **
  111089                 : **   sqlite/tool/mkkeywordhash.c
  111090                 : **
  111091                 : ** The code in this file implements a function that determines whether
  111092                 : ** or not a given identifier is really an SQL keyword.  The same thing
  111093                 : ** might be implemented more directly using a hand-written hash table.
  111094                 : ** But by using this automatically generated code, the size of the code
  111095                 : ** is substantially reduced.  This is important for embedded applications
  111096                 : ** on platforms with limited memory.
  111097                 : */
  111098                 : /* Hash score: 175 */
  111099         4694463 : static int keywordCode(const char *z, int n){
  111100                 :   /* zText[] encodes 811 bytes of keywords in 541 bytes */
  111101                 :   /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
  111102                 :   /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
  111103                 :   /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
  111104                 :   /*   UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE          */
  111105                 :   /*   CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN        */
  111106                 :   /*   SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME         */
  111107                 :   /*   AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS     */
  111108                 :   /*   CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF      */
  111109                 :   /*   ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW         */
  111110                 :   /*   INITIALLY                                                          */
  111111                 :   static const char zText[540] = {
  111112                 :     'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
  111113                 :     'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
  111114                 :     'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
  111115                 :     'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
  111116                 :     'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
  111117                 :     'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
  111118                 :     'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
  111119                 :     'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
  111120                 :     'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
  111121                 :     'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
  111122                 :     'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U',
  111123                 :     'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S',
  111124                 :     'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C',
  111125                 :     'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L',
  111126                 :     'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D',
  111127                 :     'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E',
  111128                 :     'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A',
  111129                 :     'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U',
  111130                 :     'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W',
  111131                 :     'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C',
  111132                 :     'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R',
  111133                 :     'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M',
  111134                 :     'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U',
  111135                 :     'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M',
  111136                 :     'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T',
  111137                 :     'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L',
  111138                 :     'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S',
  111139                 :     'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L',
  111140                 :     'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V',
  111141                 :     'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y',
  111142                 :   };
  111143                 :   static const unsigned char aHash[127] = {
  111144                 :       72, 101, 114,  70,   0,  45,   0,   0,  78,   0,  73,   0,   0,
  111145                 :       42,  12,  74,  15,   0, 113,  81,  50, 108,   0,  19,   0,   0,
  111146                 :      118,   0, 116, 111,   0,  22,  89,   0,   9,   0,   0,  66,  67,
  111147                 :        0,  65,   6,   0,  48,  86,  98,   0, 115,  97,   0,   0,  44,
  111148                 :        0,  99,  24,   0,  17,   0, 119,  49,  23,   0,   5, 106,  25,
  111149                 :       92,   0,   0, 121, 102,  56, 120,  53,  28,  51,   0,  87,   0,
  111150                 :       96,  26,   0,  95,   0,   0,   0,  91,  88,  93,  84, 105,  14,
  111151                 :       39, 104,   0,  77,   0,  18,  85, 107,  32,   0, 117,  76, 109,
  111152                 :       58,  46,  80,   0,   0,  90,  40,   0, 112,   0,  36,   0,   0,
  111153                 :       29,   0,  82,  59,  60,   0,  20,  57,   0,  52,
  111154                 :   };
  111155                 :   static const unsigned char aNext[121] = {
  111156                 :        0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
  111157                 :        0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
  111158                 :        0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  111159                 :        0,   0,   0,   0,  33,   0,  21,   0,   0,   0,  43,   3,  47,
  111160                 :        0,   0,   0,   0,  30,   0,  54,   0,  38,   0,   0,   0,   1,
  111161                 :       62,   0,   0,  63,   0,  41,   0,   0,   0,   0,   0,   0,   0,
  111162                 :       61,   0,   0,   0,   0,  31,  55,  16,  34,  10,   0,   0,   0,
  111163                 :        0,   0,   0,   0,  11,  68,  75,   0,   8,   0, 100,  94,   0,
  111164                 :      103,   0,  83,   0,  71,   0,   0, 110,  27,  37,  69,  79,   0,
  111165                 :       35,  64,   0,   0,
  111166                 :   };
  111167                 :   static const unsigned char aLen[121] = {
  111168                 :        7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
  111169                 :        7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
  111170                 :       11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
  111171                 :        4,   6,   2,   3,   9,   4,   2,   6,   5,   6,   6,   5,   6,
  111172                 :        5,   5,   7,   7,   7,   3,   2,   4,   4,   7,   3,   6,   4,
  111173                 :        7,   6,  12,   6,   9,   4,   6,   5,   4,   7,   6,   5,   6,
  111174                 :        7,   5,   4,   5,   6,   5,   7,   3,   7,  13,   2,   2,   4,
  111175                 :        6,   6,   8,   5,  17,  12,   7,   8,   8,   2,   4,   4,   4,
  111176                 :        4,   4,   2,   2,   6,   5,   8,   5,   5,   8,   3,   5,   5,
  111177                 :        6,   4,   9,   3,
  111178                 :   };
  111179                 :   static const unsigned short int aOffset[121] = {
  111180                 :        0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
  111181                 :       36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
  111182                 :       86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
  111183                 :      159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197,
  111184                 :      203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244,
  111185                 :      248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320,
  111186                 :      326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383,
  111187                 :      387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458,
  111188                 :      462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516,
  111189                 :      521, 527, 531, 536,
  111190                 :   };
  111191                 :   static const unsigned char aCode[121] = {
  111192                 :     TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,     
  111193                 :     TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,    
  111194                 :     TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,    
  111195                 :     TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,      
  111196                 :     TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,       
  111197                 :     TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,    
  111198                 :     TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,  
  111199                 :     TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,       
  111200                 :     TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,       
  111201                 :     TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_ATTACH,     TK_HAVING,     
  111202                 :     TK_GROUP,      TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RELEASE,    
  111203                 :     TK_BETWEEN,    TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       
  111204                 :     TK_LIKE_KW,    TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       
  111205                 :     TK_COLLATE,    TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  
  111206                 :     TK_JOIN,       TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    
  111207                 :     TK_PRAGMA,     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      
  111208                 :     TK_WHEN,       TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    
  111209                 :     TK_AND,        TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         
  111210                 :     TK_CAST,       TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    
  111211                 :     TK_CTIME_KW,   TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   
  111212                 :     TK_IS,         TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    
  111213                 :     TK_LIKE_KW,    TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      
  111214                 :     TK_RESTRICT,   TK_JOIN_KW,    TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        
  111215                 :     TK_UNION,      TK_USING,      TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  
  111216                 :     TK_ALL,        
  111217                 :   };
  111218                 :   int h, i;
  111219         4694463 :   if( n<2 ) return TK_ID;
  111220        13406874 :   h = ((charMap(z[0])*4) ^
  111221         8937916 :       (charMap(z[n-1])*3) ^
  111222                 :       n) % 127;
  111223         7376120 :   for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
  111224         4708618 :     if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
  111225                 :       testcase( i==0 ); /* REINDEX */
  111226                 :       testcase( i==1 ); /* INDEXED */
  111227                 :       testcase( i==2 ); /* INDEX */
  111228                 :       testcase( i==3 ); /* DESC */
  111229                 :       testcase( i==4 ); /* ESCAPE */
  111230                 :       testcase( i==5 ); /* EACH */
  111231                 :       testcase( i==6 ); /* CHECK */
  111232                 :       testcase( i==7 ); /* KEY */
  111233                 :       testcase( i==8 ); /* BEFORE */
  111234                 :       testcase( i==9 ); /* FOREIGN */
  111235                 :       testcase( i==10 ); /* FOR */
  111236                 :       testcase( i==11 ); /* IGNORE */
  111237                 :       testcase( i==12 ); /* REGEXP */
  111238                 :       testcase( i==13 ); /* EXPLAIN */
  111239                 :       testcase( i==14 ); /* INSTEAD */
  111240                 :       testcase( i==15 ); /* ADD */
  111241                 :       testcase( i==16 ); /* DATABASE */
  111242                 :       testcase( i==17 ); /* AS */
  111243                 :       testcase( i==18 ); /* SELECT */
  111244                 :       testcase( i==19 ); /* TABLE */
  111245                 :       testcase( i==20 ); /* LEFT */
  111246                 :       testcase( i==21 ); /* THEN */
  111247                 :       testcase( i==22 ); /* END */
  111248                 :       testcase( i==23 ); /* DEFERRABLE */
  111249                 :       testcase( i==24 ); /* ELSE */
  111250                 :       testcase( i==25 ); /* EXCEPT */
  111251                 :       testcase( i==26 ); /* TRANSACTION */
  111252                 :       testcase( i==27 ); /* ACTION */
  111253                 :       testcase( i==28 ); /* ON */
  111254                 :       testcase( i==29 ); /* NATURAL */
  111255                 :       testcase( i==30 ); /* ALTER */
  111256                 :       testcase( i==31 ); /* RAISE */
  111257                 :       testcase( i==32 ); /* EXCLUSIVE */
  111258                 :       testcase( i==33 ); /* EXISTS */
  111259                 :       testcase( i==34 ); /* SAVEPOINT */
  111260                 :       testcase( i==35 ); /* INTERSECT */
  111261                 :       testcase( i==36 ); /* TRIGGER */
  111262                 :       testcase( i==37 ); /* REFERENCES */
  111263                 :       testcase( i==38 ); /* CONSTRAINT */
  111264                 :       testcase( i==39 ); /* INTO */
  111265                 :       testcase( i==40 ); /* OFFSET */
  111266                 :       testcase( i==41 ); /* OF */
  111267                 :       testcase( i==42 ); /* SET */
  111268                 :       testcase( i==43 ); /* TEMPORARY */
  111269                 :       testcase( i==44 ); /* TEMP */
  111270                 :       testcase( i==45 ); /* OR */
  111271                 :       testcase( i==46 ); /* UNIQUE */
  111272                 :       testcase( i==47 ); /* QUERY */
  111273                 :       testcase( i==48 ); /* ATTACH */
  111274                 :       testcase( i==49 ); /* HAVING */
  111275                 :       testcase( i==50 ); /* GROUP */
  111276                 :       testcase( i==51 ); /* UPDATE */
  111277                 :       testcase( i==52 ); /* BEGIN */
  111278                 :       testcase( i==53 ); /* INNER */
  111279                 :       testcase( i==54 ); /* RELEASE */
  111280                 :       testcase( i==55 ); /* BETWEEN */
  111281                 :       testcase( i==56 ); /* NOTNULL */
  111282                 :       testcase( i==57 ); /* NOT */
  111283                 :       testcase( i==58 ); /* NO */
  111284                 :       testcase( i==59 ); /* NULL */
  111285                 :       testcase( i==60 ); /* LIKE */
  111286                 :       testcase( i==61 ); /* CASCADE */
  111287                 :       testcase( i==62 ); /* ASC */
  111288                 :       testcase( i==63 ); /* DELETE */
  111289                 :       testcase( i==64 ); /* CASE */
  111290                 :       testcase( i==65 ); /* COLLATE */
  111291                 :       testcase( i==66 ); /* CREATE */
  111292                 :       testcase( i==67 ); /* CURRENT_DATE */
  111293                 :       testcase( i==68 ); /* DETACH */
  111294                 :       testcase( i==69 ); /* IMMEDIATE */
  111295                 :       testcase( i==70 ); /* JOIN */
  111296                 :       testcase( i==71 ); /* INSERT */
  111297                 :       testcase( i==72 ); /* MATCH */
  111298                 :       testcase( i==73 ); /* PLAN */
  111299                 :       testcase( i==74 ); /* ANALYZE */
  111300                 :       testcase( i==75 ); /* PRAGMA */
  111301                 :       testcase( i==76 ); /* ABORT */
  111302                 :       testcase( i==77 ); /* VALUES */
  111303                 :       testcase( i==78 ); /* VIRTUAL */
  111304                 :       testcase( i==79 ); /* LIMIT */
  111305                 :       testcase( i==80 ); /* WHEN */
  111306                 :       testcase( i==81 ); /* WHERE */
  111307                 :       testcase( i==82 ); /* RENAME */
  111308                 :       testcase( i==83 ); /* AFTER */
  111309                 :       testcase( i==84 ); /* REPLACE */
  111310                 :       testcase( i==85 ); /* AND */
  111311                 :       testcase( i==86 ); /* DEFAULT */
  111312                 :       testcase( i==87 ); /* AUTOINCREMENT */
  111313                 :       testcase( i==88 ); /* TO */
  111314                 :       testcase( i==89 ); /* IN */
  111315                 :       testcase( i==90 ); /* CAST */
  111316                 :       testcase( i==91 ); /* COLUMN */
  111317                 :       testcase( i==92 ); /* COMMIT */
  111318                 :       testcase( i==93 ); /* CONFLICT */
  111319                 :       testcase( i==94 ); /* CROSS */
  111320                 :       testcase( i==95 ); /* CURRENT_TIMESTAMP */
  111321                 :       testcase( i==96 ); /* CURRENT_TIME */
  111322                 :       testcase( i==97 ); /* PRIMARY */
  111323                 :       testcase( i==98 ); /* DEFERRED */
  111324                 :       testcase( i==99 ); /* DISTINCT */
  111325                 :       testcase( i==100 ); /* IS */
  111326                 :       testcase( i==101 ); /* DROP */
  111327                 :       testcase( i==102 ); /* FAIL */
  111328                 :       testcase( i==103 ); /* FROM */
  111329                 :       testcase( i==104 ); /* FULL */
  111330                 :       testcase( i==105 ); /* GLOB */
  111331                 :       testcase( i==106 ); /* BY */
  111332                 :       testcase( i==107 ); /* IF */
  111333                 :       testcase( i==108 ); /* ISNULL */
  111334                 :       testcase( i==109 ); /* ORDER */
  111335                 :       testcase( i==110 ); /* RESTRICT */
  111336                 :       testcase( i==111 ); /* OUTER */
  111337                 :       testcase( i==112 ); /* RIGHT */
  111338                 :       testcase( i==113 ); /* ROLLBACK */
  111339                 :       testcase( i==114 ); /* ROW */
  111340                 :       testcase( i==115 ); /* UNION */
  111341                 :       testcase( i==116 ); /* USING */
  111342                 :       testcase( i==117 ); /* VACUUM */
  111343                 :       testcase( i==118 ); /* VIEW */
  111344                 :       testcase( i==119 ); /* INITIALLY */
  111345                 :       testcase( i==120 ); /* ALL */
  111346         1801456 :       return aCode[i];
  111347                 :     }
  111348                 :   }
  111349         2667502 :   return TK_ID;
  111350                 : }
  111351               0 : SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
  111352               0 :   return keywordCode((char*)z, n);
  111353                 : }
  111354                 : #define SQLITE_N_KEYWORD 121
  111355                 : 
  111356                 : /************** End of keywordhash.h *****************************************/
  111357                 : /************** Continuing where we left off in tokenize.c *******************/
  111358                 : 
  111359                 : 
  111360                 : /*
  111361                 : ** If X is a character that can be used in an identifier then
  111362                 : ** IdChar(X) will be true.  Otherwise it is false.
  111363                 : **
  111364                 : ** For ASCII, any character with the high-order bit set is
  111365                 : ** allowed in an identifier.  For 7-bit characters, 
  111366                 : ** sqlite3IsIdChar[X] must be 1.
  111367                 : **
  111368                 : ** For EBCDIC, the rules are more complex but have the same
  111369                 : ** end result.
  111370                 : **
  111371                 : ** Ticket #1066.  the SQL standard does not allow '$' in the
  111372                 : ** middle of identfiers.  But many SQL implementations do. 
  111373                 : ** SQLite will allow '$' in identifiers for compatibility.
  111374                 : ** But the feature is undocumented.
  111375                 : */
  111376                 : #ifdef SQLITE_ASCII
  111377                 : #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
  111378                 : #endif
  111379                 : #ifdef SQLITE_EBCDIC
  111380                 : SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
  111381                 : /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
  111382                 :     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
  111383                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
  111384                 :     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
  111385                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
  111386                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
  111387                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
  111388                 :     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
  111389                 :     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
  111390                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
  111391                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
  111392                 :     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
  111393                 :     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
  111394                 : };
  111395                 : #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
  111396                 : #endif
  111397                 : 
  111398                 : 
  111399                 : /*
  111400                 : ** Return the length of the token that begins at z[0]. 
  111401                 : ** Store the token type in *tokenType before returning.
  111402                 : */
  111403        13388593 : SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
  111404                 :   int i, c;
  111405        13388593 :   switch( *z ){
  111406                 :     case ' ': case '\t': case '\n': case '\f': case '\r': {
  111407                 :       testcase( z[0]==' ' );
  111408                 :       testcase( z[0]=='\t' );
  111409                 :       testcase( z[0]=='\n' );
  111410                 :       testcase( z[0]=='\f' );
  111411                 :       testcase( z[0]=='\r' );
  111412         4855834 :       for(i=1; sqlite3Isspace(z[i]); i++){}
  111413         4855834 :       *tokenType = TK_SPACE;
  111414         4855834 :       return i;
  111415                 :     }
  111416                 :     case '-': {
  111417           30006 :       if( z[1]=='-' ){
  111418                 :         /* IMP: R-50417-27976 -- syntax diagram for comments */
  111419           19051 :         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
  111420           19051 :         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
  111421           19051 :         return i;
  111422                 :       }
  111423           10955 :       *tokenType = TK_MINUS;
  111424           10955 :       return 1;
  111425                 :     }
  111426                 :     case '(': {
  111427          329656 :       *tokenType = TK_LP;
  111428          329656 :       return 1;
  111429                 :     }
  111430                 :     case ')': {
  111431          329655 :       *tokenType = TK_RP;
  111432          329655 :       return 1;
  111433                 :     }
  111434                 :     case ';': {
  111435           35817 :       *tokenType = TK_SEMI;
  111436           35817 :       return 1;
  111437                 :     }
  111438                 :     case '+': {
  111439            3621 :       *tokenType = TK_PLUS;
  111440            3621 :       return 1;
  111441                 :     }
  111442                 :     case '*': {
  111443           12587 :       *tokenType = TK_STAR;
  111444           12587 :       return 1;
  111445                 :     }
  111446                 :     case '/': {
  111447           14904 :       if( z[1]!='*' || z[2]==0 ){
  111448            2038 :         *tokenType = TK_SLASH;
  111449            2038 :         return 1;
  111450                 :       }
  111451                 :       /* IMP: R-50417-27976 -- syntax diagram for comments */
  111452           12866 :       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
  111453           12866 :       if( c ) i++;
  111454           12866 :       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
  111455           12866 :       return i;
  111456                 :     }
  111457                 :     case '%': {
  111458               0 :       *tokenType = TK_REM;
  111459               0 :       return 1;
  111460                 :     }
  111461                 :     case '=': {
  111462          337144 :       *tokenType = TK_EQ;
  111463          337144 :       return 1 + (z[1]=='=');
  111464                 :     }
  111465                 :     case '<': {
  111466           10359 :       if( (c=z[1])=='=' ){
  111467             655 :         *tokenType = TK_LE;
  111468             655 :         return 2;
  111469            9704 :       }else if( c=='>' ){
  111470            8011 :         *tokenType = TK_NE;
  111471            8011 :         return 2;
  111472            1693 :       }else if( c=='<' ){
  111473               0 :         *tokenType = TK_LSHIFT;
  111474               0 :         return 2;
  111475                 :       }else{
  111476            1693 :         *tokenType = TK_LT;
  111477            1693 :         return 1;
  111478                 :       }
  111479                 :     }
  111480                 :     case '>': {
  111481           10432 :       if( (c=z[1])=='=' ){
  111482            2768 :         *tokenType = TK_GE;
  111483            2768 :         return 2;
  111484            7664 :       }else if( c=='>' ){
  111485               0 :         *tokenType = TK_RSHIFT;
  111486               0 :         return 2;
  111487                 :       }else{
  111488            7664 :         *tokenType = TK_GT;
  111489            7664 :         return 1;
  111490                 :       }
  111491                 :     }
  111492                 :     case '!': {
  111493             193 :       if( z[1]!='=' ){
  111494               0 :         *tokenType = TK_ILLEGAL;
  111495               0 :         return 2;
  111496                 :       }else{
  111497             193 :         *tokenType = TK_NE;
  111498             193 :         return 2;
  111499                 :       }
  111500                 :     }
  111501                 :     case '|': {
  111502           10610 :       if( z[1]!='|' ){
  111503              39 :         *tokenType = TK_BITOR;
  111504              39 :         return 1;
  111505                 :       }else{
  111506           10571 :         *tokenType = TK_CONCAT;
  111507           10571 :         return 2;
  111508                 :       }
  111509                 :     }
  111510                 :     case ',': {
  111511         1558566 :       *tokenType = TK_COMMA;
  111512         1558566 :       return 1;
  111513                 :     }
  111514                 :     case '&': {
  111515              90 :       *tokenType = TK_BITAND;
  111516              90 :       return 1;
  111517                 :     }
  111518                 :     case '~': {
  111519              45 :       *tokenType = TK_BITNOT;
  111520              45 :       return 1;
  111521                 :     }
  111522                 :     case '`':
  111523                 :     case '\'':
  111524                 :     case '"': {
  111525          184651 :       int delim = z[0];
  111526                 :       testcase( delim=='`' );
  111527                 :       testcase( delim=='\'' );
  111528                 :       testcase( delim=='"' );
  111529         4185176 :       for(i=1; (c=z[i])!=0; i++){
  111530         4185176 :         if( c==delim ){
  111531          188907 :           if( z[i+1]==delim ){
  111532            4256 :             i++;
  111533                 :           }else{
  111534          184651 :             break;
  111535                 :           }
  111536                 :         }
  111537                 :       }
  111538          184651 :       if( c=='\'' ){
  111539          184509 :         *tokenType = TK_STRING;
  111540          184509 :         return i+1;
  111541             142 :       }else if( c!=0 ){
  111542             142 :         *tokenType = TK_ID;
  111543             142 :         return i+1;
  111544                 :       }else{
  111545               0 :         *tokenType = TK_ILLEGAL;
  111546               0 :         return i;
  111547                 :       }
  111548                 :     }
  111549                 :     case '.': {
  111550                 : #ifndef SQLITE_OMIT_FLOATING_POINT
  111551          311787 :       if( !sqlite3Isdigit(z[1]) )
  111552                 : #endif
  111553                 :       {
  111554          310435 :         *tokenType = TK_DOT;
  111555          310435 :         return 1;
  111556                 :       }
  111557                 :       /* If the next character is a digit, this is a floating point
  111558                 :       ** number that begins with ".".  Fall thru into the next case */
  111559                 :     }
  111560                 :     case '0': case '1': case '2': case '3': case '4':
  111561                 :     case '5': case '6': case '7': case '8': case '9': {
  111562                 :       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
  111563                 :       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
  111564                 :       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
  111565                 :       testcase( z[0]=='9' );
  111566          120679 :       *tokenType = TK_INTEGER;
  111567          120679 :       for(i=0; sqlite3Isdigit(z[i]); i++){}
  111568                 : #ifndef SQLITE_OMIT_FLOATING_POINT
  111569          120679 :       if( z[i]=='.' ){
  111570            1408 :         i++;
  111571            1408 :         while( sqlite3Isdigit(z[i]) ){ i++; }
  111572            1408 :         *tokenType = TK_FLOAT;
  111573                 :       }
  111574          120679 :       if( (z[i]=='e' || z[i]=='E') &&
  111575               0 :            ( sqlite3Isdigit(z[i+1]) 
  111576               0 :             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
  111577                 :            )
  111578                 :       ){
  111579               0 :         i += 2;
  111580               0 :         while( sqlite3Isdigit(z[i]) ){ i++; }
  111581               0 :         *tokenType = TK_FLOAT;
  111582                 :       }
  111583                 : #endif
  111584          241358 :       while( IdChar(z[i]) ){
  111585               0 :         *tokenType = TK_ILLEGAL;
  111586               0 :         i++;
  111587                 :       }
  111588          120679 :       return i;
  111589                 :     }
  111590                 :     case '[': {
  111591               0 :       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
  111592               0 :       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
  111593               0 :       return i;
  111594                 :     }
  111595                 :     case '?': {
  111596           21116 :       *tokenType = TK_VARIABLE;
  111597           21116 :       for(i=1; sqlite3Isdigit(z[i]); i++){}
  111598           21116 :       return i;
  111599                 :     }
  111600                 :     case '#': {
  111601           23379 :       for(i=1; sqlite3Isdigit(z[i]); i++){}
  111602           23379 :       if( i>1 ){
  111603                 :         /* Parameters of the form #NNN (where NNN is a number) are used
  111604                 :         ** internally by sqlite3NestedParse.  */
  111605           23379 :         *tokenType = TK_REGISTER;
  111606           23379 :         return i;
  111607                 :       }
  111608                 :       /* Fall through into the next case if the '#' is not followed by
  111609                 :       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
  111610                 :     }
  111611                 : #ifndef SQLITE_OMIT_TCL_VARIABLE
  111612                 :     case '$':
  111613                 : #endif
  111614                 :     case '@':  /* For compatibility with MS SQL Server */
  111615                 :     case ':': {
  111616          493948 :       int n = 0;
  111617                 :       testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
  111618          493948 :       *tokenType = TK_VARIABLE;
  111619         4528347 :       for(i=1; (c=z[i])!=0; i++){
  111620         4471036 :         if( IdChar(c) ){
  111621         4034399 :           n++;
  111622                 : #ifndef SQLITE_OMIT_TCL_VARIABLE
  111623          436637 :         }else if( c=='(' && n>0 ){
  111624                 :           do{
  111625               0 :             i++;
  111626               0 :           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
  111627               0 :           if( c==')' ){
  111628               0 :             i++;
  111629                 :           }else{
  111630               0 :             *tokenType = TK_ILLEGAL;
  111631                 :           }
  111632               0 :           break;
  111633          436637 :         }else if( c==':' && z[i+1]==':' ){
  111634               0 :           i++;
  111635                 : #endif
  111636                 :         }else{
  111637                 :           break;
  111638                 :         }
  111639                 :       }
  111640          493948 :       if( n==0 ) *tokenType = TK_ILLEGAL;
  111641          493948 :       return i;
  111642                 :     }
  111643                 : #ifndef SQLITE_OMIT_BLOB_LITERAL
  111644                 :     case 'x': case 'X': {
  111645                 :       testcase( z[0]=='x' ); testcase( z[0]=='X' );
  111646             503 :       if( z[1]=='\'' ){
  111647             403 :         *tokenType = TK_BLOB;
  111648             403 :         for(i=2; sqlite3Isxdigit(z[i]); i++){}
  111649             403 :         if( z[i]!='\'' || i%2 ){
  111650               0 :           *tokenType = TK_ILLEGAL;
  111651               0 :           while( z[i] && z[i]!='\'' ){ i++; }
  111652                 :         }
  111653             403 :         if( z[i] ) i++;
  111654             403 :         return i;
  111655                 :       }
  111656                 :       /* Otherwise fall through to the next case */
  111657                 :     }
  111658                 : #endif
  111659                 :     default: {
  111660         4694463 :       if( !IdChar(*z) ){
  111661                 :         break;
  111662                 :       }
  111663         4694463 :       for(i=1; IdChar(z[i]); i++){}
  111664         4694463 :       *tokenType = keywordCode((char*)z, i);
  111665         4694462 :       return i;
  111666                 :     }
  111667                 :   }
  111668               0 :   *tokenType = TK_ILLEGAL;
  111669               0 :   return 1;
  111670                 : }
  111671                 : 
  111672                 : /*
  111673                 : ** Run the parser on the given SQL string.  The parser structure is
  111674                 : ** passed in.  An SQLITE_ status code is returned.  If an error occurs
  111675                 : ** then an and attempt is made to write an error message into 
  111676                 : ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
  111677                 : ** error message.
  111678                 : */
  111679          193859 : SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
  111680          193859 :   int nErr = 0;                   /* Number of errors encountered */
  111681                 :   int i;                          /* Loop counter */
  111682                 :   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
  111683                 :   int tokenType;                  /* type of the next token */
  111684          193859 :   int lastTokenParsed = -1;       /* type of the previous token */
  111685                 :   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
  111686          193859 :   sqlite3 *db = pParse->db;       /* The database connection */
  111687                 :   int mxSqlLen;                   /* Max length of an SQL string */
  111688                 : 
  111689                 : 
  111690          193859 :   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
  111691          193859 :   if( db->activeVdbeCnt==0 ){
  111692          131043 :     db->u1.isInterrupted = 0;
  111693                 :   }
  111694          193859 :   pParse->rc = SQLITE_OK;
  111695          193859 :   pParse->zTail = zSql;
  111696          193859 :   i = 0;
  111697          193859 :   assert( pzErrMsg!=0 );
  111698          193859 :   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
  111699          193859 :   if( pEngine==0 ){
  111700               0 :     db->mallocFailed = 1;
  111701               0 :     return SQLITE_NOMEM;
  111702                 :   }
  111703          193859 :   assert( pParse->pNewTable==0 );
  111704          193859 :   assert( pParse->pNewTrigger==0 );
  111705          193859 :   assert( pParse->nVar==0 );
  111706          193859 :   assert( pParse->nzVar==0 );
  111707          193859 :   assert( pParse->azVar==0 );
  111708          193859 :   enableLookaside = db->lookaside.bEnabled;
  111709          193859 :   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
  111710         6555621 :   while( !db->mallocFailed && zSql[i]!=0 ){
  111711         6174142 :     assert( i>=0 );
  111712         6174142 :     pParse->sLastToken.z = &zSql[i];
  111713         6174142 :     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
  111714         6174138 :     i += pParse->sLastToken.n;
  111715         6174138 :     if( i>mxSqlLen ){
  111716               0 :       pParse->rc = SQLITE_TOOBIG;
  111717               0 :       break;
  111718                 :     }
  111719         6174138 :     switch( tokenType ){
  111720                 :       case TK_SPACE: {
  111721         2246779 :         if( db->u1.isInterrupted ){
  111722               0 :           sqlite3ErrorMsg(pParse, "interrupt");
  111723               0 :           pParse->rc = SQLITE_INTERRUPT;
  111724               0 :           goto abort_parse;
  111725                 :         }
  111726         2246779 :         break;
  111727                 :       }
  111728                 :       case TK_ILLEGAL: {
  111729               0 :         sqlite3DbFree(db, *pzErrMsg);
  111730               0 :         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
  111731                 :                         &pParse->sLastToken);
  111732               0 :         nErr++;
  111733               0 :         goto abort_parse;
  111734                 :       }
  111735                 :       case TK_SEMI: {
  111736           30285 :         pParse->zTail = &zSql[i];
  111737                 :         /* Fall thru into the default case */
  111738                 :       }
  111739                 :       default: {
  111740         3927359 :         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
  111741         3927366 :         lastTokenParsed = tokenType;
  111742         3927366 :         if( pParse->rc!=SQLITE_OK ){
  111743            6242 :           goto abort_parse;
  111744                 :         }
  111745         3921124 :         break;
  111746                 :       }
  111747                 :     }
  111748                 :   }
  111749                 : abort_parse:
  111750          193862 :   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
  111751          187617 :     if( lastTokenParsed!=TK_SEMI ){
  111752          178799 :       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
  111753          178799 :       pParse->zTail = &zSql[i];
  111754                 :     }
  111755          187617 :     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
  111756                 :   }
  111757                 : #ifdef YYTRACKMAXSTACKDEPTH
  111758                 :   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
  111759                 :       sqlite3ParserStackPeak(pEngine)
  111760                 :   );
  111761                 : #endif /* YYDEBUG */
  111762          193862 :   sqlite3ParserFree(pEngine, sqlite3_free);
  111763          193859 :   db->lookaside.bEnabled = enableLookaside;
  111764          193859 :   if( db->mallocFailed ){
  111765               0 :     pParse->rc = SQLITE_NOMEM;
  111766                 :   }
  111767          193859 :   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
  111768               0 :     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
  111769                 :   }
  111770          193859 :   assert( pzErrMsg!=0 );
  111771          193859 :   if( pParse->zErrMsg ){
  111772              89 :     *pzErrMsg = pParse->zErrMsg;
  111773              89 :     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
  111774              89 :     pParse->zErrMsg = 0;
  111775              89 :     nErr++;
  111776                 :   }
  111777          193859 :   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
  111778              33 :     sqlite3VdbeDelete(pParse->pVdbe);
  111779              33 :     pParse->pVdbe = 0;
  111780                 :   }
  111781                 : #ifndef SQLITE_OMIT_SHARED_CACHE
  111782          193859 :   if( pParse->nested==0 ){
  111783          173011 :     sqlite3DbFree(db, pParse->aTableLock);
  111784          173011 :     pParse->aTableLock = 0;
  111785          173011 :     pParse->nTableLock = 0;
  111786                 :   }
  111787                 : #endif
  111788                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  111789          193859 :   sqlite3_free(pParse->apVtabLock);
  111790                 : #endif
  111791                 : 
  111792          193859 :   if( !IN_DECLARE_VTAB ){
  111793                 :     /* If the pParse->declareVtab flag is set, do not delete any table 
  111794                 :     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
  111795                 :     ** will take responsibility for freeing the Table structure.
  111796                 :     */
  111797          193806 :     sqlite3DeleteTable(db, pParse->pNewTable);
  111798                 :   }
  111799                 : 
  111800          193859 :   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
  111801          193859 :   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
  111802          193859 :   sqlite3DbFree(db, pParse->azVar);
  111803          193859 :   sqlite3DbFree(db, pParse->aAlias);
  111804          388792 :   while( pParse->pAinc ){
  111805            1074 :     AutoincInfo *p = pParse->pAinc;
  111806            1074 :     pParse->pAinc = p->pNext;
  111807            1074 :     sqlite3DbFree(db, p);
  111808                 :   }
  111809          387781 :   while( pParse->pZombieTab ){
  111810              63 :     Table *p = pParse->pZombieTab;
  111811              63 :     pParse->pZombieTab = p->pNextZombie;
  111812              63 :     sqlite3DeleteTable(db, p);
  111813                 :   }
  111814          193859 :   if( nErr>0 && pParse->rc==SQLITE_OK ){
  111815               0 :     pParse->rc = SQLITE_ERROR;
  111816                 :   }
  111817          193859 :   return nErr;
  111818                 : }
  111819                 : 
  111820                 : /************** End of tokenize.c ********************************************/
  111821                 : /************** Begin file complete.c ****************************************/
  111822                 : /*
  111823                 : ** 2001 September 15
  111824                 : **
  111825                 : ** The author disclaims copyright to this source code.  In place of
  111826                 : ** a legal notice, here is a blessing:
  111827                 : **
  111828                 : **    May you do good and not evil.
  111829                 : **    May you find forgiveness for yourself and forgive others.
  111830                 : **    May you share freely, never taking more than you give.
  111831                 : **
  111832                 : *************************************************************************
  111833                 : ** An tokenizer for SQL
  111834                 : **
  111835                 : ** This file contains C code that implements the sqlite3_complete() API.
  111836                 : ** This code used to be part of the tokenizer.c source file.  But by
  111837                 : ** separating it out, the code will be automatically omitted from
  111838                 : ** static links that do not use it.
  111839                 : */
  111840                 : #ifndef SQLITE_OMIT_COMPLETE
  111841                 : 
  111842                 : /*
  111843                 : ** This is defined in tokenize.c.  We just have to import the definition.
  111844                 : */
  111845                 : #ifndef SQLITE_AMALGAMATION
  111846                 : #ifdef SQLITE_ASCII
  111847                 : #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
  111848                 : #endif
  111849                 : #ifdef SQLITE_EBCDIC
  111850                 : SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
  111851                 : #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
  111852                 : #endif
  111853                 : #endif /* SQLITE_AMALGAMATION */
  111854                 : 
  111855                 : 
  111856                 : /*
  111857                 : ** Token types used by the sqlite3_complete() routine.  See the header
  111858                 : ** comments on that procedure for additional information.
  111859                 : */
  111860                 : #define tkSEMI    0
  111861                 : #define tkWS      1
  111862                 : #define tkOTHER   2
  111863                 : #ifndef SQLITE_OMIT_TRIGGER
  111864                 : #define tkEXPLAIN 3
  111865                 : #define tkCREATE  4
  111866                 : #define tkTEMP    5
  111867                 : #define tkTRIGGER 6
  111868                 : #define tkEND     7
  111869                 : #endif
  111870                 : 
  111871                 : /*
  111872                 : ** Return TRUE if the given SQL string ends in a semicolon.
  111873                 : **
  111874                 : ** Special handling is require for CREATE TRIGGER statements.
  111875                 : ** Whenever the CREATE TRIGGER keywords are seen, the statement
  111876                 : ** must end with ";END;".
  111877                 : **
  111878                 : ** This implementation uses a state machine with 8 states:
  111879                 : **
  111880                 : **   (0) INVALID   We have not yet seen a non-whitespace character.
  111881                 : **
  111882                 : **   (1) START     At the beginning or end of an SQL statement.  This routine
  111883                 : **                 returns 1 if it ends in the START state and 0 if it ends
  111884                 : **                 in any other state.
  111885                 : **
  111886                 : **   (2) NORMAL    We are in the middle of statement which ends with a single
  111887                 : **                 semicolon.
  111888                 : **
  111889                 : **   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of 
  111890                 : **                 a statement.
  111891                 : **
  111892                 : **   (4) CREATE    The keyword CREATE has been seen at the beginning of a
  111893                 : **                 statement, possibly preceeded by EXPLAIN and/or followed by
  111894                 : **                 TEMP or TEMPORARY
  111895                 : **
  111896                 : **   (5) TRIGGER   We are in the middle of a trigger definition that must be
  111897                 : **                 ended by a semicolon, the keyword END, and another semicolon.
  111898                 : **
  111899                 : **   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
  111900                 : **                 the end of a trigger definition.
  111901                 : **
  111902                 : **   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
  111903                 : **                 of a trigger difinition.
  111904                 : **
  111905                 : ** Transitions between states above are determined by tokens extracted
  111906                 : ** from the input.  The following tokens are significant:
  111907                 : **
  111908                 : **   (0) tkSEMI      A semicolon.
  111909                 : **   (1) tkWS        Whitespace.
  111910                 : **   (2) tkOTHER     Any other SQL token.
  111911                 : **   (3) tkEXPLAIN   The "explain" keyword.
  111912                 : **   (4) tkCREATE    The "create" keyword.
  111913                 : **   (5) tkTEMP      The "temp" or "temporary" keyword.
  111914                 : **   (6) tkTRIGGER   The "trigger" keyword.
  111915                 : **   (7) tkEND       The "end" keyword.
  111916                 : **
  111917                 : ** Whitespace never causes a state transition and is always ignored.
  111918                 : ** This means that a SQL string of all whitespace is invalid.
  111919                 : **
  111920                 : ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
  111921                 : ** to recognize the end of a trigger can be omitted.  All we have to do
  111922                 : ** is look for a semicolon that is not part of an string or comment.
  111923                 : */
  111924               0 : SQLITE_API int sqlite3_complete(const char *zSql){
  111925               0 :   u8 state = 0;   /* Current state, using numbers defined in header comment */
  111926                 :   u8 token;       /* Value of the next token */
  111927                 : 
  111928                 : #ifndef SQLITE_OMIT_TRIGGER
  111929                 :   /* A complex statement machine used to detect the end of a CREATE TRIGGER
  111930                 :   ** statement.  This is the normal case.
  111931                 :   */
  111932                 :   static const u8 trans[8][8] = {
  111933                 :                      /* Token:                                                */
  111934                 :      /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
  111935                 :      /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
  111936                 :      /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
  111937                 :      /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
  111938                 :      /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
  111939                 :      /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
  111940                 :      /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
  111941                 :      /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
  111942                 :      /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
  111943                 :   };
  111944                 : #else
  111945                 :   /* If triggers are not supported by this compile then the statement machine
  111946                 :   ** used to detect the end of a statement is much simplier
  111947                 :   */
  111948                 :   static const u8 trans[3][3] = {
  111949                 :                      /* Token:           */
  111950                 :      /* State:       **  SEMI  WS  OTHER */
  111951                 :      /* 0 INVALID: */ {    1,  0,     2, },
  111952                 :      /* 1   START: */ {    1,  1,     2, },
  111953                 :      /* 2  NORMAL: */ {    1,  2,     2, },
  111954                 :   };
  111955                 : #endif /* SQLITE_OMIT_TRIGGER */
  111956                 : 
  111957               0 :   while( *zSql ){
  111958               0 :     switch( *zSql ){
  111959                 :       case ';': {  /* A semicolon */
  111960               0 :         token = tkSEMI;
  111961               0 :         break;
  111962                 :       }
  111963                 :       case ' ':
  111964                 :       case '\r':
  111965                 :       case '\t':
  111966                 :       case '\n':
  111967                 :       case '\f': {  /* White space is ignored */
  111968               0 :         token = tkWS;
  111969               0 :         break;
  111970                 :       }
  111971                 :       case '/': {   /* C-style comments */
  111972               0 :         if( zSql[1]!='*' ){
  111973               0 :           token = tkOTHER;
  111974               0 :           break;
  111975                 :         }
  111976               0 :         zSql += 2;
  111977               0 :         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
  111978               0 :         if( zSql[0]==0 ) return 0;
  111979               0 :         zSql++;
  111980               0 :         token = tkWS;
  111981               0 :         break;
  111982                 :       }
  111983                 :       case '-': {   /* SQL-style comments from "--" to end of line */
  111984               0 :         if( zSql[1]!='-' ){
  111985               0 :           token = tkOTHER;
  111986               0 :           break;
  111987                 :         }
  111988               0 :         while( *zSql && *zSql!='\n' ){ zSql++; }
  111989               0 :         if( *zSql==0 ) return state==1;
  111990               0 :         token = tkWS;
  111991               0 :         break;
  111992                 :       }
  111993                 :       case '[': {   /* Microsoft-style identifiers in [...] */
  111994               0 :         zSql++;
  111995               0 :         while( *zSql && *zSql!=']' ){ zSql++; }
  111996               0 :         if( *zSql==0 ) return 0;
  111997               0 :         token = tkOTHER;
  111998               0 :         break;
  111999                 :       }
  112000                 :       case '`':     /* Grave-accent quoted symbols used by MySQL */
  112001                 :       case '"':     /* single- and double-quoted strings */
  112002                 :       case '\'': {
  112003               0 :         int c = *zSql;
  112004               0 :         zSql++;
  112005               0 :         while( *zSql && *zSql!=c ){ zSql++; }
  112006               0 :         if( *zSql==0 ) return 0;
  112007               0 :         token = tkOTHER;
  112008               0 :         break;
  112009                 :       }
  112010                 :       default: {
  112011                 : #ifdef SQLITE_EBCDIC
  112012                 :         unsigned char c;
  112013                 : #endif
  112014               0 :         if( IdChar((u8)*zSql) ){
  112015                 :           /* Keywords and unquoted identifiers */
  112016                 :           int nId;
  112017               0 :           for(nId=1; IdChar(zSql[nId]); nId++){}
  112018                 : #ifdef SQLITE_OMIT_TRIGGER
  112019                 :           token = tkOTHER;
  112020                 : #else
  112021               0 :           switch( *zSql ){
  112022                 :             case 'c': case 'C': {
  112023               0 :               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
  112024               0 :                 token = tkCREATE;
  112025                 :               }else{
  112026               0 :                 token = tkOTHER;
  112027                 :               }
  112028               0 :               break;
  112029                 :             }
  112030                 :             case 't': case 'T': {
  112031               0 :               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
  112032               0 :                 token = tkTRIGGER;
  112033               0 :               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
  112034               0 :                 token = tkTEMP;
  112035               0 :               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
  112036               0 :                 token = tkTEMP;
  112037                 :               }else{
  112038               0 :                 token = tkOTHER;
  112039                 :               }
  112040               0 :               break;
  112041                 :             }
  112042                 :             case 'e':  case 'E': {
  112043               0 :               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
  112044               0 :                 token = tkEND;
  112045                 :               }else
  112046                 : #ifndef SQLITE_OMIT_EXPLAIN
  112047               0 :               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
  112048               0 :                 token = tkEXPLAIN;
  112049                 :               }else
  112050                 : #endif
  112051                 :               {
  112052               0 :                 token = tkOTHER;
  112053                 :               }
  112054               0 :               break;
  112055                 :             }
  112056                 :             default: {
  112057               0 :               token = tkOTHER;
  112058               0 :               break;
  112059                 :             }
  112060                 :           }
  112061                 : #endif /* SQLITE_OMIT_TRIGGER */
  112062               0 :           zSql += nId-1;
  112063                 :         }else{
  112064                 :           /* Operators and special symbols */
  112065               0 :           token = tkOTHER;
  112066                 :         }
  112067               0 :         break;
  112068                 :       }
  112069                 :     }
  112070               0 :     state = trans[state][token];
  112071               0 :     zSql++;
  112072                 :   }
  112073               0 :   return state==1;
  112074                 : }
  112075                 : 
  112076                 : #ifndef SQLITE_OMIT_UTF16
  112077                 : /*
  112078                 : ** This routine is the same as the sqlite3_complete() routine described
  112079                 : ** above, except that the parameter is required to be UTF-16 encoded, not
  112080                 : ** UTF-8.
  112081                 : */
  112082               0 : SQLITE_API int sqlite3_complete16(const void *zSql){
  112083                 :   sqlite3_value *pVal;
  112084                 :   char const *zSql8;
  112085               0 :   int rc = SQLITE_NOMEM;
  112086                 : 
  112087                 : #ifndef SQLITE_OMIT_AUTOINIT
  112088               0 :   rc = sqlite3_initialize();
  112089               0 :   if( rc ) return rc;
  112090                 : #endif
  112091               0 :   pVal = sqlite3ValueNew(0);
  112092               0 :   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
  112093               0 :   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
  112094               0 :   if( zSql8 ){
  112095               0 :     rc = sqlite3_complete(zSql8);
  112096                 :   }else{
  112097               0 :     rc = SQLITE_NOMEM;
  112098                 :   }
  112099               0 :   sqlite3ValueFree(pVal);
  112100               0 :   return sqlite3ApiExit(0, rc);
  112101                 : }
  112102                 : #endif /* SQLITE_OMIT_UTF16 */
  112103                 : #endif /* SQLITE_OMIT_COMPLETE */
  112104                 : 
  112105                 : /************** End of complete.c ********************************************/
  112106                 : /************** Begin file main.c ********************************************/
  112107                 : /*
  112108                 : ** 2001 September 15
  112109                 : **
  112110                 : ** The author disclaims copyright to this source code.  In place of
  112111                 : ** a legal notice, here is a blessing:
  112112                 : **
  112113                 : **    May you do good and not evil.
  112114                 : **    May you find forgiveness for yourself and forgive others.
  112115                 : **    May you share freely, never taking more than you give.
  112116                 : **
  112117                 : *************************************************************************
  112118                 : ** Main file for the SQLite library.  The routines in this file
  112119                 : ** implement the programmer interface to the library.  Routines in
  112120                 : ** other files are for internal use by SQLite and should not be
  112121                 : ** accessed by users of the library.
  112122                 : */
  112123                 : 
  112124                 : #ifdef SQLITE_ENABLE_FTS3
  112125                 : /************** Include fts3.h in the middle of main.c ***********************/
  112126                 : /************** Begin file fts3.h ********************************************/
  112127                 : /*
  112128                 : ** 2006 Oct 10
  112129                 : **
  112130                 : ** The author disclaims copyright to this source code.  In place of
  112131                 : ** a legal notice, here is a blessing:
  112132                 : **
  112133                 : **    May you do good and not evil.
  112134                 : **    May you find forgiveness for yourself and forgive others.
  112135                 : **    May you share freely, never taking more than you give.
  112136                 : **
  112137                 : ******************************************************************************
  112138                 : **
  112139                 : ** This header file is used by programs that want to link against the
  112140                 : ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
  112141                 : */
  112142                 : 
  112143                 : #if 0
  112144                 : extern "C" {
  112145                 : #endif  /* __cplusplus */
  112146                 : 
  112147                 : SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
  112148                 : 
  112149                 : #if 0
  112150                 : }  /* extern "C" */
  112151                 : #endif  /* __cplusplus */
  112152                 : 
  112153                 : /************** End of fts3.h ************************************************/
  112154                 : /************** Continuing where we left off in main.c ***********************/
  112155                 : #endif
  112156                 : #ifdef SQLITE_ENABLE_RTREE
  112157                 : /************** Include rtree.h in the middle of main.c **********************/
  112158                 : /************** Begin file rtree.h *******************************************/
  112159                 : /*
  112160                 : ** 2008 May 26
  112161                 : **
  112162                 : ** The author disclaims copyright to this source code.  In place of
  112163                 : ** a legal notice, here is a blessing:
  112164                 : **
  112165                 : **    May you do good and not evil.
  112166                 : **    May you find forgiveness for yourself and forgive others.
  112167                 : **    May you share freely, never taking more than you give.
  112168                 : **
  112169                 : ******************************************************************************
  112170                 : **
  112171                 : ** This header file is used by programs that want to link against the
  112172                 : ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
  112173                 : */
  112174                 : 
  112175                 : #if 0
  112176                 : extern "C" {
  112177                 : #endif  /* __cplusplus */
  112178                 : 
  112179                 : SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
  112180                 : 
  112181                 : #if 0
  112182                 : }  /* extern "C" */
  112183                 : #endif  /* __cplusplus */
  112184                 : 
  112185                 : /************** End of rtree.h ***********************************************/
  112186                 : /************** Continuing where we left off in main.c ***********************/
  112187                 : #endif
  112188                 : #ifdef SQLITE_ENABLE_ICU
  112189                 : /************** Include sqliteicu.h in the middle of main.c ******************/
  112190                 : /************** Begin file sqliteicu.h ***************************************/
  112191                 : /*
  112192                 : ** 2008 May 26
  112193                 : **
  112194                 : ** The author disclaims copyright to this source code.  In place of
  112195                 : ** a legal notice, here is a blessing:
  112196                 : **
  112197                 : **    May you do good and not evil.
  112198                 : **    May you find forgiveness for yourself and forgive others.
  112199                 : **    May you share freely, never taking more than you give.
  112200                 : **
  112201                 : ******************************************************************************
  112202                 : **
  112203                 : ** This header file is used by programs that want to link against the
  112204                 : ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
  112205                 : */
  112206                 : 
  112207                 : #if 0
  112208                 : extern "C" {
  112209                 : #endif  /* __cplusplus */
  112210                 : 
  112211                 : SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
  112212                 : 
  112213                 : #if 0
  112214                 : }  /* extern "C" */
  112215                 : #endif  /* __cplusplus */
  112216                 : 
  112217                 : 
  112218                 : /************** End of sqliteicu.h *******************************************/
  112219                 : /************** Continuing where we left off in main.c ***********************/
  112220                 : #endif
  112221                 : 
  112222                 : #ifndef SQLITE_AMALGAMATION
  112223                 : /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
  112224                 : ** contains the text of SQLITE_VERSION macro. 
  112225                 : */
  112226                 : SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
  112227                 : #endif
  112228                 : 
  112229                 : /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
  112230                 : ** a pointer to the to the sqlite3_version[] string constant. 
  112231                 : */
  112232               0 : SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
  112233                 : 
  112234                 : /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
  112235                 : ** pointer to a string constant whose value is the same as the
  112236                 : ** SQLITE_SOURCE_ID C preprocessor macro. 
  112237                 : */
  112238              12 : SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
  112239                 : 
  112240                 : /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
  112241                 : ** returns an integer equal to SQLITE_VERSION_NUMBER.
  112242                 : */
  112243             803 : SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
  112244                 : 
  112245                 : /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
  112246                 : ** zero if and only if SQLite was compiled with mutexing code omitted due to
  112247                 : ** the SQLITE_THREADSAFE compile-time option being set to 0.
  112248                 : */
  112249               0 : SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
  112250                 : 
  112251                 : #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
  112252                 : /*
  112253                 : ** If the following function pointer is not NULL and if
  112254                 : ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
  112255                 : ** I/O active are written using this function.  These messages
  112256                 : ** are intended for debugging activity only.
  112257                 : */
  112258                 : SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
  112259                 : #endif
  112260                 : 
  112261                 : /*
  112262                 : ** If the following global variable points to a string which is the
  112263                 : ** name of a directory, then that directory will be used to store
  112264                 : ** temporary files.
  112265                 : **
  112266                 : ** See also the "PRAGMA temp_store_directory" SQL command.
  112267                 : */
  112268                 : SQLITE_API char *sqlite3_temp_directory = 0;
  112269                 : 
  112270                 : /*
  112271                 : ** Initialize SQLite.  
  112272                 : **
  112273                 : ** This routine must be called to initialize the memory allocation,
  112274                 : ** VFS, and mutex subsystems prior to doing any serious work with
  112275                 : ** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
  112276                 : ** this routine will be called automatically by key routines such as
  112277                 : ** sqlite3_open().  
  112278                 : **
  112279                 : ** This routine is a no-op except on its very first call for the process,
  112280                 : ** or for the first call after a call to sqlite3_shutdown.
  112281                 : **
  112282                 : ** The first thread to call this routine runs the initialization to
  112283                 : ** completion.  If subsequent threads call this routine before the first
  112284                 : ** thread has finished the initialization process, then the subsequent
  112285                 : ** threads must block until the first thread finishes with the initialization.
  112286                 : **
  112287                 : ** The first thread might call this routine recursively.  Recursive
  112288                 : ** calls to this routine should not block, of course.  Otherwise the
  112289                 : ** initialization process would never complete.
  112290                 : **
  112291                 : ** Let X be the first thread to enter this routine.  Let Y be some other
  112292                 : ** thread.  Then while the initial invocation of this routine by X is
  112293                 : ** incomplete, it is required that:
  112294                 : **
  112295                 : **    *  Calls to this routine from Y must block until the outer-most
  112296                 : **       call by X completes.
  112297                 : **
  112298                 : **    *  Recursive calls to this routine from thread X return immediately
  112299                 : **       without blocking.
  112300                 : */
  112301         2475235 : SQLITE_API int sqlite3_initialize(void){
  112302                 :   MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
  112303                 :   int rc;                                      /* Result code */
  112304                 : 
  112305                 : #ifdef SQLITE_OMIT_WSD
  112306                 :   rc = sqlite3_wsd_init(4096, 24);
  112307                 :   if( rc!=SQLITE_OK ){
  112308                 :     return rc;
  112309                 :   }
  112310                 : #endif
  112311                 : 
  112312                 :   /* If SQLite is already completely initialized, then this call
  112313                 :   ** to sqlite3_initialize() should be a no-op.  But the initialization
  112314                 :   ** must be complete.  So isInit must not be set until the very end
  112315                 :   ** of this routine.
  112316                 :   */
  112317         2475235 :   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
  112318                 : 
  112319                 :   /* Make sure the mutex subsystem is initialized.  If unable to 
  112320                 :   ** initialize the mutex subsystem, return early with the error.
  112321                 :   ** If the system is so sick that we are unable to allocate a mutex,
  112322                 :   ** there is not much SQLite is going to be able to do.
  112323                 :   **
  112324                 :   ** The mutex subsystem must take care of serializing its own
  112325                 :   ** initialization.
  112326                 :   */
  112327            6448 :   rc = sqlite3MutexInit();
  112328            6448 :   if( rc ) return rc;
  112329                 : 
  112330                 :   /* Initialize the malloc() system and the recursive pInitMutex mutex.
  112331                 :   ** This operation is protected by the STATIC_MASTER mutex.  Note that
  112332                 :   ** MutexAlloc() is called for a static mutex prior to initializing the
  112333                 :   ** malloc subsystem - this implies that the allocation of a static
  112334                 :   ** mutex must not require support from the malloc subsystem.
  112335                 :   */
  112336            6448 :   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
  112337            6448 :   sqlite3_mutex_enter(pMaster);
  112338            6448 :   sqlite3GlobalConfig.isMutexInit = 1;
  112339            6448 :   if( !sqlite3GlobalConfig.isMallocInit ){
  112340             806 :     rc = sqlite3MallocInit();
  112341                 :   }
  112342            6448 :   if( rc==SQLITE_OK ){
  112343            6448 :     sqlite3GlobalConfig.isMallocInit = 1;
  112344            6448 :     if( !sqlite3GlobalConfig.pInitMutex ){
  112345             806 :       sqlite3GlobalConfig.pInitMutex =
  112346             806 :            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
  112347             806 :       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
  112348               0 :         rc = SQLITE_NOMEM;
  112349                 :       }
  112350                 :     }
  112351                 :   }
  112352            6448 :   if( rc==SQLITE_OK ){
  112353            6448 :     sqlite3GlobalConfig.nRefInitMutex++;
  112354                 :   }
  112355            6448 :   sqlite3_mutex_leave(pMaster);
  112356                 : 
  112357                 :   /* If rc is not SQLITE_OK at this point, then either the malloc
  112358                 :   ** subsystem could not be initialized or the system failed to allocate
  112359                 :   ** the pInitMutex mutex. Return an error in either case.  */
  112360            6448 :   if( rc!=SQLITE_OK ){
  112361               0 :     return rc;
  112362                 :   }
  112363                 : 
  112364                 :   /* Do the rest of the initialization under the recursive mutex so
  112365                 :   ** that we will be able to handle recursive calls into
  112366                 :   ** sqlite3_initialize().  The recursive calls normally come through
  112367                 :   ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
  112368                 :   ** recursive calls might also be possible.
  112369                 :   **
  112370                 :   ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
  112371                 :   ** to the xInit method, so the xInit method need not be threadsafe.
  112372                 :   **
  112373                 :   ** The following mutex is what serializes access to the appdef pcache xInit
  112374                 :   ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
  112375                 :   ** call to sqlite3PcacheInitialize().
  112376                 :   */
  112377            6448 :   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
  112378            6448 :   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
  112379             806 :     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
  112380             806 :     sqlite3GlobalConfig.inProgress = 1;
  112381             806 :     memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
  112382             806 :     sqlite3RegisterGlobalFunctions();
  112383             806 :     if( sqlite3GlobalConfig.isPCacheInit==0 ){
  112384             806 :       rc = sqlite3PcacheInitialize();
  112385                 :     }
  112386             806 :     if( rc==SQLITE_OK ){
  112387             806 :       sqlite3GlobalConfig.isPCacheInit = 1;
  112388             806 :       rc = sqlite3OsInit();
  112389                 :     }
  112390             806 :     if( rc==SQLITE_OK ){
  112391             806 :       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
  112392                 :           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
  112393             806 :       sqlite3GlobalConfig.isInit = 1;
  112394                 :     }
  112395             806 :     sqlite3GlobalConfig.inProgress = 0;
  112396                 :   }
  112397            6448 :   sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
  112398                 : 
  112399                 :   /* Go back under the static mutex and clean up the recursive
  112400                 :   ** mutex to prevent a resource leak.
  112401                 :   */
  112402            6448 :   sqlite3_mutex_enter(pMaster);
  112403            6448 :   sqlite3GlobalConfig.nRefInitMutex--;
  112404            6448 :   if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
  112405             806 :     assert( sqlite3GlobalConfig.nRefInitMutex==0 );
  112406             806 :     sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
  112407             806 :     sqlite3GlobalConfig.pInitMutex = 0;
  112408                 :   }
  112409            6448 :   sqlite3_mutex_leave(pMaster);
  112410                 : 
  112411                 :   /* The following is just a sanity check to make sure SQLite has
  112412                 :   ** been compiled correctly.  It is important to run this code, but
  112413                 :   ** we don't want to run it too often and soak up CPU cycles for no
  112414                 :   ** reason.  So we run it once during initialization.
  112415                 :   */
  112416                 : #ifndef NDEBUG
  112417                 : #ifndef SQLITE_OMIT_FLOATING_POINT
  112418                 :   /* This section of code's only "output" is via assert() statements. */
  112419            6448 :   if ( rc==SQLITE_OK ){
  112420            6448 :     u64 x = (((u64)1)<<63)-1;
  112421                 :     double y;
  112422                 :     assert(sizeof(x)==8);
  112423                 :     assert(sizeof(x)==sizeof(y));
  112424            6448 :     memcpy(&y, &x, 8);
  112425            6448 :     assert( sqlite3IsNaN(y) );
  112426                 :   }
  112427                 : #endif
  112428                 : #endif
  112429                 : 
  112430                 :   /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
  112431                 :   ** compile-time option.
  112432                 :   */
  112433                 : #ifdef SQLITE_EXTRA_INIT
  112434                 :   if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
  112435                 :     int SQLITE_EXTRA_INIT(const char*);
  112436                 :     rc = SQLITE_EXTRA_INIT(0);
  112437                 :   }
  112438                 : #endif
  112439                 : 
  112440            6448 :   return rc;
  112441                 : }
  112442                 : 
  112443                 : /*
  112444                 : ** Undo the effects of sqlite3_initialize().  Must not be called while
  112445                 : ** there are outstanding database connections or memory allocations or
  112446                 : ** while any part of SQLite is otherwise in use in any thread.  This
  112447                 : ** routine is not threadsafe.  But it is safe to invoke this routine
  112448                 : ** on when SQLite is already shut down.  If SQLite is already shut down
  112449                 : ** when this routine is invoked, then this routine is a harmless no-op.
  112450                 : */
  112451             795 : SQLITE_API int sqlite3_shutdown(void){
  112452             795 :   if( sqlite3GlobalConfig.isInit ){
  112453                 : #ifdef SQLITE_EXTRA_SHUTDOWN
  112454                 :     void SQLITE_EXTRA_SHUTDOWN(void);
  112455                 :     SQLITE_EXTRA_SHUTDOWN();
  112456                 : #endif
  112457             795 :     sqlite3_os_end();
  112458             795 :     sqlite3_reset_auto_extension();
  112459             795 :     sqlite3GlobalConfig.isInit = 0;
  112460                 :   }
  112461             795 :   if( sqlite3GlobalConfig.isPCacheInit ){
  112462             795 :     sqlite3PcacheShutdown();
  112463             795 :     sqlite3GlobalConfig.isPCacheInit = 0;
  112464                 :   }
  112465             795 :   if( sqlite3GlobalConfig.isMallocInit ){
  112466             795 :     sqlite3MallocEnd();
  112467             795 :     sqlite3GlobalConfig.isMallocInit = 0;
  112468                 :   }
  112469             795 :   if( sqlite3GlobalConfig.isMutexInit ){
  112470             795 :     sqlite3MutexEnd();
  112471             795 :     sqlite3GlobalConfig.isMutexInit = 0;
  112472                 :   }
  112473                 : 
  112474             795 :   return SQLITE_OK;
  112475                 : }
  112476                 : 
  112477                 : /*
  112478                 : ** This API allows applications to modify the global configuration of
  112479                 : ** the SQLite library at run-time.
  112480                 : **
  112481                 : ** This routine should only be called when there are no outstanding
  112482                 : ** database connections or memory allocations.  This routine is not
  112483                 : ** threadsafe.  Failure to heed these warnings can lead to unpredictable
  112484                 : ** behavior.
  112485                 : */
  112486            1614 : SQLITE_API int sqlite3_config(int op, ...){
  112487                 :   va_list ap;
  112488            1614 :   int rc = SQLITE_OK;
  112489                 : 
  112490                 :   /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
  112491                 :   ** the SQLite library is in use. */
  112492            1614 :   if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
  112493                 : 
  112494            1614 :   va_start(ap, op);
  112495            1614 :   switch( op ){
  112496                 : 
  112497                 :     /* Mutex configuration options are only available in a threadsafe
  112498                 :     ** compile. 
  112499                 :     */
  112500                 : #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
  112501                 :     case SQLITE_CONFIG_SINGLETHREAD: {
  112502                 :       /* Disable all mutexing */
  112503               0 :       sqlite3GlobalConfig.bCoreMutex = 0;
  112504               0 :       sqlite3GlobalConfig.bFullMutex = 0;
  112505               0 :       break;
  112506                 :     }
  112507                 :     case SQLITE_CONFIG_MULTITHREAD: {
  112508                 :       /* Disable mutexing of database connections */
  112509                 :       /* Enable mutexing of core data structures */
  112510               0 :       sqlite3GlobalConfig.bCoreMutex = 1;
  112511               0 :       sqlite3GlobalConfig.bFullMutex = 0;
  112512               0 :       break;
  112513                 :     }
  112514                 :     case SQLITE_CONFIG_SERIALIZED: {
  112515                 :       /* Enable all mutexing */
  112516               0 :       sqlite3GlobalConfig.bCoreMutex = 1;
  112517               0 :       sqlite3GlobalConfig.bFullMutex = 1;
  112518               0 :       break;
  112519                 :     }
  112520                 :     case SQLITE_CONFIG_MUTEX: {
  112521                 :       /* Specify an alternative mutex implementation */
  112522               1 :       sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
  112523               1 :       break;
  112524                 :     }
  112525                 :     case SQLITE_CONFIG_GETMUTEX: {
  112526                 :       /* Retrieve the current mutex implementation */
  112527               2 :       *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
  112528               2 :       break;
  112529                 :     }
  112530                 : #endif
  112531                 : 
  112532                 : 
  112533                 :     case SQLITE_CONFIG_MALLOC: {
  112534                 :       /* Specify an alternative malloc implementation */
  112535             806 :       sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
  112536             806 :       break;
  112537                 :     }
  112538                 :     case SQLITE_CONFIG_GETMALLOC: {
  112539                 :       /* Retrieve the current malloc() implementation */
  112540               0 :       if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
  112541               0 :       *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
  112542               0 :       break;
  112543                 :     }
  112544                 :     case SQLITE_CONFIG_MEMSTATUS: {
  112545                 :       /* Enable or disable the malloc status collection */
  112546               0 :       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
  112547               0 :       break;
  112548                 :     }
  112549                 :     case SQLITE_CONFIG_SCRATCH: {
  112550                 :       /* Designate a buffer for scratch memory space */
  112551               0 :       sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
  112552               0 :       sqlite3GlobalConfig.szScratch = va_arg(ap, int);
  112553               0 :       sqlite3GlobalConfig.nScratch = va_arg(ap, int);
  112554               0 :       break;
  112555                 :     }
  112556                 :     case SQLITE_CONFIG_PAGECACHE: {
  112557                 :       /* Designate a buffer for page cache memory space */
  112558               0 :       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
  112559               0 :       sqlite3GlobalConfig.szPage = va_arg(ap, int);
  112560               0 :       sqlite3GlobalConfig.nPage = va_arg(ap, int);
  112561               0 :       break;
  112562                 :     }
  112563                 : 
  112564                 :     case SQLITE_CONFIG_PCACHE: {
  112565                 :       /* no-op */
  112566               0 :       break;
  112567                 :     }
  112568                 :     case SQLITE_CONFIG_GETPCACHE: {
  112569                 :       /* now an error */
  112570               0 :       rc = SQLITE_ERROR;
  112571               0 :       break;
  112572                 :     }
  112573                 : 
  112574                 :     case SQLITE_CONFIG_PCACHE2: {
  112575                 :       /* Specify an alternative page cache implementation */
  112576             805 :       sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
  112577             805 :       break;
  112578                 :     }
  112579                 :     case SQLITE_CONFIG_GETPCACHE2: {
  112580               0 :       if( sqlite3GlobalConfig.pcache2.xInit==0 ){
  112581               0 :         sqlite3PCacheSetDefault();
  112582                 :       }
  112583               0 :       *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
  112584               0 :       break;
  112585                 :     }
  112586                 : 
  112587                 : #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
  112588                 :     case SQLITE_CONFIG_HEAP: {
  112589                 :       /* Designate a buffer for heap memory space */
  112590                 :       sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
  112591                 :       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
  112592                 :       sqlite3GlobalConfig.mnReq = va_arg(ap, int);
  112593                 : 
  112594                 :       if( sqlite3GlobalConfig.mnReq<1 ){
  112595                 :         sqlite3GlobalConfig.mnReq = 1;
  112596                 :       }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
  112597                 :         /* cap min request size at 2^12 */
  112598                 :         sqlite3GlobalConfig.mnReq = (1<<12);
  112599                 :       }
  112600                 : 
  112601                 :       if( sqlite3GlobalConfig.pHeap==0 ){
  112602                 :         /* If the heap pointer is NULL, then restore the malloc implementation
  112603                 :         ** back to NULL pointers too.  This will cause the malloc to go
  112604                 :         ** back to its default implementation when sqlite3_initialize() is
  112605                 :         ** run.
  112606                 :         */
  112607                 :         memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
  112608                 :       }else{
  112609                 :         /* The heap pointer is not NULL, then install one of the
  112610                 :         ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
  112611                 :         ** ENABLE_MEMSYS5 is defined, return an error.
  112612                 :         */
  112613                 : #ifdef SQLITE_ENABLE_MEMSYS3
  112614                 :         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
  112615                 : #endif
  112616                 : #ifdef SQLITE_ENABLE_MEMSYS5
  112617                 :         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
  112618                 : #endif
  112619                 :       }
  112620                 :       break;
  112621                 :     }
  112622                 : #endif
  112623                 : 
  112624                 :     case SQLITE_CONFIG_LOOKASIDE: {
  112625               0 :       sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
  112626               0 :       sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
  112627               0 :       break;
  112628                 :     }
  112629                 :     
  112630                 :     /* Record a pointer to the logger funcction and its first argument.
  112631                 :     ** The default is NULL.  Logging is disabled if the function pointer is
  112632                 :     ** NULL.
  112633                 :     */
  112634                 :     case SQLITE_CONFIG_LOG: {
  112635                 :       /* MSVC is picky about pulling func ptrs from va lists.
  112636                 :       ** http://support.microsoft.com/kb/47961
  112637                 :       ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
  112638                 :       */
  112639                 :       typedef void(*LOGFUNC_t)(void*,int,const char*);
  112640               0 :       sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
  112641               0 :       sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
  112642               0 :       break;
  112643                 :     }
  112644                 : 
  112645                 :     case SQLITE_CONFIG_URI: {
  112646               0 :       sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
  112647               0 :       break;
  112648                 :     }
  112649                 : 
  112650                 :     default: {
  112651               0 :       rc = SQLITE_ERROR;
  112652               0 :       break;
  112653                 :     }
  112654                 :   }
  112655            1614 :   va_end(ap);
  112656            1614 :   return rc;
  112657                 : }
  112658                 : 
  112659                 : /*
  112660                 : ** Set up the lookaside buffers for a database connection.
  112661                 : ** Return SQLITE_OK on success.  
  112662                 : ** If lookaside is already active, return SQLITE_BUSY.
  112663                 : **
  112664                 : ** The sz parameter is the number of bytes in each lookaside slot.
  112665                 : ** The cnt parameter is the number of slots.  If pStart is NULL the
  112666                 : ** space for the lookaside memory is obtained from sqlite3_malloc().
  112667                 : ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
  112668                 : ** the lookaside memory.
  112669                 : */
  112670            3277 : static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
  112671                 :   void *pStart;
  112672            3277 :   if( db->lookaside.nOut ){
  112673               0 :     return SQLITE_BUSY;
  112674                 :   }
  112675                 :   /* Free any existing lookaside buffer for this handle before
  112676                 :   ** allocating a new one so we don't have to have space for 
  112677                 :   ** both at the same time.
  112678                 :   */
  112679            3277 :   if( db->lookaside.bMalloced ){
  112680               0 :     sqlite3_free(db->lookaside.pStart);
  112681                 :   }
  112682                 :   /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
  112683                 :   ** than a pointer to be useful.
  112684                 :   */
  112685            3277 :   sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
  112686            3277 :   if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
  112687            3277 :   if( cnt<0 ) cnt = 0;
  112688            3277 :   if( sz==0 || cnt==0 ){
  112689               0 :     sz = 0;
  112690               0 :     pStart = 0;
  112691            3277 :   }else if( pBuf==0 ){
  112692            3277 :     sqlite3BeginBenignMalloc();
  112693            3277 :     pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
  112694            3277 :     sqlite3EndBenignMalloc();
  112695            3277 :     if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
  112696                 :   }else{
  112697               0 :     pStart = pBuf;
  112698                 :   }
  112699            3277 :   db->lookaside.pStart = pStart;
  112700            3277 :   db->lookaside.pFree = 0;
  112701            3277 :   db->lookaside.sz = (u16)sz;
  112702            3277 :   if( pStart ){
  112703                 :     int i;
  112704                 :     LookasideSlot *p;
  112705            3277 :     assert( sz > (int)sizeof(LookasideSlot*) );
  112706            3277 :     p = (LookasideSlot*)pStart;
  112707         1681101 :     for(i=cnt-1; i>=0; i--){
  112708         1677824 :       p->pNext = db->lookaside.pFree;
  112709         1677824 :       db->lookaside.pFree = p;
  112710         1677824 :       p = (LookasideSlot*)&((u8*)p)[sz];
  112711                 :     }
  112712            3277 :     db->lookaside.pEnd = p;
  112713            3277 :     db->lookaside.bEnabled = 1;
  112714            3277 :     db->lookaside.bMalloced = pBuf==0 ?1:0;
  112715                 :   }else{
  112716               0 :     db->lookaside.pEnd = 0;
  112717               0 :     db->lookaside.bEnabled = 0;
  112718               0 :     db->lookaside.bMalloced = 0;
  112719                 :   }
  112720            3277 :   return SQLITE_OK;
  112721                 : }
  112722                 : 
  112723                 : /*
  112724                 : ** Return the mutex associated with a database connection.
  112725                 : */
  112726            3277 : SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
  112727            3277 :   return db->mutex;
  112728                 : }
  112729                 : 
  112730                 : /*
  112731                 : ** Free up as much memory as we can from the given database
  112732                 : ** connection.
  112733                 : */
  112734               0 : SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
  112735                 :   int i;
  112736               0 :   sqlite3_mutex_enter(db->mutex);
  112737               0 :   sqlite3BtreeEnterAll(db);
  112738               0 :   for(i=0; i<db->nDb; i++){
  112739               0 :     Btree *pBt = db->aDb[i].pBt;
  112740               0 :     if( pBt ){
  112741               0 :       Pager *pPager = sqlite3BtreePager(pBt);
  112742               0 :       sqlite3PagerShrink(pPager);
  112743                 :     }
  112744                 :   }
  112745               0 :   sqlite3BtreeLeaveAll(db);
  112746               0 :   sqlite3_mutex_leave(db->mutex);
  112747               0 :   return SQLITE_OK;
  112748                 : }
  112749                 : 
  112750                 : /*
  112751                 : ** Configuration settings for an individual database connection
  112752                 : */
  112753               0 : SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
  112754                 :   va_list ap;
  112755                 :   int rc;
  112756               0 :   va_start(ap, op);
  112757               0 :   switch( op ){
  112758                 :     case SQLITE_DBCONFIG_LOOKASIDE: {
  112759               0 :       void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
  112760               0 :       int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
  112761               0 :       int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
  112762               0 :       rc = setupLookaside(db, pBuf, sz, cnt);
  112763               0 :       break;
  112764                 :     }
  112765                 :     default: {
  112766                 :       static const struct {
  112767                 :         int op;      /* The opcode */
  112768                 :         u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
  112769                 :       } aFlagOp[] = {
  112770                 :         { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
  112771                 :         { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
  112772                 :       };
  112773                 :       unsigned int i;
  112774               0 :       rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
  112775               0 :       for(i=0; i<ArraySize(aFlagOp); i++){
  112776               0 :         if( aFlagOp[i].op==op ){
  112777               0 :           int onoff = va_arg(ap, int);
  112778               0 :           int *pRes = va_arg(ap, int*);
  112779               0 :           int oldFlags = db->flags;
  112780               0 :           if( onoff>0 ){
  112781               0 :             db->flags |= aFlagOp[i].mask;
  112782               0 :           }else if( onoff==0 ){
  112783               0 :             db->flags &= ~aFlagOp[i].mask;
  112784                 :           }
  112785               0 :           if( oldFlags!=db->flags ){
  112786               0 :             sqlite3ExpirePreparedStatements(db);
  112787                 :           }
  112788               0 :           if( pRes ){
  112789               0 :             *pRes = (db->flags & aFlagOp[i].mask)!=0;
  112790                 :           }
  112791               0 :           rc = SQLITE_OK;
  112792               0 :           break;
  112793                 :         }
  112794                 :       }
  112795               0 :       break;
  112796                 :     }
  112797                 :   }
  112798               0 :   va_end(ap);
  112799               0 :   return rc;
  112800                 : }
  112801                 : 
  112802                 : 
  112803                 : /*
  112804                 : ** Return true if the buffer z[0..n-1] contains all spaces.
  112805                 : */
  112806               0 : static int allSpaces(const char *z, int n){
  112807               0 :   while( n>0 && z[n-1]==' ' ){ n--; }
  112808               0 :   return n==0;
  112809                 : }
  112810                 : 
  112811                 : /*
  112812                 : ** This is the default collating function named "BINARY" which is always
  112813                 : ** available.
  112814                 : **
  112815                 : ** If the padFlag argument is not NULL then space padding at the end
  112816                 : ** of strings is ignored.  This implements the RTRIM collation.
  112817                 : */
  112818          967356 : static int binCollFunc(
  112819                 :   void *padFlag,
  112820                 :   int nKey1, const void *pKey1,
  112821                 :   int nKey2, const void *pKey2
  112822                 : ){
  112823                 :   int rc, n;
  112824          967356 :   n = nKey1<nKey2 ? nKey1 : nKey2;
  112825          967356 :   rc = memcmp(pKey1, pKey2, n);
  112826          967356 :   if( rc==0 ){
  112827          347604 :     if( padFlag
  112828               0 :      && allSpaces(((char*)pKey1)+n, nKey1-n)
  112829               0 :      && allSpaces(((char*)pKey2)+n, nKey2-n)
  112830                 :     ){
  112831                 :       /* Leave rc unchanged at 0 */
  112832                 :     }else{
  112833          347604 :       rc = nKey1 - nKey2;
  112834                 :     }
  112835                 :   }
  112836          967356 :   return rc;
  112837                 : }
  112838                 : 
  112839                 : /*
  112840                 : ** Another built-in collating sequence: NOCASE. 
  112841                 : **
  112842                 : ** This collating sequence is intended to be used for "case independant
  112843                 : ** comparison". SQLite's knowledge of upper and lower case equivalents
  112844                 : ** extends only to the 26 characters used in the English language.
  112845                 : **
  112846                 : ** At the moment there is only a UTF-8 implementation.
  112847                 : */
  112848             229 : static int nocaseCollatingFunc(
  112849                 :   void *NotUsed,
  112850                 :   int nKey1, const void *pKey1,
  112851                 :   int nKey2, const void *pKey2
  112852                 : ){
  112853             229 :   int r = sqlite3StrNICmp(
  112854                 :       (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
  112855                 :   UNUSED_PARAMETER(NotUsed);
  112856             229 :   if( 0==r ){
  112857               0 :     r = nKey1-nKey2;
  112858                 :   }
  112859             229 :   return r;
  112860                 : }
  112861                 : 
  112862                 : /*
  112863                 : ** Return the ROWID of the most recent insert
  112864                 : */
  112865            5721 : SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
  112866            5721 :   return db->lastRowid;
  112867                 : }
  112868                 : 
  112869                 : /*
  112870                 : ** Return the number of changes in the most recent call to sqlite3_exec().
  112871                 : */
  112872               0 : SQLITE_API int sqlite3_changes(sqlite3 *db){
  112873               0 :   return db->nChange;
  112874                 : }
  112875                 : 
  112876                 : /*
  112877                 : ** Return the number of changes since the database handle was opened.
  112878                 : */
  112879               0 : SQLITE_API int sqlite3_total_changes(sqlite3 *db){
  112880               0 :   return db->nTotalChange;
  112881                 : }
  112882                 : 
  112883                 : /*
  112884                 : ** Close all open savepoints. This function only manipulates fields of the
  112885                 : ** database handle object, it does not close any savepoints that may be open
  112886                 : ** at the b-tree/pager level.
  112887                 : */
  112888           20097 : SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
  112889           40213 :   while( db->pSavepoint ){
  112890              19 :     Savepoint *pTmp = db->pSavepoint;
  112891              19 :     db->pSavepoint = pTmp->pNext;
  112892              19 :     sqlite3DbFree(db, pTmp);
  112893                 :   }
  112894           20097 :   db->nSavepoint = 0;
  112895           20097 :   db->nStatement = 0;
  112896           20097 :   db->isTransactionSavepoint = 0;
  112897           20097 : }
  112898                 : 
  112899                 : /*
  112900                 : ** Invoke the destructor function associated with FuncDef p, if any. Except,
  112901                 : ** if this is not the last copy of the function, do not invoke it. Multiple
  112902                 : ** copies of a single function are created when create_function() is called
  112903                 : ** with SQLITE_ANY as the encoding.
  112904                 : */
  112905          158524 : static void functionDestroy(sqlite3 *db, FuncDef *p){
  112906          158524 :   FuncDestructor *pDestructor = p->pDestructor;
  112907          158524 :   if( pDestructor ){
  112908               0 :     pDestructor->nRef--;
  112909               0 :     if( pDestructor->nRef==0 ){
  112910               0 :       pDestructor->xDestroy(pDestructor->pUserData);
  112911               0 :       sqlite3DbFree(db, pDestructor);
  112912                 :     }
  112913                 :   }
  112914          158524 : }
  112915                 : 
  112916                 : /*
  112917                 : ** Close an existing SQLite database
  112918                 : */
  112919            3276 : SQLITE_API int sqlite3_close(sqlite3 *db){
  112920                 :   HashElem *i;                    /* Hash table iterator */
  112921                 :   int j;
  112922                 : 
  112923            3276 :   if( !db ){
  112924               0 :     return SQLITE_OK;
  112925                 :   }
  112926            3276 :   if( !sqlite3SafetyCheckSickOrOk(db) ){
  112927               0 :     return SQLITE_MISUSE_BKPT;
  112928                 :   }
  112929            3277 :   sqlite3_mutex_enter(db->mutex);
  112930                 : 
  112931                 :   /* Force xDestroy calls on all virtual tables */
  112932            3277 :   sqlite3ResetInternalSchema(db, -1);
  112933                 : 
  112934                 :   /* If a transaction is open, the ResetInternalSchema() call above
  112935                 :   ** will not have called the xDisconnect() method on any virtual
  112936                 :   ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
  112937                 :   ** call will do so. We need to do this before the check for active
  112938                 :   ** SQL statements below, as the v-table implementation may be storing
  112939                 :   ** some prepared statements internally.
  112940                 :   */
  112941            3277 :   sqlite3VtabRollback(db);
  112942                 : 
  112943                 :   /* If there are any outstanding VMs, return SQLITE_BUSY. */
  112944            3277 :   if( db->pVdbe ){
  112945               0 :     sqlite3Error(db, SQLITE_BUSY, 
  112946                 :         "unable to close due to unfinalised statements");
  112947               0 :     sqlite3_mutex_leave(db->mutex);
  112948               0 :     return SQLITE_BUSY;
  112949                 :   }
  112950            3277 :   assert( sqlite3SafetyCheckSickOrOk(db) );
  112951                 : 
  112952            9831 :   for(j=0; j<db->nDb; j++){
  112953            6554 :     Btree *pBt = db->aDb[j].pBt;
  112954            6554 :     if( pBt && sqlite3BtreeIsInBackup(pBt) ){
  112955               0 :       sqlite3Error(db, SQLITE_BUSY, 
  112956                 :           "unable to close due to unfinished backup operation");
  112957               0 :       sqlite3_mutex_leave(db->mutex);
  112958               0 :       return SQLITE_BUSY;
  112959                 :     }
  112960                 :   }
  112961                 : 
  112962                 :   /* Free any outstanding Savepoint structures. */
  112963            3277 :   sqlite3CloseSavepoints(db);
  112964                 : 
  112965            9831 :   for(j=0; j<db->nDb; j++){
  112966            6554 :     struct Db *pDb = &db->aDb[j];
  112967            6554 :     if( pDb->pBt ){
  112968            3660 :       sqlite3BtreeClose(pDb->pBt);
  112969            3660 :       pDb->pBt = 0;
  112970            3660 :       if( j!=1 ){
  112971            3277 :         pDb->pSchema = 0;
  112972                 :       }
  112973                 :     }
  112974                 :   }
  112975            3277 :   sqlite3ResetInternalSchema(db, -1);
  112976                 : 
  112977                 :   /* Tell the code in notify.c that the connection no longer holds any
  112978                 :   ** locks and does not require any further unlock-notify callbacks.
  112979                 :   */
  112980            3277 :   sqlite3ConnectionClosed(db);
  112981                 : 
  112982            3277 :   assert( db->nDb<=2 );
  112983            3277 :   assert( db->aDb==db->aDbStatic );
  112984           78648 :   for(j=0; j<ArraySize(db->aFunc.a); j++){
  112985                 :     FuncDef *pNext, *pHash, *p;
  112986          110143 :     for(p=db->aFunc.a[j]; p; p=pHash){
  112987           34772 :       pHash = p->pHash;
  112988          148787 :       while( p ){
  112989           79243 :         functionDestroy(db, p);
  112990           79243 :         pNext = p->pNext;
  112991           79243 :         sqlite3DbFree(db, p);
  112992           79243 :         p = pNext;
  112993                 :       }
  112994                 :     }
  112995                 :   }
  112996           26176 :   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
  112997           22899 :     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
  112998                 :     /* Invoke any destructors registered for collation sequence user data. */
  112999           91596 :     for(j=0; j<3; j++){
  113000           68697 :       if( pColl[j].xDel ){
  113001               0 :         pColl[j].xDel(pColl[j].pUser);
  113002                 :       }
  113003                 :     }
  113004           22899 :     sqlite3DbFree(db, pColl);
  113005                 :   }
  113006            3277 :   sqlite3HashClear(&db->aCollSeq);
  113007                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  113008           13184 :   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
  113009            9907 :     Module *pMod = (Module *)sqliteHashData(i);
  113010            9907 :     if( pMod->xDestroy ){
  113011            3277 :       pMod->xDestroy(pMod->pAux);
  113012                 :     }
  113013            9907 :     sqlite3DbFree(db, pMod);
  113014                 :   }
  113015            3277 :   sqlite3HashClear(&db->aModule);
  113016                 : #endif
  113017                 : 
  113018            3277 :   sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
  113019            3277 :   if( db->pErr ){
  113020            3277 :     sqlite3ValueFree(db->pErr);
  113021                 :   }
  113022            3277 :   sqlite3CloseExtensions(db);
  113023                 : 
  113024            3277 :   db->magic = SQLITE_MAGIC_ERROR;
  113025                 : 
  113026                 :   /* The temp-database schema is allocated differently from the other schema
  113027                 :   ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
  113028                 :   ** So it needs to be freed here. Todo: Why not roll the temp schema into
  113029                 :   ** the same sqliteMalloc() as the one that allocates the database 
  113030                 :   ** structure?
  113031                 :   */
  113032            3277 :   sqlite3DbFree(db, db->aDb[1].pSchema);
  113033            3277 :   sqlite3_mutex_leave(db->mutex);
  113034            3277 :   db->magic = SQLITE_MAGIC_CLOSED;
  113035            3277 :   sqlite3_mutex_free(db->mutex);
  113036            3277 :   assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
  113037            3277 :   if( db->lookaside.bMalloced ){
  113038            3277 :     sqlite3_free(db->lookaside.pStart);
  113039                 :   }
  113040            3277 :   sqlite3_free(db);
  113041            3277 :   return SQLITE_OK;
  113042                 : }
  113043                 : 
  113044                 : /*
  113045                 : ** Rollback all database files.
  113046                 : */
  113047             365 : SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){
  113048                 :   int i;
  113049             365 :   int inTrans = 0;
  113050             365 :   assert( sqlite3_mutex_held(db->mutex) );
  113051             365 :   sqlite3BeginBenignMalloc();
  113052            1095 :   for(i=0; i<db->nDb; i++){
  113053             730 :     if( db->aDb[i].pBt ){
  113054             384 :       if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
  113055             327 :         inTrans = 1;
  113056                 :       }
  113057             384 :       sqlite3BtreeRollback(db->aDb[i].pBt);
  113058             384 :       db->aDb[i].inTrans = 0;
  113059                 :     }
  113060                 :   }
  113061             365 :   sqlite3VtabRollback(db);
  113062             365 :   sqlite3EndBenignMalloc();
  113063                 : 
  113064             365 :   if( db->flags&SQLITE_InternChanges ){
  113065               4 :     sqlite3ExpirePreparedStatements(db);
  113066               4 :     sqlite3ResetInternalSchema(db, -1);
  113067                 :   }
  113068                 : 
  113069                 :   /* Any deferred constraint violations have now been resolved. */
  113070             365 :   db->nDeferredCons = 0;
  113071                 : 
  113072                 :   /* If one has been configured, invoke the rollback-hook callback */
  113073             365 :   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
  113074               0 :     db->xRollbackCallback(db->pRollbackArg);
  113075                 :   }
  113076             365 : }
  113077                 : 
  113078                 : /*
  113079                 : ** Return a static string that describes the kind of error specified in the
  113080                 : ** argument.
  113081                 : */
  113082              24 : SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
  113083                 :   static const char* const aMsg[] = {
  113084                 :     /* SQLITE_OK          */ "not an error",
  113085                 :     /* SQLITE_ERROR       */ "SQL logic error or missing database",
  113086                 :     /* SQLITE_INTERNAL    */ 0,
  113087                 :     /* SQLITE_PERM        */ "access permission denied",
  113088                 :     /* SQLITE_ABORT       */ "callback requested query abort",
  113089                 :     /* SQLITE_BUSY        */ "database is locked",
  113090                 :     /* SQLITE_LOCKED      */ "database table is locked",
  113091                 :     /* SQLITE_NOMEM       */ "out of memory",
  113092                 :     /* SQLITE_READONLY    */ "attempt to write a readonly database",
  113093                 :     /* SQLITE_INTERRUPT   */ "interrupted",
  113094                 :     /* SQLITE_IOERR       */ "disk I/O error",
  113095                 :     /* SQLITE_CORRUPT     */ "database disk image is malformed",
  113096                 :     /* SQLITE_NOTFOUND    */ "unknown operation",
  113097                 :     /* SQLITE_FULL        */ "database or disk is full",
  113098                 :     /* SQLITE_CANTOPEN    */ "unable to open database file",
  113099                 :     /* SQLITE_PROTOCOL    */ "locking protocol",
  113100                 :     /* SQLITE_EMPTY       */ "table contains no data",
  113101                 :     /* SQLITE_SCHEMA      */ "database schema has changed",
  113102                 :     /* SQLITE_TOOBIG      */ "string or blob too big",
  113103                 :     /* SQLITE_CONSTRAINT  */ "constraint failed",
  113104                 :     /* SQLITE_MISMATCH    */ "datatype mismatch",
  113105                 :     /* SQLITE_MISUSE      */ "library routine called out of sequence",
  113106                 :     /* SQLITE_NOLFS       */ "large file support is disabled",
  113107                 :     /* SQLITE_AUTH        */ "authorization denied",
  113108                 :     /* SQLITE_FORMAT      */ "auxiliary database format error",
  113109                 :     /* SQLITE_RANGE       */ "bind or column index out of range",
  113110                 :     /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
  113111                 :   };
  113112              24 :   rc &= 0xff;
  113113              24 :   if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
  113114              24 :     return aMsg[rc];
  113115                 :   }else{
  113116               0 :     return "unknown error";
  113117                 :   }
  113118                 : }
  113119                 : 
  113120                 : /*
  113121                 : ** This routine implements a busy callback that sleeps and tries
  113122                 : ** again until a timeout value is reached.  The timeout value is
  113123                 : ** an integer number of milliseconds passed in as the first
  113124                 : ** argument.
  113125                 : */
  113126               0 : static int sqliteDefaultBusyCallback(
  113127                 :  void *ptr,               /* Database connection */
  113128                 :  int count                /* Number of times table has been busy */
  113129                 : ){
  113130                 : #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
  113131                 :   static const u8 delays[] =
  113132                 :      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
  113133                 :   static const u8 totals[] =
  113134                 :      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
  113135                 : # define NDELAY ArraySize(delays)
  113136                 :   sqlite3 *db = (sqlite3 *)ptr;
  113137                 :   int timeout = db->busyTimeout;
  113138                 :   int delay, prior;
  113139                 : 
  113140                 :   assert( count>=0 );
  113141                 :   if( count < NDELAY ){
  113142                 :     delay = delays[count];
  113143                 :     prior = totals[count];
  113144                 :   }else{
  113145                 :     delay = delays[NDELAY-1];
  113146                 :     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
  113147                 :   }
  113148                 :   if( prior + delay > timeout ){
  113149                 :     delay = timeout - prior;
  113150                 :     if( delay<=0 ) return 0;
  113151                 :   }
  113152                 :   sqlite3OsSleep(db->pVfs, delay*1000);
  113153                 :   return 1;
  113154                 : #else
  113155               0 :   sqlite3 *db = (sqlite3 *)ptr;
  113156               0 :   int timeout = ((sqlite3 *)ptr)->busyTimeout;
  113157               0 :   if( (count+1)*1000 > timeout ){
  113158               0 :     return 0;
  113159                 :   }
  113160               0 :   sqlite3OsSleep(db->pVfs, 1000000);
  113161               0 :   return 1;
  113162                 : #endif
  113163                 : }
  113164                 : 
  113165                 : /*
  113166                 : ** Invoke the given busy handler.
  113167                 : **
  113168                 : ** This routine is called when an operation failed with a lock.
  113169                 : ** If this routine returns non-zero, the lock is retried.  If it
  113170                 : ** returns 0, the operation aborts with an SQLITE_BUSY error.
  113171                 : */
  113172               2 : SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
  113173                 :   int rc;
  113174               2 :   if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
  113175               0 :   rc = p->xFunc(p->pArg, p->nBusy);
  113176               0 :   if( rc==0 ){
  113177               0 :     p->nBusy = -1;
  113178                 :   }else{
  113179               0 :     p->nBusy++;
  113180                 :   }
  113181               0 :   return rc; 
  113182                 : }
  113183                 : 
  113184                 : /*
  113185                 : ** This routine sets the busy callback for an Sqlite database to the
  113186                 : ** given callback function with the given argument.
  113187                 : */
  113188               0 : SQLITE_API int sqlite3_busy_handler(
  113189                 :   sqlite3 *db,
  113190                 :   int (*xBusy)(void*,int),
  113191                 :   void *pArg
  113192                 : ){
  113193               0 :   sqlite3_mutex_enter(db->mutex);
  113194               0 :   db->busyHandler.xFunc = xBusy;
  113195               0 :   db->busyHandler.pArg = pArg;
  113196               0 :   db->busyHandler.nBusy = 0;
  113197               0 :   sqlite3_mutex_leave(db->mutex);
  113198               0 :   return SQLITE_OK;
  113199                 : }
  113200                 : 
  113201                 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  113202                 : /*
  113203                 : ** This routine sets the progress callback for an Sqlite database to the
  113204                 : ** given callback function with the given argument. The progress callback will
  113205                 : ** be invoked every nOps opcodes.
  113206                 : */
  113207            9346 : SQLITE_API void sqlite3_progress_handler(
  113208                 :   sqlite3 *db, 
  113209                 :   int nOps,
  113210                 :   int (*xProgress)(void*), 
  113211                 :   void *pArg
  113212                 : ){
  113213            9346 :   sqlite3_mutex_enter(db->mutex);
  113214            9346 :   if( nOps>0 ){
  113215            4674 :     db->xProgress = xProgress;
  113216            4674 :     db->nProgressOps = nOps;
  113217            4674 :     db->pProgressArg = pArg;
  113218                 :   }else{
  113219            4672 :     db->xProgress = 0;
  113220            4672 :     db->nProgressOps = 0;
  113221            4672 :     db->pProgressArg = 0;
  113222                 :   }
  113223            9346 :   sqlite3_mutex_leave(db->mutex);
  113224            9346 : }
  113225                 : #endif
  113226                 : 
  113227                 : 
  113228                 : /*
  113229                 : ** This routine installs a default busy handler that waits for the
  113230                 : ** specified number of milliseconds before returning 0.
  113231                 : */
  113232               0 : SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
  113233               0 :   if( ms>0 ){
  113234               0 :     db->busyTimeout = ms;
  113235               0 :     sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
  113236                 :   }else{
  113237               0 :     sqlite3_busy_handler(db, 0, 0);
  113238                 :   }
  113239               0 :   return SQLITE_OK;
  113240                 : }
  113241                 : 
  113242                 : /*
  113243                 : ** Cause any pending operation to stop at its earliest opportunity.
  113244                 : */
  113245               0 : SQLITE_API void sqlite3_interrupt(sqlite3 *db){
  113246               0 :   db->u1.isInterrupted = 1;
  113247               0 : }
  113248                 : 
  113249                 : 
  113250                 : /*
  113251                 : ** This function is exactly the same as sqlite3_create_function(), except
  113252                 : ** that it is designed to be called by internal code. The difference is
  113253                 : ** that if a malloc() fails in sqlite3_create_function(), an error code
  113254                 : ** is returned and the mallocFailed flag cleared. 
  113255                 : */
  113256           79281 : SQLITE_PRIVATE int sqlite3CreateFunc(
  113257                 :   sqlite3 *db,
  113258                 :   const char *zFunctionName,
  113259                 :   int nArg,
  113260                 :   int enc,
  113261                 :   void *pUserData,
  113262                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
  113263                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
  113264                 :   void (*xFinal)(sqlite3_context*),
  113265                 :   FuncDestructor *pDestructor
  113266                 : ){
  113267                 :   FuncDef *p;
  113268                 :   int nName;
  113269                 : 
  113270           79281 :   assert( sqlite3_mutex_held(db->mutex) );
  113271           79281 :   if( zFunctionName==0 ||
  113272           78120 :       (xFunc && (xFinal || xStep)) || 
  113273            1161 :       (!xFunc && (xFinal && !xStep)) ||
  113274            1161 :       (!xFunc && (!xFinal && xStep)) ||
  113275           79281 :       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
  113276                 :       (255<(nName = sqlite3Strlen30( zFunctionName))) ){
  113277               0 :     return SQLITE_MISUSE_BKPT;
  113278                 :   }
  113279                 :   
  113280                 : #ifndef SQLITE_OMIT_UTF16
  113281                 :   /* If SQLITE_UTF16 is specified as the encoding type, transform this
  113282                 :   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
  113283                 :   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
  113284                 :   **
  113285                 :   ** If SQLITE_ANY is specified, add three versions of the function
  113286                 :   ** to the hash table.
  113287                 :   */
  113288           79281 :   if( enc==SQLITE_UTF16 ){
  113289           16335 :     enc = SQLITE_UTF16NATIVE;
  113290           62946 :   }else if( enc==SQLITE_ANY ){
  113291                 :     int rc;
  113292            8983 :     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
  113293                 :          pUserData, xFunc, xStep, xFinal, pDestructor);
  113294            8983 :     if( rc==SQLITE_OK ){
  113295            8983 :       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
  113296                 :           pUserData, xFunc, xStep, xFinal, pDestructor);
  113297                 :     }
  113298            8983 :     if( rc!=SQLITE_OK ){
  113299               0 :       return rc;
  113300                 :     }
  113301            8983 :     enc = SQLITE_UTF16BE;
  113302                 :   }
  113303                 : #else
  113304                 :   enc = SQLITE_UTF8;
  113305                 : #endif
  113306                 :   
  113307                 :   /* Check if an existing function is being overridden or deleted. If so,
  113308                 :   ** and there are active VMs, then return SQLITE_BUSY. If a function
  113309                 :   ** is being overridden/deleted but there are no active VMs, allow the
  113310                 :   ** operation to continue but invalidate all precompiled statements.
  113311                 :   */
  113312           79281 :   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
  113313           79281 :   if( p && p->iPrefEnc==enc && p->nArg==nArg ){
  113314              38 :     if( db->activeVdbeCnt ){
  113315               0 :       sqlite3Error(db, SQLITE_BUSY, 
  113316                 :         "unable to delete/modify user-function due to active statements");
  113317               0 :       assert( !db->mallocFailed );
  113318               0 :       return SQLITE_BUSY;
  113319                 :     }else{
  113320              38 :       sqlite3ExpirePreparedStatements(db);
  113321                 :     }
  113322                 :   }
  113323                 : 
  113324           79281 :   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
  113325           79281 :   assert(p || db->mallocFailed);
  113326           79281 :   if( !p ){
  113327               0 :     return SQLITE_NOMEM;
  113328                 :   }
  113329                 : 
  113330                 :   /* If an older version of the function with a configured destructor is
  113331                 :   ** being replaced invoke the destructor function here. */
  113332           79281 :   functionDestroy(db, p);
  113333                 : 
  113334           79281 :   if( pDestructor ){
  113335               0 :     pDestructor->nRef++;
  113336                 :   }
  113337           79281 :   p->pDestructor = pDestructor;
  113338           79281 :   p->flags = 0;
  113339           79281 :   p->xFunc = xFunc;
  113340           79281 :   p->xStep = xStep;
  113341           79281 :   p->xFinalize = xFinal;
  113342           79281 :   p->pUserData = pUserData;
  113343           79281 :   p->nArg = (u16)nArg;
  113344           79281 :   return SQLITE_OK;
  113345                 : }
  113346                 : 
  113347                 : /*
  113348                 : ** Create new user functions.
  113349                 : */
  113350           41653 : SQLITE_API int sqlite3_create_function(
  113351                 :   sqlite3 *db,
  113352                 :   const char *zFunc,
  113353                 :   int nArg,
  113354                 :   int enc,
  113355                 :   void *p,
  113356                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
  113357                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
  113358                 :   void (*xFinal)(sqlite3_context*)
  113359                 : ){
  113360           41653 :   return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
  113361                 :                                     xFinal, 0);
  113362                 : }
  113363                 : 
  113364           41653 : SQLITE_API int sqlite3_create_function_v2(
  113365                 :   sqlite3 *db,
  113366                 :   const char *zFunc,
  113367                 :   int nArg,
  113368                 :   int enc,
  113369                 :   void *p,
  113370                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
  113371                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
  113372                 :   void (*xFinal)(sqlite3_context*),
  113373                 :   void (*xDestroy)(void *)
  113374                 : ){
  113375           41653 :   int rc = SQLITE_ERROR;
  113376           41653 :   FuncDestructor *pArg = 0;
  113377           41653 :   sqlite3_mutex_enter(db->mutex);
  113378           41653 :   if( xDestroy ){
  113379               0 :     pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
  113380               0 :     if( !pArg ){
  113381               0 :       xDestroy(p);
  113382               0 :       goto out;
  113383                 :     }
  113384               0 :     pArg->xDestroy = xDestroy;
  113385               0 :     pArg->pUserData = p;
  113386                 :   }
  113387           41653 :   rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
  113388           41653 :   if( pArg && pArg->nRef==0 ){
  113389               0 :     assert( rc!=SQLITE_OK );
  113390               0 :     xDestroy(p);
  113391               0 :     sqlite3DbFree(db, pArg);
  113392                 :   }
  113393                 : 
  113394                 :  out:
  113395           41653 :   rc = sqlite3ApiExit(db, rc);
  113396           41653 :   sqlite3_mutex_leave(db->mutex);
  113397           41653 :   return rc;
  113398                 : }
  113399                 : 
  113400                 : #ifndef SQLITE_OMIT_UTF16
  113401               0 : SQLITE_API int sqlite3_create_function16(
  113402                 :   sqlite3 *db,
  113403                 :   const void *zFunctionName,
  113404                 :   int nArg,
  113405                 :   int eTextRep,
  113406                 :   void *p,
  113407                 :   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  113408                 :   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  113409                 :   void (*xFinal)(sqlite3_context*)
  113410                 : ){
  113411                 :   int rc;
  113412                 :   char *zFunc8;
  113413               0 :   sqlite3_mutex_enter(db->mutex);
  113414               0 :   assert( !db->mallocFailed );
  113415               0 :   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
  113416               0 :   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
  113417               0 :   sqlite3DbFree(db, zFunc8);
  113418               0 :   rc = sqlite3ApiExit(db, rc);
  113419               0 :   sqlite3_mutex_leave(db->mutex);
  113420               0 :   return rc;
  113421                 : }
  113422                 : #endif
  113423                 : 
  113424                 : 
  113425                 : /*
  113426                 : ** Declare that a function has been overloaded by a virtual table.
  113427                 : **
  113428                 : ** If the function already exists as a regular global function, then
  113429                 : ** this routine is a no-op.  If the function does not exist, then create
  113430                 : ** a new one that always throws a run-time error.  
  113431                 : **
  113432                 : ** When virtual tables intend to provide an overloaded function, they
  113433                 : ** should call this routine to make sure the global function exists.
  113434                 : ** A global function must exist in order for name resolution to work
  113435                 : ** properly.
  113436                 : */
  113437           19662 : SQLITE_API int sqlite3_overload_function(
  113438                 :   sqlite3 *db,
  113439                 :   const char *zName,
  113440                 :   int nArg
  113441                 : ){
  113442           19662 :   int nName = sqlite3Strlen30(zName);
  113443           19662 :   int rc = SQLITE_OK;
  113444           19662 :   sqlite3_mutex_enter(db->mutex);
  113445           19662 :   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
  113446           19662 :     rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
  113447                 :                            0, sqlite3InvalidFunction, 0, 0, 0);
  113448                 :   }
  113449           19662 :   rc = sqlite3ApiExit(db, rc);
  113450           19662 :   sqlite3_mutex_leave(db->mutex);
  113451           19662 :   return rc;
  113452                 : }
  113453                 : 
  113454                 : #ifndef SQLITE_OMIT_TRACE
  113455                 : /*
  113456                 : ** Register a trace function.  The pArg from the previously registered trace
  113457                 : ** is returned.  
  113458                 : **
  113459                 : ** A NULL trace function means that no tracing is executes.  A non-NULL
  113460                 : ** trace is a pointer to a function that is invoked at the start of each
  113461                 : ** SQL statement.
  113462                 : */
  113463            3277 : SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
  113464                 :   void *pOld;
  113465            3277 :   sqlite3_mutex_enter(db->mutex);
  113466            3277 :   pOld = db->pTraceArg;
  113467            3277 :   db->xTrace = xTrace;
  113468            3277 :   db->pTraceArg = pArg;
  113469            3277 :   sqlite3_mutex_leave(db->mutex);
  113470            3277 :   return pOld;
  113471                 : }
  113472                 : /*
  113473                 : ** Register a profile function.  The pArg from the previously registered 
  113474                 : ** profile function is returned.  
  113475                 : **
  113476                 : ** A NULL profile function means that no profiling is executes.  A non-NULL
  113477                 : ** profile is a pointer to a function that is invoked at the conclusion of
  113478                 : ** each SQL statement that is run.
  113479                 : */
  113480               0 : SQLITE_API void *sqlite3_profile(
  113481                 :   sqlite3 *db,
  113482                 :   void (*xProfile)(void*,const char*,sqlite_uint64),
  113483                 :   void *pArg
  113484                 : ){
  113485                 :   void *pOld;
  113486               0 :   sqlite3_mutex_enter(db->mutex);
  113487               0 :   pOld = db->pProfileArg;
  113488               0 :   db->xProfile = xProfile;
  113489               0 :   db->pProfileArg = pArg;
  113490               0 :   sqlite3_mutex_leave(db->mutex);
  113491               0 :   return pOld;
  113492                 : }
  113493                 : #endif /* SQLITE_OMIT_TRACE */
  113494                 : 
  113495                 : /*** EXPERIMENTAL ***
  113496                 : **
  113497                 : ** Register a function to be invoked when a transaction comments.
  113498                 : ** If the invoked function returns non-zero, then the commit becomes a
  113499                 : ** rollback.
  113500                 : */
  113501              28 : SQLITE_API void *sqlite3_commit_hook(
  113502                 :   sqlite3 *db,              /* Attach the hook to this database */
  113503                 :   int (*xCallback)(void*),  /* Function to invoke on each commit */
  113504                 :   void *pArg                /* Argument to the function */
  113505                 : ){
  113506                 :   void *pOld;
  113507              28 :   sqlite3_mutex_enter(db->mutex);
  113508              28 :   pOld = db->pCommitArg;
  113509              28 :   db->xCommitCallback = xCallback;
  113510              28 :   db->pCommitArg = pArg;
  113511              28 :   sqlite3_mutex_leave(db->mutex);
  113512              28 :   return pOld;
  113513                 : }
  113514                 : 
  113515                 : /*
  113516                 : ** Register a callback to be invoked each time a row is updated,
  113517                 : ** inserted or deleted using this database connection.
  113518                 : */
  113519               0 : SQLITE_API void *sqlite3_update_hook(
  113520                 :   sqlite3 *db,              /* Attach the hook to this database */
  113521                 :   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
  113522                 :   void *pArg                /* Argument to the function */
  113523                 : ){
  113524                 :   void *pRet;
  113525               0 :   sqlite3_mutex_enter(db->mutex);
  113526               0 :   pRet = db->pUpdateArg;
  113527               0 :   db->xUpdateCallback = xCallback;
  113528               0 :   db->pUpdateArg = pArg;
  113529               0 :   sqlite3_mutex_leave(db->mutex);
  113530               0 :   return pRet;
  113531                 : }
  113532                 : 
  113533                 : /*
  113534                 : ** Register a callback to be invoked each time a transaction is rolled
  113535                 : ** back by this database connection.
  113536                 : */
  113537               0 : SQLITE_API void *sqlite3_rollback_hook(
  113538                 :   sqlite3 *db,              /* Attach the hook to this database */
  113539                 :   void (*xCallback)(void*), /* Callback function */
  113540                 :   void *pArg                /* Argument to the function */
  113541                 : ){
  113542                 :   void *pRet;
  113543               0 :   sqlite3_mutex_enter(db->mutex);
  113544               0 :   pRet = db->pRollbackArg;
  113545               0 :   db->xRollbackCallback = xCallback;
  113546               0 :   db->pRollbackArg = pArg;
  113547               0 :   sqlite3_mutex_leave(db->mutex);
  113548               0 :   return pRet;
  113549                 : }
  113550                 : 
  113551                 : #ifndef SQLITE_OMIT_WAL
  113552                 : /*
  113553                 : ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
  113554                 : ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
  113555                 : ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
  113556                 : ** wal_autocheckpoint()).
  113557                 : */ 
  113558           25040 : SQLITE_PRIVATE int sqlite3WalDefaultHook(
  113559                 :   void *pClientData,     /* Argument */
  113560                 :   sqlite3 *db,           /* Connection */
  113561                 :   const char *zDb,       /* Database */
  113562                 :   int nFrame             /* Size of WAL */
  113563                 : ){
  113564           25040 :   if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
  113565            5271 :     sqlite3BeginBenignMalloc();
  113566            5271 :     sqlite3_wal_checkpoint(db, zDb);
  113567            5271 :     sqlite3EndBenignMalloc();
  113568                 :   }
  113569           25040 :   return SQLITE_OK;
  113570                 : }
  113571                 : #endif /* SQLITE_OMIT_WAL */
  113572                 : 
  113573                 : /*
  113574                 : ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
  113575                 : ** a database after committing a transaction if there are nFrame or
  113576                 : ** more frames in the log file. Passing zero or a negative value as the
  113577                 : ** nFrame parameter disables automatic checkpoints entirely.
  113578                 : **
  113579                 : ** The callback registered by this function replaces any existing callback
  113580                 : ** registered using sqlite3_wal_hook(). Likewise, registering a callback
  113581                 : ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
  113582                 : ** configured by this function.
  113583                 : */
  113584            3801 : SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
  113585                 : #ifdef SQLITE_OMIT_WAL
  113586                 :   UNUSED_PARAMETER(db);
  113587                 :   UNUSED_PARAMETER(nFrame);
  113588                 : #else
  113589            3801 :   if( nFrame>0 ){
  113590            3801 :     sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
  113591                 :   }else{
  113592               0 :     sqlite3_wal_hook(db, 0, 0);
  113593                 :   }
  113594                 : #endif
  113595            3801 :   return SQLITE_OK;
  113596                 : }
  113597                 : 
  113598                 : /*
  113599                 : ** Register a callback to be invoked each time a transaction is written
  113600                 : ** into the write-ahead-log by this database connection.
  113601                 : */
  113602            3801 : SQLITE_API void *sqlite3_wal_hook(
  113603                 :   sqlite3 *db,                    /* Attach the hook to this db handle */
  113604                 :   int(*xCallback)(void *, sqlite3*, const char*, int),
  113605                 :   void *pArg                      /* First argument passed to xCallback() */
  113606                 : ){
  113607                 : #ifndef SQLITE_OMIT_WAL
  113608                 :   void *pRet;
  113609            3801 :   sqlite3_mutex_enter(db->mutex);
  113610            3801 :   pRet = db->pWalArg;
  113611            3801 :   db->xWalCallback = xCallback;
  113612            3801 :   db->pWalArg = pArg;
  113613            3801 :   sqlite3_mutex_leave(db->mutex);
  113614            3801 :   return pRet;
  113615                 : #else
  113616                 :   return 0;
  113617                 : #endif
  113618                 : }
  113619                 : 
  113620                 : /*
  113621                 : ** Checkpoint database zDb.
  113622                 : */
  113623            5271 : SQLITE_API int sqlite3_wal_checkpoint_v2(
  113624                 :   sqlite3 *db,                    /* Database handle */
  113625                 :   const char *zDb,                /* Name of attached database (or NULL) */
  113626                 :   int eMode,                      /* SQLITE_CHECKPOINT_* value */
  113627                 :   int *pnLog,                     /* OUT: Size of WAL log in frames */
  113628                 :   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
  113629                 : ){
  113630                 : #ifdef SQLITE_OMIT_WAL
  113631                 :   return SQLITE_OK;
  113632                 : #else
  113633                 :   int rc;                         /* Return code */
  113634            5271 :   int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
  113635                 : 
  113636                 :   /* Initialize the output variables to -1 in case an error occurs. */
  113637            5271 :   if( pnLog ) *pnLog = -1;
  113638            5271 :   if( pnCkpt ) *pnCkpt = -1;
  113639                 : 
  113640                 :   assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
  113641                 :   assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
  113642                 :   assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
  113643            5271 :   if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
  113644               0 :     return SQLITE_MISUSE;
  113645                 :   }
  113646                 : 
  113647            5271 :   sqlite3_mutex_enter(db->mutex);
  113648            5271 :   if( zDb && zDb[0] ){
  113649            5271 :     iDb = sqlite3FindDbName(db, zDb);
  113650                 :   }
  113651            5271 :   if( iDb<0 ){
  113652               0 :     rc = SQLITE_ERROR;
  113653               0 :     sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
  113654                 :   }else{
  113655            5271 :     rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
  113656            5271 :     sqlite3Error(db, rc, 0);
  113657                 :   }
  113658            5271 :   rc = sqlite3ApiExit(db, rc);
  113659            5271 :   sqlite3_mutex_leave(db->mutex);
  113660            5271 :   return rc;
  113661                 : #endif
  113662                 : }
  113663                 : 
  113664                 : 
  113665                 : /*
  113666                 : ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
  113667                 : ** to contains a zero-length string, all attached databases are 
  113668                 : ** checkpointed.
  113669                 : */
  113670            5271 : SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
  113671            5271 :   return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
  113672                 : }
  113673                 : 
  113674                 : #ifndef SQLITE_OMIT_WAL
  113675                 : /*
  113676                 : ** Run a checkpoint on database iDb. This is a no-op if database iDb is
  113677                 : ** not currently open in WAL mode.
  113678                 : **
  113679                 : ** If a transaction is open on the database being checkpointed, this 
  113680                 : ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 
  113681                 : ** an error occurs while running the checkpoint, an SQLite error code is 
  113682                 : ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
  113683                 : **
  113684                 : ** The mutex on database handle db should be held by the caller. The mutex
  113685                 : ** associated with the specific b-tree being checkpointed is taken by
  113686                 : ** this function while the checkpoint is running.
  113687                 : **
  113688                 : ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
  113689                 : ** checkpointed. If an error is encountered it is returned immediately -
  113690                 : ** no attempt is made to checkpoint any remaining databases.
  113691                 : **
  113692                 : ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
  113693                 : */
  113694            5536 : SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
  113695            5536 :   int rc = SQLITE_OK;             /* Return code */
  113696                 :   int i;                          /* Used to iterate through attached dbs */
  113697            5536 :   int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
  113698                 : 
  113699            5536 :   assert( sqlite3_mutex_held(db->mutex) );
  113700            5536 :   assert( !pnLog || *pnLog==-1 );
  113701            5536 :   assert( !pnCkpt || *pnCkpt==-1 );
  113702                 : 
  113703           16588 :   for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
  113704           11052 :     if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
  113705            5801 :       rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
  113706            5801 :       pnLog = 0;
  113707            5801 :       pnCkpt = 0;
  113708            5801 :       if( rc==SQLITE_BUSY ){
  113709               0 :         bBusy = 1;
  113710               0 :         rc = SQLITE_OK;
  113711                 :       }
  113712                 :     }
  113713                 :   }
  113714                 : 
  113715            5536 :   return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
  113716                 : }
  113717                 : #endif /* SQLITE_OMIT_WAL */
  113718                 : 
  113719                 : /*
  113720                 : ** This function returns true if main-memory should be used instead of
  113721                 : ** a temporary file for transient pager files and statement journals.
  113722                 : ** The value returned depends on the value of db->temp_store (runtime
  113723                 : ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
  113724                 : ** following table describes the relationship between these two values
  113725                 : ** and this functions return value.
  113726                 : **
  113727                 : **   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
  113728                 : **   -----------------     --------------     ------------------------------
  113729                 : **   0                     any                file      (return 0)
  113730                 : **   1                     1                  file      (return 0)
  113731                 : **   1                     2                  memory    (return 1)
  113732                 : **   1                     0                  file      (return 0)
  113733                 : **   2                     1                  file      (return 0)
  113734                 : **   2                     2                  memory    (return 1)
  113735                 : **   2                     0                  memory    (return 1)
  113736                 : **   3                     any                memory    (return 1)
  113737                 : */
  113738           80200 : SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
  113739                 : #if SQLITE_TEMP_STORE==1
  113740           80200 :   return ( db->temp_store==2 );
  113741                 : #endif
  113742                 : #if SQLITE_TEMP_STORE==2
  113743                 :   return ( db->temp_store!=1 );
  113744                 : #endif
  113745                 : #if SQLITE_TEMP_STORE==3
  113746                 :   return 1;
  113747                 : #endif
  113748                 : #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
  113749                 :   return 0;
  113750                 : #endif
  113751                 : }
  113752                 : 
  113753                 : /*
  113754                 : ** Return UTF-8 encoded English language explanation of the most recent
  113755                 : ** error.
  113756                 : */
  113757             130 : SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
  113758                 :   const char *z;
  113759             130 :   if( !db ){
  113760               0 :     return sqlite3ErrStr(SQLITE_NOMEM);
  113761                 :   }
  113762             130 :   if( !sqlite3SafetyCheckSickOrOk(db) ){
  113763               0 :     return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
  113764                 :   }
  113765             130 :   sqlite3_mutex_enter(db->mutex);
  113766             130 :   if( db->mallocFailed ){
  113767               0 :     z = sqlite3ErrStr(SQLITE_NOMEM);
  113768                 :   }else{
  113769             130 :     z = (char*)sqlite3_value_text(db->pErr);
  113770             130 :     assert( !db->mallocFailed );
  113771             130 :     if( z==0 ){
  113772              10 :       z = sqlite3ErrStr(db->errCode);
  113773                 :     }
  113774                 :   }
  113775             130 :   sqlite3_mutex_leave(db->mutex);
  113776             130 :   return z;
  113777                 : }
  113778                 : 
  113779                 : #ifndef SQLITE_OMIT_UTF16
  113780                 : /*
  113781                 : ** Return UTF-16 encoded English language explanation of the most recent
  113782                 : ** error.
  113783                 : */
  113784               0 : SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
  113785                 :   static const u16 outOfMem[] = {
  113786                 :     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
  113787                 :   };
  113788                 :   static const u16 misuse[] = {
  113789                 :     'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', 
  113790                 :     'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', 
  113791                 :     'c', 'a', 'l', 'l', 'e', 'd', ' ', 
  113792                 :     'o', 'u', 't', ' ', 
  113793                 :     'o', 'f', ' ', 
  113794                 :     's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
  113795                 :   };
  113796                 : 
  113797                 :   const void *z;
  113798               0 :   if( !db ){
  113799               0 :     return (void *)outOfMem;
  113800                 :   }
  113801               0 :   if( !sqlite3SafetyCheckSickOrOk(db) ){
  113802               0 :     return (void *)misuse;
  113803                 :   }
  113804               0 :   sqlite3_mutex_enter(db->mutex);
  113805               0 :   if( db->mallocFailed ){
  113806               0 :     z = (void *)outOfMem;
  113807                 :   }else{
  113808               0 :     z = sqlite3_value_text16(db->pErr);
  113809               0 :     if( z==0 ){
  113810               0 :       sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
  113811                 :            SQLITE_UTF8, SQLITE_STATIC);
  113812               0 :       z = sqlite3_value_text16(db->pErr);
  113813                 :     }
  113814                 :     /* A malloc() may have failed within the call to sqlite3_value_text16()
  113815                 :     ** above. If this is the case, then the db->mallocFailed flag needs to
  113816                 :     ** be cleared before returning. Do this directly, instead of via
  113817                 :     ** sqlite3ApiExit(), to avoid setting the database handle error message.
  113818                 :     */
  113819               0 :     db->mallocFailed = 0;
  113820                 :   }
  113821               0 :   sqlite3_mutex_leave(db->mutex);
  113822               0 :   return z;
  113823                 : }
  113824                 : #endif /* SQLITE_OMIT_UTF16 */
  113825                 : 
  113826                 : /*
  113827                 : ** Return the most recent error code generated by an SQLite routine. If NULL is
  113828                 : ** passed to this function, we assume a malloc() failed during sqlite3_open().
  113829                 : */
  113830            9959 : SQLITE_API int sqlite3_errcode(sqlite3 *db){
  113831            9959 :   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
  113832               0 :     return SQLITE_MISUSE_BKPT;
  113833                 :   }
  113834            9959 :   if( !db || db->mallocFailed ){
  113835               0 :     return SQLITE_NOMEM;
  113836                 :   }
  113837            9959 :   return db->errCode & db->errMask;
  113838                 : }
  113839               0 : SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
  113840               0 :   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
  113841               0 :     return SQLITE_MISUSE_BKPT;
  113842                 :   }
  113843               0 :   if( !db || db->mallocFailed ){
  113844               0 :     return SQLITE_NOMEM;
  113845                 :   }
  113846               0 :   return db->errCode;
  113847                 : }
  113848                 : 
  113849                 : /*
  113850                 : ** Create a new collating function for database "db".  The name is zName
  113851                 : ** and the encoding is enc.
  113852                 : */
  113853           42541 : static int createCollation(
  113854                 :   sqlite3* db,
  113855                 :   const char *zName, 
  113856                 :   u8 enc,
  113857                 :   void* pCtx,
  113858                 :   int(*xCompare)(void*,int,const void*,int,const void*),
  113859                 :   void(*xDel)(void*)
  113860                 : ){
  113861                 :   CollSeq *pColl;
  113862                 :   int enc2;
  113863           42541 :   int nName = sqlite3Strlen30(zName);
  113864                 :   
  113865           42541 :   assert( sqlite3_mutex_held(db->mutex) );
  113866                 : 
  113867                 :   /* If SQLITE_UTF16 is specified as the encoding type, transform this
  113868                 :   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
  113869                 :   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
  113870                 :   */
  113871           42541 :   enc2 = enc;
  113872                 :   testcase( enc2==SQLITE_UTF16 );
  113873                 :   testcase( enc2==SQLITE_UTF16_ALIGNED );
  113874           42541 :   if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
  113875           13068 :     enc2 = SQLITE_UTF16NATIVE;
  113876                 :   }
  113877           42541 :   if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
  113878               0 :     return SQLITE_MISUSE_BKPT;
  113879                 :   }
  113880                 : 
  113881                 :   /* Check if this call is removing or replacing an existing collation 
  113882                 :   ** sequence. If so, and there are active VMs, return busy. If there
  113883                 :   ** are no active VMs, invalidate any pre-compiled statements.
  113884                 :   */
  113885           42541 :   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
  113886           42541 :   if( pColl && pColl->xCmp ){
  113887               0 :     if( db->activeVdbeCnt ){
  113888               0 :       sqlite3Error(db, SQLITE_BUSY, 
  113889                 :         "unable to delete/modify collation sequence due to active statements");
  113890               0 :       return SQLITE_BUSY;
  113891                 :     }
  113892               0 :     sqlite3ExpirePreparedStatements(db);
  113893                 : 
  113894                 :     /* If collation sequence pColl was created directly by a call to
  113895                 :     ** sqlite3_create_collation, and not generated by synthCollSeq(),
  113896                 :     ** then any copies made by synthCollSeq() need to be invalidated.
  113897                 :     ** Also, collation destructor - CollSeq.xDel() - function may need
  113898                 :     ** to be called.
  113899                 :     */ 
  113900               0 :     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
  113901               0 :       CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
  113902                 :       int j;
  113903               0 :       for(j=0; j<3; j++){
  113904               0 :         CollSeq *p = &aColl[j];
  113905               0 :         if( p->enc==pColl->enc ){
  113906               0 :           if( p->xDel ){
  113907               0 :             p->xDel(p->pUser);
  113908                 :           }
  113909               0 :           p->xCmp = 0;
  113910                 :         }
  113911                 :       }
  113912                 :     }
  113913                 :   }
  113914                 : 
  113915           42541 :   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
  113916           42541 :   if( pColl==0 ) return SQLITE_NOMEM;
  113917           42541 :   pColl->xCmp = xCompare;
  113918           42541 :   pColl->pUser = pCtx;
  113919           42541 :   pColl->xDel = xDel;
  113920           42541 :   pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
  113921           42541 :   sqlite3Error(db, SQLITE_OK, 0);
  113922           42541 :   return SQLITE_OK;
  113923                 : }
  113924                 : 
  113925                 : 
  113926                 : /*
  113927                 : ** This array defines hard upper bounds on limit values.  The
  113928                 : ** initializer must be kept in sync with the SQLITE_LIMIT_*
  113929                 : ** #defines in sqlite3.h.
  113930                 : */
  113931                 : static const int aHardLimit[] = {
  113932                 :   SQLITE_MAX_LENGTH,
  113933                 :   SQLITE_MAX_SQL_LENGTH,
  113934                 :   SQLITE_MAX_COLUMN,
  113935                 :   SQLITE_MAX_EXPR_DEPTH,
  113936                 :   SQLITE_MAX_COMPOUND_SELECT,
  113937                 :   SQLITE_MAX_VDBE_OP,
  113938                 :   SQLITE_MAX_FUNCTION_ARG,
  113939                 :   SQLITE_MAX_ATTACHED,
  113940                 :   SQLITE_MAX_LIKE_PATTERN_LENGTH,
  113941                 :   SQLITE_MAX_VARIABLE_NUMBER,
  113942                 :   SQLITE_MAX_TRIGGER_DEPTH,
  113943                 : };
  113944                 : 
  113945                 : /*
  113946                 : ** Make sure the hard limits are set to reasonable values
  113947                 : */
  113948                 : #if SQLITE_MAX_LENGTH<100
  113949                 : # error SQLITE_MAX_LENGTH must be at least 100
  113950                 : #endif
  113951                 : #if SQLITE_MAX_SQL_LENGTH<100
  113952                 : # error SQLITE_MAX_SQL_LENGTH must be at least 100
  113953                 : #endif
  113954                 : #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
  113955                 : # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
  113956                 : #endif
  113957                 : #if SQLITE_MAX_COMPOUND_SELECT<2
  113958                 : # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
  113959                 : #endif
  113960                 : #if SQLITE_MAX_VDBE_OP<40
  113961                 : # error SQLITE_MAX_VDBE_OP must be at least 40
  113962                 : #endif
  113963                 : #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
  113964                 : # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
  113965                 : #endif
  113966                 : #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
  113967                 : # error SQLITE_MAX_ATTACHED must be between 0 and 62
  113968                 : #endif
  113969                 : #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
  113970                 : # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
  113971                 : #endif
  113972                 : #if SQLITE_MAX_COLUMN>32767
  113973                 : # error SQLITE_MAX_COLUMN must not exceed 32767
  113974                 : #endif
  113975                 : #if SQLITE_MAX_TRIGGER_DEPTH<1
  113976                 : # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
  113977                 : #endif
  113978                 : 
  113979                 : 
  113980                 : /*
  113981                 : ** Change the value of a limit.  Report the old value.
  113982                 : ** If an invalid limit index is supplied, report -1.
  113983                 : ** Make no changes but still report the old value if the
  113984                 : ** new limit is negative.
  113985                 : **
  113986                 : ** A new lower limit does not shrink existing constructs.
  113987                 : ** It merely prevents new constructs that exceed the limit
  113988                 : ** from forming.
  113989                 : */
  113990               0 : SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
  113991                 :   int oldLimit;
  113992                 : 
  113993                 : 
  113994                 :   /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
  113995                 :   ** there is a hard upper bound set at compile-time by a C preprocessor
  113996                 :   ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
  113997                 :   ** "_MAX_".)
  113998                 :   */
  113999               0 :   assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
  114000               0 :   assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
  114001               0 :   assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
  114002               0 :   assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
  114003               0 :   assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
  114004               0 :   assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
  114005               0 :   assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
  114006               0 :   assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
  114007               0 :   assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
  114008                 :                                                SQLITE_MAX_LIKE_PATTERN_LENGTH );
  114009               0 :   assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
  114010               0 :   assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
  114011                 :   assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
  114012                 : 
  114013                 : 
  114014               0 :   if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
  114015               0 :     return -1;
  114016                 :   }
  114017               0 :   oldLimit = db->aLimit[limitId];
  114018               0 :   if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
  114019               0 :     if( newLimit>aHardLimit[limitId] ){
  114020               0 :       newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
  114021                 :     }
  114022               0 :     db->aLimit[limitId] = newLimit;
  114023                 :   }
  114024               0 :   return oldLimit;                     /* IMP: R-53341-35419 */
  114025                 : }
  114026                 : 
  114027                 : /*
  114028                 : ** This function is used to parse both URIs and non-URI filenames passed by the
  114029                 : ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
  114030                 : ** URIs specified as part of ATTACH statements.
  114031                 : **
  114032                 : ** The first argument to this function is the name of the VFS to use (or
  114033                 : ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
  114034                 : ** query parameter. The second argument contains the URI (or non-URI filename)
  114035                 : ** itself. When this function is called the *pFlags variable should contain
  114036                 : ** the default flags to open the database handle with. The value stored in
  114037                 : ** *pFlags may be updated before returning if the URI filename contains 
  114038                 : ** "cache=xxx" or "mode=xxx" query parameters.
  114039                 : **
  114040                 : ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
  114041                 : ** the VFS that should be used to open the database file. *pzFile is set to
  114042                 : ** point to a buffer containing the name of the file to open. It is the 
  114043                 : ** responsibility of the caller to eventually call sqlite3_free() to release
  114044                 : ** this buffer.
  114045                 : **
  114046                 : ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
  114047                 : ** may be set to point to a buffer containing an English language error 
  114048                 : ** message. It is the responsibility of the caller to eventually release
  114049                 : ** this buffer by calling sqlite3_free().
  114050                 : */
  114051            3287 : SQLITE_PRIVATE int sqlite3ParseUri(
  114052                 :   const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
  114053                 :   const char *zUri,               /* Nul-terminated URI to parse */
  114054                 :   unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
  114055                 :   sqlite3_vfs **ppVfs,            /* OUT: VFS to use */ 
  114056                 :   char **pzFile,                  /* OUT: Filename component of URI */
  114057                 :   char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
  114058                 : ){
  114059            3287 :   int rc = SQLITE_OK;
  114060            3287 :   unsigned int flags = *pFlags;
  114061            3287 :   const char *zVfs = zDefaultVfs;
  114062                 :   char *zFile;
  114063                 :   char c;
  114064            3287 :   int nUri = sqlite3Strlen30(zUri);
  114065                 : 
  114066            3287 :   assert( *pzErrMsg==0 );
  114067                 : 
  114068            3287 :   if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) 
  114069               0 :    && nUri>=5 && memcmp(zUri, "file:", 5)==0 
  114070               0 :   ){
  114071                 :     char *zOpt;
  114072                 :     int eState;                   /* Parser state when parsing URI */
  114073                 :     int iIn;                      /* Input character index */
  114074               0 :     int iOut = 0;                 /* Output character index */
  114075               0 :     int nByte = nUri+2;           /* Bytes of space to allocate */
  114076                 : 
  114077                 :     /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 
  114078                 :     ** method that there may be extra parameters following the file-name.  */
  114079               0 :     flags |= SQLITE_OPEN_URI;
  114080                 : 
  114081               0 :     for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
  114082               0 :     zFile = sqlite3_malloc(nByte);
  114083               0 :     if( !zFile ) return SQLITE_NOMEM;
  114084                 : 
  114085                 :     /* Discard the scheme and authority segments of the URI. */
  114086               0 :     if( zUri[5]=='/' && zUri[6]=='/' ){
  114087               0 :       iIn = 7;
  114088               0 :       while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
  114089                 : 
  114090               0 :       if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
  114091               0 :         *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", 
  114092                 :             iIn-7, &zUri[7]);
  114093               0 :         rc = SQLITE_ERROR;
  114094               0 :         goto parse_uri_out;
  114095                 :       }
  114096                 :     }else{
  114097               0 :       iIn = 5;
  114098                 :     }
  114099                 : 
  114100                 :     /* Copy the filename and any query parameters into the zFile buffer. 
  114101                 :     ** Decode %HH escape codes along the way. 
  114102                 :     **
  114103                 :     ** Within this loop, variable eState may be set to 0, 1 or 2, depending
  114104                 :     ** on the parsing context. As follows:
  114105                 :     **
  114106                 :     **   0: Parsing file-name.
  114107                 :     **   1: Parsing name section of a name=value query parameter.
  114108                 :     **   2: Parsing value section of a name=value query parameter.
  114109                 :     */
  114110               0 :     eState = 0;
  114111               0 :     while( (c = zUri[iIn])!=0 && c!='#' ){
  114112               0 :       iIn++;
  114113               0 :       if( c=='%' 
  114114               0 :        && sqlite3Isxdigit(zUri[iIn]) 
  114115               0 :        && sqlite3Isxdigit(zUri[iIn+1]) 
  114116               0 :       ){
  114117               0 :         int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
  114118               0 :         octet += sqlite3HexToInt(zUri[iIn++]);
  114119                 : 
  114120               0 :         assert( octet>=0 && octet<256 );
  114121               0 :         if( octet==0 ){
  114122                 :           /* This branch is taken when "%00" appears within the URI. In this
  114123                 :           ** case we ignore all text in the remainder of the path, name or
  114124                 :           ** value currently being parsed. So ignore the current character
  114125                 :           ** and skip to the next "?", "=" or "&", as appropriate. */
  114126               0 :           while( (c = zUri[iIn])!=0 && c!='#' 
  114127               0 :               && (eState!=0 || c!='?')
  114128               0 :               && (eState!=1 || (c!='=' && c!='&'))
  114129               0 :               && (eState!=2 || c!='&')
  114130                 :           ){
  114131               0 :             iIn++;
  114132                 :           }
  114133               0 :           continue;
  114134                 :         }
  114135               0 :         c = octet;
  114136               0 :       }else if( eState==1 && (c=='&' || c=='=') ){
  114137               0 :         if( zFile[iOut-1]==0 ){
  114138                 :           /* An empty option name. Ignore this option altogether. */
  114139               0 :           while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
  114140               0 :           continue;
  114141                 :         }
  114142               0 :         if( c=='&' ){
  114143               0 :           zFile[iOut++] = '\0';
  114144                 :         }else{
  114145               0 :           eState = 2;
  114146                 :         }
  114147               0 :         c = 0;
  114148               0 :       }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
  114149               0 :         c = 0;
  114150               0 :         eState = 1;
  114151                 :       }
  114152               0 :       zFile[iOut++] = c;
  114153                 :     }
  114154               0 :     if( eState==1 ) zFile[iOut++] = '\0';
  114155               0 :     zFile[iOut++] = '\0';
  114156               0 :     zFile[iOut++] = '\0';
  114157                 : 
  114158                 :     /* Check if there were any options specified that should be interpreted 
  114159                 :     ** here. Options that are interpreted here include "vfs" and those that
  114160                 :     ** correspond to flags that may be passed to the sqlite3_open_v2()
  114161                 :     ** method. */
  114162               0 :     zOpt = &zFile[sqlite3Strlen30(zFile)+1];
  114163               0 :     while( zOpt[0] ){
  114164               0 :       int nOpt = sqlite3Strlen30(zOpt);
  114165               0 :       char *zVal = &zOpt[nOpt+1];
  114166               0 :       int nVal = sqlite3Strlen30(zVal);
  114167                 : 
  114168               0 :       if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
  114169               0 :         zVfs = zVal;
  114170                 :       }else{
  114171                 :         struct OpenMode {
  114172                 :           const char *z;
  114173                 :           int mode;
  114174               0 :         } *aMode = 0;
  114175               0 :         char *zModeType = 0;
  114176               0 :         int mask = 0;
  114177               0 :         int limit = 0;
  114178                 : 
  114179               0 :         if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
  114180                 :           static struct OpenMode aCacheMode[] = {
  114181                 :             { "shared",  SQLITE_OPEN_SHAREDCACHE },
  114182                 :             { "private", SQLITE_OPEN_PRIVATECACHE },
  114183                 :             { 0, 0 }
  114184                 :           };
  114185                 : 
  114186               0 :           mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
  114187               0 :           aMode = aCacheMode;
  114188               0 :           limit = mask;
  114189               0 :           zModeType = "cache";
  114190                 :         }
  114191               0 :         if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
  114192                 :           static struct OpenMode aOpenMode[] = {
  114193                 :             { "ro",  SQLITE_OPEN_READONLY },
  114194                 :             { "rw",  SQLITE_OPEN_READWRITE }, 
  114195                 :             { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
  114196                 :             { 0, 0 }
  114197                 :           };
  114198                 : 
  114199               0 :           mask = SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
  114200               0 :           aMode = aOpenMode;
  114201               0 :           limit = mask & flags;
  114202               0 :           zModeType = "access";
  114203                 :         }
  114204                 : 
  114205               0 :         if( aMode ){
  114206                 :           int i;
  114207               0 :           int mode = 0;
  114208               0 :           for(i=0; aMode[i].z; i++){
  114209               0 :             const char *z = aMode[i].z;
  114210               0 :             if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
  114211               0 :               mode = aMode[i].mode;
  114212               0 :               break;
  114213                 :             }
  114214                 :           }
  114215               0 :           if( mode==0 ){
  114216               0 :             *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
  114217               0 :             rc = SQLITE_ERROR;
  114218               0 :             goto parse_uri_out;
  114219                 :           }
  114220               0 :           if( mode>limit ){
  114221               0 :             *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
  114222                 :                                         zModeType, zVal);
  114223               0 :             rc = SQLITE_PERM;
  114224               0 :             goto parse_uri_out;
  114225                 :           }
  114226               0 :           flags = (flags & ~mask) | mode;
  114227                 :         }
  114228                 :       }
  114229                 : 
  114230               0 :       zOpt = &zVal[nVal+1];
  114231                 :     }
  114232                 : 
  114233                 :   }else{
  114234            3287 :     zFile = sqlite3_malloc(nUri+2);
  114235            3287 :     if( !zFile ) return SQLITE_NOMEM;
  114236            3287 :     memcpy(zFile, zUri, nUri);
  114237            3287 :     zFile[nUri] = '\0';
  114238            3287 :     zFile[nUri+1] = '\0';
  114239                 :   }
  114240                 : 
  114241            3287 :   *ppVfs = sqlite3_vfs_find(zVfs);
  114242            3287 :   if( *ppVfs==0 ){
  114243               0 :     *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
  114244               0 :     rc = SQLITE_ERROR;
  114245                 :   }
  114246                 :  parse_uri_out:
  114247            3287 :   if( rc!=SQLITE_OK ){
  114248               0 :     sqlite3_free(zFile);
  114249               0 :     zFile = 0;
  114250                 :   }
  114251            3287 :   *pFlags = flags;
  114252            3287 :   *pzFile = zFile;
  114253            3287 :   return rc;
  114254                 : }
  114255                 : 
  114256                 : 
  114257                 : /*
  114258                 : ** This routine does the work of opening a database on behalf of
  114259                 : ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
  114260                 : ** is UTF-8 encoded.
  114261                 : */
  114262            3281 : static int openDatabase(
  114263                 :   const char *zFilename, /* Database filename UTF-8 encoded */
  114264                 :   sqlite3 **ppDb,        /* OUT: Returned database handle */
  114265                 :   unsigned int flags,    /* Operational flags */
  114266                 :   const char *zVfs       /* Name of the VFS to use */
  114267                 : ){
  114268                 :   sqlite3 *db;                    /* Store allocated handle here */
  114269                 :   int rc;                         /* Return code */
  114270                 :   int isThreadsafe;               /* True for threadsafe connections */
  114271            3281 :   char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
  114272            3281 :   char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
  114273                 : 
  114274            3281 :   *ppDb = 0;
  114275                 : #ifndef SQLITE_OMIT_AUTOINIT
  114276            3281 :   rc = sqlite3_initialize();
  114277            3281 :   if( rc ) return rc;
  114278                 : #endif
  114279                 : 
  114280                 :   /* Only allow sensible combinations of bits in the flags argument.  
  114281                 :   ** Throw an error if any non-sense combination is used.  If we
  114282                 :   ** do not block illegal combinations here, it could trigger
  114283                 :   ** assert() statements in deeper layers.  Sensible combinations
  114284                 :   ** are:
  114285                 :   **
  114286                 :   **  1:  SQLITE_OPEN_READONLY
  114287                 :   **  2:  SQLITE_OPEN_READWRITE
  114288                 :   **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
  114289                 :   */
  114290                 :   assert( SQLITE_OPEN_READONLY  == 0x01 );
  114291                 :   assert( SQLITE_OPEN_READWRITE == 0x02 );
  114292                 :   assert( SQLITE_OPEN_CREATE    == 0x04 );
  114293                 :   testcase( (1<<(flags&7))==0x02 ); /* READONLY */
  114294                 :   testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
  114295                 :   testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
  114296            3281 :   if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
  114297                 : 
  114298            3281 :   if( sqlite3GlobalConfig.bCoreMutex==0 ){
  114299               0 :     isThreadsafe = 0;
  114300            3281 :   }else if( flags & SQLITE_OPEN_NOMUTEX ){
  114301               0 :     isThreadsafe = 0;
  114302            3281 :   }else if( flags & SQLITE_OPEN_FULLMUTEX ){
  114303               0 :     isThreadsafe = 1;
  114304                 :   }else{
  114305            3281 :     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
  114306                 :   }
  114307            3281 :   if( flags & SQLITE_OPEN_PRIVATECACHE ){
  114308            1776 :     flags &= ~SQLITE_OPEN_SHAREDCACHE;
  114309            1505 :   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
  114310               0 :     flags |= SQLITE_OPEN_SHAREDCACHE;
  114311                 :   }
  114312                 : 
  114313                 :   /* Remove harmful bits from the flags parameter
  114314                 :   **
  114315                 :   ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
  114316                 :   ** dealt with in the previous code block.  Besides these, the only
  114317                 :   ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
  114318                 :   ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
  114319                 :   ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
  114320                 :   ** off all other flags.
  114321                 :   */
  114322            3281 :   flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
  114323                 :                SQLITE_OPEN_EXCLUSIVE |
  114324                 :                SQLITE_OPEN_MAIN_DB |
  114325                 :                SQLITE_OPEN_TEMP_DB | 
  114326                 :                SQLITE_OPEN_TRANSIENT_DB | 
  114327                 :                SQLITE_OPEN_MAIN_JOURNAL | 
  114328                 :                SQLITE_OPEN_TEMP_JOURNAL | 
  114329                 :                SQLITE_OPEN_SUBJOURNAL | 
  114330                 :                SQLITE_OPEN_MASTER_JOURNAL |
  114331                 :                SQLITE_OPEN_NOMUTEX |
  114332                 :                SQLITE_OPEN_FULLMUTEX |
  114333                 :                SQLITE_OPEN_WAL
  114334                 :              );
  114335                 : 
  114336                 :   /* Allocate the sqlite data structure */
  114337            3281 :   db = sqlite3MallocZero( sizeof(sqlite3) );
  114338            3281 :   if( db==0 ) goto opendb_out;
  114339            3281 :   if( isThreadsafe ){
  114340            3281 :     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
  114341            3281 :     if( db->mutex==0 ){
  114342               0 :       sqlite3_free(db);
  114343               0 :       db = 0;
  114344               0 :       goto opendb_out;
  114345                 :     }
  114346                 :   }
  114347            3281 :   sqlite3_mutex_enter(db->mutex);
  114348            3281 :   db->errMask = 0xff;
  114349            3281 :   db->nDb = 2;
  114350            3281 :   db->magic = SQLITE_MAGIC_BUSY;
  114351            3281 :   db->aDb = db->aDbStatic;
  114352                 : 
  114353                 :   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
  114354            3281 :   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
  114355            3281 :   db->autoCommit = 1;
  114356            3281 :   db->nextAutovac = -1;
  114357            3281 :   db->nextPagesize = 0;
  114358            3281 :   db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
  114359                 : #if SQLITE_DEFAULT_FILE_FORMAT<4
  114360                 :                  | SQLITE_LegacyFileFmt
  114361                 : #endif
  114362                 : #ifdef SQLITE_ENABLE_LOAD_EXTENSION
  114363                 :                  | SQLITE_LoadExtension
  114364                 : #endif
  114365                 : #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  114366                 :                  | SQLITE_RecTriggers
  114367                 : #endif
  114368                 : #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
  114369                 :                  | SQLITE_ForeignKeys
  114370                 : #endif
  114371                 :       ;
  114372            3281 :   sqlite3HashInit(&db->aCollSeq);
  114373                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
  114374            3281 :   sqlite3HashInit(&db->aModule);
  114375                 : #endif
  114376                 : 
  114377                 :   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
  114378                 :   ** and UTF-16, so add a version for each to avoid any unnecessary
  114379                 :   ** conversions. The only error that can occur here is a malloc() failure.
  114380                 :   */
  114381            3281 :   createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
  114382            3281 :   createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
  114383            3281 :   createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
  114384            3281 :   createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
  114385            3281 :   if( db->mallocFailed ){
  114386               0 :     goto opendb_out;
  114387                 :   }
  114388            3281 :   db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
  114389            3281 :   assert( db->pDfltColl!=0 );
  114390                 : 
  114391                 :   /* Also add a UTF-8 case-insensitive collation sequence. */
  114392            3281 :   createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
  114393                 : 
  114394                 :   /* Parse the filename/URI argument. */
  114395            3281 :   db->openFlags = flags;
  114396            3281 :   rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
  114397            3281 :   if( rc!=SQLITE_OK ){
  114398               0 :     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
  114399               0 :     sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
  114400               0 :     sqlite3_free(zErrMsg);
  114401               0 :     goto opendb_out;
  114402                 :   }
  114403                 : 
  114404                 :   /* Open the backend database driver */
  114405            3281 :   rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
  114406            3281 :                         flags | SQLITE_OPEN_MAIN_DB);
  114407            3281 :   if( rc!=SQLITE_OK ){
  114408               4 :     if( rc==SQLITE_IOERR_NOMEM ){
  114409               0 :       rc = SQLITE_NOMEM;
  114410                 :     }
  114411               4 :     sqlite3Error(db, rc, 0);
  114412               4 :     goto opendb_out;
  114413                 :   }
  114414            3277 :   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
  114415            3277 :   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
  114416                 : 
  114417                 : 
  114418                 :   /* The default safety_level for the main database is 'full'; for the temp
  114419                 :   ** database it is 'NONE'. This matches the pager layer defaults.  
  114420                 :   */
  114421            3277 :   db->aDb[0].zName = "main";
  114422            3277 :   db->aDb[0].safety_level = 3;
  114423            3277 :   db->aDb[1].zName = "temp";
  114424            3277 :   db->aDb[1].safety_level = 1;
  114425                 : 
  114426            3277 :   db->magic = SQLITE_MAGIC_OPEN;
  114427            3277 :   if( db->mallocFailed ){
  114428               0 :     goto opendb_out;
  114429                 :   }
  114430                 : 
  114431                 :   /* Register all built-in functions, but do not attempt to read the
  114432                 :   ** database schema yet. This is delayed until the first time the database
  114433                 :   ** is accessed.
  114434                 :   */
  114435            3277 :   sqlite3Error(db, SQLITE_OK, 0);
  114436            3277 :   sqlite3RegisterBuiltinFunctions(db);
  114437                 : 
  114438                 :   /* Load automatic extensions - extensions that have been registered
  114439                 :   ** using the sqlite3_automatic_extension() API.
  114440                 :   */
  114441            3277 :   rc = sqlite3_errcode(db);
  114442            3277 :   if( rc==SQLITE_OK ){
  114443            3277 :     sqlite3AutoLoadExtensions(db);
  114444            3277 :     rc = sqlite3_errcode(db);
  114445            3277 :     if( rc!=SQLITE_OK ){
  114446               0 :       goto opendb_out;
  114447                 :     }
  114448                 :   }
  114449                 : 
  114450                 : #ifdef SQLITE_ENABLE_FTS1
  114451                 :   if( !db->mallocFailed ){
  114452                 :     extern int sqlite3Fts1Init(sqlite3*);
  114453                 :     rc = sqlite3Fts1Init(db);
  114454                 :   }
  114455                 : #endif
  114456                 : 
  114457                 : #ifdef SQLITE_ENABLE_FTS2
  114458                 :   if( !db->mallocFailed && rc==SQLITE_OK ){
  114459                 :     extern int sqlite3Fts2Init(sqlite3*);
  114460                 :     rc = sqlite3Fts2Init(db);
  114461                 :   }
  114462                 : #endif
  114463                 : 
  114464                 : #ifdef SQLITE_ENABLE_FTS3
  114465            3277 :   if( !db->mallocFailed && rc==SQLITE_OK ){
  114466            3277 :     rc = sqlite3Fts3Init(db);
  114467                 :   }
  114468                 : #endif
  114469                 : 
  114470                 : #ifdef SQLITE_ENABLE_ICU
  114471                 :   if( !db->mallocFailed && rc==SQLITE_OK ){
  114472                 :     rc = sqlite3IcuInit(db);
  114473                 :   }
  114474                 : #endif
  114475                 : 
  114476                 : #ifdef SQLITE_ENABLE_RTREE
  114477                 :   if( !db->mallocFailed && rc==SQLITE_OK){
  114478                 :     rc = sqlite3RtreeInit(db);
  114479                 :   }
  114480                 : #endif
  114481                 : 
  114482            3277 :   sqlite3Error(db, rc, 0);
  114483                 : 
  114484                 :   /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  114485                 :   ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  114486                 :   ** mode.  Doing nothing at all also makes NORMAL the default.
  114487                 :   */
  114488                 : #ifdef SQLITE_DEFAULT_LOCKING_MODE
  114489                 :   db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
  114490                 :   sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
  114491                 :                           SQLITE_DEFAULT_LOCKING_MODE);
  114492                 : #endif
  114493                 : 
  114494                 :   /* Enable the lookaside-malloc subsystem */
  114495            3277 :   setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
  114496                 :                         sqlite3GlobalConfig.nLookaside);
  114497                 : 
  114498            3277 :   sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
  114499                 : 
  114500                 : opendb_out:
  114501            3281 :   sqlite3_free(zOpen);
  114502            3281 :   if( db ){
  114503            3281 :     assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
  114504            3281 :     sqlite3_mutex_leave(db->mutex);
  114505                 :   }
  114506            3281 :   rc = sqlite3_errcode(db);
  114507            3281 :   assert( db!=0 || rc==SQLITE_NOMEM );
  114508            3281 :   if( rc==SQLITE_NOMEM ){
  114509               0 :     sqlite3_close(db);
  114510               0 :     db = 0;
  114511            3281 :   }else if( rc!=SQLITE_OK ){
  114512               4 :     db->magic = SQLITE_MAGIC_SICK;
  114513                 :   }
  114514            3281 :   *ppDb = db;
  114515            3281 :   return sqlite3ApiExit(0, rc);
  114516                 : }
  114517                 : 
  114518                 : /*
  114519                 : ** Open a new database handle.
  114520                 : */
  114521               0 : SQLITE_API int sqlite3_open(
  114522                 :   const char *zFilename, 
  114523                 :   sqlite3 **ppDb 
  114524                 : ){
  114525               0 :   return openDatabase(zFilename, ppDb,
  114526                 :                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
  114527                 : }
  114528            3281 : SQLITE_API int sqlite3_open_v2(
  114529                 :   const char *filename,   /* Database filename (UTF-8) */
  114530                 :   sqlite3 **ppDb,         /* OUT: SQLite db handle */
  114531                 :   int flags,              /* Flags */
  114532                 :   const char *zVfs        /* Name of VFS module to use */
  114533                 : ){
  114534            3281 :   return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
  114535                 : }
  114536                 : 
  114537                 : #ifndef SQLITE_OMIT_UTF16
  114538                 : /*
  114539                 : ** Open a new database handle.
  114540                 : */
  114541               0 : SQLITE_API int sqlite3_open16(
  114542                 :   const void *zFilename, 
  114543                 :   sqlite3 **ppDb
  114544                 : ){
  114545                 :   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
  114546                 :   sqlite3_value *pVal;
  114547                 :   int rc;
  114548                 : 
  114549               0 :   assert( zFilename );
  114550               0 :   assert( ppDb );
  114551               0 :   *ppDb = 0;
  114552                 : #ifndef SQLITE_OMIT_AUTOINIT
  114553               0 :   rc = sqlite3_initialize();
  114554               0 :   if( rc ) return rc;
  114555                 : #endif
  114556               0 :   pVal = sqlite3ValueNew(0);
  114557               0 :   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
  114558               0 :   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
  114559               0 :   if( zFilename8 ){
  114560               0 :     rc = openDatabase(zFilename8, ppDb,
  114561                 :                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
  114562               0 :     assert( *ppDb || rc==SQLITE_NOMEM );
  114563               0 :     if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
  114564               0 :       ENC(*ppDb) = SQLITE_UTF16NATIVE;
  114565                 :     }
  114566                 :   }else{
  114567               0 :     rc = SQLITE_NOMEM;
  114568                 :   }
  114569               0 :   sqlite3ValueFree(pVal);
  114570                 : 
  114571               0 :   return sqlite3ApiExit(0, rc);
  114572                 : }
  114573                 : #endif /* SQLITE_OMIT_UTF16 */
  114574                 : 
  114575                 : /*
  114576                 : ** Register a new collation sequence with the database handle db.
  114577                 : */
  114578           26136 : SQLITE_API int sqlite3_create_collation(
  114579                 :   sqlite3* db, 
  114580                 :   const char *zName, 
  114581                 :   int enc, 
  114582                 :   void* pCtx,
  114583                 :   int(*xCompare)(void*,int,const void*,int,const void*)
  114584                 : ){
  114585                 :   int rc;
  114586           26136 :   sqlite3_mutex_enter(db->mutex);
  114587           26136 :   assert( !db->mallocFailed );
  114588           26136 :   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
  114589           26136 :   rc = sqlite3ApiExit(db, rc);
  114590           26136 :   sqlite3_mutex_leave(db->mutex);
  114591           26136 :   return rc;
  114592                 : }
  114593                 : 
  114594                 : /*
  114595                 : ** Register a new collation sequence with the database handle db.
  114596                 : */
  114597               0 : SQLITE_API int sqlite3_create_collation_v2(
  114598                 :   sqlite3* db, 
  114599                 :   const char *zName, 
  114600                 :   int enc, 
  114601                 :   void* pCtx,
  114602                 :   int(*xCompare)(void*,int,const void*,int,const void*),
  114603                 :   void(*xDel)(void*)
  114604                 : ){
  114605                 :   int rc;
  114606               0 :   sqlite3_mutex_enter(db->mutex);
  114607               0 :   assert( !db->mallocFailed );
  114608               0 :   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
  114609               0 :   rc = sqlite3ApiExit(db, rc);
  114610               0 :   sqlite3_mutex_leave(db->mutex);
  114611               0 :   return rc;
  114612                 : }
  114613                 : 
  114614                 : #ifndef SQLITE_OMIT_UTF16
  114615                 : /*
  114616                 : ** Register a new collation sequence with the database handle db.
  114617                 : */
  114618               0 : SQLITE_API int sqlite3_create_collation16(
  114619                 :   sqlite3* db, 
  114620                 :   const void *zName,
  114621                 :   int enc, 
  114622                 :   void* pCtx,
  114623                 :   int(*xCompare)(void*,int,const void*,int,const void*)
  114624                 : ){
  114625               0 :   int rc = SQLITE_OK;
  114626                 :   char *zName8;
  114627               0 :   sqlite3_mutex_enter(db->mutex);
  114628               0 :   assert( !db->mallocFailed );
  114629               0 :   zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
  114630               0 :   if( zName8 ){
  114631               0 :     rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
  114632               0 :     sqlite3DbFree(db, zName8);
  114633                 :   }
  114634               0 :   rc = sqlite3ApiExit(db, rc);
  114635               0 :   sqlite3_mutex_leave(db->mutex);
  114636               0 :   return rc;
  114637                 : }
  114638                 : #endif /* SQLITE_OMIT_UTF16 */
  114639                 : 
  114640                 : /*
  114641                 : ** Register a collation sequence factory callback with the database handle
  114642                 : ** db. Replace any previously installed collation sequence factory.
  114643                 : */
  114644               0 : SQLITE_API int sqlite3_collation_needed(
  114645                 :   sqlite3 *db, 
  114646                 :   void *pCollNeededArg, 
  114647                 :   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
  114648                 : ){
  114649               0 :   sqlite3_mutex_enter(db->mutex);
  114650               0 :   db->xCollNeeded = xCollNeeded;
  114651               0 :   db->xCollNeeded16 = 0;
  114652               0 :   db->pCollNeededArg = pCollNeededArg;
  114653               0 :   sqlite3_mutex_leave(db->mutex);
  114654               0 :   return SQLITE_OK;
  114655                 : }
  114656                 : 
  114657                 : #ifndef SQLITE_OMIT_UTF16
  114658                 : /*
  114659                 : ** Register a collation sequence factory callback with the database handle
  114660                 : ** db. Replace any previously installed collation sequence factory.
  114661                 : */
  114662               0 : SQLITE_API int sqlite3_collation_needed16(
  114663                 :   sqlite3 *db, 
  114664                 :   void *pCollNeededArg, 
  114665                 :   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
  114666                 : ){
  114667               0 :   sqlite3_mutex_enter(db->mutex);
  114668               0 :   db->xCollNeeded = 0;
  114669               0 :   db->xCollNeeded16 = xCollNeeded16;
  114670               0 :   db->pCollNeededArg = pCollNeededArg;
  114671               0 :   sqlite3_mutex_leave(db->mutex);
  114672               0 :   return SQLITE_OK;
  114673                 : }
  114674                 : #endif /* SQLITE_OMIT_UTF16 */
  114675                 : 
  114676                 : #ifndef SQLITE_OMIT_DEPRECATED
  114677                 : /*
  114678                 : ** This function is now an anachronism. It used to be used to recover from a
  114679                 : ** malloc() failure, but SQLite now does this automatically.
  114680                 : */
  114681               0 : SQLITE_API int sqlite3_global_recover(void){
  114682               0 :   return SQLITE_OK;
  114683                 : }
  114684                 : #endif
  114685                 : 
  114686                 : /*
  114687                 : ** Test to see whether or not the database connection is in autocommit
  114688                 : ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
  114689                 : ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
  114690                 : ** by the next COMMIT or ROLLBACK.
  114691                 : **
  114692                 : ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
  114693                 : */
  114694               0 : SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
  114695               0 :   return db->autoCommit;
  114696                 : }
  114697                 : 
  114698                 : /*
  114699                 : ** The following routines are subtitutes for constants SQLITE_CORRUPT,
  114700                 : ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
  114701                 : ** constants.  They server two purposes:
  114702                 : **
  114703                 : **   1.  Serve as a convenient place to set a breakpoint in a debugger
  114704                 : **       to detect when version error conditions occurs.
  114705                 : **
  114706                 : **   2.  Invoke sqlite3_log() to provide the source code location where
  114707                 : **       a low-level error is first detected.
  114708                 : */
  114709               7 : SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
  114710                 :   testcase( sqlite3GlobalConfig.xLog!=0 );
  114711               7 :   sqlite3_log(SQLITE_CORRUPT,
  114712                 :               "database corruption at line %d of [%.10s]",
  114713               7 :               lineno, 20+sqlite3_sourceid());
  114714               7 :   return SQLITE_CORRUPT;
  114715                 : }
  114716               1 : SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
  114717                 :   testcase( sqlite3GlobalConfig.xLog!=0 );
  114718               1 :   sqlite3_log(SQLITE_MISUSE, 
  114719                 :               "misuse at line %d of [%.10s]",
  114720               1 :               lineno, 20+sqlite3_sourceid());
  114721               1 :   return SQLITE_MISUSE;
  114722                 : }
  114723               4 : SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
  114724                 :   testcase( sqlite3GlobalConfig.xLog!=0 );
  114725               4 :   sqlite3_log(SQLITE_CANTOPEN, 
  114726                 :               "cannot open file at line %d of [%.10s]",
  114727               4 :               lineno, 20+sqlite3_sourceid());
  114728               4 :   return SQLITE_CANTOPEN;
  114729                 : }
  114730                 : 
  114731                 : 
  114732                 : #ifndef SQLITE_OMIT_DEPRECATED
  114733                 : /*
  114734                 : ** This is a convenience routine that makes sure that all thread-specific
  114735                 : ** data for this thread has been deallocated.
  114736                 : **
  114737                 : ** SQLite no longer uses thread-specific data so this routine is now a
  114738                 : ** no-op.  It is retained for historical compatibility.
  114739                 : */
  114740               0 : SQLITE_API void sqlite3_thread_cleanup(void){
  114741               0 : }
  114742                 : #endif
  114743                 : 
  114744                 : /*
  114745                 : ** Return meta information about a specific column of a database table.
  114746                 : ** See comment in sqlite3.h (sqlite.h.in) for details.
  114747                 : */
  114748                 : #ifdef SQLITE_ENABLE_COLUMN_METADATA
  114749                 : SQLITE_API int sqlite3_table_column_metadata(
  114750                 :   sqlite3 *db,                /* Connection handle */
  114751                 :   const char *zDbName,        /* Database name or NULL */
  114752                 :   const char *zTableName,     /* Table name */
  114753                 :   const char *zColumnName,    /* Column name */
  114754                 :   char const **pzDataType,    /* OUTPUT: Declared data type */
  114755                 :   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
  114756                 :   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  114757                 :   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  114758                 :   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  114759                 : ){
  114760                 :   int rc;
  114761                 :   char *zErrMsg = 0;
  114762                 :   Table *pTab = 0;
  114763                 :   Column *pCol = 0;
  114764                 :   int iCol;
  114765                 : 
  114766                 :   char const *zDataType = 0;
  114767                 :   char const *zCollSeq = 0;
  114768                 :   int notnull = 0;
  114769                 :   int primarykey = 0;
  114770                 :   int autoinc = 0;
  114771                 : 
  114772                 :   /* Ensure the database schema has been loaded */
  114773                 :   sqlite3_mutex_enter(db->mutex);
  114774                 :   sqlite3BtreeEnterAll(db);
  114775                 :   rc = sqlite3Init(db, &zErrMsg);
  114776                 :   if( SQLITE_OK!=rc ){
  114777                 :     goto error_out;
  114778                 :   }
  114779                 : 
  114780                 :   /* Locate the table in question */
  114781                 :   pTab = sqlite3FindTable(db, zTableName, zDbName);
  114782                 :   if( !pTab || pTab->pSelect ){
  114783                 :     pTab = 0;
  114784                 :     goto error_out;
  114785                 :   }
  114786                 : 
  114787                 :   /* Find the column for which info is requested */
  114788                 :   if( sqlite3IsRowid(zColumnName) ){
  114789                 :     iCol = pTab->iPKey;
  114790                 :     if( iCol>=0 ){
  114791                 :       pCol = &pTab->aCol[iCol];
  114792                 :     }
  114793                 :   }else{
  114794                 :     for(iCol=0; iCol<pTab->nCol; iCol++){
  114795                 :       pCol = &pTab->aCol[iCol];
  114796                 :       if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
  114797                 :         break;
  114798                 :       }
  114799                 :     }
  114800                 :     if( iCol==pTab->nCol ){
  114801                 :       pTab = 0;
  114802                 :       goto error_out;
  114803                 :     }
  114804                 :   }
  114805                 : 
  114806                 :   /* The following block stores the meta information that will be returned
  114807                 :   ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
  114808                 :   ** and autoinc. At this point there are two possibilities:
  114809                 :   ** 
  114810                 :   **     1. The specified column name was rowid", "oid" or "_rowid_" 
  114811                 :   **        and there is no explicitly declared IPK column. 
  114812                 :   **
  114813                 :   **     2. The table is not a view and the column name identified an 
  114814                 :   **        explicitly declared column. Copy meta information from *pCol.
  114815                 :   */ 
  114816                 :   if( pCol ){
  114817                 :     zDataType = pCol->zType;
  114818                 :     zCollSeq = pCol->zColl;
  114819                 :     notnull = pCol->notNull!=0;
  114820                 :     primarykey  = pCol->isPrimKey!=0;
  114821                 :     autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
  114822                 :   }else{
  114823                 :     zDataType = "INTEGER";
  114824                 :     primarykey = 1;
  114825                 :   }
  114826                 :   if( !zCollSeq ){
  114827                 :     zCollSeq = "BINARY";
  114828                 :   }
  114829                 : 
  114830                 : error_out:
  114831                 :   sqlite3BtreeLeaveAll(db);
  114832                 : 
  114833                 :   /* Whether the function call succeeded or failed, set the output parameters
  114834                 :   ** to whatever their local counterparts contain. If an error did occur,
  114835                 :   ** this has the effect of zeroing all output parameters.
  114836                 :   */
  114837                 :   if( pzDataType ) *pzDataType = zDataType;
  114838                 :   if( pzCollSeq ) *pzCollSeq = zCollSeq;
  114839                 :   if( pNotNull ) *pNotNull = notnull;
  114840                 :   if( pPrimaryKey ) *pPrimaryKey = primarykey;
  114841                 :   if( pAutoinc ) *pAutoinc = autoinc;
  114842                 : 
  114843                 :   if( SQLITE_OK==rc && !pTab ){
  114844                 :     sqlite3DbFree(db, zErrMsg);
  114845                 :     zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
  114846                 :         zColumnName);
  114847                 :     rc = SQLITE_ERROR;
  114848                 :   }
  114849                 :   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
  114850                 :   sqlite3DbFree(db, zErrMsg);
  114851                 :   rc = sqlite3ApiExit(db, rc);
  114852                 :   sqlite3_mutex_leave(db->mutex);
  114853                 :   return rc;
  114854                 : }
  114855                 : #endif
  114856                 : 
  114857                 : /*
  114858                 : ** Sleep for a little while.  Return the amount of time slept.
  114859                 : */
  114860               0 : SQLITE_API int sqlite3_sleep(int ms){
  114861                 :   sqlite3_vfs *pVfs;
  114862                 :   int rc;
  114863               0 :   pVfs = sqlite3_vfs_find(0);
  114864               0 :   if( pVfs==0 ) return 0;
  114865                 : 
  114866                 :   /* This function works in milliseconds, but the underlying OsSleep() 
  114867                 :   ** API uses microseconds. Hence the 1000's.
  114868                 :   */
  114869               0 :   rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
  114870               0 :   return rc;
  114871                 : }
  114872                 : 
  114873                 : /*
  114874                 : ** Enable or disable the extended result codes.
  114875                 : */
  114876          542896 : SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
  114877          542896 :   sqlite3_mutex_enter(db->mutex);
  114878          542896 :   db->errMask = onoff ? 0xffffffff : 0xff;
  114879          542896 :   sqlite3_mutex_leave(db->mutex);
  114880          542896 :   return SQLITE_OK;
  114881                 : }
  114882                 : 
  114883                 : /*
  114884                 : ** Invoke the xFileControl method on a particular database.
  114885                 : */
  114886             659 : SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
  114887             659 :   int rc = SQLITE_ERROR;
  114888                 :   int iDb;
  114889             659 :   sqlite3_mutex_enter(db->mutex);
  114890             659 :   if( zDbName==0 ){
  114891             659 :     iDb = 0;
  114892                 :   }else{
  114893               0 :     for(iDb=0; iDb<db->nDb; iDb++){
  114894               0 :       if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
  114895                 :     }
  114896                 :   }
  114897             659 :   if( iDb<db->nDb ){
  114898             659 :     Btree *pBtree = db->aDb[iDb].pBt;
  114899             659 :     if( pBtree ){
  114900                 :       Pager *pPager;
  114901                 :       sqlite3_file *fd;
  114902             659 :       sqlite3BtreeEnter(pBtree);
  114903             659 :       pPager = sqlite3BtreePager(pBtree);
  114904             659 :       assert( pPager!=0 );
  114905             659 :       fd = sqlite3PagerFile(pPager);
  114906             659 :       assert( fd!=0 );
  114907             659 :       if( op==SQLITE_FCNTL_FILE_POINTER ){
  114908               0 :         *(sqlite3_file**)pArg = fd;
  114909               0 :         rc = SQLITE_OK;
  114910             659 :       }else if( fd->pMethods ){
  114911             659 :         rc = sqlite3OsFileControl(fd, op, pArg);
  114912                 :       }else{
  114913               0 :         rc = SQLITE_NOTFOUND;
  114914                 :       }
  114915             659 :       sqlite3BtreeLeave(pBtree);
  114916                 :     }
  114917                 :   }
  114918             659 :   sqlite3_mutex_leave(db->mutex);
  114919             659 :   return rc;   
  114920                 : }
  114921                 : 
  114922                 : /*
  114923                 : ** Interface to the testing logic.
  114924                 : */
  114925               0 : SQLITE_API int sqlite3_test_control(int op, ...){
  114926               0 :   int rc = 0;
  114927                 : #ifndef SQLITE_OMIT_BUILTIN_TEST
  114928                 :   va_list ap;
  114929               0 :   va_start(ap, op);
  114930               0 :   switch( op ){
  114931                 : 
  114932                 :     /*
  114933                 :     ** Save the current state of the PRNG.
  114934                 :     */
  114935                 :     case SQLITE_TESTCTRL_PRNG_SAVE: {
  114936               0 :       sqlite3PrngSaveState();
  114937               0 :       break;
  114938                 :     }
  114939                 : 
  114940                 :     /*
  114941                 :     ** Restore the state of the PRNG to the last state saved using
  114942                 :     ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
  114943                 :     ** this verb acts like PRNG_RESET.
  114944                 :     */
  114945                 :     case SQLITE_TESTCTRL_PRNG_RESTORE: {
  114946               0 :       sqlite3PrngRestoreState();
  114947               0 :       break;
  114948                 :     }
  114949                 : 
  114950                 :     /*
  114951                 :     ** Reset the PRNG back to its uninitialized state.  The next call
  114952                 :     ** to sqlite3_randomness() will reseed the PRNG using a single call
  114953                 :     ** to the xRandomness method of the default VFS.
  114954                 :     */
  114955                 :     case SQLITE_TESTCTRL_PRNG_RESET: {
  114956               0 :       sqlite3PrngResetState();
  114957               0 :       break;
  114958                 :     }
  114959                 : 
  114960                 :     /*
  114961                 :     **  sqlite3_test_control(BITVEC_TEST, size, program)
  114962                 :     **
  114963                 :     ** Run a test against a Bitvec object of size.  The program argument
  114964                 :     ** is an array of integers that defines the test.  Return -1 on a
  114965                 :     ** memory allocation error, 0 on success, or non-zero for an error.
  114966                 :     ** See the sqlite3BitvecBuiltinTest() for additional information.
  114967                 :     */
  114968                 :     case SQLITE_TESTCTRL_BITVEC_TEST: {
  114969               0 :       int sz = va_arg(ap, int);
  114970               0 :       int *aProg = va_arg(ap, int*);
  114971               0 :       rc = sqlite3BitvecBuiltinTest(sz, aProg);
  114972               0 :       break;
  114973                 :     }
  114974                 : 
  114975                 :     /*
  114976                 :     **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
  114977                 :     **
  114978                 :     ** Register hooks to call to indicate which malloc() failures 
  114979                 :     ** are benign.
  114980                 :     */
  114981                 :     case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
  114982                 :       typedef void (*void_function)(void);
  114983                 :       void_function xBenignBegin;
  114984                 :       void_function xBenignEnd;
  114985               0 :       xBenignBegin = va_arg(ap, void_function);
  114986               0 :       xBenignEnd = va_arg(ap, void_function);
  114987               0 :       sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
  114988               0 :       break;
  114989                 :     }
  114990                 : 
  114991                 :     /*
  114992                 :     **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
  114993                 :     **
  114994                 :     ** Set the PENDING byte to the value in the argument, if X>0.
  114995                 :     ** Make no changes if X==0.  Return the value of the pending byte
  114996                 :     ** as it existing before this routine was called.
  114997                 :     **
  114998                 :     ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
  114999                 :     ** an incompatible database file format.  Changing the PENDING byte
  115000                 :     ** while any database connection is open results in undefined and
  115001                 :     ** dileterious behavior.
  115002                 :     */
  115003                 :     case SQLITE_TESTCTRL_PENDING_BYTE: {
  115004               0 :       rc = PENDING_BYTE;
  115005                 : #ifndef SQLITE_OMIT_WSD
  115006                 :       {
  115007               0 :         unsigned int newVal = va_arg(ap, unsigned int);
  115008               0 :         if( newVal ) sqlite3PendingByte = newVal;
  115009                 :       }
  115010                 : #endif
  115011               0 :       break;
  115012                 :     }
  115013                 : 
  115014                 :     /*
  115015                 :     **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
  115016                 :     **
  115017                 :     ** This action provides a run-time test to see whether or not
  115018                 :     ** assert() was enabled at compile-time.  If X is true and assert()
  115019                 :     ** is enabled, then the return value is true.  If X is true and
  115020                 :     ** assert() is disabled, then the return value is zero.  If X is
  115021                 :     ** false and assert() is enabled, then the assertion fires and the
  115022                 :     ** process aborts.  If X is false and assert() is disabled, then the
  115023                 :     ** return value is zero.
  115024                 :     */
  115025                 :     case SQLITE_TESTCTRL_ASSERT: {
  115026               0 :       volatile int x = 0;
  115027               0 :       assert( (x = va_arg(ap,int))!=0 );
  115028               0 :       rc = x;
  115029               0 :       break;
  115030                 :     }
  115031                 : 
  115032                 : 
  115033                 :     /*
  115034                 :     **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
  115035                 :     **
  115036                 :     ** This action provides a run-time test to see how the ALWAYS and
  115037                 :     ** NEVER macros were defined at compile-time.
  115038                 :     **
  115039                 :     ** The return value is ALWAYS(X).  
  115040                 :     **
  115041                 :     ** The recommended test is X==2.  If the return value is 2, that means
  115042                 :     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
  115043                 :     ** default setting.  If the return value is 1, then ALWAYS() is either
  115044                 :     ** hard-coded to true or else it asserts if its argument is false.
  115045                 :     ** The first behavior (hard-coded to true) is the case if
  115046                 :     ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
  115047                 :     ** behavior (assert if the argument to ALWAYS() is false) is the case if
  115048                 :     ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
  115049                 :     **
  115050                 :     ** The run-time test procedure might look something like this:
  115051                 :     **
  115052                 :     **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
  115053                 :     **      // ALWAYS() and NEVER() are no-op pass-through macros
  115054                 :     **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
  115055                 :     **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
  115056                 :     **    }else{
  115057                 :     **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
  115058                 :     **    }
  115059                 :     */
  115060                 :     case SQLITE_TESTCTRL_ALWAYS: {
  115061               0 :       int x = va_arg(ap,int);
  115062               0 :       rc = ALWAYS(x);
  115063               0 :       break;
  115064                 :     }
  115065                 : 
  115066                 :     /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
  115067                 :     **
  115068                 :     ** Set the nReserve size to N for the main database on the database
  115069                 :     ** connection db.
  115070                 :     */
  115071                 :     case SQLITE_TESTCTRL_RESERVE: {
  115072               0 :       sqlite3 *db = va_arg(ap, sqlite3*);
  115073               0 :       int x = va_arg(ap,int);
  115074               0 :       sqlite3_mutex_enter(db->mutex);
  115075               0 :       sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
  115076               0 :       sqlite3_mutex_leave(db->mutex);
  115077               0 :       break;
  115078                 :     }
  115079                 : 
  115080                 :     /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
  115081                 :     **
  115082                 :     ** Enable or disable various optimizations for testing purposes.  The 
  115083                 :     ** argument N is a bitmask of optimizations to be disabled.  For normal
  115084                 :     ** operation N should be 0.  The idea is that a test program (like the
  115085                 :     ** SQL Logic Test or SLT test module) can run the same SQL multiple times
  115086                 :     ** with various optimizations disabled to verify that the same answer
  115087                 :     ** is obtained in every case.
  115088                 :     */
  115089                 :     case SQLITE_TESTCTRL_OPTIMIZATIONS: {
  115090               0 :       sqlite3 *db = va_arg(ap, sqlite3*);
  115091               0 :       int x = va_arg(ap,int);
  115092               0 :       db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
  115093               0 :       break;
  115094                 :     }
  115095                 : 
  115096                 : #ifdef SQLITE_N_KEYWORD
  115097                 :     /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
  115098                 :     **
  115099                 :     ** If zWord is a keyword recognized by the parser, then return the
  115100                 :     ** number of keywords.  Or if zWord is not a keyword, return 0.
  115101                 :     ** 
  115102                 :     ** This test feature is only available in the amalgamation since
  115103                 :     ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
  115104                 :     ** is built using separate source files.
  115105                 :     */
  115106                 :     case SQLITE_TESTCTRL_ISKEYWORD: {
  115107               0 :       const char *zWord = va_arg(ap, const char*);
  115108               0 :       int n = sqlite3Strlen30(zWord);
  115109               0 :       rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
  115110               0 :       break;
  115111                 :     }
  115112                 : #endif 
  115113                 : 
  115114                 :     /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
  115115                 :     **
  115116                 :     ** Pass pFree into sqlite3ScratchFree(). 
  115117                 :     ** If sz>0 then allocate a scratch buffer into pNew.  
  115118                 :     */
  115119                 :     case SQLITE_TESTCTRL_SCRATCHMALLOC: {
  115120                 :       void *pFree, **ppNew;
  115121                 :       int sz;
  115122               0 :       sz = va_arg(ap, int);
  115123               0 :       ppNew = va_arg(ap, void**);
  115124               0 :       pFree = va_arg(ap, void*);
  115125               0 :       if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
  115126               0 :       sqlite3ScratchFree(pFree);
  115127               0 :       break;
  115128                 :     }
  115129                 : 
  115130                 :     /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
  115131                 :     **
  115132                 :     ** If parameter onoff is non-zero, configure the wrappers so that all
  115133                 :     ** subsequent calls to localtime() and variants fail. If onoff is zero,
  115134                 :     ** undo this setting.
  115135                 :     */
  115136                 :     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
  115137               0 :       sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
  115138               0 :       break;
  115139                 :     }
  115140                 : 
  115141                 : #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  115142                 :     /*   sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
  115143                 :     **                        sqlite3_stmt*,const char**);
  115144                 :     **
  115145                 :     ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
  115146                 :     ** a string that describes the optimized parse tree.  This test-control
  115147                 :     ** returns a pointer to that string.
  115148                 :     */
  115149                 :     case SQLITE_TESTCTRL_EXPLAIN_STMT: {
  115150                 :       sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
  115151                 :       const char **pzRet = va_arg(ap, const char**);
  115152                 :       *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
  115153                 :       break;
  115154                 :     }
  115155                 : #endif
  115156                 : 
  115157                 :   }
  115158               0 :   va_end(ap);
  115159                 : #endif /* SQLITE_OMIT_BUILTIN_TEST */
  115160               0 :   return rc;
  115161                 : }
  115162                 : 
  115163                 : /*
  115164                 : ** This is a utility routine, useful to VFS implementations, that checks
  115165                 : ** to see if a database file was a URI that contained a specific query 
  115166                 : ** parameter, and if so obtains the value of the query parameter.
  115167                 : **
  115168                 : ** The zFilename argument is the filename pointer passed into the xOpen()
  115169                 : ** method of a VFS implementation.  The zParam argument is the name of the
  115170                 : ** query parameter we seek.  This routine returns the value of the zParam
  115171                 : ** parameter if it exists.  If the parameter does not exist, this routine
  115172                 : ** returns a NULL pointer.
  115173                 : */
  115174           11393 : SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
  115175           11393 :   if( zFilename==0 ) return 0;
  115176             353 :   zFilename += sqlite3Strlen30(zFilename) + 1;
  115177             706 :   while( zFilename[0] ){
  115178               0 :     int x = strcmp(zFilename, zParam);
  115179               0 :     zFilename += sqlite3Strlen30(zFilename) + 1;
  115180               0 :     if( x==0 ) return zFilename;
  115181               0 :     zFilename += sqlite3Strlen30(zFilename) + 1;
  115182                 :   }
  115183             353 :   return 0;
  115184                 : }
  115185                 : 
  115186                 : /*
  115187                 : ** Return a boolean value for a query parameter.
  115188                 : */
  115189           11393 : SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
  115190           11393 :   const char *z = sqlite3_uri_parameter(zFilename, zParam);
  115191           11393 :   return z ? sqlite3GetBoolean(z) : (bDflt!=0);
  115192                 : }
  115193                 : 
  115194                 : /*
  115195                 : ** Return a 64-bit integer value for a query parameter.
  115196                 : */
  115197               0 : SQLITE_API sqlite3_int64 sqlite3_uri_int64(
  115198                 :   const char *zFilename,    /* Filename as passed to xOpen */
  115199                 :   const char *zParam,       /* URI parameter sought */
  115200                 :   sqlite3_int64 bDflt       /* return if parameter is missing */
  115201                 : ){
  115202               0 :   const char *z = sqlite3_uri_parameter(zFilename, zParam);
  115203                 :   sqlite3_int64 v;
  115204               0 :   if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
  115205               0 :     bDflt = v;
  115206                 :   }
  115207               0 :   return bDflt;
  115208                 : }
  115209                 : 
  115210                 : /*
  115211                 : ** Return the filename of the database associated with a database
  115212                 : ** connection.
  115213                 : */
  115214               0 : SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
  115215                 :   int i;
  115216               0 :   for(i=0; i<db->nDb; i++){
  115217               0 :     if( db->aDb[i].pBt && sqlite3StrICmp(zDbName, db->aDb[i].zName)==0 ){
  115218               0 :       return sqlite3BtreeGetFilename(db->aDb[i].pBt);
  115219                 :     }
  115220                 :   }
  115221               0 :   return 0;
  115222                 : }
  115223                 : 
  115224                 : /************** End of main.c ************************************************/
  115225                 : /************** Begin file notify.c ******************************************/
  115226                 : /*
  115227                 : ** 2009 March 3
  115228                 : **
  115229                 : ** The author disclaims copyright to this source code.  In place of
  115230                 : ** a legal notice, here is a blessing:
  115231                 : **
  115232                 : **    May you do good and not evil.
  115233                 : **    May you find forgiveness for yourself and forgive others.
  115234                 : **    May you share freely, never taking more than you give.
  115235                 : **
  115236                 : *************************************************************************
  115237                 : **
  115238                 : ** This file contains the implementation of the sqlite3_unlock_notify()
  115239                 : ** API method and its associated functionality.
  115240                 : */
  115241                 : 
  115242                 : /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
  115243                 : #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  115244                 : 
  115245                 : /*
  115246                 : ** Public interfaces:
  115247                 : **
  115248                 : **   sqlite3ConnectionBlocked()
  115249                 : **   sqlite3ConnectionUnlocked()
  115250                 : **   sqlite3ConnectionClosed()
  115251                 : **   sqlite3_unlock_notify()
  115252                 : */
  115253                 : 
  115254                 : #define assertMutexHeld() \
  115255                 :   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
  115256                 : 
  115257                 : /*
  115258                 : ** Head of a linked list of all sqlite3 objects created by this process
  115259                 : ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
  115260                 : ** is not NULL. This variable may only accessed while the STATIC_MASTER
  115261                 : ** mutex is held.
  115262                 : */
  115263                 : static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
  115264                 : 
  115265                 : #ifndef NDEBUG
  115266                 : /*
  115267                 : ** This function is a complex assert() that verifies the following 
  115268                 : ** properties of the blocked connections list:
  115269                 : **
  115270                 : **   1) Each entry in the list has a non-NULL value for either 
  115271                 : **      pUnlockConnection or pBlockingConnection, or both.
  115272                 : **
  115273                 : **   2) All entries in the list that share a common value for 
  115274                 : **      xUnlockNotify are grouped together.
  115275                 : **
  115276                 : **   3) If the argument db is not NULL, then none of the entries in the
  115277                 : **      blocked connections list have pUnlockConnection or pBlockingConnection
  115278                 : **      set to db. This is used when closing connection db.
  115279                 : */
  115280          436657 : static void checkListProperties(sqlite3 *db){
  115281                 :   sqlite3 *p;
  115282          438397 :   for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
  115283            1740 :     int seen = 0;
  115284                 :     sqlite3 *p2;
  115285                 : 
  115286                 :     /* Verify property (1) */
  115287            1740 :     assert( p->pUnlockConnection || p->pBlockingConnection );
  115288                 : 
  115289                 :     /* Verify property (2) */
  115290            1740 :     for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
  115291               0 :       if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
  115292               0 :       assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
  115293               0 :       assert( db==0 || p->pUnlockConnection!=db );
  115294               0 :       assert( db==0 || p->pBlockingConnection!=db );
  115295                 :     }
  115296                 :   }
  115297          436657 : }
  115298                 : #else
  115299                 : # define checkListProperties(x)
  115300                 : #endif
  115301                 : 
  115302                 : /*
  115303                 : ** Remove connection db from the blocked connections list. If connection
  115304                 : ** db is not currently a part of the list, this function is a no-op.
  115305                 : */
  115306            3550 : static void removeFromBlockedList(sqlite3 *db){
  115307                 :   sqlite3 **pp;
  115308            3550 :   assertMutexHeld();
  115309            3550 :   for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
  115310             274 :     if( *pp==db ){
  115311             274 :       *pp = (*pp)->pNextBlocked;
  115312             274 :       break;
  115313                 :     }
  115314                 :   }
  115315            3550 : }
  115316                 : 
  115317                 : /*
  115318                 : ** Add connection db to the blocked connections list. It is assumed
  115319                 : ** that it is not already a part of the list.
  115320                 : */
  115321             571 : static void addToBlockedList(sqlite3 *db){
  115322                 :   sqlite3 **pp;
  115323             571 :   assertMutexHeld();
  115324            1142 :   for(
  115325             571 :     pp=&sqlite3BlockedList; 
  115326             571 :     *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; 
  115327               0 :     pp=&(*pp)->pNextBlocked
  115328                 :   );
  115329             571 :   db->pNextBlocked = *pp;
  115330             571 :   *pp = db;
  115331             571 : }
  115332                 : 
  115333                 : /*
  115334                 : ** Obtain the STATIC_MASTER mutex.
  115335                 : */
  115336          216690 : static void enterMutex(void){
  115337          216690 :   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  115338          216690 :   checkListProperties(0);
  115339          216690 : }
  115340                 : 
  115341                 : /*
  115342                 : ** Release the STATIC_MASTER mutex.
  115343                 : */
  115344          216690 : static void leaveMutex(void){
  115345          216690 :   assertMutexHeld();
  115346          216690 :   checkListProperties(0);
  115347          216690 :   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
  115348          216690 : }
  115349                 : 
  115350                 : /*
  115351                 : ** Register an unlock-notify callback.
  115352                 : **
  115353                 : ** This is called after connection "db" has attempted some operation
  115354                 : ** but has received an SQLITE_LOCKED error because another connection
  115355                 : ** (call it pOther) in the same process was busy using the same shared
  115356                 : ** cache.  pOther is found by looking at db->pBlockingConnection.
  115357                 : **
  115358                 : ** If there is no blocking connection, the callback is invoked immediately,
  115359                 : ** before this routine returns.
  115360                 : **
  115361                 : ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
  115362                 : ** a deadlock.
  115363                 : **
  115364                 : ** Otherwise, make arrangements to invoke xNotify when pOther drops
  115365                 : ** its locks.
  115366                 : **
  115367                 : ** Each call to this routine overrides any prior callbacks registered
  115368                 : ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
  115369                 : ** cancelled.
  115370                 : */
  115371             297 : SQLITE_API int sqlite3_unlock_notify(
  115372                 :   sqlite3 *db,
  115373                 :   void (*xNotify)(void **, int),
  115374                 :   void *pArg
  115375                 : ){
  115376             297 :   int rc = SQLITE_OK;
  115377                 : 
  115378             297 :   sqlite3_mutex_enter(db->mutex);
  115379             297 :   enterMutex();
  115380                 : 
  115381             297 :   if( xNotify==0 ){
  115382               0 :     removeFromBlockedList(db);
  115383               0 :     db->pBlockingConnection = 0;
  115384               0 :     db->pUnlockConnection = 0;
  115385               0 :     db->xUnlockNotify = 0;
  115386               0 :     db->pUnlockArg = 0;
  115387             297 :   }else if( 0==db->pBlockingConnection ){
  115388                 :     /* The blocking transaction has been concluded. Or there never was a 
  115389                 :     ** blocking transaction. In either case, invoke the notify callback
  115390                 :     ** immediately. 
  115391                 :     */
  115392              24 :     xNotify(&pArg, 1);
  115393                 :   }else{
  115394                 :     sqlite3 *p;
  115395                 : 
  115396             273 :     for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
  115397             273 :     if( p ){
  115398               0 :       rc = SQLITE_LOCKED;              /* Deadlock detected. */
  115399                 :     }else{
  115400             273 :       db->pUnlockConnection = db->pBlockingConnection;
  115401             273 :       db->xUnlockNotify = xNotify;
  115402             273 :       db->pUnlockArg = pArg;
  115403             273 :       removeFromBlockedList(db);
  115404             273 :       addToBlockedList(db);
  115405                 :     }
  115406                 :   }
  115407                 : 
  115408             297 :   leaveMutex();
  115409             297 :   assert( !db->mallocFailed );
  115410             297 :   sqlite3Error(db, rc, (rc?"database is deadlocked":0));
  115411             297 :   sqlite3_mutex_leave(db->mutex);
  115412             297 :   return rc;
  115413                 : }
  115414                 : 
  115415                 : /*
  115416                 : ** This function is called while stepping or preparing a statement 
  115417                 : ** associated with connection db. The operation will return SQLITE_LOCKED
  115418                 : ** to the user because it requires a lock that will not be available
  115419                 : ** until connection pBlocker concludes its current transaction.
  115420                 : */
  115421             298 : SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
  115422             298 :   enterMutex();
  115423             298 :   if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
  115424             298 :     addToBlockedList(db);
  115425                 :   }
  115426             298 :   db->pBlockingConnection = pBlocker;
  115427             298 :   leaveMutex();
  115428             298 : }
  115429                 : 
  115430                 : /*
  115431                 : ** This function is called when
  115432                 : ** the transaction opened by database db has just finished. Locks held 
  115433                 : ** by database connection db have been released.
  115434                 : **
  115435                 : ** This function loops through each entry in the blocked connections
  115436                 : ** list and does the following:
  115437                 : **
  115438                 : **   1) If the sqlite3.pBlockingConnection member of a list entry is
  115439                 : **      set to db, then set pBlockingConnection=0.
  115440                 : **
  115441                 : **   2) If the sqlite3.pUnlockConnection member of a list entry is
  115442                 : **      set to db, then invoke the configured unlock-notify callback and
  115443                 : **      set pUnlockConnection=0.
  115444                 : **
  115445                 : **   3) If the two steps above mean that pBlockingConnection==0 and
  115446                 : **      pUnlockConnection==0, remove the entry from the blocked connections
  115447                 : **      list.
  115448                 : */
  115449          212818 : SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
  115450          212818 :   void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
  115451          212818 :   int nArg = 0;                            /* Number of entries in aArg[] */
  115452                 :   sqlite3 **pp;                            /* Iterator variable */
  115453                 :   void **aArg;               /* Arguments to the unlock callback */
  115454          212818 :   void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
  115455                 :   void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
  115456                 : 
  115457          212818 :   aArg = aStatic;
  115458          212818 :   enterMutex();         /* Enter STATIC_MASTER mutex */
  115459                 : 
  115460                 :   /* This loop runs once for each entry in the blocked-connections list. */
  115461          426232 :   for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
  115462             596 :     sqlite3 *p = *pp;
  115463                 : 
  115464                 :     /* Step 1. */
  115465             596 :     if( p->pBlockingConnection==db ){
  115466             297 :       p->pBlockingConnection = 0;
  115467                 :     }
  115468                 : 
  115469                 :     /* Step 2. */
  115470             596 :     if( p->pUnlockConnection==db ){
  115471             273 :       assert( p->xUnlockNotify );
  115472             273 :       if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
  115473               0 :         xUnlockNotify(aArg, nArg);
  115474               0 :         nArg = 0;
  115475                 :       }
  115476                 : 
  115477             273 :       sqlite3BeginBenignMalloc();
  115478             273 :       assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
  115479             273 :       assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
  115480             273 :       if( (!aDyn && nArg==(int)ArraySize(aStatic))
  115481             273 :        || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
  115482                 :       ){
  115483                 :         /* The aArg[] array needs to grow. */
  115484               0 :         void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
  115485               0 :         if( pNew ){
  115486               0 :           memcpy(pNew, aArg, nArg*sizeof(void *));
  115487               0 :           sqlite3_free(aDyn);
  115488               0 :           aDyn = aArg = pNew;
  115489                 :         }else{
  115490                 :           /* This occurs when the array of context pointers that need to
  115491                 :           ** be passed to the unlock-notify callback is larger than the
  115492                 :           ** aStatic[] array allocated on the stack and the attempt to 
  115493                 :           ** allocate a larger array from the heap has failed.
  115494                 :           **
  115495                 :           ** This is a difficult situation to handle. Returning an error
  115496                 :           ** code to the caller is insufficient, as even if an error code
  115497                 :           ** is returned the transaction on connection db will still be
  115498                 :           ** closed and the unlock-notify callbacks on blocked connections
  115499                 :           ** will go unissued. This might cause the application to wait
  115500                 :           ** indefinitely for an unlock-notify callback that will never 
  115501                 :           ** arrive.
  115502                 :           **
  115503                 :           ** Instead, invoke the unlock-notify callback with the context
  115504                 :           ** array already accumulated. We can then clear the array and
  115505                 :           ** begin accumulating any further context pointers without 
  115506                 :           ** requiring any dynamic allocation. This is sub-optimal because
  115507                 :           ** it means that instead of one callback with a large array of
  115508                 :           ** context pointers the application will receive two or more
  115509                 :           ** callbacks with smaller arrays of context pointers, which will
  115510                 :           ** reduce the applications ability to prioritize multiple 
  115511                 :           ** connections. But it is the best that can be done under the
  115512                 :           ** circumstances.
  115513                 :           */
  115514               0 :           xUnlockNotify(aArg, nArg);
  115515               0 :           nArg = 0;
  115516                 :         }
  115517                 :       }
  115518             273 :       sqlite3EndBenignMalloc();
  115519                 : 
  115520             273 :       aArg[nArg++] = p->pUnlockArg;
  115521             273 :       xUnlockNotify = p->xUnlockNotify;
  115522             273 :       p->pUnlockConnection = 0;
  115523             273 :       p->xUnlockNotify = 0;
  115524             273 :       p->pUnlockArg = 0;
  115525                 :     }
  115526                 : 
  115527                 :     /* Step 3. */
  115528             596 :     if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
  115529                 :       /* Remove connection p from the blocked connections list. */
  115530             297 :       *pp = p->pNextBlocked;
  115531             297 :       p->pNextBlocked = 0;
  115532                 :     }else{
  115533             299 :       pp = &p->pNextBlocked;
  115534                 :     }
  115535                 :   }
  115536                 : 
  115537          212818 :   if( nArg!=0 ){
  115538             273 :     xUnlockNotify(aArg, nArg);
  115539                 :   }
  115540          212818 :   sqlite3_free(aDyn);
  115541          212818 :   leaveMutex();         /* Leave STATIC_MASTER mutex */
  115542          212818 : }
  115543                 : 
  115544                 : /*
  115545                 : ** This is called when the database connection passed as an argument is 
  115546                 : ** being closed. The connection is removed from the blocked list.
  115547                 : */
  115548            3277 : SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
  115549            3277 :   sqlite3ConnectionUnlocked(db);
  115550            3277 :   enterMutex();
  115551            3277 :   removeFromBlockedList(db);
  115552            3277 :   checkListProperties(db);
  115553            3277 :   leaveMutex();
  115554            3277 : }
  115555                 : #endif
  115556                 : 
  115557                 : /************** End of notify.c **********************************************/
  115558                 : /************** Begin file fts3.c ********************************************/
  115559                 : /*
  115560                 : ** 2006 Oct 10
  115561                 : **
  115562                 : ** The author disclaims copyright to this source code.  In place of
  115563                 : ** a legal notice, here is a blessing:
  115564                 : **
  115565                 : **    May you do good and not evil.
  115566                 : **    May you find forgiveness for yourself and forgive others.
  115567                 : **    May you share freely, never taking more than you give.
  115568                 : **
  115569                 : ******************************************************************************
  115570                 : **
  115571                 : ** This is an SQLite module implementing full-text search.
  115572                 : */
  115573                 : 
  115574                 : /*
  115575                 : ** The code in this file is only compiled if:
  115576                 : **
  115577                 : **     * The FTS3 module is being built as an extension
  115578                 : **       (in which case SQLITE_CORE is not defined), or
  115579                 : **
  115580                 : **     * The FTS3 module is being built into the core of
  115581                 : **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  115582                 : */
  115583                 : 
  115584                 : /* The full-text index is stored in a series of b+tree (-like)
  115585                 : ** structures called segments which map terms to doclists.  The
  115586                 : ** structures are like b+trees in layout, but are constructed from the
  115587                 : ** bottom up in optimal fashion and are not updatable.  Since trees
  115588                 : ** are built from the bottom up, things will be described from the
  115589                 : ** bottom up.
  115590                 : **
  115591                 : **
  115592                 : **** Varints ****
  115593                 : ** The basic unit of encoding is a variable-length integer called a
  115594                 : ** varint.  We encode variable-length integers in little-endian order
  115595                 : ** using seven bits * per byte as follows:
  115596                 : **
  115597                 : ** KEY:
  115598                 : **         A = 0xxxxxxx    7 bits of data and one flag bit
  115599                 : **         B = 1xxxxxxx    7 bits of data and one flag bit
  115600                 : **
  115601                 : **  7 bits - A
  115602                 : ** 14 bits - BA
  115603                 : ** 21 bits - BBA
  115604                 : ** and so on.
  115605                 : **
  115606                 : ** This is similar in concept to how sqlite encodes "varints" but
  115607                 : ** the encoding is not the same.  SQLite varints are big-endian
  115608                 : ** are are limited to 9 bytes in length whereas FTS3 varints are
  115609                 : ** little-endian and can be up to 10 bytes in length (in theory).
  115610                 : **
  115611                 : ** Example encodings:
  115612                 : **
  115613                 : **     1:    0x01
  115614                 : **   127:    0x7f
  115615                 : **   128:    0x81 0x00
  115616                 : **
  115617                 : **
  115618                 : **** Document lists ****
  115619                 : ** A doclist (document list) holds a docid-sorted list of hits for a
  115620                 : ** given term.  Doclists hold docids and associated token positions.
  115621                 : ** A docid is the unique integer identifier for a single document.
  115622                 : ** A position is the index of a word within the document.  The first 
  115623                 : ** word of the document has a position of 0.
  115624                 : **
  115625                 : ** FTS3 used to optionally store character offsets using a compile-time
  115626                 : ** option.  But that functionality is no longer supported.
  115627                 : **
  115628                 : ** A doclist is stored like this:
  115629                 : **
  115630                 : ** array {
  115631                 : **   varint docid;
  115632                 : **   array {                (position list for column 0)
  115633                 : **     varint position;     (2 more than the delta from previous position)
  115634                 : **   }
  115635                 : **   array {
  115636                 : **     varint POS_COLUMN;   (marks start of position list for new column)
  115637                 : **     varint column;       (index of new column)
  115638                 : **     array {
  115639                 : **       varint position;   (2 more than the delta from previous position)
  115640                 : **     }
  115641                 : **   }
  115642                 : **   varint POS_END;        (marks end of positions for this document.
  115643                 : ** }
  115644                 : **
  115645                 : ** Here, array { X } means zero or more occurrences of X, adjacent in
  115646                 : ** memory.  A "position" is an index of a token in the token stream
  115647                 : ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur 
  115648                 : ** in the same logical place as the position element, and act as sentinals
  115649                 : ** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
  115650                 : ** The positions numbers are not stored literally but rather as two more
  115651                 : ** than the difference from the prior position, or the just the position plus
  115652                 : ** 2 for the first position.  Example:
  115653                 : **
  115654                 : **   label:       A B C D E  F  G H   I  J K
  115655                 : **   value:     123 5 9 1 1 14 35 0 234 72 0
  115656                 : **
  115657                 : ** The 123 value is the first docid.  For column zero in this document
  115658                 : ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
  115659                 : ** at D signals the start of a new column; the 1 at E indicates that the
  115660                 : ** new column is column number 1.  There are two positions at 12 and 45
  115661                 : ** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
  115662                 : ** 234 at I is the next docid.  It has one position 72 (72-2) and then
  115663                 : ** terminates with the 0 at K.
  115664                 : **
  115665                 : ** A "position-list" is the list of positions for multiple columns for
  115666                 : ** a single docid.  A "column-list" is the set of positions for a single
  115667                 : ** column.  Hence, a position-list consists of one or more column-lists,
  115668                 : ** a document record consists of a docid followed by a position-list and
  115669                 : ** a doclist consists of one or more document records.
  115670                 : **
  115671                 : ** A bare doclist omits the position information, becoming an 
  115672                 : ** array of varint-encoded docids.
  115673                 : **
  115674                 : **** Segment leaf nodes ****
  115675                 : ** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
  115676                 : ** nodes are written using LeafWriter, and read using LeafReader (to
  115677                 : ** iterate through a single leaf node's data) and LeavesReader (to
  115678                 : ** iterate through a segment's entire leaf layer).  Leaf nodes have
  115679                 : ** the format:
  115680                 : **
  115681                 : ** varint iHeight;             (height from leaf level, always 0)
  115682                 : ** varint nTerm;               (length of first term)
  115683                 : ** char pTerm[nTerm];          (content of first term)
  115684                 : ** varint nDoclist;            (length of term's associated doclist)
  115685                 : ** char pDoclist[nDoclist];    (content of doclist)
  115686                 : ** array {
  115687                 : **                             (further terms are delta-encoded)
  115688                 : **   varint nPrefix;           (length of prefix shared with previous term)
  115689                 : **   varint nSuffix;           (length of unshared suffix)
  115690                 : **   char pTermSuffix[nSuffix];(unshared suffix of next term)
  115691                 : **   varint nDoclist;          (length of term's associated doclist)
  115692                 : **   char pDoclist[nDoclist];  (content of doclist)
  115693                 : ** }
  115694                 : **
  115695                 : ** Here, array { X } means zero or more occurrences of X, adjacent in
  115696                 : ** memory.
  115697                 : **
  115698                 : ** Leaf nodes are broken into blocks which are stored contiguously in
  115699                 : ** the %_segments table in sorted order.  This means that when the end
  115700                 : ** of a node is reached, the next term is in the node with the next
  115701                 : ** greater node id.
  115702                 : **
  115703                 : ** New data is spilled to a new leaf node when the current node
  115704                 : ** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
  115705                 : ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
  115706                 : ** node (a leaf node with a single term and doclist).  The goal of
  115707                 : ** these settings is to pack together groups of small doclists while
  115708                 : ** making it efficient to directly access large doclists.  The
  115709                 : ** assumption is that large doclists represent terms which are more
  115710                 : ** likely to be query targets.
  115711                 : **
  115712                 : ** TODO(shess) It may be useful for blocking decisions to be more
  115713                 : ** dynamic.  For instance, it may make more sense to have a 2.5k leaf
  115714                 : ** node rather than splitting into 2k and .5k nodes.  My intuition is
  115715                 : ** that this might extend through 2x or 4x the pagesize.
  115716                 : **
  115717                 : **
  115718                 : **** Segment interior nodes ****
  115719                 : ** Segment interior nodes store blockids for subtree nodes and terms
  115720                 : ** to describe what data is stored by the each subtree.  Interior
  115721                 : ** nodes are written using InteriorWriter, and read using
  115722                 : ** InteriorReader.  InteriorWriters are created as needed when
  115723                 : ** SegmentWriter creates new leaf nodes, or when an interior node
  115724                 : ** itself grows too big and must be split.  The format of interior
  115725                 : ** nodes:
  115726                 : **
  115727                 : ** varint iHeight;           (height from leaf level, always >0)
  115728                 : ** varint iBlockid;          (block id of node's leftmost subtree)
  115729                 : ** optional {
  115730                 : **   varint nTerm;           (length of first term)
  115731                 : **   char pTerm[nTerm];      (content of first term)
  115732                 : **   array {
  115733                 : **                                (further terms are delta-encoded)
  115734                 : **     varint nPrefix;            (length of shared prefix with previous term)
  115735                 : **     varint nSuffix;            (length of unshared suffix)
  115736                 : **     char pTermSuffix[nSuffix]; (unshared suffix of next term)
  115737                 : **   }
  115738                 : ** }
  115739                 : **
  115740                 : ** Here, optional { X } means an optional element, while array { X }
  115741                 : ** means zero or more occurrences of X, adjacent in memory.
  115742                 : **
  115743                 : ** An interior node encodes n terms separating n+1 subtrees.  The
  115744                 : ** subtree blocks are contiguous, so only the first subtree's blockid
  115745                 : ** is encoded.  The subtree at iBlockid will contain all terms less
  115746                 : ** than the first term encoded (or all terms if no term is encoded).
  115747                 : ** Otherwise, for terms greater than or equal to pTerm[i] but less
  115748                 : ** than pTerm[i+1], the subtree for that term will be rooted at
  115749                 : ** iBlockid+i.  Interior nodes only store enough term data to
  115750                 : ** distinguish adjacent children (if the rightmost term of the left
  115751                 : ** child is "something", and the leftmost term of the right child is
  115752                 : ** "wicked", only "w" is stored).
  115753                 : **
  115754                 : ** New data is spilled to a new interior node at the same height when
  115755                 : ** the current node exceeds INTERIOR_MAX bytes (default 2048).
  115756                 : ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
  115757                 : ** interior nodes and making the tree too skinny.  The interior nodes
  115758                 : ** at a given height are naturally tracked by interior nodes at
  115759                 : ** height+1, and so on.
  115760                 : **
  115761                 : **
  115762                 : **** Segment directory ****
  115763                 : ** The segment directory in table %_segdir stores meta-information for
  115764                 : ** merging and deleting segments, and also the root node of the
  115765                 : ** segment's tree.
  115766                 : **
  115767                 : ** The root node is the top node of the segment's tree after encoding
  115768                 : ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
  115769                 : ** This could be either a leaf node or an interior node.  If the top
  115770                 : ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
  115771                 : ** and a new root interior node is generated (which should always fit
  115772                 : ** within ROOT_MAX because it only needs space for 2 varints, the
  115773                 : ** height and the blockid of the previous root).
  115774                 : **
  115775                 : ** The meta-information in the segment directory is:
  115776                 : **   level               - segment level (see below)
  115777                 : **   idx                 - index within level
  115778                 : **                       - (level,idx uniquely identify a segment)
  115779                 : **   start_block         - first leaf node
  115780                 : **   leaves_end_block    - last leaf node
  115781                 : **   end_block           - last block (including interior nodes)
  115782                 : **   root                - contents of root node
  115783                 : **
  115784                 : ** If the root node is a leaf node, then start_block,
  115785                 : ** leaves_end_block, and end_block are all 0.
  115786                 : **
  115787                 : **
  115788                 : **** Segment merging ****
  115789                 : ** To amortize update costs, segments are grouped into levels and
  115790                 : ** merged in batches.  Each increase in level represents exponentially
  115791                 : ** more documents.
  115792                 : **
  115793                 : ** New documents (actually, document updates) are tokenized and
  115794                 : ** written individually (using LeafWriter) to a level 0 segment, with
  115795                 : ** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
  115796                 : ** level 0 segments are merged into a single level 1 segment.  Level 1
  115797                 : ** is populated like level 0, and eventually MERGE_COUNT level 1
  115798                 : ** segments are merged to a single level 2 segment (representing
  115799                 : ** MERGE_COUNT^2 updates), and so on.
  115800                 : **
  115801                 : ** A segment merge traverses all segments at a given level in
  115802                 : ** parallel, performing a straightforward sorted merge.  Since segment
  115803                 : ** leaf nodes are written in to the %_segments table in order, this
  115804                 : ** merge traverses the underlying sqlite disk structures efficiently.
  115805                 : ** After the merge, all segment blocks from the merged level are
  115806                 : ** deleted.
  115807                 : **
  115808                 : ** MERGE_COUNT controls how often we merge segments.  16 seems to be
  115809                 : ** somewhat of a sweet spot for insertion performance.  32 and 64 show
  115810                 : ** very similar performance numbers to 16 on insertion, though they're
  115811                 : ** a tiny bit slower (perhaps due to more overhead in merge-time
  115812                 : ** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
  115813                 : ** 16, 2 about 66% slower than 16.
  115814                 : **
  115815                 : ** At query time, high MERGE_COUNT increases the number of segments
  115816                 : ** which need to be scanned and merged.  For instance, with 100k docs
  115817                 : ** inserted:
  115818                 : **
  115819                 : **    MERGE_COUNT   segments
  115820                 : **       16           25
  115821                 : **        8           12
  115822                 : **        4           10
  115823                 : **        2            6
  115824                 : **
  115825                 : ** This appears to have only a moderate impact on queries for very
  115826                 : ** frequent terms (which are somewhat dominated by segment merge
  115827                 : ** costs), and infrequent and non-existent terms still seem to be fast
  115828                 : ** even with many segments.
  115829                 : **
  115830                 : ** TODO(shess) That said, it would be nice to have a better query-side
  115831                 : ** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
  115832                 : ** optimizations to things like doclist merging will swing the sweet
  115833                 : ** spot around.
  115834                 : **
  115835                 : **
  115836                 : **
  115837                 : **** Handling of deletions and updates ****
  115838                 : ** Since we're using a segmented structure, with no docid-oriented
  115839                 : ** index into the term index, we clearly cannot simply update the term
  115840                 : ** index when a document is deleted or updated.  For deletions, we
  115841                 : ** write an empty doclist (varint(docid) varint(POS_END)), for updates
  115842                 : ** we simply write the new doclist.  Segment merges overwrite older
  115843                 : ** data for a particular docid with newer data, so deletes or updates
  115844                 : ** will eventually overtake the earlier data and knock it out.  The
  115845                 : ** query logic likewise merges doclists so that newer data knocks out
  115846                 : ** older data.
  115847                 : **
  115848                 : ** TODO(shess) Provide a VACUUM type operation to clear out all
  115849                 : ** deletions and duplications.  This would basically be a forced merge
  115850                 : ** into a single segment.
  115851                 : */
  115852                 : 
  115853                 : /************** Include fts3Int.h in the middle of fts3.c ********************/
  115854                 : /************** Begin file fts3Int.h *****************************************/
  115855                 : /*
  115856                 : ** 2009 Nov 12
  115857                 : **
  115858                 : ** The author disclaims copyright to this source code.  In place of
  115859                 : ** a legal notice, here is a blessing:
  115860                 : **
  115861                 : **    May you do good and not evil.
  115862                 : **    May you find forgiveness for yourself and forgive others.
  115863                 : **    May you share freely, never taking more than you give.
  115864                 : **
  115865                 : ******************************************************************************
  115866                 : **
  115867                 : */
  115868                 : #ifndef _FTSINT_H
  115869                 : #define _FTSINT_H
  115870                 : 
  115871                 : #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
  115872                 : # define NDEBUG 1
  115873                 : #endif
  115874                 : 
  115875                 : /*
  115876                 : ** FTS4 is really an extension for FTS3.  It is enabled using the
  115877                 : ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
  115878                 : ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
  115879                 : */
  115880                 : #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
  115881                 : # define SQLITE_ENABLE_FTS3
  115882                 : #endif
  115883                 : 
  115884                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  115885                 : 
  115886                 : /* If not building as part of the core, include sqlite3ext.h. */
  115887                 : #ifndef SQLITE_CORE
  115888                 : SQLITE_API extern const sqlite3_api_routines *sqlite3_api;
  115889                 : #endif
  115890                 : 
  115891                 : /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
  115892                 : /************** Begin file fts3_tokenizer.h **********************************/
  115893                 : /*
  115894                 : ** 2006 July 10
  115895                 : **
  115896                 : ** The author disclaims copyright to this source code.
  115897                 : **
  115898                 : *************************************************************************
  115899                 : ** Defines the interface to tokenizers used by fulltext-search.  There
  115900                 : ** are three basic components:
  115901                 : **
  115902                 : ** sqlite3_tokenizer_module is a singleton defining the tokenizer
  115903                 : ** interface functions.  This is essentially the class structure for
  115904                 : ** tokenizers.
  115905                 : **
  115906                 : ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
  115907                 : ** including customization information defined at creation time.
  115908                 : **
  115909                 : ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
  115910                 : ** tokens from a particular input.
  115911                 : */
  115912                 : #ifndef _FTS3_TOKENIZER_H_
  115913                 : #define _FTS3_TOKENIZER_H_
  115914                 : 
  115915                 : /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
  115916                 : ** If tokenizers are to be allowed to call sqlite3_*() functions, then
  115917                 : ** we will need a way to register the API consistently.
  115918                 : */
  115919                 : 
  115920                 : /*
  115921                 : ** Structures used by the tokenizer interface. When a new tokenizer
  115922                 : ** implementation is registered, the caller provides a pointer to
  115923                 : ** an sqlite3_tokenizer_module containing pointers to the callback
  115924                 : ** functions that make up an implementation.
  115925                 : **
  115926                 : ** When an fts3 table is created, it passes any arguments passed to
  115927                 : ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
  115928                 : ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
  115929                 : ** implementation. The xCreate() function in turn returns an 
  115930                 : ** sqlite3_tokenizer structure representing the specific tokenizer to
  115931                 : ** be used for the fts3 table (customized by the tokenizer clause arguments).
  115932                 : **
  115933                 : ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
  115934                 : ** method is called. It returns an sqlite3_tokenizer_cursor object
  115935                 : ** that may be used to tokenize a specific input buffer based on
  115936                 : ** the tokenization rules supplied by a specific sqlite3_tokenizer
  115937                 : ** object.
  115938                 : */
  115939                 : typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
  115940                 : typedef struct sqlite3_tokenizer sqlite3_tokenizer;
  115941                 : typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
  115942                 : 
  115943                 : struct sqlite3_tokenizer_module {
  115944                 : 
  115945                 :   /*
  115946                 :   ** Structure version. Should always be set to 0.
  115947                 :   */
  115948                 :   int iVersion;
  115949                 : 
  115950                 :   /*
  115951                 :   ** Create a new tokenizer. The values in the argv[] array are the
  115952                 :   ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
  115953                 :   ** TABLE statement that created the fts3 table. For example, if
  115954                 :   ** the following SQL is executed:
  115955                 :   **
  115956                 :   **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
  115957                 :   **
  115958                 :   ** then argc is set to 2, and the argv[] array contains pointers
  115959                 :   ** to the strings "arg1" and "arg2".
  115960                 :   **
  115961                 :   ** This method should return either SQLITE_OK (0), or an SQLite error 
  115962                 :   ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
  115963                 :   ** to point at the newly created tokenizer structure. The generic
  115964                 :   ** sqlite3_tokenizer.pModule variable should not be initialised by
  115965                 :   ** this callback. The caller will do so.
  115966                 :   */
  115967                 :   int (*xCreate)(
  115968                 :     int argc,                           /* Size of argv array */
  115969                 :     const char *const*argv,             /* Tokenizer argument strings */
  115970                 :     sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
  115971                 :   );
  115972                 : 
  115973                 :   /*
  115974                 :   ** Destroy an existing tokenizer. The fts3 module calls this method
  115975                 :   ** exactly once for each successful call to xCreate().
  115976                 :   */
  115977                 :   int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
  115978                 : 
  115979                 :   /*
  115980                 :   ** Create a tokenizer cursor to tokenize an input buffer. The caller
  115981                 :   ** is responsible for ensuring that the input buffer remains valid
  115982                 :   ** until the cursor is closed (using the xClose() method). 
  115983                 :   */
  115984                 :   int (*xOpen)(
  115985                 :     sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
  115986                 :     const char *pInput, int nBytes,      /* Input buffer */
  115987                 :     sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
  115988                 :   );
  115989                 : 
  115990                 :   /*
  115991                 :   ** Destroy an existing tokenizer cursor. The fts3 module calls this 
  115992                 :   ** method exactly once for each successful call to xOpen().
  115993                 :   */
  115994                 :   int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
  115995                 : 
  115996                 :   /*
  115997                 :   ** Retrieve the next token from the tokenizer cursor pCursor. This
  115998                 :   ** method should either return SQLITE_OK and set the values of the
  115999                 :   ** "OUT" variables identified below, or SQLITE_DONE to indicate that
  116000                 :   ** the end of the buffer has been reached, or an SQLite error code.
  116001                 :   **
  116002                 :   ** *ppToken should be set to point at a buffer containing the 
  116003                 :   ** normalized version of the token (i.e. after any case-folding and/or
  116004                 :   ** stemming has been performed). *pnBytes should be set to the length
  116005                 :   ** of this buffer in bytes. The input text that generated the token is
  116006                 :   ** identified by the byte offsets returned in *piStartOffset and
  116007                 :   ** *piEndOffset. *piStartOffset should be set to the index of the first
  116008                 :   ** byte of the token in the input buffer. *piEndOffset should be set
  116009                 :   ** to the index of the first byte just past the end of the token in
  116010                 :   ** the input buffer.
  116011                 :   **
  116012                 :   ** The buffer *ppToken is set to point at is managed by the tokenizer
  116013                 :   ** implementation. It is only required to be valid until the next call
  116014                 :   ** to xNext() or xClose(). 
  116015                 :   */
  116016                 :   /* TODO(shess) current implementation requires pInput to be
  116017                 :   ** nul-terminated.  This should either be fixed, or pInput/nBytes
  116018                 :   ** should be converted to zInput.
  116019                 :   */
  116020                 :   int (*xNext)(
  116021                 :     sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
  116022                 :     const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
  116023                 :     int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
  116024                 :     int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
  116025                 :     int *piPosition      /* OUT: Number of tokens returned before this one */
  116026                 :   );
  116027                 : };
  116028                 : 
  116029                 : struct sqlite3_tokenizer {
  116030                 :   const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
  116031                 :   /* Tokenizer implementations will typically add additional fields */
  116032                 : };
  116033                 : 
  116034                 : struct sqlite3_tokenizer_cursor {
  116035                 :   sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
  116036                 :   /* Tokenizer implementations will typically add additional fields */
  116037                 : };
  116038                 : 
  116039                 : int fts3_global_term_cnt(int iTerm, int iCol);
  116040                 : int fts3_term_cnt(int iTerm, int iCol);
  116041                 : 
  116042                 : 
  116043                 : #endif /* _FTS3_TOKENIZER_H_ */
  116044                 : 
  116045                 : /************** End of fts3_tokenizer.h **************************************/
  116046                 : /************** Continuing where we left off in fts3Int.h ********************/
  116047                 : /************** Include fts3_hash.h in the middle of fts3Int.h ***************/
  116048                 : /************** Begin file fts3_hash.h ***************************************/
  116049                 : /*
  116050                 : ** 2001 September 22
  116051                 : **
  116052                 : ** The author disclaims copyright to this source code.  In place of
  116053                 : ** a legal notice, here is a blessing:
  116054                 : **
  116055                 : **    May you do good and not evil.
  116056                 : **    May you find forgiveness for yourself and forgive others.
  116057                 : **    May you share freely, never taking more than you give.
  116058                 : **
  116059                 : *************************************************************************
  116060                 : ** This is the header file for the generic hash-table implemenation
  116061                 : ** used in SQLite.  We've modified it slightly to serve as a standalone
  116062                 : ** hash table implementation for the full-text indexing module.
  116063                 : **
  116064                 : */
  116065                 : #ifndef _FTS3_HASH_H_
  116066                 : #define _FTS3_HASH_H_
  116067                 : 
  116068                 : /* Forward declarations of structures. */
  116069                 : typedef struct Fts3Hash Fts3Hash;
  116070                 : typedef struct Fts3HashElem Fts3HashElem;
  116071                 : 
  116072                 : /* A complete hash table is an instance of the following structure.
  116073                 : ** The internals of this structure are intended to be opaque -- client
  116074                 : ** code should not attempt to access or modify the fields of this structure
  116075                 : ** directly.  Change this structure only by using the routines below.
  116076                 : ** However, many of the "procedures" and "functions" for modifying and
  116077                 : ** accessing this structure are really macros, so we can't really make
  116078                 : ** this structure opaque.
  116079                 : */
  116080                 : struct Fts3Hash {
  116081                 :   char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
  116082                 :   char copyKey;           /* True if copy of key made on insert */
  116083                 :   int count;              /* Number of entries in this table */
  116084                 :   Fts3HashElem *first;    /* The first element of the array */
  116085                 :   int htsize;             /* Number of buckets in the hash table */
  116086                 :   struct _fts3ht {        /* the hash table */
  116087                 :     int count;               /* Number of entries with this hash */
  116088                 :     Fts3HashElem *chain;     /* Pointer to first entry with this hash */
  116089                 :   } *ht;
  116090                 : };
  116091                 : 
  116092                 : /* Each element in the hash table is an instance of the following 
  116093                 : ** structure.  All elements are stored on a single doubly-linked list.
  116094                 : **
  116095                 : ** Again, this structure is intended to be opaque, but it can't really
  116096                 : ** be opaque because it is used by macros.
  116097                 : */
  116098                 : struct Fts3HashElem {
  116099                 :   Fts3HashElem *next, *prev; /* Next and previous elements in the table */
  116100                 :   void *data;                /* Data associated with this element */
  116101                 :   void *pKey; int nKey;      /* Key associated with this element */
  116102                 : };
  116103                 : 
  116104                 : /*
  116105                 : ** There are 2 different modes of operation for a hash table:
  116106                 : **
  116107                 : **   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
  116108                 : **                           (including the null-terminator, if any).  Case
  116109                 : **                           is respected in comparisons.
  116110                 : **
  116111                 : **   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long. 
  116112                 : **                           memcmp() is used to compare keys.
  116113                 : **
  116114                 : ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.  
  116115                 : */
  116116                 : #define FTS3_HASH_STRING    1
  116117                 : #define FTS3_HASH_BINARY    2
  116118                 : 
  116119                 : /*
  116120                 : ** Access routines.  To delete, insert a NULL pointer.
  116121                 : */
  116122                 : SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
  116123                 : SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
  116124                 : SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
  116125                 : SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
  116126                 : SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
  116127                 : 
  116128                 : /*
  116129                 : ** Shorthand for the functions above
  116130                 : */
  116131                 : #define fts3HashInit     sqlite3Fts3HashInit
  116132                 : #define fts3HashInsert   sqlite3Fts3HashInsert
  116133                 : #define fts3HashFind     sqlite3Fts3HashFind
  116134                 : #define fts3HashClear    sqlite3Fts3HashClear
  116135                 : #define fts3HashFindElem sqlite3Fts3HashFindElem
  116136                 : 
  116137                 : /*
  116138                 : ** Macros for looping over all elements of a hash table.  The idiom is
  116139                 : ** like this:
  116140                 : **
  116141                 : **   Fts3Hash h;
  116142                 : **   Fts3HashElem *p;
  116143                 : **   ...
  116144                 : **   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
  116145                 : **     SomeStructure *pData = fts3HashData(p);
  116146                 : **     // do something with pData
  116147                 : **   }
  116148                 : */
  116149                 : #define fts3HashFirst(H)  ((H)->first)
  116150                 : #define fts3HashNext(E)   ((E)->next)
  116151                 : #define fts3HashData(E)   ((E)->data)
  116152                 : #define fts3HashKey(E)    ((E)->pKey)
  116153                 : #define fts3HashKeysize(E) ((E)->nKey)
  116154                 : 
  116155                 : /*
  116156                 : ** Number of entries in a hash table
  116157                 : */
  116158                 : #define fts3HashCount(H)  ((H)->count)
  116159                 : 
  116160                 : #endif /* _FTS3_HASH_H_ */
  116161                 : 
  116162                 : /************** End of fts3_hash.h *******************************************/
  116163                 : /************** Continuing where we left off in fts3Int.h ********************/
  116164                 : 
  116165                 : /*
  116166                 : ** This constant controls how often segments are merged. Once there are
  116167                 : ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
  116168                 : ** segment of level N+1.
  116169                 : */
  116170                 : #define FTS3_MERGE_COUNT 16
  116171                 : 
  116172                 : /*
  116173                 : ** This is the maximum amount of data (in bytes) to store in the 
  116174                 : ** Fts3Table.pendingTerms hash table. Normally, the hash table is
  116175                 : ** populated as documents are inserted/updated/deleted in a transaction
  116176                 : ** and used to create a new segment when the transaction is committed.
  116177                 : ** However if this limit is reached midway through a transaction, a new 
  116178                 : ** segment is created and the hash table cleared immediately.
  116179                 : */
  116180                 : #define FTS3_MAX_PENDING_DATA (1*1024*1024)
  116181                 : 
  116182                 : /*
  116183                 : ** Macro to return the number of elements in an array. SQLite has a
  116184                 : ** similar macro called ArraySize(). Use a different name to avoid
  116185                 : ** a collision when building an amalgamation with built-in FTS3.
  116186                 : */
  116187                 : #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
  116188                 : 
  116189                 : 
  116190                 : #ifndef MIN
  116191                 : # define MIN(x,y) ((x)<(y)?(x):(y))
  116192                 : #endif
  116193                 : 
  116194                 : /*
  116195                 : ** Maximum length of a varint encoded integer. The varint format is different
  116196                 : ** from that used by SQLite, so the maximum length is 10, not 9.
  116197                 : */
  116198                 : #define FTS3_VARINT_MAX 10
  116199                 : 
  116200                 : /*
  116201                 : ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
  116202                 : ** in the document set and zero or more prefix indexes. All indexes are stored
  116203                 : ** as one or more b+-trees in the %_segments and %_segdir tables. 
  116204                 : **
  116205                 : ** It is possible to determine which index a b+-tree belongs to based on the
  116206                 : ** value stored in the "%_segdir.level" column. Given this value L, the index
  116207                 : ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
  116208                 : ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
  116209                 : ** between 1024 and 2047 to index 1, and so on.
  116210                 : **
  116211                 : ** It is considered impossible for an index to use more than 1024 levels. In 
  116212                 : ** theory though this may happen, but only after at least 
  116213                 : ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
  116214                 : */
  116215                 : #define FTS3_SEGDIR_MAXLEVEL      1024
  116216                 : #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
  116217                 : 
  116218                 : /*
  116219                 : ** The testcase() macro is only used by the amalgamation.  If undefined,
  116220                 : ** make it a no-op.
  116221                 : */
  116222                 : #ifndef testcase
  116223                 : # define testcase(X)
  116224                 : #endif
  116225                 : 
  116226                 : /*
  116227                 : ** Terminator values for position-lists and column-lists.
  116228                 : */
  116229                 : #define POS_COLUMN  (1)     /* Column-list terminator */
  116230                 : #define POS_END     (0)     /* Position-list terminator */ 
  116231                 : 
  116232                 : /*
  116233                 : ** This section provides definitions to allow the
  116234                 : ** FTS3 extension to be compiled outside of the 
  116235                 : ** amalgamation.
  116236                 : */
  116237                 : #ifndef SQLITE_AMALGAMATION
  116238                 : /*
  116239                 : ** Macros indicating that conditional expressions are always true or
  116240                 : ** false.
  116241                 : */
  116242                 : #ifdef SQLITE_COVERAGE_TEST
  116243                 : # define ALWAYS(x) (1)
  116244                 : # define NEVER(X)  (0)
  116245                 : #else
  116246                 : # define ALWAYS(x) (x)
  116247                 : # define NEVER(X)  (x)
  116248                 : #endif
  116249                 : 
  116250                 : /*
  116251                 : ** Internal types used by SQLite.
  116252                 : */
  116253                 : typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
  116254                 : typedef short int i16;            /* 2-byte (or larger) signed integer */
  116255                 : typedef unsigned int u32;         /* 4-byte unsigned integer */
  116256                 : typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
  116257                 : 
  116258                 : /*
  116259                 : ** Macro used to suppress compiler warnings for unused parameters.
  116260                 : */
  116261                 : #define UNUSED_PARAMETER(x) (void)(x)
  116262                 : 
  116263                 : /*
  116264                 : ** Activate assert() only if SQLITE_TEST is enabled.
  116265                 : */
  116266                 : #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
  116267                 : # define NDEBUG 1
  116268                 : #endif
  116269                 : 
  116270                 : /*
  116271                 : ** The TESTONLY macro is used to enclose variable declarations or
  116272                 : ** other bits of code that are needed to support the arguments
  116273                 : ** within testcase() and assert() macros.
  116274                 : */
  116275                 : #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  116276                 : # define TESTONLY(X)  X
  116277                 : #else
  116278                 : # define TESTONLY(X)
  116279                 : #endif
  116280                 : 
  116281                 : #endif /* SQLITE_AMALGAMATION */
  116282                 : 
  116283                 : #ifdef SQLITE_DEBUG
  116284                 : SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
  116285                 : # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
  116286                 : #else
  116287                 : # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
  116288                 : #endif
  116289                 : 
  116290                 : typedef struct Fts3Table Fts3Table;
  116291                 : typedef struct Fts3Cursor Fts3Cursor;
  116292                 : typedef struct Fts3Expr Fts3Expr;
  116293                 : typedef struct Fts3Phrase Fts3Phrase;
  116294                 : typedef struct Fts3PhraseToken Fts3PhraseToken;
  116295                 : 
  116296                 : typedef struct Fts3Doclist Fts3Doclist;
  116297                 : typedef struct Fts3SegFilter Fts3SegFilter;
  116298                 : typedef struct Fts3DeferredToken Fts3DeferredToken;
  116299                 : typedef struct Fts3SegReader Fts3SegReader;
  116300                 : typedef struct Fts3MultiSegReader Fts3MultiSegReader;
  116301                 : 
  116302                 : /*
  116303                 : ** A connection to a fulltext index is an instance of the following
  116304                 : ** structure. The xCreate and xConnect methods create an instance
  116305                 : ** of this structure and xDestroy and xDisconnect free that instance.
  116306                 : ** All other methods receive a pointer to the structure as one of their
  116307                 : ** arguments.
  116308                 : */
  116309                 : struct Fts3Table {
  116310                 :   sqlite3_vtab base;              /* Base class used by SQLite core */
  116311                 :   sqlite3 *db;                    /* The database connection */
  116312                 :   const char *zDb;                /* logical database name */
  116313                 :   const char *zName;              /* virtual table name */
  116314                 :   int nColumn;                    /* number of named columns in virtual table */
  116315                 :   char **azColumn;                /* column names.  malloced */
  116316                 :   sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
  116317                 :   char *zContentTbl;              /* content=xxx option, or NULL */
  116318                 : 
  116319                 :   /* Precompiled statements used by the implementation. Each of these 
  116320                 :   ** statements is run and reset within a single virtual table API call. 
  116321                 :   */
  116322                 :   sqlite3_stmt *aStmt[27];
  116323                 : 
  116324                 :   char *zReadExprlist;
  116325                 :   char *zWriteExprlist;
  116326                 : 
  116327                 :   int nNodeSize;                  /* Soft limit for node size */
  116328                 :   u8 bHasStat;                    /* True if %_stat table exists */
  116329                 :   u8 bHasDocsize;                 /* True if %_docsize table exists */
  116330                 :   u8 bDescIdx;                    /* True if doclists are in reverse order */
  116331                 :   int nPgsz;                      /* Page size for host database */
  116332                 :   char *zSegmentsTbl;             /* Name of %_segments table */
  116333                 :   sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
  116334                 : 
  116335                 :   /* TODO: Fix the first paragraph of this comment.
  116336                 :   **
  116337                 :   ** The following hash table is used to buffer pending index updates during
  116338                 :   ** transactions. Variable nPendingData estimates the memory size of the 
  116339                 :   ** pending data, including hash table overhead, but not malloc overhead. 
  116340                 :   ** When nPendingData exceeds nMaxPendingData, the buffer is flushed 
  116341                 :   ** automatically. Variable iPrevDocid is the docid of the most recently
  116342                 :   ** inserted record.
  116343                 :   **
  116344                 :   ** A single FTS4 table may have multiple full-text indexes. For each index
  116345                 :   ** there is an entry in the aIndex[] array. Index 0 is an index of all the
  116346                 :   ** terms that appear in the document set. Each subsequent index in aIndex[]
  116347                 :   ** is an index of prefixes of a specific length.
  116348                 :   */
  116349                 :   int nIndex;                     /* Size of aIndex[] */
  116350                 :   struct Fts3Index {
  116351                 :     int nPrefix;                  /* Prefix length (0 for main terms index) */
  116352                 :     Fts3Hash hPending;            /* Pending terms table for this index */
  116353                 :   } *aIndex;
  116354                 :   int nMaxPendingData;            /* Max pending data before flush to disk */
  116355                 :   int nPendingData;               /* Current bytes of pending data */
  116356                 :   sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
  116357                 : 
  116358                 : #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  116359                 :   /* State variables used for validating that the transaction control
  116360                 :   ** methods of the virtual table are called at appropriate times.  These
  116361                 :   ** values do not contribution to the FTS computation; they are used for
  116362                 :   ** verifying the SQLite core.
  116363                 :   */
  116364                 :   int inTransaction;     /* True after xBegin but before xCommit/xRollback */
  116365                 :   int mxSavepoint;       /* Largest valid xSavepoint integer */
  116366                 : #endif
  116367                 : };
  116368                 : 
  116369                 : /*
  116370                 : ** When the core wants to read from the virtual table, it creates a
  116371                 : ** virtual table cursor (an instance of the following structure) using
  116372                 : ** the xOpen method. Cursors are destroyed using the xClose method.
  116373                 : */
  116374                 : struct Fts3Cursor {
  116375                 :   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
  116376                 :   i16 eSearch;                    /* Search strategy (see below) */
  116377                 :   u8 isEof;                       /* True if at End Of Results */
  116378                 :   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
  116379                 :   sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
  116380                 :   Fts3Expr *pExpr;                /* Parsed MATCH query string */
  116381                 :   int nPhrase;                    /* Number of matchable phrases in query */
  116382                 :   Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
  116383                 :   sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
  116384                 :   char *pNextId;                  /* Pointer into the body of aDoclist */
  116385                 :   char *aDoclist;                 /* List of docids for full-text queries */
  116386                 :   int nDoclist;                   /* Size of buffer at aDoclist */
  116387                 :   u8 bDesc;                       /* True to sort in descending order */
  116388                 :   int eEvalmode;                  /* An FTS3_EVAL_XX constant */
  116389                 :   int nRowAvg;                    /* Average size of database rows, in pages */
  116390                 :   sqlite3_int64 nDoc;             /* Documents in table */
  116391                 : 
  116392                 :   int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
  116393                 :   u32 *aMatchinfo;                /* Information about most recent match */
  116394                 :   int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
  116395                 :   char *zMatchinfo;               /* Matchinfo specification */
  116396                 : };
  116397                 : 
  116398                 : #define FTS3_EVAL_FILTER    0
  116399                 : #define FTS3_EVAL_NEXT      1
  116400                 : #define FTS3_EVAL_MATCHINFO 2
  116401                 : 
  116402                 : /*
  116403                 : ** The Fts3Cursor.eSearch member is always set to one of the following.
  116404                 : ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
  116405                 : ** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
  116406                 : ** of the column to be searched.  For example, in
  116407                 : **
  116408                 : **     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
  116409                 : **     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
  116410                 : ** 
  116411                 : ** Because the LHS of the MATCH operator is 2nd column "b",
  116412                 : ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
  116413                 : ** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1" 
  116414                 : ** indicating that all columns should be searched,
  116415                 : ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
  116416                 : */
  116417                 : #define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
  116418                 : #define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
  116419                 : #define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
  116420                 : 
  116421                 : 
  116422                 : struct Fts3Doclist {
  116423                 :   char *aAll;                    /* Array containing doclist (or NULL) */
  116424                 :   int nAll;                      /* Size of a[] in bytes */
  116425                 :   char *pNextDocid;              /* Pointer to next docid */
  116426                 : 
  116427                 :   sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
  116428                 :   int bFreeList;                 /* True if pList should be sqlite3_free()d */
  116429                 :   char *pList;                   /* Pointer to position list following iDocid */
  116430                 :   int nList;                     /* Length of position list */
  116431                 : };
  116432                 : 
  116433                 : /*
  116434                 : ** A "phrase" is a sequence of one or more tokens that must match in
  116435                 : ** sequence.  A single token is the base case and the most common case.
  116436                 : ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
  116437                 : ** nToken will be the number of tokens in the string.
  116438                 : */
  116439                 : struct Fts3PhraseToken {
  116440                 :   char *z;                        /* Text of the token */
  116441                 :   int n;                          /* Number of bytes in buffer z */
  116442                 :   int isPrefix;                   /* True if token ends with a "*" character */
  116443                 :   int bFirst;                     /* True if token must appear at position 0 */
  116444                 : 
  116445                 :   /* Variables above this point are populated when the expression is
  116446                 :   ** parsed (by code in fts3_expr.c). Below this point the variables are
  116447                 :   ** used when evaluating the expression. */
  116448                 :   Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
  116449                 :   Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
  116450                 : };
  116451                 : 
  116452                 : struct Fts3Phrase {
  116453                 :   /* Cache of doclist for this phrase. */
  116454                 :   Fts3Doclist doclist;
  116455                 :   int bIncr;                 /* True if doclist is loaded incrementally */
  116456                 :   int iDoclistToken;
  116457                 : 
  116458                 :   /* Variables below this point are populated by fts3_expr.c when parsing 
  116459                 :   ** a MATCH expression. Everything above is part of the evaluation phase. 
  116460                 :   */
  116461                 :   int nToken;                /* Number of tokens in the phrase */
  116462                 :   int iColumn;               /* Index of column this phrase must match */
  116463                 :   Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
  116464                 : };
  116465                 : 
  116466                 : /*
  116467                 : ** A tree of these objects forms the RHS of a MATCH operator.
  116468                 : **
  116469                 : ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist 
  116470                 : ** points to a malloced buffer, size nDoclist bytes, containing the results 
  116471                 : ** of this phrase query in FTS3 doclist format. As usual, the initial 
  116472                 : ** "Length" field found in doclists stored on disk is omitted from this 
  116473                 : ** buffer.
  116474                 : **
  116475                 : ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
  116476                 : ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
  116477                 : ** where nCol is the number of columns in the queried FTS table. The array
  116478                 : ** is populated as follows:
  116479                 : **
  116480                 : **   aMI[iCol*3 + 0] = Undefined
  116481                 : **   aMI[iCol*3 + 1] = Number of occurrences
  116482                 : **   aMI[iCol*3 + 2] = Number of rows containing at least one instance
  116483                 : **
  116484                 : ** The aMI array is allocated using sqlite3_malloc(). It should be freed 
  116485                 : ** when the expression node is.
  116486                 : */
  116487                 : struct Fts3Expr {
  116488                 :   int eType;                 /* One of the FTSQUERY_XXX values defined below */
  116489                 :   int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
  116490                 :   Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
  116491                 :   Fts3Expr *pLeft;           /* Left operand */
  116492                 :   Fts3Expr *pRight;          /* Right operand */
  116493                 :   Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
  116494                 : 
  116495                 :   /* The following are used by the fts3_eval.c module. */
  116496                 :   sqlite3_int64 iDocid;      /* Current docid */
  116497                 :   u8 bEof;                   /* True this expression is at EOF already */
  116498                 :   u8 bStart;                 /* True if iDocid is valid */
  116499                 :   u8 bDeferred;              /* True if this expression is entirely deferred */
  116500                 : 
  116501                 :   u32 *aMI;
  116502                 : };
  116503                 : 
  116504                 : /*
  116505                 : ** Candidate values for Fts3Query.eType. Note that the order of the first
  116506                 : ** four values is in order of precedence when parsing expressions. For 
  116507                 : ** example, the following:
  116508                 : **
  116509                 : **   "a OR b AND c NOT d NEAR e"
  116510                 : **
  116511                 : ** is equivalent to:
  116512                 : **
  116513                 : **   "a OR (b AND (c NOT (d NEAR e)))"
  116514                 : */
  116515                 : #define FTSQUERY_NEAR   1
  116516                 : #define FTSQUERY_NOT    2
  116517                 : #define FTSQUERY_AND    3
  116518                 : #define FTSQUERY_OR     4
  116519                 : #define FTSQUERY_PHRASE 5
  116520                 : 
  116521                 : 
  116522                 : /* fts3_write.c */
  116523                 : SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
  116524                 : SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
  116525                 : SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
  116526                 : SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
  116527                 : SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64,
  116528                 :   sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
  116529                 : SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
  116530                 :   Fts3Table*,int,const char*,int,int,Fts3SegReader**);
  116531                 : SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
  116532                 : SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, sqlite3_stmt **);
  116533                 : SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *);
  116534                 : SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
  116535                 : 
  116536                 : SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
  116537                 : SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
  116538                 : 
  116539                 : SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
  116540                 : SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
  116541                 : SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
  116542                 : SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
  116543                 : SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
  116544                 : 
  116545                 : /* Special values interpreted by sqlite3SegReaderCursor() */
  116546                 : #define FTS3_SEGCURSOR_PENDING        -1
  116547                 : #define FTS3_SEGCURSOR_ALL            -2
  116548                 : 
  116549                 : SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
  116550                 : SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
  116551                 : SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
  116552                 : 
  116553                 : SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
  116554                 :     Fts3Table *, int, int, const char *, int, int, int, Fts3MultiSegReader *);
  116555                 : 
  116556                 : /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
  116557                 : #define FTS3_SEGMENT_REQUIRE_POS   0x00000001
  116558                 : #define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
  116559                 : #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
  116560                 : #define FTS3_SEGMENT_PREFIX        0x00000008
  116561                 : #define FTS3_SEGMENT_SCAN          0x00000010
  116562                 : #define FTS3_SEGMENT_FIRST         0x00000020
  116563                 : 
  116564                 : /* Type passed as 4th argument to SegmentReaderIterate() */
  116565                 : struct Fts3SegFilter {
  116566                 :   const char *zTerm;
  116567                 :   int nTerm;
  116568                 :   int iCol;
  116569                 :   int flags;
  116570                 : };
  116571                 : 
  116572                 : struct Fts3MultiSegReader {
  116573                 :   /* Used internally by sqlite3Fts3SegReaderXXX() calls */
  116574                 :   Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
  116575                 :   int nSegment;                   /* Size of apSegment array */
  116576                 :   int nAdvance;                   /* How many seg-readers to advance */
  116577                 :   Fts3SegFilter *pFilter;         /* Pointer to filter object */
  116578                 :   char *aBuffer;                  /* Buffer to merge doclists in */
  116579                 :   int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
  116580                 : 
  116581                 :   int iColFilter;                 /* If >=0, filter for this column */
  116582                 :   int bRestart;
  116583                 : 
  116584                 :   /* Used by fts3.c only. */
  116585                 :   int nCost;                      /* Cost of running iterator */
  116586                 :   int bLookup;                    /* True if a lookup of a single entry. */
  116587                 : 
  116588                 :   /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
  116589                 :   char *zTerm;                    /* Pointer to term buffer */
  116590                 :   int nTerm;                      /* Size of zTerm in bytes */
  116591                 :   char *aDoclist;                 /* Pointer to doclist buffer */
  116592                 :   int nDoclist;                   /* Size of aDoclist[] in bytes */
  116593                 : };
  116594                 : 
  116595                 : /* fts3.c */
  116596                 : SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
  116597                 : SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
  116598                 : SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
  116599                 : SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
  116600                 : SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
  116601                 : SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
  116602                 : SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
  116603                 : SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
  116604                 : 
  116605                 : /* fts3_tokenizer.c */
  116606                 : SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
  116607                 : SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
  116608                 : SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, 
  116609                 :     sqlite3_tokenizer **, char **
  116610                 : );
  116611                 : SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
  116612                 : 
  116613                 : /* fts3_snippet.c */
  116614                 : SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
  116615                 : SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
  116616                 :   const char *, const char *, int, int
  116617                 : );
  116618                 : SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
  116619                 : 
  116620                 : /* fts3_expr.c */
  116621                 : SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, 
  116622                 :   char **, int, int, int, const char *, int, Fts3Expr **
  116623                 : );
  116624                 : SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
  116625                 : #ifdef SQLITE_TEST
  116626                 : SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
  116627                 : SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
  116628                 : #endif
  116629                 : 
  116630                 : /* fts3_aux.c */
  116631                 : SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
  116632                 : 
  116633                 : SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
  116634                 : 
  116635                 : SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
  116636                 :     Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
  116637                 : SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
  116638                 :     Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
  116639                 : SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol); 
  116640                 : SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
  116641                 : SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
  116642                 : 
  116643                 : SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
  116644                 : 
  116645                 : #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
  116646                 : #endif /* _FTSINT_H */
  116647                 : 
  116648                 : /************** End of fts3Int.h *********************************************/
  116649                 : /************** Continuing where we left off in fts3.c ***********************/
  116650                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  116651                 : 
  116652                 : #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
  116653                 : # define SQLITE_CORE 1
  116654                 : #endif
  116655                 : 
  116656                 : /* #include <assert.h> */
  116657                 : /* #include <stdlib.h> */
  116658                 : /* #include <stddef.h> */
  116659                 : /* #include <stdio.h> */
  116660                 : /* #include <string.h> */
  116661                 : /* #include <stdarg.h> */
  116662                 : 
  116663                 : #ifndef SQLITE_CORE 
  116664                 :   SQLITE_EXTENSION_INIT1
  116665                 : #endif
  116666                 : 
  116667                 : static int fts3EvalNext(Fts3Cursor *pCsr);
  116668                 : static int fts3EvalStart(Fts3Cursor *pCsr);
  116669                 : static int fts3TermSegReaderCursor(
  116670                 :     Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
  116671                 : 
  116672                 : /* 
  116673                 : ** Write a 64-bit variable-length integer to memory starting at p[0].
  116674                 : ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
  116675                 : ** The number of bytes written is returned.
  116676                 : */
  116677             136 : SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
  116678             136 :   unsigned char *q = (unsigned char *) p;
  116679             136 :   sqlite_uint64 vu = v;
  116680                 :   do{
  116681             136 :     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
  116682             136 :     vu >>= 7;
  116683             136 :   }while( vu!=0 );
  116684             136 :   q[-1] &= 0x7f;  /* turn off high bit in final byte */
  116685             136 :   assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
  116686             136 :   return (int) (q - (unsigned char *)p);
  116687                 : }
  116688                 : 
  116689                 : /* 
  116690                 : ** Read a 64-bit variable-length integer from memory starting at p[0].
  116691                 : ** Return the number of bytes read, or 0 on error.
  116692                 : ** The value is stored in *v.
  116693                 : */
  116694              50 : SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
  116695              50 :   const unsigned char *q = (const unsigned char *) p;
  116696              50 :   sqlite_uint64 x = 0, y = 1;
  116697             100 :   while( (*q&0x80)==0x80 && q-(unsigned char *)p<FTS3_VARINT_MAX ){
  116698               0 :     x += y * (*q++ & 0x7f);
  116699               0 :     y <<= 7;
  116700                 :   }
  116701              50 :   x += y * (*q++);
  116702              50 :   *v = (sqlite_int64) x;
  116703              50 :   return (int) (q - (unsigned char *)p);
  116704                 : }
  116705                 : 
  116706                 : /*
  116707                 : ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
  116708                 : ** 32-bit integer before it is returned.
  116709                 : */
  116710              48 : SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
  116711                 :  sqlite_int64 i;
  116712              48 :  int ret = sqlite3Fts3GetVarint(p, &i);
  116713              48 :  *pi = (int) i;
  116714              48 :  return ret;
  116715                 : }
  116716                 : 
  116717                 : /*
  116718                 : ** Return the number of bytes required to encode v as a varint
  116719                 : */
  116720              60 : SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
  116721              60 :   int i = 0;
  116722                 :   do{
  116723              60 :     i++;
  116724              60 :     v >>= 7;
  116725              60 :   }while( v!=0 );
  116726              60 :   return i;
  116727                 : }
  116728                 : 
  116729                 : /*
  116730                 : ** Convert an SQL-style quoted string into a normal string by removing
  116731                 : ** the quote characters.  The conversion is done in-place.  If the
  116732                 : ** input does not begin with a quote character, then this routine
  116733                 : ** is a no-op.
  116734                 : **
  116735                 : ** Examples:
  116736                 : **
  116737                 : **     "abc"   becomes   abc
  116738                 : **     'xyz'   becomes   xyz
  116739                 : **     [pqr]   becomes   pqr
  116740                 : **     `mno`   becomes   mno
  116741                 : **
  116742                 : */
  116743               3 : SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
  116744                 :   char quote;                     /* Quote character (if any ) */
  116745                 : 
  116746               3 :   quote = z[0];
  116747               3 :   if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
  116748               0 :     int iIn = 1;                  /* Index of next byte to read from input */
  116749               0 :     int iOut = 0;                 /* Index of next byte to write to output */
  116750                 : 
  116751                 :     /* If the first byte was a '[', then the close-quote character is a ']' */
  116752               0 :     if( quote=='[' ) quote = ']';  
  116753                 : 
  116754               0 :     while( ALWAYS(z[iIn]) ){
  116755               0 :       if( z[iIn]==quote ){
  116756               0 :         if( z[iIn+1]!=quote ) break;
  116757               0 :         z[iOut++] = quote;
  116758               0 :         iIn += 2;
  116759                 :       }else{
  116760               0 :         z[iOut++] = z[iIn++];
  116761                 :       }
  116762                 :     }
  116763               0 :     z[iOut] = '\0';
  116764                 :   }
  116765               3 : }
  116766                 : 
  116767                 : /*
  116768                 : ** Read a single varint from the doclist at *pp and advance *pp to point
  116769                 : ** to the first byte past the end of the varint.  Add the value of the varint
  116770                 : ** to *pVal.
  116771                 : */
  116772               0 : static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
  116773                 :   sqlite3_int64 iVal;
  116774               0 :   *pp += sqlite3Fts3GetVarint(*pp, &iVal);
  116775               0 :   *pVal += iVal;
  116776               0 : }
  116777                 : 
  116778                 : /*
  116779                 : ** When this function is called, *pp points to the first byte following a
  116780                 : ** varint that is part of a doclist (or position-list, or any other list
  116781                 : ** of varints). This function moves *pp to point to the start of that varint,
  116782                 : ** and sets *pVal by the varint value.
  116783                 : **
  116784                 : ** Argument pStart points to the first byte of the doclist that the
  116785                 : ** varint is part of.
  116786                 : */
  116787               0 : static void fts3GetReverseVarint(
  116788                 :   char **pp, 
  116789                 :   char *pStart, 
  116790                 :   sqlite3_int64 *pVal
  116791                 : ){
  116792                 :   sqlite3_int64 iVal;
  116793                 :   char *p;
  116794                 : 
  116795                 :   /* Pointer p now points at the first byte past the varint we are 
  116796                 :   ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
  116797                 :   ** clear on character p[-1]. */
  116798               0 :   for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
  116799               0 :   p++;
  116800               0 :   *pp = p;
  116801                 : 
  116802               0 :   sqlite3Fts3GetVarint(p, &iVal);
  116803               0 :   *pVal = iVal;
  116804               0 : }
  116805                 : 
  116806                 : /*
  116807                 : ** The xDisconnect() virtual table method.
  116808                 : */
  116809               1 : static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
  116810               1 :   Fts3Table *p = (Fts3Table *)pVtab;
  116811                 :   int i;
  116812                 : 
  116813               1 :   assert( p->nPendingData==0 );
  116814               1 :   assert( p->pSegments==0 );
  116815                 : 
  116816                 :   /* Free any prepared statements held */
  116817              28 :   for(i=0; i<SizeofArray(p->aStmt); i++){
  116818              27 :     sqlite3_finalize(p->aStmt[i]);
  116819                 :   }
  116820               1 :   sqlite3_free(p->zSegmentsTbl);
  116821               1 :   sqlite3_free(p->zReadExprlist);
  116822               1 :   sqlite3_free(p->zWriteExprlist);
  116823               1 :   sqlite3_free(p->zContentTbl);
  116824                 : 
  116825                 :   /* Invoke the tokenizer destructor to free the tokenizer. */
  116826               1 :   p->pTokenizer->pModule->xDestroy(p->pTokenizer);
  116827                 : 
  116828               1 :   sqlite3_free(p);
  116829               1 :   return SQLITE_OK;
  116830                 : }
  116831                 : 
  116832                 : /*
  116833                 : ** Construct one or more SQL statements from the format string given
  116834                 : ** and then evaluate those statements. The success code is written
  116835                 : ** into *pRc.
  116836                 : **
  116837                 : ** If *pRc is initially non-zero then this routine is a no-op.
  116838                 : */
  116839               3 : static void fts3DbExec(
  116840                 :   int *pRc,              /* Success code */
  116841                 :   sqlite3 *db,           /* Database in which to run SQL */
  116842                 :   const char *zFormat,   /* Format string for SQL */
  116843                 :   ...                    /* Arguments to the format string */
  116844                 : ){
  116845                 :   va_list ap;
  116846                 :   char *zSql;
  116847               3 :   if( *pRc ) return;
  116848               3 :   va_start(ap, zFormat);
  116849               3 :   zSql = sqlite3_vmprintf(zFormat, ap);
  116850               3 :   va_end(ap);
  116851               3 :   if( zSql==0 ){
  116852               0 :     *pRc = SQLITE_NOMEM;
  116853                 :   }else{
  116854               3 :     *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
  116855               3 :     sqlite3_free(zSql);
  116856                 :   }
  116857                 : }
  116858                 : 
  116859                 : /*
  116860                 : ** The xDestroy() virtual table method.
  116861                 : */
  116862               0 : static int fts3DestroyMethod(sqlite3_vtab *pVtab){
  116863               0 :   Fts3Table *p = (Fts3Table *)pVtab;
  116864               0 :   int rc = SQLITE_OK;              /* Return code */
  116865               0 :   const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
  116866               0 :   sqlite3 *db = p->db;             /* Database handle */
  116867                 : 
  116868                 :   /* Drop the shadow tables */
  116869               0 :   if( p->zContentTbl==0 ){
  116870               0 :     fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
  116871                 :   }
  116872               0 :   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
  116873               0 :   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
  116874               0 :   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
  116875               0 :   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
  116876                 : 
  116877                 :   /* If everything has worked, invoke fts3DisconnectMethod() to free the
  116878                 :   ** memory associated with the Fts3Table structure and return SQLITE_OK.
  116879                 :   ** Otherwise, return an SQLite error code.
  116880                 :   */
  116881               0 :   return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
  116882                 : }
  116883                 : 
  116884                 : 
  116885                 : /*
  116886                 : ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
  116887                 : ** passed as the first argument. This is done as part of the xConnect()
  116888                 : ** and xCreate() methods.
  116889                 : **
  116890                 : ** If *pRc is non-zero when this function is called, it is a no-op. 
  116891                 : ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
  116892                 : ** before returning.
  116893                 : */
  116894               1 : static void fts3DeclareVtab(int *pRc, Fts3Table *p){
  116895               1 :   if( *pRc==SQLITE_OK ){
  116896                 :     int i;                        /* Iterator variable */
  116897                 :     int rc;                       /* Return code */
  116898                 :     char *zSql;                   /* SQL statement passed to declare_vtab() */
  116899                 :     char *zCols;                  /* List of user defined columns */
  116900                 : 
  116901               1 :     sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
  116902                 : 
  116903                 :     /* Create a list of user columns for the virtual table */
  116904               1 :     zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
  116905               2 :     for(i=1; zCols && i<p->nColumn; i++){
  116906               1 :       zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
  116907                 :     }
  116908                 : 
  116909                 :     /* Create the whole "CREATE TABLE" statement to pass to SQLite */
  116910               1 :     zSql = sqlite3_mprintf(
  116911                 :         "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName
  116912                 :     );
  116913               1 :     if( !zCols || !zSql ){
  116914               0 :       rc = SQLITE_NOMEM;
  116915                 :     }else{
  116916               1 :       rc = sqlite3_declare_vtab(p->db, zSql);
  116917                 :     }
  116918                 : 
  116919               1 :     sqlite3_free(zSql);
  116920               1 :     sqlite3_free(zCols);
  116921               1 :     *pRc = rc;
  116922                 :   }
  116923               1 : }
  116924                 : 
  116925                 : /*
  116926                 : ** Create the backing store tables (%_content, %_segments and %_segdir)
  116927                 : ** required by the FTS3 table passed as the only argument. This is done
  116928                 : ** as part of the vtab xCreate() method.
  116929                 : **
  116930                 : ** If the p->bHasDocsize boolean is true (indicating that this is an
  116931                 : ** FTS4 table, not an FTS3 table) then also create the %_docsize and
  116932                 : ** %_stat tables required by FTS4.
  116933                 : */
  116934               1 : static int fts3CreateTables(Fts3Table *p){
  116935               1 :   int rc = SQLITE_OK;             /* Return code */
  116936                 :   int i;                          /* Iterator variable */
  116937               1 :   sqlite3 *db = p->db;            /* The database connection */
  116938                 : 
  116939               1 :   if( p->zContentTbl==0 ){
  116940                 :     char *zContentCols;           /* Columns of %_content table */
  116941                 : 
  116942                 :     /* Create a list of user columns for the content table */
  116943               1 :     zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
  116944               3 :     for(i=0; zContentCols && i<p->nColumn; i++){
  116945               2 :       char *z = p->azColumn[i];
  116946               2 :       zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
  116947                 :     }
  116948               1 :     if( zContentCols==0 ) rc = SQLITE_NOMEM;
  116949                 :   
  116950                 :     /* Create the content table */
  116951               1 :     fts3DbExec(&rc, db, 
  116952                 :        "CREATE TABLE %Q.'%q_content'(%s)",
  116953                 :        p->zDb, p->zName, zContentCols
  116954                 :     );
  116955               1 :     sqlite3_free(zContentCols);
  116956                 :   }
  116957                 : 
  116958                 :   /* Create other tables */
  116959               1 :   fts3DbExec(&rc, db, 
  116960                 :       "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
  116961                 :       p->zDb, p->zName
  116962                 :   );
  116963               1 :   fts3DbExec(&rc, db, 
  116964                 :       "CREATE TABLE %Q.'%q_segdir'("
  116965                 :         "level INTEGER,"
  116966                 :         "idx INTEGER,"
  116967                 :         "start_block INTEGER,"
  116968                 :         "leaves_end_block INTEGER,"
  116969                 :         "end_block INTEGER,"
  116970                 :         "root BLOB,"
  116971                 :         "PRIMARY KEY(level, idx)"
  116972                 :       ");",
  116973                 :       p->zDb, p->zName
  116974                 :   );
  116975               1 :   if( p->bHasDocsize ){
  116976               0 :     fts3DbExec(&rc, db, 
  116977                 :         "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
  116978                 :         p->zDb, p->zName
  116979                 :     );
  116980                 :   }
  116981               1 :   if( p->bHasStat ){
  116982               0 :     fts3DbExec(&rc, db, 
  116983                 :         "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);",
  116984                 :         p->zDb, p->zName
  116985                 :     );
  116986                 :   }
  116987               1 :   return rc;
  116988                 : }
  116989                 : 
  116990                 : /*
  116991                 : ** Store the current database page-size in bytes in p->nPgsz.
  116992                 : **
  116993                 : ** If *pRc is non-zero when this function is called, it is a no-op. 
  116994                 : ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
  116995                 : ** before returning.
  116996                 : */
  116997               1 : static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
  116998               1 :   if( *pRc==SQLITE_OK ){
  116999                 :     int rc;                       /* Return code */
  117000                 :     char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
  117001                 :     sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
  117002                 :   
  117003               1 :     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
  117004               1 :     if( !zSql ){
  117005               0 :       rc = SQLITE_NOMEM;
  117006                 :     }else{
  117007               1 :       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
  117008               1 :       if( rc==SQLITE_OK ){
  117009               1 :         sqlite3_step(pStmt);
  117010               1 :         p->nPgsz = sqlite3_column_int(pStmt, 0);
  117011               1 :         rc = sqlite3_finalize(pStmt);
  117012               0 :       }else if( rc==SQLITE_AUTH ){
  117013               0 :         p->nPgsz = 1024;
  117014               0 :         rc = SQLITE_OK;
  117015                 :       }
  117016                 :     }
  117017               1 :     assert( p->nPgsz>0 || rc!=SQLITE_OK );
  117018               1 :     sqlite3_free(zSql);
  117019               1 :     *pRc = rc;
  117020                 :   }
  117021               1 : }
  117022                 : 
  117023                 : /*
  117024                 : ** "Special" FTS4 arguments are column specifications of the following form:
  117025                 : **
  117026                 : **   <key> = <value>
  117027                 : **
  117028                 : ** There may not be whitespace surrounding the "=" character. The <value> 
  117029                 : ** term may be quoted, but the <key> may not.
  117030                 : */
  117031               0 : static int fts3IsSpecialColumn(
  117032                 :   const char *z, 
  117033                 :   int *pnKey,
  117034                 :   char **pzValue
  117035                 : ){
  117036                 :   char *zValue;
  117037               0 :   const char *zCsr = z;
  117038                 : 
  117039               0 :   while( *zCsr!='=' ){
  117040               0 :     if( *zCsr=='\0' ) return 0;
  117041               0 :     zCsr++;
  117042                 :   }
  117043                 : 
  117044               0 :   *pnKey = (int)(zCsr-z);
  117045               0 :   zValue = sqlite3_mprintf("%s", &zCsr[1]);
  117046               0 :   if( zValue ){
  117047               0 :     sqlite3Fts3Dequote(zValue);
  117048                 :   }
  117049               0 :   *pzValue = zValue;
  117050               0 :   return 1;
  117051                 : }
  117052                 : 
  117053                 : /*
  117054                 : ** Append the output of a printf() style formatting to an existing string.
  117055                 : */
  117056               7 : static void fts3Appendf(
  117057                 :   int *pRc,                       /* IN/OUT: Error code */
  117058                 :   char **pz,                      /* IN/OUT: Pointer to string buffer */
  117059                 :   const char *zFormat,            /* Printf format string to append */
  117060                 :   ...                             /* Arguments for printf format string */
  117061                 : ){
  117062               7 :   if( *pRc==SQLITE_OK ){
  117063                 :     va_list ap;
  117064                 :     char *z;
  117065               7 :     va_start(ap, zFormat);
  117066               7 :     z = sqlite3_vmprintf(zFormat, ap);
  117067               7 :     va_end(ap);
  117068               7 :     if( z && *pz ){
  117069               5 :       char *z2 = sqlite3_mprintf("%s%s", *pz, z);
  117070               5 :       sqlite3_free(z);
  117071               5 :       z = z2;
  117072                 :     }
  117073               7 :     if( z==0 ) *pRc = SQLITE_NOMEM;
  117074               7 :     sqlite3_free(*pz);
  117075               7 :     *pz = z;
  117076                 :   }
  117077               7 : }
  117078                 : 
  117079                 : /*
  117080                 : ** Return a copy of input string zInput enclosed in double-quotes (") and
  117081                 : ** with all double quote characters escaped. For example:
  117082                 : **
  117083                 : **     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
  117084                 : **
  117085                 : ** The pointer returned points to memory obtained from sqlite3_malloc(). It
  117086                 : ** is the callers responsibility to call sqlite3_free() to release this
  117087                 : ** memory.
  117088                 : */
  117089               0 : static char *fts3QuoteId(char const *zInput){
  117090                 :   int nRet;
  117091                 :   char *zRet;
  117092               0 :   nRet = 2 + strlen(zInput)*2 + 1;
  117093               0 :   zRet = sqlite3_malloc(nRet);
  117094               0 :   if( zRet ){
  117095                 :     int i;
  117096               0 :     char *z = zRet;
  117097               0 :     *(z++) = '"';
  117098               0 :     for(i=0; zInput[i]; i++){
  117099               0 :       if( zInput[i]=='"' ) *(z++) = '"';
  117100               0 :       *(z++) = zInput[i];
  117101                 :     }
  117102               0 :     *(z++) = '"';
  117103               0 :     *(z++) = '\0';
  117104                 :   }
  117105               0 :   return zRet;
  117106                 : }
  117107                 : 
  117108                 : /*
  117109                 : ** Return a list of comma separated SQL expressions and a FROM clause that 
  117110                 : ** could be used in a SELECT statement such as the following:
  117111                 : **
  117112                 : **     SELECT <list of expressions> FROM %_content AS x ...
  117113                 : **
  117114                 : ** to return the docid, followed by each column of text data in order
  117115                 : ** from left to write. If parameter zFunc is not NULL, then instead of
  117116                 : ** being returned directly each column of text data is passed to an SQL
  117117                 : ** function named zFunc first. For example, if zFunc is "unzip" and the
  117118                 : ** table has the three user-defined columns "a", "b", and "c", the following
  117119                 : ** string is returned:
  117120                 : **
  117121                 : **     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
  117122                 : **
  117123                 : ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
  117124                 : ** is the responsibility of the caller to eventually free it.
  117125                 : **
  117126                 : ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
  117127                 : ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
  117128                 : ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
  117129                 : ** no error occurs, *pRc is left unmodified.
  117130                 : */
  117131               1 : static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
  117132               1 :   char *zRet = 0;
  117133               1 :   char *zFree = 0;
  117134                 :   char *zFunction;
  117135                 :   int i;
  117136                 : 
  117137               1 :   if( p->zContentTbl==0 ){
  117138               1 :     if( !zFunc ){
  117139               1 :       zFunction = "";
  117140                 :     }else{
  117141               0 :       zFree = zFunction = fts3QuoteId(zFunc);
  117142                 :     }
  117143               1 :     fts3Appendf(pRc, &zRet, "docid");
  117144               3 :     for(i=0; i<p->nColumn; i++){
  117145               2 :       fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
  117146                 :     }
  117147               1 :     sqlite3_free(zFree);
  117148                 :   }else{
  117149               0 :     fts3Appendf(pRc, &zRet, "rowid");
  117150               0 :     for(i=0; i<p->nColumn; i++){
  117151               0 :       fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
  117152                 :     }
  117153                 :   }
  117154               2 :   fts3Appendf(pRc, &zRet, "FROM '%q'.'%q%s' AS x", 
  117155                 :       p->zDb,
  117156               1 :       (p->zContentTbl ? p->zContentTbl : p->zName),
  117157               1 :       (p->zContentTbl ? "" : "_content")
  117158                 :   );
  117159               1 :   return zRet;
  117160                 : }
  117161                 : 
  117162                 : /*
  117163                 : ** Return a list of N comma separated question marks, where N is the number
  117164                 : ** of columns in the %_content table (one for the docid plus one for each
  117165                 : ** user-defined text column).
  117166                 : **
  117167                 : ** If argument zFunc is not NULL, then all but the first question mark
  117168                 : ** is preceded by zFunc and an open bracket, and followed by a closed
  117169                 : ** bracket. For example, if zFunc is "zip" and the FTS3 table has three 
  117170                 : ** user-defined text columns, the following string is returned:
  117171                 : **
  117172                 : **     "?, zip(?), zip(?), zip(?)"
  117173                 : **
  117174                 : ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
  117175                 : ** is the responsibility of the caller to eventually free it.
  117176                 : **
  117177                 : ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
  117178                 : ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
  117179                 : ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
  117180                 : ** no error occurs, *pRc is left unmodified.
  117181                 : */
  117182               1 : static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
  117183               1 :   char *zRet = 0;
  117184               1 :   char *zFree = 0;
  117185                 :   char *zFunction;
  117186                 :   int i;
  117187                 : 
  117188               1 :   if( !zFunc ){
  117189               1 :     zFunction = "";
  117190                 :   }else{
  117191               0 :     zFree = zFunction = fts3QuoteId(zFunc);
  117192                 :   }
  117193               1 :   fts3Appendf(pRc, &zRet, "?");
  117194               3 :   for(i=0; i<p->nColumn; i++){
  117195               2 :     fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
  117196                 :   }
  117197               1 :   sqlite3_free(zFree);
  117198               1 :   return zRet;
  117199                 : }
  117200                 : 
  117201                 : /*
  117202                 : ** This function interprets the string at (*pp) as a non-negative integer
  117203                 : ** value. It reads the integer and sets *pnOut to the value read, then 
  117204                 : ** sets *pp to point to the byte immediately following the last byte of
  117205                 : ** the integer value.
  117206                 : **
  117207                 : ** Only decimal digits ('0'..'9') may be part of an integer value. 
  117208                 : **
  117209                 : ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
  117210                 : ** the output value undefined. Otherwise SQLITE_OK is returned.
  117211                 : **
  117212                 : ** This function is used when parsing the "prefix=" FTS4 parameter.
  117213                 : */
  117214               0 : static int fts3GobbleInt(const char **pp, int *pnOut){
  117215                 :   const char *p;                  /* Iterator pointer */
  117216               0 :   int nInt = 0;                   /* Output value */
  117217                 : 
  117218               0 :   for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
  117219               0 :     nInt = nInt * 10 + (p[0] - '0');
  117220                 :   }
  117221               0 :   if( p==*pp ) return SQLITE_ERROR;
  117222               0 :   *pnOut = nInt;
  117223               0 :   *pp = p;
  117224               0 :   return SQLITE_OK;
  117225                 : }
  117226                 : 
  117227                 : /*
  117228                 : ** This function is called to allocate an array of Fts3Index structures
  117229                 : ** representing the indexes maintained by the current FTS table. FTS tables
  117230                 : ** always maintain the main "terms" index, but may also maintain one or
  117231                 : ** more "prefix" indexes, depending on the value of the "prefix=" parameter
  117232                 : ** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
  117233                 : **
  117234                 : ** Argument zParam is passed the value of the "prefix=" option if one was
  117235                 : ** specified, or NULL otherwise.
  117236                 : **
  117237                 : ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
  117238                 : ** the allocated array. *pnIndex is set to the number of elements in the
  117239                 : ** array. If an error does occur, an SQLite error code is returned.
  117240                 : **
  117241                 : ** Regardless of whether or not an error is returned, it is the responsibility
  117242                 : ** of the caller to call sqlite3_free() on the output array to free it.
  117243                 : */
  117244               1 : static int fts3PrefixParameter(
  117245                 :   const char *zParam,             /* ABC in prefix=ABC parameter to parse */
  117246                 :   int *pnIndex,                   /* OUT: size of *apIndex[] array */
  117247                 :   struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
  117248                 : ){
  117249                 :   struct Fts3Index *aIndex;       /* Allocated array */
  117250               1 :   int nIndex = 1;                 /* Number of entries in array */
  117251                 : 
  117252               1 :   if( zParam && zParam[0] ){
  117253                 :     const char *p;
  117254               0 :     nIndex++;
  117255               0 :     for(p=zParam; *p; p++){
  117256               0 :       if( *p==',' ) nIndex++;
  117257                 :     }
  117258                 :   }
  117259                 : 
  117260               1 :   aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
  117261               1 :   *apIndex = aIndex;
  117262               1 :   *pnIndex = nIndex;
  117263               1 :   if( !aIndex ){
  117264               0 :     return SQLITE_NOMEM;
  117265                 :   }
  117266                 : 
  117267               1 :   memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
  117268               1 :   if( zParam ){
  117269               0 :     const char *p = zParam;
  117270                 :     int i;
  117271               0 :     for(i=1; i<nIndex; i++){
  117272                 :       int nPrefix;
  117273               0 :       if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
  117274               0 :       aIndex[i].nPrefix = nPrefix;
  117275               0 :       p++;
  117276                 :     }
  117277                 :   }
  117278                 : 
  117279               1 :   return SQLITE_OK;
  117280                 : }
  117281                 : 
  117282                 : /*
  117283                 : ** This function is called when initializing an FTS4 table that uses the
  117284                 : ** content=xxx option. It determines the number of and names of the columns
  117285                 : ** of the new FTS4 table.
  117286                 : **
  117287                 : ** The third argument passed to this function is the value passed to the
  117288                 : ** config=xxx option (i.e. "xxx"). This function queries the database for
  117289                 : ** a table of that name. If found, the output variables are populated
  117290                 : ** as follows:
  117291                 : **
  117292                 : **   *pnCol:   Set to the number of columns table xxx has,
  117293                 : **
  117294                 : **   *pnStr:   Set to the total amount of space required to store a copy
  117295                 : **             of each columns name, including the nul-terminator.
  117296                 : **
  117297                 : **   *pazCol:  Set to point to an array of *pnCol strings. Each string is
  117298                 : **             the name of the corresponding column in table xxx. The array
  117299                 : **             and its contents are allocated using a single allocation. It
  117300                 : **             is the responsibility of the caller to free this allocation
  117301                 : **             by eventually passing the *pazCol value to sqlite3_free().
  117302                 : **
  117303                 : ** If the table cannot be found, an error code is returned and the output
  117304                 : ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
  117305                 : ** returned (and the output variables are undefined).
  117306                 : */
  117307               0 : static int fts3ContentColumns(
  117308                 :   sqlite3 *db,                    /* Database handle */
  117309                 :   const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
  117310                 :   const char *zTbl,               /* Name of content table */
  117311                 :   const char ***pazCol,           /* OUT: Malloc'd array of column names */
  117312                 :   int *pnCol,                     /* OUT: Size of array *pazCol */
  117313                 :   int *pnStr                      /* OUT: Bytes of string content */
  117314                 : ){
  117315               0 :   int rc = SQLITE_OK;             /* Return code */
  117316                 :   char *zSql;                     /* "SELECT *" statement on zTbl */  
  117317               0 :   sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
  117318                 : 
  117319               0 :   zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
  117320               0 :   if( !zSql ){
  117321               0 :     rc = SQLITE_NOMEM;
  117322                 :   }else{
  117323               0 :     rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
  117324                 :   }
  117325               0 :   sqlite3_free(zSql);
  117326                 : 
  117327               0 :   if( rc==SQLITE_OK ){
  117328                 :     const char **azCol;           /* Output array */
  117329               0 :     int nStr = 0;                 /* Size of all column names (incl. 0x00) */
  117330                 :     int nCol;                     /* Number of table columns */
  117331                 :     int i;                        /* Used to iterate through columns */
  117332                 : 
  117333                 :     /* Loop through the returned columns. Set nStr to the number of bytes of
  117334                 :     ** space required to store a copy of each column name, including the
  117335                 :     ** nul-terminator byte.  */
  117336               0 :     nCol = sqlite3_column_count(pStmt);
  117337               0 :     for(i=0; i<nCol; i++){
  117338               0 :       const char *zCol = sqlite3_column_name(pStmt, i);
  117339               0 :       nStr += strlen(zCol) + 1;
  117340                 :     }
  117341                 : 
  117342                 :     /* Allocate and populate the array to return. */
  117343               0 :     azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
  117344               0 :     if( azCol==0 ){
  117345               0 :       rc = SQLITE_NOMEM;
  117346                 :     }else{
  117347               0 :       char *p = (char *)&azCol[nCol];
  117348               0 :       for(i=0; i<nCol; i++){
  117349               0 :         const char *zCol = sqlite3_column_name(pStmt, i);
  117350               0 :         int n = strlen(zCol)+1;
  117351               0 :         memcpy(p, zCol, n);
  117352               0 :         azCol[i] = p;
  117353               0 :         p += n;
  117354                 :       }
  117355                 :     }
  117356               0 :     sqlite3_finalize(pStmt);
  117357                 : 
  117358                 :     /* Set the output variables. */
  117359               0 :     *pnCol = nCol;
  117360               0 :     *pnStr = nStr;
  117361               0 :     *pazCol = azCol;
  117362                 :   }
  117363                 : 
  117364               0 :   return rc;
  117365                 : }
  117366                 : 
  117367                 : /*
  117368                 : ** This function is the implementation of both the xConnect and xCreate
  117369                 : ** methods of the FTS3 virtual table.
  117370                 : **
  117371                 : ** The argv[] array contains the following:
  117372                 : **
  117373                 : **   argv[0]   -> module name  ("fts3" or "fts4")
  117374                 : **   argv[1]   -> database name
  117375                 : **   argv[2]   -> table name
  117376                 : **   argv[...] -> "column name" and other module argument fields.
  117377                 : */
  117378               1 : static int fts3InitVtab(
  117379                 :   int isCreate,                   /* True for xCreate, false for xConnect */
  117380                 :   sqlite3 *db,                    /* The SQLite database connection */
  117381                 :   void *pAux,                     /* Hash table containing tokenizers */
  117382                 :   int argc,                       /* Number of elements in argv array */
  117383                 :   const char * const *argv,       /* xCreate/xConnect argument array */
  117384                 :   sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
  117385                 :   char **pzErr                    /* Write any error message here */
  117386                 : ){
  117387               1 :   Fts3Hash *pHash = (Fts3Hash *)pAux;
  117388               1 :   Fts3Table *p = 0;               /* Pointer to allocated vtab */
  117389               1 :   int rc = SQLITE_OK;             /* Return code */
  117390                 :   int i;                          /* Iterator variable */
  117391                 :   int nByte;                      /* Size of allocation used for *p */
  117392                 :   int iCol;                       /* Column index */
  117393               1 :   int nString = 0;                /* Bytes required to hold all column names */
  117394               1 :   int nCol = 0;                   /* Number of columns in the FTS table */
  117395                 :   char *zCsr;                     /* Space for holding column names */
  117396                 :   int nDb;                        /* Bytes required to hold database name */
  117397                 :   int nName;                      /* Bytes required to hold table name */
  117398               1 :   int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
  117399                 :   const char **aCol;              /* Array of column names */
  117400               1 :   sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
  117401                 : 
  117402                 :   int nIndex;                     /* Size of aIndex[] array */
  117403               1 :   struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
  117404                 : 
  117405                 :   /* The results of parsing supported FTS4 key=value options: */
  117406               1 :   int bNoDocsize = 0;             /* True to omit %_docsize table */
  117407               1 :   int bDescIdx = 0;               /* True to store descending indexes */
  117408               1 :   char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
  117409               1 :   char *zCompress = 0;            /* compress=? parameter (or NULL) */
  117410               1 :   char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
  117411               1 :   char *zContent = 0;             /* content=? parameter (or NULL) */
  117412                 : 
  117413               1 :   assert( strlen(argv[0])==4 );
  117414               1 :   assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
  117415                 :        || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
  117416                 :   );
  117417                 : 
  117418               1 :   nDb = (int)strlen(argv[1]) + 1;
  117419               1 :   nName = (int)strlen(argv[2]) + 1;
  117420                 : 
  117421               1 :   aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) );
  117422               1 :   if( !aCol ) return SQLITE_NOMEM;
  117423               1 :   memset((void *)aCol, 0, sizeof(const char *) * (argc-2));
  117424                 : 
  117425                 :   /* Loop through all of the arguments passed by the user to the FTS3/4
  117426                 :   ** module (i.e. all the column names and special arguments). This loop
  117427                 :   ** does the following:
  117428                 :   **
  117429                 :   **   + Figures out the number of columns the FTSX table will have, and
  117430                 :   **     the number of bytes of space that must be allocated to store copies
  117431                 :   **     of the column names.
  117432                 :   **
  117433                 :   **   + If there is a tokenizer specification included in the arguments,
  117434                 :   **     initializes the tokenizer pTokenizer.
  117435                 :   */
  117436               3 :   for(i=3; rc==SQLITE_OK && i<argc; i++){
  117437               2 :     char const *z = argv[i];
  117438                 :     int nKey;
  117439                 :     char *zVal;
  117440                 : 
  117441                 :     /* Check if this is a tokenizer specification */
  117442               2 :     if( !pTokenizer 
  117443               2 :      && strlen(z)>8
  117444               1 :      && 0==sqlite3_strnicmp(z, "tokenize", 8) 
  117445               0 :      && 0==sqlite3Fts3IsIdChar(z[8])
  117446                 :     ){
  117447               0 :       rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
  117448                 :     }
  117449                 : 
  117450                 :     /* Check if it is an FTS4 special argument. */
  117451               2 :     else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
  117452                 :       struct Fts4Option {
  117453                 :         const char *zOpt;
  117454                 :         int nOpt;
  117455               0 :       } aFts4Opt[] = {
  117456                 :         { "matchinfo",   9 },     /* 0 -> MATCHINFO */
  117457                 :         { "prefix",      6 },     /* 1 -> PREFIX */
  117458                 :         { "compress",    8 },     /* 2 -> COMPRESS */
  117459                 :         { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
  117460                 :         { "order",       5 },     /* 4 -> ORDER */
  117461                 :         { "content",     7 }      /* 5 -> CONTENT */
  117462                 :       };
  117463                 : 
  117464                 :       int iOpt;
  117465               0 :       if( !zVal ){
  117466               0 :         rc = SQLITE_NOMEM;
  117467                 :       }else{
  117468               0 :         for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
  117469               0 :           struct Fts4Option *pOp = &aFts4Opt[iOpt];
  117470               0 :           if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
  117471               0 :             break;
  117472                 :           }
  117473                 :         }
  117474               0 :         if( iOpt==SizeofArray(aFts4Opt) ){
  117475               0 :           *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
  117476               0 :           rc = SQLITE_ERROR;
  117477                 :         }else{
  117478               0 :           switch( iOpt ){
  117479                 :             case 0:               /* MATCHINFO */
  117480               0 :               if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
  117481               0 :                 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
  117482               0 :                 rc = SQLITE_ERROR;
  117483                 :               }
  117484               0 :               bNoDocsize = 1;
  117485               0 :               break;
  117486                 : 
  117487                 :             case 1:               /* PREFIX */
  117488               0 :               sqlite3_free(zPrefix);
  117489               0 :               zPrefix = zVal;
  117490               0 :               zVal = 0;
  117491               0 :               break;
  117492                 : 
  117493                 :             case 2:               /* COMPRESS */
  117494               0 :               sqlite3_free(zCompress);
  117495               0 :               zCompress = zVal;
  117496               0 :               zVal = 0;
  117497               0 :               break;
  117498                 : 
  117499                 :             case 3:               /* UNCOMPRESS */
  117500               0 :               sqlite3_free(zUncompress);
  117501               0 :               zUncompress = zVal;
  117502               0 :               zVal = 0;
  117503               0 :               break;
  117504                 : 
  117505                 :             case 4:               /* ORDER */
  117506               0 :               if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) 
  117507               0 :                && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) 
  117508                 :               ){
  117509               0 :                 *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
  117510               0 :                 rc = SQLITE_ERROR;
  117511                 :               }
  117512               0 :               bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
  117513               0 :               break;
  117514                 : 
  117515                 :             default:              /* CONTENT */
  117516               0 :               assert( iOpt==5 );
  117517               0 :               sqlite3_free(zUncompress);
  117518               0 :               zContent = zVal;
  117519               0 :               zVal = 0;
  117520               0 :               break;
  117521                 :           }
  117522                 :         }
  117523               0 :         sqlite3_free(zVal);
  117524                 :       }
  117525                 :     }
  117526                 : 
  117527                 :     /* Otherwise, the argument is a column name. */
  117528                 :     else {
  117529               2 :       nString += (int)(strlen(z) + 1);
  117530               2 :       aCol[nCol++] = z;
  117531                 :     }
  117532                 :   }
  117533                 : 
  117534                 :   /* If a content=xxx option was specified, the following:
  117535                 :   **
  117536                 :   **   1. Ignore any compress= and uncompress= options.
  117537                 :   **
  117538                 :   **   2. If no column names were specified as part of the CREATE VIRTUAL
  117539                 :   **      TABLE statement, use all columns from the content table.
  117540                 :   */
  117541               1 :   if( rc==SQLITE_OK && zContent ){
  117542               0 :     sqlite3_free(zCompress); 
  117543               0 :     sqlite3_free(zUncompress); 
  117544               0 :     zCompress = 0;
  117545               0 :     zUncompress = 0;
  117546               0 :     if( nCol==0 ){
  117547               0 :       sqlite3_free((void*)aCol); 
  117548               0 :       aCol = 0;
  117549               0 :       rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
  117550                 :     }
  117551               0 :     assert( rc!=SQLITE_OK || nCol>0 );
  117552                 :   }
  117553               1 :   if( rc!=SQLITE_OK ) goto fts3_init_out;
  117554                 : 
  117555               1 :   if( nCol==0 ){
  117556               0 :     assert( nString==0 );
  117557               0 :     aCol[0] = "content";
  117558               0 :     nString = 8;
  117559               0 :     nCol = 1;
  117560                 :   }
  117561                 : 
  117562               1 :   if( pTokenizer==0 ){
  117563               1 :     rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
  117564               1 :     if( rc!=SQLITE_OK ) goto fts3_init_out;
  117565                 :   }
  117566               1 :   assert( pTokenizer );
  117567                 : 
  117568               1 :   rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
  117569               1 :   if( rc==SQLITE_ERROR ){
  117570               0 :     assert( zPrefix );
  117571               0 :     *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
  117572                 :   }
  117573               1 :   if( rc!=SQLITE_OK ) goto fts3_init_out;
  117574                 : 
  117575                 :   /* Allocate and populate the Fts3Table structure. */
  117576               1 :   nByte = sizeof(Fts3Table) +                  /* Fts3Table */
  117577               1 :           nCol * sizeof(char *) +              /* azColumn */
  117578               1 :           nIndex * sizeof(struct Fts3Index) +  /* aIndex */
  117579               1 :           nName +                              /* zName */
  117580               1 :           nDb +                                /* zDb */
  117581                 :           nString;                             /* Space for azColumn strings */
  117582               1 :   p = (Fts3Table*)sqlite3_malloc(nByte);
  117583               1 :   if( p==0 ){
  117584               0 :     rc = SQLITE_NOMEM;
  117585               0 :     goto fts3_init_out;
  117586                 :   }
  117587               1 :   memset(p, 0, nByte);
  117588               1 :   p->db = db;
  117589               1 :   p->nColumn = nCol;
  117590               1 :   p->nPendingData = 0;
  117591               1 :   p->azColumn = (char **)&p[1];
  117592               1 :   p->pTokenizer = pTokenizer;
  117593               1 :   p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
  117594               1 :   p->bHasDocsize = (isFts4 && bNoDocsize==0);
  117595               1 :   p->bHasStat = isFts4;
  117596               1 :   p->bDescIdx = bDescIdx;
  117597               1 :   p->zContentTbl = zContent;
  117598               1 :   zContent = 0;
  117599               1 :   TESTONLY( p->inTransaction = -1 );
  117600               1 :   TESTONLY( p->mxSavepoint = -1 );
  117601                 : 
  117602               1 :   p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
  117603               1 :   memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
  117604               1 :   p->nIndex = nIndex;
  117605               2 :   for(i=0; i<nIndex; i++){
  117606               1 :     fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
  117607                 :   }
  117608                 : 
  117609                 :   /* Fill in the zName and zDb fields of the vtab structure. */
  117610               1 :   zCsr = (char *)&p->aIndex[nIndex];
  117611               1 :   p->zName = zCsr;
  117612               1 :   memcpy(zCsr, argv[2], nName);
  117613               1 :   zCsr += nName;
  117614               1 :   p->zDb = zCsr;
  117615               1 :   memcpy(zCsr, argv[1], nDb);
  117616               1 :   zCsr += nDb;
  117617                 : 
  117618                 :   /* Fill in the azColumn array */
  117619               3 :   for(iCol=0; iCol<nCol; iCol++){
  117620                 :     char *z; 
  117621               2 :     int n = 0;
  117622               2 :     z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
  117623               2 :     memcpy(zCsr, z, n);
  117624               2 :     zCsr[n] = '\0';
  117625               2 :     sqlite3Fts3Dequote(zCsr);
  117626               2 :     p->azColumn[iCol] = zCsr;
  117627               2 :     zCsr += n+1;
  117628               2 :     assert( zCsr <= &((char *)p)[nByte] );
  117629                 :   }
  117630                 : 
  117631               1 :   if( (zCompress==0)!=(zUncompress==0) ){
  117632               0 :     char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
  117633               0 :     rc = SQLITE_ERROR;
  117634               0 :     *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
  117635                 :   }
  117636               1 :   p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
  117637               1 :   p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
  117638               1 :   if( rc!=SQLITE_OK ) goto fts3_init_out;
  117639                 : 
  117640                 :   /* If this is an xCreate call, create the underlying tables in the 
  117641                 :   ** database. TODO: For xConnect(), it could verify that said tables exist.
  117642                 :   */
  117643               1 :   if( isCreate ){
  117644               1 :     rc = fts3CreateTables(p);
  117645                 :   }
  117646                 : 
  117647                 :   /* Figure out the page-size for the database. This is required in order to
  117648                 :   ** estimate the cost of loading large doclists from the database.  */
  117649               1 :   fts3DatabasePageSize(&rc, p);
  117650               1 :   p->nNodeSize = p->nPgsz-35;
  117651                 : 
  117652                 :   /* Declare the table schema to SQLite. */
  117653               1 :   fts3DeclareVtab(&rc, p);
  117654                 : 
  117655                 : fts3_init_out:
  117656               1 :   sqlite3_free(zPrefix);
  117657               1 :   sqlite3_free(aIndex);
  117658               1 :   sqlite3_free(zCompress);
  117659               1 :   sqlite3_free(zUncompress);
  117660               1 :   sqlite3_free(zContent);
  117661               1 :   sqlite3_free((void *)aCol);
  117662               1 :   if( rc!=SQLITE_OK ){
  117663               0 :     if( p ){
  117664               0 :       fts3DisconnectMethod((sqlite3_vtab *)p);
  117665               0 :     }else if( pTokenizer ){
  117666               0 :       pTokenizer->pModule->xDestroy(pTokenizer);
  117667                 :     }
  117668                 :   }else{
  117669               1 :     assert( p->pSegments==0 );
  117670               1 :     *ppVTab = &p->base;
  117671                 :   }
  117672               1 :   return rc;
  117673                 : }
  117674                 : 
  117675                 : /*
  117676                 : ** The xConnect() and xCreate() methods for the virtual table. All the
  117677                 : ** work is done in function fts3InitVtab().
  117678                 : */
  117679               0 : static int fts3ConnectMethod(
  117680                 :   sqlite3 *db,                    /* Database connection */
  117681                 :   void *pAux,                     /* Pointer to tokenizer hash table */
  117682                 :   int argc,                       /* Number of elements in argv array */
  117683                 :   const char * const *argv,       /* xCreate/xConnect argument array */
  117684                 :   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
  117685                 :   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
  117686                 : ){
  117687               0 :   return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
  117688                 : }
  117689               1 : static int fts3CreateMethod(
  117690                 :   sqlite3 *db,                    /* Database connection */
  117691                 :   void *pAux,                     /* Pointer to tokenizer hash table */
  117692                 :   int argc,                       /* Number of elements in argv array */
  117693                 :   const char * const *argv,       /* xCreate/xConnect argument array */
  117694                 :   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
  117695                 :   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
  117696                 : ){
  117697               1 :   return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
  117698                 : }
  117699                 : 
  117700                 : /* 
  117701                 : ** Implementation of the xBestIndex method for FTS3 tables. There
  117702                 : ** are three possible strategies, in order of preference:
  117703                 : **
  117704                 : **   1. Direct lookup by rowid or docid. 
  117705                 : **   2. Full-text search using a MATCH operator on a non-docid column.
  117706                 : **   3. Linear scan of %_content table.
  117707                 : */
  117708               2 : static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
  117709               2 :   Fts3Table *p = (Fts3Table *)pVTab;
  117710                 :   int i;                          /* Iterator variable */
  117711               2 :   int iCons = -1;                 /* Index of constraint to use */
  117712                 : 
  117713                 :   /* By default use a full table scan. This is an expensive option,
  117714                 :   ** so search through the constraints to see if a more efficient 
  117715                 :   ** strategy is possible.
  117716                 :   */
  117717               2 :   pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
  117718               2 :   pInfo->estimatedCost = 500000;
  117719               2 :   for(i=0; i<pInfo->nConstraint; i++){
  117720               1 :     struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
  117721               1 :     if( pCons->usable==0 ) continue;
  117722                 : 
  117723                 :     /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
  117724               1 :     if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 
  117725               0 :      && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 )
  117726                 :     ){
  117727               0 :       pInfo->idxNum = FTS3_DOCID_SEARCH;
  117728               0 :       pInfo->estimatedCost = 1.0;
  117729               0 :       iCons = i;
  117730                 :     }
  117731                 : 
  117732                 :     /* A MATCH constraint. Use a full-text search.
  117733                 :     **
  117734                 :     ** If there is more than one MATCH constraint available, use the first
  117735                 :     ** one encountered. If there is both a MATCH constraint and a direct
  117736                 :     ** rowid/docid lookup, prefer the MATCH strategy. This is done even 
  117737                 :     ** though the rowid/docid lookup is faster than a MATCH query, selecting
  117738                 :     ** it would lead to an "unable to use function MATCH in the requested 
  117739                 :     ** context" error.
  117740                 :     */
  117741               1 :     if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH 
  117742               1 :      && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
  117743                 :     ){
  117744               1 :       pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
  117745               1 :       pInfo->estimatedCost = 2.0;
  117746               1 :       iCons = i;
  117747               1 :       break;
  117748                 :     }
  117749                 :   }
  117750                 : 
  117751               2 :   if( iCons>=0 ){
  117752               1 :     pInfo->aConstraintUsage[iCons].argvIndex = 1;
  117753               1 :     pInfo->aConstraintUsage[iCons].omit = 1;
  117754                 :   } 
  117755                 : 
  117756                 :   /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
  117757                 :   ** docid) order. Both ascending and descending are possible. 
  117758                 :   */
  117759               2 :   if( pInfo->nOrderBy==1 ){
  117760               0 :     struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
  117761               0 :     if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
  117762               0 :       if( pOrder->desc ){
  117763               0 :         pInfo->idxStr = "DESC";
  117764                 :       }else{
  117765               0 :         pInfo->idxStr = "ASC";
  117766                 :       }
  117767               0 :       pInfo->orderByConsumed = 1;
  117768                 :     }
  117769                 :   }
  117770                 : 
  117771               2 :   assert( p->pSegments==0 );
  117772               2 :   return SQLITE_OK;
  117773                 : }
  117774                 : 
  117775                 : /*
  117776                 : ** Implementation of xOpen method.
  117777                 : */
  117778               2 : static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
  117779                 :   sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
  117780                 : 
  117781                 :   UNUSED_PARAMETER(pVTab);
  117782                 : 
  117783                 :   /* Allocate a buffer large enough for an Fts3Cursor structure. If the
  117784                 :   ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, 
  117785                 :   ** if the allocation fails, return SQLITE_NOMEM.
  117786                 :   */
  117787               2 :   *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
  117788               2 :   if( !pCsr ){
  117789               0 :     return SQLITE_NOMEM;
  117790                 :   }
  117791               2 :   memset(pCsr, 0, sizeof(Fts3Cursor));
  117792               2 :   return SQLITE_OK;
  117793                 : }
  117794                 : 
  117795                 : /*
  117796                 : ** Close the cursor.  For additional information see the documentation
  117797                 : ** on the xClose method of the virtual table interface.
  117798                 : */
  117799               2 : static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
  117800               2 :   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  117801               2 :   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  117802               2 :   sqlite3_finalize(pCsr->pStmt);
  117803               2 :   sqlite3Fts3ExprFree(pCsr->pExpr);
  117804               2 :   sqlite3Fts3FreeDeferredTokens(pCsr);
  117805               2 :   sqlite3_free(pCsr->aDoclist);
  117806               2 :   sqlite3_free(pCsr->aMatchinfo);
  117807               2 :   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  117808               2 :   sqlite3_free(pCsr);
  117809               2 :   return SQLITE_OK;
  117810                 : }
  117811                 : 
  117812                 : /*
  117813                 : ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
  117814                 : ** compose and prepare an SQL statement of the form:
  117815                 : **
  117816                 : **    "SELECT <columns> FROM %_content WHERE rowid = ?"
  117817                 : **
  117818                 : ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
  117819                 : ** it. If an error occurs, return an SQLite error code.
  117820                 : **
  117821                 : ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
  117822                 : */
  117823               2 : static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
  117824               2 :   int rc = SQLITE_OK;
  117825               2 :   if( pCsr->pStmt==0 ){
  117826               1 :     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  117827                 :     char *zSql;
  117828               1 :     zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
  117829               1 :     if( !zSql ) return SQLITE_NOMEM;
  117830               1 :     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
  117831               1 :     sqlite3_free(zSql);
  117832                 :   }
  117833               2 :   *ppStmt = pCsr->pStmt;
  117834               2 :   return rc;
  117835                 : }
  117836                 : 
  117837                 : /*
  117838                 : ** Position the pCsr->pStmt statement so that it is on the row
  117839                 : ** of the %_content table that contains the last match.  Return
  117840                 : ** SQLITE_OK on success.  
  117841                 : */
  117842               4 : static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
  117843               4 :   int rc = SQLITE_OK;
  117844               4 :   if( pCsr->isRequireSeek ){
  117845               2 :     sqlite3_stmt *pStmt = 0;
  117846                 : 
  117847               2 :     rc = fts3CursorSeekStmt(pCsr, &pStmt);
  117848               2 :     if( rc==SQLITE_OK ){
  117849               2 :       sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
  117850               2 :       pCsr->isRequireSeek = 0;
  117851               2 :       if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
  117852               2 :         return SQLITE_OK;
  117853                 :       }else{
  117854               0 :         rc = sqlite3_reset(pCsr->pStmt);
  117855               0 :         if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
  117856                 :           /* If no row was found and no error has occured, then the %_content
  117857                 :           ** table is missing a row that is present in the full-text index.
  117858                 :           ** The data structures are corrupt.  */
  117859               0 :           rc = FTS_CORRUPT_VTAB;
  117860               0 :           pCsr->isEof = 1;
  117861                 :         }
  117862                 :       }
  117863                 :     }
  117864                 :   }
  117865                 : 
  117866               2 :   if( rc!=SQLITE_OK && pContext ){
  117867               0 :     sqlite3_result_error_code(pContext, rc);
  117868                 :   }
  117869               2 :   return rc;
  117870                 : }
  117871                 : 
  117872                 : /*
  117873                 : ** This function is used to process a single interior node when searching
  117874                 : ** a b-tree for a term or term prefix. The node data is passed to this 
  117875                 : ** function via the zNode/nNode parameters. The term to search for is
  117876                 : ** passed in zTerm/nTerm.
  117877                 : **
  117878                 : ** If piFirst is not NULL, then this function sets *piFirst to the blockid
  117879                 : ** of the child node that heads the sub-tree that may contain the term.
  117880                 : **
  117881                 : ** If piLast is not NULL, then *piLast is set to the right-most child node
  117882                 : ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
  117883                 : ** a prefix.
  117884                 : **
  117885                 : ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
  117886                 : */
  117887               0 : static int fts3ScanInteriorNode(
  117888                 :   const char *zTerm,              /* Term to select leaves for */
  117889                 :   int nTerm,                      /* Size of term zTerm in bytes */
  117890                 :   const char *zNode,              /* Buffer containing segment interior node */
  117891                 :   int nNode,                      /* Size of buffer at zNode */
  117892                 :   sqlite3_int64 *piFirst,         /* OUT: Selected child node */
  117893                 :   sqlite3_int64 *piLast           /* OUT: Selected child node */
  117894                 : ){
  117895               0 :   int rc = SQLITE_OK;             /* Return code */
  117896               0 :   const char *zCsr = zNode;       /* Cursor to iterate through node */
  117897               0 :   const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
  117898               0 :   char *zBuffer = 0;              /* Buffer to load terms into */
  117899               0 :   int nAlloc = 0;                 /* Size of allocated buffer */
  117900               0 :   int isFirstTerm = 1;            /* True when processing first term on page */
  117901                 :   sqlite3_int64 iChild;           /* Block id of child node to descend to */
  117902                 : 
  117903                 :   /* Skip over the 'height' varint that occurs at the start of every 
  117904                 :   ** interior node. Then load the blockid of the left-child of the b-tree
  117905                 :   ** node into variable iChild.  
  117906                 :   **
  117907                 :   ** Even if the data structure on disk is corrupted, this (reading two
  117908                 :   ** varints from the buffer) does not risk an overread. If zNode is a
  117909                 :   ** root node, then the buffer comes from a SELECT statement. SQLite does
  117910                 :   ** not make this guarantee explicitly, but in practice there are always
  117911                 :   ** either more than 20 bytes of allocated space following the nNode bytes of
  117912                 :   ** contents, or two zero bytes. Or, if the node is read from the %_segments
  117913                 :   ** table, then there are always 20 bytes of zeroed padding following the
  117914                 :   ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
  117915                 :   */
  117916               0 :   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
  117917               0 :   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
  117918               0 :   if( zCsr>zEnd ){
  117919               0 :     return FTS_CORRUPT_VTAB;
  117920                 :   }
  117921                 :   
  117922               0 :   while( zCsr<zEnd && (piFirst || piLast) ){
  117923                 :     int cmp;                      /* memcmp() result */
  117924                 :     int nSuffix;                  /* Size of term suffix */
  117925               0 :     int nPrefix = 0;              /* Size of term prefix */
  117926                 :     int nBuffer;                  /* Total term size */
  117927                 :   
  117928                 :     /* Load the next term on the node into zBuffer. Use realloc() to expand
  117929                 :     ** the size of zBuffer if required.  */
  117930               0 :     if( !isFirstTerm ){
  117931               0 :       zCsr += sqlite3Fts3GetVarint32(zCsr, &nPrefix);
  117932                 :     }
  117933               0 :     isFirstTerm = 0;
  117934               0 :     zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
  117935                 :     
  117936               0 :     if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
  117937               0 :       rc = FTS_CORRUPT_VTAB;
  117938               0 :       goto finish_scan;
  117939                 :     }
  117940               0 :     if( nPrefix+nSuffix>nAlloc ){
  117941                 :       char *zNew;
  117942               0 :       nAlloc = (nPrefix+nSuffix) * 2;
  117943               0 :       zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
  117944               0 :       if( !zNew ){
  117945               0 :         rc = SQLITE_NOMEM;
  117946               0 :         goto finish_scan;
  117947                 :       }
  117948               0 :       zBuffer = zNew;
  117949                 :     }
  117950               0 :     assert( zBuffer );
  117951               0 :     memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
  117952               0 :     nBuffer = nPrefix + nSuffix;
  117953               0 :     zCsr += nSuffix;
  117954                 : 
  117955                 :     /* Compare the term we are searching for with the term just loaded from
  117956                 :     ** the interior node. If the specified term is greater than or equal
  117957                 :     ** to the term from the interior node, then all terms on the sub-tree 
  117958                 :     ** headed by node iChild are smaller than zTerm. No need to search 
  117959                 :     ** iChild.
  117960                 :     **
  117961                 :     ** If the interior node term is larger than the specified term, then
  117962                 :     ** the tree headed by iChild may contain the specified term.
  117963                 :     */
  117964               0 :     cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
  117965               0 :     if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
  117966               0 :       *piFirst = iChild;
  117967               0 :       piFirst = 0;
  117968                 :     }
  117969                 : 
  117970               0 :     if( piLast && cmp<0 ){
  117971               0 :       *piLast = iChild;
  117972               0 :       piLast = 0;
  117973                 :     }
  117974                 : 
  117975               0 :     iChild++;
  117976                 :   };
  117977                 : 
  117978               0 :   if( piFirst ) *piFirst = iChild;
  117979               0 :   if( piLast ) *piLast = iChild;
  117980                 : 
  117981                 :  finish_scan:
  117982               0 :   sqlite3_free(zBuffer);
  117983               0 :   return rc;
  117984                 : }
  117985                 : 
  117986                 : 
  117987                 : /*
  117988                 : ** The buffer pointed to by argument zNode (size nNode bytes) contains an
  117989                 : ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
  117990                 : ** contains a term. This function searches the sub-tree headed by the zNode
  117991                 : ** node for the range of leaf nodes that may contain the specified term
  117992                 : ** or terms for which the specified term is a prefix.
  117993                 : **
  117994                 : ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the 
  117995                 : ** left-most leaf node in the tree that may contain the specified term.
  117996                 : ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
  117997                 : ** right-most leaf node that may contain a term for which the specified
  117998                 : ** term is a prefix.
  117999                 : **
  118000                 : ** It is possible that the range of returned leaf nodes does not contain 
  118001                 : ** the specified term or any terms for which it is a prefix. However, if the 
  118002                 : ** segment does contain any such terms, they are stored within the identified
  118003                 : ** range. Because this function only inspects interior segment nodes (and
  118004                 : ** never loads leaf nodes into memory), it is not possible to be sure.
  118005                 : **
  118006                 : ** If an error occurs, an error code other than SQLITE_OK is returned.
  118007                 : */ 
  118008               0 : static int fts3SelectLeaf(
  118009                 :   Fts3Table *p,                   /* Virtual table handle */
  118010                 :   const char *zTerm,              /* Term to select leaves for */
  118011                 :   int nTerm,                      /* Size of term zTerm in bytes */
  118012                 :   const char *zNode,              /* Buffer containing segment interior node */
  118013                 :   int nNode,                      /* Size of buffer at zNode */
  118014                 :   sqlite3_int64 *piLeaf,          /* Selected leaf node */
  118015                 :   sqlite3_int64 *piLeaf2          /* Selected leaf node */
  118016                 : ){
  118017                 :   int rc;                         /* Return code */
  118018                 :   int iHeight;                    /* Height of this node in tree */
  118019                 : 
  118020               0 :   assert( piLeaf || piLeaf2 );
  118021                 : 
  118022               0 :   sqlite3Fts3GetVarint32(zNode, &iHeight);
  118023               0 :   rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
  118024               0 :   assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
  118025                 : 
  118026               0 :   if( rc==SQLITE_OK && iHeight>1 ){
  118027               0 :     char *zBlob = 0;              /* Blob read from %_segments table */
  118028                 :     int nBlob;                    /* Size of zBlob in bytes */
  118029                 : 
  118030               0 :     if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
  118031               0 :       rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
  118032               0 :       if( rc==SQLITE_OK ){
  118033               0 :         rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
  118034                 :       }
  118035               0 :       sqlite3_free(zBlob);
  118036               0 :       piLeaf = 0;
  118037               0 :       zBlob = 0;
  118038                 :     }
  118039                 : 
  118040               0 :     if( rc==SQLITE_OK ){
  118041               0 :       rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
  118042                 :     }
  118043               0 :     if( rc==SQLITE_OK ){
  118044               0 :       rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
  118045                 :     }
  118046               0 :     sqlite3_free(zBlob);
  118047                 :   }
  118048                 : 
  118049               0 :   return rc;
  118050                 : }
  118051                 : 
  118052                 : /*
  118053                 : ** This function is used to create delta-encoded serialized lists of FTS3 
  118054                 : ** varints. Each call to this function appends a single varint to a list.
  118055                 : */
  118056               0 : static void fts3PutDeltaVarint(
  118057                 :   char **pp,                      /* IN/OUT: Output pointer */
  118058                 :   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
  118059                 :   sqlite3_int64 iVal              /* Write this value to the list */
  118060                 : ){
  118061               0 :   assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
  118062               0 :   *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
  118063               0 :   *piPrev = iVal;
  118064               0 : }
  118065                 : 
  118066                 : /*
  118067                 : ** When this function is called, *ppPoslist is assumed to point to the 
  118068                 : ** start of a position-list. After it returns, *ppPoslist points to the
  118069                 : ** first byte after the position-list.
  118070                 : **
  118071                 : ** A position list is list of positions (delta encoded) and columns for 
  118072                 : ** a single document record of a doclist.  So, in other words, this
  118073                 : ** routine advances *ppPoslist so that it points to the next docid in
  118074                 : ** the doclist, or to the first byte past the end of the doclist.
  118075                 : **
  118076                 : ** If pp is not NULL, then the contents of the position list are copied
  118077                 : ** to *pp. *pp is set to point to the first byte past the last byte copied
  118078                 : ** before this function returns.
  118079                 : */
  118080               0 : static void fts3PoslistCopy(char **pp, char **ppPoslist){
  118081               0 :   char *pEnd = *ppPoslist;
  118082               0 :   char c = 0;
  118083                 : 
  118084                 :   /* The end of a position list is marked by a zero encoded as an FTS3 
  118085                 :   ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
  118086                 :   ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
  118087                 :   ** of some other, multi-byte, value.
  118088                 :   **
  118089                 :   ** The following while-loop moves pEnd to point to the first byte that is not 
  118090                 :   ** immediately preceded by a byte with the 0x80 bit set. Then increments
  118091                 :   ** pEnd once more so that it points to the byte immediately following the
  118092                 :   ** last byte in the position-list.
  118093                 :   */
  118094               0 :   while( *pEnd | c ){
  118095               0 :     c = *pEnd++ & 0x80;
  118096                 :     testcase( c!=0 && (*pEnd)==0 );
  118097                 :   }
  118098               0 :   pEnd++;  /* Advance past the POS_END terminator byte */
  118099                 : 
  118100               0 :   if( pp ){
  118101               0 :     int n = (int)(pEnd - *ppPoslist);
  118102               0 :     char *p = *pp;
  118103               0 :     memcpy(p, *ppPoslist, n);
  118104               0 :     p += n;
  118105               0 :     *pp = p;
  118106                 :   }
  118107               0 :   *ppPoslist = pEnd;
  118108               0 : }
  118109                 : 
  118110                 : /*
  118111                 : ** When this function is called, *ppPoslist is assumed to point to the 
  118112                 : ** start of a column-list. After it returns, *ppPoslist points to the
  118113                 : ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
  118114                 : **
  118115                 : ** A column-list is list of delta-encoded positions for a single column
  118116                 : ** within a single document within a doclist.
  118117                 : **
  118118                 : ** The column-list is terminated either by a POS_COLUMN varint (1) or
  118119                 : ** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
  118120                 : ** the POS_COLUMN or POS_END that terminates the column-list.
  118121                 : **
  118122                 : ** If pp is not NULL, then the contents of the column-list are copied
  118123                 : ** to *pp. *pp is set to point to the first byte past the last byte copied
  118124                 : ** before this function returns.  The POS_COLUMN or POS_END terminator
  118125                 : ** is not copied into *pp.
  118126                 : */
  118127               0 : static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
  118128               0 :   char *pEnd = *ppPoslist;
  118129               0 :   char c = 0;
  118130                 : 
  118131                 :   /* A column-list is terminated by either a 0x01 or 0x00 byte that is
  118132                 :   ** not part of a multi-byte varint.
  118133                 :   */
  118134               0 :   while( 0xFE & (*pEnd | c) ){
  118135               0 :     c = *pEnd++ & 0x80;
  118136                 :     testcase( c!=0 && ((*pEnd)&0xfe)==0 );
  118137                 :   }
  118138               0 :   if( pp ){
  118139               0 :     int n = (int)(pEnd - *ppPoslist);
  118140               0 :     char *p = *pp;
  118141               0 :     memcpy(p, *ppPoslist, n);
  118142               0 :     p += n;
  118143               0 :     *pp = p;
  118144                 :   }
  118145               0 :   *ppPoslist = pEnd;
  118146               0 : }
  118147                 : 
  118148                 : /*
  118149                 : ** Value used to signify the end of an position-list. This is safe because
  118150                 : ** it is not possible to have a document with 2^31 terms.
  118151                 : */
  118152                 : #define POSITION_LIST_END 0x7fffffff
  118153                 : 
  118154                 : /*
  118155                 : ** This function is used to help parse position-lists. When this function is
  118156                 : ** called, *pp may point to the start of the next varint in the position-list
  118157                 : ** being parsed, or it may point to 1 byte past the end of the position-list
  118158                 : ** (in which case **pp will be a terminator bytes POS_END (0) or
  118159                 : ** (1)).
  118160                 : **
  118161                 : ** If *pp points past the end of the current position-list, set *pi to 
  118162                 : ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
  118163                 : ** increment the current value of *pi by the value read, and set *pp to
  118164                 : ** point to the next value before returning.
  118165                 : **
  118166                 : ** Before calling this routine *pi must be initialized to the value of
  118167                 : ** the previous position, or zero if we are reading the first position
  118168                 : ** in the position-list.  Because positions are delta-encoded, the value
  118169                 : ** of the previous position is needed in order to compute the value of
  118170                 : ** the next position.
  118171                 : */
  118172               0 : static void fts3ReadNextPos(
  118173                 :   char **pp,                    /* IN/OUT: Pointer into position-list buffer */
  118174                 :   sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
  118175                 : ){
  118176               0 :   if( (**pp)&0xFE ){
  118177               0 :     fts3GetDeltaVarint(pp, pi);
  118178               0 :     *pi -= 2;
  118179                 :   }else{
  118180               0 :     *pi = POSITION_LIST_END;
  118181                 :   }
  118182               0 : }
  118183                 : 
  118184                 : /*
  118185                 : ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
  118186                 : ** the value of iCol encoded as a varint to *pp.   This will start a new
  118187                 : ** column list.
  118188                 : **
  118189                 : ** Set *pp to point to the byte just after the last byte written before 
  118190                 : ** returning (do not modify it if iCol==0). Return the total number of bytes
  118191                 : ** written (0 if iCol==0).
  118192                 : */
  118193               0 : static int fts3PutColNumber(char **pp, int iCol){
  118194               0 :   int n = 0;                      /* Number of bytes written */
  118195               0 :   if( iCol ){
  118196               0 :     char *p = *pp;                /* Output pointer */
  118197               0 :     n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
  118198               0 :     *p = 0x01;
  118199               0 :     *pp = &p[n];
  118200                 :   }
  118201               0 :   return n;
  118202                 : }
  118203                 : 
  118204                 : /*
  118205                 : ** Compute the union of two position lists.  The output written
  118206                 : ** into *pp contains all positions of both *pp1 and *pp2 in sorted
  118207                 : ** order and with any duplicates removed.  All pointers are
  118208                 : ** updated appropriately.   The caller is responsible for insuring
  118209                 : ** that there is enough space in *pp to hold the complete output.
  118210                 : */
  118211               0 : static void fts3PoslistMerge(
  118212                 :   char **pp,                      /* Output buffer */
  118213                 :   char **pp1,                     /* Left input list */
  118214                 :   char **pp2                      /* Right input list */
  118215                 : ){
  118216               0 :   char *p = *pp;
  118217               0 :   char *p1 = *pp1;
  118218               0 :   char *p2 = *pp2;
  118219                 : 
  118220               0 :   while( *p1 || *p2 ){
  118221                 :     int iCol1;         /* The current column index in pp1 */
  118222                 :     int iCol2;         /* The current column index in pp2 */
  118223                 : 
  118224               0 :     if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1);
  118225               0 :     else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
  118226               0 :     else iCol1 = 0;
  118227                 : 
  118228               0 :     if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2);
  118229               0 :     else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
  118230               0 :     else iCol2 = 0;
  118231                 : 
  118232               0 :     if( iCol1==iCol2 ){
  118233               0 :       sqlite3_int64 i1 = 0;       /* Last position from pp1 */
  118234               0 :       sqlite3_int64 i2 = 0;       /* Last position from pp2 */
  118235               0 :       sqlite3_int64 iPrev = 0;
  118236               0 :       int n = fts3PutColNumber(&p, iCol1);
  118237               0 :       p1 += n;
  118238               0 :       p2 += n;
  118239                 : 
  118240                 :       /* At this point, both p1 and p2 point to the start of column-lists
  118241                 :       ** for the same column (the column with index iCol1 and iCol2).
  118242                 :       ** A column-list is a list of non-negative delta-encoded varints, each 
  118243                 :       ** incremented by 2 before being stored. Each list is terminated by a
  118244                 :       ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
  118245                 :       ** and writes the results to buffer p. p is left pointing to the byte
  118246                 :       ** after the list written. No terminator (POS_END or POS_COLUMN) is
  118247                 :       ** written to the output.
  118248                 :       */
  118249               0 :       fts3GetDeltaVarint(&p1, &i1);
  118250               0 :       fts3GetDeltaVarint(&p2, &i2);
  118251                 :       do {
  118252               0 :         fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2); 
  118253               0 :         iPrev -= 2;
  118254               0 :         if( i1==i2 ){
  118255               0 :           fts3ReadNextPos(&p1, &i1);
  118256               0 :           fts3ReadNextPos(&p2, &i2);
  118257               0 :         }else if( i1<i2 ){
  118258               0 :           fts3ReadNextPos(&p1, &i1);
  118259                 :         }else{
  118260               0 :           fts3ReadNextPos(&p2, &i2);
  118261                 :         }
  118262               0 :       }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
  118263               0 :     }else if( iCol1<iCol2 ){
  118264               0 :       p1 += fts3PutColNumber(&p, iCol1);
  118265               0 :       fts3ColumnlistCopy(&p, &p1);
  118266                 :     }else{
  118267               0 :       p2 += fts3PutColNumber(&p, iCol2);
  118268               0 :       fts3ColumnlistCopy(&p, &p2);
  118269                 :     }
  118270                 :   }
  118271                 : 
  118272               0 :   *p++ = POS_END;
  118273               0 :   *pp = p;
  118274               0 :   *pp1 = p1 + 1;
  118275               0 :   *pp2 = p2 + 1;
  118276               0 : }
  118277                 : 
  118278                 : /*
  118279                 : ** This function is used to merge two position lists into one. When it is
  118280                 : ** called, *pp1 and *pp2 must both point to position lists. A position-list is
  118281                 : ** the part of a doclist that follows each document id. For example, if a row
  118282                 : ** contains:
  118283                 : **
  118284                 : **     'a b c'|'x y z'|'a b b a'
  118285                 : **
  118286                 : ** Then the position list for this row for token 'b' would consist of:
  118287                 : **
  118288                 : **     0x02 0x01 0x02 0x03 0x03 0x00
  118289                 : **
  118290                 : ** When this function returns, both *pp1 and *pp2 are left pointing to the
  118291                 : ** byte following the 0x00 terminator of their respective position lists.
  118292                 : **
  118293                 : ** If isSaveLeft is 0, an entry is added to the output position list for 
  118294                 : ** each position in *pp2 for which there exists one or more positions in
  118295                 : ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
  118296                 : ** when the *pp1 token appears before the *pp2 token, but not more than nToken
  118297                 : ** slots before it.
  118298                 : **
  118299                 : ** e.g. nToken==1 searches for adjacent positions.
  118300                 : */
  118301               0 : static int fts3PoslistPhraseMerge(
  118302                 :   char **pp,                      /* IN/OUT: Preallocated output buffer */
  118303                 :   int nToken,                     /* Maximum difference in token positions */
  118304                 :   int isSaveLeft,                 /* Save the left position */
  118305                 :   int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
  118306                 :   char **pp1,                     /* IN/OUT: Left input list */
  118307                 :   char **pp2                      /* IN/OUT: Right input list */
  118308                 : ){
  118309               0 :   char *p = *pp;
  118310               0 :   char *p1 = *pp1;
  118311               0 :   char *p2 = *pp2;
  118312               0 :   int iCol1 = 0;
  118313               0 :   int iCol2 = 0;
  118314                 : 
  118315                 :   /* Never set both isSaveLeft and isExact for the same invocation. */
  118316               0 :   assert( isSaveLeft==0 || isExact==0 );
  118317                 : 
  118318               0 :   assert( p!=0 && *p1!=0 && *p2!=0 );
  118319               0 :   if( *p1==POS_COLUMN ){ 
  118320               0 :     p1++;
  118321               0 :     p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
  118322                 :   }
  118323               0 :   if( *p2==POS_COLUMN ){ 
  118324               0 :     p2++;
  118325               0 :     p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
  118326                 :   }
  118327                 : 
  118328                 :   while( 1 ){
  118329               0 :     if( iCol1==iCol2 ){
  118330               0 :       char *pSave = p;
  118331               0 :       sqlite3_int64 iPrev = 0;
  118332               0 :       sqlite3_int64 iPos1 = 0;
  118333               0 :       sqlite3_int64 iPos2 = 0;
  118334                 : 
  118335               0 :       if( iCol1 ){
  118336               0 :         *p++ = POS_COLUMN;
  118337               0 :         p += sqlite3Fts3PutVarint(p, iCol1);
  118338                 :       }
  118339                 : 
  118340               0 :       assert( *p1!=POS_END && *p1!=POS_COLUMN );
  118341               0 :       assert( *p2!=POS_END && *p2!=POS_COLUMN );
  118342               0 :       fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
  118343               0 :       fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
  118344                 : 
  118345                 :       while( 1 ){
  118346               0 :         if( iPos2==iPos1+nToken 
  118347               0 :          || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) 
  118348                 :         ){
  118349                 :           sqlite3_int64 iSave;
  118350               0 :           iSave = isSaveLeft ? iPos1 : iPos2;
  118351               0 :           fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
  118352               0 :           pSave = 0;
  118353               0 :           assert( p );
  118354                 :         }
  118355               0 :         if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
  118356               0 :           if( (*p2&0xFE)==0 ) break;
  118357               0 :           fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
  118358                 :         }else{
  118359               0 :           if( (*p1&0xFE)==0 ) break;
  118360               0 :           fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
  118361                 :         }
  118362               0 :       }
  118363                 : 
  118364               0 :       if( pSave ){
  118365               0 :         assert( pp && p );
  118366               0 :         p = pSave;
  118367                 :       }
  118368                 : 
  118369               0 :       fts3ColumnlistCopy(0, &p1);
  118370               0 :       fts3ColumnlistCopy(0, &p2);
  118371               0 :       assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
  118372               0 :       if( 0==*p1 || 0==*p2 ) break;
  118373                 : 
  118374               0 :       p1++;
  118375               0 :       p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
  118376               0 :       p2++;
  118377               0 :       p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
  118378                 :     }
  118379                 : 
  118380                 :     /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
  118381                 :     ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
  118382                 :     ** end of the position list, or the 0x01 that precedes the next 
  118383                 :     ** column-number in the position list. 
  118384                 :     */
  118385               0 :     else if( iCol1<iCol2 ){
  118386               0 :       fts3ColumnlistCopy(0, &p1);
  118387               0 :       if( 0==*p1 ) break;
  118388               0 :       p1++;
  118389               0 :       p1 += sqlite3Fts3GetVarint32(p1, &iCol1);
  118390                 :     }else{
  118391               0 :       fts3ColumnlistCopy(0, &p2);
  118392               0 :       if( 0==*p2 ) break;
  118393               0 :       p2++;
  118394               0 :       p2 += sqlite3Fts3GetVarint32(p2, &iCol2);
  118395                 :     }
  118396               0 :   }
  118397                 : 
  118398               0 :   fts3PoslistCopy(0, &p2);
  118399               0 :   fts3PoslistCopy(0, &p1);
  118400               0 :   *pp1 = p1;
  118401               0 :   *pp2 = p2;
  118402               0 :   if( *pp==p ){
  118403               0 :     return 0;
  118404                 :   }
  118405               0 :   *p++ = 0x00;
  118406               0 :   *pp = p;
  118407               0 :   return 1;
  118408                 : }
  118409                 : 
  118410                 : /*
  118411                 : ** Merge two position-lists as required by the NEAR operator. The argument
  118412                 : ** position lists correspond to the left and right phrases of an expression 
  118413                 : ** like:
  118414                 : **
  118415                 : **     "phrase 1" NEAR "phrase number 2"
  118416                 : **
  118417                 : ** Position list *pp1 corresponds to the left-hand side of the NEAR 
  118418                 : ** expression and *pp2 to the right. As usual, the indexes in the position 
  118419                 : ** lists are the offsets of the last token in each phrase (tokens "1" and "2" 
  118420                 : ** in the example above).
  118421                 : **
  118422                 : ** The output position list - written to *pp - is a copy of *pp2 with those
  118423                 : ** entries that are not sufficiently NEAR entries in *pp1 removed.
  118424                 : */
  118425               0 : static int fts3PoslistNearMerge(
  118426                 :   char **pp,                      /* Output buffer */
  118427                 :   char *aTmp,                     /* Temporary buffer space */
  118428                 :   int nRight,                     /* Maximum difference in token positions */
  118429                 :   int nLeft,                      /* Maximum difference in token positions */
  118430                 :   char **pp1,                     /* IN/OUT: Left input list */
  118431                 :   char **pp2                      /* IN/OUT: Right input list */
  118432                 : ){
  118433               0 :   char *p1 = *pp1;
  118434               0 :   char *p2 = *pp2;
  118435                 : 
  118436               0 :   char *pTmp1 = aTmp;
  118437                 :   char *pTmp2;
  118438                 :   char *aTmp2;
  118439               0 :   int res = 1;
  118440                 : 
  118441               0 :   fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
  118442               0 :   aTmp2 = pTmp2 = pTmp1;
  118443               0 :   *pp1 = p1;
  118444               0 :   *pp2 = p2;
  118445               0 :   fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
  118446               0 :   if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
  118447               0 :     fts3PoslistMerge(pp, &aTmp, &aTmp2);
  118448               0 :   }else if( pTmp1!=aTmp ){
  118449               0 :     fts3PoslistCopy(pp, &aTmp);
  118450               0 :   }else if( pTmp2!=aTmp2 ){
  118451               0 :     fts3PoslistCopy(pp, &aTmp2);
  118452                 :   }else{
  118453               0 :     res = 0;
  118454                 :   }
  118455                 : 
  118456               0 :   return res;
  118457                 : }
  118458                 : 
  118459                 : /* 
  118460                 : ** An instance of this function is used to merge together the (potentially
  118461                 : ** large number of) doclists for each term that matches a prefix query.
  118462                 : ** See function fts3TermSelectMerge() for details.
  118463                 : */
  118464                 : typedef struct TermSelect TermSelect;
  118465                 : struct TermSelect {
  118466                 :   char *aaOutput[16];             /* Malloc'd output buffers */
  118467                 :   int anOutput[16];               /* Size each output buffer in bytes */
  118468                 : };
  118469                 : 
  118470                 : /*
  118471                 : ** This function is used to read a single varint from a buffer. Parameter
  118472                 : ** pEnd points 1 byte past the end of the buffer. When this function is
  118473                 : ** called, if *pp points to pEnd or greater, then the end of the buffer
  118474                 : ** has been reached. In this case *pp is set to 0 and the function returns.
  118475                 : **
  118476                 : ** If *pp does not point to or past pEnd, then a single varint is read
  118477                 : ** from *pp. *pp is then set to point 1 byte past the end of the read varint.
  118478                 : **
  118479                 : ** If bDescIdx is false, the value read is added to *pVal before returning.
  118480                 : ** If it is true, the value read is subtracted from *pVal before this 
  118481                 : ** function returns.
  118482                 : */
  118483               0 : static void fts3GetDeltaVarint3(
  118484                 :   char **pp,                      /* IN/OUT: Point to read varint from */
  118485                 :   char *pEnd,                     /* End of buffer */
  118486                 :   int bDescIdx,                   /* True if docids are descending */
  118487                 :   sqlite3_int64 *pVal             /* IN/OUT: Integer value */
  118488                 : ){
  118489               0 :   if( *pp>=pEnd ){
  118490               0 :     *pp = 0;
  118491                 :   }else{
  118492                 :     sqlite3_int64 iVal;
  118493               0 :     *pp += sqlite3Fts3GetVarint(*pp, &iVal);
  118494               0 :     if( bDescIdx ){
  118495               0 :       *pVal -= iVal;
  118496                 :     }else{
  118497               0 :       *pVal += iVal;
  118498                 :     }
  118499                 :   }
  118500               0 : }
  118501                 : 
  118502                 : /*
  118503                 : ** This function is used to write a single varint to a buffer. The varint
  118504                 : ** is written to *pp. Before returning, *pp is set to point 1 byte past the
  118505                 : ** end of the value written.
  118506                 : **
  118507                 : ** If *pbFirst is zero when this function is called, the value written to
  118508                 : ** the buffer is that of parameter iVal. 
  118509                 : **
  118510                 : ** If *pbFirst is non-zero when this function is called, then the value 
  118511                 : ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
  118512                 : ** (if bDescIdx is non-zero).
  118513                 : **
  118514                 : ** Before returning, this function always sets *pbFirst to 1 and *piPrev
  118515                 : ** to the value of parameter iVal.
  118516                 : */
  118517               0 : static void fts3PutDeltaVarint3(
  118518                 :   char **pp,                      /* IN/OUT: Output pointer */
  118519                 :   int bDescIdx,                   /* True for descending docids */
  118520                 :   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
  118521                 :   int *pbFirst,                   /* IN/OUT: True after first int written */
  118522                 :   sqlite3_int64 iVal              /* Write this value to the list */
  118523                 : ){
  118524                 :   sqlite3_int64 iWrite;
  118525               0 :   if( bDescIdx==0 || *pbFirst==0 ){
  118526               0 :     iWrite = iVal - *piPrev;
  118527                 :   }else{
  118528               0 :     iWrite = *piPrev - iVal;
  118529                 :   }
  118530               0 :   assert( *pbFirst || *piPrev==0 );
  118531               0 :   assert( *pbFirst==0 || iWrite>0 );
  118532               0 :   *pp += sqlite3Fts3PutVarint(*pp, iWrite);
  118533               0 :   *piPrev = iVal;
  118534               0 :   *pbFirst = 1;
  118535               0 : }
  118536                 : 
  118537                 : 
  118538                 : /*
  118539                 : ** This macro is used by various functions that merge doclists. The two
  118540                 : ** arguments are 64-bit docid values. If the value of the stack variable
  118541                 : ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2). 
  118542                 : ** Otherwise, (i2-i1).
  118543                 : **
  118544                 : ** Using this makes it easier to write code that can merge doclists that are
  118545                 : ** sorted in either ascending or descending order.
  118546                 : */
  118547                 : #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
  118548                 : 
  118549                 : /*
  118550                 : ** This function does an "OR" merge of two doclists (output contains all
  118551                 : ** positions contained in either argument doclist). If the docids in the 
  118552                 : ** input doclists are sorted in ascending order, parameter bDescDoclist
  118553                 : ** should be false. If they are sorted in ascending order, it should be
  118554                 : ** passed a non-zero value.
  118555                 : **
  118556                 : ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
  118557                 : ** containing the output doclist and SQLITE_OK is returned. In this case
  118558                 : ** *pnOut is set to the number of bytes in the output doclist.
  118559                 : **
  118560                 : ** If an error occurs, an SQLite error code is returned. The output values
  118561                 : ** are undefined in this case.
  118562                 : */
  118563               0 : static int fts3DoclistOrMerge(
  118564                 :   int bDescDoclist,               /* True if arguments are desc */
  118565                 :   char *a1, int n1,               /* First doclist */
  118566                 :   char *a2, int n2,               /* Second doclist */
  118567                 :   char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
  118568                 : ){
  118569               0 :   sqlite3_int64 i1 = 0;
  118570               0 :   sqlite3_int64 i2 = 0;
  118571               0 :   sqlite3_int64 iPrev = 0;
  118572               0 :   char *pEnd1 = &a1[n1];
  118573               0 :   char *pEnd2 = &a2[n2];
  118574               0 :   char *p1 = a1;
  118575               0 :   char *p2 = a2;
  118576                 :   char *p;
  118577                 :   char *aOut;
  118578               0 :   int bFirstOut = 0;
  118579                 : 
  118580               0 :   *paOut = 0;
  118581               0 :   *pnOut = 0;
  118582                 : 
  118583                 :   /* Allocate space for the output. Both the input and output doclists
  118584                 :   ** are delta encoded. If they are in ascending order (bDescDoclist==0),
  118585                 :   ** then the first docid in each list is simply encoded as a varint. For
  118586                 :   ** each subsequent docid, the varint stored is the difference between the
  118587                 :   ** current and previous docid (a positive number - since the list is in
  118588                 :   ** ascending order).
  118589                 :   **
  118590                 :   ** The first docid written to the output is therefore encoded using the 
  118591                 :   ** same number of bytes as it is in whichever of the input lists it is
  118592                 :   ** read from. And each subsequent docid read from the same input list 
  118593                 :   ** consumes either the same or less bytes as it did in the input (since
  118594                 :   ** the difference between it and the previous value in the output must
  118595                 :   ** be a positive value less than or equal to the delta value read from 
  118596                 :   ** the input list). The same argument applies to all but the first docid
  118597                 :   ** read from the 'other' list. And to the contents of all position lists
  118598                 :   ** that will be copied and merged from the input to the output.
  118599                 :   **
  118600                 :   ** However, if the first docid copied to the output is a negative number,
  118601                 :   ** then the encoding of the first docid from the 'other' input list may
  118602                 :   ** be larger in the output than it was in the input (since the delta value
  118603                 :   ** may be a larger positive integer than the actual docid).
  118604                 :   **
  118605                 :   ** The space required to store the output is therefore the sum of the
  118606                 :   ** sizes of the two inputs, plus enough space for exactly one of the input
  118607                 :   ** docids to grow. 
  118608                 :   **
  118609                 :   ** A symetric argument may be made if the doclists are in descending 
  118610                 :   ** order.
  118611                 :   */
  118612               0 :   aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
  118613               0 :   if( !aOut ) return SQLITE_NOMEM;
  118614                 : 
  118615               0 :   p = aOut;
  118616               0 :   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  118617               0 :   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  118618               0 :   while( p1 || p2 ){
  118619               0 :     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
  118620                 : 
  118621               0 :     if( p2 && p1 && iDiff==0 ){
  118622               0 :       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
  118623               0 :       fts3PoslistMerge(&p, &p1, &p2);
  118624               0 :       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  118625               0 :       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  118626               0 :     }else if( !p2 || (p1 && iDiff<0) ){
  118627               0 :       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
  118628               0 :       fts3PoslistCopy(&p, &p1);
  118629               0 :       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  118630                 :     }else{
  118631               0 :       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
  118632               0 :       fts3PoslistCopy(&p, &p2);
  118633               0 :       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  118634                 :     }
  118635                 :   }
  118636                 : 
  118637               0 :   *paOut = aOut;
  118638               0 :   *pnOut = (p-aOut);
  118639               0 :   assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
  118640               0 :   return SQLITE_OK;
  118641                 : }
  118642                 : 
  118643                 : /*
  118644                 : ** This function does a "phrase" merge of two doclists. In a phrase merge,
  118645                 : ** the output contains a copy of each position from the right-hand input
  118646                 : ** doclist for which there is a position in the left-hand input doclist
  118647                 : ** exactly nDist tokens before it.
  118648                 : **
  118649                 : ** If the docids in the input doclists are sorted in ascending order,
  118650                 : ** parameter bDescDoclist should be false. If they are sorted in ascending 
  118651                 : ** order, it should be passed a non-zero value.
  118652                 : **
  118653                 : ** The right-hand input doclist is overwritten by this function.
  118654                 : */
  118655               0 : static void fts3DoclistPhraseMerge(
  118656                 :   int bDescDoclist,               /* True if arguments are desc */
  118657                 :   int nDist,                      /* Distance from left to right (1=adjacent) */
  118658                 :   char *aLeft, int nLeft,         /* Left doclist */
  118659                 :   char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
  118660                 : ){
  118661               0 :   sqlite3_int64 i1 = 0;
  118662               0 :   sqlite3_int64 i2 = 0;
  118663               0 :   sqlite3_int64 iPrev = 0;
  118664               0 :   char *pEnd1 = &aLeft[nLeft];
  118665               0 :   char *pEnd2 = &aRight[*pnRight];
  118666               0 :   char *p1 = aLeft;
  118667               0 :   char *p2 = aRight;
  118668                 :   char *p;
  118669               0 :   int bFirstOut = 0;
  118670               0 :   char *aOut = aRight;
  118671                 : 
  118672               0 :   assert( nDist>0 );
  118673                 : 
  118674               0 :   p = aOut;
  118675               0 :   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  118676               0 :   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  118677                 : 
  118678               0 :   while( p1 && p2 ){
  118679               0 :     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
  118680               0 :     if( iDiff==0 ){
  118681               0 :       char *pSave = p;
  118682               0 :       sqlite3_int64 iPrevSave = iPrev;
  118683               0 :       int bFirstOutSave = bFirstOut;
  118684                 : 
  118685               0 :       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
  118686               0 :       if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
  118687               0 :         p = pSave;
  118688               0 :         iPrev = iPrevSave;
  118689               0 :         bFirstOut = bFirstOutSave;
  118690                 :       }
  118691               0 :       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  118692               0 :       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  118693               0 :     }else if( iDiff<0 ){
  118694               0 :       fts3PoslistCopy(0, &p1);
  118695               0 :       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  118696                 :     }else{
  118697               0 :       fts3PoslistCopy(0, &p2);
  118698               0 :       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  118699                 :     }
  118700                 :   }
  118701                 : 
  118702               0 :   *pnRight = p - aOut;
  118703               0 : }
  118704                 : 
  118705                 : /*
  118706                 : ** Argument pList points to a position list nList bytes in size. This
  118707                 : ** function checks to see if the position list contains any entries for
  118708                 : ** a token in position 0 (of any column). If so, it writes argument iDelta
  118709                 : ** to the output buffer pOut, followed by a position list consisting only
  118710                 : ** of the entries from pList at position 0, and terminated by an 0x00 byte.
  118711                 : ** The value returned is the number of bytes written to pOut (if any).
  118712                 : */
  118713               0 : SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
  118714                 :   sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
  118715                 :   char *pList,                    /* Position list (no 0x00 term) */
  118716                 :   int nList,                      /* Size of pList in bytes */
  118717                 :   char *pOut                      /* Write output here */
  118718                 : ){
  118719               0 :   int nOut = 0;
  118720               0 :   int bWritten = 0;               /* True once iDelta has been written */
  118721               0 :   char *p = pList;
  118722               0 :   char *pEnd = &pList[nList];
  118723                 : 
  118724               0 :   if( *p!=0x01 ){
  118725               0 :     if( *p==0x02 ){
  118726               0 :       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
  118727               0 :       pOut[nOut++] = 0x02;
  118728               0 :       bWritten = 1;
  118729                 :     }
  118730               0 :     fts3ColumnlistCopy(0, &p);
  118731                 :   }
  118732                 : 
  118733               0 :   while( p<pEnd && *p==0x01 ){
  118734                 :     sqlite3_int64 iCol;
  118735               0 :     p++;
  118736               0 :     p += sqlite3Fts3GetVarint(p, &iCol);
  118737               0 :     if( *p==0x02 ){
  118738               0 :       if( bWritten==0 ){
  118739               0 :         nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
  118740               0 :         bWritten = 1;
  118741                 :       }
  118742               0 :       pOut[nOut++] = 0x01;
  118743               0 :       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
  118744               0 :       pOut[nOut++] = 0x02;
  118745                 :     }
  118746               0 :     fts3ColumnlistCopy(0, &p);
  118747                 :   }
  118748               0 :   if( bWritten ){
  118749               0 :     pOut[nOut++] = 0x00;
  118750                 :   }
  118751                 : 
  118752               0 :   return nOut;
  118753                 : }
  118754                 : 
  118755                 : 
  118756                 : /*
  118757                 : ** Merge all doclists in the TermSelect.aaOutput[] array into a single
  118758                 : ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
  118759                 : ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
  118760                 : **
  118761                 : ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
  118762                 : ** the responsibility of the caller to free any doclists left in the
  118763                 : ** TermSelect.aaOutput[] array.
  118764                 : */
  118765               0 : static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
  118766               0 :   char *aOut = 0;
  118767               0 :   int nOut = 0;
  118768                 :   int i;
  118769                 : 
  118770                 :   /* Loop through the doclists in the aaOutput[] array. Merge them all
  118771                 :   ** into a single doclist.
  118772                 :   */
  118773               0 :   for(i=0; i<SizeofArray(pTS->aaOutput); i++){
  118774               0 :     if( pTS->aaOutput[i] ){
  118775               0 :       if( !aOut ){
  118776               0 :         aOut = pTS->aaOutput[i];
  118777               0 :         nOut = pTS->anOutput[i];
  118778               0 :         pTS->aaOutput[i] = 0;
  118779                 :       }else{
  118780                 :         int nNew;
  118781                 :         char *aNew;
  118782                 : 
  118783               0 :         int rc = fts3DoclistOrMerge(p->bDescIdx, 
  118784                 :             pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
  118785                 :         );
  118786               0 :         if( rc!=SQLITE_OK ){
  118787               0 :           sqlite3_free(aOut);
  118788               0 :           return rc;
  118789                 :         }
  118790                 : 
  118791               0 :         sqlite3_free(pTS->aaOutput[i]);
  118792               0 :         sqlite3_free(aOut);
  118793               0 :         pTS->aaOutput[i] = 0;
  118794               0 :         aOut = aNew;
  118795               0 :         nOut = nNew;
  118796                 :       }
  118797                 :     }
  118798                 :   }
  118799                 : 
  118800               0 :   pTS->aaOutput[0] = aOut;
  118801               0 :   pTS->anOutput[0] = nOut;
  118802               0 :   return SQLITE_OK;
  118803                 : }
  118804                 : 
  118805                 : /*
  118806                 : ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
  118807                 : ** as the first argument. The merge is an "OR" merge (see function
  118808                 : ** fts3DoclistOrMerge() for details).
  118809                 : **
  118810                 : ** This function is called with the doclist for each term that matches
  118811                 : ** a queried prefix. It merges all these doclists into one, the doclist
  118812                 : ** for the specified prefix. Since there can be a very large number of
  118813                 : ** doclists to merge, the merging is done pair-wise using the TermSelect
  118814                 : ** object.
  118815                 : **
  118816                 : ** This function returns SQLITE_OK if the merge is successful, or an
  118817                 : ** SQLite error code (SQLITE_NOMEM) if an error occurs.
  118818                 : */
  118819               0 : static int fts3TermSelectMerge(
  118820                 :   Fts3Table *p,                   /* FTS table handle */
  118821                 :   TermSelect *pTS,                /* TermSelect object to merge into */
  118822                 :   char *aDoclist,                 /* Pointer to doclist */
  118823                 :   int nDoclist                    /* Size of aDoclist in bytes */
  118824                 : ){
  118825               0 :   if( pTS->aaOutput[0]==0 ){
  118826                 :     /* If this is the first term selected, copy the doclist to the output
  118827                 :     ** buffer using memcpy(). */
  118828               0 :     pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
  118829               0 :     pTS->anOutput[0] = nDoclist;
  118830               0 :     if( pTS->aaOutput[0] ){
  118831               0 :       memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
  118832                 :     }else{
  118833               0 :       return SQLITE_NOMEM;
  118834                 :     }
  118835                 :   }else{
  118836               0 :     char *aMerge = aDoclist;
  118837               0 :     int nMerge = nDoclist;
  118838                 :     int iOut;
  118839                 : 
  118840               0 :     for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
  118841               0 :       if( pTS->aaOutput[iOut]==0 ){
  118842               0 :         assert( iOut>0 );
  118843               0 :         pTS->aaOutput[iOut] = aMerge;
  118844               0 :         pTS->anOutput[iOut] = nMerge;
  118845               0 :         break;
  118846                 :       }else{
  118847                 :         char *aNew;
  118848                 :         int nNew;
  118849                 : 
  118850               0 :         int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge, 
  118851                 :             pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
  118852                 :         );
  118853               0 :         if( rc!=SQLITE_OK ){
  118854               0 :           if( aMerge!=aDoclist ) sqlite3_free(aMerge);
  118855               0 :           return rc;
  118856                 :         }
  118857                 : 
  118858               0 :         if( aMerge!=aDoclist ) sqlite3_free(aMerge);
  118859               0 :         sqlite3_free(pTS->aaOutput[iOut]);
  118860               0 :         pTS->aaOutput[iOut] = 0;
  118861                 :   
  118862               0 :         aMerge = aNew;
  118863               0 :         nMerge = nNew;
  118864               0 :         if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
  118865               0 :           pTS->aaOutput[iOut] = aMerge;
  118866               0 :           pTS->anOutput[iOut] = nMerge;
  118867                 :         }
  118868                 :       }
  118869                 :     }
  118870                 :   }
  118871               0 :   return SQLITE_OK;
  118872                 : }
  118873                 : 
  118874                 : /*
  118875                 : ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
  118876                 : */
  118877               8 : static int fts3SegReaderCursorAppend(
  118878                 :   Fts3MultiSegReader *pCsr, 
  118879                 :   Fts3SegReader *pNew
  118880                 : ){
  118881               8 :   if( (pCsr->nSegment%16)==0 ){
  118882                 :     Fts3SegReader **apNew;
  118883               5 :     int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
  118884               5 :     apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
  118885               5 :     if( !apNew ){
  118886               0 :       sqlite3Fts3SegReaderFree(pNew);
  118887               0 :       return SQLITE_NOMEM;
  118888                 :     }
  118889               5 :     pCsr->apSegment = apNew;
  118890                 :   }
  118891               8 :   pCsr->apSegment[pCsr->nSegment++] = pNew;
  118892               8 :   return SQLITE_OK;
  118893                 : }
  118894                 : 
  118895                 : /*
  118896                 : ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
  118897                 : ** 8th argument.
  118898                 : **
  118899                 : ** This function returns SQLITE_OK if successful, or an SQLite error code
  118900                 : ** otherwise.
  118901                 : */
  118902               6 : static int fts3SegReaderCursor(
  118903                 :   Fts3Table *p,                   /* FTS3 table handle */
  118904                 :   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
  118905                 :   int iLevel,                     /* Level of segments to scan */
  118906                 :   const char *zTerm,              /* Term to query for */
  118907                 :   int nTerm,                      /* Size of zTerm in bytes */
  118908                 :   int isPrefix,                   /* True for a prefix search */
  118909                 :   int isScan,                     /* True to scan from zTerm to EOF */
  118910                 :   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
  118911                 : ){
  118912               6 :   int rc = SQLITE_OK;             /* Error code */
  118913               6 :   sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
  118914                 :   int rc2;                        /* Result of sqlite3_reset() */
  118915                 : 
  118916                 :   /* If iLevel is less than 0 and this is not a scan, include a seg-reader 
  118917                 :   ** for the pending-terms. If this is a scan, then this call must be being
  118918                 :   ** made by an fts4aux module, not an FTS table. In this case calling
  118919                 :   ** Fts3SegReaderPending might segfault, as the data structures used by 
  118920                 :   ** fts4aux are not completely populated. So it's easiest to filter these
  118921                 :   ** calls out here.  */
  118922               6 :   if( iLevel<0 && p->aIndex ){
  118923               6 :     Fts3SegReader *pSeg = 0;
  118924               6 :     rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
  118925               6 :     if( rc==SQLITE_OK && pSeg ){
  118926               4 :       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
  118927                 :     }
  118928                 :   }
  118929                 : 
  118930               6 :   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
  118931               1 :     if( rc==SQLITE_OK ){
  118932               1 :       rc = sqlite3Fts3AllSegdirs(p, iIndex, iLevel, &pStmt);
  118933                 :     }
  118934                 : 
  118935               6 :     while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
  118936               4 :       Fts3SegReader *pSeg = 0;
  118937                 : 
  118938                 :       /* Read the values returned by the SELECT into local variables. */
  118939               4 :       sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
  118940               4 :       sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
  118941               4 :       sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
  118942               4 :       int nRoot = sqlite3_column_bytes(pStmt, 4);
  118943               4 :       char const *zRoot = sqlite3_column_blob(pStmt, 4);
  118944                 : 
  118945                 :       /* If zTerm is not NULL, and this segment is not stored entirely on its
  118946                 :       ** root node, the range of leaves scanned can be reduced. Do this. */
  118947               4 :       if( iStartBlock && zTerm ){
  118948               0 :         sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
  118949               0 :         rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
  118950               0 :         if( rc!=SQLITE_OK ) goto finished;
  118951               0 :         if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
  118952                 :       }
  118953                 :  
  118954               4 :       rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1, 
  118955                 :           iStartBlock, iLeavesEndBlock, iEndBlock, zRoot, nRoot, &pSeg
  118956                 :       );
  118957               4 :       if( rc!=SQLITE_OK ) goto finished;
  118958               4 :       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
  118959                 :     }
  118960                 :   }
  118961                 : 
  118962                 :  finished:
  118963               6 :   rc2 = sqlite3_reset(pStmt);
  118964               6 :   if( rc==SQLITE_DONE ) rc = rc2;
  118965                 : 
  118966               6 :   return rc;
  118967                 : }
  118968                 : 
  118969                 : /*
  118970                 : ** Set up a cursor object for iterating through a full-text index or a 
  118971                 : ** single level therein.
  118972                 : */
  118973               6 : SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
  118974                 :   Fts3Table *p,                   /* FTS3 table handle */
  118975                 :   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
  118976                 :   int iLevel,                     /* Level of segments to scan */
  118977                 :   const char *zTerm,              /* Term to query for */
  118978                 :   int nTerm,                      /* Size of zTerm in bytes */
  118979                 :   int isPrefix,                   /* True for a prefix search */
  118980                 :   int isScan,                     /* True to scan from zTerm to EOF */
  118981                 :   Fts3MultiSegReader *pCsr       /* Cursor object to populate */
  118982                 : ){
  118983               6 :   assert( iIndex>=0 && iIndex<p->nIndex );
  118984               6 :   assert( iLevel==FTS3_SEGCURSOR_ALL
  118985                 :       ||  iLevel==FTS3_SEGCURSOR_PENDING 
  118986                 :       ||  iLevel>=0
  118987                 :   );
  118988               6 :   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
  118989                 :   assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
  118990               6 :   assert( isPrefix==0 || isScan==0 );
  118991                 : 
  118992                 :   /* "isScan" is only set to true by the ft4aux module, an ordinary
  118993                 :   ** full-text tables. */
  118994               6 :   assert( isScan==0 || p->aIndex==0 );
  118995                 : 
  118996               6 :   memset(pCsr, 0, sizeof(Fts3MultiSegReader));
  118997                 : 
  118998               6 :   return fts3SegReaderCursor(
  118999                 :       p, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
  119000                 :   );
  119001                 : }
  119002                 : 
  119003                 : /*
  119004                 : ** In addition to its current configuration, have the Fts3MultiSegReader
  119005                 : ** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
  119006                 : **
  119007                 : ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  119008                 : */
  119009               0 : static int fts3SegReaderCursorAddZero(
  119010                 :   Fts3Table *p,                   /* FTS virtual table handle */
  119011                 :   const char *zTerm,              /* Term to scan doclist of */
  119012                 :   int nTerm,                      /* Number of bytes in zTerm */
  119013                 :   Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
  119014                 : ){
  119015               0 :   return fts3SegReaderCursor(p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr);
  119016                 : }
  119017                 : 
  119018                 : /*
  119019                 : ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
  119020                 : ** if isPrefix is true, to scan the doclist for all terms for which 
  119021                 : ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
  119022                 : ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
  119023                 : ** an SQLite error code.
  119024                 : **
  119025                 : ** It is the responsibility of the caller to free this object by eventually
  119026                 : ** passing it to fts3SegReaderCursorFree() 
  119027                 : **
  119028                 : ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  119029                 : ** Output parameter *ppSegcsr is set to 0 if an error occurs.
  119030                 : */
  119031               1 : static int fts3TermSegReaderCursor(
  119032                 :   Fts3Cursor *pCsr,               /* Virtual table cursor handle */
  119033                 :   const char *zTerm,              /* Term to query for */
  119034                 :   int nTerm,                      /* Size of zTerm in bytes */
  119035                 :   int isPrefix,                   /* True for a prefix search */
  119036                 :   Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
  119037                 : ){
  119038                 :   Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
  119039               1 :   int rc = SQLITE_NOMEM;          /* Return code */
  119040                 : 
  119041               1 :   pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
  119042               1 :   if( pSegcsr ){
  119043                 :     int i;
  119044               1 :     int bFound = 0;               /* True once an index has been found */
  119045               1 :     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  119046                 : 
  119047               1 :     if( isPrefix ){
  119048               0 :       for(i=1; bFound==0 && i<p->nIndex; i++){
  119049               0 :         if( p->aIndex[i].nPrefix==nTerm ){
  119050               0 :           bFound = 1;
  119051               0 :           rc = sqlite3Fts3SegReaderCursor(
  119052                 :               p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr);
  119053               0 :           pSegcsr->bLookup = 1;
  119054                 :         }
  119055                 :       }
  119056                 : 
  119057               0 :       for(i=1; bFound==0 && i<p->nIndex; i++){
  119058               0 :         if( p->aIndex[i].nPrefix==nTerm+1 ){
  119059               0 :           bFound = 1;
  119060               0 :           rc = sqlite3Fts3SegReaderCursor(
  119061                 :               p, i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
  119062                 :           );
  119063               0 :           if( rc==SQLITE_OK ){
  119064               0 :             rc = fts3SegReaderCursorAddZero(p, zTerm, nTerm, pSegcsr);
  119065                 :           }
  119066                 :         }
  119067                 :       }
  119068                 :     }
  119069                 : 
  119070               1 :     if( bFound==0 ){
  119071               1 :       rc = sqlite3Fts3SegReaderCursor(
  119072                 :           p, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
  119073                 :       );
  119074               1 :       pSegcsr->bLookup = !isPrefix;
  119075                 :     }
  119076                 :   }
  119077                 : 
  119078               1 :   *ppSegcsr = pSegcsr;
  119079               1 :   return rc;
  119080                 : }
  119081                 : 
  119082                 : /*
  119083                 : ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
  119084                 : */
  119085               1 : static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
  119086               1 :   sqlite3Fts3SegReaderFinish(pSegcsr);
  119087               1 :   sqlite3_free(pSegcsr);
  119088               1 : }
  119089                 : 
  119090                 : /*
  119091                 : ** This function retreives the doclist for the specified term (or term
  119092                 : ** prefix) from the database.
  119093                 : */
  119094               0 : static int fts3TermSelect(
  119095                 :   Fts3Table *p,                   /* Virtual table handle */
  119096                 :   Fts3PhraseToken *pTok,          /* Token to query for */
  119097                 :   int iColumn,                    /* Column to query (or -ve for all columns) */
  119098                 :   int *pnOut,                     /* OUT: Size of buffer at *ppOut */
  119099                 :   char **ppOut                    /* OUT: Malloced result buffer */
  119100                 : ){
  119101                 :   int rc;                         /* Return code */
  119102                 :   Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
  119103                 :   TermSelect tsc;                 /* Object for pair-wise doclist merging */
  119104                 :   Fts3SegFilter filter;           /* Segment term filter configuration */
  119105                 : 
  119106               0 :   pSegcsr = pTok->pSegcsr;
  119107               0 :   memset(&tsc, 0, sizeof(TermSelect));
  119108                 : 
  119109               0 :   filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
  119110               0 :         | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
  119111               0 :         | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
  119112               0 :         | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
  119113               0 :   filter.iCol = iColumn;
  119114               0 :   filter.zTerm = pTok->z;
  119115               0 :   filter.nTerm = pTok->n;
  119116                 : 
  119117               0 :   rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
  119118               0 :   while( SQLITE_OK==rc
  119119               0 :       && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) 
  119120                 :   ){
  119121               0 :     rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
  119122                 :   }
  119123                 : 
  119124               0 :   if( rc==SQLITE_OK ){
  119125               0 :     rc = fts3TermSelectFinishMerge(p, &tsc);
  119126                 :   }
  119127               0 :   if( rc==SQLITE_OK ){
  119128               0 :     *ppOut = tsc.aaOutput[0];
  119129               0 :     *pnOut = tsc.anOutput[0];
  119130                 :   }else{
  119131                 :     int i;
  119132               0 :     for(i=0; i<SizeofArray(tsc.aaOutput); i++){
  119133               0 :       sqlite3_free(tsc.aaOutput[i]);
  119134                 :     }
  119135                 :   }
  119136                 : 
  119137               0 :   fts3SegReaderCursorFree(pSegcsr);
  119138               0 :   pTok->pSegcsr = 0;
  119139               0 :   return rc;
  119140                 : }
  119141                 : 
  119142                 : /*
  119143                 : ** This function counts the total number of docids in the doclist stored
  119144                 : ** in buffer aList[], size nList bytes.
  119145                 : **
  119146                 : ** If the isPoslist argument is true, then it is assumed that the doclist
  119147                 : ** contains a position-list following each docid. Otherwise, it is assumed
  119148                 : ** that the doclist is simply a list of docids stored as delta encoded 
  119149                 : ** varints.
  119150                 : */
  119151               0 : static int fts3DoclistCountDocids(char *aList, int nList){
  119152               0 :   int nDoc = 0;                   /* Return value */
  119153               0 :   if( aList ){
  119154               0 :     char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
  119155               0 :     char *p = aList;              /* Cursor */
  119156               0 :     while( p<aEnd ){
  119157               0 :       nDoc++;
  119158               0 :       while( (*p++)&0x80 );     /* Skip docid varint */
  119159               0 :       fts3PoslistCopy(0, &p);   /* Skip over position list */
  119160                 :     }
  119161                 :   }
  119162                 : 
  119163               0 :   return nDoc;
  119164                 : }
  119165                 : 
  119166                 : /*
  119167                 : ** Advance the cursor to the next row in the %_content table that
  119168                 : ** matches the search criteria.  For a MATCH search, this will be
  119169                 : ** the next row that matches. For a full-table scan, this will be
  119170                 : ** simply the next row in the %_content table.  For a docid lookup,
  119171                 : ** this routine simply sets the EOF flag.
  119172                 : **
  119173                 : ** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
  119174                 : ** even if we reach end-of-file.  The fts3EofMethod() will be called
  119175                 : ** subsequently to determine whether or not an EOF was hit.
  119176                 : */
  119177               8 : static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
  119178                 :   int rc;
  119179               8 :   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  119180               8 :   if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
  119181              10 :     if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
  119182               1 :       pCsr->isEof = 1;
  119183               1 :       rc = sqlite3_reset(pCsr->pStmt);
  119184                 :     }else{
  119185               4 :       pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
  119186               4 :       rc = SQLITE_OK;
  119187                 :     }
  119188                 :   }else{
  119189               3 :     rc = fts3EvalNext((Fts3Cursor *)pCursor);
  119190                 :   }
  119191               8 :   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  119192               8 :   return rc;
  119193                 : }
  119194                 : 
  119195                 : /*
  119196                 : ** This is the xFilter interface for the virtual table.  See
  119197                 : ** the virtual table xFilter method documentation for additional
  119198                 : ** information.
  119199                 : **
  119200                 : ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
  119201                 : ** the %_content table.
  119202                 : **
  119203                 : ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
  119204                 : ** in the %_content table.
  119205                 : **
  119206                 : ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
  119207                 : ** column on the left-hand side of the MATCH operator is column
  119208                 : ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
  119209                 : ** side of the MATCH operator.
  119210                 : */
  119211               2 : static int fts3FilterMethod(
  119212                 :   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
  119213                 :   int idxNum,                     /* Strategy index */
  119214                 :   const char *idxStr,             /* Unused */
  119215                 :   int nVal,                       /* Number of elements in apVal */
  119216                 :   sqlite3_value **apVal           /* Arguments for the indexing scheme */
  119217                 : ){
  119218                 :   int rc;
  119219                 :   char *zSql;                     /* SQL statement used to access %_content */
  119220               2 :   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
  119221               2 :   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  119222                 : 
  119223                 :   UNUSED_PARAMETER(idxStr);
  119224                 :   UNUSED_PARAMETER(nVal);
  119225                 : 
  119226               2 :   assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
  119227               2 :   assert( nVal==0 || nVal==1 );
  119228               2 :   assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) );
  119229               2 :   assert( p->pSegments==0 );
  119230                 : 
  119231                 :   /* In case the cursor has been used before, clear it now. */
  119232               2 :   sqlite3_finalize(pCsr->pStmt);
  119233               2 :   sqlite3_free(pCsr->aDoclist);
  119234               2 :   sqlite3Fts3ExprFree(pCsr->pExpr);
  119235               2 :   memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
  119236                 : 
  119237               2 :   if( idxStr ){
  119238               0 :     pCsr->bDesc = (idxStr[0]=='D');
  119239                 :   }else{
  119240               2 :     pCsr->bDesc = p->bDescIdx;
  119241                 :   }
  119242               2 :   pCsr->eSearch = (i16)idxNum;
  119243                 : 
  119244               2 :   if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){
  119245               1 :     int iCol = idxNum-FTS3_FULLTEXT_SEARCH;
  119246               1 :     const char *zQuery = (const char *)sqlite3_value_text(apVal[0]);
  119247                 : 
  119248               1 :     if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  119249               0 :       return SQLITE_NOMEM;
  119250                 :     }
  119251                 : 
  119252               1 :     rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->bHasStat, 
  119253                 :         p->nColumn, iCol, zQuery, -1, &pCsr->pExpr
  119254                 :     );
  119255               1 :     if( rc!=SQLITE_OK ){
  119256               0 :       if( rc==SQLITE_ERROR ){
  119257                 :         static const char *zErr = "malformed MATCH expression: [%s]";
  119258               0 :         p->base.zErrMsg = sqlite3_mprintf(zErr, zQuery);
  119259                 :       }
  119260               0 :       return rc;
  119261                 :     }
  119262                 : 
  119263               1 :     rc = sqlite3Fts3ReadLock(p);
  119264               1 :     if( rc!=SQLITE_OK ) return rc;
  119265                 : 
  119266               1 :     rc = fts3EvalStart(pCsr);
  119267                 : 
  119268               1 :     sqlite3Fts3SegmentsClose(p);
  119269               1 :     if( rc!=SQLITE_OK ) return rc;
  119270               1 :     pCsr->pNextId = pCsr->aDoclist;
  119271               1 :     pCsr->iPrevId = 0;
  119272                 :   }
  119273                 : 
  119274                 :   /* Compile a SELECT statement for this cursor. For a full-table-scan, the
  119275                 :   ** statement loops through all rows of the %_content table. For a
  119276                 :   ** full-text query or docid lookup, the statement retrieves a single
  119277                 :   ** row by docid.
  119278                 :   */
  119279               2 :   if( idxNum==FTS3_FULLSCAN_SEARCH ){
  119280               1 :     zSql = sqlite3_mprintf(
  119281                 :         "SELECT %s ORDER BY rowid %s",
  119282               1 :         p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
  119283                 :     );
  119284               1 :     if( zSql ){
  119285               1 :       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
  119286               1 :       sqlite3_free(zSql);
  119287                 :     }else{
  119288               0 :       rc = SQLITE_NOMEM;
  119289                 :     }
  119290               1 :   }else if( idxNum==FTS3_DOCID_SEARCH ){
  119291               0 :     rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
  119292               0 :     if( rc==SQLITE_OK ){
  119293               0 :       rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
  119294                 :     }
  119295                 :   }
  119296               2 :   if( rc!=SQLITE_OK ) return rc;
  119297                 : 
  119298               2 :   return fts3NextMethod(pCursor);
  119299                 : }
  119300                 : 
  119301                 : /* 
  119302                 : ** This is the xEof method of the virtual table. SQLite calls this 
  119303                 : ** routine to find out if it has reached the end of a result set.
  119304                 : */
  119305               8 : static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
  119306               8 :   return ((Fts3Cursor *)pCursor)->isEof;
  119307                 : }
  119308                 : 
  119309                 : /* 
  119310                 : ** This is the xRowid method. The SQLite core calls this routine to
  119311                 : ** retrieve the rowid for the current row of the result set. fts3
  119312                 : ** exposes %_content.docid as the rowid for the virtual table. The
  119313                 : ** rowid should be written to *pRowid.
  119314                 : */
  119315               2 : static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
  119316               2 :   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
  119317               2 :   *pRowid = pCsr->iPrevId;
  119318               2 :   return SQLITE_OK;
  119319                 : }
  119320                 : 
  119321                 : /* 
  119322                 : ** This is the xColumn method, called by SQLite to request a value from
  119323                 : ** the row that the supplied cursor currently points to.
  119324                 : */
  119325               4 : static int fts3ColumnMethod(
  119326                 :   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
  119327                 :   sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
  119328                 :   int iCol                        /* Index of column to read value from */
  119329                 : ){
  119330               4 :   int rc = SQLITE_OK;             /* Return Code */
  119331               4 :   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
  119332               4 :   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
  119333                 : 
  119334                 :   /* The column value supplied by SQLite must be in range. */
  119335               4 :   assert( iCol>=0 && iCol<=p->nColumn+1 );
  119336                 : 
  119337               4 :   if( iCol==p->nColumn+1 ){
  119338                 :     /* This call is a request for the "docid" column. Since "docid" is an 
  119339                 :     ** alias for "rowid", use the xRowid() method to obtain the value.
  119340                 :     */
  119341               0 :     sqlite3_result_int64(pContext, pCsr->iPrevId);
  119342               4 :   }else if( iCol==p->nColumn ){
  119343                 :     /* The extra column whose name is the same as the table.
  119344                 :     ** Return a blob which is a pointer to the cursor.
  119345                 :     */
  119346               0 :     sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
  119347                 :   }else{
  119348               4 :     rc = fts3CursorSeek(0, pCsr);
  119349               4 :     if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
  119350               4 :       sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1));
  119351                 :     }
  119352                 :   }
  119353                 : 
  119354               4 :   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  119355               4 :   return rc;
  119356                 : }
  119357                 : 
  119358                 : /* 
  119359                 : ** This function is the implementation of the xUpdate callback used by 
  119360                 : ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
  119361                 : ** inserted, updated or deleted.
  119362                 : */
  119363               4 : static int fts3UpdateMethod(
  119364                 :   sqlite3_vtab *pVtab,            /* Virtual table handle */
  119365                 :   int nArg,                       /* Size of argument array */
  119366                 :   sqlite3_value **apVal,          /* Array of arguments */
  119367                 :   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
  119368                 : ){
  119369               4 :   return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
  119370                 : }
  119371                 : 
  119372                 : /*
  119373                 : ** Implementation of xSync() method. Flush the contents of the pending-terms
  119374                 : ** hash-table to the database.
  119375                 : */
  119376               5 : static int fts3SyncMethod(sqlite3_vtab *pVtab){
  119377               5 :   int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab);
  119378               5 :   sqlite3Fts3SegmentsClose((Fts3Table *)pVtab);
  119379               5 :   return rc;
  119380                 : }
  119381                 : 
  119382                 : /*
  119383                 : ** Implementation of xBegin() method. This is a no-op.
  119384                 : */
  119385               4 : static int fts3BeginMethod(sqlite3_vtab *pVtab){
  119386               4 :   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
  119387                 :   UNUSED_PARAMETER(pVtab);
  119388               4 :   assert( p->pSegments==0 );
  119389               4 :   assert( p->nPendingData==0 );
  119390               4 :   assert( p->inTransaction!=1 );
  119391               4 :   TESTONLY( p->inTransaction = 1 );
  119392               4 :   TESTONLY( p->mxSavepoint = -1; );
  119393               4 :   return SQLITE_OK;
  119394                 : }
  119395                 : 
  119396                 : /*
  119397                 : ** Implementation of xCommit() method. This is a no-op. The contents of
  119398                 : ** the pending-terms hash-table have already been flushed into the database
  119399                 : ** by fts3SyncMethod().
  119400                 : */
  119401               5 : static int fts3CommitMethod(sqlite3_vtab *pVtab){
  119402               5 :   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
  119403                 :   UNUSED_PARAMETER(pVtab);
  119404               5 :   assert( p->nPendingData==0 );
  119405               5 :   assert( p->inTransaction!=0 );
  119406               5 :   assert( p->pSegments==0 );
  119407               5 :   TESTONLY( p->inTransaction = 0 );
  119408               5 :   TESTONLY( p->mxSavepoint = -1; );
  119409               5 :   return SQLITE_OK;
  119410                 : }
  119411                 : 
  119412                 : /*
  119413                 : ** Implementation of xRollback(). Discard the contents of the pending-terms
  119414                 : ** hash-table. Any changes made to the database are reverted by SQLite.
  119415                 : */
  119416               0 : static int fts3RollbackMethod(sqlite3_vtab *pVtab){
  119417               0 :   Fts3Table *p = (Fts3Table*)pVtab;
  119418               0 :   sqlite3Fts3PendingTermsClear(p);
  119419               0 :   assert( p->inTransaction!=0 );
  119420               0 :   TESTONLY( p->inTransaction = 0 );
  119421               0 :   TESTONLY( p->mxSavepoint = -1; );
  119422               0 :   return SQLITE_OK;
  119423                 : }
  119424                 : 
  119425                 : /*
  119426                 : ** When called, *ppPoslist must point to the byte immediately following the
  119427                 : ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
  119428                 : ** moves *ppPoslist so that it instead points to the first byte of the
  119429                 : ** same position list.
  119430                 : */
  119431               0 : static void fts3ReversePoslist(char *pStart, char **ppPoslist){
  119432               0 :   char *p = &(*ppPoslist)[-2];
  119433               0 :   char c = 0;
  119434                 : 
  119435               0 :   while( p>pStart && (c=*p--)==0 );
  119436               0 :   while( p>pStart && (*p & 0x80) | c ){ 
  119437               0 :     c = *p--; 
  119438                 :   }
  119439               0 :   if( p>pStart ){ p = &p[2]; }
  119440               0 :   while( *p++&0x80 );
  119441               0 :   *ppPoslist = p;
  119442               0 : }
  119443                 : 
  119444                 : /*
  119445                 : ** Helper function used by the implementation of the overloaded snippet(),
  119446                 : ** offsets() and optimize() SQL functions.
  119447                 : **
  119448                 : ** If the value passed as the third argument is a blob of size
  119449                 : ** sizeof(Fts3Cursor*), then the blob contents are copied to the 
  119450                 : ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
  119451                 : ** message is written to context pContext and SQLITE_ERROR returned. The
  119452                 : ** string passed via zFunc is used as part of the error message.
  119453                 : */
  119454               0 : static int fts3FunctionArg(
  119455                 :   sqlite3_context *pContext,      /* SQL function call context */
  119456                 :   const char *zFunc,              /* Function name */
  119457                 :   sqlite3_value *pVal,            /* argv[0] passed to function */
  119458                 :   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
  119459                 : ){
  119460                 :   Fts3Cursor *pRet;
  119461               0 :   if( sqlite3_value_type(pVal)!=SQLITE_BLOB 
  119462               0 :    || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
  119463                 :   ){
  119464               0 :     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
  119465               0 :     sqlite3_result_error(pContext, zErr, -1);
  119466               0 :     sqlite3_free(zErr);
  119467               0 :     return SQLITE_ERROR;
  119468                 :   }
  119469               0 :   memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
  119470               0 :   *ppCsr = pRet;
  119471               0 :   return SQLITE_OK;
  119472                 : }
  119473                 : 
  119474                 : /*
  119475                 : ** Implementation of the snippet() function for FTS3
  119476                 : */
  119477               0 : static void fts3SnippetFunc(
  119478                 :   sqlite3_context *pContext,      /* SQLite function call context */
  119479                 :   int nVal,                       /* Size of apVal[] array */
  119480                 :   sqlite3_value **apVal           /* Array of arguments */
  119481                 : ){
  119482                 :   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
  119483               0 :   const char *zStart = "<b>";
  119484               0 :   const char *zEnd = "</b>";
  119485               0 :   const char *zEllipsis = "<b>...</b>";
  119486               0 :   int iCol = -1;
  119487               0 :   int nToken = 15;                /* Default number of tokens in snippet */
  119488                 : 
  119489                 :   /* There must be at least one argument passed to this function (otherwise
  119490                 :   ** the non-overloaded version would have been called instead of this one).
  119491                 :   */
  119492               0 :   assert( nVal>=1 );
  119493                 : 
  119494               0 :   if( nVal>6 ){
  119495               0 :     sqlite3_result_error(pContext, 
  119496                 :         "wrong number of arguments to function snippet()", -1);
  119497               0 :     return;
  119498                 :   }
  119499               0 :   if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
  119500                 : 
  119501               0 :   switch( nVal ){
  119502               0 :     case 6: nToken = sqlite3_value_int(apVal[5]);
  119503               0 :     case 5: iCol = sqlite3_value_int(apVal[4]);
  119504               0 :     case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
  119505               0 :     case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
  119506               0 :     case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
  119507                 :   }
  119508               0 :   if( !zEllipsis || !zEnd || !zStart ){
  119509               0 :     sqlite3_result_error_nomem(pContext);
  119510               0 :   }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
  119511               0 :     sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
  119512                 :   }
  119513                 : }
  119514                 : 
  119515                 : /*
  119516                 : ** Implementation of the offsets() function for FTS3
  119517                 : */
  119518               0 : static void fts3OffsetsFunc(
  119519                 :   sqlite3_context *pContext,      /* SQLite function call context */
  119520                 :   int nVal,                       /* Size of argument array */
  119521                 :   sqlite3_value **apVal           /* Array of arguments */
  119522                 : ){
  119523                 :   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
  119524                 : 
  119525                 :   UNUSED_PARAMETER(nVal);
  119526                 : 
  119527               0 :   assert( nVal==1 );
  119528               0 :   if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
  119529               0 :   assert( pCsr );
  119530               0 :   if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
  119531               0 :     sqlite3Fts3Offsets(pContext, pCsr);
  119532                 :   }
  119533                 : }
  119534                 : 
  119535                 : /* 
  119536                 : ** Implementation of the special optimize() function for FTS3. This 
  119537                 : ** function merges all segments in the database to a single segment.
  119538                 : ** Example usage is:
  119539                 : **
  119540                 : **   SELECT optimize(t) FROM t LIMIT 1;
  119541                 : **
  119542                 : ** where 't' is the name of an FTS3 table.
  119543                 : */
  119544               0 : static void fts3OptimizeFunc(
  119545                 :   sqlite3_context *pContext,      /* SQLite function call context */
  119546                 :   int nVal,                       /* Size of argument array */
  119547                 :   sqlite3_value **apVal           /* Array of arguments */
  119548                 : ){
  119549                 :   int rc;                         /* Return code */
  119550                 :   Fts3Table *p;                   /* Virtual table handle */
  119551                 :   Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
  119552                 : 
  119553                 :   UNUSED_PARAMETER(nVal);
  119554                 : 
  119555               0 :   assert( nVal==1 );
  119556               0 :   if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
  119557               0 :   p = (Fts3Table *)pCursor->base.pVtab;
  119558               0 :   assert( p );
  119559                 : 
  119560               0 :   rc = sqlite3Fts3Optimize(p);
  119561                 : 
  119562               0 :   switch( rc ){
  119563                 :     case SQLITE_OK:
  119564               0 :       sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
  119565               0 :       break;
  119566                 :     case SQLITE_DONE:
  119567               0 :       sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
  119568               0 :       break;
  119569                 :     default:
  119570               0 :       sqlite3_result_error_code(pContext, rc);
  119571               0 :       break;
  119572                 :   }
  119573                 : }
  119574                 : 
  119575                 : /*
  119576                 : ** Implementation of the matchinfo() function for FTS3
  119577                 : */
  119578               0 : static void fts3MatchinfoFunc(
  119579                 :   sqlite3_context *pContext,      /* SQLite function call context */
  119580                 :   int nVal,                       /* Size of argument array */
  119581                 :   sqlite3_value **apVal           /* Array of arguments */
  119582                 : ){
  119583                 :   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
  119584               0 :   assert( nVal==1 || nVal==2 );
  119585               0 :   if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
  119586               0 :     const char *zArg = 0;
  119587               0 :     if( nVal>1 ){
  119588               0 :       zArg = (const char *)sqlite3_value_text(apVal[1]);
  119589                 :     }
  119590               0 :     sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
  119591                 :   }
  119592               0 : }
  119593                 : 
  119594                 : /*
  119595                 : ** This routine implements the xFindFunction method for the FTS3
  119596                 : ** virtual table.
  119597                 : */
  119598               0 : static int fts3FindFunctionMethod(
  119599                 :   sqlite3_vtab *pVtab,            /* Virtual table handle */
  119600                 :   int nArg,                       /* Number of SQL function arguments */
  119601                 :   const char *zName,              /* Name of SQL function */
  119602                 :   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
  119603                 :   void **ppArg                    /* Unused */
  119604                 : ){
  119605                 :   struct Overloaded {
  119606                 :     const char *zName;
  119607                 :     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  119608               0 :   } aOverload[] = {
  119609                 :     { "snippet", fts3SnippetFunc },
  119610                 :     { "offsets", fts3OffsetsFunc },
  119611                 :     { "optimize", fts3OptimizeFunc },
  119612                 :     { "matchinfo", fts3MatchinfoFunc },
  119613                 :   };
  119614                 :   int i;                          /* Iterator variable */
  119615                 : 
  119616                 :   UNUSED_PARAMETER(pVtab);
  119617                 :   UNUSED_PARAMETER(nArg);
  119618                 :   UNUSED_PARAMETER(ppArg);
  119619                 : 
  119620               0 :   for(i=0; i<SizeofArray(aOverload); i++){
  119621               0 :     if( strcmp(zName, aOverload[i].zName)==0 ){
  119622               0 :       *pxFunc = aOverload[i].xFunc;
  119623               0 :       return 1;
  119624                 :     }
  119625                 :   }
  119626                 : 
  119627                 :   /* No function of the specified name was found. Return 0. */
  119628               0 :   return 0;
  119629                 : }
  119630                 : 
  119631                 : /*
  119632                 : ** Implementation of FTS3 xRename method. Rename an fts3 table.
  119633                 : */
  119634               0 : static int fts3RenameMethod(
  119635                 :   sqlite3_vtab *pVtab,            /* Virtual table handle */
  119636                 :   const char *zName               /* New name of table */
  119637                 : ){
  119638               0 :   Fts3Table *p = (Fts3Table *)pVtab;
  119639               0 :   sqlite3 *db = p->db;            /* Database connection */
  119640                 :   int rc;                         /* Return Code */
  119641                 : 
  119642                 :   /* As it happens, the pending terms table is always empty here. This is
  119643                 :   ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction 
  119644                 :   ** always opens a savepoint transaction. And the xSavepoint() method 
  119645                 :   ** flushes the pending terms table. But leave the (no-op) call to
  119646                 :   ** PendingTermsFlush() in in case that changes.
  119647                 :   */
  119648               0 :   assert( p->nPendingData==0 );
  119649               0 :   rc = sqlite3Fts3PendingTermsFlush(p);
  119650                 : 
  119651               0 :   if( p->zContentTbl==0 ){
  119652               0 :     fts3DbExec(&rc, db,
  119653                 :       "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
  119654                 :       p->zDb, p->zName, zName
  119655                 :     );
  119656                 :   }
  119657                 : 
  119658               0 :   if( p->bHasDocsize ){
  119659               0 :     fts3DbExec(&rc, db,
  119660                 :       "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
  119661                 :       p->zDb, p->zName, zName
  119662                 :     );
  119663                 :   }
  119664               0 :   if( p->bHasStat ){
  119665               0 :     fts3DbExec(&rc, db,
  119666                 :       "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
  119667                 :       p->zDb, p->zName, zName
  119668                 :     );
  119669                 :   }
  119670               0 :   fts3DbExec(&rc, db,
  119671                 :     "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
  119672                 :     p->zDb, p->zName, zName
  119673                 :   );
  119674               0 :   fts3DbExec(&rc, db,
  119675                 :     "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
  119676                 :     p->zDb, p->zName, zName
  119677                 :   );
  119678               0 :   return rc;
  119679                 : }
  119680                 : 
  119681                 : /*
  119682                 : ** The xSavepoint() method.
  119683                 : **
  119684                 : ** Flush the contents of the pending-terms table to disk.
  119685                 : */
  119686               0 : static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
  119687                 :   UNUSED_PARAMETER(iSavepoint);
  119688               0 :   assert( ((Fts3Table *)pVtab)->inTransaction );
  119689               0 :   assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
  119690               0 :   TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
  119691               0 :   return fts3SyncMethod(pVtab);
  119692                 : }
  119693                 : 
  119694                 : /*
  119695                 : ** The xRelease() method.
  119696                 : **
  119697                 : ** This is a no-op.
  119698                 : */
  119699               0 : static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
  119700               0 :   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
  119701                 :   UNUSED_PARAMETER(iSavepoint);
  119702                 :   UNUSED_PARAMETER(pVtab);
  119703               0 :   assert( p->inTransaction );
  119704               0 :   assert( p->mxSavepoint >= iSavepoint );
  119705               0 :   TESTONLY( p->mxSavepoint = iSavepoint-1 );
  119706               0 :   return SQLITE_OK;
  119707                 : }
  119708                 : 
  119709                 : /*
  119710                 : ** The xRollbackTo() method.
  119711                 : **
  119712                 : ** Discard the contents of the pending terms table.
  119713                 : */
  119714               0 : static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
  119715               0 :   Fts3Table *p = (Fts3Table*)pVtab;
  119716                 :   UNUSED_PARAMETER(iSavepoint);
  119717               0 :   assert( p->inTransaction );
  119718               0 :   assert( p->mxSavepoint >= iSavepoint );
  119719               0 :   TESTONLY( p->mxSavepoint = iSavepoint );
  119720               0 :   sqlite3Fts3PendingTermsClear(p);
  119721               0 :   return SQLITE_OK;
  119722                 : }
  119723                 : 
  119724                 : static const sqlite3_module fts3Module = {
  119725                 :   /* iVersion      */ 2,
  119726                 :   /* xCreate       */ fts3CreateMethod,
  119727                 :   /* xConnect      */ fts3ConnectMethod,
  119728                 :   /* xBestIndex    */ fts3BestIndexMethod,
  119729                 :   /* xDisconnect   */ fts3DisconnectMethod,
  119730                 :   /* xDestroy      */ fts3DestroyMethod,
  119731                 :   /* xOpen         */ fts3OpenMethod,
  119732                 :   /* xClose        */ fts3CloseMethod,
  119733                 :   /* xFilter       */ fts3FilterMethod,
  119734                 :   /* xNext         */ fts3NextMethod,
  119735                 :   /* xEof          */ fts3EofMethod,
  119736                 :   /* xColumn       */ fts3ColumnMethod,
  119737                 :   /* xRowid        */ fts3RowidMethod,
  119738                 :   /* xUpdate       */ fts3UpdateMethod,
  119739                 :   /* xBegin        */ fts3BeginMethod,
  119740                 :   /* xSync         */ fts3SyncMethod,
  119741                 :   /* xCommit       */ fts3CommitMethod,
  119742                 :   /* xRollback     */ fts3RollbackMethod,
  119743                 :   /* xFindFunction */ fts3FindFunctionMethod,
  119744                 :   /* xRename */       fts3RenameMethod,
  119745                 :   /* xSavepoint    */ fts3SavepointMethod,
  119746                 :   /* xRelease      */ fts3ReleaseMethod,
  119747                 :   /* xRollbackTo   */ fts3RollbackToMethod,
  119748                 : };
  119749                 : 
  119750                 : /*
  119751                 : ** This function is registered as the module destructor (called when an
  119752                 : ** FTS3 enabled database connection is closed). It frees the memory
  119753                 : ** allocated for the tokenizer hash table.
  119754                 : */
  119755            3277 : static void hashDestroy(void *p){
  119756            3277 :   Fts3Hash *pHash = (Fts3Hash *)p;
  119757            3277 :   sqlite3Fts3HashClear(pHash);
  119758            3277 :   sqlite3_free(pHash);
  119759            3277 : }
  119760                 : 
  119761                 : /*
  119762                 : ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are 
  119763                 : ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
  119764                 : ** respectively. The following three forward declarations are for functions
  119765                 : ** declared in these files used to retrieve the respective implementations.
  119766                 : **
  119767                 : ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
  119768                 : ** to by the argument to point to the "simple" tokenizer implementation.
  119769                 : ** And so on.
  119770                 : */
  119771                 : SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  119772                 : SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  119773                 : #ifdef SQLITE_ENABLE_ICU
  119774                 : SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  119775                 : #endif
  119776                 : 
  119777                 : /*
  119778                 : ** Initialise the fts3 extension. If this extension is built as part
  119779                 : ** of the sqlite library, then this function is called directly by
  119780                 : ** SQLite. If fts3 is built as a dynamically loadable extension, this
  119781                 : ** function is called by the sqlite3_extension_init() entry point.
  119782                 : */
  119783            3277 : SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
  119784            3277 :   int rc = SQLITE_OK;
  119785            3277 :   Fts3Hash *pHash = 0;
  119786            3277 :   const sqlite3_tokenizer_module *pSimple = 0;
  119787            3277 :   const sqlite3_tokenizer_module *pPorter = 0;
  119788                 : 
  119789                 : #ifdef SQLITE_ENABLE_ICU
  119790                 :   const sqlite3_tokenizer_module *pIcu = 0;
  119791                 :   sqlite3Fts3IcuTokenizerModule(&pIcu);
  119792                 : #endif
  119793                 : 
  119794                 : #ifdef SQLITE_TEST
  119795                 :   rc = sqlite3Fts3InitTerm(db);
  119796                 :   if( rc!=SQLITE_OK ) return rc;
  119797                 : #endif
  119798                 : 
  119799            3277 :   rc = sqlite3Fts3InitAux(db);
  119800            3277 :   if( rc!=SQLITE_OK ) return rc;
  119801                 : 
  119802            3277 :   sqlite3Fts3SimpleTokenizerModule(&pSimple);
  119803            3277 :   sqlite3Fts3PorterTokenizerModule(&pPorter);
  119804                 : 
  119805                 :   /* Allocate and initialise the hash-table used to store tokenizers. */
  119806            3277 :   pHash = sqlite3_malloc(sizeof(Fts3Hash));
  119807            3277 :   if( !pHash ){
  119808               0 :     rc = SQLITE_NOMEM;
  119809                 :   }else{
  119810            3277 :     sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
  119811                 :   }
  119812                 : 
  119813                 :   /* Load the built-in tokenizers into the hash table */
  119814            3277 :   if( rc==SQLITE_OK ){
  119815            3277 :     if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
  119816            3277 :      || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) 
  119817                 : #ifdef SQLITE_ENABLE_ICU
  119818                 :      || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
  119819                 : #endif
  119820                 :     ){
  119821               0 :       rc = SQLITE_NOMEM;
  119822                 :     }
  119823                 :   }
  119824                 : 
  119825                 : #ifdef SQLITE_TEST
  119826                 :   if( rc==SQLITE_OK ){
  119827                 :     rc = sqlite3Fts3ExprInitTestInterface(db);
  119828                 :   }
  119829                 : #endif
  119830                 : 
  119831                 :   /* Create the virtual table wrapper around the hash-table and overload 
  119832                 :   ** the two scalar functions. If this is successful, register the
  119833                 :   ** module with sqlite.
  119834                 :   */
  119835            3277 :   if( SQLITE_OK==rc 
  119836            3277 :    && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
  119837            3277 :    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
  119838            3277 :    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
  119839            3277 :    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
  119840            3277 :    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
  119841            3277 :    && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
  119842                 :   ){
  119843            3277 :     rc = sqlite3_create_module_v2(
  119844                 :         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
  119845                 :     );
  119846            3277 :     if( rc==SQLITE_OK ){
  119847            3277 :       rc = sqlite3_create_module_v2(
  119848                 :           db, "fts4", &fts3Module, (void *)pHash, 0
  119849                 :       );
  119850                 :     }
  119851            3277 :     return rc;
  119852                 :   }
  119853                 : 
  119854                 :   /* An error has occurred. Delete the hash table and return the error code. */
  119855               0 :   assert( rc!=SQLITE_OK );
  119856               0 :   if( pHash ){
  119857               0 :     sqlite3Fts3HashClear(pHash);
  119858               0 :     sqlite3_free(pHash);
  119859                 :   }
  119860               0 :   return rc;
  119861                 : }
  119862                 : 
  119863                 : /*
  119864                 : ** Allocate an Fts3MultiSegReader for each token in the expression headed
  119865                 : ** by pExpr. 
  119866                 : **
  119867                 : ** An Fts3SegReader object is a cursor that can seek or scan a range of
  119868                 : ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
  119869                 : ** Fts3SegReader objects internally to provide an interface to seek or scan
  119870                 : ** within the union of all segments of a b-tree. Hence the name.
  119871                 : **
  119872                 : ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
  119873                 : ** segment b-tree (if the term is not a prefix or it is a prefix for which
  119874                 : ** there exists prefix b-tree of the right length) then it may be traversed
  119875                 : ** and merged incrementally. Otherwise, it has to be merged into an in-memory 
  119876                 : ** doclist and then traversed.
  119877                 : */
  119878               1 : static void fts3EvalAllocateReaders(
  119879                 :   Fts3Cursor *pCsr,               /* FTS cursor handle */
  119880                 :   Fts3Expr *pExpr,                /* Allocate readers for this expression */
  119881                 :   int *pnToken,                   /* OUT: Total number of tokens in phrase. */
  119882                 :   int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
  119883                 :   int *pRc                        /* IN/OUT: Error code */
  119884                 : ){
  119885               1 :   if( pExpr && SQLITE_OK==*pRc ){
  119886               1 :     if( pExpr->eType==FTSQUERY_PHRASE ){
  119887                 :       int i;
  119888               1 :       int nToken = pExpr->pPhrase->nToken;
  119889               1 :       *pnToken += nToken;
  119890               2 :       for(i=0; i<nToken; i++){
  119891               1 :         Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
  119892               2 :         int rc = fts3TermSegReaderCursor(pCsr, 
  119893               1 :             pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
  119894                 :         );
  119895               1 :         if( rc!=SQLITE_OK ){
  119896               0 :           *pRc = rc;
  119897               0 :           return;
  119898                 :         }
  119899                 :       }
  119900               1 :       assert( pExpr->pPhrase->iDoclistToken==0 );
  119901               1 :       pExpr->pPhrase->iDoclistToken = -1;
  119902                 :     }else{
  119903               0 :       *pnOr += (pExpr->eType==FTSQUERY_OR);
  119904               0 :       fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
  119905               0 :       fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
  119906                 :     }
  119907                 :   }
  119908                 : }
  119909                 : 
  119910                 : /*
  119911                 : ** Arguments pList/nList contain the doclist for token iToken of phrase p.
  119912                 : ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
  119913                 : **
  119914                 : ** This function assumes that pList points to a buffer allocated using
  119915                 : ** sqlite3_malloc(). This function takes responsibility for eventually
  119916                 : ** freeing the buffer.
  119917                 : */
  119918               0 : static void fts3EvalPhraseMergeToken(
  119919                 :   Fts3Table *pTab,                /* FTS Table pointer */
  119920                 :   Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
  119921                 :   int iToken,                     /* Token pList/nList corresponds to */
  119922                 :   char *pList,                    /* Pointer to doclist */
  119923                 :   int nList                       /* Number of bytes in pList */
  119924                 : ){
  119925               0 :   assert( iToken!=p->iDoclistToken );
  119926                 : 
  119927               0 :   if( pList==0 ){
  119928               0 :     sqlite3_free(p->doclist.aAll);
  119929               0 :     p->doclist.aAll = 0;
  119930               0 :     p->doclist.nAll = 0;
  119931                 :   }
  119932                 : 
  119933               0 :   else if( p->iDoclistToken<0 ){
  119934               0 :     p->doclist.aAll = pList;
  119935               0 :     p->doclist.nAll = nList;
  119936                 :   }
  119937                 : 
  119938               0 :   else if( p->doclist.aAll==0 ){
  119939               0 :     sqlite3_free(pList);
  119940                 :   }
  119941                 : 
  119942                 :   else {
  119943                 :     char *pLeft;
  119944                 :     char *pRight;
  119945                 :     int nLeft;
  119946                 :     int nRight;
  119947                 :     int nDiff;
  119948                 : 
  119949               0 :     if( p->iDoclistToken<iToken ){
  119950               0 :       pLeft = p->doclist.aAll;
  119951               0 :       nLeft = p->doclist.nAll;
  119952               0 :       pRight = pList;
  119953               0 :       nRight = nList;
  119954               0 :       nDiff = iToken - p->iDoclistToken;
  119955                 :     }else{
  119956               0 :       pRight = p->doclist.aAll;
  119957               0 :       nRight = p->doclist.nAll;
  119958               0 :       pLeft = pList;
  119959               0 :       nLeft = nList;
  119960               0 :       nDiff = p->iDoclistToken - iToken;
  119961                 :     }
  119962                 : 
  119963               0 :     fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
  119964               0 :     sqlite3_free(pLeft);
  119965               0 :     p->doclist.aAll = pRight;
  119966               0 :     p->doclist.nAll = nRight;
  119967                 :   }
  119968                 : 
  119969               0 :   if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
  119970               0 : }
  119971                 : 
  119972                 : /*
  119973                 : ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
  119974                 : ** does not take deferred tokens into account.
  119975                 : **
  119976                 : ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  119977                 : */
  119978               0 : static int fts3EvalPhraseLoad(
  119979                 :   Fts3Cursor *pCsr,               /* FTS Cursor handle */
  119980                 :   Fts3Phrase *p                   /* Phrase object */
  119981                 : ){
  119982               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  119983                 :   int iToken;
  119984               0 :   int rc = SQLITE_OK;
  119985                 : 
  119986               0 :   for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
  119987               0 :     Fts3PhraseToken *pToken = &p->aToken[iToken];
  119988               0 :     assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
  119989                 : 
  119990               0 :     if( pToken->pSegcsr ){
  119991               0 :       int nThis = 0;
  119992               0 :       char *pThis = 0;
  119993               0 :       rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
  119994               0 :       if( rc==SQLITE_OK ){
  119995               0 :         fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
  119996                 :       }
  119997                 :     }
  119998               0 :     assert( pToken->pSegcsr==0 );
  119999                 :   }
  120000                 : 
  120001               0 :   return rc;
  120002                 : }
  120003                 : 
  120004                 : /*
  120005                 : ** This function is called on each phrase after the position lists for
  120006                 : ** any deferred tokens have been loaded into memory. It updates the phrases
  120007                 : ** current position list to include only those positions that are really
  120008                 : ** instances of the phrase (after considering deferred tokens). If this
  120009                 : ** means that the phrase does not appear in the current row, doclist.pList
  120010                 : ** and doclist.nList are both zeroed.
  120011                 : **
  120012                 : ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  120013                 : */
  120014               0 : static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
  120015                 :   int iToken;                     /* Used to iterate through phrase tokens */
  120016               0 :   char *aPoslist = 0;             /* Position list for deferred tokens */
  120017               0 :   int nPoslist = 0;               /* Number of bytes in aPoslist */
  120018               0 :   int iPrev = -1;                 /* Token number of previous deferred token */
  120019                 : 
  120020               0 :   assert( pPhrase->doclist.bFreeList==0 );
  120021                 : 
  120022               0 :   for(iToken=0; iToken<pPhrase->nToken; iToken++){
  120023               0 :     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
  120024               0 :     Fts3DeferredToken *pDeferred = pToken->pDeferred;
  120025                 : 
  120026               0 :     if( pDeferred ){
  120027                 :       char *pList;
  120028                 :       int nList;
  120029               0 :       int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
  120030               0 :       if( rc!=SQLITE_OK ) return rc;
  120031                 : 
  120032               0 :       if( pList==0 ){
  120033               0 :         sqlite3_free(aPoslist);
  120034               0 :         pPhrase->doclist.pList = 0;
  120035               0 :         pPhrase->doclist.nList = 0;
  120036               0 :         return SQLITE_OK;
  120037                 : 
  120038               0 :       }else if( aPoslist==0 ){
  120039               0 :         aPoslist = pList;
  120040               0 :         nPoslist = nList;
  120041                 : 
  120042                 :       }else{
  120043               0 :         char *aOut = pList;
  120044               0 :         char *p1 = aPoslist;
  120045               0 :         char *p2 = aOut;
  120046                 : 
  120047               0 :         assert( iPrev>=0 );
  120048               0 :         fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
  120049               0 :         sqlite3_free(aPoslist);
  120050               0 :         aPoslist = pList;
  120051               0 :         nPoslist = aOut - aPoslist;
  120052               0 :         if( nPoslist==0 ){
  120053               0 :           sqlite3_free(aPoslist);
  120054               0 :           pPhrase->doclist.pList = 0;
  120055               0 :           pPhrase->doclist.nList = 0;
  120056               0 :           return SQLITE_OK;
  120057                 :         }
  120058                 :       }
  120059               0 :       iPrev = iToken;
  120060                 :     }
  120061                 :   }
  120062                 : 
  120063               0 :   if( iPrev>=0 ){
  120064               0 :     int nMaxUndeferred = pPhrase->iDoclistToken;
  120065               0 :     if( nMaxUndeferred<0 ){
  120066               0 :       pPhrase->doclist.pList = aPoslist;
  120067               0 :       pPhrase->doclist.nList = nPoslist;
  120068               0 :       pPhrase->doclist.iDocid = pCsr->iPrevId;
  120069               0 :       pPhrase->doclist.bFreeList = 1;
  120070                 :     }else{
  120071                 :       int nDistance;
  120072                 :       char *p1;
  120073                 :       char *p2;
  120074                 :       char *aOut;
  120075                 : 
  120076               0 :       if( nMaxUndeferred>iPrev ){
  120077               0 :         p1 = aPoslist;
  120078               0 :         p2 = pPhrase->doclist.pList;
  120079               0 :         nDistance = nMaxUndeferred - iPrev;
  120080                 :       }else{
  120081               0 :         p1 = pPhrase->doclist.pList;
  120082               0 :         p2 = aPoslist;
  120083               0 :         nDistance = iPrev - nMaxUndeferred;
  120084                 :       }
  120085                 : 
  120086               0 :       aOut = (char *)sqlite3_malloc(nPoslist+8);
  120087               0 :       if( !aOut ){
  120088               0 :         sqlite3_free(aPoslist);
  120089               0 :         return SQLITE_NOMEM;
  120090                 :       }
  120091                 :       
  120092               0 :       pPhrase->doclist.pList = aOut;
  120093               0 :       if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
  120094               0 :         pPhrase->doclist.bFreeList = 1;
  120095               0 :         pPhrase->doclist.nList = (aOut - pPhrase->doclist.pList);
  120096                 :       }else{
  120097               0 :         sqlite3_free(aOut);
  120098               0 :         pPhrase->doclist.pList = 0;
  120099               0 :         pPhrase->doclist.nList = 0;
  120100                 :       }
  120101               0 :       sqlite3_free(aPoslist);
  120102                 :     }
  120103                 :   }
  120104                 : 
  120105               0 :   return SQLITE_OK;
  120106                 : }
  120107                 : 
  120108                 : /*
  120109                 : ** This function is called for each Fts3Phrase in a full-text query 
  120110                 : ** expression to initialize the mechanism for returning rows. Once this
  120111                 : ** function has been called successfully on an Fts3Phrase, it may be
  120112                 : ** used with fts3EvalPhraseNext() to iterate through the matching docids.
  120113                 : **
  120114                 : ** If parameter bOptOk is true, then the phrase may (or may not) use the
  120115                 : ** incremental loading strategy. Otherwise, the entire doclist is loaded into
  120116                 : ** memory within this call.
  120117                 : **
  120118                 : ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  120119                 : */
  120120               1 : static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
  120121                 :   int rc;                         /* Error code */
  120122               1 :   Fts3PhraseToken *pFirst = &p->aToken[0];
  120123               1 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  120124                 : 
  120125               1 :   if( pCsr->bDesc==pTab->bDescIdx 
  120126               1 :    && bOptOk==1 
  120127               1 :    && p->nToken==1 
  120128               1 :    && pFirst->pSegcsr 
  120129               1 :    && pFirst->pSegcsr->bLookup 
  120130               1 :    && pFirst->bFirst==0
  120131               1 :   ){
  120132                 :     /* Use the incremental approach. */
  120133               1 :     int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
  120134               2 :     rc = sqlite3Fts3MsrIncrStart(
  120135               1 :         pTab, pFirst->pSegcsr, iCol, pFirst->z, pFirst->n);
  120136               1 :     p->bIncr = 1;
  120137                 : 
  120138                 :   }else{
  120139                 :     /* Load the full doclist for the phrase into memory. */
  120140               0 :     rc = fts3EvalPhraseLoad(pCsr, p);
  120141               0 :     p->bIncr = 0;
  120142                 :   }
  120143                 : 
  120144               1 :   assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
  120145               1 :   return rc;
  120146                 : }
  120147                 : 
  120148                 : /*
  120149                 : ** This function is used to iterate backwards (from the end to start) 
  120150                 : ** through doclists. It is used by this module to iterate through phrase
  120151                 : ** doclists in reverse and by the fts3_write.c module to iterate through
  120152                 : ** pending-terms lists when writing to databases with "order=desc".
  120153                 : **
  120154                 : ** The doclist may be sorted in ascending (parameter bDescIdx==0) or 
  120155                 : ** descending (parameter bDescIdx==1) order of docid. Regardless, this
  120156                 : ** function iterates from the end of the doclist to the beginning.
  120157                 : */
  120158               0 : SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
  120159                 :   int bDescIdx,                   /* True if the doclist is desc */
  120160                 :   char *aDoclist,                 /* Pointer to entire doclist */
  120161                 :   int nDoclist,                   /* Length of aDoclist in bytes */
  120162                 :   char **ppIter,                  /* IN/OUT: Iterator pointer */
  120163                 :   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
  120164                 :   int *pnList,                    /* IN/OUT: List length pointer */
  120165                 :   u8 *pbEof                       /* OUT: End-of-file flag */
  120166                 : ){
  120167               0 :   char *p = *ppIter;
  120168                 : 
  120169               0 :   assert( nDoclist>0 );
  120170               0 :   assert( *pbEof==0 );
  120171               0 :   assert( p || *piDocid==0 );
  120172               0 :   assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
  120173                 : 
  120174               0 :   if( p==0 ){
  120175               0 :     sqlite3_int64 iDocid = 0;
  120176               0 :     char *pNext = 0;
  120177               0 :     char *pDocid = aDoclist;
  120178               0 :     char *pEnd = &aDoclist[nDoclist];
  120179               0 :     int iMul = 1;
  120180                 : 
  120181               0 :     while( pDocid<pEnd ){
  120182                 :       sqlite3_int64 iDelta;
  120183               0 :       pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
  120184               0 :       iDocid += (iMul * iDelta);
  120185               0 :       pNext = pDocid;
  120186               0 :       fts3PoslistCopy(0, &pDocid);
  120187               0 :       while( pDocid<pEnd && *pDocid==0 ) pDocid++;
  120188               0 :       iMul = (bDescIdx ? -1 : 1);
  120189                 :     }
  120190                 : 
  120191               0 :     *pnList = pEnd - pNext;
  120192               0 :     *ppIter = pNext;
  120193               0 :     *piDocid = iDocid;
  120194                 :   }else{
  120195               0 :     int iMul = (bDescIdx ? -1 : 1);
  120196                 :     sqlite3_int64 iDelta;
  120197               0 :     fts3GetReverseVarint(&p, aDoclist, &iDelta);
  120198               0 :     *piDocid -= (iMul * iDelta);
  120199                 : 
  120200               0 :     if( p==aDoclist ){
  120201               0 :       *pbEof = 1;
  120202                 :     }else{
  120203               0 :       char *pSave = p;
  120204               0 :       fts3ReversePoslist(aDoclist, &p);
  120205               0 :       *pnList = (pSave - p);
  120206                 :     }
  120207               0 :     *ppIter = p;
  120208                 :   }
  120209               0 : }
  120210                 : 
  120211                 : /*
  120212                 : ** Attempt to move the phrase iterator to point to the next matching docid. 
  120213                 : ** If an error occurs, return an SQLite error code. Otherwise, return 
  120214                 : ** SQLITE_OK.
  120215                 : **
  120216                 : ** If there is no "next" entry and no error occurs, then *pbEof is set to
  120217                 : ** 1 before returning. Otherwise, if no error occurs and the iterator is
  120218                 : ** successfully advanced, *pbEof is set to 0.
  120219                 : */
  120220               3 : static int fts3EvalPhraseNext(
  120221                 :   Fts3Cursor *pCsr,               /* FTS Cursor handle */
  120222                 :   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
  120223                 :   u8 *pbEof                       /* OUT: Set to 1 if EOF */
  120224                 : ){
  120225               3 :   int rc = SQLITE_OK;
  120226               3 :   Fts3Doclist *pDL = &p->doclist;
  120227               3 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  120228                 : 
  120229               3 :   if( p->bIncr ){
  120230               3 :     assert( p->nToken==1 );
  120231               3 :     assert( pDL->pNextDocid==0 );
  120232               3 :     rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr, 
  120233                 :         &pDL->iDocid, &pDL->pList, &pDL->nList
  120234                 :     );
  120235               3 :     if( rc==SQLITE_OK && !pDL->pList ){
  120236               1 :       *pbEof = 1;
  120237                 :     }
  120238               0 :   }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
  120239               0 :     sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll, 
  120240                 :         &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
  120241                 :     );
  120242               0 :     pDL->pList = pDL->pNextDocid;
  120243                 :   }else{
  120244                 :     char *pIter;                            /* Used to iterate through aAll */
  120245               0 :     char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
  120246               0 :     if( pDL->pNextDocid ){
  120247               0 :       pIter = pDL->pNextDocid;
  120248                 :     }else{
  120249               0 :       pIter = pDL->aAll;
  120250                 :     }
  120251                 : 
  120252               0 :     if( pIter>=pEnd ){
  120253                 :       /* We have already reached the end of this doclist. EOF. */
  120254               0 :       *pbEof = 1;
  120255                 :     }else{
  120256                 :       sqlite3_int64 iDelta;
  120257               0 :       pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
  120258               0 :       if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
  120259               0 :         pDL->iDocid += iDelta;
  120260                 :       }else{
  120261               0 :         pDL->iDocid -= iDelta;
  120262                 :       }
  120263               0 :       pDL->pList = pIter;
  120264               0 :       fts3PoslistCopy(0, &pIter);
  120265               0 :       pDL->nList = (pIter - pDL->pList);
  120266                 : 
  120267                 :       /* pIter now points just past the 0x00 that terminates the position-
  120268                 :       ** list for document pDL->iDocid. However, if this position-list was
  120269                 :       ** edited in place by fts3EvalNearTrim(), then pIter may not actually
  120270                 :       ** point to the start of the next docid value. The following line deals
  120271                 :       ** with this case by advancing pIter past the zero-padding added by
  120272                 :       ** fts3EvalNearTrim().  */
  120273               0 :       while( pIter<pEnd && *pIter==0 ) pIter++;
  120274                 : 
  120275               0 :       pDL->pNextDocid = pIter;
  120276               0 :       assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
  120277               0 :       *pbEof = 0;
  120278                 :     }
  120279                 :   }
  120280                 : 
  120281               3 :   return rc;
  120282                 : }
  120283                 : 
  120284                 : /*
  120285                 : **
  120286                 : ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  120287                 : ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
  120288                 : ** expression. Also the Fts3Expr.bDeferred variable is set to true for any
  120289                 : ** expressions for which all descendent tokens are deferred.
  120290                 : **
  120291                 : ** If parameter bOptOk is zero, then it is guaranteed that the
  120292                 : ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
  120293                 : ** each phrase in the expression (subject to deferred token processing).
  120294                 : ** Or, if bOptOk is non-zero, then one or more tokens within the expression
  120295                 : ** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
  120296                 : **
  120297                 : ** If an error occurs within this function, *pRc is set to an SQLite error
  120298                 : ** code before returning.
  120299                 : */
  120300               1 : static void fts3EvalStartReaders(
  120301                 :   Fts3Cursor *pCsr,               /* FTS Cursor handle */
  120302                 :   Fts3Expr *pExpr,                /* Expression to initialize phrases in */
  120303                 :   int bOptOk,                     /* True to enable incremental loading */
  120304                 :   int *pRc                        /* IN/OUT: Error code */
  120305                 : ){
  120306               1 :   if( pExpr && SQLITE_OK==*pRc ){
  120307               1 :     if( pExpr->eType==FTSQUERY_PHRASE ){
  120308                 :       int i;
  120309               1 :       int nToken = pExpr->pPhrase->nToken;
  120310               1 :       for(i=0; i<nToken; i++){
  120311               1 :         if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
  120312                 :       }
  120313               1 :       pExpr->bDeferred = (i==nToken);
  120314               1 :       *pRc = fts3EvalPhraseStart(pCsr, bOptOk, pExpr->pPhrase);
  120315                 :     }else{
  120316               0 :       fts3EvalStartReaders(pCsr, pExpr->pLeft, bOptOk, pRc);
  120317               0 :       fts3EvalStartReaders(pCsr, pExpr->pRight, bOptOk, pRc);
  120318               0 :       pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
  120319                 :     }
  120320                 :   }
  120321               1 : }
  120322                 : 
  120323                 : /*
  120324                 : ** An array of the following structures is assembled as part of the process
  120325                 : ** of selecting tokens to defer before the query starts executing (as part
  120326                 : ** of the xFilter() method). There is one element in the array for each
  120327                 : ** token in the FTS expression.
  120328                 : **
  120329                 : ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
  120330                 : ** to phrases that are connected only by AND and NEAR operators (not OR or
  120331                 : ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
  120332                 : ** separately. The root of a tokens AND/NEAR cluster is stored in 
  120333                 : ** Fts3TokenAndCost.pRoot.
  120334                 : */
  120335                 : typedef struct Fts3TokenAndCost Fts3TokenAndCost;
  120336                 : struct Fts3TokenAndCost {
  120337                 :   Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
  120338                 :   int iToken;                     /* Position of token in phrase */
  120339                 :   Fts3PhraseToken *pToken;        /* The token itself */
  120340                 :   Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
  120341                 :   int nOvfl;                      /* Number of overflow pages to load doclist */
  120342                 :   int iCol;                       /* The column the token must match */
  120343                 : };
  120344                 : 
  120345                 : /*
  120346                 : ** This function is used to populate an allocated Fts3TokenAndCost array.
  120347                 : **
  120348                 : ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  120349                 : ** Otherwise, if an error occurs during execution, *pRc is set to an
  120350                 : ** SQLite error code.
  120351                 : */
  120352               0 : static void fts3EvalTokenCosts(
  120353                 :   Fts3Cursor *pCsr,               /* FTS Cursor handle */
  120354                 :   Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
  120355                 :   Fts3Expr *pExpr,                /* Expression to consider */
  120356                 :   Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
  120357                 :   Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
  120358                 :   int *pRc                        /* IN/OUT: Error code */
  120359                 : ){
  120360               0 :   if( *pRc==SQLITE_OK ){
  120361               0 :     if( pExpr->eType==FTSQUERY_PHRASE ){
  120362               0 :       Fts3Phrase *pPhrase = pExpr->pPhrase;
  120363                 :       int i;
  120364               0 :       for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
  120365               0 :         Fts3TokenAndCost *pTC = (*ppTC)++;
  120366               0 :         pTC->pPhrase = pPhrase;
  120367               0 :         pTC->iToken = i;
  120368               0 :         pTC->pRoot = pRoot;
  120369               0 :         pTC->pToken = &pPhrase->aToken[i];
  120370               0 :         pTC->iCol = pPhrase->iColumn;
  120371               0 :         *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
  120372                 :       }
  120373               0 :     }else if( pExpr->eType!=FTSQUERY_NOT ){
  120374               0 :       assert( pExpr->eType==FTSQUERY_OR
  120375                 :            || pExpr->eType==FTSQUERY_AND
  120376                 :            || pExpr->eType==FTSQUERY_NEAR
  120377                 :       );
  120378               0 :       assert( pExpr->pLeft && pExpr->pRight );
  120379               0 :       if( pExpr->eType==FTSQUERY_OR ){
  120380               0 :         pRoot = pExpr->pLeft;
  120381               0 :         **ppOr = pRoot;
  120382               0 :         (*ppOr)++;
  120383                 :       }
  120384               0 :       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
  120385               0 :       if( pExpr->eType==FTSQUERY_OR ){
  120386               0 :         pRoot = pExpr->pRight;
  120387               0 :         **ppOr = pRoot;
  120388               0 :         (*ppOr)++;
  120389                 :       }
  120390               0 :       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
  120391                 :     }
  120392                 :   }
  120393               0 : }
  120394                 : 
  120395                 : /*
  120396                 : ** Determine the average document (row) size in pages. If successful,
  120397                 : ** write this value to *pnPage and return SQLITE_OK. Otherwise, return
  120398                 : ** an SQLite error code.
  120399                 : **
  120400                 : ** The average document size in pages is calculated by first calculating 
  120401                 : ** determining the average size in bytes, B. If B is less than the amount
  120402                 : ** of data that will fit on a single leaf page of an intkey table in
  120403                 : ** this database, then the average docsize is 1. Otherwise, it is 1 plus
  120404                 : ** the number of overflow pages consumed by a record B bytes in size.
  120405                 : */
  120406               0 : static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
  120407               0 :   if( pCsr->nRowAvg==0 ){
  120408                 :     /* The average document size, which is required to calculate the cost
  120409                 :     ** of each doclist, has not yet been determined. Read the required 
  120410                 :     ** data from the %_stat table to calculate it.
  120411                 :     **
  120412                 :     ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 
  120413                 :     ** varints, where nCol is the number of columns in the FTS3 table.
  120414                 :     ** The first varint is the number of documents currently stored in
  120415                 :     ** the table. The following nCol varints contain the total amount of
  120416                 :     ** data stored in all rows of each column of the table, from left
  120417                 :     ** to right.
  120418                 :     */
  120419                 :     int rc;
  120420               0 :     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
  120421                 :     sqlite3_stmt *pStmt;
  120422               0 :     sqlite3_int64 nDoc = 0;
  120423               0 :     sqlite3_int64 nByte = 0;
  120424                 :     const char *pEnd;
  120425                 :     const char *a;
  120426                 : 
  120427               0 :     rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
  120428               0 :     if( rc!=SQLITE_OK ) return rc;
  120429               0 :     a = sqlite3_column_blob(pStmt, 0);
  120430               0 :     assert( a );
  120431                 : 
  120432               0 :     pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
  120433               0 :     a += sqlite3Fts3GetVarint(a, &nDoc);
  120434               0 :     while( a<pEnd ){
  120435               0 :       a += sqlite3Fts3GetVarint(a, &nByte);
  120436                 :     }
  120437               0 :     if( nDoc==0 || nByte==0 ){
  120438               0 :       sqlite3_reset(pStmt);
  120439               0 :       return FTS_CORRUPT_VTAB;
  120440                 :     }
  120441                 : 
  120442               0 :     pCsr->nDoc = nDoc;
  120443               0 :     pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
  120444               0 :     assert( pCsr->nRowAvg>0 ); 
  120445               0 :     rc = sqlite3_reset(pStmt);
  120446               0 :     if( rc!=SQLITE_OK ) return rc;
  120447                 :   }
  120448                 : 
  120449               0 :   *pnPage = pCsr->nRowAvg;
  120450               0 :   return SQLITE_OK;
  120451                 : }
  120452                 : 
  120453                 : /*
  120454                 : ** This function is called to select the tokens (if any) that will be 
  120455                 : ** deferred. The array aTC[] has already been populated when this is
  120456                 : ** called.
  120457                 : **
  120458                 : ** This function is called once for each AND/NEAR cluster in the 
  120459                 : ** expression. Each invocation determines which tokens to defer within
  120460                 : ** the cluster with root node pRoot. See comments above the definition
  120461                 : ** of struct Fts3TokenAndCost for more details.
  120462                 : **
  120463                 : ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
  120464                 : ** called on each token to defer. Otherwise, an SQLite error code is
  120465                 : ** returned.
  120466                 : */
  120467               0 : static int fts3EvalSelectDeferred(
  120468                 :   Fts3Cursor *pCsr,               /* FTS Cursor handle */
  120469                 :   Fts3Expr *pRoot,                /* Consider tokens with this root node */
  120470                 :   Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
  120471                 :   int nTC                         /* Number of entries in aTC[] */
  120472                 : ){
  120473               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  120474               0 :   int nDocSize = 0;               /* Number of pages per doc loaded */
  120475               0 :   int rc = SQLITE_OK;             /* Return code */
  120476                 :   int ii;                         /* Iterator variable for various purposes */
  120477               0 :   int nOvfl = 0;                  /* Total overflow pages used by doclists */
  120478               0 :   int nToken = 0;                 /* Total number of tokens in cluster */
  120479                 : 
  120480               0 :   int nMinEst = 0;                /* The minimum count for any phrase so far. */
  120481               0 :   int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
  120482                 : 
  120483                 :   /* Tokens are never deferred for FTS tables created using the content=xxx
  120484                 :   ** option. The reason being that it is not guaranteed that the content
  120485                 :   ** table actually contains the same data as the index. To prevent this from
  120486                 :   ** causing any problems, the deferred token optimization is completely
  120487                 :   ** disabled for content=xxx tables. */
  120488               0 :   if( pTab->zContentTbl ){
  120489               0 :     return SQLITE_OK;
  120490                 :   }
  120491                 : 
  120492                 :   /* Count the tokens in this AND/NEAR cluster. If none of the doclists
  120493                 :   ** associated with the tokens spill onto overflow pages, or if there is
  120494                 :   ** only 1 token, exit early. No tokens to defer in this case. */
  120495               0 :   for(ii=0; ii<nTC; ii++){
  120496               0 :     if( aTC[ii].pRoot==pRoot ){
  120497               0 :       nOvfl += aTC[ii].nOvfl;
  120498               0 :       nToken++;
  120499                 :     }
  120500                 :   }
  120501               0 :   if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
  120502                 : 
  120503                 :   /* Obtain the average docsize (in pages). */
  120504               0 :   rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
  120505               0 :   assert( rc!=SQLITE_OK || nDocSize>0 );
  120506                 : 
  120507                 : 
  120508                 :   /* Iterate through all tokens in this AND/NEAR cluster, in ascending order 
  120509                 :   ** of the number of overflow pages that will be loaded by the pager layer 
  120510                 :   ** to retrieve the entire doclist for the token from the full-text index.
  120511                 :   ** Load the doclists for tokens that are either:
  120512                 :   **
  120513                 :   **   a. The cheapest token in the entire query (i.e. the one visited by the
  120514                 :   **      first iteration of this loop), or
  120515                 :   **
  120516                 :   **   b. Part of a multi-token phrase.
  120517                 :   **
  120518                 :   ** After each token doclist is loaded, merge it with the others from the
  120519                 :   ** same phrase and count the number of documents that the merged doclist
  120520                 :   ** contains. Set variable "nMinEst" to the smallest number of documents in 
  120521                 :   ** any phrase doclist for which 1 or more token doclists have been loaded.
  120522                 :   ** Let nOther be the number of other phrases for which it is certain that
  120523                 :   ** one or more tokens will not be deferred.
  120524                 :   **
  120525                 :   ** Then, for each token, defer it if loading the doclist would result in
  120526                 :   ** loading N or more overflow pages into memory, where N is computed as:
  120527                 :   **
  120528                 :   **    (nMinEst + 4^nOther - 1) / (4^nOther)
  120529                 :   */
  120530               0 :   for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
  120531                 :     int iTC;                      /* Used to iterate through aTC[] array. */
  120532               0 :     Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
  120533                 : 
  120534                 :     /* Set pTC to point to the cheapest remaining token. */
  120535               0 :     for(iTC=0; iTC<nTC; iTC++){
  120536               0 :       if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot 
  120537               0 :        && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl) 
  120538                 :       ){
  120539               0 :         pTC = &aTC[iTC];
  120540                 :       }
  120541                 :     }
  120542               0 :     assert( pTC );
  120543                 : 
  120544               0 :     if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
  120545                 :       /* The number of overflow pages to load for this (and therefore all
  120546                 :       ** subsequent) tokens is greater than the estimated number of pages 
  120547                 :       ** that will be loaded if all subsequent tokens are deferred.
  120548                 :       */
  120549               0 :       Fts3PhraseToken *pToken = pTC->pToken;
  120550               0 :       rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
  120551               0 :       fts3SegReaderCursorFree(pToken->pSegcsr);
  120552               0 :       pToken->pSegcsr = 0;
  120553                 :     }else{
  120554                 :       /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
  120555                 :       ** for-loop. Except, limit the value to 2^24 to prevent it from 
  120556                 :       ** overflowing the 32-bit integer it is stored in. */
  120557               0 :       if( ii<12 ) nLoad4 = nLoad4*4;
  120558                 : 
  120559               0 :       if( ii==0 || pTC->pPhrase->nToken>1 ){
  120560                 :         /* Either this is the cheapest token in the entire query, or it is
  120561                 :         ** part of a multi-token phrase. Either way, the entire doclist will
  120562                 :         ** (eventually) be loaded into memory. It may as well be now. */
  120563               0 :         Fts3PhraseToken *pToken = pTC->pToken;
  120564               0 :         int nList = 0;
  120565               0 :         char *pList = 0;
  120566               0 :         rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
  120567               0 :         assert( rc==SQLITE_OK || pList==0 );
  120568               0 :         if( rc==SQLITE_OK ){
  120569                 :           int nCount;
  120570               0 :           fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
  120571               0 :           nCount = fts3DoclistCountDocids(
  120572               0 :               pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
  120573                 :           );
  120574               0 :           if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
  120575                 :         }
  120576                 :       }
  120577                 :     }
  120578               0 :     pTC->pToken = 0;
  120579                 :   }
  120580                 : 
  120581               0 :   return rc;
  120582                 : }
  120583                 : 
  120584                 : /*
  120585                 : ** This function is called from within the xFilter method. It initializes
  120586                 : ** the full-text query currently stored in pCsr->pExpr. To iterate through
  120587                 : ** the results of a query, the caller does:
  120588                 : **
  120589                 : **    fts3EvalStart(pCsr);
  120590                 : **    while( 1 ){
  120591                 : **      fts3EvalNext(pCsr);
  120592                 : **      if( pCsr->bEof ) break;
  120593                 : **      ... return row pCsr->iPrevId to the caller ...
  120594                 : **    }
  120595                 : */
  120596               1 : static int fts3EvalStart(Fts3Cursor *pCsr){
  120597               1 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  120598               1 :   int rc = SQLITE_OK;
  120599               1 :   int nToken = 0;
  120600               1 :   int nOr = 0;
  120601                 : 
  120602                 :   /* Allocate a MultiSegReader for each token in the expression. */
  120603               1 :   fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
  120604                 : 
  120605                 :   /* Determine which, if any, tokens in the expression should be deferred. */
  120606               1 :   if( rc==SQLITE_OK && nToken>1 && pTab->bHasStat ){
  120607                 :     Fts3TokenAndCost *aTC;
  120608                 :     Fts3Expr **apOr;
  120609               0 :     aTC = (Fts3TokenAndCost *)sqlite3_malloc(
  120610                 :         sizeof(Fts3TokenAndCost) * nToken
  120611               0 :       + sizeof(Fts3Expr *) * nOr * 2
  120612                 :     );
  120613               0 :     apOr = (Fts3Expr **)&aTC[nToken];
  120614                 : 
  120615               0 :     if( !aTC ){
  120616               0 :       rc = SQLITE_NOMEM;
  120617                 :     }else{
  120618                 :       int ii;
  120619               0 :       Fts3TokenAndCost *pTC = aTC;
  120620               0 :       Fts3Expr **ppOr = apOr;
  120621                 : 
  120622               0 :       fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
  120623               0 :       nToken = pTC-aTC;
  120624               0 :       nOr = ppOr-apOr;
  120625                 : 
  120626               0 :       if( rc==SQLITE_OK ){
  120627               0 :         rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
  120628               0 :         for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
  120629               0 :           rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
  120630                 :         }
  120631                 :       }
  120632                 : 
  120633               0 :       sqlite3_free(aTC);
  120634                 :     }
  120635                 :   }
  120636                 : 
  120637               1 :   fts3EvalStartReaders(pCsr, pCsr->pExpr, 1, &rc);
  120638               1 :   return rc;
  120639                 : }
  120640                 : 
  120641                 : /*
  120642                 : ** Invalidate the current position list for phrase pPhrase.
  120643                 : */
  120644               4 : static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
  120645               4 :   if( pPhrase->doclist.bFreeList ){
  120646               0 :     sqlite3_free(pPhrase->doclist.pList);
  120647                 :   }
  120648               4 :   pPhrase->doclist.pList = 0;
  120649               4 :   pPhrase->doclist.nList = 0;
  120650               4 :   pPhrase->doclist.bFreeList = 0;
  120651               4 : }
  120652                 : 
  120653                 : /*
  120654                 : ** This function is called to edit the position list associated with
  120655                 : ** the phrase object passed as the fifth argument according to a NEAR
  120656                 : ** condition. For example:
  120657                 : **
  120658                 : **     abc NEAR/5 "def ghi"
  120659                 : **
  120660                 : ** Parameter nNear is passed the NEAR distance of the expression (5 in
  120661                 : ** the example above). When this function is called, *paPoslist points to
  120662                 : ** the position list, and *pnToken is the number of phrase tokens in, the
  120663                 : ** phrase on the other side of the NEAR operator to pPhrase. For example,
  120664                 : ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
  120665                 : ** the position list associated with phrase "abc".
  120666                 : **
  120667                 : ** All positions in the pPhrase position list that are not sufficiently
  120668                 : ** close to a position in the *paPoslist position list are removed. If this
  120669                 : ** leaves 0 positions, zero is returned. Otherwise, non-zero.
  120670                 : **
  120671                 : ** Before returning, *paPoslist is set to point to the position lsit 
  120672                 : ** associated with pPhrase. And *pnToken is set to the number of tokens in
  120673                 : ** pPhrase.
  120674                 : */
  120675               0 : static int fts3EvalNearTrim(
  120676                 :   int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
  120677                 :   char *aTmp,                     /* Temporary space to use */
  120678                 :   char **paPoslist,               /* IN/OUT: Position list */
  120679                 :   int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
  120680                 :   Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
  120681                 : ){
  120682               0 :   int nParam1 = nNear + pPhrase->nToken;
  120683               0 :   int nParam2 = nNear + *pnToken;
  120684                 :   int nNew;
  120685                 :   char *p2; 
  120686                 :   char *pOut; 
  120687                 :   int res;
  120688                 : 
  120689               0 :   assert( pPhrase->doclist.pList );
  120690                 : 
  120691               0 :   p2 = pOut = pPhrase->doclist.pList;
  120692               0 :   res = fts3PoslistNearMerge(
  120693                 :     &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
  120694                 :   );
  120695               0 :   if( res ){
  120696               0 :     nNew = (pOut - pPhrase->doclist.pList) - 1;
  120697               0 :     assert( pPhrase->doclist.pList[nNew]=='\0' );
  120698               0 :     assert( nNew<=pPhrase->doclist.nList && nNew>0 );
  120699               0 :     memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
  120700               0 :     pPhrase->doclist.nList = nNew;
  120701               0 :     *paPoslist = pPhrase->doclist.pList;
  120702               0 :     *pnToken = pPhrase->nToken;
  120703                 :   }
  120704                 : 
  120705               0 :   return res;
  120706                 : }
  120707                 : 
  120708                 : /*
  120709                 : ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
  120710                 : ** Otherwise, it advances the expression passed as the second argument to
  120711                 : ** point to the next matching row in the database. Expressions iterate through
  120712                 : ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
  120713                 : ** or descending if it is non-zero.
  120714                 : **
  120715                 : ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
  120716                 : ** successful, the following variables in pExpr are set:
  120717                 : **
  120718                 : **   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
  120719                 : **   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
  120720                 : **
  120721                 : ** If the expression is of type FTSQUERY_PHRASE, and the expression is not
  120722                 : ** at EOF, then the following variables are populated with the position list
  120723                 : ** for the phrase for the visited row:
  120724                 : **
  120725                 : **   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
  120726                 : **   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
  120727                 : **
  120728                 : ** It says above that this function advances the expression to the next
  120729                 : ** matching row. This is usually true, but there are the following exceptions:
  120730                 : **
  120731                 : **   1. Deferred tokens are not taken into account. If a phrase consists
  120732                 : **      entirely of deferred tokens, it is assumed to match every row in
  120733                 : **      the db. In this case the position-list is not populated at all. 
  120734                 : **
  120735                 : **      Or, if a phrase contains one or more deferred tokens and one or
  120736                 : **      more non-deferred tokens, then the expression is advanced to the 
  120737                 : **      next possible match, considering only non-deferred tokens. In other
  120738                 : **      words, if the phrase is "A B C", and "B" is deferred, the expression
  120739                 : **      is advanced to the next row that contains an instance of "A * C", 
  120740                 : **      where "*" may match any single token. The position list in this case
  120741                 : **      is populated as for "A * C" before returning.
  120742                 : **
  120743                 : **   2. NEAR is treated as AND. If the expression is "x NEAR y", it is 
  120744                 : **      advanced to point to the next row that matches "x AND y".
  120745                 : ** 
  120746                 : ** See fts3EvalTestDeferredAndNear() for details on testing if a row is
  120747                 : ** really a match, taking into account deferred tokens and NEAR operators.
  120748                 : */
  120749               3 : static void fts3EvalNextRow(
  120750                 :   Fts3Cursor *pCsr,               /* FTS Cursor handle */
  120751                 :   Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
  120752                 :   int *pRc                        /* IN/OUT: Error code */
  120753                 : ){
  120754               3 :   if( *pRc==SQLITE_OK ){
  120755               3 :     int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
  120756               3 :     assert( pExpr->bEof==0 );
  120757               3 :     pExpr->bStart = 1;
  120758                 : 
  120759               3 :     switch( pExpr->eType ){
  120760                 :       case FTSQUERY_NEAR:
  120761                 :       case FTSQUERY_AND: {
  120762               0 :         Fts3Expr *pLeft = pExpr->pLeft;
  120763               0 :         Fts3Expr *pRight = pExpr->pRight;
  120764               0 :         assert( !pLeft->bDeferred || !pRight->bDeferred );
  120765                 : 
  120766               0 :         if( pLeft->bDeferred ){
  120767                 :           /* LHS is entirely deferred. So we assume it matches every row.
  120768                 :           ** Advance the RHS iterator to find the next row visited. */
  120769               0 :           fts3EvalNextRow(pCsr, pRight, pRc);
  120770               0 :           pExpr->iDocid = pRight->iDocid;
  120771               0 :           pExpr->bEof = pRight->bEof;
  120772               0 :         }else if( pRight->bDeferred ){
  120773                 :           /* RHS is entirely deferred. So we assume it matches every row.
  120774                 :           ** Advance the LHS iterator to find the next row visited. */
  120775               0 :           fts3EvalNextRow(pCsr, pLeft, pRc);
  120776               0 :           pExpr->iDocid = pLeft->iDocid;
  120777               0 :           pExpr->bEof = pLeft->bEof;
  120778                 :         }else{
  120779                 :           /* Neither the RHS or LHS are deferred. */
  120780               0 :           fts3EvalNextRow(pCsr, pLeft, pRc);
  120781               0 :           fts3EvalNextRow(pCsr, pRight, pRc);
  120782               0 :           while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
  120783               0 :             sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
  120784               0 :             if( iDiff==0 ) break;
  120785               0 :             if( iDiff<0 ){
  120786               0 :               fts3EvalNextRow(pCsr, pLeft, pRc);
  120787                 :             }else{
  120788               0 :               fts3EvalNextRow(pCsr, pRight, pRc);
  120789                 :             }
  120790                 :           }
  120791               0 :           pExpr->iDocid = pLeft->iDocid;
  120792               0 :           pExpr->bEof = (pLeft->bEof || pRight->bEof);
  120793                 :         }
  120794               0 :         break;
  120795                 :       }
  120796                 :   
  120797                 :       case FTSQUERY_OR: {
  120798               0 :         Fts3Expr *pLeft = pExpr->pLeft;
  120799               0 :         Fts3Expr *pRight = pExpr->pRight;
  120800               0 :         sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
  120801                 : 
  120802               0 :         assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
  120803               0 :         assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
  120804                 : 
  120805               0 :         if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
  120806               0 :           fts3EvalNextRow(pCsr, pLeft, pRc);
  120807               0 :         }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
  120808               0 :           fts3EvalNextRow(pCsr, pRight, pRc);
  120809                 :         }else{
  120810               0 :           fts3EvalNextRow(pCsr, pLeft, pRc);
  120811               0 :           fts3EvalNextRow(pCsr, pRight, pRc);
  120812                 :         }
  120813                 : 
  120814               0 :         pExpr->bEof = (pLeft->bEof && pRight->bEof);
  120815               0 :         iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
  120816               0 :         if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
  120817               0 :           pExpr->iDocid = pLeft->iDocid;
  120818                 :         }else{
  120819               0 :           pExpr->iDocid = pRight->iDocid;
  120820                 :         }
  120821                 : 
  120822               0 :         break;
  120823                 :       }
  120824                 : 
  120825                 :       case FTSQUERY_NOT: {
  120826               0 :         Fts3Expr *pLeft = pExpr->pLeft;
  120827               0 :         Fts3Expr *pRight = pExpr->pRight;
  120828                 : 
  120829               0 :         if( pRight->bStart==0 ){
  120830               0 :           fts3EvalNextRow(pCsr, pRight, pRc);
  120831               0 :           assert( *pRc!=SQLITE_OK || pRight->bStart );
  120832                 :         }
  120833                 : 
  120834               0 :         fts3EvalNextRow(pCsr, pLeft, pRc);
  120835               0 :         if( pLeft->bEof==0 ){
  120836               0 :           while( !*pRc 
  120837               0 :               && !pRight->bEof 
  120838               0 :               && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0 
  120839                 :           ){
  120840               0 :             fts3EvalNextRow(pCsr, pRight, pRc);
  120841                 :           }
  120842                 :         }
  120843               0 :         pExpr->iDocid = pLeft->iDocid;
  120844               0 :         pExpr->bEof = pLeft->bEof;
  120845               0 :         break;
  120846                 :       }
  120847                 : 
  120848                 :       default: {
  120849               3 :         Fts3Phrase *pPhrase = pExpr->pPhrase;
  120850               3 :         fts3EvalInvalidatePoslist(pPhrase);
  120851               3 :         *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
  120852               3 :         pExpr->iDocid = pPhrase->doclist.iDocid;
  120853               3 :         break;
  120854                 :       }
  120855                 :     }
  120856                 :   }
  120857               3 : }
  120858                 : 
  120859                 : /*
  120860                 : ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
  120861                 : ** cluster, then this function returns 1 immediately.
  120862                 : **
  120863                 : ** Otherwise, it checks if the current row really does match the NEAR 
  120864                 : ** expression, using the data currently stored in the position lists 
  120865                 : ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression. 
  120866                 : **
  120867                 : ** If the current row is a match, the position list associated with each
  120868                 : ** phrase in the NEAR expression is edited in place to contain only those
  120869                 : ** phrase instances sufficiently close to their peers to satisfy all NEAR
  120870                 : ** constraints. In this case it returns 1. If the NEAR expression does not 
  120871                 : ** match the current row, 0 is returned. The position lists may or may not
  120872                 : ** be edited if 0 is returned.
  120873                 : */
  120874               0 : static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
  120875               0 :   int res = 1;
  120876                 : 
  120877                 :   /* The following block runs if pExpr is the root of a NEAR query.
  120878                 :   ** For example, the query:
  120879                 :   **
  120880                 :   **         "w" NEAR "x" NEAR "y" NEAR "z"
  120881                 :   **
  120882                 :   ** which is represented in tree form as:
  120883                 :   **
  120884                 :   **                               |
  120885                 :   **                          +--NEAR--+      <-- root of NEAR query
  120886                 :   **                          |        |
  120887                 :   **                     +--NEAR--+   "z"
  120888                 :   **                     |        |
  120889                 :   **                +--NEAR--+   "y"
  120890                 :   **                |        |
  120891                 :   **               "w"      "x"
  120892                 :   **
  120893                 :   ** The right-hand child of a NEAR node is always a phrase. The 
  120894                 :   ** left-hand child may be either a phrase or a NEAR node. There are
  120895                 :   ** no exceptions to this - it's the way the parser in fts3_expr.c works.
  120896                 :   */
  120897               0 :   if( *pRc==SQLITE_OK 
  120898               0 :    && pExpr->eType==FTSQUERY_NEAR 
  120899               0 :    && pExpr->bEof==0
  120900               0 :    && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
  120901                 :   ){
  120902                 :     Fts3Expr *p; 
  120903               0 :     int nTmp = 0;                 /* Bytes of temp space */
  120904                 :     char *aTmp;                   /* Temp space for PoslistNearMerge() */
  120905                 : 
  120906                 :     /* Allocate temporary working space. */
  120907               0 :     for(p=pExpr; p->pLeft; p=p->pLeft){
  120908               0 :       nTmp += p->pRight->pPhrase->doclist.nList;
  120909                 :     }
  120910               0 :     nTmp += p->pPhrase->doclist.nList;
  120911               0 :     aTmp = sqlite3_malloc(nTmp*2);
  120912               0 :     if( !aTmp ){
  120913               0 :       *pRc = SQLITE_NOMEM;
  120914               0 :       res = 0;
  120915                 :     }else{
  120916               0 :       char *aPoslist = p->pPhrase->doclist.pList;
  120917               0 :       int nToken = p->pPhrase->nToken;
  120918                 : 
  120919               0 :       for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
  120920               0 :         Fts3Phrase *pPhrase = p->pRight->pPhrase;
  120921               0 :         int nNear = p->nNear;
  120922               0 :         res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
  120923                 :       }
  120924                 :   
  120925               0 :       aPoslist = pExpr->pRight->pPhrase->doclist.pList;
  120926               0 :       nToken = pExpr->pRight->pPhrase->nToken;
  120927               0 :       for(p=pExpr->pLeft; p && res; p=p->pLeft){
  120928                 :         int nNear;
  120929                 :         Fts3Phrase *pPhrase;
  120930               0 :         assert( p->pParent && p->pParent->pLeft==p );
  120931               0 :         nNear = p->pParent->nNear;
  120932               0 :         pPhrase = (
  120933               0 :             p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
  120934                 :         );
  120935               0 :         res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
  120936                 :       }
  120937                 :     }
  120938                 : 
  120939               0 :     sqlite3_free(aTmp);
  120940                 :   }
  120941                 : 
  120942               0 :   return res;
  120943                 : }
  120944                 : 
  120945                 : /*
  120946                 : ** This function is a helper function for fts3EvalTestDeferredAndNear().
  120947                 : ** Assuming no error occurs or has occurred, It returns non-zero if the
  120948                 : ** expression passed as the second argument matches the row that pCsr 
  120949                 : ** currently points to, or zero if it does not.
  120950                 : **
  120951                 : ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  120952                 : ** If an error occurs during execution of this function, *pRc is set to 
  120953                 : ** the appropriate SQLite error code. In this case the returned value is 
  120954                 : ** undefined.
  120955                 : */
  120956               2 : static int fts3EvalTestExpr(
  120957                 :   Fts3Cursor *pCsr,               /* FTS cursor handle */
  120958                 :   Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
  120959                 :   int *pRc                        /* IN/OUT: Error code */
  120960                 : ){
  120961               2 :   int bHit = 1;                   /* Return value */
  120962               2 :   if( *pRc==SQLITE_OK ){
  120963               2 :     switch( pExpr->eType ){
  120964                 :       case FTSQUERY_NEAR:
  120965                 :       case FTSQUERY_AND:
  120966               0 :         bHit = (
  120967               0 :             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
  120968               0 :          && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
  120969               0 :          && fts3EvalNearTest(pExpr, pRc)
  120970                 :         );
  120971                 : 
  120972                 :         /* If the NEAR expression does not match any rows, zero the doclist for 
  120973                 :         ** all phrases involved in the NEAR. This is because the snippet(),
  120974                 :         ** offsets() and matchinfo() functions are not supposed to recognize 
  120975                 :         ** any instances of phrases that are part of unmatched NEAR queries. 
  120976                 :         ** For example if this expression:
  120977                 :         **
  120978                 :         **    ... MATCH 'a OR (b NEAR c)'
  120979                 :         **
  120980                 :         ** is matched against a row containing:
  120981                 :         **
  120982                 :         **        'a b d e'
  120983                 :         **
  120984                 :         ** then any snippet() should ony highlight the "a" term, not the "b"
  120985                 :         ** (as "b" is part of a non-matching NEAR clause).
  120986                 :         */
  120987               0 :         if( bHit==0 
  120988               0 :          && pExpr->eType==FTSQUERY_NEAR 
  120989               0 :          && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
  120990                 :         ){
  120991                 :           Fts3Expr *p;
  120992               0 :           for(p=pExpr; p->pPhrase==0; p=p->pLeft){
  120993               0 :             if( p->pRight->iDocid==pCsr->iPrevId ){
  120994               0 :               fts3EvalInvalidatePoslist(p->pRight->pPhrase);
  120995                 :             }
  120996                 :           }
  120997               0 :           if( p->iDocid==pCsr->iPrevId ){
  120998               0 :             fts3EvalInvalidatePoslist(p->pPhrase);
  120999                 :           }
  121000                 :         }
  121001                 : 
  121002               0 :         break;
  121003                 : 
  121004                 :       case FTSQUERY_OR: {
  121005               0 :         int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
  121006               0 :         int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
  121007               0 :         bHit = bHit1 || bHit2;
  121008               0 :         break;
  121009                 :       }
  121010                 : 
  121011                 :       case FTSQUERY_NOT:
  121012               0 :         bHit = (
  121013               0 :             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
  121014               0 :          && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
  121015                 :         );
  121016               0 :         break;
  121017                 : 
  121018                 :       default: {
  121019               2 :         if( pCsr->pDeferred 
  121020               0 :          && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
  121021               0 :         ){
  121022               0 :           Fts3Phrase *pPhrase = pExpr->pPhrase;
  121023               0 :           assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
  121024               0 :           if( pExpr->bDeferred ){
  121025               0 :             fts3EvalInvalidatePoslist(pPhrase);
  121026                 :           }
  121027               0 :           *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
  121028               0 :           bHit = (pPhrase->doclist.pList!=0);
  121029               0 :           pExpr->iDocid = pCsr->iPrevId;
  121030                 :         }else{
  121031               2 :           bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
  121032                 :         }
  121033               2 :         break;
  121034                 :       }
  121035                 :     }
  121036                 :   }
  121037               2 :   return bHit;
  121038                 : }
  121039                 : 
  121040                 : /*
  121041                 : ** This function is called as the second part of each xNext operation when
  121042                 : ** iterating through the results of a full-text query. At this point the
  121043                 : ** cursor points to a row that matches the query expression, with the
  121044                 : ** following caveats:
  121045                 : **
  121046                 : **   * Up until this point, "NEAR" operators in the expression have been
  121047                 : **     treated as "AND".
  121048                 : **
  121049                 : **   * Deferred tokens have not yet been considered.
  121050                 : **
  121051                 : ** If *pRc is not SQLITE_OK when this function is called, it immediately
  121052                 : ** returns 0. Otherwise, it tests whether or not after considering NEAR
  121053                 : ** operators and deferred tokens the current row is still a match for the
  121054                 : ** expression. It returns 1 if both of the following are true:
  121055                 : **
  121056                 : **   1. *pRc is SQLITE_OK when this function returns, and
  121057                 : **
  121058                 : **   2. After scanning the current FTS table row for the deferred tokens,
  121059                 : **      it is determined that the row does *not* match the query.
  121060                 : **
  121061                 : ** Or, if no error occurs and it seems the current row does match the FTS
  121062                 : ** query, return 0.
  121063                 : */
  121064               2 : static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
  121065               2 :   int rc = *pRc;
  121066               2 :   int bMiss = 0;
  121067               2 :   if( rc==SQLITE_OK ){
  121068                 : 
  121069                 :     /* If there are one or more deferred tokens, load the current row into
  121070                 :     ** memory and scan it to determine the position list for each deferred
  121071                 :     ** token. Then, see if this row is really a match, considering deferred
  121072                 :     ** tokens and NEAR operators (neither of which were taken into account
  121073                 :     ** earlier, by fts3EvalNextRow()). 
  121074                 :     */
  121075               2 :     if( pCsr->pDeferred ){
  121076               0 :       rc = fts3CursorSeek(0, pCsr);
  121077               0 :       if( rc==SQLITE_OK ){
  121078               0 :         rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
  121079                 :       }
  121080                 :     }
  121081               2 :     bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
  121082                 : 
  121083                 :     /* Free the position-lists accumulated for each deferred token above. */
  121084               2 :     sqlite3Fts3FreeDeferredDoclists(pCsr);
  121085               2 :     *pRc = rc;
  121086                 :   }
  121087               2 :   return (rc==SQLITE_OK && bMiss);
  121088                 : }
  121089                 : 
  121090                 : /*
  121091                 : ** Advance to the next document that matches the FTS expression in
  121092                 : ** Fts3Cursor.pExpr.
  121093                 : */
  121094               3 : static int fts3EvalNext(Fts3Cursor *pCsr){
  121095               3 :   int rc = SQLITE_OK;             /* Return Code */
  121096               3 :   Fts3Expr *pExpr = pCsr->pExpr;
  121097               3 :   assert( pCsr->isEof==0 );
  121098               3 :   if( pExpr==0 ){
  121099               0 :     pCsr->isEof = 1;
  121100                 :   }else{
  121101                 :     do {
  121102               3 :       if( pCsr->isRequireSeek==0 ){
  121103               3 :         sqlite3_reset(pCsr->pStmt);
  121104                 :       }
  121105               3 :       assert( sqlite3_data_count(pCsr->pStmt)==0 );
  121106               3 :       fts3EvalNextRow(pCsr, pExpr, &rc);
  121107               3 :       pCsr->isEof = pExpr->bEof;
  121108               3 :       pCsr->isRequireSeek = 1;
  121109               3 :       pCsr->isMatchinfoNeeded = 1;
  121110               3 :       pCsr->iPrevId = pExpr->iDocid;
  121111               3 :     }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
  121112                 :   }
  121113               3 :   return rc;
  121114                 : }
  121115                 : 
  121116                 : /*
  121117                 : ** Restart interation for expression pExpr so that the next call to
  121118                 : ** fts3EvalNext() visits the first row. Do not allow incremental 
  121119                 : ** loading or merging of phrase doclists for this iteration.
  121120                 : **
  121121                 : ** If *pRc is other than SQLITE_OK when this function is called, it is
  121122                 : ** a no-op. If an error occurs within this function, *pRc is set to an
  121123                 : ** SQLite error code before returning.
  121124                 : */
  121125               0 : static void fts3EvalRestart(
  121126                 :   Fts3Cursor *pCsr,
  121127                 :   Fts3Expr *pExpr,
  121128                 :   int *pRc
  121129                 : ){
  121130               0 :   if( pExpr && *pRc==SQLITE_OK ){
  121131               0 :     Fts3Phrase *pPhrase = pExpr->pPhrase;
  121132                 : 
  121133               0 :     if( pPhrase ){
  121134               0 :       fts3EvalInvalidatePoslist(pPhrase);
  121135               0 :       if( pPhrase->bIncr ){
  121136               0 :         assert( pPhrase->nToken==1 );
  121137               0 :         assert( pPhrase->aToken[0].pSegcsr );
  121138               0 :         sqlite3Fts3MsrIncrRestart(pPhrase->aToken[0].pSegcsr);
  121139               0 :         *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
  121140                 :       }
  121141                 : 
  121142               0 :       pPhrase->doclist.pNextDocid = 0;
  121143               0 :       pPhrase->doclist.iDocid = 0;
  121144                 :     }
  121145                 : 
  121146               0 :     pExpr->iDocid = 0;
  121147               0 :     pExpr->bEof = 0;
  121148               0 :     pExpr->bStart = 0;
  121149                 : 
  121150               0 :     fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
  121151               0 :     fts3EvalRestart(pCsr, pExpr->pRight, pRc);
  121152                 :   }
  121153               0 : }
  121154                 : 
  121155                 : /*
  121156                 : ** After allocating the Fts3Expr.aMI[] array for each phrase in the 
  121157                 : ** expression rooted at pExpr, the cursor iterates through all rows matched
  121158                 : ** by pExpr, calling this function for each row. This function increments
  121159                 : ** the values in Fts3Expr.aMI[] according to the position-list currently
  121160                 : ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase 
  121161                 : ** expression nodes.
  121162                 : */
  121163               0 : static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
  121164               0 :   if( pExpr ){
  121165               0 :     Fts3Phrase *pPhrase = pExpr->pPhrase;
  121166               0 :     if( pPhrase && pPhrase->doclist.pList ){
  121167               0 :       int iCol = 0;
  121168               0 :       char *p = pPhrase->doclist.pList;
  121169                 : 
  121170               0 :       assert( *p );
  121171                 :       while( 1 ){
  121172               0 :         u8 c = 0;
  121173               0 :         int iCnt = 0;
  121174               0 :         while( 0xFE & (*p | c) ){
  121175               0 :           if( (c&0x80)==0 ) iCnt++;
  121176               0 :           c = *p++ & 0x80;
  121177                 :         }
  121178                 : 
  121179                 :         /* aMI[iCol*3 + 1] = Number of occurrences
  121180                 :         ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
  121181                 :         */
  121182               0 :         pExpr->aMI[iCol*3 + 1] += iCnt;
  121183               0 :         pExpr->aMI[iCol*3 + 2] += (iCnt>0);
  121184               0 :         if( *p==0x00 ) break;
  121185               0 :         p++;
  121186               0 :         p += sqlite3Fts3GetVarint32(p, &iCol);
  121187               0 :       }
  121188                 :     }
  121189                 : 
  121190               0 :     fts3EvalUpdateCounts(pExpr->pLeft);
  121191               0 :     fts3EvalUpdateCounts(pExpr->pRight);
  121192                 :   }
  121193               0 : }
  121194                 : 
  121195                 : /*
  121196                 : ** Expression pExpr must be of type FTSQUERY_PHRASE.
  121197                 : **
  121198                 : ** If it is not already allocated and populated, this function allocates and
  121199                 : ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
  121200                 : ** of a NEAR expression, then it also allocates and populates the same array
  121201                 : ** for all other phrases that are part of the NEAR expression.
  121202                 : **
  121203                 : ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
  121204                 : ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
  121205                 : */
  121206               0 : static int fts3EvalGatherStats(
  121207                 :   Fts3Cursor *pCsr,               /* Cursor object */
  121208                 :   Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
  121209                 : ){
  121210               0 :   int rc = SQLITE_OK;             /* Return code */
  121211                 : 
  121212               0 :   assert( pExpr->eType==FTSQUERY_PHRASE );
  121213               0 :   if( pExpr->aMI==0 ){
  121214               0 :     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  121215                 :     Fts3Expr *pRoot;                /* Root of NEAR expression */
  121216                 :     Fts3Expr *p;                    /* Iterator used for several purposes */
  121217                 : 
  121218               0 :     sqlite3_int64 iPrevId = pCsr->iPrevId;
  121219                 :     sqlite3_int64 iDocid;
  121220                 :     u8 bEof;
  121221                 : 
  121222                 :     /* Find the root of the NEAR expression */
  121223               0 :     pRoot = pExpr;
  121224               0 :     while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
  121225               0 :       pRoot = pRoot->pParent;
  121226                 :     }
  121227               0 :     iDocid = pRoot->iDocid;
  121228               0 :     bEof = pRoot->bEof;
  121229               0 :     assert( pRoot->bStart );
  121230                 : 
  121231                 :     /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
  121232               0 :     for(p=pRoot; p; p=p->pLeft){
  121233               0 :       Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
  121234               0 :       assert( pE->aMI==0 );
  121235               0 :       pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
  121236               0 :       if( !pE->aMI ) return SQLITE_NOMEM;
  121237               0 :       memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
  121238                 :     }
  121239                 : 
  121240               0 :     fts3EvalRestart(pCsr, pRoot, &rc);
  121241                 : 
  121242               0 :     while( pCsr->isEof==0 && rc==SQLITE_OK ){
  121243                 : 
  121244                 :       do {
  121245                 :         /* Ensure the %_content statement is reset. */
  121246               0 :         if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
  121247               0 :         assert( sqlite3_data_count(pCsr->pStmt)==0 );
  121248                 : 
  121249                 :         /* Advance to the next document */
  121250               0 :         fts3EvalNextRow(pCsr, pRoot, &rc);
  121251               0 :         pCsr->isEof = pRoot->bEof;
  121252               0 :         pCsr->isRequireSeek = 1;
  121253               0 :         pCsr->isMatchinfoNeeded = 1;
  121254               0 :         pCsr->iPrevId = pRoot->iDocid;
  121255               0 :       }while( pCsr->isEof==0 
  121256               0 :            && pRoot->eType==FTSQUERY_NEAR 
  121257               0 :            && fts3EvalTestDeferredAndNear(pCsr, &rc) 
  121258               0 :       );
  121259                 : 
  121260               0 :       if( rc==SQLITE_OK && pCsr->isEof==0 ){
  121261               0 :         fts3EvalUpdateCounts(pRoot);
  121262                 :       }
  121263                 :     }
  121264                 : 
  121265               0 :     pCsr->isEof = 0;
  121266               0 :     pCsr->iPrevId = iPrevId;
  121267                 : 
  121268               0 :     if( bEof ){
  121269               0 :       pRoot->bEof = bEof;
  121270                 :     }else{
  121271                 :       /* Caution: pRoot may iterate through docids in ascending or descending
  121272                 :       ** order. For this reason, even though it seems more defensive, the 
  121273                 :       ** do loop can not be written:
  121274                 :       **
  121275                 :       **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
  121276                 :       */
  121277               0 :       fts3EvalRestart(pCsr, pRoot, &rc);
  121278                 :       do {
  121279               0 :         fts3EvalNextRow(pCsr, pRoot, &rc);
  121280               0 :         assert( pRoot->bEof==0 );
  121281               0 :       }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
  121282               0 :       fts3EvalTestDeferredAndNear(pCsr, &rc);
  121283                 :     }
  121284                 :   }
  121285               0 :   return rc;
  121286                 : }
  121287                 : 
  121288                 : /*
  121289                 : ** This function is used by the matchinfo() module to query a phrase 
  121290                 : ** expression node for the following information:
  121291                 : **
  121292                 : **   1. The total number of occurrences of the phrase in each column of 
  121293                 : **      the FTS table (considering all rows), and
  121294                 : **
  121295                 : **   2. For each column, the number of rows in the table for which the
  121296                 : **      column contains at least one instance of the phrase.
  121297                 : **
  121298                 : ** If no error occurs, SQLITE_OK is returned and the values for each column
  121299                 : ** written into the array aiOut as follows:
  121300                 : **
  121301                 : **   aiOut[iCol*3 + 1] = Number of occurrences
  121302                 : **   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
  121303                 : **
  121304                 : ** Caveats:
  121305                 : **
  121306                 : **   * If a phrase consists entirely of deferred tokens, then all output 
  121307                 : **     values are set to the number of documents in the table. In other
  121308                 : **     words we assume that very common tokens occur exactly once in each 
  121309                 : **     column of each row of the table.
  121310                 : **
  121311                 : **   * If a phrase contains some deferred tokens (and some non-deferred 
  121312                 : **     tokens), count the potential occurrence identified by considering
  121313                 : **     the non-deferred tokens instead of actual phrase occurrences.
  121314                 : **
  121315                 : **   * If the phrase is part of a NEAR expression, then only phrase instances
  121316                 : **     that meet the NEAR constraint are included in the counts.
  121317                 : */
  121318               0 : SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
  121319                 :   Fts3Cursor *pCsr,               /* FTS cursor handle */
  121320                 :   Fts3Expr *pExpr,                /* Phrase expression */
  121321                 :   u32 *aiOut                      /* Array to write results into (see above) */
  121322                 : ){
  121323               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  121324               0 :   int rc = SQLITE_OK;
  121325                 :   int iCol;
  121326                 : 
  121327               0 :   if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
  121328               0 :     assert( pCsr->nDoc>0 );
  121329               0 :     for(iCol=0; iCol<pTab->nColumn; iCol++){
  121330               0 :       aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
  121331               0 :       aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
  121332                 :     }
  121333                 :   }else{
  121334               0 :     rc = fts3EvalGatherStats(pCsr, pExpr);
  121335               0 :     if( rc==SQLITE_OK ){
  121336               0 :       assert( pExpr->aMI );
  121337               0 :       for(iCol=0; iCol<pTab->nColumn; iCol++){
  121338               0 :         aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
  121339               0 :         aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
  121340                 :       }
  121341                 :     }
  121342                 :   }
  121343                 : 
  121344               0 :   return rc;
  121345                 : }
  121346                 : 
  121347                 : /*
  121348                 : ** The expression pExpr passed as the second argument to this function
  121349                 : ** must be of type FTSQUERY_PHRASE. 
  121350                 : **
  121351                 : ** The returned value is either NULL or a pointer to a buffer containing
  121352                 : ** a position-list indicating the occurrences of the phrase in column iCol
  121353                 : ** of the current row. 
  121354                 : **
  121355                 : ** More specifically, the returned buffer contains 1 varint for each 
  121356                 : ** occurence of the phrase in the column, stored using the normal (delta+2) 
  121357                 : ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
  121358                 : ** if the requested column contains "a b X c d X X" and the position-list
  121359                 : ** for 'X' is requested, the buffer returned may contain:
  121360                 : **
  121361                 : **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
  121362                 : **
  121363                 : ** This function works regardless of whether or not the phrase is deferred,
  121364                 : ** incremental, or neither.
  121365                 : */
  121366               0 : SQLITE_PRIVATE char *sqlite3Fts3EvalPhrasePoslist(
  121367                 :   Fts3Cursor *pCsr,               /* FTS3 cursor object */
  121368                 :   Fts3Expr *pExpr,                /* Phrase to return doclist for */
  121369                 :   int iCol                        /* Column to return position list for */
  121370                 : ){
  121371               0 :   Fts3Phrase *pPhrase = pExpr->pPhrase;
  121372               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  121373               0 :   char *pIter = pPhrase->doclist.pList;
  121374                 :   int iThis;
  121375                 : 
  121376               0 :   assert( iCol>=0 && iCol<pTab->nColumn );
  121377               0 :   if( !pIter 
  121378               0 :    || pExpr->bEof 
  121379               0 :    || pExpr->iDocid!=pCsr->iPrevId
  121380               0 :    || (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) 
  121381                 :   ){
  121382               0 :     return 0;
  121383                 :   }
  121384                 : 
  121385               0 :   assert( pPhrase->doclist.nList>0 );
  121386               0 :   if( *pIter==0x01 ){
  121387               0 :     pIter++;
  121388               0 :     pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
  121389                 :   }else{
  121390               0 :     iThis = 0;
  121391                 :   }
  121392               0 :   while( iThis<iCol ){
  121393               0 :     fts3ColumnlistCopy(0, &pIter);
  121394               0 :     if( *pIter==0x00 ) return 0;
  121395               0 :     pIter++;
  121396               0 :     pIter += sqlite3Fts3GetVarint32(pIter, &iThis);
  121397                 :   }
  121398                 : 
  121399               0 :   return ((iCol==iThis)?pIter:0);
  121400                 : }
  121401                 : 
  121402                 : /*
  121403                 : ** Free all components of the Fts3Phrase structure that were allocated by
  121404                 : ** the eval module. Specifically, this means to free:
  121405                 : **
  121406                 : **   * the contents of pPhrase->doclist, and
  121407                 : **   * any Fts3MultiSegReader objects held by phrase tokens.
  121408                 : */
  121409               1 : SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
  121410               1 :   if( pPhrase ){
  121411                 :     int i;
  121412               1 :     sqlite3_free(pPhrase->doclist.aAll);
  121413               1 :     fts3EvalInvalidatePoslist(pPhrase);
  121414               1 :     memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
  121415               2 :     for(i=0; i<pPhrase->nToken; i++){
  121416               1 :       fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
  121417               1 :       pPhrase->aToken[i].pSegcsr = 0;
  121418                 :     }
  121419                 :   }
  121420               1 : }
  121421                 : 
  121422                 : /*
  121423                 : ** Return SQLITE_CORRUPT_VTAB.
  121424                 : */
  121425                 : #ifdef SQLITE_DEBUG
  121426               0 : SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
  121427               0 :   return SQLITE_CORRUPT_VTAB;
  121428                 : }
  121429                 : #endif
  121430                 : 
  121431                 : #if !SQLITE_CORE
  121432                 : /*
  121433                 : ** Initialize API pointer table, if required.
  121434                 : */
  121435                 : SQLITE_API int sqlite3_extension_init(
  121436                 :   sqlite3 *db, 
  121437                 :   char **pzErrMsg,
  121438                 :   const sqlite3_api_routines *pApi
  121439                 : ){
  121440                 :   SQLITE_EXTENSION_INIT2(pApi)
  121441                 :   return sqlite3Fts3Init(db);
  121442                 : }
  121443                 : #endif
  121444                 : 
  121445                 : #endif
  121446                 : 
  121447                 : /************** End of fts3.c ************************************************/
  121448                 : /************** Begin file fts3_aux.c ****************************************/
  121449                 : /*
  121450                 : ** 2011 Jan 27
  121451                 : **
  121452                 : ** The author disclaims copyright to this source code.  In place of
  121453                 : ** a legal notice, here is a blessing:
  121454                 : **
  121455                 : **    May you do good and not evil.
  121456                 : **    May you find forgiveness for yourself and forgive others.
  121457                 : **    May you share freely, never taking more than you give.
  121458                 : **
  121459                 : ******************************************************************************
  121460                 : **
  121461                 : */
  121462                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  121463                 : 
  121464                 : /* #include <string.h> */
  121465                 : /* #include <assert.h> */
  121466                 : 
  121467                 : typedef struct Fts3auxTable Fts3auxTable;
  121468                 : typedef struct Fts3auxCursor Fts3auxCursor;
  121469                 : 
  121470                 : struct Fts3auxTable {
  121471                 :   sqlite3_vtab base;              /* Base class used by SQLite core */
  121472                 :   Fts3Table *pFts3Tab;
  121473                 : };
  121474                 : 
  121475                 : struct Fts3auxCursor {
  121476                 :   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
  121477                 :   Fts3MultiSegReader csr;        /* Must be right after "base" */
  121478                 :   Fts3SegFilter filter;
  121479                 :   char *zStop;
  121480                 :   int nStop;                      /* Byte-length of string zStop */
  121481                 :   int isEof;                      /* True if cursor is at EOF */
  121482                 :   sqlite3_int64 iRowid;           /* Current rowid */
  121483                 : 
  121484                 :   int iCol;                       /* Current value of 'col' column */
  121485                 :   int nStat;                      /* Size of aStat[] array */
  121486                 :   struct Fts3auxColstats {
  121487                 :     sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
  121488                 :     sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
  121489                 :   } *aStat;
  121490                 : };
  121491                 : 
  121492                 : /*
  121493                 : ** Schema of the terms table.
  121494                 : */
  121495                 : #define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)"
  121496                 : 
  121497                 : /*
  121498                 : ** This function does all the work for both the xConnect and xCreate methods.
  121499                 : ** These tables have no persistent representation of their own, so xConnect
  121500                 : ** and xCreate are identical operations.
  121501                 : */
  121502               0 : static int fts3auxConnectMethod(
  121503                 :   sqlite3 *db,                    /* Database connection */
  121504                 :   void *pUnused,                  /* Unused */
  121505                 :   int argc,                       /* Number of elements in argv array */
  121506                 :   const char * const *argv,       /* xCreate/xConnect argument array */
  121507                 :   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
  121508                 :   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
  121509                 : ){
  121510                 :   char const *zDb;                /* Name of database (e.g. "main") */
  121511                 :   char const *zFts3;              /* Name of fts3 table */
  121512                 :   int nDb;                        /* Result of strlen(zDb) */
  121513                 :   int nFts3;                      /* Result of strlen(zFts3) */
  121514                 :   int nByte;                      /* Bytes of space to allocate here */
  121515                 :   int rc;                         /* value returned by declare_vtab() */
  121516                 :   Fts3auxTable *p;                /* Virtual table object to return */
  121517                 : 
  121518                 :   UNUSED_PARAMETER(pUnused);
  121519                 : 
  121520                 :   /* The user should specify a single argument - the name of an fts3 table. */
  121521               0 :   if( argc!=4 ){
  121522               0 :     *pzErr = sqlite3_mprintf(
  121523                 :         "wrong number of arguments to fts4aux constructor"
  121524                 :     );
  121525               0 :     return SQLITE_ERROR;
  121526                 :   }
  121527                 : 
  121528               0 :   zDb = argv[1]; 
  121529               0 :   nDb = strlen(zDb);
  121530               0 :   zFts3 = argv[3];
  121531               0 :   nFts3 = strlen(zFts3);
  121532                 : 
  121533               0 :   rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
  121534               0 :   if( rc!=SQLITE_OK ) return rc;
  121535                 : 
  121536               0 :   nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
  121537               0 :   p = (Fts3auxTable *)sqlite3_malloc(nByte);
  121538               0 :   if( !p ) return SQLITE_NOMEM;
  121539               0 :   memset(p, 0, nByte);
  121540                 : 
  121541               0 :   p->pFts3Tab = (Fts3Table *)&p[1];
  121542               0 :   p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
  121543               0 :   p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
  121544               0 :   p->pFts3Tab->db = db;
  121545               0 :   p->pFts3Tab->nIndex = 1;
  121546                 : 
  121547               0 :   memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
  121548               0 :   memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
  121549               0 :   sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
  121550                 : 
  121551               0 :   *ppVtab = (sqlite3_vtab *)p;
  121552               0 :   return SQLITE_OK;
  121553                 : }
  121554                 : 
  121555                 : /*
  121556                 : ** This function does the work for both the xDisconnect and xDestroy methods.
  121557                 : ** These tables have no persistent representation of their own, so xDisconnect
  121558                 : ** and xDestroy are identical operations.
  121559                 : */
  121560               0 : static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
  121561               0 :   Fts3auxTable *p = (Fts3auxTable *)pVtab;
  121562               0 :   Fts3Table *pFts3 = p->pFts3Tab;
  121563                 :   int i;
  121564                 : 
  121565                 :   /* Free any prepared statements held */
  121566               0 :   for(i=0; i<SizeofArray(pFts3->aStmt); i++){
  121567               0 :     sqlite3_finalize(pFts3->aStmt[i]);
  121568                 :   }
  121569               0 :   sqlite3_free(pFts3->zSegmentsTbl);
  121570               0 :   sqlite3_free(p);
  121571               0 :   return SQLITE_OK;
  121572                 : }
  121573                 : 
  121574                 : #define FTS4AUX_EQ_CONSTRAINT 1
  121575                 : #define FTS4AUX_GE_CONSTRAINT 2
  121576                 : #define FTS4AUX_LE_CONSTRAINT 4
  121577                 : 
  121578                 : /*
  121579                 : ** xBestIndex - Analyze a WHERE and ORDER BY clause.
  121580                 : */
  121581               0 : static int fts3auxBestIndexMethod(
  121582                 :   sqlite3_vtab *pVTab, 
  121583                 :   sqlite3_index_info *pInfo
  121584                 : ){
  121585                 :   int i;
  121586               0 :   int iEq = -1;
  121587               0 :   int iGe = -1;
  121588               0 :   int iLe = -1;
  121589                 : 
  121590                 :   UNUSED_PARAMETER(pVTab);
  121591                 : 
  121592                 :   /* This vtab delivers always results in "ORDER BY term ASC" order. */
  121593               0 :   if( pInfo->nOrderBy==1 
  121594               0 :    && pInfo->aOrderBy[0].iColumn==0 
  121595               0 :    && pInfo->aOrderBy[0].desc==0
  121596                 :   ){
  121597               0 :     pInfo->orderByConsumed = 1;
  121598                 :   }
  121599                 : 
  121600                 :   /* Search for equality and range constraints on the "term" column. */
  121601               0 :   for(i=0; i<pInfo->nConstraint; i++){
  121602               0 :     if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){
  121603               0 :       int op = pInfo->aConstraint[i].op;
  121604               0 :       if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
  121605               0 :       if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
  121606               0 :       if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
  121607               0 :       if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
  121608               0 :       if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
  121609                 :     }
  121610                 :   }
  121611                 : 
  121612               0 :   if( iEq>=0 ){
  121613               0 :     pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
  121614               0 :     pInfo->aConstraintUsage[iEq].argvIndex = 1;
  121615               0 :     pInfo->estimatedCost = 5;
  121616                 :   }else{
  121617               0 :     pInfo->idxNum = 0;
  121618               0 :     pInfo->estimatedCost = 20000;
  121619               0 :     if( iGe>=0 ){
  121620               0 :       pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
  121621               0 :       pInfo->aConstraintUsage[iGe].argvIndex = 1;
  121622               0 :       pInfo->estimatedCost /= 2;
  121623                 :     }
  121624               0 :     if( iLe>=0 ){
  121625               0 :       pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
  121626               0 :       pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0);
  121627               0 :       pInfo->estimatedCost /= 2;
  121628                 :     }
  121629                 :   }
  121630                 : 
  121631               0 :   return SQLITE_OK;
  121632                 : }
  121633                 : 
  121634                 : /*
  121635                 : ** xOpen - Open a cursor.
  121636                 : */
  121637               0 : static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
  121638                 :   Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
  121639                 : 
  121640                 :   UNUSED_PARAMETER(pVTab);
  121641                 : 
  121642               0 :   pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
  121643               0 :   if( !pCsr ) return SQLITE_NOMEM;
  121644               0 :   memset(pCsr, 0, sizeof(Fts3auxCursor));
  121645                 : 
  121646               0 :   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
  121647               0 :   return SQLITE_OK;
  121648                 : }
  121649                 : 
  121650                 : /*
  121651                 : ** xClose - Close a cursor.
  121652                 : */
  121653               0 : static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
  121654               0 :   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
  121655               0 :   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
  121656                 : 
  121657               0 :   sqlite3Fts3SegmentsClose(pFts3);
  121658               0 :   sqlite3Fts3SegReaderFinish(&pCsr->csr);
  121659               0 :   sqlite3_free((void *)pCsr->filter.zTerm);
  121660               0 :   sqlite3_free(pCsr->zStop);
  121661               0 :   sqlite3_free(pCsr->aStat);
  121662               0 :   sqlite3_free(pCsr);
  121663               0 :   return SQLITE_OK;
  121664                 : }
  121665                 : 
  121666               0 : static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
  121667               0 :   if( nSize>pCsr->nStat ){
  121668                 :     struct Fts3auxColstats *aNew;
  121669               0 :     aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, 
  121670               0 :         sizeof(struct Fts3auxColstats) * nSize
  121671                 :     );
  121672               0 :     if( aNew==0 ) return SQLITE_NOMEM;
  121673               0 :     memset(&aNew[pCsr->nStat], 0, 
  121674               0 :         sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
  121675                 :     );
  121676               0 :     pCsr->aStat = aNew;
  121677               0 :     pCsr->nStat = nSize;
  121678                 :   }
  121679               0 :   return SQLITE_OK;
  121680                 : }
  121681                 : 
  121682                 : /*
  121683                 : ** xNext - Advance the cursor to the next row, if any.
  121684                 : */
  121685               0 : static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
  121686               0 :   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
  121687               0 :   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
  121688                 :   int rc;
  121689                 : 
  121690                 :   /* Increment our pretend rowid value. */
  121691               0 :   pCsr->iRowid++;
  121692                 : 
  121693               0 :   for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
  121694               0 :     if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
  121695                 :   }
  121696                 : 
  121697               0 :   rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
  121698               0 :   if( rc==SQLITE_ROW ){
  121699               0 :     int i = 0;
  121700               0 :     int nDoclist = pCsr->csr.nDoclist;
  121701               0 :     char *aDoclist = pCsr->csr.aDoclist;
  121702                 :     int iCol;
  121703                 : 
  121704               0 :     int eState = 0;
  121705                 : 
  121706               0 :     if( pCsr->zStop ){
  121707               0 :       int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
  121708               0 :       int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
  121709               0 :       if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
  121710               0 :         pCsr->isEof = 1;
  121711               0 :         return SQLITE_OK;
  121712                 :       }
  121713                 :     }
  121714                 : 
  121715               0 :     if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
  121716               0 :     memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
  121717               0 :     iCol = 0;
  121718                 : 
  121719               0 :     while( i<nDoclist ){
  121720               0 :       sqlite3_int64 v = 0;
  121721                 : 
  121722               0 :       i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
  121723               0 :       switch( eState ){
  121724                 :         /* State 0. In this state the integer just read was a docid. */
  121725                 :         case 0:
  121726               0 :           pCsr->aStat[0].nDoc++;
  121727               0 :           eState = 1;
  121728               0 :           iCol = 0;
  121729               0 :           break;
  121730                 : 
  121731                 :         /* State 1. In this state we are expecting either a 1, indicating
  121732                 :         ** that the following integer will be a column number, or the
  121733                 :         ** start of a position list for column 0.  
  121734                 :         ** 
  121735                 :         ** The only difference between state 1 and state 2 is that if the
  121736                 :         ** integer encountered in state 1 is not 0 or 1, then we need to
  121737                 :         ** increment the column 0 "nDoc" count for this term.
  121738                 :         */
  121739                 :         case 1:
  121740               0 :           assert( iCol==0 );
  121741               0 :           if( v>1 ){
  121742               0 :             pCsr->aStat[1].nDoc++;
  121743                 :           }
  121744               0 :           eState = 2;
  121745                 :           /* fall through */
  121746                 : 
  121747                 :         case 2:
  121748               0 :           if( v==0 ){       /* 0x00. Next integer will be a docid. */
  121749               0 :             eState = 0;
  121750               0 :           }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
  121751               0 :             eState = 3;
  121752                 :           }else{            /* 2 or greater. A position. */
  121753               0 :             pCsr->aStat[iCol+1].nOcc++;
  121754               0 :             pCsr->aStat[0].nOcc++;
  121755                 :           }
  121756               0 :           break;
  121757                 : 
  121758                 :         /* State 3. The integer just read is a column number. */
  121759               0 :         default: assert( eState==3 );
  121760               0 :           iCol = (int)v;
  121761               0 :           if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
  121762               0 :           pCsr->aStat[iCol+1].nDoc++;
  121763               0 :           eState = 2;
  121764               0 :           break;
  121765                 :       }
  121766                 :     }
  121767                 : 
  121768               0 :     pCsr->iCol = 0;
  121769               0 :     rc = SQLITE_OK;
  121770                 :   }else{
  121771               0 :     pCsr->isEof = 1;
  121772                 :   }
  121773               0 :   return rc;
  121774                 : }
  121775                 : 
  121776                 : /*
  121777                 : ** xFilter - Initialize a cursor to point at the start of its data.
  121778                 : */
  121779               0 : static int fts3auxFilterMethod(
  121780                 :   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
  121781                 :   int idxNum,                     /* Strategy index */
  121782                 :   const char *idxStr,             /* Unused */
  121783                 :   int nVal,                       /* Number of elements in apVal */
  121784                 :   sqlite3_value **apVal           /* Arguments for the indexing scheme */
  121785                 : ){
  121786               0 :   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
  121787               0 :   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
  121788                 :   int rc;
  121789                 :   int isScan;
  121790                 : 
  121791                 :   UNUSED_PARAMETER(nVal);
  121792                 :   UNUSED_PARAMETER(idxStr);
  121793                 : 
  121794               0 :   assert( idxStr==0 );
  121795               0 :   assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
  121796                 :        || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
  121797                 :        || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
  121798                 :   );
  121799               0 :   isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT);
  121800                 : 
  121801                 :   /* In case this cursor is being reused, close and zero it. */
  121802                 :   testcase(pCsr->filter.zTerm);
  121803               0 :   sqlite3Fts3SegReaderFinish(&pCsr->csr);
  121804               0 :   sqlite3_free((void *)pCsr->filter.zTerm);
  121805               0 :   sqlite3_free(pCsr->aStat);
  121806               0 :   memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
  121807                 : 
  121808               0 :   pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
  121809               0 :   if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
  121810                 : 
  121811               0 :   if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){
  121812               0 :     const unsigned char *zStr = sqlite3_value_text(apVal[0]);
  121813               0 :     if( zStr ){
  121814               0 :       pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
  121815               0 :       pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
  121816               0 :       if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
  121817                 :     }
  121818                 :   }
  121819               0 :   if( idxNum&FTS4AUX_LE_CONSTRAINT ){
  121820               0 :     int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0;
  121821               0 :     pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx]));
  121822               0 :     pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]);
  121823               0 :     if( pCsr->zStop==0 ) return SQLITE_NOMEM;
  121824                 :   }
  121825                 : 
  121826               0 :   rc = sqlite3Fts3SegReaderCursor(pFts3, 0, FTS3_SEGCURSOR_ALL,
  121827                 :       pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
  121828                 :   );
  121829               0 :   if( rc==SQLITE_OK ){
  121830               0 :     rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
  121831                 :   }
  121832                 : 
  121833               0 :   if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
  121834               0 :   return rc;
  121835                 : }
  121836                 : 
  121837                 : /*
  121838                 : ** xEof - Return true if the cursor is at EOF, or false otherwise.
  121839                 : */
  121840               0 : static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
  121841               0 :   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
  121842               0 :   return pCsr->isEof;
  121843                 : }
  121844                 : 
  121845                 : /*
  121846                 : ** xColumn - Return a column value.
  121847                 : */
  121848               0 : static int fts3auxColumnMethod(
  121849                 :   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
  121850                 :   sqlite3_context *pContext,      /* Context for sqlite3_result_xxx() calls */
  121851                 :   int iCol                        /* Index of column to read value from */
  121852                 : ){
  121853               0 :   Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
  121854                 : 
  121855               0 :   assert( p->isEof==0 );
  121856               0 :   if( iCol==0 ){        /* Column "term" */
  121857               0 :     sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
  121858               0 :   }else if( iCol==1 ){  /* Column "col" */
  121859               0 :     if( p->iCol ){
  121860               0 :       sqlite3_result_int(pContext, p->iCol-1);
  121861                 :     }else{
  121862               0 :       sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC);
  121863                 :     }
  121864               0 :   }else if( iCol==2 ){  /* Column "documents" */
  121865               0 :     sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc);
  121866                 :   }else{                /* Column "occurrences" */
  121867               0 :     sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc);
  121868                 :   }
  121869                 : 
  121870               0 :   return SQLITE_OK;
  121871                 : }
  121872                 : 
  121873                 : /*
  121874                 : ** xRowid - Return the current rowid for the cursor.
  121875                 : */
  121876               0 : static int fts3auxRowidMethod(
  121877                 :   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
  121878                 :   sqlite_int64 *pRowid            /* OUT: Rowid value */
  121879                 : ){
  121880               0 :   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
  121881               0 :   *pRowid = pCsr->iRowid;
  121882               0 :   return SQLITE_OK;
  121883                 : }
  121884                 : 
  121885                 : /*
  121886                 : ** Register the fts3aux module with database connection db. Return SQLITE_OK
  121887                 : ** if successful or an error code if sqlite3_create_module() fails.
  121888                 : */
  121889            3277 : SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
  121890                 :   static const sqlite3_module fts3aux_module = {
  121891                 :      0,                           /* iVersion      */
  121892                 :      fts3auxConnectMethod,        /* xCreate       */
  121893                 :      fts3auxConnectMethod,        /* xConnect      */
  121894                 :      fts3auxBestIndexMethod,      /* xBestIndex    */
  121895                 :      fts3auxDisconnectMethod,     /* xDisconnect   */
  121896                 :      fts3auxDisconnectMethod,     /* xDestroy      */
  121897                 :      fts3auxOpenMethod,           /* xOpen         */
  121898                 :      fts3auxCloseMethod,          /* xClose        */
  121899                 :      fts3auxFilterMethod,         /* xFilter       */
  121900                 :      fts3auxNextMethod,           /* xNext         */
  121901                 :      fts3auxEofMethod,            /* xEof          */
  121902                 :      fts3auxColumnMethod,         /* xColumn       */
  121903                 :      fts3auxRowidMethod,          /* xRowid        */
  121904                 :      0,                           /* xUpdate       */
  121905                 :      0,                           /* xBegin        */
  121906                 :      0,                           /* xSync         */
  121907                 :      0,                           /* xCommit       */
  121908                 :      0,                           /* xRollback     */
  121909                 :      0,                           /* xFindFunction */
  121910                 :      0,                           /* xRename       */
  121911                 :      0,                           /* xSavepoint    */
  121912                 :      0,                           /* xRelease      */
  121913                 :      0                            /* xRollbackTo   */
  121914                 :   };
  121915                 :   int rc;                         /* Return code */
  121916                 : 
  121917            3277 :   rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
  121918            3277 :   return rc;
  121919                 : }
  121920                 : 
  121921                 : #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
  121922                 : 
  121923                 : /************** End of fts3_aux.c ********************************************/
  121924                 : /************** Begin file fts3_expr.c ***************************************/
  121925                 : /*
  121926                 : ** 2008 Nov 28
  121927                 : **
  121928                 : ** The author disclaims copyright to this source code.  In place of
  121929                 : ** a legal notice, here is a blessing:
  121930                 : **
  121931                 : **    May you do good and not evil.
  121932                 : **    May you find forgiveness for yourself and forgive others.
  121933                 : **    May you share freely, never taking more than you give.
  121934                 : **
  121935                 : ******************************************************************************
  121936                 : **
  121937                 : ** This module contains code that implements a parser for fts3 query strings
  121938                 : ** (the right-hand argument to the MATCH operator). Because the supported 
  121939                 : ** syntax is relatively simple, the whole tokenizer/parser system is
  121940                 : ** hand-coded. 
  121941                 : */
  121942                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  121943                 : 
  121944                 : /*
  121945                 : ** By default, this module parses the legacy syntax that has been 
  121946                 : ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
  121947                 : ** is defined, then it uses the new syntax. The differences between
  121948                 : ** the new and the old syntaxes are:
  121949                 : **
  121950                 : **  a) The new syntax supports parenthesis. The old does not.
  121951                 : **
  121952                 : **  b) The new syntax supports the AND and NOT operators. The old does not.
  121953                 : **
  121954                 : **  c) The old syntax supports the "-" token qualifier. This is not 
  121955                 : **     supported by the new syntax (it is replaced by the NOT operator).
  121956                 : **
  121957                 : **  d) When using the old syntax, the OR operator has a greater precedence
  121958                 : **     than an implicit AND. When using the new, both implicity and explicit
  121959                 : **     AND operators have a higher precedence than OR.
  121960                 : **
  121961                 : ** If compiled with SQLITE_TEST defined, then this module exports the
  121962                 : ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
  121963                 : ** to zero causes the module to use the old syntax. If it is set to 
  121964                 : ** non-zero the new syntax is activated. This is so both syntaxes can
  121965                 : ** be tested using a single build of testfixture.
  121966                 : **
  121967                 : ** The following describes the syntax supported by the fts3 MATCH
  121968                 : ** operator in a similar format to that used by the lemon parser
  121969                 : ** generator. This module does not use actually lemon, it uses a
  121970                 : ** custom parser.
  121971                 : **
  121972                 : **   query ::= andexpr (OR andexpr)*.
  121973                 : **
  121974                 : **   andexpr ::= notexpr (AND? notexpr)*.
  121975                 : **
  121976                 : **   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
  121977                 : **   notexpr ::= LP query RP.
  121978                 : **
  121979                 : **   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
  121980                 : **
  121981                 : **   distance_opt ::= .
  121982                 : **   distance_opt ::= / INTEGER.
  121983                 : **
  121984                 : **   phrase ::= TOKEN.
  121985                 : **   phrase ::= COLUMN:TOKEN.
  121986                 : **   phrase ::= "TOKEN TOKEN TOKEN...".
  121987                 : */
  121988                 : 
  121989                 : #ifdef SQLITE_TEST
  121990                 : SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
  121991                 : #else
  121992                 : # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS 
  121993                 : #  define sqlite3_fts3_enable_parentheses 1
  121994                 : # else
  121995                 : #  define sqlite3_fts3_enable_parentheses 0
  121996                 : # endif
  121997                 : #endif
  121998                 : 
  121999                 : /*
  122000                 : ** Default span for NEAR operators.
  122001                 : */
  122002                 : #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
  122003                 : 
  122004                 : /* #include <string.h> */
  122005                 : /* #include <assert.h> */
  122006                 : 
  122007                 : /*
  122008                 : ** isNot:
  122009                 : **   This variable is used by function getNextNode(). When getNextNode() is
  122010                 : **   called, it sets ParseContext.isNot to true if the 'next node' is a 
  122011                 : **   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
  122012                 : **   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
  122013                 : **   zero.
  122014                 : */
  122015                 : typedef struct ParseContext ParseContext;
  122016                 : struct ParseContext {
  122017                 :   sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
  122018                 :   const char **azCol;                 /* Array of column names for fts3 table */
  122019                 :   int bFts4;                          /* True to allow FTS4-only syntax */
  122020                 :   int nCol;                           /* Number of entries in azCol[] */
  122021                 :   int iDefaultCol;                    /* Default column to query */
  122022                 :   int isNot;                          /* True if getNextNode() sees a unary - */
  122023                 :   sqlite3_context *pCtx;              /* Write error message here */
  122024                 :   int nNest;                          /* Number of nested brackets */
  122025                 : };
  122026                 : 
  122027                 : /*
  122028                 : ** This function is equivalent to the standard isspace() function. 
  122029                 : **
  122030                 : ** The standard isspace() can be awkward to use safely, because although it
  122031                 : ** is defined to accept an argument of type int, its behaviour when passed
  122032                 : ** an integer that falls outside of the range of the unsigned char type
  122033                 : ** is undefined (and sometimes, "undefined" means segfault). This wrapper
  122034                 : ** is defined to accept an argument of type char, and always returns 0 for
  122035                 : ** any values that fall outside of the range of the unsigned char type (i.e.
  122036                 : ** negative values).
  122037                 : */
  122038               1 : static int fts3isspace(char c){
  122039               1 :   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
  122040                 : }
  122041                 : 
  122042                 : /*
  122043                 : ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
  122044                 : ** zero the memory before returning a pointer to it. If unsuccessful, 
  122045                 : ** return NULL.
  122046                 : */
  122047               1 : static void *fts3MallocZero(int nByte){
  122048               1 :   void *pRet = sqlite3_malloc(nByte);
  122049               1 :   if( pRet ) memset(pRet, 0, nByte);
  122050               1 :   return pRet;
  122051                 : }
  122052                 : 
  122053                 : 
  122054                 : /*
  122055                 : ** Extract the next token from buffer z (length n) using the tokenizer
  122056                 : ** and other information (column names etc.) in pParse. Create an Fts3Expr
  122057                 : ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
  122058                 : ** single token and set *ppExpr to point to it. If the end of the buffer is
  122059                 : ** reached before a token is found, set *ppExpr to zero. It is the
  122060                 : ** responsibility of the caller to eventually deallocate the allocated 
  122061                 : ** Fts3Expr structure (if any) by passing it to sqlite3_free().
  122062                 : **
  122063                 : ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
  122064                 : ** fails.
  122065                 : */
  122066               1 : static int getNextToken(
  122067                 :   ParseContext *pParse,                   /* fts3 query parse context */
  122068                 :   int iCol,                               /* Value for Fts3Phrase.iColumn */
  122069                 :   const char *z, int n,                   /* Input string */
  122070                 :   Fts3Expr **ppExpr,                      /* OUT: expression */
  122071                 :   int *pnConsumed                         /* OUT: Number of bytes consumed */
  122072                 : ){
  122073               1 :   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
  122074               1 :   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
  122075                 :   int rc;
  122076                 :   sqlite3_tokenizer_cursor *pCursor;
  122077               1 :   Fts3Expr *pRet = 0;
  122078               1 :   int nConsumed = 0;
  122079                 : 
  122080               1 :   rc = pModule->xOpen(pTokenizer, z, n, &pCursor);
  122081               1 :   if( rc==SQLITE_OK ){
  122082                 :     const char *zToken;
  122083                 :     int nToken, iStart, iEnd, iPosition;
  122084                 :     int nByte;                               /* total space to allocate */
  122085                 : 
  122086               1 :     pCursor->pTokenizer = pTokenizer;
  122087               1 :     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
  122088                 : 
  122089               1 :     if( rc==SQLITE_OK ){
  122090               1 :       nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
  122091               1 :       pRet = (Fts3Expr *)fts3MallocZero(nByte);
  122092               1 :       if( !pRet ){
  122093               0 :         rc = SQLITE_NOMEM;
  122094                 :       }else{
  122095               1 :         pRet->eType = FTSQUERY_PHRASE;
  122096               1 :         pRet->pPhrase = (Fts3Phrase *)&pRet[1];
  122097               1 :         pRet->pPhrase->nToken = 1;
  122098               1 :         pRet->pPhrase->iColumn = iCol;
  122099               1 :         pRet->pPhrase->aToken[0].n = nToken;
  122100               1 :         pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
  122101               1 :         memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
  122102                 : 
  122103               1 :         if( iEnd<n && z[iEnd]=='*' ){
  122104               0 :           pRet->pPhrase->aToken[0].isPrefix = 1;
  122105               0 :           iEnd++;
  122106                 :         }
  122107                 : 
  122108                 :         while( 1 ){
  122109               1 :           if( !sqlite3_fts3_enable_parentheses 
  122110               1 :            && iStart>0 && z[iStart-1]=='-' 
  122111                 :           ){
  122112               0 :             pParse->isNot = 1;
  122113               0 :             iStart--;
  122114               1 :           }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
  122115               0 :             pRet->pPhrase->aToken[0].bFirst = 1;
  122116               0 :             iStart--;
  122117                 :           }else{
  122118                 :             break;
  122119                 :           }
  122120               0 :         }
  122121                 : 
  122122                 :       }
  122123               1 :       nConsumed = iEnd;
  122124                 :     }
  122125                 : 
  122126               1 :     pModule->xClose(pCursor);
  122127                 :   }
  122128                 :   
  122129               1 :   *pnConsumed = nConsumed;
  122130               1 :   *ppExpr = pRet;
  122131               1 :   return rc;
  122132                 : }
  122133                 : 
  122134                 : 
  122135                 : /*
  122136                 : ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
  122137                 : ** then free the old allocation.
  122138                 : */
  122139               0 : static void *fts3ReallocOrFree(void *pOrig, int nNew){
  122140               0 :   void *pRet = sqlite3_realloc(pOrig, nNew);
  122141               0 :   if( !pRet ){
  122142               0 :     sqlite3_free(pOrig);
  122143                 :   }
  122144               0 :   return pRet;
  122145                 : }
  122146                 : 
  122147                 : /*
  122148                 : ** Buffer zInput, length nInput, contains the contents of a quoted string
  122149                 : ** that appeared as part of an fts3 query expression. Neither quote character
  122150                 : ** is included in the buffer. This function attempts to tokenize the entire
  122151                 : ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE 
  122152                 : ** containing the results.
  122153                 : **
  122154                 : ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
  122155                 : ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
  122156                 : ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
  122157                 : ** to 0.
  122158                 : */
  122159               0 : static int getNextString(
  122160                 :   ParseContext *pParse,                   /* fts3 query parse context */
  122161                 :   const char *zInput, int nInput,         /* Input string */
  122162                 :   Fts3Expr **ppExpr                       /* OUT: expression */
  122163                 : ){
  122164               0 :   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
  122165               0 :   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
  122166                 :   int rc;
  122167               0 :   Fts3Expr *p = 0;
  122168               0 :   sqlite3_tokenizer_cursor *pCursor = 0;
  122169               0 :   char *zTemp = 0;
  122170               0 :   int nTemp = 0;
  122171                 : 
  122172               0 :   const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
  122173               0 :   int nToken = 0;
  122174                 : 
  122175                 :   /* The final Fts3Expr data structure, including the Fts3Phrase,
  122176                 :   ** Fts3PhraseToken structures token buffers are all stored as a single 
  122177                 :   ** allocation so that the expression can be freed with a single call to
  122178                 :   ** sqlite3_free(). Setting this up requires a two pass approach.
  122179                 :   **
  122180                 :   ** The first pass, in the block below, uses a tokenizer cursor to iterate
  122181                 :   ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
  122182                 :   ** to assemble data in two dynamic buffers:
  122183                 :   **
  122184                 :   **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
  122185                 :   **             structure, followed by the array of Fts3PhraseToken 
  122186                 :   **             structures. This pass only populates the Fts3PhraseToken array.
  122187                 :   **
  122188                 :   **   Buffer zTemp: Contains copies of all tokens.
  122189                 :   **
  122190                 :   ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
  122191                 :   ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
  122192                 :   ** structures.
  122193                 :   */
  122194               0 :   rc = pModule->xOpen(pTokenizer, zInput, nInput, &pCursor);
  122195               0 :   if( rc==SQLITE_OK ){
  122196                 :     int ii;
  122197               0 :     pCursor->pTokenizer = pTokenizer;
  122198               0 :     for(ii=0; rc==SQLITE_OK; ii++){
  122199                 :       const char *zByte;
  122200                 :       int nByte, iBegin, iEnd, iPos;
  122201               0 :       rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
  122202               0 :       if( rc==SQLITE_OK ){
  122203                 :         Fts3PhraseToken *pToken;
  122204                 : 
  122205               0 :         p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
  122206               0 :         if( !p ) goto no_mem;
  122207                 : 
  122208               0 :         zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
  122209               0 :         if( !zTemp ) goto no_mem;
  122210                 : 
  122211               0 :         assert( nToken==ii );
  122212               0 :         pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
  122213               0 :         memset(pToken, 0, sizeof(Fts3PhraseToken));
  122214                 : 
  122215               0 :         memcpy(&zTemp[nTemp], zByte, nByte);
  122216               0 :         nTemp += nByte;
  122217                 : 
  122218               0 :         pToken->n = nByte;
  122219               0 :         pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
  122220               0 :         pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
  122221               0 :         nToken = ii+1;
  122222                 :       }
  122223                 :     }
  122224                 : 
  122225               0 :     pModule->xClose(pCursor);
  122226               0 :     pCursor = 0;
  122227                 :   }
  122228                 : 
  122229               0 :   if( rc==SQLITE_DONE ){
  122230                 :     int jj;
  122231               0 :     char *zBuf = 0;
  122232                 : 
  122233               0 :     p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
  122234               0 :     if( !p ) goto no_mem;
  122235               0 :     memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
  122236               0 :     p->eType = FTSQUERY_PHRASE;
  122237               0 :     p->pPhrase = (Fts3Phrase *)&p[1];
  122238               0 :     p->pPhrase->iColumn = pParse->iDefaultCol;
  122239               0 :     p->pPhrase->nToken = nToken;
  122240                 : 
  122241               0 :     zBuf = (char *)&p->pPhrase->aToken[nToken];
  122242               0 :     if( zTemp ){
  122243               0 :       memcpy(zBuf, zTemp, nTemp);
  122244               0 :       sqlite3_free(zTemp);
  122245                 :     }else{
  122246               0 :       assert( nTemp==0 );
  122247                 :     }
  122248                 : 
  122249               0 :     for(jj=0; jj<p->pPhrase->nToken; jj++){
  122250               0 :       p->pPhrase->aToken[jj].z = zBuf;
  122251               0 :       zBuf += p->pPhrase->aToken[jj].n;
  122252                 :     }
  122253               0 :     rc = SQLITE_OK;
  122254                 :   }
  122255                 : 
  122256               0 :   *ppExpr = p;
  122257               0 :   return rc;
  122258                 : no_mem:
  122259                 : 
  122260               0 :   if( pCursor ){
  122261               0 :     pModule->xClose(pCursor);
  122262                 :   }
  122263               0 :   sqlite3_free(zTemp);
  122264               0 :   sqlite3_free(p);
  122265               0 :   *ppExpr = 0;
  122266               0 :   return SQLITE_NOMEM;
  122267                 : }
  122268                 : 
  122269                 : /*
  122270                 : ** Function getNextNode(), which is called by fts3ExprParse(), may itself
  122271                 : ** call fts3ExprParse(). So this forward declaration is required.
  122272                 : */
  122273                 : static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
  122274                 : 
  122275                 : /*
  122276                 : ** The output variable *ppExpr is populated with an allocated Fts3Expr 
  122277                 : ** structure, or set to 0 if the end of the input buffer is reached.
  122278                 : **
  122279                 : ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
  122280                 : ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
  122281                 : ** If SQLITE_ERROR is returned, pContext is populated with an error message.
  122282                 : */
  122283               2 : static int getNextNode(
  122284                 :   ParseContext *pParse,                   /* fts3 query parse context */
  122285                 :   const char *z, int n,                   /* Input string */
  122286                 :   Fts3Expr **ppExpr,                      /* OUT: expression */
  122287                 :   int *pnConsumed                         /* OUT: Number of bytes consumed */
  122288                 : ){
  122289                 :   static const struct Fts3Keyword {
  122290                 :     char *z;                              /* Keyword text */
  122291                 :     unsigned char n;                      /* Length of the keyword */
  122292                 :     unsigned char parenOnly;              /* Only valid in paren mode */
  122293                 :     unsigned char eType;                  /* Keyword code */
  122294                 :   } aKeyword[] = {
  122295                 :     { "OR" ,  2, 0, FTSQUERY_OR   },
  122296                 :     { "AND",  3, 1, FTSQUERY_AND  },
  122297                 :     { "NOT",  3, 1, FTSQUERY_NOT  },
  122298                 :     { "NEAR", 4, 0, FTSQUERY_NEAR }
  122299                 :   };
  122300                 :   int ii;
  122301                 :   int iCol;
  122302                 :   int iColLen;
  122303                 :   int rc;
  122304               2 :   Fts3Expr *pRet = 0;
  122305                 : 
  122306               2 :   const char *zInput = z;
  122307               2 :   int nInput = n;
  122308                 : 
  122309               2 :   pParse->isNot = 0;
  122310                 : 
  122311                 :   /* Skip over any whitespace before checking for a keyword, an open or
  122312                 :   ** close bracket, or a quoted string. 
  122313                 :   */
  122314               4 :   while( nInput>0 && fts3isspace(*zInput) ){
  122315               0 :     nInput--;
  122316               0 :     zInput++;
  122317                 :   }
  122318               2 :   if( nInput==0 ){
  122319               1 :     return SQLITE_DONE;
  122320                 :   }
  122321                 : 
  122322                 :   /* See if we are dealing with a keyword. */
  122323               5 :   for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
  122324               4 :     const struct Fts3Keyword *pKey = &aKeyword[ii];
  122325                 : 
  122326               4 :     if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
  122327               2 :       continue;
  122328                 :     }
  122329                 : 
  122330               2 :     if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
  122331               0 :       int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
  122332               0 :       int nKey = pKey->n;
  122333                 :       char cNext;
  122334                 : 
  122335                 :       /* If this is a "NEAR" keyword, check for an explicit nearness. */
  122336               0 :       if( pKey->eType==FTSQUERY_NEAR ){
  122337               0 :         assert( nKey==4 );
  122338               0 :         if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
  122339               0 :           nNear = 0;
  122340               0 :           for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
  122341               0 :             nNear = nNear * 10 + (zInput[nKey] - '0');
  122342                 :           }
  122343                 :         }
  122344                 :       }
  122345                 : 
  122346                 :       /* At this point this is probably a keyword. But for that to be true,
  122347                 :       ** the next byte must contain either whitespace, an open or close
  122348                 :       ** parenthesis, a quote character, or EOF. 
  122349                 :       */
  122350               0 :       cNext = zInput[nKey];
  122351               0 :       if( fts3isspace(cNext) 
  122352               0 :        || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
  122353                 :       ){
  122354               0 :         pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
  122355               0 :         if( !pRet ){
  122356               0 :           return SQLITE_NOMEM;
  122357                 :         }
  122358               0 :         pRet->eType = pKey->eType;
  122359               0 :         pRet->nNear = nNear;
  122360               0 :         *ppExpr = pRet;
  122361               0 :         *pnConsumed = (int)((zInput - z) + nKey);
  122362               0 :         return SQLITE_OK;
  122363                 :       }
  122364                 : 
  122365                 :       /* Turns out that wasn't a keyword after all. This happens if the
  122366                 :       ** user has supplied a token such as "ORacle". Continue.
  122367                 :       */
  122368                 :     }
  122369                 :   }
  122370                 : 
  122371                 :   /* Check for an open bracket. */
  122372                 :   if( sqlite3_fts3_enable_parentheses ){
  122373                 :     if( *zInput=='(' ){
  122374                 :       int nConsumed;
  122375                 :       pParse->nNest++;
  122376                 :       rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed);
  122377                 :       if( rc==SQLITE_OK && !*ppExpr ){
  122378                 :         rc = SQLITE_DONE;
  122379                 :       }
  122380                 :       *pnConsumed = (int)((zInput - z) + 1 + nConsumed);
  122381                 :       return rc;
  122382                 :     }
  122383                 :   
  122384                 :     /* Check for a close bracket. */
  122385                 :     if( *zInput==')' ){
  122386                 :       pParse->nNest--;
  122387                 :       *pnConsumed = (int)((zInput - z) + 1);
  122388                 :       return SQLITE_DONE;
  122389                 :     }
  122390                 :   }
  122391                 : 
  122392                 :   /* See if we are dealing with a quoted phrase. If this is the case, then
  122393                 :   ** search for the closing quote and pass the whole string to getNextString()
  122394                 :   ** for processing. This is easy to do, as fts3 has no syntax for escaping
  122395                 :   ** a quote character embedded in a string.
  122396                 :   */
  122397               1 :   if( *zInput=='"' ){
  122398               0 :     for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
  122399               0 :     *pnConsumed = (int)((zInput - z) + ii + 1);
  122400               0 :     if( ii==nInput ){
  122401               0 :       return SQLITE_ERROR;
  122402                 :     }
  122403               0 :     return getNextString(pParse, &zInput[1], ii-1, ppExpr);
  122404                 :   }
  122405                 : 
  122406                 : 
  122407                 :   /* If control flows to this point, this must be a regular token, or 
  122408                 :   ** the end of the input. Read a regular token using the sqlite3_tokenizer
  122409                 :   ** interface. Before doing so, figure out if there is an explicit
  122410                 :   ** column specifier for the token. 
  122411                 :   **
  122412                 :   ** TODO: Strangely, it is not possible to associate a column specifier
  122413                 :   ** with a quoted phrase, only with a single token. Not sure if this was
  122414                 :   ** an implementation artifact or an intentional decision when fts3 was
  122415                 :   ** first implemented. Whichever it was, this module duplicates the 
  122416                 :   ** limitation.
  122417                 :   */
  122418               1 :   iCol = pParse->iDefaultCol;
  122419               1 :   iColLen = 0;
  122420               3 :   for(ii=0; ii<pParse->nCol; ii++){
  122421               2 :     const char *zStr = pParse->azCol[ii];
  122422               2 :     int nStr = (int)strlen(zStr);
  122423               2 :     if( nInput>nStr && zInput[nStr]==':' 
  122424               0 :      && sqlite3_strnicmp(zStr, zInput, nStr)==0 
  122425                 :     ){
  122426               0 :       iCol = ii;
  122427               0 :       iColLen = (int)((zInput - z) + nStr + 1);
  122428               0 :       break;
  122429                 :     }
  122430                 :   }
  122431               1 :   rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
  122432               1 :   *pnConsumed += iColLen;
  122433               1 :   return rc;
  122434                 : }
  122435                 : 
  122436                 : /*
  122437                 : ** The argument is an Fts3Expr structure for a binary operator (any type
  122438                 : ** except an FTSQUERY_PHRASE). Return an integer value representing the
  122439                 : ** precedence of the operator. Lower values have a higher precedence (i.e.
  122440                 : ** group more tightly). For example, in the C language, the == operator
  122441                 : ** groups more tightly than ||, and would therefore have a higher precedence.
  122442                 : **
  122443                 : ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
  122444                 : ** is defined), the order of the operators in precedence from highest to
  122445                 : ** lowest is:
  122446                 : **
  122447                 : **   NEAR
  122448                 : **   NOT
  122449                 : **   AND (including implicit ANDs)
  122450                 : **   OR
  122451                 : **
  122452                 : ** Note that when using the old query syntax, the OR operator has a higher
  122453                 : ** precedence than the AND operator.
  122454                 : */
  122455               0 : static int opPrecedence(Fts3Expr *p){
  122456               0 :   assert( p->eType!=FTSQUERY_PHRASE );
  122457                 :   if( sqlite3_fts3_enable_parentheses ){
  122458                 :     return p->eType;
  122459               0 :   }else if( p->eType==FTSQUERY_NEAR ){
  122460               0 :     return 1;
  122461               0 :   }else if( p->eType==FTSQUERY_OR ){
  122462               0 :     return 2;
  122463                 :   }
  122464               0 :   assert( p->eType==FTSQUERY_AND );
  122465               0 :   return 3;
  122466                 : }
  122467                 : 
  122468                 : /*
  122469                 : ** Argument ppHead contains a pointer to the current head of a query 
  122470                 : ** expression tree being parsed. pPrev is the expression node most recently
  122471                 : ** inserted into the tree. This function adds pNew, which is always a binary
  122472                 : ** operator node, into the expression tree based on the relative precedence
  122473                 : ** of pNew and the existing nodes of the tree. This may result in the head
  122474                 : ** of the tree changing, in which case *ppHead is set to the new root node.
  122475                 : */
  122476               0 : static void insertBinaryOperator(
  122477                 :   Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
  122478                 :   Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
  122479                 :   Fts3Expr *pNew           /* New binary node to insert into expression tree */
  122480                 : ){
  122481               0 :   Fts3Expr *pSplit = pPrev;
  122482               0 :   while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
  122483               0 :     pSplit = pSplit->pParent;
  122484                 :   }
  122485                 : 
  122486               0 :   if( pSplit->pParent ){
  122487               0 :     assert( pSplit->pParent->pRight==pSplit );
  122488               0 :     pSplit->pParent->pRight = pNew;
  122489               0 :     pNew->pParent = pSplit->pParent;
  122490                 :   }else{
  122491               0 :     *ppHead = pNew;
  122492                 :   }
  122493               0 :   pNew->pLeft = pSplit;
  122494               0 :   pSplit->pParent = pNew;
  122495               0 : }
  122496                 : 
  122497                 : /*
  122498                 : ** Parse the fts3 query expression found in buffer z, length n. This function
  122499                 : ** returns either when the end of the buffer is reached or an unmatched 
  122500                 : ** closing bracket - ')' - is encountered.
  122501                 : **
  122502                 : ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
  122503                 : ** parsed form of the expression and *pnConsumed is set to the number of
  122504                 : ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
  122505                 : ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
  122506                 : */
  122507               1 : static int fts3ExprParse(
  122508                 :   ParseContext *pParse,                   /* fts3 query parse context */
  122509                 :   const char *z, int n,                   /* Text of MATCH query */
  122510                 :   Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
  122511                 :   int *pnConsumed                         /* OUT: Number of bytes consumed */
  122512                 : ){
  122513               1 :   Fts3Expr *pRet = 0;
  122514               1 :   Fts3Expr *pPrev = 0;
  122515               1 :   Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
  122516               1 :   int nIn = n;
  122517               1 :   const char *zIn = z;
  122518               1 :   int rc = SQLITE_OK;
  122519               1 :   int isRequirePhrase = 1;
  122520                 : 
  122521               4 :   while( rc==SQLITE_OK ){
  122522               2 :     Fts3Expr *p = 0;
  122523               2 :     int nByte = 0;
  122524               2 :     rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
  122525               2 :     if( rc==SQLITE_OK ){
  122526                 :       int isPhrase;
  122527                 : 
  122528               1 :       if( !sqlite3_fts3_enable_parentheses 
  122529               2 :        && p->eType==FTSQUERY_PHRASE && pParse->isNot 
  122530               0 :       ){
  122531                 :         /* Create an implicit NOT operator. */
  122532               0 :         Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
  122533               0 :         if( !pNot ){
  122534               0 :           sqlite3Fts3ExprFree(p);
  122535               0 :           rc = SQLITE_NOMEM;
  122536               0 :           goto exprparse_out;
  122537                 :         }
  122538               0 :         pNot->eType = FTSQUERY_NOT;
  122539               0 :         pNot->pRight = p;
  122540               0 :         if( pNotBranch ){
  122541               0 :           pNot->pLeft = pNotBranch;
  122542                 :         }
  122543               0 :         pNotBranch = pNot;
  122544               0 :         p = pPrev;
  122545                 :       }else{
  122546               1 :         int eType = p->eType;
  122547               1 :         isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
  122548                 : 
  122549                 :         /* The isRequirePhrase variable is set to true if a phrase or
  122550                 :         ** an expression contained in parenthesis is required. If a
  122551                 :         ** binary operator (AND, OR, NOT or NEAR) is encounted when
  122552                 :         ** isRequirePhrase is set, this is a syntax error.
  122553                 :         */
  122554               1 :         if( !isPhrase && isRequirePhrase ){
  122555               0 :           sqlite3Fts3ExprFree(p);
  122556               0 :           rc = SQLITE_ERROR;
  122557               0 :           goto exprparse_out;
  122558                 :         }
  122559                 :   
  122560               1 :         if( isPhrase && !isRequirePhrase ){
  122561                 :           /* Insert an implicit AND operator. */
  122562                 :           Fts3Expr *pAnd;
  122563               0 :           assert( pRet && pPrev );
  122564               0 :           pAnd = fts3MallocZero(sizeof(Fts3Expr));
  122565               0 :           if( !pAnd ){
  122566               0 :             sqlite3Fts3ExprFree(p);
  122567               0 :             rc = SQLITE_NOMEM;
  122568               0 :             goto exprparse_out;
  122569                 :           }
  122570               0 :           pAnd->eType = FTSQUERY_AND;
  122571               0 :           insertBinaryOperator(&pRet, pPrev, pAnd);
  122572               0 :           pPrev = pAnd;
  122573                 :         }
  122574                 : 
  122575                 :         /* This test catches attempts to make either operand of a NEAR
  122576                 :         ** operator something other than a phrase. For example, either of
  122577                 :         ** the following:
  122578                 :         **
  122579                 :         **    (bracketed expression) NEAR phrase
  122580                 :         **    phrase NEAR (bracketed expression)
  122581                 :         **
  122582                 :         ** Return an error in either case.
  122583                 :         */
  122584               1 :         if( pPrev && (
  122585               0 :             (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
  122586               0 :          || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
  122587                 :         )){
  122588               0 :           sqlite3Fts3ExprFree(p);
  122589               0 :           rc = SQLITE_ERROR;
  122590               0 :           goto exprparse_out;
  122591                 :         }
  122592                 :   
  122593               1 :         if( isPhrase ){
  122594               1 :           if( pRet ){
  122595               0 :             assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
  122596               0 :             pPrev->pRight = p;
  122597               0 :             p->pParent = pPrev;
  122598                 :           }else{
  122599               1 :             pRet = p;
  122600                 :           }
  122601                 :         }else{
  122602               0 :           insertBinaryOperator(&pRet, pPrev, p);
  122603                 :         }
  122604               1 :         isRequirePhrase = !isPhrase;
  122605                 :       }
  122606               1 :       assert( nByte>0 );
  122607                 :     }
  122608               2 :     assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
  122609               2 :     nIn -= nByte;
  122610               2 :     zIn += nByte;
  122611               2 :     pPrev = p;
  122612                 :   }
  122613                 : 
  122614               1 :   if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
  122615               0 :     rc = SQLITE_ERROR;
  122616                 :   }
  122617                 : 
  122618               1 :   if( rc==SQLITE_DONE ){
  122619               1 :     rc = SQLITE_OK;
  122620               1 :     if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
  122621               0 :       if( !pRet ){
  122622               0 :         rc = SQLITE_ERROR;
  122623                 :       }else{
  122624               0 :         Fts3Expr *pIter = pNotBranch;
  122625               0 :         while( pIter->pLeft ){
  122626               0 :           pIter = pIter->pLeft;
  122627                 :         }
  122628               0 :         pIter->pLeft = pRet;
  122629               0 :         pRet = pNotBranch;
  122630                 :       }
  122631                 :     }
  122632                 :   }
  122633               1 :   *pnConsumed = n - nIn;
  122634                 : 
  122635                 : exprparse_out:
  122636               1 :   if( rc!=SQLITE_OK ){
  122637               0 :     sqlite3Fts3ExprFree(pRet);
  122638               0 :     sqlite3Fts3ExprFree(pNotBranch);
  122639               0 :     pRet = 0;
  122640                 :   }
  122641               1 :   *ppExpr = pRet;
  122642               1 :   return rc;
  122643                 : }
  122644                 : 
  122645                 : /*
  122646                 : ** Parameters z and n contain a pointer to and length of a buffer containing
  122647                 : ** an fts3 query expression, respectively. This function attempts to parse the
  122648                 : ** query expression and create a tree of Fts3Expr structures representing the
  122649                 : ** parsed expression. If successful, *ppExpr is set to point to the head
  122650                 : ** of the parsed expression tree and SQLITE_OK is returned. If an error
  122651                 : ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
  122652                 : ** error) is returned and *ppExpr is set to 0.
  122653                 : **
  122654                 : ** If parameter n is a negative number, then z is assumed to point to a
  122655                 : ** nul-terminated string and the length is determined using strlen().
  122656                 : **
  122657                 : ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
  122658                 : ** use to normalize query tokens while parsing the expression. The azCol[]
  122659                 : ** array, which is assumed to contain nCol entries, should contain the names
  122660                 : ** of each column in the target fts3 table, in order from left to right. 
  122661                 : ** Column names must be nul-terminated strings.
  122662                 : **
  122663                 : ** The iDefaultCol parameter should be passed the index of the table column
  122664                 : ** that appears on the left-hand-side of the MATCH operator (the default
  122665                 : ** column to match against for tokens for which a column name is not explicitly
  122666                 : ** specified as part of the query string), or -1 if tokens may by default
  122667                 : ** match any table column.
  122668                 : */
  122669               1 : SQLITE_PRIVATE int sqlite3Fts3ExprParse(
  122670                 :   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
  122671                 :   char **azCol,                       /* Array of column names for fts3 table */
  122672                 :   int bFts4,                          /* True to allow FTS4-only syntax */
  122673                 :   int nCol,                           /* Number of entries in azCol[] */
  122674                 :   int iDefaultCol,                    /* Default column to query */
  122675                 :   const char *z, int n,               /* Text of MATCH query */
  122676                 :   Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
  122677                 : ){
  122678                 :   int nParsed;
  122679                 :   int rc;
  122680                 :   ParseContext sParse;
  122681               1 :   sParse.pTokenizer = pTokenizer;
  122682               1 :   sParse.azCol = (const char **)azCol;
  122683               1 :   sParse.nCol = nCol;
  122684               1 :   sParse.iDefaultCol = iDefaultCol;
  122685               1 :   sParse.nNest = 0;
  122686               1 :   sParse.bFts4 = bFts4;
  122687               1 :   if( z==0 ){
  122688               0 :     *ppExpr = 0;
  122689               0 :     return SQLITE_OK;
  122690                 :   }
  122691               1 :   if( n<0 ){
  122692               1 :     n = (int)strlen(z);
  122693                 :   }
  122694               1 :   rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
  122695                 : 
  122696                 :   /* Check for mismatched parenthesis */
  122697               1 :   if( rc==SQLITE_OK && sParse.nNest ){
  122698               0 :     rc = SQLITE_ERROR;
  122699               0 :     sqlite3Fts3ExprFree(*ppExpr);
  122700               0 :     *ppExpr = 0;
  122701                 :   }
  122702                 : 
  122703               1 :   return rc;
  122704                 : }
  122705                 : 
  122706                 : /*
  122707                 : ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
  122708                 : */
  122709               6 : SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){
  122710               6 :   if( p ){
  122711               1 :     assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
  122712               1 :     sqlite3Fts3ExprFree(p->pLeft);
  122713               1 :     sqlite3Fts3ExprFree(p->pRight);
  122714               1 :     sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
  122715               1 :     sqlite3_free(p->aMI);
  122716               1 :     sqlite3_free(p);
  122717                 :   }
  122718               6 : }
  122719                 : 
  122720                 : /****************************************************************************
  122721                 : *****************************************************************************
  122722                 : ** Everything after this point is just test code.
  122723                 : */
  122724                 : 
  122725                 : #ifdef SQLITE_TEST
  122726                 : 
  122727                 : /* #include <stdio.h> */
  122728                 : 
  122729                 : /*
  122730                 : ** Function to query the hash-table of tokenizers (see README.tokenizers).
  122731                 : */
  122732                 : static int queryTestTokenizer(
  122733                 :   sqlite3 *db, 
  122734                 :   const char *zName,  
  122735                 :   const sqlite3_tokenizer_module **pp
  122736                 : ){
  122737                 :   int rc;
  122738                 :   sqlite3_stmt *pStmt;
  122739                 :   const char zSql[] = "SELECT fts3_tokenizer(?)";
  122740                 : 
  122741                 :   *pp = 0;
  122742                 :   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  122743                 :   if( rc!=SQLITE_OK ){
  122744                 :     return rc;
  122745                 :   }
  122746                 : 
  122747                 :   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
  122748                 :   if( SQLITE_ROW==sqlite3_step(pStmt) ){
  122749                 :     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
  122750                 :       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
  122751                 :     }
  122752                 :   }
  122753                 : 
  122754                 :   return sqlite3_finalize(pStmt);
  122755                 : }
  122756                 : 
  122757                 : /*
  122758                 : ** Return a pointer to a buffer containing a text representation of the
  122759                 : ** expression passed as the first argument. The buffer is obtained from
  122760                 : ** sqlite3_malloc(). It is the responsibility of the caller to use 
  122761                 : ** sqlite3_free() to release the memory. If an OOM condition is encountered,
  122762                 : ** NULL is returned.
  122763                 : **
  122764                 : ** If the second argument is not NULL, then its contents are prepended to 
  122765                 : ** the returned expression text and then freed using sqlite3_free().
  122766                 : */
  122767                 : static char *exprToString(Fts3Expr *pExpr, char *zBuf){
  122768                 :   switch( pExpr->eType ){
  122769                 :     case FTSQUERY_PHRASE: {
  122770                 :       Fts3Phrase *pPhrase = pExpr->pPhrase;
  122771                 :       int i;
  122772                 :       zBuf = sqlite3_mprintf(
  122773                 :           "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
  122774                 :       for(i=0; zBuf && i<pPhrase->nToken; i++){
  122775                 :         zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, 
  122776                 :             pPhrase->aToken[i].n, pPhrase->aToken[i].z,
  122777                 :             (pPhrase->aToken[i].isPrefix?"+":"")
  122778                 :         );
  122779                 :       }
  122780                 :       return zBuf;
  122781                 :     }
  122782                 : 
  122783                 :     case FTSQUERY_NEAR:
  122784                 :       zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
  122785                 :       break;
  122786                 :     case FTSQUERY_NOT:
  122787                 :       zBuf = sqlite3_mprintf("%zNOT ", zBuf);
  122788                 :       break;
  122789                 :     case FTSQUERY_AND:
  122790                 :       zBuf = sqlite3_mprintf("%zAND ", zBuf);
  122791                 :       break;
  122792                 :     case FTSQUERY_OR:
  122793                 :       zBuf = sqlite3_mprintf("%zOR ", zBuf);
  122794                 :       break;
  122795                 :   }
  122796                 : 
  122797                 :   if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
  122798                 :   if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
  122799                 :   if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
  122800                 : 
  122801                 :   if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
  122802                 :   if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
  122803                 : 
  122804                 :   return zBuf;
  122805                 : }
  122806                 : 
  122807                 : /*
  122808                 : ** This is the implementation of a scalar SQL function used to test the 
  122809                 : ** expression parser. It should be called as follows:
  122810                 : **
  122811                 : **   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
  122812                 : **
  122813                 : ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
  122814                 : ** to parse the query expression (see README.tokenizers). The second argument
  122815                 : ** is the query expression to parse. Each subsequent argument is the name
  122816                 : ** of a column of the fts3 table that the query expression may refer to.
  122817                 : ** For example:
  122818                 : **
  122819                 : **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
  122820                 : */
  122821                 : static void fts3ExprTest(
  122822                 :   sqlite3_context *context,
  122823                 :   int argc,
  122824                 :   sqlite3_value **argv
  122825                 : ){
  122826                 :   sqlite3_tokenizer_module const *pModule = 0;
  122827                 :   sqlite3_tokenizer *pTokenizer = 0;
  122828                 :   int rc;
  122829                 :   char **azCol = 0;
  122830                 :   const char *zExpr;
  122831                 :   int nExpr;
  122832                 :   int nCol;
  122833                 :   int ii;
  122834                 :   Fts3Expr *pExpr;
  122835                 :   char *zBuf = 0;
  122836                 :   sqlite3 *db = sqlite3_context_db_handle(context);
  122837                 : 
  122838                 :   if( argc<3 ){
  122839                 :     sqlite3_result_error(context, 
  122840                 :         "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
  122841                 :     );
  122842                 :     return;
  122843                 :   }
  122844                 : 
  122845                 :   rc = queryTestTokenizer(db,
  122846                 :                           (const char *)sqlite3_value_text(argv[0]), &pModule);
  122847                 :   if( rc==SQLITE_NOMEM ){
  122848                 :     sqlite3_result_error_nomem(context);
  122849                 :     goto exprtest_out;
  122850                 :   }else if( !pModule ){
  122851                 :     sqlite3_result_error(context, "No such tokenizer module", -1);
  122852                 :     goto exprtest_out;
  122853                 :   }
  122854                 : 
  122855                 :   rc = pModule->xCreate(0, 0, &pTokenizer);
  122856                 :   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
  122857                 :   if( rc==SQLITE_NOMEM ){
  122858                 :     sqlite3_result_error_nomem(context);
  122859                 :     goto exprtest_out;
  122860                 :   }
  122861                 :   pTokenizer->pModule = pModule;
  122862                 : 
  122863                 :   zExpr = (const char *)sqlite3_value_text(argv[1]);
  122864                 :   nExpr = sqlite3_value_bytes(argv[1]);
  122865                 :   nCol = argc-2;
  122866                 :   azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
  122867                 :   if( !azCol ){
  122868                 :     sqlite3_result_error_nomem(context);
  122869                 :     goto exprtest_out;
  122870                 :   }
  122871                 :   for(ii=0; ii<nCol; ii++){
  122872                 :     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
  122873                 :   }
  122874                 : 
  122875                 :   rc = sqlite3Fts3ExprParse(
  122876                 :       pTokenizer, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
  122877                 :   );
  122878                 :   if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
  122879                 :     sqlite3_result_error(context, "Error parsing expression", -1);
  122880                 :   }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
  122881                 :     sqlite3_result_error_nomem(context);
  122882                 :   }else{
  122883                 :     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
  122884                 :     sqlite3_free(zBuf);
  122885                 :   }
  122886                 : 
  122887                 :   sqlite3Fts3ExprFree(pExpr);
  122888                 : 
  122889                 : exprtest_out:
  122890                 :   if( pModule && pTokenizer ){
  122891                 :     rc = pModule->xDestroy(pTokenizer);
  122892                 :   }
  122893                 :   sqlite3_free(azCol);
  122894                 : }
  122895                 : 
  122896                 : /*
  122897                 : ** Register the query expression parser test function fts3_exprtest() 
  122898                 : ** with database connection db. 
  122899                 : */
  122900                 : SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
  122901                 :   return sqlite3_create_function(
  122902                 :       db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
  122903                 :   );
  122904                 : }
  122905                 : 
  122906                 : #endif
  122907                 : #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
  122908                 : 
  122909                 : /************** End of fts3_expr.c *******************************************/
  122910                 : /************** Begin file fts3_hash.c ***************************************/
  122911                 : /*
  122912                 : ** 2001 September 22
  122913                 : **
  122914                 : ** The author disclaims copyright to this source code.  In place of
  122915                 : ** a legal notice, here is a blessing:
  122916                 : **
  122917                 : **    May you do good and not evil.
  122918                 : **    May you find forgiveness for yourself and forgive others.
  122919                 : **    May you share freely, never taking more than you give.
  122920                 : **
  122921                 : *************************************************************************
  122922                 : ** This is the implementation of generic hash-tables used in SQLite.
  122923                 : ** We've modified it slightly to serve as a standalone hash table
  122924                 : ** implementation for the full-text indexing module.
  122925                 : */
  122926                 : 
  122927                 : /*
  122928                 : ** The code in this file is only compiled if:
  122929                 : **
  122930                 : **     * The FTS3 module is being built as an extension
  122931                 : **       (in which case SQLITE_CORE is not defined), or
  122932                 : **
  122933                 : **     * The FTS3 module is being built into the core of
  122934                 : **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  122935                 : */
  122936                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  122937                 : 
  122938                 : /* #include <assert.h> */
  122939                 : /* #include <stdlib.h> */
  122940                 : /* #include <string.h> */
  122941                 : 
  122942                 : 
  122943                 : /*
  122944                 : ** Malloc and Free functions
  122945                 : */
  122946           16429 : static void *fts3HashMalloc(int n){
  122947           16429 :   void *p = sqlite3_malloc(n);
  122948           16429 :   if( p ){
  122949           16429 :     memset(p, 0, n);
  122950                 :   }
  122951           16429 :   return p;
  122952                 : }
  122953           19711 : static void fts3HashFree(void *p){
  122954           19711 :   sqlite3_free(p);
  122955           19711 : }
  122956                 : 
  122957                 : /* Turn bulk memory into a hash table object by initializing the
  122958                 : ** fields of the Hash structure.
  122959                 : **
  122960                 : ** "pNew" is a pointer to the hash table that is to be initialized.
  122961                 : ** keyClass is one of the constants 
  122962                 : ** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass 
  122963                 : ** determines what kind of key the hash table will use.  "copyKey" is
  122964                 : ** true if the hash table should make its own private copy of keys and
  122965                 : ** false if it should just use the supplied pointer.
  122966                 : */
  122967            3278 : SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
  122968            3278 :   assert( pNew!=0 );
  122969            3278 :   assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
  122970            3278 :   pNew->keyClass = keyClass;
  122971            3278 :   pNew->copyKey = copyKey;
  122972            3278 :   pNew->first = 0;
  122973            3278 :   pNew->count = 0;
  122974            3278 :   pNew->htsize = 0;
  122975            3278 :   pNew->ht = 0;
  122976            3278 : }
  122977                 : 
  122978                 : /* Remove all entries from a hash table.  Reclaim all memory.
  122979                 : ** Call this routine to delete a hash table or to reset a hash table
  122980                 : ** to the empty state.
  122981                 : */
  122982            3282 : SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
  122983                 :   Fts3HashElem *elem;         /* For looping over all elements of the table */
  122984                 : 
  122985            3282 :   assert( pH!=0 );
  122986            3282 :   elem = pH->first;
  122987            3282 :   pH->first = 0;
  122988            3282 :   fts3HashFree(pH->ht);
  122989            3282 :   pH->ht = 0;
  122990            3282 :   pH->htsize = 0;
  122991           13138 :   while( elem ){
  122992            6574 :     Fts3HashElem *next_elem = elem->next;
  122993            6574 :     if( pH->copyKey && elem->pKey ){
  122994            6574 :       fts3HashFree(elem->pKey);
  122995                 :     }
  122996            6574 :     fts3HashFree(elem);
  122997            6574 :     elem = next_elem;
  122998                 :   }
  122999            3282 :   pH->count = 0;
  123000            3282 : }
  123001                 : 
  123002                 : /*
  123003                 : ** Hash and comparison functions when the mode is FTS3_HASH_STRING
  123004                 : */
  123005            6595 : static int fts3StrHash(const void *pKey, int nKey){
  123006            6595 :   const char *z = (const char *)pKey;
  123007            6595 :   int h = 0;
  123008            6595 :   if( nKey<=0 ) nKey = (int) strlen(z);
  123009           59307 :   while( nKey > 0  ){
  123010           46117 :     h = (h<<3) ^ h ^ *z++;
  123011           46117 :     nKey--;
  123012                 :   }
  123013            6595 :   return h & 0x7fffffff;
  123014                 : }
  123015            3303 : static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  123016            3303 :   if( n1!=n2 ) return 1;
  123017            3287 :   return strncmp((const char*)pKey1,(const char*)pKey2,n1);
  123018                 : }
  123019                 : 
  123020                 : /*
  123021                 : ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
  123022                 : */
  123023               0 : static int fts3BinHash(const void *pKey, int nKey){
  123024               0 :   int h = 0;
  123025               0 :   const char *z = (const char *)pKey;
  123026               0 :   while( nKey-- > 0 ){
  123027               0 :     h = (h<<3) ^ h ^ *(z++);
  123028                 :   }
  123029               0 :   return h & 0x7fffffff;
  123030                 : }
  123031               0 : static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
  123032               0 :   if( n1!=n2 ) return 1;
  123033               0 :   return memcmp(pKey1,pKey2,n1);
  123034                 : }
  123035                 : 
  123036                 : /*
  123037                 : ** Return a pointer to the appropriate hash function given the key class.
  123038                 : **
  123039                 : ** The C syntax in this function definition may be unfamilar to some 
  123040                 : ** programmers, so we provide the following additional explanation:
  123041                 : **
  123042                 : ** The name of the function is "ftsHashFunction".  The function takes a
  123043                 : ** single parameter "keyClass".  The return value of ftsHashFunction()
  123044                 : ** is a pointer to another function.  Specifically, the return value
  123045                 : ** of ftsHashFunction() is a pointer to a function that takes two parameters
  123046                 : ** with types "const void*" and "int" and returns an "int".
  123047                 : */
  123048            9876 : static int (*ftsHashFunction(int keyClass))(const void*,int){
  123049            9876 :   if( keyClass==FTS3_HASH_STRING ){
  123050            9876 :     return &fts3StrHash;
  123051                 :   }else{
  123052               0 :     assert( keyClass==FTS3_HASH_BINARY );
  123053               0 :     return &fts3BinHash;
  123054                 :   }
  123055                 : }
  123056                 : 
  123057                 : /*
  123058                 : ** Return a pointer to the appropriate hash function given the key class.
  123059                 : **
  123060                 : ** For help in interpreted the obscure C code in the function definition,
  123061                 : ** see the header comment on the previous function.
  123062                 : */
  123063            3314 : static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
  123064            3314 :   if( keyClass==FTS3_HASH_STRING ){
  123065            3314 :     return &fts3StrCompare;
  123066                 :   }else{
  123067               0 :     assert( keyClass==FTS3_HASH_BINARY );
  123068               0 :     return &fts3BinCompare;
  123069                 :   }
  123070                 : }
  123071                 : 
  123072                 : /* Link an element into the hash table
  123073                 : */
  123074            6574 : static void fts3HashInsertElement(
  123075                 :   Fts3Hash *pH,            /* The complete hash table */
  123076                 :   struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
  123077                 :   Fts3HashElem *pNew       /* The element to be inserted */
  123078                 : ){
  123079                 :   Fts3HashElem *pHead;     /* First element already in pEntry */
  123080            6574 :   pHead = pEntry->chain;
  123081            6574 :   if( pHead ){
  123082            3285 :     pNew->next = pHead;
  123083            3285 :     pNew->prev = pHead->prev;
  123084            3285 :     if( pHead->prev ){ pHead->prev->next = pNew; }
  123085            3282 :     else             { pH->first = pNew; }
  123086            3285 :     pHead->prev = pNew;
  123087                 :   }else{
  123088            3289 :     pNew->next = pH->first;
  123089            3289 :     if( pH->first ){ pH->first->prev = pNew; }
  123090            3289 :     pNew->prev = 0;
  123091            3289 :     pH->first = pNew;
  123092                 :   }
  123093            6574 :   pEntry->count++;
  123094            6574 :   pEntry->chain = pNew;
  123095            6574 : }
  123096                 : 
  123097                 : 
  123098                 : /* Resize the hash table so that it cantains "new_size" buckets.
  123099                 : ** "new_size" must be a power of 2.  The hash table might fail 
  123100                 : ** to resize if sqliteMalloc() fails.
  123101                 : **
  123102                 : ** Return non-zero if a memory allocation error occurs.
  123103                 : */
  123104            3281 : static int fts3Rehash(Fts3Hash *pH, int new_size){
  123105                 :   struct _fts3ht *new_ht;          /* The new hash table */
  123106                 :   Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
  123107                 :   int (*xHash)(const void*,int);   /* The hash function */
  123108                 : 
  123109            3281 :   assert( (new_size & (new_size-1))==0 );
  123110            3281 :   new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
  123111            3281 :   if( new_ht==0 ) return 1;
  123112            3281 :   fts3HashFree(pH->ht);
  123113            3281 :   pH->ht = new_ht;
  123114            3281 :   pH->htsize = new_size;
  123115            3281 :   xHash = ftsHashFunction(pH->keyClass);
  123116            3281 :   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
  123117               0 :     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
  123118               0 :     next_elem = elem->next;
  123119               0 :     fts3HashInsertElement(pH, &new_ht[h], elem);
  123120                 :   }
  123121            3281 :   return 0;
  123122                 : }
  123123                 : 
  123124                 : /* This function (for internal use only) locates an element in an
  123125                 : ** hash table that matches the given key.  The hash for this key has
  123126                 : ** already been computed and is passed as the 4th parameter.
  123127                 : */
  123128            6595 : static Fts3HashElem *fts3FindElementByHash(
  123129                 :   const Fts3Hash *pH, /* The pH to be searched */
  123130                 :   const void *pKey,   /* The key we are searching for */
  123131                 :   int nKey,
  123132                 :   int h               /* The hash for this key. */
  123133                 : ){
  123134                 :   Fts3HashElem *elem;            /* Used to loop thru the element list */
  123135                 :   int count;                     /* Number of elements left to test */
  123136                 :   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
  123137                 : 
  123138            6595 :   if( pH->ht ){
  123139            3314 :     struct _fts3ht *pEntry = &pH->ht[h];
  123140            3314 :     elem = pEntry->chain;
  123141            3314 :     count = pEntry->count;
  123142            3314 :     xCompare = ftsCompareFunction(pH->keyClass);
  123143            9926 :     while( count-- && elem ){
  123144            3303 :       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ 
  123145               5 :         return elem;
  123146                 :       }
  123147            3298 :       elem = elem->next;
  123148                 :     }
  123149                 :   }
  123150            6590 :   return 0;
  123151                 : }
  123152                 : 
  123153                 : /* Remove a single entry from the hash table given a pointer to that
  123154                 : ** element and a hash on the element's key.
  123155                 : */
  123156               0 : static void fts3RemoveElementByHash(
  123157                 :   Fts3Hash *pH,         /* The pH containing "elem" */
  123158                 :   Fts3HashElem* elem,   /* The element to be removed from the pH */
  123159                 :   int h                 /* Hash value for the element */
  123160                 : ){
  123161                 :   struct _fts3ht *pEntry;
  123162               0 :   if( elem->prev ){
  123163               0 :     elem->prev->next = elem->next; 
  123164                 :   }else{
  123165               0 :     pH->first = elem->next;
  123166                 :   }
  123167               0 :   if( elem->next ){
  123168               0 :     elem->next->prev = elem->prev;
  123169                 :   }
  123170               0 :   pEntry = &pH->ht[h];
  123171               0 :   if( pEntry->chain==elem ){
  123172               0 :     pEntry->chain = elem->next;
  123173                 :   }
  123174               0 :   pEntry->count--;
  123175               0 :   if( pEntry->count<=0 ){
  123176               0 :     pEntry->chain = 0;
  123177                 :   }
  123178               0 :   if( pH->copyKey && elem->pKey ){
  123179               0 :     fts3HashFree(elem->pKey);
  123180                 :   }
  123181               0 :   fts3HashFree( elem );
  123182               0 :   pH->count--;
  123183               0 :   if( pH->count<=0 ){
  123184               0 :     assert( pH->first==0 );
  123185               0 :     assert( pH->count==0 );
  123186               0 :     fts3HashClear(pH);
  123187                 :   }
  123188               0 : }
  123189                 : 
  123190              26 : SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
  123191                 :   const Fts3Hash *pH, 
  123192                 :   const void *pKey, 
  123193                 :   int nKey
  123194                 : ){
  123195                 :   int h;                          /* A hash on key */
  123196                 :   int (*xHash)(const void*,int);  /* The hash function */
  123197                 : 
  123198              26 :   if( pH==0 || pH->ht==0 ) return 0;
  123199              21 :   xHash = ftsHashFunction(pH->keyClass);
  123200              21 :   assert( xHash!=0 );
  123201              21 :   h = (*xHash)(pKey,nKey);
  123202              21 :   assert( (pH->htsize & (pH->htsize-1))==0 );
  123203              21 :   return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
  123204                 : }
  123205                 : 
  123206                 : /* 
  123207                 : ** Attempt to locate an element of the hash table pH with a key
  123208                 : ** that matches pKey,nKey.  Return the data for this element if it is
  123209                 : ** found, or NULL if there is no match.
  123210                 : */
  123211              25 : SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
  123212                 :   Fts3HashElem *pElem;            /* The element that matches key (if any) */
  123213                 : 
  123214              25 :   pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
  123215              25 :   return pElem ? pElem->data : 0;
  123216                 : }
  123217                 : 
  123218                 : /* Insert an element into the hash table pH.  The key is pKey,nKey
  123219                 : ** and the data is "data".
  123220                 : **
  123221                 : ** If no element exists with a matching key, then a new
  123222                 : ** element is created.  A copy of the key is made if the copyKey
  123223                 : ** flag is set.  NULL is returned.
  123224                 : **
  123225                 : ** If another element already exists with the same key, then the
  123226                 : ** new data replaces the old data and the old data is returned.
  123227                 : ** The key is not copied in this instance.  If a malloc fails, then
  123228                 : ** the new data is returned and the hash table is unchanged.
  123229                 : **
  123230                 : ** If the "data" parameter to this function is NULL, then the
  123231                 : ** element corresponding to "key" is removed from the hash table.
  123232                 : */
  123233            6574 : SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
  123234                 :   Fts3Hash *pH,        /* The hash table to insert into */
  123235                 :   const void *pKey,    /* The key */
  123236                 :   int nKey,            /* Number of bytes in the key */
  123237                 :   void *data           /* The data */
  123238                 : ){
  123239                 :   int hraw;                 /* Raw hash value of the key */
  123240                 :   int h;                    /* the hash of the key modulo hash table size */
  123241                 :   Fts3HashElem *elem;       /* Used to loop thru the element list */
  123242                 :   Fts3HashElem *new_elem;   /* New element added to the pH */
  123243                 :   int (*xHash)(const void*,int);  /* The hash function */
  123244                 : 
  123245            6574 :   assert( pH!=0 );
  123246            6574 :   xHash = ftsHashFunction(pH->keyClass);
  123247            6574 :   assert( xHash!=0 );
  123248            6574 :   hraw = (*xHash)(pKey, nKey);
  123249            6574 :   assert( (pH->htsize & (pH->htsize-1))==0 );
  123250            6574 :   h = hraw & (pH->htsize-1);
  123251            6574 :   elem = fts3FindElementByHash(pH,pKey,nKey,h);
  123252            6574 :   if( elem ){
  123253               0 :     void *old_data = elem->data;
  123254               0 :     if( data==0 ){
  123255               0 :       fts3RemoveElementByHash(pH,elem,h);
  123256                 :     }else{
  123257               0 :       elem->data = data;
  123258                 :     }
  123259               0 :     return old_data;
  123260                 :   }
  123261            6574 :   if( data==0 ) return 0;
  123262            6574 :   if( (pH->htsize==0 && fts3Rehash(pH,8))
  123263            6574 :    || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
  123264                 :   ){
  123265               0 :     pH->count = 0;
  123266               0 :     return data;
  123267                 :   }
  123268            6574 :   assert( pH->htsize>0 );
  123269            6574 :   new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
  123270            6574 :   if( new_elem==0 ) return data;
  123271            6574 :   if( pH->copyKey && pKey!=0 ){
  123272            6574 :     new_elem->pKey = fts3HashMalloc( nKey );
  123273            6574 :     if( new_elem->pKey==0 ){
  123274               0 :       fts3HashFree(new_elem);
  123275               0 :       return data;
  123276                 :     }
  123277            6574 :     memcpy((void*)new_elem->pKey, pKey, nKey);
  123278                 :   }else{
  123279               0 :     new_elem->pKey = (void*)pKey;
  123280                 :   }
  123281            6574 :   new_elem->nKey = nKey;
  123282            6574 :   pH->count++;
  123283            6574 :   assert( pH->htsize>0 );
  123284            6574 :   assert( (pH->htsize & (pH->htsize-1))==0 );
  123285            6574 :   h = hraw & (pH->htsize-1);
  123286            6574 :   fts3HashInsertElement(pH, &pH->ht[h], new_elem);
  123287            6574 :   new_elem->data = data;
  123288            6574 :   return 0;
  123289                 : }
  123290                 : 
  123291                 : #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
  123292                 : 
  123293                 : /************** End of fts3_hash.c *******************************************/
  123294                 : /************** Begin file fts3_porter.c *************************************/
  123295                 : /*
  123296                 : ** 2006 September 30
  123297                 : **
  123298                 : ** The author disclaims copyright to this source code.  In place of
  123299                 : ** a legal notice, here is a blessing:
  123300                 : **
  123301                 : **    May you do good and not evil.
  123302                 : **    May you find forgiveness for yourself and forgive others.
  123303                 : **    May you share freely, never taking more than you give.
  123304                 : **
  123305                 : *************************************************************************
  123306                 : ** Implementation of the full-text-search tokenizer that implements
  123307                 : ** a Porter stemmer.
  123308                 : */
  123309                 : 
  123310                 : /*
  123311                 : ** The code in this file is only compiled if:
  123312                 : **
  123313                 : **     * The FTS3 module is being built as an extension
  123314                 : **       (in which case SQLITE_CORE is not defined), or
  123315                 : **
  123316                 : **     * The FTS3 module is being built into the core of
  123317                 : **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  123318                 : */
  123319                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  123320                 : 
  123321                 : /* #include <assert.h> */
  123322                 : /* #include <stdlib.h> */
  123323                 : /* #include <stdio.h> */
  123324                 : /* #include <string.h> */
  123325                 : 
  123326                 : 
  123327                 : /*
  123328                 : ** Class derived from sqlite3_tokenizer
  123329                 : */
  123330                 : typedef struct porter_tokenizer {
  123331                 :   sqlite3_tokenizer base;      /* Base class */
  123332                 : } porter_tokenizer;
  123333                 : 
  123334                 : /*
  123335                 : ** Class derived from sqlite3_tokenizer_cursor
  123336                 : */
  123337                 : typedef struct porter_tokenizer_cursor {
  123338                 :   sqlite3_tokenizer_cursor base;
  123339                 :   const char *zInput;          /* input we are tokenizing */
  123340                 :   int nInput;                  /* size of the input */
  123341                 :   int iOffset;                 /* current position in zInput */
  123342                 :   int iToken;                  /* index of next token to be returned */
  123343                 :   char *zToken;                /* storage for current token */
  123344                 :   int nAllocated;              /* space allocated to zToken buffer */
  123345                 : } porter_tokenizer_cursor;
  123346                 : 
  123347                 : 
  123348                 : /*
  123349                 : ** Create a new tokenizer instance.
  123350                 : */
  123351               0 : static int porterCreate(
  123352                 :   int argc, const char * const *argv,
  123353                 :   sqlite3_tokenizer **ppTokenizer
  123354                 : ){
  123355                 :   porter_tokenizer *t;
  123356                 : 
  123357                 :   UNUSED_PARAMETER(argc);
  123358                 :   UNUSED_PARAMETER(argv);
  123359                 : 
  123360               0 :   t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
  123361               0 :   if( t==NULL ) return SQLITE_NOMEM;
  123362               0 :   memset(t, 0, sizeof(*t));
  123363               0 :   *ppTokenizer = &t->base;
  123364               0 :   return SQLITE_OK;
  123365                 : }
  123366                 : 
  123367                 : /*
  123368                 : ** Destroy a tokenizer
  123369                 : */
  123370               0 : static int porterDestroy(sqlite3_tokenizer *pTokenizer){
  123371               0 :   sqlite3_free(pTokenizer);
  123372               0 :   return SQLITE_OK;
  123373                 : }
  123374                 : 
  123375                 : /*
  123376                 : ** Prepare to begin tokenizing a particular string.  The input
  123377                 : ** string to be tokenized is zInput[0..nInput-1].  A cursor
  123378                 : ** used to incrementally tokenize this string is returned in 
  123379                 : ** *ppCursor.
  123380                 : */
  123381               0 : static int porterOpen(
  123382                 :   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
  123383                 :   const char *zInput, int nInput,        /* String to be tokenized */
  123384                 :   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
  123385                 : ){
  123386                 :   porter_tokenizer_cursor *c;
  123387                 : 
  123388                 :   UNUSED_PARAMETER(pTokenizer);
  123389                 : 
  123390               0 :   c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
  123391               0 :   if( c==NULL ) return SQLITE_NOMEM;
  123392                 : 
  123393               0 :   c->zInput = zInput;
  123394               0 :   if( zInput==0 ){
  123395               0 :     c->nInput = 0;
  123396               0 :   }else if( nInput<0 ){
  123397               0 :     c->nInput = (int)strlen(zInput);
  123398                 :   }else{
  123399               0 :     c->nInput = nInput;
  123400                 :   }
  123401               0 :   c->iOffset = 0;                 /* start tokenizing at the beginning */
  123402               0 :   c->iToken = 0;
  123403               0 :   c->zToken = NULL;               /* no space allocated, yet. */
  123404               0 :   c->nAllocated = 0;
  123405                 : 
  123406               0 :   *ppCursor = &c->base;
  123407               0 :   return SQLITE_OK;
  123408                 : }
  123409                 : 
  123410                 : /*
  123411                 : ** Close a tokenization cursor previously opened by a call to
  123412                 : ** porterOpen() above.
  123413                 : */
  123414               0 : static int porterClose(sqlite3_tokenizer_cursor *pCursor){
  123415               0 :   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
  123416               0 :   sqlite3_free(c->zToken);
  123417               0 :   sqlite3_free(c);
  123418               0 :   return SQLITE_OK;
  123419                 : }
  123420                 : /*
  123421                 : ** Vowel or consonant
  123422                 : */
  123423                 : static const char cType[] = {
  123424                 :    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
  123425                 :    1, 1, 1, 2, 1
  123426                 : };
  123427                 : 
  123428                 : /*
  123429                 : ** isConsonant() and isVowel() determine if their first character in
  123430                 : ** the string they point to is a consonant or a vowel, according
  123431                 : ** to Porter ruls.  
  123432                 : **
  123433                 : ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
  123434                 : ** 'Y' is a consonant unless it follows another consonant,
  123435                 : ** in which case it is a vowel.
  123436                 : **
  123437                 : ** In these routine, the letters are in reverse order.  So the 'y' rule
  123438                 : ** is that 'y' is a consonant unless it is followed by another
  123439                 : ** consonent.
  123440                 : */
  123441                 : static int isVowel(const char*);
  123442               0 : static int isConsonant(const char *z){
  123443                 :   int j;
  123444               0 :   char x = *z;
  123445               0 :   if( x==0 ) return 0;
  123446               0 :   assert( x>='a' && x<='z' );
  123447               0 :   j = cType[x-'a'];
  123448               0 :   if( j<2 ) return j;
  123449               0 :   return z[1]==0 || isVowel(z + 1);
  123450                 : }
  123451               0 : static int isVowel(const char *z){
  123452                 :   int j;
  123453               0 :   char x = *z;
  123454               0 :   if( x==0 ) return 0;
  123455               0 :   assert( x>='a' && x<='z' );
  123456               0 :   j = cType[x-'a'];
  123457               0 :   if( j<2 ) return 1-j;
  123458               0 :   return isConsonant(z + 1);
  123459                 : }
  123460                 : 
  123461                 : /*
  123462                 : ** Let any sequence of one or more vowels be represented by V and let
  123463                 : ** C be sequence of one or more consonants.  Then every word can be
  123464                 : ** represented as:
  123465                 : **
  123466                 : **           [C] (VC){m} [V]
  123467                 : **
  123468                 : ** In prose:  A word is an optional consonant followed by zero or
  123469                 : ** vowel-consonant pairs followed by an optional vowel.  "m" is the
  123470                 : ** number of vowel consonant pairs.  This routine computes the value
  123471                 : ** of m for the first i bytes of a word.
  123472                 : **
  123473                 : ** Return true if the m-value for z is 1 or more.  In other words,
  123474                 : ** return true if z contains at least one vowel that is followed
  123475                 : ** by a consonant.
  123476                 : **
  123477                 : ** In this routine z[] is in reverse order.  So we are really looking
  123478                 : ** for an instance of of a consonant followed by a vowel.
  123479                 : */
  123480               0 : static int m_gt_0(const char *z){
  123481               0 :   while( isVowel(z) ){ z++; }
  123482               0 :   if( *z==0 ) return 0;
  123483               0 :   while( isConsonant(z) ){ z++; }
  123484               0 :   return *z!=0;
  123485                 : }
  123486                 : 
  123487                 : /* Like mgt0 above except we are looking for a value of m which is
  123488                 : ** exactly 1
  123489                 : */
  123490               0 : static int m_eq_1(const char *z){
  123491               0 :   while( isVowel(z) ){ z++; }
  123492               0 :   if( *z==0 ) return 0;
  123493               0 :   while( isConsonant(z) ){ z++; }
  123494               0 :   if( *z==0 ) return 0;
  123495               0 :   while( isVowel(z) ){ z++; }
  123496               0 :   if( *z==0 ) return 1;
  123497               0 :   while( isConsonant(z) ){ z++; }
  123498               0 :   return *z==0;
  123499                 : }
  123500                 : 
  123501                 : /* Like mgt0 above except we are looking for a value of m>1 instead
  123502                 : ** or m>0
  123503                 : */
  123504               0 : static int m_gt_1(const char *z){
  123505               0 :   while( isVowel(z) ){ z++; }
  123506               0 :   if( *z==0 ) return 0;
  123507               0 :   while( isConsonant(z) ){ z++; }
  123508               0 :   if( *z==0 ) return 0;
  123509               0 :   while( isVowel(z) ){ z++; }
  123510               0 :   if( *z==0 ) return 0;
  123511               0 :   while( isConsonant(z) ){ z++; }
  123512               0 :   return *z!=0;
  123513                 : }
  123514                 : 
  123515                 : /*
  123516                 : ** Return TRUE if there is a vowel anywhere within z[0..n-1]
  123517                 : */
  123518               0 : static int hasVowel(const char *z){
  123519               0 :   while( isConsonant(z) ){ z++; }
  123520               0 :   return *z!=0;
  123521                 : }
  123522                 : 
  123523                 : /*
  123524                 : ** Return TRUE if the word ends in a double consonant.
  123525                 : **
  123526                 : ** The text is reversed here. So we are really looking at
  123527                 : ** the first two characters of z[].
  123528                 : */
  123529               0 : static int doubleConsonant(const char *z){
  123530               0 :   return isConsonant(z) && z[0]==z[1];
  123531                 : }
  123532                 : 
  123533                 : /*
  123534                 : ** Return TRUE if the word ends with three letters which
  123535                 : ** are consonant-vowel-consonent and where the final consonant
  123536                 : ** is not 'w', 'x', or 'y'.
  123537                 : **
  123538                 : ** The word is reversed here.  So we are really checking the
  123539                 : ** first three letters and the first one cannot be in [wxy].
  123540                 : */
  123541               0 : static int star_oh(const char *z){
  123542               0 :   return
  123543               0 :     isConsonant(z) &&
  123544               0 :     z[0]!='w' && z[0]!='x' && z[0]!='y' &&
  123545               0 :     isVowel(z+1) &&
  123546               0 :     isConsonant(z+2);
  123547                 : }
  123548                 : 
  123549                 : /*
  123550                 : ** If the word ends with zFrom and xCond() is true for the stem
  123551                 : ** of the word that preceeds the zFrom ending, then change the 
  123552                 : ** ending to zTo.
  123553                 : **
  123554                 : ** The input word *pz and zFrom are both in reverse order.  zTo
  123555                 : ** is in normal order. 
  123556                 : **
  123557                 : ** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
  123558                 : ** match.  Not that TRUE is returned even if xCond() fails and
  123559                 : ** no substitution occurs.
  123560                 : */
  123561               0 : static int stem(
  123562                 :   char **pz,             /* The word being stemmed (Reversed) */
  123563                 :   const char *zFrom,     /* If the ending matches this... (Reversed) */
  123564                 :   const char *zTo,       /* ... change the ending to this (not reversed) */
  123565                 :   int (*xCond)(const char*)   /* Condition that must be true */
  123566                 : ){
  123567               0 :   char *z = *pz;
  123568               0 :   while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
  123569               0 :   if( *zFrom!=0 ) return 0;
  123570               0 :   if( xCond && !xCond(z) ) return 1;
  123571               0 :   while( *zTo ){
  123572               0 :     *(--z) = *(zTo++);
  123573                 :   }
  123574               0 :   *pz = z;
  123575               0 :   return 1;
  123576                 : }
  123577                 : 
  123578                 : /*
  123579                 : ** This is the fallback stemmer used when the porter stemmer is
  123580                 : ** inappropriate.  The input word is copied into the output with
  123581                 : ** US-ASCII case folding.  If the input word is too long (more
  123582                 : ** than 20 bytes if it contains no digits or more than 6 bytes if
  123583                 : ** it contains digits) then word is truncated to 20 or 6 bytes
  123584                 : ** by taking 10 or 3 bytes from the beginning and end.
  123585                 : */
  123586               0 : static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
  123587                 :   int i, mx, j;
  123588               0 :   int hasDigit = 0;
  123589               0 :   for(i=0; i<nIn; i++){
  123590               0 :     char c = zIn[i];
  123591               0 :     if( c>='A' && c<='Z' ){
  123592               0 :       zOut[i] = c - 'A' + 'a';
  123593                 :     }else{
  123594               0 :       if( c>='0' && c<='9' ) hasDigit = 1;
  123595               0 :       zOut[i] = c;
  123596                 :     }
  123597                 :   }
  123598               0 :   mx = hasDigit ? 3 : 10;
  123599               0 :   if( nIn>mx*2 ){
  123600               0 :     for(j=mx, i=nIn-mx; i<nIn; i++, j++){
  123601               0 :       zOut[j] = zOut[i];
  123602                 :     }
  123603               0 :     i = j;
  123604                 :   }
  123605               0 :   zOut[i] = 0;
  123606               0 :   *pnOut = i;
  123607               0 : }
  123608                 : 
  123609                 : 
  123610                 : /*
  123611                 : ** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
  123612                 : ** zOut is at least big enough to hold nIn bytes.  Write the actual
  123613                 : ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
  123614                 : **
  123615                 : ** Any upper-case characters in the US-ASCII character set ([A-Z])
  123616                 : ** are converted to lower case.  Upper-case UTF characters are
  123617                 : ** unchanged.
  123618                 : **
  123619                 : ** Words that are longer than about 20 bytes are stemmed by retaining
  123620                 : ** a few bytes from the beginning and the end of the word.  If the
  123621                 : ** word contains digits, 3 bytes are taken from the beginning and
  123622                 : ** 3 bytes from the end.  For long words without digits, 10 bytes
  123623                 : ** are taken from each end.  US-ASCII case folding still applies.
  123624                 : ** 
  123625                 : ** If the input word contains not digits but does characters not 
  123626                 : ** in [a-zA-Z] then no stemming is attempted and this routine just 
  123627                 : ** copies the input into the input into the output with US-ASCII
  123628                 : ** case folding.
  123629                 : **
  123630                 : ** Stemming never increases the length of the word.  So there is
  123631                 : ** no chance of overflowing the zOut buffer.
  123632                 : */
  123633               0 : static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
  123634                 :   int i, j;
  123635                 :   char zReverse[28];
  123636                 :   char *z, *z2;
  123637               0 :   if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
  123638                 :     /* The word is too big or too small for the porter stemmer.
  123639                 :     ** Fallback to the copy stemmer */
  123640               0 :     copy_stemmer(zIn, nIn, zOut, pnOut);
  123641               0 :     return;
  123642                 :   }
  123643               0 :   for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
  123644               0 :     char c = zIn[i];
  123645               0 :     if( c>='A' && c<='Z' ){
  123646               0 :       zReverse[j] = c + 'a' - 'A';
  123647               0 :     }else if( c>='a' && c<='z' ){
  123648               0 :       zReverse[j] = c;
  123649                 :     }else{
  123650                 :       /* The use of a character not in [a-zA-Z] means that we fallback
  123651                 :       ** to the copy stemmer */
  123652               0 :       copy_stemmer(zIn, nIn, zOut, pnOut);
  123653               0 :       return;
  123654                 :     }
  123655                 :   }
  123656               0 :   memset(&zReverse[sizeof(zReverse)-5], 0, 5);
  123657               0 :   z = &zReverse[j+1];
  123658                 : 
  123659                 : 
  123660                 :   /* Step 1a */
  123661               0 :   if( z[0]=='s' ){
  123662               0 :     if(
  123663               0 :      !stem(&z, "sess", "ss", 0) &&
  123664               0 :      !stem(&z, "sei", "i", 0)  &&
  123665               0 :      !stem(&z, "ss", "ss", 0)
  123666                 :     ){
  123667               0 :       z++;
  123668                 :     }
  123669                 :   }
  123670                 : 
  123671                 :   /* Step 1b */  
  123672               0 :   z2 = z;
  123673               0 :   if( stem(&z, "dee", "ee", m_gt_0) ){
  123674                 :     /* Do nothing.  The work was all in the test */
  123675               0 :   }else if( 
  123676               0 :      (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
  123677               0 :       && z!=z2
  123678                 :   ){
  123679               0 :      if( stem(&z, "ta", "ate", 0) ||
  123680               0 :          stem(&z, "lb", "ble", 0) ||
  123681               0 :          stem(&z, "zi", "ize", 0) ){
  123682                 :        /* Do nothing.  The work was all in the test */
  123683               0 :      }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
  123684               0 :        z++;
  123685               0 :      }else if( m_eq_1(z) && star_oh(z) ){
  123686               0 :        *(--z) = 'e';
  123687                 :      }
  123688                 :   }
  123689                 : 
  123690                 :   /* Step 1c */
  123691               0 :   if( z[0]=='y' && hasVowel(z+1) ){
  123692               0 :     z[0] = 'i';
  123693                 :   }
  123694                 : 
  123695                 :   /* Step 2 */
  123696               0 :   switch( z[1] ){
  123697                 :    case 'a':
  123698               0 :      stem(&z, "lanoita", "ate", m_gt_0) ||
  123699               0 :      stem(&z, "lanoit", "tion", m_gt_0);
  123700               0 :      break;
  123701                 :    case 'c':
  123702               0 :      stem(&z, "icne", "ence", m_gt_0) ||
  123703               0 :      stem(&z, "icna", "ance", m_gt_0);
  123704               0 :      break;
  123705                 :    case 'e':
  123706               0 :      stem(&z, "rezi", "ize", m_gt_0);
  123707               0 :      break;
  123708                 :    case 'g':
  123709               0 :      stem(&z, "igol", "log", m_gt_0);
  123710               0 :      break;
  123711                 :    case 'l':
  123712               0 :      stem(&z, "ilb", "ble", m_gt_0) ||
  123713               0 :      stem(&z, "illa", "al", m_gt_0) ||
  123714               0 :      stem(&z, "iltne", "ent", m_gt_0) ||
  123715               0 :      stem(&z, "ile", "e", m_gt_0) ||
  123716               0 :      stem(&z, "ilsuo", "ous", m_gt_0);
  123717               0 :      break;
  123718                 :    case 'o':
  123719               0 :      stem(&z, "noitazi", "ize", m_gt_0) ||
  123720               0 :      stem(&z, "noita", "ate", m_gt_0) ||
  123721               0 :      stem(&z, "rota", "ate", m_gt_0);
  123722               0 :      break;
  123723                 :    case 's':
  123724               0 :      stem(&z, "msila", "al", m_gt_0) ||
  123725               0 :      stem(&z, "ssenevi", "ive", m_gt_0) ||
  123726               0 :      stem(&z, "ssenluf", "ful", m_gt_0) ||
  123727               0 :      stem(&z, "ssensuo", "ous", m_gt_0);
  123728               0 :      break;
  123729                 :    case 't':
  123730               0 :      stem(&z, "itila", "al", m_gt_0) ||
  123731               0 :      stem(&z, "itivi", "ive", m_gt_0) ||
  123732               0 :      stem(&z, "itilib", "ble", m_gt_0);
  123733               0 :      break;
  123734                 :   }
  123735                 : 
  123736                 :   /* Step 3 */
  123737               0 :   switch( z[0] ){
  123738                 :    case 'e':
  123739               0 :      stem(&z, "etaci", "ic", m_gt_0) ||
  123740               0 :      stem(&z, "evita", "", m_gt_0)   ||
  123741               0 :      stem(&z, "ezila", "al", m_gt_0);
  123742               0 :      break;
  123743                 :    case 'i':
  123744               0 :      stem(&z, "itici", "ic", m_gt_0);
  123745               0 :      break;
  123746                 :    case 'l':
  123747               0 :      stem(&z, "laci", "ic", m_gt_0) ||
  123748               0 :      stem(&z, "luf", "", m_gt_0);
  123749               0 :      break;
  123750                 :    case 's':
  123751               0 :      stem(&z, "ssen", "", m_gt_0);
  123752               0 :      break;
  123753                 :   }
  123754                 : 
  123755                 :   /* Step 4 */
  123756               0 :   switch( z[1] ){
  123757                 :    case 'a':
  123758               0 :      if( z[0]=='l' && m_gt_1(z+2) ){
  123759               0 :        z += 2;
  123760                 :      }
  123761               0 :      break;
  123762                 :    case 'c':
  123763               0 :      if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
  123764               0 :        z += 4;
  123765                 :      }
  123766               0 :      break;
  123767                 :    case 'e':
  123768               0 :      if( z[0]=='r' && m_gt_1(z+2) ){
  123769               0 :        z += 2;
  123770                 :      }
  123771               0 :      break;
  123772                 :    case 'i':
  123773               0 :      if( z[0]=='c' && m_gt_1(z+2) ){
  123774               0 :        z += 2;
  123775                 :      }
  123776               0 :      break;
  123777                 :    case 'l':
  123778               0 :      if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
  123779               0 :        z += 4;
  123780                 :      }
  123781               0 :      break;
  123782                 :    case 'n':
  123783               0 :      if( z[0]=='t' ){
  123784               0 :        if( z[2]=='a' ){
  123785               0 :          if( m_gt_1(z+3) ){
  123786               0 :            z += 3;
  123787                 :          }
  123788               0 :        }else if( z[2]=='e' ){
  123789               0 :          stem(&z, "tneme", "", m_gt_1) ||
  123790               0 :          stem(&z, "tnem", "", m_gt_1) ||
  123791               0 :          stem(&z, "tne", "", m_gt_1);
  123792                 :        }
  123793                 :      }
  123794               0 :      break;
  123795                 :    case 'o':
  123796               0 :      if( z[0]=='u' ){
  123797               0 :        if( m_gt_1(z+2) ){
  123798               0 :          z += 2;
  123799                 :        }
  123800               0 :      }else if( z[3]=='s' || z[3]=='t' ){
  123801               0 :        stem(&z, "noi", "", m_gt_1);
  123802                 :      }
  123803               0 :      break;
  123804                 :    case 's':
  123805               0 :      if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
  123806               0 :        z += 3;
  123807                 :      }
  123808               0 :      break;
  123809                 :    case 't':
  123810               0 :      stem(&z, "eta", "", m_gt_1) ||
  123811               0 :      stem(&z, "iti", "", m_gt_1);
  123812               0 :      break;
  123813                 :    case 'u':
  123814               0 :      if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
  123815               0 :        z += 3;
  123816                 :      }
  123817               0 :      break;
  123818                 :    case 'v':
  123819                 :    case 'z':
  123820               0 :      if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
  123821               0 :        z += 3;
  123822                 :      }
  123823               0 :      break;
  123824                 :   }
  123825                 : 
  123826                 :   /* Step 5a */
  123827               0 :   if( z[0]=='e' ){
  123828               0 :     if( m_gt_1(z+1) ){
  123829               0 :       z++;
  123830               0 :     }else if( m_eq_1(z+1) && !star_oh(z+1) ){
  123831               0 :       z++;
  123832                 :     }
  123833                 :   }
  123834                 : 
  123835                 :   /* Step 5b */
  123836               0 :   if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
  123837               0 :     z++;
  123838                 :   }
  123839                 : 
  123840                 :   /* z[] is now the stemmed word in reverse order.  Flip it back
  123841                 :   ** around into forward order and return.
  123842                 :   */
  123843               0 :   *pnOut = i = (int)strlen(z);
  123844               0 :   zOut[i] = 0;
  123845               0 :   while( *z ){
  123846               0 :     zOut[--i] = *(z++);
  123847                 :   }
  123848                 : }
  123849                 : 
  123850                 : /*
  123851                 : ** Characters that can be part of a token.  We assume any character
  123852                 : ** whose value is greater than 0x80 (any UTF character) can be
  123853                 : ** part of a token.  In other words, delimiters all must have
  123854                 : ** values of 0x7f or lower.
  123855                 : */
  123856                 : static const char porterIdChar[] = {
  123857                 : /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
  123858                 :     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
  123859                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
  123860                 :     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
  123861                 :     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
  123862                 :     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
  123863                 : };
  123864                 : #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
  123865                 : 
  123866                 : /*
  123867                 : ** Extract the next token from a tokenization cursor.  The cursor must
  123868                 : ** have been opened by a prior call to porterOpen().
  123869                 : */
  123870               0 : static int porterNext(
  123871                 :   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
  123872                 :   const char **pzToken,               /* OUT: *pzToken is the token text */
  123873                 :   int *pnBytes,                       /* OUT: Number of bytes in token */
  123874                 :   int *piStartOffset,                 /* OUT: Starting offset of token */
  123875                 :   int *piEndOffset,                   /* OUT: Ending offset of token */
  123876                 :   int *piPosition                     /* OUT: Position integer of token */
  123877                 : ){
  123878               0 :   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
  123879               0 :   const char *z = c->zInput;
  123880                 : 
  123881               0 :   while( c->iOffset<c->nInput ){
  123882                 :     int iStartOffset, ch;
  123883                 : 
  123884                 :     /* Scan past delimiter characters */
  123885               0 :     while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
  123886               0 :       c->iOffset++;
  123887                 :     }
  123888                 : 
  123889                 :     /* Count non-delimiter characters. */
  123890               0 :     iStartOffset = c->iOffset;
  123891               0 :     while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
  123892               0 :       c->iOffset++;
  123893                 :     }
  123894                 : 
  123895               0 :     if( c->iOffset>iStartOffset ){
  123896               0 :       int n = c->iOffset-iStartOffset;
  123897               0 :       if( n>c->nAllocated ){
  123898                 :         char *pNew;
  123899               0 :         c->nAllocated = n+20;
  123900               0 :         pNew = sqlite3_realloc(c->zToken, c->nAllocated);
  123901               0 :         if( !pNew ) return SQLITE_NOMEM;
  123902               0 :         c->zToken = pNew;
  123903                 :       }
  123904               0 :       porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
  123905               0 :       *pzToken = c->zToken;
  123906               0 :       *piStartOffset = iStartOffset;
  123907               0 :       *piEndOffset = c->iOffset;
  123908               0 :       *piPosition = c->iToken++;
  123909               0 :       return SQLITE_OK;
  123910                 :     }
  123911                 :   }
  123912               0 :   return SQLITE_DONE;
  123913                 : }
  123914                 : 
  123915                 : /*
  123916                 : ** The set of routines that implement the porter-stemmer tokenizer
  123917                 : */
  123918                 : static const sqlite3_tokenizer_module porterTokenizerModule = {
  123919                 :   0,
  123920                 :   porterCreate,
  123921                 :   porterDestroy,
  123922                 :   porterOpen,
  123923                 :   porterClose,
  123924                 :   porterNext,
  123925                 : };
  123926                 : 
  123927                 : /*
  123928                 : ** Allocate a new porter tokenizer.  Return a pointer to the new
  123929                 : ** tokenizer in *ppModule
  123930                 : */
  123931            3277 : SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
  123932                 :   sqlite3_tokenizer_module const**ppModule
  123933                 : ){
  123934            3277 :   *ppModule = &porterTokenizerModule;
  123935            3277 : }
  123936                 : 
  123937                 : #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
  123938                 : 
  123939                 : /************** End of fts3_porter.c *****************************************/
  123940                 : /************** Begin file fts3_tokenizer.c **********************************/
  123941                 : /*
  123942                 : ** 2007 June 22
  123943                 : **
  123944                 : ** The author disclaims copyright to this source code.  In place of
  123945                 : ** a legal notice, here is a blessing:
  123946                 : **
  123947                 : **    May you do good and not evil.
  123948                 : **    May you find forgiveness for yourself and forgive others.
  123949                 : **    May you share freely, never taking more than you give.
  123950                 : **
  123951                 : ******************************************************************************
  123952                 : **
  123953                 : ** This is part of an SQLite module implementing full-text search.
  123954                 : ** This particular file implements the generic tokenizer interface.
  123955                 : */
  123956                 : 
  123957                 : /*
  123958                 : ** The code in this file is only compiled if:
  123959                 : **
  123960                 : **     * The FTS3 module is being built as an extension
  123961                 : **       (in which case SQLITE_CORE is not defined), or
  123962                 : **
  123963                 : **     * The FTS3 module is being built into the core of
  123964                 : **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  123965                 : */
  123966                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  123967                 : 
  123968                 : /* #include <assert.h> */
  123969                 : /* #include <string.h> */
  123970                 : 
  123971                 : /*
  123972                 : ** Implementation of the SQL scalar function for accessing the underlying 
  123973                 : ** hash table. This function may be called as follows:
  123974                 : **
  123975                 : **   SELECT <function-name>(<key-name>);
  123976                 : **   SELECT <function-name>(<key-name>, <pointer>);
  123977                 : **
  123978                 : ** where <function-name> is the name passed as the second argument
  123979                 : ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
  123980                 : **
  123981                 : ** If the <pointer> argument is specified, it must be a blob value
  123982                 : ** containing a pointer to be stored as the hash data corresponding
  123983                 : ** to the string <key-name>. If <pointer> is not specified, then
  123984                 : ** the string <key-name> must already exist in the has table. Otherwise,
  123985                 : ** an error is returned.
  123986                 : **
  123987                 : ** Whether or not the <pointer> argument is specified, the value returned
  123988                 : ** is a blob containing the pointer stored as the hash data corresponding
  123989                 : ** to string <key-name> (after the hash-table is updated, if applicable).
  123990                 : */
  123991               0 : static void scalarFunc(
  123992                 :   sqlite3_context *context,
  123993                 :   int argc,
  123994                 :   sqlite3_value **argv
  123995                 : ){
  123996                 :   Fts3Hash *pHash;
  123997               0 :   void *pPtr = 0;
  123998                 :   const unsigned char *zName;
  123999                 :   int nName;
  124000                 : 
  124001               0 :   assert( argc==1 || argc==2 );
  124002                 : 
  124003               0 :   pHash = (Fts3Hash *)sqlite3_user_data(context);
  124004                 : 
  124005               0 :   zName = sqlite3_value_text(argv[0]);
  124006               0 :   nName = sqlite3_value_bytes(argv[0])+1;
  124007                 : 
  124008               0 :   if( argc==2 ){
  124009                 :     void *pOld;
  124010               0 :     int n = sqlite3_value_bytes(argv[1]);
  124011               0 :     if( n!=sizeof(pPtr) ){
  124012               0 :       sqlite3_result_error(context, "argument type mismatch", -1);
  124013               0 :       return;
  124014                 :     }
  124015               0 :     pPtr = *(void **)sqlite3_value_blob(argv[1]);
  124016               0 :     pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
  124017               0 :     if( pOld==pPtr ){
  124018               0 :       sqlite3_result_error(context, "out of memory", -1);
  124019               0 :       return;
  124020                 :     }
  124021                 :   }else{
  124022               0 :     pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
  124023               0 :     if( !pPtr ){
  124024               0 :       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
  124025               0 :       sqlite3_result_error(context, zErr, -1);
  124026               0 :       sqlite3_free(zErr);
  124027               0 :       return;
  124028                 :     }
  124029                 :   }
  124030                 : 
  124031               0 :   sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
  124032                 : }
  124033                 : 
  124034              24 : SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
  124035                 :   static const char isFtsIdChar[] = {
  124036                 :       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
  124037                 :       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
  124038                 :       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
  124039                 :       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
  124040                 :       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
  124041                 :       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
  124042                 :       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
  124043                 :       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
  124044                 :   };
  124045              24 :   return (c&0x80 || isFtsIdChar[(int)(c)]);
  124046                 : }
  124047                 : 
  124048               3 : SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
  124049                 :   const char *z1;
  124050               3 :   const char *z2 = 0;
  124051                 : 
  124052                 :   /* Find the start of the next token. */
  124053               3 :   z1 = zStr;
  124054               9 :   while( z2==0 ){
  124055               3 :     char c = *z1;
  124056               3 :     switch( c ){
  124057               0 :       case '\0': return 0;        /* No more tokens here */
  124058                 :       case '\'':
  124059                 :       case '"':
  124060                 :       case '`': {
  124061               0 :         z2 = z1;
  124062               0 :         while( *++z2 && (*z2!=c || *++z2==c) );
  124063               0 :         break;
  124064                 :       }
  124065                 :       case '[':
  124066               0 :         z2 = &z1[1];
  124067               0 :         while( *z2 && z2[0]!=']' ) z2++;
  124068               0 :         if( *z2 ) z2++;
  124069               0 :         break;
  124070                 : 
  124071                 :       default:
  124072               3 :         if( sqlite3Fts3IsIdChar(*z1) ){
  124073               3 :           z2 = &z1[1];
  124074               3 :           while( sqlite3Fts3IsIdChar(*z2) ) z2++;
  124075                 :         }else{
  124076               0 :           z1++;
  124077                 :         }
  124078                 :     }
  124079                 :   }
  124080                 : 
  124081               3 :   *pn = (int)(z2-z1);
  124082               3 :   return z1;
  124083                 : }
  124084                 : 
  124085               1 : SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
  124086                 :   Fts3Hash *pHash,                /* Tokenizer hash table */
  124087                 :   const char *zArg,               /* Tokenizer name */
  124088                 :   sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
  124089                 :   char **pzErr                    /* OUT: Set to malloced error message */
  124090                 : ){
  124091                 :   int rc;
  124092               1 :   char *z = (char *)zArg;
  124093               1 :   int n = 0;
  124094                 :   char *zCopy;
  124095                 :   char *zEnd;                     /* Pointer to nul-term of zCopy */
  124096                 :   sqlite3_tokenizer_module *m;
  124097                 : 
  124098               1 :   zCopy = sqlite3_mprintf("%s", zArg);
  124099               1 :   if( !zCopy ) return SQLITE_NOMEM;
  124100               1 :   zEnd = &zCopy[strlen(zCopy)];
  124101                 : 
  124102               1 :   z = (char *)sqlite3Fts3NextToken(zCopy, &n);
  124103               1 :   z[n] = '\0';
  124104               1 :   sqlite3Fts3Dequote(z);
  124105                 : 
  124106               1 :   m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
  124107               1 :   if( !m ){
  124108               0 :     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
  124109               0 :     rc = SQLITE_ERROR;
  124110                 :   }else{
  124111               1 :     char const **aArg = 0;
  124112               1 :     int iArg = 0;
  124113               1 :     z = &z[n+1];
  124114               2 :     while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
  124115               0 :       int nNew = sizeof(char *)*(iArg+1);
  124116               0 :       char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
  124117               0 :       if( !aNew ){
  124118               0 :         sqlite3_free(zCopy);
  124119               0 :         sqlite3_free((void *)aArg);
  124120               0 :         return SQLITE_NOMEM;
  124121                 :       }
  124122               0 :       aArg = aNew;
  124123               0 :       aArg[iArg++] = z;
  124124               0 :       z[n] = '\0';
  124125               0 :       sqlite3Fts3Dequote(z);
  124126               0 :       z = &z[n+1];
  124127                 :     }
  124128               1 :     rc = m->xCreate(iArg, aArg, ppTok);
  124129               1 :     assert( rc!=SQLITE_OK || *ppTok );
  124130               1 :     if( rc!=SQLITE_OK ){
  124131               0 :       *pzErr = sqlite3_mprintf("unknown tokenizer");
  124132                 :     }else{
  124133               1 :       (*ppTok)->pModule = m; 
  124134                 :     }
  124135               1 :     sqlite3_free((void *)aArg);
  124136                 :   }
  124137                 : 
  124138               1 :   sqlite3_free(zCopy);
  124139               1 :   return rc;
  124140                 : }
  124141                 : 
  124142                 : 
  124143                 : #ifdef SQLITE_TEST
  124144                 : 
  124145                 : /* #include <tcl.h> */
  124146                 : /* #include <string.h> */
  124147                 : 
  124148                 : /*
  124149                 : ** Implementation of a special SQL scalar function for testing tokenizers 
  124150                 : ** designed to be used in concert with the Tcl testing framework. This
  124151                 : ** function must be called with two arguments:
  124152                 : **
  124153                 : **   SELECT <function-name>(<key-name>, <input-string>);
  124154                 : **   SELECT <function-name>(<key-name>, <pointer>);
  124155                 : **
  124156                 : ** where <function-name> is the name passed as the second argument
  124157                 : ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
  124158                 : ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
  124159                 : **
  124160                 : ** The return value is a string that may be interpreted as a Tcl
  124161                 : ** list. For each token in the <input-string>, three elements are
  124162                 : ** added to the returned list. The first is the token position, the 
  124163                 : ** second is the token text (folded, stemmed, etc.) and the third is the
  124164                 : ** substring of <input-string> associated with the token. For example, 
  124165                 : ** using the built-in "simple" tokenizer:
  124166                 : **
  124167                 : **   SELECT fts_tokenizer_test('simple', 'I don't see how');
  124168                 : **
  124169                 : ** will return the string:
  124170                 : **
  124171                 : **   "{0 i I 1 dont don't 2 see see 3 how how}"
  124172                 : **   
  124173                 : */
  124174                 : static void testFunc(
  124175                 :   sqlite3_context *context,
  124176                 :   int argc,
  124177                 :   sqlite3_value **argv
  124178                 : ){
  124179                 :   Fts3Hash *pHash;
  124180                 :   sqlite3_tokenizer_module *p;
  124181                 :   sqlite3_tokenizer *pTokenizer = 0;
  124182                 :   sqlite3_tokenizer_cursor *pCsr = 0;
  124183                 : 
  124184                 :   const char *zErr = 0;
  124185                 : 
  124186                 :   const char *zName;
  124187                 :   int nName;
  124188                 :   const char *zInput;
  124189                 :   int nInput;
  124190                 : 
  124191                 :   const char *zArg = 0;
  124192                 : 
  124193                 :   const char *zToken;
  124194                 :   int nToken;
  124195                 :   int iStart;
  124196                 :   int iEnd;
  124197                 :   int iPos;
  124198                 : 
  124199                 :   Tcl_Obj *pRet;
  124200                 : 
  124201                 :   assert( argc==2 || argc==3 );
  124202                 : 
  124203                 :   nName = sqlite3_value_bytes(argv[0]);
  124204                 :   zName = (const char *)sqlite3_value_text(argv[0]);
  124205                 :   nInput = sqlite3_value_bytes(argv[argc-1]);
  124206                 :   zInput = (const char *)sqlite3_value_text(argv[argc-1]);
  124207                 : 
  124208                 :   if( argc==3 ){
  124209                 :     zArg = (const char *)sqlite3_value_text(argv[1]);
  124210                 :   }
  124211                 : 
  124212                 :   pHash = (Fts3Hash *)sqlite3_user_data(context);
  124213                 :   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
  124214                 : 
  124215                 :   if( !p ){
  124216                 :     char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
  124217                 :     sqlite3_result_error(context, zErr, -1);
  124218                 :     sqlite3_free(zErr);
  124219                 :     return;
  124220                 :   }
  124221                 : 
  124222                 :   pRet = Tcl_NewObj();
  124223                 :   Tcl_IncrRefCount(pRet);
  124224                 : 
  124225                 :   if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){
  124226                 :     zErr = "error in xCreate()";
  124227                 :     goto finish;
  124228                 :   }
  124229                 :   pTokenizer->pModule = p;
  124230                 :   if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){
  124231                 :     zErr = "error in xOpen()";
  124232                 :     goto finish;
  124233                 :   }
  124234                 :   pCsr->pTokenizer = pTokenizer;
  124235                 : 
  124236                 :   while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
  124237                 :     Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
  124238                 :     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
  124239                 :     zToken = &zInput[iStart];
  124240                 :     nToken = iEnd-iStart;
  124241                 :     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
  124242                 :   }
  124243                 : 
  124244                 :   if( SQLITE_OK!=p->xClose(pCsr) ){
  124245                 :     zErr = "error in xClose()";
  124246                 :     goto finish;
  124247                 :   }
  124248                 :   if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
  124249                 :     zErr = "error in xDestroy()";
  124250                 :     goto finish;
  124251                 :   }
  124252                 : 
  124253                 : finish:
  124254                 :   if( zErr ){
  124255                 :     sqlite3_result_error(context, zErr, -1);
  124256                 :   }else{
  124257                 :     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
  124258                 :   }
  124259                 :   Tcl_DecrRefCount(pRet);
  124260                 : }
  124261                 : 
  124262                 : static
  124263                 : int registerTokenizer(
  124264                 :   sqlite3 *db, 
  124265                 :   char *zName, 
  124266                 :   const sqlite3_tokenizer_module *p
  124267                 : ){
  124268                 :   int rc;
  124269                 :   sqlite3_stmt *pStmt;
  124270                 :   const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
  124271                 : 
  124272                 :   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  124273                 :   if( rc!=SQLITE_OK ){
  124274                 :     return rc;
  124275                 :   }
  124276                 : 
  124277                 :   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
  124278                 :   sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
  124279                 :   sqlite3_step(pStmt);
  124280                 : 
  124281                 :   return sqlite3_finalize(pStmt);
  124282                 : }
  124283                 : 
  124284                 : static
  124285                 : int queryTokenizer(
  124286                 :   sqlite3 *db, 
  124287                 :   char *zName,  
  124288                 :   const sqlite3_tokenizer_module **pp
  124289                 : ){
  124290                 :   int rc;
  124291                 :   sqlite3_stmt *pStmt;
  124292                 :   const char zSql[] = "SELECT fts3_tokenizer(?)";
  124293                 : 
  124294                 :   *pp = 0;
  124295                 :   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  124296                 :   if( rc!=SQLITE_OK ){
  124297                 :     return rc;
  124298                 :   }
  124299                 : 
  124300                 :   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
  124301                 :   if( SQLITE_ROW==sqlite3_step(pStmt) ){
  124302                 :     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
  124303                 :       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
  124304                 :     }
  124305                 :   }
  124306                 : 
  124307                 :   return sqlite3_finalize(pStmt);
  124308                 : }
  124309                 : 
  124310                 : SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  124311                 : 
  124312                 : /*
  124313                 : ** Implementation of the scalar function fts3_tokenizer_internal_test().
  124314                 : ** This function is used for testing only, it is not included in the
  124315                 : ** build unless SQLITE_TEST is defined.
  124316                 : **
  124317                 : ** The purpose of this is to test that the fts3_tokenizer() function
  124318                 : ** can be used as designed by the C-code in the queryTokenizer and
  124319                 : ** registerTokenizer() functions above. These two functions are repeated
  124320                 : ** in the README.tokenizer file as an example, so it is important to
  124321                 : ** test them.
  124322                 : **
  124323                 : ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
  124324                 : ** function with no arguments. An assert() will fail if a problem is
  124325                 : ** detected. i.e.:
  124326                 : **
  124327                 : **     SELECT fts3_tokenizer_internal_test();
  124328                 : **
  124329                 : */
  124330                 : static void intTestFunc(
  124331                 :   sqlite3_context *context,
  124332                 :   int argc,
  124333                 :   sqlite3_value **argv
  124334                 : ){
  124335                 :   int rc;
  124336                 :   const sqlite3_tokenizer_module *p1;
  124337                 :   const sqlite3_tokenizer_module *p2;
  124338                 :   sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
  124339                 : 
  124340                 :   UNUSED_PARAMETER(argc);
  124341                 :   UNUSED_PARAMETER(argv);
  124342                 : 
  124343                 :   /* Test the query function */
  124344                 :   sqlite3Fts3SimpleTokenizerModule(&p1);
  124345                 :   rc = queryTokenizer(db, "simple", &p2);
  124346                 :   assert( rc==SQLITE_OK );
  124347                 :   assert( p1==p2 );
  124348                 :   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
  124349                 :   assert( rc==SQLITE_ERROR );
  124350                 :   assert( p2==0 );
  124351                 :   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
  124352                 : 
  124353                 :   /* Test the storage function */
  124354                 :   rc = registerTokenizer(db, "nosuchtokenizer", p1);
  124355                 :   assert( rc==SQLITE_OK );
  124356                 :   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
  124357                 :   assert( rc==SQLITE_OK );
  124358                 :   assert( p2==p1 );
  124359                 : 
  124360                 :   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
  124361                 : }
  124362                 : 
  124363                 : #endif
  124364                 : 
  124365                 : /*
  124366                 : ** Set up SQL objects in database db used to access the contents of
  124367                 : ** the hash table pointed to by argument pHash. The hash table must
  124368                 : ** been initialised to use string keys, and to take a private copy 
  124369                 : ** of the key when a value is inserted. i.e. by a call similar to:
  124370                 : **
  124371                 : **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
  124372                 : **
  124373                 : ** This function adds a scalar function (see header comment above
  124374                 : ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
  124375                 : ** defined at compilation time, a temporary virtual table (see header 
  124376                 : ** comment above struct HashTableVtab) to the database schema. Both 
  124377                 : ** provide read/write access to the contents of *pHash.
  124378                 : **
  124379                 : ** The third argument to this function, zName, is used as the name
  124380                 : ** of both the scalar and, if created, the virtual table.
  124381                 : */
  124382            3277 : SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
  124383                 :   sqlite3 *db, 
  124384                 :   Fts3Hash *pHash, 
  124385                 :   const char *zName
  124386                 : ){
  124387            3277 :   int rc = SQLITE_OK;
  124388            3277 :   void *p = (void *)pHash;
  124389            3277 :   const int any = SQLITE_ANY;
  124390                 : 
  124391                 : #ifdef SQLITE_TEST
  124392                 :   char *zTest = 0;
  124393                 :   char *zTest2 = 0;
  124394                 :   void *pdb = (void *)db;
  124395                 :   zTest = sqlite3_mprintf("%s_test", zName);
  124396                 :   zTest2 = sqlite3_mprintf("%s_internal_test", zName);
  124397                 :   if( !zTest || !zTest2 ){
  124398                 :     rc = SQLITE_NOMEM;
  124399                 :   }
  124400                 : #endif
  124401                 : 
  124402            3277 :   if( SQLITE_OK==rc ){
  124403            3277 :     rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
  124404                 :   }
  124405            3277 :   if( SQLITE_OK==rc ){
  124406            3277 :     rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
  124407                 :   }
  124408                 : #ifdef SQLITE_TEST
  124409                 :   if( SQLITE_OK==rc ){
  124410                 :     rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0);
  124411                 :   }
  124412                 :   if( SQLITE_OK==rc ){
  124413                 :     rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0);
  124414                 :   }
  124415                 :   if( SQLITE_OK==rc ){
  124416                 :     rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
  124417                 :   }
  124418                 : #endif
  124419                 : 
  124420                 : #ifdef SQLITE_TEST
  124421                 :   sqlite3_free(zTest);
  124422                 :   sqlite3_free(zTest2);
  124423                 : #endif
  124424                 : 
  124425            3277 :   return rc;
  124426                 : }
  124427                 : 
  124428                 : #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
  124429                 : 
  124430                 : /************** End of fts3_tokenizer.c **************************************/
  124431                 : /************** Begin file fts3_tokenizer1.c *********************************/
  124432                 : /*
  124433                 : ** 2006 Oct 10
  124434                 : **
  124435                 : ** The author disclaims copyright to this source code.  In place of
  124436                 : ** a legal notice, here is a blessing:
  124437                 : **
  124438                 : **    May you do good and not evil.
  124439                 : **    May you find forgiveness for yourself and forgive others.
  124440                 : **    May you share freely, never taking more than you give.
  124441                 : **
  124442                 : ******************************************************************************
  124443                 : **
  124444                 : ** Implementation of the "simple" full-text-search tokenizer.
  124445                 : */
  124446                 : 
  124447                 : /*
  124448                 : ** The code in this file is only compiled if:
  124449                 : **
  124450                 : **     * The FTS3 module is being built as an extension
  124451                 : **       (in which case SQLITE_CORE is not defined), or
  124452                 : **
  124453                 : **     * The FTS3 module is being built into the core of
  124454                 : **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  124455                 : */
  124456                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  124457                 : 
  124458                 : /* #include <assert.h> */
  124459                 : /* #include <stdlib.h> */
  124460                 : /* #include <stdio.h> */
  124461                 : /* #include <string.h> */
  124462                 : 
  124463                 : 
  124464                 : typedef struct simple_tokenizer {
  124465                 :   sqlite3_tokenizer base;
  124466                 :   char delim[128];             /* flag ASCII delimiters */
  124467                 : } simple_tokenizer;
  124468                 : 
  124469                 : typedef struct simple_tokenizer_cursor {
  124470                 :   sqlite3_tokenizer_cursor base;
  124471                 :   const char *pInput;          /* input we are tokenizing */
  124472                 :   int nBytes;                  /* size of the input */
  124473                 :   int iOffset;                 /* current position in pInput */
  124474                 :   int iToken;                  /* index of next token to be returned */
  124475                 :   char *pToken;                /* storage for current token */
  124476                 :   int nTokenAllocated;         /* space allocated to zToken buffer */
  124477                 : } simple_tokenizer_cursor;
  124478                 : 
  124479                 : 
  124480             206 : static int simpleDelim(simple_tokenizer *t, unsigned char c){
  124481             206 :   return c<0x80 && t->delim[c];
  124482                 : }
  124483             127 : static int fts3_isalnum(int x){
  124484             127 :   return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
  124485                 : }
  124486                 : 
  124487                 : /*
  124488                 : ** Create a new tokenizer instance.
  124489                 : */
  124490               1 : static int simpleCreate(
  124491                 :   int argc, const char * const *argv,
  124492                 :   sqlite3_tokenizer **ppTokenizer
  124493                 : ){
  124494                 :   simple_tokenizer *t;
  124495                 : 
  124496               1 :   t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
  124497               1 :   if( t==NULL ) return SQLITE_NOMEM;
  124498               1 :   memset(t, 0, sizeof(*t));
  124499                 : 
  124500                 :   /* TODO(shess) Delimiters need to remain the same from run to run,
  124501                 :   ** else we need to reindex.  One solution would be a meta-table to
  124502                 :   ** track such information in the database, then we'd only want this
  124503                 :   ** information on the initial create.
  124504                 :   */
  124505               1 :   if( argc>1 ){
  124506               0 :     int i, n = (int)strlen(argv[1]);
  124507               0 :     for(i=0; i<n; i++){
  124508               0 :       unsigned char ch = argv[1][i];
  124509                 :       /* We explicitly don't support UTF-8 delimiters for now. */
  124510               0 :       if( ch>=0x80 ){
  124511               0 :         sqlite3_free(t);
  124512               0 :         return SQLITE_ERROR;
  124513                 :       }
  124514               0 :       t->delim[ch] = 1;
  124515                 :     }
  124516                 :   } else {
  124517                 :     /* Mark non-alphanumeric ASCII characters as delimiters */
  124518                 :     int i;
  124519             128 :     for(i=1; i<0x80; i++){
  124520             127 :       t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
  124521                 :     }
  124522                 :   }
  124523                 : 
  124524               1 :   *ppTokenizer = &t->base;
  124525               1 :   return SQLITE_OK;
  124526                 : }
  124527                 : 
  124528                 : /*
  124529                 : ** Destroy a tokenizer
  124530                 : */
  124531               1 : static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
  124532               1 :   sqlite3_free(pTokenizer);
  124533               1 :   return SQLITE_OK;
  124534                 : }
  124535                 : 
  124536                 : /*
  124537                 : ** Prepare to begin tokenizing a particular string.  The input
  124538                 : ** string to be tokenized is pInput[0..nBytes-1].  A cursor
  124539                 : ** used to incrementally tokenize this string is returned in 
  124540                 : ** *ppCursor.
  124541                 : */
  124542               9 : static int simpleOpen(
  124543                 :   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
  124544                 :   const char *pInput, int nBytes,        /* String to be tokenized */
  124545                 :   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
  124546                 : ){
  124547                 :   simple_tokenizer_cursor *c;
  124548                 : 
  124549                 :   UNUSED_PARAMETER(pTokenizer);
  124550                 : 
  124551               9 :   c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
  124552               9 :   if( c==NULL ) return SQLITE_NOMEM;
  124553                 : 
  124554               9 :   c->pInput = pInput;
  124555               9 :   if( pInput==0 ){
  124556               0 :     c->nBytes = 0;
  124557               9 :   }else if( nBytes<0 ){
  124558               8 :     c->nBytes = (int)strlen(pInput);
  124559                 :   }else{
  124560               1 :     c->nBytes = nBytes;
  124561                 :   }
  124562               9 :   c->iOffset = 0;                 /* start tokenizing at the beginning */
  124563               9 :   c->iToken = 0;
  124564               9 :   c->pToken = NULL;               /* no space allocated, yet. */
  124565               9 :   c->nTokenAllocated = 0;
  124566                 : 
  124567               9 :   *ppCursor = &c->base;
  124568               9 :   return SQLITE_OK;
  124569                 : }
  124570                 : 
  124571                 : /*
  124572                 : ** Close a tokenization cursor previously opened by a call to
  124573                 : ** simpleOpen() above.
  124574                 : */
  124575               9 : static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
  124576               9 :   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
  124577               9 :   sqlite3_free(c->pToken);
  124578               9 :   sqlite3_free(c);
  124579               9 :   return SQLITE_OK;
  124580                 : }
  124581                 : 
  124582                 : /*
  124583                 : ** Extract the next token from a tokenization cursor.  The cursor must
  124584                 : ** have been opened by a prior call to simpleOpen().
  124585                 : */
  124586              33 : static int simpleNext(
  124587                 :   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
  124588                 :   const char **ppToken,               /* OUT: *ppToken is the token text */
  124589                 :   int *pnBytes,                       /* OUT: Number of bytes in token */
  124590                 :   int *piStartOffset,                 /* OUT: Starting offset of token */
  124591                 :   int *piEndOffset,                   /* OUT: Ending offset of token */
  124592                 :   int *piPosition                     /* OUT: Position integer of token */
  124593                 : ){
  124594              33 :   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
  124595              33 :   simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
  124596              33 :   unsigned char *p = (unsigned char *)c->pInput;
  124597                 : 
  124598              66 :   while( c->iOffset<c->nBytes ){
  124599                 :     int iStartOffset;
  124600                 : 
  124601                 :     /* Scan past delimiter characters */
  124602              66 :     while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
  124603              16 :       c->iOffset++;
  124604                 :     }
  124605                 : 
  124606                 :     /* Count non-delimiter characters. */
  124607              25 :     iStartOffset = c->iOffset;
  124608             199 :     while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
  124609             149 :       c->iOffset++;
  124610                 :     }
  124611                 : 
  124612              25 :     if( c->iOffset>iStartOffset ){
  124613              25 :       int i, n = c->iOffset-iStartOffset;
  124614              25 :       if( n>c->nTokenAllocated ){
  124615                 :         char *pNew;
  124616               9 :         c->nTokenAllocated = n+20;
  124617               9 :         pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
  124618               9 :         if( !pNew ) return SQLITE_NOMEM;
  124619               9 :         c->pToken = pNew;
  124620                 :       }
  124621             174 :       for(i=0; i<n; i++){
  124622                 :         /* TODO(shess) This needs expansion to handle UTF-8
  124623                 :         ** case-insensitivity.
  124624                 :         */
  124625             149 :         unsigned char ch = p[iStartOffset+i];
  124626             149 :         c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
  124627                 :       }
  124628              25 :       *ppToken = c->pToken;
  124629              25 :       *pnBytes = n;
  124630              25 :       *piStartOffset = iStartOffset;
  124631              25 :       *piEndOffset = c->iOffset;
  124632              25 :       *piPosition = c->iToken++;
  124633                 : 
  124634              25 :       return SQLITE_OK;
  124635                 :     }
  124636                 :   }
  124637               8 :   return SQLITE_DONE;
  124638                 : }
  124639                 : 
  124640                 : /*
  124641                 : ** The set of routines that implement the simple tokenizer
  124642                 : */
  124643                 : static const sqlite3_tokenizer_module simpleTokenizerModule = {
  124644                 :   0,
  124645                 :   simpleCreate,
  124646                 :   simpleDestroy,
  124647                 :   simpleOpen,
  124648                 :   simpleClose,
  124649                 :   simpleNext,
  124650                 : };
  124651                 : 
  124652                 : /*
  124653                 : ** Allocate a new simple tokenizer.  Return a pointer to the new
  124654                 : ** tokenizer in *ppModule
  124655                 : */
  124656            3277 : SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
  124657                 :   sqlite3_tokenizer_module const**ppModule
  124658                 : ){
  124659            3277 :   *ppModule = &simpleTokenizerModule;
  124660            3277 : }
  124661                 : 
  124662                 : #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
  124663                 : 
  124664                 : /************** End of fts3_tokenizer1.c *************************************/
  124665                 : /************** Begin file fts3_write.c **************************************/
  124666                 : /*
  124667                 : ** 2009 Oct 23
  124668                 : **
  124669                 : ** The author disclaims copyright to this source code.  In place of
  124670                 : ** a legal notice, here is a blessing:
  124671                 : **
  124672                 : **    May you do good and not evil.
  124673                 : **    May you find forgiveness for yourself and forgive others.
  124674                 : **    May you share freely, never taking more than you give.
  124675                 : **
  124676                 : ******************************************************************************
  124677                 : **
  124678                 : ** This file is part of the SQLite FTS3 extension module. Specifically,
  124679                 : ** this file contains code to insert, update and delete rows from FTS3
  124680                 : ** tables. It also contains code to merge FTS3 b-tree segments. Some
  124681                 : ** of the sub-routines used to merge segments are also used by the query 
  124682                 : ** code in fts3.c.
  124683                 : */
  124684                 : 
  124685                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  124686                 : 
  124687                 : /* #include <string.h> */
  124688                 : /* #include <assert.h> */
  124689                 : /* #include <stdlib.h> */
  124690                 : 
  124691                 : /*
  124692                 : ** When full-text index nodes are loaded from disk, the buffer that they
  124693                 : ** are loaded into has the following number of bytes of padding at the end 
  124694                 : ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
  124695                 : ** of 920 bytes is allocated for it.
  124696                 : **
  124697                 : ** This means that if we have a pointer into a buffer containing node data,
  124698                 : ** it is always safe to read up to two varints from it without risking an
  124699                 : ** overread, even if the node data is corrupted.
  124700                 : */
  124701                 : #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
  124702                 : 
  124703                 : /*
  124704                 : ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
  124705                 : ** memory incrementally instead of all at once. This can be a big performance
  124706                 : ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
  124707                 : ** method before retrieving all query results (as may happen, for example,
  124708                 : ** if a query has a LIMIT clause).
  124709                 : **
  124710                 : ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD 
  124711                 : ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
  124712                 : ** The code is written so that the hard lower-limit for each of these values 
  124713                 : ** is 1. Clearly such small values would be inefficient, but can be useful 
  124714                 : ** for testing purposes.
  124715                 : **
  124716                 : ** If this module is built with SQLITE_TEST defined, these constants may
  124717                 : ** be overridden at runtime for testing purposes. File fts3_test.c contains
  124718                 : ** a Tcl interface to read and write the values.
  124719                 : */
  124720                 : #ifdef SQLITE_TEST
  124721                 : int test_fts3_node_chunksize = (4*1024);
  124722                 : int test_fts3_node_chunk_threshold = (4*1024)*4;
  124723                 : # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
  124724                 : # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
  124725                 : #else
  124726                 : # define FTS3_NODE_CHUNKSIZE (4*1024) 
  124727                 : # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
  124728                 : #endif
  124729                 : 
  124730                 : typedef struct PendingList PendingList;
  124731                 : typedef struct SegmentNode SegmentNode;
  124732                 : typedef struct SegmentWriter SegmentWriter;
  124733                 : 
  124734                 : /*
  124735                 : ** An instance of the following data structure is used to build doclists
  124736                 : ** incrementally. See function fts3PendingListAppend() for details.
  124737                 : */
  124738                 : struct PendingList {
  124739                 :   int nData;
  124740                 :   char *aData;
  124741                 :   int nSpace;
  124742                 :   sqlite3_int64 iLastDocid;
  124743                 :   sqlite3_int64 iLastCol;
  124744                 :   sqlite3_int64 iLastPos;
  124745                 : };
  124746                 : 
  124747                 : 
  124748                 : /*
  124749                 : ** Each cursor has a (possibly empty) linked list of the following objects.
  124750                 : */
  124751                 : struct Fts3DeferredToken {
  124752                 :   Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
  124753                 :   int iCol;                       /* Column token must occur in */
  124754                 :   Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
  124755                 :   PendingList *pList;             /* Doclist is assembled here */
  124756                 : };
  124757                 : 
  124758                 : /*
  124759                 : ** An instance of this structure is used to iterate through the terms on
  124760                 : ** a contiguous set of segment b-tree leaf nodes. Although the details of
  124761                 : ** this structure are only manipulated by code in this file, opaque handles
  124762                 : ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
  124763                 : ** terms when querying the full-text index. See functions:
  124764                 : **
  124765                 : **   sqlite3Fts3SegReaderNew()
  124766                 : **   sqlite3Fts3SegReaderFree()
  124767                 : **   sqlite3Fts3SegReaderIterate()
  124768                 : **
  124769                 : ** Methods used to manipulate Fts3SegReader structures:
  124770                 : **
  124771                 : **   fts3SegReaderNext()
  124772                 : **   fts3SegReaderFirstDocid()
  124773                 : **   fts3SegReaderNextDocid()
  124774                 : */
  124775                 : struct Fts3SegReader {
  124776                 :   int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
  124777                 : 
  124778                 :   sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
  124779                 :   sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
  124780                 :   sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
  124781                 :   sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
  124782                 : 
  124783                 :   char *aNode;                    /* Pointer to node data (or NULL) */
  124784                 :   int nNode;                      /* Size of buffer at aNode (or 0) */
  124785                 :   int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
  124786                 :   sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
  124787                 : 
  124788                 :   Fts3HashElem **ppNextElem;
  124789                 : 
  124790                 :   /* Variables set by fts3SegReaderNext(). These may be read directly
  124791                 :   ** by the caller. They are valid from the time SegmentReaderNew() returns
  124792                 :   ** until SegmentReaderNext() returns something other than SQLITE_OK
  124793                 :   ** (i.e. SQLITE_DONE).
  124794                 :   */
  124795                 :   int nTerm;                      /* Number of bytes in current term */
  124796                 :   char *zTerm;                    /* Pointer to current term */
  124797                 :   int nTermAlloc;                 /* Allocated size of zTerm buffer */
  124798                 :   char *aDoclist;                 /* Pointer to doclist of current entry */
  124799                 :   int nDoclist;                   /* Size of doclist in current entry */
  124800                 : 
  124801                 :   /* The following variables are used by fts3SegReaderNextDocid() to iterate 
  124802                 :   ** through the current doclist (aDoclist/nDoclist).
  124803                 :   */
  124804                 :   char *pOffsetList;
  124805                 :   int nOffsetList;                /* For descending pending seg-readers only */
  124806                 :   sqlite3_int64 iDocid;
  124807                 : };
  124808                 : 
  124809                 : #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
  124810                 : #define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1])
  124811                 : 
  124812                 : /*
  124813                 : ** An instance of this structure is used to create a segment b-tree in the
  124814                 : ** database. The internal details of this type are only accessed by the
  124815                 : ** following functions:
  124816                 : **
  124817                 : **   fts3SegWriterAdd()
  124818                 : **   fts3SegWriterFlush()
  124819                 : **   fts3SegWriterFree()
  124820                 : */
  124821                 : struct SegmentWriter {
  124822                 :   SegmentNode *pTree;             /* Pointer to interior tree structure */
  124823                 :   sqlite3_int64 iFirst;           /* First slot in %_segments written */
  124824                 :   sqlite3_int64 iFree;            /* Next free slot in %_segments */
  124825                 :   char *zTerm;                    /* Pointer to previous term buffer */
  124826                 :   int nTerm;                      /* Number of bytes in zTerm */
  124827                 :   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
  124828                 :   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
  124829                 :   int nSize;                      /* Size of allocation at aData */
  124830                 :   int nData;                      /* Bytes of data in aData */
  124831                 :   char *aData;                    /* Pointer to block from malloc() */
  124832                 : };
  124833                 : 
  124834                 : /*
  124835                 : ** Type SegmentNode is used by the following three functions to create
  124836                 : ** the interior part of the segment b+-tree structures (everything except
  124837                 : ** the leaf nodes). These functions and type are only ever used by code
  124838                 : ** within the fts3SegWriterXXX() family of functions described above.
  124839                 : **
  124840                 : **   fts3NodeAddTerm()
  124841                 : **   fts3NodeWrite()
  124842                 : **   fts3NodeFree()
  124843                 : **
  124844                 : ** When a b+tree is written to the database (either as a result of a merge
  124845                 : ** or the pending-terms table being flushed), leaves are written into the 
  124846                 : ** database file as soon as they are completely populated. The interior of
  124847                 : ** the tree is assembled in memory and written out only once all leaves have
  124848                 : ** been populated and stored. This is Ok, as the b+-tree fanout is usually
  124849                 : ** very large, meaning that the interior of the tree consumes relatively 
  124850                 : ** little memory.
  124851                 : */
  124852                 : struct SegmentNode {
  124853                 :   SegmentNode *pParent;           /* Parent node (or NULL for root node) */
  124854                 :   SegmentNode *pRight;            /* Pointer to right-sibling */
  124855                 :   SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
  124856                 :   int nEntry;                     /* Number of terms written to node so far */
  124857                 :   char *zTerm;                    /* Pointer to previous term buffer */
  124858                 :   int nTerm;                      /* Number of bytes in zTerm */
  124859                 :   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
  124860                 :   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
  124861                 :   int nData;                      /* Bytes of valid data so far */
  124862                 :   char *aData;                    /* Node data */
  124863                 : };
  124864                 : 
  124865                 : /*
  124866                 : ** Valid values for the second argument to fts3SqlStmt().
  124867                 : */
  124868                 : #define SQL_DELETE_CONTENT             0
  124869                 : #define SQL_IS_EMPTY                   1
  124870                 : #define SQL_DELETE_ALL_CONTENT         2 
  124871                 : #define SQL_DELETE_ALL_SEGMENTS        3
  124872                 : #define SQL_DELETE_ALL_SEGDIR          4
  124873                 : #define SQL_DELETE_ALL_DOCSIZE         5
  124874                 : #define SQL_DELETE_ALL_STAT            6
  124875                 : #define SQL_SELECT_CONTENT_BY_ROWID    7
  124876                 : #define SQL_NEXT_SEGMENT_INDEX         8
  124877                 : #define SQL_INSERT_SEGMENTS            9
  124878                 : #define SQL_NEXT_SEGMENTS_ID          10
  124879                 : #define SQL_INSERT_SEGDIR             11
  124880                 : #define SQL_SELECT_LEVEL              12
  124881                 : #define SQL_SELECT_LEVEL_RANGE        13
  124882                 : #define SQL_SELECT_LEVEL_COUNT        14
  124883                 : #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
  124884                 : #define SQL_DELETE_SEGDIR_LEVEL       16
  124885                 : #define SQL_DELETE_SEGMENTS_RANGE     17
  124886                 : #define SQL_CONTENT_INSERT            18
  124887                 : #define SQL_DELETE_DOCSIZE            19
  124888                 : #define SQL_REPLACE_DOCSIZE           20
  124889                 : #define SQL_SELECT_DOCSIZE            21
  124890                 : #define SQL_SELECT_DOCTOTAL           22
  124891                 : #define SQL_REPLACE_DOCTOTAL          23
  124892                 : 
  124893                 : #define SQL_SELECT_ALL_PREFIX_LEVEL   24
  124894                 : #define SQL_DELETE_ALL_TERMS_SEGDIR   25
  124895                 : 
  124896                 : #define SQL_DELETE_SEGDIR_RANGE       26
  124897                 : 
  124898                 : /*
  124899                 : ** This function is used to obtain an SQLite prepared statement handle
  124900                 : ** for the statement identified by the second argument. If successful,
  124901                 : ** *pp is set to the requested statement handle and SQLITE_OK returned.
  124902                 : ** Otherwise, an SQLite error code is returned and *pp is set to 0.
  124903                 : **
  124904                 : ** If argument apVal is not NULL, then it must point to an array with
  124905                 : ** at least as many entries as the requested statement has bound 
  124906                 : ** parameters. The values are bound to the statements parameters before
  124907                 : ** returning.
  124908                 : */
  124909              18 : static int fts3SqlStmt(
  124910                 :   Fts3Table *p,                   /* Virtual table handle */
  124911                 :   int eStmt,                      /* One of the SQL_XXX constants above */
  124912                 :   sqlite3_stmt **pp,              /* OUT: Statement handle */
  124913                 :   sqlite3_value **apVal           /* Values to bind to statement */
  124914                 : ){
  124915              18 :   const char *azSql[] = {
  124916                 : /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
  124917                 : /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
  124918                 : /* 2  */  "DELETE FROM %Q.'%q_content'",
  124919                 : /* 3  */  "DELETE FROM %Q.'%q_segments'",
  124920                 : /* 4  */  "DELETE FROM %Q.'%q_segdir'",
  124921                 : /* 5  */  "DELETE FROM %Q.'%q_docsize'",
  124922                 : /* 6  */  "DELETE FROM %Q.'%q_stat'",
  124923                 : /* 7  */  "SELECT %s WHERE rowid=?",
  124924                 : /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
  124925                 : /* 9  */  "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
  124926                 : /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
  124927                 : /* 11 */  "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
  124928                 : 
  124929                 :           /* Return segments in order from oldest to newest.*/ 
  124930                 : /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
  124931                 :             "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
  124932                 : /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
  124933                 :             "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
  124934                 :             "ORDER BY level DESC, idx ASC",
  124935                 : 
  124936                 : /* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
  124937                 : /* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
  124938                 : 
  124939                 : /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
  124940                 : /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
  124941                 : /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
  124942                 : /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
  124943                 : /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
  124944                 : /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
  124945                 : /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=0",
  124946                 : /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(0,?)",
  124947                 : /* 24 */  "",
  124948                 : /* 25 */  "",
  124949                 : 
  124950                 : /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
  124951                 : 
  124952                 :   };
  124953              18 :   int rc = SQLITE_OK;
  124954                 :   sqlite3_stmt *pStmt;
  124955                 : 
  124956                 :   assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
  124957              18 :   assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
  124958                 :   
  124959              18 :   pStmt = p->aStmt[eStmt];
  124960              18 :   if( !pStmt ){
  124961                 :     char *zSql;
  124962               6 :     if( eStmt==SQL_CONTENT_INSERT ){
  124963               1 :       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
  124964               5 :     }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
  124965               1 :       zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
  124966                 :     }else{
  124967               4 :       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
  124968                 :     }
  124969               6 :     if( !zSql ){
  124970               0 :       rc = SQLITE_NOMEM;
  124971                 :     }else{
  124972               6 :       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
  124973               6 :       sqlite3_free(zSql);
  124974               6 :       assert( rc==SQLITE_OK || pStmt==0 );
  124975               6 :       p->aStmt[eStmt] = pStmt;
  124976                 :     }
  124977                 :   }
  124978              18 :   if( apVal ){
  124979                 :     int i;
  124980               4 :     int nParam = sqlite3_bind_parameter_count(pStmt);
  124981              16 :     for(i=0; rc==SQLITE_OK && i<nParam; i++){
  124982              12 :       rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
  124983                 :     }
  124984                 :   }
  124985              18 :   *pp = pStmt;
  124986              18 :   return rc;
  124987                 : }
  124988                 : 
  124989               0 : static int fts3SelectDocsize(
  124990                 :   Fts3Table *pTab,                /* FTS3 table handle */
  124991                 :   int eStmt,                      /* Either SQL_SELECT_DOCSIZE or DOCTOTAL */
  124992                 :   sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
  124993                 :   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
  124994                 : ){
  124995               0 :   sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
  124996                 :   int rc;                         /* Return code */
  124997                 : 
  124998               0 :   assert( eStmt==SQL_SELECT_DOCSIZE || eStmt==SQL_SELECT_DOCTOTAL );
  124999                 : 
  125000               0 :   rc = fts3SqlStmt(pTab, eStmt, &pStmt, 0);
  125001               0 :   if( rc==SQLITE_OK ){
  125002               0 :     if( eStmt==SQL_SELECT_DOCSIZE ){
  125003               0 :       sqlite3_bind_int64(pStmt, 1, iDocid);
  125004                 :     }
  125005               0 :     rc = sqlite3_step(pStmt);
  125006               0 :     if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
  125007               0 :       rc = sqlite3_reset(pStmt);
  125008               0 :       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
  125009               0 :       pStmt = 0;
  125010                 :     }else{
  125011               0 :       rc = SQLITE_OK;
  125012                 :     }
  125013                 :   }
  125014                 : 
  125015               0 :   *ppStmt = pStmt;
  125016               0 :   return rc;
  125017                 : }
  125018                 : 
  125019               0 : SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
  125020                 :   Fts3Table *pTab,                /* Fts3 table handle */
  125021                 :   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
  125022                 : ){
  125023               0 :   return fts3SelectDocsize(pTab, SQL_SELECT_DOCTOTAL, 0, ppStmt);
  125024                 : }
  125025                 : 
  125026               0 : SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
  125027                 :   Fts3Table *pTab,                /* Fts3 table handle */
  125028                 :   sqlite3_int64 iDocid,           /* Docid to read size data for */
  125029                 :   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
  125030                 : ){
  125031               0 :   return fts3SelectDocsize(pTab, SQL_SELECT_DOCSIZE, iDocid, ppStmt);
  125032                 : }
  125033                 : 
  125034                 : /*
  125035                 : ** Similar to fts3SqlStmt(). Except, after binding the parameters in
  125036                 : ** array apVal[] to the SQL statement identified by eStmt, the statement
  125037                 : ** is executed.
  125038                 : **
  125039                 : ** Returns SQLITE_OK if the statement is successfully executed, or an
  125040                 : ** SQLite error code otherwise.
  125041                 : */
  125042               0 : static void fts3SqlExec(
  125043                 :   int *pRC,                /* Result code */
  125044                 :   Fts3Table *p,            /* The FTS3 table */
  125045                 :   int eStmt,               /* Index of statement to evaluate */
  125046                 :   sqlite3_value **apVal    /* Parameters to bind */
  125047                 : ){
  125048                 :   sqlite3_stmt *pStmt;
  125049                 :   int rc;
  125050               0 :   if( *pRC ) return;
  125051               0 :   rc = fts3SqlStmt(p, eStmt, &pStmt, apVal); 
  125052               0 :   if( rc==SQLITE_OK ){
  125053               0 :     sqlite3_step(pStmt);
  125054               0 :     rc = sqlite3_reset(pStmt);
  125055                 :   }
  125056               0 :   *pRC = rc;
  125057                 : }
  125058                 : 
  125059                 : 
  125060                 : /*
  125061                 : ** This function ensures that the caller has obtained a shared-cache
  125062                 : ** table-lock on the %_content table. This is required before reading
  125063                 : ** data from the fts3 table. If this lock is not acquired first, then
  125064                 : ** the caller may end up holding read-locks on the %_segments and %_segdir
  125065                 : ** tables, but no read-lock on the %_content table. If this happens 
  125066                 : ** a second connection will be able to write to the fts3 table, but
  125067                 : ** attempting to commit those writes might return SQLITE_LOCKED or
  125068                 : ** SQLITE_LOCKED_SHAREDCACHE (because the commit attempts to obtain 
  125069                 : ** write-locks on the %_segments and %_segdir ** tables). 
  125070                 : **
  125071                 : ** We try to avoid this because if FTS3 returns any error when committing
  125072                 : ** a transaction, the whole transaction will be rolled back. And this is
  125073                 : ** not what users expect when they get SQLITE_LOCKED_SHAREDCACHE. It can
  125074                 : ** still happen if the user reads data directly from the %_segments or
  125075                 : ** %_segdir tables instead of going through FTS3 though.
  125076                 : **
  125077                 : ** This reasoning does not apply to a content=xxx table.
  125078                 : */
  125079               1 : SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *p){
  125080                 :   int rc;                         /* Return code */
  125081                 :   sqlite3_stmt *pStmt;            /* Statement used to obtain lock */
  125082                 : 
  125083               1 :   if( p->zContentTbl==0 ){
  125084               1 :     rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pStmt, 0);
  125085               1 :     if( rc==SQLITE_OK ){
  125086               1 :       sqlite3_bind_null(pStmt, 1);
  125087               1 :       sqlite3_step(pStmt);
  125088               1 :       rc = sqlite3_reset(pStmt);
  125089                 :     }
  125090                 :   }else{
  125091               0 :     rc = SQLITE_OK;
  125092                 :   }
  125093                 : 
  125094               1 :   return rc;
  125095                 : }
  125096                 : 
  125097                 : /*
  125098                 : ** Set *ppStmt to a statement handle that may be used to iterate through
  125099                 : ** all rows in the %_segdir table, from oldest to newest. If successful,
  125100                 : ** return SQLITE_OK. If an error occurs while preparing the statement, 
  125101                 : ** return an SQLite error code.
  125102                 : **
  125103                 : ** There is only ever one instance of this SQL statement compiled for
  125104                 : ** each FTS3 table.
  125105                 : **
  125106                 : ** The statement returns the following columns from the %_segdir table:
  125107                 : **
  125108                 : **   0: idx
  125109                 : **   1: start_block
  125110                 : **   2: leaves_end_block
  125111                 : **   3: end_block
  125112                 : **   4: root
  125113                 : */
  125114               1 : SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
  125115                 :   Fts3Table *p,                   /* FTS3 table */
  125116                 :   int iIndex,                     /* Index for p->aIndex[] */
  125117                 :   int iLevel,                     /* Level to select */
  125118                 :   sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
  125119                 : ){
  125120                 :   int rc;
  125121               1 :   sqlite3_stmt *pStmt = 0;
  125122                 : 
  125123               1 :   assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
  125124               1 :   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
  125125               1 :   assert( iIndex>=0 && iIndex<p->nIndex );
  125126                 : 
  125127               1 :   if( iLevel<0 ){
  125128                 :     /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
  125129               1 :     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
  125130               1 :     if( rc==SQLITE_OK ){ 
  125131               1 :       sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
  125132               1 :       sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL-1);
  125133                 :     }
  125134                 :   }else{
  125135                 :     /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
  125136               0 :     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
  125137               0 :     if( rc==SQLITE_OK ){ 
  125138               0 :       sqlite3_bind_int(pStmt, 1, iLevel+iIndex*FTS3_SEGDIR_MAXLEVEL);
  125139                 :     }
  125140                 :   }
  125141               1 :   *ppStmt = pStmt;
  125142               1 :   return rc;
  125143                 : }
  125144                 : 
  125145                 : 
  125146                 : /*
  125147                 : ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
  125148                 : ** if successful, or an SQLite error code otherwise.
  125149                 : **
  125150                 : ** This function also serves to allocate the PendingList structure itself.
  125151                 : ** For example, to create a new PendingList structure containing two
  125152                 : ** varints:
  125153                 : **
  125154                 : **   PendingList *p = 0;
  125155                 : **   fts3PendingListAppendVarint(&p, 1);
  125156                 : **   fts3PendingListAppendVarint(&p, 2);
  125157                 : */
  125158              76 : static int fts3PendingListAppendVarint(
  125159                 :   PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
  125160                 :   sqlite3_int64 i                 /* Value to append to data */
  125161                 : ){
  125162              76 :   PendingList *p = *pp;
  125163                 : 
  125164                 :   /* Allocate or grow the PendingList as required. */
  125165              76 :   if( !p ){
  125166              20 :     p = sqlite3_malloc(sizeof(*p) + 100);
  125167              20 :     if( !p ){
  125168               0 :       return SQLITE_NOMEM;
  125169                 :     }
  125170              20 :     p->nSpace = 100;
  125171              20 :     p->aData = (char *)&p[1];
  125172              20 :     p->nData = 0;
  125173                 :   }
  125174              56 :   else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
  125175               0 :     int nNew = p->nSpace * 2;
  125176               0 :     p = sqlite3_realloc(p, sizeof(*p) + nNew);
  125177               0 :     if( !p ){
  125178               0 :       sqlite3_free(*pp);
  125179               0 :       *pp = 0;
  125180               0 :       return SQLITE_NOMEM;
  125181                 :     }
  125182               0 :     p->nSpace = nNew;
  125183               0 :     p->aData = (char *)&p[1];
  125184                 :   }
  125185                 : 
  125186                 :   /* Append the new serialized varint to the end of the list. */
  125187              76 :   p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
  125188              76 :   p->aData[p->nData] = '\0';
  125189              76 :   *pp = p;
  125190              76 :   return SQLITE_OK;
  125191                 : }
  125192                 : 
  125193                 : /*
  125194                 : ** Add a docid/column/position entry to a PendingList structure. Non-zero
  125195                 : ** is returned if the structure is sqlite3_realloced as part of adding
  125196                 : ** the entry. Otherwise, zero.
  125197                 : **
  125198                 : ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
  125199                 : ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
  125200                 : ** it is set to SQLITE_OK.
  125201                 : */
  125202              24 : static int fts3PendingListAppend(
  125203                 :   PendingList **pp,               /* IN/OUT: PendingList structure */
  125204                 :   sqlite3_int64 iDocid,           /* Docid for entry to add */
  125205                 :   sqlite3_int64 iCol,             /* Column for entry to add */
  125206                 :   sqlite3_int64 iPos,             /* Position of term for entry to add */
  125207                 :   int *pRc                        /* OUT: Return code */
  125208                 : ){
  125209              24 :   PendingList *p = *pp;
  125210              24 :   int rc = SQLITE_OK;
  125211                 : 
  125212              24 :   assert( !p || p->iLastDocid<=iDocid );
  125213                 : 
  125214              24 :   if( !p || p->iLastDocid!=iDocid ){
  125215              20 :     sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
  125216              20 :     if( p ){
  125217               0 :       assert( p->nData<p->nSpace );
  125218               0 :       assert( p->aData[p->nData]==0 );
  125219               0 :       p->nData++;
  125220                 :     }
  125221              20 :     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
  125222               0 :       goto pendinglistappend_out;
  125223                 :     }
  125224              20 :     p->iLastCol = -1;
  125225              20 :     p->iLastPos = 0;
  125226              20 :     p->iLastDocid = iDocid;
  125227                 :   }
  125228              24 :   if( iCol>0 && p->iLastCol!=iCol ){
  125229              16 :     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
  125230              16 :      || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
  125231                 :     ){
  125232                 :       goto pendinglistappend_out;
  125233                 :     }
  125234              16 :     p->iLastCol = iCol;
  125235              16 :     p->iLastPos = 0;
  125236                 :   }
  125237              24 :   if( iCol>=0 ){
  125238              24 :     assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
  125239              24 :     rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
  125240              24 :     if( rc==SQLITE_OK ){
  125241              24 :       p->iLastPos = iPos;
  125242                 :     }
  125243                 :   }
  125244                 : 
  125245                 :  pendinglistappend_out:
  125246              24 :   *pRc = rc;
  125247              24 :   if( p!=*pp ){
  125248              20 :     *pp = p;
  125249              20 :     return 1;
  125250                 :   }
  125251               4 :   return 0;
  125252                 : }
  125253                 : 
  125254                 : /*
  125255                 : ** Free a PendingList object allocated by fts3PendingListAppend().
  125256                 : */
  125257              20 : static void fts3PendingListDelete(PendingList *pList){
  125258              20 :   sqlite3_free(pList);
  125259              20 : }
  125260                 : 
  125261                 : /*
  125262                 : ** Add an entry to one of the pending-terms hash tables.
  125263                 : */
  125264              24 : static int fts3PendingTermsAddOne(
  125265                 :   Fts3Table *p,
  125266                 :   int iCol,
  125267                 :   int iPos,
  125268                 :   Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
  125269                 :   const char *zToken,
  125270                 :   int nToken
  125271                 : ){
  125272                 :   PendingList *pList;
  125273              24 :   int rc = SQLITE_OK;
  125274                 : 
  125275              24 :   pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
  125276              24 :   if( pList ){
  125277               4 :     p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
  125278                 :   }
  125279              24 :   if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
  125280              20 :     if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
  125281                 :       /* Malloc failed while inserting the new entry. This can only 
  125282                 :       ** happen if there was no previous entry for this token.
  125283                 :       */
  125284               0 :       assert( 0==fts3HashFind(pHash, zToken, nToken) );
  125285               0 :       sqlite3_free(pList);
  125286               0 :       rc = SQLITE_NOMEM;
  125287                 :     }
  125288                 :   }
  125289              24 :   if( rc==SQLITE_OK ){
  125290              24 :     p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
  125291                 :   }
  125292              24 :   return rc;
  125293                 : }
  125294                 : 
  125295                 : /*
  125296                 : ** Tokenize the nul-terminated string zText and add all tokens to the
  125297                 : ** pending-terms hash-table. The docid used is that currently stored in
  125298                 : ** p->iPrevDocid, and the column is specified by argument iCol.
  125299                 : **
  125300                 : ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
  125301                 : */
  125302               8 : static int fts3PendingTermsAdd(
  125303                 :   Fts3Table *p,                   /* Table into which text will be inserted */
  125304                 :   const char *zText,              /* Text of document to be inserted */
  125305                 :   int iCol,                       /* Column into which text is being inserted */
  125306                 :   u32 *pnWord                     /* OUT: Number of tokens inserted */
  125307                 : ){
  125308                 :   int rc;
  125309                 :   int iStart;
  125310                 :   int iEnd;
  125311                 :   int iPos;
  125312               8 :   int nWord = 0;
  125313                 : 
  125314                 :   char const *zToken;
  125315                 :   int nToken;
  125316                 : 
  125317               8 :   sqlite3_tokenizer *pTokenizer = p->pTokenizer;
  125318               8 :   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
  125319                 :   sqlite3_tokenizer_cursor *pCsr;
  125320                 :   int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
  125321                 :       const char**,int*,int*,int*,int*);
  125322                 : 
  125323               8 :   assert( pTokenizer && pModule );
  125324                 : 
  125325                 :   /* If the user has inserted a NULL value, this function may be called with
  125326                 :   ** zText==0. In this case, add zero token entries to the hash table and 
  125327                 :   ** return early. */
  125328               8 :   if( zText==0 ){
  125329               0 :     *pnWord = 0;
  125330               0 :     return SQLITE_OK;
  125331                 :   }
  125332                 : 
  125333               8 :   rc = pModule->xOpen(pTokenizer, zText, -1, &pCsr);
  125334               8 :   if( rc!=SQLITE_OK ){
  125335               0 :     return rc;
  125336                 :   }
  125337               8 :   pCsr->pTokenizer = pTokenizer;
  125338                 : 
  125339               8 :   xNext = pModule->xNext;
  125340              40 :   while( SQLITE_OK==rc
  125341              32 :       && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
  125342                 :   ){
  125343                 :     int i;
  125344              24 :     if( iPos>=nWord ) nWord = iPos+1;
  125345                 : 
  125346                 :     /* Positions cannot be negative; we use -1 as a terminator internally.
  125347                 :     ** Tokens must have a non-zero length.
  125348                 :     */
  125349              24 :     if( iPos<0 || !zToken || nToken<=0 ){
  125350               0 :       rc = SQLITE_ERROR;
  125351               0 :       break;
  125352                 :     }
  125353                 : 
  125354                 :     /* Add the term to the terms index */
  125355              48 :     rc = fts3PendingTermsAddOne(
  125356              24 :         p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
  125357                 :     );
  125358                 :     
  125359                 :     /* Add the term to each of the prefix indexes that it is not too 
  125360                 :     ** short for. */
  125361              24 :     for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
  125362               0 :       struct Fts3Index *pIndex = &p->aIndex[i];
  125363               0 :       if( nToken<pIndex->nPrefix ) continue;
  125364               0 :       rc = fts3PendingTermsAddOne(
  125365                 :           p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
  125366                 :       );
  125367                 :     }
  125368                 :   }
  125369                 : 
  125370               8 :   pModule->xClose(pCsr);
  125371               8 :   *pnWord = nWord;
  125372               8 :   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
  125373                 : }
  125374                 : 
  125375                 : /* 
  125376                 : ** Calling this function indicates that subsequent calls to 
  125377                 : ** fts3PendingTermsAdd() are to add term/position-list pairs for the
  125378                 : ** contents of the document with docid iDocid.
  125379                 : */
  125380               4 : static int fts3PendingTermsDocid(Fts3Table *p, sqlite_int64 iDocid){
  125381                 :   /* TODO(shess) Explore whether partially flushing the buffer on
  125382                 :   ** forced-flush would provide better performance.  I suspect that if
  125383                 :   ** we ordered the doclists by size and flushed the largest until the
  125384                 :   ** buffer was half empty, that would let the less frequent terms
  125385                 :   ** generate longer doclists.
  125386                 :   */
  125387               4 :   if( iDocid<=p->iPrevDocid || p->nPendingData>p->nMaxPendingData ){
  125388               0 :     int rc = sqlite3Fts3PendingTermsFlush(p);
  125389               0 :     if( rc!=SQLITE_OK ) return rc;
  125390                 :   }
  125391               4 :   p->iPrevDocid = iDocid;
  125392               4 :   return SQLITE_OK;
  125393                 : }
  125394                 : 
  125395                 : /*
  125396                 : ** Discard the contents of the pending-terms hash tables. 
  125397                 : */
  125398               5 : SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
  125399                 :   int i;
  125400              10 :   for(i=0; i<p->nIndex; i++){
  125401                 :     Fts3HashElem *pElem;
  125402               5 :     Fts3Hash *pHash = &p->aIndex[i].hPending;
  125403              25 :     for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
  125404              20 :       PendingList *pList = (PendingList *)fts3HashData(pElem);
  125405              20 :       fts3PendingListDelete(pList);
  125406                 :     }
  125407               5 :     fts3HashClear(pHash);
  125408                 :   }
  125409               5 :   p->nPendingData = 0;
  125410               5 : }
  125411                 : 
  125412                 : /*
  125413                 : ** This function is called by the xUpdate() method as part of an INSERT
  125414                 : ** operation. It adds entries for each term in the new record to the
  125415                 : ** pendingTerms hash table.
  125416                 : **
  125417                 : ** Argument apVal is the same as the similarly named argument passed to
  125418                 : ** fts3InsertData(). Parameter iDocid is the docid of the new row.
  125419                 : */
  125420               4 : static int fts3InsertTerms(Fts3Table *p, sqlite3_value **apVal, u32 *aSz){
  125421                 :   int i;                          /* Iterator variable */
  125422              12 :   for(i=2; i<p->nColumn+2; i++){
  125423               8 :     const char *zText = (const char *)sqlite3_value_text(apVal[i]);
  125424               8 :     int rc = fts3PendingTermsAdd(p, zText, i-2, &aSz[i-2]);
  125425               8 :     if( rc!=SQLITE_OK ){
  125426               0 :       return rc;
  125427                 :     }
  125428               8 :     aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
  125429                 :   }
  125430               4 :   return SQLITE_OK;
  125431                 : }
  125432                 : 
  125433                 : /*
  125434                 : ** This function is called by the xUpdate() method for an INSERT operation.
  125435                 : ** The apVal parameter is passed a copy of the apVal argument passed by
  125436                 : ** SQLite to the xUpdate() method. i.e:
  125437                 : **
  125438                 : **   apVal[0]                Not used for INSERT.
  125439                 : **   apVal[1]                rowid
  125440                 : **   apVal[2]                Left-most user-defined column
  125441                 : **   ...
  125442                 : **   apVal[p->nColumn+1]     Right-most user-defined column
  125443                 : **   apVal[p->nColumn+2]     Hidden column with same name as table
  125444                 : **   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
  125445                 : */
  125446               4 : static int fts3InsertData(
  125447                 :   Fts3Table *p,                   /* Full-text table */
  125448                 :   sqlite3_value **apVal,          /* Array of values to insert */
  125449                 :   sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
  125450                 : ){
  125451                 :   int rc;                         /* Return code */
  125452                 :   sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
  125453                 : 
  125454               4 :   if( p->zContentTbl ){
  125455               0 :     sqlite3_value *pRowid = apVal[p->nColumn+3];
  125456               0 :     if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
  125457               0 :       pRowid = apVal[1];
  125458                 :     }
  125459               0 :     if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
  125460               0 :       return SQLITE_CONSTRAINT;
  125461                 :     }
  125462               0 :     *piDocid = sqlite3_value_int64(pRowid);
  125463               0 :     return SQLITE_OK;
  125464                 :   }
  125465                 : 
  125466                 :   /* Locate the statement handle used to insert data into the %_content
  125467                 :   ** table. The SQL for this statement is:
  125468                 :   **
  125469                 :   **   INSERT INTO %_content VALUES(?, ?, ?, ...)
  125470                 :   **
  125471                 :   ** The statement features N '?' variables, where N is the number of user
  125472                 :   ** defined columns in the FTS3 table, plus one for the docid field.
  125473                 :   */
  125474               4 :   rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
  125475               4 :   if( rc!=SQLITE_OK ){
  125476               0 :     return rc;
  125477                 :   }
  125478                 : 
  125479                 :   /* There is a quirk here. The users INSERT statement may have specified
  125480                 :   ** a value for the "rowid" field, for the "docid" field, or for both.
  125481                 :   ** Which is a problem, since "rowid" and "docid" are aliases for the
  125482                 :   ** same value. For example:
  125483                 :   **
  125484                 :   **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
  125485                 :   **
  125486                 :   ** In FTS3, this is an error. It is an error to specify non-NULL values
  125487                 :   ** for both docid and some other rowid alias.
  125488                 :   */
  125489               4 :   if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
  125490               0 :     if( SQLITE_NULL==sqlite3_value_type(apVal[0])
  125491               0 :      && SQLITE_NULL!=sqlite3_value_type(apVal[1])
  125492                 :     ){
  125493                 :       /* A rowid/docid conflict. */
  125494               0 :       return SQLITE_ERROR;
  125495                 :     }
  125496               0 :     rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
  125497               0 :     if( rc!=SQLITE_OK ) return rc;
  125498                 :   }
  125499                 : 
  125500                 :   /* Execute the statement to insert the record. Set *piDocid to the 
  125501                 :   ** new docid value. 
  125502                 :   */
  125503               4 :   sqlite3_step(pContentInsert);
  125504               4 :   rc = sqlite3_reset(pContentInsert);
  125505                 : 
  125506               4 :   *piDocid = sqlite3_last_insert_rowid(p->db);
  125507               4 :   return rc;
  125508                 : }
  125509                 : 
  125510                 : 
  125511                 : 
  125512                 : /*
  125513                 : ** Remove all data from the FTS3 table. Clear the hash table containing
  125514                 : ** pending terms.
  125515                 : */
  125516               0 : static int fts3DeleteAll(Fts3Table *p, int bContent){
  125517               0 :   int rc = SQLITE_OK;             /* Return code */
  125518                 : 
  125519                 :   /* Discard the contents of the pending-terms hash table. */
  125520               0 :   sqlite3Fts3PendingTermsClear(p);
  125521                 : 
  125522                 :   /* Delete everything from the shadow tables. Except, leave %_content as
  125523                 :   ** is if bContent is false.  */
  125524               0 :   assert( p->zContentTbl==0 || bContent==0 );
  125525               0 :   if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
  125526               0 :   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
  125527               0 :   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
  125528               0 :   if( p->bHasDocsize ){
  125529               0 :     fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
  125530                 :   }
  125531               0 :   if( p->bHasStat ){
  125532               0 :     fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
  125533                 :   }
  125534               0 :   return rc;
  125535                 : }
  125536                 : 
  125537                 : /*
  125538                 : ** The first element in the apVal[] array is assumed to contain the docid
  125539                 : ** (an integer) of a row about to be deleted. Remove all terms from the
  125540                 : ** full-text index.
  125541                 : */
  125542               0 : static void fts3DeleteTerms( 
  125543                 :   int *pRC,               /* Result code */
  125544                 :   Fts3Table *p,           /* The FTS table to delete from */
  125545                 :   sqlite3_value *pRowid,  /* The docid to be deleted */
  125546                 :   u32 *aSz                /* Sizes of deleted document written here */
  125547                 : ){
  125548                 :   int rc;
  125549                 :   sqlite3_stmt *pSelect;
  125550                 : 
  125551               0 :   if( *pRC ) return;
  125552               0 :   rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
  125553               0 :   if( rc==SQLITE_OK ){
  125554               0 :     if( SQLITE_ROW==sqlite3_step(pSelect) ){
  125555                 :       int i;
  125556               0 :       for(i=1; i<=p->nColumn; i++){
  125557               0 :         const char *zText = (const char *)sqlite3_column_text(pSelect, i);
  125558               0 :         rc = fts3PendingTermsAdd(p, zText, -1, &aSz[i-1]);
  125559               0 :         if( rc!=SQLITE_OK ){
  125560               0 :           sqlite3_reset(pSelect);
  125561               0 :           *pRC = rc;
  125562               0 :           return;
  125563                 :         }
  125564               0 :         aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
  125565                 :       }
  125566                 :     }
  125567               0 :     rc = sqlite3_reset(pSelect);
  125568                 :   }else{
  125569               0 :     sqlite3_reset(pSelect);
  125570                 :   }
  125571               0 :   *pRC = rc;
  125572                 : }
  125573                 : 
  125574                 : /*
  125575                 : ** Forward declaration to account for the circular dependency between
  125576                 : ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
  125577                 : */
  125578                 : static int fts3SegmentMerge(Fts3Table *, int, int);
  125579                 : 
  125580                 : /* 
  125581                 : ** This function allocates a new level iLevel index in the segdir table.
  125582                 : ** Usually, indexes are allocated within a level sequentially starting
  125583                 : ** with 0, so the allocated index is one greater than the value returned
  125584                 : ** by:
  125585                 : **
  125586                 : **   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
  125587                 : **
  125588                 : ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
  125589                 : ** level, they are merged into a single level (iLevel+1) segment and the 
  125590                 : ** allocated index is 0.
  125591                 : **
  125592                 : ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
  125593                 : ** returned. Otherwise, an SQLite error code is returned.
  125594                 : */
  125595               4 : static int fts3AllocateSegdirIdx(
  125596                 :   Fts3Table *p, 
  125597                 :   int iIndex,                     /* Index for p->aIndex */
  125598                 :   int iLevel, 
  125599                 :   int *piIdx
  125600                 : ){
  125601                 :   int rc;                         /* Return Code */
  125602                 :   sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
  125603               4 :   int iNext = 0;                  /* Result of query pNextIdx */
  125604                 : 
  125605                 :   /* Set variable iNext to the next available segdir index at level iLevel. */
  125606               4 :   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
  125607               4 :   if( rc==SQLITE_OK ){
  125608               4 :     sqlite3_bind_int(pNextIdx, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
  125609               4 :     if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
  125610               4 :       iNext = sqlite3_column_int(pNextIdx, 0);
  125611                 :     }
  125612               4 :     rc = sqlite3_reset(pNextIdx);
  125613                 :   }
  125614                 : 
  125615               4 :   if( rc==SQLITE_OK ){
  125616                 :     /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
  125617                 :     ** full, merge all segments in level iLevel into a single iLevel+1
  125618                 :     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
  125619                 :     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
  125620                 :     */
  125621               4 :     if( iNext>=FTS3_MERGE_COUNT ){
  125622               0 :       rc = fts3SegmentMerge(p, iIndex, iLevel);
  125623               0 :       *piIdx = 0;
  125624                 :     }else{
  125625               4 :       *piIdx = iNext;
  125626                 :     }
  125627                 :   }
  125628                 : 
  125629               4 :   return rc;
  125630                 : }
  125631                 : 
  125632                 : /*
  125633                 : ** The %_segments table is declared as follows:
  125634                 : **
  125635                 : **   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
  125636                 : **
  125637                 : ** This function reads data from a single row of the %_segments table. The
  125638                 : ** specific row is identified by the iBlockid parameter. If paBlob is not
  125639                 : ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
  125640                 : ** with the contents of the blob stored in the "block" column of the 
  125641                 : ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
  125642                 : ** to the size of the blob in bytes before returning.
  125643                 : **
  125644                 : ** If an error occurs, or the table does not contain the specified row,
  125645                 : ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
  125646                 : ** paBlob is non-NULL, then it is the responsibility of the caller to
  125647                 : ** eventually free the returned buffer.
  125648                 : **
  125649                 : ** This function may leave an open sqlite3_blob* handle in the
  125650                 : ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
  125651                 : ** to this function. The handle may be closed by calling the
  125652                 : ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
  125653                 : ** performance improvement, but the blob handle should always be closed
  125654                 : ** before control is returned to the user (to prevent a lock being held
  125655                 : ** on the database file for longer than necessary). Thus, any virtual table
  125656                 : ** method (xFilter etc.) that may directly or indirectly call this function
  125657                 : ** must call sqlite3Fts3SegmentsClose() before returning.
  125658                 : */
  125659               0 : SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
  125660                 :   Fts3Table *p,                   /* FTS3 table handle */
  125661                 :   sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
  125662                 :   char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
  125663                 :   int *pnBlob,                    /* OUT: Size of blob data */
  125664                 :   int *pnLoad                     /* OUT: Bytes actually loaded */
  125665                 : ){
  125666                 :   int rc;                         /* Return code */
  125667                 : 
  125668                 :   /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
  125669               0 :   assert( pnBlob);
  125670                 : 
  125671               0 :   if( p->pSegments ){
  125672               0 :     rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
  125673                 :   }else{
  125674               0 :     if( 0==p->zSegmentsTbl ){
  125675               0 :       p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
  125676               0 :       if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
  125677                 :     }
  125678               0 :     rc = sqlite3_blob_open(
  125679               0 :        p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
  125680                 :     );
  125681                 :   }
  125682                 : 
  125683               0 :   if( rc==SQLITE_OK ){
  125684               0 :     int nByte = sqlite3_blob_bytes(p->pSegments);
  125685               0 :     *pnBlob = nByte;
  125686               0 :     if( paBlob ){
  125687               0 :       char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
  125688               0 :       if( !aByte ){
  125689               0 :         rc = SQLITE_NOMEM;
  125690                 :       }else{
  125691               0 :         if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
  125692               0 :           nByte = FTS3_NODE_CHUNKSIZE;
  125693               0 :           *pnLoad = nByte;
  125694                 :         }
  125695               0 :         rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
  125696               0 :         memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
  125697               0 :         if( rc!=SQLITE_OK ){
  125698               0 :           sqlite3_free(aByte);
  125699               0 :           aByte = 0;
  125700                 :         }
  125701                 :       }
  125702               0 :       *paBlob = aByte;
  125703                 :     }
  125704                 :   }
  125705                 : 
  125706               0 :   return rc;
  125707                 : }
  125708                 : 
  125709                 : /*
  125710                 : ** Close the blob handle at p->pSegments, if it is open. See comments above
  125711                 : ** the sqlite3Fts3ReadBlock() function for details.
  125712                 : */
  125713              10 : SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
  125714              10 :   sqlite3_blob_close(p->pSegments);
  125715              10 :   p->pSegments = 0;
  125716              10 : }
  125717                 :     
  125718               0 : static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
  125719                 :   int nRead;                      /* Number of bytes to read */
  125720                 :   int rc;                         /* Return code */
  125721                 : 
  125722               0 :   nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
  125723               0 :   rc = sqlite3_blob_read(
  125724                 :       pReader->pBlob, 
  125725               0 :       &pReader->aNode[pReader->nPopulate],
  125726                 :       nRead,
  125727                 :       pReader->nPopulate
  125728                 :   );
  125729                 : 
  125730               0 :   if( rc==SQLITE_OK ){
  125731               0 :     pReader->nPopulate += nRead;
  125732               0 :     memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
  125733               0 :     if( pReader->nPopulate==pReader->nNode ){
  125734               0 :       sqlite3_blob_close(pReader->pBlob);
  125735               0 :       pReader->pBlob = 0;
  125736               0 :       pReader->nPopulate = 0;
  125737                 :     }
  125738                 :   }
  125739               0 :   return rc;
  125740                 : }
  125741                 : 
  125742              34 : static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
  125743              34 :   int rc = SQLITE_OK;
  125744              34 :   assert( !pReader->pBlob 
  125745                 :        || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
  125746                 :   );
  125747              68 :   while( pReader->pBlob && rc==SQLITE_OK 
  125748               0 :      &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
  125749                 :   ){
  125750               0 :     rc = fts3SegReaderIncrRead(pReader);
  125751                 :   }
  125752              34 :   return rc;
  125753                 : }
  125754                 : 
  125755                 : /*
  125756                 : ** Move the iterator passed as the first argument to the next term in the
  125757                 : ** segment. If successful, SQLITE_OK is returned. If there is no next term,
  125758                 : ** SQLITE_DONE. Otherwise, an SQLite error code.
  125759                 : */
  125760              40 : static int fts3SegReaderNext(
  125761                 :   Fts3Table *p, 
  125762                 :   Fts3SegReader *pReader,
  125763                 :   int bIncr
  125764                 : ){
  125765                 :   int rc;                         /* Return code of various sub-routines */
  125766                 :   char *pNext;                    /* Cursor variable */
  125767                 :   int nPrefix;                    /* Number of bytes in term prefix */
  125768                 :   int nSuffix;                    /* Number of bytes in term suffix */
  125769                 : 
  125770              40 :   if( !pReader->aDoclist ){
  125771               8 :     pNext = pReader->aNode;
  125772                 :   }else{
  125773              32 :     pNext = &pReader->aDoclist[pReader->nDoclist];
  125774                 :   }
  125775                 : 
  125776              40 :   if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
  125777                 : 
  125778              24 :     if( fts3SegReaderIsPending(pReader) ){
  125779              24 :       Fts3HashElem *pElem = *(pReader->ppNextElem);
  125780              24 :       if( pElem==0 ){
  125781               4 :         pReader->aNode = 0;
  125782                 :       }else{
  125783              20 :         PendingList *pList = (PendingList *)fts3HashData(pElem);
  125784              20 :         pReader->zTerm = (char *)fts3HashKey(pElem);
  125785              20 :         pReader->nTerm = fts3HashKeysize(pElem);
  125786              20 :         pReader->nNode = pReader->nDoclist = pList->nData + 1;
  125787              20 :         pReader->aNode = pReader->aDoclist = pList->aData;
  125788              20 :         pReader->ppNextElem++;
  125789              20 :         assert( pReader->aNode );
  125790                 :       }
  125791              24 :       return SQLITE_OK;
  125792                 :     }
  125793                 : 
  125794               0 :     if( !fts3SegReaderIsRootOnly(pReader) ){
  125795               0 :       sqlite3_free(pReader->aNode);
  125796               0 :       sqlite3_blob_close(pReader->pBlob);
  125797               0 :       pReader->pBlob = 0;
  125798                 :     }
  125799               0 :     pReader->aNode = 0;
  125800                 : 
  125801                 :     /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf 
  125802                 :     ** blocks have already been traversed.  */
  125803               0 :     assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
  125804               0 :     if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
  125805               0 :       return SQLITE_OK;
  125806                 :     }
  125807                 : 
  125808               0 :     rc = sqlite3Fts3ReadBlock(
  125809               0 :         p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode, 
  125810                 :         (bIncr ? &pReader->nPopulate : 0)
  125811                 :     );
  125812               0 :     if( rc!=SQLITE_OK ) return rc;
  125813               0 :     assert( pReader->pBlob==0 );
  125814               0 :     if( bIncr && pReader->nPopulate<pReader->nNode ){
  125815               0 :       pReader->pBlob = p->pSegments;
  125816               0 :       p->pSegments = 0;
  125817                 :     }
  125818               0 :     pNext = pReader->aNode;
  125819                 :   }
  125820                 : 
  125821              16 :   assert( !fts3SegReaderIsPending(pReader) );
  125822                 : 
  125823              16 :   rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
  125824              16 :   if( rc!=SQLITE_OK ) return rc;
  125825                 :   
  125826                 :   /* Because of the FTS3_NODE_PADDING bytes of padding, the following is 
  125827                 :   ** safe (no risk of overread) even if the node data is corrupted. */
  125828              16 :   pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
  125829              16 :   pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
  125830              16 :   if( nPrefix<0 || nSuffix<=0 
  125831              16 :    || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] 
  125832                 :   ){
  125833               0 :     return FTS_CORRUPT_VTAB;
  125834                 :   }
  125835                 : 
  125836              16 :   if( nPrefix+nSuffix>pReader->nTermAlloc ){
  125837               4 :     int nNew = (nPrefix+nSuffix)*2;
  125838               4 :     char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
  125839               4 :     if( !zNew ){
  125840               0 :       return SQLITE_NOMEM;
  125841                 :     }
  125842               4 :     pReader->zTerm = zNew;
  125843               4 :     pReader->nTermAlloc = nNew;
  125844                 :   }
  125845                 : 
  125846              16 :   rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
  125847              16 :   if( rc!=SQLITE_OK ) return rc;
  125848                 : 
  125849              16 :   memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
  125850              16 :   pReader->nTerm = nPrefix+nSuffix;
  125851              16 :   pNext += nSuffix;
  125852              16 :   pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist);
  125853              16 :   pReader->aDoclist = pNext;
  125854              16 :   pReader->pOffsetList = 0;
  125855                 : 
  125856                 :   /* Check that the doclist does not appear to extend past the end of the
  125857                 :   ** b-tree node. And that the final byte of the doclist is 0x00. If either 
  125858                 :   ** of these statements is untrue, then the data structure is corrupt.
  125859                 :   */
  125860              16 :   if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] 
  125861              16 :    || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
  125862                 :   ){
  125863               0 :     return FTS_CORRUPT_VTAB;
  125864                 :   }
  125865              16 :   return SQLITE_OK;
  125866                 : }
  125867                 : 
  125868                 : /*
  125869                 : ** Set the SegReader to point to the first docid in the doclist associated
  125870                 : ** with the current term.
  125871                 : */
  125872               2 : static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
  125873               2 :   int rc = SQLITE_OK;
  125874               2 :   assert( pReader->aDoclist );
  125875               2 :   assert( !pReader->pOffsetList );
  125876               2 :   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
  125877               0 :     u8 bEof = 0;
  125878               0 :     pReader->iDocid = 0;
  125879               0 :     pReader->nOffsetList = 0;
  125880               0 :     sqlite3Fts3DoclistPrev(0,
  125881                 :         pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList, 
  125882                 :         &pReader->iDocid, &pReader->nOffsetList, &bEof
  125883                 :     );
  125884                 :   }else{
  125885               2 :     rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
  125886               2 :     if( rc==SQLITE_OK ){
  125887               2 :       int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
  125888               2 :       pReader->pOffsetList = &pReader->aDoclist[n];
  125889                 :     }
  125890                 :   }
  125891               2 :   return rc;
  125892                 : }
  125893                 : 
  125894                 : /*
  125895                 : ** Advance the SegReader to point to the next docid in the doclist
  125896                 : ** associated with the current term.
  125897                 : ** 
  125898                 : ** If arguments ppOffsetList and pnOffsetList are not NULL, then 
  125899                 : ** *ppOffsetList is set to point to the first column-offset list
  125900                 : ** in the doclist entry (i.e. immediately past the docid varint).
  125901                 : ** *pnOffsetList is set to the length of the set of column-offset
  125902                 : ** lists, not including the nul-terminator byte. For example:
  125903                 : */
  125904               2 : static int fts3SegReaderNextDocid(
  125905                 :   Fts3Table *pTab,
  125906                 :   Fts3SegReader *pReader,         /* Reader to advance to next docid */
  125907                 :   char **ppOffsetList,            /* OUT: Pointer to current position-list */
  125908                 :   int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
  125909                 : ){
  125910               2 :   int rc = SQLITE_OK;
  125911               2 :   char *p = pReader->pOffsetList;
  125912               2 :   char c = 0;
  125913                 : 
  125914               2 :   assert( p );
  125915                 : 
  125916               2 :   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
  125917                 :     /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
  125918                 :     ** Pending-terms doclists are always built up in ascending order, so
  125919                 :     ** we have to iterate through them backwards here. */
  125920               0 :     u8 bEof = 0;
  125921               0 :     if( ppOffsetList ){
  125922               0 :       *ppOffsetList = pReader->pOffsetList;
  125923               0 :       *pnOffsetList = pReader->nOffsetList - 1;
  125924                 :     }
  125925               0 :     sqlite3Fts3DoclistPrev(0,
  125926                 :         pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
  125927                 :         &pReader->nOffsetList, &bEof
  125928                 :     );
  125929               0 :     if( bEof ){
  125930               0 :       pReader->pOffsetList = 0;
  125931                 :     }else{
  125932               0 :       pReader->pOffsetList = p;
  125933                 :     }
  125934                 :   }else{
  125935               2 :     char *pEnd = &pReader->aDoclist[pReader->nDoclist];
  125936                 : 
  125937                 :     /* Pointer p currently points at the first byte of an offset list. The
  125938                 :     ** following block advances it to point one byte past the end of
  125939                 :     ** the same offset list. */
  125940                 :     while( 1 ){
  125941                 :   
  125942                 :       /* The following line of code (and the "p++" below the while() loop) is
  125943                 :       ** normally all that is required to move pointer p to the desired 
  125944                 :       ** position. The exception is if this node is being loaded from disk
  125945                 :       ** incrementally and pointer "p" now points to the first byte passed
  125946                 :       ** the populated part of pReader->aNode[].
  125947                 :       */
  125948               2 :       while( *p | c ) c = *p++ & 0x80;
  125949               2 :       assert( *p==0 );
  125950                 :   
  125951               2 :       if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
  125952               0 :       rc = fts3SegReaderIncrRead(pReader);
  125953               0 :       if( rc!=SQLITE_OK ) return rc;
  125954               0 :     }
  125955               2 :     p++;
  125956                 :   
  125957                 :     /* If required, populate the output variables with a pointer to and the
  125958                 :     ** size of the previous offset-list.
  125959                 :     */
  125960               2 :     if( ppOffsetList ){
  125961               2 :       *ppOffsetList = pReader->pOffsetList;
  125962               2 :       *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
  125963                 :     }
  125964                 : 
  125965               2 :     while( p<pEnd && *p==0 ) p++;
  125966                 :   
  125967                 :     /* If there are no more entries in the doclist, set pOffsetList to
  125968                 :     ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
  125969                 :     ** Fts3SegReader.pOffsetList to point to the next offset list before
  125970                 :     ** returning.
  125971                 :     */
  125972               2 :     if( p>=pEnd ){
  125973               2 :       pReader->pOffsetList = 0;
  125974                 :     }else{
  125975               0 :       rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
  125976               0 :       if( rc==SQLITE_OK ){
  125977                 :         sqlite3_int64 iDelta;
  125978               0 :         pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
  125979               0 :         if( pTab->bDescIdx ){
  125980               0 :           pReader->iDocid -= iDelta;
  125981                 :         }else{
  125982               0 :           pReader->iDocid += iDelta;
  125983                 :         }
  125984                 :       }
  125985                 :     }
  125986                 :   }
  125987                 : 
  125988               2 :   return SQLITE_OK;
  125989                 : }
  125990                 : 
  125991                 : 
  125992               0 : SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
  125993                 :   Fts3Cursor *pCsr, 
  125994                 :   Fts3MultiSegReader *pMsr,
  125995                 :   int *pnOvfl
  125996                 : ){
  125997               0 :   Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
  125998               0 :   int nOvfl = 0;
  125999                 :   int ii;
  126000               0 :   int rc = SQLITE_OK;
  126001               0 :   int pgsz = p->nPgsz;
  126002                 : 
  126003               0 :   assert( p->bHasStat );
  126004               0 :   assert( pgsz>0 );
  126005                 : 
  126006               0 :   for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
  126007               0 :     Fts3SegReader *pReader = pMsr->apSegment[ii];
  126008               0 :     if( !fts3SegReaderIsPending(pReader) 
  126009               0 :      && !fts3SegReaderIsRootOnly(pReader) 
  126010                 :     ){
  126011                 :       sqlite3_int64 jj;
  126012               0 :       for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
  126013                 :         int nBlob;
  126014               0 :         rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
  126015               0 :         if( rc!=SQLITE_OK ) break;
  126016               0 :         if( (nBlob+35)>pgsz ){
  126017               0 :           nOvfl += (nBlob + 34)/pgsz;
  126018                 :         }
  126019                 :       }
  126020                 :     }
  126021                 :   }
  126022               0 :   *pnOvfl = nOvfl;
  126023               0 :   return rc;
  126024                 : }
  126025                 : 
  126026                 : /*
  126027                 : ** Free all allocations associated with the iterator passed as the 
  126028                 : ** second argument.
  126029                 : */
  126030               8 : SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
  126031               8 :   if( pReader && !fts3SegReaderIsPending(pReader) ){
  126032               4 :     sqlite3_free(pReader->zTerm);
  126033               4 :     if( !fts3SegReaderIsRootOnly(pReader) ){
  126034               0 :       sqlite3_free(pReader->aNode);
  126035               0 :       sqlite3_blob_close(pReader->pBlob);
  126036                 :     }
  126037                 :   }
  126038               8 :   sqlite3_free(pReader);
  126039               8 : }
  126040                 : 
  126041                 : /*
  126042                 : ** Allocate a new SegReader object.
  126043                 : */
  126044               4 : SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
  126045                 :   int iAge,                       /* Segment "age". */
  126046                 :   sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
  126047                 :   sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
  126048                 :   sqlite3_int64 iEndBlock,        /* Final block of segment */
  126049                 :   const char *zRoot,              /* Buffer containing root node */
  126050                 :   int nRoot,                      /* Size of buffer containing root node */
  126051                 :   Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
  126052                 : ){
  126053                 :   Fts3SegReader *pReader;         /* Newly allocated SegReader object */
  126054               4 :   int nExtra = 0;                 /* Bytes to allocate segment root node */
  126055                 : 
  126056               4 :   assert( iStartLeaf<=iEndLeaf );
  126057               4 :   if( iStartLeaf==0 ){
  126058               4 :     nExtra = nRoot + FTS3_NODE_PADDING;
  126059                 :   }
  126060                 : 
  126061               4 :   pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
  126062               4 :   if( !pReader ){
  126063               0 :     return SQLITE_NOMEM;
  126064                 :   }
  126065               4 :   memset(pReader, 0, sizeof(Fts3SegReader));
  126066               4 :   pReader->iIdx = iAge;
  126067               4 :   pReader->iStartBlock = iStartLeaf;
  126068               4 :   pReader->iLeafEndBlock = iEndLeaf;
  126069               4 :   pReader->iEndBlock = iEndBlock;
  126070                 : 
  126071               4 :   if( nExtra ){
  126072                 :     /* The entire segment is stored in the root node. */
  126073               4 :     pReader->aNode = (char *)&pReader[1];
  126074               4 :     pReader->nNode = nRoot;
  126075               4 :     memcpy(pReader->aNode, zRoot, nRoot);
  126076               4 :     memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
  126077                 :   }else{
  126078               0 :     pReader->iCurrentBlock = iStartLeaf-1;
  126079                 :   }
  126080               4 :   *ppReader = pReader;
  126081               4 :   return SQLITE_OK;
  126082                 : }
  126083                 : 
  126084                 : /*
  126085                 : ** This is a comparison function used as a qsort() callback when sorting
  126086                 : ** an array of pending terms by term. This occurs as part of flushing
  126087                 : ** the contents of the pending-terms hash table to the database.
  126088                 : */
  126089              29 : static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
  126090              29 :   char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
  126091              29 :   char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
  126092              29 :   int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
  126093              29 :   int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
  126094                 : 
  126095              29 :   int n = (n1<n2 ? n1 : n2);
  126096              29 :   int c = memcmp(z1, z2, n);
  126097              29 :   if( c==0 ){
  126098               0 :     c = n1 - n2;
  126099                 :   }
  126100              29 :   return c;
  126101                 : }
  126102                 : 
  126103                 : /*
  126104                 : ** This function is used to allocate an Fts3SegReader that iterates through
  126105                 : ** a subset of the terms stored in the Fts3Table.pendingTerms array.
  126106                 : **
  126107                 : ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
  126108                 : ** through each term in the pending-terms table. Or, if isPrefixIter is
  126109                 : ** non-zero, it iterates through each term and its prefixes. For example, if
  126110                 : ** the pending terms hash table contains the terms "sqlite", "mysql" and
  126111                 : ** "firebird", then the iterator visits the following 'terms' (in the order
  126112                 : ** shown):
  126113                 : **
  126114                 : **   f fi fir fire fireb firebi firebir firebird
  126115                 : **   m my mys mysq mysql
  126116                 : **   s sq sql sqli sqlit sqlite
  126117                 : **
  126118                 : ** Whereas if isPrefixIter is zero, the terms visited are:
  126119                 : **
  126120                 : **   firebird mysql sqlite
  126121                 : */
  126122               6 : SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
  126123                 :   Fts3Table *p,                   /* Virtual table handle */
  126124                 :   int iIndex,                     /* Index for p->aIndex */
  126125                 :   const char *zTerm,              /* Term to search for */
  126126                 :   int nTerm,                      /* Size of buffer zTerm */
  126127                 :   int bPrefix,                    /* True for a prefix iterator */
  126128                 :   Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
  126129                 : ){
  126130               6 :   Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
  126131                 :   Fts3HashElem *pE;               /* Iterator variable */
  126132               6 :   Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
  126133               6 :   int nElem = 0;                  /* Size of array at aElem */
  126134               6 :   int rc = SQLITE_OK;             /* Return Code */
  126135                 :   Fts3Hash *pHash;
  126136                 : 
  126137               6 :   pHash = &p->aIndex[iIndex].hPending;
  126138               6 :   if( bPrefix ){
  126139               5 :     int nAlloc = 0;               /* Size of allocated array at aElem */
  126140                 : 
  126141              25 :     for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
  126142              20 :       char *zKey = (char *)fts3HashKey(pE);
  126143              20 :       int nKey = fts3HashKeysize(pE);
  126144              20 :       if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
  126145              20 :         if( nElem==nAlloc ){
  126146                 :           Fts3HashElem **aElem2;
  126147               4 :           nAlloc += 16;
  126148               4 :           aElem2 = (Fts3HashElem **)sqlite3_realloc(
  126149               4 :               aElem, nAlloc*sizeof(Fts3HashElem *)
  126150                 :           );
  126151               4 :           if( !aElem2 ){
  126152               0 :             rc = SQLITE_NOMEM;
  126153               0 :             nElem = 0;
  126154               0 :             break;
  126155                 :           }
  126156               4 :           aElem = aElem2;
  126157                 :         }
  126158                 : 
  126159              20 :         aElem[nElem++] = pE;
  126160                 :       }
  126161                 :     }
  126162                 : 
  126163                 :     /* If more than one term matches the prefix, sort the Fts3HashElem
  126164                 :     ** objects in term order using qsort(). This uses the same comparison
  126165                 :     ** callback as is used when flushing terms to disk.
  126166                 :     */
  126167               5 :     if( nElem>1 ){
  126168               4 :       qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
  126169                 :     }
  126170                 : 
  126171                 :   }else{
  126172                 :     /* The query is a simple term lookup that matches at most one term in
  126173                 :     ** the index. All that is required is a straight hash-lookup. 
  126174                 :     **
  126175                 :     ** Because the stack address of pE may be accessed via the aElem pointer
  126176                 :     ** below, the "Fts3HashElem *pE" must be declared so that it is valid
  126177                 :     ** within this entire function, not just this "else{...}" block.
  126178                 :     */
  126179               1 :     pE = fts3HashFindElem(pHash, zTerm, nTerm);
  126180               1 :     if( pE ){
  126181               0 :       aElem = &pE;
  126182               0 :       nElem = 1;
  126183                 :     }
  126184                 :   }
  126185                 : 
  126186               6 :   if( nElem>0 ){
  126187               4 :     int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
  126188               4 :     pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
  126189               4 :     if( !pReader ){
  126190               0 :       rc = SQLITE_NOMEM;
  126191                 :     }else{
  126192               4 :       memset(pReader, 0, nByte);
  126193               4 :       pReader->iIdx = 0x7FFFFFFF;
  126194               4 :       pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
  126195               4 :       memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
  126196                 :     }
  126197                 :   }
  126198                 : 
  126199               6 :   if( bPrefix ){
  126200               5 :     sqlite3_free(aElem);
  126201                 :   }
  126202               6 :   *ppReader = pReader;
  126203               6 :   return rc;
  126204                 : }
  126205                 : 
  126206                 : /*
  126207                 : ** Compare the entries pointed to by two Fts3SegReader structures. 
  126208                 : ** Comparison is as follows:
  126209                 : **
  126210                 : **   1) EOF is greater than not EOF.
  126211                 : **
  126212                 : **   2) The current terms (if any) are compared using memcmp(). If one
  126213                 : **      term is a prefix of another, the longer term is considered the
  126214                 : **      larger.
  126215                 : **
  126216                 : **   3) By segment age. An older segment is considered larger.
  126217                 : */
  126218               8 : static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
  126219                 :   int rc;
  126220              16 :   if( pLhs->aNode && pRhs->aNode ){
  126221               8 :     int rc2 = pLhs->nTerm - pRhs->nTerm;
  126222               8 :     if( rc2<0 ){
  126223               2 :       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
  126224                 :     }else{
  126225               6 :       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
  126226                 :     }
  126227               8 :     if( rc==0 ){
  126228               2 :       rc = rc2;
  126229                 :     }
  126230                 :   }else{
  126231               0 :     rc = (pLhs->aNode==0) - (pRhs->aNode==0);
  126232                 :   }
  126233               8 :   if( rc==0 ){
  126234               2 :     rc = pRhs->iIdx - pLhs->iIdx;
  126235                 :   }
  126236               8 :   assert( rc!=0 );
  126237               8 :   return rc;
  126238                 : }
  126239                 : 
  126240                 : /*
  126241                 : ** A different comparison function for SegReader structures. In this
  126242                 : ** version, it is assumed that each SegReader points to an entry in
  126243                 : ** a doclist for identical terms. Comparison is made as follows:
  126244                 : **
  126245                 : **   1) EOF (end of doclist in this case) is greater than not EOF.
  126246                 : **
  126247                 : **   2) By current docid.
  126248                 : **
  126249                 : **   3) By segment age. An older segment is considered larger.
  126250                 : */
  126251               3 : static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
  126252               3 :   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
  126253               3 :   if( rc==0 ){
  126254               2 :     if( pLhs->iDocid==pRhs->iDocid ){
  126255               0 :       rc = pRhs->iIdx - pLhs->iIdx;
  126256                 :     }else{
  126257               2 :       rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
  126258                 :     }
  126259                 :   }
  126260               3 :   assert( pLhs->aNode && pRhs->aNode );
  126261               3 :   return rc;
  126262                 : }
  126263               0 : static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
  126264               0 :   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
  126265               0 :   if( rc==0 ){
  126266               0 :     if( pLhs->iDocid==pRhs->iDocid ){
  126267               0 :       rc = pRhs->iIdx - pLhs->iIdx;
  126268                 :     }else{
  126269               0 :       rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
  126270                 :     }
  126271                 :   }
  126272               0 :   assert( pLhs->aNode && pRhs->aNode );
  126273               0 :   return rc;
  126274                 : }
  126275                 : 
  126276                 : /*
  126277                 : ** Compare the term that the Fts3SegReader object passed as the first argument
  126278                 : ** points to with the term specified by arguments zTerm and nTerm. 
  126279                 : **
  126280                 : ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
  126281                 : ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
  126282                 : ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
  126283                 : */
  126284              19 : static int fts3SegReaderTermCmp(
  126285                 :   Fts3SegReader *pSeg,            /* Segment reader object */
  126286                 :   const char *zTerm,              /* Term to compare to */
  126287                 :   int nTerm                       /* Size of term zTerm in bytes */
  126288                 : ){
  126289              19 :   int res = 0;
  126290              19 :   if( pSeg->aNode ){
  126291              19 :     if( pSeg->nTerm>nTerm ){
  126292              15 :       res = memcmp(pSeg->zTerm, zTerm, nTerm);
  126293                 :     }else{
  126294               4 :       res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
  126295                 :     }
  126296              19 :     if( res==0 ){
  126297               4 :       res = pSeg->nTerm-nTerm;
  126298                 :     }
  126299                 :   }
  126300              19 :   return res;
  126301                 : }
  126302                 : 
  126303                 : /*
  126304                 : ** Argument apSegment is an array of nSegment elements. It is known that
  126305                 : ** the final (nSegment-nSuspect) members are already in sorted order
  126306                 : ** (according to the comparison function provided). This function shuffles
  126307                 : ** the array around until all entries are in sorted order.
  126308                 : */
  126309              32 : static void fts3SegReaderSort(
  126310                 :   Fts3SegReader **apSegment,                     /* Array to sort entries of */
  126311                 :   int nSegment,                                  /* Size of apSegment array */
  126312                 :   int nSuspect,                                  /* Unsorted entry count */
  126313                 :   int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
  126314                 : ){
  126315                 :   int i;                          /* Iterator variable */
  126316                 : 
  126317              32 :   assert( nSuspect<=nSegment );
  126318                 : 
  126319              32 :   if( nSuspect==nSegment ) nSuspect--;
  126320              38 :   for(i=nSuspect-1; i>=0; i--){
  126321                 :     int j;
  126322              15 :     for(j=i; j<(nSegment-1); j++){
  126323                 :       Fts3SegReader *pTmp;
  126324               9 :       if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
  126325               9 :       pTmp = apSegment[j+1];
  126326               9 :       apSegment[j+1] = apSegment[j];
  126327               9 :       apSegment[j] = pTmp;
  126328                 :     }
  126329                 :   }
  126330                 : 
  126331                 : #ifndef NDEBUG
  126332                 :   /* Check that the list really is sorted now. */
  126333              34 :   for(i=0; i<(nSuspect-1); i++){
  126334               2 :     assert( xCmp(apSegment[i], apSegment[i+1])<0 );
  126335                 :   }
  126336                 : #endif
  126337              32 : }
  126338                 : 
  126339                 : /* 
  126340                 : ** Insert a record into the %_segments table.
  126341                 : */
  126342               0 : static int fts3WriteSegment(
  126343                 :   Fts3Table *p,                   /* Virtual table handle */
  126344                 :   sqlite3_int64 iBlock,           /* Block id for new block */
  126345                 :   char *z,                        /* Pointer to buffer containing block data */
  126346                 :   int n                           /* Size of buffer z in bytes */
  126347                 : ){
  126348                 :   sqlite3_stmt *pStmt;
  126349               0 :   int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
  126350               0 :   if( rc==SQLITE_OK ){
  126351               0 :     sqlite3_bind_int64(pStmt, 1, iBlock);
  126352               0 :     sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
  126353               0 :     sqlite3_step(pStmt);
  126354               0 :     rc = sqlite3_reset(pStmt);
  126355                 :   }
  126356               0 :   return rc;
  126357                 : }
  126358                 : 
  126359                 : /* 
  126360                 : ** Insert a record into the %_segdir table.
  126361                 : */
  126362               4 : static int fts3WriteSegdir(
  126363                 :   Fts3Table *p,                   /* Virtual table handle */
  126364                 :   int iLevel,                     /* Value for "level" field */
  126365                 :   int iIdx,                       /* Value for "idx" field */
  126366                 :   sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
  126367                 :   sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
  126368                 :   sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
  126369                 :   char *zRoot,                    /* Blob value for "root" field */
  126370                 :   int nRoot                       /* Number of bytes in buffer zRoot */
  126371                 : ){
  126372                 :   sqlite3_stmt *pStmt;
  126373               4 :   int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
  126374               4 :   if( rc==SQLITE_OK ){
  126375               4 :     sqlite3_bind_int(pStmt, 1, iLevel);
  126376               4 :     sqlite3_bind_int(pStmt, 2, iIdx);
  126377               4 :     sqlite3_bind_int64(pStmt, 3, iStartBlock);
  126378               4 :     sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
  126379               4 :     sqlite3_bind_int64(pStmt, 5, iEndBlock);
  126380               4 :     sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
  126381               4 :     sqlite3_step(pStmt);
  126382               4 :     rc = sqlite3_reset(pStmt);
  126383                 :   }
  126384               4 :   return rc;
  126385                 : }
  126386                 : 
  126387                 : /*
  126388                 : ** Return the size of the common prefix (if any) shared by zPrev and
  126389                 : ** zNext, in bytes. For example, 
  126390                 : **
  126391                 : **   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
  126392                 : **   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
  126393                 : **   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
  126394                 : */
  126395              20 : static int fts3PrefixCompress(
  126396                 :   const char *zPrev,              /* Buffer containing previous term */
  126397                 :   int nPrev,                      /* Size of buffer zPrev in bytes */
  126398                 :   const char *zNext,              /* Buffer containing next term */
  126399                 :   int nNext                       /* Size of buffer zNext in bytes */
  126400                 : ){
  126401                 :   int n;
  126402                 :   UNUSED_PARAMETER(nNext);
  126403              20 :   for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
  126404              20 :   return n;
  126405                 : }
  126406                 : 
  126407                 : /*
  126408                 : ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
  126409                 : ** (according to memcmp) than the previous term.
  126410                 : */
  126411               0 : static int fts3NodeAddTerm(
  126412                 :   Fts3Table *p,                   /* Virtual table handle */
  126413                 :   SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */ 
  126414                 :   int isCopyTerm,                 /* True if zTerm/nTerm is transient */
  126415                 :   const char *zTerm,              /* Pointer to buffer containing term */
  126416                 :   int nTerm                       /* Size of term in bytes */
  126417                 : ){
  126418               0 :   SegmentNode *pTree = *ppTree;
  126419                 :   int rc;
  126420                 :   SegmentNode *pNew;
  126421                 : 
  126422                 :   /* First try to append the term to the current node. Return early if 
  126423                 :   ** this is possible.
  126424                 :   */
  126425               0 :   if( pTree ){
  126426               0 :     int nData = pTree->nData;     /* Current size of node in bytes */
  126427               0 :     int nReq = nData;             /* Required space after adding zTerm */
  126428                 :     int nPrefix;                  /* Number of bytes of prefix compression */
  126429                 :     int nSuffix;                  /* Suffix length */
  126430                 : 
  126431               0 :     nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
  126432               0 :     nSuffix = nTerm-nPrefix;
  126433                 : 
  126434               0 :     nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
  126435               0 :     if( nReq<=p->nNodeSize || !pTree->zTerm ){
  126436                 : 
  126437               0 :       if( nReq>p->nNodeSize ){
  126438                 :         /* An unusual case: this is the first term to be added to the node
  126439                 :         ** and the static node buffer (p->nNodeSize bytes) is not large
  126440                 :         ** enough. Use a separately malloced buffer instead This wastes
  126441                 :         ** p->nNodeSize bytes, but since this scenario only comes about when
  126442                 :         ** the database contain two terms that share a prefix of almost 2KB, 
  126443                 :         ** this is not expected to be a serious problem. 
  126444                 :         */
  126445               0 :         assert( pTree->aData==(char *)&pTree[1] );
  126446               0 :         pTree->aData = (char *)sqlite3_malloc(nReq);
  126447               0 :         if( !pTree->aData ){
  126448               0 :           return SQLITE_NOMEM;
  126449                 :         }
  126450                 :       }
  126451                 : 
  126452               0 :       if( pTree->zTerm ){
  126453                 :         /* There is no prefix-length field for first term in a node */
  126454               0 :         nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
  126455                 :       }
  126456                 : 
  126457               0 :       nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
  126458               0 :       memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
  126459               0 :       pTree->nData = nData + nSuffix;
  126460               0 :       pTree->nEntry++;
  126461                 : 
  126462               0 :       if( isCopyTerm ){
  126463               0 :         if( pTree->nMalloc<nTerm ){
  126464               0 :           char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
  126465               0 :           if( !zNew ){
  126466               0 :             return SQLITE_NOMEM;
  126467                 :           }
  126468               0 :           pTree->nMalloc = nTerm*2;
  126469               0 :           pTree->zMalloc = zNew;
  126470                 :         }
  126471               0 :         pTree->zTerm = pTree->zMalloc;
  126472               0 :         memcpy(pTree->zTerm, zTerm, nTerm);
  126473               0 :         pTree->nTerm = nTerm;
  126474                 :       }else{
  126475               0 :         pTree->zTerm = (char *)zTerm;
  126476               0 :         pTree->nTerm = nTerm;
  126477                 :       }
  126478               0 :       return SQLITE_OK;
  126479                 :     }
  126480                 :   }
  126481                 : 
  126482                 :   /* If control flows to here, it was not possible to append zTerm to the
  126483                 :   ** current node. Create a new node (a right-sibling of the current node).
  126484                 :   ** If this is the first node in the tree, the term is added to it.
  126485                 :   **
  126486                 :   ** Otherwise, the term is not added to the new node, it is left empty for
  126487                 :   ** now. Instead, the term is inserted into the parent of pTree. If pTree 
  126488                 :   ** has no parent, one is created here.
  126489                 :   */
  126490               0 :   pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
  126491               0 :   if( !pNew ){
  126492               0 :     return SQLITE_NOMEM;
  126493                 :   }
  126494               0 :   memset(pNew, 0, sizeof(SegmentNode));
  126495               0 :   pNew->nData = 1 + FTS3_VARINT_MAX;
  126496               0 :   pNew->aData = (char *)&pNew[1];
  126497                 : 
  126498               0 :   if( pTree ){
  126499               0 :     SegmentNode *pParent = pTree->pParent;
  126500               0 :     rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
  126501               0 :     if( pTree->pParent==0 ){
  126502               0 :       pTree->pParent = pParent;
  126503                 :     }
  126504               0 :     pTree->pRight = pNew;
  126505               0 :     pNew->pLeftmost = pTree->pLeftmost;
  126506               0 :     pNew->pParent = pParent;
  126507               0 :     pNew->zMalloc = pTree->zMalloc;
  126508               0 :     pNew->nMalloc = pTree->nMalloc;
  126509               0 :     pTree->zMalloc = 0;
  126510                 :   }else{
  126511               0 :     pNew->pLeftmost = pNew;
  126512               0 :     rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); 
  126513                 :   }
  126514                 : 
  126515               0 :   *ppTree = pNew;
  126516               0 :   return rc;
  126517                 : }
  126518                 : 
  126519                 : /*
  126520                 : ** Helper function for fts3NodeWrite().
  126521                 : */
  126522               0 : static int fts3TreeFinishNode(
  126523                 :   SegmentNode *pTree, 
  126524                 :   int iHeight, 
  126525                 :   sqlite3_int64 iLeftChild
  126526                 : ){
  126527                 :   int nStart;
  126528               0 :   assert( iHeight>=1 && iHeight<128 );
  126529               0 :   nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
  126530               0 :   pTree->aData[nStart] = (char)iHeight;
  126531               0 :   sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
  126532               0 :   return nStart;
  126533                 : }
  126534                 : 
  126535                 : /*
  126536                 : ** Write the buffer for the segment node pTree and all of its peers to the
  126537                 : ** database. Then call this function recursively to write the parent of 
  126538                 : ** pTree and its peers to the database. 
  126539                 : **
  126540                 : ** Except, if pTree is a root node, do not write it to the database. Instead,
  126541                 : ** set output variables *paRoot and *pnRoot to contain the root node.
  126542                 : **
  126543                 : ** If successful, SQLITE_OK is returned and output variable *piLast is
  126544                 : ** set to the largest blockid written to the database (or zero if no
  126545                 : ** blocks were written to the db). Otherwise, an SQLite error code is 
  126546                 : ** returned.
  126547                 : */
  126548               0 : static int fts3NodeWrite(
  126549                 :   Fts3Table *p,                   /* Virtual table handle */
  126550                 :   SegmentNode *pTree,             /* SegmentNode handle */
  126551                 :   int iHeight,                    /* Height of this node in tree */
  126552                 :   sqlite3_int64 iLeaf,            /* Block id of first leaf node */
  126553                 :   sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
  126554                 :   sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
  126555                 :   char **paRoot,                  /* OUT: Data for root node */
  126556                 :   int *pnRoot                     /* OUT: Size of root node in bytes */
  126557                 : ){
  126558               0 :   int rc = SQLITE_OK;
  126559                 : 
  126560               0 :   if( !pTree->pParent ){
  126561                 :     /* Root node of the tree. */
  126562               0 :     int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
  126563               0 :     *piLast = iFree-1;
  126564               0 :     *pnRoot = pTree->nData - nStart;
  126565               0 :     *paRoot = &pTree->aData[nStart];
  126566                 :   }else{
  126567                 :     SegmentNode *pIter;
  126568               0 :     sqlite3_int64 iNextFree = iFree;
  126569               0 :     sqlite3_int64 iNextLeaf = iLeaf;
  126570               0 :     for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
  126571               0 :       int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
  126572               0 :       int nWrite = pIter->nData - nStart;
  126573                 :   
  126574               0 :       rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
  126575               0 :       iNextFree++;
  126576               0 :       iNextLeaf += (pIter->nEntry+1);
  126577                 :     }
  126578               0 :     if( rc==SQLITE_OK ){
  126579               0 :       assert( iNextLeaf==iFree );
  126580               0 :       rc = fts3NodeWrite(
  126581                 :           p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
  126582                 :       );
  126583                 :     }
  126584                 :   }
  126585                 : 
  126586               0 :   return rc;
  126587                 : }
  126588                 : 
  126589                 : /*
  126590                 : ** Free all memory allocations associated with the tree pTree.
  126591                 : */
  126592               4 : static void fts3NodeFree(SegmentNode *pTree){
  126593               4 :   if( pTree ){
  126594               0 :     SegmentNode *p = pTree->pLeftmost;
  126595               0 :     fts3NodeFree(p->pParent);
  126596               0 :     while( p ){
  126597               0 :       SegmentNode *pRight = p->pRight;
  126598               0 :       if( p->aData!=(char *)&p[1] ){
  126599               0 :         sqlite3_free(p->aData);
  126600                 :       }
  126601               0 :       assert( pRight==0 || p->zMalloc==0 );
  126602               0 :       sqlite3_free(p->zMalloc);
  126603               0 :       sqlite3_free(p);
  126604               0 :       p = pRight;
  126605                 :     }
  126606                 :   }
  126607               4 : }
  126608                 : 
  126609                 : /*
  126610                 : ** Add a term to the segment being constructed by the SegmentWriter object
  126611                 : ** *ppWriter. When adding the first term to a segment, *ppWriter should
  126612                 : ** be passed NULL. This function will allocate a new SegmentWriter object
  126613                 : ** and return it via the input/output variable *ppWriter in this case.
  126614                 : **
  126615                 : ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
  126616                 : */
  126617              20 : static int fts3SegWriterAdd(
  126618                 :   Fts3Table *p,                   /* Virtual table handle */
  126619                 :   SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */ 
  126620                 :   int isCopyTerm,                 /* True if buffer zTerm must be copied */
  126621                 :   const char *zTerm,              /* Pointer to buffer containing term */
  126622                 :   int nTerm,                      /* Size of term in bytes */
  126623                 :   const char *aDoclist,           /* Pointer to buffer containing doclist */
  126624                 :   int nDoclist                    /* Size of doclist in bytes */
  126625                 : ){
  126626                 :   int nPrefix;                    /* Size of term prefix in bytes */
  126627                 :   int nSuffix;                    /* Size of term suffix in bytes */
  126628                 :   int nReq;                       /* Number of bytes required on leaf page */
  126629                 :   int nData;
  126630              20 :   SegmentWriter *pWriter = *ppWriter;
  126631                 : 
  126632              20 :   if( !pWriter ){
  126633                 :     int rc;
  126634                 :     sqlite3_stmt *pStmt;
  126635                 : 
  126636                 :     /* Allocate the SegmentWriter structure */
  126637               4 :     pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
  126638               4 :     if( !pWriter ) return SQLITE_NOMEM;
  126639               4 :     memset(pWriter, 0, sizeof(SegmentWriter));
  126640               4 :     *ppWriter = pWriter;
  126641                 : 
  126642                 :     /* Allocate a buffer in which to accumulate data */
  126643               4 :     pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
  126644               4 :     if( !pWriter->aData ) return SQLITE_NOMEM;
  126645               4 :     pWriter->nSize = p->nNodeSize;
  126646                 : 
  126647                 :     /* Find the next free blockid in the %_segments table */
  126648               4 :     rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
  126649               4 :     if( rc!=SQLITE_OK ) return rc;
  126650               4 :     if( SQLITE_ROW==sqlite3_step(pStmt) ){
  126651               4 :       pWriter->iFree = sqlite3_column_int64(pStmt, 0);
  126652               4 :       pWriter->iFirst = pWriter->iFree;
  126653                 :     }
  126654               4 :     rc = sqlite3_reset(pStmt);
  126655               4 :     if( rc!=SQLITE_OK ) return rc;
  126656                 :   }
  126657              20 :   nData = pWriter->nData;
  126658                 : 
  126659              20 :   nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
  126660              20 :   nSuffix = nTerm-nPrefix;
  126661                 : 
  126662                 :   /* Figure out how many bytes are required by this new entry */
  126663              60 :   nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
  126664              40 :     sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
  126665              20 :     nSuffix +                               /* Term suffix */
  126666              20 :     sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
  126667                 :     nDoclist;                               /* Doclist data */
  126668                 : 
  126669              20 :   if( nData>0 && nData+nReq>p->nNodeSize ){
  126670                 :     int rc;
  126671                 : 
  126672                 :     /* The current leaf node is full. Write it out to the database. */
  126673               0 :     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
  126674               0 :     if( rc!=SQLITE_OK ) return rc;
  126675                 : 
  126676                 :     /* Add the current term to the interior node tree. The term added to
  126677                 :     ** the interior tree must:
  126678                 :     **
  126679                 :     **   a) be greater than the largest term on the leaf node just written
  126680                 :     **      to the database (still available in pWriter->zTerm), and
  126681                 :     **
  126682                 :     **   b) be less than or equal to the term about to be added to the new
  126683                 :     **      leaf node (zTerm/nTerm).
  126684                 :     **
  126685                 :     ** In other words, it must be the prefix of zTerm 1 byte longer than
  126686                 :     ** the common prefix (if any) of zTerm and pWriter->zTerm.
  126687                 :     */
  126688               0 :     assert( nPrefix<nTerm );
  126689               0 :     rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
  126690               0 :     if( rc!=SQLITE_OK ) return rc;
  126691                 : 
  126692               0 :     nData = 0;
  126693               0 :     pWriter->nTerm = 0;
  126694                 : 
  126695               0 :     nPrefix = 0;
  126696               0 :     nSuffix = nTerm;
  126697               0 :     nReq = 1 +                              /* varint containing prefix size */
  126698               0 :       sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
  126699               0 :       nTerm +                               /* Term suffix */
  126700               0 :       sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
  126701                 :       nDoclist;                             /* Doclist data */
  126702                 :   }
  126703                 : 
  126704                 :   /* If the buffer currently allocated is too small for this entry, realloc
  126705                 :   ** the buffer to make it large enough.
  126706                 :   */
  126707              20 :   if( nReq>pWriter->nSize ){
  126708               0 :     char *aNew = sqlite3_realloc(pWriter->aData, nReq);
  126709               0 :     if( !aNew ) return SQLITE_NOMEM;
  126710               0 :     pWriter->aData = aNew;
  126711               0 :     pWriter->nSize = nReq;
  126712                 :   }
  126713              20 :   assert( nData+nReq<=pWriter->nSize );
  126714                 : 
  126715                 :   /* Append the prefix-compressed term and doclist to the buffer. */
  126716              20 :   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
  126717              20 :   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
  126718              20 :   memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
  126719              20 :   nData += nSuffix;
  126720              20 :   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
  126721              20 :   memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
  126722              20 :   pWriter->nData = nData + nDoclist;
  126723                 : 
  126724                 :   /* Save the current term so that it can be used to prefix-compress the next.
  126725                 :   ** If the isCopyTerm parameter is true, then the buffer pointed to by
  126726                 :   ** zTerm is transient, so take a copy of the term data. Otherwise, just
  126727                 :   ** store a copy of the pointer.
  126728                 :   */
  126729              20 :   if( isCopyTerm ){
  126730              20 :     if( nTerm>pWriter->nMalloc ){
  126731               4 :       char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
  126732               4 :       if( !zNew ){
  126733               0 :         return SQLITE_NOMEM;
  126734                 :       }
  126735               4 :       pWriter->nMalloc = nTerm*2;
  126736               4 :       pWriter->zMalloc = zNew;
  126737               4 :       pWriter->zTerm = zNew;
  126738                 :     }
  126739              20 :     assert( pWriter->zTerm==pWriter->zMalloc );
  126740              20 :     memcpy(pWriter->zTerm, zTerm, nTerm);
  126741                 :   }else{
  126742               0 :     pWriter->zTerm = (char *)zTerm;
  126743                 :   }
  126744              20 :   pWriter->nTerm = nTerm;
  126745                 : 
  126746              20 :   return SQLITE_OK;
  126747                 : }
  126748                 : 
  126749                 : /*
  126750                 : ** Flush all data associated with the SegmentWriter object pWriter to the
  126751                 : ** database. This function must be called after all terms have been added
  126752                 : ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
  126753                 : ** returned. Otherwise, an SQLite error code.
  126754                 : */
  126755               4 : static int fts3SegWriterFlush(
  126756                 :   Fts3Table *p,                   /* Virtual table handle */
  126757                 :   SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
  126758                 :   int iLevel,                     /* Value for 'level' column of %_segdir */
  126759                 :   int iIdx                        /* Value for 'idx' column of %_segdir */
  126760                 : ){
  126761                 :   int rc;                         /* Return code */
  126762               4 :   if( pWriter->pTree ){
  126763               0 :     sqlite3_int64 iLast = 0;      /* Largest block id written to database */
  126764                 :     sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
  126765               0 :     char *zRoot = NULL;           /* Pointer to buffer containing root node */
  126766               0 :     int nRoot = 0;                /* Size of buffer zRoot */
  126767                 : 
  126768               0 :     iLastLeaf = pWriter->iFree;
  126769               0 :     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
  126770               0 :     if( rc==SQLITE_OK ){
  126771               0 :       rc = fts3NodeWrite(p, pWriter->pTree, 1,
  126772                 :           pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
  126773                 :     }
  126774               0 :     if( rc==SQLITE_OK ){
  126775               0 :       rc = fts3WriteSegdir(
  126776                 :           p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
  126777                 :     }
  126778                 :   }else{
  126779                 :     /* The entire tree fits on the root node. Write it to the segdir table. */
  126780               4 :     rc = fts3WriteSegdir(
  126781                 :         p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
  126782                 :   }
  126783               4 :   return rc;
  126784                 : }
  126785                 : 
  126786                 : /*
  126787                 : ** Release all memory held by the SegmentWriter object passed as the 
  126788                 : ** first argument.
  126789                 : */
  126790               5 : static void fts3SegWriterFree(SegmentWriter *pWriter){
  126791               5 :   if( pWriter ){
  126792               4 :     sqlite3_free(pWriter->aData);
  126793               4 :     sqlite3_free(pWriter->zMalloc);
  126794               4 :     fts3NodeFree(pWriter->pTree);
  126795               4 :     sqlite3_free(pWriter);
  126796                 :   }
  126797               5 : }
  126798                 : 
  126799                 : /*
  126800                 : ** The first value in the apVal[] array is assumed to contain an integer.
  126801                 : ** This function tests if there exist any documents with docid values that
  126802                 : ** are different from that integer. i.e. if deleting the document with docid
  126803                 : ** pRowid would mean the FTS3 table were empty.
  126804                 : **
  126805                 : ** If successful, *pisEmpty is set to true if the table is empty except for
  126806                 : ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
  126807                 : ** error occurs, an SQLite error code is returned.
  126808                 : */
  126809               0 : static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
  126810                 :   sqlite3_stmt *pStmt;
  126811                 :   int rc;
  126812               0 :   if( p->zContentTbl ){
  126813                 :     /* If using the content=xxx option, assume the table is never empty */
  126814               0 :     *pisEmpty = 0;
  126815               0 :     rc = SQLITE_OK;
  126816                 :   }else{
  126817               0 :     rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
  126818               0 :     if( rc==SQLITE_OK ){
  126819               0 :       if( SQLITE_ROW==sqlite3_step(pStmt) ){
  126820               0 :         *pisEmpty = sqlite3_column_int(pStmt, 0);
  126821                 :       }
  126822               0 :       rc = sqlite3_reset(pStmt);
  126823                 :     }
  126824                 :   }
  126825               0 :   return rc;
  126826                 : }
  126827                 : 
  126828                 : /*
  126829                 : ** Set *pnMax to the largest segment level in the database for the index
  126830                 : ** iIndex.
  126831                 : **
  126832                 : ** Segment levels are stored in the 'level' column of the %_segdir table.
  126833                 : **
  126834                 : ** Return SQLITE_OK if successful, or an SQLite error code if not.
  126835                 : */
  126836               0 : static int fts3SegmentMaxLevel(Fts3Table *p, int iIndex, int *pnMax){
  126837                 :   sqlite3_stmt *pStmt;
  126838                 :   int rc;
  126839               0 :   assert( iIndex>=0 && iIndex<p->nIndex );
  126840                 : 
  126841                 :   /* Set pStmt to the compiled version of:
  126842                 :   **
  126843                 :   **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
  126844                 :   **
  126845                 :   ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
  126846                 :   */
  126847               0 :   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
  126848               0 :   if( rc!=SQLITE_OK ) return rc;
  126849               0 :   sqlite3_bind_int(pStmt, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
  126850               0 :   sqlite3_bind_int(pStmt, 2, (iIndex+1)*FTS3_SEGDIR_MAXLEVEL - 1);
  126851               0 :   if( SQLITE_ROW==sqlite3_step(pStmt) ){
  126852               0 :     *pnMax = sqlite3_column_int(pStmt, 0);
  126853                 :   }
  126854               0 :   return sqlite3_reset(pStmt);
  126855                 : }
  126856                 : 
  126857                 : /*
  126858                 : ** This function is used after merging multiple segments into a single large
  126859                 : ** segment to delete the old, now redundant, segment b-trees. Specifically,
  126860                 : ** it:
  126861                 : ** 
  126862                 : **   1) Deletes all %_segments entries for the segments associated with 
  126863                 : **      each of the SegReader objects in the array passed as the third 
  126864                 : **      argument, and
  126865                 : **
  126866                 : **   2) deletes all %_segdir entries with level iLevel, or all %_segdir
  126867                 : **      entries regardless of level if (iLevel<0).
  126868                 : **
  126869                 : ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
  126870                 : */
  126871               0 : static int fts3DeleteSegdir(
  126872                 :   Fts3Table *p,                   /* Virtual table handle */
  126873                 :   int iIndex,                     /* Index for p->aIndex */
  126874                 :   int iLevel,                     /* Level of %_segdir entries to delete */
  126875                 :   Fts3SegReader **apSegment,      /* Array of SegReader objects */
  126876                 :   int nReader                     /* Size of array apSegment */
  126877                 : ){
  126878                 :   int rc;                         /* Return Code */
  126879                 :   int i;                          /* Iterator variable */
  126880                 :   sqlite3_stmt *pDelete;          /* SQL statement to delete rows */
  126881                 : 
  126882               0 :   rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
  126883               0 :   for(i=0; rc==SQLITE_OK && i<nReader; i++){
  126884               0 :     Fts3SegReader *pSegment = apSegment[i];
  126885               0 :     if( pSegment->iStartBlock ){
  126886               0 :       sqlite3_bind_int64(pDelete, 1, pSegment->iStartBlock);
  126887               0 :       sqlite3_bind_int64(pDelete, 2, pSegment->iEndBlock);
  126888               0 :       sqlite3_step(pDelete);
  126889               0 :       rc = sqlite3_reset(pDelete);
  126890                 :     }
  126891                 :   }
  126892               0 :   if( rc!=SQLITE_OK ){
  126893               0 :     return rc;
  126894                 :   }
  126895                 : 
  126896               0 :   assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
  126897               0 :   if( iLevel==FTS3_SEGCURSOR_ALL ){
  126898               0 :     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
  126899               0 :     if( rc==SQLITE_OK ){
  126900               0 :       sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL);
  126901               0 :       sqlite3_bind_int(pDelete, 2, (iIndex+1) * FTS3_SEGDIR_MAXLEVEL - 1);
  126902                 :     }
  126903                 :   }else{
  126904               0 :     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
  126905               0 :     if( rc==SQLITE_OK ){
  126906               0 :       sqlite3_bind_int(pDelete, 1, iIndex*FTS3_SEGDIR_MAXLEVEL + iLevel);
  126907                 :     }
  126908                 :   }
  126909                 : 
  126910               0 :   if( rc==SQLITE_OK ){
  126911               0 :     sqlite3_step(pDelete);
  126912               0 :     rc = sqlite3_reset(pDelete);
  126913                 :   }
  126914                 : 
  126915               0 :   return rc;
  126916                 : }
  126917                 : 
  126918                 : /*
  126919                 : ** When this function is called, buffer *ppList (size *pnList bytes) contains 
  126920                 : ** a position list that may (or may not) feature multiple columns. This
  126921                 : ** function adjusts the pointer *ppList and the length *pnList so that they
  126922                 : ** identify the subset of the position list that corresponds to column iCol.
  126923                 : **
  126924                 : ** If there are no entries in the input position list for column iCol, then
  126925                 : ** *pnList is set to zero before returning.
  126926                 : */
  126927               2 : static void fts3ColumnFilter(
  126928                 :   int iCol,                       /* Column to filter on */
  126929                 :   char **ppList,                  /* IN/OUT: Pointer to position list */
  126930                 :   int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
  126931                 : ){
  126932               2 :   char *pList = *ppList;
  126933               2 :   int nList = *pnList;
  126934               2 :   char *pEnd = &pList[nList];
  126935               2 :   int iCurrent = 0;
  126936               2 :   char *p = pList;
  126937                 : 
  126938               2 :   assert( iCol>=0 );
  126939                 :   while( 1 ){
  126940               2 :     char c = 0;
  126941               2 :     while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
  126942                 :   
  126943               2 :     if( iCol==iCurrent ){
  126944               2 :       nList = (int)(p - pList);
  126945               2 :       break;
  126946                 :     }
  126947                 : 
  126948               0 :     nList -= (int)(p - pList);
  126949               0 :     pList = p;
  126950               0 :     if( nList==0 ){
  126951               0 :       break;
  126952                 :     }
  126953               0 :     p = &pList[1];
  126954               0 :     p += sqlite3Fts3GetVarint32(p, &iCurrent);
  126955               0 :   }
  126956                 : 
  126957               2 :   *ppList = pList;
  126958               2 :   *pnList = nList;
  126959               2 : }
  126960                 : 
  126961                 : /*
  126962                 : ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
  126963                 : ** existing data). Grow the buffer if required.
  126964                 : **
  126965                 : ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
  126966                 : ** trying to resize the buffer, return SQLITE_NOMEM.
  126967                 : */
  126968              20 : static int fts3MsrBufferData(
  126969                 :   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
  126970                 :   char *pList,
  126971                 :   int nList
  126972                 : ){
  126973              20 :   if( nList>pMsr->nBuffer ){
  126974                 :     char *pNew;
  126975               4 :     pMsr->nBuffer = nList*2;
  126976               4 :     pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
  126977               4 :     if( !pNew ) return SQLITE_NOMEM;
  126978               4 :     pMsr->aBuffer = pNew;
  126979                 :   }
  126980                 : 
  126981              20 :   memcpy(pMsr->aBuffer, pList, nList);
  126982              20 :   return SQLITE_OK;
  126983                 : }
  126984                 : 
  126985               3 : SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
  126986                 :   Fts3Table *p,                   /* Virtual table handle */
  126987                 :   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
  126988                 :   sqlite3_int64 *piDocid,         /* OUT: Docid value */
  126989                 :   char **paPoslist,               /* OUT: Pointer to position list */
  126990                 :   int *pnPoslist                  /* OUT: Size of position list in bytes */
  126991                 : ){
  126992               3 :   int nMerge = pMsr->nAdvance;
  126993               3 :   Fts3SegReader **apSegment = pMsr->apSegment;
  126994               3 :   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
  126995               3 :     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
  126996                 :   );
  126997                 : 
  126998               3 :   if( nMerge==0 ){
  126999               0 :     *paPoslist = 0;
  127000               0 :     return SQLITE_OK;
  127001                 :   }
  127002                 : 
  127003                 :   while( 1 ){
  127004                 :     Fts3SegReader *pSeg;
  127005               3 :     pSeg = pMsr->apSegment[0];
  127006                 : 
  127007               3 :     if( pSeg->pOffsetList==0 ){
  127008               1 :       *paPoslist = 0;
  127009               1 :       break;
  127010                 :     }else{
  127011                 :       int rc;
  127012                 :       char *pList;
  127013                 :       int nList;
  127014                 :       int j;
  127015               2 :       sqlite3_int64 iDocid = apSegment[0]->iDocid;
  127016                 : 
  127017               2 :       rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
  127018               2 :       j = 1;
  127019               4 :       while( rc==SQLITE_OK 
  127020               2 :         && j<nMerge
  127021               2 :         && apSegment[j]->pOffsetList
  127022               1 :         && apSegment[j]->iDocid==iDocid
  127023                 :       ){
  127024               0 :         rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
  127025               0 :         j++;
  127026                 :       }
  127027               2 :       if( rc!=SQLITE_OK ) return rc;
  127028               2 :       fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
  127029                 : 
  127030               2 :       if( pMsr->iColFilter>=0 ){
  127031               2 :         fts3ColumnFilter(pMsr->iColFilter, &pList, &nList);
  127032                 :       }
  127033                 : 
  127034               2 :       if( nList>0 ){
  127035               2 :         if( fts3SegReaderIsPending(apSegment[0]) ){
  127036               0 :           rc = fts3MsrBufferData(pMsr, pList, nList+1);
  127037               0 :           if( rc!=SQLITE_OK ) return rc;
  127038               0 :           *paPoslist = pMsr->aBuffer;
  127039               0 :           assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
  127040                 :         }else{
  127041               2 :           *paPoslist = pList;
  127042                 :         }
  127043               2 :         *piDocid = iDocid;
  127044               2 :         *pnPoslist = nList;
  127045               2 :         break;
  127046                 :       }
  127047                 :     }
  127048               0 :   }
  127049                 : 
  127050               3 :   return SQLITE_OK;
  127051                 : }
  127052                 : 
  127053               5 : static int fts3SegReaderStart(
  127054                 :   Fts3Table *p,                   /* Virtual table handle */
  127055                 :   Fts3MultiSegReader *pCsr,       /* Cursor object */
  127056                 :   const char *zTerm,              /* Term searched for (or NULL) */
  127057                 :   int nTerm                       /* Length of zTerm in bytes */
  127058                 : ){
  127059                 :   int i;
  127060               5 :   int nSeg = pCsr->nSegment;
  127061                 : 
  127062                 :   /* If the Fts3SegFilter defines a specific term (or term prefix) to search 
  127063                 :   ** for, then advance each segment iterator until it points to a term of
  127064                 :   ** equal or greater value than the specified term. This prevents many
  127065                 :   ** unnecessary merge/sort operations for the case where single segment
  127066                 :   ** b-tree leaf nodes contain more than one term.
  127067                 :   */
  127068              13 :   for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
  127069               8 :     Fts3SegReader *pSeg = pCsr->apSegment[i];
  127070                 :     do {
  127071              20 :       int rc = fts3SegReaderNext(p, pSeg, 0);
  127072              20 :       if( rc!=SQLITE_OK ) return rc;
  127073              20 :     }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 );
  127074                 :   }
  127075               5 :   fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
  127076                 : 
  127077               5 :   return SQLITE_OK;
  127078                 : }
  127079                 : 
  127080               4 : SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
  127081                 :   Fts3Table *p,                   /* Virtual table handle */
  127082                 :   Fts3MultiSegReader *pCsr,       /* Cursor object */
  127083                 :   Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
  127084                 : ){
  127085               4 :   pCsr->pFilter = pFilter;
  127086               4 :   return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
  127087                 : }
  127088                 : 
  127089               1 : SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
  127090                 :   Fts3Table *p,                   /* Virtual table handle */
  127091                 :   Fts3MultiSegReader *pCsr,       /* Cursor object */
  127092                 :   int iCol,                       /* Column to match on. */
  127093                 :   const char *zTerm,              /* Term to iterate through a doclist for */
  127094                 :   int nTerm                       /* Number of bytes in zTerm */
  127095                 : ){
  127096                 :   int i;
  127097                 :   int rc;
  127098               1 :   int nSegment = pCsr->nSegment;
  127099               1 :   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
  127100               1 :     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
  127101                 :   );
  127102                 : 
  127103               1 :   assert( pCsr->pFilter==0 );
  127104               1 :   assert( zTerm && nTerm>0 );
  127105                 : 
  127106                 :   /* Advance each segment iterator until it points to the term zTerm/nTerm. */
  127107               1 :   rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
  127108               1 :   if( rc!=SQLITE_OK ) return rc;
  127109                 : 
  127110                 :   /* Determine how many of the segments actually point to zTerm/nTerm. */
  127111               3 :   for(i=0; i<nSegment; i++){
  127112               3 :     Fts3SegReader *pSeg = pCsr->apSegment[i];
  127113               3 :     if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
  127114                 :       break;
  127115                 :     }
  127116                 :   }
  127117               1 :   pCsr->nAdvance = i;
  127118                 : 
  127119                 :   /* Advance each of the segments to point to the first docid. */
  127120               3 :   for(i=0; i<pCsr->nAdvance; i++){
  127121               2 :     rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
  127122               2 :     if( rc!=SQLITE_OK ) return rc;
  127123                 :   }
  127124               1 :   fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
  127125                 : 
  127126               1 :   assert( iCol<0 || iCol<p->nColumn );
  127127               1 :   pCsr->iColFilter = iCol;
  127128                 : 
  127129               1 :   return SQLITE_OK;
  127130                 : }
  127131                 : 
  127132                 : /*
  127133                 : ** This function is called on a MultiSegReader that has been started using
  127134                 : ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
  127135                 : ** have been made. Calling this function puts the MultiSegReader in such
  127136                 : ** a state that if the next two calls are:
  127137                 : **
  127138                 : **   sqlite3Fts3SegReaderStart()
  127139                 : **   sqlite3Fts3SegReaderStep()
  127140                 : **
  127141                 : ** then the entire doclist for the term is available in 
  127142                 : ** MultiSegReader.aDoclist/nDoclist.
  127143                 : */
  127144               0 : SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
  127145                 :   int i;                          /* Used to iterate through segment-readers */
  127146                 : 
  127147               0 :   assert( pCsr->zTerm==0 );
  127148               0 :   assert( pCsr->nTerm==0 );
  127149               0 :   assert( pCsr->aDoclist==0 );
  127150               0 :   assert( pCsr->nDoclist==0 );
  127151                 : 
  127152               0 :   pCsr->nAdvance = 0;
  127153               0 :   pCsr->bRestart = 1;
  127154               0 :   for(i=0; i<pCsr->nSegment; i++){
  127155               0 :     pCsr->apSegment[i]->pOffsetList = 0;
  127156               0 :     pCsr->apSegment[i]->nOffsetList = 0;
  127157               0 :     pCsr->apSegment[i]->iDocid = 0;
  127158                 :   }
  127159                 : 
  127160               0 :   return SQLITE_OK;
  127161                 : }
  127162                 : 
  127163                 : 
  127164              24 : SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
  127165                 :   Fts3Table *p,                   /* Virtual table handle */
  127166                 :   Fts3MultiSegReader *pCsr        /* Cursor object */
  127167                 : ){
  127168              24 :   int rc = SQLITE_OK;
  127169                 : 
  127170              24 :   int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
  127171              24 :   int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
  127172              24 :   int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
  127173              24 :   int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
  127174              24 :   int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
  127175              24 :   int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
  127176                 : 
  127177              24 :   Fts3SegReader **apSegment = pCsr->apSegment;
  127178              24 :   int nSegment = pCsr->nSegment;
  127179              24 :   Fts3SegFilter *pFilter = pCsr->pFilter;
  127180              24 :   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
  127181              24 :     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
  127182                 :   );
  127183                 : 
  127184              24 :   if( pCsr->nSegment==0 ) return SQLITE_OK;
  127185                 : 
  127186                 :   do {
  127187                 :     int nMerge;
  127188                 :     int i;
  127189                 :   
  127190                 :     /* Advance the first pCsr->nAdvance entries in the apSegment[] array
  127191                 :     ** forward. Then sort the list in order of current term again.  
  127192                 :     */
  127193              44 :     for(i=0; i<pCsr->nAdvance; i++){
  127194              20 :       rc = fts3SegReaderNext(p, apSegment[i], 0);
  127195              20 :       if( rc!=SQLITE_OK ) return rc;
  127196                 :     }
  127197              24 :     fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
  127198              24 :     pCsr->nAdvance = 0;
  127199                 : 
  127200                 :     /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
  127201              24 :     assert( rc==SQLITE_OK );
  127202              24 :     if( apSegment[0]->aNode==0 ) break;
  127203                 : 
  127204              20 :     pCsr->nTerm = apSegment[0]->nTerm;
  127205              20 :     pCsr->zTerm = apSegment[0]->zTerm;
  127206                 : 
  127207                 :     /* If this is a prefix-search, and if the term that apSegment[0] points
  127208                 :     ** to does not share a suffix with pFilter->zTerm/nTerm, then all 
  127209                 :     ** required callbacks have been made. In this case exit early.
  127210                 :     **
  127211                 :     ** Similarly, if this is a search for an exact match, and the first term
  127212                 :     ** of segment apSegment[0] is not a match, exit early.
  127213                 :     */
  127214              20 :     if( pFilter->zTerm && !isScan ){
  127215               0 :       if( pCsr->nTerm<pFilter->nTerm 
  127216               0 :        || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
  127217               0 :        || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) 
  127218                 :       ){
  127219                 :         break;
  127220                 :       }
  127221                 :     }
  127222                 : 
  127223              20 :     nMerge = 1;
  127224              40 :     while( nMerge<nSegment 
  127225               0 :         && apSegment[nMerge]->aNode
  127226               0 :         && apSegment[nMerge]->nTerm==pCsr->nTerm 
  127227               0 :         && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
  127228                 :     ){
  127229               0 :       nMerge++;
  127230                 :     }
  127231                 : 
  127232              20 :     assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
  127233              20 :     if( nMerge==1 
  127234              20 :      && !isIgnoreEmpty 
  127235              20 :      && !isFirst 
  127236              20 :      && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
  127237                 :     ){
  127238              20 :       pCsr->nDoclist = apSegment[0]->nDoclist;
  127239              20 :       if( fts3SegReaderIsPending(apSegment[0]) ){
  127240              20 :         rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
  127241              20 :         pCsr->aDoclist = pCsr->aBuffer;
  127242                 :       }else{
  127243               0 :         pCsr->aDoclist = apSegment[0]->aDoclist;
  127244                 :       }
  127245              20 :       if( rc==SQLITE_OK ) rc = SQLITE_ROW;
  127246                 :     }else{
  127247               0 :       int nDoclist = 0;           /* Size of doclist */
  127248               0 :       sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
  127249                 : 
  127250                 :       /* The current term of the first nMerge entries in the array
  127251                 :       ** of Fts3SegReader objects is the same. The doclists must be merged
  127252                 :       ** and a single term returned with the merged doclist.
  127253                 :       */
  127254               0 :       for(i=0; i<nMerge; i++){
  127255               0 :         fts3SegReaderFirstDocid(p, apSegment[i]);
  127256                 :       }
  127257               0 :       fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
  127258               0 :       while( apSegment[0]->pOffsetList ){
  127259                 :         int j;                    /* Number of segments that share a docid */
  127260                 :         char *pList;
  127261                 :         int nList;
  127262                 :         int nByte;
  127263               0 :         sqlite3_int64 iDocid = apSegment[0]->iDocid;
  127264               0 :         fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
  127265               0 :         j = 1;
  127266               0 :         while( j<nMerge
  127267               0 :             && apSegment[j]->pOffsetList
  127268               0 :             && apSegment[j]->iDocid==iDocid
  127269                 :         ){
  127270               0 :           fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
  127271               0 :           j++;
  127272                 :         }
  127273                 : 
  127274               0 :         if( isColFilter ){
  127275               0 :           fts3ColumnFilter(pFilter->iCol, &pList, &nList);
  127276                 :         }
  127277                 : 
  127278               0 :         if( !isIgnoreEmpty || nList>0 ){
  127279                 : 
  127280                 :           /* Calculate the 'docid' delta value to write into the merged 
  127281                 :           ** doclist. */
  127282                 :           sqlite3_int64 iDelta;
  127283               0 :           if( p->bDescIdx && nDoclist>0 ){
  127284               0 :             iDelta = iPrev - iDocid;
  127285                 :           }else{
  127286               0 :             iDelta = iDocid - iPrev;
  127287                 :           }
  127288               0 :           assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
  127289               0 :           assert( nDoclist>0 || iDelta==iDocid );
  127290                 : 
  127291               0 :           nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
  127292               0 :           if( nDoclist+nByte>pCsr->nBuffer ){
  127293                 :             char *aNew;
  127294               0 :             pCsr->nBuffer = (nDoclist+nByte)*2;
  127295               0 :             aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
  127296               0 :             if( !aNew ){
  127297               0 :               return SQLITE_NOMEM;
  127298                 :             }
  127299               0 :             pCsr->aBuffer = aNew;
  127300                 :           }
  127301                 : 
  127302               0 :           if( isFirst ){
  127303               0 :             char *a = &pCsr->aBuffer[nDoclist];
  127304                 :             int nWrite;
  127305                 :            
  127306               0 :             nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
  127307               0 :             if( nWrite ){
  127308               0 :               iPrev = iDocid;
  127309               0 :               nDoclist += nWrite;
  127310                 :             }
  127311                 :           }else{
  127312               0 :             nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
  127313               0 :             iPrev = iDocid;
  127314               0 :             if( isRequirePos ){
  127315               0 :               memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
  127316               0 :               nDoclist += nList;
  127317               0 :               pCsr->aBuffer[nDoclist++] = '\0';
  127318                 :             }
  127319                 :           }
  127320                 :         }
  127321                 : 
  127322               0 :         fts3SegReaderSort(apSegment, nMerge, j, xCmp);
  127323                 :       }
  127324               0 :       if( nDoclist>0 ){
  127325               0 :         pCsr->aDoclist = pCsr->aBuffer;
  127326               0 :         pCsr->nDoclist = nDoclist;
  127327               0 :         rc = SQLITE_ROW;
  127328                 :       }
  127329                 :     }
  127330              20 :     pCsr->nAdvance = nMerge;
  127331              20 :   }while( rc==SQLITE_OK );
  127332                 : 
  127333              24 :   return rc;
  127334                 : }
  127335                 : 
  127336                 : 
  127337               6 : SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
  127338                 :   Fts3MultiSegReader *pCsr       /* Cursor object */
  127339                 : ){
  127340               6 :   if( pCsr ){
  127341                 :     int i;
  127342              14 :     for(i=0; i<pCsr->nSegment; i++){
  127343               8 :       sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
  127344                 :     }
  127345               6 :     sqlite3_free(pCsr->apSegment);
  127346               6 :     sqlite3_free(pCsr->aBuffer);
  127347                 : 
  127348               6 :     pCsr->nSegment = 0;
  127349               6 :     pCsr->apSegment = 0;
  127350               6 :     pCsr->aBuffer = 0;
  127351                 :   }
  127352               6 : }
  127353                 : 
  127354                 : /*
  127355                 : ** Merge all level iLevel segments in the database into a single 
  127356                 : ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
  127357                 : ** single segment with a level equal to the numerically largest level 
  127358                 : ** currently present in the database.
  127359                 : **
  127360                 : ** If this function is called with iLevel<0, but there is only one
  127361                 : ** segment in the database, SQLITE_DONE is returned immediately. 
  127362                 : ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, 
  127363                 : ** an SQLite error code is returned.
  127364                 : */
  127365               5 : static int fts3SegmentMerge(Fts3Table *p, int iIndex, int iLevel){
  127366                 :   int rc;                         /* Return code */
  127367               5 :   int iIdx = 0;                   /* Index of new segment */
  127368               5 :   int iNewLevel = 0;              /* Level/index to create new segment at */
  127369               5 :   SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
  127370                 :   Fts3SegFilter filter;           /* Segment term filter condition */
  127371                 :   Fts3MultiSegReader csr;        /* Cursor to iterate through level(s) */
  127372               5 :   int bIgnoreEmpty = 0;           /* True to ignore empty segments */
  127373                 : 
  127374               5 :   assert( iLevel==FTS3_SEGCURSOR_ALL
  127375                 :        || iLevel==FTS3_SEGCURSOR_PENDING
  127376                 :        || iLevel>=0
  127377                 :   );
  127378               5 :   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
  127379               5 :   assert( iIndex>=0 && iIndex<p->nIndex );
  127380                 : 
  127381               5 :   rc = sqlite3Fts3SegReaderCursor(p, iIndex, iLevel, 0, 0, 1, 0, &csr);
  127382               5 :   if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
  127383                 : 
  127384               4 :   if( iLevel==FTS3_SEGCURSOR_ALL ){
  127385                 :     /* This call is to merge all segments in the database to a single
  127386                 :     ** segment. The level of the new segment is equal to the the numerically 
  127387                 :     ** greatest segment level currently present in the database for this
  127388                 :     ** index. The idx of the new segment is always 0.  */
  127389               0 :     if( csr.nSegment==1 ){
  127390               0 :       rc = SQLITE_DONE;
  127391               0 :       goto finished;
  127392                 :     }
  127393               0 :     rc = fts3SegmentMaxLevel(p, iIndex, &iNewLevel);
  127394               0 :     bIgnoreEmpty = 1;
  127395                 : 
  127396               4 :   }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
  127397               4 :     iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL; 
  127398               4 :     rc = fts3AllocateSegdirIdx(p, iIndex, 0, &iIdx);
  127399                 :   }else{
  127400                 :     /* This call is to merge all segments at level iLevel. find the next
  127401                 :     ** available segment index at level iLevel+1. The call to
  127402                 :     ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to 
  127403                 :     ** a single iLevel+2 segment if necessary.  */
  127404               0 :     rc = fts3AllocateSegdirIdx(p, iIndex, iLevel+1, &iIdx);
  127405               0 :     iNewLevel = iIndex * FTS3_SEGDIR_MAXLEVEL + iLevel+1;
  127406                 :   }
  127407               4 :   if( rc!=SQLITE_OK ) goto finished;
  127408               4 :   assert( csr.nSegment>0 );
  127409               4 :   assert( iNewLevel>=(iIndex*FTS3_SEGDIR_MAXLEVEL) );
  127410               4 :   assert( iNewLevel<((iIndex+1)*FTS3_SEGDIR_MAXLEVEL) );
  127411                 : 
  127412               4 :   memset(&filter, 0, sizeof(Fts3SegFilter));
  127413               4 :   filter.flags = FTS3_SEGMENT_REQUIRE_POS;
  127414               4 :   filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
  127415                 : 
  127416               4 :   rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
  127417              28 :   while( SQLITE_OK==rc ){
  127418              24 :     rc = sqlite3Fts3SegReaderStep(p, &csr);
  127419              24 :     if( rc!=SQLITE_ROW ) break;
  127420              60 :     rc = fts3SegWriterAdd(p, &pWriter, 1, 
  127421              40 :         csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
  127422                 :   }
  127423               4 :   if( rc!=SQLITE_OK ) goto finished;
  127424               4 :   assert( pWriter );
  127425                 : 
  127426               4 :   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
  127427               0 :     rc = fts3DeleteSegdir(p, iIndex, iLevel, csr.apSegment, csr.nSegment);
  127428               0 :     if( rc!=SQLITE_OK ) goto finished;
  127429                 :   }
  127430               4 :   rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
  127431                 : 
  127432                 :  finished:
  127433               5 :   fts3SegWriterFree(pWriter);
  127434               5 :   sqlite3Fts3SegReaderFinish(&csr);
  127435               5 :   return rc;
  127436                 : }
  127437                 : 
  127438                 : 
  127439                 : /* 
  127440                 : ** Flush the contents of pendingTerms to level 0 segments.
  127441                 : */
  127442               5 : SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
  127443               5 :   int rc = SQLITE_OK;
  127444                 :   int i;
  127445              10 :   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
  127446               5 :     rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_PENDING);
  127447               5 :     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
  127448                 :   }
  127449               5 :   sqlite3Fts3PendingTermsClear(p);
  127450               5 :   return rc;
  127451                 : }
  127452                 : 
  127453                 : /*
  127454                 : ** Encode N integers as varints into a blob.
  127455                 : */
  127456               0 : static void fts3EncodeIntArray(
  127457                 :   int N,             /* The number of integers to encode */
  127458                 :   u32 *a,            /* The integer values */
  127459                 :   char *zBuf,        /* Write the BLOB here */
  127460                 :   int *pNBuf         /* Write number of bytes if zBuf[] used here */
  127461                 : ){
  127462                 :   int i, j;
  127463               0 :   for(i=j=0; i<N; i++){
  127464               0 :     j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
  127465                 :   }
  127466               0 :   *pNBuf = j;
  127467               0 : }
  127468                 : 
  127469                 : /*
  127470                 : ** Decode a blob of varints into N integers
  127471                 : */
  127472               0 : static void fts3DecodeIntArray(
  127473                 :   int N,             /* The number of integers to decode */
  127474                 :   u32 *a,            /* Write the integer values */
  127475                 :   const char *zBuf,  /* The BLOB containing the varints */
  127476                 :   int nBuf           /* size of the BLOB */
  127477                 : ){
  127478                 :   int i, j;
  127479                 :   UNUSED_PARAMETER(nBuf);
  127480               0 :   for(i=j=0; i<N; i++){
  127481                 :     sqlite3_int64 x;
  127482               0 :     j += sqlite3Fts3GetVarint(&zBuf[j], &x);
  127483               0 :     assert(j<=nBuf);
  127484               0 :     a[i] = (u32)(x & 0xffffffff);
  127485                 :   }
  127486               0 : }
  127487                 : 
  127488                 : /*
  127489                 : ** Insert the sizes (in tokens) for each column of the document
  127490                 : ** with docid equal to p->iPrevDocid.  The sizes are encoded as
  127491                 : ** a blob of varints.
  127492                 : */
  127493               0 : static void fts3InsertDocsize(
  127494                 :   int *pRC,                       /* Result code */
  127495                 :   Fts3Table *p,                   /* Table into which to insert */
  127496                 :   u32 *aSz                        /* Sizes of each column, in tokens */
  127497                 : ){
  127498                 :   char *pBlob;             /* The BLOB encoding of the document size */
  127499                 :   int nBlob;               /* Number of bytes in the BLOB */
  127500                 :   sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
  127501                 :   int rc;                  /* Result code from subfunctions */
  127502                 : 
  127503               0 :   if( *pRC ) return;
  127504               0 :   pBlob = sqlite3_malloc( 10*p->nColumn );
  127505               0 :   if( pBlob==0 ){
  127506               0 :     *pRC = SQLITE_NOMEM;
  127507               0 :     return;
  127508                 :   }
  127509               0 :   fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
  127510               0 :   rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
  127511               0 :   if( rc ){
  127512               0 :     sqlite3_free(pBlob);
  127513               0 :     *pRC = rc;
  127514               0 :     return;
  127515                 :   }
  127516               0 :   sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
  127517               0 :   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
  127518               0 :   sqlite3_step(pStmt);
  127519               0 :   *pRC = sqlite3_reset(pStmt);
  127520                 : }
  127521                 : 
  127522                 : /*
  127523                 : ** Record 0 of the %_stat table contains a blob consisting of N varints,
  127524                 : ** where N is the number of user defined columns in the fts3 table plus
  127525                 : ** two. If nCol is the number of user defined columns, then values of the 
  127526                 : ** varints are set as follows:
  127527                 : **
  127528                 : **   Varint 0:       Total number of rows in the table.
  127529                 : **
  127530                 : **   Varint 1..nCol: For each column, the total number of tokens stored in
  127531                 : **                   the column for all rows of the table.
  127532                 : **
  127533                 : **   Varint 1+nCol:  The total size, in bytes, of all text values in all
  127534                 : **                   columns of all rows of the table.
  127535                 : **
  127536                 : */
  127537               0 : static void fts3UpdateDocTotals(
  127538                 :   int *pRC,                       /* The result code */
  127539                 :   Fts3Table *p,                   /* Table being updated */
  127540                 :   u32 *aSzIns,                    /* Size increases */
  127541                 :   u32 *aSzDel,                    /* Size decreases */
  127542                 :   int nChng                       /* Change in the number of documents */
  127543                 : ){
  127544                 :   char *pBlob;             /* Storage for BLOB written into %_stat */
  127545                 :   int nBlob;               /* Size of BLOB written into %_stat */
  127546                 :   u32 *a;                  /* Array of integers that becomes the BLOB */
  127547                 :   sqlite3_stmt *pStmt;     /* Statement for reading and writing */
  127548                 :   int i;                   /* Loop counter */
  127549                 :   int rc;                  /* Result code from subfunctions */
  127550                 : 
  127551               0 :   const int nStat = p->nColumn+2;
  127552                 : 
  127553               0 :   if( *pRC ) return;
  127554               0 :   a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
  127555               0 :   if( a==0 ){
  127556               0 :     *pRC = SQLITE_NOMEM;
  127557               0 :     return;
  127558                 :   }
  127559               0 :   pBlob = (char*)&a[nStat];
  127560               0 :   rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0);
  127561               0 :   if( rc ){
  127562               0 :     sqlite3_free(a);
  127563               0 :     *pRC = rc;
  127564               0 :     return;
  127565                 :   }
  127566               0 :   if( sqlite3_step(pStmt)==SQLITE_ROW ){
  127567               0 :     fts3DecodeIntArray(nStat, a,
  127568               0 :          sqlite3_column_blob(pStmt, 0),
  127569                 :          sqlite3_column_bytes(pStmt, 0));
  127570                 :   }else{
  127571               0 :     memset(a, 0, sizeof(u32)*(nStat) );
  127572                 :   }
  127573               0 :   sqlite3_reset(pStmt);
  127574               0 :   if( nChng<0 && a[0]<(u32)(-nChng) ){
  127575               0 :     a[0] = 0;
  127576                 :   }else{
  127577               0 :     a[0] += nChng;
  127578                 :   }
  127579               0 :   for(i=0; i<p->nColumn+1; i++){
  127580               0 :     u32 x = a[i+1];
  127581               0 :     if( x+aSzIns[i] < aSzDel[i] ){
  127582               0 :       x = 0;
  127583                 :     }else{
  127584               0 :       x = x + aSzIns[i] - aSzDel[i];
  127585                 :     }
  127586               0 :     a[i+1] = x;
  127587                 :   }
  127588               0 :   fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
  127589               0 :   rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0);
  127590               0 :   if( rc ){
  127591               0 :     sqlite3_free(a);
  127592               0 :     *pRC = rc;
  127593               0 :     return;
  127594                 :   }
  127595               0 :   sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC);
  127596               0 :   sqlite3_step(pStmt);
  127597               0 :   *pRC = sqlite3_reset(pStmt);
  127598               0 :   sqlite3_free(a);
  127599                 : }
  127600                 : 
  127601               0 : static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
  127602                 :   int i;
  127603               0 :   int bSeenDone = 0;
  127604               0 :   int rc = SQLITE_OK;
  127605               0 :   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
  127606               0 :     rc = fts3SegmentMerge(p, i, FTS3_SEGCURSOR_ALL);
  127607               0 :     if( rc==SQLITE_DONE ){
  127608               0 :       bSeenDone = 1;
  127609               0 :       rc = SQLITE_OK;
  127610                 :     }
  127611                 :   }
  127612               0 :   sqlite3Fts3SegmentsClose(p);
  127613               0 :   sqlite3Fts3PendingTermsClear(p);
  127614                 : 
  127615               0 :   return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
  127616                 : }
  127617                 : 
  127618                 : /*
  127619                 : ** This function is called when the user executes the following statement:
  127620                 : **
  127621                 : **     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
  127622                 : **
  127623                 : ** The entire FTS index is discarded and rebuilt. If the table is one 
  127624                 : ** created using the content=xxx option, then the new index is based on
  127625                 : ** the current contents of the xxx table. Otherwise, it is rebuilt based
  127626                 : ** on the contents of the %_content table.
  127627                 : */
  127628               0 : static int fts3DoRebuild(Fts3Table *p){
  127629                 :   int rc;                         /* Return Code */
  127630                 : 
  127631               0 :   rc = fts3DeleteAll(p, 0);
  127632               0 :   if( rc==SQLITE_OK ){
  127633               0 :     u32 *aSz = 0;
  127634               0 :     u32 *aSzIns = 0;
  127635               0 :     u32 *aSzDel = 0;
  127636               0 :     sqlite3_stmt *pStmt = 0;
  127637               0 :     int nEntry = 0;
  127638                 : 
  127639                 :     /* Compose and prepare an SQL statement to loop through the content table */
  127640               0 :     char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
  127641               0 :     if( !zSql ){
  127642               0 :       rc = SQLITE_NOMEM;
  127643                 :     }else{
  127644               0 :       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
  127645               0 :       sqlite3_free(zSql);
  127646                 :     }
  127647                 : 
  127648               0 :     if( rc==SQLITE_OK ){
  127649               0 :       int nByte = sizeof(u32) * (p->nColumn+1)*3;
  127650               0 :       aSz = (u32 *)sqlite3_malloc(nByte);
  127651               0 :       if( aSz==0 ){
  127652               0 :         rc = SQLITE_NOMEM;
  127653                 :       }else{
  127654               0 :         memset(aSz, 0, nByte);
  127655               0 :         aSzIns = &aSz[p->nColumn+1];
  127656               0 :         aSzDel = &aSzIns[p->nColumn+1];
  127657                 :       }
  127658                 :     }
  127659                 : 
  127660               0 :     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  127661                 :       int iCol;
  127662               0 :       rc = fts3PendingTermsDocid(p, sqlite3_column_int64(pStmt, 0));
  127663               0 :       aSz[p->nColumn] = 0;
  127664               0 :       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
  127665               0 :         const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
  127666               0 :         rc = fts3PendingTermsAdd(p, z, iCol, &aSz[iCol]);
  127667               0 :         aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
  127668                 :       }
  127669               0 :       if( p->bHasDocsize ){
  127670               0 :         fts3InsertDocsize(&rc, p, aSz);
  127671                 :       }
  127672               0 :       if( rc!=SQLITE_OK ){
  127673               0 :         sqlite3_finalize(pStmt);
  127674               0 :         pStmt = 0;
  127675                 :       }else{
  127676               0 :         nEntry++;
  127677               0 :         for(iCol=0; iCol<=p->nColumn; iCol++){
  127678               0 :           aSzIns[iCol] += aSz[iCol];
  127679                 :         }
  127680                 :       }
  127681                 :     }
  127682               0 :     if( p->bHasStat ){
  127683               0 :       fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
  127684                 :     }
  127685               0 :     sqlite3_free(aSz);
  127686                 : 
  127687               0 :     if( pStmt ){
  127688               0 :       int rc2 = sqlite3_finalize(pStmt);
  127689               0 :       if( rc==SQLITE_OK ){
  127690               0 :         rc = rc2;
  127691                 :       }
  127692                 :     }
  127693                 :   }
  127694                 : 
  127695               0 :   return rc;
  127696                 : }
  127697                 : 
  127698                 : /*
  127699                 : ** Handle a 'special' INSERT of the form:
  127700                 : **
  127701                 : **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
  127702                 : **
  127703                 : ** Argument pVal contains the result of <expr>. Currently the only 
  127704                 : ** meaningful value to insert is the text 'optimize'.
  127705                 : */
  127706               0 : static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
  127707                 :   int rc;                         /* Return Code */
  127708               0 :   const char *zVal = (const char *)sqlite3_value_text(pVal);
  127709               0 :   int nVal = sqlite3_value_bytes(pVal);
  127710                 : 
  127711               0 :   if( !zVal ){
  127712               0 :     return SQLITE_NOMEM;
  127713               0 :   }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
  127714               0 :     rc = fts3DoOptimize(p, 0);
  127715               0 :   }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
  127716               0 :     rc = fts3DoRebuild(p);
  127717                 : #ifdef SQLITE_TEST
  127718                 :   }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
  127719                 :     p->nNodeSize = atoi(&zVal[9]);
  127720                 :     rc = SQLITE_OK;
  127721                 :   }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
  127722                 :     p->nMaxPendingData = atoi(&zVal[11]);
  127723                 :     rc = SQLITE_OK;
  127724                 : #endif
  127725                 :   }else{
  127726               0 :     rc = SQLITE_ERROR;
  127727                 :   }
  127728                 : 
  127729               0 :   return rc;
  127730                 : }
  127731                 : 
  127732                 : /*
  127733                 : ** Delete all cached deferred doclists. Deferred doclists are cached
  127734                 : ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
  127735                 : */
  127736               2 : SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
  127737                 :   Fts3DeferredToken *pDef;
  127738               2 :   for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
  127739               0 :     fts3PendingListDelete(pDef->pList);
  127740               0 :     pDef->pList = 0;
  127741                 :   }
  127742               2 : }
  127743                 : 
  127744                 : /*
  127745                 : ** Free all entries in the pCsr->pDeffered list. Entries are added to 
  127746                 : ** this list using sqlite3Fts3DeferToken().
  127747                 : */
  127748               2 : SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
  127749                 :   Fts3DeferredToken *pDef;
  127750                 :   Fts3DeferredToken *pNext;
  127751               2 :   for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
  127752               0 :     pNext = pDef->pNext;
  127753               0 :     fts3PendingListDelete(pDef->pList);
  127754               0 :     sqlite3_free(pDef);
  127755                 :   }
  127756               2 :   pCsr->pDeferred = 0;
  127757               2 : }
  127758                 : 
  127759                 : /*
  127760                 : ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
  127761                 : ** based on the row that pCsr currently points to.
  127762                 : **
  127763                 : ** A deferred-doclist is like any other doclist with position information
  127764                 : ** included, except that it only contains entries for a single row of the
  127765                 : ** table, not for all rows.
  127766                 : */
  127767               0 : SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
  127768               0 :   int rc = SQLITE_OK;             /* Return code */
  127769               0 :   if( pCsr->pDeferred ){
  127770                 :     int i;                        /* Used to iterate through table columns */
  127771                 :     sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
  127772                 :     Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
  127773                 :   
  127774               0 :     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  127775               0 :     sqlite3_tokenizer *pT = p->pTokenizer;
  127776               0 :     sqlite3_tokenizer_module const *pModule = pT->pModule;
  127777                 :    
  127778               0 :     assert( pCsr->isRequireSeek==0 );
  127779               0 :     iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
  127780                 :   
  127781               0 :     for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
  127782               0 :       const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
  127783               0 :       sqlite3_tokenizer_cursor *pTC = 0;
  127784                 :   
  127785               0 :       rc = pModule->xOpen(pT, zText, -1, &pTC);
  127786               0 :       while( rc==SQLITE_OK ){
  127787                 :         char const *zToken;       /* Buffer containing token */
  127788                 :         int nToken;               /* Number of bytes in token */
  127789                 :         int iDum1, iDum2;         /* Dummy variables */
  127790                 :         int iPos;                 /* Position of token in zText */
  127791                 :   
  127792               0 :         pTC->pTokenizer = pT;
  127793               0 :         rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
  127794               0 :         for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
  127795               0 :           Fts3PhraseToken *pPT = pDef->pToken;
  127796               0 :           if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
  127797               0 :            && (pPT->bFirst==0 || iPos==0)
  127798               0 :            && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
  127799               0 :            && (0==memcmp(zToken, pPT->z, pPT->n))
  127800                 :           ){
  127801               0 :             fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
  127802                 :           }
  127803                 :         }
  127804                 :       }
  127805               0 :       if( pTC ) pModule->xClose(pTC);
  127806               0 :       if( rc==SQLITE_DONE ) rc = SQLITE_OK;
  127807                 :     }
  127808                 :   
  127809               0 :     for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
  127810               0 :       if( pDef->pList ){
  127811               0 :         rc = fts3PendingListAppendVarint(&pDef->pList, 0);
  127812                 :       }
  127813                 :     }
  127814                 :   }
  127815                 : 
  127816               0 :   return rc;
  127817                 : }
  127818                 : 
  127819               0 : SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
  127820                 :   Fts3DeferredToken *p, 
  127821                 :   char **ppData, 
  127822                 :   int *pnData
  127823                 : ){
  127824                 :   char *pRet;
  127825                 :   int nSkip;
  127826                 :   sqlite3_int64 dummy;
  127827                 : 
  127828               0 :   *ppData = 0;
  127829               0 :   *pnData = 0;
  127830                 : 
  127831               0 :   if( p->pList==0 ){
  127832               0 :     return SQLITE_OK;
  127833                 :   }
  127834                 : 
  127835               0 :   pRet = (char *)sqlite3_malloc(p->pList->nData);
  127836               0 :   if( !pRet ) return SQLITE_NOMEM;
  127837                 : 
  127838               0 :   nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
  127839               0 :   *pnData = p->pList->nData - nSkip;
  127840               0 :   *ppData = pRet;
  127841                 :   
  127842               0 :   memcpy(pRet, &p->pList->aData[nSkip], *pnData);
  127843               0 :   return SQLITE_OK;
  127844                 : }
  127845                 : 
  127846                 : /*
  127847                 : ** Add an entry for token pToken to the pCsr->pDeferred list.
  127848                 : */
  127849               0 : SQLITE_PRIVATE int sqlite3Fts3DeferToken(
  127850                 :   Fts3Cursor *pCsr,               /* Fts3 table cursor */
  127851                 :   Fts3PhraseToken *pToken,        /* Token to defer */
  127852                 :   int iCol                        /* Column that token must appear in (or -1) */
  127853                 : ){
  127854                 :   Fts3DeferredToken *pDeferred;
  127855               0 :   pDeferred = sqlite3_malloc(sizeof(*pDeferred));
  127856               0 :   if( !pDeferred ){
  127857               0 :     return SQLITE_NOMEM;
  127858                 :   }
  127859               0 :   memset(pDeferred, 0, sizeof(*pDeferred));
  127860               0 :   pDeferred->pToken = pToken;
  127861               0 :   pDeferred->pNext = pCsr->pDeferred; 
  127862               0 :   pDeferred->iCol = iCol;
  127863               0 :   pCsr->pDeferred = pDeferred;
  127864                 : 
  127865               0 :   assert( pToken->pDeferred==0 );
  127866               0 :   pToken->pDeferred = pDeferred;
  127867                 : 
  127868               0 :   return SQLITE_OK;
  127869                 : }
  127870                 : 
  127871                 : /*
  127872                 : ** SQLite value pRowid contains the rowid of a row that may or may not be
  127873                 : ** present in the FTS3 table. If it is, delete it and adjust the contents
  127874                 : ** of subsiduary data structures accordingly.
  127875                 : */
  127876               0 : static int fts3DeleteByRowid(
  127877                 :   Fts3Table *p, 
  127878                 :   sqlite3_value *pRowid, 
  127879                 :   int *pnDoc,
  127880                 :   u32 *aSzDel
  127881                 : ){
  127882               0 :   int isEmpty = 0;
  127883               0 :   int rc = fts3IsEmpty(p, pRowid, &isEmpty);
  127884               0 :   if( rc==SQLITE_OK ){
  127885               0 :     if( isEmpty ){
  127886                 :       /* Deleting this row means the whole table is empty. In this case
  127887                 :       ** delete the contents of all three tables and throw away any
  127888                 :       ** data in the pendingTerms hash table.  */
  127889               0 :       rc = fts3DeleteAll(p, 1);
  127890               0 :       *pnDoc = *pnDoc - 1;
  127891                 :     }else{
  127892               0 :       sqlite3_int64 iRemove = sqlite3_value_int64(pRowid);
  127893               0 :       rc = fts3PendingTermsDocid(p, iRemove);
  127894               0 :       fts3DeleteTerms(&rc, p, pRowid, aSzDel);
  127895               0 :       if( p->zContentTbl==0 ){
  127896               0 :         fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
  127897               0 :         if( sqlite3_changes(p->db) ) *pnDoc = *pnDoc - 1;
  127898                 :       }else{
  127899               0 :         *pnDoc = *pnDoc - 1;
  127900                 :       }
  127901               0 :       if( p->bHasDocsize ){
  127902               0 :         fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
  127903                 :       }
  127904                 :     }
  127905                 :   }
  127906                 : 
  127907               0 :   return rc;
  127908                 : }
  127909                 : 
  127910                 : /*
  127911                 : ** This function does the work for the xUpdate method of FTS3 virtual
  127912                 : ** tables.
  127913                 : */
  127914               4 : SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
  127915                 :   sqlite3_vtab *pVtab,            /* FTS3 vtab object */
  127916                 :   int nArg,                       /* Size of argument array */
  127917                 :   sqlite3_value **apVal,          /* Array of arguments */
  127918                 :   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
  127919                 : ){
  127920               4 :   Fts3Table *p = (Fts3Table *)pVtab;
  127921               4 :   int rc = SQLITE_OK;             /* Return Code */
  127922               4 :   int isRemove = 0;               /* True for an UPDATE or DELETE */
  127923               4 :   u32 *aSzIns = 0;                /* Sizes of inserted documents */
  127924                 :   u32 *aSzDel;                    /* Sizes of deleted documents */
  127925               4 :   int nChng = 0;                  /* Net change in number of documents */
  127926               4 :   int bInsertDone = 0;
  127927                 : 
  127928               4 :   assert( p->pSegments==0 );
  127929                 : 
  127930                 :   /* Check for a "special" INSERT operation. One of the form:
  127931                 :   **
  127932                 :   **   INSERT INTO xyz(xyz) VALUES('command');
  127933                 :   */
  127934               4 :   if( nArg>1 
  127935               4 :    && sqlite3_value_type(apVal[0])==SQLITE_NULL 
  127936               4 :    && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL 
  127937                 :   ){
  127938               0 :     rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
  127939               0 :     goto update_out;
  127940                 :   }
  127941                 : 
  127942                 :   /* Allocate space to hold the change in document sizes */
  127943               4 :   aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 );
  127944               4 :   if( aSzIns==0 ){
  127945               0 :     rc = SQLITE_NOMEM;
  127946               0 :     goto update_out;
  127947                 :   }
  127948               4 :   aSzDel = &aSzIns[p->nColumn+1];
  127949               4 :   memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2);
  127950                 : 
  127951                 :   /* If this is an INSERT operation, or an UPDATE that modifies the rowid
  127952                 :   ** value, then this operation requires constraint handling.
  127953                 :   **
  127954                 :   ** If the on-conflict mode is REPLACE, this means that the existing row
  127955                 :   ** should be deleted from the database before inserting the new row. Or,
  127956                 :   ** if the on-conflict mode is other than REPLACE, then this method must
  127957                 :   ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
  127958                 :   ** modify the database file.
  127959                 :   */
  127960               4 :   if( nArg>1 && p->zContentTbl==0 ){
  127961                 :     /* Find the value object that holds the new rowid value. */
  127962               4 :     sqlite3_value *pNewRowid = apVal[3+p->nColumn];
  127963               4 :     if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
  127964               4 :       pNewRowid = apVal[1];
  127965                 :     }
  127966                 : 
  127967               4 :     if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && ( 
  127968               0 :         sqlite3_value_type(apVal[0])==SQLITE_NULL
  127969               0 :      || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
  127970                 :     )){
  127971                 :       /* The new rowid is not NULL (in this case the rowid will be
  127972                 :       ** automatically assigned and there is no chance of a conflict), and 
  127973                 :       ** the statement is either an INSERT or an UPDATE that modifies the
  127974                 :       ** rowid column. So if the conflict mode is REPLACE, then delete any
  127975                 :       ** existing row with rowid=pNewRowid. 
  127976                 :       **
  127977                 :       ** Or, if the conflict mode is not REPLACE, insert the new record into 
  127978                 :       ** the %_content table. If we hit the duplicate rowid constraint (or any
  127979                 :       ** other error) while doing so, return immediately.
  127980                 :       **
  127981                 :       ** This branch may also run if pNewRowid contains a value that cannot
  127982                 :       ** be losslessly converted to an integer. In this case, the eventual 
  127983                 :       ** call to fts3InsertData() (either just below or further on in this
  127984                 :       ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is 
  127985                 :       ** invoked, it will delete zero rows (since no row will have
  127986                 :       ** docid=$pNewRowid if $pNewRowid is not an integer value).
  127987                 :       */
  127988               0 :       if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
  127989               0 :         rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
  127990                 :       }else{
  127991               0 :         rc = fts3InsertData(p, apVal, pRowid);
  127992               0 :         bInsertDone = 1;
  127993                 :       }
  127994                 :     }
  127995                 :   }
  127996               4 :   if( rc!=SQLITE_OK ){
  127997               0 :     goto update_out;
  127998                 :   }
  127999                 : 
  128000                 :   /* If this is a DELETE or UPDATE operation, remove the old record. */
  128001               4 :   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  128002               0 :     assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
  128003               0 :     rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
  128004               0 :     isRemove = 1;
  128005                 :   }
  128006                 :   
  128007                 :   /* If this is an INSERT or UPDATE operation, insert the new record. */
  128008               4 :   if( nArg>1 && rc==SQLITE_OK ){
  128009               4 :     if( bInsertDone==0 ){
  128010               4 :       rc = fts3InsertData(p, apVal, pRowid);
  128011               4 :       if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
  128012               0 :         rc = FTS_CORRUPT_VTAB;
  128013                 :       }
  128014                 :     }
  128015               4 :     if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
  128016               4 :       rc = fts3PendingTermsDocid(p, *pRowid);
  128017                 :     }
  128018               4 :     if( rc==SQLITE_OK ){
  128019               4 :       assert( p->iPrevDocid==*pRowid );
  128020               4 :       rc = fts3InsertTerms(p, apVal, aSzIns);
  128021                 :     }
  128022               4 :     if( p->bHasDocsize ){
  128023               0 :       fts3InsertDocsize(&rc, p, aSzIns);
  128024                 :     }
  128025               4 :     nChng++;
  128026                 :   }
  128027                 : 
  128028               4 :   if( p->bHasStat ){
  128029               0 :     fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
  128030                 :   }
  128031                 : 
  128032                 :  update_out:
  128033               4 :   sqlite3_free(aSzIns);
  128034               4 :   sqlite3Fts3SegmentsClose(p);
  128035               4 :   return rc;
  128036                 : }
  128037                 : 
  128038                 : /* 
  128039                 : ** Flush any data in the pending-terms hash table to disk. If successful,
  128040                 : ** merge all segments in the database (including the new segment, if 
  128041                 : ** there was any data to flush) into a single segment. 
  128042                 : */
  128043               0 : SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
  128044                 :   int rc;
  128045               0 :   rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
  128046               0 :   if( rc==SQLITE_OK ){
  128047               0 :     rc = fts3DoOptimize(p, 1);
  128048               0 :     if( rc==SQLITE_OK || rc==SQLITE_DONE ){
  128049               0 :       int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
  128050               0 :       if( rc2!=SQLITE_OK ) rc = rc2;
  128051                 :     }else{
  128052               0 :       sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
  128053               0 :       sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
  128054                 :     }
  128055                 :   }
  128056               0 :   sqlite3Fts3SegmentsClose(p);
  128057               0 :   return rc;
  128058                 : }
  128059                 : 
  128060                 : #endif
  128061                 : 
  128062                 : /************** End of fts3_write.c ******************************************/
  128063                 : /************** Begin file fts3_snippet.c ************************************/
  128064                 : /*
  128065                 : ** 2009 Oct 23
  128066                 : **
  128067                 : ** The author disclaims copyright to this source code.  In place of
  128068                 : ** a legal notice, here is a blessing:
  128069                 : **
  128070                 : **    May you do good and not evil.
  128071                 : **    May you find forgiveness for yourself and forgive others.
  128072                 : **    May you share freely, never taking more than you give.
  128073                 : **
  128074                 : ******************************************************************************
  128075                 : */
  128076                 : 
  128077                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  128078                 : 
  128079                 : /* #include <string.h> */
  128080                 : /* #include <assert.h> */
  128081                 : 
  128082                 : /*
  128083                 : ** Characters that may appear in the second argument to matchinfo().
  128084                 : */
  128085                 : #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
  128086                 : #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
  128087                 : #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
  128088                 : #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
  128089                 : #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
  128090                 : #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
  128091                 : #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
  128092                 : 
  128093                 : /*
  128094                 : ** The default value for the second argument to matchinfo(). 
  128095                 : */
  128096                 : #define FTS3_MATCHINFO_DEFAULT   "pcx"
  128097                 : 
  128098                 : 
  128099                 : /*
  128100                 : ** Used as an fts3ExprIterate() context when loading phrase doclists to
  128101                 : ** Fts3Expr.aDoclist[]/nDoclist.
  128102                 : */
  128103                 : typedef struct LoadDoclistCtx LoadDoclistCtx;
  128104                 : struct LoadDoclistCtx {
  128105                 :   Fts3Cursor *pCsr;               /* FTS3 Cursor */
  128106                 :   int nPhrase;                    /* Number of phrases seen so far */
  128107                 :   int nToken;                     /* Number of tokens seen so far */
  128108                 : };
  128109                 : 
  128110                 : /*
  128111                 : ** The following types are used as part of the implementation of the 
  128112                 : ** fts3BestSnippet() routine.
  128113                 : */
  128114                 : typedef struct SnippetIter SnippetIter;
  128115                 : typedef struct SnippetPhrase SnippetPhrase;
  128116                 : typedef struct SnippetFragment SnippetFragment;
  128117                 : 
  128118                 : struct SnippetIter {
  128119                 :   Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
  128120                 :   int iCol;                       /* Extract snippet from this column */
  128121                 :   int nSnippet;                   /* Requested snippet length (in tokens) */
  128122                 :   int nPhrase;                    /* Number of phrases in query */
  128123                 :   SnippetPhrase *aPhrase;         /* Array of size nPhrase */
  128124                 :   int iCurrent;                   /* First token of current snippet */
  128125                 : };
  128126                 : 
  128127                 : struct SnippetPhrase {
  128128                 :   int nToken;                     /* Number of tokens in phrase */
  128129                 :   char *pList;                    /* Pointer to start of phrase position list */
  128130                 :   int iHead;                      /* Next value in position list */
  128131                 :   char *pHead;                    /* Position list data following iHead */
  128132                 :   int iTail;                      /* Next value in trailing position list */
  128133                 :   char *pTail;                    /* Position list data following iTail */
  128134                 : };
  128135                 : 
  128136                 : struct SnippetFragment {
  128137                 :   int iCol;                       /* Column snippet is extracted from */
  128138                 :   int iPos;                       /* Index of first token in snippet */
  128139                 :   u64 covered;                    /* Mask of query phrases covered */
  128140                 :   u64 hlmask;                     /* Mask of snippet terms to highlight */
  128141                 : };
  128142                 : 
  128143                 : /*
  128144                 : ** This type is used as an fts3ExprIterate() context object while 
  128145                 : ** accumulating the data returned by the matchinfo() function.
  128146                 : */
  128147                 : typedef struct MatchInfo MatchInfo;
  128148                 : struct MatchInfo {
  128149                 :   Fts3Cursor *pCursor;            /* FTS3 Cursor */
  128150                 :   int nCol;                       /* Number of columns in table */
  128151                 :   int nPhrase;                    /* Number of matchable phrases in query */
  128152                 :   sqlite3_int64 nDoc;             /* Number of docs in database */
  128153                 :   u32 *aMatchinfo;                /* Pre-allocated buffer */
  128154                 : };
  128155                 : 
  128156                 : 
  128157                 : 
  128158                 : /*
  128159                 : ** The snippet() and offsets() functions both return text values. An instance
  128160                 : ** of the following structure is used to accumulate those values while the
  128161                 : ** functions are running. See fts3StringAppend() for details.
  128162                 : */
  128163                 : typedef struct StrBuffer StrBuffer;
  128164                 : struct StrBuffer {
  128165                 :   char *z;                        /* Pointer to buffer containing string */
  128166                 :   int n;                          /* Length of z in bytes (excl. nul-term) */
  128167                 :   int nAlloc;                     /* Allocated size of buffer z in bytes */
  128168                 : };
  128169                 : 
  128170                 : 
  128171                 : /*
  128172                 : ** This function is used to help iterate through a position-list. A position
  128173                 : ** list is a list of unique integers, sorted from smallest to largest. Each
  128174                 : ** element of the list is represented by an FTS3 varint that takes the value
  128175                 : ** of the difference between the current element and the previous one plus
  128176                 : ** two. For example, to store the position-list:
  128177                 : **
  128178                 : **     4 9 113
  128179                 : **
  128180                 : ** the three varints:
  128181                 : **
  128182                 : **     6 7 106
  128183                 : **
  128184                 : ** are encoded.
  128185                 : **
  128186                 : ** When this function is called, *pp points to the start of an element of
  128187                 : ** the list. *piPos contains the value of the previous entry in the list.
  128188                 : ** After it returns, *piPos contains the value of the next element of the
  128189                 : ** list and *pp is advanced to the following varint.
  128190                 : */
  128191               0 : static void fts3GetDeltaPosition(char **pp, int *piPos){
  128192                 :   int iVal;
  128193               0 :   *pp += sqlite3Fts3GetVarint32(*pp, &iVal);
  128194               0 :   *piPos += (iVal-2);
  128195               0 : }
  128196                 : 
  128197                 : /*
  128198                 : ** Helper function for fts3ExprIterate() (see below).
  128199                 : */
  128200               0 : static int fts3ExprIterate2(
  128201                 :   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
  128202                 :   int *piPhrase,                  /* Pointer to phrase counter */
  128203                 :   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
  128204                 :   void *pCtx                      /* Second argument to pass to callback */
  128205                 : ){
  128206                 :   int rc;                         /* Return code */
  128207               0 :   int eType = pExpr->eType;       /* Type of expression node pExpr */
  128208                 : 
  128209               0 :   if( eType!=FTSQUERY_PHRASE ){
  128210               0 :     assert( pExpr->pLeft && pExpr->pRight );
  128211               0 :     rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
  128212               0 :     if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
  128213               0 :       rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
  128214                 :     }
  128215                 :   }else{
  128216               0 :     rc = x(pExpr, *piPhrase, pCtx);
  128217               0 :     (*piPhrase)++;
  128218                 :   }
  128219               0 :   return rc;
  128220                 : }
  128221                 : 
  128222                 : /*
  128223                 : ** Iterate through all phrase nodes in an FTS3 query, except those that
  128224                 : ** are part of a sub-tree that is the right-hand-side of a NOT operator.
  128225                 : ** For each phrase node found, the supplied callback function is invoked.
  128226                 : **
  128227                 : ** If the callback function returns anything other than SQLITE_OK, 
  128228                 : ** the iteration is abandoned and the error code returned immediately.
  128229                 : ** Otherwise, SQLITE_OK is returned after a callback has been made for
  128230                 : ** all eligible phrase nodes.
  128231                 : */
  128232               0 : static int fts3ExprIterate(
  128233                 :   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
  128234                 :   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
  128235                 :   void *pCtx                      /* Second argument to pass to callback */
  128236                 : ){
  128237               0 :   int iPhrase = 0;                /* Variable used as the phrase counter */
  128238               0 :   return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
  128239                 : }
  128240                 : 
  128241                 : /*
  128242                 : ** This is an fts3ExprIterate() callback used while loading the doclists
  128243                 : ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
  128244                 : ** fts3ExprLoadDoclists().
  128245                 : */
  128246               0 : static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
  128247               0 :   int rc = SQLITE_OK;
  128248               0 :   Fts3Phrase *pPhrase = pExpr->pPhrase;
  128249               0 :   LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
  128250                 : 
  128251                 :   UNUSED_PARAMETER(iPhrase);
  128252                 : 
  128253               0 :   p->nPhrase++;
  128254               0 :   p->nToken += pPhrase->nToken;
  128255                 : 
  128256               0 :   return rc;
  128257                 : }
  128258                 : 
  128259                 : /*
  128260                 : ** Load the doclists for each phrase in the query associated with FTS3 cursor
  128261                 : ** pCsr. 
  128262                 : **
  128263                 : ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable 
  128264                 : ** phrases in the expression (all phrases except those directly or 
  128265                 : ** indirectly descended from the right-hand-side of a NOT operator). If 
  128266                 : ** pnToken is not NULL, then it is set to the number of tokens in all
  128267                 : ** matchable phrases of the expression.
  128268                 : */
  128269               0 : static int fts3ExprLoadDoclists(
  128270                 :   Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
  128271                 :   int *pnPhrase,                  /* OUT: Number of phrases in query */
  128272                 :   int *pnToken                    /* OUT: Number of tokens in query */
  128273                 : ){
  128274                 :   int rc;                         /* Return Code */
  128275               0 :   LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
  128276               0 :   sCtx.pCsr = pCsr;
  128277               0 :   rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
  128278               0 :   if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
  128279               0 :   if( pnToken ) *pnToken = sCtx.nToken;
  128280               0 :   return rc;
  128281                 : }
  128282                 : 
  128283               0 : static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
  128284               0 :   (*(int *)ctx)++;
  128285                 :   UNUSED_PARAMETER(pExpr);
  128286                 :   UNUSED_PARAMETER(iPhrase);
  128287               0 :   return SQLITE_OK;
  128288                 : }
  128289               0 : static int fts3ExprPhraseCount(Fts3Expr *pExpr){
  128290               0 :   int nPhrase = 0;
  128291               0 :   (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
  128292               0 :   return nPhrase;
  128293                 : }
  128294                 : 
  128295                 : /*
  128296                 : ** Advance the position list iterator specified by the first two 
  128297                 : ** arguments so that it points to the first element with a value greater
  128298                 : ** than or equal to parameter iNext.
  128299                 : */
  128300               0 : static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
  128301               0 :   char *pIter = *ppIter;
  128302               0 :   if( pIter ){
  128303               0 :     int iIter = *piIter;
  128304                 : 
  128305               0 :     while( iIter<iNext ){
  128306               0 :       if( 0==(*pIter & 0xFE) ){
  128307               0 :         iIter = -1;
  128308               0 :         pIter = 0;
  128309               0 :         break;
  128310                 :       }
  128311               0 :       fts3GetDeltaPosition(&pIter, &iIter);
  128312                 :     }
  128313                 : 
  128314               0 :     *piIter = iIter;
  128315               0 :     *ppIter = pIter;
  128316                 :   }
  128317               0 : }
  128318                 : 
  128319                 : /*
  128320                 : ** Advance the snippet iterator to the next candidate snippet.
  128321                 : */
  128322               0 : static int fts3SnippetNextCandidate(SnippetIter *pIter){
  128323                 :   int i;                          /* Loop counter */
  128324                 : 
  128325               0 :   if( pIter->iCurrent<0 ){
  128326                 :     /* The SnippetIter object has just been initialized. The first snippet
  128327                 :     ** candidate always starts at offset 0 (even if this candidate has a
  128328                 :     ** score of 0.0).
  128329                 :     */
  128330               0 :     pIter->iCurrent = 0;
  128331                 : 
  128332                 :     /* Advance the 'head' iterator of each phrase to the first offset that
  128333                 :     ** is greater than or equal to (iNext+nSnippet).
  128334                 :     */
  128335               0 :     for(i=0; i<pIter->nPhrase; i++){
  128336               0 :       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
  128337               0 :       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
  128338                 :     }
  128339                 :   }else{
  128340                 :     int iStart;
  128341               0 :     int iEnd = 0x7FFFFFFF;
  128342                 : 
  128343               0 :     for(i=0; i<pIter->nPhrase; i++){
  128344               0 :       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
  128345               0 :       if( pPhrase->pHead && pPhrase->iHead<iEnd ){
  128346               0 :         iEnd = pPhrase->iHead;
  128347                 :       }
  128348                 :     }
  128349               0 :     if( iEnd==0x7FFFFFFF ){
  128350               0 :       return 1;
  128351                 :     }
  128352                 : 
  128353               0 :     pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
  128354               0 :     for(i=0; i<pIter->nPhrase; i++){
  128355               0 :       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
  128356               0 :       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
  128357               0 :       fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
  128358                 :     }
  128359                 :   }
  128360                 : 
  128361               0 :   return 0;
  128362                 : }
  128363                 : 
  128364                 : /*
  128365                 : ** Retrieve information about the current candidate snippet of snippet 
  128366                 : ** iterator pIter.
  128367                 : */
  128368               0 : static void fts3SnippetDetails(
  128369                 :   SnippetIter *pIter,             /* Snippet iterator */
  128370                 :   u64 mCovered,                   /* Bitmask of phrases already covered */
  128371                 :   int *piToken,                   /* OUT: First token of proposed snippet */
  128372                 :   int *piScore,                   /* OUT: "Score" for this snippet */
  128373                 :   u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
  128374                 :   u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
  128375                 : ){
  128376               0 :   int iStart = pIter->iCurrent;   /* First token of snippet */
  128377               0 :   int iScore = 0;                 /* Score of this snippet */
  128378                 :   int i;                          /* Loop counter */
  128379               0 :   u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
  128380               0 :   u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
  128381                 : 
  128382               0 :   for(i=0; i<pIter->nPhrase; i++){
  128383               0 :     SnippetPhrase *pPhrase = &pIter->aPhrase[i];
  128384               0 :     if( pPhrase->pTail ){
  128385               0 :       char *pCsr = pPhrase->pTail;
  128386               0 :       int iCsr = pPhrase->iTail;
  128387                 : 
  128388               0 :       while( iCsr<(iStart+pIter->nSnippet) ){
  128389                 :         int j;
  128390               0 :         u64 mPhrase = (u64)1 << i;
  128391               0 :         u64 mPos = (u64)1 << (iCsr - iStart);
  128392               0 :         assert( iCsr>=iStart );
  128393               0 :         if( (mCover|mCovered)&mPhrase ){
  128394               0 :           iScore++;
  128395                 :         }else{
  128396               0 :           iScore += 1000;
  128397                 :         }
  128398               0 :         mCover |= mPhrase;
  128399                 : 
  128400               0 :         for(j=0; j<pPhrase->nToken; j++){
  128401               0 :           mHighlight |= (mPos>>j);
  128402                 :         }
  128403                 : 
  128404               0 :         if( 0==(*pCsr & 0x0FE) ) break;
  128405               0 :         fts3GetDeltaPosition(&pCsr, &iCsr);
  128406                 :       }
  128407                 :     }
  128408                 :   }
  128409                 : 
  128410                 :   /* Set the output variables before returning. */
  128411               0 :   *piToken = iStart;
  128412               0 :   *piScore = iScore;
  128413               0 :   *pmCover = mCover;
  128414               0 :   *pmHighlight = mHighlight;
  128415               0 : }
  128416                 : 
  128417                 : /*
  128418                 : ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
  128419                 : ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
  128420                 : */
  128421               0 : static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
  128422               0 :   SnippetIter *p = (SnippetIter *)ctx;
  128423               0 :   SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
  128424                 :   char *pCsr;
  128425                 : 
  128426               0 :   pPhrase->nToken = pExpr->pPhrase->nToken;
  128427                 : 
  128428               0 :   pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
  128429               0 :   if( pCsr ){
  128430               0 :     int iFirst = 0;
  128431               0 :     pPhrase->pList = pCsr;
  128432               0 :     fts3GetDeltaPosition(&pCsr, &iFirst);
  128433               0 :     assert( iFirst>=0 );
  128434               0 :     pPhrase->pHead = pCsr;
  128435               0 :     pPhrase->pTail = pCsr;
  128436               0 :     pPhrase->iHead = iFirst;
  128437               0 :     pPhrase->iTail = iFirst;
  128438                 :   }else{
  128439               0 :     assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 );
  128440                 :   }
  128441                 : 
  128442               0 :   return SQLITE_OK;
  128443                 : }
  128444                 : 
  128445                 : /*
  128446                 : ** Select the fragment of text consisting of nFragment contiguous tokens 
  128447                 : ** from column iCol that represent the "best" snippet. The best snippet
  128448                 : ** is the snippet with the highest score, where scores are calculated
  128449                 : ** by adding:
  128450                 : **
  128451                 : **   (a) +1 point for each occurence of a matchable phrase in the snippet.
  128452                 : **
  128453                 : **   (b) +1000 points for the first occurence of each matchable phrase in 
  128454                 : **       the snippet for which the corresponding mCovered bit is not set.
  128455                 : **
  128456                 : ** The selected snippet parameters are stored in structure *pFragment before
  128457                 : ** returning. The score of the selected snippet is stored in *piScore
  128458                 : ** before returning.
  128459                 : */
  128460               0 : static int fts3BestSnippet(
  128461                 :   int nSnippet,                   /* Desired snippet length */
  128462                 :   Fts3Cursor *pCsr,               /* Cursor to create snippet for */
  128463                 :   int iCol,                       /* Index of column to create snippet from */
  128464                 :   u64 mCovered,                   /* Mask of phrases already covered */
  128465                 :   u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
  128466                 :   SnippetFragment *pFragment,     /* OUT: Best snippet found */
  128467                 :   int *piScore                    /* OUT: Score of snippet pFragment */
  128468                 : ){
  128469                 :   int rc;                         /* Return Code */
  128470                 :   int nList;                      /* Number of phrases in expression */
  128471                 :   SnippetIter sIter;              /* Iterates through snippet candidates */
  128472                 :   int nByte;                      /* Number of bytes of space to allocate */
  128473               0 :   int iBestScore = -1;            /* Best snippet score found so far */
  128474                 :   int i;                          /* Loop counter */
  128475                 : 
  128476               0 :   memset(&sIter, 0, sizeof(sIter));
  128477                 : 
  128478                 :   /* Iterate through the phrases in the expression to count them. The same
  128479                 :   ** callback makes sure the doclists are loaded for each phrase.
  128480                 :   */
  128481               0 :   rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
  128482               0 :   if( rc!=SQLITE_OK ){
  128483               0 :     return rc;
  128484                 :   }
  128485                 : 
  128486                 :   /* Now that it is known how many phrases there are, allocate and zero
  128487                 :   ** the required space using malloc().
  128488                 :   */
  128489               0 :   nByte = sizeof(SnippetPhrase) * nList;
  128490               0 :   sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
  128491               0 :   if( !sIter.aPhrase ){
  128492               0 :     return SQLITE_NOMEM;
  128493                 :   }
  128494               0 :   memset(sIter.aPhrase, 0, nByte);
  128495                 : 
  128496                 :   /* Initialize the contents of the SnippetIter object. Then iterate through
  128497                 :   ** the set of phrases in the expression to populate the aPhrase[] array.
  128498                 :   */
  128499               0 :   sIter.pCsr = pCsr;
  128500               0 :   sIter.iCol = iCol;
  128501               0 :   sIter.nSnippet = nSnippet;
  128502               0 :   sIter.nPhrase = nList;
  128503               0 :   sIter.iCurrent = -1;
  128504               0 :   (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
  128505                 : 
  128506                 :   /* Set the *pmSeen output variable. */
  128507               0 :   for(i=0; i<nList; i++){
  128508               0 :     if( sIter.aPhrase[i].pHead ){
  128509               0 :       *pmSeen |= (u64)1 << i;
  128510                 :     }
  128511                 :   }
  128512                 : 
  128513                 :   /* Loop through all candidate snippets. Store the best snippet in 
  128514                 :   ** *pFragment. Store its associated 'score' in iBestScore.
  128515                 :   */
  128516               0 :   pFragment->iCol = iCol;
  128517               0 :   while( !fts3SnippetNextCandidate(&sIter) ){
  128518                 :     int iPos;
  128519                 :     int iScore;
  128520                 :     u64 mCover;
  128521                 :     u64 mHighlight;
  128522               0 :     fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
  128523               0 :     assert( iScore>=0 );
  128524               0 :     if( iScore>iBestScore ){
  128525               0 :       pFragment->iPos = iPos;
  128526               0 :       pFragment->hlmask = mHighlight;
  128527               0 :       pFragment->covered = mCover;
  128528               0 :       iBestScore = iScore;
  128529                 :     }
  128530                 :   }
  128531                 : 
  128532               0 :   sqlite3_free(sIter.aPhrase);
  128533               0 :   *piScore = iBestScore;
  128534               0 :   return SQLITE_OK;
  128535                 : }
  128536                 : 
  128537                 : 
  128538                 : /*
  128539                 : ** Append a string to the string-buffer passed as the first argument.
  128540                 : **
  128541                 : ** If nAppend is negative, then the length of the string zAppend is
  128542                 : ** determined using strlen().
  128543                 : */
  128544               0 : static int fts3StringAppend(
  128545                 :   StrBuffer *pStr,                /* Buffer to append to */
  128546                 :   const char *zAppend,            /* Pointer to data to append to buffer */
  128547                 :   int nAppend                     /* Size of zAppend in bytes (or -1) */
  128548                 : ){
  128549               0 :   if( nAppend<0 ){
  128550               0 :     nAppend = (int)strlen(zAppend);
  128551                 :   }
  128552                 : 
  128553                 :   /* If there is insufficient space allocated at StrBuffer.z, use realloc()
  128554                 :   ** to grow the buffer until so that it is big enough to accomadate the
  128555                 :   ** appended data.
  128556                 :   */
  128557               0 :   if( pStr->n+nAppend+1>=pStr->nAlloc ){
  128558               0 :     int nAlloc = pStr->nAlloc+nAppend+100;
  128559               0 :     char *zNew = sqlite3_realloc(pStr->z, nAlloc);
  128560               0 :     if( !zNew ){
  128561               0 :       return SQLITE_NOMEM;
  128562                 :     }
  128563               0 :     pStr->z = zNew;
  128564               0 :     pStr->nAlloc = nAlloc;
  128565                 :   }
  128566                 : 
  128567                 :   /* Append the data to the string buffer. */
  128568               0 :   memcpy(&pStr->z[pStr->n], zAppend, nAppend);
  128569               0 :   pStr->n += nAppend;
  128570               0 :   pStr->z[pStr->n] = '\0';
  128571                 : 
  128572               0 :   return SQLITE_OK;
  128573                 : }
  128574                 : 
  128575                 : /*
  128576                 : ** The fts3BestSnippet() function often selects snippets that end with a
  128577                 : ** query term. That is, the final term of the snippet is always a term
  128578                 : ** that requires highlighting. For example, if 'X' is a highlighted term
  128579                 : ** and '.' is a non-highlighted term, BestSnippet() may select:
  128580                 : **
  128581                 : **     ........X.....X
  128582                 : **
  128583                 : ** This function "shifts" the beginning of the snippet forward in the 
  128584                 : ** document so that there are approximately the same number of 
  128585                 : ** non-highlighted terms to the right of the final highlighted term as there
  128586                 : ** are to the left of the first highlighted term. For example, to this:
  128587                 : **
  128588                 : **     ....X.....X....
  128589                 : **
  128590                 : ** This is done as part of extracting the snippet text, not when selecting
  128591                 : ** the snippet. Snippet selection is done based on doclists only, so there
  128592                 : ** is no way for fts3BestSnippet() to know whether or not the document 
  128593                 : ** actually contains terms that follow the final highlighted term. 
  128594                 : */
  128595               0 : static int fts3SnippetShift(
  128596                 :   Fts3Table *pTab,                /* FTS3 table snippet comes from */
  128597                 :   int nSnippet,                   /* Number of tokens desired for snippet */
  128598                 :   const char *zDoc,               /* Document text to extract snippet from */
  128599                 :   int nDoc,                       /* Size of buffer zDoc in bytes */
  128600                 :   int *piPos,                     /* IN/OUT: First token of snippet */
  128601                 :   u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
  128602                 : ){
  128603               0 :   u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
  128604                 : 
  128605               0 :   if( hlmask ){
  128606                 :     int nLeft;                    /* Tokens to the left of first highlight */
  128607                 :     int nRight;                   /* Tokens to the right of last highlight */
  128608                 :     int nDesired;                 /* Ideal number of tokens to shift forward */
  128609                 : 
  128610               0 :     for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
  128611               0 :     for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
  128612               0 :     nDesired = (nLeft-nRight)/2;
  128613                 : 
  128614                 :     /* Ideally, the start of the snippet should be pushed forward in the
  128615                 :     ** document nDesired tokens. This block checks if there are actually
  128616                 :     ** nDesired tokens to the right of the snippet. If so, *piPos and
  128617                 :     ** *pHlMask are updated to shift the snippet nDesired tokens to the
  128618                 :     ** right. Otherwise, the snippet is shifted by the number of tokens
  128619                 :     ** available.
  128620                 :     */
  128621               0 :     if( nDesired>0 ){
  128622                 :       int nShift;                 /* Number of tokens to shift snippet by */
  128623               0 :       int iCurrent = 0;           /* Token counter */
  128624                 :       int rc;                     /* Return Code */
  128625                 :       sqlite3_tokenizer_module *pMod;
  128626                 :       sqlite3_tokenizer_cursor *pC;
  128627               0 :       pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
  128628                 : 
  128629                 :       /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
  128630                 :       ** or more tokens in zDoc/nDoc.
  128631                 :       */
  128632               0 :       rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
  128633               0 :       if( rc!=SQLITE_OK ){
  128634               0 :         return rc;
  128635                 :       }
  128636               0 :       pC->pTokenizer = pTab->pTokenizer;
  128637               0 :       while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
  128638                 :         const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3;
  128639               0 :         rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
  128640                 :       }
  128641               0 :       pMod->xClose(pC);
  128642               0 :       if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
  128643                 : 
  128644               0 :       nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
  128645               0 :       assert( nShift<=nDesired );
  128646               0 :       if( nShift>0 ){
  128647               0 :         *piPos += nShift;
  128648               0 :         *pHlmask = hlmask >> nShift;
  128649                 :       }
  128650                 :     }
  128651                 :   }
  128652               0 :   return SQLITE_OK;
  128653                 : }
  128654                 : 
  128655                 : /*
  128656                 : ** Extract the snippet text for fragment pFragment from cursor pCsr and
  128657                 : ** append it to string buffer pOut.
  128658                 : */
  128659               0 : static int fts3SnippetText(
  128660                 :   Fts3Cursor *pCsr,               /* FTS3 Cursor */
  128661                 :   SnippetFragment *pFragment,     /* Snippet to extract */
  128662                 :   int iFragment,                  /* Fragment number */
  128663                 :   int isLast,                     /* True for final fragment in snippet */
  128664                 :   int nSnippet,                   /* Number of tokens in extracted snippet */
  128665                 :   const char *zOpen,              /* String inserted before highlighted term */
  128666                 :   const char *zClose,             /* String inserted after highlighted term */
  128667                 :   const char *zEllipsis,          /* String inserted between snippets */
  128668                 :   StrBuffer *pOut                 /* Write output here */
  128669                 : ){
  128670               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  128671                 :   int rc;                         /* Return code */
  128672                 :   const char *zDoc;               /* Document text to extract snippet from */
  128673                 :   int nDoc;                       /* Size of zDoc in bytes */
  128674               0 :   int iCurrent = 0;               /* Current token number of document */
  128675               0 :   int iEnd = 0;                   /* Byte offset of end of current token */
  128676               0 :   int isShiftDone = 0;            /* True after snippet is shifted */
  128677               0 :   int iPos = pFragment->iPos;     /* First token of snippet */
  128678               0 :   u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
  128679               0 :   int iCol = pFragment->iCol+1;   /* Query column to extract text from */
  128680                 :   sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
  128681                 :   sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
  128682                 :   const char *ZDUMMY;             /* Dummy argument used with tokenizer */
  128683                 :   int DUMMY1;                     /* Dummy argument used with tokenizer */
  128684                 :   
  128685               0 :   zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
  128686               0 :   if( zDoc==0 ){
  128687               0 :     if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
  128688               0 :       return SQLITE_NOMEM;
  128689                 :     }
  128690               0 :     return SQLITE_OK;
  128691                 :   }
  128692               0 :   nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
  128693                 : 
  128694                 :   /* Open a token cursor on the document. */
  128695               0 :   pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
  128696               0 :   rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
  128697               0 :   if( rc!=SQLITE_OK ){
  128698               0 :     return rc;
  128699                 :   }
  128700               0 :   pC->pTokenizer = pTab->pTokenizer;
  128701                 : 
  128702               0 :   while( rc==SQLITE_OK ){
  128703                 :     int iBegin;                   /* Offset in zDoc of start of token */
  128704                 :     int iFin;                     /* Offset in zDoc of end of token */
  128705                 :     int isHighlight;              /* True for highlighted terms */
  128706                 : 
  128707               0 :     rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
  128708               0 :     if( rc!=SQLITE_OK ){
  128709               0 :       if( rc==SQLITE_DONE ){
  128710                 :         /* Special case - the last token of the snippet is also the last token
  128711                 :         ** of the column. Append any punctuation that occurred between the end
  128712                 :         ** of the previous token and the end of the document to the output. 
  128713                 :         ** Then break out of the loop. */
  128714               0 :         rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
  128715                 :       }
  128716               0 :       break;
  128717                 :     }
  128718               0 :     if( iCurrent<iPos ){ continue; }
  128719                 : 
  128720               0 :     if( !isShiftDone ){
  128721               0 :       int n = nDoc - iBegin;
  128722               0 :       rc = fts3SnippetShift(pTab, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask);
  128723               0 :       isShiftDone = 1;
  128724                 : 
  128725                 :       /* Now that the shift has been done, check if the initial "..." are
  128726                 :       ** required. They are required if (a) this is not the first fragment,
  128727                 :       ** or (b) this fragment does not begin at position 0 of its column. 
  128728                 :       */
  128729               0 :       if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
  128730               0 :         rc = fts3StringAppend(pOut, zEllipsis, -1);
  128731                 :       }
  128732               0 :       if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
  128733                 :     }
  128734                 : 
  128735               0 :     if( iCurrent>=(iPos+nSnippet) ){
  128736               0 :       if( isLast ){
  128737               0 :         rc = fts3StringAppend(pOut, zEllipsis, -1);
  128738                 :       }
  128739               0 :       break;
  128740                 :     }
  128741                 : 
  128742                 :     /* Set isHighlight to true if this term should be highlighted. */
  128743               0 :     isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
  128744                 : 
  128745               0 :     if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
  128746               0 :     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
  128747               0 :     if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
  128748               0 :     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
  128749                 : 
  128750               0 :     iEnd = iFin;
  128751                 :   }
  128752                 : 
  128753               0 :   pMod->xClose(pC);
  128754               0 :   return rc;
  128755                 : }
  128756                 : 
  128757                 : 
  128758                 : /*
  128759                 : ** This function is used to count the entries in a column-list (a 
  128760                 : ** delta-encoded list of term offsets within a single column of a single 
  128761                 : ** row). When this function is called, *ppCollist should point to the
  128762                 : ** beginning of the first varint in the column-list (the varint that
  128763                 : ** contains the position of the first matching term in the column data).
  128764                 : ** Before returning, *ppCollist is set to point to the first byte after
  128765                 : ** the last varint in the column-list (either the 0x00 signifying the end
  128766                 : ** of the position-list, or the 0x01 that precedes the column number of
  128767                 : ** the next column in the position-list).
  128768                 : **
  128769                 : ** The number of elements in the column-list is returned.
  128770                 : */
  128771               0 : static int fts3ColumnlistCount(char **ppCollist){
  128772               0 :   char *pEnd = *ppCollist;
  128773               0 :   char c = 0;
  128774               0 :   int nEntry = 0;
  128775                 : 
  128776                 :   /* A column-list is terminated by either a 0x01 or 0x00. */
  128777               0 :   while( 0xFE & (*pEnd | c) ){
  128778               0 :     c = *pEnd++ & 0x80;
  128779               0 :     if( !c ) nEntry++;
  128780                 :   }
  128781                 : 
  128782               0 :   *ppCollist = pEnd;
  128783               0 :   return nEntry;
  128784                 : }
  128785                 : 
  128786                 : /*
  128787                 : ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
  128788                 : ** for a single query. 
  128789                 : **
  128790                 : ** fts3ExprIterate() callback to load the 'global' elements of a
  128791                 : ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements 
  128792                 : ** of the matchinfo array that are constant for all rows returned by the 
  128793                 : ** current query.
  128794                 : **
  128795                 : ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
  128796                 : ** function populates Matchinfo.aMatchinfo[] as follows:
  128797                 : **
  128798                 : **   for(iCol=0; iCol<nCol; iCol++){
  128799                 : **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
  128800                 : **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
  128801                 : **   }
  128802                 : **
  128803                 : ** where X is the number of matches for phrase iPhrase is column iCol of all
  128804                 : ** rows of the table. Y is the number of rows for which column iCol contains
  128805                 : ** at least one instance of phrase iPhrase.
  128806                 : **
  128807                 : ** If the phrase pExpr consists entirely of deferred tokens, then all X and
  128808                 : ** Y values are set to nDoc, where nDoc is the number of documents in the 
  128809                 : ** file system. This is done because the full-text index doclist is required
  128810                 : ** to calculate these values properly, and the full-text index doclist is
  128811                 : ** not available for deferred tokens.
  128812                 : */
  128813               0 : static int fts3ExprGlobalHitsCb(
  128814                 :   Fts3Expr *pExpr,                /* Phrase expression node */
  128815                 :   int iPhrase,                    /* Phrase number (numbered from zero) */
  128816                 :   void *pCtx                      /* Pointer to MatchInfo structure */
  128817                 : ){
  128818               0 :   MatchInfo *p = (MatchInfo *)pCtx;
  128819               0 :   return sqlite3Fts3EvalPhraseStats(
  128820               0 :       p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
  128821                 :   );
  128822                 : }
  128823                 : 
  128824                 : /*
  128825                 : ** fts3ExprIterate() callback used to collect the "local" part of the
  128826                 : ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the 
  128827                 : ** array that are different for each row returned by the query.
  128828                 : */
  128829               0 : static int fts3ExprLocalHitsCb(
  128830                 :   Fts3Expr *pExpr,                /* Phrase expression node */
  128831                 :   int iPhrase,                    /* Phrase number */
  128832                 :   void *pCtx                      /* Pointer to MatchInfo structure */
  128833                 : ){
  128834               0 :   MatchInfo *p = (MatchInfo *)pCtx;
  128835               0 :   int iStart = iPhrase * p->nCol * 3;
  128836                 :   int i;
  128837                 : 
  128838               0 :   for(i=0; i<p->nCol; i++){
  128839                 :     char *pCsr;
  128840               0 :     pCsr = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i);
  128841               0 :     if( pCsr ){
  128842               0 :       p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
  128843                 :     }else{
  128844               0 :       p->aMatchinfo[iStart+i*3] = 0;
  128845                 :     }
  128846                 :   }
  128847                 : 
  128848               0 :   return SQLITE_OK;
  128849                 : }
  128850                 : 
  128851               0 : static int fts3MatchinfoCheck(
  128852                 :   Fts3Table *pTab, 
  128853                 :   char cArg,
  128854                 :   char **pzErr
  128855                 : ){
  128856               0 :   if( (cArg==FTS3_MATCHINFO_NPHRASE)
  128857               0 :    || (cArg==FTS3_MATCHINFO_NCOL)
  128858               0 :    || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat)
  128859               0 :    || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat)
  128860               0 :    || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
  128861               0 :    || (cArg==FTS3_MATCHINFO_LCS)
  128862               0 :    || (cArg==FTS3_MATCHINFO_HITS)
  128863                 :   ){
  128864               0 :     return SQLITE_OK;
  128865                 :   }
  128866               0 :   *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
  128867               0 :   return SQLITE_ERROR;
  128868                 : }
  128869                 : 
  128870               0 : static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
  128871                 :   int nVal;                       /* Number of integers output by cArg */
  128872                 : 
  128873               0 :   switch( cArg ){
  128874                 :     case FTS3_MATCHINFO_NDOC:
  128875                 :     case FTS3_MATCHINFO_NPHRASE: 
  128876                 :     case FTS3_MATCHINFO_NCOL: 
  128877               0 :       nVal = 1;
  128878               0 :       break;
  128879                 : 
  128880                 :     case FTS3_MATCHINFO_AVGLENGTH:
  128881                 :     case FTS3_MATCHINFO_LENGTH:
  128882                 :     case FTS3_MATCHINFO_LCS:
  128883               0 :       nVal = pInfo->nCol;
  128884               0 :       break;
  128885                 : 
  128886                 :     default:
  128887               0 :       assert( cArg==FTS3_MATCHINFO_HITS );
  128888               0 :       nVal = pInfo->nCol * pInfo->nPhrase * 3;
  128889               0 :       break;
  128890                 :   }
  128891                 : 
  128892               0 :   return nVal;
  128893                 : }
  128894                 : 
  128895               0 : static int fts3MatchinfoSelectDoctotal(
  128896                 :   Fts3Table *pTab,
  128897                 :   sqlite3_stmt **ppStmt,
  128898                 :   sqlite3_int64 *pnDoc,
  128899                 :   const char **paLen
  128900                 : ){
  128901                 :   sqlite3_stmt *pStmt;
  128902                 :   const char *a;
  128903                 :   sqlite3_int64 nDoc;
  128904                 : 
  128905               0 :   if( !*ppStmt ){
  128906               0 :     int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
  128907               0 :     if( rc!=SQLITE_OK ) return rc;
  128908                 :   }
  128909               0 :   pStmt = *ppStmt;
  128910               0 :   assert( sqlite3_data_count(pStmt)==1 );
  128911                 : 
  128912               0 :   a = sqlite3_column_blob(pStmt, 0);
  128913               0 :   a += sqlite3Fts3GetVarint(a, &nDoc);
  128914               0 :   if( nDoc==0 ) return FTS_CORRUPT_VTAB;
  128915               0 :   *pnDoc = (u32)nDoc;
  128916                 : 
  128917               0 :   if( paLen ) *paLen = a;
  128918               0 :   return SQLITE_OK;
  128919                 : }
  128920                 : 
  128921                 : /*
  128922                 : ** An instance of the following structure is used to store state while 
  128923                 : ** iterating through a multi-column position-list corresponding to the
  128924                 : ** hits for a single phrase on a single row in order to calculate the
  128925                 : ** values for a matchinfo() FTS3_MATCHINFO_LCS request.
  128926                 : */
  128927                 : typedef struct LcsIterator LcsIterator;
  128928                 : struct LcsIterator {
  128929                 :   Fts3Expr *pExpr;                /* Pointer to phrase expression */
  128930                 :   int iPosOffset;                 /* Tokens count up to end of this phrase */
  128931                 :   char *pRead;                    /* Cursor used to iterate through aDoclist */
  128932                 :   int iPos;                       /* Current position */
  128933                 : };
  128934                 : 
  128935                 : /* 
  128936                 : ** If LcsIterator.iCol is set to the following value, the iterator has
  128937                 : ** finished iterating through all offsets for all columns.
  128938                 : */
  128939                 : #define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
  128940                 : 
  128941               0 : static int fts3MatchinfoLcsCb(
  128942                 :   Fts3Expr *pExpr,                /* Phrase expression node */
  128943                 :   int iPhrase,                    /* Phrase number (numbered from zero) */
  128944                 :   void *pCtx                      /* Pointer to MatchInfo structure */
  128945                 : ){
  128946               0 :   LcsIterator *aIter = (LcsIterator *)pCtx;
  128947               0 :   aIter[iPhrase].pExpr = pExpr;
  128948               0 :   return SQLITE_OK;
  128949                 : }
  128950                 : 
  128951                 : /*
  128952                 : ** Advance the iterator passed as an argument to the next position. Return
  128953                 : ** 1 if the iterator is at EOF or if it now points to the start of the
  128954                 : ** position list for the next column.
  128955                 : */
  128956               0 : static int fts3LcsIteratorAdvance(LcsIterator *pIter){
  128957               0 :   char *pRead = pIter->pRead;
  128958                 :   sqlite3_int64 iRead;
  128959               0 :   int rc = 0;
  128960                 : 
  128961               0 :   pRead += sqlite3Fts3GetVarint(pRead, &iRead);
  128962               0 :   if( iRead==0 || iRead==1 ){
  128963               0 :     pRead = 0;
  128964               0 :     rc = 1;
  128965                 :   }else{
  128966               0 :     pIter->iPos += (int)(iRead-2);
  128967                 :   }
  128968                 : 
  128969               0 :   pIter->pRead = pRead;
  128970               0 :   return rc;
  128971                 : }
  128972                 :   
  128973                 : /*
  128974                 : ** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. 
  128975                 : **
  128976                 : ** If the call is successful, the longest-common-substring lengths for each
  128977                 : ** column are written into the first nCol elements of the pInfo->aMatchinfo[] 
  128978                 : ** array before returning. SQLITE_OK is returned in this case.
  128979                 : **
  128980                 : ** Otherwise, if an error occurs, an SQLite error code is returned and the
  128981                 : ** data written to the first nCol elements of pInfo->aMatchinfo[] is 
  128982                 : ** undefined.
  128983                 : */
  128984               0 : static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
  128985                 :   LcsIterator *aIter;
  128986                 :   int i;
  128987                 :   int iCol;
  128988               0 :   int nToken = 0;
  128989                 : 
  128990                 :   /* Allocate and populate the array of LcsIterator objects. The array
  128991                 :   ** contains one element for each matchable phrase in the query.
  128992                 :   **/
  128993               0 :   aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
  128994               0 :   if( !aIter ) return SQLITE_NOMEM;
  128995               0 :   memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
  128996               0 :   (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
  128997                 : 
  128998               0 :   for(i=0; i<pInfo->nPhrase; i++){
  128999               0 :     LcsIterator *pIter = &aIter[i];
  129000               0 :     nToken -= pIter->pExpr->pPhrase->nToken;
  129001               0 :     pIter->iPosOffset = nToken;
  129002                 :   }
  129003                 : 
  129004               0 :   for(iCol=0; iCol<pInfo->nCol; iCol++){
  129005               0 :     int nLcs = 0;                 /* LCS value for this column */
  129006               0 :     int nLive = 0;                /* Number of iterators in aIter not at EOF */
  129007                 : 
  129008               0 :     for(i=0; i<pInfo->nPhrase; i++){
  129009               0 :       LcsIterator *pIt = &aIter[i];
  129010               0 :       pIt->pRead = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol);
  129011               0 :       if( pIt->pRead ){
  129012               0 :         pIt->iPos = pIt->iPosOffset;
  129013               0 :         fts3LcsIteratorAdvance(&aIter[i]);
  129014               0 :         nLive++;
  129015                 :       }
  129016                 :     }
  129017                 : 
  129018               0 :     while( nLive>0 ){
  129019               0 :       LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
  129020               0 :       int nThisLcs = 0;           /* LCS for the current iterator positions */
  129021                 : 
  129022               0 :       for(i=0; i<pInfo->nPhrase; i++){
  129023               0 :         LcsIterator *pIter = &aIter[i];
  129024               0 :         if( pIter->pRead==0 ){
  129025                 :           /* This iterator is already at EOF for this column. */
  129026               0 :           nThisLcs = 0;
  129027                 :         }else{
  129028               0 :           if( pAdv==0 || pIter->iPos<pAdv->iPos ){
  129029               0 :             pAdv = pIter;
  129030                 :           }
  129031               0 :           if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
  129032               0 :             nThisLcs++;
  129033                 :           }else{
  129034               0 :             nThisLcs = 1;
  129035                 :           }
  129036               0 :           if( nThisLcs>nLcs ) nLcs = nThisLcs;
  129037                 :         }
  129038                 :       }
  129039               0 :       if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
  129040                 :     }
  129041                 : 
  129042               0 :     pInfo->aMatchinfo[iCol] = nLcs;
  129043                 :   }
  129044                 : 
  129045               0 :   sqlite3_free(aIter);
  129046               0 :   return SQLITE_OK;
  129047                 : }
  129048                 : 
  129049                 : /*
  129050                 : ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
  129051                 : ** be returned by the matchinfo() function. Argument zArg contains the 
  129052                 : ** format string passed as the second argument to matchinfo (or the
  129053                 : ** default value "pcx" if no second argument was specified). The format
  129054                 : ** string has already been validated and the pInfo->aMatchinfo[] array
  129055                 : ** is guaranteed to be large enough for the output.
  129056                 : **
  129057                 : ** If bGlobal is true, then populate all fields of the matchinfo() output.
  129058                 : ** If it is false, then assume that those fields that do not change between
  129059                 : ** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
  129060                 : ** have already been populated.
  129061                 : **
  129062                 : ** Return SQLITE_OK if successful, or an SQLite error code if an error 
  129063                 : ** occurs. If a value other than SQLITE_OK is returned, the state the
  129064                 : ** pInfo->aMatchinfo[] buffer is left in is undefined.
  129065                 : */
  129066               0 : static int fts3MatchinfoValues(
  129067                 :   Fts3Cursor *pCsr,               /* FTS3 cursor object */
  129068                 :   int bGlobal,                    /* True to grab the global stats */
  129069                 :   MatchInfo *pInfo,               /* Matchinfo context object */
  129070                 :   const char *zArg                /* Matchinfo format string */
  129071                 : ){
  129072               0 :   int rc = SQLITE_OK;
  129073                 :   int i;
  129074               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  129075               0 :   sqlite3_stmt *pSelect = 0;
  129076                 : 
  129077               0 :   for(i=0; rc==SQLITE_OK && zArg[i]; i++){
  129078                 : 
  129079               0 :     switch( zArg[i] ){
  129080                 :       case FTS3_MATCHINFO_NPHRASE:
  129081               0 :         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
  129082               0 :         break;
  129083                 : 
  129084                 :       case FTS3_MATCHINFO_NCOL:
  129085               0 :         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
  129086               0 :         break;
  129087                 :         
  129088                 :       case FTS3_MATCHINFO_NDOC:
  129089               0 :         if( bGlobal ){
  129090               0 :           sqlite3_int64 nDoc = 0;
  129091               0 :           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
  129092               0 :           pInfo->aMatchinfo[0] = (u32)nDoc;
  129093                 :         }
  129094               0 :         break;
  129095                 : 
  129096                 :       case FTS3_MATCHINFO_AVGLENGTH: 
  129097               0 :         if( bGlobal ){
  129098                 :           sqlite3_int64 nDoc;     /* Number of rows in table */
  129099                 :           const char *a;          /* Aggregate column length array */
  129100                 : 
  129101               0 :           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
  129102               0 :           if( rc==SQLITE_OK ){
  129103                 :             int iCol;
  129104               0 :             for(iCol=0; iCol<pInfo->nCol; iCol++){
  129105                 :               u32 iVal;
  129106                 :               sqlite3_int64 nToken;
  129107               0 :               a += sqlite3Fts3GetVarint(a, &nToken);
  129108               0 :               iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
  129109               0 :               pInfo->aMatchinfo[iCol] = iVal;
  129110                 :             }
  129111                 :           }
  129112                 :         }
  129113               0 :         break;
  129114                 : 
  129115                 :       case FTS3_MATCHINFO_LENGTH: {
  129116               0 :         sqlite3_stmt *pSelectDocsize = 0;
  129117               0 :         rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
  129118               0 :         if( rc==SQLITE_OK ){
  129119                 :           int iCol;
  129120               0 :           const char *a = sqlite3_column_blob(pSelectDocsize, 0);
  129121               0 :           for(iCol=0; iCol<pInfo->nCol; iCol++){
  129122                 :             sqlite3_int64 nToken;
  129123               0 :             a += sqlite3Fts3GetVarint(a, &nToken);
  129124               0 :             pInfo->aMatchinfo[iCol] = (u32)nToken;
  129125                 :           }
  129126                 :         }
  129127               0 :         sqlite3_reset(pSelectDocsize);
  129128               0 :         break;
  129129                 :       }
  129130                 : 
  129131                 :       case FTS3_MATCHINFO_LCS:
  129132               0 :         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
  129133               0 :         if( rc==SQLITE_OK ){
  129134               0 :           rc = fts3MatchinfoLcs(pCsr, pInfo);
  129135                 :         }
  129136               0 :         break;
  129137                 : 
  129138                 :       default: {
  129139                 :         Fts3Expr *pExpr;
  129140               0 :         assert( zArg[i]==FTS3_MATCHINFO_HITS );
  129141               0 :         pExpr = pCsr->pExpr;
  129142               0 :         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
  129143               0 :         if( rc!=SQLITE_OK ) break;
  129144               0 :         if( bGlobal ){
  129145               0 :           if( pCsr->pDeferred ){
  129146               0 :             rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
  129147               0 :             if( rc!=SQLITE_OK ) break;
  129148                 :           }
  129149               0 :           rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
  129150               0 :           if( rc!=SQLITE_OK ) break;
  129151                 :         }
  129152               0 :         (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
  129153               0 :         break;
  129154                 :       }
  129155                 :     }
  129156                 : 
  129157               0 :     pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
  129158                 :   }
  129159                 : 
  129160               0 :   sqlite3_reset(pSelect);
  129161               0 :   return rc;
  129162                 : }
  129163                 : 
  129164                 : 
  129165                 : /*
  129166                 : ** Populate pCsr->aMatchinfo[] with data for the current row. The 
  129167                 : ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
  129168                 : */
  129169               0 : static int fts3GetMatchinfo(
  129170                 :   Fts3Cursor *pCsr,               /* FTS3 Cursor object */
  129171                 :   const char *zArg                /* Second argument to matchinfo() function */
  129172                 : ){
  129173                 :   MatchInfo sInfo;
  129174               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  129175               0 :   int rc = SQLITE_OK;
  129176               0 :   int bGlobal = 0;                /* Collect 'global' stats as well as local */
  129177                 : 
  129178               0 :   memset(&sInfo, 0, sizeof(MatchInfo));
  129179               0 :   sInfo.pCursor = pCsr;
  129180               0 :   sInfo.nCol = pTab->nColumn;
  129181                 : 
  129182                 :   /* If there is cached matchinfo() data, but the format string for the 
  129183                 :   ** cache does not match the format string for this request, discard 
  129184                 :   ** the cached data. */
  129185               0 :   if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
  129186               0 :     assert( pCsr->aMatchinfo );
  129187               0 :     sqlite3_free(pCsr->aMatchinfo);
  129188               0 :     pCsr->zMatchinfo = 0;
  129189               0 :     pCsr->aMatchinfo = 0;
  129190                 :   }
  129191                 : 
  129192                 :   /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
  129193                 :   ** matchinfo function has been called for this query. In this case 
  129194                 :   ** allocate the array used to accumulate the matchinfo data and
  129195                 :   ** initialize those elements that are constant for every row.
  129196                 :   */
  129197               0 :   if( pCsr->aMatchinfo==0 ){
  129198               0 :     int nMatchinfo = 0;           /* Number of u32 elements in match-info */
  129199                 :     int nArg;                     /* Bytes in zArg */
  129200                 :     int i;                        /* Used to iterate through zArg */
  129201                 : 
  129202                 :     /* Determine the number of phrases in the query */
  129203               0 :     pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
  129204               0 :     sInfo.nPhrase = pCsr->nPhrase;
  129205                 : 
  129206                 :     /* Determine the number of integers in the buffer returned by this call. */
  129207               0 :     for(i=0; zArg[i]; i++){
  129208               0 :       nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
  129209                 :     }
  129210                 : 
  129211                 :     /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
  129212               0 :     nArg = (int)strlen(zArg);
  129213               0 :     pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
  129214               0 :     if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
  129215                 : 
  129216               0 :     pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
  129217               0 :     pCsr->nMatchinfo = nMatchinfo;
  129218               0 :     memcpy(pCsr->zMatchinfo, zArg, nArg+1);
  129219               0 :     memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
  129220               0 :     pCsr->isMatchinfoNeeded = 1;
  129221               0 :     bGlobal = 1;
  129222                 :   }
  129223                 : 
  129224               0 :   sInfo.aMatchinfo = pCsr->aMatchinfo;
  129225               0 :   sInfo.nPhrase = pCsr->nPhrase;
  129226               0 :   if( pCsr->isMatchinfoNeeded ){
  129227               0 :     rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
  129228               0 :     pCsr->isMatchinfoNeeded = 0;
  129229                 :   }
  129230                 : 
  129231               0 :   return rc;
  129232                 : }
  129233                 : 
  129234                 : /*
  129235                 : ** Implementation of snippet() function.
  129236                 : */
  129237               0 : SQLITE_PRIVATE void sqlite3Fts3Snippet(
  129238                 :   sqlite3_context *pCtx,          /* SQLite function call context */
  129239                 :   Fts3Cursor *pCsr,               /* Cursor object */
  129240                 :   const char *zStart,             /* Snippet start text - "<b>" */
  129241                 :   const char *zEnd,               /* Snippet end text - "</b>" */
  129242                 :   const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
  129243                 :   int iCol,                       /* Extract snippet from this column */
  129244                 :   int nToken                      /* Approximate number of tokens in snippet */
  129245                 : ){
  129246               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  129247               0 :   int rc = SQLITE_OK;
  129248                 :   int i;
  129249               0 :   StrBuffer res = {0, 0, 0};
  129250                 : 
  129251                 :   /* The returned text includes up to four fragments of text extracted from
  129252                 :   ** the data in the current row. The first iteration of the for(...) loop
  129253                 :   ** below attempts to locate a single fragment of text nToken tokens in 
  129254                 :   ** size that contains at least one instance of all phrases in the query
  129255                 :   ** expression that appear in the current row. If such a fragment of text
  129256                 :   ** cannot be found, the second iteration of the loop attempts to locate
  129257                 :   ** a pair of fragments, and so on.
  129258                 :   */
  129259               0 :   int nSnippet = 0;               /* Number of fragments in this snippet */
  129260                 :   SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
  129261               0 :   int nFToken = -1;               /* Number of tokens in each fragment */
  129262                 : 
  129263               0 :   if( !pCsr->pExpr ){
  129264               0 :     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
  129265               0 :     return;
  129266                 :   }
  129267                 : 
  129268               0 :   for(nSnippet=1; 1; nSnippet++){
  129269                 : 
  129270                 :     int iSnip;                    /* Loop counter 0..nSnippet-1 */
  129271               0 :     u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
  129272               0 :     u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
  129273                 : 
  129274               0 :     if( nToken>=0 ){
  129275               0 :       nFToken = (nToken+nSnippet-1) / nSnippet;
  129276                 :     }else{
  129277               0 :       nFToken = -1 * nToken;
  129278                 :     }
  129279                 : 
  129280               0 :     for(iSnip=0; iSnip<nSnippet; iSnip++){
  129281               0 :       int iBestScore = -1;        /* Best score of columns checked so far */
  129282                 :       int iRead;                  /* Used to iterate through columns */
  129283               0 :       SnippetFragment *pFragment = &aSnippet[iSnip];
  129284                 : 
  129285               0 :       memset(pFragment, 0, sizeof(*pFragment));
  129286                 : 
  129287                 :       /* Loop through all columns of the table being considered for snippets.
  129288                 :       ** If the iCol argument to this function was negative, this means all
  129289                 :       ** columns of the FTS3 table. Otherwise, only column iCol is considered.
  129290                 :       */
  129291               0 :       for(iRead=0; iRead<pTab->nColumn; iRead++){
  129292               0 :         SnippetFragment sF = {0, 0, 0, 0};
  129293                 :         int iS;
  129294               0 :         if( iCol>=0 && iRead!=iCol ) continue;
  129295                 : 
  129296                 :         /* Find the best snippet of nFToken tokens in column iRead. */
  129297               0 :         rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
  129298               0 :         if( rc!=SQLITE_OK ){
  129299               0 :           goto snippet_out;
  129300                 :         }
  129301               0 :         if( iS>iBestScore ){
  129302               0 :           *pFragment = sF;
  129303               0 :           iBestScore = iS;
  129304                 :         }
  129305                 :       }
  129306                 : 
  129307               0 :       mCovered |= pFragment->covered;
  129308                 :     }
  129309                 : 
  129310                 :     /* If all query phrases seen by fts3BestSnippet() are present in at least
  129311                 :     ** one of the nSnippet snippet fragments, break out of the loop.
  129312                 :     */
  129313               0 :     assert( (mCovered&mSeen)==mCovered );
  129314               0 :     if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
  129315               0 :   }
  129316                 : 
  129317               0 :   assert( nFToken>0 );
  129318                 : 
  129319               0 :   for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
  129320               0 :     rc = fts3SnippetText(pCsr, &aSnippet[i], 
  129321               0 :         i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
  129322                 :     );
  129323                 :   }
  129324                 : 
  129325                 :  snippet_out:
  129326               0 :   sqlite3Fts3SegmentsClose(pTab);
  129327               0 :   if( rc!=SQLITE_OK ){
  129328               0 :     sqlite3_result_error_code(pCtx, rc);
  129329               0 :     sqlite3_free(res.z);
  129330                 :   }else{
  129331               0 :     sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
  129332                 :   }
  129333                 : }
  129334                 : 
  129335                 : 
  129336                 : typedef struct TermOffset TermOffset;
  129337                 : typedef struct TermOffsetCtx TermOffsetCtx;
  129338                 : 
  129339                 : struct TermOffset {
  129340                 :   char *pList;                    /* Position-list */
  129341                 :   int iPos;                       /* Position just read from pList */
  129342                 :   int iOff;                       /* Offset of this term from read positions */
  129343                 : };
  129344                 : 
  129345                 : struct TermOffsetCtx {
  129346                 :   Fts3Cursor *pCsr;
  129347                 :   int iCol;                       /* Column of table to populate aTerm for */
  129348                 :   int iTerm;
  129349                 :   sqlite3_int64 iDocid;
  129350                 :   TermOffset *aTerm;
  129351                 : };
  129352                 : 
  129353                 : /*
  129354                 : ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
  129355                 : */
  129356               0 : static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
  129357               0 :   TermOffsetCtx *p = (TermOffsetCtx *)ctx;
  129358                 :   int nTerm;                      /* Number of tokens in phrase */
  129359                 :   int iTerm;                      /* For looping through nTerm phrase terms */
  129360                 :   char *pList;                    /* Pointer to position list for phrase */
  129361               0 :   int iPos = 0;                   /* First position in position-list */
  129362                 : 
  129363                 :   UNUSED_PARAMETER(iPhrase);
  129364               0 :   pList = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol);
  129365               0 :   nTerm = pExpr->pPhrase->nToken;
  129366               0 :   if( pList ){
  129367               0 :     fts3GetDeltaPosition(&pList, &iPos);
  129368               0 :     assert( iPos>=0 );
  129369                 :   }
  129370                 : 
  129371               0 :   for(iTerm=0; iTerm<nTerm; iTerm++){
  129372               0 :     TermOffset *pT = &p->aTerm[p->iTerm++];
  129373               0 :     pT->iOff = nTerm-iTerm-1;
  129374               0 :     pT->pList = pList;
  129375               0 :     pT->iPos = iPos;
  129376                 :   }
  129377                 : 
  129378               0 :   return SQLITE_OK;
  129379                 : }
  129380                 : 
  129381                 : /*
  129382                 : ** Implementation of offsets() function.
  129383                 : */
  129384               0 : SQLITE_PRIVATE void sqlite3Fts3Offsets(
  129385                 :   sqlite3_context *pCtx,          /* SQLite function call context */
  129386                 :   Fts3Cursor *pCsr                /* Cursor object */
  129387                 : ){
  129388               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  129389               0 :   sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
  129390                 :   const char *ZDUMMY;             /* Dummy argument used with xNext() */
  129391                 :   int NDUMMY;                     /* Dummy argument used with xNext() */
  129392                 :   int rc;                         /* Return Code */
  129393                 :   int nToken;                     /* Number of tokens in query */
  129394                 :   int iCol;                       /* Column currently being processed */
  129395               0 :   StrBuffer res = {0, 0, 0};      /* Result string */
  129396                 :   TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
  129397                 : 
  129398               0 :   if( !pCsr->pExpr ){
  129399               0 :     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
  129400               0 :     return;
  129401                 :   }
  129402                 : 
  129403               0 :   memset(&sCtx, 0, sizeof(sCtx));
  129404               0 :   assert( pCsr->isRequireSeek==0 );
  129405                 : 
  129406                 :   /* Count the number of terms in the query */
  129407               0 :   rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
  129408               0 :   if( rc!=SQLITE_OK ) goto offsets_out;
  129409                 : 
  129410                 :   /* Allocate the array of TermOffset iterators. */
  129411               0 :   sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
  129412               0 :   if( 0==sCtx.aTerm ){
  129413               0 :     rc = SQLITE_NOMEM;
  129414               0 :     goto offsets_out;
  129415                 :   }
  129416               0 :   sCtx.iDocid = pCsr->iPrevId;
  129417               0 :   sCtx.pCsr = pCsr;
  129418                 : 
  129419                 :   /* Loop through the table columns, appending offset information to 
  129420                 :   ** string-buffer res for each column.
  129421                 :   */
  129422               0 :   for(iCol=0; iCol<pTab->nColumn; iCol++){
  129423                 :     sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
  129424                 :     int iStart;
  129425                 :     int iEnd;
  129426                 :     int iCurrent;
  129427                 :     const char *zDoc;
  129428                 :     int nDoc;
  129429                 : 
  129430                 :     /* Initialize the contents of sCtx.aTerm[] for column iCol. There is 
  129431                 :     ** no way that this operation can fail, so the return code from
  129432                 :     ** fts3ExprIterate() can be discarded.
  129433                 :     */
  129434               0 :     sCtx.iCol = iCol;
  129435               0 :     sCtx.iTerm = 0;
  129436               0 :     (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
  129437                 : 
  129438                 :     /* Retreive the text stored in column iCol. If an SQL NULL is stored 
  129439                 :     ** in column iCol, jump immediately to the next iteration of the loop.
  129440                 :     ** If an OOM occurs while retrieving the data (this can happen if SQLite
  129441                 :     ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM 
  129442                 :     ** to the caller. 
  129443                 :     */
  129444               0 :     zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
  129445               0 :     nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
  129446               0 :     if( zDoc==0 ){
  129447               0 :       if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
  129448               0 :         continue;
  129449                 :       }
  129450               0 :       rc = SQLITE_NOMEM;
  129451               0 :       goto offsets_out;
  129452                 :     }
  129453                 : 
  129454                 :     /* Initialize a tokenizer iterator to iterate through column iCol. */
  129455               0 :     rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC);
  129456               0 :     if( rc!=SQLITE_OK ) goto offsets_out;
  129457               0 :     pC->pTokenizer = pTab->pTokenizer;
  129458                 : 
  129459               0 :     rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
  129460               0 :     while( rc==SQLITE_OK ){
  129461                 :       int i;                      /* Used to loop through terms */
  129462               0 :       int iMinPos = 0x7FFFFFFF;   /* Position of next token */
  129463               0 :       TermOffset *pTerm = 0;      /* TermOffset associated with next token */
  129464                 : 
  129465               0 :       for(i=0; i<nToken; i++){
  129466               0 :         TermOffset *pT = &sCtx.aTerm[i];
  129467               0 :         if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
  129468               0 :           iMinPos = pT->iPos-pT->iOff;
  129469               0 :           pTerm = pT;
  129470                 :         }
  129471                 :       }
  129472                 : 
  129473               0 :       if( !pTerm ){
  129474                 :         /* All offsets for this column have been gathered. */
  129475               0 :         rc = SQLITE_DONE;
  129476                 :       }else{
  129477               0 :         assert( iCurrent<=iMinPos );
  129478               0 :         if( 0==(0xFE&*pTerm->pList) ){
  129479               0 :           pTerm->pList = 0;
  129480                 :         }else{
  129481               0 :           fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
  129482                 :         }
  129483               0 :         while( rc==SQLITE_OK && iCurrent<iMinPos ){
  129484               0 :           rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
  129485                 :         }
  129486               0 :         if( rc==SQLITE_OK ){
  129487                 :           char aBuffer[64];
  129488               0 :           sqlite3_snprintf(sizeof(aBuffer), aBuffer, 
  129489               0 :               "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
  129490                 :           );
  129491               0 :           rc = fts3StringAppend(&res, aBuffer, -1);
  129492               0 :         }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
  129493               0 :           rc = FTS_CORRUPT_VTAB;
  129494                 :         }
  129495                 :       }
  129496                 :     }
  129497               0 :     if( rc==SQLITE_DONE ){
  129498               0 :       rc = SQLITE_OK;
  129499                 :     }
  129500                 : 
  129501               0 :     pMod->xClose(pC);
  129502               0 :     if( rc!=SQLITE_OK ) goto offsets_out;
  129503                 :   }
  129504                 : 
  129505                 :  offsets_out:
  129506               0 :   sqlite3_free(sCtx.aTerm);
  129507               0 :   assert( rc!=SQLITE_DONE );
  129508               0 :   sqlite3Fts3SegmentsClose(pTab);
  129509               0 :   if( rc!=SQLITE_OK ){
  129510               0 :     sqlite3_result_error_code(pCtx,  rc);
  129511               0 :     sqlite3_free(res.z);
  129512                 :   }else{
  129513               0 :     sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
  129514                 :   }
  129515               0 :   return;
  129516                 : }
  129517                 : 
  129518                 : /*
  129519                 : ** Implementation of matchinfo() function.
  129520                 : */
  129521               0 : SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
  129522                 :   sqlite3_context *pContext,      /* Function call context */
  129523                 :   Fts3Cursor *pCsr,               /* FTS3 table cursor */
  129524                 :   const char *zArg                /* Second arg to matchinfo() function */
  129525                 : ){
  129526               0 :   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  129527                 :   int rc;
  129528                 :   int i;
  129529                 :   const char *zFormat;
  129530                 : 
  129531               0 :   if( zArg ){
  129532               0 :     for(i=0; zArg[i]; i++){
  129533               0 :       char *zErr = 0;
  129534               0 :       if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
  129535               0 :         sqlite3_result_error(pContext, zErr, -1);
  129536               0 :         sqlite3_free(zErr);
  129537               0 :         return;
  129538                 :       }
  129539                 :     }
  129540               0 :     zFormat = zArg;
  129541                 :   }else{
  129542               0 :     zFormat = FTS3_MATCHINFO_DEFAULT;
  129543                 :   }
  129544                 : 
  129545               0 :   if( !pCsr->pExpr ){
  129546               0 :     sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
  129547               0 :     return;
  129548                 :   }
  129549                 : 
  129550                 :   /* Retrieve matchinfo() data. */
  129551               0 :   rc = fts3GetMatchinfo(pCsr, zFormat);
  129552               0 :   sqlite3Fts3SegmentsClose(pTab);
  129553                 : 
  129554               0 :   if( rc!=SQLITE_OK ){
  129555               0 :     sqlite3_result_error_code(pContext, rc);
  129556                 :   }else{
  129557               0 :     int n = pCsr->nMatchinfo * sizeof(u32);
  129558               0 :     sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
  129559                 :   }
  129560                 : }
  129561                 : 
  129562                 : #endif
  129563                 : 
  129564                 : /************** End of fts3_snippet.c ****************************************/
  129565                 : /************** Begin file rtree.c *******************************************/
  129566                 : /*
  129567                 : ** 2001 September 15
  129568                 : **
  129569                 : ** The author disclaims copyright to this source code.  In place of
  129570                 : ** a legal notice, here is a blessing:
  129571                 : **
  129572                 : **    May you do good and not evil.
  129573                 : **    May you find forgiveness for yourself and forgive others.
  129574                 : **    May you share freely, never taking more than you give.
  129575                 : **
  129576                 : *************************************************************************
  129577                 : ** This file contains code for implementations of the r-tree and r*-tree
  129578                 : ** algorithms packaged as an SQLite virtual table module.
  129579                 : */
  129580                 : 
  129581                 : /*
  129582                 : ** Database Format of R-Tree Tables
  129583                 : ** --------------------------------
  129584                 : **
  129585                 : ** The data structure for a single virtual r-tree table is stored in three 
  129586                 : ** native SQLite tables declared as follows. In each case, the '%' character
  129587                 : ** in the table name is replaced with the user-supplied name of the r-tree
  129588                 : ** table.
  129589                 : **
  129590                 : **   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
  129591                 : **   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
  129592                 : **   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
  129593                 : **
  129594                 : ** The data for each node of the r-tree structure is stored in the %_node
  129595                 : ** table. For each node that is not the root node of the r-tree, there is
  129596                 : ** an entry in the %_parent table associating the node with its parent.
  129597                 : ** And for each row of data in the table, there is an entry in the %_rowid
  129598                 : ** table that maps from the entries rowid to the id of the node that it
  129599                 : ** is stored on.
  129600                 : **
  129601                 : ** The root node of an r-tree always exists, even if the r-tree table is
  129602                 : ** empty. The nodeno of the root node is always 1. All other nodes in the
  129603                 : ** table must be the same size as the root node. The content of each node
  129604                 : ** is formatted as follows:
  129605                 : **
  129606                 : **   1. If the node is the root node (node 1), then the first 2 bytes
  129607                 : **      of the node contain the tree depth as a big-endian integer.
  129608                 : **      For non-root nodes, the first 2 bytes are left unused.
  129609                 : **
  129610                 : **   2. The next 2 bytes contain the number of entries currently 
  129611                 : **      stored in the node.
  129612                 : **
  129613                 : **   3. The remainder of the node contains the node entries. Each entry
  129614                 : **      consists of a single 8-byte integer followed by an even number
  129615                 : **      of 4-byte coordinates. For leaf nodes the integer is the rowid
  129616                 : **      of a record. For internal nodes it is the node number of a
  129617                 : **      child page.
  129618                 : */
  129619                 : 
  129620                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
  129621                 : 
  129622                 : /*
  129623                 : ** This file contains an implementation of a couple of different variants
  129624                 : ** of the r-tree algorithm. See the README file for further details. The 
  129625                 : ** same data-structure is used for all, but the algorithms for insert and
  129626                 : ** delete operations vary. The variants used are selected at compile time 
  129627                 : ** by defining the following symbols:
  129628                 : */
  129629                 : 
  129630                 : /* Either, both or none of the following may be set to activate 
  129631                 : ** r*tree variant algorithms.
  129632                 : */
  129633                 : #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
  129634                 : #define VARIANT_RSTARTREE_REINSERT      1
  129635                 : 
  129636                 : /* 
  129637                 : ** Exactly one of the following must be set to 1.
  129638                 : */
  129639                 : #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
  129640                 : #define VARIANT_GUTTMAN_LINEAR_SPLIT    0
  129641                 : #define VARIANT_RSTARTREE_SPLIT         1
  129642                 : 
  129643                 : #define VARIANT_GUTTMAN_SPLIT \
  129644                 :         (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
  129645                 : 
  129646                 : #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
  129647                 :   #define PickNext QuadraticPickNext
  129648                 :   #define PickSeeds QuadraticPickSeeds
  129649                 :   #define AssignCells splitNodeGuttman
  129650                 : #endif
  129651                 : #if VARIANT_GUTTMAN_LINEAR_SPLIT
  129652                 :   #define PickNext LinearPickNext
  129653                 :   #define PickSeeds LinearPickSeeds
  129654                 :   #define AssignCells splitNodeGuttman
  129655                 : #endif
  129656                 : #if VARIANT_RSTARTREE_SPLIT
  129657                 :   #define AssignCells splitNodeStartree
  129658                 : #endif
  129659                 : 
  129660                 : #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
  129661                 : # define NDEBUG 1
  129662                 : #endif
  129663                 : 
  129664                 : #ifndef SQLITE_CORE
  129665                 :   SQLITE_EXTENSION_INIT1
  129666                 : #else
  129667                 : #endif
  129668                 : 
  129669                 : /* #include <string.h> */
  129670                 : /* #include <assert.h> */
  129671                 : 
  129672                 : #ifndef SQLITE_AMALGAMATION
  129673                 : #include "sqlite3rtree.h"
  129674                 : typedef sqlite3_int64 i64;
  129675                 : typedef unsigned char u8;
  129676                 : typedef unsigned int u32;
  129677                 : #endif
  129678                 : 
  129679                 : /*  The following macro is used to suppress compiler warnings.
  129680                 : */
  129681                 : #ifndef UNUSED_PARAMETER
  129682                 : # define UNUSED_PARAMETER(x) (void)(x)
  129683                 : #endif
  129684                 : 
  129685                 : typedef struct Rtree Rtree;
  129686                 : typedef struct RtreeCursor RtreeCursor;
  129687                 : typedef struct RtreeNode RtreeNode;
  129688                 : typedef struct RtreeCell RtreeCell;
  129689                 : typedef struct RtreeConstraint RtreeConstraint;
  129690                 : typedef struct RtreeMatchArg RtreeMatchArg;
  129691                 : typedef struct RtreeGeomCallback RtreeGeomCallback;
  129692                 : typedef union RtreeCoord RtreeCoord;
  129693                 : 
  129694                 : /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
  129695                 : #define RTREE_MAX_DIMENSIONS 5
  129696                 : 
  129697                 : /* Size of hash table Rtree.aHash. This hash table is not expected to
  129698                 : ** ever contain very many entries, so a fixed number of buckets is 
  129699                 : ** used.
  129700                 : */
  129701                 : #define HASHSIZE 128
  129702                 : 
  129703                 : /* 
  129704                 : ** An rtree virtual-table object.
  129705                 : */
  129706                 : struct Rtree {
  129707                 :   sqlite3_vtab base;
  129708                 :   sqlite3 *db;                /* Host database connection */
  129709                 :   int iNodeSize;              /* Size in bytes of each node in the node table */
  129710                 :   int nDim;                   /* Number of dimensions */
  129711                 :   int nBytesPerCell;          /* Bytes consumed per cell */
  129712                 :   int iDepth;                 /* Current depth of the r-tree structure */
  129713                 :   char *zDb;                  /* Name of database containing r-tree table */
  129714                 :   char *zName;                /* Name of r-tree table */ 
  129715                 :   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 
  129716                 :   int nBusy;                  /* Current number of users of this structure */
  129717                 : 
  129718                 :   /* List of nodes removed during a CondenseTree operation. List is
  129719                 :   ** linked together via the pointer normally used for hash chains -
  129720                 :   ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 
  129721                 :   ** headed by the node (leaf nodes have RtreeNode.iNode==0).
  129722                 :   */
  129723                 :   RtreeNode *pDeleted;
  129724                 :   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
  129725                 : 
  129726                 :   /* Statements to read/write/delete a record from xxx_node */
  129727                 :   sqlite3_stmt *pReadNode;
  129728                 :   sqlite3_stmt *pWriteNode;
  129729                 :   sqlite3_stmt *pDeleteNode;
  129730                 : 
  129731                 :   /* Statements to read/write/delete a record from xxx_rowid */
  129732                 :   sqlite3_stmt *pReadRowid;
  129733                 :   sqlite3_stmt *pWriteRowid;
  129734                 :   sqlite3_stmt *pDeleteRowid;
  129735                 : 
  129736                 :   /* Statements to read/write/delete a record from xxx_parent */
  129737                 :   sqlite3_stmt *pReadParent;
  129738                 :   sqlite3_stmt *pWriteParent;
  129739                 :   sqlite3_stmt *pDeleteParent;
  129740                 : 
  129741                 :   int eCoordType;
  129742                 : };
  129743                 : 
  129744                 : /* Possible values for eCoordType: */
  129745                 : #define RTREE_COORD_REAL32 0
  129746                 : #define RTREE_COORD_INT32  1
  129747                 : 
  129748                 : /*
  129749                 : ** The minimum number of cells allowed for a node is a third of the 
  129750                 : ** maximum. In Gutman's notation:
  129751                 : **
  129752                 : **     m = M/3
  129753                 : **
  129754                 : ** If an R*-tree "Reinsert" operation is required, the same number of
  129755                 : ** cells are removed from the overfull node and reinserted into the tree.
  129756                 : */
  129757                 : #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
  129758                 : #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
  129759                 : #define RTREE_MAXCELLS 51
  129760                 : 
  129761                 : /*
  129762                 : ** The smallest possible node-size is (512-64)==448 bytes. And the largest
  129763                 : ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
  129764                 : ** Therefore all non-root nodes must contain at least 3 entries. Since 
  129765                 : ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
  129766                 : ** 40 or less.
  129767                 : */
  129768                 : #define RTREE_MAX_DEPTH 40
  129769                 : 
  129770                 : /* 
  129771                 : ** An rtree cursor object.
  129772                 : */
  129773                 : struct RtreeCursor {
  129774                 :   sqlite3_vtab_cursor base;
  129775                 :   RtreeNode *pNode;                 /* Node cursor is currently pointing at */
  129776                 :   int iCell;                        /* Index of current cell in pNode */
  129777                 :   int iStrategy;                    /* Copy of idxNum search parameter */
  129778                 :   int nConstraint;                  /* Number of entries in aConstraint */
  129779                 :   RtreeConstraint *aConstraint;     /* Search constraints. */
  129780                 : };
  129781                 : 
  129782                 : union RtreeCoord {
  129783                 :   float f;
  129784                 :   int i;
  129785                 : };
  129786                 : 
  129787                 : /*
  129788                 : ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
  129789                 : ** formatted as a double. This macro assumes that local variable pRtree points
  129790                 : ** to the Rtree structure associated with the RtreeCoord.
  129791                 : */
  129792                 : #define DCOORD(coord) (                           \
  129793                 :   (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
  129794                 :     ((double)coord.f) :                           \
  129795                 :     ((double)coord.i)                             \
  129796                 : )
  129797                 : 
  129798                 : /*
  129799                 : ** A search constraint.
  129800                 : */
  129801                 : struct RtreeConstraint {
  129802                 :   int iCoord;                     /* Index of constrained coordinate */
  129803                 :   int op;                         /* Constraining operation */
  129804                 :   double rValue;                  /* Constraint value. */
  129805                 :   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
  129806                 :   sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
  129807                 : };
  129808                 : 
  129809                 : /* Possible values for RtreeConstraint.op */
  129810                 : #define RTREE_EQ    0x41
  129811                 : #define RTREE_LE    0x42
  129812                 : #define RTREE_LT    0x43
  129813                 : #define RTREE_GE    0x44
  129814                 : #define RTREE_GT    0x45
  129815                 : #define RTREE_MATCH 0x46
  129816                 : 
  129817                 : /* 
  129818                 : ** An rtree structure node.
  129819                 : */
  129820                 : struct RtreeNode {
  129821                 :   RtreeNode *pParent;               /* Parent node */
  129822                 :   i64 iNode;
  129823                 :   int nRef;
  129824                 :   int isDirty;
  129825                 :   u8 *zData;
  129826                 :   RtreeNode *pNext;                 /* Next node in this hash chain */
  129827                 : };
  129828                 : #define NCELL(pNode) readInt16(&(pNode)->zData[2])
  129829                 : 
  129830                 : /* 
  129831                 : ** Structure to store a deserialized rtree record.
  129832                 : */
  129833                 : struct RtreeCell {
  129834                 :   i64 iRowid;
  129835                 :   RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
  129836                 : };
  129837                 : 
  129838                 : 
  129839                 : /*
  129840                 : ** Value for the first field of every RtreeMatchArg object. The MATCH
  129841                 : ** operator tests that the first field of a blob operand matches this
  129842                 : ** value to avoid operating on invalid blobs (which could cause a segfault).
  129843                 : */
  129844                 : #define RTREE_GEOMETRY_MAGIC 0x891245AB
  129845                 : 
  129846                 : /*
  129847                 : ** An instance of this structure must be supplied as a blob argument to
  129848                 : ** the right-hand-side of an SQL MATCH operator used to constrain an
  129849                 : ** r-tree query.
  129850                 : */
  129851                 : struct RtreeMatchArg {
  129852                 :   u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
  129853                 :   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
  129854                 :   void *pContext;
  129855                 :   int nParam;
  129856                 :   double aParam[1];
  129857                 : };
  129858                 : 
  129859                 : /*
  129860                 : ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
  129861                 : ** a single instance of the following structure is allocated. It is used
  129862                 : ** as the context for the user-function created by by s_r_g_c(). The object
  129863                 : ** is eventually deleted by the destructor mechanism provided by
  129864                 : ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
  129865                 : ** the geometry callback function).
  129866                 : */
  129867                 : struct RtreeGeomCallback {
  129868                 :   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *);
  129869                 :   void *pContext;
  129870                 : };
  129871                 : 
  129872                 : #ifndef MAX
  129873                 : # define MAX(x,y) ((x) < (y) ? (y) : (x))
  129874                 : #endif
  129875                 : #ifndef MIN
  129876                 : # define MIN(x,y) ((x) > (y) ? (y) : (x))
  129877                 : #endif
  129878                 : 
  129879                 : /*
  129880                 : ** Functions to deserialize a 16 bit integer, 32 bit real number and
  129881                 : ** 64 bit integer. The deserialized value is returned.
  129882                 : */
  129883                 : static int readInt16(u8 *p){
  129884                 :   return (p[0]<<8) + p[1];
  129885                 : }
  129886                 : static void readCoord(u8 *p, RtreeCoord *pCoord){
  129887                 :   u32 i = (
  129888                 :     (((u32)p[0]) << 24) + 
  129889                 :     (((u32)p[1]) << 16) + 
  129890                 :     (((u32)p[2]) <<  8) + 
  129891                 :     (((u32)p[3]) <<  0)
  129892                 :   );
  129893                 :   *(u32 *)pCoord = i;
  129894                 : }
  129895                 : static i64 readInt64(u8 *p){
  129896                 :   return (
  129897                 :     (((i64)p[0]) << 56) + 
  129898                 :     (((i64)p[1]) << 48) + 
  129899                 :     (((i64)p[2]) << 40) + 
  129900                 :     (((i64)p[3]) << 32) + 
  129901                 :     (((i64)p[4]) << 24) + 
  129902                 :     (((i64)p[5]) << 16) + 
  129903                 :     (((i64)p[6]) <<  8) + 
  129904                 :     (((i64)p[7]) <<  0)
  129905                 :   );
  129906                 : }
  129907                 : 
  129908                 : /*
  129909                 : ** Functions to serialize a 16 bit integer, 32 bit real number and
  129910                 : ** 64 bit integer. The value returned is the number of bytes written
  129911                 : ** to the argument buffer (always 2, 4 and 8 respectively).
  129912                 : */
  129913                 : static int writeInt16(u8 *p, int i){
  129914                 :   p[0] = (i>> 8)&0xFF;
  129915                 :   p[1] = (i>> 0)&0xFF;
  129916                 :   return 2;
  129917                 : }
  129918                 : static int writeCoord(u8 *p, RtreeCoord *pCoord){
  129919                 :   u32 i;
  129920                 :   assert( sizeof(RtreeCoord)==4 );
  129921                 :   assert( sizeof(u32)==4 );
  129922                 :   i = *(u32 *)pCoord;
  129923                 :   p[0] = (i>>24)&0xFF;
  129924                 :   p[1] = (i>>16)&0xFF;
  129925                 :   p[2] = (i>> 8)&0xFF;
  129926                 :   p[3] = (i>> 0)&0xFF;
  129927                 :   return 4;
  129928                 : }
  129929                 : static int writeInt64(u8 *p, i64 i){
  129930                 :   p[0] = (i>>56)&0xFF;
  129931                 :   p[1] = (i>>48)&0xFF;
  129932                 :   p[2] = (i>>40)&0xFF;
  129933                 :   p[3] = (i>>32)&0xFF;
  129934                 :   p[4] = (i>>24)&0xFF;
  129935                 :   p[5] = (i>>16)&0xFF;
  129936                 :   p[6] = (i>> 8)&0xFF;
  129937                 :   p[7] = (i>> 0)&0xFF;
  129938                 :   return 8;
  129939                 : }
  129940                 : 
  129941                 : /*
  129942                 : ** Increment the reference count of node p.
  129943                 : */
  129944                 : static void nodeReference(RtreeNode *p){
  129945                 :   if( p ){
  129946                 :     p->nRef++;
  129947                 :   }
  129948                 : }
  129949                 : 
  129950                 : /*
  129951                 : ** Clear the content of node p (set all bytes to 0x00).
  129952                 : */
  129953                 : static void nodeZero(Rtree *pRtree, RtreeNode *p){
  129954                 :   memset(&p->zData[2], 0, pRtree->iNodeSize-2);
  129955                 :   p->isDirty = 1;
  129956                 : }
  129957                 : 
  129958                 : /*
  129959                 : ** Given a node number iNode, return the corresponding key to use
  129960                 : ** in the Rtree.aHash table.
  129961                 : */
  129962                 : static int nodeHash(i64 iNode){
  129963                 :   return (
  129964                 :     (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ 
  129965                 :     (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
  129966                 :   ) % HASHSIZE;
  129967                 : }
  129968                 : 
  129969                 : /*
  129970                 : ** Search the node hash table for node iNode. If found, return a pointer
  129971                 : ** to it. Otherwise, return 0.
  129972                 : */
  129973                 : static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
  129974                 :   RtreeNode *p;
  129975                 :   for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
  129976                 :   return p;
  129977                 : }
  129978                 : 
  129979                 : /*
  129980                 : ** Add node pNode to the node hash table.
  129981                 : */
  129982                 : static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
  129983                 :   int iHash;
  129984                 :   assert( pNode->pNext==0 );
  129985                 :   iHash = nodeHash(pNode->iNode);
  129986                 :   pNode->pNext = pRtree->aHash[iHash];
  129987                 :   pRtree->aHash[iHash] = pNode;
  129988                 : }
  129989                 : 
  129990                 : /*
  129991                 : ** Remove node pNode from the node hash table.
  129992                 : */
  129993                 : static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
  129994                 :   RtreeNode **pp;
  129995                 :   if( pNode->iNode!=0 ){
  129996                 :     pp = &pRtree->aHash[nodeHash(pNode->iNode)];
  129997                 :     for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
  129998                 :     *pp = pNode->pNext;
  129999                 :     pNode->pNext = 0;
  130000                 :   }
  130001                 : }
  130002                 : 
  130003                 : /*
  130004                 : ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
  130005                 : ** indicating that node has not yet been assigned a node number. It is
  130006                 : ** assigned a node number when nodeWrite() is called to write the
  130007                 : ** node contents out to the database.
  130008                 : */
  130009                 : static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
  130010                 :   RtreeNode *pNode;
  130011                 :   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
  130012                 :   if( pNode ){
  130013                 :     memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
  130014                 :     pNode->zData = (u8 *)&pNode[1];
  130015                 :     pNode->nRef = 1;
  130016                 :     pNode->pParent = pParent;
  130017                 :     pNode->isDirty = 1;
  130018                 :     nodeReference(pParent);
  130019                 :   }
  130020                 :   return pNode;
  130021                 : }
  130022                 : 
  130023                 : /*
  130024                 : ** Obtain a reference to an r-tree node.
  130025                 : */
  130026                 : static int
  130027                 : nodeAcquire(
  130028                 :   Rtree *pRtree,             /* R-tree structure */
  130029                 :   i64 iNode,                 /* Node number to load */
  130030                 :   RtreeNode *pParent,        /* Either the parent node or NULL */
  130031                 :   RtreeNode **ppNode         /* OUT: Acquired node */
  130032                 : ){
  130033                 :   int rc;
  130034                 :   int rc2 = SQLITE_OK;
  130035                 :   RtreeNode *pNode;
  130036                 : 
  130037                 :   /* Check if the requested node is already in the hash table. If so,
  130038                 :   ** increase its reference count and return it.
  130039                 :   */
  130040                 :   if( (pNode = nodeHashLookup(pRtree, iNode)) ){
  130041                 :     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
  130042                 :     if( pParent && !pNode->pParent ){
  130043                 :       nodeReference(pParent);
  130044                 :       pNode->pParent = pParent;
  130045                 :     }
  130046                 :     pNode->nRef++;
  130047                 :     *ppNode = pNode;
  130048                 :     return SQLITE_OK;
  130049                 :   }
  130050                 : 
  130051                 :   sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
  130052                 :   rc = sqlite3_step(pRtree->pReadNode);
  130053                 :   if( rc==SQLITE_ROW ){
  130054                 :     const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
  130055                 :     if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
  130056                 :       pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
  130057                 :       if( !pNode ){
  130058                 :         rc2 = SQLITE_NOMEM;
  130059                 :       }else{
  130060                 :         pNode->pParent = pParent;
  130061                 :         pNode->zData = (u8 *)&pNode[1];
  130062                 :         pNode->nRef = 1;
  130063                 :         pNode->iNode = iNode;
  130064                 :         pNode->isDirty = 0;
  130065                 :         pNode->pNext = 0;
  130066                 :         memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
  130067                 :         nodeReference(pParent);
  130068                 :       }
  130069                 :     }
  130070                 :   }
  130071                 :   rc = sqlite3_reset(pRtree->pReadNode);
  130072                 :   if( rc==SQLITE_OK ) rc = rc2;
  130073                 : 
  130074                 :   /* If the root node was just loaded, set pRtree->iDepth to the height
  130075                 :   ** of the r-tree structure. A height of zero means all data is stored on
  130076                 :   ** the root node. A height of one means the children of the root node
  130077                 :   ** are the leaves, and so on. If the depth as specified on the root node
  130078                 :   ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
  130079                 :   */
  130080                 :   if( pNode && iNode==1 ){
  130081                 :     pRtree->iDepth = readInt16(pNode->zData);
  130082                 :     if( pRtree->iDepth>RTREE_MAX_DEPTH ){
  130083                 :       rc = SQLITE_CORRUPT_VTAB;
  130084                 :     }
  130085                 :   }
  130086                 : 
  130087                 :   /* If no error has occurred so far, check if the "number of entries"
  130088                 :   ** field on the node is too large. If so, set the return code to 
  130089                 :   ** SQLITE_CORRUPT_VTAB.
  130090                 :   */
  130091                 :   if( pNode && rc==SQLITE_OK ){
  130092                 :     if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
  130093                 :       rc = SQLITE_CORRUPT_VTAB;
  130094                 :     }
  130095                 :   }
  130096                 : 
  130097                 :   if( rc==SQLITE_OK ){
  130098                 :     if( pNode!=0 ){
  130099                 :       nodeHashInsert(pRtree, pNode);
  130100                 :     }else{
  130101                 :       rc = SQLITE_CORRUPT_VTAB;
  130102                 :     }
  130103                 :     *ppNode = pNode;
  130104                 :   }else{
  130105                 :     sqlite3_free(pNode);
  130106                 :     *ppNode = 0;
  130107                 :   }
  130108                 : 
  130109                 :   return rc;
  130110                 : }
  130111                 : 
  130112                 : /*
  130113                 : ** Overwrite cell iCell of node pNode with the contents of pCell.
  130114                 : */
  130115                 : static void nodeOverwriteCell(
  130116                 :   Rtree *pRtree, 
  130117                 :   RtreeNode *pNode,  
  130118                 :   RtreeCell *pCell, 
  130119                 :   int iCell
  130120                 : ){
  130121                 :   int ii;
  130122                 :   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
  130123                 :   p += writeInt64(p, pCell->iRowid);
  130124                 :   for(ii=0; ii<(pRtree->nDim*2); ii++){
  130125                 :     p += writeCoord(p, &pCell->aCoord[ii]);
  130126                 :   }
  130127                 :   pNode->isDirty = 1;
  130128                 : }
  130129                 : 
  130130                 : /*
  130131                 : ** Remove cell the cell with index iCell from node pNode.
  130132                 : */
  130133                 : static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
  130134                 :   u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
  130135                 :   u8 *pSrc = &pDst[pRtree->nBytesPerCell];
  130136                 :   int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
  130137                 :   memmove(pDst, pSrc, nByte);
  130138                 :   writeInt16(&pNode->zData[2], NCELL(pNode)-1);
  130139                 :   pNode->isDirty = 1;
  130140                 : }
  130141                 : 
  130142                 : /*
  130143                 : ** Insert the contents of cell pCell into node pNode. If the insert
  130144                 : ** is successful, return SQLITE_OK.
  130145                 : **
  130146                 : ** If there is not enough free space in pNode, return SQLITE_FULL.
  130147                 : */
  130148                 : static int
  130149                 : nodeInsertCell(
  130150                 :   Rtree *pRtree, 
  130151                 :   RtreeNode *pNode, 
  130152                 :   RtreeCell *pCell 
  130153                 : ){
  130154                 :   int nCell;                    /* Current number of cells in pNode */
  130155                 :   int nMaxCell;                 /* Maximum number of cells for pNode */
  130156                 : 
  130157                 :   nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
  130158                 :   nCell = NCELL(pNode);
  130159                 : 
  130160                 :   assert( nCell<=nMaxCell );
  130161                 :   if( nCell<nMaxCell ){
  130162                 :     nodeOverwriteCell(pRtree, pNode, pCell, nCell);
  130163                 :     writeInt16(&pNode->zData[2], nCell+1);
  130164                 :     pNode->isDirty = 1;
  130165                 :   }
  130166                 : 
  130167                 :   return (nCell==nMaxCell);
  130168                 : }
  130169                 : 
  130170                 : /*
  130171                 : ** If the node is dirty, write it out to the database.
  130172                 : */
  130173                 : static int
  130174                 : nodeWrite(Rtree *pRtree, RtreeNode *pNode){
  130175                 :   int rc = SQLITE_OK;
  130176                 :   if( pNode->isDirty ){
  130177                 :     sqlite3_stmt *p = pRtree->pWriteNode;
  130178                 :     if( pNode->iNode ){
  130179                 :       sqlite3_bind_int64(p, 1, pNode->iNode);
  130180                 :     }else{
  130181                 :       sqlite3_bind_null(p, 1);
  130182                 :     }
  130183                 :     sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
  130184                 :     sqlite3_step(p);
  130185                 :     pNode->isDirty = 0;
  130186                 :     rc = sqlite3_reset(p);
  130187                 :     if( pNode->iNode==0 && rc==SQLITE_OK ){
  130188                 :       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
  130189                 :       nodeHashInsert(pRtree, pNode);
  130190                 :     }
  130191                 :   }
  130192                 :   return rc;
  130193                 : }
  130194                 : 
  130195                 : /*
  130196                 : ** Release a reference to a node. If the node is dirty and the reference
  130197                 : ** count drops to zero, the node data is written to the database.
  130198                 : */
  130199                 : static int
  130200                 : nodeRelease(Rtree *pRtree, RtreeNode *pNode){
  130201                 :   int rc = SQLITE_OK;
  130202                 :   if( pNode ){
  130203                 :     assert( pNode->nRef>0 );
  130204                 :     pNode->nRef--;
  130205                 :     if( pNode->nRef==0 ){
  130206                 :       if( pNode->iNode==1 ){
  130207                 :         pRtree->iDepth = -1;
  130208                 :       }
  130209                 :       if( pNode->pParent ){
  130210                 :         rc = nodeRelease(pRtree, pNode->pParent);
  130211                 :       }
  130212                 :       if( rc==SQLITE_OK ){
  130213                 :         rc = nodeWrite(pRtree, pNode);
  130214                 :       }
  130215                 :       nodeHashDelete(pRtree, pNode);
  130216                 :       sqlite3_free(pNode);
  130217                 :     }
  130218                 :   }
  130219                 :   return rc;
  130220                 : }
  130221                 : 
  130222                 : /*
  130223                 : ** Return the 64-bit integer value associated with cell iCell of
  130224                 : ** node pNode. If pNode is a leaf node, this is a rowid. If it is
  130225                 : ** an internal node, then the 64-bit integer is a child page number.
  130226                 : */
  130227                 : static i64 nodeGetRowid(
  130228                 :   Rtree *pRtree, 
  130229                 :   RtreeNode *pNode, 
  130230                 :   int iCell
  130231                 : ){
  130232                 :   assert( iCell<NCELL(pNode) );
  130233                 :   return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
  130234                 : }
  130235                 : 
  130236                 : /*
  130237                 : ** Return coordinate iCoord from cell iCell in node pNode.
  130238                 : */
  130239                 : static void nodeGetCoord(
  130240                 :   Rtree *pRtree, 
  130241                 :   RtreeNode *pNode, 
  130242                 :   int iCell,
  130243                 :   int iCoord,
  130244                 :   RtreeCoord *pCoord           /* Space to write result to */
  130245                 : ){
  130246                 :   readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
  130247                 : }
  130248                 : 
  130249                 : /*
  130250                 : ** Deserialize cell iCell of node pNode. Populate the structure pointed
  130251                 : ** to by pCell with the results.
  130252                 : */
  130253                 : static void nodeGetCell(
  130254                 :   Rtree *pRtree, 
  130255                 :   RtreeNode *pNode, 
  130256                 :   int iCell,
  130257                 :   RtreeCell *pCell
  130258                 : ){
  130259                 :   int ii;
  130260                 :   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
  130261                 :   for(ii=0; ii<pRtree->nDim*2; ii++){
  130262                 :     nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
  130263                 :   }
  130264                 : }
  130265                 : 
  130266                 : 
  130267                 : /* Forward declaration for the function that does the work of
  130268                 : ** the virtual table module xCreate() and xConnect() methods.
  130269                 : */
  130270                 : static int rtreeInit(
  130271                 :   sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
  130272                 : );
  130273                 : 
  130274                 : /* 
  130275                 : ** Rtree virtual table module xCreate method.
  130276                 : */
  130277                 : static int rtreeCreate(
  130278                 :   sqlite3 *db,
  130279                 :   void *pAux,
  130280                 :   int argc, const char *const*argv,
  130281                 :   sqlite3_vtab **ppVtab,
  130282                 :   char **pzErr
  130283                 : ){
  130284                 :   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
  130285                 : }
  130286                 : 
  130287                 : /* 
  130288                 : ** Rtree virtual table module xConnect method.
  130289                 : */
  130290                 : static int rtreeConnect(
  130291                 :   sqlite3 *db,
  130292                 :   void *pAux,
  130293                 :   int argc, const char *const*argv,
  130294                 :   sqlite3_vtab **ppVtab,
  130295                 :   char **pzErr
  130296                 : ){
  130297                 :   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
  130298                 : }
  130299                 : 
  130300                 : /*
  130301                 : ** Increment the r-tree reference count.
  130302                 : */
  130303                 : static void rtreeReference(Rtree *pRtree){
  130304                 :   pRtree->nBusy++;
  130305                 : }
  130306                 : 
  130307                 : /*
  130308                 : ** Decrement the r-tree reference count. When the reference count reaches
  130309                 : ** zero the structure is deleted.
  130310                 : */
  130311                 : static void rtreeRelease(Rtree *pRtree){
  130312                 :   pRtree->nBusy--;
  130313                 :   if( pRtree->nBusy==0 ){
  130314                 :     sqlite3_finalize(pRtree->pReadNode);
  130315                 :     sqlite3_finalize(pRtree->pWriteNode);
  130316                 :     sqlite3_finalize(pRtree->pDeleteNode);
  130317                 :     sqlite3_finalize(pRtree->pReadRowid);
  130318                 :     sqlite3_finalize(pRtree->pWriteRowid);
  130319                 :     sqlite3_finalize(pRtree->pDeleteRowid);
  130320                 :     sqlite3_finalize(pRtree->pReadParent);
  130321                 :     sqlite3_finalize(pRtree->pWriteParent);
  130322                 :     sqlite3_finalize(pRtree->pDeleteParent);
  130323                 :     sqlite3_free(pRtree);
  130324                 :   }
  130325                 : }
  130326                 : 
  130327                 : /* 
  130328                 : ** Rtree virtual table module xDisconnect method.
  130329                 : */
  130330                 : static int rtreeDisconnect(sqlite3_vtab *pVtab){
  130331                 :   rtreeRelease((Rtree *)pVtab);
  130332                 :   return SQLITE_OK;
  130333                 : }
  130334                 : 
  130335                 : /* 
  130336                 : ** Rtree virtual table module xDestroy method.
  130337                 : */
  130338                 : static int rtreeDestroy(sqlite3_vtab *pVtab){
  130339                 :   Rtree *pRtree = (Rtree *)pVtab;
  130340                 :   int rc;
  130341                 :   char *zCreate = sqlite3_mprintf(
  130342                 :     "DROP TABLE '%q'.'%q_node';"
  130343                 :     "DROP TABLE '%q'.'%q_rowid';"
  130344                 :     "DROP TABLE '%q'.'%q_parent';",
  130345                 :     pRtree->zDb, pRtree->zName, 
  130346                 :     pRtree->zDb, pRtree->zName,
  130347                 :     pRtree->zDb, pRtree->zName
  130348                 :   );
  130349                 :   if( !zCreate ){
  130350                 :     rc = SQLITE_NOMEM;
  130351                 :   }else{
  130352                 :     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
  130353                 :     sqlite3_free(zCreate);
  130354                 :   }
  130355                 :   if( rc==SQLITE_OK ){
  130356                 :     rtreeRelease(pRtree);
  130357                 :   }
  130358                 : 
  130359                 :   return rc;
  130360                 : }
  130361                 : 
  130362                 : /* 
  130363                 : ** Rtree virtual table module xOpen method.
  130364                 : */
  130365                 : static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
  130366                 :   int rc = SQLITE_NOMEM;
  130367                 :   RtreeCursor *pCsr;
  130368                 : 
  130369                 :   pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
  130370                 :   if( pCsr ){
  130371                 :     memset(pCsr, 0, sizeof(RtreeCursor));
  130372                 :     pCsr->base.pVtab = pVTab;
  130373                 :     rc = SQLITE_OK;
  130374                 :   }
  130375                 :   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
  130376                 : 
  130377                 :   return rc;
  130378                 : }
  130379                 : 
  130380                 : 
  130381                 : /*
  130382                 : ** Free the RtreeCursor.aConstraint[] array and its contents.
  130383                 : */
  130384                 : static void freeCursorConstraints(RtreeCursor *pCsr){
  130385                 :   if( pCsr->aConstraint ){
  130386                 :     int i;                        /* Used to iterate through constraint array */
  130387                 :     for(i=0; i<pCsr->nConstraint; i++){
  130388                 :       sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
  130389                 :       if( pGeom ){
  130390                 :         if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
  130391                 :         sqlite3_free(pGeom);
  130392                 :       }
  130393                 :     }
  130394                 :     sqlite3_free(pCsr->aConstraint);
  130395                 :     pCsr->aConstraint = 0;
  130396                 :   }
  130397                 : }
  130398                 : 
  130399                 : /* 
  130400                 : ** Rtree virtual table module xClose method.
  130401                 : */
  130402                 : static int rtreeClose(sqlite3_vtab_cursor *cur){
  130403                 :   Rtree *pRtree = (Rtree *)(cur->pVtab);
  130404                 :   int rc;
  130405                 :   RtreeCursor *pCsr = (RtreeCursor *)cur;
  130406                 :   freeCursorConstraints(pCsr);
  130407                 :   rc = nodeRelease(pRtree, pCsr->pNode);
  130408                 :   sqlite3_free(pCsr);
  130409                 :   return rc;
  130410                 : }
  130411                 : 
  130412                 : /*
  130413                 : ** Rtree virtual table module xEof method.
  130414                 : **
  130415                 : ** Return non-zero if the cursor does not currently point to a valid 
  130416                 : ** record (i.e if the scan has finished), or zero otherwise.
  130417                 : */
  130418                 : static int rtreeEof(sqlite3_vtab_cursor *cur){
  130419                 :   RtreeCursor *pCsr = (RtreeCursor *)cur;
  130420                 :   return (pCsr->pNode==0);
  130421                 : }
  130422                 : 
  130423                 : /*
  130424                 : ** The r-tree constraint passed as the second argument to this function is
  130425                 : ** guaranteed to be a MATCH constraint.
  130426                 : */
  130427                 : static int testRtreeGeom(
  130428                 :   Rtree *pRtree,                  /* R-Tree object */
  130429                 :   RtreeConstraint *pConstraint,   /* MATCH constraint to test */
  130430                 :   RtreeCell *pCell,               /* Cell to test */
  130431                 :   int *pbRes                      /* OUT: Test result */
  130432                 : ){
  130433                 :   int i;
  130434                 :   double aCoord[RTREE_MAX_DIMENSIONS*2];
  130435                 :   int nCoord = pRtree->nDim*2;
  130436                 : 
  130437                 :   assert( pConstraint->op==RTREE_MATCH );
  130438                 :   assert( pConstraint->pGeom );
  130439                 : 
  130440                 :   for(i=0; i<nCoord; i++){
  130441                 :     aCoord[i] = DCOORD(pCell->aCoord[i]);
  130442                 :   }
  130443                 :   return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
  130444                 : }
  130445                 : 
  130446                 : /* 
  130447                 : ** Cursor pCursor currently points to a cell in a non-leaf page.
  130448                 : ** Set *pbEof to true if the sub-tree headed by the cell is filtered
  130449                 : ** (excluded) by the constraints in the pCursor->aConstraint[] 
  130450                 : ** array, or false otherwise.
  130451                 : **
  130452                 : ** Return SQLITE_OK if successful or an SQLite error code if an error
  130453                 : ** occurs within a geometry callback.
  130454                 : */
  130455                 : static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
  130456                 :   RtreeCell cell;
  130457                 :   int ii;
  130458                 :   int bRes = 0;
  130459                 :   int rc = SQLITE_OK;
  130460                 : 
  130461                 :   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
  130462                 :   for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
  130463                 :     RtreeConstraint *p = &pCursor->aConstraint[ii];
  130464                 :     double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
  130465                 :     double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
  130466                 : 
  130467                 :     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
  130468                 :         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
  130469                 :     );
  130470                 : 
  130471                 :     switch( p->op ){
  130472                 :       case RTREE_LE: case RTREE_LT: 
  130473                 :         bRes = p->rValue<cell_min; 
  130474                 :         break;
  130475                 : 
  130476                 :       case RTREE_GE: case RTREE_GT: 
  130477                 :         bRes = p->rValue>cell_max; 
  130478                 :         break;
  130479                 : 
  130480                 :       case RTREE_EQ:
  130481                 :         bRes = (p->rValue>cell_max || p->rValue<cell_min);
  130482                 :         break;
  130483                 : 
  130484                 :       default: {
  130485                 :         assert( p->op==RTREE_MATCH );
  130486                 :         rc = testRtreeGeom(pRtree, p, &cell, &bRes);
  130487                 :         bRes = !bRes;
  130488                 :         break;
  130489                 :       }
  130490                 :     }
  130491                 :   }
  130492                 : 
  130493                 :   *pbEof = bRes;
  130494                 :   return rc;
  130495                 : }
  130496                 : 
  130497                 : /* 
  130498                 : ** Test if the cell that cursor pCursor currently points to
  130499                 : ** would be filtered (excluded) by the constraints in the 
  130500                 : ** pCursor->aConstraint[] array. If so, set *pbEof to true before
  130501                 : ** returning. If the cell is not filtered (excluded) by the constraints,
  130502                 : ** set pbEof to zero.
  130503                 : **
  130504                 : ** Return SQLITE_OK if successful or an SQLite error code if an error
  130505                 : ** occurs within a geometry callback.
  130506                 : **
  130507                 : ** This function assumes that the cell is part of a leaf node.
  130508                 : */
  130509                 : static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
  130510                 :   RtreeCell cell;
  130511                 :   int ii;
  130512                 :   *pbEof = 0;
  130513                 : 
  130514                 :   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
  130515                 :   for(ii=0; ii<pCursor->nConstraint; ii++){
  130516                 :     RtreeConstraint *p = &pCursor->aConstraint[ii];
  130517                 :     double coord = DCOORD(cell.aCoord[p->iCoord]);
  130518                 :     int res;
  130519                 :     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 
  130520                 :         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
  130521                 :     );
  130522                 :     switch( p->op ){
  130523                 :       case RTREE_LE: res = (coord<=p->rValue); break;
  130524                 :       case RTREE_LT: res = (coord<p->rValue);  break;
  130525                 :       case RTREE_GE: res = (coord>=p->rValue); break;
  130526                 :       case RTREE_GT: res = (coord>p->rValue);  break;
  130527                 :       case RTREE_EQ: res = (coord==p->rValue); break;
  130528                 :       default: {
  130529                 :         int rc;
  130530                 :         assert( p->op==RTREE_MATCH );
  130531                 :         rc = testRtreeGeom(pRtree, p, &cell, &res);
  130532                 :         if( rc!=SQLITE_OK ){
  130533                 :           return rc;
  130534                 :         }
  130535                 :         break;
  130536                 :       }
  130537                 :     }
  130538                 : 
  130539                 :     if( !res ){
  130540                 :       *pbEof = 1;
  130541                 :       return SQLITE_OK;
  130542                 :     }
  130543                 :   }
  130544                 : 
  130545                 :   return SQLITE_OK;
  130546                 : }
  130547                 : 
  130548                 : /*
  130549                 : ** Cursor pCursor currently points at a node that heads a sub-tree of
  130550                 : ** height iHeight (if iHeight==0, then the node is a leaf). Descend
  130551                 : ** to point to the left-most cell of the sub-tree that matches the 
  130552                 : ** configured constraints.
  130553                 : */
  130554                 : static int descendToCell(
  130555                 :   Rtree *pRtree, 
  130556                 :   RtreeCursor *pCursor, 
  130557                 :   int iHeight,
  130558                 :   int *pEof                 /* OUT: Set to true if cannot descend */
  130559                 : ){
  130560                 :   int isEof;
  130561                 :   int rc;
  130562                 :   int ii;
  130563                 :   RtreeNode *pChild;
  130564                 :   sqlite3_int64 iRowid;
  130565                 : 
  130566                 :   RtreeNode *pSavedNode = pCursor->pNode;
  130567                 :   int iSavedCell = pCursor->iCell;
  130568                 : 
  130569                 :   assert( iHeight>=0 );
  130570                 : 
  130571                 :   if( iHeight==0 ){
  130572                 :     rc = testRtreeEntry(pRtree, pCursor, &isEof);
  130573                 :   }else{
  130574                 :     rc = testRtreeCell(pRtree, pCursor, &isEof);
  130575                 :   }
  130576                 :   if( rc!=SQLITE_OK || isEof || iHeight==0 ){
  130577                 :     goto descend_to_cell_out;
  130578                 :   }
  130579                 : 
  130580                 :   iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
  130581                 :   rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
  130582                 :   if( rc!=SQLITE_OK ){
  130583                 :     goto descend_to_cell_out;
  130584                 :   }
  130585                 : 
  130586                 :   nodeRelease(pRtree, pCursor->pNode);
  130587                 :   pCursor->pNode = pChild;
  130588                 :   isEof = 1;
  130589                 :   for(ii=0; isEof && ii<NCELL(pChild); ii++){
  130590                 :     pCursor->iCell = ii;
  130591                 :     rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
  130592                 :     if( rc!=SQLITE_OK ){
  130593                 :       goto descend_to_cell_out;
  130594                 :     }
  130595                 :   }
  130596                 : 
  130597                 :   if( isEof ){
  130598                 :     assert( pCursor->pNode==pChild );
  130599                 :     nodeReference(pSavedNode);
  130600                 :     nodeRelease(pRtree, pChild);
  130601                 :     pCursor->pNode = pSavedNode;
  130602                 :     pCursor->iCell = iSavedCell;
  130603                 :   }
  130604                 : 
  130605                 : descend_to_cell_out:
  130606                 :   *pEof = isEof;
  130607                 :   return rc;
  130608                 : }
  130609                 : 
  130610                 : /*
  130611                 : ** One of the cells in node pNode is guaranteed to have a 64-bit 
  130612                 : ** integer value equal to iRowid. Return the index of this cell.
  130613                 : */
  130614                 : static int nodeRowidIndex(
  130615                 :   Rtree *pRtree, 
  130616                 :   RtreeNode *pNode, 
  130617                 :   i64 iRowid,
  130618                 :   int *piIndex
  130619                 : ){
  130620                 :   int ii;
  130621                 :   int nCell = NCELL(pNode);
  130622                 :   for(ii=0; ii<nCell; ii++){
  130623                 :     if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
  130624                 :       *piIndex = ii;
  130625                 :       return SQLITE_OK;
  130626                 :     }
  130627                 :   }
  130628                 :   return SQLITE_CORRUPT_VTAB;
  130629                 : }
  130630                 : 
  130631                 : /*
  130632                 : ** Return the index of the cell containing a pointer to node pNode
  130633                 : ** in its parent. If pNode is the root node, return -1.
  130634                 : */
  130635                 : static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
  130636                 :   RtreeNode *pParent = pNode->pParent;
  130637                 :   if( pParent ){
  130638                 :     return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
  130639                 :   }
  130640                 :   *piIndex = -1;
  130641                 :   return SQLITE_OK;
  130642                 : }
  130643                 : 
  130644                 : /* 
  130645                 : ** Rtree virtual table module xNext method.
  130646                 : */
  130647                 : static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
  130648                 :   Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
  130649                 :   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
  130650                 :   int rc = SQLITE_OK;
  130651                 : 
  130652                 :   /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
  130653                 :   ** already at EOF. It is against the rules to call the xNext() method of
  130654                 :   ** a cursor that has already reached EOF.
  130655                 :   */
  130656                 :   assert( pCsr->pNode );
  130657                 : 
  130658                 :   if( pCsr->iStrategy==1 ){
  130659                 :     /* This "scan" is a direct lookup by rowid. There is no next entry. */
  130660                 :     nodeRelease(pRtree, pCsr->pNode);
  130661                 :     pCsr->pNode = 0;
  130662                 :   }else{
  130663                 :     /* Move to the next entry that matches the configured constraints. */
  130664                 :     int iHeight = 0;
  130665                 :     while( pCsr->pNode ){
  130666                 :       RtreeNode *pNode = pCsr->pNode;
  130667                 :       int nCell = NCELL(pNode);
  130668                 :       for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
  130669                 :         int isEof;
  130670                 :         rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
  130671                 :         if( rc!=SQLITE_OK || !isEof ){
  130672                 :           return rc;
  130673                 :         }
  130674                 :       }
  130675                 :       pCsr->pNode = pNode->pParent;
  130676                 :       rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
  130677                 :       if( rc!=SQLITE_OK ){
  130678                 :         return rc;
  130679                 :       }
  130680                 :       nodeReference(pCsr->pNode);
  130681                 :       nodeRelease(pRtree, pNode);
  130682                 :       iHeight++;
  130683                 :     }
  130684                 :   }
  130685                 : 
  130686                 :   return rc;
  130687                 : }
  130688                 : 
  130689                 : /* 
  130690                 : ** Rtree virtual table module xRowid method.
  130691                 : */
  130692                 : static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
  130693                 :   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
  130694                 :   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
  130695                 : 
  130696                 :   assert(pCsr->pNode);
  130697                 :   *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
  130698                 : 
  130699                 :   return SQLITE_OK;
  130700                 : }
  130701                 : 
  130702                 : /* 
  130703                 : ** Rtree virtual table module xColumn method.
  130704                 : */
  130705                 : static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
  130706                 :   Rtree *pRtree = (Rtree *)cur->pVtab;
  130707                 :   RtreeCursor *pCsr = (RtreeCursor *)cur;
  130708                 : 
  130709                 :   if( i==0 ){
  130710                 :     i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
  130711                 :     sqlite3_result_int64(ctx, iRowid);
  130712                 :   }else{
  130713                 :     RtreeCoord c;
  130714                 :     nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
  130715                 :     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  130716                 :       sqlite3_result_double(ctx, c.f);
  130717                 :     }else{
  130718                 :       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
  130719                 :       sqlite3_result_int(ctx, c.i);
  130720                 :     }
  130721                 :   }
  130722                 : 
  130723                 :   return SQLITE_OK;
  130724                 : }
  130725                 : 
  130726                 : /* 
  130727                 : ** Use nodeAcquire() to obtain the leaf node containing the record with 
  130728                 : ** rowid iRowid. If successful, set *ppLeaf to point to the node and
  130729                 : ** return SQLITE_OK. If there is no such record in the table, set
  130730                 : ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
  130731                 : ** to zero and return an SQLite error code.
  130732                 : */
  130733                 : static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
  130734                 :   int rc;
  130735                 :   *ppLeaf = 0;
  130736                 :   sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
  130737                 :   if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
  130738                 :     i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
  130739                 :     rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
  130740                 :     sqlite3_reset(pRtree->pReadRowid);
  130741                 :   }else{
  130742                 :     rc = sqlite3_reset(pRtree->pReadRowid);
  130743                 :   }
  130744                 :   return rc;
  130745                 : }
  130746                 : 
  130747                 : /*
  130748                 : ** This function is called to configure the RtreeConstraint object passed
  130749                 : ** as the second argument for a MATCH constraint. The value passed as the
  130750                 : ** first argument to this function is the right-hand operand to the MATCH
  130751                 : ** operator.
  130752                 : */
  130753                 : static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
  130754                 :   RtreeMatchArg *p;
  130755                 :   sqlite3_rtree_geometry *pGeom;
  130756                 :   int nBlob;
  130757                 : 
  130758                 :   /* Check that value is actually a blob. */
  130759                 :   if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
  130760                 : 
  130761                 :   /* Check that the blob is roughly the right size. */
  130762                 :   nBlob = sqlite3_value_bytes(pValue);
  130763                 :   if( nBlob<(int)sizeof(RtreeMatchArg) 
  130764                 :    || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
  130765                 :   ){
  130766                 :     return SQLITE_ERROR;
  130767                 :   }
  130768                 : 
  130769                 :   pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
  130770                 :       sizeof(sqlite3_rtree_geometry) + nBlob
  130771                 :   );
  130772                 :   if( !pGeom ) return SQLITE_NOMEM;
  130773                 :   memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
  130774                 :   p = (RtreeMatchArg *)&pGeom[1];
  130775                 : 
  130776                 :   memcpy(p, sqlite3_value_blob(pValue), nBlob);
  130777                 :   if( p->magic!=RTREE_GEOMETRY_MAGIC 
  130778                 :    || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double))
  130779                 :   ){
  130780                 :     sqlite3_free(pGeom);
  130781                 :     return SQLITE_ERROR;
  130782                 :   }
  130783                 : 
  130784                 :   pGeom->pContext = p->pContext;
  130785                 :   pGeom->nParam = p->nParam;
  130786                 :   pGeom->aParam = p->aParam;
  130787                 : 
  130788                 :   pCons->xGeom = p->xGeom;
  130789                 :   pCons->pGeom = pGeom;
  130790                 :   return SQLITE_OK;
  130791                 : }
  130792                 : 
  130793                 : /* 
  130794                 : ** Rtree virtual table module xFilter method.
  130795                 : */
  130796                 : static int rtreeFilter(
  130797                 :   sqlite3_vtab_cursor *pVtabCursor, 
  130798                 :   int idxNum, const char *idxStr,
  130799                 :   int argc, sqlite3_value **argv
  130800                 : ){
  130801                 :   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
  130802                 :   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
  130803                 : 
  130804                 :   RtreeNode *pRoot = 0;
  130805                 :   int ii;
  130806                 :   int rc = SQLITE_OK;
  130807                 : 
  130808                 :   rtreeReference(pRtree);
  130809                 : 
  130810                 :   freeCursorConstraints(pCsr);
  130811                 :   pCsr->iStrategy = idxNum;
  130812                 : 
  130813                 :   if( idxNum==1 ){
  130814                 :     /* Special case - lookup by rowid. */
  130815                 :     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
  130816                 :     i64 iRowid = sqlite3_value_int64(argv[0]);
  130817                 :     rc = findLeafNode(pRtree, iRowid, &pLeaf);
  130818                 :     pCsr->pNode = pLeaf; 
  130819                 :     if( pLeaf ){
  130820                 :       assert( rc==SQLITE_OK );
  130821                 :       rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
  130822                 :     }
  130823                 :   }else{
  130824                 :     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
  130825                 :     ** with the configured constraints. 
  130826                 :     */
  130827                 :     if( argc>0 ){
  130828                 :       pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
  130829                 :       pCsr->nConstraint = argc;
  130830                 :       if( !pCsr->aConstraint ){
  130831                 :         rc = SQLITE_NOMEM;
  130832                 :       }else{
  130833                 :         memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
  130834                 :         assert( (idxStr==0 && argc==0)
  130835                 :                 || (idxStr && (int)strlen(idxStr)==argc*2) );
  130836                 :         for(ii=0; ii<argc; ii++){
  130837                 :           RtreeConstraint *p = &pCsr->aConstraint[ii];
  130838                 :           p->op = idxStr[ii*2];
  130839                 :           p->iCoord = idxStr[ii*2+1]-'a';
  130840                 :           if( p->op==RTREE_MATCH ){
  130841                 :             /* A MATCH operator. The right-hand-side must be a blob that
  130842                 :             ** can be cast into an RtreeMatchArg object. One created using
  130843                 :             ** an sqlite3_rtree_geometry_callback() SQL user function.
  130844                 :             */
  130845                 :             rc = deserializeGeometry(argv[ii], p);
  130846                 :             if( rc!=SQLITE_OK ){
  130847                 :               break;
  130848                 :             }
  130849                 :           }else{
  130850                 :             p->rValue = sqlite3_value_double(argv[ii]);
  130851                 :           }
  130852                 :         }
  130853                 :       }
  130854                 :     }
  130855                 :   
  130856                 :     if( rc==SQLITE_OK ){
  130857                 :       pCsr->pNode = 0;
  130858                 :       rc = nodeAcquire(pRtree, 1, 0, &pRoot);
  130859                 :     }
  130860                 :     if( rc==SQLITE_OK ){
  130861                 :       int isEof = 1;
  130862                 :       int nCell = NCELL(pRoot);
  130863                 :       pCsr->pNode = pRoot;
  130864                 :       for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
  130865                 :         assert( pCsr->pNode==pRoot );
  130866                 :         rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
  130867                 :         if( !isEof ){
  130868                 :           break;
  130869                 :         }
  130870                 :       }
  130871                 :       if( rc==SQLITE_OK && isEof ){
  130872                 :         assert( pCsr->pNode==pRoot );
  130873                 :         nodeRelease(pRtree, pRoot);
  130874                 :         pCsr->pNode = 0;
  130875                 :       }
  130876                 :       assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
  130877                 :     }
  130878                 :   }
  130879                 : 
  130880                 :   rtreeRelease(pRtree);
  130881                 :   return rc;
  130882                 : }
  130883                 : 
  130884                 : /*
  130885                 : ** Rtree virtual table module xBestIndex method. There are three
  130886                 : ** table scan strategies to choose from (in order from most to 
  130887                 : ** least desirable):
  130888                 : **
  130889                 : **   idxNum     idxStr        Strategy
  130890                 : **   ------------------------------------------------
  130891                 : **     1        Unused        Direct lookup by rowid.
  130892                 : **     2        See below     R-tree query or full-table scan.
  130893                 : **   ------------------------------------------------
  130894                 : **
  130895                 : ** If strategy 1 is used, then idxStr is not meaningful. If strategy
  130896                 : ** 2 is used, idxStr is formatted to contain 2 bytes for each 
  130897                 : ** constraint used. The first two bytes of idxStr correspond to 
  130898                 : ** the constraint in sqlite3_index_info.aConstraintUsage[] with
  130899                 : ** (argvIndex==1) etc.
  130900                 : **
  130901                 : ** The first of each pair of bytes in idxStr identifies the constraint
  130902                 : ** operator as follows:
  130903                 : **
  130904                 : **   Operator    Byte Value
  130905                 : **   ----------------------
  130906                 : **      =        0x41 ('A')
  130907                 : **     <=        0x42 ('B')
  130908                 : **      <        0x43 ('C')
  130909                 : **     >=        0x44 ('D')
  130910                 : **      >        0x45 ('E')
  130911                 : **   MATCH       0x46 ('F')
  130912                 : **   ----------------------
  130913                 : **
  130914                 : ** The second of each pair of bytes identifies the coordinate column
  130915                 : ** to which the constraint applies. The leftmost coordinate column
  130916                 : ** is 'a', the second from the left 'b' etc.
  130917                 : */
  130918                 : static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  130919                 :   int rc = SQLITE_OK;
  130920                 :   int ii;
  130921                 : 
  130922                 :   int iIdx = 0;
  130923                 :   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
  130924                 :   memset(zIdxStr, 0, sizeof(zIdxStr));
  130925                 :   UNUSED_PARAMETER(tab);
  130926                 : 
  130927                 :   assert( pIdxInfo->idxStr==0 );
  130928                 :   for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
  130929                 :     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
  130930                 : 
  130931                 :     if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
  130932                 :       /* We have an equality constraint on the rowid. Use strategy 1. */
  130933                 :       int jj;
  130934                 :       for(jj=0; jj<ii; jj++){
  130935                 :         pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
  130936                 :         pIdxInfo->aConstraintUsage[jj].omit = 0;
  130937                 :       }
  130938                 :       pIdxInfo->idxNum = 1;
  130939                 :       pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
  130940                 :       pIdxInfo->aConstraintUsage[jj].omit = 1;
  130941                 : 
  130942                 :       /* This strategy involves a two rowid lookups on an B-Tree structures
  130943                 :       ** and then a linear search of an R-Tree node. This should be 
  130944                 :       ** considered almost as quick as a direct rowid lookup (for which 
  130945                 :       ** sqlite uses an internal cost of 0.0).
  130946                 :       */ 
  130947                 :       pIdxInfo->estimatedCost = 10.0;
  130948                 :       return SQLITE_OK;
  130949                 :     }
  130950                 : 
  130951                 :     if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
  130952                 :       u8 op;
  130953                 :       switch( p->op ){
  130954                 :         case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
  130955                 :         case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
  130956                 :         case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
  130957                 :         case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
  130958                 :         case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
  130959                 :         default:
  130960                 :           assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
  130961                 :           op = RTREE_MATCH; 
  130962                 :           break;
  130963                 :       }
  130964                 :       zIdxStr[iIdx++] = op;
  130965                 :       zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
  130966                 :       pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
  130967                 :       pIdxInfo->aConstraintUsage[ii].omit = 1;
  130968                 :     }
  130969                 :   }
  130970                 : 
  130971                 :   pIdxInfo->idxNum = 2;
  130972                 :   pIdxInfo->needToFreeIdxStr = 1;
  130973                 :   if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
  130974                 :     return SQLITE_NOMEM;
  130975                 :   }
  130976                 :   assert( iIdx>=0 );
  130977                 :   pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1));
  130978                 :   return rc;
  130979                 : }
  130980                 : 
  130981                 : /*
  130982                 : ** Return the N-dimensional volumn of the cell stored in *p.
  130983                 : */
  130984                 : static float cellArea(Rtree *pRtree, RtreeCell *p){
  130985                 :   float area = 1.0;
  130986                 :   int ii;
  130987                 :   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  130988                 :     area = (float)(area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
  130989                 :   }
  130990                 :   return area;
  130991                 : }
  130992                 : 
  130993                 : /*
  130994                 : ** Return the margin length of cell p. The margin length is the sum
  130995                 : ** of the objects size in each dimension.
  130996                 : */
  130997                 : static float cellMargin(Rtree *pRtree, RtreeCell *p){
  130998                 :   float margin = 0.0;
  130999                 :   int ii;
  131000                 :   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  131001                 :     margin += (float)(DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
  131002                 :   }
  131003                 :   return margin;
  131004                 : }
  131005                 : 
  131006                 : /*
  131007                 : ** Store the union of cells p1 and p2 in p1.
  131008                 : */
  131009                 : static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
  131010                 :   int ii;
  131011                 :   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  131012                 :     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  131013                 :       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
  131014                 :       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
  131015                 :     }
  131016                 :   }else{
  131017                 :     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  131018                 :       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
  131019                 :       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
  131020                 :     }
  131021                 :   }
  131022                 : }
  131023                 : 
  131024                 : /*
  131025                 : ** Return true if the area covered by p2 is a subset of the area covered
  131026                 : ** by p1. False otherwise.
  131027                 : */
  131028                 : static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
  131029                 :   int ii;
  131030                 :   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
  131031                 :   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  131032                 :     RtreeCoord *a1 = &p1->aCoord[ii];
  131033                 :     RtreeCoord *a2 = &p2->aCoord[ii];
  131034                 :     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 
  131035                 :      || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 
  131036                 :     ){
  131037                 :       return 0;
  131038                 :     }
  131039                 :   }
  131040                 :   return 1;
  131041                 : }
  131042                 : 
  131043                 : /*
  131044                 : ** Return the amount cell p would grow by if it were unioned with pCell.
  131045                 : */
  131046                 : static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
  131047                 :   float area;
  131048                 :   RtreeCell cell;
  131049                 :   memcpy(&cell, p, sizeof(RtreeCell));
  131050                 :   area = cellArea(pRtree, &cell);
  131051                 :   cellUnion(pRtree, &cell, pCell);
  131052                 :   return (cellArea(pRtree, &cell)-area);
  131053                 : }
  131054                 : 
  131055                 : #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
  131056                 : static float cellOverlap(
  131057                 :   Rtree *pRtree, 
  131058                 :   RtreeCell *p, 
  131059                 :   RtreeCell *aCell, 
  131060                 :   int nCell, 
  131061                 :   int iExclude
  131062                 : ){
  131063                 :   int ii;
  131064                 :   float overlap = 0.0;
  131065                 :   for(ii=0; ii<nCell; ii++){
  131066                 : #if VARIANT_RSTARTREE_CHOOSESUBTREE
  131067                 :     if( ii!=iExclude )
  131068                 : #else
  131069                 :     assert( iExclude==-1 );
  131070                 :     UNUSED_PARAMETER(iExclude);
  131071                 : #endif
  131072                 :     {
  131073                 :       int jj;
  131074                 :       float o = 1.0;
  131075                 :       for(jj=0; jj<(pRtree->nDim*2); jj+=2){
  131076                 :         double x1;
  131077                 :         double x2;
  131078                 : 
  131079                 :         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
  131080                 :         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
  131081                 : 
  131082                 :         if( x2<x1 ){
  131083                 :           o = 0.0;
  131084                 :           break;
  131085                 :         }else{
  131086                 :           o = o * (float)(x2-x1);
  131087                 :         }
  131088                 :       }
  131089                 :       overlap += o;
  131090                 :     }
  131091                 :   }
  131092                 :   return overlap;
  131093                 : }
  131094                 : #endif
  131095                 : 
  131096                 : #if VARIANT_RSTARTREE_CHOOSESUBTREE
  131097                 : static float cellOverlapEnlargement(
  131098                 :   Rtree *pRtree, 
  131099                 :   RtreeCell *p, 
  131100                 :   RtreeCell *pInsert, 
  131101                 :   RtreeCell *aCell, 
  131102                 :   int nCell, 
  131103                 :   int iExclude
  131104                 : ){
  131105                 :   double before;
  131106                 :   double after;
  131107                 :   before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
  131108                 :   cellUnion(pRtree, p, pInsert);
  131109                 :   after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
  131110                 :   return (float)(after-before);
  131111                 : }
  131112                 : #endif
  131113                 : 
  131114                 : 
  131115                 : /*
  131116                 : ** This function implements the ChooseLeaf algorithm from Gutman[84].
  131117                 : ** ChooseSubTree in r*tree terminology.
  131118                 : */
  131119                 : static int ChooseLeaf(
  131120                 :   Rtree *pRtree,               /* Rtree table */
  131121                 :   RtreeCell *pCell,            /* Cell to insert into rtree */
  131122                 :   int iHeight,                 /* Height of sub-tree rooted at pCell */
  131123                 :   RtreeNode **ppLeaf           /* OUT: Selected leaf page */
  131124                 : ){
  131125                 :   int rc;
  131126                 :   int ii;
  131127                 :   RtreeNode *pNode;
  131128                 :   rc = nodeAcquire(pRtree, 1, 0, &pNode);
  131129                 : 
  131130                 :   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
  131131                 :     int iCell;
  131132                 :     sqlite3_int64 iBest = 0;
  131133                 : 
  131134                 :     float fMinGrowth = 0.0;
  131135                 :     float fMinArea = 0.0;
  131136                 : #if VARIANT_RSTARTREE_CHOOSESUBTREE
  131137                 :     float fMinOverlap = 0.0;
  131138                 :     float overlap;
  131139                 : #endif
  131140                 : 
  131141                 :     int nCell = NCELL(pNode);
  131142                 :     RtreeCell cell;
  131143                 :     RtreeNode *pChild;
  131144                 : 
  131145                 :     RtreeCell *aCell = 0;
  131146                 : 
  131147                 : #if VARIANT_RSTARTREE_CHOOSESUBTREE
  131148                 :     if( ii==(pRtree->iDepth-1) ){
  131149                 :       int jj;
  131150                 :       aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
  131151                 :       if( !aCell ){
  131152                 :         rc = SQLITE_NOMEM;
  131153                 :         nodeRelease(pRtree, pNode);
  131154                 :         pNode = 0;
  131155                 :         continue;
  131156                 :       }
  131157                 :       for(jj=0; jj<nCell; jj++){
  131158                 :         nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
  131159                 :       }
  131160                 :     }
  131161                 : #endif
  131162                 : 
  131163                 :     /* Select the child node which will be enlarged the least if pCell
  131164                 :     ** is inserted into it. Resolve ties by choosing the entry with
  131165                 :     ** the smallest area.
  131166                 :     */
  131167                 :     for(iCell=0; iCell<nCell; iCell++){
  131168                 :       int bBest = 0;
  131169                 :       float growth;
  131170                 :       float area;
  131171                 :       nodeGetCell(pRtree, pNode, iCell, &cell);
  131172                 :       growth = cellGrowth(pRtree, &cell, pCell);
  131173                 :       area = cellArea(pRtree, &cell);
  131174                 : 
  131175                 : #if VARIANT_RSTARTREE_CHOOSESUBTREE
  131176                 :       if( ii==(pRtree->iDepth-1) ){
  131177                 :         overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
  131178                 :       }else{
  131179                 :         overlap = 0.0;
  131180                 :       }
  131181                 :       if( (iCell==0) 
  131182                 :        || (overlap<fMinOverlap) 
  131183                 :        || (overlap==fMinOverlap && growth<fMinGrowth)
  131184                 :        || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
  131185                 :       ){
  131186                 :         bBest = 1;
  131187                 :         fMinOverlap = overlap;
  131188                 :       }
  131189                 : #else
  131190                 :       if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
  131191                 :         bBest = 1;
  131192                 :       }
  131193                 : #endif
  131194                 :       if( bBest ){
  131195                 :         fMinGrowth = growth;
  131196                 :         fMinArea = area;
  131197                 :         iBest = cell.iRowid;
  131198                 :       }
  131199                 :     }
  131200                 : 
  131201                 :     sqlite3_free(aCell);
  131202                 :     rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
  131203                 :     nodeRelease(pRtree, pNode);
  131204                 :     pNode = pChild;
  131205                 :   }
  131206                 : 
  131207                 :   *ppLeaf = pNode;
  131208                 :   return rc;
  131209                 : }
  131210                 : 
  131211                 : /*
  131212                 : ** A cell with the same content as pCell has just been inserted into
  131213                 : ** the node pNode. This function updates the bounding box cells in
  131214                 : ** all ancestor elements.
  131215                 : */
  131216                 : static int AdjustTree(
  131217                 :   Rtree *pRtree,                    /* Rtree table */
  131218                 :   RtreeNode *pNode,                 /* Adjust ancestry of this node. */
  131219                 :   RtreeCell *pCell                  /* This cell was just inserted */
  131220                 : ){
  131221                 :   RtreeNode *p = pNode;
  131222                 :   while( p->pParent ){
  131223                 :     RtreeNode *pParent = p->pParent;
  131224                 :     RtreeCell cell;
  131225                 :     int iCell;
  131226                 : 
  131227                 :     if( nodeParentIndex(pRtree, p, &iCell) ){
  131228                 :       return SQLITE_CORRUPT_VTAB;
  131229                 :     }
  131230                 : 
  131231                 :     nodeGetCell(pRtree, pParent, iCell, &cell);
  131232                 :     if( !cellContains(pRtree, &cell, pCell) ){
  131233                 :       cellUnion(pRtree, &cell, pCell);
  131234                 :       nodeOverwriteCell(pRtree, pParent, &cell, iCell);
  131235                 :     }
  131236                 :  
  131237                 :     p = pParent;
  131238                 :   }
  131239                 :   return SQLITE_OK;
  131240                 : }
  131241                 : 
  131242                 : /*
  131243                 : ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
  131244                 : */
  131245                 : static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
  131246                 :   sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
  131247                 :   sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
  131248                 :   sqlite3_step(pRtree->pWriteRowid);
  131249                 :   return sqlite3_reset(pRtree->pWriteRowid);
  131250                 : }
  131251                 : 
  131252                 : /*
  131253                 : ** Write mapping (iNode->iPar) to the <rtree>_parent table.
  131254                 : */
  131255                 : static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
  131256                 :   sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
  131257                 :   sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
  131258                 :   sqlite3_step(pRtree->pWriteParent);
  131259                 :   return sqlite3_reset(pRtree->pWriteParent);
  131260                 : }
  131261                 : 
  131262                 : static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
  131263                 : 
  131264                 : #if VARIANT_GUTTMAN_LINEAR_SPLIT
  131265                 : /*
  131266                 : ** Implementation of the linear variant of the PickNext() function from
  131267                 : ** Guttman[84].
  131268                 : */
  131269                 : static RtreeCell *LinearPickNext(
  131270                 :   Rtree *pRtree,
  131271                 :   RtreeCell *aCell, 
  131272                 :   int nCell, 
  131273                 :   RtreeCell *pLeftBox, 
  131274                 :   RtreeCell *pRightBox,
  131275                 :   int *aiUsed
  131276                 : ){
  131277                 :   int ii;
  131278                 :   for(ii=0; aiUsed[ii]; ii++);
  131279                 :   aiUsed[ii] = 1;
  131280                 :   return &aCell[ii];
  131281                 : }
  131282                 : 
  131283                 : /*
  131284                 : ** Implementation of the linear variant of the PickSeeds() function from
  131285                 : ** Guttman[84].
  131286                 : */
  131287                 : static void LinearPickSeeds(
  131288                 :   Rtree *pRtree,
  131289                 :   RtreeCell *aCell, 
  131290                 :   int nCell, 
  131291                 :   int *piLeftSeed, 
  131292                 :   int *piRightSeed
  131293                 : ){
  131294                 :   int i;
  131295                 :   int iLeftSeed = 0;
  131296                 :   int iRightSeed = 1;
  131297                 :   float maxNormalInnerWidth = 0.0;
  131298                 : 
  131299                 :   /* Pick two "seed" cells from the array of cells. The algorithm used
  131300                 :   ** here is the LinearPickSeeds algorithm from Gutman[1984]. The 
  131301                 :   ** indices of the two seed cells in the array are stored in local
  131302                 :   ** variables iLeftSeek and iRightSeed.
  131303                 :   */
  131304                 :   for(i=0; i<pRtree->nDim; i++){
  131305                 :     float x1 = DCOORD(aCell[0].aCoord[i*2]);
  131306                 :     float x2 = DCOORD(aCell[0].aCoord[i*2+1]);
  131307                 :     float x3 = x1;
  131308                 :     float x4 = x2;
  131309                 :     int jj;
  131310                 : 
  131311                 :     int iCellLeft = 0;
  131312                 :     int iCellRight = 0;
  131313                 : 
  131314                 :     for(jj=1; jj<nCell; jj++){
  131315                 :       float left = DCOORD(aCell[jj].aCoord[i*2]);
  131316                 :       float right = DCOORD(aCell[jj].aCoord[i*2+1]);
  131317                 : 
  131318                 :       if( left<x1 ) x1 = left;
  131319                 :       if( right>x4 ) x4 = right;
  131320                 :       if( left>x3 ){
  131321                 :         x3 = left;
  131322                 :         iCellRight = jj;
  131323                 :       }
  131324                 :       if( right<x2 ){
  131325                 :         x2 = right;
  131326                 :         iCellLeft = jj;
  131327                 :       }
  131328                 :     }
  131329                 : 
  131330                 :     if( x4!=x1 ){
  131331                 :       float normalwidth = (x3 - x2) / (x4 - x1);
  131332                 :       if( normalwidth>maxNormalInnerWidth ){
  131333                 :         iLeftSeed = iCellLeft;
  131334                 :         iRightSeed = iCellRight;
  131335                 :       }
  131336                 :     }
  131337                 :   }
  131338                 : 
  131339                 :   *piLeftSeed = iLeftSeed;
  131340                 :   *piRightSeed = iRightSeed;
  131341                 : }
  131342                 : #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
  131343                 : 
  131344                 : #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
  131345                 : /*
  131346                 : ** Implementation of the quadratic variant of the PickNext() function from
  131347                 : ** Guttman[84].
  131348                 : */
  131349                 : static RtreeCell *QuadraticPickNext(
  131350                 :   Rtree *pRtree,
  131351                 :   RtreeCell *aCell, 
  131352                 :   int nCell, 
  131353                 :   RtreeCell *pLeftBox, 
  131354                 :   RtreeCell *pRightBox,
  131355                 :   int *aiUsed
  131356                 : ){
  131357                 :   #define FABS(a) ((a)<0.0?-1.0*(a):(a))
  131358                 : 
  131359                 :   int iSelect = -1;
  131360                 :   float fDiff;
  131361                 :   int ii;
  131362                 :   for(ii=0; ii<nCell; ii++){
  131363                 :     if( aiUsed[ii]==0 ){
  131364                 :       float left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
  131365                 :       float right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
  131366                 :       float diff = FABS(right-left);
  131367                 :       if( iSelect<0 || diff>fDiff ){
  131368                 :         fDiff = diff;
  131369                 :         iSelect = ii;
  131370                 :       }
  131371                 :     }
  131372                 :   }
  131373                 :   aiUsed[iSelect] = 1;
  131374                 :   return &aCell[iSelect];
  131375                 : }
  131376                 : 
  131377                 : /*
  131378                 : ** Implementation of the quadratic variant of the PickSeeds() function from
  131379                 : ** Guttman[84].
  131380                 : */
  131381                 : static void QuadraticPickSeeds(
  131382                 :   Rtree *pRtree,
  131383                 :   RtreeCell *aCell, 
  131384                 :   int nCell, 
  131385                 :   int *piLeftSeed, 
  131386                 :   int *piRightSeed
  131387                 : ){
  131388                 :   int ii;
  131389                 :   int jj;
  131390                 : 
  131391                 :   int iLeftSeed = 0;
  131392                 :   int iRightSeed = 1;
  131393                 :   float fWaste = 0.0;
  131394                 : 
  131395                 :   for(ii=0; ii<nCell; ii++){
  131396                 :     for(jj=ii+1; jj<nCell; jj++){
  131397                 :       float right = cellArea(pRtree, &aCell[jj]);
  131398                 :       float growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
  131399                 :       float waste = growth - right;
  131400                 : 
  131401                 :       if( waste>fWaste ){
  131402                 :         iLeftSeed = ii;
  131403                 :         iRightSeed = jj;
  131404                 :         fWaste = waste;
  131405                 :       }
  131406                 :     }
  131407                 :   }
  131408                 : 
  131409                 :   *piLeftSeed = iLeftSeed;
  131410                 :   *piRightSeed = iRightSeed;
  131411                 : }
  131412                 : #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
  131413                 : 
  131414                 : /*
  131415                 : ** Arguments aIdx, aDistance and aSpare all point to arrays of size
  131416                 : ** nIdx. The aIdx array contains the set of integers from 0 to 
  131417                 : ** (nIdx-1) in no particular order. This function sorts the values
  131418                 : ** in aIdx according to the indexed values in aDistance. For
  131419                 : ** example, assuming the inputs:
  131420                 : **
  131421                 : **   aIdx      = { 0,   1,   2,   3 }
  131422                 : **   aDistance = { 5.0, 2.0, 7.0, 6.0 }
  131423                 : **
  131424                 : ** this function sets the aIdx array to contain:
  131425                 : **
  131426                 : **   aIdx      = { 0,   1,   2,   3 }
  131427                 : **
  131428                 : ** The aSpare array is used as temporary working space by the
  131429                 : ** sorting algorithm.
  131430                 : */
  131431                 : static void SortByDistance(
  131432                 :   int *aIdx, 
  131433                 :   int nIdx, 
  131434                 :   float *aDistance, 
  131435                 :   int *aSpare
  131436                 : ){
  131437                 :   if( nIdx>1 ){
  131438                 :     int iLeft = 0;
  131439                 :     int iRight = 0;
  131440                 : 
  131441                 :     int nLeft = nIdx/2;
  131442                 :     int nRight = nIdx-nLeft;
  131443                 :     int *aLeft = aIdx;
  131444                 :     int *aRight = &aIdx[nLeft];
  131445                 : 
  131446                 :     SortByDistance(aLeft, nLeft, aDistance, aSpare);
  131447                 :     SortByDistance(aRight, nRight, aDistance, aSpare);
  131448                 : 
  131449                 :     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
  131450                 :     aLeft = aSpare;
  131451                 : 
  131452                 :     while( iLeft<nLeft || iRight<nRight ){
  131453                 :       if( iLeft==nLeft ){
  131454                 :         aIdx[iLeft+iRight] = aRight[iRight];
  131455                 :         iRight++;
  131456                 :       }else if( iRight==nRight ){
  131457                 :         aIdx[iLeft+iRight] = aLeft[iLeft];
  131458                 :         iLeft++;
  131459                 :       }else{
  131460                 :         float fLeft = aDistance[aLeft[iLeft]];
  131461                 :         float fRight = aDistance[aRight[iRight]];
  131462                 :         if( fLeft<fRight ){
  131463                 :           aIdx[iLeft+iRight] = aLeft[iLeft];
  131464                 :           iLeft++;
  131465                 :         }else{
  131466                 :           aIdx[iLeft+iRight] = aRight[iRight];
  131467                 :           iRight++;
  131468                 :         }
  131469                 :       }
  131470                 :     }
  131471                 : 
  131472                 : #if 0
  131473                 :     /* Check that the sort worked */
  131474                 :     {
  131475                 :       int jj;
  131476                 :       for(jj=1; jj<nIdx; jj++){
  131477                 :         float left = aDistance[aIdx[jj-1]];
  131478                 :         float right = aDistance[aIdx[jj]];
  131479                 :         assert( left<=right );
  131480                 :       }
  131481                 :     }
  131482                 : #endif
  131483                 :   }
  131484                 : }
  131485                 : 
  131486                 : /*
  131487                 : ** Arguments aIdx, aCell and aSpare all point to arrays of size
  131488                 : ** nIdx. The aIdx array contains the set of integers from 0 to 
  131489                 : ** (nIdx-1) in no particular order. This function sorts the values
  131490                 : ** in aIdx according to dimension iDim of the cells in aCell. The
  131491                 : ** minimum value of dimension iDim is considered first, the
  131492                 : ** maximum used to break ties.
  131493                 : **
  131494                 : ** The aSpare array is used as temporary working space by the
  131495                 : ** sorting algorithm.
  131496                 : */
  131497                 : static void SortByDimension(
  131498                 :   Rtree *pRtree,
  131499                 :   int *aIdx, 
  131500                 :   int nIdx, 
  131501                 :   int iDim, 
  131502                 :   RtreeCell *aCell, 
  131503                 :   int *aSpare
  131504                 : ){
  131505                 :   if( nIdx>1 ){
  131506                 : 
  131507                 :     int iLeft = 0;
  131508                 :     int iRight = 0;
  131509                 : 
  131510                 :     int nLeft = nIdx/2;
  131511                 :     int nRight = nIdx-nLeft;
  131512                 :     int *aLeft = aIdx;
  131513                 :     int *aRight = &aIdx[nLeft];
  131514                 : 
  131515                 :     SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
  131516                 :     SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
  131517                 : 
  131518                 :     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
  131519                 :     aLeft = aSpare;
  131520                 :     while( iLeft<nLeft || iRight<nRight ){
  131521                 :       double xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
  131522                 :       double xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
  131523                 :       double xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
  131524                 :       double xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
  131525                 :       if( (iLeft!=nLeft) && ((iRight==nRight)
  131526                 :        || (xleft1<xright1)
  131527                 :        || (xleft1==xright1 && xleft2<xright2)
  131528                 :       )){
  131529                 :         aIdx[iLeft+iRight] = aLeft[iLeft];
  131530                 :         iLeft++;
  131531                 :       }else{
  131532                 :         aIdx[iLeft+iRight] = aRight[iRight];
  131533                 :         iRight++;
  131534                 :       }
  131535                 :     }
  131536                 : 
  131537                 : #if 0
  131538                 :     /* Check that the sort worked */
  131539                 :     {
  131540                 :       int jj;
  131541                 :       for(jj=1; jj<nIdx; jj++){
  131542                 :         float xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
  131543                 :         float xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
  131544                 :         float xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
  131545                 :         float xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
  131546                 :         assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
  131547                 :       }
  131548                 :     }
  131549                 : #endif
  131550                 :   }
  131551                 : }
  131552                 : 
  131553                 : #if VARIANT_RSTARTREE_SPLIT
  131554                 : /*
  131555                 : ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
  131556                 : */
  131557                 : static int splitNodeStartree(
  131558                 :   Rtree *pRtree,
  131559                 :   RtreeCell *aCell,
  131560                 :   int nCell,
  131561                 :   RtreeNode *pLeft,
  131562                 :   RtreeNode *pRight,
  131563                 :   RtreeCell *pBboxLeft,
  131564                 :   RtreeCell *pBboxRight
  131565                 : ){
  131566                 :   int **aaSorted;
  131567                 :   int *aSpare;
  131568                 :   int ii;
  131569                 : 
  131570                 :   int iBestDim = 0;
  131571                 :   int iBestSplit = 0;
  131572                 :   float fBestMargin = 0.0;
  131573                 : 
  131574                 :   int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
  131575                 : 
  131576                 :   aaSorted = (int **)sqlite3_malloc(nByte);
  131577                 :   if( !aaSorted ){
  131578                 :     return SQLITE_NOMEM;
  131579                 :   }
  131580                 : 
  131581                 :   aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
  131582                 :   memset(aaSorted, 0, nByte);
  131583                 :   for(ii=0; ii<pRtree->nDim; ii++){
  131584                 :     int jj;
  131585                 :     aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
  131586                 :     for(jj=0; jj<nCell; jj++){
  131587                 :       aaSorted[ii][jj] = jj;
  131588                 :     }
  131589                 :     SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
  131590                 :   }
  131591                 : 
  131592                 :   for(ii=0; ii<pRtree->nDim; ii++){
  131593                 :     float margin = 0.0;
  131594                 :     float fBestOverlap = 0.0;
  131595                 :     float fBestArea = 0.0;
  131596                 :     int iBestLeft = 0;
  131597                 :     int nLeft;
  131598                 : 
  131599                 :     for(
  131600                 :       nLeft=RTREE_MINCELLS(pRtree); 
  131601                 :       nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 
  131602                 :       nLeft++
  131603                 :     ){
  131604                 :       RtreeCell left;
  131605                 :       RtreeCell right;
  131606                 :       int kk;
  131607                 :       float overlap;
  131608                 :       float area;
  131609                 : 
  131610                 :       memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
  131611                 :       memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
  131612                 :       for(kk=1; kk<(nCell-1); kk++){
  131613                 :         if( kk<nLeft ){
  131614                 :           cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
  131615                 :         }else{
  131616                 :           cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
  131617                 :         }
  131618                 :       }
  131619                 :       margin += cellMargin(pRtree, &left);
  131620                 :       margin += cellMargin(pRtree, &right);
  131621                 :       overlap = cellOverlap(pRtree, &left, &right, 1, -1);
  131622                 :       area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
  131623                 :       if( (nLeft==RTREE_MINCELLS(pRtree))
  131624                 :        || (overlap<fBestOverlap)
  131625                 :        || (overlap==fBestOverlap && area<fBestArea)
  131626                 :       ){
  131627                 :         iBestLeft = nLeft;
  131628                 :         fBestOverlap = overlap;
  131629                 :         fBestArea = area;
  131630                 :       }
  131631                 :     }
  131632                 : 
  131633                 :     if( ii==0 || margin<fBestMargin ){
  131634                 :       iBestDim = ii;
  131635                 :       fBestMargin = margin;
  131636                 :       iBestSplit = iBestLeft;
  131637                 :     }
  131638                 :   }
  131639                 : 
  131640                 :   memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
  131641                 :   memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
  131642                 :   for(ii=0; ii<nCell; ii++){
  131643                 :     RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
  131644                 :     RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
  131645                 :     RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
  131646                 :     nodeInsertCell(pRtree, pTarget, pCell);
  131647                 :     cellUnion(pRtree, pBbox, pCell);
  131648                 :   }
  131649                 : 
  131650                 :   sqlite3_free(aaSorted);
  131651                 :   return SQLITE_OK;
  131652                 : }
  131653                 : #endif
  131654                 : 
  131655                 : #if VARIANT_GUTTMAN_SPLIT
  131656                 : /*
  131657                 : ** Implementation of the regular R-tree SplitNode from Guttman[1984].
  131658                 : */
  131659                 : static int splitNodeGuttman(
  131660                 :   Rtree *pRtree,
  131661                 :   RtreeCell *aCell,
  131662                 :   int nCell,
  131663                 :   RtreeNode *pLeft,
  131664                 :   RtreeNode *pRight,
  131665                 :   RtreeCell *pBboxLeft,
  131666                 :   RtreeCell *pBboxRight
  131667                 : ){
  131668                 :   int iLeftSeed = 0;
  131669                 :   int iRightSeed = 1;
  131670                 :   int *aiUsed;
  131671                 :   int i;
  131672                 : 
  131673                 :   aiUsed = sqlite3_malloc(sizeof(int)*nCell);
  131674                 :   if( !aiUsed ){
  131675                 :     return SQLITE_NOMEM;
  131676                 :   }
  131677                 :   memset(aiUsed, 0, sizeof(int)*nCell);
  131678                 : 
  131679                 :   PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
  131680                 : 
  131681                 :   memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
  131682                 :   memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
  131683                 :   nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
  131684                 :   nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
  131685                 :   aiUsed[iLeftSeed] = 1;
  131686                 :   aiUsed[iRightSeed] = 1;
  131687                 : 
  131688                 :   for(i=nCell-2; i>0; i--){
  131689                 :     RtreeCell *pNext;
  131690                 :     pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
  131691                 :     float diff =  
  131692                 :       cellGrowth(pRtree, pBboxLeft, pNext) - 
  131693                 :       cellGrowth(pRtree, pBboxRight, pNext)
  131694                 :     ;
  131695                 :     if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
  131696                 :      || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
  131697                 :     ){
  131698                 :       nodeInsertCell(pRtree, pRight, pNext);
  131699                 :       cellUnion(pRtree, pBboxRight, pNext);
  131700                 :     }else{
  131701                 :       nodeInsertCell(pRtree, pLeft, pNext);
  131702                 :       cellUnion(pRtree, pBboxLeft, pNext);
  131703                 :     }
  131704                 :   }
  131705                 : 
  131706                 :   sqlite3_free(aiUsed);
  131707                 :   return SQLITE_OK;
  131708                 : }
  131709                 : #endif
  131710                 : 
  131711                 : static int updateMapping(
  131712                 :   Rtree *pRtree, 
  131713                 :   i64 iRowid, 
  131714                 :   RtreeNode *pNode, 
  131715                 :   int iHeight
  131716                 : ){
  131717                 :   int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
  131718                 :   xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
  131719                 :   if( iHeight>0 ){
  131720                 :     RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
  131721                 :     if( pChild ){
  131722                 :       nodeRelease(pRtree, pChild->pParent);
  131723                 :       nodeReference(pNode);
  131724                 :       pChild->pParent = pNode;
  131725                 :     }
  131726                 :   }
  131727                 :   return xSetMapping(pRtree, iRowid, pNode->iNode);
  131728                 : }
  131729                 : 
  131730                 : static int SplitNode(
  131731                 :   Rtree *pRtree,
  131732                 :   RtreeNode *pNode,
  131733                 :   RtreeCell *pCell,
  131734                 :   int iHeight
  131735                 : ){
  131736                 :   int i;
  131737                 :   int newCellIsRight = 0;
  131738                 : 
  131739                 :   int rc = SQLITE_OK;
  131740                 :   int nCell = NCELL(pNode);
  131741                 :   RtreeCell *aCell;
  131742                 :   int *aiUsed;
  131743                 : 
  131744                 :   RtreeNode *pLeft = 0;
  131745                 :   RtreeNode *pRight = 0;
  131746                 : 
  131747                 :   RtreeCell leftbbox;
  131748                 :   RtreeCell rightbbox;
  131749                 : 
  131750                 :   /* Allocate an array and populate it with a copy of pCell and 
  131751                 :   ** all cells from node pLeft. Then zero the original node.
  131752                 :   */
  131753                 :   aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
  131754                 :   if( !aCell ){
  131755                 :     rc = SQLITE_NOMEM;
  131756                 :     goto splitnode_out;
  131757                 :   }
  131758                 :   aiUsed = (int *)&aCell[nCell+1];
  131759                 :   memset(aiUsed, 0, sizeof(int)*(nCell+1));
  131760                 :   for(i=0; i<nCell; i++){
  131761                 :     nodeGetCell(pRtree, pNode, i, &aCell[i]);
  131762                 :   }
  131763                 :   nodeZero(pRtree, pNode);
  131764                 :   memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
  131765                 :   nCell++;
  131766                 : 
  131767                 :   if( pNode->iNode==1 ){
  131768                 :     pRight = nodeNew(pRtree, pNode);
  131769                 :     pLeft = nodeNew(pRtree, pNode);
  131770                 :     pRtree->iDepth++;
  131771                 :     pNode->isDirty = 1;
  131772                 :     writeInt16(pNode->zData, pRtree->iDepth);
  131773                 :   }else{
  131774                 :     pLeft = pNode;
  131775                 :     pRight = nodeNew(pRtree, pLeft->pParent);
  131776                 :     nodeReference(pLeft);
  131777                 :   }
  131778                 : 
  131779                 :   if( !pLeft || !pRight ){
  131780                 :     rc = SQLITE_NOMEM;
  131781                 :     goto splitnode_out;
  131782                 :   }
  131783                 : 
  131784                 :   memset(pLeft->zData, 0, pRtree->iNodeSize);
  131785                 :   memset(pRight->zData, 0, pRtree->iNodeSize);
  131786                 : 
  131787                 :   rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
  131788                 :   if( rc!=SQLITE_OK ){
  131789                 :     goto splitnode_out;
  131790                 :   }
  131791                 : 
  131792                 :   /* Ensure both child nodes have node numbers assigned to them by calling
  131793                 :   ** nodeWrite(). Node pRight always needs a node number, as it was created
  131794                 :   ** by nodeNew() above. But node pLeft sometimes already has a node number.
  131795                 :   ** In this case avoid the all to nodeWrite().
  131796                 :   */
  131797                 :   if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
  131798                 :    || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
  131799                 :   ){
  131800                 :     goto splitnode_out;
  131801                 :   }
  131802                 : 
  131803                 :   rightbbox.iRowid = pRight->iNode;
  131804                 :   leftbbox.iRowid = pLeft->iNode;
  131805                 : 
  131806                 :   if( pNode->iNode==1 ){
  131807                 :     rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
  131808                 :     if( rc!=SQLITE_OK ){
  131809                 :       goto splitnode_out;
  131810                 :     }
  131811                 :   }else{
  131812                 :     RtreeNode *pParent = pLeft->pParent;
  131813                 :     int iCell;
  131814                 :     rc = nodeParentIndex(pRtree, pLeft, &iCell);
  131815                 :     if( rc==SQLITE_OK ){
  131816                 :       nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
  131817                 :       rc = AdjustTree(pRtree, pParent, &leftbbox);
  131818                 :     }
  131819                 :     if( rc!=SQLITE_OK ){
  131820                 :       goto splitnode_out;
  131821                 :     }
  131822                 :   }
  131823                 :   if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
  131824                 :     goto splitnode_out;
  131825                 :   }
  131826                 : 
  131827                 :   for(i=0; i<NCELL(pRight); i++){
  131828                 :     i64 iRowid = nodeGetRowid(pRtree, pRight, i);
  131829                 :     rc = updateMapping(pRtree, iRowid, pRight, iHeight);
  131830                 :     if( iRowid==pCell->iRowid ){
  131831                 :       newCellIsRight = 1;
  131832                 :     }
  131833                 :     if( rc!=SQLITE_OK ){
  131834                 :       goto splitnode_out;
  131835                 :     }
  131836                 :   }
  131837                 :   if( pNode->iNode==1 ){
  131838                 :     for(i=0; i<NCELL(pLeft); i++){
  131839                 :       i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
  131840                 :       rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
  131841                 :       if( rc!=SQLITE_OK ){
  131842                 :         goto splitnode_out;
  131843                 :       }
  131844                 :     }
  131845                 :   }else if( newCellIsRight==0 ){
  131846                 :     rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
  131847                 :   }
  131848                 : 
  131849                 :   if( rc==SQLITE_OK ){
  131850                 :     rc = nodeRelease(pRtree, pRight);
  131851                 :     pRight = 0;
  131852                 :   }
  131853                 :   if( rc==SQLITE_OK ){
  131854                 :     rc = nodeRelease(pRtree, pLeft);
  131855                 :     pLeft = 0;
  131856                 :   }
  131857                 : 
  131858                 : splitnode_out:
  131859                 :   nodeRelease(pRtree, pRight);
  131860                 :   nodeRelease(pRtree, pLeft);
  131861                 :   sqlite3_free(aCell);
  131862                 :   return rc;
  131863                 : }
  131864                 : 
  131865                 : /*
  131866                 : ** If node pLeaf is not the root of the r-tree and its pParent pointer is 
  131867                 : ** still NULL, load all ancestor nodes of pLeaf into memory and populate
  131868                 : ** the pLeaf->pParent chain all the way up to the root node.
  131869                 : **
  131870                 : ** This operation is required when a row is deleted (or updated - an update
  131871                 : ** is implemented as a delete followed by an insert). SQLite provides the
  131872                 : ** rowid of the row to delete, which can be used to find the leaf on which
  131873                 : ** the entry resides (argument pLeaf). Once the leaf is located, this 
  131874                 : ** function is called to determine its ancestry.
  131875                 : */
  131876                 : static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
  131877                 :   int rc = SQLITE_OK;
  131878                 :   RtreeNode *pChild = pLeaf;
  131879                 :   while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
  131880                 :     int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
  131881                 :     sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
  131882                 :     rc = sqlite3_step(pRtree->pReadParent);
  131883                 :     if( rc==SQLITE_ROW ){
  131884                 :       RtreeNode *pTest;           /* Used to test for reference loops */
  131885                 :       i64 iNode;                  /* Node number of parent node */
  131886                 : 
  131887                 :       /* Before setting pChild->pParent, test that we are not creating a
  131888                 :       ** loop of references (as we would if, say, pChild==pParent). We don't
  131889                 :       ** want to do this as it leads to a memory leak when trying to delete
  131890                 :       ** the referenced counted node structures.
  131891                 :       */
  131892                 :       iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
  131893                 :       for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
  131894                 :       if( !pTest ){
  131895                 :         rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
  131896                 :       }
  131897                 :     }
  131898                 :     rc = sqlite3_reset(pRtree->pReadParent);
  131899                 :     if( rc==SQLITE_OK ) rc = rc2;
  131900                 :     if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
  131901                 :     pChild = pChild->pParent;
  131902                 :   }
  131903                 :   return rc;
  131904                 : }
  131905                 : 
  131906                 : static int deleteCell(Rtree *, RtreeNode *, int, int);
  131907                 : 
  131908                 : static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
  131909                 :   int rc;
  131910                 :   int rc2;
  131911                 :   RtreeNode *pParent = 0;
  131912                 :   int iCell;
  131913                 : 
  131914                 :   assert( pNode->nRef==1 );
  131915                 : 
  131916                 :   /* Remove the entry in the parent cell. */
  131917                 :   rc = nodeParentIndex(pRtree, pNode, &iCell);
  131918                 :   if( rc==SQLITE_OK ){
  131919                 :     pParent = pNode->pParent;
  131920                 :     pNode->pParent = 0;
  131921                 :     rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
  131922                 :   }
  131923                 :   rc2 = nodeRelease(pRtree, pParent);
  131924                 :   if( rc==SQLITE_OK ){
  131925                 :     rc = rc2;
  131926                 :   }
  131927                 :   if( rc!=SQLITE_OK ){
  131928                 :     return rc;
  131929                 :   }
  131930                 : 
  131931                 :   /* Remove the xxx_node entry. */
  131932                 :   sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
  131933                 :   sqlite3_step(pRtree->pDeleteNode);
  131934                 :   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
  131935                 :     return rc;
  131936                 :   }
  131937                 : 
  131938                 :   /* Remove the xxx_parent entry. */
  131939                 :   sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
  131940                 :   sqlite3_step(pRtree->pDeleteParent);
  131941                 :   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
  131942                 :     return rc;
  131943                 :   }
  131944                 :   
  131945                 :   /* Remove the node from the in-memory hash table and link it into
  131946                 :   ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
  131947                 :   */
  131948                 :   nodeHashDelete(pRtree, pNode);
  131949                 :   pNode->iNode = iHeight;
  131950                 :   pNode->pNext = pRtree->pDeleted;
  131951                 :   pNode->nRef++;
  131952                 :   pRtree->pDeleted = pNode;
  131953                 : 
  131954                 :   return SQLITE_OK;
  131955                 : }
  131956                 : 
  131957                 : static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
  131958                 :   RtreeNode *pParent = pNode->pParent;
  131959                 :   int rc = SQLITE_OK; 
  131960                 :   if( pParent ){
  131961                 :     int ii; 
  131962                 :     int nCell = NCELL(pNode);
  131963                 :     RtreeCell box;                            /* Bounding box for pNode */
  131964                 :     nodeGetCell(pRtree, pNode, 0, &box);
  131965                 :     for(ii=1; ii<nCell; ii++){
  131966                 :       RtreeCell cell;
  131967                 :       nodeGetCell(pRtree, pNode, ii, &cell);
  131968                 :       cellUnion(pRtree, &box, &cell);
  131969                 :     }
  131970                 :     box.iRowid = pNode->iNode;
  131971                 :     rc = nodeParentIndex(pRtree, pNode, &ii);
  131972                 :     if( rc==SQLITE_OK ){
  131973                 :       nodeOverwriteCell(pRtree, pParent, &box, ii);
  131974                 :       rc = fixBoundingBox(pRtree, pParent);
  131975                 :     }
  131976                 :   }
  131977                 :   return rc;
  131978                 : }
  131979                 : 
  131980                 : /*
  131981                 : ** Delete the cell at index iCell of node pNode. After removing the
  131982                 : ** cell, adjust the r-tree data structure if required.
  131983                 : */
  131984                 : static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
  131985                 :   RtreeNode *pParent;
  131986                 :   int rc;
  131987                 : 
  131988                 :   if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
  131989                 :     return rc;
  131990                 :   }
  131991                 : 
  131992                 :   /* Remove the cell from the node. This call just moves bytes around
  131993                 :   ** the in-memory node image, so it cannot fail.
  131994                 :   */
  131995                 :   nodeDeleteCell(pRtree, pNode, iCell);
  131996                 : 
  131997                 :   /* If the node is not the tree root and now has less than the minimum
  131998                 :   ** number of cells, remove it from the tree. Otherwise, update the
  131999                 :   ** cell in the parent node so that it tightly contains the updated
  132000                 :   ** node.
  132001                 :   */
  132002                 :   pParent = pNode->pParent;
  132003                 :   assert( pParent || pNode->iNode==1 );
  132004                 :   if( pParent ){
  132005                 :     if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
  132006                 :       rc = removeNode(pRtree, pNode, iHeight);
  132007                 :     }else{
  132008                 :       rc = fixBoundingBox(pRtree, pNode);
  132009                 :     }
  132010                 :   }
  132011                 : 
  132012                 :   return rc;
  132013                 : }
  132014                 : 
  132015                 : static int Reinsert(
  132016                 :   Rtree *pRtree, 
  132017                 :   RtreeNode *pNode, 
  132018                 :   RtreeCell *pCell, 
  132019                 :   int iHeight
  132020                 : ){
  132021                 :   int *aOrder;
  132022                 :   int *aSpare;
  132023                 :   RtreeCell *aCell;
  132024                 :   float *aDistance;
  132025                 :   int nCell;
  132026                 :   float aCenterCoord[RTREE_MAX_DIMENSIONS];
  132027                 :   int iDim;
  132028                 :   int ii;
  132029                 :   int rc = SQLITE_OK;
  132030                 : 
  132031                 :   memset(aCenterCoord, 0, sizeof(float)*RTREE_MAX_DIMENSIONS);
  132032                 : 
  132033                 :   nCell = NCELL(pNode)+1;
  132034                 : 
  132035                 :   /* Allocate the buffers used by this operation. The allocation is
  132036                 :   ** relinquished before this function returns.
  132037                 :   */
  132038                 :   aCell = (RtreeCell *)sqlite3_malloc(nCell * (
  132039                 :     sizeof(RtreeCell) +         /* aCell array */
  132040                 :     sizeof(int)       +         /* aOrder array */
  132041                 :     sizeof(int)       +         /* aSpare array */
  132042                 :     sizeof(float)               /* aDistance array */
  132043                 :   ));
  132044                 :   if( !aCell ){
  132045                 :     return SQLITE_NOMEM;
  132046                 :   }
  132047                 :   aOrder    = (int *)&aCell[nCell];
  132048                 :   aSpare    = (int *)&aOrder[nCell];
  132049                 :   aDistance = (float *)&aSpare[nCell];
  132050                 : 
  132051                 :   for(ii=0; ii<nCell; ii++){
  132052                 :     if( ii==(nCell-1) ){
  132053                 :       memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
  132054                 :     }else{
  132055                 :       nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
  132056                 :     }
  132057                 :     aOrder[ii] = ii;
  132058                 :     for(iDim=0; iDim<pRtree->nDim; iDim++){
  132059                 :       aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2]);
  132060                 :       aCenterCoord[iDim] += (float)DCOORD(aCell[ii].aCoord[iDim*2+1]);
  132061                 :     }
  132062                 :   }
  132063                 :   for(iDim=0; iDim<pRtree->nDim; iDim++){
  132064                 :     aCenterCoord[iDim] = (float)(aCenterCoord[iDim]/((float)nCell*2.0));
  132065                 :   }
  132066                 : 
  132067                 :   for(ii=0; ii<nCell; ii++){
  132068                 :     aDistance[ii] = 0.0;
  132069                 :     for(iDim=0; iDim<pRtree->nDim; iDim++){
  132070                 :       float coord = (float)(DCOORD(aCell[ii].aCoord[iDim*2+1]) - 
  132071                 :           DCOORD(aCell[ii].aCoord[iDim*2]));
  132072                 :       aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
  132073                 :     }
  132074                 :   }
  132075                 : 
  132076                 :   SortByDistance(aOrder, nCell, aDistance, aSpare);
  132077                 :   nodeZero(pRtree, pNode);
  132078                 : 
  132079                 :   for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
  132080                 :     RtreeCell *p = &aCell[aOrder[ii]];
  132081                 :     nodeInsertCell(pRtree, pNode, p);
  132082                 :     if( p->iRowid==pCell->iRowid ){
  132083                 :       if( iHeight==0 ){
  132084                 :         rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
  132085                 :       }else{
  132086                 :         rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
  132087                 :       }
  132088                 :     }
  132089                 :   }
  132090                 :   if( rc==SQLITE_OK ){
  132091                 :     rc = fixBoundingBox(pRtree, pNode);
  132092                 :   }
  132093                 :   for(; rc==SQLITE_OK && ii<nCell; ii++){
  132094                 :     /* Find a node to store this cell in. pNode->iNode currently contains
  132095                 :     ** the height of the sub-tree headed by the cell.
  132096                 :     */
  132097                 :     RtreeNode *pInsert;
  132098                 :     RtreeCell *p = &aCell[aOrder[ii]];
  132099                 :     rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
  132100                 :     if( rc==SQLITE_OK ){
  132101                 :       int rc2;
  132102                 :       rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
  132103                 :       rc2 = nodeRelease(pRtree, pInsert);
  132104                 :       if( rc==SQLITE_OK ){
  132105                 :         rc = rc2;
  132106                 :       }
  132107                 :     }
  132108                 :   }
  132109                 : 
  132110                 :   sqlite3_free(aCell);
  132111                 :   return rc;
  132112                 : }
  132113                 : 
  132114                 : /*
  132115                 : ** Insert cell pCell into node pNode. Node pNode is the head of a 
  132116                 : ** subtree iHeight high (leaf nodes have iHeight==0).
  132117                 : */
  132118                 : static int rtreeInsertCell(
  132119                 :   Rtree *pRtree,
  132120                 :   RtreeNode *pNode,
  132121                 :   RtreeCell *pCell,
  132122                 :   int iHeight
  132123                 : ){
  132124                 :   int rc = SQLITE_OK;
  132125                 :   if( iHeight>0 ){
  132126                 :     RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
  132127                 :     if( pChild ){
  132128                 :       nodeRelease(pRtree, pChild->pParent);
  132129                 :       nodeReference(pNode);
  132130                 :       pChild->pParent = pNode;
  132131                 :     }
  132132                 :   }
  132133                 :   if( nodeInsertCell(pRtree, pNode, pCell) ){
  132134                 : #if VARIANT_RSTARTREE_REINSERT
  132135                 :     if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
  132136                 :       rc = SplitNode(pRtree, pNode, pCell, iHeight);
  132137                 :     }else{
  132138                 :       pRtree->iReinsertHeight = iHeight;
  132139                 :       rc = Reinsert(pRtree, pNode, pCell, iHeight);
  132140                 :     }
  132141                 : #else
  132142                 :     rc = SplitNode(pRtree, pNode, pCell, iHeight);
  132143                 : #endif
  132144                 :   }else{
  132145                 :     rc = AdjustTree(pRtree, pNode, pCell);
  132146                 :     if( rc==SQLITE_OK ){
  132147                 :       if( iHeight==0 ){
  132148                 :         rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
  132149                 :       }else{
  132150                 :         rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
  132151                 :       }
  132152                 :     }
  132153                 :   }
  132154                 :   return rc;
  132155                 : }
  132156                 : 
  132157                 : static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
  132158                 :   int ii;
  132159                 :   int rc = SQLITE_OK;
  132160                 :   int nCell = NCELL(pNode);
  132161                 : 
  132162                 :   for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
  132163                 :     RtreeNode *pInsert;
  132164                 :     RtreeCell cell;
  132165                 :     nodeGetCell(pRtree, pNode, ii, &cell);
  132166                 : 
  132167                 :     /* Find a node to store this cell in. pNode->iNode currently contains
  132168                 :     ** the height of the sub-tree headed by the cell.
  132169                 :     */
  132170                 :     rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
  132171                 :     if( rc==SQLITE_OK ){
  132172                 :       int rc2;
  132173                 :       rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
  132174                 :       rc2 = nodeRelease(pRtree, pInsert);
  132175                 :       if( rc==SQLITE_OK ){
  132176                 :         rc = rc2;
  132177                 :       }
  132178                 :     }
  132179                 :   }
  132180                 :   return rc;
  132181                 : }
  132182                 : 
  132183                 : /*
  132184                 : ** Select a currently unused rowid for a new r-tree record.
  132185                 : */
  132186                 : static int newRowid(Rtree *pRtree, i64 *piRowid){
  132187                 :   int rc;
  132188                 :   sqlite3_bind_null(pRtree->pWriteRowid, 1);
  132189                 :   sqlite3_bind_null(pRtree->pWriteRowid, 2);
  132190                 :   sqlite3_step(pRtree->pWriteRowid);
  132191                 :   rc = sqlite3_reset(pRtree->pWriteRowid);
  132192                 :   *piRowid = sqlite3_last_insert_rowid(pRtree->db);
  132193                 :   return rc;
  132194                 : }
  132195                 : 
  132196                 : /*
  132197                 : ** Remove the entry with rowid=iDelete from the r-tree structure.
  132198                 : */
  132199                 : static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
  132200                 :   int rc;                         /* Return code */
  132201                 :   RtreeNode *pLeaf;               /* Leaf node containing record iDelete */
  132202                 :   int iCell;                      /* Index of iDelete cell in pLeaf */
  132203                 :   RtreeNode *pRoot;               /* Root node of rtree structure */
  132204                 : 
  132205                 : 
  132206                 :   /* Obtain a reference to the root node to initialise Rtree.iDepth */
  132207                 :   rc = nodeAcquire(pRtree, 1, 0, &pRoot);
  132208                 : 
  132209                 :   /* Obtain a reference to the leaf node that contains the entry 
  132210                 :   ** about to be deleted. 
  132211                 :   */
  132212                 :   if( rc==SQLITE_OK ){
  132213                 :     rc = findLeafNode(pRtree, iDelete, &pLeaf);
  132214                 :   }
  132215                 : 
  132216                 :   /* Delete the cell in question from the leaf node. */
  132217                 :   if( rc==SQLITE_OK ){
  132218                 :     int rc2;
  132219                 :     rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
  132220                 :     if( rc==SQLITE_OK ){
  132221                 :       rc = deleteCell(pRtree, pLeaf, iCell, 0);
  132222                 :     }
  132223                 :     rc2 = nodeRelease(pRtree, pLeaf);
  132224                 :     if( rc==SQLITE_OK ){
  132225                 :       rc = rc2;
  132226                 :     }
  132227                 :   }
  132228                 : 
  132229                 :   /* Delete the corresponding entry in the <rtree>_rowid table. */
  132230                 :   if( rc==SQLITE_OK ){
  132231                 :     sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
  132232                 :     sqlite3_step(pRtree->pDeleteRowid);
  132233                 :     rc = sqlite3_reset(pRtree->pDeleteRowid);
  132234                 :   }
  132235                 : 
  132236                 :   /* Check if the root node now has exactly one child. If so, remove
  132237                 :   ** it, schedule the contents of the child for reinsertion and 
  132238                 :   ** reduce the tree height by one.
  132239                 :   **
  132240                 :   ** This is equivalent to copying the contents of the child into
  132241                 :   ** the root node (the operation that Gutman's paper says to perform 
  132242                 :   ** in this scenario).
  132243                 :   */
  132244                 :   if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
  132245                 :     int rc2;
  132246                 :     RtreeNode *pChild;
  132247                 :     i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
  132248                 :     rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
  132249                 :     if( rc==SQLITE_OK ){
  132250                 :       rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
  132251                 :     }
  132252                 :     rc2 = nodeRelease(pRtree, pChild);
  132253                 :     if( rc==SQLITE_OK ) rc = rc2;
  132254                 :     if( rc==SQLITE_OK ){
  132255                 :       pRtree->iDepth--;
  132256                 :       writeInt16(pRoot->zData, pRtree->iDepth);
  132257                 :       pRoot->isDirty = 1;
  132258                 :     }
  132259                 :   }
  132260                 : 
  132261                 :   /* Re-insert the contents of any underfull nodes removed from the tree. */
  132262                 :   for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
  132263                 :     if( rc==SQLITE_OK ){
  132264                 :       rc = reinsertNodeContent(pRtree, pLeaf);
  132265                 :     }
  132266                 :     pRtree->pDeleted = pLeaf->pNext;
  132267                 :     sqlite3_free(pLeaf);
  132268                 :   }
  132269                 : 
  132270                 :   /* Release the reference to the root node. */
  132271                 :   if( rc==SQLITE_OK ){
  132272                 :     rc = nodeRelease(pRtree, pRoot);
  132273                 :   }else{
  132274                 :     nodeRelease(pRtree, pRoot);
  132275                 :   }
  132276                 : 
  132277                 :   return rc;
  132278                 : }
  132279                 : 
  132280                 : /*
  132281                 : ** The xUpdate method for rtree module virtual tables.
  132282                 : */
  132283                 : static int rtreeUpdate(
  132284                 :   sqlite3_vtab *pVtab, 
  132285                 :   int nData, 
  132286                 :   sqlite3_value **azData, 
  132287                 :   sqlite_int64 *pRowid
  132288                 : ){
  132289                 :   Rtree *pRtree = (Rtree *)pVtab;
  132290                 :   int rc = SQLITE_OK;
  132291                 :   RtreeCell cell;                 /* New cell to insert if nData>1 */
  132292                 :   int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
  132293                 : 
  132294                 :   rtreeReference(pRtree);
  132295                 :   assert(nData>=1);
  132296                 : 
  132297                 :   /* Constraint handling. A write operation on an r-tree table may return
  132298                 :   ** SQLITE_CONSTRAINT for two reasons:
  132299                 :   **
  132300                 :   **   1. A duplicate rowid value, or
  132301                 :   **   2. The supplied data violates the "x2>=x1" constraint.
  132302                 :   **
  132303                 :   ** In the first case, if the conflict-handling mode is REPLACE, then
  132304                 :   ** the conflicting row can be removed before proceeding. In the second
  132305                 :   ** case, SQLITE_CONSTRAINT must be returned regardless of the
  132306                 :   ** conflict-handling mode specified by the user.
  132307                 :   */
  132308                 :   if( nData>1 ){
  132309                 :     int ii;
  132310                 : 
  132311                 :     /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
  132312                 :     assert( nData==(pRtree->nDim*2 + 3) );
  132313                 :     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
  132314                 :       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  132315                 :         cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]);
  132316                 :         cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]);
  132317                 :         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
  132318                 :           rc = SQLITE_CONSTRAINT;
  132319                 :           goto constraint;
  132320                 :         }
  132321                 :       }
  132322                 :     }else{
  132323                 :       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
  132324                 :         cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
  132325                 :         cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
  132326                 :         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
  132327                 :           rc = SQLITE_CONSTRAINT;
  132328                 :           goto constraint;
  132329                 :         }
  132330                 :       }
  132331                 :     }
  132332                 : 
  132333                 :     /* If a rowid value was supplied, check if it is already present in 
  132334                 :     ** the table. If so, the constraint has failed. */
  132335                 :     if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
  132336                 :       cell.iRowid = sqlite3_value_int64(azData[2]);
  132337                 :       if( sqlite3_value_type(azData[0])==SQLITE_NULL
  132338                 :        || sqlite3_value_int64(azData[0])!=cell.iRowid
  132339                 :       ){
  132340                 :         int steprc;
  132341                 :         sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
  132342                 :         steprc = sqlite3_step(pRtree->pReadRowid);
  132343                 :         rc = sqlite3_reset(pRtree->pReadRowid);
  132344                 :         if( SQLITE_ROW==steprc ){
  132345                 :           if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
  132346                 :             rc = rtreeDeleteRowid(pRtree, cell.iRowid);
  132347                 :           }else{
  132348                 :             rc = SQLITE_CONSTRAINT;
  132349                 :             goto constraint;
  132350                 :           }
  132351                 :         }
  132352                 :       }
  132353                 :       bHaveRowid = 1;
  132354                 :     }
  132355                 :   }
  132356                 : 
  132357                 :   /* If azData[0] is not an SQL NULL value, it is the rowid of a
  132358                 :   ** record to delete from the r-tree table. The following block does
  132359                 :   ** just that.
  132360                 :   */
  132361                 :   if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
  132362                 :     rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
  132363                 :   }
  132364                 : 
  132365                 :   /* If the azData[] array contains more than one element, elements
  132366                 :   ** (azData[2]..azData[argc-1]) contain a new record to insert into
  132367                 :   ** the r-tree structure.
  132368                 :   */
  132369                 :   if( rc==SQLITE_OK && nData>1 ){
  132370                 :     /* Insert the new record into the r-tree */
  132371                 :     RtreeNode *pLeaf;
  132372                 : 
  132373                 :     /* Figure out the rowid of the new row. */
  132374                 :     if( bHaveRowid==0 ){
  132375                 :       rc = newRowid(pRtree, &cell.iRowid);
  132376                 :     }
  132377                 :     *pRowid = cell.iRowid;
  132378                 : 
  132379                 :     if( rc==SQLITE_OK ){
  132380                 :       rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
  132381                 :     }
  132382                 :     if( rc==SQLITE_OK ){
  132383                 :       int rc2;
  132384                 :       pRtree->iReinsertHeight = -1;
  132385                 :       rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
  132386                 :       rc2 = nodeRelease(pRtree, pLeaf);
  132387                 :       if( rc==SQLITE_OK ){
  132388                 :         rc = rc2;
  132389                 :       }
  132390                 :     }
  132391                 :   }
  132392                 : 
  132393                 : constraint:
  132394                 :   rtreeRelease(pRtree);
  132395                 :   return rc;
  132396                 : }
  132397                 : 
  132398                 : /*
  132399                 : ** The xRename method for rtree module virtual tables.
  132400                 : */
  132401                 : static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
  132402                 :   Rtree *pRtree = (Rtree *)pVtab;
  132403                 :   int rc = SQLITE_NOMEM;
  132404                 :   char *zSql = sqlite3_mprintf(
  132405                 :     "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
  132406                 :     "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
  132407                 :     "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
  132408                 :     , pRtree->zDb, pRtree->zName, zNewName 
  132409                 :     , pRtree->zDb, pRtree->zName, zNewName 
  132410                 :     , pRtree->zDb, pRtree->zName, zNewName
  132411                 :   );
  132412                 :   if( zSql ){
  132413                 :     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
  132414                 :     sqlite3_free(zSql);
  132415                 :   }
  132416                 :   return rc;
  132417                 : }
  132418                 : 
  132419                 : static sqlite3_module rtreeModule = {
  132420                 :   0,                          /* iVersion */
  132421                 :   rtreeCreate,                /* xCreate - create a table */
  132422                 :   rtreeConnect,               /* xConnect - connect to an existing table */
  132423                 :   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
  132424                 :   rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
  132425                 :   rtreeDestroy,               /* xDestroy - Drop a table */
  132426                 :   rtreeOpen,                  /* xOpen - open a cursor */
  132427                 :   rtreeClose,                 /* xClose - close a cursor */
  132428                 :   rtreeFilter,                /* xFilter - configure scan constraints */
  132429                 :   rtreeNext,                  /* xNext - advance a cursor */
  132430                 :   rtreeEof,                   /* xEof */
  132431                 :   rtreeColumn,                /* xColumn - read data */
  132432                 :   rtreeRowid,                 /* xRowid - read data */
  132433                 :   rtreeUpdate,                /* xUpdate - write data */
  132434                 :   0,                          /* xBegin - begin transaction */
  132435                 :   0,                          /* xSync - sync transaction */
  132436                 :   0,                          /* xCommit - commit transaction */
  132437                 :   0,                          /* xRollback - rollback transaction */
  132438                 :   0,                          /* xFindFunction - function overloading */
  132439                 :   rtreeRename,                /* xRename - rename the table */
  132440                 :   0,                          /* xSavepoint */
  132441                 :   0,                          /* xRelease */
  132442                 :   0                           /* xRollbackTo */
  132443                 : };
  132444                 : 
  132445                 : static int rtreeSqlInit(
  132446                 :   Rtree *pRtree, 
  132447                 :   sqlite3 *db, 
  132448                 :   const char *zDb, 
  132449                 :   const char *zPrefix, 
  132450                 :   int isCreate
  132451                 : ){
  132452                 :   int rc = SQLITE_OK;
  132453                 : 
  132454                 :   #define N_STATEMENT 9
  132455                 :   static const char *azSql[N_STATEMENT] = {
  132456                 :     /* Read and write the xxx_node table */
  132457                 :     "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
  132458                 :     "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
  132459                 :     "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
  132460                 : 
  132461                 :     /* Read and write the xxx_rowid table */
  132462                 :     "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
  132463                 :     "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
  132464                 :     "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
  132465                 : 
  132466                 :     /* Read and write the xxx_parent table */
  132467                 :     "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
  132468                 :     "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
  132469                 :     "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
  132470                 :   };
  132471                 :   sqlite3_stmt **appStmt[N_STATEMENT];
  132472                 :   int i;
  132473                 : 
  132474                 :   pRtree->db = db;
  132475                 : 
  132476                 :   if( isCreate ){
  132477                 :     char *zCreate = sqlite3_mprintf(
  132478                 : "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
  132479                 : "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
  132480                 : "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
  132481                 : "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
  132482                 :       zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
  132483                 :     );
  132484                 :     if( !zCreate ){
  132485                 :       return SQLITE_NOMEM;
  132486                 :     }
  132487                 :     rc = sqlite3_exec(db, zCreate, 0, 0, 0);
  132488                 :     sqlite3_free(zCreate);
  132489                 :     if( rc!=SQLITE_OK ){
  132490                 :       return rc;
  132491                 :     }
  132492                 :   }
  132493                 : 
  132494                 :   appStmt[0] = &pRtree->pReadNode;
  132495                 :   appStmt[1] = &pRtree->pWriteNode;
  132496                 :   appStmt[2] = &pRtree->pDeleteNode;
  132497                 :   appStmt[3] = &pRtree->pReadRowid;
  132498                 :   appStmt[4] = &pRtree->pWriteRowid;
  132499                 :   appStmt[5] = &pRtree->pDeleteRowid;
  132500                 :   appStmt[6] = &pRtree->pReadParent;
  132501                 :   appStmt[7] = &pRtree->pWriteParent;
  132502                 :   appStmt[8] = &pRtree->pDeleteParent;
  132503                 : 
  132504                 :   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
  132505                 :     char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
  132506                 :     if( zSql ){
  132507                 :       rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0); 
  132508                 :     }else{
  132509                 :       rc = SQLITE_NOMEM;
  132510                 :     }
  132511                 :     sqlite3_free(zSql);
  132512                 :   }
  132513                 : 
  132514                 :   return rc;
  132515                 : }
  132516                 : 
  132517                 : /*
  132518                 : ** The second argument to this function contains the text of an SQL statement
  132519                 : ** that returns a single integer value. The statement is compiled and executed
  132520                 : ** using database connection db. If successful, the integer value returned
  132521                 : ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
  132522                 : ** code is returned and the value of *piVal after returning is not defined.
  132523                 : */
  132524                 : static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
  132525                 :   int rc = SQLITE_NOMEM;
  132526                 :   if( zSql ){
  132527                 :     sqlite3_stmt *pStmt = 0;
  132528                 :     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
  132529                 :     if( rc==SQLITE_OK ){
  132530                 :       if( SQLITE_ROW==sqlite3_step(pStmt) ){
  132531                 :         *piVal = sqlite3_column_int(pStmt, 0);
  132532                 :       }
  132533                 :       rc = sqlite3_finalize(pStmt);
  132534                 :     }
  132535                 :   }
  132536                 :   return rc;
  132537                 : }
  132538                 : 
  132539                 : /*
  132540                 : ** This function is called from within the xConnect() or xCreate() method to
  132541                 : ** determine the node-size used by the rtree table being created or connected
  132542                 : ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
  132543                 : ** Otherwise, an SQLite error code is returned.
  132544                 : **
  132545                 : ** If this function is being called as part of an xConnect(), then the rtree
  132546                 : ** table already exists. In this case the node-size is determined by inspecting
  132547                 : ** the root node of the tree.
  132548                 : **
  132549                 : ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. 
  132550                 : ** This ensures that each node is stored on a single database page. If the 
  132551                 : ** database page-size is so large that more than RTREE_MAXCELLS entries 
  132552                 : ** would fit in a single node, use a smaller node-size.
  132553                 : */
  132554                 : static int getNodeSize(
  132555                 :   sqlite3 *db,                    /* Database handle */
  132556                 :   Rtree *pRtree,                  /* Rtree handle */
  132557                 :   int isCreate                    /* True for xCreate, false for xConnect */
  132558                 : ){
  132559                 :   int rc;
  132560                 :   char *zSql;
  132561                 :   if( isCreate ){
  132562                 :     int iPageSize = 0;
  132563                 :     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
  132564                 :     rc = getIntFromStmt(db, zSql, &iPageSize);
  132565                 :     if( rc==SQLITE_OK ){
  132566                 :       pRtree->iNodeSize = iPageSize-64;
  132567                 :       if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
  132568                 :         pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
  132569                 :       }
  132570                 :     }
  132571                 :   }else{
  132572                 :     zSql = sqlite3_mprintf(
  132573                 :         "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
  132574                 :         pRtree->zDb, pRtree->zName
  132575                 :     );
  132576                 :     rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
  132577                 :   }
  132578                 : 
  132579                 :   sqlite3_free(zSql);
  132580                 :   return rc;
  132581                 : }
  132582                 : 
  132583                 : /* 
  132584                 : ** This function is the implementation of both the xConnect and xCreate
  132585                 : ** methods of the r-tree virtual table.
  132586                 : **
  132587                 : **   argv[0]   -> module name
  132588                 : **   argv[1]   -> database name
  132589                 : **   argv[2]   -> table name
  132590                 : **   argv[...] -> column names...
  132591                 : */
  132592                 : static int rtreeInit(
  132593                 :   sqlite3 *db,                        /* Database connection */
  132594                 :   void *pAux,                         /* One of the RTREE_COORD_* constants */
  132595                 :   int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
  132596                 :   sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
  132597                 :   char **pzErr,                       /* OUT: Error message, if any */
  132598                 :   int isCreate                        /* True for xCreate, false for xConnect */
  132599                 : ){
  132600                 :   int rc = SQLITE_OK;
  132601                 :   Rtree *pRtree;
  132602                 :   int nDb;              /* Length of string argv[1] */
  132603                 :   int nName;            /* Length of string argv[2] */
  132604                 :   int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
  132605                 : 
  132606                 :   const char *aErrMsg[] = {
  132607                 :     0,                                                    /* 0 */
  132608                 :     "Wrong number of columns for an rtree table",         /* 1 */
  132609                 :     "Too few columns for an rtree table",                 /* 2 */
  132610                 :     "Too many columns for an rtree table"                 /* 3 */
  132611                 :   };
  132612                 : 
  132613                 :   int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
  132614                 :   if( aErrMsg[iErr] ){
  132615                 :     *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
  132616                 :     return SQLITE_ERROR;
  132617                 :   }
  132618                 : 
  132619                 :   sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
  132620                 : 
  132621                 :   /* Allocate the sqlite3_vtab structure */
  132622                 :   nDb = strlen(argv[1]);
  132623                 :   nName = strlen(argv[2]);
  132624                 :   pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
  132625                 :   if( !pRtree ){
  132626                 :     return SQLITE_NOMEM;
  132627                 :   }
  132628                 :   memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
  132629                 :   pRtree->nBusy = 1;
  132630                 :   pRtree->base.pModule = &rtreeModule;
  132631                 :   pRtree->zDb = (char *)&pRtree[1];
  132632                 :   pRtree->zName = &pRtree->zDb[nDb+1];
  132633                 :   pRtree->nDim = (argc-4)/2;
  132634                 :   pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
  132635                 :   pRtree->eCoordType = eCoordType;
  132636                 :   memcpy(pRtree->zDb, argv[1], nDb);
  132637                 :   memcpy(pRtree->zName, argv[2], nName);
  132638                 : 
  132639                 :   /* Figure out the node size to use. */
  132640                 :   rc = getNodeSize(db, pRtree, isCreate);
  132641                 : 
  132642                 :   /* Create/Connect to the underlying relational database schema. If
  132643                 :   ** that is successful, call sqlite3_declare_vtab() to configure
  132644                 :   ** the r-tree table schema.
  132645                 :   */
  132646                 :   if( rc==SQLITE_OK ){
  132647                 :     if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
  132648                 :       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
  132649                 :     }else{
  132650                 :       char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
  132651                 :       char *zTmp;
  132652                 :       int ii;
  132653                 :       for(ii=4; zSql && ii<argc; ii++){
  132654                 :         zTmp = zSql;
  132655                 :         zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
  132656                 :         sqlite3_free(zTmp);
  132657                 :       }
  132658                 :       if( zSql ){
  132659                 :         zTmp = zSql;
  132660                 :         zSql = sqlite3_mprintf("%s);", zTmp);
  132661                 :         sqlite3_free(zTmp);
  132662                 :       }
  132663                 :       if( !zSql ){
  132664                 :         rc = SQLITE_NOMEM;
  132665                 :       }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
  132666                 :         *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
  132667                 :       }
  132668                 :       sqlite3_free(zSql);
  132669                 :     }
  132670                 :   }
  132671                 : 
  132672                 :   if( rc==SQLITE_OK ){
  132673                 :     *ppVtab = (sqlite3_vtab *)pRtree;
  132674                 :   }else{
  132675                 :     rtreeRelease(pRtree);
  132676                 :   }
  132677                 :   return rc;
  132678                 : }
  132679                 : 
  132680                 : 
  132681                 : /*
  132682                 : ** Implementation of a scalar function that decodes r-tree nodes to
  132683                 : ** human readable strings. This can be used for debugging and analysis.
  132684                 : **
  132685                 : ** The scalar function takes two arguments, a blob of data containing
  132686                 : ** an r-tree node, and the number of dimensions the r-tree indexes.
  132687                 : ** For a two-dimensional r-tree structure called "rt", to deserialize
  132688                 : ** all nodes, a statement like:
  132689                 : **
  132690                 : **   SELECT rtreenode(2, data) FROM rt_node;
  132691                 : **
  132692                 : ** The human readable string takes the form of a Tcl list with one
  132693                 : ** entry for each cell in the r-tree node. Each entry is itself a
  132694                 : ** list, containing the 8-byte rowid/pageno followed by the 
  132695                 : ** <num-dimension>*2 coordinates.
  132696                 : */
  132697                 : static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
  132698                 :   char *zText = 0;
  132699                 :   RtreeNode node;
  132700                 :   Rtree tree;
  132701                 :   int ii;
  132702                 : 
  132703                 :   UNUSED_PARAMETER(nArg);
  132704                 :   memset(&node, 0, sizeof(RtreeNode));
  132705                 :   memset(&tree, 0, sizeof(Rtree));
  132706                 :   tree.nDim = sqlite3_value_int(apArg[0]);
  132707                 :   tree.nBytesPerCell = 8 + 8 * tree.nDim;
  132708                 :   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
  132709                 : 
  132710                 :   for(ii=0; ii<NCELL(&node); ii++){
  132711                 :     char zCell[512];
  132712                 :     int nCell = 0;
  132713                 :     RtreeCell cell;
  132714                 :     int jj;
  132715                 : 
  132716                 :     nodeGetCell(&tree, &node, ii, &cell);
  132717                 :     sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
  132718                 :     nCell = strlen(zCell);
  132719                 :     for(jj=0; jj<tree.nDim*2; jj++){
  132720                 :       sqlite3_snprintf(512-nCell,&zCell[nCell]," %f",(double)cell.aCoord[jj].f);
  132721                 :       nCell = strlen(zCell);
  132722                 :     }
  132723                 : 
  132724                 :     if( zText ){
  132725                 :       char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
  132726                 :       sqlite3_free(zText);
  132727                 :       zText = zTextNew;
  132728                 :     }else{
  132729                 :       zText = sqlite3_mprintf("{%s}", zCell);
  132730                 :     }
  132731                 :   }
  132732                 :   
  132733                 :   sqlite3_result_text(ctx, zText, -1, sqlite3_free);
  132734                 : }
  132735                 : 
  132736                 : static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
  132737                 :   UNUSED_PARAMETER(nArg);
  132738                 :   if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB 
  132739                 :    || sqlite3_value_bytes(apArg[0])<2
  132740                 :   ){
  132741                 :     sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 
  132742                 :   }else{
  132743                 :     u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
  132744                 :     sqlite3_result_int(ctx, readInt16(zBlob));
  132745                 :   }
  132746                 : }
  132747                 : 
  132748                 : /*
  132749                 : ** Register the r-tree module with database handle db. This creates the
  132750                 : ** virtual table module "rtree" and the debugging/analysis scalar 
  132751                 : ** function "rtreenode".
  132752                 : */
  132753                 : SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
  132754                 :   const int utf8 = SQLITE_UTF8;
  132755                 :   int rc;
  132756                 : 
  132757                 :   rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
  132758                 :   if( rc==SQLITE_OK ){
  132759                 :     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
  132760                 :   }
  132761                 :   if( rc==SQLITE_OK ){
  132762                 :     void *c = (void *)RTREE_COORD_REAL32;
  132763                 :     rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
  132764                 :   }
  132765                 :   if( rc==SQLITE_OK ){
  132766                 :     void *c = (void *)RTREE_COORD_INT32;
  132767                 :     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
  132768                 :   }
  132769                 : 
  132770                 :   return rc;
  132771                 : }
  132772                 : 
  132773                 : /*
  132774                 : ** A version of sqlite3_free() that can be used as a callback. This is used
  132775                 : ** in two places - as the destructor for the blob value returned by the
  132776                 : ** invocation of a geometry function, and as the destructor for the geometry
  132777                 : ** functions themselves.
  132778                 : */
  132779                 : static void doSqlite3Free(void *p){
  132780                 :   sqlite3_free(p);
  132781                 : }
  132782                 : 
  132783                 : /*
  132784                 : ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
  132785                 : ** scalar user function. This C function is the callback used for all such
  132786                 : ** registered SQL functions.
  132787                 : **
  132788                 : ** The scalar user functions return a blob that is interpreted by r-tree
  132789                 : ** table MATCH operators.
  132790                 : */
  132791                 : static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
  132792                 :   RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
  132793                 :   RtreeMatchArg *pBlob;
  132794                 :   int nBlob;
  132795                 : 
  132796                 :   nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(double);
  132797                 :   pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
  132798                 :   if( !pBlob ){
  132799                 :     sqlite3_result_error_nomem(ctx);
  132800                 :   }else{
  132801                 :     int i;
  132802                 :     pBlob->magic = RTREE_GEOMETRY_MAGIC;
  132803                 :     pBlob->xGeom = pGeomCtx->xGeom;
  132804                 :     pBlob->pContext = pGeomCtx->pContext;
  132805                 :     pBlob->nParam = nArg;
  132806                 :     for(i=0; i<nArg; i++){
  132807                 :       pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
  132808                 :     }
  132809                 :     sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
  132810                 :   }
  132811                 : }
  132812                 : 
  132813                 : /*
  132814                 : ** Register a new geometry function for use with the r-tree MATCH operator.
  132815                 : */
  132816                 : SQLITE_API int sqlite3_rtree_geometry_callback(
  132817                 :   sqlite3 *db,
  132818                 :   const char *zGeom,
  132819                 :   int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *),
  132820                 :   void *pContext
  132821                 : ){
  132822                 :   RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
  132823                 : 
  132824                 :   /* Allocate and populate the context object. */
  132825                 :   pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
  132826                 :   if( !pGeomCtx ) return SQLITE_NOMEM;
  132827                 :   pGeomCtx->xGeom = xGeom;
  132828                 :   pGeomCtx->pContext = pContext;
  132829                 : 
  132830                 :   /* Create the new user-function. Register a destructor function to delete
  132831                 :   ** the context object when it is no longer required.  */
  132832                 :   return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, 
  132833                 :       (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
  132834                 :   );
  132835                 : }
  132836                 : 
  132837                 : #if !SQLITE_CORE
  132838                 : SQLITE_API int sqlite3_extension_init(
  132839                 :   sqlite3 *db,
  132840                 :   char **pzErrMsg,
  132841                 :   const sqlite3_api_routines *pApi
  132842                 : ){
  132843                 :   SQLITE_EXTENSION_INIT2(pApi)
  132844                 :   return sqlite3RtreeInit(db);
  132845                 : }
  132846                 : #endif
  132847                 : 
  132848                 : #endif
  132849                 : 
  132850                 : /************** End of rtree.c ***********************************************/
  132851                 : /************** Begin file icu.c *********************************************/
  132852                 : /*
  132853                 : ** 2007 May 6
  132854                 : **
  132855                 : ** The author disclaims copyright to this source code.  In place of
  132856                 : ** a legal notice, here is a blessing:
  132857                 : **
  132858                 : **    May you do good and not evil.
  132859                 : **    May you find forgiveness for yourself and forgive others.
  132860                 : **    May you share freely, never taking more than you give.
  132861                 : **
  132862                 : *************************************************************************
  132863                 : ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
  132864                 : **
  132865                 : ** This file implements an integration between the ICU library 
  132866                 : ** ("International Components for Unicode", an open-source library 
  132867                 : ** for handling unicode data) and SQLite. The integration uses 
  132868                 : ** ICU to provide the following to SQLite:
  132869                 : **
  132870                 : **   * An implementation of the SQL regexp() function (and hence REGEXP
  132871                 : **     operator) using the ICU uregex_XX() APIs.
  132872                 : **
  132873                 : **   * Implementations of the SQL scalar upper() and lower() functions
  132874                 : **     for case mapping.
  132875                 : **
  132876                 : **   * Integration of ICU and SQLite collation seqences.
  132877                 : **
  132878                 : **   * An implementation of the LIKE operator that uses ICU to 
  132879                 : **     provide case-independent matching.
  132880                 : */
  132881                 : 
  132882                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
  132883                 : 
  132884                 : /* Include ICU headers */
  132885                 : #include <unicode/utypes.h>
  132886                 : #include <unicode/uregex.h>
  132887                 : #include <unicode/ustring.h>
  132888                 : #include <unicode/ucol.h>
  132889                 : 
  132890                 : /* #include <assert.h> */
  132891                 : 
  132892                 : #ifndef SQLITE_CORE
  132893                 :   SQLITE_EXTENSION_INIT1
  132894                 : #else
  132895                 : #endif
  132896                 : 
  132897                 : /*
  132898                 : ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
  132899                 : ** operator.
  132900                 : */
  132901                 : #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
  132902                 : # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
  132903                 : #endif
  132904                 : 
  132905                 : /*
  132906                 : ** Version of sqlite3_free() that is always a function, never a macro.
  132907                 : */
  132908                 : static void xFree(void *p){
  132909                 :   sqlite3_free(p);
  132910                 : }
  132911                 : 
  132912                 : /*
  132913                 : ** Compare two UTF-8 strings for equality where the first string is
  132914                 : ** a "LIKE" expression. Return true (1) if they are the same and 
  132915                 : ** false (0) if they are different.
  132916                 : */
  132917                 : static int icuLikeCompare(
  132918                 :   const uint8_t *zPattern,   /* LIKE pattern */
  132919                 :   const uint8_t *zString,    /* The UTF-8 string to compare against */
  132920                 :   const UChar32 uEsc         /* The escape character */
  132921                 : ){
  132922                 :   static const int MATCH_ONE = (UChar32)'_';
  132923                 :   static const int MATCH_ALL = (UChar32)'%';
  132924                 : 
  132925                 :   int iPattern = 0;       /* Current byte index in zPattern */
  132926                 :   int iString = 0;        /* Current byte index in zString */
  132927                 : 
  132928                 :   int prevEscape = 0;     /* True if the previous character was uEsc */
  132929                 : 
  132930                 :   while( zPattern[iPattern]!=0 ){
  132931                 : 
  132932                 :     /* Read (and consume) the next character from the input pattern. */
  132933                 :     UChar32 uPattern;
  132934                 :     U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
  132935                 :     assert(uPattern!=0);
  132936                 : 
  132937                 :     /* There are now 4 possibilities:
  132938                 :     **
  132939                 :     **     1. uPattern is an unescaped match-all character "%",
  132940                 :     **     2. uPattern is an unescaped match-one character "_",
  132941                 :     **     3. uPattern is an unescaped escape character, or
  132942                 :     **     4. uPattern is to be handled as an ordinary character
  132943                 :     */
  132944                 :     if( !prevEscape && uPattern==MATCH_ALL ){
  132945                 :       /* Case 1. */
  132946                 :       uint8_t c;
  132947                 : 
  132948                 :       /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
  132949                 :       ** MATCH_ALL. For each MATCH_ONE, skip one character in the 
  132950                 :       ** test string.
  132951                 :       */
  132952                 :       while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
  132953                 :         if( c==MATCH_ONE ){
  132954                 :           if( zString[iString]==0 ) return 0;
  132955                 :           U8_FWD_1_UNSAFE(zString, iString);
  132956                 :         }
  132957                 :         iPattern++;
  132958                 :       }
  132959                 : 
  132960                 :       if( zPattern[iPattern]==0 ) return 1;
  132961                 : 
  132962                 :       while( zString[iString] ){
  132963                 :         if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
  132964                 :           return 1;
  132965                 :         }
  132966                 :         U8_FWD_1_UNSAFE(zString, iString);
  132967                 :       }
  132968                 :       return 0;
  132969                 : 
  132970                 :     }else if( !prevEscape && uPattern==MATCH_ONE ){
  132971                 :       /* Case 2. */
  132972                 :       if( zString[iString]==0 ) return 0;
  132973                 :       U8_FWD_1_UNSAFE(zString, iString);
  132974                 : 
  132975                 :     }else if( !prevEscape && uPattern==uEsc){
  132976                 :       /* Case 3. */
  132977                 :       prevEscape = 1;
  132978                 : 
  132979                 :     }else{
  132980                 :       /* Case 4. */
  132981                 :       UChar32 uString;
  132982                 :       U8_NEXT_UNSAFE(zString, iString, uString);
  132983                 :       uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
  132984                 :       uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
  132985                 :       if( uString!=uPattern ){
  132986                 :         return 0;
  132987                 :       }
  132988                 :       prevEscape = 0;
  132989                 :     }
  132990                 :   }
  132991                 : 
  132992                 :   return zString[iString]==0;
  132993                 : }
  132994                 : 
  132995                 : /*
  132996                 : ** Implementation of the like() SQL function.  This function implements
  132997                 : ** the build-in LIKE operator.  The first argument to the function is the
  132998                 : ** pattern and the second argument is the string.  So, the SQL statements:
  132999                 : **
  133000                 : **       A LIKE B
  133001                 : **
  133002                 : ** is implemented as like(B, A). If there is an escape character E, 
  133003                 : **
  133004                 : **       A LIKE B ESCAPE E
  133005                 : **
  133006                 : ** is mapped to like(B, A, E).
  133007                 : */
  133008                 : static void icuLikeFunc(
  133009                 :   sqlite3_context *context, 
  133010                 :   int argc, 
  133011                 :   sqlite3_value **argv
  133012                 : ){
  133013                 :   const unsigned char *zA = sqlite3_value_text(argv[0]);
  133014                 :   const unsigned char *zB = sqlite3_value_text(argv[1]);
  133015                 :   UChar32 uEsc = 0;
  133016                 : 
  133017                 :   /* Limit the length of the LIKE or GLOB pattern to avoid problems
  133018                 :   ** of deep recursion and N*N behavior in patternCompare().
  133019                 :   */
  133020                 :   if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
  133021                 :     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
  133022                 :     return;
  133023                 :   }
  133024                 : 
  133025                 : 
  133026                 :   if( argc==3 ){
  133027                 :     /* The escape character string must consist of a single UTF-8 character.
  133028                 :     ** Otherwise, return an error.
  133029                 :     */
  133030                 :     int nE= sqlite3_value_bytes(argv[2]);
  133031                 :     const unsigned char *zE = sqlite3_value_text(argv[2]);
  133032                 :     int i = 0;
  133033                 :     if( zE==0 ) return;
  133034                 :     U8_NEXT(zE, i, nE, uEsc);
  133035                 :     if( i!=nE){
  133036                 :       sqlite3_result_error(context, 
  133037                 :           "ESCAPE expression must be a single character", -1);
  133038                 :       return;
  133039                 :     }
  133040                 :   }
  133041                 : 
  133042                 :   if( zA && zB ){
  133043                 :     sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
  133044                 :   }
  133045                 : }
  133046                 : 
  133047                 : /*
  133048                 : ** This function is called when an ICU function called from within
  133049                 : ** the implementation of an SQL scalar function returns an error.
  133050                 : **
  133051                 : ** The scalar function context passed as the first argument is 
  133052                 : ** loaded with an error message based on the following two args.
  133053                 : */
  133054                 : static void icuFunctionError(
  133055                 :   sqlite3_context *pCtx,       /* SQLite scalar function context */
  133056                 :   const char *zName,           /* Name of ICU function that failed */
  133057                 :   UErrorCode e                 /* Error code returned by ICU function */
  133058                 : ){
  133059                 :   char zBuf[128];
  133060                 :   sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
  133061                 :   zBuf[127] = '\0';
  133062                 :   sqlite3_result_error(pCtx, zBuf, -1);
  133063                 : }
  133064                 : 
  133065                 : /*
  133066                 : ** Function to delete compiled regexp objects. Registered as
  133067                 : ** a destructor function with sqlite3_set_auxdata().
  133068                 : */
  133069                 : static void icuRegexpDelete(void *p){
  133070                 :   URegularExpression *pExpr = (URegularExpression *)p;
  133071                 :   uregex_close(pExpr);
  133072                 : }
  133073                 : 
  133074                 : /*
  133075                 : ** Implementation of SQLite REGEXP operator. This scalar function takes
  133076                 : ** two arguments. The first is a regular expression pattern to compile
  133077                 : ** the second is a string to match against that pattern. If either 
  133078                 : ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
  133079                 : ** is 1 if the string matches the pattern, or 0 otherwise.
  133080                 : **
  133081                 : ** SQLite maps the regexp() function to the regexp() operator such
  133082                 : ** that the following two are equivalent:
  133083                 : **
  133084                 : **     zString REGEXP zPattern
  133085                 : **     regexp(zPattern, zString)
  133086                 : **
  133087                 : ** Uses the following ICU regexp APIs:
  133088                 : **
  133089                 : **     uregex_open()
  133090                 : **     uregex_matches()
  133091                 : **     uregex_close()
  133092                 : */
  133093                 : static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
  133094                 :   UErrorCode status = U_ZERO_ERROR;
  133095                 :   URegularExpression *pExpr;
  133096                 :   UBool res;
  133097                 :   const UChar *zString = sqlite3_value_text16(apArg[1]);
  133098                 : 
  133099                 :   (void)nArg;  /* Unused parameter */
  133100                 : 
  133101                 :   /* If the left hand side of the regexp operator is NULL, 
  133102                 :   ** then the result is also NULL. 
  133103                 :   */
  133104                 :   if( !zString ){
  133105                 :     return;
  133106                 :   }
  133107                 : 
  133108                 :   pExpr = sqlite3_get_auxdata(p, 0);
  133109                 :   if( !pExpr ){
  133110                 :     const UChar *zPattern = sqlite3_value_text16(apArg[0]);
  133111                 :     if( !zPattern ){
  133112                 :       return;
  133113                 :     }
  133114                 :     pExpr = uregex_open(zPattern, -1, 0, 0, &status);
  133115                 : 
  133116                 :     if( U_SUCCESS(status) ){
  133117                 :       sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
  133118                 :     }else{
  133119                 :       assert(!pExpr);
  133120                 :       icuFunctionError(p, "uregex_open", status);
  133121                 :       return;
  133122                 :     }
  133123                 :   }
  133124                 : 
  133125                 :   /* Configure the text that the regular expression operates on. */
  133126                 :   uregex_setText(pExpr, zString, -1, &status);
  133127                 :   if( !U_SUCCESS(status) ){
  133128                 :     icuFunctionError(p, "uregex_setText", status);
  133129                 :     return;
  133130                 :   }
  133131                 : 
  133132                 :   /* Attempt the match */
  133133                 :   res = uregex_matches(pExpr, 0, &status);
  133134                 :   if( !U_SUCCESS(status) ){
  133135                 :     icuFunctionError(p, "uregex_matches", status);
  133136                 :     return;
  133137                 :   }
  133138                 : 
  133139                 :   /* Set the text that the regular expression operates on to a NULL
  133140                 :   ** pointer. This is not really necessary, but it is tidier than 
  133141                 :   ** leaving the regular expression object configured with an invalid
  133142                 :   ** pointer after this function returns.
  133143                 :   */
  133144                 :   uregex_setText(pExpr, 0, 0, &status);
  133145                 : 
  133146                 :   /* Return 1 or 0. */
  133147                 :   sqlite3_result_int(p, res ? 1 : 0);
  133148                 : }
  133149                 : 
  133150                 : /*
  133151                 : ** Implementations of scalar functions for case mapping - upper() and 
  133152                 : ** lower(). Function upper() converts its input to upper-case (ABC).
  133153                 : ** Function lower() converts to lower-case (abc).
  133154                 : **
  133155                 : ** ICU provides two types of case mapping, "general" case mapping and
  133156                 : ** "language specific". Refer to ICU documentation for the differences
  133157                 : ** between the two.
  133158                 : **
  133159                 : ** To utilise "general" case mapping, the upper() or lower() scalar 
  133160                 : ** functions are invoked with one argument:
  133161                 : **
  133162                 : **     upper('ABC') -> 'abc'
  133163                 : **     lower('abc') -> 'ABC'
  133164                 : **
  133165                 : ** To access ICU "language specific" case mapping, upper() or lower()
  133166                 : ** should be invoked with two arguments. The second argument is the name
  133167                 : ** of the locale to use. Passing an empty string ("") or SQL NULL value
  133168                 : ** as the second argument is the same as invoking the 1 argument version
  133169                 : ** of upper() or lower().
  133170                 : **
  133171                 : **     lower('I', 'en_us') -> 'i'
  133172                 : **     lower('I', 'tr_tr') -> 'ı' (small dotless i)
  133173                 : **
  133174                 : ** http://www.icu-project.org/userguide/posix.html#case_mappings
  133175                 : */
  133176                 : static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
  133177                 :   const UChar *zInput;
  133178                 :   UChar *zOutput;
  133179                 :   int nInput;
  133180                 :   int nOutput;
  133181                 : 
  133182                 :   UErrorCode status = U_ZERO_ERROR;
  133183                 :   const char *zLocale = 0;
  133184                 : 
  133185                 :   assert(nArg==1 || nArg==2);
  133186                 :   if( nArg==2 ){
  133187                 :     zLocale = (const char *)sqlite3_value_text(apArg[1]);
  133188                 :   }
  133189                 : 
  133190                 :   zInput = sqlite3_value_text16(apArg[0]);
  133191                 :   if( !zInput ){
  133192                 :     return;
  133193                 :   }
  133194                 :   nInput = sqlite3_value_bytes16(apArg[0]);
  133195                 : 
  133196                 :   nOutput = nInput * 2 + 2;
  133197                 :   zOutput = sqlite3_malloc(nOutput);
  133198                 :   if( !zOutput ){
  133199                 :     return;
  133200                 :   }
  133201                 : 
  133202                 :   if( sqlite3_user_data(p) ){
  133203                 :     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
  133204                 :   }else{
  133205                 :     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
  133206                 :   }
  133207                 : 
  133208                 :   if( !U_SUCCESS(status) ){
  133209                 :     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
  133210                 :     return;
  133211                 :   }
  133212                 : 
  133213                 :   sqlite3_result_text16(p, zOutput, -1, xFree);
  133214                 : }
  133215                 : 
  133216                 : /*
  133217                 : ** Collation sequence destructor function. The pCtx argument points to
  133218                 : ** a UCollator structure previously allocated using ucol_open().
  133219                 : */
  133220                 : static void icuCollationDel(void *pCtx){
  133221                 :   UCollator *p = (UCollator *)pCtx;
  133222                 :   ucol_close(p);
  133223                 : }
  133224                 : 
  133225                 : /*
  133226                 : ** Collation sequence comparison function. The pCtx argument points to
  133227                 : ** a UCollator structure previously allocated using ucol_open().
  133228                 : */
  133229                 : static int icuCollationColl(
  133230                 :   void *pCtx,
  133231                 :   int nLeft,
  133232                 :   const void *zLeft,
  133233                 :   int nRight,
  133234                 :   const void *zRight
  133235                 : ){
  133236                 :   UCollationResult res;
  133237                 :   UCollator *p = (UCollator *)pCtx;
  133238                 :   res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
  133239                 :   switch( res ){
  133240                 :     case UCOL_LESS:    return -1;
  133241                 :     case UCOL_GREATER: return +1;
  133242                 :     case UCOL_EQUAL:   return 0;
  133243                 :   }
  133244                 :   assert(!"Unexpected return value from ucol_strcoll()");
  133245                 :   return 0;
  133246                 : }
  133247                 : 
  133248                 : /*
  133249                 : ** Implementation of the scalar function icu_load_collation().
  133250                 : **
  133251                 : ** This scalar function is used to add ICU collation based collation 
  133252                 : ** types to an SQLite database connection. It is intended to be called
  133253                 : ** as follows:
  133254                 : **
  133255                 : **     SELECT icu_load_collation(<locale>, <collation-name>);
  133256                 : **
  133257                 : ** Where <locale> is a string containing an ICU locale identifier (i.e.
  133258                 : ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
  133259                 : ** collation sequence to create.
  133260                 : */
  133261                 : static void icuLoadCollation(
  133262                 :   sqlite3_context *p, 
  133263                 :   int nArg, 
  133264                 :   sqlite3_value **apArg
  133265                 : ){
  133266                 :   sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
  133267                 :   UErrorCode status = U_ZERO_ERROR;
  133268                 :   const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
  133269                 :   const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
  133270                 :   UCollator *pUCollator;    /* ICU library collation object */
  133271                 :   int rc;                   /* Return code from sqlite3_create_collation_x() */
  133272                 : 
  133273                 :   assert(nArg==2);
  133274                 :   zLocale = (const char *)sqlite3_value_text(apArg[0]);
  133275                 :   zName = (const char *)sqlite3_value_text(apArg[1]);
  133276                 : 
  133277                 :   if( !zLocale || !zName ){
  133278                 :     return;
  133279                 :   }
  133280                 : 
  133281                 :   pUCollator = ucol_open(zLocale, &status);
  133282                 :   if( !U_SUCCESS(status) ){
  133283                 :     icuFunctionError(p, "ucol_open", status);
  133284                 :     return;
  133285                 :   }
  133286                 :   assert(p);
  133287                 : 
  133288                 :   rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, 
  133289                 :       icuCollationColl, icuCollationDel
  133290                 :   );
  133291                 :   if( rc!=SQLITE_OK ){
  133292                 :     ucol_close(pUCollator);
  133293                 :     sqlite3_result_error(p, "Error registering collation function", -1);
  133294                 :   }
  133295                 : }
  133296                 : 
  133297                 : /*
  133298                 : ** Register the ICU extension functions with database db.
  133299                 : */
  133300                 : SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
  133301                 :   struct IcuScalar {
  133302                 :     const char *zName;                        /* Function name */
  133303                 :     int nArg;                                 /* Number of arguments */
  133304                 :     int enc;                                  /* Optimal text encoding */
  133305                 :     void *pContext;                           /* sqlite3_user_data() context */
  133306                 :     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  133307                 :   } scalars[] = {
  133308                 :     {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
  133309                 : 
  133310                 :     {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
  133311                 :     {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
  133312                 :     {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
  133313                 :     {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
  133314                 : 
  133315                 :     {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
  133316                 :     {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
  133317                 :     {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
  133318                 :     {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
  133319                 : 
  133320                 :     {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
  133321                 :     {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
  133322                 : 
  133323                 :     {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
  133324                 :   };
  133325                 : 
  133326                 :   int rc = SQLITE_OK;
  133327                 :   int i;
  133328                 : 
  133329                 :   for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
  133330                 :     struct IcuScalar *p = &scalars[i];
  133331                 :     rc = sqlite3_create_function(
  133332                 :         db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
  133333                 :     );
  133334                 :   }
  133335                 : 
  133336                 :   return rc;
  133337                 : }
  133338                 : 
  133339                 : #if !SQLITE_CORE
  133340                 : SQLITE_API int sqlite3_extension_init(
  133341                 :   sqlite3 *db, 
  133342                 :   char **pzErrMsg,
  133343                 :   const sqlite3_api_routines *pApi
  133344                 : ){
  133345                 :   SQLITE_EXTENSION_INIT2(pApi)
  133346                 :   return sqlite3IcuInit(db);
  133347                 : }
  133348                 : #endif
  133349                 : 
  133350                 : #endif
  133351                 : 
  133352                 : /************** End of icu.c *************************************************/
  133353                 : /************** Begin file fts3_icu.c ****************************************/
  133354                 : /*
  133355                 : ** 2007 June 22
  133356                 : **
  133357                 : ** The author disclaims copyright to this source code.  In place of
  133358                 : ** a legal notice, here is a blessing:
  133359                 : **
  133360                 : **    May you do good and not evil.
  133361                 : **    May you find forgiveness for yourself and forgive others.
  133362                 : **    May you share freely, never taking more than you give.
  133363                 : **
  133364                 : *************************************************************************
  133365                 : ** This file implements a tokenizer for fts3 based on the ICU library.
  133366                 : */
  133367                 : #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  133368                 : #ifdef SQLITE_ENABLE_ICU
  133369                 : 
  133370                 : /* #include <assert.h> */
  133371                 : /* #include <string.h> */
  133372                 : 
  133373                 : #include <unicode/ubrk.h>
  133374                 : /* #include <unicode/ucol.h> */
  133375                 : /* #include <unicode/ustring.h> */
  133376                 : #include <unicode/utf16.h>
  133377                 : 
  133378                 : typedef struct IcuTokenizer IcuTokenizer;
  133379                 : typedef struct IcuCursor IcuCursor;
  133380                 : 
  133381                 : struct IcuTokenizer {
  133382                 :   sqlite3_tokenizer base;
  133383                 :   char *zLocale;
  133384                 : };
  133385                 : 
  133386                 : struct IcuCursor {
  133387                 :   sqlite3_tokenizer_cursor base;
  133388                 : 
  133389                 :   UBreakIterator *pIter;      /* ICU break-iterator object */
  133390                 :   int nChar;                  /* Number of UChar elements in pInput */
  133391                 :   UChar *aChar;               /* Copy of input using utf-16 encoding */
  133392                 :   int *aOffset;               /* Offsets of each character in utf-8 input */
  133393                 : 
  133394                 :   int nBuffer;
  133395                 :   char *zBuffer;
  133396                 : 
  133397                 :   int iToken;
  133398                 : };
  133399                 : 
  133400                 : /*
  133401                 : ** Create a new tokenizer instance.
  133402                 : */
  133403                 : static int icuCreate(
  133404                 :   int argc,                            /* Number of entries in argv[] */
  133405                 :   const char * const *argv,            /* Tokenizer creation arguments */
  133406                 :   sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
  133407                 : ){
  133408                 :   IcuTokenizer *p;
  133409                 :   int n = 0;
  133410                 : 
  133411                 :   if( argc>0 ){
  133412                 :     n = strlen(argv[0])+1;
  133413                 :   }
  133414                 :   p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
  133415                 :   if( !p ){
  133416                 :     return SQLITE_NOMEM;
  133417                 :   }
  133418                 :   memset(p, 0, sizeof(IcuTokenizer));
  133419                 : 
  133420                 :   if( n ){
  133421                 :     p->zLocale = (char *)&p[1];
  133422                 :     memcpy(p->zLocale, argv[0], n);
  133423                 :   }
  133424                 : 
  133425                 :   *ppTokenizer = (sqlite3_tokenizer *)p;
  133426                 : 
  133427                 :   return SQLITE_OK;
  133428                 : }
  133429                 : 
  133430                 : /*
  133431                 : ** Destroy a tokenizer
  133432                 : */
  133433                 : static int icuDestroy(sqlite3_tokenizer *pTokenizer){
  133434                 :   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
  133435                 :   sqlite3_free(p);
  133436                 :   return SQLITE_OK;
  133437                 : }
  133438                 : 
  133439                 : /*
  133440                 : ** Prepare to begin tokenizing a particular string.  The input
  133441                 : ** string to be tokenized is pInput[0..nBytes-1].  A cursor
  133442                 : ** used to incrementally tokenize this string is returned in 
  133443                 : ** *ppCursor.
  133444                 : */
  133445                 : static int icuOpen(
  133446                 :   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
  133447                 :   const char *zInput,                    /* Input string */
  133448                 :   int nInput,                            /* Length of zInput in bytes */
  133449                 :   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
  133450                 : ){
  133451                 :   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
  133452                 :   IcuCursor *pCsr;
  133453                 : 
  133454                 :   const int32_t opt = U_FOLD_CASE_DEFAULT;
  133455                 :   UErrorCode status = U_ZERO_ERROR;
  133456                 :   int nChar;
  133457                 : 
  133458                 :   UChar32 c;
  133459                 :   int iInput = 0;
  133460                 :   int iOut = 0;
  133461                 : 
  133462                 :   *ppCursor = 0;
  133463                 : 
  133464                 :   if( nInput<0 ){
  133465                 :     nInput = strlen(zInput);
  133466                 :   }
  133467                 :   nChar = nInput+1;
  133468                 :   pCsr = (IcuCursor *)sqlite3_malloc(
  133469                 :       sizeof(IcuCursor) +                /* IcuCursor */
  133470                 :       nChar * sizeof(UChar) +            /* IcuCursor.aChar[] */
  133471                 :       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
  133472                 :   );
  133473                 :   if( !pCsr ){
  133474                 :     return SQLITE_NOMEM;
  133475                 :   }
  133476                 :   memset(pCsr, 0, sizeof(IcuCursor));
  133477                 :   pCsr->aChar = (UChar *)&pCsr[1];
  133478                 :   pCsr->aOffset = (int *)&pCsr->aChar[nChar];
  133479                 : 
  133480                 :   pCsr->aOffset[iOut] = iInput;
  133481                 :   U8_NEXT(zInput, iInput, nInput, c); 
  133482                 :   while( c>0 ){
  133483                 :     int isError = 0;
  133484                 :     c = u_foldCase(c, opt);
  133485                 :     U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
  133486                 :     if( isError ){
  133487                 :       sqlite3_free(pCsr);
  133488                 :       return SQLITE_ERROR;
  133489                 :     }
  133490                 :     pCsr->aOffset[iOut] = iInput;
  133491                 : 
  133492                 :     if( iInput<nInput ){
  133493                 :       U8_NEXT(zInput, iInput, nInput, c);
  133494                 :     }else{
  133495                 :       c = 0;
  133496                 :     }
  133497                 :   }
  133498                 : 
  133499                 :   pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
  133500                 :   if( !U_SUCCESS(status) ){
  133501                 :     sqlite3_free(pCsr);
  133502                 :     return SQLITE_ERROR;
  133503                 :   }
  133504                 :   pCsr->nChar = iOut;
  133505                 : 
  133506                 :   ubrk_first(pCsr->pIter);
  133507                 :   *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
  133508                 :   return SQLITE_OK;
  133509                 : }
  133510                 : 
  133511                 : /*
  133512                 : ** Close a tokenization cursor previously opened by a call to icuOpen().
  133513                 : */
  133514                 : static int icuClose(sqlite3_tokenizer_cursor *pCursor){
  133515                 :   IcuCursor *pCsr = (IcuCursor *)pCursor;
  133516                 :   ubrk_close(pCsr->pIter);
  133517                 :   sqlite3_free(pCsr->zBuffer);
  133518                 :   sqlite3_free(pCsr);
  133519                 :   return SQLITE_OK;
  133520                 : }
  133521                 : 
  133522                 : /*
  133523                 : ** Extract the next token from a tokenization cursor.
  133524                 : */
  133525                 : static int icuNext(
  133526                 :   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
  133527                 :   const char **ppToken,               /* OUT: *ppToken is the token text */
  133528                 :   int *pnBytes,                       /* OUT: Number of bytes in token */
  133529                 :   int *piStartOffset,                 /* OUT: Starting offset of token */
  133530                 :   int *piEndOffset,                   /* OUT: Ending offset of token */
  133531                 :   int *piPosition                     /* OUT: Position integer of token */
  133532                 : ){
  133533                 :   IcuCursor *pCsr = (IcuCursor *)pCursor;
  133534                 : 
  133535                 :   int iStart = 0;
  133536                 :   int iEnd = 0;
  133537                 :   int nByte = 0;
  133538                 : 
  133539                 :   while( iStart==iEnd ){
  133540                 :     UChar32 c;
  133541                 : 
  133542                 :     iStart = ubrk_current(pCsr->pIter);
  133543                 :     iEnd = ubrk_next(pCsr->pIter);
  133544                 :     if( iEnd==UBRK_DONE ){
  133545                 :       return SQLITE_DONE;
  133546                 :     }
  133547                 : 
  133548                 :     while( iStart<iEnd ){
  133549                 :       int iWhite = iStart;
  133550                 :       U8_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
  133551                 :       if( u_isspace(c) ){
  133552                 :         iStart = iWhite;
  133553                 :       }else{
  133554                 :         break;
  133555                 :       }
  133556                 :     }
  133557                 :     assert(iStart<=iEnd);
  133558                 :   }
  133559                 : 
  133560                 :   do {
  133561                 :     UErrorCode status = U_ZERO_ERROR;
  133562                 :     if( nByte ){
  133563                 :       char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
  133564                 :       if( !zNew ){
  133565                 :         return SQLITE_NOMEM;
  133566                 :       }
  133567                 :       pCsr->zBuffer = zNew;
  133568                 :       pCsr->nBuffer = nByte;
  133569                 :     }
  133570                 : 
  133571                 :     u_strToUTF8(
  133572                 :         pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
  133573                 :         &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
  133574                 :         &status                                  /* Output success/failure */
  133575                 :     );
  133576                 :   } while( nByte>pCsr->nBuffer );
  133577                 : 
  133578                 :   *ppToken = pCsr->zBuffer;
  133579                 :   *pnBytes = nByte;
  133580                 :   *piStartOffset = pCsr->aOffset[iStart];
  133581                 :   *piEndOffset = pCsr->aOffset[iEnd];
  133582                 :   *piPosition = pCsr->iToken++;
  133583                 : 
  133584                 :   return SQLITE_OK;
  133585                 : }
  133586                 : 
  133587                 : /*
  133588                 : ** The set of routines that implement the simple tokenizer
  133589                 : */
  133590                 : static const sqlite3_tokenizer_module icuTokenizerModule = {
  133591                 :   0,                           /* iVersion */
  133592                 :   icuCreate,                   /* xCreate  */
  133593                 :   icuDestroy,                  /* xCreate  */
  133594                 :   icuOpen,                     /* xOpen    */
  133595                 :   icuClose,                    /* xClose   */
  133596                 :   icuNext,                     /* xNext    */
  133597                 : };
  133598                 : 
  133599                 : /*
  133600                 : ** Set *ppModule to point at the implementation of the ICU tokenizer.
  133601                 : */
  133602                 : SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
  133603                 :   sqlite3_tokenizer_module const**ppModule
  133604                 : ){
  133605                 :   *ppModule = &icuTokenizerModule;
  133606                 : }
  133607                 : 
  133608                 : #endif /* defined(SQLITE_ENABLE_ICU) */
  133609                 : #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
  133610                 : 
  133611                 : /************** End of fts3_icu.c ********************************************/

Generated by: LCOV version 1.7